A map data structure is defined as follows: Map <tuple4, ctcpmanage> m_maptcp;
The key-value type is tuple4, which stores the TCP tuples and is defined as follows:
Struct tuple4
{
U_int32_t sourceip;
U_int32_t destip;
Unsigned short sport;
Unsigned short dport;
};
The associated value type is ctcpmanage, which is responsible for various operations on a TCP connection. Its constructor is as follows:
Ctcpmanage (tuple4 tuple4, bool bc2s );
Problems:
1. when the key value is used to search for elements in the map, Map <tuple4, ctcpmanage>: iterator iter = m_maptcp.find (tuple); statement compilation error, error message: /usr/lib/GCC/i386-redhat-linux/4.1.2/operator error: no match is 'operator' <'in' _ x <_ y'
Error cause:
The internal data structure of map is a binary search tree (rb tree). During the search operation, it starts from the root node of the binary tree. If the target key value is greater than the key value of the node, search for the right subtree. Otherwise, search for the left subtree. Therefore, when using map, you must define the relationship between key values, that is, you must define the operator "<".
Solution:
Define the tuple4 less than operator as follows:
Bool operator <(const tuple4 elem1, const tuple4 elem2)
{
If (elem1.sourceip! = Elem2.sourceip)
{
Return (elem1.sourceip <elem2.sourceip );
}
Else if (elem1.destip! = Elem2.destip)
{
Return (elem1.destip <elem2.destip );
}
Else if (elem1.sport! = Elem2.sport)
{
Return (elem1.sport <elem2.sport );
}
Else if (elem1.dport! = Elem2.dport)
{
Return (elem1.dport <elem2.dport );
}
Else
{
Return false;
}
}
2. m_maptcp [tuple] = objtcpmanage; error, error message:/usr/lib/GCC/i386-redhat-linux/4.1.2/errors: To 'ctcpmanage :: no matching function is called for ctcpmanage () '.
Error cause:
Map uses the keyword tuple to find the corresponding Association value. If the association value is found, the corresponding Association value data is returned. If not, add an element. The key value of the added element is tuple and the associated value is an automatically generated object. By default, this object calls a non-parameter constructor.
Solution: ctcpmanage objtcpmanage (tuple, true); m_maptcp.insert (STD: make_pair (tuple, objtcpmanage ));