Hi *,
I am trying to find out if it is possible to obtain the header of an incoming UDP packet via its header. I am using the sendto/recvfrom functions but am curious as to whether it can be done. I am able to use the return value of recvfrom, but suppose that the buffer was not big enough then I don't mind the discarding of excess, but I still need to know how many bytes were in the original incoming packet.
here is the minimal server you should worry about.
Code:
int sockfd,n;
struct sockaddr_in servaddr,cliaddr;
socklen_t len;
int s_port = 9000;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(s_port);
bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
printf("Port: %d\n", s_port);
char buf[5000];
n = recvfrom(sockfd, buf, 5000, 0, (struct sockaddr *)&cliaddr, &len);
if (n < 0)
return -1;
// do more stuff
oh and I am not willing to recompile kernel or use external programs to feed this info, this is for high performance and should be accessible via standard *nix software API.
cheers