Back to index

lightning-sunbird  0.9+nobinonly
rcnetdb.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** Base class implementation for network access functions (ref: prnetdb.h)
00040 */
00041 
00042 #include "rclock.h"
00043 #include "rcnetdb.h"
00044 
00045 #include <prmem.h>
00046 #include <prlog.h>
00047 #include <string.h>
00048 
00049 RCNetAddr::RCNetAddr(const RCNetAddr& his): RCBase()
00050     { address = his.address; }
00051 
00052 RCNetAddr::RCNetAddr(const RCNetAddr& his, PRUint16 port): RCBase()
00053 {
00054     address = his.address;
00055     switch (address.raw.family)
00056     {
00057         case PR_AF_INET: address.inet.port = port; break;
00058         case PR_AF_INET6: address.ipv6.port = port; break;
00059         default: break;
00060     }
00061 }  /* RCNetAddr::RCNetAddr */
00062 
00063 RCNetAddr::RCNetAddr(RCNetAddr::HostValue host, PRUint16 port): RCBase()
00064 {
00065     PRNetAddrValue how;
00066     switch (host)
00067     {
00068         case RCNetAddr::any: how = PR_IpAddrAny; break;
00069         case RCNetAddr::loopback: how = PR_IpAddrLoopback; break;
00070         default: PR_ASSERT(!"This can't happen -- and did!");
00071     }
00072     (void)PR_InitializeNetAddr(how, port, &address);
00073 }  /* RCNetAddr::RCNetAddr */
00074 
00075 RCNetAddr::~RCNetAddr() { }
00076 
00077 void RCNetAddr::operator=(const RCNetAddr& his) { address = his.address; }
00078 
00079 PRStatus RCNetAddr::FromString(const char* string)
00080     { return PR_StringToNetAddr(string, &address); }
00081 
00082 void RCNetAddr::operator=(const PRNetAddr* addr) { address = *addr; }
00083 
00084 PRBool RCNetAddr::operator==(const RCNetAddr& his) const
00085 {
00086     PRBool rv = EqualHost(his);
00087     if (rv)
00088     {
00089         switch (address.raw.family)
00090         {
00091             case PR_AF_INET:
00092                 rv = (address.inet.port == his.address.inet.port); break;
00093             case PR_AF_INET6:
00094                 rv = (address.ipv6.port == his.address.ipv6.port); break;
00095             case PR_AF_LOCAL:
00096             default: break;
00097         }
00098     }
00099     return rv;
00100 }  /* RCNetAddr::operator== */
00101 
00102 PRBool RCNetAddr::EqualHost(const RCNetAddr& his) const
00103 {
00104     PRBool rv;
00105     switch (address.raw.family)
00106     {
00107         case PR_AF_INET:
00108             rv = (address.inet.ip == his.address.inet.ip); break;
00109         case PR_AF_INET6:
00110             rv = (0 == memcmp(
00111                 &address.ipv6.ip, &his.address.ipv6.ip,
00112                 sizeof(address.ipv6.ip)));
00113             break;
00114 #if defined(XP_UNIX)
00115         case PR_AF_LOCAL:
00116             rv = (0 == strncmp(
00117                 address.local.path, his.address.local.path,
00118                 sizeof(address.local.path)));
00119             break;
00120 #endif
00121         default: break;
00122     }
00123     return rv;
00124 }  /* RCNetAddr::operator== */
00125 
00126 PRStatus RCNetAddr::ToString(char *string, PRSize size) const
00127     { return PR_NetAddrToString(&address, string, size); }
00128 
00129 /*
00130 ** RCHostLookup
00131 */
00132 
00133 RCHostLookup::~RCHostLookup()
00134 {
00135     if (NULL != address) delete [] address;
00136 }  /* RCHostLookup::~RCHostLookup */
00137 
00138 RCHostLookup::RCHostLookup(): RCBase()
00139 {
00140     address = NULL;
00141     max_index = 0;
00142 }  /* RCHostLookup::RCHostLookup */
00143 
00144 PRStatus RCHostLookup::ByName(const char* name)
00145 {
00146     PRStatus rv;
00147     PRNetAddr addr;
00148     PRHostEnt hostentry;
00149     PRIntn index = 0, max;
00150     RCNetAddr* vector = NULL;
00151     RCNetAddr* old_vector = NULL;
00152     void* buffer = PR_Malloc(PR_NETDB_BUF_SIZE);
00153     if (NULL == buffer) return PR_FAILURE;
00154     rv = PR_GetHostByName(name, (char*)buffer, PR_NETDB_BUF_SIZE, &hostentry);
00155     if (PR_SUCCESS == rv)
00156     {
00157         for (max = 0, index = 0;; ++max)
00158         {
00159             index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr);
00160             if (0 == index) break;
00161         }
00162         if (max > 0)
00163         {
00164             vector = new RCNetAddr[max];
00165             while (--max > 0)
00166             {
00167                 index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr);
00168                 if (0 == index) break;
00169                 vector[index] = &addr;
00170             }
00171             {
00172                 RCEnter entry(&ml);
00173                 old_vector = address;
00174                 address = vector;
00175                 max_index = max;
00176             }
00177             if (NULL != old_vector) delete [] old_vector;
00178         }
00179     }
00180     if (NULL != buffer) PR_DELETE(buffer);
00181     return PR_SUCCESS;
00182 }  /* RCHostLookup::ByName */
00183 
00184 PRStatus RCHostLookup::ByAddress(const RCNetAddr& host_addr)
00185 {
00186     PRStatus rv;
00187     PRNetAddr addr;
00188     PRHostEnt hostentry;
00189     PRIntn index = 0, max;
00190     RCNetAddr* vector = NULL;
00191     RCNetAddr* old_vector = NULL;
00192     char *buffer = (char*)PR_Malloc(PR_NETDB_BUF_SIZE);
00193     if (NULL == buffer) return PR_FAILURE;
00194     rv = PR_GetHostByAddr(host_addr, buffer, PR_NETDB_BUF_SIZE, &hostentry);
00195     if (PR_SUCCESS == rv)
00196     {
00197         for (max = 0, index = 0;; ++max)
00198         {
00199             index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr);
00200             if (0 == index) break;
00201         }
00202         if (max > 0)
00203         {
00204             vector = new RCNetAddr[max];
00205             while (--max > 0)
00206             {
00207                 index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr);
00208                 if (0 == index) break;
00209                 vector[index] = &addr;
00210             }
00211             {
00212                 RCEnter entry(&ml);
00213                 old_vector = address;
00214                 address = vector;
00215                 max_index = max;
00216             }
00217             if (NULL != old_vector) delete [] old_vector;
00218         }
00219     }
00220     if (NULL != buffer) PR_DELETE(buffer);
00221     return PR_SUCCESS;
00222 }  /* RCHostLookup::ByAddress */
00223 
00224 const RCNetAddr* RCHostLookup::operator[](PRUintn which)
00225 {
00226     RCNetAddr* addr = NULL;
00227     if (which < max_index)
00228         addr = &address[which];
00229     return addr;
00230 }  /* RCHostLookup::operator[] */
00231 
00232 /* RCNetdb.cpp */