As a last resort, match packets to processes when only the local part matches
Helps for applications with many short-lived connections (such as bittorrent) and is correct except for exotic use cases anyway
This commit is contained in:
@@ -165,13 +165,24 @@ void Connection::add (Packet * packet)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* finds connection to which this packet belongs.
|
||||
* a packet belongs to a connection if it matches
|
||||
* to its reference packet
|
||||
*/
|
||||
Connection * findConnection (Packet * packet)
|
||||
{
|
||||
Connection * findConnectionWithMatchingSource(Packet * packet) {
|
||||
assert(packet->Outgoing());
|
||||
|
||||
ConnList * current = connections;
|
||||
while (current != NULL)
|
||||
{
|
||||
/* the reference packet is always outgoing */
|
||||
if (packet->matchSource(current->getVal()->refpacket))
|
||||
{
|
||||
return current->getVal();
|
||||
}
|
||||
|
||||
current = current->getNext();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Connection * findConnectionWithMatchingRefpacketOrSource(Packet * packet) {
|
||||
ConnList * current = connections;
|
||||
while (current != NULL)
|
||||
{
|
||||
@@ -183,25 +194,26 @@ Connection * findConnection (Packet * packet)
|
||||
|
||||
current = current->getNext();
|
||||
}
|
||||
return findConnectionWithMatchingSource(packet);
|
||||
}
|
||||
|
||||
// Try again, now with the packet inverted:
|
||||
current = connections;
|
||||
Packet * invertedPacket = packet->newInverted();
|
||||
|
||||
while (current != NULL)
|
||||
/*
|
||||
* finds connection to which this packet belongs.
|
||||
* a packet belongs to a connection if it matches
|
||||
* to its reference packet
|
||||
*/
|
||||
Connection * findConnection (Packet * packet)
|
||||
{
|
||||
if (packet->Outgoing())
|
||||
return findConnectionWithMatchingRefpacketOrSource(packet);
|
||||
else
|
||||
{
|
||||
/* the reference packet is always *outgoing* */
|
||||
if (invertedPacket->match(current->getVal()->refpacket))
|
||||
{
|
||||
delete invertedPacket;
|
||||
return current->getVal();
|
||||
}
|
||||
Packet * invertedPacket = packet->newInverted();
|
||||
Connection * result = findConnectionWithMatchingRefpacketOrSource(invertedPacket);
|
||||
|
||||
current = current->getNext();
|
||||
delete invertedPacket;
|
||||
return result;
|
||||
}
|
||||
|
||||
delete invertedPacket;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
12
packet.cpp
12
packet.cpp
@@ -231,11 +231,12 @@ bool Packet::Outgoing () {
|
||||
dir = dir_outgoing;
|
||||
return true;
|
||||
} else {
|
||||
/*if (DEBUG) {
|
||||
if (DEBUG) {
|
||||
if (sa_family == AF_INET)
|
||||
islocal = local_addrs->contains(dip.s_addr);
|
||||
else
|
||||
islocal = local_addrs->contains(dip6);
|
||||
|
||||
if (!islocal) {
|
||||
std::cerr << "Neither dip nor sip are local: ";
|
||||
char addy [50];
|
||||
@@ -246,7 +247,7 @@ bool Packet::Outgoing () {
|
||||
|
||||
return false;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
dir = dir_incoming;
|
||||
return false;
|
||||
}
|
||||
@@ -273,7 +274,7 @@ char * Packet::gethashstring ()
|
||||
inet_ntop(sa_family, &dip, remote_string, 49);
|
||||
} else {
|
||||
inet_ntop(sa_family, &sip6, local_string, 49);
|
||||
inet_ntop(sa_family, &dip6, remote_string, 49);
|
||||
inet_ntop(sa_family, &dip6, remote_string, 49);
|
||||
}
|
||||
if (Outgoing()) {
|
||||
snprintf(hashstring, HASHKEYSIZE * sizeof(char), "%s:%d-%s:%d", local_string, sport, remote_string, dport);
|
||||
@@ -294,3 +295,8 @@ bool Packet::match (Packet * other)
|
||||
return (sport == other->sport) && (dport == other->dport)
|
||||
&& (sameinaddr(sip, other->sip)) && (sameinaddr(dip, other->dip));
|
||||
}
|
||||
|
||||
bool Packet::matchSource (Packet * other)
|
||||
{
|
||||
return (sport == other->sport) && (sameinaddr(sip, other->sip));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user