nspr: import 3.0 RC1 cutoff from CVS
[mozilla-nspr.git] / nsprpub / pr / src / cplus / rcnetdb.cpp
blob9890c9aea84749ebc30097f6b045f0faa872cef3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 ** Base class implementation for network access functions (ref: prnetdb.h)
42 #include "rclock.h"
43 #include "rcnetdb.h"
45 #include <prmem.h>
46 #include <prlog.h>
47 #include <string.h>
49 RCNetAddr::RCNetAddr(const RCNetAddr& his): RCBase()
50 { address = his.address; }
52 RCNetAddr::RCNetAddr(const RCNetAddr& his, PRUint16 port): RCBase()
54 address = his.address;
55 switch (address.raw.family)
57 case PR_AF_INET: address.inet.port = port; break;
58 case PR_AF_INET6: address.ipv6.port = port; break;
59 default: break;
61 } /* RCNetAddr::RCNetAddr */
63 RCNetAddr::RCNetAddr(RCNetAddr::HostValue host, PRUint16 port): RCBase()
65 PRNetAddrValue how;
66 switch (host)
68 case RCNetAddr::any: how = PR_IpAddrAny; break;
69 case RCNetAddr::loopback: how = PR_IpAddrLoopback; break;
70 default: PR_ASSERT(!"This can't happen -- and did!");
72 (void)PR_InitializeNetAddr(how, port, &address);
73 } /* RCNetAddr::RCNetAddr */
75 RCNetAddr::~RCNetAddr() { }
77 void RCNetAddr::operator=(const RCNetAddr& his) { address = his.address; }
79 PRStatus RCNetAddr::FromString(const char* string)
80 { return PR_StringToNetAddr(string, &address); }
82 void RCNetAddr::operator=(const PRNetAddr* addr) { address = *addr; }
84 PRBool RCNetAddr::operator==(const RCNetAddr& his) const
86 PRBool rv = EqualHost(his);
87 if (rv)
89 switch (address.raw.family)
91 case PR_AF_INET:
92 rv = (address.inet.port == his.address.inet.port); break;
93 case PR_AF_INET6:
94 rv = (address.ipv6.port == his.address.ipv6.port); break;
95 case PR_AF_LOCAL:
96 default: break;
99 return rv;
100 } /* RCNetAddr::operator== */
102 PRBool RCNetAddr::EqualHost(const RCNetAddr& his) const
104 PRBool rv;
105 switch (address.raw.family)
107 case PR_AF_INET:
108 rv = (address.inet.ip == his.address.inet.ip); break;
109 case PR_AF_INET6:
110 rv = (0 == memcmp(
111 &address.ipv6.ip, &his.address.ipv6.ip,
112 sizeof(address.ipv6.ip)));
113 break;
114 #if defined(XP_UNIX)
115 case PR_AF_LOCAL:
116 rv = (0 == strncmp(
117 address.local.path, his.address.local.path,
118 sizeof(address.local.path)));
119 break;
120 #endif
121 default: break;
123 return rv;
124 } /* RCNetAddr::operator== */
126 PRStatus RCNetAddr::ToString(char *string, PRSize size) const
127 { return PR_NetAddrToString(&address, string, size); }
130 ** RCHostLookup
133 RCHostLookup::~RCHostLookup()
135 if (NULL != address) delete [] address;
136 } /* RCHostLookup::~RCHostLookup */
138 RCHostLookup::RCHostLookup(): RCBase()
140 address = NULL;
141 max_index = 0;
142 } /* RCHostLookup::RCHostLookup */
144 PRStatus RCHostLookup::ByName(const char* name)
146 PRStatus rv;
147 PRNetAddr addr;
148 PRHostEnt hostentry;
149 PRIntn index = 0, max;
150 RCNetAddr* vector = NULL;
151 RCNetAddr* old_vector = NULL;
152 void* buffer = PR_Malloc(PR_NETDB_BUF_SIZE);
153 if (NULL == buffer) return PR_FAILURE;
154 rv = PR_GetHostByName(name, (char*)buffer, PR_NETDB_BUF_SIZE, &hostentry);
155 if (PR_SUCCESS == rv)
157 for (max = 0, index = 0;; ++max)
159 index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr);
160 if (0 == index) break;
162 if (max > 0)
164 vector = new RCNetAddr[max];
165 while (--max > 0)
167 index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr);
168 if (0 == index) break;
169 vector[index] = &addr;
172 RCEnter entry(&ml);
173 old_vector = address;
174 address = vector;
175 max_index = max;
177 if (NULL != old_vector) delete [] old_vector;
180 if (NULL != buffer) PR_DELETE(buffer);
181 return PR_SUCCESS;
182 } /* RCHostLookup::ByName */
184 PRStatus RCHostLookup::ByAddress(const RCNetAddr& host_addr)
186 PRStatus rv;
187 PRNetAddr addr;
188 PRHostEnt hostentry;
189 PRIntn index = 0, max;
190 RCNetAddr* vector = NULL;
191 RCNetAddr* old_vector = NULL;
192 char *buffer = (char*)PR_Malloc(PR_NETDB_BUF_SIZE);
193 if (NULL == buffer) return PR_FAILURE;
194 rv = PR_GetHostByAddr(host_addr, buffer, PR_NETDB_BUF_SIZE, &hostentry);
195 if (PR_SUCCESS == rv)
197 for (max = 0, index = 0;; ++max)
199 index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr);
200 if (0 == index) break;
202 if (max > 0)
204 vector = new RCNetAddr[max];
205 while (--max > 0)
207 index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr);
208 if (0 == index) break;
209 vector[index] = &addr;
212 RCEnter entry(&ml);
213 old_vector = address;
214 address = vector;
215 max_index = max;
217 if (NULL != old_vector) delete [] old_vector;
220 if (NULL != buffer) PR_DELETE(buffer);
221 return PR_SUCCESS;
222 } /* RCHostLookup::ByAddress */
224 const RCNetAddr* RCHostLookup::operator[](PRUintn which)
226 RCNetAddr* addr = NULL;
227 if (which < max_index)
228 addr = &address[which];
229 return addr;
230 } /* RCHostLookup::operator[] */
232 /* RCNetdb.cpp */