Class: Iphdr

Inherits:
Object
  • Object
show all
Defined in:
lib/emtraceroute/ip.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proto = Socket::IPPROTO_ICMP, src = '0.0.0.0', dst = nil) ⇒ Iphdr

Returns a new instance of Iphdr.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/emtraceroute/ip.rb', line 9

def initialize(proto=Socket::IPPROTO_ICMP, src='0.0.0.0', dst=nil)
  @version = 4
  @hlen = 5
  @tos = 0
  @length = 20
  @id = $$
  @frag = 0
  @ttl = 255
  @proto = proto
  @cksum = 0
  @src = src
  @saddr = Socket.inet_aton(src)
  @dst = dst || '0.0.0.0'
  @daddr = Socket.inet_aton(@dst)
  @data = ''
end

Instance Attribute Details

#cksumObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def cksum
  @cksum
end

#daddrObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def daddr
  @daddr
end

#dataObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def data
  @data
end

#dstObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def dst
  @dst
end

#fragObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def frag
  @frag
end

#hlenObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def hlen
  @hlen
end

#idObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def id
  @id
end

#lengthObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def length
  @length
end

#protoObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def proto
  @proto
end

#saddrObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def saddr
  @saddr
end

#srcObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def src
  @src
end

#tosObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def tos
  @tos
end

#ttlObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def ttl
  @ttl
end

#versionObject

This represents an IP packet header.

Iphdr#assemble packages the packet Iphdr.disassemble disassembles the packet



7
8
9
# File 'lib/emtraceroute/ip.rb', line 7

def version
  @version
end

Class Method Details

.disassemble(data) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/emtraceroute/ip.rb', line 35

def self.disassemble data
  ip = Iphdr.new
  pkt = data[0..11].unpack('CCnnnCCn')
  ip.version = (pkt.first >> 4 & 0x0f)
  ip.hlen = (pkt.first & 0x0f)
  ip.tos, ip.length, ip.id, ip.frag, ip.ttl, ip.proto, ip.cksum = pkt[1..-1]
  ip.saddr = data[12..15]
  ip.daddr = data[16..19]
  ip.src = Socket.inet_ntoa(ip.saddr)
  ip.dst = Socket.inet_ntoa(ip.daddr)
  ip
end

Instance Method Details

#assembleObject



26
27
28
29
30
31
32
33
# File 'lib/emtraceroute/ip.rb', line 26

def assemble
  header = [(@version & 0x0f) << 4 | (@hlen & 0x0f),
            @tos, @length + @data.size,
            Socket.htons(@id), @frag,
            @ttl, @proto
           ].pack('CCSSSCC')
  header + "\000\000" + @saddr.to_s + @daddr.to_s + @data
end