|
1 #ifndef NETWORK_SOCKADDR_H |
|
2 #define NETWORK_SOCKADDR_H |
|
3 |
|
4 #include "Address.hh" |
|
5 |
|
6 /* |
|
7 * Platform-specific includes |
|
8 */ |
|
9 #ifndef WIN32 |
|
10 // linux |
|
11 #include <netinet/in.h> |
|
12 #else |
|
13 #error "This network code won't compile on win32 :)" |
|
14 #endif |
|
15 |
|
16 |
|
17 /** |
|
18 * This represents a `struct sockaddr` as used by the socket API. |
|
19 * |
|
20 * It can be used like a NetworkEndpoint, but it's also suitable for use with recvfrom/sendto |
|
21 */ |
|
22 class NetworkAddress : public NetworkEndpoint { |
|
23 protected: |
|
24 /** |
|
25 * The machine-readable address |
|
26 */ |
|
27 sockaddr_storage address; |
|
28 |
|
29 /** |
|
30 * The address length |
|
31 */ |
|
32 socklen_t address_length; |
|
33 |
|
34 public: |
|
35 /** |
|
36 * Construct an empty SockAddr for later update() |
|
37 */ |
|
38 NetworkAddress (void); |
|
39 |
|
40 /** |
|
41 * Construct a NetworkAddress from a machine-readable address of the given length |
|
42 */ |
|
43 NetworkAddress (const sockaddr *addr, socklen_t len); |
|
44 |
|
45 /** |
|
46 * Get a const sockaddr* in this address |
|
47 * |
|
48 * @return read-only sockaddr pointer |
|
49 */ |
|
50 const sockaddr* get_sockaddr (void) const { return (const sockaddr *) &address; } |
|
51 |
|
52 /** |
|
53 * Get a mutable sockaddr* in this address |
|
54 * |
|
55 * @return writeable sockaddr pointer |
|
56 */ |
|
57 sockaddr* get_sockaddr (void) { return (sockaddr *) &address; } |
|
58 |
|
59 /** |
|
60 * Get the current address length |
|
61 * |
|
62 * @return address length |
|
63 */ |
|
64 socklen_t get_socklen (void) const { return address_length; } |
|
65 |
|
66 /** |
|
67 * Get a the address length pointer, initialized to the size of our sockaddr_storage. |
|
68 * |
|
69 * @return address length pointer |
|
70 */ |
|
71 socklen_t* get_socklen_ptr (void) { address_length = sizeof(address); return &address_length; } |
|
72 |
|
73 /** |
|
74 * Copy given sockaddr/len + update |
|
75 */ |
|
76 void set_sockaddr (const sockaddr *addr, socklen_t len); |
|
77 |
|
78 /** |
|
79 * Update internal state for NetworkAddress after sockaddr/socklen_ptr have been modified. |
|
80 */ |
|
81 void update (void); |
|
82 |
|
83 /** |
|
84 * Returns a "fake" addrinfo |
|
85 */ |
|
86 virtual addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const; |
|
87 |
|
88 /** |
|
89 * Free an addrinfo returned by get_addrinfo |
|
90 */ |
|
91 virtual void free_addrinfo (addrinfo *info) const; |
|
92 |
|
93 /** |
|
94 * Equal-to comparison operator. Invalid addresses compare equal and are always smaller |
|
95 */ |
|
96 bool operator== (const NetworkAddress &other) const { |
|
97 return (address_length == other.address_length) && memcmp(&address, &other.address, address_length) == 0; |
|
98 } |
|
99 |
|
100 /** |
|
101 * Not-equal-to comparison operator. Invalid addresses compare equal and are always smaller |
|
102 */ |
|
103 bool operator!= (const NetworkAddress &other) const { |
|
104 return (address_length != other.address_length) || memcmp(&address, &other.address, address_length) != 0; |
|
105 } |
|
106 |
|
107 /** |
|
108 * Less-than comparison operator. Smaller addresses are always lesser. |
|
109 */ |
|
110 bool operator< (const NetworkAddress &other) const { |
|
111 return (address_length < other.address_length) || memcmp(&address, &other.address, other.address_length) < 0; |
|
112 } |
|
113 |
|
114 /** |
|
115 * Greater-than comparison operator. Bigger addresses are always greater. |
|
116 */ |
|
117 bool operator> (const NetworkAddress &other) const { |
|
118 return (address_length > other.address_length) || memcmp(&address, &other.address, address_length) > 0; |
|
119 } |
|
120 }; |
|
121 |
|
122 #endif |