Chapter 07 Socket Options q Goal IT COOKBOOK
Chapter 07. Socket Options
q Goal IT COOKBOOK • Understanding and practice of various socket options -1 -
q Overview (1/7) IT COOKBOOK • Socket programming model application socket Socket code TCP UDP Protocol implementation code IP Ir. DA -2 - Bluetooth . . .
q Overview (2/7) IT COOKBOOK • socket options – Changing the basic operation of socket call • Two types of socket option ① handling in socket code • socket option is analyzed and proceeded in socket code ② handling in protocol implementation code • socket option is analyzed and proceeded in protocol implementation code -3 -
q Overview (3/7) IT COOKBOOK • Socket option setting int setsockopt ( SOCKET s, int level, int optname, const char* optval, int optlen ); success: 0, fail: SOCKET_ERROR -4 -
q Overview (4/7) IT COOKBOOK • Socket option getting int getsockopt ( SOCKET s, int level, int optname, char* optval, int* optlen ); success: 0, fail: SOCKET_ERROR -5 -
q Overview (5/7) IT COOKBOOK • Socket option - SOL_SOCKET optname optval type get set description SO_BROADCAST BOOL permit sending broadcast datagram SO_DONTROUTE BOOL No routing table reference in data transfer SO_KEEPALIVE BOOL Checking connection status periodically SO_LINGER struct linger{} Changing closesocket() property SO_SNDBUF SO_RCVBUF int Setting socket send/receive buffer size SO_SNDTIMEO SO_RCVTIMEO int Setting timeout for send(), recv() and so on SO_REUSEADDR BOOL Reuse of Local address(IP address, port number) -6 -
q Overview (6/7) IT COOKBOOK • Socket option - IPPROTO_IP optname optval type get set description IP_HDRINCL BOOL Including IP header in data sending IP_TTL int Changing IP packet TTL(time-to-live) IP_MULTICAST_IF IN_ADDR{} Setting interface to send multicast packet IP_MULTICAST_TTL int Changing TTL of multicast packet IP_MULTICAST_LOOP BOOL Setting of loop-back of multicast packet IP_ADD_MEMBERSHIP IP_DROP_MEMBERSHI P struct ip_mreq{} Join and leave multicast group -7 -
q Overview (7/7) IT COOKBOOK • Socket option - IPPROTO_TCP optname optval type get set description TCP_NODELAY BOOL Stop Nagle algorithm in TCP -8 -
q SO_BROADCAST option IT COOKBOOK • Enables or disables the ability of a process to send broadcast messages. • It is supported only for datagram sockets. • Its default value is off. -9 -
q SO_DONTROUTE option IT COOKBOOK • usage – used by routing daemon to make sure a packet goes through a certain interface • Code example BOOL optval = TRUE; if(setsockopt(listen_sock, SOL_SOCKET, SO_DONTROUTE, (char *)&optval, sizeof(optval)) == SOCKET_ERROR) { err_quit("setsockopt()"); } - 10 -
q SO_KEEPALIVE option IT COOKBOOK – If set, this option is used for detecting peer host crashes. – If this option is set and no data has been exchanged for 2 hours, then TCP sends keepalive probe to the peer. Cases: • Peer responds with ACK. Another probe will be sent only after 2 hours of inactivity. • Peer responds with RST (has crashed and rebooted). Error is set to ECONNRESET and the socket is closed. • No response. 8 more probes are sent after which the socket’s pending error is set to ETIMEDOUT and the socket is closed. – Code example BOOL optval = TRUE; if(setsockopt(listen_sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval, sizeof(optval)) == SOCKET_ERROR) { err_quit("setsockopt()"); } - 11 -
q SO_LINGER option IT COOKBOOK – This option specifies how the close function operates for connection-oriented protocols – struct linger { int l_onoff; int l_linger; /* Linger active /* How long to linger for */ */ }; – l_onoff=0 : defualt behaviour – l_onoff=1, l_linger=val : close returns only after val seconds, if socket is blocking – l_onoff=1, l_linger=0 : RST sent on close - 12 -
q SO_SNDBUF, SO_RCVBUF option • usage – send and receive buffer size • Code example int optval; int optlen = sizeof(optval); if(getsockopt(listen_sock, SOL_SOCKET, SO_RCVBUF, (char *)&optval, &optlen) == SOCKET_ERROR) err_quit("getsockopt()"); printf(“receive buffer size = %d bytes n", optval); optval *= 2; if(setsockopt(listen_sock, SOL_SOCKET, SO_RCVBUF, (char *)&optval, sizeof(optval)) == SOCKET_ERROR) err_quit("setsockopt()"); - 13 - IT COOKBOOK
q SO_SNDTIMEO, SO_RCVTIMEO option • Usage – Setting timeout for send(), recv(), sendto(), recvfrom(), etc • Code example int optval = 3000; if(setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&optval, sizeof(optval)) == SOCKET_ERROR) { err_quit("setsockopt()"); } - 14 - IT COOKBOOK
q SO_REUSEADDR option IT COOKBOOK – It allows a listening server to restart and bind its well known port even if previously established connections exist. – It allows multiple instances of the same server to be started on the same port, as long as each instance binds a different local IP address. – It allows a single process to bind the same port to multiple sockets, as long as each bind specifies a different local IP address. – It allows completely duplicate bindings only for UDP sockets (broadcasting and multicasting). - 15 -
q multicasting (1/3) IT COOKBOOK • Multicast address 28 bit 1 1 1 0 Multicast group ID • Features – Join and leave a multicast group , all group members are equal – A member have to join a multicast group to receive multicast data – A member don’t have to join a multicast group to send multicast data - 16 -
q multicasting (2/3) IT COOKBOOK • Multicast data transfer (1) A A B B Multicast group - 17 -
q multicasting (3/3) IT COOKBOOK • Multicast data transfer (2) A C B Multicast group • Multicast. Sender. cpp, Multicastreceiver. cpp - 18 -
q. API support Socket options IT COOKBOOK setsockopt() getsockopt() IP_MULTICAST_LOOP yes IP_MULTICAST_TTL yes IP_MULTICAST_IF yes IP_ADD_MEMBERSHIP yes no IP_DROP_MEMBERSHIP yes no - 19 -
q IP_MULTICAST_IF option (1/2) IT COOKBOOK • Usage – Specify the interface for outgoing multicast datagrams sent on this socket • Code example IN_ADDR localaddr; localaddr. s_addr = inet_addr("147. 46. 114. 70"); if(setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localaddr, sizeof(localaddr)) == SOCKET_ERROR) { err_quit("setsockopt()"); } - 20 -
q IP_MULTICAST_IF option (2/2) IT COOKBOOK • Option setting result application 147. 46. 114. 70 147. 46. 115. 38 - 21 -
q IP_MULTICAST_TTL option IT COOKBOOK • Usage – Set the IPv 4 TTL parameter (if not specified, default=1) • Code example // multicast TTL setting int ttl = 2; retval = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(ttl)); if(retval == SOCKET_ERROR) err_quit("setsockopt()"); - 22 -
q IP_MULTICAST_LOOP option IT COOKBOOK • Usage • Enable or disable local loopback (default is enabled) • Code example BOOL optval = FALSE; // no receiving the data sent if(setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&optval, sizeof(optval)) == SOCKET_ERROR) { err_quit("setsockopt()"); } - 23 -
q IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP option (1/3) IT COOKBOOK • Usage – Join/leave a multicast group on a specified local interface • Option value #include <ws 2 tcpip. h> struct ip_mreq { struct in_addr imr_multiaddr; /* IP multicast address of group */ struct in_addr imr_interface; /* local IP address of interface */ }; - 24 -
q IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP option (2/3) IT COOKBOOK • Code example struct ip_mreq; mreq. imr_multiaddr. s_addr = inet_addr("235. 7. 8. 9"); mreq. imr_interface. s_addr = inet_addr("147. 46. 114. 70"); if(setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) == SOCKET_ERROR) { err_quit("setsockopt()"); } - 25 -
q IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP option (3/3) IT COOKBOOK • Option setting result application 147. 46. 114. 70 147. 46. 115. 38 - 26 -
q TCP_NODELAY option (1/2) IT COOKBOOK • usage – Used to enable/disable Nagle’s algorithm in TCP • Nagle algorithm ① send the data when the size of the sending data becomes to MSS(maximum segment size) ② wait for the ACK of the previous data when the sending data size is less that. Once ACK is arrived, data is sent to peer side BOOL optval = TRUE; // Stop Nagle algorithm if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&optval, sizeof(optval)) == SOCKET_ERROR) { err_quit("setsockopt()"); } - 27 -
- Slides: 28