From c3e054c876e5ff3b87126e7937ff8c1d6a7be4c1 Mon Sep 17 00:00:00 2001 From: Hugo Santos Date: Thu, 5 Apr 2007 18:35:22 +0000 Subject: [PATCH] fixed an issue in UDP due to copying addresses to sockaddr, which may be too small for protocols other than IPv4. In fact, we don't need to copy the addresses at all, we can use the original address pointers in the hash key for lookup and hashing. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20588 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../kernel/network/protocols/udp/udp.cpp | 38 ++++++------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/src/add-ons/kernel/network/protocols/udp/udp.cpp b/src/add-ons/kernel/network/protocols/udp/udp.cpp index a45f6a5745..ae86f98d96 100644 --- a/src/add-ons/kernel/network/protocols/udp/udp.cpp +++ b/src/add-ons/kernel/network/protocols/udp/udp.cpp @@ -1,9 +1,10 @@ /* - * Copyright 2006, Haiku, Inc. All Rights Reserved. + * Copyright 2006-2007, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: * Oliver Tappe, zooey@hirschkaefer.de + * Hugo Santos, hugosantos@gmail.com */ @@ -25,6 +26,7 @@ #include #include #include +#include //#define TRACE_UDP @@ -81,14 +83,8 @@ private: class UdpEndpointManager { + typedef std::pair HashKey; - struct hash_key { - hash_key(sockaddr *ourAddress, sockaddr *peerAddress); - - sockaddr ourAddress; - sockaddr peerAddress; - }; - class Ephemerals { public: Ephemerals(); @@ -155,16 +151,6 @@ static net_stack_module_info *sStackModule; // #pragma mark - -UdpEndpointManager::hash_key::hash_key(sockaddr *_ourAddress, sockaddr *_peerAddress) -{ - memcpy(&ourAddress, _ourAddress, sizeof(sockaddr)); - memcpy(&peerAddress, _peerAddress, sizeof(sockaddr)); -} - - -// #pragma mark - - - UdpEndpointManager::Ephemerals::Ephemerals() : fLastUsed(kLast) @@ -278,13 +264,13 @@ UdpEndpointManager::InitCheck() const UdpEndpointManager::Compare(void *_udpEndpoint, const void *_key) { struct UdpEndpoint *udpEndpoint = (UdpEndpoint*)_udpEndpoint; - hash_key *key = (hash_key *)_key; + const HashKey *key = (const HashKey *)_key; sockaddr *ourAddr = (sockaddr *)&udpEndpoint->socket->address; sockaddr *peerAddr = (sockaddr *)&udpEndpoint->socket->peer; - if (sAddressModule->equal_addresses_and_ports(ourAddr, &key->ourAddress) - && sAddressModule->equal_addresses_and_ports(peerAddr, &key->peerAddress)) + if (sAddressModule->equal_addresses_and_ports(ourAddr, key->first) + && sAddressModule->equal_addresses_and_ports(peerAddr, key->second)) return 0; return 1; @@ -309,8 +295,8 @@ UdpEndpointManager::Hash(void *_udpEndpoint, const void *_key, uint32 range) sockaddr *peerAddr = (sockaddr*)&udpEndpoint->socket->peer; hash = ComputeHash(ourAddr, peerAddr); } else { - hash_key *key = (hash_key *)_key; - hash = ComputeHash(&key->ourAddress, &key->peerAddress); + const HashKey *key = (const HashKey *)_key; + hash = ComputeHash(key->first, key->second); } // move the bits into the relevant range (as defined by kNumHashBuckets): @@ -332,9 +318,9 @@ UdpEndpointManager::FindActiveEndpoint(sockaddr *ourAddress, TRACE(("trying to find UDP-endpoint for (l:%s p:%s)\n", AddressString(sDomain, ourAddress, true).Data(), AddressString(sDomain, peerAddress, true).Data())); - hash_key key(ourAddress, peerAddress); - UdpEndpoint *endpoint = (UdpEndpoint *)hash_lookup(fActiveEndpoints, &key); - return endpoint; + + HashKey key(ourAddress, peerAddress); + return (UdpEndpoint *)hash_lookup(fActiveEndpoints, &key); }