getsockopt返回setsockopt中设置的不同IP_TOS值

时间:2012-10-31 16:35:56

标签: c linux sockets setsockopt getsockopt

我正在尝试使用setsockoptIPTOS值设置为IPTOS_THROUGHPUTsetsockopt调用已返回0。但是,getsockopt显示IP_TOS值设置为1,这与IPTOS_THROUGHPUT0x8)不同。有谁知道可能导致IPTOSsetsockopt getsockopt值不匹配的原因是什么?

这是日志输出:

  

在套接字26上设置DSCP标记

     

套接字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;
      }
   }
}

1 个答案:

答案 0 :(得分:4)

您应该使用级别选项IPPROTO_IP代替IPPROTO_TCP