Thread: [Javanetsim-cvs] javaNetSim/core CommandProcessor.java, 1.21, 1.22 DeviceConfig.java, 1.17, 1.18 NA
Status: Beta
Brought to you by:
darkkey
Update of /cvsroot/javanetsim/javaNetSim/core In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv31762/core Modified Files: CommandProcessor.java DeviceConfig.java NATEngine.java NetworkInterface.java NetworkLayerDevice.java Log Message: NAT command was added Index: NetworkLayerDevice.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/NetworkLayerDevice.java,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** NetworkLayerDevice.java 7 Oct 2008 08:25:02 -0000 1.20 --- NetworkLayerDevice.java 10 Oct 2008 22:18:05 -0000 1.21 *************** *** 51,54 **** --- 51,55 ---- private DeviceConfig config = new DeviceConfig(this); private AccessListEngine acls = new AccessListEngine(this); + private NATEngine nat = new NATEngine(this); /** *************** *** 435,437 **** --- 436,442 ---- return acls; } + + public NATEngine getNAT(){ + return nat; + } }//EOF Index: NATEngine.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/NATEngine.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** NATEngine.java 7 Oct 2008 08:25:02 -0000 1.1 --- NATEngine.java 10 Oct 2008 22:18:05 -0000 1.2 *************** *** 13,16 **** --- 13,22 ---- public int acl = 0; // acl number for dynamic + public final static int IP = 0; + public final static int ICMP = 1; + public final static int UDP = 2; + public final static int TCP = 3; + public int protocol = IP; + public String in_ip = ""; public int in_port = 0; *************** *** 29,31 **** --- 35,87 ---- rules = new Vector<NAT_rule>(); } + + public int addRule(NAT_rule rule){ + return addRule(rule,-1); + } + + public int addRule(NAT_rule rule, int pos){ + if(pos<0 || pos>=rules.size()){ + rules.add(rule); + return rules.size()-1; + } + rules.add(pos, rule); + return pos; + } + + public boolean removeRule(int rnum){ + if(rnum>=0 && rnum<rules.size()){ + rules.remove(rnum); + return true; + } + return false; + } + + public Vector<Integer> findRules(Boolean dynamic, Boolean pool, Integer acl, String in_ip, Integer in_port, String out_ip, Integer out_port, String out_int){ + Vector<Integer> result = new Vector<Integer>(); + for(int i=0; i<rules.size(); i++){ + NAT_rule rule = rules.get(i); + if((dynamic==null || rule.dynamic==dynamic.booleanValue()) + && (pool==null || rule.pool==pool.booleanValue()) + && (acl==null || rule.acl==acl.intValue()) + && (in_ip==null || rule.in_ip.equalsIgnoreCase(in_ip)) + && (in_port==null || rule.in_port==in_port.intValue()) + && (out_ip==null || rule.out_ip.equalsIgnoreCase(out_ip)) + && (out_port==null || rule.out_port==out_port.intValue()) + && (out_int==null || rule.out_int.equalsIgnoreCase(out_int))){ + result.add(new Integer(i)); + } + } + return result; + } + + public NAT_rule getRule(int i){ + if(i>=0 && i<rules.size()){ + return rules.get(i); + } + return null; + } + + public int countRules(){ + return rules.size(); + } } Index: CommandProcessor.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/CommandProcessor.java,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** CommandProcessor.java 5 Oct 2008 20:57:00 -0000 1.21 --- CommandProcessor.java 10 Oct 2008 22:18:05 -0000 1.22 *************** *** 32,35 **** --- 32,36 ---- import core.CommandInterface.Modes; + import core.NATEngine.NAT_rule; import core.WiFiPort.APClient; import core.AccessListEngine.access_list; *************** *** 100,103 **** --- 101,105 ---- private show_ip_CommandClass show_ip_Command = new show_ip_CommandClass(); private show_ip_route_CommandClass show_ip_route_Command = new show_ip_route_CommandClass(); + private show_ip_nat_translation_CommandClass show_ip_nat_translation_Command = new show_ip_nat_translation_CommandClass(); private show_kron_CommandClass show_kron_Command = new show_kron_CommandClass(); private show_location_CommandClass show_location_Command = new show_location_CommandClass(); *************** *** 185,191 **** commands.addDescription("ip nat","NAT configuration commands"); commands.addDescription("ip nat inside","Inside address translation"); ! commands.addDescription("ip nat inside destination","Destination address translation"); commands.addDescription("ip nat inside source","Source address translation"); ! commands.add("ip nat inside destination list", ip_nat_inside_destination_list_Command, "Specify access list describing global addresses"); commands.add("ip nat inside source list", ip_nat_inside_source_list_Command, "Specify access list describing local addresses"); commands.add("ip nat inside source static", ip_nat_inside_source_static_Command, "Specify static local->global mapping"); --- 187,193 ---- commands.addDescription("ip nat","NAT configuration commands"); commands.addDescription("ip nat inside","Inside address translation"); ! //commands.addDescription("ip nat inside destination","Destination address translation"); commands.addDescription("ip nat inside source","Source address translation"); ! //commands.add("ip nat inside destination list", ip_nat_inside_destination_list_Command, "Specify access list describing global addresses"); commands.add("ip nat inside source list", ip_nat_inside_source_list_Command, "Specify access list describing local addresses"); commands.add("ip nat inside source static", ip_nat_inside_source_static_Command, "Specify static local->global mapping"); *************** *** 214,217 **** --- 216,221 ---- commands.add("show ip", show_ip_Command, "IP information"); commands.add("show ip route", show_ip_route_Command, "Print route table"); + commands.addDescription("show ip nat","IP NAT information"); + commands.add("show ip nat translation", show_ip_nat_translation_Command, "Translation entries"); commands.add("show kron", show_kron_Command, "Kron Subsystem"); commands.add("show location", show_location_Command, "Display the system location"); *************** *** 987,995 **** } public String call(Vector<String> params){ ! String out = "Command not supported yet.\n"; if(device instanceof ApplicationLayerDevice){ if(params.size()==2){ try { ! device.getNetworkInterface(params.get(0)); } catch (InvalidNetworkInterfaceNameException ex) { out += "error: invalid inferface\n"; --- 991,1009 ---- } public String call(Vector<String> params){ ! String out = ""; if(device instanceof ApplicationLayerDevice){ if(params.size()==2){ try { ! NetworkInterface ni = device.getNetworkInterface(params.get(0)); ! String natt = params.get(1); ! if(natt.equalsIgnoreCase("inside")){ ! ni.setNAT(NetworkInterface.INSIDE_NAT); ! } ! else if(natt.equalsIgnoreCase("outside")){ ! ni.setNAT(NetworkInterface.OUTSIDE_NAT); ! } ! else{ ! out += "error: invalid parameter '"+natt+"'\n"; ! } } catch (InvalidNetworkInterfaceNameException ex) { out += "error: invalid inferface\n"; *************** *** 1006,1018 **** } public String no_call(Vector<String> params){ ! String out = "Command not supported yet.\n"; if(device instanceof ApplicationLayerDevice){ ! if(params.size()==2){ try { ! device.getNetworkInterface(params.get(0)); } catch (InvalidNetworkInterfaceNameException ex) { out += "error: invalid inferface\n"; } - // device.getConfig().remove("^interface "+params.get(0)+" ip nat "+params.get(1)); } else{ --- 1020,1046 ---- } public String no_call(Vector<String> params){ ! String out = ""; if(device instanceof ApplicationLayerDevice){ ! if(params.size()==2 || params.size()==1){ try { ! NetworkInterface ni = device.getNetworkInterface(params.get(0)); ! if(params.size()==1){ ! ni.setNAT(NetworkInterface.NO_NAT); ! } ! else{ ! String natt = params.get(1); ! if(natt.equalsIgnoreCase("inside") && ni.getNAT()==NetworkInterface.INSIDE_NAT){ ! ni.setNAT(NetworkInterface.NO_NAT); ! } ! else if(natt.equalsIgnoreCase("outside") && ni.getNAT()==NetworkInterface.OUTSIDE_NAT){ ! ni.setNAT(NetworkInterface.NO_NAT); ! } ! else{ ! out += "error: invalid parameter '"+natt+"'\n"; ! } ! } } catch (InvalidNetworkInterfaceNameException ex) { out += "error: invalid inferface\n"; } } else{ *************** *** 2059,2066 **** } if(add){ ! //nat.inside.src.list.add(iacl,poolname,overload); } else{ ! //nat.inside.src.list.remove(iacl,poolname); } } --- 2087,2144 ---- } if(add){ ! if(overload){ ! NAT_rule rule = device.getNAT().new NAT_rule(); ! rule.dynamic = false; ! rule.pool = true; ! rule.acl = iacl; ! rule.out_int = poolname; ! device.getNAT().addRule(rule); ! } ! else{ ! out += "error: parameter 'overload' expected\n"; ! } } else{ ! Vector<Integer> finds = device.getNAT().findRules(false, true, iacl, "", 0, "", 0, poolname); ! if(finds.size()>0){ ! device.getNAT().removeRule(finds.get(0).intValue()); ! } ! else{ ! out += "error: record not found\n"; ! } ! } ! } ! else if(params.get(1).equalsIgnoreCase("interface")){ ! String intname = params.get(2); ! try { ! device.getNetworkInterface(intname); ! boolean overload = false; ! if(params.size()==4){ ! overload = params.get(3).equalsIgnoreCase("overload"); ! } ! if(add){ ! if(overload){ ! NAT_rule rule = device.getNAT().new NAT_rule(); ! rule.dynamic = false; ! rule.pool = false; ! rule.acl = iacl; ! rule.out_int = intname; ! device.getNAT().addRule(rule); ! } ! else{ ! out += "error: parameter 'overload' expected\n"; ! } ! } ! else{ ! Vector<Integer> finds = device.getNAT().findRules(false, false, iacl, "", 0, "", 0, intname); ! if(finds.size()>0){ ! device.getNAT().removeRule(finds.get(0).intValue()); ! } ! else{ ! out += "error: record not found\n"; ! } ! } ! } catch (InvalidNetworkInterfaceNameException e) { ! out += "error: invalid interface name\n"; } } *************** *** 2086,2091 **** public ip_nat_inside_source_static_CommandClass(){ modes = new Modes(CommandInterface.CONF_MODE, CommandInterface.APPLICATION_LAYER, CommandInterface.NO_CALL); ! call_params = "(ip|tcp|udp) <local ip> [<local port>] <global ip> [<global ip>]"; ! no_call_params = "(ip|tcp|udp) <local ip> [<local port>] <global ip> [<global ip>]"; } public String call(Vector<String> params){ --- 2164,2169 ---- public ip_nat_inside_source_static_CommandClass(){ modes = new Modes(CommandInterface.CONF_MODE, CommandInterface.APPLICATION_LAYER, CommandInterface.NO_CALL); ! call_params = "(ip|tcp|udp) <local ip> [<local port>] <global ip> [<global port>]"; ! no_call_params = "(ip|tcp|udp) <local ip> [<local port>] <global ip> [<global port>]"; } public String call(Vector<String> params){ *************** *** 2096,2106 **** } private String parse(Vector<String> params, boolean add){ ! int IP_protocol = 0; ! int TCP_protocol = 1; ! int UDP_protocol = 2; String out = ""; if(params.size()==3 || params.size()==5){ try{ String sprotocol = params.get(0); int protocol = -1; --- 2174,2185 ---- } private String parse(Vector<String> params, boolean add){ ! int IP_protocol = NAT_rule.IP; ! int TCP_protocol = NAT_rule.TCP; ! int UDP_protocol = NAT_rule.UDP; String out = ""; if(params.size()==3 || params.size()==5){ try{ + boolean valid = true; String sprotocol = params.get(0); int protocol = -1; *************** *** 2131,2142 **** } } ! if(IPV4Address.validateDecIP(local_ip) && IPV4Address.validateDecIP(global_ip)){ if(add){ ! //nat.inside.src.static.add(protocol, local_ip, local_port, global_ip, global_port); } else{ ! //nat.inside.src.static.remove(protocol, local_ip, local_port, global_ip, global_port); } } } catch(NumberFormatException e){ --- 2210,2245 ---- } } ! else{ ! valid = false; ! } ! if(valid && IPV4Address.validateDecIP(local_ip) && IPV4Address.validateDecIP(global_ip)){ if(add){ ! NAT_rule rule = device.getNAT().new NAT_rule(); ! rule.in_ip = local_ip; ! rule.out_ip = global_ip; ! rule.in_port = local_port; ! rule.out_port = local_port; ! rule.protocol = protocol; ! rule.dynamic = false; ! rule.pool = false; ! device.getNAT().addRule(rule); } else{ ! Vector<Integer> finds; ! if(params.size()==3) ! finds = device.getNAT().findRules(false, false, 0, local_ip, 0, global_ip, 0, ""); ! else ! finds = device.getNAT().findRules(false, false, 0, local_ip, local_port, global_ip, global_port, ""); ! if(finds.size()>0){ ! device.getNAT().removeRule(finds.get(0).intValue()); ! } ! else{ ! out += "error: nat rule not found\n"; ! } } } + else{ + out += "error: invalid parameters\n"; + } } catch(NumberFormatException e){ *************** *** 2163,2167 **** } private String parse(Vector<String> params, boolean add){ ! String out = ""; if(params.size()==3){ String poolname = params.get(0); --- 2266,2270 ---- } private String parse(Vector<String> params, boolean add){ ! String out = "Command not supported yet\n"; if(params.size()==3){ String poolname = params.get(0); *************** *** 2817,2820 **** --- 2920,2955 ---- } }; + class show_ip_nat_translation_CommandClass extends CommandInterface{ + public show_ip_nat_translation_CommandClass(){ + modes = new Modes(CommandInterface.STD_CONF_MODE, CommandInterface.NETWORK_LAYER, CommandInterface.CALL_ONLY); + call_params = "<cr>"; + } + public String call(Vector<String> params){ + String out = ""; + if(params.size()==0){ + out += "Protocol Inside Outside\n"; + NATEngine nat = device.getNAT(); + for(int i=0; i<nat.countRules(); i++){ + NAT_rule rule = nat.getRule(i); + String inside = rule.in_ip; + if(rule.in_port>0) inside += ":"+rule.in_port; + String outside = rule.out_ip; + if(rule.out_port>0) outside += ":"+rule.out_port; + String protocol = ""; + switch(rule.protocol){ + case NAT_rule.IP: protocol="ip"; break; + case NAT_rule.ICMP: protocol="icmp"; break; + case NAT_rule.UDP: protocol="udp"; break; + case NAT_rule.TCP: protocol="tcp"; break; + } + out += String.format("%-12s%-23s%s\n", protocol, inside, outside); + } + } + else{ + out += "error: invalid parameters\n"; + } + return out; + } + }; class show_kron_CommandClass extends CommandInterface{ public show_kron_CommandClass(){ Index: DeviceConfig.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/DeviceConfig.java,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** DeviceConfig.java 5 Oct 2008 18:08:55 -0000 1.17 --- DeviceConfig.java 10 Oct 2008 22:18:05 -0000 1.18 *************** *** 16,19 **** --- 16,21 ---- import java.util.regex.PatternSyntaxException; import core.CommandInterface.Modes; + import core.NATEngine.NAT_rule; + import java.util.Enumeration; import java.util.Hashtable; *************** *** 457,460 **** --- 459,467 ---- if(ni.getACLout()!=0) conf.add("interface "+intName+" ip access-group "+ni.getACLout()+" out"); + switch(ni.getNAT()){ + case NetworkInterface.NO_NAT: break; + case NetworkInterface.INSIDE_NAT: conf.add("interface "+intName+" ip nat inside"); break; + case NetworkInterface.OUTSIDE_NAT: conf.add("interface "+intName+" ip nat outside"); break; + } if(!ni.unreachables) conf.add("no interface "+intName+" ip unreachables"); *************** *** 560,564 **** --- 567,600 ---- } + protected void fillIP(LinkedList conf){ + if(device instanceof NetworkLayerDevice){ + NetworkLayerDevice ndev = (NetworkLayerDevice)device; + NATEngine nat = ndev.getNAT(); + for(int i=0; i<nat.countRules(); i++){ + String params; + NAT_rule rule = nat.getRule(i); + if(!rule.dynamic){ + if(rule.acl>0){ + params = " list "+rule.acl; + params += (rule.pool?" pool ":" interface ")+rule.out_int; + } + else{ + params = " static"; + switch(rule.protocol){ + case NAT_rule.IP: params+=" ip"; break; + case NAT_rule.ICMP: params+=" icmp"; break; + case NAT_rule.UDP: params+=" udp"; break; + case NAT_rule.TCP: params+=" tcp"; break; + } + params += " "+rule.in_ip; + if(rule.in_port>0) params += " "+rule.in_port; + params += " "+rule.out_ip; + if(rule.out_port>0) params += " "+rule.out_port; + } + conf.add("ip nat inside source"+params); + } + } + } if(device instanceof ApplicationLayerDevice){ DHCPD dhcpd = (DHCPD)((ApplicationLayerDevice)device).getApp(PC.DHCP_SERVER_ID); Index: NetworkInterface.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/NetworkInterface.java,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** NetworkInterface.java 7 Oct 2008 08:25:02 -0000 1.18 --- NetworkInterface.java 10 Oct 2008 22:18:05 -0000 1.19 *************** *** 197,202 **** public final static int NO_NAT = 0; ! public final static int INSIDE_NAT = 0; ! public final static int OUTSIDE_NAT = 1; private int NAT_STATUS = 0; --- 197,202 ---- public final static int NO_NAT = 0; ! public final static int INSIDE_NAT = 1; ! public final static int OUTSIDE_NAT = 2; private int NAT_STATUS = 0; |