Specific explanations and tests of getsockopt and setsockopt under Linux

Source: Internet
Author: User

Specific explanations and tests of getsockopt and setsockopt under Linux

NAME

name

GetSockOpt, Setsockopt-get and SET options on sockets

Gets or sets the options for the socket


Synopsis

Function prototypes
       #include <sys/types.h>          /* See NOTES *       /#include <sys/socket.h>       int getsockopt (int sockfd, int level, int optname,                      void *optval, socklen_t *optlen);       int setsockopt (int sockfd, int level, int optname,                      const void *optval, socklen_t optlen);

Number of references:

Sock: The socket that will be set or get the option.
Level: The protocol layer where the option is located.
Optname: The name of the option that needs to be interviewed.


Optval: For GetSockOpt (), point to the buffer that returns the value of the option.

For setsockopt (), point to the buffer that includes the new option value.


Optlen: For GetSockOpt (), the maximum length of the option value as the entry parameter. The actual length of the option value as the export parameter.

For setsockopt (), the size, in bytes, of the optval buffer.

level specifies the hierarchy of the control sockets. Can take three kinds of values:
1) Sol_socket: Universal socket option.
2) ipproto_ip:ip option.
3) ipproto_tcp:tcp option.


RETURN VALUE

return value

On success, the zero is returned. On error, 1 are returned, and errno is set appropriately.


Error return

ERRORS       EBADF The     argument sockfd is not a valid descriptor.       Efault the address pointed to by Optval are not in a valid part of the  process address space.  For getsockopt (), this error could                 also be returned if Optlen was not in a valid part of the process address SPACE.//
   einval    Optlen Invalid in setsockopt ().  In some cases this error can also occur for a invalid value  in  optval  (e.g., for the                 ip_ad D_membership option described in IP (7)).       Enoprotoopt the option is unknown at the level                 indicated.       Enotsock  The argument sockfd is a file and not a socket.



Ebadf:sock is not a valid document descriptive narrative word
The memory that Efault:optval points to is not a valid process space
EINVAL: Optlen Invalid when calling setsockopt ()
ENOPROTOOPT: The specified protocol layer does not recognize the option
Enotsock:sock description is not a socket


The socket options and socket options for the IP layer are summarized in the third edition of the UNIX Network programming Volume One P151 figure 7-1

The following is a small code to check whether the option is supported and gets the default value:

[email protected]:/myworkspace/unixnetwork/unpv13e/sockopt# cat-n checkopts.c 1/* include checkopts1 */2/*     *indent-off* */3#include "unp.h" 4#include<netinet/tcp.h>/* for TCP_XXX defines */5 6union val {     7 Inti_val;     8 Longl_val;    9 struct lingerlinger_val;    ten struct timevaltimeval_val;    One} Val;    13static Char*sock_str_flag (Union val *, int);    14static Char*sock_str_int (Union val *, int);    15static Char*sock_str_linger (Union val *, int);    16static Char*sock_str_timeval (Union val *, int);    18struct sock_opts {const char *opt_str;    Intopt_level;    Intopt_name;    char * (*OPT_VAL_STR) (Union val *, int); Sock_opts[] = {24{"So_broadcast", Sol_socket,so_broadcast,sock_str_flag}, 25{"So_debug", sol_socket,so_debug,s Ock_str_flag}, 26{"So_dontroute", Sol_socket,so_dontroute,sock_str_flag}, 27{"So_error", SOL_SOCKET,SO_ERROR,sock_ Str_int}, 28{"So_keepalive", Sol_Socket,so_keepalive,sock_str_flag}, 29{"So_linger", Sol_socket,so_linger,sock_str_linger}, 30{"SO_OOBINLINE", SOL_ Socket,so_oobinline,sock_str_flag}, 31{"So_rcvbuf", Sol_socket,so_rcvbuf,sock_str_int}, 32{"SO_SNDBUF", SOL_SOCKET , so_sndbuf,sock_str_int}, 33{"So_rcvlowat", Sol_socket,so_rcvlowat,sock_str_int}, 34{"So_sndlowat", SOL_SOCKET,SO_ Sndlowat,sock_str_int}, 35{"So_rcvtimeo", Sol_socket,so_rcvtimeo,sock_str_timeval}, 36{"SO_SNDTIMEO", SOL_SOCKET,S O_sndtimeo,sock_str_timeval}, 37{"So_reuseaddr", Sol_socket,so_reuseaddr,sock_str_flag}, 38#ifdefSO_REUSEPORT 3  9{"So_reuseport", Sol_socket,so_reuseport,sock_str_flag}, 40#else 41{"So_reuseport", 0,0,null}, 42#endif 43{ "So_type", Sol_socket,so_type,sock_str_int}, 44//{"So_useloopback", Sol_socket,so_useloopback,sock_str_flag}, 45{ "Ip_tos", Ipproto_ip,ip_tos,sock_str_int}, 46{"Ip_ttl", Ipproto_ip,ip_ttl,sock_str_int}, 47#ifdefIPV6_DONTFRAG 4 8{"Ipv6_dontfrag", IpproTo_ipv6,ipv6_dontfrag,sock_str_flag}, 49#else 50{"Ipv6_dontfrag", 0,0,null}, 51#endif 52#ifdefipv6_unicast_h OPS 53{"Ipv6_unicast_hops", Ipproto_ipv6,ipv6_unicast_hops,sock_str_int}, 54#else 55{"Ipv6_unicast_hops", 0,0,NU LL}, 56#endif 57#ifdefipv6_v6only 58{"Ipv6_v6only", Ipproto_ipv6,ipv6_v6only,sock_str_flag}, 59#else 60{ "Ipv6_v6only", 0,0,null}, 61#endif 62{"Tcp_maxseg", Ipproto_tcp,tcp_maxseg,sock_str_int}, 63{"TCP_NODELAY", IPPR Oto_tcp,tcp_nodelay,sock_str_flag}, 64#ifdefsctp_autoclose 65{"Sctp_autoclose", Ipproto_sctp,sctp_autoclose,sock_ Str_int}, 66#else 67{"Sctp_autoclose", 0,0,null}, 68#endif 69#ifdefsctp_maxburst 70{"Sctp_maxburst", IPPR    Oto_sctp,sctp_maxburst,sock_str_int}, 71#else 72{"Sctp_maxburst", 0,0,null}, 73#endif 74#ifdefsctp_maxseg 75{"Sctp_maxseg", Ipproto_sctp,sctp_maxseg,sock_str_int}, 76#else 77{"Sctp_maxseg", 0,0,null}, 78#endif 79# Ifdefsctp_nodelay 80{"Sctp_nodelay ", Ipproto_sctp,sctp_nodelay,sock_str_flag}, 81#else 82{" Sctp_nodelay ", 0,0,null}, 83#endif 84{N    Ull,0,0,null} 85}; 86/* *indent-on* */87/* End CHECKOPTS1 */89/* include checkopts2 */90int 91main (int argc, char **argv    ) 92{93intfd;    94socklen_tlen;    95struct sock_opts*ptr;    97for (ptr = sock_opts; Ptr->opt_str! = NULL; ptr++) {98printf ("%s:", PTR-&GT;OPT_STR);   99if (Ptr->opt_val_str = = NULL) 100printf ("(undefined) \ n"); 101else {102switch (ptr->opt_level) {103case sol_socket:104case ipproto_ip:105case ipproto_tcp:106fd = So   Cket (af_inet, sock_stream, 0);   107break;   108#ifdefipv6 109case ipproto_ipv6:110fd = Socket (Af_inet6, sock_stream, 0);   111break;   112#endif 113#IFDEFIPPROTO_SCTP 114case ipproto_sctp:115fd = Socket (Af_inet, Sock_seqpacket, IPPROTO_SCTP);   116break;   117#endif 118default:119err_quit ("Can ' t create fd for Level%d\n", ptr->opt_level); 120} 121 122len = sizeof (val); 123if (getsockopt (FD, Ptr->opt_level, Ptr->opt_name, 124 &val, &len) = =-1) {125err_ret ("GetSockOpt E   Rror ");   126} else {127printf ("default =%s\n", (*PTR-&GT;OPT_VAL_STR) (&val, Len));   129close (FD);   131} 132exit (0);   133} 134/* End CHECKOPTS2 */135 136/* include CHECKOPTS3 */137static charstrres[128]; 138 139static char* 140sock_str_flag (union val *ptr, int len) 141{142/* *indent-off* */143if (len! = sizeof (in   T)) 144snprintf (strres, sizeof (strres), "size (%d) not sizeof (int)", Len); 145else 146snprintf (strres, sizeof (strres), 147 "%s", (Ptr->i_val = = 0)?   "Off": "on");   148return (Strres);   149/* *indent-on* * * * * 151/* End CHECKOPTS3 */153static char* 154sock_str_int (union val *ptr, int len)   155{156if (len! = sizeof (int)) 157snprintf (strres, sizeof (strres), "size (%d) not sizeof (int)", Len); 158else 159snprintf (strres, sizeof (strres), "%D ", ptr->i_val);   160return (Strres); 161} 162 163static char* 164sock_str_linger (union val *ptr, int len) 165{166struct linger*lptr = &ptr->l   Inger_val; 167 168if (len! = sizeof (struct linger)) 169snprintf (strres, sizeof (strres), "size (%d) not sizeof (struct linger   ) ", Len);   171else 172snprintf (strres, sizeof (strres), "L_onoff =%d, L_linger =%d", 173 lptr->l_onoff, Lptr->l_linger);   174return (Strres); 175} 176 177static char* 178sock_str_timeval (union val *ptr, int len) 179{180struct timeval*tvptr = &AMP;PTR-&G   T;timeval_val; 181 182if (len! = sizeof (struct timeval)) 183snprintf (strres, sizeof (strres), 184 "size (%d) not sizeof (struct TIMEV   AL) ", Len);   185else 186snprintf (strres, sizeof (strres), "%d sec,%d usec", 187 tvptr->tv_sec, tvptr->tv_usec);   188return (Strres); 189}[email protected]:/myworkspace/unixnetwork/unpv13e/sockopt# Mans getsockopt

Test Execution Results





Test: UNIX Network Programming Volume One

Linux Programmer ' s Manual

Specific explanations and tests of getsockopt and setsockopt under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.