Mercurial > pidgin
annotate src/protocols/zephyr/Zinternal.c @ 11173:91ca67258564
[gaim-migrate @ 13278]
The DBus plugin example I forgot to upload last time.
committer: Tailor Script <tailor@pidgin.im>
| author | Piotr Zielinski <zielaj> |
|---|---|
| date | Sat, 30 Jul 2005 16:34:18 +0000 |
| parents | 64895571248f |
| children | 519dc2186438 |
| rev | line source |
|---|---|
| 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
43d6c08d7e96
[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 extern char *inet_ntoa (); | |
| 37 | |
| 38 int __Zephyr_fd = -1; | |
| 39 int __Zephyr_open; | |
| 40 int __Zephyr_port = -1; | |
|
2419
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
41 struct in_addr __My_addr; |
| 2086 | 42 int __Q_CompleteLength; |
| 43 int __Q_Size; | |
| 44 struct _Z_InputQ *__Q_Head, *__Q_Tail; | |
| 45 struct sockaddr_in __HM_addr; | |
| 46 struct sockaddr_in __HM_addr_real; | |
| 47 int __HM_set; | |
| 48 int __Zephyr_server; | |
| 49 ZLocations_t *__locate_list; | |
| 50 int __locate_num; | |
| 51 int __locate_next; | |
| 52 ZSubscription_t *__subscriptions_list; | |
| 53 int __subscriptions_num; | |
| 54 int __subscriptions_next; | |
|
2419
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
55 int Z_discarded_packets = 0; |
| 2086 | 56 |
| 57 #ifdef ZEPHYR_USES_KERBEROS | |
| 58 C_Block __Zephyr_session; | |
|
2419
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
59 #endif |
| 2086 | 60 char __Zephyr_realm[REALM_SZ]; |
| 61 | |
| 62 #ifdef Z_DEBUG | |
| 63 void (*__Z_debug_print) __P((const char *fmt, va_list args, void *closure)); | |
| 64 void *__Z_debug_print_closure; | |
| 65 #endif | |
| 66 | |
| 67 #define min(a,b) ((a)<(b)?(a):(b)) | |
| 68 | |
| 5136 | 69 static int Z_AddField __P((char **ptr, const char *field, char *end)); |
| 2086 | 70 static int find_or_insert_uid __P((ZUnique_Id_t *uid, ZNotice_Kind_t kind)); |
| 71 | |
| 72 /* Find or insert uid in the old uids buffer. The buffer is a sorted | |
| 73 * circular queue. We make the assumption that most packets arrive in | |
| 74 * order, so we can usually search for a uid or insert it into the buffer | |
| 75 * by looking back just a few entries from the end. Since this code is | |
| 76 * only executed by the client, the implementation isn't microoptimized. */ | |
| 77 static int find_or_insert_uid(uid, kind) | |
| 78 ZUnique_Id_t *uid; | |
| 79 ZNotice_Kind_t kind; | |
| 80 { | |
| 81 static struct _filter { | |
| 82 ZUnique_Id_t uid; | |
| 83 ZNotice_Kind_t kind; | |
| 84 time_t t; | |
| 85 } *buffer; | |
| 86 static long size; | |
| 87 static long start; | |
| 88 static long num; | |
| 89 | |
| 90 time_t now; | |
| 91 struct _filter *new; | |
| 92 long i, j, new_size; | |
| 93 int result; | |
| 94 | |
| 95 /* Initialize the uid buffer if it hasn't been done already. */ | |
| 96 if (!buffer) { | |
| 97 size = Z_INITFILTERSIZE; | |
| 98 buffer = (struct _filter *) malloc(size * sizeof(*buffer)); | |
| 99 if (!buffer) | |
| 100 return 0; | |
| 101 } | |
| 102 | |
| 103 /* Age the uid buffer, discarding any uids older than the clock skew. */ | |
| 104 time(&now); | |
| 105 while (num && (now - buffer[start % size].t) > CLOCK_SKEW) | |
| 106 start++, num--; | |
| 107 start %= size; | |
| 108 | |
| 109 /* Make room for a new uid, since we'll probably have to insert one. */ | |
| 110 if (num == size) { | |
| 111 new_size = size * 2 + 2; | |
| 112 new = (struct _filter *) malloc(new_size * sizeof(*new)); | |
| 113 if (!new) | |
| 114 return 0; | |
| 115 for (i = 0; i < num; i++) | |
| 116 new[i] = buffer[(start + i) % size]; | |
| 117 free(buffer); | |
| 118 buffer = new; | |
| 119 size = new_size; | |
| 120 start = 0; | |
| 121 } | |
| 122 | |
| 123 /* Search for this uid in the buffer, starting from the end. */ | |
| 124 for (i = start + num - 1; i >= start; i--) { | |
| 125 result = memcmp(uid, &buffer[i % size].uid, sizeof(*uid)); | |
| 126 if (result == 0 && buffer[i % size].kind == kind) | |
| 127 return 1; | |
| 128 if (result > 0) | |
| 129 break; | |
| 130 } | |
| 131 | |
| 132 /* We didn't find it; insert the uid into the buffer after i. */ | |
| 133 i++; | |
| 134 for (j = start + num; j > i; j--) | |
| 135 buffer[j % size] = buffer[(j - 1) % size]; | |
| 136 buffer[i % size].uid = *uid; | |
| 137 buffer[i % size].kind = kind; | |
| 138 buffer[i % size].t = now; | |
| 139 num++; | |
| 140 | |
| 141 return 0; | |
| 142 } | |
| 143 | |
| 144 | |
| 145 /* Return 1 if there is a packet waiting, 0 otherwise */ | |
| 146 | |
| 147 int Z_PacketWaiting() | |
| 148 { | |
| 149 struct timeval tv; | |
| 150 fd_set read; | |
| 151 | |
| 152 tv.tv_sec = tv.tv_usec = 0; | |
| 153 FD_ZERO(&read); | |
| 154 FD_SET(ZGetFD(), &read); | |
| 155 return (select(ZGetFD() + 1, &read, NULL, NULL, &tv)); | |
| 156 } | |
| 157 | |
| 158 | |
| 159 /* Wait for a complete notice to become available */ | |
| 160 | |
| 161 Code_t Z_WaitForComplete() | |
| 162 { | |
| 163 Code_t retval; | |
| 164 | |
| 165 if (__Q_CompleteLength) | |
| 166 return (Z_ReadEnqueue()); | |
| 167 | |
| 168 while (!__Q_CompleteLength) | |
| 169 if ((retval = Z_ReadWait()) != ZERR_NONE) | |
| 170 return (retval); | |
| 171 | |
| 172 return (ZERR_NONE); | |
| 173 } | |
| 174 | |
| 175 | |
| 176 /* Read any available packets and enqueue them */ | |
| 177 | |
| 178 Code_t Z_ReadEnqueue() | |
| 179 { | |
| 180 Code_t retval; | |
| 181 | |
| 182 if (ZGetFD() < 0) | |
| 183 return (ZERR_NOPORT); | |
| 184 | |
| 185 while (Z_PacketWaiting()) | |
| 186 if ((retval = Z_ReadWait()) != ZERR_NONE) | |
| 187 return (retval); | |
| 188 | |
| 189 return (ZERR_NONE); | |
| 190 } | |
| 191 | |
| 192 | |
| 193 /* | |
| 194 * Search the queue for a notice with the proper multiuid - remove any | |
| 195 * notices that haven't been touched in a while | |
| 196 */ | |
| 197 | |
| 198 struct _Z_InputQ *Z_SearchQueue(uid, kind) | |
| 199 ZUnique_Id_t *uid; | |
| 200 ZNotice_Kind_t kind; | |
| 201 { | |
| 202 register struct _Z_InputQ *qptr; | |
| 203 struct _Z_InputQ *next; | |
| 204 struct timeval tv; | |
| 205 | |
| 206 (void) gettimeofday(&tv, (struct timezone *)0); | |
| 207 | |
| 208 qptr = __Q_Head; | |
| 209 | |
| 210 while (qptr) { | |
| 211 if (ZCompareUID(uid, &qptr->uid) && qptr->kind == kind) | |
| 212 return (qptr); | |
| 213 next = qptr->next; | |
| 214 if (qptr->timep && (qptr->timep+Z_NOTICETIMELIMIT < tv.tv_sec)) | |
| 215 Z_RemQueue(qptr); | |
| 216 qptr = next; | |
| 217 } | |
| 218 return (NULL); | |
| 219 } | |
| 220 | |
| 221 /* | |
| 222 * Now we delve into really convoluted queue handling and | |
| 223 * fragmentation reassembly algorithms and other stuff you probably | |
| 224 * don't want to look at... | |
| 225 * | |
| 226 * This routine does NOT guarantee a complete packet will be ready when it | |
| 227 * returns. | |
| 228 */ | |
| 229 | |
| 230 Code_t Z_ReadWait() | |
| 231 { | |
| 232 register struct _Z_InputQ *qptr; | |
| 233 ZNotice_t notice; | |
| 234 ZPacket_t packet; | |
| 235 struct sockaddr_in olddest, from; | |
|
2419
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
236 int from_len, packet_len, zvlen, part, partof; |
| 2086 | 237 char *slash; |
| 238 Code_t retval; | |
|
2419
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
239 fd_set fds; |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
240 struct timeval tv; |
| 2086 | 241 |
| 242 if (ZGetFD() < 0) | |
| 243 return (ZERR_NOPORT); | |
| 244 | |
|
2419
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
245 FD_ZERO(&fds); |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
246 FD_SET(ZGetFD(), &fds); |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
247 tv.tv_sec = 60; |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
248 tv.tv_usec = 0; |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
249 |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
250 if (select(ZGetFD() + 1, &fds, NULL, NULL, &tv) < 0) |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
251 return (errno); |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
252 if (!FD_ISSET(ZGetFD(), &fds)) |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
253 return ETIMEDOUT; |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
254 |
| 2086 | 255 from_len = sizeof(struct sockaddr_in); |
| 256 | |
| 257 packet_len = recvfrom(ZGetFD(), packet, sizeof(packet), 0, | |
| 258 (struct sockaddr *)&from, &from_len); | |
| 259 | |
| 260 if (packet_len < 0) | |
| 261 return (errno); | |
| 262 | |
| 263 if (!packet_len) | |
| 264 return (ZERR_EOF); | |
| 265 | |
|
2419
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
266 /* Ignore obviously non-Zephyr packets. */ |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
267 zvlen = sizeof(ZVERSIONHDR) - 1; |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
268 if (packet_len < zvlen || memcmp(packet, ZVERSIONHDR, zvlen) != 0) { |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
269 Z_discarded_packets++; |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
270 return (ZERR_NONE); |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
271 } |
| 2086 | 272 |
| 273 /* Parse the notice */ | |
| 274 if ((retval = ZParseNotice(packet, packet_len, ¬ice)) != ZERR_NONE) | |
| 275 return (retval); | |
| 276 | |
| 277 /* | |
| 278 * If we're not a server and the notice is of an appropriate kind, | |
| 279 * send back a CLIENTACK to whoever sent it to say we got it. | |
| 280 */ | |
| 281 if (!__Zephyr_server) { | |
| 282 if (notice.z_kind != HMACK && notice.z_kind != SERVACK && | |
| 283 notice.z_kind != SERVNAK && notice.z_kind != CLIENTACK) { | |
| 284 ZNotice_t tmpnotice; | |
| 285 ZPacket_t pkt; | |
| 286 int len; | |
| 287 | |
| 288 tmpnotice = notice; | |
| 289 tmpnotice.z_kind = CLIENTACK; | |
| 290 tmpnotice.z_message_len = 0; | |
| 291 olddest = __HM_addr; | |
| 292 __HM_addr = from; | |
| 293 if ((retval = ZFormatSmallRawNotice(&tmpnotice, pkt, &len)) | |
| 294 != ZERR_NONE) | |
| 295 return(retval); | |
| 296 if ((retval = ZSendPacket(pkt, len, 0)) != ZERR_NONE) | |
| 297 return (retval); | |
| 298 __HM_addr = olddest; | |
| 299 } | |
| 300 if (find_or_insert_uid(¬ice.z_uid, notice.z_kind)) | |
| 301 return(ZERR_NONE); | |
| 302 | |
| 303 /* Check authentication on the notice. */ | |
| 304 notice.z_checked_auth = ZCheckAuthentication(¬ice, &from); | |
| 305 } | |
| 306 | |
| 307 | |
| 308 /* | |
| 309 * Parse apart the z_multinotice field - if the field is blank for | |
| 310 * some reason, assume this packet stands by itself. | |
| 311 */ | |
| 312 slash = strchr(notice.z_multinotice, '/'); | |
| 313 if (slash) { | |
| 314 part = atoi(notice.z_multinotice); | |
| 315 partof = atoi(slash+1); | |
| 316 if (part > partof || partof == 0) { | |
| 317 part = 0; | |
| 318 partof = notice.z_message_len; | |
| 319 } | |
| 320 } | |
| 321 else { | |
| 322 part = 0; | |
| 323 partof = notice.z_message_len; | |
| 324 } | |
| 325 | |
| 326 /* Too big a packet...just ignore it! */ | |
| 327 if (partof > Z_MAXNOTICESIZE) | |
| 328 return (ZERR_NONE); | |
| 329 | |
| 330 /* | |
| 331 * If we aren't a server and we can find a notice in the queue | |
| 332 * with the same multiuid field, insert the current fragment as | |
| 333 * appropriate. | |
| 334 */ | |
| 335 switch (notice.z_kind) { | |
| 336 case SERVACK: | |
| 337 case SERVNAK: | |
| 338 /* The SERVACK and SERVNAK replies shouldn't be reassembled | |
| 339 (they have no parts). Instead, we should hold on to the reply | |
| 340 ONLY if it's the first part of a fragmented message, i.e. | |
| 341 multi_uid == uid. This allows programs to wait for the uid | |
| 342 of the first packet, and get a response when that notice | |
| 343 arrives. Acknowledgements of the other fragments are discarded | |
| 344 (XXX we assume here that they all carry the same information | |
| 345 regarding failure/success) | |
| 346 */ | |
| 347 if (!__Zephyr_server && | |
| 348 !ZCompareUID(¬ice.z_multiuid, ¬ice.z_uid)) | |
| 349 /* they're not the same... throw away this packet. */ | |
| 350 return(ZERR_NONE); | |
| 351 /* fall thru & process it */ | |
| 352 default: | |
| 353 /* for HMACK types, we assume no packet loss (local loopback | |
| 354 connections). The other types can be fragmented and MUST | |
| 355 run through this code. */ | |
| 356 if (!__Zephyr_server && (qptr = Z_SearchQueue(¬ice.z_multiuid, | |
| 357 notice.z_kind))) { | |
| 358 /* | |
| 359 * If this is the first fragment, and we haven't already | |
| 360 * gotten a first fragment, grab the header from it. | |
| 361 */ | |
| 362 if (part == 0 && !qptr->header) { | |
| 363 qptr->header_len = packet_len-notice.z_message_len; | |
| 364 qptr->header = (char *) malloc((unsigned) qptr->header_len); | |
| 365 if (!qptr->header) | |
| 366 return (ENOMEM); | |
| 367 (void) memcpy(qptr->header, packet, qptr->header_len); | |
| 368 } | |
| 369 return (Z_AddNoticeToEntry(qptr, ¬ice, part)); | |
| 370 } | |
| 371 } | |
| 372 | |
| 373 /* | |
| 374 * We'll have to create a new entry...make sure the queue isn't | |
| 375 * going to get too big. | |
| 376 */ | |
| 377 if (__Q_Size+(__Zephyr_server ? notice.z_message_len : partof) > Z_MAXQUEUESIZE) | |
| 378 return (ZERR_NONE); | |
| 379 | |
| 380 /* | |
| 381 * This is a notice we haven't heard of, so create a new queue | |
| 382 * entry for it and zero it out. | |
| 383 */ | |
| 384 qptr = (struct _Z_InputQ *)malloc(sizeof(struct _Z_InputQ)); | |
| 385 if (!qptr) | |
| 386 return (ENOMEM); | |
| 387 (void) memset((char *)qptr, 0, sizeof(struct _Z_InputQ)); | |
| 388 | |
| 389 /* Insert the entry at the end of the queue */ | |
| 390 qptr->next = NULL; | |
| 391 qptr->prev = __Q_Tail; | |
| 392 if (__Q_Tail) | |
| 393 __Q_Tail->next = qptr; | |
| 394 __Q_Tail = qptr; | |
| 395 | |
| 396 if (!__Q_Head) | |
| 397 __Q_Head = qptr; | |
| 398 | |
| 399 | |
| 400 /* Copy the from field, multiuid, kind, and checked authentication. */ | |
| 401 qptr->from = from; | |
| 402 qptr->uid = notice.z_multiuid; | |
| 403 qptr->kind = notice.z_kind; | |
| 404 qptr->auth = notice.z_checked_auth; | |
| 405 | |
| 406 /* | |
| 407 * If this is the first part of the notice, we take the header | |
| 408 * from it. We only take it if this is the first fragment so that | |
| 409 * the Unique ID's will be predictable. | |
| 410 * | |
| 411 * If a Zephyr Server, we always take the header. | |
| 412 */ | |
| 413 if (__Zephyr_server || part == 0) { | |
| 414 qptr->header_len = packet_len-notice.z_message_len; | |
| 415 qptr->header = (char *) malloc((unsigned) qptr->header_len); | |
| 416 if (!qptr->header) | |
| 417 return ENOMEM; | |
| 418 (void) memcpy(qptr->header, packet, qptr->header_len); | |
| 419 } | |
| 420 | |
| 421 /* | |
| 422 * If this is not a fragmented notice, then don't bother with a | |
| 423 * hole list. | |
| 424 * If we are a Zephyr server, all notices are treated as complete. | |
| 425 */ | |
| 426 if (__Zephyr_server || (part == 0 && notice.z_message_len == partof)) { | |
| 427 __Q_CompleteLength++; | |
| 428 qptr->holelist = (struct _Z_Hole *) 0; | |
| 429 qptr->complete = 1; | |
| 430 /* allocate a msg buf for this piece */ | |
| 431 if (notice.z_message_len == 0) | |
| 432 qptr->msg = 0; | |
| 433 else if (!(qptr->msg = (char *) malloc((unsigned) notice.z_message_len))) | |
| 434 return(ENOMEM); | |
| 435 else | |
| 436 (void) memcpy(qptr->msg, notice.z_message, notice.z_message_len); | |
| 437 qptr->msg_len = notice.z_message_len; | |
| 438 __Q_Size += notice.z_message_len; | |
| 439 qptr->packet_len = qptr->header_len+qptr->msg_len; | |
| 440 if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len))) | |
| 441 return (ENOMEM); | |
| 442 (void) memcpy(qptr->packet, qptr->header, qptr->header_len); | |
| 443 if(qptr->msg) | |
| 444 (void) memcpy(qptr->packet+qptr->header_len, qptr->msg, | |
| 445 qptr->msg_len); | |
| 446 return (ZERR_NONE); | |
| 447 } | |
| 448 | |
| 449 /* | |
| 450 * We know how long the message is going to be (this is better | |
| 451 * than IP fragmentation...), so go ahead and allocate it all. | |
| 452 */ | |
| 453 if (!(qptr->msg = (char *) malloc((unsigned) partof)) && partof) | |
| 454 return (ENOMEM); | |
| 455 qptr->msg_len = partof; | |
| 456 __Q_Size += partof; | |
| 457 | |
| 458 /* | |
| 459 * Well, it's a fragmented notice...allocate a hole list and | |
| 460 * initialize it to the full packet size. Then insert the | |
| 461 * current fragment. | |
| 462 */ | |
| 463 if (!(qptr->holelist = (struct _Z_Hole *) | |
| 464 malloc(sizeof(struct _Z_Hole)))) | |
| 465 return (ENOMEM); | |
| 466 qptr->holelist->next = (struct _Z_Hole *) 0; | |
| 467 qptr->holelist->first = 0; | |
| 468 qptr->holelist->last = partof-1; | |
| 469 return (Z_AddNoticeToEntry(qptr, ¬ice, part)); | |
| 470 } | |
| 471 | |
| 472 | |
| 473 /* Fragment management routines - compliments, more or less, of RFC815 */ | |
| 474 | |
| 475 Code_t Z_AddNoticeToEntry(qptr, notice, part) | |
| 476 struct _Z_InputQ *qptr; | |
| 477 ZNotice_t *notice; | |
| 478 int part; | |
| 479 { | |
| 480 int last, oldfirst, oldlast; | |
| 481 struct _Z_Hole *hole, *lasthole; | |
| 482 struct timeval tv; | |
| 483 | |
| 484 /* Incorporate this notice's checked authentication. */ | |
| 485 if (notice->z_checked_auth == ZAUTH_FAILED) | |
| 486 qptr->auth = ZAUTH_FAILED; | |
| 487 else if (notice->z_checked_auth == ZAUTH_NO && qptr->auth != ZAUTH_FAILED) | |
| 488 qptr->auth = ZAUTH_NO; | |
| 489 | |
| 490 (void) gettimeofday(&tv, (struct timezone *)0); | |
| 491 qptr->timep = tv.tv_sec; | |
| 492 | |
| 493 last = part+notice->z_message_len-1; | |
| 494 | |
| 495 hole = qptr->holelist; | |
| 496 lasthole = (struct _Z_Hole *) 0; | |
| 497 | |
| 498 /* copy in the message body */ | |
| 499 (void) memcpy(qptr->msg+part, notice->z_message, notice->z_message_len); | |
| 500 | |
| 501 /* Search for a hole that overlaps with the current fragment */ | |
| 502 while (hole) { | |
| 503 if (part <= hole->last && last >= hole->first) | |
| 504 break; | |
| 505 lasthole = hole; | |
| 506 hole = hole->next; | |
| 507 } | |
| 508 | |
| 509 /* If we found one, delete it and reconstruct a new hole */ | |
| 510 if (hole) { | |
| 511 oldfirst = hole->first; | |
| 512 oldlast = hole->last; | |
| 513 if (lasthole) | |
| 514 lasthole->next = hole->next; | |
| 515 else | |
| 516 qptr->holelist = hole->next; | |
| 517 free((char *)hole); | |
| 518 /* | |
| 519 * Now create a new hole that is the original hole without the | |
| 520 * current fragment. | |
| 521 */ | |
| 522 if (part > oldfirst) { | |
| 523 /* Search for the end of the hole list */ | |
| 524 hole = qptr->holelist; | |
| 525 lasthole = (struct _Z_Hole *) 0; | |
| 526 while (hole) { | |
| 527 lasthole = hole; | |
| 528 hole = hole->next; | |
| 529 } | |
| 530 if (lasthole) { | |
| 531 if (!(lasthole->next = (struct _Z_Hole *) | |
| 532 malloc(sizeof(struct _Z_InputQ)))) | |
| 533 return (ENOMEM); | |
| 534 hole = lasthole->next; | |
| 535 } | |
| 536 else { | |
| 537 if (!(qptr->holelist = (struct _Z_Hole *) | |
| 538 malloc(sizeof(struct _Z_InputQ)))) | |
| 539 return (ENOMEM); | |
| 540 hole = qptr->holelist; | |
| 541 } | |
| 542 hole->next = NULL; | |
| 543 hole->first = oldfirst; | |
| 544 hole->last = part-1; | |
| 545 } | |
| 546 if (last < oldlast) { | |
| 547 /* Search for the end of the hole list */ | |
| 548 hole = qptr->holelist; | |
| 549 lasthole = (struct _Z_Hole *) 0; | |
| 550 while (hole) { | |
| 551 lasthole = hole; | |
| 552 hole = hole->next; | |
| 553 } | |
| 554 if (lasthole) { | |
| 555 if (!(lasthole->next = (struct _Z_Hole *) | |
| 556 malloc(sizeof(struct _Z_InputQ)))) | |
| 557 return (ENOMEM); | |
| 558 hole = lasthole->next; | |
| 559 } | |
| 560 else { | |
| 561 if (!(qptr->holelist = (struct _Z_Hole *) | |
| 562 malloc(sizeof(struct _Z_InputQ)))) | |
| 563 return (ENOMEM); | |
| 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 } | |
| 584 | |
| 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; | |
| 598 int namelen = sizeof(name); | |
| 599 | |
| 600 if (!notice->z_sender) | |
| 601 notice->z_sender = ZGetSender(); | |
| 602 | |
| 603 if (notice->z_port == 0) { | |
| 604 if (ZGetFD() < 0) { | |
| 7475 | 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 = ""; | |
| 616 | |
| 617 (void) gettimeofday(¬ice->z_uid.tv, (struct timezone *)0); | |
| 7475 | 618 notice->z_uid.tv.tv_sec = htonl((unsigned long) notice->z_uid.tv.tv_sec); |
| 619 notice->z_uid.tv.tv_usec = htonl((unsigned long) notice->z_uid.tv.tv_usec); | |
| 2086 | 620 |
|
2419
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
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 } | |
| 648 | |
| 649 return ((*cert_routine)(notice, buffer, buffer_len, len)); | |
| 650 } | |
| 651 | |
| 652 Code_t Z_FormatRawHeader(notice, buffer, buffer_len, len, cstart, cend) | |
| 653 ZNotice_t *notice; | |
| 654 char *buffer; | |
| 655 int buffer_len; | |
| 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 | |
| 684 (void) strcpy(ptr, notice->z_version); | |
| 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 | |
| 696 if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_uid, | |
| 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
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
729 sizeof(newrecip)) |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
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); | |
| 734 } | |
| 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 | |
| 750 if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_multiuid, | |
| 751 sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN) | |
| 752 return (ZERR_HEADERLEN); | |
| 753 ptr += strlen(ptr)+1; | |
| 754 | |
| 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); | |
| 758 | |
| 759 *len = ptr-buffer; | |
| 760 | |
| 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) | |
| 774 (void) strcpy(*ptr, field); | |
| 775 else | |
| 776 **ptr = '\0'; | |
| 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; | |
| 814 | |
| 815 if (qptr->complete) | |
| 816 __Q_CompleteLength--; | |
| 817 | |
| 818 __Q_Size -= qptr->msg_len; | |
| 819 | |
| 820 if (qptr->header) | |
| 821 free(qptr->header); | |
| 822 if (qptr->msg) | |
| 823 free(qptr->msg); | |
| 824 if (qptr->packet) | |
| 825 free(qptr->packet); | |
| 826 | |
| 827 hole = qptr->holelist; | |
| 828 while (hole) { | |
| 829 nexthole = hole->next; | |
| 830 free((char *)hole); | |
| 831 hole = nexthole; | |
| 832 } | |
| 833 | |
| 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 } | |
| 840 | |
| 841 if (qptr == __Q_Head) { | |
| 842 __Q_Head = qptr->next; | |
| 843 __Q_Head->prev = (struct _Z_InputQ *)0; | |
| 844 free ((char *)qptr); | |
| 845 return; | |
| 846 } | |
| 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; | |
| 870 | |
| 871 hdrsize = len-notice->z_message_len; | |
| 872 fragsize = Z_MAXPKTLEN-hdrsize-Z_FRAGFUDGE; | |
| 873 | |
| 874 offset = 0; | |
| 875 | |
| 876 waitforack = ((notice->z_kind == UNACKED || notice->z_kind == ACKED) | |
| 877 && !__Zephyr_server); | |
| 878 | |
| 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 | 888 htonl((unsigned long) partnotice.z_uid.tv.tv_sec); |
| 2086 | 889 partnotice.z_uid.tv.tv_usec = |
| 7475 | 890 htonl((unsigned long) partnotice.z_uid.tv.tv_usec); |
|
2419
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
891 (void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr, |
|
7ba69b8e0de5
[gaim-migrate @ 2432]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
892 sizeof(__My_addr)); |
| 2086 | 893 } |
| 894 message_len = min(notice->z_message_len-offset, fragsize); | |
| 895 partnotice.z_message = notice->z_message+offset; | |
| 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 |
