getsockopt returns different IP_TOS value from the one set in setsockopt

岁酱吖の 提交于 2019-12-23 15:41:06

问题


I'm trying to use setsockopt to set IPTOS value to IPTOS_THROUGHPUT. The setsockopt call returned 0. However the getsockopt shows the IP_TOS value is set to 1, which is different from IPTOS_THROUGHPUT (0x8). Does anyone have idea what could have caused the mismatch of IPTOS value in the setsockopt and getsockopt?

Here's the log output:

Set DSCP Marking on socket 26

setsockopt on socket 26 to 8 returns 0

DSCP marking on socket 26 is 1, different from expected 8

Below is the code:

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:


You should use the level option IPPROTO_IP instead of IPPROTO_TCP



来源:https://stackoverflow.com/questions/13162655/getsockopt-returns-different-ip-tos-value-from-the-one-set-in-setsockopt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!