Class: IPv4Header

Inherits:
Object
  • Object
show all
Defined in:
lib/sflow/lib/sflow/models/ipv4header.rb

Overview

TODO: チェックサムを確認する

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packet, offset = 0) ⇒ IPv4Header

Returns a new instance of IPv4Header.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 10

def initialize(packet,offset=0)
  @packet = packet.force_encoding("ASCII-8BIT")
  @offset = offset
  header = packet.unpack("x#{offset}n10")
  @version = header[0] >> 12
  @header_length  = ((header[0] >> 8) & 0x0f)*4
  @packet_length = header[1]
  @identification = header[2]
  @frag_dont = (header[3] >> 14) & 0x01 != 0
  @frag_more  = (header[3] >> 13) & 0x01 != 0
  @frag_offset = header[3] & 0x1fff
  @ttl = header[4] >> 8
  @protocol = header[4] & 0x00ff
  @checksum = header[5]
  @sndr_addr = ip_to_s(packet[12..15])
  @dest_addr = ip_to_s(packet[16..19])
  @data_length = @packet_length - @header_length

  @virtual_header = packet[12..19] + [0,6,@data_length].pack("CCn")

end

Instance Attribute Details

#checksumObject (readonly)

Returns the value of attribute checksum.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def checksum
  @checksum
end

#data_lengthObject (readonly)

Returns the value of attribute data_length.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def data_length
  @data_length
end

#dest_addrObject (readonly)

Returns the value of attribute dest_addr.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def dest_addr
  @dest_addr
end

#frag_dontObject (readonly)

Returns the value of attribute frag_dont.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def frag_dont
  @frag_dont
end

#frag_moreObject (readonly)

Returns the value of attribute frag_more.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def frag_more
  @frag_more
end

#frag_offsetObject (readonly)

Returns the value of attribute frag_offset.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def frag_offset
  @frag_offset
end

#header_lengthObject (readonly)

Returns the value of attribute header_length.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def header_length
  @header_length
end

#identificationObject (readonly)

Returns the value of attribute identification.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def identification
  @identification
end

#packet_lengthObject (readonly)

Returns the value of attribute packet_length.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def packet_length
  @packet_length
end

#protocolObject (readonly)

Returns the value of attribute protocol.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def protocol
  @protocol
end

#sndr_addrObject (readonly)

Returns the value of attribute sndr_addr.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def sndr_addr
  @sndr_addr
end

#ttlObject (readonly)

Returns the value of attribute ttl.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def ttl
  @ttl
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 7

def version
  @version
end

Instance Method Details

#dataObject



38
39
40
41
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 38

def data
  start = @offset+@header_length
  @packet[start..start+@data_length]
end

#get_virtual_headerObject



43
44
45
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 43

def get_virtual_header
  @virtual_header
end

#ip_to_s(ip) ⇒ Object



47
48
49
50
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 47

def ip_to_s(ip)
  ip = ip.unpack("n2")
  sprintf("%d.%d.%d.%d",ip[0]>>8,ip[0]&0x00ff,ip[1]>>8,ip[1]&0x00ff)
end

#to_sObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 52

def to_s
  "IPv4 Header\n" <<
  "  Version            : #{@version}\n" <<
  "  Header Length      : #{@header_length}\n" <<
  "  Packet Length      : #{@packet_length}\n" <<
  "  Identification     : #{@identification}\n" <<
  "  Don't fragment     : #{@frag_dont}\n" <<
  "  More fragments     : #{@frag_more}\n" <<
  "  Fragment Offset    : #{@frag_offset}\n" <<
  "  TTL                : #{@ttl}\n" <<
  "  Protocol           : #{Protocol.to_s(@protocol)}\n" <<
  "  Header Checksum    : #{@checksum}\n" <<
  "  Sender Address     : #{@sndr_addr}\n" <<
  "  Destination Address: #{@dest_addr}\n" <<
  "  (Data Length)      : #{@data_length}"
end

#upperObject



32
33
34
35
36
# File 'lib/sflow/lib/sflow/models/ipv4header.rb', line 32

def upper
  upper_header = Protocol.to_class(@protocol)
  offset = @offset+@header_length
  upper_header.new(@packet,offset,@data_length,self)
end