]>
Commit | Line | Data |
---|---|---|
bac41a7b A |
1 | /* |
2 | * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved. | |
3 | * | |
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 | |
8 | * using this file. | |
9 | * | |
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. | |
16 | */ | |
17 | ||
18 | ||
19 | // | |
20 | // hosts - value-semantics host identifier class | |
21 | // | |
22 | #include "hosts.h" | |
23 | #include <arpa/inet.h> | |
24 | #include <netdb.h> | |
25 | ||
26 | ||
27 | namespace Security { | |
28 | namespace IPPlusPlus { | |
29 | ||
30 | ||
31 | class NamedHost : public Host::Spec { | |
32 | public: | |
33 | NamedHost(const char *name); | |
34 | ||
35 | string name() const; | |
36 | set<IPAddress> addresses() const; | |
37 | ||
38 | bool operator == (const NamedHost &other) const | |
39 | { return mName == other.mName; } | |
40 | ||
41 | private: | |
42 | string mName; | |
43 | set<IPAddress> mAddrs; | |
44 | }; | |
45 | ||
46 | ||
47 | class IPv4NumberHost : public Host::Spec { | |
48 | public: | |
49 | IPv4NumberHost(IPAddress addr) : mAddr(addr) { } | |
50 | ||
51 | string name() const; | |
52 | set<IPAddress> addresses() const; | |
53 | ||
54 | bool operator == (const IPv4NumberHost &other) const | |
55 | { return mAddr == other.mAddr; } | |
56 | ||
57 | private: | |
58 | IPAddress mAddr; | |
59 | }; | |
60 | ||
61 | ||
62 | // | |
63 | // Host basics | |
64 | // | |
65 | Host::Host(const char *form) | |
66 | { | |
67 | //@@@ IPv4 only at this time | |
68 | IPAddress addr; | |
69 | if (inet_aton(form, &addr)) | |
70 | mSpec = new IPv4NumberHost(addr); | |
71 | else | |
72 | mSpec = new NamedHost(form); | |
73 | } | |
74 | ||
75 | ||
76 | // | |
77 | // Compare for equality | |
78 | // | |
79 | bool Host::operator == (const Host &other) const | |
80 | { | |
81 | // really silly hack alert: just compare lexicographically by name | |
82 | return mSpec ? (name() == other.name()) : !other.mSpec; | |
83 | } | |
84 | ||
85 | bool Host::operator < (const Host &other) const | |
86 | { | |
87 | // really silly hack alert: just compare lexicographically by name | |
88 | return !mSpec || (other.mSpec && name() < other.name()); | |
89 | } | |
90 | ||
91 | ||
92 | // | |
93 | // Compare for subsumption | |
94 | // | |
95 | bool Host::operator <= (const Host &other) const | |
96 | { | |
97 | return false; | |
98 | } | |
99 | ||
100 | ||
101 | // | |
102 | // IPv4 address host specs (a single IPv4 address) | |
103 | // | |
104 | string IPv4NumberHost::name() const | |
105 | { | |
106 | return mAddr; | |
107 | } | |
108 | ||
109 | set<IPAddress> IPv4NumberHost::addresses() const | |
110 | { | |
111 | set<IPAddress> result; | |
112 | result.insert(mAddr); | |
113 | return result; | |
114 | } | |
115 | ||
116 | ||
117 | // | |
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. | |
120 | // | |
121 | NamedHost::NamedHost(const char *name) : mName(name) | |
122 | { | |
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 | debug("ipname", "host %s resolves to %ld address(es)", mName.c_str(), mAddrs.size()); | |
128 | return; | |
129 | } | |
130 | UnixError::throwMe(ENOENT); //@@@ h_errno translation or other source | |
131 | } | |
132 | ||
133 | string NamedHost::name() const | |
134 | { | |
135 | return mName; | |
136 | } | |
137 | ||
138 | set<IPAddress> NamedHost::addresses() const | |
139 | { | |
140 | return mAddrs; | |
141 | } | |
142 | ||
143 | ||
144 | } // end namespace IPPlusPlus | |
145 | } // end namespace Security |