[Javanetsim-cvs] javaNetSim/core AccessListEngine.java, 1.1, 1.2 CommandProcessor.java, 1.7, 1.8 Co
Status: Beta
Brought to you by:
darkkey
From: QweR <qw...@us...> - 2007-10-23 20:14:55
|
Update of /cvsroot/javanetsim/javaNetSim/core In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv14031/core Modified Files: AccessListEngine.java CommandProcessor.java CommandsTree.java DeviceConfig.java NetworkLayerDevice.java Log Message: ACLs in configs (ip acc, show acc, show run) works Index: NetworkLayerDevice.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/NetworkLayerDevice.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** NetworkLayerDevice.java 19 Oct 2007 08:52:26 -0000 1.12 --- NetworkLayerDevice.java 23 Oct 2007 20:14:48 -0000 1.13 *************** *** 49,52 **** --- 49,53 ---- private DeviceConfig config = new DeviceConfig(this); + private AccessListEngine acls = new AccessListEngine(this); /** *************** *** 74,78 **** public void turnOff(){ - config.clear(DeviceConfig.RUNNING_CONFIG); super.turnOff(); } --- 75,78 ---- *************** *** 350,352 **** --- 350,355 ---- } + public AccessListEngine getACL(){ + return acls; + } }//EOF Index: CommandProcessor.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/CommandProcessor.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** CommandProcessor.java 20 Oct 2007 22:54:40 -0000 1.7 --- CommandProcessor.java 23 Oct 2007 20:14:48 -0000 1.8 *************** *** 26,29 **** --- 26,30 ---- import java.util.Vector; import core.CommandInterface.Modes; + import core.AccessListEngine.access_list; /** *************** *** 43,47 **** private copy_running_startup_CommandClass copy_running_startup_Command = new copy_running_startup_CommandClass(); private copy_startup_running_CommandClass copy_startup_running_Command = new copy_startup_running_CommandClass(); - private erase_running_config_CommandClass erase_running_config_Command = new erase_running_config_CommandClass(); private erase_startup_config_CommandClass erase_startup_config_Command = new erase_startup_config_CommandClass(); private help_CommandClass help_Command = new help_CommandClass(); --- 44,47 ---- *************** *** 123,127 **** commands.add("copy startup-config running-config", copy_startup_running_Command, "Copy from one file to another"); commands.addDescription("erase","Erase a filesystem"); - commands.add("erase running-config", erase_running_config_Command, "Erase a running-config"); commands.add("erase startup-config", erase_startup_config_Command, "Erase a startup-config"); commands.add("help", help_Command, "Description of the interactive help system"); --- 123,126 ---- *************** *** 343,347 **** } public String call(Vector<String> params){ ! boolean res = device.getConfig().copy(DeviceConfig.RUNNING_CONFIG, DeviceConfig.STARTUP_CONFIG); String out = "Startup-config is not empty!\n"; if(res){ --- 342,346 ---- } public String call(Vector<String> params){ ! boolean res = device.getConfig().writeMemory(); String out = "Startup-config is not empty!\n"; if(res){ *************** *** 372,386 **** } public String call(Vector<String> params){ ! device.getConfig().clear(DeviceConfig.STARTUP_CONFIG); ! return "Config was erased\n"; ! } ! }; ! class erase_running_config_CommandClass extends CommandInterface{ ! public erase_running_config_CommandClass(){ ! modes = new Modes(CommandInterface.STD_CONF_MODE, CommandInterface.NETWORK_LAYER, CommandInterface.CALL_ONLY); ! call_params = "<cr>"; ! } ! public String call(Vector<String> params){ ! device.getConfig().clear(DeviceConfig.RUNNING_CONFIG); return "Config was erased\n"; } --- 371,375 ---- } public String call(Vector<String> params){ ! device.getConfig().clearStartupConfig(); return "Config was erased\n"; } *************** *** 944,959 **** public ip_access_list_standart_CommandClass(){ modes = new Modes(CommandInterface.CONF_MODE, CommandInterface.NETWORK_LAYER, CommandInterface.NO_CALL); ! call_params = "<1-2699 | WORD> [<line>] (permit|deny) (any|host <ip>|<ip> <mask>)"; ! no_call_params = "<1-2699 | WORD> [<line>] (permit|deny) (any|host <ip>|<ip> <mask>)"; } public String call(Vector<String> params){ String out = ""; ! if(params.size()>=3){ ! device.getConfig().remove("^"); ! device.getConfig().add(""); } ! else{ out += "error: invalid parameters\n"; } return out; } --- 933,993 ---- public ip_access_list_standart_CommandClass(){ modes = new Modes(CommandInterface.CONF_MODE, CommandInterface.NETWORK_LAYER, CommandInterface.NO_CALL); ! call_params = "<1-99> [<line>] (permit|deny) (any|host <ip>|<ip> <mask>)"; ! no_call_params = "<1-99> [<line>] (permit|deny) (any|host <ip>|<ip> <mask>)"; } public String call(Vector<String> params){ String out = ""; ! try{ ! int iacl = Integer.parseInt(params.get(0)); ! if(iacl>=1 && iacl<=2699){ ! int iline = -1; ! int index = 1; ! try{ ! iline = Integer.parseInt(params.get(1)); ! index = 2; ! } catch(NumberFormatException e){ } ! String permit_deny = params.get(index); ! ! short action=-1; ! if(permit_deny.equalsIgnoreCase("permit")){ ! action = AccessListEngine.access_list.PERMIT; ! } ! else if(permit_deny.equalsIgnoreCase("deny")){ ! action = AccessListEngine.access_list.DENY; ! } ! ! if(action>-1){ ! String ip; ! String mask; ! String hosts = params.get(index+1); ! if(hosts.equalsIgnoreCase("any")){ ! ip = "0.0.0.0"; ! mask = "0.0.0.0"; ! } ! else if(hosts.equals("host")){ ! ip = params.get(index+2); ! mask = "255.255.255.255"; ! } ! else{ ! ip = hosts; ! mask = params.get(index+2); ! } ! //device.getConfig().add("ip access-list "+iacl+" "+permit_deny+" "+hosts); ! device.getACL().addACL(iacl, iline, action, ip, mask); ! } ! else{ ! out += "error: invalid parameter: '"+permit_deny+"'\n"; ! } ! } ! else{ ! out += "error: invalid ACL name '"+params.get(0)+"', ACL name must be number from 1 to 2699\n"; ! } } ! catch(ArrayIndexOutOfBoundsException e){ out += "error: invalid parameters\n"; } + catch(NumberFormatException e){ + out += "error: invalid ACL name '"+params.get(0)+"', ACL name must be number from 1 to 2699\n"; + } return out; } *************** *** 972,977 **** public ip_access_list_extended_CommandClass(){ modes = new Modes(CommandInterface.CONF_MODE, CommandInterface.APPLICATION_LAYER, CommandInterface.NO_CALL); ! call_params = "<1-2699 | WORD> [<line>] (permit|deny) (ip|tcp|udp|icmp) (any|host <ip>|<ip> <mask>) (any|host <ip>|<ip> <mask>) [(eq|lt|gt|neq) <port>] [established]"; ! no_call_params = "<1-2699 | WORD> [<line>] (permit|deny) (ip|tcp|udp|icmp) (any|host <ip>|<ip> <mask>) (any|host <ip>|<ip> <mask>) [(eq|lt|gt|neq) <port>] [established]"; } public String call(Vector<String> params){ --- 1006,1011 ---- public ip_access_list_extended_CommandClass(){ modes = new Modes(CommandInterface.CONF_MODE, CommandInterface.APPLICATION_LAYER, CommandInterface.NO_CALL); ! call_params = "<100-2699> [<line>] (permit|deny) (ip|tcp|udp|icmp) (any|host <ip>|<ip> <mask>) (any|host <ip>|<ip> <mask>) [(eq|lt|gt|neq) <port>] [established]"; ! no_call_params = "<100-2699> [<line>] (permit|deny) (ip|tcp|udp|icmp) (any|host <ip>|<ip> <mask>) (any|host <ip>|<ip> <mask>) [(eq|lt|gt|neq) <port>] [established]"; } public String call(Vector<String> params){ *************** *** 1524,1533 **** } public String call(Vector<String> params){ ! String out = "Command not supported yet.\n"; ! if(device instanceof ApplicationLayerDevice){ ! ! } ! else{ ! out += "This instruction not supported by device\n"; } return out; --- 1558,1646 ---- } public String call(Vector<String> params){ ! String out = ""; ! final int ERROR = 0; ! final int STANDART = 1; ! final int EXTENDED = 2; ! Hashtable acls = device.getACL().ACLs; ! ! Enumeration<Integer> aclkeys = acls.keys(); ! while(aclkeys.hasMoreElements()){ ! Hashtable acl = (Hashtable)acls.get((Integer)aclkeys.nextElement()); ! ! Enumeration rulekeys = acl.keys(); ! while(rulekeys.hasMoreElements()){ ! access_list rule = (access_list)acl.get((Integer)rulekeys.nextElement()); ! ! String type = "unsupported_acl_type"; ! int itype = 0; ! if(rule.number>=1 && rule.number<=99){ ! type = "standart"; ! itype = STANDART; ! } ! else if(rule.number>=100 && rule.number<=2699){ ! type = "extended"; ! itype = EXTENDED; ! } ! ! String permit_deny = "unsupported_action"; ! if(rule.action==rule.DENY){ ! permit_deny = "deny "; ! } ! else if(rule.action==rule.PERMIT){ ! permit_deny = "permit"; ! } ! ! String hosts1 = "unsupported_hosts_1"; ! if(rule.IP1.equals("0.0.0.0") && rule.SubnetMask1.equals("0.0.0.0")){ ! hosts1 = "any"; ! } ! else if(rule.SubnetMask1.equals("255.255.255.255")){ ! hosts1 = "host "+rule.IP1; ! } ! else{ ! hosts1 = rule.IP1+" "+rule.SubnetMask1; ! } ! ! String hosts2 = "unsupported_hosts_2"; ! if(itype==EXTENDED){ ! if(rule.IP2.equals("0.0.0.0") && rule.SubnetMask2.equals("0.0.0.0")){ ! hosts2 = "any"; ! } ! else if(rule.SubnetMask2.equals("255.255.255.255")){ ! hosts2 = "host "+rule.IP2; ! } ! else{ ! hosts2 = rule.IP2+" "+rule.SubnetMask2; ! } ! } ! ! String protocol = "unsupported_protocol"; ! if(rule.protocol==rule.IP){ ! protocol = "ip "; ! } ! else if(rule.protocol==rule.TCP){ ! protocol = "tcp "; ! } ! else if(rule.protocol==rule.UDP){ ! protocol = "udp "; ! } ! else if(rule.protocol==rule.ICMP){ ! protocol = "icmp"; ! } ! ! String port = "unsupported_port"; ! if(rule.Port1 == 0){ ! port = ""; ! } ! else{ ! port = "eq "+rule.Port1; ! } ! ! switch(itype){ ! case STANDART: out += "ip access-list "+type+" "+rule.number+" "+rule.line+" "+permit_deny+" "+hosts1+"\n"; break; ! case EXTENDED: out += "ip access-list "+type+" "+rule.number+" "+rule.line+" "+permit_deny+" "+protocol+" "+hosts1+" "+hosts2+" "+port+"\n"; break; ! default: out += "ip access-list "+type+" "+rule.number+" "+rule.line+" "+permit_deny+" "+protocol+" "+hosts1+" "+hosts2+" "+rule.Port2+"\n"; ! } ! } } return out; *************** *** 2280,2285 **** } public String call(Vector<String> params){ ! device.getConfig().clear(DeviceConfig.STARTUP_CONFIG); ! boolean res = device.getConfig().copy(DeviceConfig.RUNNING_CONFIG, DeviceConfig.STARTUP_CONFIG); String out = "Startup-config is not empty!\n"; if(res){ --- 2393,2398 ---- } public String call(Vector<String> params){ ! device.getConfig().clearStartupConfig(); ! boolean res = device.getConfig().writeMemory(); String out = "Startup-config is not empty!\n"; if(res){ Index: DeviceConfig.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/DeviceConfig.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** DeviceConfig.java 20 Oct 2007 22:54:40 -0000 1.7 --- DeviceConfig.java 23 Oct 2007 20:14:48 -0000 1.8 *************** *** 10,13 **** --- 10,14 ---- package core; + import core.AccessListEngine.access_list; import java.util.Iterator; import java.util.LinkedList; *************** *** 16,19 **** --- 17,24 ---- import java.util.regex.PatternSyntaxException; import core.CommandInterface.Modes; + import core.protocolsuite.tcp_ip.IPV4Address; + import java.util.Enumeration; + import java.util.Hashtable; + import core.AccessListEngine.access_list; /** *************** *** 25,28 **** --- 30,34 ---- public final static int RUNNING_CONFIG = 1; public final static int STARTUP_CONFIG = 2; + private final static String EXEC = "#"; public int working_config = RUNNING_CONFIG; *************** *** 36,39 **** --- 42,46 ---- /** Creates a new instance of DeviceConfig */ public DeviceConfig(NetworkLayerDevice dev) { + initRunningConfig(); cmdproc = new CommandProcessor(dev); device = dev; *************** *** 51,55 **** public void load(){ Iterator<String> it = startup_config.iterator(); ! clear(RUNNING_CONFIG); while(it.hasNext()){ --- 58,62 ---- public void load(){ Iterator<String> it = startup_config.iterator(); ! initRunningConfig(); while(it.hasNext()){ *************** *** 63,67 **** LinkedList config = null; switch(config_name){ ! case RUNNING_CONFIG: config = running_config; break; case STARTUP_CONFIG: config = startup_config; break; default: --- 70,74 ---- LinkedList config = null; switch(config_name){ ! case RUNNING_CONFIG: config = prepareConfig(running_config); break; case STARTUP_CONFIG: config = startup_config; break; default: *************** *** 70,90 **** } ! public void clear(int config_name){ ! LinkedList config = getConfig(config_name); ! while(!config.isEmpty()) ! config.removeLast(); } ! public boolean copy(int config_out, int config_in){ boolean result = false; ! if(config_out != config_in){ ! LinkedList cnfin = getConfig(config_in); ! if(cnfin.isEmpty()){ ! Iterator<String> it = getConfig(config_out).iterator(); ! while(it.hasNext()){ ! cnfin.add(it.next()); ! } ! result = true; } } return result; --- 77,99 ---- } ! public void clearStartupConfig(){ ! while(!startup_config.isEmpty()) ! startup_config.removeLast(); } ! private void initRunningConfig(){ ! while(!running_config.isEmpty()) ! running_config.removeLast(); ! running_config.add("#ACLS"); ! } ! ! public boolean writeMemory(){ boolean result = false; ! if(startup_config.isEmpty()){ ! Iterator<String> it = prepareConfig(running_config).iterator(); ! while(it.hasNext()){ ! startup_config.add(it.next()); } + result = true; } return result; *************** *** 212,214 **** --- 221,350 ---- } + private LinkedList prepareConfig(LinkedList in){ + LinkedList out = new LinkedList(); + Iterator it = in.iterator(); + while(it.hasNext()){ + String nc = (String)it.next(); + if(nc.startsWith(EXEC)){ + nc = nc.substring(EXEC.length()); + if(nc.equalsIgnoreCase("ACLS")){ + fillConfigACLS(out); + } + // else if(nc.equalsIgnoreCase("")){ + // + // } + // else if(nc.equalsIgnoreCase("")){ + // + // } + // else if(nc.equalsIgnoreCase("")){ + // + // } + // else if(nc.equalsIgnoreCase("")){ + // + // } + // else if(nc.equalsIgnoreCase("")){ + // + // } + // else if(nc.equalsIgnoreCase("")){ + // + // } + else{ + out.add("#ERROR: unsupported keyword #"+nc); + } + } + else{ + out.add(nc); + } + } + return out; + } + + private void fillConfigACLS(LinkedList conf){ + final int ERROR = 0; + final int STANDART = 1; + final int EXTENDED = 2; + Hashtable acls = device.getACL().ACLs; + + Enumeration<Integer> aclkeys = acls.keys(); + while(aclkeys.hasMoreElements()){ + Hashtable acl = (Hashtable)acls.get((Integer)aclkeys.nextElement()); + + Enumeration rulekeys = acl.keys(); + while(rulekeys.hasMoreElements()){ + access_list rule = (access_list)acl.get((Integer)rulekeys.nextElement()); + + String type = "unsupported_acl_type"; + int itype = 0; + if(rule.number>=1 && rule.number<=99){ + type = "standart"; + itype = STANDART; + } + else if(rule.number>=100 && rule.number<=2699){ + type = "extended"; + itype = EXTENDED; + } + + String permit_deny = "unsupported_action"; + if(rule.action==rule.DENY){ + permit_deny = "deny "; + } + else if(rule.action==rule.PERMIT){ + permit_deny = "permit"; + } + + String hosts1 = "unsupported_hosts_1"; + if(rule.IP1.equals("0.0.0.0") && rule.SubnetMask1.equals("0.0.0.0")){ + hosts1 = "any"; + } + else if(rule.SubnetMask1.equals("255.255.255.255")){ + hosts1 = "host "+rule.IP1; + } + else{ + hosts1 = rule.IP1+" "+rule.SubnetMask1; + } + + String hosts2 = "unsupported_hosts_2"; + if(itype==EXTENDED){ + if(rule.IP2.equals("0.0.0.0") && rule.SubnetMask2.equals("0.0.0.0")){ + hosts2 = "any"; + } + else if(rule.SubnetMask2.equals("255.255.255.255")){ + hosts2 = "host "+rule.IP2; + } + else{ + hosts2 = rule.IP2+" "+rule.SubnetMask2; + } + } + + String protocol = "unsupported_protocol"; + if(rule.protocol==rule.IP){ + protocol = "ip "; + } + else if(rule.protocol==rule.TCP){ + protocol = "tcp "; + } + else if(rule.protocol==rule.UDP){ + protocol = "udp "; + } + else if(rule.protocol==rule.ICMP){ + protocol = "icmp"; + } + + String port = "unsupported_port"; + if(rule.Port1 == 0){ + port = ""; + } + else{ + port = "eq "+rule.Port1; + } + + switch(itype){ + case STANDART: conf.add("ip access-list "+type+" "+rule.number+" "+permit_deny+" "+hosts1); break; + case EXTENDED: conf.add("ip access-list "+type+" "+rule.number+" "+permit_deny+" "+protocol+" "+hosts1+" "+hosts2+" "+port); break; + default: conf.add("ip access-list "+type+" "+rule.number+" "+permit_deny+" "+protocol+" "+hosts1+" "+hosts2+" "+rule.Port2); + } + } + } + } + } Index: CommandsTree.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/CommandsTree.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CommandsTree.java 20 Oct 2007 22:54:40 -0000 1.4 --- CommandsTree.java 23 Oct 2007 20:14:48 -0000 1.5 *************** *** 21,24 **** --- 21,25 ---- public static final String ANYWORD = "*"; + public static final String COMMENT = "!"; CommandNode root = new CommandNode("."); *************** *** 269,315 **** public String call(String command, Modes mode){ String result = null; ! String cmds[] = command.split(" "); ! ! CommandInterface func = null; ! Modes modes = new Modes(0,0,0); ! String params = ""; ! Vector<String> vprms = new Vector<String>(0); ! ! CommandNode node = root; ! int i; ! for(i=0; i<cmds.length && node!=null && node.modes.isContain(mode); i++){ ! node = node.getNode(cmds[i], mode); ! if(node != null){ ! if(node.getName().equals(ANYWORD)){ ! vprms.add(cmds[i]); ! } ! func = node.getFunction(); ! modes = node.modes; ! if(func!=null){ ! if(cmds[0].equalsIgnoreCase("no")){ ! params = node.getFunction().no_call_params; } else{ ! params = node.getFunction().call_params; } } else{ ! params = ""; } } } - if(func!=null && modes.isContain(mode)){ - //String sprms[] = params.split(" "); - if(node==null) i--; - for(; i<cmds.length; i++){ - vprms.add(cmds[i]); - } - if(cmds[0].equalsIgnoreCase("no")){ - result = func.no_call(vprms); - } - else{ - result = func.call(vprms); - } - } return result; } --- 270,323 ---- public String call(String command, Modes mode){ String result = null; ! if(command.startsWith(COMMENT)){ ! //result = command; ! //result = command.substring(COMMENT.length()); ! result = ""; ! } ! else{ ! String cmds[] = command.split(" "); ! ! CommandInterface func = null; ! Modes modes = new Modes(0,0,0); ! String params = ""; ! Vector<String> vprms = new Vector<String>(0); ! ! CommandNode node = root; ! int i; ! for(i=0; i<cmds.length && node!=null && node.modes.isContain(mode); i++){ ! node = node.getNode(cmds[i], mode); ! if(node != null){ ! if(node.getName().equals(ANYWORD)){ ! vprms.add(cmds[i]); ! } ! func = node.getFunction(); ! modes = node.modes; ! if(func!=null){ ! if(cmds[0].equalsIgnoreCase("no")){ ! params = node.getFunction().no_call_params; ! } ! else{ ! params = node.getFunction().call_params; ! } } else{ ! params = ""; } } + } + if(func!=null && modes.isContain(mode)){ + //String sprms[] = params.split(" "); + if(node==null) i--; + for(; i<cmds.length; i++){ + vprms.add(cmds[i]); + } + if(cmds[0].equalsIgnoreCase("no")){ + result = func.no_call(vprms); + } else{ ! result = func.call(vprms); } } } return result; } Index: AccessListEngine.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/AccessListEngine.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** AccessListEngine.java 19 Oct 2007 15:57:01 -0000 1.1 --- AccessListEngine.java 23 Oct 2007 20:14:48 -0000 1.2 *************** *** 37,41 **** Node parentNode; ! Hashtable ACLs; public AccessListEngine(Node inParentNode){ --- 37,41 ---- Node parentNode; ! Hashtable ACLs = new Hashtable(); public AccessListEngine(Node inParentNode){ *************** *** 43,47 **** } ! public boolean addACL(int number, int line, short action, String IP1, String SubnetMask1){ Hashtable ACLl = (Hashtable)ACLs.get(Integer.valueOf(number)); boolean new_acl = false; --- 43,47 ---- } ! public int addACL(int number, int line, short action, String IP1, String SubnetMask1){ Hashtable ACLl = (Hashtable)ACLs.get(Integer.valueOf(number)); boolean new_acl = false; *************** *** 52,56 **** } ! if(ACLl.get(Integer.valueOf(line)) == null) return false; if(line < 0){ --- 52,56 ---- } ! if(ACLl.get(Integer.valueOf(line)) != null) return -1; if(line < 0){ *************** *** 78,82 **** if(new_acl) ACLs.put(Integer.valueOf(number), ACLl); ! return true; } --- 78,82 ---- if(new_acl) ACLs.put(Integer.valueOf(number), ACLl); ! return line; } *************** *** 119,122 **** --- 119,128 ---- return found; } + + public boolean isACLExists(int number, int line){ + Hashtable acllist = (Hashtable)ACLs.get(new Integer(number)); + if(acllist==null) return false; + return (acllist.containsKey(new Integer(line))); + } public boolean passStandartACL(int number, String IP){ |