]>
git.saurik.com Git - apple/network_cmds.git/blob - timed.tproj/timed.tproj/readmsg.c
88efb49e4f271ddbb6bc827972a5c3c943b8ae92
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1985, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 static char sccsid
[] = "@(#)readmsg.c 8.1 (Berkeley) 6/6/93";
62 #ident "$Revision: 1.2 $"
67 extern char *tsptype
[];
70 * LOOKAT checks if the message is of the requested type and comes from
71 * the right machine, returning 1 in case of affirmative answer
73 #define LOOKAT(msg, mtype, mfrom, netp, froms) \
74 (((mtype) == TSP_ANY || (mtype) == (msg).tsp_type) && \
75 ((mfrom) == 0 || !strcmp((mfrom), (msg).tsp_name)) && \
77 ((netp)->mask & (froms).sin_addr.s_addr) == (netp)->net.s_addr))
79 struct timeval rtime
, rwait
, rtout
;
81 static struct tsplist
{
84 struct sockaddr_in addr
;
87 struct sockaddr_in from
;
88 struct netinfo
*fromnet
;
89 struct timeval from_when
;
92 * `readmsg' returns message `type' sent by `machfrom' if it finds it
93 * either in the receive queue, or in a linked list of previously received
94 * messages that it maintains.
95 * Otherwise it waits to see if the appropriate message arrives within
96 * `intvl' seconds. If not, it returns NULL.
100 readmsg(type
, machfrom
, intvl
, netfrom
)
103 struct timeval
*intvl
;
104 struct netinfo
*netfrom
;
108 static struct tsplist
*head
= &msgslist
;
109 static struct tsplist
*tail
= &msgslist
;
110 static int msgcnt
= 0;
111 struct tsplist
*prev
;
112 register struct netinfo
*ntp
;
113 register struct tsplist
*ptr
;
117 fprintf(fd
, "readmsg: looking for %s from %s, %s\n",
118 tsptype
[type
], machfrom
== NULL
? "ANY" : machfrom
,
119 netfrom
== NULL
? "ANYNET" : inet_ntoa(netfrom
->net
));
122 for (ptr
= head
->p
; ptr
!= 0; ptr
= ptr
->p
) {
123 /* do not repeat the hundreds of messages */
126 fprintf(fd
,"\t ...%d skipped\n",
132 fprintf(fd
, length
> 1 ? "\t" : "queue:\t");
133 print(&ptr
->info
, &ptr
->addr
);
142 * Look for the requested message scanning through the
143 * linked list. If found, return it and free the space
146 while (ptr
!= NULL
) {
147 if (LOOKAT(ptr
->info
, type
, machfrom
, netfrom
, ptr
->addr
)) {
151 from_when
= ptr
->when
;
158 for (ntp
= nettab
; ntp
!= NULL
; ntp
= ntp
->next
) {
159 if ((ntp
->mask
& from
.sin_addr
.s_addr
) ==
168 fprintf(fd
, "readmsg: found ");
169 print(&msgin
, &from
);
172 /* The protocol can get far behind. When it does, it gets
173 * hopelessly confused. So delete duplicate messages.
175 for (ptr
= prev
; (ptr
= ptr
->p
) != NULL
; prev
= ptr
) {
176 if (ptr
->addr
.sin_addr
.s_addr
177 == from
.sin_addr
.s_addr
178 && ptr
->info
.tsp_type
== msgin
.tsp_type
) {
180 fprintf(fd
, "\tdup ");
193 * If the message was not in the linked list, it may still be
194 * coming from the network. Set the timer and wait
195 * on a select to read the next incoming message: if it is the
196 * right one, return it, otherwise insert it in the linked list.
199 (void)gettimeofday(&rtout
, 0);
200 timevaladd(&rtout
, intvl
);
203 (void)gettimeofday(&rtime
, 0);
204 timevalsub(&rwait
, &rtout
, &rtime
);
205 if (rwait
.tv_sec
< 0)
206 rwait
.tv_sec
= rwait
.tv_usec
= 0;
207 else if (rwait
.tv_sec
== 0
208 && rwait
.tv_usec
< 1000000/CLK_TCK
)
209 rwait
.tv_usec
= 1000000/CLK_TCK
;
212 fprintf(fd
, "readmsg: wait %ld.%6ld at %s\n",
213 rwait
.tv_sec
, rwait
.tv_usec
, date());
214 /* Notice a full disk, as we flush trace info.
215 * It is better to flush periodically than at
216 * every line because the tracing consists of bursts
217 * of many lines. Without care, tracing slows
218 * down the code enough to break the protocol.
220 if (rwait
.tv_sec
!= 0
221 && EOF
== fflush(fd
))
222 traceoff("Tracing ended for cause at %s\n");
225 FD_SET(sock
, &ready
);
226 if (!select(sock
+1, &ready
, (fd_set
*)0, (fd_set
*)0,
228 if (rwait
.tv_sec
== 0 && rwait
.tv_usec
== 0)
232 length
= sizeof(from
);
233 if ((n
= recvfrom(sock
, (char *)&msgin
, sizeof(struct tsp
), 0,
234 (struct sockaddr
*)&from
, &length
)) < 0) {
235 syslog(LOG_ERR
, "recvfrom: %m");
238 if (n
< (ssize_t
)sizeof(struct tsp
)) {
240 "short packet (%u/%u bytes) from %s",
241 n
, sizeof(struct tsp
),
242 inet_ntoa(from
.sin_addr
));
245 (void)gettimeofday(&from_when
, (struct timezone
*)0);
246 bytehostorder(&msgin
);
248 if (msgin
.tsp_vers
> TSPVERSION
) {
250 fprintf(fd
,"readmsg: version mismatch\n");
251 /* should do a dump of the packet */
256 if (memchr(msgin
.tsp_name
,
257 '\0', sizeof msgin
.tsp_name
) == NULL
) {
258 syslog(LOG_NOTICE
, "hostname field not NUL terminated "
259 "in packet from %s", inet_ntoa(from
.sin_addr
));
264 for (ntp
= nettab
; ntp
!= NULL
; ntp
= ntp
->next
)
265 if ((ntp
->mask
& from
.sin_addr
.s_addr
) ==
272 * drop packets from nets we are ignoring permanently
274 if (fromnet
== NULL
) {
276 * The following messages may originate on
277 * this host with an ignored network address
279 if (msgin
.tsp_type
!= TSP_TRACEON
&&
280 msgin
.tsp_type
!= TSP_SETDATE
&&
281 msgin
.tsp_type
!= TSP_MSITE
&&
282 msgin
.tsp_type
!= TSP_TEST
&&
283 msgin
.tsp_type
!= TSP_TRACEOFF
) {
285 fprintf(fd
,"readmsg: discard null net ");
286 print(&msgin
, &from
);
293 * Throw away messages coming from this machine,
294 * unless they are of some particular type.
295 * This gets rid of broadcast messages and reduces
296 * master processing time.
298 if (!strcmp(msgin
.tsp_name
, hostname
)
299 && msgin
.tsp_type
!= TSP_SETDATE
300 && msgin
.tsp_type
!= TSP_TEST
301 && msgin
.tsp_type
!= TSP_MSITE
302 && msgin
.tsp_type
!= TSP_TRACEON
303 && msgin
.tsp_type
!= TSP_TRACEOFF
304 && msgin
.tsp_type
!= TSP_LOOP
) {
306 fprintf(fd
, "readmsg: discard own ");
307 print(&msgin
, &from
);
313 * Send acknowledgements here; this is faster and
314 * avoids deadlocks that would occur if acks were
315 * sent from a higher level routine. Different
316 * acknowledgements are necessary, depending on
319 if (fromnet
== NULL
) /* do not de-reference 0 */
321 else if (fromnet
->status
== MASTER
)
323 else if (fromnet
->status
== SLAVE
)
328 if (LOOKAT(msgin
, type
, machfrom
, netfrom
, from
)) {
330 fprintf(fd
, "readmsg: ");
331 print(&msgin
, &from
);
334 } else if (++msgcnt
> NHOSTS
*3) {
336 /* The protocol gets hopelessly confused if it gets too far
337 * behind. However, it seems able to recover from all cases of lost
338 * packets. Therefore, if we are swamped, throw everything away.
342 "readmsg: discarding %d msgs\n",
345 while ((ptr
=head
->p
) != NULL
) {
351 tail
->p
= (struct tsplist
*)
352 malloc(sizeof(struct tsplist
));
357 /* timestamp msgs so SETTIMEs are correct */
358 tail
->when
= from_when
;
364 * Send the necessary acknowledgements:
365 * only the type ACK is to be sent by a slave
370 switch(msgin
.tsp_type
) {
380 fprintf(fd
, "Slaveack: ");
381 print(&msgin
, &from
);
383 xmit(TSP_ACK
,msgin
.tsp_seq
, &from
);
388 fprintf(fd
, "Slaveack: no ack: ");
389 print(&msgin
, &from
);
396 * Certain packets may arrive from this machine on ignored networks.
397 * These packets should be acknowledged.
402 switch(msgin
.tsp_type
) {
408 fprintf(fd
, "Ignoreack: ");
409 print(&msgin
, &from
);
411 xmit(TSP_ACK
,msgin
.tsp_seq
, &from
);
416 fprintf(fd
, "Ignoreack: no ack: ");
417 print(&msgin
, &from
);
424 * `masterack' sends the necessary acknowledgments
425 * to the messages received by a master
433 resp
.tsp_vers
= TSPVERSION
;
434 (void)strcpy(resp
.tsp_name
, hostname
);
436 switch(msgin
.tsp_type
) {
443 fprintf(fd
, "Masterack: ");
444 print(&msgin
, &from
);
446 xmit(TSP_ACK
,msgin
.tsp_seq
, &from
);
452 fprintf(fd
, "Masterack: ");
453 print(&msgin
, &from
);
455 xmit(TSP_MASTERACK
,msgin
.tsp_seq
, &from
);
460 fprintf(fd
,"Masterack: no ack: ");
461 print(&msgin
, &from
);
468 * Print a TSP message
473 struct sockaddr_in
*addr
;
477 if (msg
->tsp_type
>= TSPTYPENUMBER
) {
478 fprintf(fd
, "bad type (%u) on packet from %s\n",
479 msg
->tsp_type
, inet_ntoa(addr
->sin_addr
));
483 switch (msg
->tsp_type
) {
486 fprintf(fd
, "%s %d %-6u #%d %-15s %s\n",
487 tsptype
[msg
->tsp_type
],
491 inet_ntoa(addr
->sin_addr
),
499 (void)cftime(tm
, "%D %T", &msg
->tsp_time
.tv_sec
);
501 strncpy(tm
, ctime(&msg
->tsp_time
.tv_sec
)+3+1, sizeof(tm
));
502 tm
[15] = '\0'; /* ugh */
504 fprintf(fd
, "%s %d %-6u %s %-15s %s\n",
505 tsptype
[msg
->tsp_type
],
509 inet_ntoa(addr
->sin_addr
),
514 fprintf(fd
, "%s %d %-6u (%ld,%ld) %-15s %s\n",
515 tsptype
[msg
->tsp_type
],
518 msg
->tsp_time
.tv_sec
,
519 msg
->tsp_time
.tv_usec
,
520 inet_ntoa(addr
->sin_addr
),
525 fprintf(fd
, "%s %d %-6u %-15s %s\n",
526 tsptype
[msg
->tsp_type
],
529 inet_ntoa(addr
->sin_addr
),