in "sys/socket.h" it defines the function:
int accept (int socket, struct sockaddr *address, socklen_t *address_len);
My question has to do with socklen_t *address_len which based on the manual points to a socklen_t which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the stored address.
Under what conditions will the address_len size input be different from output?
I need this so I can emulate a test case on a wrapper I have created for the sockaddr_in structure.
Thanks a lot!
It can never be more (the input length is a bounds to prevent overflows), but it could be less for certain socket types, for instance unix domain sockets whose addresses are essentially pathnames. For IP (v4 or v6) sockets, it will always be the nominal size of the corresponding sockaddr_in
or sockaddr_in6
structure.
Also note that it's possible to use some interfaces like this without knowing what type of address/protocol family is involved. For instance you might have a function as part of your library code that takes a socket of unknown type and calls accept on it with a sockaddr_storage
structure. It might find an IPv4 address, and IPv6 address, or something else entirely, depending on what the caller did.
This usage is fairly non-typical for accept
, but it's a lot more likely for getpeername
which uses the same interface style. In fact this is the way all daemons that run from inetd
and which want to know the remote address must operate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With