]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_utilities/lib/hosts.cpp
2 * Copyright (c) 2000-2001,2003-2004,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // hosts - value-semantics host identifier class
29 #include <arpa/inet.h>
34 namespace IPPlusPlus
{
37 class NamedHost
: public Host::Spec
{
39 NamedHost(const char *name
);
42 set
<IPAddress
> addresses() const;
44 bool operator == (const NamedHost
&other
) const
45 { return mName
== other
.mName
; }
49 set
<IPAddress
> mAddrs
;
53 class IPv4NumberHost
: public Host::Spec
{
55 IPv4NumberHost(IPAddress addr
) : mAddr(addr
) { }
58 set
<IPAddress
> addresses() const;
60 bool operator == (const IPv4NumberHost
&other
) const
61 { return mAddr
== other
.mAddr
; }
71 Host::Host(const char *form
)
73 //@@@ IPv4 only at this time
75 if (inet_aton(form
, &addr
))
76 mSpec
= new IPv4NumberHost(addr
);
78 mSpec
= new NamedHost(form
);
83 // Compare for equality
85 bool Host::operator == (const Host
&other
) const
87 // really silly hack alert: just compare lexicographically by name
88 return mSpec
? (name() == other
.name()) : !other
.mSpec
;
91 bool Host::operator < (const Host
&other
) const
93 // really silly hack alert: just compare lexicographically by name
94 return !mSpec
|| (other
.mSpec
&& name() < other
.name());
99 // Compare for subsumption
101 bool Host::operator <= (const Host
&other
) const
108 // IPv4 address host specs (a single IPv4 address)
110 string
IPv4NumberHost::name() const
115 set
<IPAddress
> IPv4NumberHost::addresses() const
117 set
<IPAddress
> result
;
118 result
.insert(mAddr
);
124 // IPv4 hostname host specs (a set of addresses derived from a name lookup)
125 // @@@ If we want to support IPv6, this should ALSO contain IPv6 lookup results.
127 NamedHost::NamedHost(const char *name
) : mName(name
)
129 //@@@ NOT THREAD SAFE - find another way to do name resolution
130 if (hostent
*he
= gethostbyname(name
)) {
131 for (char **p
= he
->h_addr_list
; *p
; p
++)
132 mAddrs
.insert(*reinterpret_cast<in_addr
*>(*p
));
133 secdebug("ipname", "host %s resolves to %ld address(es)", mName
.c_str(), mAddrs
.size());
136 UnixError::throwMe(ENOENT
); //@@@ h_errno translation or other source
139 string
NamedHost::name() const
144 set
<IPAddress
> NamedHost::addresses() const
150 } // end namespace IPPlusPlus
151 } // end namespace Security