gpt4 book ai didi

c++ - 从 Linux 中 TCP 套接字上接收到的数据包中获取 TOS 字段?

转载 作者:行者123 更新时间:2023-12-05 07:42:31 24 4
gpt4 key购买 nike

我有一个使用 TCP 套接字在 Linux 上运行的 C++ 网络应用程序。我一直在尝试从接收到的 TCP 数据包中获取 DSCP 标记。我们有 UDP 的 IP_RECVTOS 套接字选项。但我已经在代码中测试过它不适用于 TCP。

除了原始套接字之外,我还有什么选择可以解决这个问题?

   Example code : The following is for UDP. I am looking for something similar for TCP. 

//Set the socket option to receive IP_TOS:

unsigned char set = 0x03;
if(setsockopt(udpSocket, IPPROTO_IP, IP_RECVTOS, &set,sizeof(set))<0)
{
printf("cannot set recvtos\n");
}
else
{
printf("socket set to recvtos\n");
}

// and Retrieve the IP_TOS value from every packet header by:

struct PC_Pkt pkt;
int *ecnptr;
unsigned char received_ecn;
struct msghdr msg;
struct iovec iov[1];
memset(&msg, '\0', sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = 1;
iov[0].iov_base = (char *) &pkt;
iov[0].iov_len = sizeof(pkt);

int cmsg_size = sizeof(struct cmsghdr)+sizeof(received_ecn);
char buf[CMSG_SPACE(sizeof(received_ecn))];
msg.msg_control = buf;
msg.msg_controllen = sizeof(buf);

nRet = recvmsg(udpSocket, &msg, 0);

if (nRet > 0) {
struct cmsghdr *cmsg;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msg,cmsg)) {
if ((cmsg->cmsg_level == IPPROTO_IP) &&
(cmsg->cmsg_type == IP_TOS) && (cmsg->cmsg_len) ){
ecnptr = (int *) CMSG_DATA(cmsg);
received_ecn = *ecnptr;
int isecn = ((received_ecn & INET_ECN_MASK) == INET_ECN_CE);

printf("received_ecn = %i and %d, is ECN CE marked = %d \n", ecnptr, received_ecn, isecn);

break;
}
}
}

最佳答案

link表示 IP_RECVTOS 只能用于获取对等 UDP TOS,但不能用于 TCP。但这是 10 年前。

关于c++ - 从 Linux 中 TCP 套接字上接收到的数据包中获取 TOS 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44413671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com