I tried to use SO_RCVTIMEO option to timeout when there is no data in recvfrom API. But this is not taking effect, and the code is stuck when there is no data from the source.
I even tried using recv instead of recvfrom. Still the same issue.
Please let me know if anything is missing in my code.
int sock_r=socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
if(sock_r<0)
{
printf("open_raw_socket(): error in opening the socket");
return -1;
}
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
int ret = setsockopt(sock_r, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
perror("error: ");
socklen_t ntrcv, ntsnd;
struct timeval trcv;
ntrcv = sizeof(struct timeval);
if (getsockopt(sock_r, SOL_SOCKET, SO_RCVTIMEO, &trcv, &ntrcv) < 0)
{
perror("2");
TF_MSG("error");
}
while(1)
{
saddr_len=sizeof saddr;
recv_len = recvfrom(sock_r,buffer,buflen,0,&saddr,(socklen_t *)&saddr_len);
if(recv_len<=0)
{
printf("error in reading recvfrom function");
return 0;
}
.....