Download presentation
Presentation is loading. Please wait.
1
Socket Options Jari Kellokoski
2
Introduction So far we have discovered some basic usage of socket With socket options we control sockets more suitable way for our purposes (e.g. closing the socket) Covers: –Getsocketopt & setsocketopt –Socket Options like: Generic Options IPv4 IPv6 TCP SCTP
3
getsockopt & setsockopt int getsocketopt( int sockfd, int level, int optname, void *optval, socketlnet_t *oplen) int setsocketopt( int sockfd, int level, int optname, const void *optval, socklen_t oplen) where –sckfd – open socket descriptor –level – code in the system that interprets the option –optval – pointer to a variable into which the current value of the option is stored by getsocketopt or which the new value of the option is fetched by setsocketopt –oplen – size of optval as a value-result for getsocketopt or as a value for setsockopt
4
getsockopt & setsockopt Idea with options: –Some features can be enabled or disabled –Values can be set or read
5
Level, Optname, Flag Level: SOL_SOCKET, IPPROTO_IP, IPPROTO_ICMPv6… optname: SO_BROADCAST, IP_OPTIONS, MCAST_JOIN_GROUP… get: value can be read set: value can be set Flag: on/off Datatype: int, struct…
6
Generic Socket Options SO_BROADCAST SO_ERROR SO_KEEPALIVE SO_LINGER SO_RECVBUF & SO_NDBUF SO_REUSEADDR & REUSEPORT
7
Generic Socket Options SO_BROADCAST –enable/disable to send broadcast messages –of course this is only possible on networks supporting broadcast – like Ethernet – but not with point-to-point links –Applications must set this – wise since failed use can be problematic - flooding
8
Generic Socket Options SO_ERROR –Pending error for socket – standard Exxx values filled on failures –If blocked call – socket returns condition on readability or write ability –If signal-driven – SIGIO signal is generated
9
Generic Socket Options SO_KEEPALIVE –TCP will keep the connection alive even no actual communication data is not transferred –Scenarios to keepalive: Response with ACK – everything is ok Response with RST – crash or reboot in peer host No response – retransmissions – eventually ETIMEOUT or EHOSTUNREACH
10
Generic Socket Options SO_LINGER –How the close function is operating –struct linger with int l_onoff and l_linger –Where: l-onoff is 0 – close returns immediately l_onoff is nonzero and l_linger is 0 – TCP aborts the connection when it is closed – data is discarded from buffer – RST to the peer l_onoff is nonzero and l_linger is nonzero – kernel will linger when socket is closed. Remaining data is tried to be send or linger time expires
11
Generic Socket Options SO_RECVBUF & SO_NDBUF –Send and receive buffers –SO_RCVBUF must be set before calling connect –Buffer sizes should be at least four times the MSS –File transfer in one direction – send buffer in sending side and receiving buffer in receiving side –Bi-directional transfer – both buffer on both ends
12
Generic Socket Options SO_REUSEADDR & SO_REUSEPORT –Allows a listening server to bind well-know port even previously established connections exist (child was spawned) –Allows a new server to be stared on the same port as an existing server (different local IP address – IP aliasing) –Single process to bind same port to multiple sockets (UDP based server) –Completely duplicate binding – same port and IP – UDP multicasting
13
IPv4 Socket Options IP_HDRINCL – for building own IP header IP_TOS – (legendary) type of service field IP_TTL – setting/getting(default) time-to- live
14
IPv6 Socket Options IPv6_CHECKSUM – tells where to look checksum field IPv6_DONTFRAG – disables the automatic fragment header (for UDP and raw sockets) IPv6_UNICAST_HOPS – similar to IPv4 TTL – specifies the default hop limit for outgoing datagrams sent on the socket (or default hop limit that kernel uses) IPv6_PATHMTU – retrieves current MTU returned by path-MTU discovery
15
TCP Socket Options TCP_MAXSEG – fetching/setting MSS for connection – however we can always send less then announced MSS TCP_NODELAY – disables Nagle algorithm –Reasons for that: small packets used in communications (telnet) –small packet – packet smaller then MSS –Nagle algorithm reduces these ”small” packets however in some cases this is creating outstanding delays or making communication almost impossible –delayed ACK – the TCP ACK is delayed for piggyback reasons
16
SCTP Socket Options SCTP_ASSOCINFO – get information about existing association, change parameters of existing association, set defaults for future associations SCTP_AUTOCLOSE – autoclose time for SCTP endpoint SCTP_EVENTS – fetch, enable, disable various SCTP notifications SCTP_INITMSG – get or set initial parameters with INIT message SCTP_MAXBUST – max burst size – no more than this can be sent at once – avoids flooding SCTP_STATUS – current status of SCTP association (data amounts, number of streams, primary addresses and state)
17
fcntl File control – meaning control operations for descriptor –set to nonblocking I/O –set to signal-driven I/O –SIGIO and SIGURG signal based control for socket owner socket owner – only from listening socket – the socket owner is inherited from the listening socket by the connected socket
18
Summary Good to understand – SO_ERROR, SO_KEEPALIVE, SO_REUSEADDR, SO_LINGER SO_ERROR - Pending error for socket – standard Exxx values filled on failures SO_KEEPALIVE – TCP (SCTP) will keep the connection alive even no actual communication data is not transferred SO_REUSEADDR – different scenarios of address/port reusage SO_LINGER - How the close function is operating
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.