1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/src/cplus/rcnetdb.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,200 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 +** Base class implementation for network access functions (ref: prnetdb.h) 1.11 +*/ 1.12 + 1.13 +#include "rclock.h" 1.14 +#include "rcnetdb.h" 1.15 + 1.16 +#include <prmem.h> 1.17 +#include <prlog.h> 1.18 +#include <string.h> 1.19 + 1.20 +RCNetAddr::RCNetAddr(const RCNetAddr& his): RCBase() 1.21 + { address = his.address; } 1.22 + 1.23 +RCNetAddr::RCNetAddr(const RCNetAddr& his, PRUint16 port): RCBase() 1.24 +{ 1.25 + address = his.address; 1.26 + switch (address.raw.family) 1.27 + { 1.28 + case PR_AF_INET: address.inet.port = port; break; 1.29 + case PR_AF_INET6: address.ipv6.port = port; break; 1.30 + default: break; 1.31 + } 1.32 +} /* RCNetAddr::RCNetAddr */ 1.33 + 1.34 +RCNetAddr::RCNetAddr(RCNetAddr::HostValue host, PRUint16 port): RCBase() 1.35 +{ 1.36 + PRNetAddrValue how; 1.37 + switch (host) 1.38 + { 1.39 + case RCNetAddr::any: how = PR_IpAddrAny; break; 1.40 + case RCNetAddr::loopback: how = PR_IpAddrLoopback; break; 1.41 + default: PR_ASSERT(!"This can't happen -- and did!"); 1.42 + } 1.43 + (void)PR_InitializeNetAddr(how, port, &address); 1.44 +} /* RCNetAddr::RCNetAddr */ 1.45 + 1.46 +RCNetAddr::~RCNetAddr() { } 1.47 + 1.48 +void RCNetAddr::operator=(const RCNetAddr& his) { address = his.address; } 1.49 + 1.50 +PRStatus RCNetAddr::FromString(const char* string) 1.51 + { return PR_StringToNetAddr(string, &address); } 1.52 + 1.53 +void RCNetAddr::operator=(const PRNetAddr* addr) { address = *addr; } 1.54 + 1.55 +PRBool RCNetAddr::operator==(const RCNetAddr& his) const 1.56 +{ 1.57 + PRBool rv = EqualHost(his); 1.58 + if (rv) 1.59 + { 1.60 + switch (address.raw.family) 1.61 + { 1.62 + case PR_AF_INET: 1.63 + rv = (address.inet.port == his.address.inet.port); break; 1.64 + case PR_AF_INET6: 1.65 + rv = (address.ipv6.port == his.address.ipv6.port); break; 1.66 + case PR_AF_LOCAL: 1.67 + default: break; 1.68 + } 1.69 + } 1.70 + return rv; 1.71 +} /* RCNetAddr::operator== */ 1.72 + 1.73 +PRBool RCNetAddr::EqualHost(const RCNetAddr& his) const 1.74 +{ 1.75 + PRBool rv; 1.76 + switch (address.raw.family) 1.77 + { 1.78 + case PR_AF_INET: 1.79 + rv = (address.inet.ip == his.address.inet.ip); break; 1.80 + case PR_AF_INET6: 1.81 + rv = (0 == memcmp( 1.82 + &address.ipv6.ip, &his.address.ipv6.ip, 1.83 + sizeof(address.ipv6.ip))); 1.84 + break; 1.85 +#if defined(XP_UNIX) 1.86 + case PR_AF_LOCAL: 1.87 + rv = (0 == strncmp( 1.88 + address.local.path, his.address.local.path, 1.89 + sizeof(address.local.path))); 1.90 + break; 1.91 +#endif 1.92 + default: break; 1.93 + } 1.94 + return rv; 1.95 +} /* RCNetAddr::operator== */ 1.96 + 1.97 +PRStatus RCNetAddr::ToString(char *string, PRSize size) const 1.98 + { return PR_NetAddrToString(&address, string, size); } 1.99 + 1.100 +/* 1.101 +** RCHostLookup 1.102 +*/ 1.103 + 1.104 +RCHostLookup::~RCHostLookup() 1.105 +{ 1.106 + if (NULL != address) delete [] address; 1.107 +} /* RCHostLookup::~RCHostLookup */ 1.108 + 1.109 +RCHostLookup::RCHostLookup(): RCBase() 1.110 +{ 1.111 + address = NULL; 1.112 + max_index = 0; 1.113 +} /* RCHostLookup::RCHostLookup */ 1.114 + 1.115 +PRStatus RCHostLookup::ByName(const char* name) 1.116 +{ 1.117 + PRStatus rv; 1.118 + PRNetAddr addr; 1.119 + PRHostEnt hostentry; 1.120 + PRIntn index = 0, max; 1.121 + RCNetAddr* vector = NULL; 1.122 + RCNetAddr* old_vector = NULL; 1.123 + void* buffer = PR_Malloc(PR_NETDB_BUF_SIZE); 1.124 + if (NULL == buffer) return PR_FAILURE; 1.125 + rv = PR_GetHostByName(name, (char*)buffer, PR_NETDB_BUF_SIZE, &hostentry); 1.126 + if (PR_SUCCESS == rv) 1.127 + { 1.128 + for (max = 0, index = 0;; ++max) 1.129 + { 1.130 + index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); 1.131 + if (0 == index) break; 1.132 + } 1.133 + if (max > 0) 1.134 + { 1.135 + vector = new RCNetAddr[max]; 1.136 + while (--max > 0) 1.137 + { 1.138 + index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); 1.139 + if (0 == index) break; 1.140 + vector[index] = &addr; 1.141 + } 1.142 + { 1.143 + RCEnter entry(&ml); 1.144 + old_vector = address; 1.145 + address = vector; 1.146 + max_index = max; 1.147 + } 1.148 + if (NULL != old_vector) delete [] old_vector; 1.149 + } 1.150 + } 1.151 + if (NULL != buffer) PR_DELETE(buffer); 1.152 + return PR_SUCCESS; 1.153 +} /* RCHostLookup::ByName */ 1.154 + 1.155 +PRStatus RCHostLookup::ByAddress(const RCNetAddr& host_addr) 1.156 +{ 1.157 + PRStatus rv; 1.158 + PRNetAddr addr; 1.159 + PRHostEnt hostentry; 1.160 + PRIntn index = 0, max; 1.161 + RCNetAddr* vector = NULL; 1.162 + RCNetAddr* old_vector = NULL; 1.163 + char *buffer = (char*)PR_Malloc(PR_NETDB_BUF_SIZE); 1.164 + if (NULL == buffer) return PR_FAILURE; 1.165 + rv = PR_GetHostByAddr(host_addr, buffer, PR_NETDB_BUF_SIZE, &hostentry); 1.166 + if (PR_SUCCESS == rv) 1.167 + { 1.168 + for (max = 0, index = 0;; ++max) 1.169 + { 1.170 + index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); 1.171 + if (0 == index) break; 1.172 + } 1.173 + if (max > 0) 1.174 + { 1.175 + vector = new RCNetAddr[max]; 1.176 + while (--max > 0) 1.177 + { 1.178 + index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); 1.179 + if (0 == index) break; 1.180 + vector[index] = &addr; 1.181 + } 1.182 + { 1.183 + RCEnter entry(&ml); 1.184 + old_vector = address; 1.185 + address = vector; 1.186 + max_index = max; 1.187 + } 1.188 + if (NULL != old_vector) delete [] old_vector; 1.189 + } 1.190 + } 1.191 + if (NULL != buffer) PR_DELETE(buffer); 1.192 + return PR_SUCCESS; 1.193 +} /* RCHostLookup::ByAddress */ 1.194 + 1.195 +const RCNetAddr* RCHostLookup::operator[](PRUintn which) 1.196 +{ 1.197 + RCNetAddr* addr = NULL; 1.198 + if (which < max_index) 1.199 + addr = &address[which]; 1.200 + return addr; 1.201 +} /* RCHostLookup::operator[] */ 1.202 + 1.203 +/* RCNetdb.cpp */