Skip to content

Instantly share code, notes, and snippets.

@shun159
Last active December 20, 2015 06:19
Show Gist options
  • Save shun159/6085392 to your computer and use it in GitHub Desktop.
Save shun159/6085392 to your computer and use it in GitHub Desktop.
#
# Routing Table And Routing Cache implementation in TremaEdge
# Auther: Eishun Kondoh(e_kondo@ap-com.co.jp)
# Copyright (C) 2013 APCOMMUNICATIONS CO., LTD.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
require "ipaddr"
require "rubygems"
require "sqlite3"
require "narray"
class RoutingTable
def initialize
@entry = {}
@rt_array = {}
@MASK_LEN = NArray[ 32..0 ]
@ROUTING_TABLE = SQLite3::Database.new("Traffic_Load_Balancer/RoutingTable.sqlite")
@ROUTING_TABLE.results_as_hash = true
begin
@ROUTING_TABLE.execute(<<-SQL)
CREATE TABLE IF NOT EXISTS ROUTING_TABLE ( Network PRIMARY KEY, masklen INTEGER, Nexthop );
SQL
rescue SQLite3::Exception => e
puts " #{ e.message } initialize"
end
sql = <<-SQL
SELECT * FROM ROUTING_TABLE;
SQL
@lookup_route3 = @ROUTING_TABLE.execute( sql ) do | row |
@rt_array[row["Network"]] = row
end
end
def lookup_route dest
@MASK_LEN.each do | masklen |
return @rt_array[( IPAddr.new( dest.to_s ).mask( masklen )).to_s] if @rt_array.include? (( IPAddr.new( dest.to_s ).mask( masklen )).to_s)
end
end
end
### Local variables:
### mode: Ruby
### coding: utf-8-unix
### indent-tabs-mode: nil
### End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment