forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdgclibcast6.lc
63 lines (51 loc) · 3.25 KB
/
dgclibcast6.lc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "unp.h"## 1 ##src/bcast/dgclibcast6.c##
static void recvfrom_alarm(int);## 2 ##src/bcast/dgclibcast6.c##
static int pipefd[2];## 3 ##src/bcast/dgclibcast6.c##
void## 4 ##src/bcast/dgclibcast6.c##
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)## 5 ##src/bcast/dgclibcast6.c##
{## 6 ##src/bcast/dgclibcast6.c##
int n, maxfdp1;## 7 ##src/bcast/dgclibcast6.c##
const int on = 1;## 8 ##src/bcast/dgclibcast6.c##
char sendline[MAXLINE], recvline[MAXLINE + 1];## 9 ##src/bcast/dgclibcast6.c##
fd_set rset;## 10 ##src/bcast/dgclibcast6.c##
socklen_t len;## 11 ##src/bcast/dgclibcast6.c##
struct sockaddr *preply_addr;## 12 ##src/bcast/dgclibcast6.c##
preply_addr = Malloc(servlen);## 13 ##src/bcast/dgclibcast6.c##
Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));## 14 ##src/bcast/dgclibcast6.c##
Pipe(pipefd);## 15 ##src/bcast/dgclibcast6.c##
maxfdp1 = max(sockfd, pipefd[0]) + 1;## 16 ##src/bcast/dgclibcast6.c##
FD_ZERO(&rset);## 17 ##src/bcast/dgclibcast6.c##
Signal(SIGALRM, recvfrom_alarm);## 18 ##src/bcast/dgclibcast6.c##
while (Fgets(sendline, MAXLINE, fp) != NULL) {## 19 ##src/bcast/dgclibcast6.c##
Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);## 20 ##src/bcast/dgclibcast6.c##
alarm(5);## 21 ##src/bcast/dgclibcast6.c##
for (;;) {## 22 ##src/bcast/dgclibcast6.c##
FD_SET(sockfd, &rset);## 23 ##src/bcast/dgclibcast6.c##
FD_SET(pipefd[0], &rset);## 24 ##src/bcast/dgclibcast6.c##
if ((n = select(maxfdp1, &rset, NULL, NULL, NULL)) < 0) {## 25 ##src/bcast/dgclibcast6.c##
if (errno == EINTR)## 26 ##src/bcast/dgclibcast6.c##
continue;## 27 ##src/bcast/dgclibcast6.c##
else## 28 ##src/bcast/dgclibcast6.c##
err_sys("select error");## 29 ##src/bcast/dgclibcast6.c##
}## 30 ##src/bcast/dgclibcast6.c##
if (FD_ISSET(sockfd, &rset)) {## 31 ##src/bcast/dgclibcast6.c##
len = servlen;## 32 ##src/bcast/dgclibcast6.c##
n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr,## 33 ##src/bcast/dgclibcast6.c##
&len);## 34 ##src/bcast/dgclibcast6.c##
recvline[n] = 0; /* null terminate */## 35 ##src/bcast/dgclibcast6.c##
printf("from %s: %s",## 36 ##src/bcast/dgclibcast6.c##
Sock_ntop_host(preply_addr, len), recvline);## 37 ##src/bcast/dgclibcast6.c##
}## 38 ##src/bcast/dgclibcast6.c##
if (FD_ISSET(pipefd[0], &rset)) {## 39 ##src/bcast/dgclibcast6.c##
Read(pipefd[0], &n, 1); /* timer expired */## 40 ##src/bcast/dgclibcast6.c##
break;## 41 ##src/bcast/dgclibcast6.c##
}## 42 ##src/bcast/dgclibcast6.c##
}## 43 ##src/bcast/dgclibcast6.c##
}## 44 ##src/bcast/dgclibcast6.c##
}## 45 ##src/bcast/dgclibcast6.c##
static void## 46 ##src/bcast/dgclibcast6.c##
recvfrom_alarm(int signo)## 47 ##src/bcast/dgclibcast6.c##
{## 48 ##src/bcast/dgclibcast6.c##
Write(pipefd[1], "", 1); /* write 1 null byte to pipe */## 49 ##src/bcast/dgclibcast6.c##
return;## 50 ##src/bcast/dgclibcast6.c##
}## 51 ##src/bcast/dgclibcast6.c##