Class: OSPFv2::Neighbor
- Includes:
- OSPFv2, Common, NeighborState
- Defined in:
- lib/neighbor/neighbor.rb,
lib/neighbor/recv_hello.rb,
lib/neighbor/recv_ospf_packet.rb,
lib/neighbor/recv_database_description.rb
Defined Under Namespace
Classes: Trace
Constant Summary collapse
- InactivityTimer =
Class.new(Timer)
- HelloTimer =
Class.new(PeriodicTimer)
- RxmtIntervalTimer =
Class.new(PeriodicTimer)
- RefreshTimer =
Class.new(PeriodicTimer)
- RouterId =
Class.new(Id)
- AreadId =
Class.new(Id)
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, SUMMARY_BASE_ADDRESS, SUMMARY_LSA, VERSION
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#dd_rxmt_interval ⇒ Object
readonly
Returns the value of attribute dd_rxmt_interval.
- #dead_int ⇒ Object
-
#hello ⇒ Object
readonly
Returns the value of attribute hello.
- #hello_int ⇒ Object
-
#inactivity_timer ⇒ Object
readonly
Returns the value of attribute inactivity_timer.
Instance Method Summary collapse
- #change_state(new_state, event = nil) ⇒ Object (also: #new_state)
- #clear_lsa_request_list ⇒ Object
- #dd_sequence_number ⇒ Object
- #debug(obj) ⇒ Object
- #exchange_done ⇒ Object
-
#flood(lsas, dest = AllSPFRouters) ⇒ Object
AllSPFRouters = “224.0.0.5” AllDRouters = “224.0.0.6”.
- #in_state?(*states) ⇒ Boolean
-
#initialize(arg = {}) ⇒ Neighbor
constructor
A new instance of Neighbor.
- #log(ev, obj) ⇒ Object
- #ls_db=(val) ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
- #negotiation_done ⇒ Object
- #recv_database_description(rcv_dd, *args) ⇒ Object
- #recv_hello(hello, from, port) ⇒ Object
- #recv_link_state_ack(ls_ack, from, port) ⇒ Object
- #recv_link_state_request(ls_request, from, port) ⇒ Object
- #recv_link_state_update(ls_update, from, port) ⇒ Object
- #router_id ⇒ Object
- #send(packet, dest = OSPFv2::AllSPFRouters) ⇒ Object
- #send_dd(dd, rxmt = false) ⇒ Object
- #start ⇒ Object
- #start_ls_refresh ⇒ Object
- #start_periodic_hellos ⇒ Object
- #start_periodic_rxmt ⇒ Object
- #state ⇒ Object
- #stop ⇒ Object
- #update(*args) ⇒ Object
Methods included from Common
#ivar_to_klassname, #ivars, #set, #to_hash
Constructor Details
#initialize(arg = {}) ⇒ Neighbor
Returns a new instance of Neighbor.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/neighbor/neighbor.rb', line 64 def initialize(arg={}) #TODO: accept prefix arg and set @address and @netmask in hello... @address = arg[:src_addr] || '127.0.0.1' @state = NeighborState::Down.new @inactivity_timer = InactivityTimer.new(self.dead_int) @periodic_hellos = HelloTimer.new(self.hello_int) @router_id= RouterId.new arg[:router_id] || '1.1.1.1' @area_id= AreaId.new arg[:aread_id] || '0.0.0.0' @lsa_request_list = {} @ls_db, @ev = nil, nil if arg[:log_fname] @trace = Trace.new(arg[:log_fname]) else @trace = Trace.new end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/neighbor/neighbor.rb', line 263 def method_missing(method, *args, &block) if method.to_s =~ /^(recv|unpack)/ puts %{ Method missing : #{method} !!!! #{@neighbor_state} } puts caller.reverse.join("\n") raise else super end end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
53 54 55 |
# File 'lib/neighbor/neighbor.rb', line 53 def address @address end |
#dd_rxmt_interval ⇒ Object (readonly)
Returns the value of attribute dd_rxmt_interval.
53 54 55 |
# File 'lib/neighbor/neighbor.rb', line 53 def dd_rxmt_interval @dd_rxmt_interval end |
#dead_int ⇒ Object
84 85 86 |
# File 'lib/neighbor/neighbor.rb', line 84 def dead_int @dead_int ||= hello_int*4 end |
#hello ⇒ Object (readonly)
Returns the value of attribute hello.
53 54 55 |
# File 'lib/neighbor/neighbor.rb', line 53 def hello @hello end |
#hello_int ⇒ Object
81 82 83 |
# File 'lib/neighbor/neighbor.rb', line 81 def hello_int @hello_int ||= 10 end |
#inactivity_timer ⇒ Object (readonly)
Returns the value of attribute inactivity_timer.
53 54 55 |
# File 'lib/neighbor/neighbor.rb', line 53 def inactivity_timer @inactivity_timer end |
Instance Method Details
#change_state(new_state, event = nil) ⇒ Object Also known as: new_state
132 133 134 135 136 137 |
# File 'lib/neighbor/neighbor.rb', line 132 def change_state(new_state, event=nil) @num ||=0 @num +=1 log 'state change', "#{@num}\# [#{event}]: #{state} -> #{new_state.class.to_s.split('::').last}" @state = new_state end |
#clear_lsa_request_list ⇒ Object
124 125 126 |
# File 'lib/neighbor/neighbor.rb', line 124 def clear_lsa_request_list @lsa_request_list={} end |
#dd_sequence_number ⇒ Object
140 141 142 |
# File 'lib/neighbor/neighbor.rb', line 140 def dd_sequence_number @dd_sequence_number = DatabaseDescription.seqn end |
#debug(obj) ⇒ Object
120 121 122 |
# File 'lib/neighbor/neighbor.rb', line 120 def debug(obj) log :debug, obj if defined? $debug and $debug ==1 end |
#exchange_done ⇒ Object
252 253 254 |
# File 'lib/neighbor/neighbor.rb', line 252 def exchange_done @state.exchange_done(self) end |
#flood(lsas, dest = AllSPFRouters) ⇒ Object
AllSPFRouters = “224.0.0.5” AllDRouters = “224.0.0.6”
200 201 202 |
# File 'lib/neighbor/neighbor.rb', line 200 def flood(lsas, dest=AllSPFRouters) send LinkStateUpdate.new_lsas(lsas), dest end |
#in_state?(*states) ⇒ Boolean
92 93 94 95 96 97 98 |
# File 'lib/neighbor/neighbor.rb', line 92 def in_state?(*states) if states.size==0 state else states.include? state end end |
#log(ev, obj) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/neighbor/neighbor.rb', line 105 def log(ev, obj) if obj.is_a?(String) @trace.trace "\n#{Time.to_ts} #{ev}: #{obj}" else s = [] s << "\n#{Time.to_ts} (#{state}) #{ev} #{obj.name.to_camel}:\n#{obj}" s << obj.encode.hexlify.join("\n ") if defined?($debug) and $debug == 1 s << "\n" @trace.trace s.join end rescue => e p e raise end |
#ls_db=(val) ⇒ Object
100 101 102 103 |
# File 'lib/neighbor/neighbor.rb', line 100 def ls_db=(val) raise ArgumentError, "expecting a LinkStateDatabase object!" unless val.is_a?(LSDB::LinkStateDatabase) @ls_db=val end |
#negotiation_done ⇒ Object
249 250 251 |
# File 'lib/neighbor/neighbor.rb', line 249 def negotiation_done @state.negotiation_done end |
#recv_database_description(rcv_dd, *args) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/neighbor/recv_database_description.rb', line 26 def recv_database_description(rcv_dd, *args) case state when :exstart if router_id.to_i > rcv_dd.router_id.to_i debug "*** #{rcv_dd.router_id} is slave ***" if rcv_dd.master? debug "*** #{rcv_dd.router_id} claims mastership ***" @last_dd_seqn += 1 dd = DatabaseDescription.new :router_id=> @router_id, :area_id=> @area_id, :imms=>7, :dd_sequence_number=>@last_dd_seqn send_dd dd, true elsif rcv_dd.seqn == @last_dd_seqn negotiation_done @last_dd_seqn += 1 if @ls_db @ls_db.recv_dd(rcv_dd, @ls_req_list) dd = DatabaseDescription.new :router_id=> @router_id, :area_id=> @area_id, :ls_db => @ls_db, :number_of_lsa=>60 else dd = DatabaseDescription.new :router_id=> @router_id, :area_id=> @area_id end dd.is_master @last_dd_seqn = dd.seqn = @last_dd_seqn send_dd dd, true else # just stay in ExStart ... debug "*** @last_dd_seqn=#{@last_dd_seqn}, rcv_dd.seqn=#{rcv_dd.seqn} " end else debug "*** #{rcv_dd.router_id} is master ***" if rcv_dd.imms == 0x7 if @ls_db @ls_db.recv_dd(rcv_dd, @ls_req_list) dd = DatabaseDescription.new :router_id=> @router_id, :area_id=> @area_id, :ls_db => @ls_db else dd = DatabaseDescription.new :router_id=> @router_id, :area_id=> @area_id dd.imms = 0 # no more end @last_dd_seqn_rcv = dd.seqn = rcv_dd.seqn dd_rxmt_interval.cancel send_dd dd, true negotiation_done end end when :exchange @tot_dd ||=0 if rcv_dd.init? new_state ExStart.new(self), 'Init' elsif rcv_dd.master? debug "*** #{rcv_dd.router_id} is master ***" if rcv_dd.seqn - @last_dd_seqn_rcv == 1 @tot_dd += 1 @last_dd_seqn_rcv = rcv_dd.seqn if @ls_db @ls_db.recv_dd(rcv_dd, @ls_req_list) @dd = DatabaseDescription.new :router_id=> @router_id, :area_id=> @area_id, :ls_db => @ls_db, :dd_sequence_number=>rcv_dd.seqn else @dd = DatabaseDescription.new :router_id=> @router_id, :area_id=> @area_id @dd.imms = 0 # no more end @dd.dd_sequence_number = rcv_dd.seqn send_dd @dd, true elsif rcv_dd.seqn == @last_dd_seqn_rcv # use rxmt else @state.seq_number_mismatch(self) end unless rcv_dd.more? || @dd.more? dd_rxmt_interval.cancel new_state Loading.new(self), 'exchange_done' new_state Full.new, 'no loading: req list is empty' if @ls_req_list.empty? end else debug "*** #{rcv_dd.router_id} is slave ***" if rcv_dd.seqn == @last_dd_seqn if ! @sent_dd.more? && ! rcv_dd.more? dd_rxmt_interval.cancel new_state Loading.new(self), 'exchange_done' new_state Full.new, 'no loading: req list is empty' if @ls_req_list.empty? else debug "*** OK: @last_dd_seqn=#{@last_dd_seqn}, rcv_dd.seqn=#{rcv_dd.seqn} " @last_dd_seqn += 1 if @ls_db @ls_db.recv_dd(rcv_dd, @ls_req_list) @dd = DatabaseDescription.new :router_id=> @router_id, :area_id=> @area_id, :ls_db => @ls_db, :number_of_lsa=>60 else @dd.no_more end @dd.is_master @dd.seqn = @last_dd_seqn send_dd @dd, true end elsif @last_dd_seqn - rcv_dd.seqn == 1 debug "*** RXMT SHOULD FIX THIS: @last_dd_seqn=#{@last_dd_seqn}, rcv_dd.seqn=#{rcv_dd.seqn} " else debug "*** SHOULD GO TO EXSTART: @last_dd_seqn=#{@last_dd_seqn}, rcv_dd.seqn=#{rcv_dd.seqn} " new_state Full.new, "SeqNumberMismatch" end end else debug "*** recv dd packet while in #{state} #{rcv_dd.imms}***" unless rcv_dd.imms == 0 new_state ExStart.new(self), 'recv dd packet' debug "*** recv dd packet while in #{state} ***" end end end |
#recv_hello(hello, from, port) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/neighbor/recv_hello.rb', line 26 def recv_hello(hello, from, port) @neighbor_ip = from @state.recv_hello self, hello, from rescue Exception => e debug "rescued #{e.inspect}" end |
#recv_link_state_ack(ls_ack, from, port) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/neighbor/recv_ospf_packet.rb', line 58 def recv_link_state_ack(ls_ack, from, port) return unless @ls_db before = @ls_db.all_not_acked.size ls_ack.each { |lsa| @ls_db.ls_ack lsa } debug "*** number of lsa acked : #{before - @ls_db.all_not_acked.size} ***" end |
#recv_link_state_request(ls_request, from, port) ⇒ Object
36 37 38 |
# File 'lib/neighbor/recv_ospf_packet.rb', line 36 def recv_link_state_request(ls_request, from, port) send ls_request.to_lsu(@ls_db, :area_id=> @aread_id, :router_id => @router_id), from end |
#recv_link_state_update(ls_update, from, port) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/neighbor/recv_hello.rb', line 33 def recv_link_state_update(ls_update, from, port) ls_ack = LinkStateAck.ack_ls_update ls_update, :area_id=> @area_id, :router_id=> @router_id send ls_ack, OSPFv2::AllDRouters #from unless @ls_req_list.empty? ls_update.each { |l| if @ls_req_list.has_key?(l.key) debug "*** deleting #{l.key.inspect} from Ls Req List! ***" @ls_req_list.delete(l.key) end } new_state Full.new, 'loading_done' if @ls_req_list.empty? end @ls_db.recv_link_state_update ls_update end |
#router_id ⇒ Object
128 129 130 |
# File 'lib/neighbor/neighbor.rb', line 128 def router_id hello.router_id end |
#send(packet, dest = OSPFv2::AllSPFRouters) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/neighbor/neighbor.rb', line 176 def send(packet, dest=OSPFv2::AllSPFRouters) # p "about to send 111111 #{packet.class} " # p "@output? #{@output ? true : false} #{packet.class}" # p @output # p "--------------" return unless @output case packet when Array # p "WE HAVE AN ARRAY OF PACKET!" packet.each { |p| # p "about to send p 333333 #{p.inspect}" # log :snd, p @output.enq [p,dest] } else log :snd, packet # p "about to send packet 333333 #{packet.inspect}" @output.enq [packet,dest] # p "about to send 333333 #{packet.inspect}" end end |
#send_dd(dd, rxmt = false) ⇒ Object
256 257 258 259 260 261 |
# File 'lib/neighbor/neighbor.rb', line 256 def send_dd(dd, rxmt=false) dd_rxmt_interval.cancel dd_rxmt_interval.start { debug "\n\n\n*** re-transmitting #{dd} ***\n\n\n" ; send dd } if rxmt send dd, @neighbor_ip @sent_dd = dd end |
#start ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/neighbor/neighbor.rb', line 143 def start unless @ev @ev = NeighborEventHandler.new(self) @evQ = Queue.new end @dd_rxmt_interval = RxmtIntervalTimer.new(5,@ev) @periodic_rxmt = RxmtIntervalTimer.new(5,@ev) @periodic_refresh = RefreshTimer.new(60,@ev) init_sockets @address init_io start_io start_periodic_hellos @state.start(self) self end |
#start_ls_refresh ⇒ Object
239 240 241 242 243 244 245 246 |
# File 'lib/neighbor/neighbor.rb', line 239 def start_ls_refresh return unless @ls_db debug "*** about to start periodic refresh timer ***" @periodic_refresh.start { @ls_db.refresh if in_state? :full, :loading, :exchange } nil end |
#start_periodic_hellos ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/neighbor/neighbor.rb', line 204 def start_periodic_hellos @periodic_hellos.cancel hh = { :router_id=> @router_id, :netmask=> 0xffffff00, :designated_router_id=> '0.0.0.0', :backup_designated_router_id=> '0.0.0.0', :helloInt=>hello_int, :options=>2, :rtr_pri=>0, :deadInt=>dead_int, } @hello = Hello.new(hh) @periodic_hellos.start { send(@hello) } end |
#start_periodic_rxmt ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/neighbor/neighbor.rb', line 222 def start_periodic_rxmt debug "*** about to start periodic rxmt timer ***" @periodic_rxmt.start { if @ls_req_list debug "There are #{@ls_req_list.size} LS Request to re-transmit!" send LinkStateRequest.new :area_id => @area_id, :router_id=> @router_id, :requests=> @ls_req_list.keys \ unless @ls_req_list.empty? end if @ls_db lsas = @ls_db.all_not_acked debug "There are #{lsas.size} LSA to re-transmit!" send LinkStateUpdate.new_lsas :router_id=> @router_id, :area_id => @area_id, :lsas => lsas unless lsas.empty? end } end |
#state ⇒ Object
88 89 90 |
# File 'lib/neighbor/neighbor.rb', line 88 def state @state.class.to_s.split('::').last.downcase.to_sym end |
#stop ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/neighbor/neighbor.rb', line 159 def stop debug "*** stopping #{router_id}" @periodic_hellos.cancel @periodic_rxmt.cancel @periodic_refresh.cancel stop_io close_sockets rescue Exception => e debug "#{e} while stopping neighor" ensure @state.kill_nbr(self) self end |
#update(*args) ⇒ Object
173 174 175 |
# File 'lib/neighbor/neighbor.rb', line 173 def update(*args) @evQ.enq(*args) end |