I have finally managed to solve the problem and am adding the code here for others to use. I hope this is of help to others. This one is for IP_TTL:
Set the UDPSocket to receive IP_TTL values:
int ttl = 60;
if(setsockopt(udpSocket, IPPROTO_IP, IP_RECVTTL, &ttl,sizeof(ttl))<0)
{
printf("cannot set recvttl\n");
}
else
{
printf("socket set to recvttl\n");
}
And retrieve the IP_TTL values from every packet in the following manner(The following program can retrieve the data message via iov[0], code snippet given below):
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 *ttlptr=NULL;
int received_ttl = 0;
int cmsg_size = sizeof(struct cmsghdr)+sizeof(received_ttl); // NOTE: Size of header + size of data
char buf[CMSG_SPACE(sizeof(received_ttl))];
msg.msg_control = buf; // Assign buffer space for control header + header data/value
msg.msg_controllen = sizeof(buf); //just initializing it
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_TTL) &&
(cmsg->cmsg_len) ){
ttlptr = (int *) CMSG_DATA(cmsg);
received_ttl = *ttlptr;
printf("received_ttl = %i and %d \n", ttlptr, received_ttl);
break;
}
}
}
The data message can be send and obtained in the following manner:
Sender Side:
struct DATA_to_SEND pkt;
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);
nRet = sendmsg(udpSocket, &msg,0);
Receiver Side (assumption DATA_To_SEND has a parameter named "seq"):
struct DATA_to_SEND pkt;
seqNum = ((struct DATA_to_SEND *) iov[0].iov_base)->seq;
The following is for IP_TOS.
Set the socket 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;
}
}
}