📄 osoperationslinuxipv4.java
字号:
/*JAdhoc ver 0.11 - Java AODV (RFC 3561) Protocol HandlerCopyright 2003-2004 ComNets, University of BremenThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/package jadhoc.os;import java.net.*;import java.io.*;import java.util.*;import jadhoc.conf.*;import jadhoc.net.*;import jadhoc.other.*;/*** This class defines all the functions related to manipulating* the routing environment of a Linux/IPv4 environment. Implements* the OSOperationsInterface to provide a consistent interface* to the route manager.** @author : Asanga Udugama* @date : 28-jul-2003* @email : adu@comnets.uni-bremen.de**/public class OSOperationsLinuxIPv4 implements OSOperationsInterface { public ConfigInfo cfgInfo; public CurrentInfo curInfo; private Process proc; private String cmd, str, s; private BufferedReader stdInput, stdError; /** * Constructs the object */ public OSOperationsLinuxIPv4(ConfigInfo cfg, CurrentInfo cur) { cfgInfo = cfg; curInfo = cur; } /** * Method to initialize the routing environment * in a Linux IPv4 environment to perform AODV protocol * handling * @param int level - The initialization level * 0 = full initialization level * 1 - 100 = other init levels * @return int - returns the success or failure */ public int initializeRouteEnvironment(int level) { try { switch(level) { case 0: fullInit(); break; default: // do nothing break; } } catch(Exception e) { // log curInfo.log.write(Logging.CRITICAL_LOGGING, "OS Ops IPv4 - Init failed"); return 1; } return 0; } private void fullInit() throws Exception { curInfo.log.write(Logging.INFO_LOGGING, "OS Ops IPv4 - Init started"); cmd = cfgInfo.pathToSystemCmdsVal + "/ip route replace default dev " + cfgInfo.loIfaceNameVal; curInfo.log.write(Logging.INFO_LOGGING, "OS Ops IPv4 - " + cmd); proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); str = new String(); while((s = stdError.readLine()) != null) { str += s; } if(str.trim().length() > 0) { throw new Exception(str); } cmd = cfgInfo.pathToSystemCmdsVal + "/ip route replace " + cfgInfo.ipAddressMulticastVal.getHostAddress() + " dev " + cfgInfo.ifaceNameVal; curInfo.log.write(Logging.INFO_LOGGING, "OS Ops IPv4 - " + cmd); proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); str = new String(); while((s = stdError.readLine()) != null) { str += s; } if(str.trim().length() > 0) { throw new Exception(str); } // log curInfo.log.write(Logging.INFO_LOGGING, "OS Ops IPv4 - Init completed"); } /** * Method to add a route entry in the routing environment * of a Linux/IPv4 environment. * @param RouteEntry rtEntry - the route entry from which to get * information * @return int - returns the success or failure */ public int addRoute(RouteEntry rtEntry) { try { // the "nexthop via" clause is required only if the destination is // not in link local network (i.e. if only hop count > 1) if(rtEntry.destIPAddr.equals(rtEntry.nextHopIPAddr)) { cmd = cfgInfo.pathToSystemCmdsVal + "/ip route replace " + rtEntry.destIPAddr.getHostAddress() + " dev " + rtEntry.ifaceName; } else { cmd = cfgInfo.pathToSystemCmdsVal + "/ip route replace " + rtEntry.destIPAddr.getHostAddress() + " nexthop via " + rtEntry.nextHopIPAddr.getHostAddress() + " dev " + rtEntry.ifaceName; } curInfo.log.write(Logging.INFO_LOGGING, "OS Ops IPv4 - Route add " + cmd); proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); // TODO : check for errors } catch(Exception e) { // log curInfo.log.write(Logging.CRITICAL_LOGGING, "OS Ops IPv4 - Route add failed"); return 1; } return 0; } /** * Method to remove a route entry in the routing environment of * a Linux/IPv4 environment * @param RouteEntry rtEntry - the route entry from which to get * information * @return int - returns the success or failure */ public int deleteRoute(RouteEntry rtEntry) { try { cmd = cfgInfo.pathToSystemCmdsVal + "/ip route del " + rtEntry.destIPAddr.getHostAddress(); curInfo.log.write(Logging.INFO_LOGGING, "OS Ops IPv4 - Route delete " + cmd); proc = Runtime.getRuntime().exec(cmd); // remove entry from ARP cache; only for routes in the link // network if(rtEntry.destIPAddr.equals(rtEntry.nextHopIPAddr)) { cmd = cfgInfo.pathToSystemCmdsVal + "/ip neigh del " + rtEntry.destIPAddr.getHostAddress() + " dev " + rtEntry.ifaceName; proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); } // TODO : check for errors } catch(Exception e) { curInfo.log.write(Logging.CRITICAL_LOGGING, "OS Ops IPv4 - Route delete failed"); return 1; } return 0; } /** * Method to put the route environment to the original state * before terminating the protocol handler in Linux/IPv4 * environment. * @return int - returns the success or failure */ public int finalizeRouteEnvironment() { try { curInfo.log.write(Logging.INFO_LOGGING, "OS Ops IPv4 - Termination started"); cmd = cfgInfo.pathToSystemCmdsVal + "/ip route replace default nexthop via " + cfgInfo.ipAddressGatewayVal.getHostAddress() + " dev " + cfgInfo.ifaceNameVal; curInfo.log.write(Logging.INFO_LOGGING, "OS Ops IPv4 - " + cmd); proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); str = new String(); while((s = stdError.readLine()) != null) { str += s; } if(str.trim().length() > 0) { throw new Exception(str); } cmd = cfgInfo.pathToSystemCmdsVal + "/ip route del " + cfgInfo.ipAddressMulticastVal.getHostAddress() + " dev " + cfgInfo.ifaceNameVal; curInfo.log.write(Logging.INFO_LOGGING, "OS Ops IPv4 - " + cmd); proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); str = new String(); while((s = stdError.readLine()) != null) { str += s; } if(str.trim().length() > 0) { throw new Exception(str); } curInfo.log.write(Logging.INFO_LOGGING, "OS Ops IPv4 - Termination completed"); } catch(Exception e) { curInfo.log.write(Logging.CRITICAL_LOGGING, "OS Ops IPv4 - Termination failed"); return 1; } return 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -