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