we can see the api is defined as blow
int getsockopt(int sockfd, int level, int optname,
void *optval, socklen_t *optlen);
Question 1:
Suppose given a level and optname, then we know the optlen, why do we need the user to provide the optlen parameter as well?
even user provide the optlen, why it is a pointer to socklen_t rather than socklen_t directly? do the implementation will change the value of optlen somehow?
Thanks
Have you looked at the Linux man page for
getsockopt(2)
or the POSIX specification ofgetsockopt()
?The
optlen
parameter is an in-out parameter. On input, it specifies how much space is available in theoptval
space, and on output, it reports how much of the space inoptval
was written to.Yes, the implementation changes the value of
*optlen
.For example, POSIX says:
The specification uses
option_len
where you usedoptlen
, andoption_value
where you usedoptval
.