Thu, 03 Apr 2014 19:07:20 +0200
Fix rest of clang scan-build warnings
| 2086 | 1 | /* This file is part of the Project Athena Zephyr Notification System. |
| 2 | * It contains source for the internal Zephyr routines. | |
| 3 | * | |
| 4 | * Created by: Robert French | |
| 5 | * | |
| 6 | * Copyright (c) 1987,1988,1991 by the Massachusetts Institute of | |
| 7 | * Technology. | |
| 8 | * For copying and distribution information, see the file | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
9 | * "mit-copyright.h". |
| 2086 | 10 | */ |
| 11 | ||
|
8792
b0645c9dc276
[gaim-migrate @ 9554]
Christian Hammond <chipx86@chipx86.com>
parents:
7475
diff
changeset
|
12 | #include "internal.h" |
| 10867 | 13 | #ifdef WIN32 |
| 14 | #include <winsock2.h> | |
| 15 | ||
| 16 | #ifndef ZEPHYR_USES_KERBEROS | |
| 17 | int gettimeofday(struct timeval* p, struct timezone* tz ){ | |
| 18 | union { | |
| 19 | long long ns100; /*time since 1 Jan 1601 in 100ns units */ | |
| 20 | FILETIME ft; | |
| 21 | } _now; | |
| 22 | ||
| 23 | GetSystemTimeAsFileTime( &(_now.ft) ); | |
| 24 | p->tv_usec=(long)((_now.ns100 / 10LL) % 1000000LL ); | |
| 25 | p->tv_sec= (long)((_now.ns100-(116444736000000000LL))/10000000LL); | |
| 26 | return 0; | |
| 27 | } | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
28 | #endif |
| 10867 | 29 | |
| 30 | #else | |
| 2086 | 31 | #include <arpa/inet.h> |
| 32 | #include <sys/socket.h> | |
| 10867 | 33 | #endif |
| 2086 | 34 | |
| 35 | int __Zephyr_fd = -1; | |
| 36 | int __Zephyr_open; | |
| 37 | int __Zephyr_port = -1; | |
| 2419 | 38 | struct in_addr __My_addr; |
| 2086 | 39 | int __Q_CompleteLength; |
| 40 | int __Q_Size; | |
| 41 | struct _Z_InputQ *__Q_Head, *__Q_Tail; | |
| 42 | struct sockaddr_in __HM_addr; | |
| 43 | struct sockaddr_in __HM_addr_real; | |
| 44 | int __HM_set; | |
| 45 | int __Zephyr_server; | |
| 46 | ZLocations_t *__locate_list; | |
| 47 | int __locate_num; | |
| 48 | int __locate_next; | |
| 49 | ZSubscription_t *__subscriptions_list; | |
| 50 | int __subscriptions_num; | |
| 51 | int __subscriptions_next; | |
| 2419 | 52 | int Z_discarded_packets = 0; |
| 2086 | 53 | |
| 54 | #ifdef ZEPHYR_USES_KERBEROS | |
| 55 | C_Block __Zephyr_session; | |
| 2419 | 56 | #endif |
| 2086 | 57 | char __Zephyr_realm[REALM_SZ]; |
| 58 | ||
| 59 | #ifdef Z_DEBUG | |
| 60 | void (*__Z_debug_print) __P((const char *fmt, va_list args, void *closure)); | |
| 61 | void *__Z_debug_print_closure; | |
| 62 | #endif | |
| 63 | ||
| 64 | #define min(a,b) ((a)<(b)?(a):(b)) | |
| 65 | ||
| 5136 | 66 | static int Z_AddField __P((char **ptr, const char *field, char *end)); |
| 2086 | 67 | static int find_or_insert_uid __P((ZUnique_Id_t *uid, ZNotice_Kind_t kind)); |
| 68 | ||
| 69 | /* Find or insert uid in the old uids buffer. The buffer is a sorted | |
| 70 | * circular queue. We make the assumption that most packets arrive in | |
| 71 | * order, so we can usually search for a uid or insert it into the buffer | |
| 72 | * by looking back just a few entries from the end. Since this code is | |
| 73 | * only executed by the client, the implementation isn't microoptimized. */ | |
| 74 | static int find_or_insert_uid(uid, kind) | |
| 75 | ZUnique_Id_t *uid; | |
| 76 | ZNotice_Kind_t kind; | |
| 77 | { | |
| 78 | static struct _filter { | |
| 79 | ZUnique_Id_t uid; | |
| 80 | ZNotice_Kind_t kind; | |
| 81 | time_t t; | |
| 82 | } *buffer; | |
| 83 | static long size; | |
| 84 | static long start; | |
| 85 | static long num; | |
| 86 | ||
| 87 | time_t now; | |
| 88 | struct _filter *new; | |
| 89 | long i, j, new_size; | |
| 90 | int result; | |
| 91 | ||
| 92 | /* Initialize the uid buffer if it hasn't been done already. */ | |
| 93 | if (!buffer) { | |
| 94 | size = Z_INITFILTERSIZE; | |
| 95 | buffer = (struct _filter *) malloc(size * sizeof(*buffer)); | |
| 96 | if (!buffer) | |
| 97 | return 0; | |
| 98 | } | |
| 99 | ||
| 100 | /* Age the uid buffer, discarding any uids older than the clock skew. */ | |
| 101 | time(&now); | |
| 102 | while (num && (now - buffer[start % size].t) > CLOCK_SKEW) | |
| 103 | start++, num--; | |
| 104 | start %= size; | |
| 105 | ||
| 106 | /* Make room for a new uid, since we'll probably have to insert one. */ | |
| 107 | if (num == size) { | |
| 108 | new_size = size * 2 + 2; | |
| 109 | new = (struct _filter *) malloc(new_size * sizeof(*new)); | |
| 110 | if (!new) | |
| 111 | return 0; | |
| 112 | for (i = 0; i < num; i++) | |
| 113 | new[i] = buffer[(start + i) % size]; | |
| 114 | free(buffer); | |
| 115 | buffer = new; | |
| 116 | size = new_size; | |
| 117 | start = 0; | |
| 118 | } | |
| 119 | ||
| 120 | /* Search for this uid in the buffer, starting from the end. */ | |
| 121 | for (i = start + num - 1; i >= start; i--) { | |
| 122 | result = memcmp(uid, &buffer[i % size].uid, sizeof(*uid)); | |
| 123 | if (result == 0 && buffer[i % size].kind == kind) | |
| 124 | return 1; | |
| 125 | if (result > 0) | |
| 126 | break; | |
| 127 | } | |
| 128 | ||
| 129 | /* We didn't find it; insert the uid into the buffer after i. */ | |
| 130 | i++; | |
| 131 | for (j = start + num; j > i; j--) | |
| 132 | buffer[j % size] = buffer[(j - 1) % size]; | |
| 133 | buffer[i % size].uid = *uid; | |
| 134 | buffer[i % size].kind = kind; | |
| 135 | buffer[i % size].t = now; | |
| 136 | num++; | |
| 137 | ||
| 138 | return 0; | |
| 139 | } | |
| 140 | ||
| 141 | ||
| 142 | /* Return 1 if there is a packet waiting, 0 otherwise */ | |
| 143 | ||
|
22104
56970903b8e9
Probe for -Wstrict-prototypes to get some more warnings. I then cleaned up
Richard Laager <rlaager@pidgin.im>
parents:
15435
diff
changeset
|
144 | static int Z_PacketWaiting(void) |
| 2086 | 145 | { |
| 146 | struct timeval tv; | |
| 147 | fd_set read; | |
| 148 | ||
| 149 | tv.tv_sec = tv.tv_usec = 0; | |
| 150 | FD_ZERO(&read); | |
| 151 | FD_SET(ZGetFD(), &read); | |
| 152 | return (select(ZGetFD() + 1, &read, NULL, NULL, &tv)); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
153 | } |
| 2086 | 154 | |
| 155 | ||
| 156 | /* Wait for a complete notice to become available */ | |
| 157 | ||
|
22104
56970903b8e9
Probe for -Wstrict-prototypes to get some more warnings. I then cleaned up
Richard Laager <rlaager@pidgin.im>
parents:
15435
diff
changeset
|
158 | Code_t Z_WaitForComplete(void) |
| 2086 | 159 | { |
| 160 | Code_t retval; | |
| 161 | ||
| 162 | if (__Q_CompleteLength) | |
| 163 | return (Z_ReadEnqueue()); | |
| 164 | ||
| 165 | while (!__Q_CompleteLength) | |
| 166 | if ((retval = Z_ReadWait()) != ZERR_NONE) | |
| 167 | return (retval); | |
| 168 | ||
| 169 | return (ZERR_NONE); | |
| 170 | } | |
| 171 | ||
| 172 | ||
| 173 | /* Read any available packets and enqueue them */ | |
| 174 | ||
| 175 | Code_t Z_ReadEnqueue() | |
| 176 | { | |
| 177 | Code_t retval; | |
| 178 | ||
| 179 | if (ZGetFD() < 0) | |
| 180 | return (ZERR_NOPORT); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
181 | |
| 2086 | 182 | while (Z_PacketWaiting()) |
| 183 | if ((retval = Z_ReadWait()) != ZERR_NONE) | |
| 184 | return (retval); | |
| 185 | ||
| 186 | return (ZERR_NONE); | |
| 187 | } | |
| 188 | ||
| 189 | ||
| 190 | /* | |
| 191 | * Search the queue for a notice with the proper multiuid - remove any | |
| 192 | * notices that haven't been touched in a while | |
| 193 | */ | |
| 194 | ||
|
22104
56970903b8e9
Probe for -Wstrict-prototypes to get some more warnings. I then cleaned up
Richard Laager <rlaager@pidgin.im>
parents:
15435
diff
changeset
|
195 | static struct _Z_InputQ *Z_SearchQueue(ZUnique_Id_t *uid, ZNotice_Kind_t kind) |
| 2086 | 196 | { |
| 197 | register struct _Z_InputQ *qptr; | |
| 198 | struct _Z_InputQ *next; | |
| 199 | struct timeval tv; | |
| 200 | ||
| 201 | (void) gettimeofday(&tv, (struct timezone *)0); | |
| 202 | ||
| 203 | qptr = __Q_Head; | |
| 204 | ||
| 205 | while (qptr) { | |
| 206 | if (ZCompareUID(uid, &qptr->uid) && qptr->kind == kind) | |
| 207 | return (qptr); | |
| 208 | next = qptr->next; | |
|
34304
faf0414a8b51
Fix most of libpurple warnings about -Wsign-compare
Tomasz Wasilczyk <tomkiewicz@cpw.pidgin.im>
parents:
32016
diff
changeset
|
209 | if (qptr->timep && ((time_t)qptr->timep+Z_NOTICETIMELIMIT < tv.tv_sec)) |
| 2086 | 210 | Z_RemQueue(qptr); |
| 211 | qptr = next; | |
| 212 | } | |
| 213 | return (NULL); | |
| 214 | } | |
| 215 | ||
| 216 | /* | |
| 217 | * Now we delve into really convoluted queue handling and | |
| 218 | * fragmentation reassembly algorithms and other stuff you probably | |
| 219 | * don't want to look at... | |
| 220 | * | |
| 221 | * This routine does NOT guarantee a complete packet will be ready when it | |
| 222 | * returns. | |
| 223 | */ | |
| 224 | ||
| 225 | Code_t Z_ReadWait() | |
| 226 | { | |
| 227 | register struct _Z_InputQ *qptr; | |
| 228 | ZNotice_t notice; | |
| 229 | ZPacket_t packet; | |
| 230 | struct sockaddr_in olddest, from; | |
|
11318
13fa1d5134f3
[gaim-migrate @ 13521]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
11105
diff
changeset
|
231 | int packet_len, zvlen, part, partof; |
|
13fa1d5134f3
[gaim-migrate @ 13521]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
11105
diff
changeset
|
232 | socklen_t from_len; |
| 2086 | 233 | char *slash; |
| 234 | Code_t retval; | |
| 2419 | 235 | fd_set fds; |
| 236 | struct timeval tv; | |
| 2086 | 237 | |
| 238 | if (ZGetFD() < 0) | |
| 239 | return (ZERR_NOPORT); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
240 | |
| 2419 | 241 | FD_ZERO(&fds); |
| 242 | FD_SET(ZGetFD(), &fds); | |
| 243 | tv.tv_sec = 60; | |
| 244 | tv.tv_usec = 0; | |
| 245 | ||
| 246 | if (select(ZGetFD() + 1, &fds, NULL, NULL, &tv) < 0) | |
| 247 | return (errno); | |
| 248 | if (!FD_ISSET(ZGetFD(), &fds)) | |
| 249 | return ETIMEDOUT; | |
| 250 | ||
| 2086 | 251 | from_len = sizeof(struct sockaddr_in); |
| 252 | ||
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
253 | packet_len = recvfrom(ZGetFD(), packet, sizeof(packet), 0, |
| 2086 | 254 | (struct sockaddr *)&from, &from_len); |
| 255 | ||
| 256 | if (packet_len < 0) | |
| 257 | return (errno); | |
| 258 | ||
| 259 | if (!packet_len) | |
| 260 | return (ZERR_EOF); | |
| 261 | ||
| 2419 | 262 | /* Ignore obviously non-Zephyr packets. */ |
| 263 | zvlen = sizeof(ZVERSIONHDR) - 1; | |
| 264 | if (packet_len < zvlen || memcmp(packet, ZVERSIONHDR, zvlen) != 0) { | |
| 265 | Z_discarded_packets++; | |
| 266 | return (ZERR_NONE); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
267 | } |
| 2086 | 268 | |
| 269 | /* Parse the notice */ | |
| 270 | if ((retval = ZParseNotice(packet, packet_len, ¬ice)) != ZERR_NONE) | |
| 271 | return (retval); | |
| 272 | ||
| 273 | /* | |
| 274 | * If we're not a server and the notice is of an appropriate kind, | |
| 275 | * send back a CLIENTACK to whoever sent it to say we got it. | |
| 276 | */ | |
| 277 | if (!__Zephyr_server) { | |
| 278 | if (notice.z_kind != HMACK && notice.z_kind != SERVACK && | |
| 279 | notice.z_kind != SERVNAK && notice.z_kind != CLIENTACK) { | |
| 280 | ZNotice_t tmpnotice; | |
| 281 | ZPacket_t pkt; | |
| 282 | int len; | |
| 283 | ||
| 284 | tmpnotice = notice; | |
| 285 | tmpnotice.z_kind = CLIENTACK; | |
| 286 | tmpnotice.z_message_len = 0; | |
| 287 | olddest = __HM_addr; | |
| 288 | __HM_addr = from; | |
| 289 | if ((retval = ZFormatSmallRawNotice(&tmpnotice, pkt, &len)) | |
| 290 | != ZERR_NONE) | |
| 291 | return(retval); | |
| 292 | if ((retval = ZSendPacket(pkt, len, 0)) != ZERR_NONE) | |
| 293 | return (retval); | |
| 294 | __HM_addr = olddest; | |
| 295 | } | |
| 296 | if (find_or_insert_uid(¬ice.z_uid, notice.z_kind)) | |
| 297 | return(ZERR_NONE); | |
| 298 | ||
| 299 | /* Check authentication on the notice. */ | |
| 300 | notice.z_checked_auth = ZCheckAuthentication(¬ice, &from); | |
| 301 | } | |
| 302 | ||
| 303 | ||
| 304 | /* | |
| 305 | * Parse apart the z_multinotice field - if the field is blank for | |
| 306 | * some reason, assume this packet stands by itself. | |
| 307 | */ | |
| 308 | slash = strchr(notice.z_multinotice, '/'); | |
| 309 | if (slash) { | |
| 310 | part = atoi(notice.z_multinotice); | |
| 311 | partof = atoi(slash+1); | |
| 312 | if (part > partof || partof == 0) { | |
| 313 | part = 0; | |
| 314 | partof = notice.z_message_len; | |
| 315 | } | |
| 316 | } | |
| 317 | else { | |
| 318 | part = 0; | |
| 319 | partof = notice.z_message_len; | |
| 320 | } | |
| 321 | ||
| 322 | /* Too big a packet...just ignore it! */ | |
| 323 | if (partof > Z_MAXNOTICESIZE) | |
| 324 | return (ZERR_NONE); | |
| 325 | ||
| 326 | /* | |
| 327 | * If we aren't a server and we can find a notice in the queue | |
| 328 | * with the same multiuid field, insert the current fragment as | |
| 329 | * appropriate. | |
| 330 | */ | |
| 331 | switch (notice.z_kind) { | |
| 332 | case SERVACK: | |
| 333 | case SERVNAK: | |
| 334 | /* The SERVACK and SERVNAK replies shouldn't be reassembled | |
| 335 | (they have no parts). Instead, we should hold on to the reply | |
| 336 | ONLY if it's the first part of a fragmented message, i.e. | |
| 337 | multi_uid == uid. This allows programs to wait for the uid | |
| 338 | of the first packet, and get a response when that notice | |
| 339 | arrives. Acknowledgements of the other fragments are discarded | |
| 340 | (XXX we assume here that they all carry the same information | |
| 341 | regarding failure/success) | |
| 342 | */ | |
| 343 | if (!__Zephyr_server && | |
| 344 | !ZCompareUID(¬ice.z_multiuid, ¬ice.z_uid)) | |
| 345 | /* they're not the same... throw away this packet. */ | |
| 346 | return(ZERR_NONE); | |
| 347 | /* fall thru & process it */ | |
| 348 | default: | |
| 349 | /* for HMACK types, we assume no packet loss (local loopback | |
| 350 | connections). The other types can be fragmented and MUST | |
| 351 | run through this code. */ | |
| 352 | if (!__Zephyr_server && (qptr = Z_SearchQueue(¬ice.z_multiuid, | |
| 353 | notice.z_kind))) { | |
| 354 | /* | |
| 355 | * If this is the first fragment, and we haven't already | |
| 356 | * gotten a first fragment, grab the header from it. | |
| 357 | */ | |
| 358 | if (part == 0 && !qptr->header) { | |
| 359 | qptr->header_len = packet_len-notice.z_message_len; | |
| 360 | qptr->header = (char *) malloc((unsigned) qptr->header_len); | |
| 361 | if (!qptr->header) | |
| 362 | return (ENOMEM); | |
| 363 | (void) memcpy(qptr->header, packet, qptr->header_len); | |
| 364 | } | |
| 365 | return (Z_AddNoticeToEntry(qptr, ¬ice, part)); | |
| 366 | } | |
| 367 | } | |
| 368 | ||
| 369 | /* | |
| 370 | * We'll have to create a new entry...make sure the queue isn't | |
| 371 | * going to get too big. | |
| 372 | */ | |
| 373 | if (__Q_Size+(__Zephyr_server ? notice.z_message_len : partof) > Z_MAXQUEUESIZE) | |
| 374 | return (ZERR_NONE); | |
| 375 | ||
| 376 | /* | |
| 377 | * This is a notice we haven't heard of, so create a new queue | |
| 378 | * entry for it and zero it out. | |
| 379 | */ | |
| 380 | qptr = (struct _Z_InputQ *)malloc(sizeof(struct _Z_InputQ)); | |
| 381 | if (!qptr) | |
| 382 | return (ENOMEM); | |
| 383 | (void) memset((char *)qptr, 0, sizeof(struct _Z_InputQ)); | |
| 384 | ||
| 385 | /* Insert the entry at the end of the queue */ | |
| 386 | qptr->next = NULL; | |
| 387 | qptr->prev = __Q_Tail; | |
| 388 | if (__Q_Tail) | |
| 389 | __Q_Tail->next = qptr; | |
| 390 | __Q_Tail = qptr; | |
| 391 | ||
| 392 | if (!__Q_Head) | |
| 393 | __Q_Head = qptr; | |
| 394 | ||
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
395 | |
| 2086 | 396 | /* Copy the from field, multiuid, kind, and checked authentication. */ |
| 397 | qptr->from = from; | |
| 398 | qptr->uid = notice.z_multiuid; | |
| 399 | qptr->kind = notice.z_kind; | |
| 400 | qptr->auth = notice.z_checked_auth; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
401 | |
| 2086 | 402 | /* |
| 403 | * If this is the first part of the notice, we take the header | |
| 404 | * from it. We only take it if this is the first fragment so that | |
| 405 | * the Unique ID's will be predictable. | |
| 406 | * | |
| 407 | * If a Zephyr Server, we always take the header. | |
| 408 | */ | |
| 409 | if (__Zephyr_server || part == 0) { | |
| 410 | qptr->header_len = packet_len-notice.z_message_len; | |
| 411 | qptr->header = (char *) malloc((unsigned) qptr->header_len); | |
| 412 | if (!qptr->header) | |
| 413 | return ENOMEM; | |
| 414 | (void) memcpy(qptr->header, packet, qptr->header_len); | |
| 415 | } | |
| 416 | ||
| 417 | /* | |
| 418 | * If this is not a fragmented notice, then don't bother with a | |
| 419 | * hole list. | |
| 420 | * If we are a Zephyr server, all notices are treated as complete. | |
| 421 | */ | |
| 422 | if (__Zephyr_server || (part == 0 && notice.z_message_len == partof)) { | |
| 423 | __Q_CompleteLength++; | |
| 424 | qptr->holelist = (struct _Z_Hole *) 0; | |
| 425 | qptr->complete = 1; | |
| 426 | /* allocate a msg buf for this piece */ | |
| 427 | if (notice.z_message_len == 0) | |
| 428 | qptr->msg = 0; | |
| 429 | else if (!(qptr->msg = (char *) malloc((unsigned) notice.z_message_len))) | |
| 430 | return(ENOMEM); | |
| 431 | else | |
| 432 | (void) memcpy(qptr->msg, notice.z_message, notice.z_message_len); | |
| 433 | qptr->msg_len = notice.z_message_len; | |
| 434 | __Q_Size += notice.z_message_len; | |
| 435 | qptr->packet_len = qptr->header_len+qptr->msg_len; | |
| 436 | if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len))) | |
| 437 | return (ENOMEM); | |
| 438 | (void) memcpy(qptr->packet, qptr->header, qptr->header_len); | |
| 439 | if(qptr->msg) | |
| 440 | (void) memcpy(qptr->packet+qptr->header_len, qptr->msg, | |
| 441 | qptr->msg_len); | |
| 442 | return (ZERR_NONE); | |
| 443 | } | |
| 444 | ||
| 445 | /* | |
| 446 | * We know how long the message is going to be (this is better | |
| 447 | * than IP fragmentation...), so go ahead and allocate it all. | |
| 448 | */ | |
| 449 | if (!(qptr->msg = (char *) malloc((unsigned) partof)) && partof) | |
| 450 | return (ENOMEM); | |
| 451 | qptr->msg_len = partof; | |
| 452 | __Q_Size += partof; | |
| 453 | ||
| 454 | /* | |
| 455 | * Well, it's a fragmented notice...allocate a hole list and | |
| 456 | * initialize it to the full packet size. Then insert the | |
| 457 | * current fragment. | |
| 458 | */ | |
| 459 | if (!(qptr->holelist = (struct _Z_Hole *) | |
| 460 | malloc(sizeof(struct _Z_Hole)))) | |
| 461 | return (ENOMEM); | |
| 462 | qptr->holelist->next = (struct _Z_Hole *) 0; | |
| 463 | qptr->holelist->first = 0; | |
| 464 | qptr->holelist->last = partof-1; | |
| 465 | return (Z_AddNoticeToEntry(qptr, ¬ice, part)); | |
| 466 | } | |
| 467 | ||
| 468 | ||
| 469 | /* Fragment management routines - compliments, more or less, of RFC815 */ | |
| 470 | ||
| 471 | Code_t Z_AddNoticeToEntry(qptr, notice, part) | |
| 472 | struct _Z_InputQ *qptr; | |
| 473 | ZNotice_t *notice; | |
| 474 | int part; | |
| 475 | { | |
| 476 | int last, oldfirst, oldlast; | |
| 477 | struct _Z_Hole *hole, *lasthole; | |
| 478 | struct timeval tv; | |
| 479 | ||
| 480 | /* Incorporate this notice's checked authentication. */ | |
| 481 | if (notice->z_checked_auth == ZAUTH_FAILED) | |
| 482 | qptr->auth = ZAUTH_FAILED; | |
| 483 | else if (notice->z_checked_auth == ZAUTH_NO && qptr->auth != ZAUTH_FAILED) | |
| 484 | qptr->auth = ZAUTH_NO; | |
| 485 | ||
| 486 | (void) gettimeofday(&tv, (struct timezone *)0); | |
| 487 | qptr->timep = tv.tv_sec; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
488 | |
| 2086 | 489 | last = part+notice->z_message_len-1; |
| 490 | ||
| 491 | hole = qptr->holelist; | |
| 492 | lasthole = (struct _Z_Hole *) 0; | |
| 493 | ||
| 494 | /* copy in the message body */ | |
| 495 | (void) memcpy(qptr->msg+part, notice->z_message, notice->z_message_len); | |
| 496 | ||
| 497 | /* Search for a hole that overlaps with the current fragment */ | |
| 498 | while (hole) { | |
| 499 | if (part <= hole->last && last >= hole->first) | |
| 500 | break; | |
| 501 | lasthole = hole; | |
| 502 | hole = hole->next; | |
| 503 | } | |
| 504 | ||
| 505 | /* If we found one, delete it and reconstruct a new hole */ | |
| 506 | if (hole) { | |
| 507 | oldfirst = hole->first; | |
| 508 | oldlast = hole->last; | |
| 509 | if (lasthole) | |
| 510 | lasthole->next = hole->next; | |
| 511 | else | |
| 512 | qptr->holelist = hole->next; | |
| 513 | free((char *)hole); | |
| 514 | /* | |
| 515 | * Now create a new hole that is the original hole without the | |
| 516 | * current fragment. | |
| 517 | */ | |
| 518 | if (part > oldfirst) { | |
| 519 | /* Search for the end of the hole list */ | |
| 520 | hole = qptr->holelist; | |
| 521 | lasthole = (struct _Z_Hole *) 0; | |
| 522 | while (hole) { | |
| 523 | lasthole = hole; | |
| 524 | hole = hole->next; | |
| 525 | } | |
| 526 | if (lasthole) { | |
|
35692
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
527 | struct _Z_InputQ *inputq = malloc(sizeof(struct _Z_InputQ)); |
|
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
528 | if (!inputq) |
| 2086 | 529 | return (ENOMEM); |
|
35692
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
530 | lasthole->next = (struct _Z_Hole *)inputq; |
| 2086 | 531 | hole = lasthole->next; |
| 532 | } | |
| 533 | else { | |
|
35692
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
534 | struct _Z_InputQ *inputq = malloc(sizeof(struct _Z_InputQ)); |
|
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
535 | if (!inputq) |
| 2086 | 536 | return (ENOMEM); |
|
35692
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
537 | qptr->holelist = (struct _Z_Hole *)inputq; |
| 2086 | 538 | hole = qptr->holelist; |
| 539 | } | |
| 540 | hole->next = NULL; | |
| 541 | hole->first = oldfirst; | |
| 542 | hole->last = part-1; | |
| 543 | } | |
| 544 | if (last < oldlast) { | |
| 545 | /* Search for the end of the hole list */ | |
| 546 | hole = qptr->holelist; | |
| 547 | lasthole = (struct _Z_Hole *) 0; | |
| 548 | while (hole) { | |
| 549 | lasthole = hole; | |
| 550 | hole = hole->next; | |
| 551 | } | |
| 552 | if (lasthole) { | |
|
35692
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
553 | struct _Z_InputQ *inputq = malloc(sizeof(struct _Z_InputQ)); |
|
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
554 | if (!inputq) |
| 2086 | 555 | return (ENOMEM); |
|
35692
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
556 | lasthole->next = (struct _Z_Hole *)inputq; |
| 2086 | 557 | hole = lasthole->next; |
| 558 | } | |
| 559 | else { | |
|
35692
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
560 | struct _Z_InputQ *inputq = malloc(sizeof(struct _Z_InputQ)); |
|
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
561 | if (!inputq) |
| 2086 | 562 | return (ENOMEM); |
|
35692
adeb081c2224
Fix rest of clang scan-build warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
34304
diff
changeset
|
563 | qptr->holelist = (struct _Z_Hole *)inputq; |
| 2086 | 564 | hole = qptr->holelist; |
| 565 | } | |
| 566 | hole->next = (struct _Z_Hole *) 0; | |
| 567 | hole->first = last+1; | |
| 568 | hole->last = oldlast; | |
| 569 | } | |
| 570 | } | |
| 571 | ||
| 572 | if (!qptr->holelist) { | |
| 573 | if (!qptr->complete) | |
| 574 | __Q_CompleteLength++; | |
| 575 | qptr->complete = 1; | |
| 576 | qptr->timep = 0; /* don't time out anymore */ | |
| 577 | qptr->packet_len = qptr->header_len+qptr->msg_len; | |
| 578 | if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len))) | |
| 579 | return (ENOMEM); | |
| 580 | (void) memcpy(qptr->packet, qptr->header, qptr->header_len); | |
| 581 | (void) memcpy(qptr->packet+qptr->header_len, qptr->msg, | |
| 582 | qptr->msg_len); | |
| 583 | } | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
584 | |
| 2086 | 585 | return (ZERR_NONE); |
| 586 | } | |
| 587 | ||
| 588 | Code_t Z_FormatHeader(notice, buffer, buffer_len, len, cert_routine) | |
| 589 | ZNotice_t *notice; | |
| 590 | char *buffer; | |
| 591 | int buffer_len; | |
| 592 | int *len; | |
| 593 | Z_AuthProc cert_routine; | |
| 594 | { | |
| 595 | Code_t retval; | |
| 596 | static char version[BUFSIZ]; /* default init should be all \0 */ | |
| 597 | struct sockaddr_in name; | |
|
11318
13fa1d5134f3
[gaim-migrate @ 13521]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
11105
diff
changeset
|
598 | socklen_t namelen = sizeof(name); |
| 2086 | 599 | |
| 600 | if (!notice->z_sender) | |
| 601 | notice->z_sender = ZGetSender(); | |
| 602 | ||
| 603 | if (notice->z_port == 0) { | |
| 604 | if (ZGetFD() < 0) { | |
|
7475
987384816492
[gaim-migrate @ 8088]
Mark Doliner <markdoliner@pidgin.im>
parents:
5136
diff
changeset
|
605 | retval = ZOpenPort((unsigned short *)0); |
| 2086 | 606 | if (retval != ZERR_NONE) |
| 607 | return (retval); | |
| 608 | } | |
| 609 | retval = getsockname(ZGetFD(), (struct sockaddr *) &name, &namelen); | |
| 610 | if (retval != 0) | |
| 611 | return (retval); | |
| 612 | notice->z_port = name.sin_port; | |
| 613 | } | |
| 614 | ||
| 615 | notice->z_multinotice = ""; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
616 | |
| 2086 | 617 | (void) gettimeofday(¬ice->z_uid.tv, (struct timezone *)0); |
|
7475
987384816492
[gaim-migrate @ 8088]
Mark Doliner <markdoliner@pidgin.im>
parents:
5136
diff
changeset
|
618 | notice->z_uid.tv.tv_sec = htonl((unsigned long) notice->z_uid.tv.tv_sec); |
|
987384816492
[gaim-migrate @ 8088]
Mark Doliner <markdoliner@pidgin.im>
parents:
5136
diff
changeset
|
619 | notice->z_uid.tv.tv_usec = htonl((unsigned long) notice->z_uid.tv.tv_usec); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
620 | |
| 2419 | 621 | (void) memcpy(¬ice->z_uid.zuid_addr, &__My_addr, sizeof(__My_addr)); |
| 2086 | 622 | |
| 623 | notice->z_multiuid = notice->z_uid; | |
| 624 | ||
| 625 | if (!version[0]) | |
| 626 | (void) sprintf(version, "%s%d.%d", ZVERSIONHDR, ZVERSIONMAJOR, | |
| 627 | ZVERSIONMINOR); | |
| 628 | notice->z_version = version; | |
| 629 | ||
| 630 | return Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine); | |
| 631 | } | |
| 632 | ||
| 633 | Code_t Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine) | |
| 634 | ZNotice_t *notice; | |
| 635 | char *buffer; | |
| 636 | int buffer_len; | |
| 637 | int *len; | |
| 638 | Z_AuthProc cert_routine; | |
| 639 | { | |
| 640 | if (!cert_routine) { | |
| 641 | notice->z_auth = 0; | |
| 642 | notice->z_authent_len = 0; | |
| 643 | notice->z_ascii_authent = ""; | |
| 644 | notice->z_checksum = 0; | |
| 645 | return (Z_FormatRawHeader(notice, buffer, buffer_len, | |
| 646 | len, NULL, NULL)); | |
| 647 | } | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
648 | |
| 2086 | 649 | return ((*cert_routine)(notice, buffer, buffer_len, len)); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
650 | } |
|
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
651 | |
| 2086 | 652 | Code_t Z_FormatRawHeader(notice, buffer, buffer_len, len, cstart, cend) |
| 653 | ZNotice_t *notice; | |
| 654 | char *buffer; | |
|
34304
faf0414a8b51
Fix most of libpurple warnings about -Wsign-compare
Tomasz Wasilczyk <tomkiewicz@cpw.pidgin.im>
parents:
32016
diff
changeset
|
655 | gsize buffer_len; |
| 2086 | 656 | int *len; |
| 657 | char **cstart, **cend; | |
| 658 | { | |
| 659 | char newrecip[BUFSIZ]; | |
| 660 | char *ptr, *end; | |
| 661 | int i; | |
| 662 | ||
| 663 | if (!notice->z_class) | |
| 664 | notice->z_class = ""; | |
| 665 | ||
| 666 | if (!notice->z_class_inst) | |
| 667 | notice->z_class_inst = ""; | |
| 668 | ||
| 669 | if (!notice->z_opcode) | |
| 670 | notice->z_opcode = ""; | |
| 671 | ||
| 672 | if (!notice->z_recipient) | |
| 673 | notice->z_recipient = ""; | |
| 674 | ||
| 675 | if (!notice->z_default_format) | |
| 676 | notice->z_default_format = ""; | |
| 677 | ||
| 678 | ptr = buffer; | |
| 679 | end = buffer+buffer_len; | |
| 680 | ||
| 681 | if (buffer_len < strlen(notice->z_version)+1) | |
| 682 | return (ZERR_HEADERLEN); | |
| 683 | ||
|
31954
e5631d5590cc
Replace numerous strcpy() invocations with strlcpy() in Zephyr.
Ethan Blanton <elb@pidgin.im>
parents:
31363
diff
changeset
|
684 | g_strlcpy(ptr, notice->z_version, buffer_len); |
| 2086 | 685 | ptr += strlen(ptr)+1; |
| 686 | ||
| 687 | if (ZMakeAscii32(ptr, end-ptr, Z_NUMFIELDS + notice->z_num_other_fields) | |
| 688 | == ZERR_FIELDLEN) | |
| 689 | return (ZERR_HEADERLEN); | |
| 690 | ptr += strlen(ptr)+1; | |
| 691 | ||
| 692 | if (ZMakeAscii32(ptr, end-ptr, notice->z_kind) == ZERR_FIELDLEN) | |
| 693 | return (ZERR_HEADERLEN); | |
| 694 | ptr += strlen(ptr)+1; | |
| 695 | ||
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
696 | if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_uid, |
| 2086 | 697 | sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN) |
| 698 | return (ZERR_HEADERLEN); | |
| 699 | ptr += strlen(ptr)+1; | |
| 700 | ||
| 701 | if (ZMakeAscii16(ptr, end-ptr, ntohs(notice->z_port)) == ZERR_FIELDLEN) | |
| 702 | return (ZERR_HEADERLEN); | |
| 703 | ptr += strlen(ptr)+1; | |
| 704 | ||
| 705 | if (ZMakeAscii32(ptr, end-ptr, notice->z_auth) == ZERR_FIELDLEN) | |
| 706 | return (ZERR_HEADERLEN); | |
| 707 | ptr += strlen(ptr)+1; | |
| 708 | ||
| 709 | if (ZMakeAscii32(ptr, end-ptr, notice->z_authent_len) == ZERR_FIELDLEN) | |
| 710 | return (ZERR_HEADERLEN); | |
| 711 | ptr += strlen(ptr)+1; | |
| 712 | ||
| 713 | if (Z_AddField(&ptr, notice->z_ascii_authent, end)) | |
| 714 | return (ZERR_HEADERLEN); | |
| 715 | if (Z_AddField(&ptr, notice->z_class, end)) | |
| 716 | return (ZERR_HEADERLEN); | |
| 717 | if (Z_AddField(&ptr, notice->z_class_inst, end)) | |
| 718 | return (ZERR_HEADERLEN); | |
| 719 | if (Z_AddField(&ptr, notice->z_opcode, end)) | |
| 720 | return (ZERR_HEADERLEN); | |
| 721 | if (Z_AddField(&ptr, notice->z_sender, end)) | |
| 722 | return (ZERR_HEADERLEN); | |
| 723 | if (strchr(notice->z_recipient, '@') || !*notice->z_recipient) { | |
| 724 | if (Z_AddField(&ptr, notice->z_recipient, end)) | |
| 725 | return (ZERR_HEADERLEN); | |
| 726 | } | |
| 727 | else { | |
| 728 | if (strlen(notice->z_recipient) + strlen(__Zephyr_realm) + 2 > | |
| 2419 | 729 | sizeof(newrecip)) |
| 730 | return (ZERR_HEADERLEN); | |
| 2086 | 731 | (void) sprintf(newrecip, "%s@%s", notice->z_recipient, __Zephyr_realm); |
| 732 | if (Z_AddField(&ptr, newrecip, end)) | |
| 733 | return (ZERR_HEADERLEN); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
734 | } |
| 2086 | 735 | if (Z_AddField(&ptr, notice->z_default_format, end)) |
| 736 | return (ZERR_HEADERLEN); | |
| 737 | ||
| 738 | /* copy back the end pointer location for crypto checksum */ | |
| 739 | if (cstart) | |
| 740 | *cstart = ptr; | |
| 741 | if (ZMakeAscii32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN) | |
| 742 | return (ZERR_HEADERLEN); | |
| 743 | ptr += strlen(ptr)+1; | |
| 744 | if (cend) | |
| 745 | *cend = ptr; | |
| 746 | ||
| 747 | if (Z_AddField(&ptr, notice->z_multinotice, end)) | |
| 748 | return (ZERR_HEADERLEN); | |
| 749 | ||
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
750 | if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_multiuid, |
| 2086 | 751 | sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN) |
| 752 | return (ZERR_HEADERLEN); | |
| 753 | ptr += strlen(ptr)+1; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
754 | |
| 2086 | 755 | for (i=0;i<notice->z_num_other_fields;i++) |
| 756 | if (Z_AddField(&ptr, notice->z_other_fields[i], end)) | |
| 757 | return (ZERR_HEADERLEN); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
758 | |
| 2086 | 759 | *len = ptr-buffer; |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
760 | |
| 2086 | 761 | return (ZERR_NONE); |
| 762 | } | |
| 763 | ||
| 764 | static int | |
| 5136 | 765 | Z_AddField(char **ptr, const char *field, char *end) |
| 2086 | 766 | { |
| 767 | register int len; | |
| 768 | ||
| 769 | len = field ? strlen (field) + 1 : 1; | |
| 770 | ||
| 771 | if (*ptr+len > end) | |
| 772 | return 1; | |
| 773 | if (field) | |
|
32016
b1662f03fa07
Fix some un-fixes in the Zephyr strlcpy changes
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
774 | strcpy(*ptr, field); |
| 2086 | 775 | else |
|
31954
e5631d5590cc
Replace numerous strcpy() invocations with strlcpy() in Zephyr.
Ethan Blanton <elb@pidgin.im>
parents:
31363
diff
changeset
|
776 | **ptr = '\0'; |
| 2086 | 777 | *ptr += len; |
| 778 | ||
| 779 | return 0; | |
| 780 | } | |
| 781 | ||
| 782 | struct _Z_InputQ *Z_GetFirstComplete() | |
| 783 | { | |
| 784 | struct _Z_InputQ *qptr; | |
| 785 | ||
| 786 | qptr = __Q_Head; | |
| 787 | ||
| 788 | while (qptr) { | |
| 789 | if (qptr->complete) | |
| 790 | return (qptr); | |
| 791 | qptr = qptr->next; | |
| 792 | } | |
| 793 | ||
| 794 | return ((struct _Z_InputQ *)0); | |
| 795 | } | |
| 796 | ||
| 797 | struct _Z_InputQ *Z_GetNextComplete(qptr) | |
| 798 | struct _Z_InputQ *qptr; | |
| 799 | { | |
| 800 | qptr = qptr->next; | |
| 801 | while (qptr) { | |
| 802 | if (qptr->complete) | |
| 803 | return (qptr); | |
| 804 | qptr = qptr->next; | |
| 805 | } | |
| 806 | ||
| 807 | return ((struct _Z_InputQ *)0); | |
| 808 | } | |
| 809 | ||
| 810 | void Z_RemQueue(qptr) | |
| 811 | struct _Z_InputQ *qptr; | |
| 812 | { | |
| 813 | struct _Z_Hole *hole, *nexthole; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
814 | |
| 2086 | 815 | if (qptr->complete) |
| 816 | __Q_CompleteLength--; | |
| 817 | ||
| 818 | __Q_Size -= qptr->msg_len; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
819 | |
| 2086 | 820 | if (qptr->header) |
| 821 | free(qptr->header); | |
| 822 | if (qptr->msg) | |
| 823 | free(qptr->msg); | |
| 824 | if (qptr->packet) | |
| 825 | free(qptr->packet); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
826 | |
| 2086 | 827 | hole = qptr->holelist; |
| 828 | while (hole) { | |
| 829 | nexthole = hole->next; | |
| 830 | free((char *)hole); | |
| 831 | hole = nexthole; | |
| 832 | } | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
833 | |
| 2086 | 834 | if (qptr == __Q_Head && __Q_Head == __Q_Tail) { |
| 835 | free ((char *)qptr); | |
| 836 | __Q_Head = (struct _Z_InputQ *)0; | |
| 837 | __Q_Tail = (struct _Z_InputQ *)0; | |
| 838 | return; | |
| 839 | } | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
840 | |
| 2086 | 841 | if (qptr == __Q_Head) { |
| 842 | __Q_Head = qptr->next; | |
| 843 | __Q_Head->prev = (struct _Z_InputQ *)0; | |
| 844 | free ((char *)qptr); | |
| 845 | return; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
846 | } |
| 2086 | 847 | if (qptr == __Q_Tail) { |
| 848 | __Q_Tail = qptr->prev; | |
| 849 | __Q_Tail->next = (struct _Z_InputQ *)0; | |
| 850 | free ((char *)qptr); | |
| 851 | return; | |
| 852 | } | |
| 853 | qptr->prev->next = qptr->next; | |
| 854 | qptr->next->prev = qptr->prev; | |
| 855 | free ((char *)qptr); | |
| 856 | return; | |
| 857 | } | |
| 858 | ||
| 859 | Code_t Z_SendFragmentedNotice(notice, len, cert_func, send_func) | |
| 860 | ZNotice_t *notice; | |
| 861 | int len; | |
| 862 | Z_AuthProc cert_func; | |
| 863 | Z_SendProc send_func; | |
| 864 | { | |
| 865 | ZNotice_t partnotice; | |
| 866 | ZPacket_t buffer; | |
| 867 | char multi[64]; | |
| 868 | int offset, hdrsize, fragsize, ret_len, message_len, waitforack; | |
| 869 | Code_t retval; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
870 | |
| 2086 | 871 | hdrsize = len-notice->z_message_len; |
| 872 | fragsize = Z_MAXPKTLEN-hdrsize-Z_FRAGFUDGE; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
873 | |
| 2086 | 874 | offset = 0; |
| 875 | ||
| 876 | waitforack = ((notice->z_kind == UNACKED || notice->z_kind == ACKED) | |
| 877 | && !__Zephyr_server); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
878 | |
| 2086 | 879 | partnotice = *notice; |
| 880 | ||
| 881 | while (offset < notice->z_message_len || !notice->z_message_len) { | |
| 882 | (void) sprintf(multi, "%d/%d", offset, notice->z_message_len); | |
| 883 | partnotice.z_multinotice = multi; | |
| 884 | if (offset > 0) { | |
| 885 | (void) gettimeofday(&partnotice.z_uid.tv, | |
| 886 | (struct timezone *)0); | |
| 887 | partnotice.z_uid.tv.tv_sec = | |
|
7475
987384816492
[gaim-migrate @ 8088]
Mark Doliner <markdoliner@pidgin.im>
parents:
5136
diff
changeset
|
888 | htonl((unsigned long) partnotice.z_uid.tv.tv_sec); |
| 2086 | 889 | partnotice.z_uid.tv.tv_usec = |
|
7475
987384816492
[gaim-migrate @ 8088]
Mark Doliner <markdoliner@pidgin.im>
parents:
5136
diff
changeset
|
890 | htonl((unsigned long) partnotice.z_uid.tv.tv_usec); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
22104
diff
changeset
|
891 | (void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr, |
| 2419 | 892 | sizeof(__My_addr)); |
| 2086 | 893 | } |
| 894 | message_len = min(notice->z_message_len-offset, fragsize); | |
|
15018
7ce510403b58
[gaim-migrate @ 17735]
Luke Schierer <lschiere@pidgin.im>
parents:
14254
diff
changeset
|
895 | partnotice.z_message = (char*)notice->z_message+offset; |
| 2086 | 896 | partnotice.z_message_len = message_len; |
| 897 | if ((retval = Z_FormatAuthHeader(&partnotice, buffer, Z_MAXHEADERLEN, | |
| 898 | &ret_len, cert_func)) != ZERR_NONE) { | |
| 899 | return (retval); | |
| 900 | } | |
| 901 | memcpy(buffer + ret_len, partnotice.z_message, message_len); | |
| 902 | if ((retval = (*send_func)(&partnotice, buffer, ret_len+message_len, | |
| 903 | waitforack)) != ZERR_NONE) { | |
| 904 | return (retval); | |
| 905 | } | |
| 906 | offset += fragsize; | |
| 907 | if (!notice->z_message_len) | |
| 908 | break; | |
| 909 | } | |
| 910 | ||
| 911 | return (ZERR_NONE); | |
| 912 | } | |
| 913 | ||
| 914 | /*ARGSUSED*/ | |
| 915 | Code_t Z_XmitFragment(notice, buf, len, wait) | |
| 916 | ZNotice_t *notice; | |
| 917 | char *buf; | |
| 918 | int len; | |
| 919 | int wait; | |
| 920 | { | |
| 921 | return(ZSendPacket(buf, len, wait)); | |
| 922 | } | |
| 923 | ||
| 924 | #ifdef Z_DEBUG | |
| 925 | /* For debugging printing */ | |
| 926 | const char *const ZNoticeKinds[] = { | |
| 927 | "UNSAFE", "UNACKED", "ACKED", "HMACK", "HMCTL", "SERVACK", "SERVNAK", | |
| 928 | "CLIENTACK", "STAT" | |
| 929 | }; | |
| 930 | #endif | |
| 931 | ||
| 932 | #ifdef Z_DEBUG | |
| 933 | ||
| 934 | #undef Z_debug | |
| 935 | #ifdef HAVE_STDARG_H | |
| 936 | void Z_debug (const char *format, ...) | |
| 937 | { | |
| 938 | va_list pvar; | |
| 939 | if (!__Z_debug_print) | |
| 940 | return; | |
| 941 | va_start (pvar, format); | |
| 942 | (*__Z_debug_print) (format, pvar, __Z_debug_print_closure); | |
| 943 | va_end (pvar); | |
| 944 | } | |
| 945 | #else /* stdarg */ | |
| 946 | void Z_debug (va_alist) va_dcl | |
| 947 | { | |
| 948 | va_list pvar; | |
| 949 | char *format; | |
| 950 | if (!__Z_debug_print) | |
| 951 | return; | |
| 952 | va_start (pvar); | |
| 953 | format = va_arg (pvar, char *); | |
| 954 | (*__Z_debug_print) (format, pvar, __Z_debug_print_closure); | |
| 955 | va_end (pvar); | |
| 956 | } | |
| 957 | #endif | |
| 958 | ||
| 959 | void Z_debug_stderr (format, args, closure) | |
| 960 | const char *format; | |
| 961 | va_list args; | |
| 962 | void *closure; | |
| 963 | { | |
| 964 | #ifdef HAVE_VPRINTF | |
| 965 | vfprintf (stderr, format, args); | |
| 966 | #else | |
| 967 | _doprnt (format, args, stderr); | |
| 968 | #endif | |
| 969 | putc ('\n', stderr); | |
| 970 | } | |
| 971 | ||
| 972 | #undef ZGetFD | |
| 973 | int ZGetFD () { return __Zephyr_fd; } | |
| 974 | ||
| 975 | #undef ZQLength | |
| 976 | int ZQLength () { return __Q_CompleteLength; } | |
| 977 | ||
| 978 | #undef ZGetDestAddr | |
| 979 | struct sockaddr_in ZGetDestAddr () { return __HM_addr; } | |
| 980 | ||
| 981 | #undef ZGetRealm | |
| 982 | Zconst char * ZGetRealm () { return __Zephyr_realm; } | |
| 983 | ||
| 984 | #undef ZSetDebug | |
| 985 | void ZSetDebug(proc, arg) | |
| 986 | void (*proc) __P((const char *, va_list, void *)); | |
| 987 | char *arg; | |
| 988 | { | |
| 989 | __Z_debug_print = proc; | |
| 990 | __Z_debug_print_closure = arg; | |
| 991 | } | |
| 992 | #endif /* Z_DEBUG */ | |
| 993 |