forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketAddress.h
More file actions
239 lines (188 loc) · 7.46 KB
/
SocketAddress.h
File metadata and controls
239 lines (188 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// SocketAddress.h
//
// Library: Net
// Package: NetCore
// Module: SocketAddress
//
// Definition of the SocketAddress class.
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Net_SocketAddress_INCLUDED
#define Net_SocketAddress_INCLUDED
#include "Poco/Net/Net.h"
#include "Poco/Net/SocketAddressImpl.h"
#include <ostream>
namespace Poco {
class BinaryReader;
class BinaryWriter;
namespace Net {
class IPAddress;
class Net_API SocketAddress
/// This class represents an internet (IP) endpoint/socket
/// address. The address can belong either to the
/// IPv4 or the IPv6 address family and consists of a
/// host address and a port number.
{
public:
// The following declarations keep the Family type
// backwards compatible with the previously used
// enum declaration.
typedef AddressFamily::Family Family;
static const Family IPv4 = AddressFamily::IPv4;
#if defined(POCO_HAVE_IPv6)
static const Family IPv6 = AddressFamily::IPv6;
#endif
#if defined(POCO_OS_FAMILY_UNIX)
static const Family UNIX_LOCAL = AddressFamily::UNIX_LOCAL;
#endif
SocketAddress();
/// Creates a wildcard (all zero) IPv4 SocketAddress.
explicit SocketAddress(Family family);
/// Creates a SocketAddress with unspecified (wildcard) IP address
/// of the given family.
SocketAddress(const IPAddress& hostAddress, Poco::UInt16 portNumber);
/// Creates a SocketAddress from an IP address and given port number.
explicit SocketAddress(Poco::UInt16 port);
/// Creates a SocketAddress with unspecified (wildcard) IP address
/// and given port number.
SocketAddress(Family family, Poco::UInt16 port);
/// Creates a SocketAddress with unspecified (wildcard) IP address
/// of the given family, and given port number.
SocketAddress(const std::string& hostAddress, Poco::UInt16 portNumber);
/// Creates a SocketAddress from an IP address and given port number.
///
/// The IP address must either be a domain name, or it must
/// be in dotted decimal (IPv4) or hex string (IPv6) format.
SocketAddress(Family family, const std::string& hostAddress, Poco::UInt16 portNumber);
/// Creates a SocketAddress from an IP address and given port number.
///
/// The IP address must either be a domain name, or it must
/// be in dotted decimal (IPv4) or hex string (IPv6) format.
///
/// If a domain name is given in hostAddress, it is resolved and the address
/// matching the given family is used. If no address matching the given family
/// is found, or the IP address given in hostAddress does not match the given
/// family, an AddressFamilyMismatchException is thrown.
SocketAddress(const std::string& hostAddress, const std::string& portNumber);
/// Creates a SocketAddress from an IP address and the
/// service name or port number.
///
/// The IP address must either be a domain name, or it must
/// be in dotted decimal (IPv4) or hex string (IPv6) format.
///
/// The given port must either be a decimal port number, or
/// a service name.
SocketAddress(Family family, const std::string& hostAddress, const std::string& portNumber);
/// Creates a SocketAddress from an IP address and the
/// service name or port number.
///
/// The IP address must either be a domain name, or it must
/// be in dotted decimal (IPv4) or hex string (IPv6) format.
///
/// The given port must either be a decimal port number, or
/// a service name.
///
/// If a domain name is given in hostAddress, it is resolved and the address
/// matching the given family is used. If no address matching the given family
/// is found, or the IP address given in hostAddress does not match the given
/// family, an AddressFamilyMismatchException is thrown.
explicit SocketAddress(const std::string& hostAndPort);
/// Creates a SocketAddress from an IP address or host name and the
/// port number/service name. Host name/address and port number must
/// be separated by a colon. In case of an IPv6 address,
/// the address part must be enclosed in brackets.
///
/// Examples:
/// 192.168.1.10:80
/// [::ffff:192.168.1.120]:2040
/// www.appinf.com:8080
///
/// On POSIX platforms supporting UNIX_LOCAL sockets, hostAndPort
/// can also be the absolute path of a local socket, starting with a
/// slash, e.g. "/tmp/local.socket".
SocketAddress(Family family, const std::string& addr);
/// Creates a SocketAddress of the given family from a
/// string representation of the address, which is
/// either an IP address and port number, separated by
/// a colon for IPv4 or IPv6 addresses, or a path for
/// UNIX_LOCAL sockets.
SocketAddress(const SocketAddress& addr);
/// Creates a SocketAddress by copying another one.
SocketAddress(const struct sockaddr* addr, poco_socklen_t length);
/// Creates a SocketAddress from a native socket address.
~SocketAddress();
/// Destroys the SocketAddress.
SocketAddress& operator = (const SocketAddress& socketAddress);
/// Assigns another SocketAddress.
IPAddress host() const;
/// Returns the host IP address.
Poco::UInt16 port() const;
/// Returns the port number.
poco_socklen_t length() const;
/// Returns the length of the internal native socket address.
const struct sockaddr* addr() const;
/// Returns a pointer to the internal native socket address.
int af() const;
/// Returns the address family (AF_INET or AF_INET6) of the address.
std::string toString() const;
/// Returns a string representation of the address.
Family family() const;
/// Returns the address family of the host's address.
bool operator < (const SocketAddress& socketAddress) const;
bool operator == (const SocketAddress& socketAddress) const;
bool operator != (const SocketAddress& socketAddress) const;
enum
{
MAX_ADDRESS_LENGTH =
#if defined(POCO_OS_FAMILY_UNIX)
sizeof(struct sockaddr_un)
#elif defined(POCO_HAVE_IPv6)
sizeof(struct sockaddr_in6)
#else
sizeof(struct sockaddr_in)
#endif
/// Maximum length in bytes of a socket address.
};
protected:
void init(const IPAddress& hostAddress, Poco::UInt16 portNumber);
void init(const std::string& hostAddress, Poco::UInt16 portNumber);
void init(Family family, const std::string& hostAddress, Poco::UInt16 portNumber);
void init(Family family, const std::string& address);
void init(const std::string& hostAndPort);
Poco::UInt16 resolveService(const std::string& service);
private:
typedef Poco::Net::Impl::SocketAddressImpl Impl;
typedef Impl* Ptr;
Ptr pImpl() const;
void newIPv4();
void newIPv4(const sockaddr_in*);
void newIPv4(const IPAddress& hostAddress, Poco::UInt16 portNumber);
#if defined(POCO_HAVE_IPv6)
void newIPv6(const sockaddr_in6*);
void newIPv6(const IPAddress& hostAddress, Poco::UInt16 portNumber);
#endif
#if defined(POCO_OS_FAMILY_UNIX)
void newLocal(const sockaddr_un* sockAddr);
void newLocal(const std::string& path);
#endif
void destruct();
char* storage();
static const unsigned sz = sizeof(Poco::Net::Impl::IPv6SocketAddressImpl);
typedef std::aligned_storage<sz>::type AlignerType;
union
{
char buffer[sz];
private:
AlignerType aligner;
} _memory;
};
} } // namespace Poco::Net
Net_API Poco::BinaryWriter& operator << (Poco::BinaryWriter& writer, const Poco::Net::SocketAddress& value);
Net_API Poco::BinaryReader& operator >> (Poco::BinaryReader& reader, Poco::Net::SocketAddress& value);
Net_API std::ostream& operator << (std::ostream& ostr, const Poco::Net::SocketAddress& address);
#endif // Net_SocketAddress_INCLUDED