views:

94

answers:

6

In testing certain network device driver receive features, I need to send special packets on the wire. I know I need to open a raw socket and push the bytes out. Is there some well-known example (C, perl, whatever) code already available for playing at this level?

(added later) I would prefer non-platform-specific answers, they'll be the most useful for everyone.

+2  A: 

http://www.codeproject.com/KB/IP/sendrawpacket.aspx

There's already an existing project that may be able to help you with this.

Robert Greiner
Hmmm - this looks like a platform specific answer (aspx) and I have to set up some account to get access? Bah.
Shannon Nelson
A: 

I can't think of any examples. But you should just be able to open up a UDP socket to any IP address you like and start writing data to it. Make sure its UDP or this will not work.

EToreo
+2  A: 

Look at the documentation for packet. Basically, you create a socket with SOCK_RAW or SOCK_DGRAM, then write to the socket using normal socket i/o. However, the data you send will be put directly on the line, rather than automatically getting the headers that are necessary for most network interop.

JSBangs
A: 

I found that there's a good C example here at Security-Freak, which only needed a little modification for flexibility. I'm hoping there are more answers in other languages.

Shannon Nelson
It may be poor form so select one's own answer, but this is what I was looking for and found for myself: simple, quick and dirty code that already existed.
Shannon Nelson
+1  A: 

Seems to me you are looking for a tool to generate your own packets, Scapy is such a tool often used in the security industry (such as pentesters).

Demo is available: http://www.secdev.org/projects/scapy/demo.html

Jay Zeng