问题描述
我正在尝试使用 setsockopt
将 IPTOS
值设置为 IPTOS_THROUGHPUT
. setsockopt
调用返回 0
.但是, getsockopt
显示 IP_TOS
值设置为 1
,这与 IPTOS_THROUGHPUT
( 0x8
).有谁知道是什么原因导致了 setsockopt
和 getsockopt
中的 IPTOS
值不匹配?
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
?
以下是日志输出:
套接字26到8上的setsockopt返回0
setsockopt on socket 26 to 8 returns 0
插槽26上的DSCP标记为1,与预期的8不同
DSCP marking on socket 26 is 1, different from expected 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;
}
}
}
推荐答案
您应该使用级别选项 IPPROTO_IP
而不是 IPPROTO_TCP
You should use the level option IPPROTO_IP
instead of IPPROTO_TCP
这篇关于getsockopt返回与setockopt中的一组不同的IP_TOS值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!