1 /* include loop1 */
2 #include	"sntp.h"
3 
4 /* *INDENT-OFF* */
5 static int		 check_loop(struct sockaddr *, socklen_t);
6 static int		 check_dup(socklen_t);
7 
8 static char		 buf1[MAXLINE], buf2[MAXLINE];
9 static char		*buf[2] = { buf1, buf2 };
10 struct sockaddr	*from[2];
11 static size_t	 nread[2] = { -1, -1 };
12 static int		 currb = 0, lastb = 1;
13 /* *INDENT-ON* */
14 
15 void
read_loop(void)16 read_loop(void)
17 {
18 	int				nsel, maxfd;
19 	Addrs			*aptr;
20 	fd_set			rset, allrset;
21 	socklen_t		len;
22 	struct timeval	now;
23 
24 		/* 4allocate two socket address structures */
25 	from[0] = Malloc(addrs[0].addr_salen);
26 	from[1] = Malloc(addrs[0].addr_salen);
27 
28 	maxfd = -1;
29 	for (aptr = &addrs[0]; aptr < &addrs[naddrs]; aptr++) {
30 		FD_SET(aptr->addr_fd, &allrset);
31 		if (aptr->addr_fd > maxfd)
32 			maxfd = aptr->addr_fd;
33 	}
34 /* end loop1 */
35 
36 /* include loop2 */
37 	for ( ; ; ) {
38 		rset = allrset;
39 		nsel = Select(maxfd+1, &rset, NULL, NULL, NULL);
40 
41 		Gettimeofday(&now, NULL);	/* get time when select returns */
42 
43 		for (aptr = &addrs[0]; aptr < &addrs[naddrs]; aptr++) {
44 			if (FD_ISSET(aptr->addr_fd, &rset)) {
45 				len = aptr->addr_salen;
46 				nread[currb] = recvfrom(aptr->addr_fd,
47 										buf[currb], MAXLINE, 0,
48 										from[currb], &len);
49 				if (aptr->addr_flags & ADDR_MCAST) {
50 					printf("%d bytes from %s", nread[currb],
51 							Sock_ntop(from[currb], aptr->addr_salen));
52 					printf(" multicast to %s", aptr->addr_ifname);
53 
54 				} else if (aptr->addr_flags & ADDR_BCAST) {
55 					printf("%d bytes from %s", nread[currb],
56 							Sock_ntop(from[currb], aptr->addr_salen));
57 					printf(" broadcast to %s",
58 							Sock_ntop(aptr->addr_sa, aptr->addr_salen));
59 
60 				} else {
61 					printf("%d bytes from %s", nread[currb],
62 							Sock_ntop(from[currb], aptr->addr_salen));
63 					printf(" to %s",
64 							Sock_ntop(aptr->addr_sa, aptr->addr_salen));
65 				}
66 
67 				if (check_loop(from[currb], aptr->addr_salen)) {
68 					printf(" (ignored)\n");
69 					continue;		/* it's one of ours, looped back */
70 				}
71 				if (check_dup(aptr->addr_salen)) {
72 					printf(" (dup)\n");
73 					continue;		/* it's a duplicate */
74 				}
75 
76 				sntp_proc(buf[lastb], nread[lastb], &now);
77 
78 				if (--nsel <= 0)
79 					break;		/* all done with selectable descriptors */
80 			}
81 		}
82 	}
83 }
84 /* end loop2 */
85 
86 /* include check_loop */
87 int
check_loop(struct sockaddr * sa,socklen_t salen)88 check_loop(struct sockaddr *sa, socklen_t salen)
89 {
90 	Addrs	*aptr;
91 
92 	for (aptr = &addrs[0]; aptr < &addrs[naddrs]; aptr++) {
93 		if (sock_cmp_addr(sa, aptr->addr_sa, salen) == 0)
94 			return(1);		/* it is one of our addresses */
95 	}
96 	return(0);
97 }
98 /* end check_loop */
99 
100 /* include check_dup */
101 int
check_dup(socklen_t salen)102 check_dup(socklen_t salen)
103 {
104 	int		temp;
105 
106 	if (nread[currb] == nread[lastb] &&
107 		memcmp(from[currb], from[lastb], salen) == 0 &&
108 		memcmp(buf[currb], buf[lastb], nread[currb]) == 0) {
109 			return(1);	/* it is a duplicate */
110 	}
111 
112 	temp = currb;	/* swap currb and lastb */
113 	currb = lastb;
114 	lastb = temp;
115 	return(0);
116 }
117 /* end check_dup */
118