Skip to content

Instantly share code, notes, and snippets.

@toxuin
Created April 12, 2015 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toxuin/c1b9f6812d6e4c7787d6 to your computer and use it in GitHub Desktop.
Save toxuin/c1b9f6812d6e4c7787d6 to your computer and use it in GitHub Desktop.
Router simulation
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import org.apache.commons.net.util.SubnetUtils;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Router {
private Map<String, String> interfaces;
private Set<Route> routingTable;
private Route defaultRoute;
public Router() {
interfaces = new HashMap<>();
routingTable = new HashSet<>();
}
public static void main(String[] args) {
Router router = new Router();
if (args.length < 2) {
System.out.println("No arguments supplied!");
return;
}
File routesFile = new File(args[0]);
File packetsFile = new File(args[1]);
if (!routesFile.exists()) {
System.out.println("Routing table file not found!");
}
if (!packetsFile.exists()) {
System.out.println("Routing table file not found!");
}
try {
router.parseRoutingFile(routesFile);
System.out.println("Added " + router.routingTable.size() + " routes from config");
} catch (IOException e) {
System.out.println("Routing table file has invalid format! " + e.getMessage());
e.printStackTrace();
return;
}
try {
router.parsePacketsFile(packetsFile);
} catch (IOException e) {
System.out.println("Packets file has invalid format! " + e.getMessage());
e.printStackTrace();
return;
}
}
public void parseRoutingFile(File file) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = buf.readLine()) != null) {
String[] strLine = line.trim().split("//");
String destination = strLine[0].trim().substring(5);
String netmask = strLine[1].trim().substring(5);
String gateway = strLine[2].trim().substring(4);
String iface = strLine[3].trim().substring(4);
int metric = Integer.parseInt(strLine[4].trim().substring(5));
SubnetUtils subnet = new SubnetUtils(destination, netmask);
Inet4Address ifce = (Inet4Address) Inet4Address.getByName(iface);
Route route = new Route(subnet.getInfo(), (Inet4Address) Inet4Address.getByName(gateway), ifce, metric);
if (destination.equals("0.0.0.0")) defaultRoute = route;
if (!interfaces.keySet().contains(ifce.getHostAddress())) interfaces.put(ifce.getHostAddress(), "eth" + interfaces.size());
routingTable.add(route);
}
buf.close();
}
public void parsePacketsFile(File file) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = buf.readLine()) != null) {
String[] strLine = line.trim().split(":");
String dest = strLine[1].trim();
Route bestRoute = null;
int bestMetric = -1;
routingTable.remove(defaultRoute);
for (Route route : routingTable) {
// CHECK IF DESTINATION BELONGS TO THIS ROUTE
if (route.network.isInRange(dest)) {
// CHECK METRICS
if (bestMetric == -1 || bestMetric > route.metric) {
bestMetric = route.metric;
bestRoute = route;
}
}
}
if (bestRoute == null) {
bestRoute = defaultRoute;
}
System.out.println("Packet with destination " + dest + " will be forwarded to " + bestRoute.gateway.getHostAddress() + " on interface " + interfaces.get(bestRoute.iface.getHostAddress()));
}
buf.close();
}
private class Route {
final SubnetUtils.SubnetInfo network;
final Inet4Address gateway;
final Inet4Address iface;
final int metric;
public Route(SubnetUtils.SubnetInfo subnet, Inet4Address gateway, Inet4Address address, int metric) {
this.network = subnet;
this.gateway = gateway;
this.iface = address;
this.metric = metric;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment