ospfv2 0.0.1

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.
Files changed (56) hide show
  1. data/bin/ospfv2 +136 -0
  2. data/lib/ie/au_type.rb +79 -0
  3. data/lib/ie/external_route.rb +181 -0
  4. data/lib/ie/id.rb +97 -0
  5. data/lib/ie/interface_mtu.rb +64 -0
  6. data/lib/ie/ls_age.rb +89 -0
  7. data/lib/ie/ls_type.rb +148 -0
  8. data/lib/ie/metric.rb +63 -0
  9. data/lib/ie/mt_metric.rb +119 -0
  10. data/lib/ie/options.rb +356 -0
  11. data/lib/ie/ospf_version.rb +67 -0
  12. data/lib/ie/packet_type.rb +65 -0
  13. data/lib/ie/router_link.rb +167 -0
  14. data/lib/ie/router_link_factory.rb +53 -0
  15. data/lib/ie/router_link_type.rb +86 -0
  16. data/lib/ie/sequence_number.rb +144 -0
  17. data/lib/ie/tos_metric.rb +102 -0
  18. data/lib/infra/ospf_common.rb +291 -0
  19. data/lib/infra/ospf_constants.rb +73 -0
  20. data/lib/infra/ospf_io.rb +133 -0
  21. data/lib/infra/ospf_socket.rb +126 -0
  22. data/lib/infra/parse_options.rb +135 -0
  23. data/lib/infra/timer.rb +104 -0
  24. data/lib/infra/to_s.rb +38 -0
  25. data/lib/ls_db/advertised_routers.rb +78 -0
  26. data/lib/ls_db/common.rb +31 -0
  27. data/lib/ls_db/link_state_database.rb +376 -0
  28. data/lib/ls_db/link_state_database_build.rb +181 -0
  29. data/lib/ls_db/link_state_database_links.rb +178 -0
  30. data/lib/ls_db/links.rb +160 -0
  31. data/lib/lsa/external.rb +347 -0
  32. data/lib/lsa/lsa.rb +438 -0
  33. data/lib/lsa/lsa_factory.rb +59 -0
  34. data/lib/lsa/network.rb +166 -0
  35. data/lib/lsa/router.rb +336 -0
  36. data/lib/lsa/summary.rb +393 -0
  37. data/lib/neighbor/neighbor.rb +298 -0
  38. data/lib/neighbor/neighbor_event_handler.rb +61 -0
  39. data/lib/neighbor/recv_database_description.rb +153 -0
  40. data/lib/neighbor/recv_hello.rb +53 -0
  41. data/lib/neighbor/recv_ospf_packet.rb +68 -0
  42. data/lib/neighbor_sm/attempt_state.rb +44 -0
  43. data/lib/neighbor_sm/down_state.rb +46 -0
  44. data/lib/neighbor_sm/exchange_state.rb +32 -0
  45. data/lib/neighbor_sm/exstart_state.rb +69 -0
  46. data/lib/neighbor_sm/full_state.rb +36 -0
  47. data/lib/neighbor_sm/init_state.rb +43 -0
  48. data/lib/neighbor_sm/loading_state.rb +33 -0
  49. data/lib/neighbor_sm/neighbor_state.rb +87 -0
  50. data/lib/packet/database_description.rb +300 -0
  51. data/lib/packet/hello.rb +327 -0
  52. data/lib/packet/link_state_ack.rb +144 -0
  53. data/lib/packet/link_state_request.rb +153 -0
  54. data/lib/packet/link_state_update.rb +189 -0
  55. data/lib/packet/ospf_packet.rb +306 -0
  56. metadata +116 -0
data/lib/lsa/router.rb ADDED
@@ -0,0 +1,336 @@
1
+ #--
2
+ # Copyright 2010 Jean-Michel Esnault.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #
6
+ #
7
+ # This file is part of OSPFv2.
8
+ #
9
+ # OSPFv2 is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # OSPFv2 is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with OSPFv2. If not, see <http://www.gnu.org/licenses/>.
21
+ #++
22
+
23
+
24
+ =begin rdoc
25
+ A.4.2 Router-LSAs
26
+
27
+ Router-LSAs are the Type 1 LSAs. Each router in an area originates
28
+ a router-LSA. The LSA describes the state and cost of the router's
29
+ links (i.e., interfaces) to the area. All of the router's links to
30
+ the area must be described in a single router-LSA. For details
31
+ concerning the construction of router-LSAs, see Section 12.4.1.
32
+
33
+
34
+ 0 1 2 3
35
+ 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
36
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37
+ | LS age | Options | 1 |
38
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39
+ | Link State ID |
40
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41
+ | Advertising Router |
42
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43
+ | LS sequence number |
44
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45
+ | LS checksum | length |
46
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47
+ | 0 |V|E|B| 0 | # links |
48
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49
+ | Link ID |
50
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51
+ | Link Data |
52
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53
+ | Type | # TOS | metric |
54
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55
+ | ... |
56
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57
+ | TOS | 0 | TOS metric |
58
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59
+ | Link ID |
60
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61
+ | Link Data |
62
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63
+ | ... |
64
+
65
+
66
+
67
+ In router-LSAs, the Link State ID field is set to the router's OSPF
68
+ Router ID. Router-LSAs are flooded throughout a single area only.
69
+
70
+ bit V
71
+ When set, the router is an endpoint of one or more fully
72
+ adjacent virtual links having the described area as Transit area
73
+ (V is for virtual link endpoint).
74
+
75
+ bit E
76
+ When set, the router is an AS boundary router (E is for
77
+ external).
78
+
79
+ bit B
80
+ When set, the router is an area border router (B is for border).
81
+
82
+ # links
83
+ The number of router links described in this LSA. This must be
84
+ the total collection of router links (i.e., interfaces) to the
85
+ area.
86
+
87
+
88
+ The following fields are used to describe each router link (i.e.,
89
+ interface). Each router link is typed (see the below Type field).
90
+ The Type field indicates the kind of link being described. It may
91
+ be a link to a transit network, to another router or to a stub
92
+ network. The values of all the other fields describing a router
93
+ link depend on the link's Type. For example, each link has an
94
+ associated 32-bit Link Data field. For links to stub networks this
95
+ field specifies the network's IP address mask. For other link types
96
+ the Link Data field specifies the router interface's IP address.
97
+
98
+
99
+ Type
100
+ A quick description of the router link. One of the following.
101
+ Note that host routes are classified as links to stub networks
102
+ with network mask of 0xffffffff.
103
+
104
+
105
+
106
+
107
+ Type Description
108
+ __________________________________________________
109
+ 1 Point-to-point connection to another router
110
+ 2 Connection to a transit network
111
+ 3 Connection to a stub network
112
+ 4 Virtual link
113
+
114
+
115
+
116
+
117
+ Link ID
118
+ Identifies the object that this router link connects to. Value
119
+ depends on the link's Type. When connecting to an object that
120
+ also originates an LSA (i.e., another router or a transit
121
+ network) the Link ID is equal to the neighboring LSA's Link
122
+ State ID. This provides the key for looking up the neighboring
123
+ LSA in the link state database during the routing table
124
+ calculation. See Section 12.2 for more details.
125
+
126
+
127
+
128
+ Type Link ID
129
+ ______________________________________
130
+ 1 Neighboring router's Router ID
131
+ 2 IP address of Designated Router
132
+ 3 IP network/subnet number
133
+ 4 Neighboring router's Router ID
134
+
135
+
136
+
137
+
138
+ Link Data
139
+ Value again depends on the link's Type field. For connections to
140
+ stub networks, Link Data specifies the network's IP address
141
+ mask. For unnumbered point-to-point connections, it specifies
142
+ the interface's MIB-II [Ref8] ifIndex value. For the other link
143
+ types it specifies the router interface's IP address. This
144
+ latter piece of information is needed during the routing table
145
+ build process, when calculating the IP address of the next hop.
146
+ See Section 16.1.1 for more details.
147
+
148
+
149
+
150
+ # TOS
151
+ The number of different TOS metrics given for this link, not
152
+ counting the required link metric (referred to as the TOS 0
153
+ metric in [Ref9]). For example, if no additional TOS metrics
154
+ are given, this field is set to 0.
155
+
156
+ metric
157
+ The cost of using this router link.
158
+
159
+
160
+ Additional TOS-specific information may also be included, for
161
+ backward compatibility with previous versions of the OSPF
162
+ specification ([Ref9]). Within each link, and for each desired TOS,
163
+ TOS TOS-specific link information may be encoded as follows:
164
+
165
+ TOS IP Type of Service that this metric refers to. The encoding of
166
+ TOS in OSPF LSAs is described in Section 12.3.
167
+
168
+ TOS metric
169
+ TOS-specific metric information.
170
+
171
+
172
+
173
+ NSSA:
174
+
175
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
176
+ | 0 Nt|W|V|E|B| 0 | # links |
177
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
178
+
179
+
180
+ bit W
181
+ When set, the router is a wild-card multicast receiver (W is
182
+ for wild).
183
+
184
+ bit Nt
185
+ When set, the router is an NSSA border router that is
186
+ unconditionally translating Type-7 LSAs into Type-5 LSAs (Nt
187
+ stands for NSSA translation). Note that such routers have
188
+ their NSSATranslatorRole area configuration parameter set to
189
+ Always. (See Appendix D and Section 3.1.)
190
+
191
+
192
+ =end
193
+
194
+ require 'lsa/lsa'
195
+ require 'ie/router_link'
196
+ require 'ie/router_link_factory'
197
+
198
+ module OSPFv2
199
+
200
+ class Router < Lsa
201
+
202
+ attr_reader :links, :nwveb
203
+
204
+ def initialize(arg={})
205
+ super
206
+ @links=[]
207
+ @nwveb ||=0
208
+ # 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 }
210
+ end
211
+
212
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213
+ # | 0 Nt|W|V|E|B| 0 | # links |
214
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215
+ def encode
216
+ lsa = []
217
+ lsa << [@nwveb,0,@links.size].pack('CCn')
218
+ lsa << @links.collect { |x| x.encode } unless @links.empty?
219
+ super(lsa.join)
220
+ end
221
+
222
+ def links=(val)
223
+ [val].flatten.each { |x| self << x }
224
+ end
225
+
226
+ def <<(link)
227
+ @links << RouterLink.factory(link)
228
+ self
229
+ end
230
+
231
+ def number_of_link
232
+ @links.size
233
+ end
234
+
235
+ def parse(s)
236
+ @nwveb, _, nlinks, links = super(s).unpack('CCna*')
237
+ while links.size>0
238
+ ntos= links[9..10].unpack('C')[0]
239
+ self << links.slice!(0,12+ntos*4)
240
+ end
241
+ end
242
+
243
+ def to_s_default
244
+ super +
245
+ ['', nwveb_to_s, *links.collect {|x| x.to_s6 }].join("\n ")
246
+ end
247
+
248
+ def to_s_junos
249
+ super
250
+ end
251
+
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
257
+
258
+ def has_link?(*args)
259
+ self[*args] ? true : false
260
+ end
261
+
262
+ def [](*args)
263
+ if args.size==1
264
+ @links[*args]
265
+ else
266
+ ltype, link_id = args_to_key(*args)
267
+ link = links.find_all { |x| (x.router_link_type.to_i == ltype) and (x.link_id.to_hash == link_id) }
268
+ link.empty? ? nil : link[0]
269
+ end
270
+ end
271
+ alias :lookup :[]
272
+
273
+ def delete(*args)
274
+ ltype, link_id = args_to_key(*args)
275
+ links.delete_if { |x| x.router_link_type.to_i == ltype and x.link_id.to_hash == link_id }
276
+ end
277
+
278
+ def each
279
+ links.each { |x| yield(x) }
280
+ end
281
+
282
+ #FIXME: make link_id an integer ...
283
+ def args_to_key(*args)
284
+ if args.size==1 and args[0].is_a?(RouterLink)
285
+ [args[0].router_link_type.to_i, args[0].link_id.to_hash]
286
+ elsif args.size==2 and args[0].is_a?(Symbol) and args[1].is_a?(String)
287
+ [RouterLinkType.to_i(args[0]), args[1]]
288
+ elsif args.size==2 and args[0].is_a?(Fixnum) and args[1].is_a?(String)
289
+ args
290
+ end
291
+ end
292
+
293
+ private
294
+
295
+ def nwveb_to_s
296
+ "|Nt|W|V|E|B| " + [@nwveb].pack('C').unpack('B8')[0]
297
+ end
298
+
299
+ def def_bit(name, pos)
300
+ self.class.class_eval {
301
+ define_method("set_#{name}") do
302
+ @nwveb = @nwveb | pos
303
+ end
304
+ define_method("unset_#{name}") do
305
+ @nwveb = @nwveb & ~pos
306
+ end
307
+ define_method("#{name}?") do
308
+ @nwveb & pos>0
309
+ end
310
+ }
311
+ end
312
+
313
+ 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
+
325
+ class Router
326
+ def self.new_hash(h)
327
+ r = new(h)
328
+ r.instance_eval { @nwveb = h[:nwveb] || 0}
329
+ h[:links].each { |l| r << l } if h[:links]
330
+ r
331
+ end
332
+ end
333
+
334
+ end
335
+
336
+ load "../../../test/ospfv2/lsa/#{ File.basename($0.gsub(/.rb/,'_test.rb'))}" if __FILE__ == $0