1 #ifndef NETWORK_ENDPOINT_HH |
1 #ifndef NETWORK_ENDPOINT_HH |
2 #define NETWORK_ENDPOINT_HH |
2 #define NETWORK_ENDPOINT_HH |
3 |
3 |
|
4 /** |
|
5 * @file |
|
6 * |
|
7 * Textual NetworkEndpoint addresses |
|
8 */ |
|
9 |
4 #include "../Error.hh" |
10 #include "../Error.hh" |
5 |
11 #include "Platform.hh" |
6 /* |
|
7 * Platform-specific includes |
|
8 */ |
|
9 #ifndef WIN32 |
|
10 // linux |
|
11 #include <sys/types.h> |
|
12 #include <netdb.h> |
|
13 #else |
|
14 #error "This network code won't compile on win32 :)" |
|
15 #endif |
|
16 |
12 |
17 #include <string> |
13 #include <string> |
18 |
14 |
19 // assume... |
15 // assume... |
20 #if INET6_ADDRSTRLEN < INET_ADDRSTRLEN |
16 #if INET6_ADDRSTRLEN < INET_ADDRSTRLEN |
21 #error INET6_ADDRSTRLEN is smaller than INET_ADDRSTRLEN |
17 #error INET6_ADDRSTRLEN is smaller than INET_ADDRSTRLEN |
22 #endif |
18 #endif |
23 |
19 |
24 /** |
20 /** |
25 * Length of a network address |
21 * Length of a network address in text form |
26 */ |
22 */ |
27 const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN; |
23 const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN; |
28 |
24 |
29 /** |
25 /** |
30 * We use ClanLib's IPAddress API, but with our own name |
26 * NetworkEndpoint is mostly used to pass addresses from the (human) user to NetworkSocket's bind()/connect(). The |
|
27 * constructor accepts a textual service name (port) and hostname (literal IP adddress or hostname), and can then |
|
28 * provide a list of resolved addresses for use by NetworkSocket (using the libc getaddrinfo). Additionally, |
|
29 * methods/operators are defined for textual output of the address. |
|
30 * |
|
31 * It may be of value to note that the hostname/service is only interpreted by the get_addrinfo() method, which means |
|
32 * that invalid/non-existant hostnames/services will only raise an error once the NetworkEndpoint is passed to |
|
33 * NetworkSocket. |
31 */ |
34 */ |
32 class NetworkEndpoint { |
35 class NetworkEndpoint { |
33 protected: |
36 protected: |
34 /** |
37 /** |
35 * Our human-readable hostname |
38 * Our human-readable hostname |
96 * Formatted as [<addr>:<port>] |
99 * Formatted as [<addr>:<port>] |
97 */ |
100 */ |
98 std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr); |
101 std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr); |
99 |
102 |
100 /** |
103 /** |
101 * |
104 * Errors raised by NetworkEndpoint, so e.g. DNS errors (hostname not found etc.) |
102 */ |
105 */ |
103 class NetworkAddressError : public Error { |
106 class NetworkAddressError : public Error { |
104 protected: |
107 protected: |
105 static std::string build_str (const NetworkEndpoint &addr, const char *op, const char *msg); |
108 static std::string build_str (const NetworkEndpoint &addr, const char *op, const char *msg); |
106 |
109 |