author | Tero Marttila <terom@fixme.fi> |
Fri, 16 Jan 2009 21:24:45 +0200 | |
changeset 399 | c7295b72731a |
parent 382 | 190f81d30624 |
child 400 | d64bf28c4340 |
permissions | -rw-r--r-- |
382
190f81d30624
rename files: Address -> Endpoint, SockAddr -> Address
terom
parents:
381
diff
changeset
|
1 |
#ifndef NETWORK_ENDPOINT_HH |
190f81d30624
rename files: Address -> Endpoint, SockAddr -> Address
terom
parents:
381
diff
changeset
|
2 |
#define NETWORK_ENDPOINT_HH |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
3 |
|
399
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
4 |
/** |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
5 |
* @file |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
6 |
* |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
7 |
* Textual NetworkEndpoint addresses |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
8 |
*/ |
399
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
9 |
|
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
10 |
#include "../Error.hh" |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
11 |
#include "Platform.hh" |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
12 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
13 |
#include <string> |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
14 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
15 |
// assume... |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
16 |
#if INET6_ADDRSTRLEN < INET_ADDRSTRLEN |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
17 |
#error INET6_ADDRSTRLEN is smaller than INET_ADDRSTRLEN |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
18 |
#endif |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
19 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
20 |
/** |
399
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
21 |
* Length of a network address in text form |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
22 |
*/ |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
23 |
const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
24 |
|
284 | 25 |
/** |
399
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
26 |
* NetworkEndpoint is mostly used to pass addresses from the (human) user to NetworkSocket's bind()/connect(). The |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
27 |
* constructor accepts a textual service name (port) and hostname (literal IP adddress or hostname), and can then |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
28 |
* provide a list of resolved addresses for use by NetworkSocket (using the libc getaddrinfo). Additionally, |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
29 |
* methods/operators are defined for textual output of the address. |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
30 |
* |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
31 |
* It may be of value to note that the hostname/service is only interpreted by the get_addrinfo() method, which means |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
32 |
* that invalid/non-existant hostnames/services will only raise an error once the NetworkEndpoint is passed to |
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
33 |
* NetworkSocket. |
284 | 34 |
*/ |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
35 |
class NetworkEndpoint { |
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
36 |
protected: |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
37 |
/** |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
38 |
* Our human-readable hostname |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
39 |
*/ |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
40 |
std::string hostname; |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
41 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
42 |
/** |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
43 |
* Our human-readable service |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
44 |
*/ |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
45 |
std::string service; |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
46 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
47 |
public: |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
48 |
/** |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
49 |
* Construct an empty NetworkEndpoint |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
50 |
*/ |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
51 |
NetworkEndpoint (void); |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
52 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
53 |
/** |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
54 |
* Construct a NetworkEndpoint with a NULL hostname, and a specific service |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
55 |
*/ |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
56 |
explicit NetworkEndpoint (std::string service); |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
57 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
58 |
/** |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
59 |
* Construct a NetworkEndpoint on a specific hostname and service |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
60 |
*/ |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
61 |
NetworkEndpoint (std::string hostname, std::string service); |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
62 |
|
380 | 63 |
/* |
64 |
* We can use the default copy-constructor and assignment operator |
|
65 |
*/ |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
66 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
67 |
public: |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
68 |
/** |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
69 |
* Get a addrinfo* for this address using the given family/type/protocol/flags. |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
70 |
* |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
71 |
* Remember to free the returned pointer using freeaddrinfo() after use. |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
72 |
* |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
73 |
* @param family the socket family for hints.ai_family |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
74 |
* @param socktype the socket type for hints.ai_socktype |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
75 |
* @param protoocl the socket protocol for hints.ai_protocol |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
76 |
* @param flags the flags for hints.ai_flags |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
77 |
* @return linked list of addrinfo's |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
78 |
* @throw NetworkAddressError if resolving this address fails |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
79 |
*/ |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
80 |
virtual addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const; |
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
81 |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
82 |
/** |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
83 |
* Free an addrinfo returned by get_addrinfo |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
84 |
*/ |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
85 |
virtual void free_addrinfo (addrinfo *info) const; |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
86 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
87 |
/** |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
88 |
* Get the human-readable hostname |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
89 |
*/ |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
90 |
virtual std::string get_hostname (void) const { return hostname; } |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
91 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
92 |
/** |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
93 |
* Get the human-readable service name |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
94 |
*/ |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
95 |
virtual std::string get_service (void) const { return service; } |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
96 |
}; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
97 |
|
284 | 98 |
/** |
99 |
* Formatted as [<addr>:<port>] |
|
100 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
101 |
std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
102 |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
103 |
/** |
399
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
104 |
* Errors raised by NetworkEndpoint, so e.g. DNS errors (hostname not found etc.) |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
105 |
*/ |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
106 |
class NetworkAddressError : public Error { |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
107 |
protected: |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
108 |
static std::string build_str (const NetworkEndpoint &addr, const char *op, const char *msg); |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
109 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
110 |
public: |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
111 |
NetworkAddressError (const NetworkEndpoint &addr, const char *op, const char *msg); |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
112 |
}; |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
113 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
114 |
#endif /* NETWORK_ADDRESS_HH */ |