Download presentation
Presentation is loading. Please wait.
Published byDominic Schulze Modified over 6 years ago
1
Chapter 11 Name and Address Conversions (Part II)
Presented by: Erin-Ee-Lin Lau - 18th April
2
Outline: 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
3
11.11 host_serv Function Two arguments Address family and socket type
Initializes hints structure, calls getaddrinfo and returns a null pointer if an error occurs
4
11.12 tcp_connect Function Create a TCP socket and connect to a server
We now write two functions that use getaddrinfo to handle most scenarios for the TCP clients and servers that we write. The first function, tcp_connect, performs the normal client steps: create a TCP socket and connect to a server.
5
tcp_connect Function (cont.)
Call getaddrinfo Try each addrinfo structure until success or end of list Returns all the dynamic memory
6
Example: 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 server’s address
7
Example: 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 Sun Jul 27 23:06: Host that supports both IPv4 and IPv6 freebsd % daytimetcpcli aix daytime connected to 3ffe:b80:1f8d:2:204:acff:fe17:bf38 Sun Jul 27 23:17: Force the use of IPv4 address by specifying the host-name with -4 suffix freebsd % daytimetcpcli aix-4 daytime connected to Sun Jul 27 23:17:
8
11.13 tcp_listen Function Performs normal TCP server steps
Create a TCP socket, bind the server’s port and allow incoming connection requests to be accepted
9
11.13 tcp_listen Function 1. Call getaddrinfo
2. Create socket and bind address 3. Check for failure 4. Return size of socket address structure We initilialize an addrinfo structure with our hints -> AI PASSIVE (for server), AF UNSPEC (for address family) and SOCK Stream The socket and bind function are call. We always set the So_REUSEADDR socket option in a TCPserver SO_REUSEADDR allows a listening server to start and bind its well-known port, even if previously established connections exist that use this port as their local port. SO_REUSEADDR allows a single process to bind the same port to multiple sockets, as long as each bind specifies a different local IP address. If all calls to socket and bind fail, we pint an error and terminate. Socket is turned into a listening socket by listen. If the addrlenp argument is non-null, we return the size of the protocol address through this pointer. This allows caller to allocate memory for a socket address stucture to obtain the client’s protocol address from ACCEPT.
10
Example: Daytime Server
Waits for requests from Client Accepts client connections Send the current time Terminates connection and goes back waiting for more connections.
11
Example: Daytime Server
Require service name or port no. as command line argument 2. Create a listening socket 3. Server loop
12
Daytime 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 explicitly specifies IPv4 % server 0::0 explicitly specifies IPv6
13
Daytime 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
14
Daytime 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
15
Next… udp_client udp_connect upd_server
16
11.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 We will look at unconnected UDP and connect UDP socket
17
11.14 udp_client: Unconnected UDP Socket
- Converts hostname and service arguments - Datagram socket is created - Memory is allocated for one socket address structure And the created socket address structure is copied into the memory
18
Protocol-Independent Daytime Client using udp_client function
1. Call udp_client function 2. Print IP address and port of the server To which we will send the UDP datagram. We sent a one-byte datagram and then read and print the reply Protocol independent : caller des not care what type of protocol address is contained in the socket address structure…the detail is handled by function
19
Protocol-Independent Daytime Client using udp_client function
freebsd % daytimeudpcli1 aix daytime sending to 3ffe:b80:1f8d:2:204:acff:fe17:bf38 Sun Jul 27 23:21: IPv6 socket freebsd % daytimeudpclil daytime sending to Sun Jul 27 23:21: IPv4 socket We run our client specifying a hostname that has a AAAA and an A record. AAAA is returned first by getaddrinfo and IPv6 socket is created.
20
11.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. Identical to tcp_connect. The peer is unreachable or there is no server at the specified port.
21
11.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
22
udp_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 TCP’s TIME_WAIT state for UDP socket
23
Protocol-independent Daytime Server
24
11.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)
25
11.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
26
11.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 Eg: gethostbyaddr return a name of aix.unpbook.com If this flag is specified to getnameinfo, return hostname aix
27
11.17 getnameinfo Function NI_NUMERICHOST NI_NUMERICSERV
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 Specify by server as client port numbers typically have no associated service name – they are ephemeral port
28
11.18 Re-entrant Functions Re-entrancy problem!
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
29
11.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 -
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.