3

I have a C++ network application running on linux using TCP sockets. I have been trying to fetch the DSCP marking from a received packet for TCP. We have IP_RECVTOS socket option for UDP. But I have tested in code that it is not working for TCP.

What options do I have except raw sockets to address the issue ?

   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;
   }
    }
  }
user1765709
  • 441
  • 1
  • 4
  • 8
  • Can you post a [mcve]? I am not aware of this but seem that DSCP is about IP and not UDP or TCP. – Stargateur Jun 07 '17 at 13:17
  • [related](https://stackoverflow.com/questions/13162655/getsockopt-returns-different-ip-tos-value-from-the-one-set-in-setsockopt) – Stargateur Jun 07 '17 at 13:19
  • Thanks ! Yes I understand that DSCP is in IP header. But when we are exchanging packets over TCP/UDP and need some ancillary info(like an L3 field in this case), kernel allows for setting socket options and handover that info as ancillary data. Editing the post to help understand. Thanks ! – user1765709 Jun 07 '17 at 14:33

1 Answers1

0

This link said the IP_RECVTOS can only be used to fetch peer UDP TOS, but not for TCP. but it is 10 years before.

HobbyTan
  • 51
  • 4