Chapter 11 Name and Address Conversions Part II - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Chapter 11 Name and Address Conversions Part II

Description:

... involve DNS and just returns a printable version of IP address and port number ... Six flags arguments to change the default operation of getnameinfo. NI_DGRAM ... – PowerPoint PPT presentation

Number of Views:393
Avg rating:3.0/5.0
Slides: 31
Provided by: Eri6160
Category:

less

Transcript and Presenter's Notes

Title: Chapter 11 Name and Address Conversions Part II


1
Chapter 11 Name and Address Conversions(Part
II)
  • Presented by
  • Erin-Ee-Lin Lau
  • - 18th April 2007 -

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
  • host_serv function

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

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 servers 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 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

8
11.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

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
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 0.0.0.0 ? explicitly specifies IPv4
  • server 00 ? 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

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
18
Protocol-Independent Daytime Client using
udp_client function
1. Call udp_client function 2. Print IP address
and port of the server
19
Protocol-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

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.

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 TCPs 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
  • Eg192.168.42.2
  • 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
  • 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

28
11.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
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 -
Write a Comment
User Comments (0)
About PowerShow.com