2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)readmsg.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.sbin/timed/timed/readmsg.c,v 1.5.2.3 2001/08/31 08:02:05 kris Exp $
35 extern char *tsptype[];
38 * LOOKAT checks if the message is of the requested type and comes from
39 * the right machine, returning 1 in case of affirmative answer
41 #define LOOKAT(msg, mtype, mfrom, netp, froms) \
42 (((mtype) == TSP_ANY || (mtype) == (msg).tsp_type) && \
43 ((mfrom) == 0 || !strcmp((mfrom), (msg).tsp_name)) && \
45 ((netp)->mask & (froms).sin_addr.s_addr) == (netp)->net.s_addr))
47 struct timeval rtime, rwait, rtout;
49 static struct tsplist {
52 struct sockaddr_in addr;
55 struct sockaddr_in from;
56 struct netinfo *fromnet;
57 struct timeval from_when;
60 * `readmsg' returns message `type' sent by `machfrom' if it finds it
61 * either in the receive queue, or in a linked list of previously received
62 * messages that it maintains.
63 * Otherwise it waits to see if the appropriate message arrives within
64 * `intvl' seconds. If not, it returns NULL.
68 readmsg(int type, char *machfrom, struct timeval *intvl,
69 struct netinfo *netfrom)
73 static struct tsplist *head = &msgslist;
74 static struct tsplist *tail = &msgslist;
75 static int msgcnt = 0;
82 fprintf(fd, "readmsg: looking for %s from %s, %s\n",
83 tsptype[type], machfrom == NULL ? "ANY" : machfrom,
84 netfrom == NULL ? "ANYNET" : inet_ntoa(netfrom->net));
85 if (head->p != NULL) {
87 for (ptr = head->p; ptr != NULL; ptr = ptr->p) {
88 /* do not repeat the hundreds of messages */
91 fprintf(fd,"\t ...%d skipped\n",
97 fprintf(fd, length > 1 ? "\t" : "queue:\t");
98 print(&ptr->info, &ptr->addr);
107 * Look for the requested message scanning through the
108 * linked list. If found, return it and free the space
111 while (ptr != NULL) {
112 if (LOOKAT(ptr->info, type, machfrom, netfrom, ptr->addr)) {
116 from_when = ptr->when;
123 for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
124 if ((ntp->mask & from.sin_addr.s_addr) ==
133 fprintf(fd, "readmsg: found ");
134 print(&msgin, &from);
137 /* The protocol can get far behind. When it does, it gets
138 * hopelessly confused. So delete duplicate messages.
140 for (ptr = prev; (ptr = ptr->p) != NULL; prev = ptr) {
141 if (ptr->addr.sin_addr.s_addr
142 == from.sin_addr.s_addr
143 && ptr->info.tsp_type == msgin.tsp_type) {
145 fprintf(fd, "\tdup ");
158 * If the message was not in the linked list, it may still be
159 * coming from the network. Set the timer and wait
160 * on a select to read the next incoming message: if it is the
161 * right one, return it, otherwise insert it in the linked list.
164 gettimeofday(&rtout, 0);
165 timevaladd(&rtout, intvl);
168 gettimeofday(&rtime, 0);
169 timevalsub(&rwait, &rtout, &rtime);
170 if (rwait.tv_sec < 0)
171 rwait.tv_sec = rwait.tv_usec = 0;
172 else if (rwait.tv_sec == 0
173 && rwait.tv_usec < 1000000/CLK_TCK)
174 rwait.tv_usec = 1000000/CLK_TCK;
177 fprintf(fd, "readmsg: wait %ld.%6ld at %s\n",
178 rwait.tv_sec, rwait.tv_usec, date());
179 /* Notice a full disk, as we flush trace info.
180 * It is better to flush periodically than at
181 * every line because the tracing consists of bursts
182 * of many lines. Without care, tracing slows
183 * down the code enough to break the protocol.
185 if (rwait.tv_sec != 0
186 && EOF == fflush(fd))
187 traceoff("Tracing ended for cause at %s\n");
190 FD_SET(sock, &ready);
191 if (!select(sock+1, &ready, NULL, NULL, &rwait)) {
192 if (rwait.tv_sec == 0 && rwait.tv_usec == 0)
196 length = sizeof(from);
197 if ((n = recvfrom(sock, (char *)&msgin, sizeof(struct tsp), 0,
198 (struct sockaddr*)&from, &length)) < 0) {
199 syslog(LOG_ERR, "recvfrom: %m");
203 * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
204 * this is still OS-dependent. Demand that the packet is at
205 * least long enough to hold a 4.3BSD packet.
207 if (n < (ssize_t)(sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
209 "short packet (%zd/%zu bytes) from %s",
210 n, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
211 inet_ntoa(from.sin_addr));
214 gettimeofday(&from_when, NULL);
215 bytehostorder(&msgin);
217 if (msgin.tsp_vers > TSPVERSION) {
219 fprintf(fd,"readmsg: version mismatch\n");
220 /* should do a dump of the packet */
225 if (memchr(msgin.tsp_name,
226 '\0', sizeof msgin.tsp_name) == NULL) {
227 syslog(LOG_NOTICE, "hostname field not NUL terminated "
228 "in packet from %s", inet_ntoa(from.sin_addr));
233 for (ntp = nettab; ntp != NULL; ntp = ntp->next)
234 if ((ntp->mask & from.sin_addr.s_addr) ==
241 * drop packets from nets we are ignoring permanently
243 if (fromnet == NULL) {
245 * The following messages may originate on
246 * this host with an ignored network address
248 if (msgin.tsp_type != TSP_TRACEON &&
249 msgin.tsp_type != TSP_SETDATE &&
250 msgin.tsp_type != TSP_MSITE &&
251 msgin.tsp_type != TSP_TEST &&
252 msgin.tsp_type != TSP_TRACEOFF) {
254 fprintf(fd,"readmsg: discard null net ");
255 print(&msgin, &from);
262 * Throw away messages coming from this machine,
263 * unless they are of some particular type.
264 * This gets rid of broadcast messages and reduces
265 * master processing time.
267 if (!strcmp(msgin.tsp_name, hostname)
268 && msgin.tsp_type != TSP_SETDATE
269 && msgin.tsp_type != TSP_TEST
270 && msgin.tsp_type != TSP_MSITE
271 && msgin.tsp_type != TSP_TRACEON
272 && msgin.tsp_type != TSP_TRACEOFF
273 && msgin.tsp_type != TSP_LOOP) {
275 fprintf(fd, "readmsg: discard own ");
276 print(&msgin, &from);
282 * Send acknowledgements here; this is faster and
283 * avoids deadlocks that would occur if acks were
284 * sent from a higher level routine. Different
285 * acknowledgements are necessary, depending on
288 if (fromnet == NULL) /* do not de-reference 0 */
290 else if (fromnet->status == MASTER)
292 else if (fromnet->status == SLAVE)
297 if (LOOKAT(msgin, type, machfrom, netfrom, from)) {
299 fprintf(fd, "readmsg: ");
300 print(&msgin, &from);
303 } else if (++msgcnt > NHOSTS*3) {
305 /* The protocol gets hopelessly confused if it gets too far
306 * behind. However, it seems able to recover from all cases of lost
307 * packets. Therefore, if we are swamped, throw everything away.
311 "readmsg: discarding %d msgs\n",
314 while ((ptr=head->p) != NULL) {
320 tail->p = (struct tsplist *)
321 malloc(sizeof(struct tsplist));
326 /* timestamp msgs so SETTIMEs are correct */
327 tail->when = from_when;
333 * Send the necessary acknowledgements:
334 * only the type ACK is to be sent by a slave
340 switch(msgin.tsp_type) {
350 fprintf(fd, "Slaveack: ");
351 print(&msgin, &from);
353 xmit(TSP_ACK,msgin.tsp_seq, &from);
358 fprintf(fd, "Slaveack: no ack: ");
359 print(&msgin, &from);
366 * Certain packets may arrive from this machine on ignored networks.
367 * These packets should be acknowledged.
373 switch(msgin.tsp_type) {
379 fprintf(fd, "Ignoreack: ");
380 print(&msgin, &from);
382 xmit(TSP_ACK,msgin.tsp_seq, &from);
387 fprintf(fd, "Ignoreack: no ack: ");
388 print(&msgin, &from);
395 * `masterack' sends the necessary acknowledgments
396 * to the messages received by a master
404 resp.tsp_vers = TSPVERSION;
405 strlcpy(resp.tsp_name, hostname, sizeof(resp.tsp_name));
407 switch(msgin.tsp_type) {
414 fprintf(fd, "Masterack: ");
415 print(&msgin, &from);
417 xmit(TSP_ACK,msgin.tsp_seq, &from);
423 fprintf(fd, "Masterack: ");
424 print(&msgin, &from);
426 xmit(TSP_MASTERACK,msgin.tsp_seq, &from);
431 fprintf(fd,"Masterack: no ack: ");
432 print(&msgin, &from);
439 * Print a TSP message
442 print(struct tsp *msg, struct sockaddr_in *addr)
447 if (msg->tsp_type >= TSPTYPENUMBER) {
448 fprintf(fd, "bad type (%u) on packet from %s\n",
449 msg->tsp_type, inet_ntoa(addr->sin_addr));
453 switch (msg->tsp_type) {
456 fprintf(fd, "%s %d %-6u #%d %-15s %s\n",
457 tsptype[msg->tsp_type],
461 inet_ntoa(addr->sin_addr),
468 tsp_time_sec = msg->tsp_time.tv_sec;
469 strlcpy(tm, ctime(&tsp_time_sec)+3+1, sizeof(tm));
470 fprintf(fd, "%s %d %-6u %s %-15s %s\n",
471 tsptype[msg->tsp_type],
475 inet_ntoa(addr->sin_addr),
480 fprintf(fd, "%s %d %-6u (%ld,%ld) %-15s %s\n",
481 tsptype[msg->tsp_type],
484 msg->tsp_time.tv_sec,
485 msg->tsp_time.tv_usec,
486 inet_ntoa(addr->sin_addr),
491 fprintf(fd, "%s %d %-6u %-15s %s\n",
492 tsptype[msg->tsp_type],
495 inet_ntoa(addr->sin_addr),