linux下getsockopt和setsockopt详解及测试

NAME

名字

getsockopt, setsockopt - get and set options on sockets

获取或者设置套接字的选项

SYNOPSIS

函数原型

  1. #include <sys/types.h> /* See NOTES */
  2. #include <sys/socket.h>
  3.  
  4. int getsockopt(int sockfd, int level, int optname,
  5. void *optval, socklen_t *optlen);
  6. int setsockopt(int sockfd, int level, int optname,
  7. const void *optval, socklen_t optlen);

参数:

sock:将要被设置或者获取选项的套接字。
level:选项所在的协议层。
optname:需要访问的选项名。

optval:对于getsockopt(),指向返回选项值的缓冲。

对于setsockopt(),指向包含新选项值的缓冲。

optlen:对于getsockopt(),作为入口参数时,选项值的最大长度。作为出口参数时,选项值的实际长度。

对于setsockopt(),The size, in bytes, of the optval buffer.

level指定控制套接字的层次.可以取三种值:
     1)SOL_SOCKET:通用套接字选项.
     2)IPPROTO_IP:IP选项.
     3)IPPROTO_TCP:TCP选项.

RETURN VALUE

返回值

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

错误返回

  1. ERRORS
  2. EBADF The argument sockfd is not a valid descriptor.
  3.  
  4. EFAULT The address pointed to by optval is not in a valid part of the process address space. For getsockopt(), this error may
  5. also be returned if optlen is not in a valid part of the process address space.//
  6.  
  7. EINVAL optlen invalid in setsockopt(). In some cases this error can also occur for an invalid value in optval (e.g., for the
  8. IP_ADD_MEMBERSHIP option described in ip(7)).
  9.  
  10. ENOPROTOOPT
  11. The option is unknown at the level indicated.
  12.  
  13. ENOTSOCK The argument sockfd is a file, not a socket.

EBADF:sock不是有效的文件描述词
EFAULT:optval指向的内存并非有效的进程空间
EINVAL:在调用setsockopt()时,optlen无效
ENOPROTOOPT:指定的协议层不能识别选项
ENOTSOCK:sock描述的不是套接字

套接字选项和IP层的套接字选项汇总见《unix网络编程第三版卷一》P151图7-1

下面用一段小代码来检查选项是否受支持并获取默认值:

  1. root@wl-Lenovo-B590:/myworkspace/unixnetwork/unpv13e/sockopt# cat -n checkopts.c
  2. 1 /* include checkopts1 */
  3. 2 /* *INDENT-OFF* */
  4. 3 #include "unp.h"
  5. 4 #include <netinet/tcp.h> /* for TCP_xxx defines */
  6. 5
  7. 6 union val {
  8. 7 int i_val;
  9. 8 long l_val;
  10. 9 struct linger linger_val;
  11. 10 struct timeval timeval_val;
  12. 11 } val;
  13. 12
  14. 13 static char *sock_str_flag(union val *, int);
  15. 14 static char *sock_str_int(union val *, int);
  16. 15 static char *sock_str_linger(union val *, int);
  17. 16 static char *sock_str_timeval(union val *, int);
  18. 17
  19. 18 struct sock_opts {
  20. 19 const char *opt_str;
  21. 20 int opt_level;
  22. 21 int opt_name;
  23. 22 char *(*opt_val_str)(union val *, int);
  24. 23 } sock_opts[] = {
  25. 24 { "SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, sock_str_flag },
  26. 25 { "SO_DEBUG", SOL_SOCKET, SO_DEBUG, sock_str_flag },
  27. 26 { "SO_DONTROUTE", SOL_SOCKET, SO_DONTROUTE, sock_str_flag },
  28. 27 { "SO_ERROR", SOL_SOCKET, SO_ERROR, sock_str_int },
  29. 28 { "SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, sock_str_flag },
  30. 29 { "SO_LINGER", SOL_SOCKET, SO_LINGER, sock_str_linger },
  31. 30 { "SO_OOBINLINE", SOL_SOCKET, SO_OOBINLINE, sock_str_flag },
  32. 31 { "SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, sock_str_int },
  33. 32 { "SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, sock_str_int },
  34. 33 { "SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, sock_str_int },
  35. 34 { "SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, sock_str_int },
  36. 35 { "SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, sock_str_timeval },
  37. 36 { "SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, sock_str_timeval },
  38. 37 { "SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, sock_str_flag },
  39. 38 #ifdef SO_REUSEPORT
  40. 39 { "SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, sock_str_flag },
  41. 40 #else
  42. 41 { "SO_REUSEPORT", 0, 0, NULL },
  43. 42 #endif
  44. 43 { "SO_TYPE", SOL_SOCKET, SO_TYPE, sock_str_int },
  45. 44 // { "SO_USELOOPBACK", SOL_SOCKET, SO_USELOOPBACK, sock_str_flag },
  46. 45 { "IP_TOS", IPPROTO_IP, IP_TOS, sock_str_int },
  47. 46 { "IP_TTL", IPPROTO_IP, IP_TTL, sock_str_int },
  48. 47 #ifdef IPV6_DONTFRAG
  49. 48 { "IPV6_DONTFRAG", IPPROTO_IPV6,IPV6_DONTFRAG, sock_str_flag },
  50. 49 #else
  51. 50 { "IPV6_DONTFRAG", 0, 0, NULL },
  52. 51 #endif
  53. 52 #ifdef IPV6_UNICAST_HOPS
  54. 53 { "IPV6_UNICAST_HOPS", IPPROTO_IPV6,IPV6_UNICAST_HOPS,sock_str_int },
  55. 54 #else
  56. 55 { "IPV6_UNICAST_HOPS", 0, 0, NULL },
  57. 56 #endif
  58. 57 #ifdef IPV6_V6ONLY
  59. 58 { "IPV6_V6ONLY", IPPROTO_IPV6,IPV6_V6ONLY, sock_str_flag },
  60. 59 #else
  61. 60 { "IPV6_V6ONLY", 0, 0, NULL },
  62. 61 #endif
  63. 62 { "TCP_MAXSEG", IPPROTO_TCP,TCP_MAXSEG, sock_str_int },
  64. 63 { "TCP_NODELAY", IPPROTO_TCP,TCP_NODELAY, sock_str_flag },
  65. 64 #ifdef SCTP_AUTOCLOSE
  66. 65 { "SCTP_AUTOCLOSE", IPPROTO_SCTP,SCTP_AUTOCLOSE,sock_str_int },
  67. 66 #else
  68. 67 { "SCTP_AUTOCLOSE", 0, 0, NULL },
  69. 68 #endif
  70. 69 #ifdef SCTP_MAXBURST
  71. 70 { "SCTP_MAXBURST", IPPROTO_SCTP,SCTP_MAXBURST, sock_str_int },
  72. 71 #else
  73. 72 { "SCTP_MAXBURST", 0, 0, NULL },
  74. 73 #endif
  75. 74 #ifdef SCTP_MAXSEG
  76. 75 { "SCTP_MAXSEG", IPPROTO_SCTP,SCTP_MAXSEG, sock_str_int },
  77. 76 #else
  78. 77 { "SCTP_MAXSEG", 0, 0, NULL },
  79. 78 #endif
  80. 79 #ifdef SCTP_NODELAY
  81. 80 { "SCTP_NODELAY", IPPROTO_SCTP,SCTP_NODELAY, sock_str_flag },
  82. 81 #else
  83. 82 { "SCTP_NODELAY", 0, 0, NULL },
  84. 83 #endif
  85. 84 { NULL, 0, 0, NULL }
  86. 85 };
  87. 86 /* *INDENT-ON* */
  88. 87 /* end checkopts1 */
  89. 88
  90. 89 /* include checkopts2 */
  91. 90 int
  92. 91 main(int argc, char **argv)
  93. 92 {
  94. 93 int fd;
  95. 94 socklen_t len;
  96. 95 struct sock_opts *ptr;
  97. 96
  98. 97 for (ptr = sock_opts; ptr->opt_str != NULL; ptr++) {
  99. 98 printf("%s: ", ptr->opt_str);
  100. 99 if (ptr->opt_val_str == NULL)
  101. 100 printf("(undefined)\n");
  102. 101 else {
  103. 102 switch(ptr->opt_level) {
  104. 103 case SOL_SOCKET:
  105. 104 case IPPROTO_IP:
  106. 105 case IPPROTO_TCP:
  107. 106 fd = Socket(AF_INET, SOCK_STREAM, 0);
  108. 107 break;
  109. 108 #ifdef IPV6
  110. 109 case IPPROTO_IPV6:
  111. 110 fd = Socket(AF_INET6, SOCK_STREAM, 0);
  112. 111 break;
  113. 112 #endif
  114. 113 #ifdef IPPROTO_SCTP
  115. 114 case IPPROTO_SCTP:
  116. 115 fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
  117. 116 break;
  118. 117 #endif
  119. 118 default:
  120. 119 err_quit("Can't create fd for level %d\n", ptr->opt_level);
  121. 120 }
  122. 121
  123. 122 len = sizeof(val);
  124. 123 if (getsockopt(fd, ptr->opt_level, ptr->opt_name,
  125. 124 &val, &len) == -1) {
  126. 125 err_ret("getsockopt error");
  127. 126 } else {
  128. 127 printf("default = %s\n", (*ptr->opt_val_str)(&val, len));
  129. 128 }
  130. 129 close(fd);
  131. 130 }
  132. 131 }
  133. 132 exit(0);
  134. 133 }
  135. 134 /* end checkopts2 */
  136. 135
  137. 136 /* include checkopts3 */
  138. 137 static char strres[128];
  139. 138
  140. 139 static char *
  141. 140 sock_str_flag(union val *ptr, int len)
  142. 141 {
  143. 142 /* *INDENT-OFF* */
  144. 143 if (len != sizeof(int))
  145. 144 snprintf(strres, sizeof(strres), "size (%d) not sizeof(int)", len);
  146. 145 else
  147. 146 snprintf(strres, sizeof(strres),
  148. 147 "%s", (ptr->i_val == 0) ? "off" : "on");
  149. 148 return(strres);
  150. 149 /* *INDENT-ON* */
  151. 150 }
  152. 151 /* end checkopts3 */
  153. 152
  154. 153 static char *
  155. 154 sock_str_int(union val *ptr, int len)
  156. 155 {
  157. 156 if (len != sizeof(int))
  158. 157 snprintf(strres, sizeof(strres), "size (%d) not sizeof(int)", len);
  159. 158 else
  160. 159 snprintf(strres, sizeof(strres), "%d", ptr->i_val);
  161. 160 return(strres);
  162. 161 }
  163. 162
  164. 163 static char *
  165. 164 sock_str_linger(union val *ptr, int len)
  166. 165 {
  167. 166 struct linger *lptr = &ptr->linger_val;
  168. 167
  169. 168 if (len != sizeof(struct linger))
  170. 169 snprintf(strres, sizeof(strres),
  171. 170 "size (%d) not sizeof(struct linger)", len);
  172. 171 else
  173. 172 snprintf(strres, sizeof(strres), "l_onoff = %d, l_linger = %d",
  174. 173 lptr->l_onoff, lptr->l_linger);
  175. 174 return(strres);
  176. 175 }
  177. 176
  178. 177 static char *
  179. 178 sock_str_timeval(union val *ptr, int len)
  180. 179 {
  181. 180 struct timeval *tvptr = &ptr->timeval_val;
  182. 181
  183. 182 if (len != sizeof(struct timeval))
  184. 183 snprintf(strres, sizeof(strres),
  185. 184 "size (%d) not sizeof(struct timeval)", len);
  186. 185 else
  187. 186 snprintf(strres, sizeof(strres), "%d sec, %d usec",
  188. 187 tvptr->tv_sec, tvptr->tv_usec);
  189. 188 return(strres);
  190. 189 }
  191. root@wl-Lenovo-B590:/myworkspace/unixnetwork/unpv13e/sockopt# man getsockopt

测试运行结果如图:

linux下getsockopt和setsockopt详解及测试的更多相关文章

  1. linux下getsockopt和setsockopt具体解释及測试

    linux下getsockopt和setsockopt具体解释及測试 NAME 名字 getsockopt, setsockopt - get and set options on sockets 获 ...

  2. Linux下tomcat的安装详解

    Linux下tomcat的安装详解 来源: ChinaUnix博客 日期: 2007.01.21 22:59 (共有0条评论) 我要评论 一,安装前的准备:1,Linux版本:我的是企业版.(至于红帽 ...

  3. Linux下的文件目录结构详解

    Linux下的文件目录结构详解 / Linux文件系统的上层根目录 /bin 存放用户可执行的程序 /boot 操作系统启动时所需要的文件 /dev 接口设备文件目录,例如:had表示硬盘 /etc ...

  4. Linux下find命令用法详解

    Linux下find命令用法详解   学神VIP烟火 学神IT教育:XueGod-IT   最负责任的线上直播教育平台   本文作者为VIP学员 烟火   第一部分:根据文件名查找   1.在当前目录 ...

  5. linux下sort命令使用详解---linux将文本文件内容加以排序命令

    转载自:http://www.cnblogs.com/hitwtx/archive/2011/12/03/2274592.html linux下sort命令使用详解---linux将文本文件内容加以排 ...

  6. Linux下DNS服务器搭建详解

    Linux下DNS服务器搭建详解 DNS  即Domain Name System(域名系统)的缩写,它是一种将ip地址转换成对应的主机名或将主机名转换成与之相对应ip地址的一种机制.其中通过域名解析 ...

  7. Linux下usb设备驱动详解

    USB驱动分为两块,一块是USB的bus驱动,这个东西,Linux内核已经做好了,我们可以不管,我们只需要了解它的功能.形象的说,USB的bus驱动相当于铺出一条路来,让所有的信息都可以通过这条USB ...

  8. Linux下面的yum命令详解

    yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RP ...

  9. Linux下内存查看及详解

    在Linux下面,我们常用top命令来查看系统进程,top也能显示系统内存.我们常用的Linux下查看内容的专用工具是free命令. Linux下内存查看命令free详解: 在Linux下查看内存我们 ...

随机推荐

  1. tensorflow的升级与版本管理

    1 查看cuda与cudnn的版本 cuda一般安装在 /usr/local/cuda/ 路径下,该路径下有一个version.txt文档,里面记录了cuda的版本信息 cat  /usr/local ...

  2. tensorflow prelu的实现细节

    tensorflow prelu的实现细节 output = tf.nn.leaky_relu(input, alpha=tf_gamma_data,name=name) #tf.nn.leaky_r ...

  3. C艹函数与结构体

    传递指针 代码: #include <iostream> #include <cmath> struct polar{ double distance; double angl ...

  4. linux 软硬链接

    1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...

  5. Failed to import package with error: Couldn't decompress package的解决方案

    问题的出现 在Unity中导入一个Package文件,出现以下出错信息 解决方案 遇到这样的报错信息,一般就是要将该文件包的存储路径设置为英文,因为Unity不识别中文路径:其次对文件包的命名中间应没 ...

  6. e559. 创建窗口

    A frame is a component container that displays its contents in a top-level window with a title bar a ...

  7. Python——signal

    该模块为在Python中使用信号处理句柄提供支持.下面是一些使用信号和他们的句柄时需要注意的事项: 除了信号 SIGCHLD 的句柄遵从底层的实现外,专门针对一个信号的句柄一旦设置,除非被明确地重置, ...

  8. List转DataTable(反射) ; 将泛型集合类转换成DataTable ; 将集合类转换成DataTable

    public class ConvertX { #region 将集合类转换成DataTable /// <summary> /// 将集合类转换成DataTable /// </s ...

  9. 腾讯爱玩某处csrf导致骚扰用户(QQ弹窗+QQ会话+微博)

    点击提醒,然后抓包: POST /dyid_proc.php HTTP/1.1 Host: tx.qq.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW ...

  10. 【WP8】ScrollViewer滑动到底触发器(ListBox失效)

    很多时候会有到底加载更多的需求,而ScrollViewer不支持继承,无法继承它进行扩展,只能通过触发器来控制到底的事件(当然,可以通过UserControl去扩展) 思路:定义一个Trigger,自 ...