Class: OSPFv2::Neighbor

Inherits:
Object show all
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, AttachRouter, 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, NetworkMask, 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

Instance Method Summary collapse

Methods included from Common

#ivar_to_klassname, #ivars, #set, #to_hash

Constructor Details

#initialize(arg = {}) ⇒ Neighbor

Returns a new instance of Neighbor.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/neighbor/neighbor.rb', line 65

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=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



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/neighbor/neighbor.rb', line 248

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

#addressObject (readonly)

Returns the value of attribute address.



54
55
56
# File 'lib/neighbor/neighbor.rb', line 54

def address
  @address
end

#dd_rxmt_intervalObject (readonly)

Returns the value of attribute dd_rxmt_interval.



54
55
56
# File 'lib/neighbor/neighbor.rb', line 54

def dd_rxmt_interval
  @dd_rxmt_interval
end

#dead_intObject



85
86
87
# File 'lib/neighbor/neighbor.rb', line 85

def dead_int
  @dead_int ||= hello_int*4
end

#helloObject (readonly)

Returns the value of attribute hello.



54
55
56
# File 'lib/neighbor/neighbor.rb', line 54

def hello
  @hello
end

#hello_intObject



82
83
84
# File 'lib/neighbor/neighbor.rb', line 82

def hello_int
  @hello_int ||= 10
end

#inactivity_timerObject (readonly)

Returns the value of attribute inactivity_timer.



54
55
56
# File 'lib/neighbor/neighbor.rb', line 54

def inactivity_timer
  @inactivity_timer
end

Instance Method Details

#change_state(new_state, event = nil) ⇒ Object Also known as: new_state



130
131
132
133
134
135
# File 'lib/neighbor/neighbor.rb', line 130

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_listObject



122
123
124
# File 'lib/neighbor/neighbor.rb', line 122

def clear_lsa_request_list
  @lsa_request_list={}
end

#dd_sequence_numberObject



138
139
140
# File 'lib/neighbor/neighbor.rb', line 138

def dd_sequence_number
  @dd_sequence_number = DatabaseDescription.seqn
end

#debug(obj) ⇒ Object



118
119
120
# File 'lib/neighbor/neighbor.rb', line 118

def debug(obj)
  log :debug, obj  if defined? $debug and $debug ==1
end

#exchange_doneObject



237
238
239
# File 'lib/neighbor/neighbor.rb', line 237

def exchange_done
  @state.exchange_done(self)
end

#flood(lsas, dest = AllSPFRouters) ⇒ Object

AllSPFRouters = “224.0.0.5” AllDRouters = “224.0.0.6”



184
185
186
187
# File 'lib/neighbor/neighbor.rb', line 184

def flood(lsas, dest=AllSPFRouters)
  # return unless @output
  send (LinkStateUpdate.new_lsas lsas), dest
end

#in_state?(*states) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
# File 'lib/neighbor/neighbor.rb', line 93

def in_state?(*states)
  if states.size==0
    state
  else
    states.include? state
  end
end

#log(ev, obj) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/neighbor/neighbor.rb', line 106

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
end

#ls_db=(val) ⇒ Object

Raises:

  • (ArgumentError)


101
102
103
104
# File 'lib/neighbor/neighbor.rb', line 101

def ls_db=(val)
  raise ArgumentError, "expecting a LinkStateDatabase object!" unless val.is_a?(LSDB::LinkStateDatabase)
  @ls_db=val
end

#negotiation_doneObject



234
235
236
# File 'lib/neighbor/neighbor.rb', line 234

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


59
60
61
62
63
64
# File 'lib/neighbor/recv_ospf_packet.rb', line 59

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


33
34
35
36
# File 'lib/neighbor/recv_hello.rb', line 33

def recv_link_state_request(ls_request, from, port)
  #TODO: check what address the LSU shoul be send to ? unicast ? AllDRouteres ? AllSpfRouters ?
  send ls_request.to_lsu(@ls_db, :area_id=> @aread_id, :router_id => @router_id), from 
end


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/neighbor/recv_hello.rb', line 38

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_idObject



126
127
128
# File 'lib/neighbor/neighbor.rb', line 126

def router_id
  hello.router_id
end

#send(packet, dest = OSPFv2::AllSPFRouters) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/neighbor/neighbor.rb', line 174

def send(packet, dest=OSPFv2::AllSPFRouters)
  return unless @output
  [packet].flatten.each { |p| 
    log :snd, p
    @output.enq [p,dest]
  }
end

#send_dd(dd, rxmt = false) ⇒ Object



241
242
243
244
245
246
# File 'lib/neighbor/neighbor.rb', line 241

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

#startObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/neighbor/neighbor.rb', line 141

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_refreshObject



224
225
226
227
228
229
230
231
# File 'lib/neighbor/neighbor.rb', line 224

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_hellosObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/neighbor/neighbor.rb', line 189

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_rxmtObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/neighbor/neighbor.rb', line 207

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

#stateObject



89
90
91
# File 'lib/neighbor/neighbor.rb', line 89

def state
  @state.class.to_s.split('::').last.downcase.to_sym
end

#stopObject



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/neighbor/neighbor.rb', line 157

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



171
172
173
# File 'lib/neighbor/neighbor.rb', line 171

def update(*args)
  @evQ.enq *args
end