Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | ** Base class implementation for network access functions (ref: prnetdb.h) |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | #include "rclock.h" |
michael@0 | 11 | #include "rcnetdb.h" |
michael@0 | 12 | |
michael@0 | 13 | #include <prmem.h> |
michael@0 | 14 | #include <prlog.h> |
michael@0 | 15 | #include <string.h> |
michael@0 | 16 | |
michael@0 | 17 | RCNetAddr::RCNetAddr(const RCNetAddr& his): RCBase() |
michael@0 | 18 | { address = his.address; } |
michael@0 | 19 | |
michael@0 | 20 | RCNetAddr::RCNetAddr(const RCNetAddr& his, PRUint16 port): RCBase() |
michael@0 | 21 | { |
michael@0 | 22 | address = his.address; |
michael@0 | 23 | switch (address.raw.family) |
michael@0 | 24 | { |
michael@0 | 25 | case PR_AF_INET: address.inet.port = port; break; |
michael@0 | 26 | case PR_AF_INET6: address.ipv6.port = port; break; |
michael@0 | 27 | default: break; |
michael@0 | 28 | } |
michael@0 | 29 | } /* RCNetAddr::RCNetAddr */ |
michael@0 | 30 | |
michael@0 | 31 | RCNetAddr::RCNetAddr(RCNetAddr::HostValue host, PRUint16 port): RCBase() |
michael@0 | 32 | { |
michael@0 | 33 | PRNetAddrValue how; |
michael@0 | 34 | switch (host) |
michael@0 | 35 | { |
michael@0 | 36 | case RCNetAddr::any: how = PR_IpAddrAny; break; |
michael@0 | 37 | case RCNetAddr::loopback: how = PR_IpAddrLoopback; break; |
michael@0 | 38 | default: PR_ASSERT(!"This can't happen -- and did!"); |
michael@0 | 39 | } |
michael@0 | 40 | (void)PR_InitializeNetAddr(how, port, &address); |
michael@0 | 41 | } /* RCNetAddr::RCNetAddr */ |
michael@0 | 42 | |
michael@0 | 43 | RCNetAddr::~RCNetAddr() { } |
michael@0 | 44 | |
michael@0 | 45 | void RCNetAddr::operator=(const RCNetAddr& his) { address = his.address; } |
michael@0 | 46 | |
michael@0 | 47 | PRStatus RCNetAddr::FromString(const char* string) |
michael@0 | 48 | { return PR_StringToNetAddr(string, &address); } |
michael@0 | 49 | |
michael@0 | 50 | void RCNetAddr::operator=(const PRNetAddr* addr) { address = *addr; } |
michael@0 | 51 | |
michael@0 | 52 | PRBool RCNetAddr::operator==(const RCNetAddr& his) const |
michael@0 | 53 | { |
michael@0 | 54 | PRBool rv = EqualHost(his); |
michael@0 | 55 | if (rv) |
michael@0 | 56 | { |
michael@0 | 57 | switch (address.raw.family) |
michael@0 | 58 | { |
michael@0 | 59 | case PR_AF_INET: |
michael@0 | 60 | rv = (address.inet.port == his.address.inet.port); break; |
michael@0 | 61 | case PR_AF_INET6: |
michael@0 | 62 | rv = (address.ipv6.port == his.address.ipv6.port); break; |
michael@0 | 63 | case PR_AF_LOCAL: |
michael@0 | 64 | default: break; |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | return rv; |
michael@0 | 68 | } /* RCNetAddr::operator== */ |
michael@0 | 69 | |
michael@0 | 70 | PRBool RCNetAddr::EqualHost(const RCNetAddr& his) const |
michael@0 | 71 | { |
michael@0 | 72 | PRBool rv; |
michael@0 | 73 | switch (address.raw.family) |
michael@0 | 74 | { |
michael@0 | 75 | case PR_AF_INET: |
michael@0 | 76 | rv = (address.inet.ip == his.address.inet.ip); break; |
michael@0 | 77 | case PR_AF_INET6: |
michael@0 | 78 | rv = (0 == memcmp( |
michael@0 | 79 | &address.ipv6.ip, &his.address.ipv6.ip, |
michael@0 | 80 | sizeof(address.ipv6.ip))); |
michael@0 | 81 | break; |
michael@0 | 82 | #if defined(XP_UNIX) |
michael@0 | 83 | case PR_AF_LOCAL: |
michael@0 | 84 | rv = (0 == strncmp( |
michael@0 | 85 | address.local.path, his.address.local.path, |
michael@0 | 86 | sizeof(address.local.path))); |
michael@0 | 87 | break; |
michael@0 | 88 | #endif |
michael@0 | 89 | default: break; |
michael@0 | 90 | } |
michael@0 | 91 | return rv; |
michael@0 | 92 | } /* RCNetAddr::operator== */ |
michael@0 | 93 | |
michael@0 | 94 | PRStatus RCNetAddr::ToString(char *string, PRSize size) const |
michael@0 | 95 | { return PR_NetAddrToString(&address, string, size); } |
michael@0 | 96 | |
michael@0 | 97 | /* |
michael@0 | 98 | ** RCHostLookup |
michael@0 | 99 | */ |
michael@0 | 100 | |
michael@0 | 101 | RCHostLookup::~RCHostLookup() |
michael@0 | 102 | { |
michael@0 | 103 | if (NULL != address) delete [] address; |
michael@0 | 104 | } /* RCHostLookup::~RCHostLookup */ |
michael@0 | 105 | |
michael@0 | 106 | RCHostLookup::RCHostLookup(): RCBase() |
michael@0 | 107 | { |
michael@0 | 108 | address = NULL; |
michael@0 | 109 | max_index = 0; |
michael@0 | 110 | } /* RCHostLookup::RCHostLookup */ |
michael@0 | 111 | |
michael@0 | 112 | PRStatus RCHostLookup::ByName(const char* name) |
michael@0 | 113 | { |
michael@0 | 114 | PRStatus rv; |
michael@0 | 115 | PRNetAddr addr; |
michael@0 | 116 | PRHostEnt hostentry; |
michael@0 | 117 | PRIntn index = 0, max; |
michael@0 | 118 | RCNetAddr* vector = NULL; |
michael@0 | 119 | RCNetAddr* old_vector = NULL; |
michael@0 | 120 | void* buffer = PR_Malloc(PR_NETDB_BUF_SIZE); |
michael@0 | 121 | if (NULL == buffer) return PR_FAILURE; |
michael@0 | 122 | rv = PR_GetHostByName(name, (char*)buffer, PR_NETDB_BUF_SIZE, &hostentry); |
michael@0 | 123 | if (PR_SUCCESS == rv) |
michael@0 | 124 | { |
michael@0 | 125 | for (max = 0, index = 0;; ++max) |
michael@0 | 126 | { |
michael@0 | 127 | index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); |
michael@0 | 128 | if (0 == index) break; |
michael@0 | 129 | } |
michael@0 | 130 | if (max > 0) |
michael@0 | 131 | { |
michael@0 | 132 | vector = new RCNetAddr[max]; |
michael@0 | 133 | while (--max > 0) |
michael@0 | 134 | { |
michael@0 | 135 | index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); |
michael@0 | 136 | if (0 == index) break; |
michael@0 | 137 | vector[index] = &addr; |
michael@0 | 138 | } |
michael@0 | 139 | { |
michael@0 | 140 | RCEnter entry(&ml); |
michael@0 | 141 | old_vector = address; |
michael@0 | 142 | address = vector; |
michael@0 | 143 | max_index = max; |
michael@0 | 144 | } |
michael@0 | 145 | if (NULL != old_vector) delete [] old_vector; |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | if (NULL != buffer) PR_DELETE(buffer); |
michael@0 | 149 | return PR_SUCCESS; |
michael@0 | 150 | } /* RCHostLookup::ByName */ |
michael@0 | 151 | |
michael@0 | 152 | PRStatus RCHostLookup::ByAddress(const RCNetAddr& host_addr) |
michael@0 | 153 | { |
michael@0 | 154 | PRStatus rv; |
michael@0 | 155 | PRNetAddr addr; |
michael@0 | 156 | PRHostEnt hostentry; |
michael@0 | 157 | PRIntn index = 0, max; |
michael@0 | 158 | RCNetAddr* vector = NULL; |
michael@0 | 159 | RCNetAddr* old_vector = NULL; |
michael@0 | 160 | char *buffer = (char*)PR_Malloc(PR_NETDB_BUF_SIZE); |
michael@0 | 161 | if (NULL == buffer) return PR_FAILURE; |
michael@0 | 162 | rv = PR_GetHostByAddr(host_addr, buffer, PR_NETDB_BUF_SIZE, &hostentry); |
michael@0 | 163 | if (PR_SUCCESS == rv) |
michael@0 | 164 | { |
michael@0 | 165 | for (max = 0, index = 0;; ++max) |
michael@0 | 166 | { |
michael@0 | 167 | index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); |
michael@0 | 168 | if (0 == index) break; |
michael@0 | 169 | } |
michael@0 | 170 | if (max > 0) |
michael@0 | 171 | { |
michael@0 | 172 | vector = new RCNetAddr[max]; |
michael@0 | 173 | while (--max > 0) |
michael@0 | 174 | { |
michael@0 | 175 | index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); |
michael@0 | 176 | if (0 == index) break; |
michael@0 | 177 | vector[index] = &addr; |
michael@0 | 178 | } |
michael@0 | 179 | { |
michael@0 | 180 | RCEnter entry(&ml); |
michael@0 | 181 | old_vector = address; |
michael@0 | 182 | address = vector; |
michael@0 | 183 | max_index = max; |
michael@0 | 184 | } |
michael@0 | 185 | if (NULL != old_vector) delete [] old_vector; |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | if (NULL != buffer) PR_DELETE(buffer); |
michael@0 | 189 | return PR_SUCCESS; |
michael@0 | 190 | } /* RCHostLookup::ByAddress */ |
michael@0 | 191 | |
michael@0 | 192 | const RCNetAddr* RCHostLookup::operator[](PRUintn which) |
michael@0 | 193 | { |
michael@0 | 194 | RCNetAddr* addr = NULL; |
michael@0 | 195 | if (which < max_index) |
michael@0 | 196 | addr = &address[which]; |
michael@0 | 197 | return addr; |
michael@0 | 198 | } /* RCHostLookup::operator[] */ |
michael@0 | 199 | |
michael@0 | 200 | /* RCNetdb.cpp */ |