| author | morasa@smirgeline.hut.fi |
| Fri, 11 Sep 2009 16:45:04 +0300 | |
| branch | new-physics |
| changeset 447 | fc9e4305fddf |
| parent 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. |
|
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
399
diff
changeset
|
34 |
* |
|
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
399
diff
changeset
|
35 |
* All DNS lookups are blocking. |
| 284 | 36 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
37 |
class NetworkEndpoint {
|
|
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
38 |
protected: |
|
378
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 |
* Our human-readable 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 |
std::string hostname; |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
43 |
|
|
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 |
* Our human-readable 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 |
std::string service; |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
48 |
|
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
49 |
public: |
|
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 |
* 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
|
52 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
53 |
NetworkEndpoint (void); |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
54 |
|
|
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 |
* 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
|
57 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
58 |
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
|
59 |
|
|
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 |
* 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
|
62 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
63 |
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
|
64 |
|
| 380 | 65 |
/* |
66 |
* We can use the default copy-constructor and assignment operator |
|
67 |
*/ |
|
|
378
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 |
public: |
|
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 |
* 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
|
72 |
* |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
73 |
* 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
|
74 |
* |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
75 |
* @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
|
76 |
* @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
|
77 |
* @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
|
78 |
* @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
|
79 |
* @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
|
80 |
* @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
|
81 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
82 |
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
|
83 |
|
|
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 |
* 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
|
86 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
87 |
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
|
88 |
|
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
89 |
/** |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
90 |
* 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
|
91 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
92 |
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
|
93 |
|
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
94 |
/** |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
95 |
* 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
|
96 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
97 |
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
|
98 |
}; |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
99 |
|
| 284 | 100 |
/** |
101 |
* Formatted as [<addr>:<port>] |
|
102 |
*/ |
|
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
103 |
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
|
104 |
|
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
105 |
/** |
|
399
c7295b72731a
documentation work on Network
Tero Marttila <terom@fixme.fi>
parents:
382
diff
changeset
|
106 |
* 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
|
107 |
*/ |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
108 |
class NetworkAddressError : public Error {
|
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
109 |
protected: |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
110 |
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
|
111 |
|
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
112 |
public: |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
113 |
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
|
114 |
}; |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
284
diff
changeset
|
115 |
|
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
116 |
#endif /* NETWORK_ADDRESS_HH */ |