Class: Arp

Inherits:
Object show all
Includes:
Args
Defined in:
lib/arp.rb

Constant Summary collapse

HwSrc =
Class.new(Ethernet::Address)
HwTgt =
Class.new(Ethernet::Address)
ProtoSrc =
Class.new(IPAddr)
ProtoTgt =
Class.new(IPAddr)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Args

#ivars, #set, #to_hash

Constructor Details

#initialize(arg = {}) ⇒ Arp

Returns a new instance of Arp.



17
18
19
20
21
22
23
24
25
26
# File 'lib/arp.rb', line 17

def initialize(arg={})
  if arg.is_a?(Hash)
    @hw_tgt = HwTgt.new
    @opcode, @hw_src, @proto_src, @proto_tgt = 1, nil, nil, nil
    set arg
  elsif arg.is_a?(String)
    parse arg
  else
  end
end

Instance Attribute Details

#hw_srcObject (readonly)

Returns the value of attribute hw_src.



15
16
17
# File 'lib/arp.rb', line 15

def hw_src
  @hw_src
end

#hw_tgtObject (readonly)

Returns the value of attribute hw_tgt.



15
16
17
# File 'lib/arp.rb', line 15

def hw_tgt
  @hw_tgt
end

#opcodeObject (readonly)

Returns the value of attribute opcode.



15
16
17
# File 'lib/arp.rb', line 15

def opcode
  @opcode
end

#proto_srcObject (readonly)

Returns the value of attribute proto_src.



15
16
17
# File 'lib/arp.rb', line 15

def proto_src
  @proto_src
end

#proto_tgtObject (readonly)

Returns the value of attribute proto_tgt.



15
16
17
# File 'lib/arp.rb', line 15

def proto_tgt
  @proto_tgt
end

Class Method Details

.new_reply(arg = {}) ⇒ Object



70
71
72
73
74
75
# File 'lib/arp.rb', line 70

def Arp.new_reply(arg={})
  if arg.is_a?(Hash)
    arg[:opcode]=2
    new arg
  end
end

.new_request(arg = {}) ⇒ Object



64
65
66
67
68
69
# File 'lib/arp.rb', line 64

def Arp.new_request(arg={})
  if arg.is_a?(Hash)
    arg[:opcode]=1
    new arg
  end
end

Instance Method Details

#encodeObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/arp.rb', line 28

def encode
  s=[]
  hw_src = @hw_src.encode
  proto_src = @proto_src.hton
  hw_tgt = @hw_tgt.encode
  proto_tgt = @proto_tgt.hton
  s << eth_hdr
  s << [1,0x0800, 6, 4, @opcode, hw_src, proto_src, hw_tgt, proto_tgt].pack("nnCCn"+"a6a4"*2)
  s.join
end

#parse(s) ⇒ Object

Raises:

  • (RuntimeError)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/arp.rb', line 39

def parse(s)
  s.slice!(0,14) if s.size>32
  htype, ptype, hlen, plen, opcode = s.unpack('nnccn')
  hw_src, proto_src, hw_tgt, proto_tgt = s[8..-1].unpack("a#{hlen}a#{plen}"*2)
  raise RuntimeError, "Unsupported Hardware Type" unless htype == 1 && ptype == 0x0800
  @hw_src = HwSrc.new(hw_src)
  @hw_tgt = HwTgt.new(hw_tgt)
  @proto_src = ProtoSrc.ntop(proto_src)
  @proto_tgt = ProtoTgt.ntop(proto_tgt)
  @opcode = opcode
end

#reply(hw_src) ⇒ Object



58
59
60
61
# File 'lib/arp.rb', line 58

def reply(hw_src)
  self.class.new_reply :hw_src=> hw_src, :proto_src=> "#{@proto_tgt}",
                 :hw_tgt => "#{@hw_src}", :proto_tgt => "#{@proto_src}"
end

#to_sObject



51
52
53
54
55
56
# File 'lib/arp.rb', line 51

def to_s
  case @opcode
  when 1 ; "ARP, Request who-has #{@proto_tgt} tell #{@proto_src}"
  when 2 ; "ARP, Reply #{@proto_src} is-at #{@hw_src}"
  end
end