P. 1
Unit 4

Unit 4

|Views: 312|Likes:
Published by srsr1987

More info:

Published by: srsr1987 on May 31, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less







(A) SOCKET OPTIONS (i) INTRODUCTION There are various ways to get and set the options that affect a socket:  The getsockopt and setsockopt functions  The fcntl function  The ioctl function (ii) getsockopt and setsockopt FUNCTIONS These two functions apply only to sockets.

#include <sys/socket.h> int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); int setsockopt(int sockfd, int level, int optname, const void *optval socklen_t optlen); Both return: 0 if OK,–1 on error
Mrs.D.Anow Fanny, MCA Dept, RMDEC Page 75

MC9241 – NETWORK PROGRAMMING – UNIT - IV sockfd must refer to an open socket descriptor. level specifies the code in the system that interprets the option: the general socket code or some protocol-specific code (e.g., IPv4, IPv6, TCP, or SCTP). optval is a pointer to a variable from which the new value of the option is fetched by setsockopt, or into which the current value of the option is stored by getsockopt. The size of this variable is specified by the final argument, as a value for setsockopt and as a value-result for getsockopt. (iii) GENERIC SOCKET OPTIONS

These options are protocol-independent (that is, they are handled by the protocolindependent code within the kernel, not by one particular protocol module such as IPv4), but some of the options apply to only certain types of sockets. For example, even though the SO_BROADCAST socket option is called "generic," it applies only to datagram sockets.  SO_BROADCAST Socket Option This option enables or disables the ability of the process to send broadcast messages. Broadcasting is supported for only datagram sockets and only on networks that support the concept of a broadcast message (e.g., Ethernet, token ring, etc.). You cannot broadcast on a point-to-point link or any connection-based transport protocol such as SCTP or TCP. Since an application must set this socket option before sending a broadcast datagram, it prevents a process from sending a broadcast when the application was never designed to broadcast.  SO_DEBUG Socket Option This option is supported only by TCP. When enabled for a TCP socket, the kernel keeps track of detailed information about all the packets sent or received by TCP for the socket. These are kept in a circular buffer within the kernel that can be examined with the trpt program.  SO_DONTROUTE Socket Option This option specifies that outgoing packets are to bypass the normal routing mechanisms of the underlying protocol. For example, with IPv4, the packet is directed to the appropriate local interface, as specified by the network and subnet portions of the destination address. If the local interface cannot be determined from the destination address (e.g., the destination is not on the other end of a point-to-point link, or is not on a shared network), ENETUNREACH is returned. The equivalent of this option can also be applied to individual datagrams using the MSG_DONTROUTE flag with the send, sendto, or sendmsg functions. This option is often used by routing daemons (e.g., routed and gated) to bypass the routing table and force a packet to be sent out a particular interface.

Mrs.D.Anow Fanny, MCA Dept, RMDEC

Page 76

–1 is returned with errno set to the value of so_error (p. One of three scenarios results: 1. TCP will give up if there is no response within 11 minutes and 15 seconds after sending the first probe. which tells the local TCP that the peer host has crashed and rebooted. If there is data queued for the socket. MCA Dept. for either readability or writability. Berkeley-derived TCPs send 8 additional probes. The peer responds with an RST. 2. 495 of TCPv2) and so_error is reset to 0. trying to elicit a response. that data is returned by read instead of the error condition. This probe is a TCP segment to which the peer must respond. read returns–1 with errno set to the value of so_error. The process can be immediately notified of the error in one of two ways: 1. RMDEC Page 77 . select returns with either or both conditions set.Anow Fanny. If the process is using signal-driven I/O. This is called the pending error for the socket. the protocol module in a Berkeley-derived kernel sets a variable named so_error for that socket to one of the standard Unix Exxx values. 2.   SO_KEEPALIVE Socket Option When the keep-alive option is set for a TCP socket and no data has been exchanged across the socket in either direction for two hours. TCP automatically sends a keep-alive probe to the peer. The socket's pending error is set to ECONNRESET and the socket is closed. The application is not notified (since everything is okay).  If so_error is nonzero when the process calls read and there is no data to return. the SIGIO signal is generated for either the process or the process group. The value of so_error is then reset to 0. TCP will send another probe following another two hours of inactivity. The peer responds with the expected ACK.D. If the process is blocked in a call to select on the socket. 3. The integer value returned by getsockopt is the pending error for the socket. If so_error is nonzero when the process calls write. The figure summarizes the various methods that have to be detected when something happens on the other end of a TCP connection. 75 seconds apart. There is no response from the peer to the keep-alive probe.IV  SO_ERROR Socket Option When an error occurs on a socket. The process can then obtain the value of so_error by fetching the SO_ERROR socket option.MC9241 – NETWORK PROGRAMMING – UNIT . The value of so_error is then reset to 0 by the kernel. Mrs.

g.Anow Fanny. depending on the values of the two structure members: 1. but if there is any data still remaining in the socket send buffer. MCA Dept. If l_onoff is nonzero and l_linger is nonzero. }. The value of l_linger is ignored and the previously discussed TCP default applies: close returns immediately. int l_linger. the system will try to deliver the data to the peer. POSIX specifies units as seconds */ Calling setsockopt leads to one of the following three scenarios.  SO_LINGER Socket Option This option specifies how the close function operates for a connection-oriented protocol (e. the option is turned off. RMDEC Page 78 . That is. TCP aborts the connection when it is closed. the process is put to sleep until either: (i) all the data is sent and acknowledged by the peer TCP. That is. 2. then the kernel will linger when the socket is closed. close returns immediately. not the normal four-packet connection termination 3. TCP discards any data still remaining in the socket send buffer and sends an RST to the peer. If l_onoff is nonzero and l_linger is zero. if there is any data still remaining in the socket send buffer. This option requires the following structure to be passed between the user process and the kernel.D. The SO_LINGER socket option helps in changing this default condition. It is defined by including <sys/socket.h>. Mrs. nonzero=on */ /* linger time.. struct linger { int l_onoff. By default. but not for UDP).IV Ways to detect various TCP conditions. or (ii) the linger time expires. for TCP and SCTP.MC9241 – NETWORK PROGRAMMING – UNIT . If l_onoff is 0. /* 0=off.

This is TCP's flow control.192 to 61. the available room in the socket receive buffer limits the window that TCP can advertise to the other end. UDP.IV Summary of shutdown and SO_LINGER scenarios. In fact. The TCP socket receive buffer cannot overflow because the peer is not allowed to send data beyond the advertised window. These two socket options changes the default sizes.. out-of-band data will be placed in the normal input queue (i.D.  SO_OOBINLINE Socket Option When this option is set. Mrs. The UDP send buffer size often defaults to a value around 9.440 bytes.096 bytes. Recall that UDP has no flow control: It is easy for a fast sender to overwhelm a slower receiver. MCA Dept. When this occurs. The receive buffers are used by TCP.000 bytes. and if the peer ignores the advertised window and sends data beyond the window. causing datagrams to be discarded by the sender itself.  SO_RCVBUF and SO_SNDBUF Socket Options Every socket has a send buffer and a receive buffer.000 bytes if the host supports NFS. and the UDP receive buffer size often defaults to a value around 40. when a datagram arrives that will not fit in the socket receive buffer. that datagram is discarded. anywhere from 8. With UDP. inline). causing datagrams to be discarded by the receiver's UDP. the MSG_OOB flag to the receive functions cannot be used to read the out-of-band data. however. but newer systems use larger values.MC9241 – NETWORK PROGRAMMING – UNIT . The default values differ widely between implementations.Anow Fanny. and SCTP to hold received data until it is read by the application. a fast sender can overwhelm its own network interface. With TCP. Older Berkeley-derived implementations would default the TCP send and receive buffers to 4. RMDEC Page 79 . the receiving TCP discards it.e.

The receive timeout affects the five input functions: read. This is common for UDP servers that need to know the destination IP address of client requests on systems that do not provide the IP_RECVDSTADDR socket option. sendto. A listening server is started.Anow Fanny. SO_RCVLOWAT and SO_SNDLOWAT changes these two low-water marks. This is because of TCP's window scale option. This technique is normally not used with Mrs. MCA Dept. and sendmsg. even if previously established connections exist that use this port as their local port. RMDEC Page 80 . c. this means the socket option must be set for the listening socket before calling listen. the ordering of the function calls is important.IV When setting the size of the TCP socket receive buffer.D. readv.  SO_RCVTIMEO and SO_SNDTIMEO Socket Options These two socket options allow us to place a timeout on socket receives and sends. as long as each bind specifies a different local IP address. For a server. Setting this option for the connected socket will have no effect whatsoever on the possible window scale option because accept does not return with the connected socket until TCP's three-way handshake is complete." It defaults to 1 for TCP. recvfrom. send. d. writev.  SO_RCVLOWAT and SO_SNDLOWAT Socket Options Every socket also has a receive low-water mark and a send low-water mark. A connection request arrives and a child process is spawned to handle that client. These are used by the select function." This low-water mark normally defaults to 2. 3. which is exchanged with the peer on the SYN segments when the connection is established. SO_REUSEADDR allows a single process to bind the same port to multiple sockets. The listening server is restarted. The receive low-water mark is the amount of data that must be in the socket receive buffer for select to return "readable. This is common for a site hosting multiple HTTP servers using the IP alias technique. 2.MC9241 – NETWORK PROGRAMMING – UNIT .  SO_REUSEADDR and SO_REUSEPORT Socket Options The SO_REUSEADDR socket option serves four different purposes: 1. SO_REUSEADDR allows a new server to be started on the same port as an existing server that is bound to the wildcard address. this means the SO_RCVBUF socket option must be set before calling connect. and SCTP sockets. The send lowwater mark is the amount of available space that must exist in the socket send buffer for select to return "writable. but the child continues to service the client on the existing connection. as long as each instance binds a different local IP address. For a client. SO_REUSEADDR allows a listening server to start and bind its well-known port.048 for TCP sockets. These two socket options. That is why this option must be set for the listening socket. b. This allows to specify the timeouts in seconds and microseconds. This condition is typically encountered as follows: a. The send timeout affects the five output functions: write. recdv. and recvmsg. The listening server terminates. UDP.

.IV TCP servers since a TCP server can always determine the destination IP address by calling getsockname after the connection is established. MCA Dept.  SO_USELOOPBACK Socket Option This option applies only to sockets in the routing domain (AF_ROUTE). Instead of overloading SO_REUSEADDR with the desired multicast semantics that allow completely duplicate bindings. This option defaults to ON for these sockets (the only one of the SO_xxx socket options that defaults to ON instead of OFF). 4.4BSD introduced the SO_REUSEPORT socket option when support for multicasting was added. addresses belonging to a multihomed host should use this technique. if the transport protocol supports it. The integer value returned is a value such as SOCK_STREAM or SOCK_DGRAM. RMDEC Page 81 . Mrs. 2. when that same IP address and port are already bound to another socket. but not all. SO_REUSEADDR is used instead of SO_REUSEPORT to allow completely duplicate bindings when it makes sense (i. However. SO_REUSEADDR is considered equivalent to SO_REUSEPORT if the IP address being bound is a multicast address (p.e. When writing a multicast application that can be run multiple times on the same host at the same time. and on those that do not support the option but do support multicasting. a UDP server that can be run multiple times on the same host at the same time and that expects to receive either broadcast or multicast datagrams). 731 of TCPv2). SO_REUSEADDR allows completely duplicate bindings: a bind of an IP address and port.D. a TCP server wishing to serve connections to some. This option is typically used by a process that inherits a socket when it is started. 2. but only if each socket that wants to bind the same IP address and port specify this socket option.MC9241 – NETWORK PROGRAMMING – UNIT .  SO_TYPE Socket Option This option returns the socket type. This option allows completely duplicate bindings. The problem with this socket option is that not all systems support it. Normally this feature is supported only for UDP sockets. the socket receives a copy of everything sent on the socket. this new socket option was introduced with the following semantics: 1. We can summarize our discussion of these socket options with the following recommendations: 1. 4.Anow Fanny. Set the SO_REUSEADDR socket option before calling bind in all TCP servers. set the SO_REUSEADDR socket option and bind the group's multicast address as the local IP address. When this option is enabled.

which makes writing raw packets with IP_HDRINCL not as portable as we'd like.D.  IP_OPTIONS Socket Option Setting this option allows to set IP options in the IPv4 header. When this option is set.  IP_TTL Socket Option Mrs.  Setting IP options is implementation-dependent.  IP_RECVDSTADDR Socket Option This socket option causes the destination IP address of a received UDP datagram to be returned as ancillary data by recvmsg. in the IP header for a TCP.IV (iv) IPv4 SOCKET OPTIONS These socket options are processed by IPv4 and have a level of IPPROTO_IP.  If we set the IP identification field to 0.  If the source IP address is INADDR_ANY. This is implementation-dependent.  IP_TOS Socket Option This option lets us set the type-of-service (TOS) field (which contains the DSCP and ECN fields. own IP header is built for all the datagrams that is send on the raw socket. IP sets it to the primary IP address of the outgoing interface. the current value that would be placed into the DSCP and ECN fields in the IP header (which defaults to 0) is returned. the kernel builds the IP header for datagrams sent on a raw socket. MCA Dept. There is no way to fetch the value from a received IP datagram.  IP_HDRINCL Socket Option If this option is set for a raw IP socket. the kernel will set the field. Some implementations take any IP options that were set using the IP_OPTIONS socket option.  IP_RECVIF Socket Option This socket option causes the index of the interface on which a UDP datagram is received to be returned as ancillary data by recvmsg.. When the getsockopt function is called for this option. a complete IP header is built. This requires intimate knowledge of the format of the IP options in the IP header. or SCTP socket.  Some fields must be in host byte order. but there are some applications (notably traceroute) that build their own IP header to override values that IP would place into certain header fields. RMDEC Page 82 .Anow Fanny.MC9241 – NETWORK PROGRAMMING – UNIT . with the following exceptions:  IP always calculates and stores the IP header checksum. UDP. Normally. and some in network byte order.

the kernel will not calculate and store the checksum for outgoing packets on this raw socket and will not verify the checksum for received packets. discarding packets with an invalid checksum. and is a privileged operation. If this value is non-negative. If a value of -1 is specified (the default). When this option is retrieved.  IPV6_DONTFRAG Socket Option Setting this option disables the automatic insertion of a fragment header for UDP and raw sockets.  IPV6_PATHMTU Socket Option This option cannot be set. the kernel will: (i) (ii) compute and store a checksum for all outgoing packets.D. RMDEC Page 83 . This option affects all IPv6 raw sockets. except ICMPv6 raw sockets. output packets larger than the MTU of the outgoing interface will be dropped. Mrs.IV With this option. only retrieved. MCA Dept. (v) ICMPv6 SOCKET OPTIONS This socket option is processed by ICMPv6 and has a level of IPPROTO_ICMPV6. (vi) IPv6 SOCKET OPTIONS These socket options are processed by IPv6 and have a level of IPPROTO_IPV6. Instead. the application should enable the IPV6_RECVPATHMTU option to learn about path MTU changes. No error needs to be returned from the system call that sends the packet. the current MTU as determined by path-MTU discovery is returned.  ICMP6_FILTER Socket Option This option lets us fetch and set an icmp6_filter structure that specifies which of the 256 possible ICMPv6 message types will be passed to the process on a raw socket. The multicast TTL is set using the IP_MULTICAST_TTL socket option. When this option is set. and verify the received checksum on input.  IPV6_NEXTHOP Socket Option This option specifies the next-hop address for a datagram as a socket address structure. we can set and fetch the default TTL that the system will use for unicast packets sent on a given socket.  IPV6_CHECKSUM Socket Option This socket option specifies the byte offset into the user data where the checksum field is located.MC9241 – NETWORK PROGRAMMING – UNIT .Anow Fanny. since the packet might exceed the path MTU en-route.

 IPV6_RECVPATHMTU Socket Option Setting this option specifies that the path MTU of a path is to be returned as ancillary data by recvmsg (without any accompanying data) when it changes.  IPV6_USE_MIN_MTU Socket Option Setting this option to 1 specifies that path MTU discovery is not to be performed and that packets are sent using the minimum IPv6 MTU to avoid fragmentation. MCA Dept.  IPV6_RECVTCLASS Socket Option Setting this option specifies that the received traffic class (containing the DSCP and ECN fields) is to be returned as ancillary data by recvmsg. while fetching the socket option returns the value for the hop limit that the kernel will use for the socket. Setting it to 0 causes Mrs.  IPV6_RECVRTHDR Socket Option Setting this option specifies that a received IPv6 routing header is to be returned as ancillary data by recvmsg.  IPV6_RECVHOPLIMIT Socket Option Setting this option specifies that the received hop limit field is to be returned as ancillary data by recvmsg.  IPV6_UNICAST_HOPS Socket Option This IPv6 option is similar to the IPv4 IP_TTL socket option.  IPV6_RECVHOPOPTS Socket Option Setting this option specifies that any received IPv6 hop-by-hop options are to be returned as ancillary data by recvmsg. RMDEC Page 84 . There is no way with IPv4 to obtain the received TTL field.MC9241 – NETWORK PROGRAMMING – UNIT .Anow Fanny.D. Setting the socket option specifies the default hop limit for outgoing datagrams sent on the socket. This option defaults to OFF.IV  IPV6_RECVDSTOPTS Socket Option Setting this option specifies that any received IPv6 destination options are to be returned as ancillary data by recvmsg. This option defaults to OFF. This option defaults to OFF. This option defaults to OFF.  IPV6_RECVPKTINFO Socket Option Setting this option specifies that the following two pieces of information about a received IPv6 datagram are to be returned as ancillary data by recvmsg: the destination IPv6 address and the arriving interface index. This option defaults to OFF.

and for which it is currently awaiting an acknowledgment). (vii) TCP SOCKET OPTIONS There are two socket options for TCP. If this value is fetched before the socket is connected.  TCP_NODELAY Socket Option If set. this value can go up or down. although some systems have an option to turn it ON by default.e.  IPV6_V6ONLY Socket Option Setting this option on an AF_INET6 socket restricts it to IPv6 communication only. This option defaults to OFF. the purpose of the Nagle algorithm is to prevent a connection from having multiple small packets outstanding at any time. The definition of a "small" packet is any packet smaller than the MSS. Mrs. A TCP socket fetches and stores these values using getsockopt and setsockopt instead. The maximum amount of data that our TCP will send per segment can also change during the life of a connection if TCP supports path MTU discovery. RMDEC Page 85 . Setting it to–1 specifies that path MTU discovery is performed for unicast destinations but the minimum MTU is used when sending to multicast destinations.Anow Fanny. MCA Dept. The value returned is the maximum amount of data that our TCP will send to the other end. is in use.MC9241 – NETWORK PROGRAMMING – UNIT . because this option occupies 12 bytes of TCP options in each segment. Also be aware that a value smaller than the returned value can actually be used for the connection if the timestamp option. often.D. The algorithm states that if a given connection has outstanding data (i. it is the MSS announced by the other end with its SYN. this option disables TCP's Nagle algorithm.  IPV6_XXX Socket Options Most of the IPv6 options for header modification assume a UDP socket with information being passed between the kernel and the application using ancillary data with recvmsg and sendmsg.IV path MTU discovery to occur for all destinations.. unless our TCP chooses to use a smaller value than the peer's announced MSS. By default. We specify the level as IPPROTO_TCP. for example. data that our TCP has sent. then no small packets will be sent on the connection in response to a user write operation until the existing data is acknowledged. TCP will always send a fullsized packet if possible. This option defaults to –1. the value returned is the default value that will be used if an MSS option is not received from the other end. If the route to the peer changes. this algorithm is enabled. The purpose of the Nagle algorithm is to reduce the number of small packets on a WAN.  TCP_MAXSEG Socket Option This socket option allows us to fetch or set the MSS for a TCP connection.

D. and the ACKs from right to left.Anow Fanny. Assume that the ACK of the client's character is sent back to the client along with the character echo and we ignore the ACKs that the client sends for the server's echo. On a fast LAN. MCA Dept. with exactly 250 ms between each character. when the ACK of the first packet is received. since the connection has a small packet outstanding.IV The two common generators of small packets are the Rlogin and Telnet clients. Mrs. then eight packets are shown in the figure. along with the echo of the first character. because the time required for a small packet to be acknowledged is typically a few milliseconds—far less than the time between two successive characters that we type. these two characters are sent. But on a WAN. At time 600. If the Nagle algorithm is enabled (the default). where it can take a second for a small packet to be acknowledged. Consider the following example: Type the six-character string "hello!" to either an Rlogin or Telnet client. and this delay is often exaggerated by the Nagle algorithm. Until this packet is ACKed at time 1200. since they normally send each keystroke as a separate packet. we normally do not notice the Nagle algorithm with these clients. no more small packets are sent. RMDEC Page 86 . and have 12 packets as shown in figure. The RTT to the server is 600 ms and the server immediately sends back the echo of each character. but the next two characters are not sent. Each character is sent in a packet by itself: the data segments from left to right. The first character is sent as a packet by itself. we can notice a delay in the character echoing. Six characters echoed by server with Nagle algorithm disabled. Assuming the Nagle algorithm is disabled.MC9241 – NETWORK PROGRAMMING – UNIT .

instead. so the ACK of the client's character piggybacks with the server's echo of that character. which waits until data arrives from some Mrs. This is normally the case with the Rlogin and Telnet clients. saving one TCP segment. reliable byte stream provided by TCP. MCA Dept. (B) ELEMENTARY UDP SOCKETS (i) INTRODUCTION There are some fundamental differences between applications written using TCP versus those that use UDP.D. These clients can detect noticeable delays because the client TCP will not send any data to the server until the server's delayed ACK timer expires. there will be data to send back to the peer. the server does not accept a connection from a client. The problem is with other clients whose servers do not generate traffic in the reverse direction on which ACKs can piggyback. quite unlike the connection-oriented. The diagram shows the function calls for a typical UDP client/server. The hope is that in this small amount of time.MC9241 – NETWORK PROGRAMMING – UNIT . which requires the address of the destination (the server) as a parameter. datagram protocol. The Nagle algorithm often interacts with another TCP algorithm: the delayed ACK algorithm. Instead. TCP will wait some small amount of time (typically 50–200 ms) and only then send the ACK. Similarly. the client just sends a datagram to the server using the sendto function. These clients need a way to disable the Nagle algorithm. unreliable. hence the TCP_NODELAY option. and the ACK can piggyback with the data. RMDEC Page 87 . Instead. because the servers typically echo each character sent by the client. the server just calls the recvfrom function. These are because of the differences in the two transport layers: UDP is a connectionless.Anow Fanny.IV Six characters echoed by server with Nagle algorithm enabled. The client does not establish a connection with the server. This algorithm causes TCP to not send an ACK immediately when it receives data.

are identical to the first three arguments for read and write: descriptor. buff. RMDEC Page 88 . const struct sockaddr *to. #include <sys/socket.Anow Fanny. recvfrom returns the protocol address of the client. Both return: number of bytes read or written if OK. so the server can send a response to the correct client. pointer to buffer to read into or write from. int flags. along with the datagram. –1 on error The first three arguments.MC9241 – NETWORK PROGRAMMING – UNIT .D. sockfd.IV client. socklen_t *addrlen). void *buff. int flags. MCA Dept. const void *buff. ssize_t sendto(int sockfd. but three additional arguments are required. size_t nbytes. size_t nbytes. (ii) recvfrom and sendto FUNCTIONS These two functions are similar to the standard read and write functions. struct sockaddr *from. Mrs. socklen_t addrlen). SOCKET FUNCTIONS FOR UDP CLIENT/SERVER.h> ssize_t recvfrom(int sockfd. and number of bytes to read or write. and nbytes.

. char **argv) { int sockfd. sizeof(servaddr)). (iii)UDP ECHO SERVER UDP client and server programs follow the function call flow as depicted in the diagram Simple echo client/server using UDP.sin_addr.D.sin_port = htons(SERV_PORT). while the final argument to recvfrom is a pointer to an integer value (a value-result argument). bzero(&servaddr. (SA *) &servaddr. RMDEC Page 89 . struct sockaddr_in servaddr. } Create UDP socket. int main(int argc. bind server's well-known port A UDP socket is created by specifying the second argument to socket as SOCK_DGRAM (a datagram socket in the IPv4 protocol). the IPv4 address for the bind is specified as INADDR_ANY and the server's well-known port is the Mrs. UDP echo server Program.IV The to argument for sendto is a socket address structure containing the protocol address (e. sizeof(cliaddr)). The recvfrom function fills in the socket address structure pointed to by from with the protocol address of who sent the datagram. IP address and port number) of where the data is to be sent.MC9241 – NETWORK PROGRAMMING – UNIT .sin_family = AF_INET. The number of bytes stored in this socket address structure is also returned to the caller in the integer pointed to by addrlen. cliaddr. The final two arguments to sendto are similar to the final two arguments to connect: We fill in the socket address structure with the protocol address of where to send the datagram (in the case of UDP) or with whom to establish a connection (in the case of TCP).g. 0). The final two arguments to recvfrom are similar to the final two arguments to accept: The contents of the socket address structure upon return tell us who sent the datagram (in the case of UDP) or who initiated the connection (in the case of TCP). As with the TCP server example.Anow Fanny. SOCK_DGRAM.s_addr = htonl(INADDR_ANY). dg_echo(sockfd. The size of this socket address structure is specified by addrlen. MCA Dept. (SA *) &cliaddr. servaddr. The final argument to sendto is an integer value. servaddr. servaddr. sizeof(servaddr)). sockfd = socket(AF_INET. bind(sockfd.

0).h header. servaddr. . Next. (iv)UDP ECHO CLIENT The UDP client main function is shown below. socklen_t len. &len). MCA Dept. sockfd = Socket(AF_INET. char **argv) { int sockfd. there are numerous details to consider. not a concurrent server as we had with TCP. RMDEC Page 90 .sin_port = htons(SERV_PORT). SA *pcliaddr. Since UDP is a connectionless protocol. sizeof(servaddr)).MC9241 – NETWORK PROGRAMMING – UNIT . this function never terminates. MAXLINE. &servaddr. void dg_echo(int sockfd. Despite the simplicity of this function. 0.D. this function provides an iterative server. pcliaddr. There is no call to fork. int main(int argc. The function dg_echo is called to perform server processing. mesg. inet_pton(AF_INET. SOCK_DGRAM.sin_family = AF_INET. char mesg[MAXLINE]. bzero(&servaddr. dg_echo function: echo lines on a datagram socket. n = Recvfrom(sockfd. n. for ( .sin_addr). mesg. echo back to sender This function is a simple loop that reads the next datagram arriving at the server's port using recvfrom and sends it back using sendto. struct sockaddr_in servaddr. Mrs.Anow Fanny.IV constant SERV_PORT from the unp. servaddr. there is nothing like an EOF as we have with TCP. In general. most TCP servers are concurrent and most UDP servers are iterative. 0. so a single server process handles any and all clients. UDP echo client. pcliaddr. argv[1]. len). } } Read datagram. First. ) { len = clilen. Sendto(sockfd. if(argc != 2) err_quit("usage: udpcli <IPaddress>"). socklen_t clilen) { int n.

As with TCP. servlen). MCA Dept. (SA *) &servaddr.D. but this is rarely done. MAXLINE. recvline[MAXLINE + 1]. NULL. who will think it is the server's reply. which performs most of the client processing. } Fill in socket address structure with server's address An IPv4 socket address structure is filled in with the IP address and port number of the server.Anow Fanny. stdout). sizeof(servaddr)). This structure will be passed to dg_cli. exit(0). sockfd. send the line to the server using sendto. to dg_cli Mrs. can send a datagram to the client's IP address and port. n = recvfrom(sockfd. This tells the kernel that we are not interested in knowing who sent the reply. There is a risk that any process. 0. dg_cli function: client processing loop. but the client main function is protocol-dependent. and print the echoed line to standard output using fputs. along with its size. The main function allocates and initializes a socket address structure of some protocol type and then passes a pointer to this structure. socklen_t servlen) { int n. the client function dg_cli is protocol-independent. on either the same host or some other host. pservaddr. sendline. 0. Our client has not asked the kernel to assign an ephemeral port to its socket. char sendline[MAXLINE]. strlen(sendline). RMDEC Page 91 . the client can call bind explicitly. that is when an ephemeral port is chosen by the kernel for the socket. int sockfd. With a UDP socket. MAXLINE. With a TCP client. As with the server function dg_echo. and that datagram will be read by the client. Notice that the call to recvfrom specifies a null pointer as the fifth and sixth arguments. fp) != NULL) { sendto(sockfd. if the socket has not yet had a local port bound to it. specifying where to send datagrams. /* null terminate */ fputs(recvline.MC9241 – NETWORK PROGRAMMING – UNIT . while (fgets(sendline. recvline[n] = 0.IV dg_cli(stdin. NULL). The function dg_cli. we said the call to connect is where this takes place. const SA *pservaddr. A UDP socket is created and the function dg_cli is called. read back the server's echo using recvfrom. the first time the process calls sendto. void dg_cli(FILE *fp. } } There are four steps in the client processing loop: read a line from standard input using fgets. recvline.

fork a child. Create UDP socket A UDP socket is also created and bound to the same port. nready. udpfd.IV (v)TCP AND UDP USING SELECT (MULTIPLEXING) Combination of concurrent TCP echo server with iterative UDP echo server into a single server that uses select to multiplex a TCP and UDP socket. Prepare for select Initialize a descriptor set for select and calculate the maximum of the two descriptors for which has to be waited. Handle new client connection Accept a new client connection when the listening TCP socket is readable.MC9241 – NETWORK PROGRAMMING – UNIT . Since our sig_chld handler can interrupt our call to select. Handle arrival of datagram If the UDP socket is readable. Call select Call select. connfd. Establish signal handler for SIGCHLD A signal handler is established for SIGCHLD because TCP connections will be handled by a child process. RMDEC . int main(int argc. there is no need to set the SO_REUSEADDR socket option before this call to bind. We set the SO_REUSEADDR socket option in case connections exist on this port. char **argv) { int listenfd.D. maxfdp1. because TCP ports are independent of UDP ports. Page 92 Mrs. and call our str_echo function in the child. we handle an error of EINTR. Even though the same port is used for TCP and UDP sockets. First half of echo server that handles TCP and UDP using select. MCA Dept. Create listening TCP socket A listening TCP socket is created that is bound to the server's well-known port.Anow Fanny. a datagram has arrived. waiting only for readability on the listening TCP socket or readability on the UDP socket. We read it with recvfrom and send it back to the client with sendto.

} Mrs. /* must call waitpid() */ FD_ZERO(&rset). &rset. const int on = 1.sin_port = htons(SERV_PORT). Second half of echo server that handles TCP and UDP using select. /* back to for() */ else err_sys("select error"). socklen_t len. sizeof(servaddr)). servaddr.MC9241 – NETWORK PROGRAMMING – UNIT . bzero(&servaddr. 0).sin_port = htons(SERV_PORT). b ind(udpfd. servaddr. SOCK_DGRAM. sizeof(servaddr)). LISTENQ). (SA *) &servaddr. servaddr. setsockopt(listenfd. sizeof(servaddr)). for ( . sizeof(servaddr)). RMDEC Page 93 . Signal(SIGCHLD.s_addr = htonl(INADDR_ANY). listen(listenfd. bind(listenfd. struct sockaddr_in cliaddr. . ssize_t n. fd_set rset. &rset).sin_family = AF_INET. servaddr.Anow Fanny.sin_addr. servaddr. udpfd) + 1. NULL.sin_family = AF_INET. if ( (nready = select(maxfdp1. MCA Dept. (SA *) &servaddr. servaddr. 0). &rset). servaddr. SO_REUSEADDR. sig_chld). sizeof(on)). ) { FD_SET(listenfd.D. pid_t childpid.s_addr = htonl(INADDR_ANY).sin_addr. &on. /* create UDP socket */ udpfd = socket(AF_INET. SOCK_STREAM. void sig_chld(int). bzero(&servaddr. maxfdp1 = max(listenfd. /* create listening TCP socket */ listenfd = socket(AF_INET.IV char mesg[MAXLINE]. FD_SET(udpfd. NULL)) < 0) { if (errno == EINTR) continue. NULL. SOL_SOCKET.

&rset)) { len = sizeof(cliaddr). called a "quad A" record. or a fully qualified domain name '(FQDN).MC9241 – NETWORK PROGRAMMING – UNIT . connfd = accept(listenfd. mesg.com. 0. Page 94 Mrs.254 3ffe:b80:1f8d:1:a00:20ff:fea7:686b 5 freebsd. n.32. maps a hostname into a 128-bit IPv6 address. The term "quad A" was chosen because a 128-bit address is four times larger than a 32-bit address. MCA Dept. &rset)) { len = sizeof(cliaddr). A hostname can be either a simple name. /* parent closes connected socket */ } if (FD_ISSET(udpfd. } } } ( C) NAME AND ADDRESS CONVERSIONS (i) DOMAIN NAME SYSTEM (DNS) The DNS is used primarily to map between hostnames and IP addresses. MAXLINE.D.com. &len). A An A record maps a hostname into a 32-bit IPv4 address.unpbook. } close(connfd). The trailing period tells the resolver that this name is fully qualified and it doesn't need to search its list of possible domains. /* close listening socket */ str_echo(connfd). sendto(udpfd.IV if (FD_ISSET(listenfd. /* process the request */ exit(0). &len).Anow Fanny.106. For example. RMDEC . such as solaris or freebsd.  Resource Records Entries in the DNS are known as resource records (RRs). mesg. (SA *) &cliaddr. 0. AAAA A AAAA record. 10 mailhost. (SA *) &cliaddr.com domain. len). an FQDN is also called an absolute name and must end with a period.unpbook. n = recvfrom(udpfd. (SA *) &cliaddr. Technically. but users often omit the ending period. the first of which is an A record: freebsd IN A IN AAAA IN MX IN MX 12. here are the four DNS records for the host freebsd in the unpbook. if ( (childpid = Fork()) == 0) { /* child process */ close(listenfd).

such as ftp and www.32. two MX records are provided: The first has a preference value of 5 and the second has a preference value of 10. In the example for the host freebsd above. Applications such as the clients and servers that we are writing in this text contact a DNS server by calling functions in a library known as the resolver.106.com. the following could be CNAMEs for our host linux: ftp www IN IN CNAME linux. it is transparent when a service is moved to another host. The common resolver functions are gethostbyname and gethostbyaddr. For example. they are used in order of preference.MC9241 – NETWORK PROGRAMMING – UNIT .com. RMDEC Page 95 . On some systems. each nibble is converted to its corresponding hexadecimal ASCII value (0–9a–f). here are the four DNS records for the host freebsd in the unpbook. PTR PTR records (called "pointer records") map IP addresses into hostnames. resolvers. If people use these service names instead of the actual hostnames.unpbook. For example. On others.com domain. each byte is converted to its decimal ASCII value (0–255).IV A An A record maps a hostname into a 32-bit IPv4 address.unpbook. the 32 4-bit nibbles of the 128-bit address are reversed. 10 mailhost. The former maps a hostname into its IPv4 addresses.unpbook. the first of which is an A record: freebsd IN A IN AAAA IN MX IN MX 12.  Resolvers and Name Servers Organizations run one or more name servers. CNAME linux.com. When multiple MX records exist. typically calling the functions gethostbyname and gethostbyaddr. MCA Dept. there is a centralized resolver daemon that all applications share. For an IPv6 address. In either case. The resulting string is used in the PTR query.Anow Fanny. then 4 bytes of the 32-bit address are reversed. The figure shows a typical arrangement of applications. CNAME CNAME stands for "canonical name. often the program known as BIND (Berkeley Internet Name Domain). For an IPv4 address. and the system library code performs RPCs to this daemon. application code calls the resolver code using normal function calls. starting with the smallest value.254 3ffe:b80:1f8d:1:a00:20ff:fea7:686b 5 freebsd." A common use is to assign CNAME records for common services.unpbook. and name servers and then write the application code. both of which are described in this chapter. MX An MX record specifies a host to act as a "mail exchanger" for the specified host. Mrs. and the latter does the reverse mapping.D.com. the resolver code is contained in a system library and is link-edited into the application when the application is built.

conf.MC9241 – NETWORK PROGRAMMING – UNIT . Mrs. but this introduces a chicken-and-egg problem of where to go to do the name-to-address conversion for the server that will do the name and address conversion! The resolver sends the query to the local name server using UDP.2.conf to specify the IP addresses of the name servers. which uses the file /etc/irs. since the names are easier to remember and configure.x and later use the file /etc/nsswitch. MCA Dept. and AIX uses the file /etc/netsvc. BIND 9.Anow Fanny. and name servers.conf.D.IV Typical arrangement of clients. Solaris 2. and FreeBSD 5. If the answers are too large to fit in a UDP packet. If the local name server does not know the answer.2 supplies its own version named the Information Retrieval Service (IRS). RMDEC Page 96 . the Network Information System (NIS) or Lightweight Directory Access Protocol (LDAP). The file /etc/resolv. Common alternatives are static host files (normally the file /etc/hosts). it is implementationdependent how an administrator configures a host to use the different types of name services. It might be nice to use the names of the name servers in the /etc/resolv. If a name server is to be used for hostname lookups. Unfortunately.x. also using UDP. resolvers. The resolver code reads its system-dependent configuration files to determine the location of the organization's name servers. it will normally query other name servers across the Internet. the resolver will automatically switch to TCP.conf normally contains the IP addresses of the local name servers. HP-UX 10 and later.conf.  DNS Alternatives It is possible to obtain name and address information without using the DNS. then all these systems use the file /etc/resolv.conf file.

it is limited in that it can only return IPv4 addresses.Anow Fanny. hostent structure and the information it contains Mrs. /* pointer to array of pointers to alias names */ int h_addrtype. However. /* host address type: AF_INET */ int h_length. But. The most basic function that looks up a hostname is gethostbyname. not addresses. so that it is known exactly what goes into the socket address structures for functions such as connect and sendto. RMDEC Page 97 .D. most applications should deal with names. it returns a pointer to a hostent structure that contains all the IPv4 addresses for the host. /* ptr to array of ptrs with IPv4 addrs */ }.IV (ii) GETHOSTBYNAME FUNCTION Host computers are normally known by human-readable names. /* length of address: 4 */ char **h_addr_list. If successful.h> struct hostent *gethostbyname (const char *hostname). Returns: non-null pointer if OK.MC9241 – NETWORK PROGRAMMING – UNIT .NULL on error with h_errno set The non-null pointer returned by this function points to the following hostent structure: struct hostent { char *h_name. #include <netdb. The POSIX specification cautions that gethostbyname may be withdrawn in a future version of the spec. All the examples uses IP addresses instead of names. MCA Dept. /* official (canonical) name of host */ char **h_aliases. and what is returned by functions such as accept and recvfrom.

but not both. gethostbyname differs from the other socket functions that it does not set errno when an error occurs. MCA Dept. gethostbyname could only return either IPv6 or IPv4 addresses. Some versions of gethostbyname allow the hostname argument to be a dotted-decimal string.ai_family struct entry). Since the hostent structure only has one address length field. will work. Most modern resolvers provide the function hstrerror. RMDEC Page 98 . a call of the form hptr = gethostbyname ("192. int af) .h> struct hostent *gethostbyname2 (const char *name. the first revision of the API used the RES_USE_INET6 constant.h> #include <netdb. but it does not have an A record. #include <sys/socket. internal interface.h>:     HOST_NOT_FOUND TRY_AGAIN NO_RECOVERY NO_DATA (identical to NO_ADDRESS) The NO_DATA error means the specified name is valid.IV The returned h_name is called the canonical name of the host. Enabling RES_USE_INET6 caused gethostbyname to look up AAAA records first.D. We will describe gethostbyname2 next. which takes an h_errno value as its only argument and returns a const char * pointer to a description of the error. Instead. calling gethostbyname. NULL on error with h_errno set Mrs. An example of this is a hostname with only an MX record.168.Anow Fanny. That is.MC9241 – NETWORK PROGRAMMING – UNIT . This code was added because the Rlogin client accepts only a hostname. Returns: non-null pointer if OK. and will not accept a dotted-decimal string. it sets the global integer h_errno to one of the following constants defined by including <netdb.42. and only look up A records if a name had no AAAA records. which had to be added to the resolver flags using a private.2"). This API was not very portable since systems that used a different internal resolver interface had to mimic the BIND resolver interface to provide it. (iii) IPv6 SUPPORT IN DNS  The RES_USE_INET6 Constant Since gethostbyname doesn't have an argument to specify what address family is of interest (like getaddrinfo's hints. Enabling RES_USE_INET6 also caused gethostbyname2 to return IPv4 addresses as IPv4mapped IPv6 addresses. The gethostbyname2 Function The gethostbyname2 function adds an address family argument to gethostbyname.

In terms of the DNS. #include <netdb. It introduced the getipnodebyname function to solve some of these problems.h> void freehostent (struct hostent *ptr) .ai_flags arguments. For thread safety. gethostbyname2 behaves just like gethostbyname. the canonical hostname. When the af argument is AF_INET6.Anow Fanny.h> #include <netdb. gethostbyaddr queries a name server for a PTR record in the inaddr.ai_family and hints.h> struct hostent *gethostbyaddr (const char *addr. int flags. The getipnodebyname Function RFC 2553 deprecated RES_USE_INET6 and gethostbyname2 because of the global nature of the RES_USE_INET6 flag and the wish to provide more control over the returned information. #include <sys/socket.IV When the af argument is AF_INET. gethostbyname2 looks up and returns only AAAA records for IPv6 addresses. Mrs. the return value is dynamically allocated. Returns: non-null pointer if OK.h> struct hostent *getipnodebyname (const char *name. The field of interest in this structure is normally h_name. The af and flags arguments map directly to getaddrinfo's hints. len is the size of this structure: 4 for an IPv4 address. int family). AF_INET6. socklen_t len. but is really a pointer to an in_addr structure containing the IPv4 address.arpa domain. #include <netdb. so it must be freed with the freehostent function. int *error_num) . looking up and returning IPv4 addresses. The family argument is AF_INET. AF_INET. This is the reverse of gethostbyname. MCA Dept. Returns: non-null pointer if OK. The addr argument is not a char*. RMDEC Page 99 . int af. NULL on error with error_num set This function returns a pointer to the same hostent structure that was described with gethostbyname. NULL on error with h_errno set This function returns a pointer to the same hostent structure that was described with gethostbyname.D. (iv) GETHOSTBYADDR FUNCTION The function gethostbyaddr takes a binary IPv4 address and tries to find the hostname corresponding to that address.MC9241 – NETWORK PROGRAMMING – UNIT .

NULL on error This function returns a pointer to the following structure: struct servent { char *s_name. /* FTP using TCP */ sptr = getservbyname("ftp". looks up a service given its name. The getservbyname function. #include <netdb. we must not call htons when storing this into a socket address structure.D. and if the mapping from the name to port number is contained in a file (normally /etc/services). The canonical list of port numbers assigned to services is maintained by the IANA. /* protocol to use */ }.g. RMDEC Page 100 . /* FTP using TCP */ sptr = getservbyname("ftp". network-byte order */ char *s_proto. The main field of interest in the servent structure is the port number. /* this call will fail */ Mrs.MC9241 – NETWORK PROGRAMMING – UNIT . like hosts. /* DNS using UDP */ sptr = getservbyname("ftp". "udp"). are often known by names. then if the port number changes. "udp"). The service name servname must be specified. Since the port number is returned in network byte order. /* port number. /* alias list */ int s-port. A given /etc/services file is likely to contain a subset of the IANA assignments. sptr = getservbyname("domain". "tcp").IV (v) GETSERVBYNAME FUNCTION Services. Returns: non-null pointer if OK. it is implementation-dependent as to which port number is returned.Anow Fanny. instead of by its port number. If a protocol is also specified (protoname is a non-null pointer). FTP requires TCP). MCA Dept.. Typical calls to this function could be as follows: struct servent *sptr. If protoname is not specified and the service supports multiple protocols. too. NULL). When a service is refered by its name in the code. const char *protoname). Normally this does not matter. because services that support multiple protocols often use the same TCP and UDP port number.h> struct servent *getservbyname (const char *servname. Some Internet services are provided using either TCP or UDP while others support only a single protocol (e. the modification lies in only one line of /etc/services file instead of having to recompile the applications. but this is not guaranteed. /* official service name */ char **s_aliases. then the entry must also have a matching protocol.

h> struct servent *getservbyport (int port. /* FTP using TCP */ sptr = getservbyport (htons (21).MC9241 – NETWORK PROGRAMMING – UNIT . NULL on error The port value must be network byte ordered. NULL).IV (vi) GETSERVBYPORT FUNCTION The getservbyport function. "tcp"). "udp"). #include <netdb. Typical calls to this function could be as follows: struct servent *sptr. "udp").D. RMDEC Page 101 . Be aware that a few port numbers are used with TCP for one service. const char *protoname). Returns: non-null pointer if OK. /* FTP using TCP */ sptr = getservbyport (htons (21). looks up a service given its port number and an optional protocol. but the same port number is used with UDP for a totally different service. /* DNS using UDP */ sptr = getservbyport (htons (21). /* this call will fail */ The last call fails because there is no service that uses port 21 with UDP. sptr = getservbyport (htons (53). Mrs.Anow Fanny. MCA Dept.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->