【问题标题】:getsockopt returns different IP_TOS value from the one set in setsockoptgetsockopt 返回与 setsockopt 中的一组不同的 IP_TOS 值
【发布时间】:2012-10-21 04:32:28
【问题描述】:

我正在尝试使用setsockoptIPTOS 值设置为IPTOS_THROUGHPUTsetsockopt 调用返回 0。然而getsockopt 显示IP_TOS 值设置为1,这与IPTOS_THROUGHPUT (0x8) 不同。有谁知道是什么原因导致setsockoptgetsockopt 中的IPTOS 值不匹配?

这是日志输出:

在套接字 26 上设置 DSCP 标记

socket 26 到 8 上的 setsockopt 返回 0

套接字 26 上的 DSCP 标记为 1,与预期的 8 不同

下面是代码:

int iptos = IPTOS_THROUGHPUT;

log(debug, 10, "Set DSCP Marking on socket %d\n", sockfd);

retval = setsockopt(sockfd, IPPROTO_TCP, IP_TOS,  &iptos, sizeof(iptos));
if (retval<0) {
   log(error, 99, "Failed to set DSCP marking on socket %d with error %d\n",
      sockfd, retval);
} else {
   log(debug, 10, "setsockopt on socket %d to %d returns %d\n", sockfd, iptos,
      retval);
   int tos=0;
   socklen_t toslen=sizeof(tos);

   retval = getsockopt(sockfd, IPPROTO_TCP, IP_TOS,  &tos, &toslen);
   if(retval<0) {
      log(warning, 99, "Failed to get DSCP marking on socket %d with error %d\n",
         sockfd, retval);
   }else {
      if( tos != iptos ) {
         log(warning, 99, "DSCP marking on socket %d is %d, different from expected %d\n",
            sockfd, tos, iptos);
         retval = 9999;
      }
      else {
         log(debug, 10, "Success: Set DSCP Marking on socket %d to %d\n",
            sockfd, iptos);
         retval = 0;
      }
   }
}

【问题讨论】:

    标签: c linux sockets setsockopt getsockopt


    【解决方案1】:

    您应该使用级别选项IPPROTO_IP 而不是IPPROTO_TCP

    【讨论】:

    • 非常感谢胡里奥!这就是问题所在!
    猜你喜欢
    • 2019-06-16
    • 2011-09-02
    • 2013-07-06
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    相关资源
    最近更新 更多