ospfv2 0.0.2 → 0.0.3

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 (107) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +674 -0
  3. data/LICENSE.txt +53 -0
  4. data/README +50 -0
  5. data/bin/ospfv2 +112 -105
  6. data/changelog.txt +7 -0
  7. data/gemspec +17 -0
  8. data/lib/ie/external_route.rb +9 -0
  9. data/lib/ie/id.rb +3 -4
  10. data/lib/ie/ie.rb +12 -0
  11. data/lib/ie/ls_age.rb +35 -5
  12. data/lib/ie/ls_type.rb +40 -37
  13. data/lib/ie/opaque_id.rb +46 -0
  14. data/lib/ie/opaque_type.rb +96 -0
  15. data/lib/ie/options.rb +5 -3
  16. data/lib/ie/router_link.rb +30 -7
  17. data/lib/ie/router_link_type.rb +116 -27
  18. data/lib/ie/sequence_number.rb +7 -3
  19. data/lib/infra/ospf_common.rb +1 -12
  20. data/lib/infra/ospf_io.rb +14 -8
  21. data/lib/infra/ospf_socket.rb +14 -14
  22. data/lib/infra/parse_options.rb +69 -25
  23. data/lib/ls_db/advertised_routers.rb +1 -1
  24. data/lib/ls_db/link_state_database.rb +74 -39
  25. data/lib/ls_db/link_state_database_build.rb +3 -3
  26. data/lib/ls_db/link_state_database_links.rb +7 -2
  27. data/lib/ls_db/lsdb_ios.rb +59 -0
  28. data/lib/lsa/external.rb +35 -2
  29. data/lib/lsa/lsa.rb +128 -35
  30. data/lib/lsa/lsa_factory.rb +20 -7
  31. data/lib/lsa/network.rb +44 -9
  32. data/lib/lsa/router.rb +14 -2
  33. data/lib/lsa/summary.rb +16 -3
  34. data/lib/lsa/tlv/color.rb +47 -0
  35. data/lib/lsa/tlv/link.rb +122 -0
  36. data/lib/lsa/tlv/link_id.rb +55 -0
  37. data/lib/lsa/tlv/link_type.rb +44 -0
  38. data/lib/lsa/tlv/local_interface.rb +56 -0
  39. data/lib/lsa/tlv/maximum_bandwidth.rb +55 -0
  40. data/lib/lsa/tlv/maximum_reservable_bandwidth.rb +52 -0
  41. data/lib/lsa/tlv/remote_interface.rb +58 -0
  42. data/lib/lsa/tlv/router_address.rb +57 -0
  43. data/lib/lsa/tlv/tlv.rb +32 -0
  44. data/lib/lsa/tlv/tlv_factory.rb +72 -0
  45. data/lib/lsa/tlv/traffic_engineering_metric.rb +52 -0
  46. data/lib/lsa/tlv/unreserved_bandwidth.rb +55 -0
  47. data/lib/lsa/traffic_engineering.rb +95 -0
  48. data/lib/neighbor/neighbor.rb +9 -9
  49. data/lib/packet/link_state_update.rb +16 -14
  50. data/lib/packet/ospf_packet.rb +13 -9
  51. data/test/unit/ie/au_type_test.rb +18 -0
  52. data/test/unit/ie/external_route_test.rb +51 -0
  53. data/test/unit/ie/id_test.rb +22 -0
  54. data/test/unit/ie/interface_mtu_test.rb +23 -0
  55. data/test/unit/ie/ls_age_test.rb +46 -0
  56. data/test/unit/ie/ls_type_test.rb +41 -0
  57. data/test/unit/ie/metric_test.rb +24 -0
  58. data/test/unit/ie/mt_metric_test.rb +27 -0
  59. data/test/unit/ie/opaque_id_test.rb +22 -0
  60. data/test/unit/ie/opaque_type_test.rb +22 -0
  61. data/test/unit/ie/options_test.rb +142 -0
  62. data/test/unit/ie/ospf_version_test.rb +0 -0
  63. data/test/unit/ie/packet_type_test.rb +33 -0
  64. data/test/unit/ie/router_link_factory_test.rb +26 -0
  65. data/test/unit/ie/router_link_test.rb +276 -0
  66. data/test/unit/ie/router_link_type_test.rb +24 -0
  67. data/test/unit/ie/sequence_number_test.rb +31 -0
  68. data/test/unit/ie/tos_metric_test.rb +27 -0
  69. data/test/unit/infra/ospf_common_test.rb +42 -0
  70. data/test/unit/infra/parse_options_test.rb +58 -0
  71. data/test/unit/infra/timer_test.rb +94 -0
  72. data/test/unit/ls_db/all +10 -0
  73. data/test/unit/ls_db/link_state_database_build_test.rb +140 -0
  74. data/test/unit/ls_db/link_state_database_links_tests.rb +74 -0
  75. data/test/unit/ls_db/link_state_database_test.rb +444 -0
  76. data/test/unit/ls_db/links_test.rb +49 -0
  77. data/test/unit/lsa/all +10 -0
  78. data/test/unit/lsa/external_test.rb +218 -0
  79. data/test/unit/lsa/lsa_factory_test.rb +314 -0
  80. data/test/unit/lsa/lsa_test.rb +191 -0
  81. data/test/unit/lsa/network_test.rb +107 -0
  82. data/test/unit/lsa/router_test.rb +614 -0
  83. data/test/unit/lsa/summary_test.rb +135 -0
  84. data/test/unit/lsa/tlv/color_test.rb +39 -0
  85. data/test/unit/lsa/tlv/link_id_test.rb +14 -0
  86. data/test/unit/lsa/tlv/link_test.rb +413 -0
  87. data/test/unit/lsa/tlv/link_type_test.rb +16 -0
  88. data/test/unit/lsa/tlv/local_interface_test.rb +15 -0
  89. data/test/unit/lsa/tlv/maximum_bandwidth_test.rb +14 -0
  90. data/test/unit/lsa/tlv/maximum_reservable_bandwidth_test.rb +13 -0
  91. data/test/unit/lsa/tlv/remote_interface_test.rb +14 -0
  92. data/test/unit/lsa/tlv/router_address_test.rb +16 -0
  93. data/test/unit/lsa/tlv/tlv_factory_test.rb +89 -0
  94. data/test/unit/lsa/tlv/traffic_engineering_metric_test.rb +15 -0
  95. data/test/unit/lsa/traffic_engineering_test.rb +65 -0
  96. data/test/unit/neighbor/neighbor_test.rb +82 -0
  97. data/test/unit/packet/all +10 -0
  98. data/test/unit/packet/database_description_test.rb +114 -0
  99. data/test/unit/packet/hello_test.rb +107 -0
  100. data/test/unit/packet/link_state_ack_test.rb +64 -0
  101. data/test/unit/packet/link_state_request_test.rb +168 -0
  102. data/test/unit/packet/link_state_update_test.rb +62 -0
  103. data/test/unit/packet/ospf_packet_test.rb +91 -0
  104. metadata +106 -51
  105. data/lib/infra/to_s.rb +0 -38
  106. data/lib/lsa/lsa_base.rb +0 -430
  107. data/lib/lsa/opaque.rb +0 -143
metadata CHANGED
@@ -1,40 +1,41 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ospfv2
3
- version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Jean Michel Esnault
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-04-16 00:00:00 Z
11
+ date: 2014-10-17 00:00:00.000000000 Z
19
12
  dependencies: []
20
-
21
- description:
13
+ description: Playing with OSPF version 2 using ruby.
22
14
  email: ospfv2@esnault.org
23
- executables:
15
+ executables:
24
16
  - ospfv2
25
17
  extensions: []
26
-
27
- extra_rdoc_files: []
28
-
29
- files:
18
+ extra_rdoc_files:
19
+ - LICENSE.txt
20
+ - README
21
+ files:
22
+ - COPYING
23
+ - LICENSE.txt
24
+ - README
25
+ - bin/ospfv2
26
+ - changelog.txt
27
+ - gemspec
30
28
  - lib/ie/au_type.rb
31
29
  - lib/ie/external_route.rb
32
30
  - lib/ie/id.rb
31
+ - lib/ie/ie.rb
33
32
  - lib/ie/interface_mtu.rb
34
33
  - lib/ie/ls_age.rb
35
34
  - lib/ie/ls_type.rb
36
35
  - lib/ie/metric.rb
37
36
  - lib/ie/mt_metric.rb
37
+ - lib/ie/opaque_id.rb
38
+ - lib/ie/opaque_type.rb
38
39
  - lib/ie/options.rb
39
40
  - lib/ie/ospf_version.rb
40
41
  - lib/ie/packet_type.rb
@@ -49,22 +50,33 @@ files:
49
50
  - lib/infra/ospf_socket.rb
50
51
  - lib/infra/parse_options.rb
51
52
  - lib/infra/timer.rb
52
- - lib/infra/to_s.rb
53
53
  - lib/ls_db/advertised_routers.rb
54
54
  - lib/ls_db/common.rb
55
55
  - lib/ls_db/link_state_database.rb
56
56
  - lib/ls_db/link_state_database_build.rb
57
57
  - lib/ls_db/link_state_database_links.rb
58
58
  - lib/ls_db/links.rb
59
+ - lib/ls_db/lsdb_ios.rb
59
60
  - lib/lsa/external.rb
60
61
  - lib/lsa/lsa.rb
61
- - lib/lsa/lsa_base.rb
62
62
  - lib/lsa/lsa_factory.rb
63
63
  - lib/lsa/network.rb
64
- - lib/lsa/opaque.rb
65
64
  - lib/lsa/router.rb
66
65
  - lib/lsa/summary.rb
66
+ - lib/lsa/tlv/color.rb
67
+ - lib/lsa/tlv/link.rb
68
+ - lib/lsa/tlv/link_id.rb
69
+ - lib/lsa/tlv/link_type.rb
70
+ - lib/lsa/tlv/local_interface.rb
71
+ - lib/lsa/tlv/maximum_bandwidth.rb
72
+ - lib/lsa/tlv/maximum_reservable_bandwidth.rb
73
+ - lib/lsa/tlv/remote_interface.rb
74
+ - lib/lsa/tlv/router_address.rb
67
75
  - lib/lsa/tlv/tlv.rb
76
+ - lib/lsa/tlv/tlv_factory.rb
77
+ - lib/lsa/tlv/traffic_engineering_metric.rb
78
+ - lib/lsa/tlv/unreserved_bandwidth.rb
79
+ - lib/lsa/traffic_engineering.rb
68
80
  - lib/neighbor/neighbor.rb
69
81
  - lib/neighbor/neighbor_event_handler.rb
70
82
  - lib/neighbor/recv_database_description.rb
@@ -84,41 +96,84 @@ files:
84
96
  - lib/packet/link_state_request.rb
85
97
  - lib/packet/link_state_update.rb
86
98
  - lib/packet/ospf_packet.rb
87
- - changelog.txt
88
- - bin/ospfv2
89
- homepage:
99
+ - test/unit/ie/au_type_test.rb
100
+ - test/unit/ie/external_route_test.rb
101
+ - test/unit/ie/id_test.rb
102
+ - test/unit/ie/interface_mtu_test.rb
103
+ - test/unit/ie/ls_age_test.rb
104
+ - test/unit/ie/ls_type_test.rb
105
+ - test/unit/ie/metric_test.rb
106
+ - test/unit/ie/mt_metric_test.rb
107
+ - test/unit/ie/opaque_id_test.rb
108
+ - test/unit/ie/opaque_type_test.rb
109
+ - test/unit/ie/options_test.rb
110
+ - test/unit/ie/ospf_version_test.rb
111
+ - test/unit/ie/packet_type_test.rb
112
+ - test/unit/ie/router_link_factory_test.rb
113
+ - test/unit/ie/router_link_test.rb
114
+ - test/unit/ie/router_link_type_test.rb
115
+ - test/unit/ie/sequence_number_test.rb
116
+ - test/unit/ie/tos_metric_test.rb
117
+ - test/unit/infra/ospf_common_test.rb
118
+ - test/unit/infra/parse_options_test.rb
119
+ - test/unit/infra/timer_test.rb
120
+ - test/unit/ls_db/all
121
+ - test/unit/ls_db/link_state_database_build_test.rb
122
+ - test/unit/ls_db/link_state_database_links_tests.rb
123
+ - test/unit/ls_db/link_state_database_test.rb
124
+ - test/unit/ls_db/links_test.rb
125
+ - test/unit/lsa/all
126
+ - test/unit/lsa/external_test.rb
127
+ - test/unit/lsa/lsa_factory_test.rb
128
+ - test/unit/lsa/lsa_test.rb
129
+ - test/unit/lsa/network_test.rb
130
+ - test/unit/lsa/router_test.rb
131
+ - test/unit/lsa/summary_test.rb
132
+ - test/unit/lsa/tlv/color_test.rb
133
+ - test/unit/lsa/tlv/link_id_test.rb
134
+ - test/unit/lsa/tlv/link_test.rb
135
+ - test/unit/lsa/tlv/link_type_test.rb
136
+ - test/unit/lsa/tlv/local_interface_test.rb
137
+ - test/unit/lsa/tlv/maximum_bandwidth_test.rb
138
+ - test/unit/lsa/tlv/maximum_reservable_bandwidth_test.rb
139
+ - test/unit/lsa/tlv/remote_interface_test.rb
140
+ - test/unit/lsa/tlv/router_address_test.rb
141
+ - test/unit/lsa/tlv/tlv_factory_test.rb
142
+ - test/unit/lsa/tlv/traffic_engineering_metric_test.rb
143
+ - test/unit/lsa/traffic_engineering_test.rb
144
+ - test/unit/neighbor/neighbor_test.rb
145
+ - test/unit/packet/all
146
+ - test/unit/packet/database_description_test.rb
147
+ - test/unit/packet/hello_test.rb
148
+ - test/unit/packet/link_state_ack_test.rb
149
+ - test/unit/packet/link_state_request_test.rb
150
+ - test/unit/packet/link_state_update_test.rb
151
+ - test/unit/packet/ospf_packet_test.rb
152
+ homepage: https://github.com/jesnault/ospfv2
90
153
  licenses: []
91
-
154
+ metadata: {}
92
155
  post_install_message:
93
- rdoc_options: []
94
-
95
- require_paths:
96
- - lib
156
+ rdoc_options:
157
+ - "--quiet"
158
+ - "--title"
159
+ - ospfv2
160
+ - "--line-numbers"
161
+ require_paths:
97
162
  - lib
98
- required_ruby_version: !ruby/object:Gem::Requirement
99
- none: false
100
- requirements:
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
101
165
  - - ">="
102
- - !ruby/object:Gem::Version
103
- hash: 3
104
- segments:
105
- - 0
106
- version: "0"
107
- required_rubygems_version: !ruby/object:Gem::Requirement
108
- none: false
109
- requirements:
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
110
170
  - - ">="
111
- - !ruby/object:Gem::Version
112
- hash: 3
113
- segments:
114
- - 0
115
- version: "0"
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
116
173
  requirements: []
117
-
118
174
  rubyforge_project:
119
- rubygems_version: 1.7.2
175
+ rubygems_version: 2.2.2
120
176
  signing_key:
121
- specification_version: 3
122
- summary: An OSPFv2 Emulator.
177
+ specification_version: 4
178
+ summary: Playing with OSPF version 2 using ruby.
123
179
  test_files: []
124
-
@@ -1,38 +0,0 @@
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
@@ -1,430 +0,0 @@
1
-
2
- =begin rdoc
3
-
4
- A.4.1 The LSA header
5
-
6
- All LSAs begin with a common 20 byte header. This header contains
7
- enough information to uniquely identify the LSA (LS type, Link State
8
- ID, and Advertising Router). Multiple instances of the LSA may
9
- exist in the routing domain at the same time. It is then necessary
10
- to determine which instance is more recent. This is accomplished by
11
- examining the LS age, LS sequence number and LS checksum fields that
12
- are also contained in the LSA header.
13
-
14
-
15
- 0 1 2 3
16
- 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
17
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18
- | LS age | Options | LS type |
19
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20
- | Link State ID |
21
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22
- | Advertising Router |
23
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24
- | LS sequence number |
25
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26
- | LS checksum | length |
27
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28
-
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
-
51
-
52
-
53
- LS age
54
- The time in seconds since the LSA was originated.
55
-
56
- Options
57
- The optional capabilities supported by the described portion of
58
- the routing domain. OSPF's optional capabilities are documented
59
- in Section A.2.
60
-
61
- LS type
62
- The type of the LSA. Each LSA type has a separate advertisement
63
- format. The LSA types defined in this memo are as follows (see
64
- Section 12.1.3 for further explanation):
65
-
66
-
67
- LS Type Description
68
- ___________________________________
69
- 1 Router-LSAs
70
- 2 Network-LSAs
71
- 3 Summary-LSAs (IP network)
72
- 4 Summary-LSAs (ASBR)
73
- 5 AS-external-LSAs
74
-
75
-
76
- Link State ID
77
- This field identifies the portion of the internet environment
78
- that is being described by the LSA. The contents of this field
79
- depend on the LSA's LS type. For example, in network-LSAs the
80
- Link State ID is set to the IP interface address of the
81
- network's Designated Router (from which the network's IP address
82
- can be derived). The Link State ID is further discussed in
83
- Section 12.1.4.
84
-
85
- Advertising Router
86
- The Router ID of the router that originated the LSA. For
87
- example, in network-LSAs this field is equal to the Router ID of
88
- the network's Designated Router.
89
-
90
- LS sequence number
91
- Detects old or duplicate LSAs. Successive instances of an LSA
92
- are given successive LS sequence numbers. See Section 12.1.6
93
- for more details.
94
-
95
- LS checksum
96
- The Fletcher checksum of the complete contents of the LSA,
97
- including the LSA header but excluding the LS age field. See
98
- Section 12.1.7 for more details.
99
-
100
- length
101
- The length in bytes of the LSA. This includes the 20 byte LSA
102
- header.
103
-
104
- =end
105
-
106
- require 'infra/ospf_common'
107
- require 'infra/ospf_constants'
108
- require 'ie/id'
109
- require 'ie/ls_type'
110
- require 'ie/ls_age'
111
- require 'ie/sequence_number'
112
- require 'ie/options'
113
- require 'ls_db/advertised_routers'
114
-
115
- require 'infra/to_s'
116
-
117
- module OSPFv2
118
-
119
- class Lsa_Base
120
- include Comparable
121
-
122
- AdvertisingRouter = Class.new(Id)
123
- LsId = Class.new(Id)
124
- LsAge = Class.new(LsAge)
125
-
126
- class << self
127
- def new_ntop(arg)
128
- lsa = new
129
- if arg.is_a?(String)
130
- lsa.parse(arg)
131
- elsif arg.is_a?(self)
132
- lsa.parse arg.encode
133
- else
134
- raise ArgumentError, "Invalid Argument: #{arg.inspect}"
135
- end
136
- lsa
137
- end
138
- end
139
-
140
- include OSPFv2::Common
141
- include OSPFv2::Constant
142
-
143
- # FIXME: when adding LSA in LSDB should be acked when init, rxmt otherwise ....
144
- def ack
145
- @_rxmt_=false
146
- end
147
- def retransmit
148
- @_rxmt_=true
149
- end
150
- def is_acked?
151
- @_rxmt_ == false
152
- end
153
- alias :acked? :is_acked?
154
- alias :ack? :acked?
155
-
156
- attr_reader :ls_age, :options, :ls_type
157
- attr_reader :ls_id
158
- attr_reader :advertising_router
159
- attr_reader :sequence_number
160
-
161
- attr_writer_delegate :advertising_router, :ls_id, :ls_age
162
-
163
- def initialize(arg={})
164
- arg = arg.dup
165
- @ls_age = LsAge.new
166
- @sequence_number = SequenceNumber.new
167
- @options = Options.new
168
- @ls_type = LsType.new klass_to_ls_type
169
- @ls_id = LsId.new
170
- @advertising_router = AdvertisingRouter.new
171
- @_length = 0
172
- @_rxmt_ = false
173
-
174
- if arg.is_a?(Hash)
175
- set arg
176
- elsif arg.is_a?(String)
177
- parse arg
178
- elsif arg.is_a?(self.class)
179
- parse arg.encode
180
- else
181
- raise ArgumentError, "Invalid Argument: #{arg.inspect}"
182
- end
183
-
184
- end
185
-
186
- def sequence_number=(seqn)
187
- @sequence_number = SequenceNumber.new(seqn)
188
- end
189
-
190
- def to_s_default
191
- len = encode.size
192
- ls_type_to_s = ls_type.to_sym.to_s.chomp('_lsa')
193
- sprintf("%-4.0d 0x%2.2x %-8s %-15.15s %-15.15s 0x%8.8x 0x%4.4x %-7d",
194
- ls_age.to_i, options.to_i, ls_type.to_s_short, ls_id.to_ip, advertising_router.to_ip, seqn.to_I,csum_to_i,len)
195
- end
196
- alias :to_s_dd :to_s_default
197
-
198
- def to_s_verbose
199
- len = encode.size
200
- s=[]
201
- s << self.class.to_s.split('::').last + ":"
202
- s << ls_age.to_s
203
- s << options.to_s
204
- s << ls_type.to_s
205
- s << advertising_router.to_s
206
- s << ls_id.to_s
207
- s << "SequenceNumber: " + sequence_number.to_s
208
- s << "LS checksum: #{format "%4x", csum_to_i}" if @_csum
209
- s << "length: #{@_size.unpack('n')}" if @_size
210
- s.join("\n ")
211
- end
212
-
213
- alias :to_s_header :to_s
214
-
215
- def to_s_junos
216
- len = encode.size
217
- sprintf("%-7s %-1.1s%-15.15s %-15.15s 0x%8.8x %4.0d 0x%2.2x 0x%4.4x %3d", LsType.to_junos(ls_type.to_i), '', ls_id.to_ip, advertising_router.to_ip, seqn.to_I, ls_age.to_i, options.to_i, csum_to_i, len)
218
- end
219
- include OSPFv2::TO_S
220
- alias :to_s_junos_verbose :to_s_junos
221
-
222
- def encode_header
223
- header = []
224
- header << ls_age.encode
225
- header << [options.to_i].pack('C')
226
- header << ls_type.encode
227
- header << ls_id.encode
228
- header << advertising_router.encode
229
- header << sequence_number.encode
230
- header << [''].pack('a4')
231
- header.join
232
- end
233
- alias :header_encode :encode_header
234
-
235
- def header_lsa
236
- self.class.new self
237
- end
238
-
239
- def encode(content='')
240
- lsa = []
241
- lsa << encode_header
242
- lsa << content
243
- lsa = lsa.join
244
- lsa[18..19]= @_size = [lsa.size].pack('n')
245
- lsa[16..17]= self.csum = cheksum(lsa[2..-1], 15).pack('CC')
246
- lsa
247
- end
248
-
249
- def encode_request
250
- req=[]
251
- req << ls_id.encode
252
- req << ls_type.encode
253
- req << @advertising_router.encode
254
- req.join
255
- end
256
-
257
- def parse(s)
258
- validate_checksum(s)
259
- ls_age, options, ls_type, ls_id, advr, seqn, csum, length, lsa = s.unpack('nCCNNNnna*')
260
- @ls_type = LsType.new ls_type
261
- @options = Options.new options
262
- @ls_age = LsAge.new ls_age
263
- @sequence_number = SequenceNumber.new seqn
264
- @advertising_router = AdvertisingRouter.new advr
265
- @ls_id = LsId.new ls_id
266
- lsa
267
- end
268
-
269
- def key
270
- [ls_type.to_i, ls_id.to_i, advertising_router.to_i]
271
- end
272
-
273
- # -1 self older than other
274
- # 0 self equivalent to other
275
- # +1 self newer than other
276
- # FIXME: rename to 'newer'
277
- # TODO: compare advr router id.
278
- def <=>(other)
279
- raise RuntimeError unless self.key == other.key
280
- if self.sequence_number < other.sequence_number
281
- # puts "*** jme: our lsa older than other: our seq less than other seq ***"
282
- -1
283
- elsif self.sequence_number > other.sequence_number
284
- # puts "*** jme: our lsa newer than other: our seq greater than other seq ***"
285
- +1
286
- else
287
- if self.csum_to_i < other.csum_to_i
288
- # puts "*** jme: our lsa older than other: our csum less than other csum ***"
289
- -1
290
- elsif self.csum_to_i > other.csum_to_i
291
- # puts "*** jme: our lsa newer than other: our csum greater than other csum ***"
292
- +1
293
- else
294
- if (self.ls_age != other.ls_age and other.ls_age >= MaxAge) or
295
- ((other.ls_age - self.ls_age) > OSPFv2::MaxAgeDiff)
296
- # puts "*** jme: our lsa newer than other: age diff < maxage diff: #{(other.ls_age - self.ls_age)} ***"
297
- +1
298
- else
299
- # puts "*** jme: same lsa: age diff < maxage diff: #{(other.ls_age - self.ls_age)} ***"
300
- 0
301
- end
302
- end
303
- end
304
- end
305
-
306
- def refresh(advertised_routers, refresh_time, seqn=nil)
307
- return unless advertised_routers.has?(advertising_router)
308
- return unless refresh?(refresh_time)
309
- @sequence_number = SequenceNumber.new(seqn) if seqn
310
- @sequence_number + 1
311
- @ls_age = LsAge.new
312
- retransmit
313
- self
314
- end
315
-
316
- def force_refresh(seqn)
317
- @sequence_number = SequenceNumber.new(seqn) if seqn
318
- @sequence_number + 1
319
- @ls_age = LsAge.new
320
- retransmit
321
- self
322
- end
323
-
324
- def maxage
325
- ls_age.maxage and retransmit
326
- self
327
- end
328
-
329
- def maxaged?
330
- @ls_age.maxaged?
331
- end
332
-
333
- def to_hash
334
- h = super
335
- h.delete(:time)
336
- h
337
- end
338
-
339
- def method_missing(method, *args, &block)
340
- # puts "&&&&& #{self.class}: method: #{method}"
341
- # p caller[0]
342
- if method == :to_s_junos
343
- :to_s_default
344
- else
345
- super
346
- end
347
- end
348
-
349
- protected
350
-
351
- def csum_to_i
352
- @_csum.unpack('n')[0]
353
- rescue Exception => e
354
- encode
355
- retry
356
- end
357
-
358
-
359
- private
360
-
361
- def csum=(value)
362
- raise if value.is_a?(Fixnum)
363
- @_csum=value
364
- end
365
-
366
- def refresh?(refresh_time)
367
- ls_age.to_i > refresh_time
368
- end
369
-
370
- def seqn
371
- @sequence_number
372
- end
373
-
374
- def validate_checksum(s)
375
- if ! cheksum(s[2..-1], 0) == [0,0]
376
- puts "*** checksum error ? #{cheksum(s[2..-1], 0)}"
377
- end
378
- end
379
-
380
- #FIXME
381
- def klass_to_ls_type
382
- case self.class
383
- when OSPFv2::Router ; 1
384
- when OSPFv2::Network ; 2
385
- when OSPFv2::Summary ; 3
386
- # when AsExternal ; 5
387
- else
388
- raise
389
- end
390
- end
391
-
392
- MODX=4102
393
-
394
- def cheksum(mess, k=0)
395
- len = mess.size
396
-
397
- if (k>0)
398
- mess[k-1] = [0].pack('C')
399
- mess[k] = [0].pack('C')
400
- end
401
-
402
- c0,c1,n=0,0,0
403
-
404
- s = mess.dup
405
- while s.size>0 and n <= 4102 # MODX
406
- n +=1
407
- c0 += s.slice!(0,1).unpack('C')[0]
408
- c1 +=c0
409
- end
410
-
411
- c0 = c0%255
412
- c1 = c1%255
413
-
414
- ip = (c1 <<8) + c0
415
-
416
- if k>0
417
- iq = ((len-k)*c0 - c1)%255 ; iq += 255 if (iq <= 0)
418
- ir = (510 - c0 - iq) ; ir += -255 if (ir>255)
419
- return [iq,ir]
420
- else
421
- [c0,c1]
422
- end
423
-
424
- end
425
-
426
- end
427
-
428
- end
429
-
430
- load "../../../test/ospfv2/lsa/#{ File.basename($0.gsub(/.rb/,'_test.rb'))}" if __FILE__ == $0