more on socket api. how to place timeouts on sockets (1) using sigalrm signal connection timeout...

21
More on Socket API

Upload: christiana-coleen-anthony

Post on 01-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

More on Socket API

Page 2: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

How to Place Timeouts on Sockets (1)

Using SIGALRM signalConnection timeout 기간의 축소 Response timeout

advio/dgclitimeo3.c lib/connect_timeo.c

alarm() 은 초 단위setitimer() 는 micro sec 단위 설정 가능 ( 실제는 msec 단위로 동작 )

Page 3: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

How to Place Timeouts on Sockets (2)

Using select with timeoutadvio/dgclitimeo1.c lib/readable_timeo.c

Page 4: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

How to Place Timeouts on Sockets (3)

Using SO_RCVTIMEO and SO_SNDTIMEO socket options Caution: timeout applies to all I/O operations for the

socket descriptor

advio/dgclitimeo2.c

Page 5: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

More on Socket I/O Functions

recv and send (only for sockets)

Scatter read and gather write

Page 6: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

More Advanced Socket I/O Functions

Page 7: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Ancillary data - cmsghdr Structure

Page 8: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Socket I/O Summary

Page 9: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Socket and Standard I/O

Buffering in Standard I/O library fully buffered: all stream except for terminal devices line buffered : terminal devices unbuffered: stderr

Caution Socket 에 standard I/O functions(fgets, fputs) 를 쓰면

fully buffered 됨

Page 10: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Socket Options

Page 11: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

getsockopt and setsockopt Functions

Page 12: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Generic Socket Options(1)

SO_BROADCAST Enable the ability of the process to send broadcast messages Application must set this option before broadcasting

SO_DEBUG Kernel keeps track of detailed information about all the packets

sent or received by TCP for the socket SO_DONTROUTE

Bypass the normal routing mechanism The packet is directed to the appropriate local interface. If the

local interface cannot be determined (i.e. destination is not on the same network), ENETUNREACH is returned.

SO_ERROR Get the value of pending error Socket 에 error 가 발생하면 , so_error (pending error) 에 먼저

setting 하고 나서 errno 에 setting 함

Page 13: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Generic Socket Options(2)

SO_KEEPALIVE Enforce TCP to send a keepalive probe automatically to the peer

if no data has been exchanged for 2 hours (to detect if the peer host crashes)

Peer’s response is either ACK: everything is OK (application is not notified) RST: the peer host has crashed and rebooted. Socket’s pending

error = ECONNRESET and socket is closed No response: send 8 additional probes, 75 sec apart (totally, 11mim

and 15sec). Listen the peer’s response. Still no response ETIMEOUT and socket is closed Receives ICMP error, EHOSTUNREACH or …

To shorten probe period Use TCP_KEEPALIVE option (Not all implementation support this

option). But, this will affect all sockets on the host. Or, implement your own heartbeat mechanism.

Page 14: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Ways to Detect Various TCP Condition

Page 15: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Default Operation of close

Any data remaining in the socket send buffer is sent. Close returns immediately without receiving ACK.

Client cannot know whether the data has been delivered correctly to the server !!!

Page 16: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Generic Socket Options(3) SO_LINGER: linger when the socket is closed

Data type for option value

If l_onoff = 0, default close(). i.e. return immediately, but the remaining data is delivered to the peer

else linger when the socket is closed If l_linger==0, TCP aborts the connection. i.e. TCP discards any

data still remaining in the socket send buffer and sends an RST to the peer.

else linger when the socket is closed. Any data still remaining in the socket send buffer is sent. But, if linger time expires, return EWOULDBLOCK and any remaining data is discarded.

Page 17: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Using shutdown to know that peer has received data

Page 18: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Generic Socket Options(4) SO_OOBINLINE

OOB data will be placed in the normal input queue SO_RCVBUF and SO_SNDBUF

Set or get socket send buffer size / socket receive buffer size TCP:

advertise receive buffer(window) to the peer sliding window mechanism(flow control) buffer size 3 * MSS

UDP: typical send buffer size 9000, receive buffer size 40,000, If data received > available receive buffer, datagram is discarded

Capacity of full-duplex pipe (bandwidth-delay product) bandwidth * RTT (<= 64KB)

SO_RCVLOWAT, SO_SNDLOWAT: socket low water mark data in socket receive buffer >= SO_RCVLOWAT readable available space in socket send buffer >= SO_SNDLOWAT writable SO_RCVLOWAT = 1, SO_SNDLOWAT = 2048 by default

SO_RCVTIMEO, SO_SNDTIMEO: socket time out for receiving and sending message

Page 19: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Generic Socket Options(6) SO_REUSEADDR: reuse the established port

Allows a listening server to start and bind its well-known port even if previously established connections exist that use this port

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 hosting multiple HTTP servers using IP alias technique

Allows a single process to bind the same port to multiple socket as long as each bind specifies a different local IP address

Allows completely duplicate bindings when same IP address and port are already bound to another socket. Used with multicasting to allow the same application to be

run multiple times on the same host when the SO_REUSEPORT option is not supported

Page 20: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

TCP Socket Options

TCP_KEEPALIVE: specifies the idle time in seconds for the connection before TCP starts sending keepalive probes 7200 sec (2 hours), default Effective only when SO_KEEPALIVE socket option is enabled

TCP_MAXSEG: fetch or set MSS for TCP connection TCP_NODELAY

e.g) rlogin, telnet Nagle algorithm / delayed algorithm disabled TCP’s Nagle algorithm

No small packet( < MSS ) will not be sent until the existing data is ACKed

TCP’s delayed ACK algorithm piggyback: TCP sends an ACK after some small amount of time( 50 ~

200msec)

Page 21: More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c

Nagle Algorithm Enabled/Disabled

Nagle Algorithm Enabled No small packet( < MSS )

will not be sent until the existing data is ACKed

Nagle Algorithm Disabled