「生活可以更简单, 欢迎来到我的开源世界」
  1. 7.1 概述
  2. 7.2 getsockopt和setsockopt函数
  3. 7.3 检查选项是否受支持并获取默认值
  4. 7.4 套接字状态
  5. 7.5 通用套接字选项
    1. SO_BROADCAST套接字选项
    2. SO_DEBUG套接字选项
    3. SO_DONTROUTE套接字选项
    4. SO_ERROR套接字选项
  6. 7.6 ipv4套接字选项
  7. 7.7 icmpv6套接字选项
  8. 7.8 ipv6套接字选项
  9. 7.9 tcp套接字选项
  10. 7.10 sctp套接字选项
  11. 7.11 fcntl函数
Unix网络编程-第7章 套接字选项
2019-12-26
」 「

第7章 套接字选项

7.1 概述

获取和设置套接字选项的方法:

7.2 getsockopt和setsockopt函数

这两个函数仅用于套接字:

#include <sys/socket.h>
//若成功都返回0,出错都返回-1
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);

套接字选项粗分为两大基本类型:

7.3 检查选项是否受支持并获取默认值

/* include checkopts1 */
/* *INDENT-OFF* */
#include "unp.h"
#include <netinet/tcp.h> /* for TCP_xxx defines */

//getsockopt的每个可能的返回值,union类型中都有一个成员
union val {
int i_val;
long l_val;
struct linger linger_val;
struct timeval timeval_val;
} val;

//用于输出给定套接字选项的值的4个函数的原型
static char *sock_str_flag(union val *, int);
static char *sock_str_int(union val *, int);
static char *sock_str_linger(union val *, int);
static char *sock_str_timeval(union val *, int);

//定义结构体,声明并定义结构体数组
struct sock_opts {
const char *opt_str;
int opt_level;
int opt_name;
char *(*opt_val_str)(union val *, int); //函数指针,指向输出函数
} sock_opts[] = {
{ "SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, sock_str_flag },
{ "SO_DEBUG", SOL_SOCKET, SO_DEBUG, sock_str_flag },
{ "SO_DONTROUTE", SOL_SOCKET, SO_DONTROUTE, sock_str_flag },
{ "SO_ERROR", SOL_SOCKET, SO_ERROR, sock_str_int },
{ "SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, sock_str_flag },
{ "SO_LINGER", SOL_SOCKET, SO_LINGER, sock_str_linger },
{ "SO_OOBINLINE", SOL_SOCKET, SO_OOBINLINE, sock_str_flag },
{ "SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, sock_str_int },
{ "SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, sock_str_int },
{ "SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, sock_str_int },
{ "SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, sock_str_int },
{ "SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, sock_str_timeval },
{ "SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, sock_str_timeval },
{ "SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, sock_str_flag },
#ifdef SO_REUSEPORT
{ "SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, sock_str_flag },
#else
{ "SO_REUSEPORT", 0, 0, NULL },
#endif
{ "SO_TYPE", SOL_SOCKET, SO_TYPE, sock_str_int },
{ "SO_USELOOPBACK", SOL_SOCKET, SO_USELOOPBACK, sock_str_flag },
{ "IP_TOS", IPPROTO_IP, IP_TOS, sock_str_int },
{ "IP_TTL", IPPROTO_IP, IP_TTL, sock_str_int },
#ifdef IPV6_DONTFRAG
{ "IPV6_DONTFRAG", IPPROTO_IPV6,IPV6_DONTFRAG, sock_str_flag },
#else
{ "IPV6_DONTFRAG", 0, 0, NULL },
#endif
#ifdef IPV6_UNICAST_HOPS
{ "IPV6_UNICAST_HOPS", IPPROTO_IPV6,IPV6_UNICAST_HOPS,sock_str_int },
#else
{ "IPV6_UNICAST_HOPS", 0, 0, NULL },
#endif
#ifdef IPV6_V6ONLY
{ "IPV6_V6ONLY", IPPROTO_IPV6,IPV6_V6ONLY, sock_str_flag },
#else
{ "IPV6_V6ONLY", 0, 0, NULL },
#endif
{ "TCP_MAXSEG", IPPROTO_TCP,TCP_MAXSEG, sock_str_int },
{ "TCP_NODELAY", IPPROTO_TCP,TCP_NODELAY, sock_str_flag },
#ifdef SCTP_AUTOCLOSE
{ "SCTP_AUTOCLOSE", IPPROTO_SCTP,SCTP_AUTOCLOSE,sock_str_int },
#else
{ "SCTP_AUTOCLOSE", 0, 0, NULL },
#endif
#ifdef SCTP_MAXBURST
{ "SCTP_MAXBURST", IPPROTO_SCTP,SCTP_MAXBURST, sock_str_int },
#else
{ "SCTP_MAXBURST", 0, 0, NULL },
#endif
#ifdef SCTP_MAXSEG
{ "SCTP_MAXSEG", IPPROTO_SCTP,SCTP_MAXSEG, sock_str_int },
#else
{ "SCTP_MAXSEG", 0, 0, NULL },
#endif
#ifdef SCTP_NODELAY
{ "SCTP_NODELAY", IPPROTO_SCTP,SCTP_NODELAY, sock_str_flag },
#else
{ "SCTP_NODELAY", 0, 0, NULL },
#endif
{ NULL, 0, 0, NULL }
};
/* *INDENT-ON* */
/* end checkopts1 */

/* include checkopts2 */
int
main(int argc, char **argv)
{
int fd;
socklen_t len;
struct sock_opts *ptr;

for (ptr = sock_opts; ptr->opt_str != NULL; ptr++) {
printf("%s: ", ptr->opt_str);
if (ptr->opt_val_str == NULL)
printf("(undefined)\n");
else {
switch(ptr->opt_level) {
case SOL_SOCKET:
case IPPROTO_IP:
case IPPROTO_TCP:
fd = Socket(AF_INET, SOCK_STREAM, 0);
break;
#ifdef IPV6
case IPPROTO_IPV6:
fd = Socket(AF_INET6, SOCK_STREAM, 0);
break;
#endif
#ifdef IPPROTO_SCTP
case IPPROTO_SCTP:
fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
break;
#endif
default:
err_quit("Can't create fd for level %d\n", ptr->opt_level);
}

len = sizeof(val);
//不支持的选项应该会引发一个ENOPROTOOPT错误
if (getsockopt(fd, ptr->opt_level, ptr->opt_name,
&val, &len) == -1) {
err_ret("getsockopt error");
} else {
printf("default = %s\n", (*ptr->opt_val_str)(&val, len));
}
close(fd);
}
}
exit(0);
}
/* end checkopts2 */

/* include checkopts3 */
static char strres[128];

static char *
sock_str_flag(union val *ptr, int len)
{
/* *INDENT-OFF* */
if (len != sizeof(int))
snprintf(strres, sizeof(strres), "size (%d) not sizeof(int)", len);
else
snprintf(strres, sizeof(strres),
"%s", (ptr->i_val == 0) ? "off" : "on");
return(strres);
/* *INDENT-ON* */
}
/* end checkopts3 */

static char *
sock_str_int(union val *ptr, int len)
{
if (len != sizeof(int))
snprintf(strres, sizeof(strres), "size (%d) not sizeof(int)", len);
else
snprintf(strres, sizeof(strres), "%d", ptr->i_val);
return(strres);
}

static char *
sock_str_linger(union val *ptr, int len)
{
struct linger *lptr = &ptr->linger_val;

if (len != sizeof(struct linger))
snprintf(strres, sizeof(strres),
"size (%d) not sizeof(struct linger)", len);
else
snprintf(strres, sizeof(strres), "l_onoff = %d, l_linger = %d",
lptr->l_onoff, lptr->l_linger);
return(strres);
}

static char *
sock_str_timeval(union val *ptr, int len)
{
struct timeval *tvptr = &ptr->timeval_val;

if (len != sizeof(struct timeval))
snprintf(strres, sizeof(strres),
"size (%d) not sizeof(struct timeval)", len);
else
snprintf(strres, sizeof(strres), "%d sec, %d usec",
tvptr->tv_sec, tvptr->tv_usec);
return(strres);
}

7.4 套接字状态

对于某些套接字选项,针对套接字的状态,什么时候设置或获取选项有时序上的考虑。

TCP已连接套接字选项从监听套接字继承的选项:SO_DEBUG、SO_DONTROUTE、SO_KEEPALIVE、SO_LINGER、SO_OOBINLINE、SO_RCVBUF、SO_RCVLOWAT、SO_SNDBUF、SO_SNDLOWAT、TCP_MAXSEG和TCP_NODELAY。

因为accept一直要到三次握手完成时,才会给服务器返回已连接套接字,如果想在三次握手完成时确保这些套接字选项中的某一个是给已连接套接字设置的,那么必须先给监听套接字设置该选项。

7.5 通用套接字选项

通用套接字选项是协议无关的,由内核中协议无关代码处理。某些通用套接字选项只能应用到某些特定类型的套接字中。

SO_BROADCAST套接字选项

开启或禁止进程发送广播消息的能力,只有数据报套接字支持广播,并且还必须在支持广播消息的网络上。

SO_DEBUG套接字选项

仅由TCP支持,开启本选项时,内核将为TCP在该套接字发送和接收的所有分组保留详细跟踪信息。

SO_DONTROUTE套接字选项

本选项规定外出的分组将绕过底层协议的正常路由机制。

SO_ERROR套接字选项

7.6 ipv4套接字选项

7.7 icmpv6套接字选项

7.8 ipv6套接字选项

7.9 tcp套接字选项

7.10 sctp套接字选项

7.11 fcntl函数

<⇧>