WvStreams
include/wvaddr.h
1/* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * Device-independent and device-specific hardware/protocol address classes
6 * that can store themselves efficiently as well as create a printable string
7 * version of themselves.
8 */
9#ifndef __WVADDR_H
10#define __WVADDR_H
11
12#include "wvautoconf.h"
13#include "if_arp.h" /* To define ARPHRD_.* */
14
15
16#include <stdio.h>
17#if HAVE_SYS_TYPES_H
18# include <sys/types.h>
19#endif
20#if STDC_HEADERS
21# include <stdlib.h>
22# include <stddef.h>
23#else
24# if HAVE_STDLIB_H
25# include <stdlib.h>
26# endif
27#endif
28#if HAVE_SYS_SOCKET_H
29# include <sys/socket.h>
30#endif
31#if HAVE_NET_ETHERNET_H
32# include <net/ethernet.h>
33#endif
34#if HAVE_NET_IF_H
35# include <net/if.h>
36#endif
37#if HAVE_NETINET_IF_ETHER_H
38# include <netinet/if_ether.h>
39#endif
40#if HAVE_NETINET_IN_H
41# include <netinet/in.h>
42#endif
43
44#ifdef _WIN32
45#include <winsock2.h>
46#define ETHER_ADDR_LEN 6
47#define IP_ALEN 4
48#endif
49#ifdef _MSC_VER
50typedef unsigned int uint32_t;
51typedef unsigned short uint16_t;
52#endif
53
54#include "wvstring.h"
55
56static const char * type_wvaddr = "WvAddr";
57static const char * type_wvipaddr = "WvIPAddr";
58static const char * type_wvipnet = "WvIPNet";
59static const char * type_wvipportaddr = "WvIPPortAddr";
60
61#define WVADDR type_wvaddr
62#define WVIPADDR type_wvipaddr
63#define WVIPNET type_wvipnet
64#define WVIPPORTADDR type_wvipportaddr
65
66
72class WvEncap
73{
74 static const char strings[][20]; // printable-string names per type
75 static int extypes[]; // external types (ARPHRD_*, etc)
76public:
77 // NOTE: if you change enum CapType, don't forget to change extypes[]
78 // and strings[] in wvaddr.cc!
79 enum CapType {
80 // hardware encapsulation
81 Unknown = 0,
82 Loopback,
83 Ethertap,
84 Ethernet,
85 ARCnet,
86 SLIP,
87 CSLIP,
88 PPP,
89 IPsec,
90
91 // protocol encapsulation
92 IPv4,
93 Unix,
94
95 // END
96 NUM_ENCAP_TYPES
97 };
98 CapType cap;
99
100 WvEncap(CapType _cap = Unknown)
101 { cap = _cap; }
102
103 WvEncap(int extype);
104
105 operator CapType () const
106 { return cap; }
107
108 operator WvString () const
109 { return strings[cap]; }
110};
111
112
118class WvAddr
119{
120protected:
121 virtual WvString printable() const = 0;
122
123public:
124 WvAddr() {};
125 virtual ~WvAddr() {};
126 static WvAddr *gen(struct sockaddr *addr);
127
128 virtual WvEncap encap() const = 0;
129 operator WvString() const
130 { return printable(); }
131
132 virtual bool comparator(const WvAddr *a2, bool first_pass = true) const;
133 // Poor man's rtti
134 virtual const char *type() const
135 { return WVADDR; };
136
137 virtual bool isbroadcast() const;
138
139 virtual struct sockaddr *sockaddr() const = 0;
140 virtual size_t sockaddr_len() const = 0;
141 virtual const unsigned char *rawdata() const;
142 virtual size_t rawdata_len() const;
143
144 virtual unsigned WvHash() const;
145
146 bool operator== (const WvAddr &a2) const
147 { return comparator(&a2); }
148 bool operator!= (const WvAddr &a2) const
149 { return ! (*this == a2); }
150};
151
152
153// useful for hash tables (see wvhashtable.h)
154unsigned WvHash(const WvAddr &addr);
155
156
161class WvStringAddr : public WvAddr
162{
163 WvString addr;
164 WvEncap cap;
165
166protected:
167 virtual WvString printable() const;
168
169public:
170 WvStringAddr(WvStringParm s, const WvEncap &_cap);
171 WvStringAddr(const struct sockaddr *_addr);
172 virtual ~WvStringAddr();
173 virtual WvEncap encap() const;
174 virtual struct sockaddr *sockaddr() const;
175 virtual size_t sockaddr_len() const;
176 virtual const unsigned char *rawdata() const;
177 virtual size_t rawdata_len() const;
178};
179
180
181#ifndef _WIN32
186class WvEtherAddr : public WvAddr
187{
188 unsigned char binaddr[ETHER_ADDR_LEN];
189
190protected:
191 virtual WvString printable() const;
192
193public:
194 WvEtherAddr(const unsigned char _binaddr[ETHER_ADDR_LEN] = NULL)
195 { if (_binaddr) memcpy(binaddr, _binaddr, ETHER_ADDR_LEN); }
196 WvEtherAddr(const char string[])
197 { string_init(string); }
199 { string_init(string); }
200 void string_init(const char string[]);
201 WvEtherAddr(const struct sockaddr *addr)
202 { memcpy(binaddr, (void *)addr->sa_data, ETHER_ADDR_LEN); }
203 virtual ~WvEtherAddr();
204
205 virtual WvEncap encap() const;
206 virtual bool isbroadcast() const;
207 virtual struct sockaddr *sockaddr() const;
208 virtual size_t sockaddr_len() const;
209 virtual const unsigned char *rawdata() const;
210 virtual size_t rawdata_len() const;
211};
212
213
215class WvARCnetAddr : public WvAddr
216{
217 unsigned char binaddr;
218
219protected:
220 virtual WvString printable() const;
221
222public:
223 WvARCnetAddr(const unsigned char _binaddr[1] = NULL)
224 { if (_binaddr) binaddr = _binaddr[0]; }
225 WvARCnetAddr(const char string[])
226 { binaddr = strtoul(string, NULL, 16); }
228 { binaddr = strtoul(string, NULL, 16); }
229 WvARCnetAddr(const struct sockaddr *addr)
230 { binaddr = ((unsigned char *)addr->sa_data)[0]; }
231 virtual ~WvARCnetAddr();
232
233 virtual WvEncap encap() const;
234 virtual struct sockaddr *sockaddr() const;
235 virtual size_t sockaddr_len() const;
236 virtual const unsigned char *rawdata() const;
237 virtual size_t rawdata_len() const;
238};
239#endif // !_WIN32
240
249class WvIPAddr : public WvAddr
250{
251protected:
252 virtual WvString printable() const;
253public:
254 unsigned char binaddr[4];
255
256 WvIPAddr(const unsigned char *_binaddr)
257 { if (_binaddr) memcpy(binaddr, _binaddr, 4); }
258 WvIPAddr(const uint32_t _binaddr = 0)
259 { memcpy(binaddr, &_binaddr, 4); }
260 WvIPAddr(const char string[])
261 { string_init(string); }
262 WvIPAddr(WvStringParm string)
263 { string_init(string); }
264 void string_init(const char string[]);
265 WvIPAddr(const struct sockaddr *addr)
266 { memcpy(binaddr,
267 (void *)&((struct sockaddr_in *)addr)->sin_addr.s_addr, 4); }
268 WvIPAddr(const WvIPAddr &_addr)
269 { memcpy(binaddr, _addr.binaddr, 4); }
270 virtual ~WvIPAddr();
271
272 virtual bool comparator(const WvAddr *a2, bool first_pass = true) const;
273 virtual const char *type() const
274 { return WVIPADDR; };
275
276 WvIPAddr operator& (const WvIPAddr &a2) const;
277 WvIPAddr operator| (const WvIPAddr &a2) const;
278 WvIPAddr operator^ (const WvIPAddr &a2) const;
279 WvIPAddr operator~ () const;
280 WvIPAddr operator+ (int n) const;
281 WvIPAddr operator- (int n) const;
282
283 uint32_t addr() const
284 { return *(uint32_t *)binaddr; }
285
286 bool is_zero() const
287 { return addr() == 0; }
288
289 virtual WvEncap encap() const;
290
291 virtual struct sockaddr *sockaddr() const;
292 virtual size_t sockaddr_len() const;
293 virtual const unsigned char *rawdata() const;
294 virtual size_t rawdata_len() const;
295};
296
297
312class WvIPNet : public WvIPAddr
313{
314protected:
315 WvIPAddr mask;
316 virtual WvString printable() const;
317
318public:
319 WvIPNet(const WvIPNet &_net);
320 WvIPNet(const char string[]) : WvIPAddr(string)
321 { string_init(string); }
322 WvIPNet(WvStringParm string) : WvIPAddr(string)
323 { string_init(string); }
324 void string_init(const char string[]);
325 WvIPNet(const WvIPAddr &base, const WvIPAddr &_mask);
326
327 virtual bool comparator(const WvAddr *a2, bool first_pass = true) const;
328 virtual const char *type() const
329 { return WVIPNET; };
330
336 WvIPNet(const WvIPAddr &base, int bits = 32);
337
340
341 virtual ~WvIPNet();
342
344 virtual unsigned WvHash() const;
345
348 { return *this; }
349 WvIPAddr netmask() const
350 { return mask; }
351 WvIPAddr network() const
352 { return *this & mask; }
353 WvIPAddr broadcast() const
354 { return *this | ~mask; }
355
357 void include(const WvIPNet &addr);
358
360 bool includes(const WvIPNet &addr) const;
361
372 int bits() const;
373 void normalize();
374
376 bool is_default() const
377 { return mask.binaddr[0] == 0; }
378
380 bool is_host() const
381 { return mask.binaddr[3] == 255; }
382};
383
384
385
393class WvIPPortAddr : public WvIPAddr
394{
395protected:
396 virtual WvString printable() const;
397
398public:
399 uint16_t port;
400
401 WvIPPortAddr();
402 WvIPPortAddr(const unsigned char _ipaddr[4], uint16_t _port = 0)
403 : WvIPAddr(_ipaddr), port(_port) { };
404 WvIPPortAddr(const WvIPAddr &_ipaddr, uint16_t _port = 0);
405 WvIPPortAddr(const char string[]) : WvIPAddr(string)
406 { string_init(string); }
407 WvIPPortAddr(WvStringParm string) : WvIPAddr(string)
408 { string_init(string); }
409 void string_init(const char string[]);
410 WvIPPortAddr(uint16_t _port); // assumes address 0.0.0.0, (ie local)
411 WvIPPortAddr(const char string[], uint16_t _port);
412
413 WvIPPortAddr(struct sockaddr_in *sin) : WvIPAddr(sin->sin_addr.s_addr)
414 { port = ntohs(sin->sin_port); }
415 virtual ~WvIPPortAddr();
416
417 virtual bool comparator(const WvAddr *a2, bool first_pass = true) const;
418 virtual const char *type() const
419 { return WVIPPORTADDR; };
420
421 virtual struct sockaddr *sockaddr() const;
422
423 // Override the hash and comparison functions
424 virtual unsigned WvHash() const;
425};
426
427#ifndef _WIN32
429class WvUnixAddr : public WvAddr
430{
431protected:
432 WvString sockname;
433 virtual WvString printable() const;
434
435public:
436 WvUnixAddr(const char *_sockname);
437 WvUnixAddr(WvStringParm _sockname);
438 WvUnixAddr(const WvUnixAddr &_addr);
439 virtual ~WvUnixAddr();
440
441 virtual WvEncap encap() const;
442
443 virtual struct sockaddr *sockaddr() const;
444 virtual size_t sockaddr_len() const;
445 virtual const unsigned char *rawdata() const;
446 virtual size_t rawdata_len() const;
447};
448
449#endif //windows
450#endif // __WVADDR_H
An ARCnet address is made up of a single hex number.
Base class for different address types, each of which will have the ability to convert itself to/from...
Common packet encapsulation types, with the ability to convert a Linux ARPHRD_* value or (struct sock...
An ethernet address is made up of a string of hex numbers, in the form AA:BB:CC:DD:EE:FF.
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
An IP address is made up of a "dotted quad" – four decimal numbers in the form www....
An IP network comprises two WvIPAddr structures: an address and a netmask.
bool includes(const WvIPNet &addr) const
determine whether the given address is already included in this net
void include(const WvIPNet &addr)
adjust the netmask so that 'addr' would be included in this network
WvIPNet()
construct an empty IPNet for later copying (probably by operator=)
Definition wvaddr.cc:534
WvIPNet(const WvIPAddr &base, int bits=32)
construct an IPNet from a base address and a number of bits in the netmask.
int bits() const
weird netmasks such as 255.0.255.0 (easy example) are almost never used – they have '0' bits in the m...
Definition wvaddr.cc:632
virtual unsigned WvHash() const
Override the hash and comparison functions.
WvIPAddr base() const
Get the 'base IP address' component, netmask, network, and broadcast.
bool is_default() const
is this net the default gateway? (0.0.0.0/0)
bool is_host() const
is it a plain host? (x.x.x.x/32)
An IP+Port address also includes a port number, with the resulting form www.xxx.yyy....
A WvAddr that simply contains a printable string with a user-defined encapsulation type.
WvString is an implementation of a simple and efficient printable-string class.
A Unix domain socket address is really just a filename.