The length of the address of the accept function in <sys / socket.h>
in "sys / socket.h" it defines a function:
int accept (int socket, struct sockaddr *address, socklen_t *address_len);
My question is related to socklen_t * address_len , which, according to the manual, points to socklen_t , which in the input sets the length of the supplied sockaddr structure, and the output sets the length of the saved address.
Under what conditions is the size of the input address_len different from the output?
I need this so that I can emulate a test case on the shell that I created for the sockaddr_in structure.
Thanks a lot!
It can never be more (input length is a limitation to prevent overflow), but for certain types of sockets it can be less, for example, unix domain sockets, whose addresses are essentially paths. For IP sockets (v4 or v6) there will always be a nominal size of the corresponding sockaddr_in
or sockaddr_in6
structure.
Also note that you can use some interfaces like this without knowing what type of address / protocol family is involved. For example, you might have a function as part of your library code that accepts a socket of an unknown type and calls accept it using the sockaddr_storage
structure. He can find the IPv4 address and IPv6 address or something else completely, depending on what the caller did.
This usage is not quite typical for accept
, but it is much more likely for getpeername
, which uses the same interface style. In fact, all the daemons that work from inetd
and that want to know the remote address work like this.