22.6 Binding Interface Addresses
One common use for our get_ifi_info function is with UDP applications that need to monitor all interfaces on a host to know when a datagram arrives, and on which interface it arrives. This allows the receiving program to know the destination address of the UDP datagram, since that address is what determines the socket to which a datagram is delivered, even if the host does not support the IP_RECVDSTADDR socket option.
Recall our discussion at the end of Section 22.2. If the host employs the common weak end system model, the destination IP address may differ from the IP address of the receiving interface. In this case, all we can determine is the destination address of the datagram, which does not need to be an address assigned to the receiving interface. To determine the receiving interface requires either the IP_RECVIF or IPV6_PKTINFO socket option.
Figure 22.15 is the first part of a simple example of this technique with a UDP server that binds all the unicast addresses, all the broadcast addresses, and finally the wildcard address.
Call get_ifi_info, to obtain interface information
1112 get_ifi_info, obtains all the IPv4 addresses, including aliases, for all interfaces. The program then loops through each returned ifi_info structure.
Create UDP socket and bind unicast address
1320 A UDP socket is created and the unicast address is bound to it. We also set the SO_REUSEADDR socket option, as we are binding the same port (SERV_PORT) for all IP addresses.
Figure 22.15 First part of UDP server that binds all addresses.
advio/udpserv03.c
1 #include "unpifi.h"
2 void mydg_echo(int, SA *, socklen_t, SA *);
3 int
4 main(int argc, char **argv)
5 {
6 int sockfd;
7 const int on = 1;
8 pid_t pid;
9 struct ifi_info *ifi, *ifihead;
10 struct sockaddr_in *sa, cliaddr, wildaddr;
11 for (ifihead = ifi = Get_ifi_info(AF_INET, 1);
12 ifi != NULL; ifi = ifi->ifi_next) {
13 /* bind unicast address */
14 sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
15 Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
16 sa = (struct sockaddr_in *) ifi->ifi_addr;
17 sa->sin_family = AF_INET;
18 sa->sin_port = htons(SERV_PORT);
19 Bind(sockfd, (SA *) sa, sizeof(*sa));
20 printf("bound %s\n", Sock_ntop((SA *) sa, sizeof(*sa)));
21 if ( (pid = Fork()) == 0) { /* child */
22 mydg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr), (SA *) sa);
23 exit(0); /* never executed */
24 }
Not all implementations require that this socket option be set. Berkeley-derived implementations, for example, do not require the option and allow a new bind of an already bound port if the new IP address being bound: (i) is not the wildcard, and (ii) differs from all the IP addresses that are already bound to the port.
fork child for this address
2124 A child is forked and the function mydg_echo is called for the child. This function waits for any datagram to arrive on this socket and echoes it back to the sender.Figure 22.16 shows the next part of the main function, which handles broadcast addresses.
Bind broadcast address
2542 If the interface supports broadcasting, a UDP socket is created and the broadcast address is bound to it. This time, we allow the bind to fail with an error of EADDRINUSE because if an interface has multiple addresses (aliases) on the same subnet, then each of the different unicast addresses will have the same broadcast address. We showed an example of this following Figure 17.6. In this scenario, we expect only the first bind to succeed.
Figure 22.16 Second part of UDP server that binds all addresses.
advio/udpserv03.c
25 if (ifi->ifi_flags & IFF_BROADCAST) {
26 /* try to bind broadcast address */
27 sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
28 Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
29 sa = (struct sockaddr_in *) ifi->ifi_brdaddr;
30 sa->sin_family = AF_INET;
31 sa->sin_port = htons(SERV_PORT);
32 if (bind(sockfd, (SA *) sa, sizeof(*sa)) < 0) {
33 if (errno == EADDRINUSE) {
34 printf("EADDRINUSE: %s\n",
35 Sock_ntop((SA *) sa, sizeof(*sa)));
36 Close(sockfd);
37 continue;
38 } else
39 err_sys("bind error for %s",
40 Sock_ntop((SA *) sa, sizeof(*sa)));
41 }
42 printf("bound %s\n", Sock_ntop((SA *) sa, sizeof(*sa)));
43 if ( (pid = Fork()) == 0) { /* child */
44 mydg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr),
45 (SA *) sa);
46 exit(0); /* never executed */
47 }
48 }
49 }
fork child
4347 A child is spawned and it calls the function mydg_echo.The final part of the main function is shown in Figure 22.17. This code binds the wildcard address to handle any destination addresses except the unicast and broadcast addresses we have already bound. The only datagrams that should arrive on this socket should be those destined to the limited broadcast address (255.255.255.255).
Create socket and bind wildcard address
5062 A UDP socket is created, the SO_REUSEADDR socket option is set, and the wildcard IP address is bound. A child is spawned, which calls the mydg_echo function.
main function terminates
63 The main function terminates, and the server continues executing all the children that were spawned.
Figure 22.17 Final part of UDP server that binds all addresses.
advio/udpserv03.c
50 /* bind wildcard address */
51 sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
52 Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
53 bzero(&wildaddr, sizeof(wildaddr));
54 wildaddr.sin_family = AF_INET;
55 wildaddr.sin_addr.s_addr = htonl(INADDR_ANY);
56 wildaddr.sin_port = htons(SERV_PORT);
57 Bind(sockfd, (SA *) &wildaddr, sizeof(wildaddr));
58 printf("bound %s\n", Sock_ntop((SA *) &wildaddr, sizeof(wildaddr)));
59 if ( (pid = Fork()) == 0) { /* child */
60 mydg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr), (SA *) sa);
61 exit(0); /* never executed */
62 }
63 exit(0);
64 }
The function mydg_echo, which is executed by all the children, is shown in Figure 22.18.
Figure 22.18 mydg_echo function.
advio/udpserv03.c
65 void
66 mydg_echo(int sockfd, SA *pcliaddr, socklen_t clilen, SA *myaddr)
67 {
68 int n;
69 char mesg[MAXLINE];
70 socklen_t len;
71 for ( ; ; ) {
72 len = clilen;
73 n = Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
74 printf("child %d, datagram from %s", getpid(),
75 Sock_ntop(pcliaddr, len));
76 printf(", to %s\n", Sock_ntop(myaddr, clilen));
77 Sendto(sockfd, mesg, n, 0, pcliaddr, len);
78 }
79 }
New argument
6566 The fourth argument to this function is the IP address that was bound to the socket. This socket should receive only datagrams destined to that IP address. If the IP address is the wildcard, then the socket should receive only datagrams that are not matched by some other socket bound to the same port.
Read datagram and echo reply
7178 The datagram is read with recvfrom and sent back to the client with sendto.This function also prints the client's IP address and the IP address that was bound to the socket.We now run this program on our host solaris after establishing an alias address for the hme0 Ethernet interface. The alias address is host number 200 on 10.0.0/24.
solaris % udpserv03 | |
bound 127.0.0.1:9877 | loopback interface |
bound 10.0.0.200:9877 | unicast address of hme0:1 interface |
bound 10.0.0.255:9877 | broadcast address of hme0:1 interface |
bound 192.168.1.20:9877 | unicast address of hme0 interface |
bound 192.168.1.255:9877 | broadcast address of hme0 interface |
bound 0.0.0.0.9877 | wildcard |
solaris % netstat -na | grep 9877 | |
127.0.0.1.9877 | Idle |
10.0.0.200.9877 | Idle |
*.9877 | Idle |
192.129.100.100.9877 | Idle |
*.9877 | Idle |
*.9877 | Idle |