aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Net/src/IPAddressImpl.cpp
blob: 316bba33f3c429c0f12eac4a3e9465ab20be6236 (plain) (blame)
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
//
// IPAddress.cpp
//
// Library: Net
// Package: NetCore
// Module:  IPAddress
//
// Copyright (c) 2005-2011, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/Net/IPAddressImpl.h"
#include "Poco/Net/NetException.h"
#include "Poco/RefCountedObject.h"
#include "Poco/NumberFormatter.h"
#include "Poco/ByteOrder.h"
#include "Poco/String.h"
#include "Poco/Types.h"


using Poco::RefCountedObject;
using Poco::NumberFormatter;
using Poco::toLower;
using Poco::UInt8;
using Poco::UInt16;
using Poco::UInt32;


namespace {


template <typename T>
unsigned maskBits(T val, unsigned size)
	/// Returns the length of the mask (number of bits set in val).
	/// The val should be either all zeros or two contiguos areas of 1s and 0s. 
	/// The algorithm ignores invalid non-contiguous series of 1s and treats val 
	/// as if all bits between MSb and last non-zero bit are set to 1.
{
	unsigned count = 0;
	if (val)
	{
		val = (val ^ (val - 1)) >> 1;
		for (count = 0; val; ++count) val >>= 1;
	}
	else count = size;
	return size - count;
}


} // namespace


namespace Poco {
namespace Net {
namespace Impl {


//
// IPAddressImpl
//


IPAddressImpl::IPAddressImpl()
{
}


IPAddressImpl::~IPAddressImpl()
{
}


//
// IPv4AddressImpl
//


IPv4AddressImpl::IPv4AddressImpl()
{
	std::memset(&_addr, 0, sizeof(_addr));
}


IPv4AddressImpl::IPv4AddressImpl(const void* addr)
{
	std::memcpy(&_addr, addr, sizeof(_addr));
}


IPv4AddressImpl::IPv4AddressImpl(unsigned prefix)
{
	UInt32 addr = (prefix == 32) ? 0xffffffff : ~(0xffffffff >> prefix);
	_addr.s_addr = ByteOrder::toNetwork(addr);
}


IPv4AddressImpl::IPv4AddressImpl(const IPv4AddressImpl& addr)
{
	std::memcpy(&_addr, &addr._addr, sizeof(_addr));
}


IPv4AddressImpl& IPv4AddressImpl::operator = (const IPv4AddressImpl& addr)
{
	if (this == &addr)
		return *this;

	std::memcpy(&_addr, &addr._addr, sizeof(_addr));
	return *this;
}


std::string IPv4AddressImpl::toString() const
{
	const UInt8* bytes = reinterpret_cast<const UInt8*>(&_addr);
	std::string result;
	result.reserve(16);
	NumberFormatter::append(result, bytes[0]);
	result.append(".");
	NumberFormatter::append(result, bytes[1]);
	result.append(".");
	NumberFormatter::append(result, bytes[2]);
	result.append(".");
	NumberFormatter::append(result, bytes[3]);
	return result;
}


poco_socklen_t IPv4AddressImpl::length() const
{
	return sizeof(_addr);
}


const void* IPv4AddressImpl::addr() const
{
	return &_addr;
}


IPAddressImpl::Family IPv4AddressImpl::family() const
{
	return AddressFamily::IPv4;
}


int IPv4AddressImpl::af() const
{
	return AF_INET;
}


unsigned IPv4AddressImpl::prefixLength() const
{
	return maskBits(ntohl(_addr.s_addr), 32);
}


Poco::UInt32 IPv4AddressImpl::scope() const
{
	return 0;
}


bool IPv4AddressImpl::isWildcard() const
{
	return _addr.s_addr == INADDR_ANY;
}


bool IPv4AddressImpl::isBroadcast() const
{
	return _addr.s_addr == INADDR_NONE;
}


bool IPv4AddressImpl::isLoopback() const
{
	return (ntohl(_addr.s_addr) & 0xFF000000) == 0x7F000000; // 127.0.0.1 to 127.255.255.255
}


bool IPv4AddressImpl::isMulticast() const
{
	return (ntohl(_addr.s_addr) & 0xF0000000) == 0xE0000000; // 224.0.0.0/24 to 239.0.0.0/24
}


bool IPv4AddressImpl::isLinkLocal() const
{
	return (ntohl(_addr.s_addr) & 0xFFFF0000) == 0xA9FE0000; // 169.254.0.0/16
}


bool IPv4AddressImpl::isSiteLocal() const
{
	UInt32 addr = ntohl(_addr.s_addr);
	return (addr & 0xFF000000) == 0x0A000000 ||     // 10.0.0.0/24
		(addr & 0xFFFF0000) == 0xC0A80000 ||        // 192.68.0.0/16
		(addr >= 0xAC100000 && addr <= 0xAC1FFFFF); // 172.16.0.0 to 172.31.255.255
}


bool IPv4AddressImpl::isIPv4Compatible() const
{
	return true;
}


bool IPv4AddressImpl::isIPv4Mapped() const
{
	return true;
}


bool IPv4AddressImpl::isWellKnownMC() const
{
	return (ntohl(_addr.s_addr) & 0xFFFFFF00) == 0xE0000000; // 224.0.0.0/8
}


bool IPv4AddressImpl::isNodeLocalMC() const
{
	return false;
}


bool IPv4AddressImpl::isLinkLocalMC() const
{
	return (ntohl(_addr.s_addr) & 0xFF000000) == 0xE0000000; // 244.0.0.0/24
}


bool IPv4AddressImpl::isSiteLocalMC() const
{
	return (ntohl(_addr.s_addr) & 0xFFFF0000) == 0xEFFF0000; // 239.255.0.0/16
}


bool IPv4AddressImpl::isOrgLocalMC() const
{
	return (ntohl(_addr.s_addr) & 0xFFFF0000) == 0xEFC00000; // 239.192.0.0/16
}


bool IPv4AddressImpl::isGlobalMC() const
{
	UInt32 addr = ntohl(_addr.s_addr);
	return addr >= 0xE0000100 && addr <= 0xEE000000; // 224.0.1.0 to 238.255.255.255
}


IPv4AddressImpl IPv4AddressImpl::parse(const std::string& addr)
{
	if (addr.empty()) return IPv4AddressImpl();
#if defined(_WIN32) 
	struct in_addr ia;
	ia.s_addr = inet_addr(addr.c_str());
	if (ia.s_addr == INADDR_NONE && addr != "255.255.255.255")
		return IPv4AddressImpl();
	else
		return IPv4AddressImpl(&ia);
#else
#if __GNUC__ < 3 || defined(POCO_VXWORKS)
	struct in_addr ia;
	ia.s_addr = inet_addr(const_cast<char*>(addr.c_str()));
	if (ia.s_addr == INADDR_NONE && addr != "255.255.255.255")
		return IPv4AddressImpl();
	else
		return IPv4AddressImpl(&ia);
#else
	struct in_addr ia;
	if (inet_aton(addr.c_str(), &ia))
		return IPv4AddressImpl(&ia);
	else
		return IPv4AddressImpl();
#endif
#endif
}


void IPv4AddressImpl::mask(const IPAddressImpl* pMask, const IPAddressImpl* pSet)
{
	poco_assert (pMask->af() == AF_INET && pSet->af() == AF_INET);
	
	_addr.s_addr &= static_cast<const IPv4AddressImpl*>(pMask)->_addr.s_addr;
	_addr.s_addr |= static_cast<const IPv4AddressImpl*>(pSet)->_addr.s_addr & ~static_cast<const IPv4AddressImpl*>(pMask)->_addr.s_addr;
}


IPAddressImpl* IPv4AddressImpl::clone() const
{
	return new IPv4AddressImpl(&_addr);
}


IPv4AddressImpl IPv4AddressImpl::operator & (const IPv4AddressImpl& addr) const
{
	IPv4AddressImpl result(&_addr);
	result._addr.s_addr &= addr._addr.s_addr;
	return result;
}


IPv4AddressImpl IPv4AddressImpl::operator | (const IPv4AddressImpl& addr) const
{
	IPv4AddressImpl result(&_addr);
	result._addr.s_addr |= addr._addr.s_addr;
	return result;
}


IPv4AddressImpl IPv4AddressImpl::operator ^ (const IPv4AddressImpl& addr) const
{
	IPv4AddressImpl result(&_addr);
	result._addr.s_addr ^= addr._addr.s_addr;
	return result;
}


IPv4AddressImpl IPv4AddressImpl::operator ~ () const
{
IPv4AddressImpl result(&_addr);
	result._addr.s_addr ^= 0xffffffff;
	return result;
}


bool IPv4AddressImpl::operator == (const IPv4AddressImpl& addr) const
{
	return 0 == std::memcmp(&addr._addr, &_addr, sizeof(_addr));
}


bool IPv4AddressImpl::operator != (const IPv4AddressImpl& addr) const
{
	return !(*this == addr);
}


#if defined(POCO_HAVE_IPv6)


//
// IPv6AddressImpl
//


IPv6AddressImpl::IPv6AddressImpl(): _scope(0)
{
	std::memset(&_addr, 0, sizeof(_addr));
}


IPv6AddressImpl::IPv6AddressImpl(const void* addr): _scope(0)
{
	std::memcpy(&_addr, addr, sizeof(_addr));
}


IPv6AddressImpl::IPv6AddressImpl(const void* addr, Poco::UInt32 scope): _scope(scope)
{
	std::memcpy(&_addr, addr, sizeof(_addr));
}


IPv6AddressImpl::IPv6AddressImpl(const IPv6AddressImpl& addr): _scope(addr._scope)
{
	std::memcpy((void*) &_addr, (void*) &addr._addr, sizeof(_addr));
}


IPv6AddressImpl& IPv6AddressImpl::operator = (const IPv6AddressImpl& addr)
{
	if (this == &addr)
		return *this;

	_scope = addr._scope;
	std::memcpy(&_addr, &addr._addr, sizeof(_addr));
	return *this;
}


IPv6AddressImpl::IPv6AddressImpl(unsigned prefix):
	_scope(0)
{
	unsigned i = 0;
#ifdef POCO_OS_FAMILY_WINDOWS
	for (; prefix >= 16; ++i, prefix -= 16) 
	{
		_addr.s6_addr16[i] = 0xffff;
	}
	if (prefix > 0)
	{
		_addr.s6_addr16[i++] = ByteOrder::toNetwork(static_cast<Poco::UInt16>(~(0xffff >> prefix)));
	}
	while (i < 8)
	{
		_addr.s6_addr16[i++] = 0;
	}
#else
	for (; prefix >= 32; ++i, prefix -= 32) 
	{
		_addr.s6_addr32[i] = 0xffffffff;
	}
	if (prefix > 0)
	{
		_addr.s6_addr32[i++] = ByteOrder::toNetwork(~(0xffffffffU >> prefix));
	}
	while (i < 4)
	{
		_addr.s6_addr32[i++] = 0;
	}
#endif
}


std::string IPv6AddressImpl::toString() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	if ((isIPv4Compatible() && !isLoopback()) || isIPv4Mapped())
	{
		std::string result;
		result.reserve(24);
		if (words[5] == 0)
			result.append("::");
		else
			result.append("::ffff:");
		const UInt8* bytes = reinterpret_cast<const UInt8*>(&_addr);
		if (bytes[12] != 0) // only 0.0.0.0 can start with zero
		{
			NumberFormatter::append(result, bytes[12]);
			result.append(".");
			NumberFormatter::append(result, bytes[13]);
			result.append(".");
			NumberFormatter::append(result, bytes[14]);
			result.append(".");
			NumberFormatter::append(result, bytes[15]);
		}
		return result;
	}
	else
	{
		std::string result;
		result.reserve(64);
		bool zeroSequence = false;
		int i = 0;
		while (i < 8)
		{
			if (!zeroSequence && words[i] == 0)
			{
				int zi = i;
				while (zi < 8 && words[zi] == 0) ++zi;
				if (zi > i + 1)
				{
					i = zi;
					result.append(":");
					zeroSequence = true;
				}
			}
			if (i > 0) result.append(":");
			if (i < 8) NumberFormatter::appendHex(result, ByteOrder::fromNetwork(words[i++]));
		}
		if (_scope > 0)
		{
			result.append("%");
#if defined(_WIN32)
			NumberFormatter::append(result, _scope);
#else
			char buffer[IFNAMSIZ];
			if (if_indextoname(_scope, buffer))
			{
				result.append(buffer);
			}
			else
			{
				NumberFormatter::append(result, _scope);
			}
#endif
		}
		return toLower(result);
	}
}


poco_socklen_t IPv6AddressImpl::length() const
{
	return sizeof(_addr);
}


const void* IPv6AddressImpl::addr() const
{
	return &_addr;
}


IPAddressImpl::Family IPv6AddressImpl::family() const
{
	return AddressFamily::IPv6;
}


int IPv6AddressImpl::af() const
{
	return AF_INET6;
}


unsigned IPv6AddressImpl::prefixLength() const
{
	unsigned bits = 0;
	unsigned bitPos = 128;
#if defined(POCO_OS_FAMILY_UNIX)
	for (int i = 3; i >= 0; --i)
	{
		unsigned addr = ntohl(_addr.s6_addr32[i]);
		if ((bits = maskBits(addr, 32))) return (bitPos - (32 - bits));
		bitPos -= 32;
	}
	return 0;
#elif defined(POCO_OS_FAMILY_WINDOWS)
	for (int i = 7; i >= 0; --i)
	{
		unsigned short addr = ByteOrder::fromNetwork(_addr.s6_addr16[i]);
		bits = maskBits(addr, 16);
		if (bits) 
			return (bitPos - (16 - bits));
		bitPos -= 16;
	}
	return 0;
#else
#warning prefixLength() not implemented
	throw NotImplementedException("prefixLength() not implemented");
#endif
}


Poco::UInt32 IPv6AddressImpl::scope() const
{
	return _scope;
}


bool IPv6AddressImpl::isWildcard() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && 
		words[4] == 0 && words[5] == 0 && words[6] == 0 && words[7] == 0;
}


bool IPv6AddressImpl::isBroadcast() const
{
	return false;
}


bool IPv6AddressImpl::isLoopback() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && 
		words[4] == 0 && words[5] == 0 && words[6] == 0 && ByteOrder::fromNetwork(words[7]) == 0x0001;
}


bool IPv6AddressImpl::isMulticast() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return (ByteOrder::fromNetwork(words[0]) & 0xFFE0) == 0xFF00;
}


bool IPv6AddressImpl::isLinkLocal() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return (ByteOrder::fromNetwork(words[0]) & 0xFFE0) == 0xFE80;
}


bool IPv6AddressImpl::isSiteLocal() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return ((ByteOrder::fromNetwork(words[0]) & 0xFFE0) == 0xFEC0) || ((ByteOrder::fromNetwork(words[0]) & 0xFF00) == 0xFC00);
}


bool IPv6AddressImpl::isIPv4Compatible() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && words[4] == 0 && words[5] == 0;
}


bool IPv6AddressImpl::isIPv4Mapped() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && words[4] == 0 && ByteOrder::fromNetwork(words[5]) == 0xFFFF;
}


bool IPv6AddressImpl::isWellKnownMC() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return (ByteOrder::fromNetwork(words[0]) & 0xFFF0) == 0xFF00;
}


bool IPv6AddressImpl::isNodeLocalMC() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return (ByteOrder::fromNetwork(words[0]) & 0xFFEF) == 0xFF01;
}


bool IPv6AddressImpl::isLinkLocalMC() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return (ByteOrder::fromNetwork(words[0]) & 0xFFEF) == 0xFF02;
}


bool IPv6AddressImpl::isSiteLocalMC() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return (ByteOrder::fromNetwork(words[0]) & 0xFFEF) == 0xFF05;
}


bool IPv6AddressImpl::isOrgLocalMC() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return (ByteOrder::fromNetwork(words[0]) & 0xFFEF) == 0xFF08;
}


bool IPv6AddressImpl::isGlobalMC() const
{
	const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
	return (ByteOrder::fromNetwork(words[0]) & 0xFFEF) == 0xFF0F;
}


IPv6AddressImpl IPv6AddressImpl::parse(const std::string& addr)
{
	if (addr.empty()) return IPv6AddressImpl();
#if defined(_WIN32)
	struct addrinfo* pAI;
	struct addrinfo hints;
	std::memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_NUMERICHOST;
	int rc = getaddrinfo(addr.c_str(), NULL, &hints, &pAI);
	if (rc == 0)
	{
		IPv6AddressImpl result = IPv6AddressImpl(&reinterpret_cast<struct sockaddr_in6*>(pAI->ai_addr)->sin6_addr, static_cast<int>(reinterpret_cast<struct sockaddr_in6*>(pAI->ai_addr)->sin6_scope_id));
		freeaddrinfo(pAI);
		return result;
	}
	else return IPv6AddressImpl();
#else
	struct in6_addr ia;
	std::string::size_type pos = addr.find('%');
	if (std::string::npos != pos)
	{
		std::string::size_type start = ('[' == addr[0]) ? 1 : 0;
		std::string unscopedAddr(addr, start, pos - start);
		std::string scope(addr, pos + 1, addr.size() - start - pos);
		Poco::UInt32 scopeId(0);
		if (!(scopeId = if_nametoindex(scope.c_str())))
			return IPv6AddressImpl();
		if (inet_pton(AF_INET6, unscopedAddr.c_str(), &ia) == 1)
			return IPv6AddressImpl(&ia, scopeId);
		else
			return IPv6AddressImpl();
	}
	else
	{
		if (inet_pton(AF_INET6, addr.c_str(), &ia) == 1)
			return IPv6AddressImpl(&ia);
		else
			return IPv6AddressImpl();
	}
#endif
}


void IPv6AddressImpl::mask(const IPAddressImpl* /*pMask*/, const IPAddressImpl* /*pSet*/)
{
	throw Poco::NotImplementedException("mask() is only supported for IPv4 addresses");
}


IPAddressImpl* IPv6AddressImpl::clone() const
{
	return new IPv6AddressImpl(*this);
}


IPv6AddressImpl IPv6AddressImpl::operator & (const IPv6AddressImpl& addr) const
{
	if (_scope != addr._scope)
		throw Poco::InvalidArgumentException("Scope ID of passed IPv6 address does not match with the source one.");

	IPv6AddressImpl result(*this);
#ifdef POCO_OS_FAMILY_WINDOWS
	result._addr.s6_addr16[0] &= addr._addr.s6_addr16[0];
	result._addr.s6_addr16[1] &= addr._addr.s6_addr16[1];
	result._addr.s6_addr16[2] &= addr._addr.s6_addr16[2];
	result._addr.s6_addr16[3] &= addr._addr.s6_addr16[3];
	result._addr.s6_addr16[4] &= addr._addr.s6_addr16[4];
	result._addr.s6_addr16[5] &= addr._addr.s6_addr16[5];
	result._addr.s6_addr16[6] &= addr._addr.s6_addr16[6];
	result._addr.s6_addr16[7] &= addr._addr.s6_addr16[7];
#else
	result._addr.s6_addr32[0] &= addr._addr.s6_addr32[0];
	result._addr.s6_addr32[1] &= addr._addr.s6_addr32[1];
	result._addr.s6_addr32[2] &= addr._addr.s6_addr32[2];
	result._addr.s6_addr32[3] &= addr._addr.s6_addr32[3];
#endif
	return result;
}


IPv6AddressImpl IPv6AddressImpl::operator | (const IPv6AddressImpl& addr) const
{
	if (_scope != addr._scope) 
		throw Poco::InvalidArgumentException("Scope ID of passed IPv6 address does not match with the source one.");
    
	IPv6AddressImpl result(*this);
#ifdef POCO_OS_FAMILY_WINDOWS
	result._addr.s6_addr16[0] |= addr._addr.s6_addr16[0];
	result._addr.s6_addr16[1] |= addr._addr.s6_addr16[1];
	result._addr.s6_addr16[2] |= addr._addr.s6_addr16[2];
	result._addr.s6_addr16[3] |= addr._addr.s6_addr16[3];
	result._addr.s6_addr16[4] |= addr._addr.s6_addr16[4];
	result._addr.s6_addr16[5] |= addr._addr.s6_addr16[5];
	result._addr.s6_addr16[6] |= addr._addr.s6_addr16[6];
	result._addr.s6_addr16[7] |= addr._addr.s6_addr16[7];
#else
	result._addr.s6_addr32[0] |= addr._addr.s6_addr32[0];
	result._addr.s6_addr32[1] |= addr._addr.s6_addr32[1];
	result._addr.s6_addr32[2] |= addr._addr.s6_addr32[2];
	result._addr.s6_addr32[3] |= addr._addr.s6_addr32[3];
#endif
	return result;
}


IPv6AddressImpl IPv6AddressImpl::operator ^ (const IPv6AddressImpl& addr) const
{
	if (_scope != addr._scope)
		throw Poco::InvalidArgumentException("Scope ID of passed IPv6 address does not match  with the source one.");
    
	IPv6AddressImpl result(*this);

#ifdef POCO_OS_FAMILY_WINDOWS
	result._addr.s6_addr16[0] ^= addr._addr.s6_addr16[0];
	result._addr.s6_addr16[1] ^= addr._addr.s6_addr16[1];
	result._addr.s6_addr16[2] ^= addr._addr.s6_addr16[2];
	result._addr.s6_addr16[3] ^= addr._addr.s6_addr16[3];
	result._addr.s6_addr16[4] ^= addr._addr.s6_addr16[4];
	result._addr.s6_addr16[5] ^= addr._addr.s6_addr16[5];
	result._addr.s6_addr16[6] ^= addr._addr.s6_addr16[6];
	result._addr.s6_addr16[7] ^= addr._addr.s6_addr16[7];
#else
	result._addr.s6_addr32[0] ^= addr._addr.s6_addr32[0];
	result._addr.s6_addr32[1] ^= addr._addr.s6_addr32[1];
	result._addr.s6_addr32[2] ^= addr._addr.s6_addr32[2];
	result._addr.s6_addr32[3] ^= addr._addr.s6_addr32[3];
#endif
	return result;
}


IPv6AddressImpl IPv6AddressImpl::operator ~ () const
{
	IPv6AddressImpl result(*this);
#ifdef POCO_OS_FAMILY_WINDOWS
	result._addr.s6_addr16[0] ^= 0xffff;
	result._addr.s6_addr16[1] ^= 0xffff;
	result._addr.s6_addr16[2] ^= 0xffff;
	result._addr.s6_addr16[3] ^= 0xffff;
	result._addr.s6_addr16[4] ^= 0xffff;
	result._addr.s6_addr16[5] ^= 0xffff;
	result._addr.s6_addr16[6] ^= 0xffff;
	result._addr.s6_addr16[7] ^= 0xffff;
#else
	result._addr.s6_addr32[0] ^= 0xffffffff;
	result._addr.s6_addr32[1] ^= 0xffffffff;
	result._addr.s6_addr32[2] ^= 0xffffffff;
	result._addr.s6_addr32[3] ^= 0xffffffff;
#endif
	return result;
}


bool IPv6AddressImpl::operator == (const IPv6AddressImpl& addr) const
{
	return _scope == addr._scope && 0 == std::memcmp(&addr._addr, &_addr, sizeof(_addr));
}


bool IPv6AddressImpl::operator != (const IPv6AddressImpl& addr) const
{
	return !(*this == addr);
}


#endif // POCO_HAVE_IPv6


} } } // namespace Poco::Net::Impl