LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-22-2007, 12:53 AM   #1
thelordmule
LQ Newbie
 
Registered: Jul 2006
Location: Australia
Distribution: Mac OSX 10.6, Ubuntu 10.10
Posts: 23

Rep: Reputation: 0
trivia: extracting length from UDP packets


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
 
Old 01-22-2007, 07:30 PM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
You can try passing MSG_TRUNC for the flags argument. This will make recvfrom return the length of the packet received, even if it was truncated (ie. the size of the packet, not the amount of data that was stored in the buffer). If it's larger than the buffer length, then you can figure out how much was cut off by subtracting the buffer length from the return value.

Last edited by 95se; 01-22-2007 at 07:31 PM.
 
Old 01-23-2007, 10:46 PM   #3
thelordmule
LQ Newbie
 
Registered: Jul 2006
Location: Australia
Distribution: Mac OSX 10.6, Ubuntu 10.10
Posts: 23

Original Poster
Rep: Reputation: 0
I was planning to use MSG_PEEK to find out the actual amount of data readable from the recv buffer, but I found that I cannot use this in winsock since it is strongly discouraged. I would still like to know the amount that was readable and the amount delivered. thanks for the help though.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
UDP: Short Packets: and UDP bad checksum: entries in dmesg minutes2memories Linux - Networking 2 02-26-2006 07:28 PM
How to read udp datagram length? sceadu Programming 1 01-05-2005 08:22 PM
udp packets m00t00 Programming 1 11-04-2004 09:20 PM
encapsulating TCP packets in UDP packets... yoshi95 Programming 3 06-03-2004 02:53 PM
How to receive UDP and ICMP packets, by one UDP socket(PMTUD) myself_rajat Linux - Networking 0 05-28-2004 05:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration