From: “Ari B.” [email protected]
- Can UDP packets be used to emulate basic IP or TCP packets?
Not exactly. UDP is a transport layer protocol, at the same
level as TCP. You could emulate the behavior of TCP in UDP,
by writing your own streaming protocol on top of UDP.
If you really want to manually construct your own IP packets,
it’s possible to use raw sockets.
The IPPROTO_ constants ruby knows about are:
Socket.constants.sort.grep /IPPROTO_/
=> [“IPPROTO_GGP”, “IPPROTO_ICMP”, “IPPROTO_IDP”, “IPPROTO_IGMP”,
“IPPROTO_IP”, “IPPROTO_MAX”, “IPPROTO_ND”, “IPPROTO_PUP”, “IPPROTO_RAW”,
“IPPROTO_TCP”, “IPPROTO_UDP”]
For example, this would be creating an ICMP packet:
sock = Socket.new(Socket::PF_INET, Socket::SOCK_RAW,
Socket::IPPROTO_ICMP)
To see an example using ICMP packets, gem install net-ping, and look at:
lib/net/ping/icmp.rb.
You could similarly create your own TCP packet:
sock = Socket.new(Socket::PF_INET, Socket::SOCK_RAW,
Socket::IPPROTO_TCP)
…constructing the data for a TCP packet properly, I’ll leave
to you. (Note: On some operating systems, I think you may
need admin privileges to use RAW sockets.)
From: “Peña, Botp” [email protected]
From: Ari B. [mailto:[email protected]]
- Is there a (slightly) easier way to do it, or should I just start
writing a wrapper?
you might want to take a peek at EventMachine…
I’d second that recommendataion! (Although, if Ari is really
wanting to do raw sockets, EventMachine doesn’t support that.)
Regards,
Bill