Search code examples
c++dictionarynetwork-programmingpacket

How to discern between network flows


I want to be able to discern between networks flows. I am defining a flow as a tuple of three values (sourceIP, destIP, protocol). I am storing these in a c++ map for fast access. However, if the destinationIP and the sourceIP are different, but contain the same values, (e.g. )

[packet 1: source = 1.2.3.4, dest = 5.6.7.8] 

[packet 2: source = 5.6.7.8, dest = 1.2.3.4 ]

I would like to create a key that treats these as the same.

I could solve this by creating a secondary key and a primary key, and if the primary key doesn't match I could loop through the elements in my table and see if the secondary key matches, but this seems really inefficient.

I think this might be a perfect opportunity for hashing, but the it seems like string hashes are only available through boost, and we are not allowed to bring in libraries, and I am not sure if I know of a hash function that only computes on elements, not ordering.

How can I easily tell flows apart according to these rules?


Solution

  • Compare the values of the source and dest IPs as 64-bit numbers. Use the lower one as the hash key, and put the higher one, the protocol and the direction as the values.

    Do lookups the same way, use the lower value as the key.