Socket Option
Three ways to get and set the socket option
that affect a socket
getsockopt , setsockopt function=>IPv4 and IPv6 multicasting options
fcntl function =>nonblocking I/O, signal driven I/O
ioctl function
getsockopt
and setsockopt:
#include
int getsockopt(int sockfd, , int level, int
optname, void *optval, socklent_t *optlen);
int setsockopt(int sockfd, int level , int
optname, const void *optval, socklent_t
optlen);
sockfd => open socket descriptor
level => code in the system to
interprete the option(generic, IPv4, IPv6, TCP)
optval => pointer to a variable from
which the new value of option is fetched
by setsockopt, or into which the current value of the option is stored
by setsockopt.
optlen => the size of the option
variable.
Generic
socket option: level -> SO
SO_BROCAST =>enable or disable the ability of the process to send broadcast message
SO_DEBUG =>kernel keep track of detailed information about all packets sent or received by TCP(only supported by TCP)
SO_DONTROUTE=>outgoing packets are to bypass the normal routing mechanisms 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
SO_KEEPALIVE=>wait 2hours, and then TCP automatically sends a keepalive probe to the peer. Peer response: ACK(everything OK)
RST(peer crashed and rebooted):ECONNRESET
no response:ETIMEOUT =>socket closed
SO_LINGER =>specify how the close function operates for a connection-oriented protocol(default:close returns immediately)
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.
SO_TYPE: Return the socket type.
Returned value is such as SOCK_STREAM,
SOCK_DGRAM...
IPV4
Socket Options: Level -> IPPROTO_IP
IP_HDRINCL => If this option is set for a raw IP socket, we must build our IP header for all the datagrams that we send on the raw socket.
IP_OPTIONS=>allows us to set IP option in IPv4 header.
IP_RECVDSTADDR=>This socket option causes the destination IP address of a received UDP datagram to be returned as ancillary data by recvmsg.
IP_RECVIF: Cause the index of the interface on which a UDP datagram is received to be returned as ancillary data by recvmsg.
IP_TOS: lets us set the type-of-service(TOS) field in IP header for a TCP or UDP socket.
If we call getsockopt for this option, the
current value that would be placed into the TOS(type of service) field in the
IP header is returned.
IP_TTL: We can set and fetch the default TTL.
IPV6
Socket Option:
This socket option is processed by IPv6 and
have a level of IPPROTO_IPV6.
IPV6_ADDRFORM=>allow a socket to be converted from IPv4 to IPv6 or vice versa.(chapter 10)
IPV6_CHECKSUM=>specifies the byte offset into the user data of where the checksum field is located.
IPV6_DSTOPTS: Specifies that any received IPv6 destination options are to be returned as ancillary data by recvmsg.
IPV6_HOPLIMIT: Setting this option specifies that the received hop limit field be returned as ancillary data by recvmsg.
IPV6_HOPOPTS: Setting this option specifies that any received IPv6 hop-by-hop option are to be returned as ancillary data by recvmsg.
IPV6_NEXTHOP: This is not a socket option but the type of an ancillary data object that can be specified to sendmsg. This object specifies the next-hop address for a datagram as a socket address structure.
TCP
Socket Options:
There are five socket option for TCP, but
three are new with Posix.1g and not widely supported.
Specify the level as IPPROTO_TCP.
TCP_KEEPALIVE: 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.
TCP_MAXRT: It specifies the amount of time in seconds before a connection is broken once TCP starts retransmitting data. 0 : use default , -1:retransmit forever, positive value :rounded up to next transmission time
TCP_MAXSEG: This allows us to fetch or set the maximum segment size(MSS) for TCP connection.
TCP_NODELAY: This option disables TCP’s Nagle algorithm. (default this algorithm enabled). Purpose of the Nagle algorithm: prevent a connection from having multiple small packets outstanding at any time.
No comments:
Post a Comment