forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpserv03.c
101 lines (87 loc) · 2.71 KB
/
udpserv03.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* include udpserv1 */
#include "unpifi.h"
void mydg_echo(int, SA *, socklen_t, SA *);
int
main(int argc, char **argv)
{
int sockfd;
const int on = 1;
pid_t pid;
struct ifi_info *ifi, *ifihead;
struct sockaddr_in *sa, cliaddr, wildaddr;
for (ifihead = ifi = Get_ifi_info(AF_INET, 1);
ifi != NULL; ifi = ifi->ifi_next) {
/*4bind unicast address */
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
sa = (struct sockaddr_in *) ifi->ifi_addr;
sa->sin_family = AF_INET;
sa->sin_port = htons(SERV_PORT);
Bind(sockfd, (SA *) sa, sizeof(*sa));
printf("bound %s\n", Sock_ntop((SA *) sa, sizeof(*sa)));
if ( (pid = Fork()) == 0) { /* child */
mydg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr), (SA *) sa);
exit(0); /* never executed */
}
/* end udpserv1 */
/* include udpserv2 */
if (ifi->ifi_flags & IFF_BROADCAST) {
/* 4try to bind broadcast address */
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
sa = (struct sockaddr_in *) ifi->ifi_brdaddr;
sa->sin_family = AF_INET;
sa->sin_port = htons(SERV_PORT);
if (bind(sockfd, (SA *) sa, sizeof(*sa)) < 0) {
if (errno == EADDRINUSE) {
printf("EADDRINUSE: %s\n",
Sock_ntop((SA *) sa, sizeof(*sa)));
Close(sockfd);
continue;
} else
err_sys("bind error for %s",
Sock_ntop((SA *) sa, sizeof(*sa)));
}
printf("bound %s\n", Sock_ntop((SA *) sa, sizeof(*sa)));
if ( (pid = Fork()) == 0) { /* child */
mydg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr),
(SA *) sa);
exit(0); /* never executed */
}
}
}
/* end udpserv2 */
/* include udpserv3 */
/* 4bind wildcard address */
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
bzero(&wildaddr, sizeof(wildaddr));
wildaddr.sin_family = AF_INET;
wildaddr.sin_addr.s_addr = htonl(INADDR_ANY);
wildaddr.sin_port = htons(SERV_PORT);
Bind(sockfd, (SA *) &wildaddr, sizeof(wildaddr));
printf("bound %s\n", Sock_ntop((SA *) &wildaddr, sizeof(wildaddr)));
if ( (pid = Fork()) == 0) { /* child */
mydg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr), (SA *) sa);
exit(0); /* never executed */
}
exit(0);
}
/* end udpserv3 */
/* include mydg_echo */
void
mydg_echo(int sockfd, SA *pcliaddr, socklen_t clilen, SA *myaddr)
{
int n;
char mesg[MAXLINE];
socklen_t len;
for ( ; ; ) {
len = clilen;
n = Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
printf("child %d, datagram from %s", getpid(),
Sock_ntop(pcliaddr, len));
printf(", to %s\n", Sock_ntop(myaddr, clilen));
Sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
/* end mydg_echo */