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
This commit is contained in:
Hugo Santos 2007-04-05 18:57:15 +00:00
parent c3e054c876
commit 9e084902c1
2 changed files with 20 additions and 23 deletions

View File

@ -350,11 +350,11 @@ ipv4_set_to_empty_address(sockaddr *address)
static uint32 static uint32
ipv4_hash_address_pair(const sockaddr *ourAddress, const sockaddr *peerAddress) ipv4_hash_address_pair(const sockaddr *ourAddress, const sockaddr *peerAddress)
{ {
int32 hash = (((sockaddr_in *)ourAddress)->sin_port const sockaddr_in *our = (const sockaddr_in *)ourAddress;
| ((sockaddr_in *)peerAddress)->sin_port << 16) const sockaddr_in *peer = (const sockaddr_in *)peerAddress;
^ ((sockaddr_in *)ourAddress)->sin_addr.s_addr
^ ((sockaddr_in *)peerAddress)->sin_addr.s_addr; return ((our ? our->sin_port : 0) | ((peer ? peer->sin_port : 0) << 16))
return hash; ^ (our ? our->sin_addr.s_addr : 0) ^ (peer ? peer->sin_addr.s_addr : 0);
} }

View File

@ -83,7 +83,7 @@ private:
class UdpEndpointManager { class UdpEndpointManager {
typedef std::pair<sockaddr *, sockaddr *> HashKey; typedef std::pair<const sockaddr *, const sockaddr *> HashKey;
class Ephemerals { class Ephemerals {
public: public:
@ -108,7 +108,8 @@ public:
status_t ReceiveData(net_buffer *buffer); status_t ReceiveData(net_buffer *buffer);
static int Compare(void *udpEndpoint, const void *_key); 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); static uint32 Hash(void *udpEndpoint, const void *key, uint32 range);
UdpEndpoint *FindActiveEndpoint(sockaddr *ourAddress, UdpEndpoint *FindActiveEndpoint(sockaddr *ourAddress,
@ -278,7 +279,8 @@ UdpEndpointManager::Compare(void *_udpEndpoint, const void *_key)
/*static*/ inline uint32 /*static*/ inline uint32
UdpEndpointManager::ComputeHash(sockaddr *ourAddress, sockaddr *peerAddress) UdpEndpointManager::ComputeHash(const sockaddr *ourAddress,
const sockaddr *peerAddress)
{ {
return sAddressModule->hash_address_pair(ourAddress, peerAddress); return sAddressModule->hash_address_pair(ourAddress, peerAddress);
} }
@ -287,18 +289,19 @@ UdpEndpointManager::ComputeHash(sockaddr *ourAddress, sockaddr *peerAddress)
/*static*/ uint32 /*static*/ uint32
UdpEndpointManager::Hash(void *_udpEndpoint, const void *_key, uint32 range) UdpEndpointManager::Hash(void *_udpEndpoint, const void *_key, uint32 range)
{ {
HashKey addresses;
uint32 hash; uint32 hash;
if (_udpEndpoint) { if (_udpEndpoint) {
struct UdpEndpoint *udpEndpoint = (UdpEndpoint*)_udpEndpoint; const UdpEndpoint *udpEndpoint = (const UdpEndpoint *)_udpEndpoint;
sockaddr *ourAddr = (sockaddr*)&udpEndpoint->socket->address; addresses = HashKey((const sockaddr *)&udpEndpoint->socket->address,
sockaddr *peerAddr = (sockaddr*)&udpEndpoint->socket->peer; (const sockaddr *)&udpEndpoint->socket->peer);
hash = ComputeHash(ourAddr, peerAddr);
} else { } else {
const HashKey *key = (const HashKey *)_key; addresses = *(const HashKey *)_key;
hash = ComputeHash(key->first, key->second);
} }
hash = ComputeHash(addresses.first, addresses.second);
// move the bits into the relevant range (as defined by kNumHashBuckets): // move the bits into the relevant range (as defined by kNumHashBuckets):
hash = (hash & 0x000007FF) ^ (hash & 0x003FF800) >> 11 hash = (hash & 0x000007FF) ^ (hash & 0x003FF800) >> 11
^ (hash & 0xFFC00000UL) >> 22; ^ (hash & 0xFFC00000UL) >> 22;
@ -335,9 +338,6 @@ UdpEndpointManager::DemuxBroadcast(net_buffer *buffer)
TRACE(("demuxing buffer %p as broadcast...\n", buffer)); TRACE(("demuxing buffer %p as broadcast...\n", buffer));
sockaddr anyAddr;
sAddressModule->set_to_empty_address(&anyAddr);
uint16 incomingPort = sAddressModule->get_port(broadcastAddr); uint16 incomingPort = sAddressModule->get_port(broadcastAddr);
UdpEndpoint *endpoint; UdpEndpoint *endpoint;
@ -369,7 +369,7 @@ UdpEndpointManager::DemuxBroadcast(net_buffer *buffer)
} }
if (sAddressModule->equal_masked_addresses(addr, broadcastAddr, mask) 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: // address matches, dispatch to this endpoint:
endpoint->StoreData(buffer); endpoint->StoreData(buffer);
} }
@ -394,15 +394,12 @@ UdpEndpointManager::DemuxUnicast(net_buffer *buffer)
TRACE(("demuxing buffer %p as unicast...\n", buffer)); TRACE(("demuxing buffer %p as unicast...\n", buffer));
struct sockaddr anyAddr;
sAddressModule->set_to_empty_address(&anyAddr);
UdpEndpoint *endpoint; UdpEndpoint *endpoint;
// look for full (most special) match: // look for full (most special) match:
endpoint = FindActiveEndpoint(localAddr, peerAddr); endpoint = FindActiveEndpoint(localAddr, peerAddr);
if (!endpoint) { if (!endpoint) {
// look for endpoint matching local address & port: // look for endpoint matching local address & port:
endpoint = FindActiveEndpoint(localAddr, &anyAddr); endpoint = FindActiveEndpoint(localAddr, NULL);
if (!endpoint) { if (!endpoint) {
// look for endpoint matching peer address & port and local port: // look for endpoint matching peer address & port and local port:
sockaddr localPortAddr; sockaddr localPortAddr;
@ -412,7 +409,7 @@ UdpEndpointManager::DemuxUnicast(net_buffer *buffer)
endpoint = FindActiveEndpoint(&localPortAddr, peerAddr); endpoint = FindActiveEndpoint(&localPortAddr, peerAddr);
if (!endpoint) { if (!endpoint) {
// last chance: look for endpoint matching local port only: // last chance: look for endpoint matching local port only:
endpoint = FindActiveEndpoint(&localPortAddr, &anyAddr); endpoint = FindActiveEndpoint(&localPortAddr, NULL);
} }
} }
} }