Search in sources :

Example 1 with RouteConfig

use of org.eclipse.kura.net.route.RouteConfig in project kura by eclipse.

the class RouteServiceImpl method removeStaticRoute.

@Override
public void removeStaticRoute(IPAddress destination, IPAddress gateway, IPAddress netmask, String iface) throws Exception {
    RouteConfig tmpRoute = null;
    StringBuffer command = new StringBuffer();
    command.append("route del -net " + destination.getHostAddress() + " ");
    if (netmask != null) {
        command.append("netmask " + netmask.getHostAddress() + " ");
    }
    if (gateway != null) {
        if (gateway.getHostAddress().compareTo("127.0.0.1") != 0) {
            command.append("gw " + gateway.getHostAddress() + " ");
        }
    }
    if (iface != null) {
        command.append("dev " + iface + " ");
    }
    SafeProcess proc = null;
    try {
        s_logger.debug("Executing command: {}", command.toString());
        proc = ProcessUtil.exec(command.toString());
        proc.waitFor();
        if (proc.exitValue() != 0) {
            s_logger.error("Error removing static Route");
            throw new Exception("Error removing Static Route");
        }
    } catch (IOException e) {
        s_logger.error("Error executing command:  route -n");
        throw e;
    } finally {
        if (proc != null) {
            ProcessUtil.destroy(proc);
        }
    }
    if (destination instanceof IP4Address) {
        tmpRoute = new RouteConfigIP4((IP4Address) destination, (IP4Address) gateway, (IP4Address) netmask, iface, -1);
    } else if (destination instanceof IP6Address) {
        tmpRoute = new RouteConfigIP6((IP6Address) destination, (IP6Address) gateway, (IP6Address) netmask, iface, -1);
    }
    s_logger.info("Static route removed successfully");
    s_logger.debug(tmpRoute.getDescription());
}
Also used : RouteConfigIP6(org.eclipse.kura.net.route.RouteConfigIP6) IP4Address(org.eclipse.kura.net.IP4Address) RouteConfigIP4(org.eclipse.kura.net.route.RouteConfigIP4) IP6Address(org.eclipse.kura.net.IP6Address) SafeProcess(org.eclipse.kura.core.util.SafeProcess) RouteConfig(org.eclipse.kura.net.route.RouteConfig) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with RouteConfig

use of org.eclipse.kura.net.route.RouteConfig in project kura by eclipse.

the class RouteServiceImpl method getDefaultRoute.

@Override
public RouteConfig getDefaultRoute(String iface) {
    RouteConfig[] routes = getRoutes();
    RouteConfig defaultRoute;
    ArrayList<RouteConfig> defaultRoutes = new ArrayList<RouteConfig>();
    // Search through routes and construct a list of all default routes for the specified interface
    for (RouteConfig route : routes) {
        if (route.getInterfaceName().compareTo(iface) == 0 && route.getDestination().getHostAddress().compareTo("0.0.0.0") == 0) {
            defaultRoutes.add(route);
        }
    }
    // If no default routes exist, return null
    if (defaultRoutes.size() == 0) {
        s_logger.debug("No default routes exist for inteface: {}", iface);
        return null;
    }
    // Set the default route to the first one in the list
    defaultRoute = defaultRoutes.get(0);
    // Search for the default route with the lowest metric value
    for (int i = 1; i < defaultRoutes.size(); i++) {
        if (defaultRoute.getMetric() > defaultRoutes.get(i).getMetric()) {
            defaultRoute = defaultRoutes.get(i);
        }
    }
    s_logger.info("Default route found for interface: " + iface);
    s_logger.debug("Default route:\n{}", defaultRoute.getDescription());
    return defaultRoute;
}
Also used : ArrayList(java.util.ArrayList) RouteConfig(org.eclipse.kura.net.route.RouteConfig)

Example 3 with RouteConfig

use of org.eclipse.kura.net.route.RouteConfig in project kura by eclipse.

the class RouteServiceImpl method getRoutes.

@Override
public RouteConfig[] getRoutes() {
    String routeEntry = null;
    ArrayList<RouteConfig> routeList = new ArrayList<RouteConfig>();
    RouteConfig[] routes = null;
    RouteConfig tmpRoute = null;
    SafeProcess proc = null;
    BufferedReader br = null;
    try {
        proc = ProcessUtil.exec("route -n");
        proc.waitFor();
        br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        br.readLine();
        br.readLine();
        while ((routeEntry = br.readLine()) != null) {
            tmpRoute = entryToRoute(routeEntry);
            if (tmpRoute != null) {
                routeList.add(tmpRoute);
            }
        }
    } catch (Exception e) {
        s_logger.error("Error executing command:  route -n", e);
        return null;
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ex) {
                s_logger.error("I/O Exception while closing BufferedReader!");
            }
        }
        if (proc != null) {
            ProcessUtil.destroy(proc);
        }
    }
    routes = new RouteConfig[routeList.size()];
    for (int i = 0; i < routes.length; i++) {
        routes[i] = routeList.get(i);
    }
    return routes;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) SafeProcess(org.eclipse.kura.core.util.SafeProcess) BufferedReader(java.io.BufferedReader) RouteConfig(org.eclipse.kura.net.route.RouteConfig) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with RouteConfig

use of org.eclipse.kura.net.route.RouteConfig in project kura by eclipse.

the class RouteServiceImpl method addStaticRoute.

@Override
public void addStaticRoute(IPAddress destination, IPAddress gateway, IPAddress netmask, String iface, int metric) throws Exception {
    RouteConfig tmpRoute = null;
    StringBuffer command = new StringBuffer();
    command.append("route add -net " + destination.getHostAddress() + " ");
    if (netmask != null) {
        command.append("netmask " + netmask.getHostAddress() + " ");
    }
    if (gateway != null) {
        if (gateway.getHostAddress().compareTo("0.0.0.0") != 0 && gateway.getHostAddress().compareTo("127.0.0.1") != 0) {
            command.append("gw " + gateway.getHostAddress() + " ");
        }
    }
    if (iface != null) {
        command.append("dev " + iface + " ");
    }
    if (metric != 0 && metric != -1) {
        command.append("metric " + metric);
    }
    SafeProcess proc = null;
    try {
        s_logger.debug("Executing command:  {}", command.toString());
        proc = ProcessUtil.exec(command.toString());
        proc.waitFor();
        if (proc.exitValue() != 0) {
            s_logger.error("Error adding static Route: " + command.toString());
            throw new Exception("Error adding Static Route");
        }
    } catch (IOException e) {
        s_logger.error("Error executing command:  route -n");
        throw e;
    } finally {
        if (proc != null) {
            ProcessUtil.destroy(proc);
        }
    }
    if (destination instanceof IP4Address) {
        tmpRoute = new RouteConfigIP4((IP4Address) destination, (IP4Address) gateway, (IP4Address) netmask, iface, -1);
    } else if (destination instanceof IP6Address) {
        tmpRoute = new RouteConfigIP6((IP6Address) destination, (IP6Address) gateway, (IP6Address) netmask, iface, -1);
    }
    s_logger.info("Static route added successfully");
    s_logger.debug(tmpRoute.getDescription());
}
Also used : RouteConfigIP6(org.eclipse.kura.net.route.RouteConfigIP6) IP4Address(org.eclipse.kura.net.IP4Address) RouteConfigIP4(org.eclipse.kura.net.route.RouteConfigIP4) IP6Address(org.eclipse.kura.net.IP6Address) SafeProcess(org.eclipse.kura.core.util.SafeProcess) RouteConfig(org.eclipse.kura.net.route.RouteConfig) IOException(java.io.IOException) IOException(java.io.IOException)

Example 5 with RouteConfig

use of org.eclipse.kura.net.route.RouteConfig in project kura by eclipse.

the class RouteFile method readFile.

private void readFile() {
    int i = 0;
    RouteConfig newRoute = null;
    FileInputStream in = null;
    Properties routeProps = new Properties();
    try {
        in = new FileInputStream(this.file);
        routeProps.load(in);
    } catch (FileNotFoundException e) {
        s_logger.warn("File not found", e);
    } catch (IOException e) {
        s_logger.warn("Exception while reading file", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                s_logger.error("I/O Exception while closing BufferedReader!");
            }
        }
    }
    newRoute = findRoute(routeProps, i);
    while (newRoute != null) {
        this.routes.add(newRoute);
        i++;
        newRoute = findRoute(routeProps, i);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) RouteConfig(org.eclipse.kura.net.route.RouteConfig) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Aggregations

RouteConfig (org.eclipse.kura.net.route.RouteConfig)15 IOException (java.io.IOException)9 IP4Address (org.eclipse.kura.net.IP4Address)7 IP6Address (org.eclipse.kura.net.IP6Address)6 RouteConfigIP4 (org.eclipse.kura.net.route.RouteConfigIP4)6 RouteConfigIP6 (org.eclipse.kura.net.route.RouteConfigIP6)6 ArrayList (java.util.ArrayList)4 KuraException (org.eclipse.kura.KuraException)4 UnknownHostException (java.net.UnknownHostException)3 SafeProcess (org.eclipse.kura.core.util.SafeProcess)3 RouteService (org.eclipse.kura.linux.net.route.RouteService)3 IPAddress (org.eclipse.kura.net.IPAddress)3 FileNotFoundException (java.io.FileNotFoundException)2 Properties (java.util.Properties)2 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 InetAddress (java.net.InetAddress)1