Title: Chapter 11 Name and Address Conversions Part II
1Chapter 11 Name and Address Conversions(Part
II)
- Presented by
- Erin-Ee-Lin Lau
- - 18th April 2007 -
2Outline
- host_serv Function
- tcp_connect Function
- tcp_listen Function
- udp_client Function
- udp_connect Function
- udp_server Function
- getnameinfo Function
- Re-entrant Function
- gethostbyname_r and gethostbyaddr_r Function
311.11 host_serv Function
- Two arguments
- Address family and socket type
- host_serv function
Initializes hints structure, calls getaddrinfo
and returns a null pointer if an error occurs
411.12 tcp_connect Function
- Create a TCP socket and connect to a server
5tcp_connect Function (cont.)
Call getaddrinfo
Try each addrinfo structure until success or end
of list
Returns all the dynamic memory
6Example Daytime client
- Connects to a daytime server
- Retrieves the current date and time
Command line argument - To specify either the
service name or the port number, which allows our
program to connect to other ports
Connect to server
Print servers address
7Example Daytime client (cont)
- tcp_connect does not return the size of socket
address structure used for connect - The design goal for tcp_connect is to reduce the
number of arguments compared to getaddrinfo. - Able to works with both IPv4 and IPv6
- Host that supports only IPv4
- freebsd daytimetcpcli linux daytime
- connected to 206.168.112.96
- Sun Jul 27 230624 2003
- Host that supports both IPv4 and IPv6
- freebsd daytimetcpcli aix daytime
- connected to 3ffeb801f8d2204acfffe17bf38
- Sun Jul 27 231713 2003
- Force the use of IPv4 address by specifying the
host-name with -4 suffix - freebsd daytimetcpcli aix-4 daytime
- connected to 192.168.42.2
- Sun Jul 27 231748 2003
811.13 tcp_listen Function
- Performs normal TCP server steps
- Create a TCP socket, bind the servers port and
allow incoming connection requests to be accepted
911.13 tcp_listen Function
1. Call getaddrinfo
2. Create socket and bind address
3. Check for failure
4. Return size of socket address structure
10Example Daytime Server
- Waits for requests from Client
- Accepts client connections
- Send the current time
- Terminates connection and goes back waiting for
more connections.
11Example Daytime Server
- Require service name or port no. as command line
argument
2. Create a listening socket
3. Server loop
12Daytime server with protocol specification
- Previous function exhibits some problem
- Might cause getaddrinfo to return a socket
address structure with an address family other
than what is desired - Clients always specify either an IP address or a
hostname - Enter as a command-line argument
- Able to force a given protocol on a server
- If we enter
- server ? it defaults to IPv6 on a dual-stack
host - server 0.0.0.0 ? explicitly specifies IPv4
- server 00 ? explicitly specifies IPv6
13Daytime Server (Final Version)
- The only changes is the command-line arguments
- Allows user to specify either a hostname or IP
- address for server to bind, in addition to a
- service name or port
14Daytime Server (Final Version)
- Start server with an IPv4 socket and connect to
server from clients on two other hosts on local
subnet - Start server with an IPv6 socket
aix
IPv6
macosx
aix
IPv4
macosx
15Next
- udp_client
- udp_connect
- upd_server
1611.14 udp_client Function
- Creates an unconnected UDP socket
- Returns three items
- Socket descriptor
- saptr address of a pointer to socket address
structure - lenp returned the size of socket address
structure - Cannot be null pointer
- Length of socket address structure is required in
any calls to sendto and recvfrom
1711.14 udp_client Unconnected UDP Socket
- Converts hostname and service arguments -
Datagram socket is created - Memory is allocated
for one socket address structure
18Protocol-Independent Daytime Client using
udp_client function
1. Call udp_client function 2. Print IP address
and port of the server
19Protocol-Independent Daytime Client using
udp_client function
- freebsd daytimeudpcli1 aix daytime
- sending to 3ffeb801f8d2204acfffe17bf38
- Sun Jul 27 232112 2003
- ? IPv6 socket
- freebsd daytimeudpclil 192.168.42.2 daytime
- sending to 192.168.42.2
- Sun Jul 27 232140 2003
- ? IPv4 socket
-
2011.15 udp_connect Function
- Creates a connected UDP socket
- The final 2 arguments required by udp_client are
no longer needed
- Similar to tcp_connect
- Different
- The call to connect with a UDP socket does not
send anything to the peer. - If something is wrong, caller does not discover
until it sends a datagram to the peer.
2111.16 udp_server Function
- Arguments similar to tcp_listen
- An optional hostname
- A required service (to bound port number)
- An optional pointer to variable in which the size
of socket address structure is returned
22udp_server function Unconnected socket
- Similar to tcp_listen but without the call to
listen. - SO_REUSEADDR socket option are not used for UDP
socket - Can allow multiple sockets to bind the same UDP
port on hosts that support multicasting - There is no TCPs TIME_WAIT state for UDP socket
23Protocol-independent Daytime Server
2411.17 getnameinfo Function
- Complement of getaddrinfo
- Takes a socket address and returns character
strings describing host and service - Translate a socket address to a node name and
service location - Protocol-independent fashion
- sockaddr points to socket address structure
containing the protocol address to be converted
into a human-readable string - addrlen length of structure (normally returned
by accept, recvfrom, getsockname, or getpeername)
2511.17 getnameinfo Function
- Caller allocates space for two human-readable
strings - Host string host and hostlen
- Service string serv and servlen
- 0 if caller does not want the host string or
information on service to be returned. - sock_ntop vs. getnameinfo
- sock_ntop does not involve DNS and just returns
a printable version of IP address and port number - getnameinfo tries to obtain a name for both host
and service
2611.17 getnameinfo Function
- Six flags arguments to change the default
operation of getnameinfo - NI_DGRAM
- Specified when caller knows it is dealing with a
datagram socket (SOCK_DGRAM) - Given only IP address and port number in socket
address structure, getnameinfo cannot determine
the protocol (TCP or UDP) - Eg Port 514 rsh service for TCP syslog
service for UDP - NI_NAMEREQD
- Causes an error to be returned if the hostname
cannot be located using DNS - NI_NOFQDN
- Causes returned hostname to truncated at the
first period - Eg192.168.42.2
- gethostbyaddr return a name of aix.unpbook.com
- If this flag is specified to getnameinfo, return
hostname ? aix
2711.17 getnameinfo Function
- NI_NUMERICHOST
- Numeric representation of IP address shall be
returned instead of its name - NI_NUMERICSERV
- Specifies the numeric form of the service address
shall be returned (for example, its port number)
instead of its name - NI_NUMERICSCOPE
- Specifies numeric form of scope identifier to be
returned instead of its name - NI_NUMERICSERV
- Specify by server as client port numbers
typically have no associated service name they
are ephemeral port
2811.18 Re-entrant Functions
- A function that can be safely called while it's
already executing is said to be re-entrant
Re-entrancy problem!
Called and main flow control is temporarily
stopped
2911.19 gethostbyname_r and gethostbyaddr_r
- To make a nonre-entrant function becomes
re-entrant - Four additional arguments
- Result
- Buffer
- buflen buffer size
- h_errnop to return error code when error occurs
30 - Thank You -