Hi all,
I am trying to get the TOS and TTL values for a received multicast UDP
packet. The code I have written (pasted below) works well if I try to
get just the TTL or try to get just the TOS. However, if I try to get
both the TTL and TOS it does not work. The length of the control
message (msghdr. msg_controllen) comes out to be 0 in this case.
I am assuming that once I do setsockopt with the IP_RECVTOS option and
IP_RECVTTL option, and then I do a recvmsg on the socket, I should be
able to get the TTL and TOS as control message in the structure msghdr.
Am I correct? I would guess so since they work individually but not
together.
Please let me know if I am doing anything wrong or if anyone else had
faced this problem before.
Thanks
bhaskar
Linux version: GV 2.3.4 Linux/RedHat Enterprise 3 AS
Compiler version: gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20)
// setting the RECVTTL option
unsigned char tos = 1;
int tosSize = sizeof(tos);
if(setsockopt(sd,IPPROTO_IP, IP_RECVTOS, &tos,tosSize)<0)
{
printf("cannot set RECV_TOS \n");
exit(1);
}
// setting the RECVTOS option
unsigned char ttl = 1;
int ttlSize = sizeof(ttl);
if(setsockopt(sd,IPPROTO_IP, IP_RECVTTL, &ttl,ttlSize)<0)
{
printf("cannot set RECV_TTL \n");
exit(1);
}
/* infinite server loop */
while(1) {
struct msghdr ttlHdr;
ttlHdr.msg_control = (struct cmsghdr*)malloc (10*sizeof(struct
cmsghdr));
// Peek at message header to get the TTL and TOS
if ( recvmsg (sd, &ttlHdr, MSG_PEEK) == -1 )
{
printf("Error in recvmsg");
exit(0);
}
printf("The lenght of control message = %d\n",
ttlHdr.msg_controllen);
struct cmsghdr *cmsg;
int *ttlptr;
unsigned char *tosptr;
int received_ttl;
unsigned char received_tos;
int found = 0;
/* Receive auxiliary data in msgh */
for (cmsg = CMSG_FIRSTHDR(&ttlHdr);
cmsg != NULL;
cmsg = CMSG_NXTHDR(&ttlHdr,cmsg))
{
printf("type = %d \n", cmsg->cmsg_type);
if (cmsg->cmsg_level == IPPROTO_IP
&& cmsg->cmsg_type == IP_TTL)
{
ttlptr = (int *) CMSG_DATA(cmsg);
received_ttl = *ttlptr;
printf ("------------>The ttl received is %d\n", received_ttl);
found = 1;
}
if (cmsg->cmsg_level == IPPROTO_IP
&& cmsg->cmsg_type == IP_TOS)
{
tosptr = (unsigned char*) CMSG_DATA(cmsg);
received_tos = *tosptr;
printf ("------------>The tos received is %x\n", received_tos);
found = 1;
}
}
|