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());
}
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;
}
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;
}
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());
}
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);
}
}
Aggregations