Class: OSPFv2::OspfPacket

Inherits:
Object show all
Includes:
OSPFv2, Common
Defined in:
lib/packet/ospf_packet.rb,
lib/packet/ospf_packet.rb

Constant Summary collapse

HELLO =
1
DATABASE_DESCRIPTION =
2
3
4
5

Constants included from OSPFv2

ASBR_SUMMMARY_LSA, AllDRouters, AllSPFRouters, AreaId, CheckAge, DefaultDestination, EXTERNAL_BASE_ADDRESS, EXTERNAL_LSA, IPPROTO_OSPF, InitialSequenceNumber, LINK_BASE_ADDRESS, LSA_HEADER_LEN, LSInfinity, LSRefreshTime, MaxAge, MaxAgeDiff, MaxSequenceNumber, MinLSArrival, MinLSInterval, N, NETWORK_BASE_ADDRESS, NETWORK_LSA, NSSA_LSA, Netmask, PACKET_HEADER_LEN, ROUTER_LINK_P2P, ROUTER_LINK_STUB, ROUTER_LINK_TRANSIT, ROUTER_LINK_VL, ROUTER_LSA, RouterId, SUMMARY_BASE_ADDRESS, SUMMARY_LSA, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#ivar_to_klassname, #ivars, #set

Constructor Details

#initialize(arg = {}) ⇒ OspfPacket

Returns a new instance of OspfPacket.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/packet/ospf_packet.rb', line 135

def initialize(arg={})
  
  if arg.is_a?(Hash)
    @ospf_version = OspfVersion.new
    @packet_type = PacketType.new
    @area_id = AreaId.new
    @router_id = RouterId.new
    @au_type = AuType.new
    @authentication=''
    set(arg)
  elsif arg.is_a?(String)
    parse(arg)
  else
    raise ArgumentError, "Invalid argument", caller
  end
  @_len=nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



205
206
207
208
209
210
211
# File 'lib/packet/ospf_packet.rb', line 205

def method_missing(method, *args, &block)
  if method == :to_s_junos
    to_s_default(*args)
  else
    super
  end
end

Instance Attribute Details

#area_idObject (readonly)

Returns the value of attribute area_id.



127
128
129
# File 'lib/packet/ospf_packet.rb', line 127

def area_id
  @area_id
end

#au_typeObject (readonly)

Returns the value of attribute au_type.



127
128
129
# File 'lib/packet/ospf_packet.rb', line 127

def au_type
  @au_type
end

#ospf_versionObject (readonly)

Returns the value of attribute ospf_version.



127
128
129
# File 'lib/packet/ospf_packet.rb', line 127

def ospf_version
  @ospf_version
end

#packet_typeObject (readonly)

Returns the value of attribute packet_type.



127
128
129
# File 'lib/packet/ospf_packet.rb', line 127

def packet_type
  @packet_type
end

#router_idObject (readonly)

Returns the value of attribute router_id.



127
128
129
# File 'lib/packet/ospf_packet.rb', line 127

def router_id
  @router_id
end

#versionObject (readonly)

Returns the value of attribute version.



127
128
129
# File 'lib/packet/ospf_packet.rb', line 127

def version
  @version
end

Class Method Details

.factory(arg) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/packet/ospf_packet.rb', line 271

def factory(arg)

  if arg.is_a?(String)
    s = arg.dup
    version, type, len = s.unpack('CCn')

    raise RuntimeError, "Bogus OSPF Version #{version}" unless version==2
    case type
    when HELLO                     ; Hello.new(s)
    when DATABASE_DESCRIPTION      ; DatabaseDescription.new(s)
    when LINK_STATE_REQUEST        ; LinkStateRequest.new(s)
    when LINK_STATE_UPDATE         ; LinkStateUpdate.new(s)
    when LINK_STATE_ACKNOWLEDGMENT ; LinkStateAck.new(s)
    else
      raise RuntimeError, "unknown OSPFv2 packet type #{type}"
    end
    
  elsif arg.is_a?(Hash)
    case arg[:packet_type]
    when :hello     ; Hello.new(arg)
    when :dd        ; DatabaseDescription.new(arg)
    else
      raise RuntimeError, "Cannot yet build this packet from hash #{arg[:packet_type]}"
    end
  end

end

Instance Method Details

#encode(payload = '', router_id = @router_id) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/packet/ospf_packet.rb', line 153

def encode(payload='',router_id=@router_id)
  @_len = 24 + payload.size
  packet = @ospf_version.encode
  packet << @packet_type.encode
  packet << [24 + payload.size].pack('n')
  packet << router_id.encode
  packet << area_id.encode
  packet << [0].pack('n')
  packet << @au_type.encode
  packet << [''].pack('a8')
  packet << payload
  packet[12..13]= self.csum = [checksum(packet)].pack('n')
  packet
end

#packet_nameObject Also known as: name



130
131
132
# File 'lib/packet/ospf_packet.rb', line 130

def packet_name
  self.class.to_s.split('::').last.to_underscore
end

#to_hash(verbose = false) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/packet/ospf_packet.rb', line 213

def to_hash(verbose=false)
  encode
  h = super()
  h.delete(:authentication) if @authentication==''
  h.delete(:csum) unless verbose
  h
end

#to_s(*args) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/packet/ospf_packet.rb', line 195

def to_s(*args)
  return to_s_default(*args) unless defined?($style)
  case $style
  when :junos ; to_s_junos(*args)
  when :junos_verbose ; to_s_junos_verbose(*args)
  else
    to_s_default(*args)
  end
end

#to_s_briefObject

Version 2, RouterId 0.0.0.1, AreaId 0.0.0.0, AuType 0, Checksum 0x9580, len 9999



170
171
172
173
# File 'lib/packet/ospf_packet.rb', line 170

def to_s_brief
  encode
  "Version #{ospf_version.to_i}, RouterId #{router_id.to_ip}, AreaId #{area_id.to_ip}, Checksum #{0}, len #{@_len}"
end

#to_s_default(verbosity = :brief) ⇒ Object Also known as: to_s_junos



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/packet/ospf_packet.rb', line 175

def to_s_default(verbosity=:brief)
  case verbosity
  when :brief
    to_s_brief
  else
    s=[]
    s << self.class.to_s.split('::').last + ":"
    s << ospf_version.to_s
    s << packet_type.to_s
    s << router_id.to_s
    s << area_id.to_s
    s << au_type.to_s
    s << [@authentication].pack('a8')
    s.join("\n ")
  end
end