+ All Categories
Home > Documents > Socket option Getsockopt ou setsockopt fcntl ioctl.

Socket option Getsockopt ou setsockopt fcntl ioctl.

Date post: 22-Dec-2015
Category:
View: 227 times
Download: 2 times
Share this document with a friend
Popular Tags:
40
Socket option Getsockopt ou setsockopt fcntl ioctl
Transcript
Page 1: Socket option Getsockopt ou setsockopt fcntl ioctl.

Socket option

Getsockopt ou setsockopt fcntl ioctl

Page 2: 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

Page 3: Socket option Getsockopt ou setsockopt fcntl ioctl.

Options

Page 4: Socket option Getsockopt ou setsockopt fcntl ioctl.

Options

Page 5: Socket option Getsockopt ou setsockopt fcntl ioctl.

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.

Page 6: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_BROADCAST

Enables/disable the ability to send broadcast messages

Only for networks that allow broadcast

Code EACCESS returned

Page 7: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 8: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 9: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_ERROR

Used to retrieve error code

Blocked process is notified via a select or a signal driven I/O

Page 10: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 11: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 12: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_KEEPALIVE

Page 13: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 14: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 15: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_LINGER

Page 16: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_LINGER

Page 17: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_LINGER

Page 18: Socket option Getsockopt ou setsockopt fcntl ioctl.

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);

Page 19: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_LINGER

Page 20: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_LINGER

Page 21: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_OOBINLINE

Out-of-band data is put in the normal buffer queue

Page 22: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 23: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 24: Socket option Getsockopt ou setsockopt fcntl ioctl.

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)

Page 25: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 26: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 27: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 28: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_REUSEPORT

SO_REUSEADDR is equivalent to SO_REUSEPORT when multicast is used

Page 29: Socket option Getsockopt ou setsockopt fcntl ioctl.

SO_TYPE e SO_USELOOPBACK

SO_TYPE: returns the socket type

SO_USELOOPBACK: receives copy of all transmitted

Page 30: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 31: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 32: Socket option Getsockopt ou setsockopt fcntl ioctl.

IPv4 Options

IP_TTL – allow writing and reading to filed Time to Live (TTL). Default = 64

Page 33: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 34: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 35: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 36: Socket option Getsockopt ou setsockopt fcntl ioctl.

ICMPv6

Level IPROTO_ICMPv6 ICMP6_FILTER – specifies which of the 256

ICMPv6 messages can be sent on a podem RAWSOCKET

Page 37: Socket option Getsockopt ou setsockopt fcntl ioctl.

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

Page 38: Socket option Getsockopt ou setsockopt fcntl ioctl.

Opções TCP

Page 39: Socket option Getsockopt ou setsockopt fcntl ioctl.

Opções TCP

Page 40: Socket option Getsockopt ou setsockopt fcntl ioctl.

Opções TCP

TCP_STDURG – changes interpretation of the urgent pointer field


Recommended