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:
Arnout Engelen
2013-05-12 18:17:59 +00:00
parent 9d8555e17c
commit d7497c1ebf
3 changed files with 44 additions and 25 deletions

View File

@@ -165,13 +165,24 @@ void Connection::add (Packet * packet)
} }
} }
/* Connection * findConnectionWithMatchingSource(Packet * packet) {
* finds connection to which this packet belongs. assert(packet->Outgoing());
* a packet belongs to a connection if it matches
* to its reference packet ConnList * current = connections;
*/ while (current != NULL)
Connection * findConnection (Packet * packet) {
{ /* 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; ConnList * current = connections;
while (current != NULL) while (current != NULL)
{ {
@@ -183,25 +194,26 @@ Connection * findConnection (Packet * packet)
current = current->getNext(); current = current->getNext();
} }
return findConnectionWithMatchingSource(packet);
}
// Try again, now with the packet inverted: /*
current = connections; * finds connection to which this packet belongs.
Packet * invertedPacket = packet->newInverted(); * a packet belongs to a connection if it matches
* to its reference packet
while (current != NULL) */
Connection * findConnection (Packet * packet)
{
if (packet->Outgoing())
return findConnectionWithMatchingRefpacketOrSource(packet);
else
{ {
/* the reference packet is always *outgoing* */ Packet * invertedPacket = packet->newInverted();
if (invertedPacket->match(current->getVal()->refpacket)) Connection * result = findConnectionWithMatchingRefpacketOrSource(invertedPacket);
{
delete invertedPacket;
return current->getVal();
}
current = current->getNext(); delete invertedPacket;
return result;
} }
delete invertedPacket;
return NULL;
} }
/* /*

View File

@@ -231,11 +231,12 @@ bool Packet::Outgoing () {
dir = dir_outgoing; dir = dir_outgoing;
return true; return true;
} else { } else {
/*if (DEBUG) { if (DEBUG) {
if (sa_family == AF_INET) if (sa_family == AF_INET)
islocal = local_addrs->contains(dip.s_addr); islocal = local_addrs->contains(dip.s_addr);
else else
islocal = local_addrs->contains(dip6); islocal = local_addrs->contains(dip6);
if (!islocal) { if (!islocal) {
std::cerr << "Neither dip nor sip are local: "; std::cerr << "Neither dip nor sip are local: ";
char addy [50]; char addy [50];
@@ -246,7 +247,7 @@ bool Packet::Outgoing () {
return false; return false;
} }
}*/ }
dir = dir_incoming; dir = dir_incoming;
return false; return false;
} }
@@ -273,7 +274,7 @@ char * Packet::gethashstring ()
inet_ntop(sa_family, &dip, remote_string, 49); inet_ntop(sa_family, &dip, remote_string, 49);
} else { } else {
inet_ntop(sa_family, &sip6, local_string, 49); 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()) { if (Outgoing()) {
snprintf(hashstring, HASHKEYSIZE * sizeof(char), "%s:%d-%s:%d", local_string, sport, remote_string, dport); 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) return (sport == other->sport) && (dport == other->dport)
&& (sameinaddr(sip, other->sip)) && (sameinaddr(dip, other->dip)); && (sameinaddr(sip, other->sip)) && (sameinaddr(dip, other->dip));
} }
bool Packet::matchSource (Packet * other)
{
return (sport == other->sport) && (sameinaddr(sip, other->sip));
}

View File

@@ -74,6 +74,7 @@ public:
bool Outgoing (); bool Outgoing ();
bool match (Packet * other); bool match (Packet * other);
bool matchSource (Packet * other);
/* returns '1.2.3.4:5-1.2.3.4:6'-style string */ /* returns '1.2.3.4:5-1.2.3.4:6'-style string */
char * gethashstring(); char * gethashstring();
private: private: