This project is read-only.

Building an Extended Router

Notice: You should complete Writing a Router before starting this tutorial.

Adding new components to an existing system is not very difficult. If you worked through the first tutorial, you have already tried to link components together. Adding new components to our existing router is easy. You just have to create the new traffic handles, then link them into the graph and then update the shutdown routine.

For example, add this code snippet below the code where you create and start your router:

LibPcapDumper lcpDumper = new LibPcapDumper();
NetMap nmMap = new NetMap();
WANEmulator wanEmulator = new WANEmulator();

The LibPcapDumper is a class which can dump Frames into a wireshark-readable format, the NetMap provides information about other hosts in the network and the WAN-Emulator simulates losses and errors which can happen in a WAN.

Let's get this components to make some output:

//Set traffic dumper properties
lcpDumper.StartLogging(Path.Combine(System.Environment.CurrentDirectory, "Dump " + 
 DateTime.Now.ToLongDateString()), false);

nmMap.HostInformationChanged += 
 new NetMap.HostChangedEventHandler(nmMap_HostInformationChanged);
This will cause the dumper to start writing a dump file to the given path and the NetMap to notify us about the newest information about hosts in our network.

Here is the event handler for the NetMap:

static void nmMap_HostInformationChanged(HostInformationChangedEventArgs args, 
 object sender)
    Console.Write("Host found: " + args.Host.Name + " ");
    if (args.Host.IPAddresses.Count > 0)
        Console.Write(args.Host.IPAddresses[0].ToString() + " ");
The next step is to link the traffic handlers accordingly together. We have to set the WAN emulator as the output handler of the traffic splitter and the router as the output handler of the WAN emulator. Further we have to add the dumper and the network map to the analyzer list of the traffic splitter.

//Let the router forward traffic from the interfaces to the traffic splitter
rRouter.OutputHandler = tsSplitter;
//Let the traffic splitter forward received traffic to the WAN emulator
tsSplitter.OutputHandler = wanEmulator;
//Let the WAN emulator forward received traffic back to the router
wanEmulator.OutputHandler = rRouter;
//Let the traffic splitter clone each frame and send it to the traffic dumper and the NetMap
Don't forget to add calls to the Cleanup() and Stop() methods for the LibPcapDumper, the WANEmualtor and the NetMap at the end of the program.

If this looks complicated to you, have a look at this image:


If our little program was a NetLab compilation, it would look like this. The arrows indicate the flow of the traffic. The traffic flows from the interface to the router (white arrows) and the router forwards the joined traffic streams to the traffic splitter (orange arrow). The traffic splitter then clones the frame once. This is very important to keep in mind as this is the reason why traffic analyzers must not modify any property of the frame because the traffic splitter forwards the one cloned frame to all traffic splitter simultaneously and modifying a frame while another traffic splitter can reads or also modifies it could cause racing conditions and serious program errors. The other instance will be forwarded to the next traffic modifier connected with the out port of the traffic splitter, even if it is the router like in the first lesson. Because the frame is cloned, the next traffic analyzer can modify the frame without affecting any of the traffic analyzers. The WAN emulator will then forward the frame to the router which will send it out to the according interface (white arrow). The case where a single frame is sent to multiple handlers occurs only when the traffic splitter sends frames to traffic analyzers (blue arrows). At each other circumstance (red, white and orange arrows), the frames are only edited or viewed by one traffic handler at one time. Keeping this in mind, it is possible to build very complex scenarios including conditional splitters, which split up traffic streams according to frame properties and other components.


For writing applications, it is necessary to understand how traffic flows in the network. Once understand, you can harness the power of the network library. This way, you can also add Traffic Handlers implemented by yourself.

Last edited Apr 24, 2011 at 3:12 PM by emiswelt, version 8


No comments yet.