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