📄 zinternal.c
字号:
/* copy in the message body */ (void) memcpy(qptr->msg+part, notice->z_message, notice->z_message_len); /* Search for a hole that overlaps with the current fragment */ while (hole) { if (part <= hole->last && last >= hole->first) break; lasthole = hole; hole = hole->next; } /* If we found one, delete it and reconstruct a new hole */ if (hole) { oldfirst = hole->first; oldlast = hole->last; if (lasthole) lasthole->next = hole->next; else qptr->holelist = hole->next; free((char *)hole); /* * Now create a new hole that is the original hole without the * current fragment. */ if (part > oldfirst) { /* Search for the end of the hole list */ hole = qptr->holelist; lasthole = (struct _Z_Hole *) 0; while (hole) { lasthole = hole; hole = hole->next; } if (lasthole) { if (!(lasthole->next = (struct _Z_Hole *) malloc(sizeof(struct _Z_InputQ)))) return (ENOMEM); hole = lasthole->next; } else { if (!(qptr->holelist = (struct _Z_Hole *) malloc(sizeof(struct _Z_InputQ)))) return (ENOMEM); hole = qptr->holelist; } hole->next = NULL; hole->first = oldfirst; hole->last = part-1; } if (last < oldlast) { /* Search for the end of the hole list */ hole = qptr->holelist; lasthole = (struct _Z_Hole *) 0; while (hole) { lasthole = hole; hole = hole->next; } if (lasthole) { if (!(lasthole->next = (struct _Z_Hole *) malloc(sizeof(struct _Z_InputQ)))) return (ENOMEM); hole = lasthole->next; } else { if (!(qptr->holelist = (struct _Z_Hole *) malloc(sizeof(struct _Z_InputQ)))) return (ENOMEM); hole = qptr->holelist; } hole->next = (struct _Z_Hole *) 0; hole->first = last+1; hole->last = oldlast; } } if (!qptr->holelist) { if (!qptr->complete) __Q_CompleteLength++; qptr->complete = 1; qptr->timep = 0; /* don't time out anymore */ qptr->packet_len = qptr->header_len+qptr->msg_len; if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len))) return (ENOMEM); (void) memcpy(qptr->packet, qptr->header, qptr->header_len); (void) memcpy(qptr->packet+qptr->header_len, qptr->msg, qptr->msg_len); } return (ZERR_NONE);}Code_t Z_FormatHeader(notice, buffer, buffer_len, len, cert_routine) ZNotice_t *notice; char *buffer; int buffer_len; int *len; Z_AuthProc cert_routine;{ Code_t retval; static char version[BUFSIZ]; /* default init should be all \0 */ struct sockaddr_in name; socklen_t namelen = sizeof(name); if (!notice->z_sender) notice->z_sender = ZGetSender(); if (notice->z_port == 0) { if (ZGetFD() < 0) { retval = ZOpenPort((unsigned short *)0); if (retval != ZERR_NONE) return (retval); } retval = getsockname(ZGetFD(), (struct sockaddr *) &name, &namelen); if (retval != 0) return (retval); notice->z_port = name.sin_port; } notice->z_multinotice = ""; (void) gettimeofday(¬ice->z_uid.tv, (struct timezone *)0); notice->z_uid.tv.tv_sec = htonl((unsigned long) notice->z_uid.tv.tv_sec); notice->z_uid.tv.tv_usec = htonl((unsigned long) notice->z_uid.tv.tv_usec); (void) memcpy(¬ice->z_uid.zuid_addr, &__My_addr, sizeof(__My_addr)); notice->z_multiuid = notice->z_uid; if (!version[0]) (void) sprintf(version, "%s%d.%d", ZVERSIONHDR, ZVERSIONMAJOR, ZVERSIONMINOR); notice->z_version = version; return Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine);}Code_t Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine) ZNotice_t *notice; char *buffer; int buffer_len; int *len; Z_AuthProc cert_routine;{ if (!cert_routine) { notice->z_auth = 0; notice->z_authent_len = 0; notice->z_ascii_authent = ""; notice->z_checksum = 0; return (Z_FormatRawHeader(notice, buffer, buffer_len, len, NULL, NULL)); } return ((*cert_routine)(notice, buffer, buffer_len, len));} Code_t Z_FormatRawHeader(notice, buffer, buffer_len, len, cstart, cend) ZNotice_t *notice; char *buffer; int buffer_len; int *len; char **cstart, **cend;{ char newrecip[BUFSIZ]; char *ptr, *end; int i; if (!notice->z_class) notice->z_class = ""; if (!notice->z_class_inst) notice->z_class_inst = ""; if (!notice->z_opcode) notice->z_opcode = ""; if (!notice->z_recipient) notice->z_recipient = ""; if (!notice->z_default_format) notice->z_default_format = ""; ptr = buffer; end = buffer+buffer_len; if (buffer_len < strlen(notice->z_version)+1) return (ZERR_HEADERLEN); (void) strcpy(ptr, notice->z_version); ptr += strlen(ptr)+1; if (ZMakeAscii32(ptr, end-ptr, Z_NUMFIELDS + notice->z_num_other_fields) == ZERR_FIELDLEN) return (ZERR_HEADERLEN); ptr += strlen(ptr)+1; if (ZMakeAscii32(ptr, end-ptr, notice->z_kind) == ZERR_FIELDLEN) return (ZERR_HEADERLEN); ptr += strlen(ptr)+1; if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_uid, sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN) return (ZERR_HEADERLEN); ptr += strlen(ptr)+1; if (ZMakeAscii16(ptr, end-ptr, ntohs(notice->z_port)) == ZERR_FIELDLEN) return (ZERR_HEADERLEN); ptr += strlen(ptr)+1; if (ZMakeAscii32(ptr, end-ptr, notice->z_auth) == ZERR_FIELDLEN) return (ZERR_HEADERLEN); ptr += strlen(ptr)+1; if (ZMakeAscii32(ptr, end-ptr, notice->z_authent_len) == ZERR_FIELDLEN) return (ZERR_HEADERLEN); ptr += strlen(ptr)+1; if (Z_AddField(&ptr, notice->z_ascii_authent, end)) return (ZERR_HEADERLEN); if (Z_AddField(&ptr, notice->z_class, end)) return (ZERR_HEADERLEN); if (Z_AddField(&ptr, notice->z_class_inst, end)) return (ZERR_HEADERLEN); if (Z_AddField(&ptr, notice->z_opcode, end)) return (ZERR_HEADERLEN); if (Z_AddField(&ptr, notice->z_sender, end)) return (ZERR_HEADERLEN); if (strchr(notice->z_recipient, '@') || !*notice->z_recipient) { if (Z_AddField(&ptr, notice->z_recipient, end)) return (ZERR_HEADERLEN); } else { if (strlen(notice->z_recipient) + strlen(__Zephyr_realm) + 2 > sizeof(newrecip)) return (ZERR_HEADERLEN); (void) sprintf(newrecip, "%s@%s", notice->z_recipient, __Zephyr_realm); if (Z_AddField(&ptr, newrecip, end)) return (ZERR_HEADERLEN); } if (Z_AddField(&ptr, notice->z_default_format, end)) return (ZERR_HEADERLEN); /* copy back the end pointer location for crypto checksum */ if (cstart) *cstart = ptr; if (ZMakeAscii32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN) return (ZERR_HEADERLEN); ptr += strlen(ptr)+1; if (cend) *cend = ptr; if (Z_AddField(&ptr, notice->z_multinotice, end)) return (ZERR_HEADERLEN); if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_multiuid, sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN) return (ZERR_HEADERLEN); ptr += strlen(ptr)+1; for (i=0;i<notice->z_num_other_fields;i++) if (Z_AddField(&ptr, notice->z_other_fields[i], end)) return (ZERR_HEADERLEN); *len = ptr-buffer; return (ZERR_NONE);}static intZ_AddField(char **ptr, const char *field, char *end){ register int len; len = field ? strlen (field) + 1 : 1; if (*ptr+len > end) return 1; if (field) (void) strcpy(*ptr, field); else **ptr = '\0'; *ptr += len; return 0;}struct _Z_InputQ *Z_GetFirstComplete(){ struct _Z_InputQ *qptr; qptr = __Q_Head; while (qptr) { if (qptr->complete) return (qptr); qptr = qptr->next; } return ((struct _Z_InputQ *)0);}struct _Z_InputQ *Z_GetNextComplete(qptr) struct _Z_InputQ *qptr;{ qptr = qptr->next; while (qptr) { if (qptr->complete) return (qptr); qptr = qptr->next; } return ((struct _Z_InputQ *)0);}void Z_RemQueue(qptr) struct _Z_InputQ *qptr;{ struct _Z_Hole *hole, *nexthole; if (qptr->complete) __Q_CompleteLength--; __Q_Size -= qptr->msg_len; if (qptr->header) free(qptr->header); if (qptr->msg) free(qptr->msg); if (qptr->packet) free(qptr->packet); hole = qptr->holelist; while (hole) { nexthole = hole->next; free((char *)hole); hole = nexthole; } if (qptr == __Q_Head && __Q_Head == __Q_Tail) { free ((char *)qptr); __Q_Head = (struct _Z_InputQ *)0; __Q_Tail = (struct _Z_InputQ *)0; return; } if (qptr == __Q_Head) { __Q_Head = qptr->next; __Q_Head->prev = (struct _Z_InputQ *)0; free ((char *)qptr); return; } if (qptr == __Q_Tail) { __Q_Tail = qptr->prev; __Q_Tail->next = (struct _Z_InputQ *)0; free ((char *)qptr); return; } qptr->prev->next = qptr->next; qptr->next->prev = qptr->prev; free ((char *)qptr); return;}Code_t Z_SendFragmentedNotice(notice, len, cert_func, send_func) ZNotice_t *notice; int len; Z_AuthProc cert_func; Z_SendProc send_func;{ ZNotice_t partnotice; ZPacket_t buffer; char multi[64]; int offset, hdrsize, fragsize, ret_len, message_len, waitforack; Code_t retval; hdrsize = len-notice->z_message_len; fragsize = Z_MAXPKTLEN-hdrsize-Z_FRAGFUDGE; offset = 0; waitforack = ((notice->z_kind == UNACKED || notice->z_kind == ACKED) && !__Zephyr_server); partnotice = *notice; while (offset < notice->z_message_len || !notice->z_message_len) { (void) sprintf(multi, "%d/%d", offset, notice->z_message_len); partnotice.z_multinotice = multi; if (offset > 0) { (void) gettimeofday(&partnotice.z_uid.tv, (struct timezone *)0); partnotice.z_uid.tv.tv_sec = htonl((unsigned long) partnotice.z_uid.tv.tv_sec); partnotice.z_uid.tv.tv_usec = htonl((unsigned long) partnotice.z_uid.tv.tv_usec); (void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr, sizeof(__My_addr)); } message_len = min(notice->z_message_len-offset, fragsize); partnotice.z_message = (char*)notice->z_message+offset; partnotice.z_message_len = message_len; if ((retval = Z_FormatAuthHeader(&partnotice, buffer, Z_MAXHEADERLEN, &ret_len, cert_func)) != ZERR_NONE) { return (retval); } memcpy(buffer + ret_len, partnotice.z_message, message_len); if ((retval = (*send_func)(&partnotice, buffer, ret_len+message_len, waitforack)) != ZERR_NONE) { return (retval); } offset += fragsize; if (!notice->z_message_len) break; } return (ZERR_NONE);}/*ARGSUSED*/Code_t Z_XmitFragment(notice, buf, len, wait)ZNotice_t *notice;char *buf;int len;int wait;{ return(ZSendPacket(buf, len, wait));}#ifdef Z_DEBUG/* For debugging printing */const char *const ZNoticeKinds[] = { "UNSAFE", "UNACKED", "ACKED", "HMACK", "HMCTL", "SERVACK", "SERVNAK", "CLIENTACK", "STAT"};#endif#ifdef Z_DEBUG#undef Z_debug#ifdef HAVE_STDARG_Hvoid Z_debug (const char *format, ...){ va_list pvar; if (!__Z_debug_print) return; va_start (pvar, format); (*__Z_debug_print) (format, pvar, __Z_debug_print_closure); va_end (pvar);}#else /* stdarg */void Z_debug (va_alist) va_dcl{ va_list pvar; char *format; if (!__Z_debug_print) return; va_start (pvar); format = va_arg (pvar, char *); (*__Z_debug_print) (format, pvar, __Z_debug_print_closure); va_end (pvar);}#endifvoid Z_debug_stderr (format, args, closure) const char *format; va_list args; void *closure;{#ifdef HAVE_VPRINTF vfprintf (stderr, format, args);#else _doprnt (format, args, stderr);#endif putc ('\n', stderr);}#undef ZGetFDint ZGetFD () { return __Zephyr_fd; }#undef ZQLengthint ZQLength () { return __Q_CompleteLength; }#undef ZGetDestAddrstruct sockaddr_in ZGetDestAddr () { return __HM_addr; }#undef ZGetRealmZconst char * ZGetRealm () { return __Zephyr_realm; }#undef ZSetDebugvoid ZSetDebug(proc, arg) void (*proc) __P((const char *, va_list, void *)); char *arg;{ __Z_debug_print = proc; __Z_debug_print_closure = arg;}#endif /* Z_DEBUG */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -