]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/hosts.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // hosts - value-semantics host identifier class
23 #include <arpa/inet.h>
28 namespace IPPlusPlus
{
31 class NamedHost
: public Host::Spec
{
33 NamedHost(const char *name
);
36 set
<IPAddress
> addresses() const;
38 bool operator == (const NamedHost
&other
) const
39 { return mName
== other
.mName
; }
43 set
<IPAddress
> mAddrs
;
47 class IPv4NumberHost
: public Host::Spec
{
49 IPv4NumberHost(IPAddress addr
) : mAddr(addr
) { }
52 set
<IPAddress
> addresses() const;
54 bool operator == (const IPv4NumberHost
&other
) const
55 { return mAddr
== other
.mAddr
; }
65 Host::Host(const char *form
)
67 //@@@ IPv4 only at this time
69 if (inet_aton(form
, &addr
))
70 mSpec
= new IPv4NumberHost(addr
);
72 mSpec
= new NamedHost(form
);
77 // Compare for equality
79 bool Host::operator == (const Host
&other
) const
81 // really silly hack alert: just compare lexicographically by name
82 return mSpec
? (name() == other
.name()) : !other
.mSpec
;
85 bool Host::operator < (const Host
&other
) const
87 // really silly hack alert: just compare lexicographically by name
88 return !mSpec
|| (other
.mSpec
&& name() < other
.name());
93 // Compare for subsumption
95 bool Host::operator <= (const Host
&other
) const
102 // IPv4 address host specs (a single IPv4 address)
104 string
IPv4NumberHost::name() const
109 set
<IPAddress
> IPv4NumberHost::addresses() const
111 set
<IPAddress
> result
;
112 result
.insert(mAddr
);
118 // IPv4 hostname host specs (a set of addresses derived from a name lookup)
119 // @@@ If we want to support IPv6, this should ALSO contain IPv6 lookup results.
121 NamedHost::NamedHost(const char *name
) : mName(name
)
123 //@@@ NOT THREAD SAFE - find another way to do name resolution
124 if (hostent
*he
= gethostbyname(name
)) {
125 for (char **p
= he
->h_addr_list
; *p
; p
++)
126 mAddrs
.insert(*reinterpret_cast<in_addr
*>(*p
));
127 secdebug("ipname", "host %s resolves to %ld address(es)", mName
.c_str(), mAddrs
.size());
130 UnixError::throwMe(ENOENT
); //@@@ h_errno translation or other source
133 string
NamedHost::name() const
138 set
<IPAddress
> NamedHost::addresses() const
144 } // end namespace IPPlusPlus
145 } // end namespace Security