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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
//
// SocketAddress.cpp
//
// Library: Net
// Package: NetCore
// Module: SocketAddress
//
// Copyright (c) 2005-2011, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/IPAddress.h"
#include "Poco/Net/NetException.h"
#include "Poco/Net/DNS.h"
#include "Poco/RefCountedObject.h"
#include "Poco/NumberParser.h"
#include "Poco/BinaryReader.h"
#include "Poco/BinaryWriter.h"
#include <algorithm>
#include <cstring>
using Poco::RefCountedObject;
using Poco::NumberParser;
using Poco::UInt16;
using Poco::InvalidArgumentException;
using Poco::Net::Impl::SocketAddressImpl;
using Poco::Net::Impl::IPv4SocketAddressImpl;
#ifdef POCO_HAVE_IPv6
using Poco::Net::Impl::IPv6SocketAddressImpl;
#endif
#ifdef POCO_OS_FAMILY_UNIX
using Poco::Net::Impl::LocalSocketAddressImpl;
#endif
namespace Poco {
namespace Net {
struct AFLT
{
bool operator () (const IPAddress& a1, const IPAddress& a2)
{
return a1.af() < a2.af();
}
};
//
// SocketAddress
//
#if !defined(_MSC_VER) || defined(__STDC__)
// Go home MSVC, you're drunk...
// See http://stackoverflow.com/questions/5899857/multiple-definition-error-for-static-const-class-members
const SocketAddress::Family SocketAddress::IPv4;
#if defined(POCO_HAVE_IPv6)
const SocketAddress::Family SocketAddress::IPv6;
#endif
#if defined(POCO_OS_FAMILY_UNIX)
const SocketAddress::Family SocketAddress::UNIX_LOCAL;
#endif
#endif
SocketAddress::SocketAddress()
{
newIPv4();
}
SocketAddress::SocketAddress(Family fam)
{
init(IPAddress(fam), 0);
}
SocketAddress::SocketAddress(const IPAddress& hostAddress, Poco::UInt16 portNumber)
{
init(hostAddress, portNumber);
}
SocketAddress::SocketAddress(Poco::UInt16 portNumber)
{
init(IPAddress(), portNumber);
}
SocketAddress::SocketAddress(Family fam, Poco::UInt16 portNumber)
{
init(IPAddress(fam), portNumber);
}
SocketAddress::SocketAddress(const std::string& hostAddress, Poco::UInt16 portNumber)
{
init(hostAddress, portNumber);
}
SocketAddress::SocketAddress(Family fam, const std::string& hostAddress, Poco::UInt16 portNumber)
{
init(fam, hostAddress, portNumber);
}
SocketAddress::SocketAddress(const std::string& hostAddress, const std::string& portNumber)
{
init(hostAddress, resolveService(portNumber));
}
SocketAddress::SocketAddress(Family fam, const std::string& hostAddress, const std::string& portNumber)
{
init(fam, hostAddress, resolveService(portNumber));
}
SocketAddress::SocketAddress(Family fam, const std::string& addr)
{
init(fam, addr);
}
SocketAddress::SocketAddress(const std::string& hostAndPort)
{
init(hostAndPort);
}
SocketAddress::SocketAddress(const SocketAddress& socketAddress)
{
if (socketAddress.family() == IPv4)
newIPv4(reinterpret_cast<const sockaddr_in*>(socketAddress.addr()));
#if defined(POCO_HAVE_IPv6)
else if (socketAddress.family() == IPv6)
newIPv6(reinterpret_cast<const sockaddr_in6*>(socketAddress.addr()));
#endif
#if defined(POCO_OS_FAMILY_UNIX)
else if (socketAddress.family() == UNIX_LOCAL)
newLocal(reinterpret_cast<const sockaddr_un*>(socketAddress.addr()));
#endif
}
SocketAddress::SocketAddress(const struct sockaddr* sockAddr, poco_socklen_t length)
{
if (length == sizeof(struct sockaddr_in) && sockAddr->sa_family == AF_INET)
newIPv4(reinterpret_cast<const struct sockaddr_in*>(sockAddr));
#if defined(POCO_HAVE_IPv6)
else if (length == sizeof(struct sockaddr_in6) && sockAddr->sa_family == AF_INET6)
newIPv6(reinterpret_cast<const struct sockaddr_in6*>(sockAddr));
#endif
#if defined(POCO_OS_FAMILY_UNIX)
else if (length > 0 && length <= sizeof(struct sockaddr_un) && sockAddr->sa_family == AF_UNIX)
newLocal(reinterpret_cast<const sockaddr_un*>(sockAddr));
#endif
else throw Poco::InvalidArgumentException("Invalid address length or family passed to SocketAddress()");
}
SocketAddress::~SocketAddress()
{
}
bool SocketAddress::operator < (const SocketAddress& socketAddress) const
{
if (family() < socketAddress.family()) return true;
if (family() > socketAddress.family()) return false;
#if defined(POCO_OS_FAMILY_UNIX)
if (family() == UNIX_LOCAL) return toString() < socketAddress.toString();
#endif
if (host() < socketAddress.host()) return true;
if (host() > socketAddress.host()) return false;
return (port() < socketAddress.port());
}
SocketAddress& SocketAddress::operator = (const SocketAddress& socketAddress)
{
if (&socketAddress != this)
{
if (socketAddress.family() == IPv4)
newIPv4(reinterpret_cast<const sockaddr_in*>(socketAddress.addr()));
#if defined(POCO_HAVE_IPv6)
else if (socketAddress.family() == IPv6)
newIPv6(reinterpret_cast<const sockaddr_in6*>(socketAddress.addr()));
#endif
#if defined(POCO_OS_FAMILY_UNIX)
else if (socketAddress.family() == UNIX_LOCAL)
newLocal(reinterpret_cast<const sockaddr_un*>(socketAddress.addr()));
#endif
}
return *this;
}
IPAddress SocketAddress::host() const
{
return pImpl()->host();
}
Poco::UInt16 SocketAddress::port() const
{
return ntohs(pImpl()->port());
}
poco_socklen_t SocketAddress::length() const
{
return pImpl()->length();
}
const struct sockaddr* SocketAddress::addr() const
{
return pImpl()->addr();
}
int SocketAddress::af() const
{
return pImpl()->af();
}
SocketAddress::Family SocketAddress::family() const
{
return static_cast<Family>(pImpl()->family());
}
std::string SocketAddress::toString() const
{
return pImpl()->toString();
}
void SocketAddress::init(const IPAddress& hostAddress, Poco::UInt16 portNumber)
{
if (hostAddress.family() == IPAddress::IPv4)
newIPv4(hostAddress, portNumber);
#if defined(POCO_HAVE_IPv6)
else if (hostAddress.family() == IPAddress::IPv6)
newIPv6(hostAddress, portNumber);
#endif
else throw Poco::NotImplementedException("unsupported IP address family");
}
void SocketAddress::init(const std::string& hostAddress, Poco::UInt16 portNumber)
{
IPAddress ip;
if (IPAddress::tryParse(hostAddress, ip))
{
init(ip, portNumber);
}
else
{
HostEntry he = DNS::hostByName(hostAddress);
HostEntry::AddressList addresses = he.addresses();
if (addresses.size() > 0)
{
#if defined(POCO_HAVE_IPv6) && defined(POCO_SOCKETADDRESS_PREFER_IPv4)
// if we get both IPv4 and IPv6 addresses, prefer IPv4
std::stable_sort(addresses.begin(), addresses.end(), AFLT());
#endif
init(addresses[0], portNumber);
}
else throw HostNotFoundException("No address found for host", hostAddress);
}
}
void SocketAddress::init(Family fam, const std::string& hostAddress, Poco::UInt16 portNumber)
{
IPAddress ip;
if (IPAddress::tryParse(hostAddress, ip))
{
if (ip.family() != fam) throw AddressFamilyMismatchException(hostAddress);
init(ip, portNumber);
}
else
{
HostEntry he = DNS::hostByName(hostAddress);
HostEntry::AddressList addresses = he.addresses();
if (addresses.size() > 0)
{
for (HostEntry::AddressList::const_iterator it = addresses.begin(); it != addresses.end(); ++it)
{
if (it->family() == fam)
{
init(*it, portNumber);
return;
}
}
throw AddressFamilyMismatchException(hostAddress);
}
else throw HostNotFoundException("No address found for host", hostAddress);
}
}
void SocketAddress::init(Family fam, const std::string& address)
{
#if defined(POCO_OS_FAMILY_UNIX)
if (fam == UNIX_LOCAL)
{
newLocal(address);
}
else
#endif
{
std::string host;
std::string port;
std::string::const_iterator it = address.begin();
std::string::const_iterator end = address.end();
if (*it == '[')
{
++it;
while (it != end && *it != ']') host += *it++;
if (it == end) throw InvalidArgumentException("Malformed IPv6 address");
++it;
}
else
{
while (it != end && *it != ':') host += *it++;
}
if (it != end && *it == ':')
{
++it;
while (it != end) port += *it++;
}
else throw InvalidArgumentException("Missing port number");
init(fam, host, resolveService(port));
}
}
void SocketAddress::init(const std::string& hostAndPort)
{
poco_assert (!hostAndPort.empty());
std::string host;
std::string port;
std::string::const_iterator it = hostAndPort.begin();
std::string::const_iterator end = hostAndPort.end();
#if defined(POCO_OS_FAMILY_UNIX)
if (*it == '/')
{
newLocal(hostAndPort);
return;
}
#endif
if (*it == '[')
{
++it;
while (it != end && *it != ']') host += *it++;
if (it == end) throw InvalidArgumentException("Malformed IPv6 address");
++it;
}
else
{
while (it != end && *it != ':') host += *it++;
}
if (it != end && *it == ':')
{
++it;
while (it != end) port += *it++;
}
else throw InvalidArgumentException("Missing port number");
init(host, resolveService(port));
}
Poco::UInt16 SocketAddress::resolveService(const std::string& service)
{
unsigned port;
if (NumberParser::tryParseUnsigned(service, port) && port <= 0xFFFF)
{
return (UInt16) port;
}
else
{
#if defined(POCO_VXWORKS)
throw ServiceNotFoundException(service);
#else
struct servent* se = getservbyname(service.c_str(), NULL);
if (se)
return ntohs(se->s_port);
else
throw ServiceNotFoundException(service);
#endif
}
}
} } // namespace Poco::Net
Poco::BinaryWriter& operator << (Poco::BinaryWriter& writer, const Poco::Net::SocketAddress& value)
{
writer << value.host();
writer << value.port();
return writer;
}
Poco::BinaryReader& operator >> (Poco::BinaryReader& reader, Poco::Net::SocketAddress& value)
{
Poco::Net::IPAddress host;
reader >> host;
Poco::UInt16 port;
reader >> port;
value = Poco::Net::SocketAddress(host, port);
return reader;
}
std::ostream& operator << (std::ostream& ostr, const Poco::Net::SocketAddress& address)
{
ostr << address.toString();
return ostr;
}
|