Npc07

download Npc07

of 51

Transcript of Npc07

  • 8/13/2019 Npc07

    1/51

    Chapter 7

    Socket Option

  • 8/13/2019 Npc07

    2/51

    abstraction

    Introduction getsockopt and setsockopt function socket state Generic socket option IPv4 socket option ICMPv6 socket option IPv6 socket option TCP socket option fcnl function

  • 8/13/2019 Npc07

    3/51

    Introduction

    Three ways to get and set the socket option thataffect a socket getsockopt , setsockopt function=>IPv4 and IPv6

    multicasting options fcntl function => nonblocking I/O, signal driven I/O ioctl function =>chapter16

  • 8/13/2019 Npc07

    4/51

  • 8/13/2019 Npc07

    5/51

    socket state

    We must set that option for the listening socket=> because connected socket is not returned to aserver by accept until the three way handshake iscompleted by the TCP layer.

  • 8/13/2019 Npc07

    6/51

    Generic socket option SO_BROCAST => enable or disable the ability of the process to send

    broadcast message.(only datagram socket : Ethernet, token ring..):chapter18

    SO_DEBUG =>kernel keep track of detailed information about all packets sentor received by TCP(only supported by TCP)

    SO_DONTROUTE=>outgoing packets are to bypass the normal routingmechanisms of the underlying protocol. SO_ERROR=>when error occurs on a socket, the protocol module in a

    Berkeley-derived kernel sets a variable named so_error for that socket.Process can obtain the value of so_error by fetching the SO_ERROR socket

    option

  • 8/13/2019 Npc07

    7/51

    SO_KEEPALIVE=>wait 2hours, and then TCPautomatically sends a keepalive probe to the peer. Peer response

    ACK(everything OK) RST(peer crashed and rebooted):ECONNRESET no response:ETIMEOUT =>socket closed

    example: Rlogin, Telnet

    SO_KEEPALIVE

  • 8/13/2019 Npc07

    8/51

    SO_LINGER

    SO_LINGER =>specify how the close function operates for aconnection-oriented protocol(default:close returns immediately) struct linger{

    int l_onoff; /* 0 = off, nonzero = on */

    int l_linger; /*linger time : second*/};

    l_onoff = 0 : turn off , l_linger is ignored l_onoff = nonzero and l_linger is 0:TCP abort the connection, discard

    any remaining data in send buffer. l_onoff = nonzero and l_linger is nonzero : process wait until

    remained data sending, or until linger time expired. If socket has beenset nonblocking it will not wait for the close to complete, even if lingertime is nonzero.

  • 8/13/2019 Npc07

    9/51

    SO_LINGER

    client server

    write

    Closeclose returns

    Data queued by TCP

    Application reads queueddata and FIN

    close

    data

    FIN

    Ack of data and FIN

    Ack of data and FIN

    FIN

    Default operation of close:it returns immediately

  • 8/13/2019 Npc07

    10/51

    SO_LINGER

    client server

    write

    CloseData queued by TCP

    Application reads queueddata and FIN

    close

    data

    FIN

    Ack of data and FIN

    Ack of data and FIN

    FIN

    close returns

    Close with SO_LINGER socket option set and l_linger a positive value

  • 8/13/2019 Npc07

    11/51

    SO_LINGER

    client server

    write

    Shutdownread block

    Data queued by TCP

    Application reads queueddata and FIN

    close

    data

    FIN

    Ack of data and FIN

    Ack of data and FIN

    FIN

    read returns 0

    Using shutdown to know that peer has received our data

  • 8/13/2019 Npc07

    12/51

    An way to know that the peer application has readthe data use an application-level ack or application ACK client

    char ack;

    Write(sockfd, data, nbytes); // data from client to servern=Read(sockfd, &ack, 1); // wait for application-level ack

    servernbytes=Read(sockfd, buff, sizeof(buff)); //data from client//server verifies it received the correct amount of data from// the clientWrite(sockfd, , 1);//servers ACK back to client

  • 8/13/2019 Npc07

    13/51

  • 8/13/2019 Npc07

    14/51

  • 8/13/2019 Npc07

    15/51

    SO_RCVBUF , SO_SNDBUF

    let us change the default send-buffer,receive-buffer size. Default TCP send and receive buffer size :

    4096bytes 8192-61440 bytes

    Default UDP buffer size : 9000bytes, 40000 bytes

    SO_RCVBUF option must be setting before

    connection established. TCP socket buffer size should be at least three

    times the MSSs

  • 8/13/2019 Npc07

    16/51

  • 8/13/2019 Npc07

    17/51

    SO_RCVTIMEO, SO_SNDTIMEO

    allow us to place a timeout on socket receives andsends. Default disabled

  • 8/13/2019 Npc07

    18/51

    SO_REUSEADDR, SO_REUSEPORT

    Allow a listening server to start and bind its well known port evenif previously established connection exist that use this port as theirlocal port.

    Allow multiple instance of the same server to be started on thesame port, as long as each instance binds a different local IPaddress.

    Allow a single process to bind the same port to multiple sockets, aslong as each bind specifies a different local IP address.

    Allow completely duplicate bindings : multicasting

  • 8/13/2019 Npc07

    19/51

  • 8/13/2019 Npc07

    20/51

    SO_USELOOPBACK

    This option applies only to sockets in the routingdomain(AF_ROUTE). The socket receives a copy of everything sent on

    the socket.

  • 8/13/2019 Npc07

    21/51

    IPv4 socket option

    L evel => IPPROTO_IP IP_HDRINCL => If this option is set for a raw IP

    socket, we must build our IP header for all thedatagrams that we send on the raw socket.(chapter26)

  • 8/13/2019 Npc07

    22/51

    IP_OPTIONS=>allows us to set IP option in IPv4header.(chapter 24) IP_RECVDSTADDR=>This socket option causes

    the destination IP address of a received UDPdatagram to be returned as ancillary data byrecvmsg .(chapter20)

  • 8/13/2019 Npc07

    23/51

    IP_RECVIF

    Cause the index of the interface on which a UDPdatagram is received to be returned as ancillarydata by recvmsg.(chapter20)

  • 8/13/2019 Npc07

    24/51

    IP_TOS

    lets us set the type-of-service(TOS) field in IPheader for a TCP or UDP socket. If we call getsockopt for this option, the current

    value that would be placed into the TOS(type ofservice) field in the IP header is returned.(figureA.1)

  • 8/13/2019 Npc07

    25/51

    IP_TTL

    We can set and fetch the default TTL(time to livefield, figure A.1).

  • 8/13/2019 Npc07

    26/51

    ICMPv6 socket option

    This socket option is processed by ICMPv6 andhas a level of IPPROTO_ICMPV6. ICMP6_FILTER =>lets us fetch and set an

    icmp6_filter structure that specifies which of the256possible ICMPv6 message types are passed tothe process on a raw socket.(chapter 25)

  • 8/13/2019 Npc07

    27/51

    IPv6 socket option

    This socket option is processed by IPv6 and havea level of IPPROTO_IPV6. IPV6_ADDRFORM=>allow a socket to be

    converted from IPv4 to IPv6 or viceversa.(chapter 10)

    IPV6_CHECKSUM=>specifies the byte offsetinto the user data of where the checksum field islocated.

  • 8/13/2019 Npc07

    28/51

    IPV6_DSTOPTS

    Specifies that any received IPv6 destinationoptions are to be returned as ancillary data byrecvmsg.

  • 8/13/2019 Npc07

    29/51

    IPV6_HOPLIMIT

    Setting this option specifies that the received hoplimit field be returned as ancillary data byrecvmsg.(chapter 20)

    Default off.

  • 8/13/2019 Npc07

    30/51

    IPV6_HOPOPTS

    Setting this option specifies that any receivedIPv6 hop-by-hop option are to be returned asancillary data by recvmsg.(chapter 24)

  • 8/13/2019 Npc07

    31/51

    IPV6_NEXTHOP

    This is not a socket option but the type of anancillary data object that can be specified tosendmsg. This object specifies the next-hopaddress for a datagram as a socket addressstructure.(chapter20)

  • 8/13/2019 Npc07

    32/51

    IPV6_PKTINFO

    Setting this option specifies that the followingtwo pieces of infoemation about a received IPv6datagram are to be returned as ancillary data byrecvmsg: the destination IPv6 address and thearriving interface index.(chapter 20)

  • 8/13/2019 Npc07

    33/51

    IPV6_PKTOPTIONS

    Most of the IPv6 socket options assume a UDPsocket with the information being passed betweenthe kernel and the application using ancillary datawith recvmsg and sendmsg.

    A TCP socket fetch and store these values usingIPV6_ PKTOPTIONS socket option.

  • 8/13/2019 Npc07

    34/51

    IPV6_RTHDR

    Setting this option specifies that a received IPv6routing header is to be returned as ancillary data by recvmsg. (chapter 24)

    Default off

  • 8/13/2019 Npc07

    35/51

    IPV6_UNICAST_HOPS This is similar to the IPv4 IP_TTL. Specifies the default hop limit for outgoing

    datagram sent on the socket, while fetching thesocket option returns the value for the hop limitthat the kernel will use for the socket.

  • 8/13/2019 Npc07

    36/51

    TCP socket option There are five socket option for TCP, but three are

    new with Posix.1g and not widely supported. Specify the level as IPPROTO_TCP.

  • 8/13/2019 Npc07

    37/51

    TCP_KEEPALIVE This is new with Posix.1g It specifies the idle time in second for the

    connection before TCP starts sending keepalive probe.

    Default 2hours this option is effective only when the

    SO_KEEPALIVE socket option enabled.

  • 8/13/2019 Npc07

    38/51

    TCP_MAXRT This is new with Posix.1g. It specifies the amount of time in seconds before a

    connection is broken once TCP startsretransmitting data. 0 : use default -1:retransmit forever positive value:rounded up to next transmission time

  • 8/13/2019 Npc07

    39/51

    TCP_MAXSEG This allows us to fetch or set the maximum

    segment size(MSS) for TCP connection.

  • 8/13/2019 Npc07

    40/51

    TCP_NODELAY This option disables TCPs Nagle algor ithm .

    (default this algorithm enabled)

    purpose of the Nagle algor i thm .

    ==>prevent a connection from having multiplesmall packets outstanding at any time. Small packet => any packet smaller than MSS.

  • 8/13/2019 Npc07

    41/51

    Nagle algor i thm

    Default enabled. Reduce the number of small packet on the WAN. If given connection has outstanding data , then no

    small packet data will be sent on connection untilthe existing data is acknowledged.

  • 8/13/2019 Npc07

    42/51

    0250500

    750100012501500

    15001750

    2000

    h

    el

    lo!

    Nagle algorithm disabled Nagle algorithm disabled

  • 8/13/2019 Npc07

    43/51

    Nagle algorithm enabled

    0250500

    750100012501500

    15001750

    2000

    h

    el

    lo!

    22502500

    h

    el

    lo

    !

  • 8/13/2019 Npc07

    44/51

    fcntl function File control This function perform various descriptor control

    operation.

    Provide the following features Nonblocking I/O(chapter 15) signal-driven I/O(chapter 22) set socket owner to receive SIGIO signal.

    (chapter 21,22)

  • 8/13/2019 Npc07

    45/51

    #include int fcntl(int fd, int cmd, ./* int arg */);

    Returns:depends on cmd if OK, -1 on error

    O_NONBLOCK : nonblocking I/OO_ASYNC : signal driven I/O notification

  • 8/13/2019 Npc07

    46/51

    Nonblocking I/O using fcntl Int flags;

    /* set socket nonblocking */if((flags = fcntl(fd, f_GETFL, 0)) < 0)

    err_sys(F_GETFL error);

    flags |= O_NONBLOCK;if(fcntl(fd, F_SETFL, flags) < 0)err_sys(F_ SETFL error);

    each descriptor has a set of file flags that fetched withthe F_GETFL command

    and set with F_SETFL command .

  • 8/13/2019 Npc07

    47/51

    Misuse of fcntl/* wrong way to set socket nonblocking */if(fcntl(fd, F_SETFL,O_NONBLOCK) < 0)

    err_sys(F_ SETFL error);

    /* because it also clears all the other file status flags.*/

  • 8/13/2019 Npc07

    48/51

    Turn off the nonblocking flagFlags &= ~O_NONBLOCK;if(fcntl(fd, F_SETFL, flags) < 0)

    err_sys(F_SETFL error);

  • 8/13/2019 Npc07

    49/51

    F_SETOWN The integer arg value can be either

    positive(process ID) or negative (group ID)valueto receive the signal.

    F_GETOWN => retrurn the socket owner by fcntl function, either process ID or process group ID.

  • 8/13/2019 Npc07

    50/51

  • 8/13/2019 Npc07

    51/51