代码如下:
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);
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);
}
sock_str_flag函数:将标志选项转换为字符串
static char strres[128];
static char*
sock_str_flag(union val *ptr,int len)
{
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);
}