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
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.
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)
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;
61 } /* RCNetAddr::RCNetAddr */
63 RCNetAddr::RCNetAddr(RCNetAddr::HostValue host
, PRUint16 port
): RCBase()
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
);
89 switch (address
.raw
.family
)
92 rv
= (address
.inet
.port
== his
.address
.inet
.port
); break;
94 rv
= (address
.ipv6
.port
== his
.address
.ipv6
.port
); break;
100 } /* RCNetAddr::operator== */
102 PRBool
RCNetAddr::EqualHost(const RCNetAddr
& his
) const
105 switch (address
.raw
.family
)
108 rv
= (address
.inet
.ip
== his
.address
.inet
.ip
); break;
111 &address
.ipv6
.ip
, &his
.address
.ipv6
.ip
,
112 sizeof(address
.ipv6
.ip
)));
117 address
.local
.path
, his
.address
.local
.path
,
118 sizeof(address
.local
.path
)));
124 } /* RCNetAddr::operator== */
126 PRStatus
RCNetAddr::ToString(char *string
, PRSize size
) const
127 { return PR_NetAddrToString(&address
, string
, size
); }
133 RCHostLookup::~RCHostLookup()
135 if (NULL
!= address
) delete [] address
;
136 } /* RCHostLookup::~RCHostLookup */
138 RCHostLookup::RCHostLookup(): RCBase()
142 } /* RCHostLookup::RCHostLookup */
144 PRStatus
RCHostLookup::ByName(const char* name
)
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;
164 vector
= new RCNetAddr
[max
];
167 index
= PR_EnumerateHostEnt(index
, &hostentry
, 0, &addr
);
168 if (0 == index
) break;
169 vector
[index
] = &addr
;
173 old_vector
= address
;
177 if (NULL
!= old_vector
) delete [] old_vector
;
180 if (NULL
!= buffer
) PR_DELETE(buffer
);
182 } /* RCHostLookup::ByName */
184 PRStatus
RCHostLookup::ByAddress(const RCNetAddr
& host_addr
)
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;
204 vector
= new RCNetAddr
[max
];
207 index
= PR_EnumerateHostEnt(index
, &hostentry
, 0, &addr
);
208 if (0 == index
) break;
209 vector
[index
] = &addr
;
213 old_vector
= address
;
217 if (NULL
!= old_vector
) delete [] old_vector
;
220 if (NULL
!= buffer
) PR_DELETE(buffer
);
222 } /* RCHostLookup::ByAddress */
224 const RCNetAddr
* RCHostLookup::operator[](PRUintn which
)
226 RCNetAddr
* addr
= NULL
;
227 if (which
< max_index
)
228 addr
= &address
[which
];
230 } /* RCHostLookup::operator[] */