Debian Codegraph @

protolib/include/protoAddress.h

raw
1 #ifndef _PROTO_ADDRESS
2 #define _PROTO_ADDRESS
3 // This is an attempt at some generic network address generalization
4 // along with some methods for standard address lookup/ manipulation.
5 // This will evolve as we actually support address types besides IPv4
6
7 #include "protoDefs.h"
8 #include "protoTree.h"
9 #include "protoDebug.h"
10
11 #ifdef UNIX
12 #include <sys/types.h>
13 #include <netdb.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <string.h>
18 #ifndef INADDR_NONE
19 const unsigned long INADDR_NONE = 0xffffffff;
20 #endif // !INADDR_NONE
21 #endif // UNIX
22
23 #ifdef WIN32
24 #include <winsock2.h>
25 #include <ws2tcpip.h>
26 #ifdef HAVE_IPV6
27 #ifndef _SS_PAD1SIZE
28 #include "tpipv6.h" // not in older Platform SDKs
29 #endif // !_SS_PAD1SIZE
30 #endif // HAVE_IPV6
31 #endif // WIN32
32
33 #ifdef SIMULATE
34
35 #ifdef NS2
36 #include "config.h" // for nsaddr_t which is a 32 bit int?
37 typedef nsaddr_t SIMADDR;
38 #endif // NS2
39
40 #ifdef NS3
41 #error "ns-3 work in progress"
42 #endif // NS3
43
44 #ifdef OPNET
45 #include "opnet.h"
46 #include "ip_addr_v4.h"
47 typedef IpT_Address SIMADDR;
48 extern IpT_Address IPI_BroadcastAddr;
49 #endif // OPNET
50
51 #ifndef _SOCKADDRSIM
52 #define _SOCKADDRSIM
53 struct sockaddr_sim
54 {
55 SIMADDR addr;
56 unsigned short port;
57 };
58 #endif // ! _SOCKADDRSIM
59
60 #endif // SIMULATE
61
62 #ifdef HAVE_IPV6
63 // returns IPv4 portion of mapped address in network byte order
64 inline unsigned long IN6_V4MAPPED_ADDR(struct in6_addr* a) {return (((UINT32*)a)[3]);}
65 #endif // HAVE_IPV6
66
67 /**
68 * @class ProtoAddress
69 *
70 * @brief Network address container class with support
71 * for IPv4, IPv6, and "SIM" address types. Also
72 * includes functions for name/address
73 * resolution.
74 */
75
76 #ifdef INVALID
77 #undef INVALID
78 #endif
79
80 class ProtoAddress
81 {
82 public:
83 enum Type
84 {
85 INVALID,
86 IPv4,
87 IPv6,
88 ETH,
89 SIM
90 };
91 // Construction/initialization
92 ProtoAddress();
93 ProtoAddress(const ProtoAddress& theAddr);
94 ProtoAddress(const char* theAddr); // must be a numeric address, not a host name!
95 ~ProtoAddress();
96 bool IsValid() const {return (INVALID != type);}
97 void Invalidate()
98 {
99 type = INVALID;
100 length = 0;
101 }
102 void Reset(ProtoAddress::Type theType, bool zero = true);
103
104
105 static UINT8 GetLength(Type type)
106 {
107 switch (type)
108 {
109 case IPv4:
110 return 4;
111 case IPv6:
112 return 16;
113 case ETH:
114 return 6;
115 #ifdef SIMULATE
116 case SIM:
117 return sizeof(SIMADDR);
118 #endif // SIMULATE
119 default:
120 return 0;
121 }
122 }
123
124 // Address info
125 ProtoAddress::Type GetType() const {return type;}
126 UINT8 GetLength() const {return length;}
127 bool IsBroadcast() const;
128 bool IsMulticast() const;
129 bool IsLoopback() const;
130 bool IsSiteLocal() const;
131 bool IsLinkLocal() const;
132 bool IsUnspecified() const;
133 bool IsUnicast() const
134 {return (!IsMulticast() && !IsBroadcast() && !IsUnspecified());}
135
136 const char* GetHostString(char* buffer = NULL,
137 unsigned int buflen = 0) const;
138
139 // This infers the ProtoAddress::Type from length (in bytes)
140 static ProtoAddress::Type GetType(UINT8 addrLength);
141
142 // Host address/port get/set
143 const char* GetRawHostAddress() const;
144 bool SetRawHostAddress(ProtoAddress::Type theType,
145 const char* buffer,
146 UINT8 bufferLen);
147 const struct sockaddr& GetSockAddr() const
148 {return ((const struct sockaddr&)addr);}
149 #ifdef HAVE_IPV6
150 const struct sockaddr_storage& GetSockAddrStorage() const
151 {return addr;}
152 #endif // HAVE_IPV6
153 bool SetSockAddr(const struct sockaddr& theAddr);
154 struct sockaddr& AccessSockAddr()
155 {return ((struct sockaddr&)addr);}
156 UINT16 GetPort() const;
157 void SetPort(UINT16 thePort);
158 void PortSet(UINT16 thePort);
159
160 // Address comparison
161 bool IsEqual(const ProtoAddress& theAddr) const
162 {return (HostIsEqual(theAddr) && (GetPort() == theAddr.GetPort()));}
163 bool HostIsEqual(const ProtoAddress& theAddr) const;
164 int CompareHostAddr(const ProtoAddress& theAddr) const;
165 bool operator==(const ProtoAddress &theAddr) const {return IsEqual(theAddr);}
166 bool operator!=(const ProtoAddress &theAddr) const {return !IsEqual(theAddr);}
167 bool operator>(const ProtoAddress &theAddr) const {return (CompareHostAddr(theAddr)>0);}
168 bool operator<(const ProtoAddress &theAddr) const {return (CompareHostAddr(theAddr)<0);}
169 bool operator>=(const ProtoAddress &theAddr) const {return !(*this<theAddr);}
170 bool operator<=(const ProtoAddress &theAddr) const {return !(*this>theAddr);}
171
172 unsigned int SetCommonHead(const ProtoAddress &theAddr);
173 unsigned int SetCommonTail(const ProtoAddress &theAddr);
174
175
176 bool PrefixIsEqual(const ProtoAddress& theAddr, UINT8 prefixLen) const;
177 UINT8 GetPrefixLength() const; // finds length (in bits) of non-zero prefix
178 void GeneratePrefixMask(ProtoAddress::Type theType, UINT8 prefixLen);
179 void ApplyPrefixMask(UINT8 prefixLen);
180 void ApplySuffixMask(UINT8 suffixLen);
181 void GetSubnetAddress(UINT8 prefixLen,
182 ProtoAddress& subnetAddr) const;
183 void GetBroadcastAddress(UINT8 prefixLen,
184 ProtoAddress& broadcastAddr) const;
185 // if you start at the subnet address, you can use
186 // this Increment() method to iterate over the range
187 // of subnet addresses, up to the broadcast address
188 bool Increment();
189
190 // Generates Ethernet mcast addr from IP mcast addr
191 ProtoAddress& GetEthernetMulticastAddress(const ProtoAddress& ipMcastAddr);
192
193 // Name/address resolution
194 bool ResolveFromString(const char* text);
195 bool ResolveToName(char* nameBuffer, unsigned int buflen) const;
196 bool ResolveLocalAddress(char* nameBuffer = NULL, unsigned int buflen = 0);
197
198 // Expects IPv4, IPv6, or ETH address in numeric notation form
199 bool ConvertFromString(const char* text);
200 // This expects a an "Ethernet" MAC address string
201 // in colon-delimited hexadecmial format
202 bool ResolveEthFromString(const char* text);
203
204 // Miscellaneous
205 // This function returns a 32-bit number which might _sometimes_
206 // be useful as an address-based end system identifier.
207 // (For IPv4 it's the address in host byte order)
208
209 UINT32 GetEndIdentifier() const;
210 UINT32 EndIdentifier() const // this is deprecated
211 {return GetEndIdentifier();}
212
213 // This lets us "manufacture" addresses from a 32-bit identifier value
214 // The default addrType is IPv4, but if the address already has
215 // a valid type, it is retained with the identifier applied
216 // such that GetEndIdentifier() will return the set value
217 void SetEndIdentifier(UINT32 endIdentifier);
218
219 // Return IPv4 address in host byte order
220 UINT32 IPv4GetAddress() const
221 {return (IPv4 == type) ? EndIdentifier() : 0;}
222 #ifdef SIMULATE
223 SIMADDR SimGetAddress() const
224 {
225 return ((SIM == type) ? ((struct sockaddr_sim*)&addr)->addr :
226 0);
227 }
228 void SimSetAddress(SIMADDR theAddr)
229 {
230 type = SIM;
231 length = sizeof(SIMADDR); // NSv2 addresses are 4 bytes? (32 bits)
232 ((struct sockaddr_sim*)&addr)->addr = theAddr;
233 }
234 #endif // SIMULATE
235 #ifdef WIN32
236 static bool Win32Startup()
237 {
238 WSADATA wsaData;
239 WORD wVersionRequested = MAKEWORD(2, 2);
240 return (0 == WSAStartup(wVersionRequested, &wsaData));
241 }
242 static void Win32Cleanup() {WSACleanup(); }
243 #endif // WIN32
244
245 private:
246 char* AccessRawHostAddress() const;
247
248 Type type;
249 UINT8 length;
250 #ifdef SIMULATE
251 struct sockaddr_sim addr; // for OPNET and NS2 builds
252 #else
253 #ifdef _WIN32_WCE
254 struct sockaddr_in addr; // WINCE doesn't like anything else???
255 #else
256 struct sockaddr_storage addr; // this is the _preferred_ way to build Protolib
257 #endif // if/else _WIN32_WCE
258 #endif // if/else SIMULATE
259 }; // end class ProtoAddress
260
261
262 /**
263 * @class ProtoAddressList
264 *
265 * @brief This "ProtoAddressList" helper class uses a Patricia
266 * ree (ProtoTree) to keep a listing of addresses. Note it keeps in
267 * own internal copy of addresses that are added to the list.
268 *
269 * An "Iterator" is also provided
270 * to go through the list when needed.
271 */
272 class ProtoAddressList
273 {
274 public:
275 ProtoAddressList();
276 ~ProtoAddressList();
277 void Destroy();
278 // Note the Insert() makes an internal copy of the addr passed in.
279 bool Insert(const ProtoAddress& addr, const void* userData = NULL);
280
281 bool Contains(const ProtoAddress& addr) const
282 {return (NULL != addr_tree.Find(addr.GetRawHostAddress(), addr.GetLength() << 3));}
283
284 const void* GetUserData(const ProtoAddress& addr) const
285 {
286 Item* entry = static_cast<Item*>(addr_tree.Find(addr.GetRawHostAddress(), addr.GetLength() << 3));
287 return (NULL != entry) ? entry->GetUserData() : NULL;
288 }
289 void Remove(const ProtoAddress& addr);
290
291 // Add (non-duplicative) / Remove addresses from input list
292 // (input list is not modified)
293 bool AddList(ProtoAddressList& addrList);
294 void RemoveList(ProtoAddressList& addrList);
295
296 bool IsEmpty() const
297 {return (addr_tree.IsEmpty());}
298
299 // Note this is not really a "first address" necessarily
300 bool GetFirstAddress(ProtoAddress& firstAddr) const;
301
302 class Iterator;
303 friend class Iterator;
304 class Iterator
305 {
306 public:
307 Iterator(ProtoAddressList& addrList);
308 ~Iterator();
309
310 void Reset() {ptree_iterator.Reset();}
311 bool GetNextAddress(ProtoAddress& nextAddr);
312 bool PeekNextAddress(ProtoAddress& nextAddr);
313
314 private:
315 ProtoTree::Iterator ptree_iterator;
316 }; // end class ProtoAddressList::Iterator
317
318 class Item : public ProtoTree::Item
319 {
320 public:
321 Item(const ProtoAddress& theAddr, const void* userData = NULL);
322 ~Item();
323
324 const ProtoAddress& GetAddress() const
325 {return addr;}
326
327 const void* GetUserData() const
328 {return user_data;}
329
330 // Use this with care (only when item is not in list)
331 void SetAddress(const ProtoAddress& theAddress)
332 {addr = theAddress;}
333
334 void SetUserData(const void* userData)
335 {user_data = userData;}
336
337 // Required ProtoTree::Item overrides
338 const char* GetKey() const {return addr.GetRawHostAddress();}
339 unsigned int GetKeysize() const {return (addr.GetLength() << 3);}
340
341 private:
342 ProtoAddress addr;
343 const void* user_data; // this will be deprecated!
344 }; // end class ProtoAddressList::Item
345
346 bool InsertItem(ProtoAddressList::Item& theItem)
347 {return addr_tree.Insert(theItem);}
348 void RemoveItem(ProtoAddressList::Item& theItem)
349 {addr_tree.Remove(theItem);}
350
351 Item* RemoveRootItem()
352 {return static_cast<Item*>(addr_tree.RemoveRoot());}
353
354 private:
355 ProtoTree addr_tree;
356 }; // end class ProtoAddressList
357
358 extern const ProtoAddress PROTO_ADDR_NONE; // invalid ProtoAddress (useful as a default)
359 extern const ProtoAddress PROTO_ADDR_BROADCAST; // Ethernet broadcast address
360
361 #endif // _PROTO_ADDRESS
362