Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are socket options inherited across accept() from the listening socket?

Tags:

c

posix

sockets

Suppose the listening socket passed to accept has non-default options set on it with setsockopt. Are these options (some or all of them?) inherited by the resulting file descriptors for accepted connections?

like image 871
R.. GitHub STOP HELPING ICE Avatar asked May 11 '11 17:05

R.. GitHub STOP HELPING ICE


People also ask

Can a socket accept without listening?

No and no. The socket isn't put into listening mode until you call listen() . It must be listening in order to accept() . And, once you're listening, you cannot convert the socket to a connected socket.

What are socket options in network programming?

In addition to binding a socket to a local address or connecting it to a destination address, application programs need a method to control the socket. For example, when using protocols that use time out and retransmission, the application program may want to obtain or set the time-out parameters.

Why do we use Setsockopt?

Remarks. The setsockopt function sets the current value for a socket option associated with a socket of any type, in any state. Although options can exist at multiple protocol levels, they are always present at the uppermost socket level.

What is generic socket?

These socket options are protocol independent meaning that the protocol independent code within the kernel handles these and not particular module of any protocol. Some options apply to only certain types of sockets.


1 Answers

Several of the socket options are handled at lower levels of the system. While most of the socket options could be set using the setsockopt. Reference:man setsockopt And since you are mentioning only POSIX on any Linux, in general, as your scope. The accept() (Reference: man accept) does have a certain amount of discretion on what socket options should be inherited and what options to reject from the listening fd.

accept() does not modify the original socket passed to it as argument. The new socket returned by accept() does not inherit file status flags such as O_NONBLOCK,O_ASYNC from the listening socket.

So, instead of relying on the inheritance or non-inheritance of the listening socket properties(which is bound to vary across implementations and licenses), the accepted socket should be explicitly set with the desired socket options.(Best practice)

man pages and the implementation codes in your machine would be the most relevant specification for the accept() behavior.There's no common or standard specification existing across multiple variants of Linux.

like image 116
askmish Avatar answered Sep 18 '22 16:09

askmish