[ Team LiB ] |
22.6 Binding Interface AddressesOne 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.
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 information11–12 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 address13–20 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 }
fork child for this address21–24 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 address25–42 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 child43–47 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 address50–62 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 terminates63 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 argument65–66 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 reply71–78 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.
We can check that all these sockets are bound to the indicated IP address and port using netstat.
We should note that our design of one child process per socket is for simplicity and other designs are possible. For example, to reduce the number of processes, the program could manage all the descriptors itself using select, never calling fork. The problem with this design is the added code complexity. While it is easy to use select for all the descriptors, we would have to maintain some type of mapping of each descriptor to its bound IP address (probably an array of structures) so we could print the destination IP address when a datagram was read from a socket. It is often simpler to use a single process or a single thread for one operation or descriptor instead of having a single process multiplex many different operations or descriptors. |
[ Team LiB ] |