# Bit-struct and constructing ARP messages

**URL:** <https://www.ruby-forum.com/t/bit-struct-and-constructing-arp-messages/99714>\
**Category:** Ruby\
**Created:** [July 25, 2006, 3:02am UTC](https://www.ruby-forum.com/t/bit-struct-and-constructing-arp-messages/99714 "2006-07-25T03:02:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![inspire22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a4c791/32.png) [@inspire22](https://www.ruby-forum.com/u/inspire22)\
**Post date:** [July 25, 2006, 3:02am UTC](https://www.ruby-forum.com/t/bit-struct-and-constructing-arp-messages/99714/1 "2006-07-25T03:02:03Z")

</div>

Hello all.

I have been trying to write a ARP packet injector script for Ruby  
and did quite a bit of research on how to construct ARP Reply datagrams  
and what not, and now that I think I have an idea how to construct them  
but also I’d like to transmit them to a host.

Courtesy of bit-struct I am able to craft and fill in the headers of an  
IPv4 datagram with ease, but transmitting this information is not so  
easy. What I’d like to know is is there some specific syntax using  
Socket#new for just transmitting packets not to any specific TCP port?,  
this is a snippet from the script:

* * *

class IP \< BitStruct  
unsigned :ip\_v, 4, “Version”  
unsigned :ip\_hl, 4, “Header length”  
unsigned :ip\_tos, 8, “TOS”  
unsigned :ip\_len, 16, “Length”  
unsigned :ip\_id, 16, “ID”  
unsigned :ip\_off, 16, “Frag offset”  
unsigned :ip\_ttl, 8, “TTL”  
unsigned :ip\_p, 8, “Protocol”  
unsigned :ip\_sum, 16, “Checksum”  
octets :ip\_src, 32, “Source addr”  
octets :ip\_dst, 32, “Dest addr”  
rest :body, “Body of message”

```
      note " rest is application defined message body"

      initial_value.ip_v = 4
      initial_value.ip_hl = 5

```

end

def mk\_dgram(src\_ip,dst\_ip,src\_h,dst\_h)  
arp\_msg = [‘1’, ‘0x800’, ‘6’, ‘4’, ‘2’, src\_h, src\_ip, dst\_h,  
dst\_ip]

```
    ip = IP.new
    ip.ip_tos = 0
    ip.ip_len = 0
    ip.ip_id = 0
    ip.ip_off = 0
    ip.ip_ttl = 255
    ip.ip_p = 255
    ip.ip_sum = 0
    ip.ip_src = src_ip
    ip.ip_dst = dst_ip
    ip.body = arp_msg.to_s
    ip.ip_len = ip.length
    puts ip.inspect_detailed

    # TRANSMISSION CODE WOULD GO HERE
    # .....

```

## end

Now I’m stuck with transmitting this datagram, any idea how I would  
implement this?. Also I’m just taking a guess there as how to craft an  
ARP Message encapsulated in an IP datagram so if you know the correct  
way of doing that too your feedback is much appreciated.

I apologize in advance if this was not an appropriate place to post this  
subject .

---

<div class="post-metadata">

**Author:** ![inspire22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a4c791/32.png) [@inspire22](https://www.ruby-forum.com/u/inspire22)\
**Post date:** [July 25, 2006, 6:20am UTC](https://www.ruby-forum.com/t/bit-struct-and-constructing-arp-messages/99714/2 "2006-07-25T06:20:00Z")

</div>

You can achieve the goal using Array#pack / String#unpack by  
hand. But there are already good libraries supporting C function  
calling and type conversion in Ruby: ruby-dl2[1](http://rubyforge.org/cgi-bin/viewvc.cgi/ruby-dl2/doc/struct.txt?revision=1.2&root=ruby-dl2&view=markup), ruby-dlx[2](http://ruby-dlx.rubyforge.org/).

HTH,

---

<div class="post-metadata">

**Author:** ![inspire22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a4c791/32.png) [@inspire22](https://www.ruby-forum.com/u/inspire22)\
**Post date:** [July 25, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/bit-struct-and-constructing-arp-messages/99714/3 "2006-07-25T21:33:57Z")

</div>

Gyoung-Yoon N. wrote:

> You can achieve the goal using Array#pack / String#unpack by  
> hand. But there are already good libraries supporting C function  
> calling and type conversion in Ruby: ruby-dl2[1](http://rubyforge.org/cgi-bin/viewvc.cgi/ruby-dl2/doc/struct.txt?revision=1.2&root=ruby-dl2&view=markup), ruby-dlx[2](http://ruby-dlx.rubyforge.org/).
> 
> HTH,

Array#pack / Array#unpack is useful for packaging the datagram yes, what  
I want to know is what Class#method would I use to transmit an ARP  
message contained in a datagram since it’s not going to any TCP port,  
it’s just a broadcast arp message.

---

<div class="post-metadata">

**Author:** ![inspire22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a4c791/32.png) [@inspire22](https://www.ruby-forum.com/u/inspire22)\
**Post date:** [July 27, 2006, 4:11am UTC](https://www.ruby-forum.com/t/bit-struct-and-constructing-arp-messages/99714/4 "2006-07-27T04:11:03Z")

</div>

Sorry for misreading you post.

I’d like to say that I haven’t done such a thing before.  
However, I just found that arping[1](http://www.habets.pp.se/synscan/programs.php?prog=arping) uses the libnet[2](http://www.packetfactory.net/libnet/) library,  
which also has Ruby binding[3](http://www.shmoo.com/~bmc/software/ruby/).  
Sorry for telling without doing.

Regards,
