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