[ Team LiB ] |
11.13 tcp_listen FunctionOur next function, tcp_listen, performs the normal TCP server steps: create a TCP socket, bind the server's well-known port, and allow incoming connection requests to be accepted. Figure 11.12 shows the source code.
Call getaddrinfo8�15 We initialize an addrinfo structure with our hints: AI_PASSIVE, since this function is for a server, AF_UNSPEC for the address family, and SOCK_STREAM. Recall from Figure 11.8 that if a hostname is not specified (which is common for a server that wants to bind the wildcard address), the AI_PASSIVE and AF_UNSPEC hints will cause two socket address structures to be returned: the first for IPv6 and the next for IPv4 (assuming a dual-stack host). Create socket and bind address16�25 The socket and bind functions are called. If either call fails, we just ignore this addrinfo structure and move on to the next one. As stated in Section 7.5, we always set thè SO_REUSEADDR socket option for a TCP server. Check for failure26�27 If all the calls to socket and bind fail, we print an error and terminate. As with our tcp_connect function in the previous section, we do not try to return an error from this function. 28 The socket is turned into a listening socket by listen. Return size of socket address structure29�32 If the addrlenp argument is non-null, we return the size of the protocol addresses through this pointer. This allows the caller to allocate memory for a socket address structure to obtain the client's protocol address from accept. (See Exercise 11.7 also.) Example: Daytime ServerFigure 11.13 shows our daytime server from Figure 4.11, recoded to use tcp_listen. Require service name or port number as command-line argument11�12 We require a command-line argument to specify either the service name or port number. This makes it easier to test our server, since binding port 13 for the daytime server requires superuser privileges. Create listening socket13 tcp_listen creates the listening socket. We pass a NULL pointer as the third argument because we don't care what size address structure the address family uses; we will use sockaddr_storage. Figure 11.12 tcp_listen function: performs normal server steps.lib/tcp_listen.c
Server loop14�22 accept waits for each client connection. We print the client address by calling sock_ntop. In the case of either IPv4 or IPv6, this function prints the IP address and port number. We could use the function getnameinfo (Section 11.17) to try to obtain the hostname of the client, but that involves a PTR query in the DNS, which can take some time, especially if the PTR query fails. Section 14.8 of TCPv3 notes that on a busy Web server, almost 25% of all clients connecting to that server did not have PTR records in the DNS. Since we do not want a server (especially an iterative server) to wait seconds for a PTR query, we just print the IP address and port. Figure 11.13 Daytime server recoded to use tcp_listen (see also Figure 11.14).names/daytimetcpsrv1.c
Example: Daytime Server with Protocol SpecificationThere is a slight problem with Figure 11.13: The first argument to tcp_listen is a null pointer, which combined with the address family of AF_UNSPEC that tcp_listen specifies might cause getaddrinfo to return a socket address structure with an address family other than what is desired. For example, the first socket address structure returned will be for IPv6 on a dual-stack host Figure 11.8, but we might want our server to handle only IPv4. Clients do not have this problem since the client must always specify either an IP address or a hostname. Client applications normally allow the user to enter this as a command-line argument. This gives us the opportunity to specify a hostname that is associated with a particular type of IP address (recall our -4 and -6 hostnames in Section 11.2), or to specify either an IPv4 dotted-decimal string (forcing IPv4) or an IPv6 hex string (forcing IPv6). But there is a simple technique for servers that lets us force a given protocol on a server, either IPv4 or IPv6: Allow the user to enter either an IP address or a hostname as a command-line argument to the program and pass this to getaddrinfo. In the case of an IP address, an IPv4 dotted-decimal string differs from an IPv6 hex string. The following calls to inet_pton either fail or succeed, as indicated:
Therefore, if we change our servers to accept an optional argument, and if we enter
it defaults to IPv6 on a dual-stack host, but entering
explicitly specifies IPv4 and
explicitly specifies IPv6. Figure 11.14 shows this final version of our daytime server. Handle command-line arguments11�16 The only change from Figure 11.13 is the handling of the command-line arguments, allowing the user to specify either a hostname or an IP address for the server to bind, in addition to a service name or port. We first start this server with an IPv4 socket and then connect to the server from clients on two other hosts on the local subnet.
Now we start the server with an IPv6 socket.
The first connection is from the host aix using IPv6 and the second is from the host macosx using IPv6. The next two connections are from the hosts aix and macosx, but using IPv4, not IPv6. We can tell this because the client's addresses returned by accept are both IPv4-mapped IPv6 addresses. What we have just shown is that an IPv6 server running on a dual-stack host can handle either IPv4 or IPv6 clients. The IPv4 client addresses are passed to the IPv6 server as IPv4-mapped IPv6 addresses, as we will discuss in Section 12.2. Figure 11.14 Protocol-independent daytime server that uses tcp_listen.names/daytimetcpsrv2.c
|
[ Team LiB ] |
No comments:
Post a Comment