[ Team LiB ] Previous Section Next Section



Modify the program in Figure 11.3 to call gethostbyaddr for each returned address, and then print the h_name that is returned. First run the program specifying a hostname with just one IP address and then run the program specifying a hostname that has more than one IP address. What happens?


Fix the problem shown in the preceding exercise.


Run Figure 11.4 specifying a service name of chargen.


Run Figure 11.4 specifying a dotted-decimal IP address as the hostname. Does your resolver allow this? Modify Figure 11.4 to allow a dotted-decimal IP address as the hostname and a decimal port number string as the service name. In testing the IP address for either a dotted-decimal string or a hostname, in what order should these two tests be performed?


Modify Figure 11.4 to work with either IPv4 or IPv6.


Modify Figure 8.9 to query the DNS and compare the returned IP address with all the destination host's IP addresses. That is, call gethostbyaddr using the IP address returned by recvfrom, followed by gethostbyname to find all the IP addresses for the host.


In Figure 11.12, the caller must pass a pointer to an integer to obtain the size of the protocol address. If the caller does not do this (i.e., passes a null pointer as the final argument), how can the caller still obtain the actual size of the protocol's addresses?


Modify Figure 11.14 to call getnameinfo instead of sock_ntop. What flags should you pass to getnameinfo?


In Section 7.5, we discussed port stealing with the SO_REUSEADDR socket option. To see how this works, build the protocol-independent UDP daytime server in Figure 11.19. Start one instance of the server in one window, binding the wildcard address and some port of your choosing. Start a client in another window and verify that this server is handling the client (note the printf in the server). Next, start another instance of the server in another window, this time binding one of the host's unicast addresses and the same port as the first server. What problem do you immediately encounter? Fix this problem and restart this second server. Start a client, send a datagram, and verify that the second server has stolen the port from the first server. If possible, start the second server again from a different login account on the first server to see if the stealing still succeeds. Some vendors will not allow the second bind unless the user ID is the same as that of the process that has already bound the port.


At the end of Section 2.12, we showed two telnet examples: to the daytime server and to the echo server. Knowing that a client goes through the two steps gethostbyname and connect, which lines output by the client indicate which steps?


getnameinfo can take a long time (up to 80 seconds) to return an error if a hostname cannot be found for an IP address. Write a new function named getnameinfo_timeo that takes an additional integer argument specifying the maximum number of seconds to wait for a reply. If the timer expires and the NI_NAMEREQD flag is not specified, just call inet_ntop and return an address string.

    [ Team LiB ] Previous Section Next Section