PDA

Click to See Complete Forum and Search --> : network programming


johnnymier
March 17th, 2004, 04:46 AM
Hello:
I am currently in a project that involves net programming inC. The part I must solve is how to read chunks from a given file, and then pack them into UDP packets. I am guessing that I most use fread() or read(). But how do I handle the correct assembly of packets at the receiver? Any clues, tips or references are welcome.


Thanks,
J

AngelofRevenge
March 17th, 2004, 08:01 AM
communication of that type is easy to access, use a packet sniffer, like a port sniffer, except reads packets.

SirDice
March 17th, 2004, 05:06 PM
Because it's UDP you need to code something and send it allong with the data to put it all back into order again. I also suggest sending along some sort of verification to make sure the packets came in unmolested.

Something like:

<normal udp header>[udp data contains:<sequence number><checksum><size><data>]

As I said you have to take care of the seq. number, checksum and size. If you send this information along it should be easy to verify the chucks and put them in the correct order on the receiving end.

gothic_type
March 18th, 2004, 12:32 AM
Why don't you just add a number at the start of each packet, and then when it gets to the receiving computer, it puts the packets into numerical order and strips the numbers back out. SirDice is right in that you should add in validation (I think it's validation, not verification...just being picky) of some kind, but I think that the tcp/ip network model handles that in any case.

ac

Relyt
March 18th, 2004, 01:35 AM
John,

The part I must solve is how to read chunks from a given file, and then pack them into UDP packets

Do a search at this site for comments about sniffers and of course www.google.com for one as well.

Don't be afraid of grabbing a packet builder to help you in your venture instead of just winging it.

www.engagesecurity.com/products/engagepacketbuilder/

johnnymier
March 18th, 2004, 03:39 AM
Hey, thanks everyone for your replies. I have another question: what is the max payload size that can be put on an UDP datagram? This is so i know the how to split a certain file in the right block sizes.

thanks
J

ammo
March 18th, 2004, 05:02 AM
Actually, the maximum size of a datagram is the biggest value you can fit in the length field of the udp header (header length + data length). Since the length field is 16 bits, you can fit 2^16-8 bytes in a single udp datagram.

And what about the mtu? Well that's the magic in the osi/tcp layers, you don't have to worry about the lower layers. If the ip packet the datagram is wrapped in encounters an mtu smaller that 64k (rather likely!), the IP packet will be fragged; however, IP handles the fragmentation and reassembly for you!


Ammo

johnnymier
March 19th, 2004, 05:51 AM
Ammo:
So I can partition the file in 2^16-8 bytes blocks and then use UDP socket primitive sendto() and I dont have to worry about IP or ethernet MTU's, cool
I need some insight on how to actually partition a file in a given size blocks and construct my UDP packet in C.
I am new to network programming and it sure is a complex subject. Any functions that might be useful for this will be greatly appreciated.

Thanks
J

PS will someone tell me why this thread's status is "very negative"?

SirDice
March 19th, 2004, 02:49 PM
Originally posted here (http://www.AntiOnline.com/showthread.php?threadid=255863#post730094) by johnnymier
PS will someone tell me why this thread's status is "very negative"?

It's because AngelofRevenge was the receiver of sh*tloads of negative antipoints.

jdenny
March 20th, 2004, 04:13 PM
and I dont have to worry about IP or ethernet MTU's, cool
Read this if you're coding on Windows platform: UDP Datagram Can Be Silently Discarded if Larger than MTU (http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q233/4/01.ASP&NoWebContent=1).

Peace always,
<jdenny>

johnnymier
March 24th, 2004, 01:34 AM
My advance so far: I have used read() to read from a file in a given block size and put that data in a struct. What happens if my block size is bigger that the whole file I am reading from?
Will my struct be filled with the actual contents of the file and then random data will be added. or zeroes added?
The protocol I'm implementing splits a file in 60k blocks, and it identifies the last packet as one with less than 60k of data.

thnks
J

ammo
March 24th, 2004, 01:57 AM
Well, it depends how exactly your "putting your data in a struct" but you probably will have random data (perhaps 0s, depending on the OS and/or malloc, and previous allocations...).

Generally you'll want to have a header for each packet (datagram) with the packet id and data length, and optionally but preferably a checksum to make sure your file is intact when all is said and done...

While it's true that the max for a datagram is technically 64K, that does imply fragmentation (at best) or the datagram being dropped (if the router doesn't want to frag...) or more chances of loosing a packet fragment in route...

You might want to check the code of a TFTP implementation to see all everything's done (and perhaps copy your whole implementation from a BSD licensed source if you feel like it!)


Ammo