forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpserv04.c
103 lines (85 loc) · 2.6 KB
/
udpserv04.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
102
103
#include "unpifi.h"
void mydg_echo(int, SA *, socklen_t);
int
main(int argc, char **argv)
{
int sockfd, family, port;
const int on = 1;
pid_t pid;
socklen_t salen;
struct sockaddr *sa, *wild;
struct ifi_info *ifi, *ifihead;
if (argc == 2)
sockfd = Udp_client(NULL, argv[1], (void **) &sa, &salen);
else if (argc == 3)
sockfd = Udp_client(argv[1], argv[2], (void **) &sa, &salen);
else
err_quit("usage: udpserv04 [ <host> ] <service or port>");
family = sa->sa_family;
port = sock_get_port(sa, salen);
Close(sockfd); /* we just want family, port, salen */
for (ifihead = ifi = Get_ifi_info(family, 1);
ifi != NULL; ifi = ifi->ifi_next) {
/*4bind unicast address */
sockfd = Socket(family, SOCK_DGRAM, 0);
Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
sock_set_port(ifi->ifi_addr, salen, port);
Bind(sockfd, ifi->ifi_addr, salen);
printf("bound %s\n", Sock_ntop(ifi->ifi_addr, salen));
if ( (pid = Fork()) == 0) { /* child */
mydg_echo(sockfd, ifi->ifi_addr, salen);
exit(0); /* never executed */
}
if (ifi->ifi_flags & IFF_BROADCAST) {
/* 4try to bind broadcast address */
sockfd = Socket(family, SOCK_DGRAM, 0);
Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
sock_set_port(ifi->ifi_brdaddr, salen, port);
if (bind(sockfd, ifi->ifi_brdaddr, salen) < 0) {
if (errno == EADDRINUSE) {
printf("EADDRINUSE: %s\n",
Sock_ntop(ifi->ifi_brdaddr, salen));
Close(sockfd);
continue;
} else
err_sys("bind error for %s",
Sock_ntop(ifi->ifi_brdaddr, salen));
}
printf("bound %s\n", Sock_ntop(ifi->ifi_brdaddr, salen));
if ( (pid = Fork()) == 0) { /* child */
mydg_echo(sockfd, ifi->ifi_brdaddr, salen);
exit(0); /* never executed */
}
}
}
/* 4bind wildcard address */
sockfd = Socket(family, SOCK_DGRAM, 0);
Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
wild = Malloc(salen);
memcpy(wild, sa, salen); /* copy family and port */
sock_set_wild(wild, salen);
Bind(sockfd, wild, salen);
printf("bound %s\n", Sock_ntop(wild, salen));
if ( (pid = Fork()) == 0) { /* child */
mydg_echo(sockfd, wild, salen);
exit(0); /* never executed */
}
exit(0);
}
void
mydg_echo(int sockfd, SA *myaddr, socklen_t salen)
{
int n;
char mesg[MAXLINE];
socklen_t len;
struct sockaddr *cli;
cli = Malloc(salen);
for ( ; ; ) {
len = salen;
n = Recvfrom(sockfd, mesg, MAXLINE, 0, cli, &len);
printf("child %d, datagram from %s",
getpid(), Sock_ntop(cli, len));
printf(", to %s\n", Sock_ntop(myaddr, salen));
Sendto(sockfd, mesg, n, 0, cli, len);
}
}