To implement NAT penetration, You need to search for the client's IP address and port at the same time. In this case, you need to customize the struct and then declare a vector of this type, each time a customer initiates a connection, he/she needs to search for this table. If the client's ip address and port exist, the system returns the result. Otherwise, the structure is added to the forwarding list.
# Include "stdafx. h"
# Include <iostream>
# Include <string>
# Include <functional>
# Include <list>
# Include <vector>
# Include <algorithm>
Using namespace std;
Typedef struct _ NAT
{
Char userip [30];
Int userport;
} NatType;
// Query class
Class FindIP
{
Public:
Bool operator () (natType & dValue)
{
Return (strcmp (dValue. userip, m_val.userip) = 0) & dValue. userport = m_val.userport;
}
Static void SetUserNat (natType dval)
{
M_val = dval;
}
Private:
Static natType m_val;
};
NatType natvalue, destvalue;
NatType FindIP: m_val = natvalue;
Int main (int argc, char * argv [])
{Vector <natType> userList;
NatType tempnat;
Strcpy (tempnat. userip, "192.168.0.1 ");
Tempnat. userport = 9000;
UserList. push_back (tempnat );
//, Destvalue;
Strcpy (natvalue. userip, "192.168.0.1 ");
Natvalue. userport = 9000;
FindIP: SetUserNat (natvalue );
Vector <natType>: iterator iter = find_if (userList. begin (), userList. end (), FindIP ());
If (iter = userList. end ())
{
/* Strcpy (destvalue. userip, (* iter). userip );
*/Destvalue. userport = (* iter). userport;
Printf ("nothing/n ");
UserList. push_back (natvalue );
}
Return 0;
}