From 9e084902c1193d64fb3339e4a1fdcea9821dc42b Mon Sep 17 00:00:00 2001 From: Hugo Santos Date: Thu, 5 Apr 2007 18:57:15 +0000 Subject: [PATCH] small cleanups in UDP - don't set_to_empty_address, instead consider NULL to be the empty address and use is_empty_address(). - most ipv4 address module calls already consider NULL to be the empty address, ipv4_hash_address_pair didn't, fixed. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20589 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../network/protocols/ipv4/ipv4_address.cpp | 10 +++--- .../kernel/network/protocols/udp/udp.cpp | 33 +++++++++---------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/add-ons/kernel/network/protocols/ipv4/ipv4_address.cpp b/src/add-ons/kernel/network/protocols/ipv4/ipv4_address.cpp index ece9af6cdc..5e3cc99353 100644 --- a/src/add-ons/kernel/network/protocols/ipv4/ipv4_address.cpp +++ b/src/add-ons/kernel/network/protocols/ipv4/ipv4_address.cpp @@ -350,11 +350,11 @@ ipv4_set_to_empty_address(sockaddr *address) static uint32 ipv4_hash_address_pair(const sockaddr *ourAddress, const sockaddr *peerAddress) { - int32 hash = (((sockaddr_in *)ourAddress)->sin_port - | ((sockaddr_in *)peerAddress)->sin_port << 16) - ^ ((sockaddr_in *)ourAddress)->sin_addr.s_addr - ^ ((sockaddr_in *)peerAddress)->sin_addr.s_addr; - return hash; + const sockaddr_in *our = (const sockaddr_in *)ourAddress; + const sockaddr_in *peer = (const sockaddr_in *)peerAddress; + + return ((our ? our->sin_port : 0) | ((peer ? peer->sin_port : 0) << 16)) + ^ (our ? our->sin_addr.s_addr : 0) ^ (peer ? peer->sin_addr.s_addr : 0); } diff --git a/src/add-ons/kernel/network/protocols/udp/udp.cpp b/src/add-ons/kernel/network/protocols/udp/udp.cpp index ae86f98d96..c30d4a9b53 100644 --- a/src/add-ons/kernel/network/protocols/udp/udp.cpp +++ b/src/add-ons/kernel/network/protocols/udp/udp.cpp @@ -83,7 +83,7 @@ private: class UdpEndpointManager { - typedef std::pair HashKey; + typedef std::pair HashKey; class Ephemerals { public: @@ -108,7 +108,8 @@ public: status_t ReceiveData(net_buffer *buffer); static int Compare(void *udpEndpoint, const void *_key); - static uint32 ComputeHash(sockaddr *ourAddress, sockaddr *peerAddress); + static uint32 ComputeHash(const sockaddr *ourAddress, + const sockaddr *peerAddress); static uint32 Hash(void *udpEndpoint, const void *key, uint32 range); UdpEndpoint *FindActiveEndpoint(sockaddr *ourAddress, @@ -278,7 +279,8 @@ UdpEndpointManager::Compare(void *_udpEndpoint, const void *_key) /*static*/ inline uint32 -UdpEndpointManager::ComputeHash(sockaddr *ourAddress, sockaddr *peerAddress) +UdpEndpointManager::ComputeHash(const sockaddr *ourAddress, + const sockaddr *peerAddress) { return sAddressModule->hash_address_pair(ourAddress, peerAddress); } @@ -287,18 +289,19 @@ UdpEndpointManager::ComputeHash(sockaddr *ourAddress, sockaddr *peerAddress) /*static*/ uint32 UdpEndpointManager::Hash(void *_udpEndpoint, const void *_key, uint32 range) { + HashKey addresses; uint32 hash; if (_udpEndpoint) { - struct UdpEndpoint *udpEndpoint = (UdpEndpoint*)_udpEndpoint; - sockaddr *ourAddr = (sockaddr*)&udpEndpoint->socket->address; - sockaddr *peerAddr = (sockaddr*)&udpEndpoint->socket->peer; - hash = ComputeHash(ourAddr, peerAddr); + const UdpEndpoint *udpEndpoint = (const UdpEndpoint *)_udpEndpoint; + addresses = HashKey((const sockaddr *)&udpEndpoint->socket->address, + (const sockaddr *)&udpEndpoint->socket->peer); } else { - const HashKey *key = (const HashKey *)_key; - hash = ComputeHash(key->first, key->second); + addresses = *(const HashKey *)_key; } + hash = ComputeHash(addresses.first, addresses.second); + // move the bits into the relevant range (as defined by kNumHashBuckets): hash = (hash & 0x000007FF) ^ (hash & 0x003FF800) >> 11 ^ (hash & 0xFFC00000UL) >> 22; @@ -335,9 +338,6 @@ UdpEndpointManager::DemuxBroadcast(net_buffer *buffer) TRACE(("demuxing buffer %p as broadcast...\n", buffer)); - sockaddr anyAddr; - sAddressModule->set_to_empty_address(&anyAddr); - uint16 incomingPort = sAddressModule->get_port(broadcastAddr); UdpEndpoint *endpoint; @@ -369,7 +369,7 @@ UdpEndpointManager::DemuxBroadcast(net_buffer *buffer) } if (sAddressModule->equal_masked_addresses(addr, broadcastAddr, mask) - || sAddressModule->equal_addresses(addr, &anyAddr)) { + || sAddressModule->is_empty_address(addr, false)) { // address matches, dispatch to this endpoint: endpoint->StoreData(buffer); } @@ -394,15 +394,12 @@ UdpEndpointManager::DemuxUnicast(net_buffer *buffer) TRACE(("demuxing buffer %p as unicast...\n", buffer)); - struct sockaddr anyAddr; - sAddressModule->set_to_empty_address(&anyAddr); - UdpEndpoint *endpoint; // look for full (most special) match: endpoint = FindActiveEndpoint(localAddr, peerAddr); if (!endpoint) { // look for endpoint matching local address & port: - endpoint = FindActiveEndpoint(localAddr, &anyAddr); + endpoint = FindActiveEndpoint(localAddr, NULL); if (!endpoint) { // look for endpoint matching peer address & port and local port: sockaddr localPortAddr; @@ -412,7 +409,7 @@ UdpEndpointManager::DemuxUnicast(net_buffer *buffer) endpoint = FindActiveEndpoint(&localPortAddr, peerAddr); if (!endpoint) { // last chance: look for endpoint matching local port only: - endpoint = FindActiveEndpoint(&localPortAddr, &anyAddr); + endpoint = FindActiveEndpoint(&localPortAddr, NULL); } } }