socket option getsockopt ou setsockopt fcntl ioctl
Post on 22-Dec-2015
258 views
TRANSCRIPT
Socket option
Getsockopt ou setsockopt fcntl ioctl
Socket option
#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
Level – code in the system to interpret the option
Optval – pointer to a variable from which the new value of the option is fetched by setsocopt or into which the current value of the option is stored
Options
Options
Socket options
Options inherited by a connected TCP socket from the listening socket SO_DEBUG; SO_DONTROUTE; SO_KEEPALIVE; SO_LINGER; SO_OOBINLINE; SO_RCVBUF; SO_SNDBUF.
SO_BROADCAST
Enables/disable the ability to send broadcast messages
Only for networks that allow broadcast
Code EACCESS returned
SO_DEBUG
Valid for TCP
Keeps track of all segment sent and received by TCP, kept on a circular buffer which can be examined by the kernel using the trpt function
SO_DONTROUTE
Used to bypass the routing mechanism
For UDP sockets, can set flag MSG_DONTROUTE when using send, sento, sendmsg
Used by routing daemons to ignore the content of routing tables
SO_ERROR
Used to retrieve error code
Blocked process is notified via a select or a signal driven I/O
SO_KEEPALIVE
If option is set TCP send automatically a keepalive message every two hours without exchanging data across the socket.
What can happen: TCP peer sends an ACK. The applications is not
notified; TCP peer sends na RST. Socket is closed and
the error message ECONNRESET is returned. If no response is given, at most eight messages
are sent, one every 75 seconds. After that an error code ETIMEDOUT is sent
SO_KEEPALIVE
(cont.): If an ICMP message “host unreachable” is
recieved, it is passed to the application The duration of the interval to send the keepalive
message can be altered to all sockets Used in servers
SO_KEEPALIVE
SO_LINGER
By default, close send data in buffer and returns immediately.
So-linger allows change this way of operation
struct linger {
int l_onoff; /* 0=off, nonzero=on */
int l_linger; /* linger time (seconds) */
};
l_onoff is zero, option is turned off close is called. Option is ignored
l_onoff is nonzsero and l_linger = zero buffer content is discarded and connection is discarded, an RST is sent to the peer, avoids time-wait
SO_LINGER
l_onoff non zero, l_linger non zero – process is put to sleep until all data in buffer is sent and acked or linger period expires
The expiration of lingertime can be checked
SO_LINGER
SO_LINGER
SO_LINGER
SO_LINGER
Application level ack Client:
char ack;
Write(sockfd, data, nbytes);
n = Read(sockfd, &ack, 1); Server:
nbytes = Read(sockfd, buff, sizeof(buff));
Write(sockfd, "", 1);
SO_LINGER
SO_LINGER
SO_OOBINLINE
Out-of-band data is put in the normal buffer queue
SO_RCVBUF e SO_SNDBUF
Set value for the size of send buffer and receive buffer
Default values TCP old versions: 4096 bytes TCP current versions: 8192 a 61440 bytes UDP send buffer 9000 bytes and UDP receive
buffer 40000
SO_RCVBUF e SO_SNDBUF
The size of the buffer needs to be set before connect is called at the client and before listen is called at the server in the server A atribuição do tamanho do buffer deve ser feita no cliente antes de chamar connect e no servidor antes de chamar listen
At least 3 MSS is recommended
Bandwidth – delay product
Bandwidth – Delay product – amount of byte in the pipe
Transmission rate x RTT For instance: T1 (1.536.000 bytes/sec) with
an RTT of 60ms, bandwidth-delay product = 11,520 bytes
If buffer is smaller than bandwidth-delay product TCP becomes the bottleneck
Need larger buffers for high speed networks or networks with long propagation delays (satellite networks)
SO_RCVLOWAT e SO_SNDLOWAT
Changes the low-water mark of the buffer
Minimum amount of data required by select to consider data as either read or written
SO_REUSEADDR
Allows server to perform bind on a port even if it is being used by another process (typically connected socket derived from a listening sockets)
Allows multiple instances of the same server to be started on the same port as each instance binds a different local IP address
SO_REUSEADDR
Allows a single process to bind the same port to multiple sockets, as long as each bind specifies a different local IP
Allows completely duplicated bindings: a bind of an IP address and port when the same IP address and port are already bound to another socket. Useful for multicast when the socket option SO_REUSEPORT is not available
SO_REUSEPORT
SO_REUSEADDR is equivalent to SO_REUSEPORT when multicast is used
SO_TYPE e SO_USELOOPBACK
SO_TYPE: returns the socket type
SO_USELOOPBACK: receives copy of all transmitted
IPv4 Options
Level IPROTO_IP IP_HDRINCL – allows writing to IP header –
used in RAW_SOCKET (ICMP) What cannot be attributed
Checksum IP identification field Output interface INADDR_V4 Implementation dependent option
IPv4 Options
IP_options – allow setting of IP datagram fields
IP_RECVDSTADDR – allow to return a UDP packet as ancillary data (control) by using recvmsg
IP_TOS – allow setting field ToS
IPv4 Options
IP_TTL – allow writing and reading to filed Time to Live (TTL). Default = 64
IPv6 Options
LEVEL IPTPROTO_IPv6 IPv6_ADDRFOMR – allows converting an
IPv4 socket to an IPv6 socket IPv6 checksum – computes and store
checksum for outgoing packets and verify checksum for incoming packets. Parameter indicates where checksum field is located
IPv6 Options
IPv6_DSTOPTS – specifies that any received IPv6 destination option are to be returned as ancillary data by recvmsg
IPv6_HOPLIMIT – specifies that the received hop limit field to be returned as ancillary data by recvmsg
IPv6_HOPOPTS – specifies that the option hop-by-hop has to be returned as ancillary by recvmsg
IPv6_NEXTHOP – specifies the next hop a packet should go
IPv6 Options
IPv6_PKTINFO – specifies that destination address and interface are to be returned as ancillary data
IPv6_RTHDR – specifies that the routing fileds of the header should be returned as ancillary data by rcvmsg
IPv6-UNICAST_HOPS – set thehop limit
ICMPv6
Level IPROTO_ICMPv6 ICMP6_FILTER – specifies which of the 256
ICMPv6 messages can be sent on a podem RAWSOCKET
Opções TCP
TCP_KEEPALIVE – specifies the time interval in seconds that the connection should send keepalive messages
TCP_MAXSEG – allows setting and retrieving value of MSS
TCP_NODELAY – disable Nagle’s algorithm which prevents small packets to be sent. Small segments are sent in case there are pending acks
Opções TCP
Opções TCP
Opções TCP
TCP_STDURG – changes interpretation of the urgent pointer field