michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** Base class implementation for network access functions (ref: prnetdb.h) michael@0: */ michael@0: michael@0: #include "rclock.h" michael@0: #include "rcnetdb.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: RCNetAddr::RCNetAddr(const RCNetAddr& his): RCBase() michael@0: { address = his.address; } michael@0: michael@0: RCNetAddr::RCNetAddr(const RCNetAddr& his, PRUint16 port): RCBase() michael@0: { michael@0: address = his.address; michael@0: switch (address.raw.family) michael@0: { michael@0: case PR_AF_INET: address.inet.port = port; break; michael@0: case PR_AF_INET6: address.ipv6.port = port; break; michael@0: default: break; michael@0: } michael@0: } /* RCNetAddr::RCNetAddr */ michael@0: michael@0: RCNetAddr::RCNetAddr(RCNetAddr::HostValue host, PRUint16 port): RCBase() michael@0: { michael@0: PRNetAddrValue how; michael@0: switch (host) michael@0: { michael@0: case RCNetAddr::any: how = PR_IpAddrAny; break; michael@0: case RCNetAddr::loopback: how = PR_IpAddrLoopback; break; michael@0: default: PR_ASSERT(!"This can't happen -- and did!"); michael@0: } michael@0: (void)PR_InitializeNetAddr(how, port, &address); michael@0: } /* RCNetAddr::RCNetAddr */ michael@0: michael@0: RCNetAddr::~RCNetAddr() { } michael@0: michael@0: void RCNetAddr::operator=(const RCNetAddr& his) { address = his.address; } michael@0: michael@0: PRStatus RCNetAddr::FromString(const char* string) michael@0: { return PR_StringToNetAddr(string, &address); } michael@0: michael@0: void RCNetAddr::operator=(const PRNetAddr* addr) { address = *addr; } michael@0: michael@0: PRBool RCNetAddr::operator==(const RCNetAddr& his) const michael@0: { michael@0: PRBool rv = EqualHost(his); michael@0: if (rv) michael@0: { michael@0: switch (address.raw.family) michael@0: { michael@0: case PR_AF_INET: michael@0: rv = (address.inet.port == his.address.inet.port); break; michael@0: case PR_AF_INET6: michael@0: rv = (address.ipv6.port == his.address.ipv6.port); break; michael@0: case PR_AF_LOCAL: michael@0: default: break; michael@0: } michael@0: } michael@0: return rv; michael@0: } /* RCNetAddr::operator== */ michael@0: michael@0: PRBool RCNetAddr::EqualHost(const RCNetAddr& his) const michael@0: { michael@0: PRBool rv; michael@0: switch (address.raw.family) michael@0: { michael@0: case PR_AF_INET: michael@0: rv = (address.inet.ip == his.address.inet.ip); break; michael@0: case PR_AF_INET6: michael@0: rv = (0 == memcmp( michael@0: &address.ipv6.ip, &his.address.ipv6.ip, michael@0: sizeof(address.ipv6.ip))); michael@0: break; michael@0: #if defined(XP_UNIX) michael@0: case PR_AF_LOCAL: michael@0: rv = (0 == strncmp( michael@0: address.local.path, his.address.local.path, michael@0: sizeof(address.local.path))); michael@0: break; michael@0: #endif michael@0: default: break; michael@0: } michael@0: return rv; michael@0: } /* RCNetAddr::operator== */ michael@0: michael@0: PRStatus RCNetAddr::ToString(char *string, PRSize size) const michael@0: { return PR_NetAddrToString(&address, string, size); } michael@0: michael@0: /* michael@0: ** RCHostLookup michael@0: */ michael@0: michael@0: RCHostLookup::~RCHostLookup() michael@0: { michael@0: if (NULL != address) delete [] address; michael@0: } /* RCHostLookup::~RCHostLookup */ michael@0: michael@0: RCHostLookup::RCHostLookup(): RCBase() michael@0: { michael@0: address = NULL; michael@0: max_index = 0; michael@0: } /* RCHostLookup::RCHostLookup */ michael@0: michael@0: PRStatus RCHostLookup::ByName(const char* name) michael@0: { michael@0: PRStatus rv; michael@0: PRNetAddr addr; michael@0: PRHostEnt hostentry; michael@0: PRIntn index = 0, max; michael@0: RCNetAddr* vector = NULL; michael@0: RCNetAddr* old_vector = NULL; michael@0: void* buffer = PR_Malloc(PR_NETDB_BUF_SIZE); michael@0: if (NULL == buffer) return PR_FAILURE; michael@0: rv = PR_GetHostByName(name, (char*)buffer, PR_NETDB_BUF_SIZE, &hostentry); michael@0: if (PR_SUCCESS == rv) michael@0: { michael@0: for (max = 0, index = 0;; ++max) michael@0: { michael@0: index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); michael@0: if (0 == index) break; michael@0: } michael@0: if (max > 0) michael@0: { michael@0: vector = new RCNetAddr[max]; michael@0: while (--max > 0) michael@0: { michael@0: index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); michael@0: if (0 == index) break; michael@0: vector[index] = &addr; michael@0: } michael@0: { michael@0: RCEnter entry(&ml); michael@0: old_vector = address; michael@0: address = vector; michael@0: max_index = max; michael@0: } michael@0: if (NULL != old_vector) delete [] old_vector; michael@0: } michael@0: } michael@0: if (NULL != buffer) PR_DELETE(buffer); michael@0: return PR_SUCCESS; michael@0: } /* RCHostLookup::ByName */ michael@0: michael@0: PRStatus RCHostLookup::ByAddress(const RCNetAddr& host_addr) michael@0: { michael@0: PRStatus rv; michael@0: PRNetAddr addr; michael@0: PRHostEnt hostentry; michael@0: PRIntn index = 0, max; michael@0: RCNetAddr* vector = NULL; michael@0: RCNetAddr* old_vector = NULL; michael@0: char *buffer = (char*)PR_Malloc(PR_NETDB_BUF_SIZE); michael@0: if (NULL == buffer) return PR_FAILURE; michael@0: rv = PR_GetHostByAddr(host_addr, buffer, PR_NETDB_BUF_SIZE, &hostentry); michael@0: if (PR_SUCCESS == rv) michael@0: { michael@0: for (max = 0, index = 0;; ++max) michael@0: { michael@0: index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); michael@0: if (0 == index) break; michael@0: } michael@0: if (max > 0) michael@0: { michael@0: vector = new RCNetAddr[max]; michael@0: while (--max > 0) michael@0: { michael@0: index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); michael@0: if (0 == index) break; michael@0: vector[index] = &addr; michael@0: } michael@0: { michael@0: RCEnter entry(&ml); michael@0: old_vector = address; michael@0: address = vector; michael@0: max_index = max; michael@0: } michael@0: if (NULL != old_vector) delete [] old_vector; michael@0: } michael@0: } michael@0: if (NULL != buffer) PR_DELETE(buffer); michael@0: return PR_SUCCESS; michael@0: } /* RCHostLookup::ByAddress */ michael@0: michael@0: const RCNetAddr* RCHostLookup::operator[](PRUintn which) michael@0: { michael@0: RCNetAddr* addr = NULL; michael@0: if (which < max_index) michael@0: addr = &address[which]; michael@0: return addr; michael@0: } /* RCHostLookup::operator[] */ michael@0: michael@0: /* RCNetdb.cpp */