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
@@ -0,0 +1,135 @@
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
+ require 'optparse'
24
+ require 'ostruct'
25
+
26
+ require 'ie/id'
27
+
28
+ class OptParse
29
+ def self.parse(args)
30
+
31
+ options = OpenStruct.new
32
+ options.ipaddr = '192.168.1.123'
33
+ options.network = '192.168.1.0'
34
+ options.netmask = '255.255.255.0'
35
+ options.base_link_addr = '1.3.0.0/30'
36
+ options.base_router_id = 0
37
+ options.hello_int = 10
38
+ options.router_id = 1
39
+ options.neighbor_id = 2
40
+ options.area_id = 0
41
+ options.log_fname = $stdout
42
+ options.grid = [2,2]
43
+ options.num_sum = 10
44
+ options.num_ext = 10
45
+
46
+ options.parse = Proc.new do |filename|
47
+ y_conf = YAML::load_file(filename)
48
+ y_conf.keys.each do |k|
49
+ case k.downcase
50
+ when "key1"
51
+ when "key2"
52
+ end
53
+ end
54
+ options
55
+ end
56
+
57
+ to_ip = lambda { |id| [id].pack('N').unpack('C*').join('.') }
58
+ to_id = lambda { |x| OSPFv2::Id.to_i(x) }
59
+ dead_int = lambda { @dead_int || (options.hello_int * 4)}
60
+
61
+ option_help = "blabla ...."
62
+ hlp_address = "IP Address of the OSPF Interface."
63
+ hlp_base_link_addr = "base p2p links addres [#{options.base_link_addr}]"
64
+ hlp_base_router_id = "base router-id [#{to_ip.call(options.base_router_id)}]"
65
+ hlp_neighbor_id= "Neighbor Id. [#{to_ip.call(options.neighbor_id)}]"
66
+ hlp_router_id = "Router Id. [#{to_ip.call(options.router_id)}]"
67
+ hlp_area_id = "Area Id. [#{to_ip.call(options.area_id)}]"
68
+ hlp_grid = "Area Grid. [#{options.grid.join('x')}]"
69
+ hlp_hello_int = "Hello Int. [#{options.hello_int}]"
70
+ hlp_dead_int = "Dead Int. [#{dead_int.call}]"
71
+ hlp_sum = "\#Summary. [#{options.num_sum}]"
72
+ hlp_ext = "\#External. [#{options.num_ext}]"
73
+
74
+ optparse = OptionParser.new do |opts|
75
+
76
+ opts.banner = "Usage: #{$0} [options]"
77
+
78
+ opts.on( "--base-p2p-addr [PREFIX]", hlp_base_link_addr) { |x|
79
+ options.base_link_addr = x
80
+ }
81
+ opts.on("-i", "--address [PREFIX]", hlp_address) { |x|
82
+ options.ipaddr = x
83
+ options.ipaddr = x.split('/')[0]
84
+ _addr = IPAddr.new x
85
+ options.network = _addr.to_s
86
+ options.netmask = _addr.netmask
87
+ }
88
+ opts.on("--base-router-id [ID]", hlp_base_router_id) { |id|
89
+ options.base_router_id = id.to_i
90
+ }
91
+ opts.on("-r", "--router-id [ID]", hlp_router_id) { |id|
92
+ options.router_id = OSPFv2::Id.to_i(id)
93
+ }
94
+ opts.on("-n", "--neighbor-id [ID]", hlp_neighbor_id) { |id|
95
+ options.neighbor_id = OSPFv2::Id.to_i(id)
96
+ }
97
+ opts.on("-a", "--area-id [ID]", hlp_area_id) { |id|
98
+ options.area_id = to_id.call(id)
99
+ }
100
+ opts.on("-g", "--grid [colxrow]", hlp_grid) { |grid|
101
+ options.grid = grid.split('x').collect { |x| x.to_i }
102
+ }
103
+ opts.on( "--hello-interval [INT]", hlp_hello_int) { |int|
104
+ options.hello_int = int.to_i
105
+ }
106
+ opts.on("--dead-interval [INT]", hlp_dead_int) { |int|
107
+ @dead_int = int.to_i
108
+ }
109
+ opts.on("-g", "--grid [colxrow]", hlp_grid) { |grid|
110
+ options.grid = grid.split('x').collect { |x| x.to_i }
111
+ }
112
+ opts.on('-S', "--number-of-summary [INT]", "Number of Summary LSA.") { |x|
113
+ options.num_sum = x.to_i
114
+ }
115
+ opts.on('-E',"--number-of-external [INT]", "Number AsExternal") { |x|
116
+ options.num_ext = x.to_i
117
+ }
118
+ opts.on( '-f', "--log-fname [FILENAME]", "To redirect logs to a file.") { |fname|
119
+ options.log_fname = fname
120
+ }
121
+ opts.on_tail("-h", "--help", "Show this message") { puts "\n #{opts}\n" ; exit }
122
+ opts.on_tail("-?", "--help") { puts "\n #{opts}\n" ; exit }
123
+ opts.on
124
+ end
125
+
126
+ optparse.parse!(args)
127
+ options.dead_int = @dead_int || (options.hello_int * 4)
128
+
129
+ options
130
+
131
+ end
132
+
133
+ end
134
+
135
+ load "../../../test/ospfv2/infra/#{ File.basename($0.gsub(/.rb/,'_test.rb'))}" if __FILE__ == $0
@@ -0,0 +1,104 @@
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
+ require 'observer'
24
+ require 'thread'
25
+
26
+ class Timer
27
+ include Observable
28
+
29
+ class << self
30
+ def start interval, observer=nil, &block
31
+ timer = if block
32
+ new interval, observer, &block
33
+ else
34
+ new interval, observer
35
+ end
36
+ timer.start
37
+ end
38
+ end
39
+
40
+ def initialize interval, observer=nil, &block
41
+ @interval = interval
42
+ @code = block if block
43
+ add_observer observer if observer
44
+ end
45
+
46
+ def start _interval=@interval, &block
47
+ stop if running?
48
+ changed and notify_observers(:ev_timer_start, id, Time.now.strftime("%M:%S"))
49
+ _code = block || @code
50
+ @_timer_thread_ = Thread.new(_interval, _code) do |interval, code|
51
+ sleep(interval)
52
+ code.call if code
53
+ changed and notify_observers(:ev_timer_fire, id, Time.now.strftime("%M:%S"))
54
+ end
55
+ self
56
+ end
57
+
58
+ def cancel
59
+ return unless @_timer_thread_
60
+ [:exit, :join].each { |x| @_timer_thread_.send x }
61
+ changed and notify_observers(:ev_timer_cancel, id, Time.now.strftime("%M:%S"))
62
+ yield if block_given?
63
+ end
64
+ alias :stop :cancel
65
+
66
+ def running?
67
+ @_timer_thread_ and @_timer_thread_.alive?
68
+ end
69
+
70
+ def reset &block
71
+ cancel
72
+ start &block
73
+ end
74
+
75
+ private
76
+
77
+ def id
78
+ self.class.to_s.to_sym
79
+ end
80
+
81
+ end
82
+
83
+ class PeriodicTimer < Timer
84
+ def start _interval=@interval, &block
85
+ @continue = true
86
+ changed and notify_observers(:ev_timer_start, id, Time.now.strftime("%M:%S"))
87
+ _code = block || @code
88
+ @_timer_thread_ = Thread.new(_interval, _code) do |interval, code|
89
+ loop do
90
+ sleep(interval)
91
+ code.call if code
92
+ changed and notify_observers(:ev_timer_fire, id, Time.now.strftime("%M:%S"))
93
+ break unless @continue
94
+ end
95
+ changed and notify_observers(:ev_timer_stop, id, Time.now.strftime("%M:%S"))
96
+ end
97
+ self
98
+ end
99
+ def last_shot
100
+ @continue = false
101
+ end
102
+ end
103
+
104
+ load "../../../test/ospfv2/infra/#{ File.basename($0.gsub(/.rb/,'_test.rb'))}" if __FILE__ == $0
data/lib/infra/to_s.rb ADDED
@@ -0,0 +1,38 @@
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
+ module OSPFv2
24
+ module TO_S
25
+
26
+ def to_s(*args)
27
+ return to_s_default(*args) unless defined?($style)
28
+ case $style
29
+ when :junos ; to_s_junos(*args)
30
+ when :junos_verbose ; to_s_junos_verbose(*args)
31
+ else
32
+ to_s_default(*args)
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,78 @@
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
+ require 'set'
24
+ require 'ie/id'
25
+
26
+ module OSPFv2::LSDB
27
+ class AdvertisedRouters
28
+ AdvertisedRouter = Class.new(OSPFv2::Id)
29
+ attr_reader :routers
30
+ def initialize
31
+ @set = Set.new
32
+ end
33
+ def +(id)
34
+ @set << router_id(id)
35
+ end
36
+ def routers
37
+ @set.collect.sort
38
+ end
39
+ alias :ids :routers
40
+ def has?(id)
41
+ routers.include?(router_id(id))
42
+ end
43
+ def -(id)
44
+ @set.delete router_id(id)
45
+ end
46
+ private
47
+ def router_id(id)
48
+ AdvertisedRouter.new(id).to_i
49
+ end
50
+ end
51
+ end
52
+
53
+ if __FILE__ == $0
54
+
55
+ require "test/unit"
56
+
57
+ # require "ls_db/advertised_routers"
58
+
59
+ class TestLsDbAdvertisedRouters < Test::Unit::TestCase
60
+ include OSPFv2::LSDB
61
+ def tests
62
+ assert AdvertisedRouters.new
63
+ routers = AdvertisedRouters.new
64
+ routers + 1
65
+ routers + '0.0.0.1'
66
+ routers + 2
67
+ routers + OSPFv2::Id.new(3)
68
+ assert_equal [1,2,3], routers.routers
69
+ routers -1
70
+ assert_equal [2,3], routers.routers
71
+ routers -3
72
+ assert_equal [2], routers.routers
73
+ routers - '0.0.0.2'
74
+ assert_equal [], routers.routers
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,31 @@
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
+ module OSPFv2
24
+ module LSDB
25
+ ROUTER_ID_BASE=0x01000000
26
+ EXTERNAL_BASE_ADDRESS='50.0.0.0/24'
27
+ SUMMARY_BASE_ADDRESS='30.0.0.0/24'
28
+ NETWORK_BASE_ADDRESS='20.0.0.0/24'
29
+ LINK_BASE_ADDRESS='13.0.0.0/30'
30
+ end
31
+ end