Thread: [Javanetsim-cvs] javaNetSim/guiUI RunCMD.java,NONE,1.1 MainScreen.java,1.37,1.38 NetworkLayerDevice.
Status: Beta
Brought to you by:
darkkey
From: QweR <qw...@us...> - 2006-02-27 16:54:52
|
Update of /cvsroot/javanetsim/javaNetSim/guiUI In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2508/guiUI Modified Files: MainScreen.java NetworkLayerDevice.java Added Files: RunCMD.java Log Message: Index: NetworkLayerDevice.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/guiUI/NetworkLayerDevice.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** NetworkLayerDevice.java 23 Feb 2006 11:56:00 -0000 1.6 --- NetworkLayerDevice.java 27 Feb 2006 16:54:49 -0000 1.7 *************** *** 1,166 **** ! /* ! Java Firewall Simulator (jFirewallSim) ! ! Copyright (c) 2004, jFirewallSim development team All rights reserved. ! ! Redistribution and use in source and binary forms, with or without modification, are ! permitted provided that the following conditions are met: ! ! - Redistributions of source code must retain the above copyright notice, this list ! of conditions and the following disclaimer. ! - Redistributions in binary form must reproduce the above copyright notice, this list ! of conditions and the following disclaimer in the documentation and/or other ! materials provided with the distribution. ! - Neither the name of the Canberra Institute of Technology nor the names of its ! contributors may be used to endorse or promote products derived from this software ! without specific prior written permission. ! ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY ! EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ! THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ! EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF ! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ! HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR ! TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, ! EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ! */ ! package guiUI; ! ! ! import java.awt.Dimension; ! import java.awt.event.ActionEvent; ! import java.awt.event.ActionListener; ! ! import javax.swing.JMenuItem; ! import javax.swing.JMenu; ! ! /** ! * <P>The NetworkLayerDevice class is an abstract class used for further classification of Network </P> ! * <P>layer devices such as PC's and Routers. ! * ! * @author VC2 Team. ! * @since 15th November 2004 ! * @version v0.20 ! */ ! ! public abstract class NetworkLayerDevice extends GuiNode { ! ! private JMenuItem mnuSetTCPIPProperties = new JMenuItem("Set TCP/IP Properties ..."); ! private JMenuItem mnuSendPing = new JMenuItem("Send Ping ..."); ! private JMenuItem mnuProperties = new JMenuItem("Properties ..."); ! private JMenu mnuARPMenu = new JMenu("ARP"); ! private JMenu mnuCountersMenu = new JMenu("Counters"); ! ! private JMenuItem mnuShowCounters = new JMenuItem("Show Packet Counters"); ! private JMenuItem mnuResetCounters = new JMenuItem("Reset Packet Counters"); ! ! private JMenuItem mnuArpStaticAdd = new JMenuItem("Add static entry to ARP table..."); ! private JMenuItem mnuArpRemove = new JMenuItem("Remove entry from ARP table..."); ! ! private JMenuItem mnuRunCmd = new JMenuItem("Run low-level command..."); ! private JMenuItem mnuPR = new JMenuItem("Print route table"); ! private JMenuItem mnuArp = new JMenuItem("Print ARP Table"); ! private JMenuItem mnuLink = new JMenuItem("Links properties"); ! private JMenuItem mnuBreakLink = new JMenuItem("Break link"); ! ! ! /** ! * @param inName The name of the Network Layer Device ! * @param inMainscreen The JFrame that the router will be created on ! * @param imageLocation Location of the Image used on the GUI ! */ ! ! public NetworkLayerDevice(String inName, MainScreen inMainScreen, String imageLocation) { ! super(inName, inMainScreen, imageLocation); ! ! mnuSetTCPIPProperties.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.setTCPIPProperties(lblNodeName.getText()); ! ! ! } ! }); ! mnuSendPing.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.SendPing(lblNodeName.getText()); ! } ! }); ! mnuArp.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.printARP(lblNodeName.getText()); ! } ! }); ! ! mnuArpStaticAdd.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.addStaticARP(lblNodeName.getText()); ! } ! }); ! mnuArpRemove.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.removeARP(lblNodeName.getText()); ! } ! }); ! mnuRunCmd.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.RunCmd(lblNodeName.getText()); ! } ! }); ! mnuProperties.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.showPropertiesDialog(lblNodeName.getText()); ! } ! }); ! mnuBreakLink.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.showBreakLinkDialog(lblNodeName.getText()); ! } ! }); ! ! mnuPR.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.PrintRouteTable(lblNodeName.getText()); ! } ! }); ! mnuLink.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.showLinkDialog(lblNodeName.getText()); ! } ! }); ! ! ! mnuShowCounters.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.showCounters(lblNodeName.getText()); ! } ! }); ! mnuResetCounters.addActionListener(new ActionListener(){ ! public void actionPerformed(ActionEvent e){ ! controller.resetCounters(lblNodeName.getText()); ! } ! }); ! ! ! GuiNodePopMenu.add(mnuSetTCPIPProperties); ! GuiNodePopMenu.add(mnuSendPing); ! GuiNodePopMenu.add(mnuARPMenu); ! mnuARPMenu.add(mnuArpStaticAdd); ! mnuARPMenu.add(mnuArpRemove); ! mnuARPMenu.add(mnuArp); ! GuiNodePopMenu.add(mnuCountersMenu); ! mnuCountersMenu.add(mnuShowCounters); ! mnuCountersMenu.add(mnuResetCounters); ! GuiNodePopMenu.add(mnuRunCmd); ! GuiNodePopMenu.add(mnuPR); ! GuiNodePopMenu.add(mnuBreakLink); ! GuiNodePopMenu.add(mnuLink); ! GuiNodePopMenu.add(mnuProperties); ! ! ! } ! ! ! ! ! } --- 1,332 ---- ! /* ! ! Java Firewall Simulator (jFirewallSim) ! ! ! ! Copyright (c) 2004, jFirewallSim development team All rights reserved. ! ! ! ! Redistribution and use in source and binary forms, with or without modification, are ! ! permitted provided that the following conditions are met: ! ! ! ! - Redistributions of source code must retain the above copyright notice, this list ! ! of conditions and the following disclaimer. ! ! - Redistributions in binary form must reproduce the above copyright notice, this list ! ! of conditions and the following disclaimer in the documentation and/or other ! ! materials provided with the distribution. ! ! - Neither the name of the Canberra Institute of Technology nor the names of its ! ! contributors may be used to endorse or promote products derived from this software ! ! without specific prior written permission. ! ! ! ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY ! ! EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! ! OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ! ! THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ! ! EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF ! ! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ! ! HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR ! ! TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, ! ! EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ! ! */ ! ! package guiUI; ! ! ! ! ! ! import java.awt.Dimension; ! ! import java.awt.event.ActionEvent; ! ! import java.awt.event.ActionListener; ! ! ! ! import javax.swing.JMenuItem; ! ! import javax.swing.JMenu; ! ! ! ! /** ! ! * <P>The NetworkLayerDevice class is an abstract class used for further classification of Network </P> ! ! * <P>layer devices such as PC's and Routers. ! ! * ! ! * @author VC2 Team. ! ! * @since 15th November 2004 ! ! * @version v0.20 ! ! */ ! ! ! ! public abstract class NetworkLayerDevice extends GuiNode { ! ! ! ! private JMenuItem mnuSetTCPIPProperties = new JMenuItem("Set TCP/IP Properties ..."); ! ! private JMenuItem mnuSendPing = new JMenuItem("Send Ping ..."); ! ! private JMenuItem mnuProperties = new JMenuItem("Properties ..."); ! ! private JMenu mnuARPMenu = new JMenu("ARP"); ! ! private JMenu mnuCountersMenu = new JMenu("Counters"); ! ! ! ! private JMenuItem mnuShowCounters = new JMenuItem("Show Packet Counters"); ! ! private JMenuItem mnuResetCounters = new JMenuItem("Reset Packet Counters"); ! ! ! ! private JMenuItem mnuArpStaticAdd = new JMenuItem("Add static entry to ARP table..."); ! ! private JMenuItem mnuArpRemove = new JMenuItem("Remove entry from ARP table..."); ! ! ! ! private JMenuItem mnuRunCmd = new JMenuItem("Run low-level command..."); ! ! private JMenuItem mnuPR = new JMenuItem("Print route table"); ! ! private JMenuItem mnuArp = new JMenuItem("Print ARP Table"); ! ! private JMenuItem mnuLink = new JMenuItem("Links properties"); ! ! private JMenuItem mnuBreakLink = new JMenuItem("Break link"); ! ! ! ! ! ! /** ! ! * @param inName The name of the Network Layer Device ! ! * @param inMainscreen The JFrame that the router will be created on ! ! * @param imageLocation Location of the Image used on the GUI ! ! */ ! ! ! ! public NetworkLayerDevice(String inName, MainScreen inMainScreen, String imageLocation) { ! ! super(inName, inMainScreen, imageLocation); ! ! ! ! mnuSetTCPIPProperties.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.setTCPIPProperties(lblNodeName.getText()); ! ! ! ! ! ! } ! ! }); ! ! mnuSendPing.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.SendPing(lblNodeName.getText()); ! ! } ! ! }); ! ! mnuArp.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.printARP(lblNodeName.getText()); ! ! } ! ! }); ! ! ! ! mnuArpStaticAdd.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.addStaticARP(lblNodeName.getText()); ! ! } ! ! }); ! ! mnuArpRemove.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.removeARP(lblNodeName.getText()); ! ! } ! ! }); ! ! mnuRunCmd.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.RunCmd(lblNodeName.getText()); ! ! } ! ! }); ! ! mnuProperties.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.showPropertiesDialog(lblNodeName.getText()); ! ! } ! ! }); ! ! mnuBreakLink.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.showBreakLinkDialog(lblNodeName.getText()); ! ! } ! ! }); ! ! ! ! mnuPR.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.PrintRouteTable(lblNodeName.getText()); ! ! } ! ! }); ! ! mnuLink.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.showLinkDialog(lblNodeName.getText()); ! ! } ! ! }); ! ! ! ! ! ! mnuShowCounters.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.showCounters(lblNodeName.getText()); ! ! } ! ! }); ! ! mnuResetCounters.addActionListener(new ActionListener(){ ! ! public void actionPerformed(ActionEvent e){ ! ! controller.resetCounters(lblNodeName.getText()); ! ! } ! ! }); ! ! ! ! ! ! GuiNodePopMenu.add(mnuSetTCPIPProperties); ! ! GuiNodePopMenu.add(mnuSendPing); ! ! GuiNodePopMenu.add(mnuARPMenu); ! ! mnuARPMenu.add(mnuArpStaticAdd); ! ! mnuARPMenu.add(mnuArpRemove); ! ! mnuARPMenu.add(mnuArp); ! ! GuiNodePopMenu.add(mnuCountersMenu); ! ! mnuCountersMenu.add(mnuShowCounters); ! ! mnuCountersMenu.add(mnuResetCounters); ! ! GuiNodePopMenu.add(mnuRunCmd); ! ! GuiNodePopMenu.add(mnuPR); ! ! GuiNodePopMenu.add(mnuBreakLink); ! ! GuiNodePopMenu.add(mnuLink); ! ! GuiNodePopMenu.add(mnuProperties); ! ! ! ! ! ! } ! ! ! ! ! ! ! ! ! ! } ! Index: MainScreen.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/guiUI/MainScreen.java,v retrieving revision 1.37 retrieving revision 1.38 diff -C2 -d -r1.37 -r1.38 *** MainScreen.java 27 Feb 2006 00:49:10 -0000 1.37 --- MainScreen.java 27 Feb 2006 16:54:49 -0000 1.38 *************** *** 124,127 **** --- 124,128 ---- import core.protocolsuite.tcp_ip.Telnet_server; import core.protocolsuite.tcp_ip.Telnet_client; + import guiUI.RunCMD; *************** *** 3273,3355 **** public void RunCmd(String inNodeName){ ! ! String cmd = JOptionPane.showInputDialog(this, "Command (e.g. route print):", "Run network configure command.", JOptionPane.QUESTION_MESSAGE); ! ! if(cmd!=null){ ! ! String tokens[]=cmd.split(" "); ! ! ! ! if(tokens[0].contains("route")){ ! ! try{ ! ! if(tokens[1].contains("print")){ ! ! String routes[] = Sim.getRouteTableEntries(inNodeName); ! ! this.addToConsole("IP routing table:\n" + "Destination" + "\t" + "Gateway" + "\t" + "Genmask" + "\t" + "Type" + "\t" + "Iface\n"); ! ! for(int i=0; i<routes.length - 1; i++){ ! ! Route_entry r = Sim.getRouteEntry(inNodeName, routes[i]); ! ! this.addToConsole(routes[i] + "\t" + r.gateway + "\t" + r.genMask + "\t" + r.Type + "\t" + r.iFace + "\n"); ! ! } ! ! }else if(tokens[1].contains("add")){ ! ! String gw = ""; ! ! if(tokens.length > 5) gw = tokens[5]; ! ! Route_entry r = new Route_entry(tokens[2], gw, tokens[4], tokens[3], 0); ! ! Sim.addRoute(inNodeName, r); ! ! this.addToConsole("Route added.\n"); ! ! }else if(tokens[1].contains("del")){ ! ! Sim.removeRoute(inNodeName, tokens[2]); ! ! this.addToConsole("Route to " + tokens[2] + "removed.\n"); ! ! }else{ ! ! this.addToConsole("Unknown route command. Usage:\n"); ! ! this.addToConsole("route add (host_ip|network_ip) target_interface netmask (gateway|*) \n"); ! ! this.addToConsole("route del (host_ip|network_ip) \n"); ! ! this.addToConsole("route print \n"); ! ! } ! ! }catch(ArrayIndexOutOfBoundsException e){ ! ! this.addToConsole("Unknown route command. Usage:\n"); ! ! this.addToConsole("route add (host_ip|network_ip) target_interface netmask (gateway|*) \n"); ! ! this.addToConsole("route del (host_ip|network_ip) \n"); ! ! this.addToConsole("route print \n"); ! ! } ! ! ! ! }else{ ! ! this.addToConsole("Unknown or invalid command.\n"); ! ! } ! ! } ! } --- 3274,3322 ---- public void RunCmd(String inNodeName){ ! try{ ! RunCMD r = new RunCMD(this, (core.NetworkLayerDevice)Sim.getNode(inNodeName)); ! r.pack(); ! r.setVisible(true); ! } ! catch(InvalidNodeNameException e) { ! System.out.print(e.toString() + ": You change non-existent device!"); ! } ! // String cmd = JOptionPane.showInputDialog(this, "Command (e.g. route print):", "Run network configure command.", JOptionPane.QUESTION_MESSAGE); ! // if(cmd!=null){ ! // String tokens[]=cmd.split(" "); ! // if(tokens[0].contains("route")){ ! // try{ ! // if(tokens[1].contains("print")){ ! // String routes[] = Sim.getRouteTableEntries(inNodeName); ! // this.addToConsole("IP routing table:\n" + "Destination" + "\t" + "Gateway" + "\t" + "Genmask" + "\t" + "Type" + "\t" + "Iface\n"); ! // for(int i=0; i<routes.length - 1; i++){ ! // Route_entry r = Sim.getRouteEntry(inNodeName, routes[i]); ! // this.addToConsole(routes[i] + "\t" + r.gateway + "\t" + r.genMask + "\t" + r.Type + "\t" + r.iFace + "\n"); ! // } ! // }else if(tokens[1].contains("add")){ ! // String gw = ""; ! // if(tokens.length > 5) gw = tokens[5]; ! // Route_entry r = new Route_entry(tokens[2], gw, tokens[4], tokens[3], 0); ! // Sim.addRoute(inNodeName, r); ! // this.addToConsole("Route added.\n"); ! // }else if(tokens[1].contains("del")){ ! // Sim.removeRoute(inNodeName, tokens[2]); ! // this.addToConsole("Route to " + tokens[2] + "removed.\n"); ! // }else{ ! // this.addToConsole("Unknown route command. Usage:\n"); ! // this.addToConsole("route add (host_ip|network_ip) target_interface netmask (gateway|*) \n"); ! // this.addToConsole("route del (host_ip|network_ip) \n"); ! // this.addToConsole("route print \n"); ! // } ! // }catch(ArrayIndexOutOfBoundsException e){ ! // this.addToConsole("Unknown route command. Usage:\n"); ! // this.addToConsole("route add (host_ip|network_ip) target_interface netmask (gateway|*) \n"); ! // this.addToConsole("route del (host_ip|network_ip) \n"); ! // this.addToConsole("route print \n"); ! // } ! // }else{ ! // this.addToConsole("Unknown or invalid command.\n"); ! // } ! // } } --- NEW FILE: RunCMD.java --- /* * TelnetEmulator.java * * Created on 22 Feb 2006, 16:38 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package guiUI; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.GridBagLayout; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JScrollPane; import javax.swing.JTextArea; import java.awt.Insets; import java.awt.GridBagConstraints; import java.awt.BorderLayout; import javax.swing.JButton; import java.awt.Dimension; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JComboBox; import core.Simulation; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.Color; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.util.Arrays; import java.awt.Component; import javax.swing.SwingConstants; import core.protocolsuite.tcp_ip.Telnet_client; import core.CommunicationException; import core.LowLinkException; import core.InvalidNetworkLayerDeviceException; import core.TransportLayerException; import java.awt.Font; import java.awt.event.KeyListener; import java.util.regex.*; import core.protocolsuite.tcp_ip.*; import java.util.Vector; /** * * @author key */ public class RunCMD extends JFrame { private JPanel panel; private JScrollPane scrollpane; private JTextArea terminal; private JTextField cmdline; private String text=""; private KeyListener kl; private core.NetworkLayerDevice device; MainScreen parent; private Vector<String> history = new Vector<String>(0); private int pos_history=0; private final static int max_history = 128; /** Creates a new instance of TelnetEmulator */ public RunCMD(MainScreen parent, core.NetworkLayerDevice dev) { super("Terminal"); device = dev; this.parent = parent; panel = new JPanel(); scrollpane = new JScrollPane(); terminal = new JTextArea(); cmdline = new JTextField(); this.setContentPane(panel); panel.setLayout(new java.awt.BorderLayout()); panel.add(scrollpane); panel.add(cmdline, BorderLayout.SOUTH); panel.setPreferredSize(new java.awt.Dimension(600,500)); scrollpane.setViewportView(terminal); terminal.setEnabled(false); terminal.setEditable(true); terminal.setBackground(Color.BLACK); terminal.setForeground(Color.WHITE); terminal.setFont(new Font("Courier New", Font.PLAIN, 12)); cmdline.addKeyListener( kl = new KeyAdapter(){ // terminal.addKeyListener( kl = new KeyAdapter(){ public void keyPressed(KeyEvent e) { char c = e.getKeyChar(); switch(c) { case 0x0A: { if(cmdline.getText().compareTo("")!=0) { if(history.size()>=max_history) history.remove(0); history.add(cmdline.getText()); pos_history = history.size(); } text += cmdline.getText() + "\n"; terminal.setText(text); text += runcmd(removeSpaces(cmdline.getText())); terminal.setText(text); cmdline.setText(""); printInfo(); break; } case 0x04: { exitWindow(); break; } case 0x1B: { cmdline.setText(""); break; } default: { if(c>=65535) { int i = e.getKeyCode(); switch(i) { case 0x26: { if(history.size()>0) { if(pos_history == history.size() && cmdline.getText().compareTo("")!=0) { if(history.size()>=max_history) history.remove(0); history.add(cmdline.getText()); pos_history = history.size()-1; } if(pos_history>0) { cmdline.setText(history.get(--pos_history)); } else if(pos_history>0) { cmdline.setText(history.get(pos_history-1)); } } break; } case 0x28: { if(history.size()>0) { if(pos_history+1<history.size()) { cmdline.setText(history.get(++pos_history)); } } break; } default: //terminal.setText(text+"\n"+Integer.toHexString(i)); } } else { pos_history = history.size(); } break; } } } }); this.addWindowListener( new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent winEvt) { printInfo(); } }); } public void exitWindow() { printInfo(); this.dispose(); } public void printInfo(){ parent.printLayerInfo(false); //!!!!!: add more headers here } private String runcmd(String cmd) { String out=""; if(cmd.compareTo("")==0) { //nothing } else if(cmd.compareTo("?")==0 || cmd.compareToIgnoreCase("help")==0) { out += " route \t show/edit route table\n"; out += " arp \t show/edit arp table\n"; out += " snmp \t on/off snmp agent\n"; out += " counters\t show network counters\n"; out += " quit \t close terminal session\n"; out += " ? \t show this screen\n"; } else if(cmd.compareToIgnoreCase("quit")==0) { this.dispose(); } else { String tokens[]=cmd.split(" "); Matcher m; if(tokens[0].compareTo("route")==0){ if((m=Pattern.compile(" +print$").matcher(cmd)).find()) { String routes[] = device.NodeProtocolStack.getRouteTableEntries(); out += "IP routing table:\n" + "Destination" + "\t" + "Gateway" + "\t" + "Genmask" + "\t" + "Type" + "\t" + "Iface\n"; for(int i=0; i<routes.length - 1; i++){ Route_entry r = device.NodeProtocolStack.getRouteTableEntry(routes[i]); out += routes[i] + "\t" + r.gateway + "\t" + r.genMask + "\t" + r.Type + "\t" + r.iFace + "\n"; } } else if((m=Pattern.compile(" +add +([^ ]+) +([^ ]+) +([^ ]+)( +([^ ]+|\\*))?$").matcher(cmd)).find()) { if(m.group(5)!=null) { device.NodeProtocolStack.addRoute(new Route_entry(m.group(1), m.group(5), m.group(3), m.group(2), 0)); } else { device.NodeProtocolStack.addRoute(new Route_entry(m.group(1), "", m.group(3), m.group(2), 0)); } out+="Route added.\n"; } else if((m=Pattern.compile(" +del +([^ ]+)$").matcher(cmd)).find()) { device.NodeProtocolStack.removeRoute(m.group(1)); out+="Route to " + tokens[2] + "removed.\n"; } else{ out+="Unknown route command. Usage:\n"; out+=" route add (<host ip>|<network ip>) <target interface> <netmask> [<gateway>|*]\n" + " add new route record\n"; out+=" route del (<host ip>|<network ip>) delete route record\n"; out+=" route print print route table\n"; } } else if(tokens[0].compareTo("snmp")==0){ if(device instanceof core.ApplicationLayerDevice) { if((m=Pattern.compile(" +(on|\\d+)( +([^ ]+))?$").matcher(cmd)).find()) { int l_port=161; String l_pass="public"; if(m.group(1).compareTo("on")!=0) l_port = Integer.parseInt(m.group(1)); if(m.group(3)!=null) { if(m.group(3).compareTo("")!=0) l_pass = m.group(3); } SNMP snmpa = (SNMP) ((core.ApplicationLayerDevice)device).getApp(161); try{ snmpa.Close(); snmpa.Disconnect(); out+="SNMP agent stoped\n"; snmpa.setPassword(l_pass); snmpa.setPort(l_port); try{ snmpa.Listen(); out+="Now SNMP agent listen on port " + String.valueOf(l_port) + "\n"; } catch(TransportLayerException e) { out+="Unable to open connection on SNMP agent\n"; } } catch(TransportLayerException e) { out+="Unable to close connection on SNMP agent\n"; } } else if((m=Pattern.compile(" +off$").matcher(cmd)).find()) { SNMP snmpa = (SNMP) ((core.ApplicationLayerDevice)device).getApp(161); try{ snmpa.Close(); out+="SNMP agent stoped\n"; } catch(TransportLayerException e) { out+="Unable to close connection on SNMP agent\n"; } } else { out+="Unknown snmp command. Usage:\n"; out+=" snmp (on|<port number>) [community name] Start SNMP agent\n"; out+=" snmp off Stop SNMP agent\n"; } } else { out += "Sorry, but this device not supported SNMP.\n"; } } else if(tokens[0].compareTo("counters")==0){ out += " Recieved IP Packets: " + Integer.valueOf(device.NodeProtocolStack.getinputIPCount()).toString() + "\n Sent IP Packets: " + Integer.valueOf(device.NodeProtocolStack.getoutputIPCount()).toString() + "\n ARP Packets: " + Integer.valueOf(device.NodeProtocolStack.getARPCount()).toString() + "\n Recieved TCP segments: " + Integer.valueOf(device.NodeProtocolStack.getTCPinputCount()).toString() + "\n Sent TCP segments: " + Integer.valueOf(device.NodeProtocolStack.getTCPoutputCount()).toString() + "\n Sent TCP ACK's: " + Integer.valueOf(device.NodeProtocolStack.getTCPACKCount()).toString() + "\n Sent TCP Dublicates: " + Integer.valueOf(device.NodeProtocolStack.getTCPSDCount()).toString() + "\n Recieved TCP Dublicates: " + Integer.valueOf(device.NodeProtocolStack.getTCPRDCount()).toString() + "\n Recieved UDP segments: " + Integer.valueOf(device.NodeProtocolStack.getUDPinputCount()).toString() + "\n Sent UDP segments: " + Integer.valueOf(device.NodeProtocolStack.getUDPoutputCount()).toString() + "\n"; } else if(tokens[0].compareTo("arp")==0){ if((m=Pattern.compile(" +-a$").matcher(cmd)).find()) { try{ String ArpTable[] = device.NodeProtocolStack.getARPTable(); for(int i=0;i<ArpTable.length;i++) { out += ArpTable[i] + "\n"; } } catch(Exception e) { //Should never get here. } } else if((m=Pattern.compile(" +-d +([^ ]+)$").matcher(cmd)).find()) { device.NodeProtocolStack.removeARP(m.group(1)); out += "Removed ARP entry for ip " + m.group(1) + "\n"; } else if((m=Pattern.compile(" +-s +([^ ]+) +([^ ]+)$").matcher(cmd)).find()) { device.NodeProtocolStack.addToARPStatic(m.group(1), m.group(2)); out += "Created new static ARP entry: " + m.group(1) + " is " + m.group(2) + "\n"; } else { out+="Unknown arp command. Usage:\n"; out+=" arp -a print ARP table\n"; out+=" arp -d <ip address> delete record from ARP table\n"; out+=" arp -s <ip address> <MAC address> add new ARP record\n"; } } else out = tokens[0] + ": command not found\n"; } out+=device.NodeProtocolStack.getParentNodeName() + " # "; return out; } private String removeSpaces(String s) { while(s.startsWith(" ")) s = s.substring(1); while(s.endsWith(" ")) s = s.substring(0, s.length()-1); return s; } } |