Skip to content

Instantly share code, notes, and snippets.

@shun159
Last active December 20, 2015 06:19
Show Gist options
  • Save shun159/6085336 to your computer and use it in GitHub Desktop.
Save shun159/6085336 to your computer and use it in GitHub Desktop.
#
# NAPT TABLE 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 "rubygems"
require "narray"
class NaptTable
def initialize
@data = {}
@free_ports = ((1025..65535).to_a).reverse
end
def create_entry i_local_addr, i_local_tp, i_global_addr,o_local_addr, o_local_tp
index = i_local_addr + i_local_tp.to_s + o_local_addr + o_local_tp.to_s
if not( @data.include?(index))
i_global_tp = @free_ports.pop
@data[index] = NArray[i_local_addr, i_local_tp, i_global_addr, i_global_tp, o_local_addr, o_local_tp]
end
return @data[index]
end
def napt_entry_aged_by nw_src, nw_dst, tp_src, tp_dst
index = nw_src.to_s + tp_src.to_s + nw_dst.to_s + tp_dst.to_s
if @data.include?(index)
port = @data[index][3]
@data.delete(index)
@free_ports.push( port )
end
end
end
### Local variables:
### mode: Ruby
### coding: utf-8
### indent-tabs-mode: nil
### End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment