ospfv2 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,13 +32,15 @@ module OSPFv2
32
32
  def factory(arg)
33
33
  if arg.is_a?(String)
34
34
  return unless (arg.size>=20)
35
- case arg[3]
35
+ case arg.slice(3,1).unpack('C')[0]
36
36
  when 1 ; OSPFv2::Router.new_ntop(arg)
37
37
  when 2 ; OSPFv2::Network.new_ntop(arg)
38
38
  when 3 ; OSPFv2::Summary.new_ntop(arg)
39
39
  when 4 ; OSPFv2::AsbrSummary.new_ntop(arg)
40
40
  when 5 ; OSPFv2::AsExternal.new_ntop(arg)
41
41
  when 7 ; OSPFv2::AsExternal7.new_ntop(arg)
42
+ else
43
+ raise
42
44
  end
43
45
  elsif arg.is_a?(Hash)
44
46
  case arg[:ls_type]
@@ -48,6 +50,8 @@ module OSPFv2
48
50
  when :asbr_summary_lsa ; OSPFv2::AsbrSummary.new_hash(arg)
49
51
  when :as_external_lsa ; OSPFv2::AsExternal.new_hash(arg)
50
52
  when :as_external7_lsa ; OSPFv2::AsExternal7.new_hash(arg)
53
+ else
54
+ raise
51
55
  end
52
56
  elsif arg.is_a?(Lsa)
53
57
  factory(arg.encode)
data/lib/lsa/network.rb CHANGED
@@ -80,15 +80,20 @@ require 'ie/id'
80
80
  require 'lsa/lsa'
81
81
  module OSPFv2
82
82
 
83
- NetworkMask = Class.new(Id)
84
- AttachRouter = Class.new(Id)
83
+
85
84
 
86
85
  class Network < Lsa
87
86
 
87
+ unless const_defined?(:NetworkMask)
88
+ NetworkMask = Class.new(Id)
89
+ AttachRouter = Class.new(Id)
90
+ end
91
+
88
92
  attr_reader :network_mask, :attached_routers
89
93
 
90
94
  def initialize(arg={})
91
95
  @network_mask, @attached_routers = nil, []
96
+ @ls_type = LsType.new(:network_lsa)
92
97
  super
93
98
  end
94
99
 
data/lib/lsa/opaque.rb ADDED
@@ -0,0 +1,143 @@
1
+ # A.2. The Opaque LSA
2
+ #
3
+ # Opaque LSAs are Type 9, 10, and 11 link state advertisements. These
4
+ # advertisements MAY be used directly by OSPF or indirectly by some
5
+ # application wishing to distribute information throughout the OSPF
6
+ # domain. The function of the Opaque LSA option is to provide for
7
+ # future OSPF extensibility.
8
+ #
9
+ # Opaque LSAs contain some number of octets (of application-specific
10
+ # data) padded to 32-bit alignment. Like any other LSA, the Opaque LSA
11
+ # uses the link-state database distribution mechanism for flooding this
12
+ # information throughout the topology. However, the Opaque LSA has a
13
+ # flooding scope associated with it so that the scope of flooding may
14
+ # be link-local (type-9), area-local (type-10), or the entire OSPF
15
+ # routing domain (type-11). Section 3 of this document describes the
16
+ # flooding procedures for the Opaque LSA.
17
+ #
18
+ #
19
+ #
20
+ #
21
+ #
22
+ #
23
+ #
24
+ #
25
+ #
26
+ # Berger, et al. Standards Track [Page 14]
27
+ #
28
+ # RFC 5250 OSPF Opaque LSA Option July 2008
29
+ #
30
+ #
31
+ # 0 1 2 3
32
+ # 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
33
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34
+ # | LS age | Options | 9, 10, or 11 |
35
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36
+ # | Opaque Type | Opaque ID |
37
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38
+ # | Advertising Router |
39
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40
+ # | LS Sequence Number |
41
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42
+ # | LS checksum | Length |
43
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44
+ # | |
45
+ # + +
46
+ # | Opaque Information |
47
+ # + +
48
+ # | ... |
49
+ #
50
+ # Link-State Type
51
+ #
52
+ # The link-state type of the Opaque LSA identifies the LSA's range
53
+ # of topological distribution. This range is referred to as the
54
+ # flooding scope. The following explains the flooding scope of each
55
+ # of the link-state types.
56
+ #
57
+ # o A value of 9 denotes a link-local scope. Opaque LSAs with a
58
+ # link-local scope MUST NOT be flooded beyond the local
59
+ # (sub)network.
60
+ #
61
+ # o A value of 10 denotes an area-local scope. Opaque LSAs with an
62
+ # area-local scope MUST NOT be flooded beyond their area of
63
+ # origin.
64
+ #
65
+ # o A value of 11 denotes that the LSA is flooded throughout the
66
+ # Autonomous System (e.g., has the same scope as type-5 LSAs).
67
+ # Opaque LSAs with AS-wide scope MUST NOT be flooded into stub
68
+ # areas or NSSAs.
69
+ #
70
+ # Syntax of the Opaque LSA's Link-State ID
71
+ #
72
+ # The link-state ID of the Opaque LSA is divided into an Opaque Type
73
+ # field (the first 8 bits) and an Opaque ID (the remaining 24 bits).
74
+ # See section 7 of this document for a description of Opaque type
75
+ # allocation and assignment.
76
+ #
77
+ #
78
+
79
+ module OSPFv2
80
+ class Opaque_Base < Lsa
81
+ end
82
+ end
83
+
84
+ __END__
85
+
86
+
87
+ 3. The Opaque LSA
88
+
89
+ Opaque LSAs are types 9, 10, and 11 link state advertisements.
90
+ Opaque LSAs consist of a standard LSA header followed by a 32-bit
91
+ aligned application-specific information field. Standard link-state
92
+ database flooding mechanisms are used for distribution of Opaque
93
+ LSAs. The range of topological distribution (i.e., the flooding
94
+ scope) of an Opaque LSA is identified by its link-state type. This
95
+ section documents the flooding of Opaque LSAs.
96
+
97
+ The flooding scope associated with each Opaque link-state type is
98
+ defined as follows.
99
+
100
+ o Link-state type-9 denotes a link-local scope. Type-9 Opaque LSAs
101
+ are not flooded beyond the local (sub)network.
102
+
103
+ o Link-state type-10 denotes an area-local scope. Type-10 Opaque
104
+ LSAs are not flooded beyond the borders of their associated area.
105
+
106
+ o Link-state type-11 denotes that the LSA is flooded throughout the
107
+ Autonomous System (AS). The flooding scope of type-11 LSAs are
108
+ equivalent to the flooding scope of AS-External (type-5) LSAs.
109
+ Specifically, type-11 Opaque LSAs are 1) flooded throughout all
110
+ transit areas, 2) not flooded into stub areas or Not-So-Stubby
111
+ Areas (NSSAs), see [NSSA], from the backbone, and 3) not
112
+ originated by routers into their connected stub areas or NSSAs.
113
+ As with type-5 LSAs, if a type-11 Opaque LSA is received in a stub
114
+ area or NSSA from a neighboring router within the stub area or
115
+ NSSA, the LSA is rejected.
116
+
117
+ The link-state ID of the Opaque LSA is divided into an Opaque type
118
+ field (the first 8 bits) and a type-specific ID (the remaining 24
119
+ bits). The packet format of the Opaque LSA is given in Appendix A.
120
+ Section 7 describes Opaque type allocation and assignment.
121
+
122
+ The responsibility for proper handling of the Opaque LSA's flooding
123
+ scope is placed on both the sender and receiver of the LSA. The
124
+ receiver must always store a valid received Opaque LSA in its link-
125
+ state database. The receiver must not accept Opaque LSAs that
126
+ violate the flooding scope (e.g., a type-11 (domain-wide) Opaque LSA
127
+
128
+
129
+
130
+
131
+
132
+ Berger, et al. Standards Track [Page 4]
133
+
134
+ RFC 5250 OSPF Opaque LSA Option July 2008
135
+
136
+
137
+ is not accepted in a stub area or NSSA). The flooding scope affects
138
+ both the synchronization of the link-state database and the flooding
139
+ procedure.
140
+
141
+ The following describes the modifications to these procedures that
142
+ are necessary to insure conformance to the Opaque LSA's Scoping
143
+ Rules.
data/lib/lsa/router.rb CHANGED
@@ -205,8 +205,10 @@ module OSPFv2
205
205
  super
206
206
  @links=[]
207
207
  @nwveb ||=0
208
+ @ls_type = LsType.new(:router_lsa)
209
+
208
210
  # arg.merge!({:ls_type => 1}) if arg.is_a?(Hash)
209
- [[:abr,1],[:asbr,2],[:vl,4],[:wild,8],[:nssa,16]].each { |x| def_bit *x }
211
+ [[:abr,1],[:asbr,2],[:vl,4],[:wild,8],[:nssa,16]].each { |x| def_bit(*x) } unless respond_to? :set_abr
210
212
  end
211
213
 
212
214
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -239,21 +241,21 @@ module OSPFv2
239
241
  self << links.slice!(0,12+ntos*4)
240
242
  end
241
243
  end
242
-
244
+
243
245
  def to_s_default
244
- super +
245
- ['', nwveb_to_s, *links.collect {|x| x.to_s6 }].join("\n ")
246
- end
246
+ super +
247
+ ['', nwveb_to_s, *links.collect {|x| x.to_s }].join("\n ")
248
+ end
247
249
 
248
- def to_s_junos
249
- super
250
- end
250
+ def to_s_junos
251
+ super
252
+ end
251
253
 
252
- def to_s_junos_verbose
253
- link_hdr = " bits 0x#{nwveb.to_i}, link count #{links.size}"
254
- links_to_s = links.collect {|link| link.to_s_junos }
255
- super + ['', link_hdr, *links_to_s].join("\n")
256
- end
254
+ def to_s_junos_verbose
255
+ link_hdr = " bits 0x#{nwveb.to_i}, link count #{links.size}"
256
+ links_to_s = links.collect {|link| link.to_s_junos }
257
+ super + ['', link_hdr, *links_to_s].join("\n")
258
+ end
257
259
 
258
260
  def has_link?(*args)
259
261
  self[*args] ? true : false
@@ -279,7 +281,7 @@ module OSPFv2
279
281
  links.each { |x| yield(x) }
280
282
  end
281
283
 
282
- #FIXME: make link_id an integer ...
284
+ #FIXME: make link_id an integer ...
283
285
  def args_to_key(*args)
284
286
  if args.size==1 and args[0].is_a?(RouterLink)
285
287
  [args[0].router_link_type.to_i, args[0].link_id.to_hash]
@@ -311,17 +313,7 @@ module OSPFv2
311
313
  end
312
314
 
313
315
  end
314
-
315
- #
316
- # rlsa = Router.new( :advertising_router => '1.1.1.1', :ls_id => '2.2.2.2')
317
- # p rlsa.advertising_router
318
- # p rlsa.ls_id
319
- #
320
- # $style=:default
321
- #
322
- # puts rlsa
323
-
324
-
316
+
325
317
  class Router
326
318
  def self.new_hash(h)
327
319
  r = new(h)
data/lib/lsa/summary.rb CHANGED
@@ -26,31 +26,25 @@ require 'ie/metric'
26
26
  require 'ie/mt_metric'
27
27
  require 'ie/id'
28
28
 
29
-
30
29
  module OSPFv2
31
30
 
32
- Netmask = Class.new(Id)
31
+ unless const_defined?(:Netmask)
32
+ Netmask = Class.new(Id)
33
+ end
33
34
 
34
35
  class Summary_Base < Lsa
36
+ include CommonMetric
35
37
 
36
38
  attr_reader :metric, :netmask, :mt_metrics
37
39
 
38
- def initialize(arg={})
40
+ def initialize(type, arg={})
39
41
  @netmask=nil
40
42
  @metric=nil
41
43
  @mt_metrics=[]
42
- super
44
+ @ls_type = LsType.new(type)
45
+ super arg
43
46
  end
44
47
 
45
- def mt_metrics=(val)
46
- [val].flatten.each { |x| self << x }
47
- end
48
-
49
- def <<(metric)
50
- @mt_metrics ||=[]
51
- @mt_metrics << MtMetric.new(metric)
52
- end
53
-
54
48
  def encode
55
49
  @netmask ||= Netmask.new
56
50
  @metric ||= Metric.new
@@ -121,15 +115,17 @@ module OSPFv2
121
115
 
122
116
 
123
117
  def initialize(arg={})
124
- if arg.is_a?(Hash)
125
- arg = fix_hash(arg).merge!({:ls_type => :summary_lsa,})
118
+ case arg
119
+ when Hash
120
+ arg = fix_hash(arg).merge!({:ls_type => :summary_lsa,})
126
121
  end
127
- super
122
+ super 3, arg
128
123
  end
129
124
 
130
125
  private
131
126
 
132
127
  def fix_hash(arg)
128
+
133
129
  if arg[:network]
134
130
  addr = IPAddr.new arg[:network]
135
131
  arg.delete :network
@@ -137,14 +133,17 @@ module OSPFv2
137
133
  arg.store :ls_id, addr.to_s
138
134
  end
139
135
  arg
136
+ rescue => e
137
+ p 'HERE'
138
+ p arg
139
+ raise
140
140
  end
141
141
 
142
142
  end
143
143
 
144
144
  class AsbrSummary < Summary_Base
145
145
  def initialize(arg={})
146
- arg.merge!({:ls_type => :asbr_summary_lsa,}) if arg.is_a?(Hash)
147
- super
146
+ super 4, arg
148
147
  end
149
148
  end
150
149
 
@@ -162,17 +161,6 @@ load "../../../test/ospfv2/lsa/#{ File.basename($0.gsub(/.rb/,'_test.rb'))}" if
162
161
 
163
162
  __END__
164
163
 
165
- # if arg.is_a?(Hash)
166
- # arg.merge!({:ls_type=> :summary_lsa}) unless arg.has_key?(:ls_type)
167
- # set arg
168
- # super
169
- # elsif arg.is_a?(String)
170
- # parse arg
171
- # elsif arg.is_a?(self.class)
172
- # parse arg.encode
173
- # else
174
- # raise ArgumentError, "Invalid argument", caller
175
- # end
176
164
 
177
165
  =begin rdoc
178
166
 
@@ -249,145 +237,3 @@ A.4.4 Summary-LSAs
249
237
 
250
238
 
251
239
  =end
252
-
253
-
254
- module Ospf
255
- module Summary_Shared
256
- private
257
- def __to_s_junos_style__(sum_type, rtype=' ')
258
- enc
259
- s = @header.to_s_junos_style(sum_type,rtype)
260
- s +="\n mask #{netmask}"
261
- s +="\n Topology default (ID #{@mt_id[0].id}) -> Metric: #{@mt_id[0].metric}"
262
- s += @mt_id[1..-1].collect { |mt| "\n #{mt.to_s_junos_style}" }.join
263
- s
264
- end
265
- def __to_hash__(sum_type)
266
- enc
267
- h = @header.to_hash.merge({:lsa_type=>sum_type, :netmask => netmask,})
268
- if @mt_id.size > 1 or @mt_id[0].id != 0
269
- h[:mt_id] = @mt_id.collect { |mt_id| mt_id.to_hash } unless @mt_id.nil?
270
- else
271
- h[:metric] = @mt_id[0].metric
272
- end
273
- h
274
- end
275
- private :__to_s_junos_style__, :__to_hash__
276
-
277
- end
278
- end
279
-
280
- require 'lsa_header'
281
-
282
- module Ospf
283
-
284
- class SummaryLSAs < LSA
285
- include Ospf
286
- include Ospf::Ip
287
- include Ospf::Summary_Shared
288
-
289
- attr :header
290
- attr_accessor :mt_id
291
-
292
- def initialize(arg={})
293
- @netmask, @mt_id = 0, []
294
- if arg.is_a?(Hash) then
295
- arg[:lstype]=3 if arg[:lstype].nil?
296
- @header = LSA_Header.new(arg)
297
- set(arg)
298
- elsif arg.is_a?(String)
299
- __parse(arg)
300
- else
301
- raise ArgumentError, "Invalid argument", caller
302
- end
303
- end
304
-
305
- def set(arg)
306
- return self unless arg.is_a?(Hash)
307
- @header.set(arg)
308
- unless arg[:netmask].nil?
309
- _netmask = arg[:netmask]
310
- @netmask = _netmask.is_a?(String) ? ip2long(_netmask) : _netmask
311
- end
312
-
313
- unless arg[:metric].nil?
314
- metric=arg[:metric]
315
- @mt_id << MT.new({:metric=>metric})
316
- end
317
- unless arg[:mt_id].nil?
318
- arg[:mt_id].each { |mt_id|
319
- mt_id.is_a?(MT) ? @mt_id << mt_id : @mt_id << MT.new(mt_id)
320
- }
321
- end
322
- self
323
- end
324
-
325
- def <<(arg)
326
- append_metric(arg)
327
- end
328
-
329
- def enc
330
- packet = @header.enc
331
- packet+= __enc([[@netmask,'N'],])
332
- packet +=@mt_id.collect {|mt_id| mt_id.enc}.join
333
- packet_size(packet,@header)
334
- packet_fletchsum(packet)
335
- end
336
-
337
- def __parse(s)
338
- @header = LSA_Header.new(s)
339
- arr = s[20..24].unpack("N")
340
- @netmask = arr[0]
341
- mt_ids = s[24..-1]
342
- while mt_ids.size>0
343
- @mt_id << MT.new(mt_ids.slice!(0,4))
344
- end
345
- end
346
- private :__parse
347
-
348
- def to_s
349
- enc
350
- s = @header.to_s
351
- s += "\n netmask #{netmask}"
352
- s += "\n " if @mt_id.size>0
353
- s += @mt_id.collect { |mt| mt.to_s }.join("\n ")
354
- end
355
-
356
- def metric
357
- @mt_id[0].metric
358
- end
359
-
360
- end
361
-
362
- class SummaryLSA < SummaryLSAs
363
- def initialize(arg={})
364
- arg.merge!({:lstype => 3,}) if arg.is_a?(Hash)
365
- super
366
- end
367
- def to_s_junos_style(rtype=' ')
368
- __to_s_junos_style__('Summary', rtype)
369
- end
370
- def to_hash
371
- __to_hash__('Summary')
372
- end
373
- end
374
-
375
- class ASBR_SummaryLSA < SummaryLSAs
376
- def initialize(arg={})
377
- arg.merge!({:lstype => 4, :metric=>0}) if arg.is_a?(Hash)
378
- super(arg)
379
- end
380
- def to_s_junos_style(rtype=' ')
381
- __to_s_junos_style__('ASBRSum', rtype)
382
- end
383
- def to_hash
384
- __to_hash__('ASBRSum')
385
- end
386
- end
387
- end
388
-
389
- if __FILE__ == $0
390
- load '../test/lsa_summary_test.rb'
391
- end
392
-
393
-