Search in sources :

Example 1 with RouteConfigIP6

use of org.eclipse.kura.net.route.RouteConfigIP6 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 RouteConfigIP6

use of org.eclipse.kura.net.route.RouteConfigIP6 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 3 with RouteConfigIP6

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

the class RouteFile method findRoute.

private RouteConfig findRoute(Properties props, int index) {
    RouteConfig route = null;
    IPAddress dest = null;
    IPAddress gw = null;
    IPAddress mask = null;
    if (!props.containsKey("ADDRESS" + index)) {
        return null;
    }
    try {
        dest = IPAddress.parseHostAddress((String) props.get("ADDRESS" + index));
        gw = IPAddress.parseHostAddress((String) props.get("GATEWAY" + index));
        mask = IPAddress.parseHostAddress((String) props.get("NETMASK" + index));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    if (dest != null && gw != null && mask != null) {
        if (dest instanceof IP4Address) {
            route = new RouteConfigIP4((IP4Address) dest, (IP4Address) gw, (IP4Address) mask, this.interfaceName, -1);
        } else if (dest instanceof IP6Address) {
            route = new RouteConfigIP6((IP6Address) dest, (IP6Address) gw, (IP6Address) mask, this.interfaceName, -1);
        }
    }
    return route;
}
Also used : RouteConfigIP6(org.eclipse.kura.net.route.RouteConfigIP6) UnknownHostException(java.net.UnknownHostException) IP4Address(org.eclipse.kura.net.IP4Address) RouteConfigIP4(org.eclipse.kura.net.route.RouteConfigIP4) IP6Address(org.eclipse.kura.net.IP6Address) RouteConfig(org.eclipse.kura.net.route.RouteConfig) IPAddress(org.eclipse.kura.net.IPAddress)

Example 4 with RouteConfigIP6

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

the class RouteFile method removeRoute.

public boolean removeRoute(IPAddress destination, IPAddress gateway, IPAddress netmask, String iface) {
    RouteConfig route = null;
    if (destination instanceof IP4Address) {
        route = new RouteConfigIP4((IP4Address) destination, (IP4Address) gateway, (IP4Address) netmask, this.interfaceName, -1);
    } else if (destination instanceof IP6Address) {
        route = new RouteConfigIP6((IP6Address) destination, (IP6Address) gateway, (IP6Address) netmask, this.interfaceName, -1);
    }
    int index = routeIndex(route);
    if (index != -1) {
        this.routes.remove(index);
        storeFile();
        return true;
    }
    return false;
}
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) RouteConfig(org.eclipse.kura.net.route.RouteConfig)

Example 5 with RouteConfigIP6

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

the class RouteFile method addRoute.

public boolean addRoute(IPAddress destination, IPAddress gateway, IPAddress netmask, String ifaceName) {
    RouteConfig route = null;
    if (destination instanceof IP4Address) {
        route = new RouteConfigIP4((IP4Address) destination, (IP4Address) gateway, (IP4Address) netmask, ifaceName, -1);
    } else if (destination instanceof IP6Address) {
        route = new RouteConfigIP6((IP6Address) destination, (IP6Address) gateway, (IP6Address) netmask, ifaceName, -1);
    }
    if (routeIndex(route) != -1) {
        return false;
    }
    this.routes.add(route);
    storeFile();
    return true;
}
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) RouteConfig(org.eclipse.kura.net.route.RouteConfig)

Aggregations

IP4Address (org.eclipse.kura.net.IP4Address)6 IP6Address (org.eclipse.kura.net.IP6Address)6 RouteConfig (org.eclipse.kura.net.route.RouteConfig)6 RouteConfigIP4 (org.eclipse.kura.net.route.RouteConfigIP4)6 RouteConfigIP6 (org.eclipse.kura.net.route.RouteConfigIP6)6 IOException (java.io.IOException)3 SafeProcess (org.eclipse.kura.core.util.SafeProcess)2 IPAddress (org.eclipse.kura.net.IPAddress)2 UnknownHostException (java.net.UnknownHostException)1 StringTokenizer (java.util.StringTokenizer)1