Class: PcapParser::Ethertype::IPv4

Inherits:
Object
  • Object
show all
Defined in:
lib/pcap_parser/ethertype/ipv4.rb

Overview

IP protocal version 4 Always big-endian (networking) byte order

Constant Summary collapse

LENGTH =

IPv4 header length is 20 bytes

20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex_string) ⇒ IPv4

Returns a new instance of IPv4


10
11
12
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 10

def initialize(hex_string)
  @hexstr = hex_string
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options


6
7
8
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 6

def options
  @options
end

Instance Method Details

#chsumObject


65
66
67
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 65

def chsum
  @hexstr[10..11].unpack("n").pop
end

#congestion?Boolean

Returns:

  • (Boolean)

34
35
36
37
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 34

def congestion?
  # last 2 bits
  (@hexstr[1].unpack("C").pop & 0b11) > 0
end

#flagObject

first 3 bit of 16b Int


48
49
50
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 48

def flag
  @hexstr[6..7].unpack("n").pop >> 13
end

#frag_offsetObject

last 13 bit of 16b Int


53
54
55
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 53

def frag_offset
  @hexstr[6..7].unpack("n").pop & 0x1fff
end

#has_opts?Boolean

Returns:

  • (Boolean)

26
27
28
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 26

def has_opts?
  self.header_len > 20
end

#header_lenObject

Return length in bytes IHL * 32 = TOTAL bits / 8 = TOTAL bytes

  • 32 == shift left 5

/ 8 == shift right 3


22
23
24
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 22

def header_len
  (@hexstr[0].unpack("C").pop & 0b1111) << 2
end

#idObject


43
44
45
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 43

def id
  @hexstr[4..5].unpack("n").pop
end

#int2ip(ip_int) ⇒ Object

Human readable IP address


97
98
99
100
101
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 97

def int2ip(ip_int)
  Array(0..3).reverse.
    map { |x| (ip_int >>(x<<3)) & 0xff }.
    join ?.
end

#ip_dstObject


92
93
94
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 92

def ip_dst
  int2ip ip_dst_long
end

#ip_dst_longObject


84
85
86
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 84

def ip_dst_long
  @hexstr[16..20].unpack("N").pop
end

#ip_srcObject


88
89
90
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 88

def ip_src
  int2ip ip_src_long
end

#ip_src_longObject


80
81
82
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 80

def ip_src_long
  @hexstr[12..15].unpack("N").pop
end

#lengthObject


39
40
41
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 39

def length
  @hexstr[2..3].unpack("n").pop
end

#protoObject


61
62
63
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 61

def proto
  @hexstr[9].unpack("C").pop
end

#proto_supported?Boolean

Returns:

  • (Boolean)

76
77
78
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 76

def proto_supported?
  PROTO.keys.include? proto
end

#tosObject


30
31
32
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 30

def tos
  @hexstr[1].unpack("C").pop >> 2
end

#ttlObject


57
58
59
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 57

def ttl
  @hexstr[8].unpack("C").pop
end

#valid?Boolean

Check if packet corrupted using check sum en.wikipedia.org/wiki/IPv4_header_checksum

Returns:

  • (Boolean)

71
72
73
74
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 71

def valid?
  sum = @hexstr.unpack("n*").inject :+
  ((sum>>16) + (sum & 0xffff)) == 0xffff
end

#versionObject


14
15
16
# File 'lib/pcap_parser/ethertype/ipv4.rb', line 14

def version
  @hexstr[0].unpack("C").pop >> 4
end