This project is read-only.

Implementing your own Traffic Handler

The Traffic Handler class is one of the base classes in the Network Library. If you want to implement your own networking tasks, you will have to derive from eExNetworkLibrary.TrafficHandler or one of it's subclasses.

When you derive from Traffic Handler, two methods have to be implemented by you:

/// <summary>
/// This method starts the handlers cleanup process which will release network resources or
/// remote allocated resources. It should be called before stopping the handler to ensure 
/// a clean shutdown.
/// </summary>
public abstract void Cleanup();

/// <summary>
/// This method is called for every frame in the input queue per default. 
// It should be used to process received traffic.
/// </summary>
///<param name="fInputFrame">The frame to process</param>
protected abstract void HandleTraffic(Frame fInputFrame);
The cleanup method provides you with the opportunity to perform cleanup tasks before the handler is shut down. If you, for example, wrote a handler which leases addresses from a DHCP server, the cleanup method would be the right place to release the addresses again before shutdown.

The HandleTraffic method is responsible for performing the handler's operation, regardless of analysis or modification. The Frame fInputFrame is the frame which was received from the previous handler. It is the core method where all the work should be done.

Furthermore, you can override the Start() and Stop() methods, but do not forget to call base.Start() and base.Stop() to start and stop the handlers worker thread.

Traffic Handler provides you with some methods to make working with traffic easier. This includes especially methods for protocol parsing:

// Gets the IP component of the input frame, or null, if no IP component is present.
eExNetworkLibrary.IP.IPFrame ipFrame = GetIPFrame(fInputFrame);
eExNetworkLibrary.IP.IPv4Frame ipv4Frame = GetIPv4Frame(fInputFrame);
eExNetworkLibrary.IP.V6.IPv6Frame ipv6Frame = GetIPv6Frame(fInputFrame); 

//This is also possible for TCP-Frames and so on
eExNetworkLibrary.TCP.TCPFrame tcpFrame = GetTCPFrame(fInputFrame);

If a frame, for example the TCP frame is present, you can edit the frame.

if (tcpFrame != null)
    //Now, do something with the frame.
    tcpFrame.DestinationPort = 8080;

    //But don't forget to adjust checksums if you modify traffic.
    //Also you have to use the pseudo-header of the right frame 
    tcpFrame.Checksum = tcpFrame.CalculateChecksum(ipFrame.GetPseudoHeader());
As already written in the code, don't forget to update the checksum of the frame. Each frame which uses checksums provides you with a method to do that.
Needless to say, you can edit the payload of each frame. The payload of a Frame is accessed via the Frame.EncapsulatedFrame property, but if you want to edit TCP stream data, it may be more easy to derive from the TCPStreamModifier class.

Finally, you can push your frame to the OutputHandler.

//Push the frame to the next handler. 
//Omit this call, if you want to discard the frame. 
If you omit this call, the frame is discarded. You can also call the NotifyNext method, for example, each five seconds by using a timer or by user interaction to send self-crafted frames.

Have a look at trunk/Examples/HandlerPlugInTemplate/HandlerTempate.cs at the repository for a template for implementing own Traffic Handlers.

Don't forget that you can derive from special Traffic Handler classes, if you need certain features.

Last edited Apr 25, 2011 at 11:50 AM by emiswelt, version 9


No comments yet.