A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-address.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007-2008 Louis Pasteur University
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
18 */
19
20#include "ipv6-address.h"
21
22#include "mac16-address.h"
23#include "mac48-address.h"
24#include "mac64-address.h"
25
26#include "ns3/assert.h"
27#include "ns3/log.h"
28
29#include <iomanip>
30#include <memory>
31
32#ifdef __WIN32__
33#include <WS2tcpip.h>
34#else
35#include <arpa/inet.h>
36#endif
37
38namespace ns3
39{
40
41NS_LOG_COMPONENT_DEFINE("Ipv6Address");
42
43#ifdef __cplusplus
44extern "C"
45{ /* } */
46#endif
47 /**
48 * \brief Mix hash keys in-place for lookuphash
49 *
50 * \param a first word of the hash key
51 * \param b second word of the hash key
52 * \param c third word of the hash key
53 */
55 {
56 (a) -= (b);
57 (a) -= (c);
58 (a) ^= ((c) >> 13);
59 (b) -= (c);
60 (b) -= (a);
61 (b) ^= ((a) << 8);
62 (c) -= (a);
63 (c) -= (b);
64 (c) ^= ((b) >> 13);
65 (a) -= (b);
66 (a) -= (c);
67 (a) ^= ((c) >> 12);
68 (b) -= (c);
69 (b) -= (a);
70 (b) ^= ((a) << 16);
71 (c) -= (a);
72 (c) -= (b);
73 (c) ^= ((b) >> 5);
74 (a) -= (b);
75 (a) -= (c);
76 (a) ^= ((c) >> 3);
77 (b) -= (c);
78 (b) -= (a);
79 (b) ^= ((a) << 10);
80 (c) -= (a);
81 (c) -= (b);
82 (c) ^= ((b) >> 15);
83 }
84
85 /**
86 * \brief Get a hash key.
87 * \param k the key
88 * \param length the length of the key
89 * \param level the previous hash, or an arbitrary value
90 * \return hash
91 * \note Adapted from Jens Jakobsen implementation (chillispot).
92 */
93 static uint32_t lookuphash(unsigned char* k, uint32_t length, uint32_t level)
94 {
95 NS_LOG_FUNCTION(k << length << level);
96
97 typedef uint32_t ub4; /* unsigned 4-byte quantities */
98 uint32_t a = 0;
99 uint32_t b = 0;
100 uint32_t c = 0;
101 uint32_t len = 0;
102
103 /* Set up the internal state */
104 len = length;
105 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
106 c = level; /* the previous hash value */
107
108 /* handle most of the key */
109 while (len >= 12)
110 {
111 a += (k[0] + ((ub4)k[1] << 8) + ((ub4)k[2] << 16) + ((ub4)k[3] << 24));
112 b += (k[4] + ((ub4)k[5] << 8) + ((ub4)k[6] << 16) + ((ub4)k[7] << 24));
113 c += (k[8] + ((ub4)k[9] << 8) + ((ub4)k[10] << 16) + ((ub4)k[11] << 24));
114 mixHashKey(a, b, c);
115 k += 12;
116 len -= 12;
117 }
118
119 /* handle the last 11 bytes */
120 c += length;
121 switch (len) /* all the case statements fall through */
122 {
123 case 11:
124 c += ((ub4)k[10] << 24);
125 case 10:
126 c += ((ub4)k[9] << 16);
127 case 9:
128 c += ((ub4)k[8] << 8); /* the first byte of c is reserved for the length */
129 case 8:
130 b += ((ub4)k[7] << 24);
131 case 7:
132 b += ((ub4)k[6] << 16);
133 case 6:
134 b += ((ub4)k[5] << 8);
135 case 5:
136 b += k[4];
137 case 4:
138 a += ((ub4)k[3] << 24);
139 case 3:
140 a += ((ub4)k[2] << 16);
141 case 2:
142 a += ((ub4)k[1] << 8);
143 case 1:
144 a += k[0];
145 /* case 0: nothing left to add */
146 }
147 mixHashKey(a, b, c);
148
149 /* report the result */
150 return c;
151 }
152
153#ifdef __cplusplus
154}
155#endif
156
158{
159 NS_LOG_FUNCTION(this);
160 memset(m_address, 0x00, 16);
161 m_initialized = false;
162}
163
165{
166 // Do not add function logging here, to avoid stack overflow
167 memcpy(m_address, addr.m_address, 16);
168 m_initialized = true;
169}
170
172{
173 // Do not add function logging here, to avoid stack overflow
174 memcpy(m_address, addr->m_address, 16);
175 m_initialized = true;
176}
177
178Ipv6Address::Ipv6Address(const char* address)
179{
180 NS_LOG_FUNCTION(this << address);
181
182 if (inet_pton(AF_INET6, address, m_address) <= 0)
183 {
184 memset(m_address, 0x00, 16);
185 NS_LOG_LOGIC("Error, can not build an IPv6 address from an invalid string: " << address);
186 m_initialized = false;
187 return;
188 }
189 m_initialized = true;
190}
191
192Ipv6Address::Ipv6Address(uint8_t address[16])
193{
194 NS_LOG_FUNCTION(this << &address);
195 /* 128 bit => 16 bytes */
196 memcpy(m_address, address, 16);
197 m_initialized = true;
198}
199
201{
202 /* do nothing */
203 NS_LOG_FUNCTION(this);
204}
205
206void
207Ipv6Address::Set(const char* address)
208{
209 NS_LOG_FUNCTION(this << address);
210 if (inet_pton(AF_INET6, address, m_address) <= 0)
211 {
212 memset(m_address, 0x00, 16);
213 NS_LOG_LOGIC("Error, can not build an IPv6 address from an invalid string: " << address);
214 m_initialized = false;
215 return;
216 }
217 m_initialized = true;
218}
219
220void
221Ipv6Address::Set(uint8_t address[16])
222{
223 /* 128 bit => 16 bytes */
224 NS_LOG_FUNCTION(this << &address);
225 memcpy(m_address, address, 16);
226 m_initialized = true;
227}
228
229void
230Ipv6Address::Serialize(uint8_t buf[16]) const
231{
232 NS_LOG_FUNCTION(this << &buf);
233 memcpy(buf, m_address, 16);
234}
235
237Ipv6Address::Deserialize(const uint8_t buf[16])
238{
239 NS_LOG_FUNCTION(&buf);
240 Ipv6Address ipv6((uint8_t*)buf);
241 ipv6.m_initialized = true;
242 return ipv6;
243}
244
247{
248 NS_LOG_FUNCTION(addr);
249 uint8_t buf[16] = {
250 0x00,
251 0x00,
252 0x00,
253 0x00,
254 0x00,
255 0x00,
256 0x00,
257 0x00,
258 0x00,
259 0x00,
260 0xff,
261 0xff,
262 0x00,
263 0x00,
264 0x00,
265 0x00,
266 };
267 addr.Serialize(&buf[12]);
268 return Ipv6Address(buf);
269}
270
273{
274 NS_LOG_FUNCTION(this);
275 uint8_t buf[16];
276 Ipv4Address v4Addr;
277
278 Serialize(buf);
279 v4Addr = Ipv4Address::Deserialize(&buf[12]);
280 return v4Addr;
281}
282
285{
286 Ipv6Address ipv6Addr = Ipv6Address::GetAny();
287
289 {
291 }
292 else if (Mac48Address::IsMatchingType(addr))
293 {
295 }
296 else if (Mac16Address::IsMatchingType(addr))
297 {
299 }
300 else if (Mac8Address::IsMatchingType(addr))
301 {
303 }
304
305 if (ipv6Addr.IsAny())
306 {
307 NS_ABORT_MSG("Unknown address type");
308 }
309 return ipv6Addr;
310}
311
314{
315 Ipv6Address ipv6PrefixAddr = Ipv6Address::GetOnes().CombinePrefix(prefix);
316 return MakeAutoconfiguredAddress(addr, ipv6PrefixAddr);
317}
318
321{
322 NS_LOG_FUNCTION(addr << prefix);
323 Ipv6Address ret;
324 uint8_t buf[2];
325 uint8_t buf2[16];
326
327 addr.CopyTo(buf);
328 prefix.GetBytes(buf2);
329 memset(buf2 + 8, 0, 8);
330
331 memcpy(buf2 + 14, buf, 2);
332 buf2[11] = 0xff;
333 buf2[12] = 0xfe;
334
335 ret.Set(buf2);
336 return ret;
337}
338
341{
342 NS_LOG_FUNCTION(addr << prefix);
343 Ipv6Address ret;
344 uint8_t buf[16];
345 uint8_t buf2[16];
346
347 addr.CopyTo(buf);
348 prefix.GetBytes(buf2);
349
350 memcpy(buf2 + 8, buf, 3);
351 buf2[11] = 0xff;
352 buf2[12] = 0xfe;
353 memcpy(buf2 + 13, buf + 3, 3);
354 buf2[8] ^= 0x02;
355
356 ret.Set(buf2);
357 return ret;
358}
359
362{
363 NS_LOG_FUNCTION(addr << prefix);
364 Ipv6Address ret;
365 uint8_t buf[8];
366 uint8_t buf2[16];
367
368 addr.CopyTo(buf);
369 prefix.GetBytes(buf2);
370
371 memcpy(buf2 + 8, buf, 8);
372
373 ret.Set(buf2);
374 return ret;
375}
376
379{
380 NS_LOG_FUNCTION(addr << prefix);
381 Ipv6Address ret;
382 uint8_t buf[2];
383 uint8_t buf2[16];
384
385 buf[0] = 0;
386 addr.CopyTo(&buf[1]);
387 prefix.GetBytes(buf2);
388 memset(buf2 + 8, 0, 8);
389
390 memcpy(buf2 + 14, buf, 2);
391 buf2[11] = 0xff;
392 buf2[12] = 0xfe;
393
394 ret.Set(buf2);
395 return ret;
396}
397
400{
401 Ipv6Address ipv6Addr = Ipv6Address::GetAny();
402
404 {
406 }
407 else if (Mac48Address::IsMatchingType(addr))
408 {
410 }
411 else if (Mac16Address::IsMatchingType(addr))
412 {
414 }
415 else if (Mac8Address::IsMatchingType(addr))
416 {
418 }
419
420 if (ipv6Addr.IsAny())
421 {
422 NS_ABORT_MSG("Unknown address type");
423 }
424 return ipv6Addr;
425}
426
429{
430 NS_LOG_FUNCTION(addr);
431 Ipv6Address ret;
432 uint8_t buf[2];
433 uint8_t buf2[16];
434
435 addr.CopyTo(buf);
436
437 memset(buf2, 0x00, sizeof(buf2));
438 buf2[0] = 0xfe;
439 buf2[1] = 0x80;
440 memcpy(buf2 + 14, buf, 2);
441 buf2[11] = 0xff;
442 buf2[12] = 0xfe;
443
444 ret.Set(buf2);
445 return ret;
446}
447
450{
451 NS_LOG_FUNCTION(addr);
452 Ipv6Address ret;
453 uint8_t buf[16];
454 uint8_t buf2[16];
455
456 addr.CopyTo(buf);
457
458 memset(buf2, 0x00, sizeof(buf2));
459 buf2[0] = 0xfe;
460 buf2[1] = 0x80;
461 memcpy(buf2 + 8, buf, 3);
462 buf2[11] = 0xff;
463 buf2[12] = 0xfe;
464 memcpy(buf2 + 13, buf + 3, 3);
465 buf2[8] ^= 0x02;
466
467 ret.Set(buf2);
468 return ret;
469}
470
473{
474 NS_LOG_FUNCTION(addr);
475 Ipv6Address ret;
476 uint8_t buf[8];
477 uint8_t buf2[16];
478
479 addr.CopyTo(buf);
480
481 memset(buf2, 0x00, sizeof(buf2));
482 buf2[0] = 0xfe;
483 buf2[1] = 0x80;
484 memcpy(buf2 + 8, buf, 8);
485
486 ret.Set(buf2);
487 return ret;
488}
489
492{
493 NS_LOG_FUNCTION(addr);
494 Ipv6Address ret;
495 uint8_t buf[2];
496 uint8_t buf2[16];
497
498 buf[0] = 0;
499 addr.CopyTo(&buf[1]);
500
501 memset(buf2, 0x00, sizeof(buf2));
502 buf2[0] = 0xfe;
503 buf2[1] = 0x80;
504 memcpy(buf2 + 14, buf, 2);
505 buf2[11] = 0xff;
506 buf2[12] = 0xfe;
507
508 ret.Set(buf2);
509 return ret;
510}
511
514{
515 NS_LOG_FUNCTION(addr);
516 uint8_t buf[16];
517 uint8_t buf2[16];
518 Ipv6Address ret;
519
520 addr.Serialize(buf2);
521
522 memset(buf, 0x00, sizeof(buf));
523 buf[0] = 0xff;
524 buf[1] = 0x02;
525 buf[11] = 0x01;
526 buf[12] = 0xff;
527 buf[13] = buf2[13];
528 buf[14] = buf2[14];
529 buf[15] = buf2[15];
530
531 ret.Set(buf);
532 return ret;
533}
534
535void
536Ipv6Address::Print(std::ostream& os) const
537{
538 NS_LOG_FUNCTION(this << &os);
539
540 char str[INET6_ADDRSTRLEN];
541
542 if (inet_ntop(AF_INET6, m_address, str, INET6_ADDRSTRLEN))
543 {
544 os << str;
545 }
546}
547
548bool
550{
551 NS_LOG_FUNCTION(this);
552 static Ipv6Address localhost("::1");
553 return (*this == localhost);
554}
555
556bool
558{
559 NS_LOG_FUNCTION(this);
560 return m_address[0] == 0xff;
561}
562
563bool
565{
566 NS_LOG_FUNCTION(this);
567 return m_address[0] == 0xff && m_address[1] == 0x02;
568}
569
570bool
572{
573 NS_LOG_FUNCTION(this);
574 static uint8_t v4MappedPrefix[12] =
575 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff};
576 return memcmp(m_address, v4MappedPrefix, sizeof(v4MappedPrefix)) == 0;
577}
578
581{
582 NS_LOG_FUNCTION(this << prefix);
583 Ipv6Address ipv6;
584 uint8_t addr[16];
585 uint8_t pref[16];
586 unsigned int i = 0;
587
588 memcpy(addr, m_address, 16);
589 ((Ipv6Prefix)prefix).GetBytes(pref);
590
591 /* a little bit ugly... */
592 for (i = 0; i < 16; i++)
593 {
594 addr[i] = addr[i] & pref[i];
595 }
596 ipv6.Set(addr);
597 return ipv6;
598}
599
600bool
602{
603 NS_LOG_FUNCTION(this);
604
605 static Ipv6Address documentation("ff02::1:ff00:0");
606 return CombinePrefix(Ipv6Prefix(104)) == documentation;
607}
608
609bool
611{
612 NS_LOG_FUNCTION(this);
613 static Ipv6Address allNodesI("ff01::1");
614 static Ipv6Address allNodesL("ff02::1");
615 static Ipv6Address allNodesR("ff03::1");
616 return (*this == allNodesI || *this == allNodesL || *this == allNodesR);
617}
618
619bool
621{
622 NS_LOG_FUNCTION(this);
623 static Ipv6Address allroutersI("ff01::2");
624 static Ipv6Address allroutersL("ff02::2");
625 static Ipv6Address allroutersR("ff03::2");
626 static Ipv6Address allroutersS("ff05::2");
627 return (*this == allroutersI || *this == allroutersL || *this == allroutersR ||
628 *this == allroutersS);
629}
630
631bool
633{
634 NS_LOG_FUNCTION(this);
635 static Ipv6Address any("::");
636 return (*this == any);
637}
638
639bool
641{
642 NS_LOG_FUNCTION(this);
643 static Ipv6Address documentation("2001:db8::0");
644 return CombinePrefix(Ipv6Prefix(32)) == documentation;
645}
646
647bool
649{
650 NS_LOG_FUNCTION(this << prefix);
651
652 Ipv6Address masked = CombinePrefix(prefix);
653 Ipv6Address reference = Ipv6Address::GetOnes().CombinePrefix(prefix);
654
655 return masked == reference;
656}
657
658bool
660{
661 NS_LOG_FUNCTION(address);
662 return address.CheckCompatible(GetType(), 16);
663}
664
665Ipv6Address::operator Address() const
666{
667 return ConvertTo();
668}
669
672{
673 NS_LOG_FUNCTION(this);
674 uint8_t buf[16];
675 Serialize(buf);
676 return Address(GetType(), buf, 16);
677}
678
681{
682 NS_LOG_FUNCTION(address);
683 NS_ASSERT(address.CheckCompatible(GetType(), 16));
684 uint8_t buf[16];
685 address.CopyTo(buf);
686 return Deserialize(buf);
687}
688
689uint8_t
691{
693 static uint8_t type = Address::Register();
694 return type;
695}
696
699{
701 static Ipv6Address nmc("ff02::1");
702 return nmc;
703}
704
707{
709 static Ipv6Address rmc("ff02::2");
710 return rmc;
711}
712
715{
717 static Ipv6Address hmc("ff02::3");
718 return hmc;
719}
720
723{
725 static Ipv6Address loopback("::1");
726 return loopback;
727}
728
731{
733 static Ipv6Address zero("::");
734 return zero;
735}
736
739{
741 static Ipv6Address any("::");
742 return any;
743}
744
747{
749 static Ipv6Address ones("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
750 return ones;
751}
752
753void
754Ipv6Address::GetBytes(uint8_t buf[16]) const
755{
756 NS_LOG_FUNCTION(this << &buf);
757 memcpy(buf, m_address, 16);
758}
759
760bool
762{
763 NS_LOG_FUNCTION(this);
764 static Ipv6Address linkLocal("fe80::0");
765 return CombinePrefix(Ipv6Prefix(64)) == linkLocal;
766}
767
768bool
770{
771 NS_LOG_FUNCTION(this);
772 return m_initialized;
773}
774
775std::ostream&
776operator<<(std::ostream& os, const Ipv6Address& address)
777{
778 address.Print(os);
779 return os;
780}
781
782std::istream&
783operator>>(std::istream& is, Ipv6Address& address)
784{
785 std::string str;
786 is >> str;
787 address = Ipv6Address(str.c_str());
788 return is;
789}
790
792{
793 NS_LOG_FUNCTION(this);
794 memset(m_prefix, 0x00, 16);
795 m_prefixLength = 64;
796}
797
798Ipv6Prefix::Ipv6Prefix(const char* prefix)
799{
800 NS_LOG_FUNCTION(this << prefix);
801 if (inet_pton(AF_INET6, prefix, m_prefix) <= 0)
802 {
803 NS_ABORT_MSG("Error, can not build an IPv6 prefix from an invalid string: " << prefix);
804 }
806}
807
808Ipv6Prefix::Ipv6Prefix(uint8_t prefix[16])
809{
810 NS_LOG_FUNCTION(this << &prefix);
811 memcpy(m_prefix, prefix, 16);
813}
814
815Ipv6Prefix::Ipv6Prefix(const char* prefix, uint8_t prefixLength)
816{
817 NS_LOG_FUNCTION(this << prefix);
818 if (inet_pton(AF_INET6, prefix, m_prefix) <= 0)
819 {
820 NS_ABORT_MSG("Error, can not build an IPv6 prefix from an invalid string: " << prefix);
821 }
822 uint8_t autoLength = GetMinimumPrefixLength();
823 NS_ASSERT_MSG(autoLength <= prefixLength,
824 "Ipv6Prefix: address and prefix are not compatible: " << Ipv6Address(prefix)
825 << "/" << +prefixLength);
826
827 m_prefixLength = prefixLength;
828}
829
830Ipv6Prefix::Ipv6Prefix(uint8_t prefix[16], uint8_t prefixLength)
831{
832 NS_LOG_FUNCTION(this << &prefix);
833 memcpy(m_prefix, prefix, 16);
834
835 uint8_t autoLength = GetMinimumPrefixLength();
836 NS_ASSERT_MSG(autoLength <= prefixLength,
837 "Ipv6Prefix: address and prefix are not compatible: " << Ipv6Address(prefix)
838 << "/" << +prefixLength);
839
840 m_prefixLength = prefixLength;
841}
842
844{
845 NS_LOG_FUNCTION(this << static_cast<uint32_t>(prefix));
846 unsigned int nb = 0;
847 unsigned int mod = 0;
848 unsigned int i = 0;
849
850 memset(m_prefix, 0x00, 16);
851 m_prefixLength = prefix;
852
853 NS_ASSERT(prefix <= 128);
854
855 nb = prefix / 8;
856 mod = prefix % 8;
857
858 // protect memset with 'nb > 0' check to suppress
859 // __warn_memset_zero_len compiler errors in some gcc>4.5.x
860 if (nb > 0)
861 {
862 memset(m_prefix, 0xff, nb);
863 }
864 if (mod)
865 {
866 m_prefix[nb] = 0xff << (8 - mod);
867 }
868
869 if (nb < 16)
870 {
871 nb++;
872 for (i = nb; i < 16; i++)
873 {
874 m_prefix[i] = 0x00;
875 }
876 }
877}
878
880{
881 memcpy(m_prefix, prefix.m_prefix, 16);
883}
884
886{
887 memcpy(m_prefix, prefix->m_prefix, 16);
889}
890
892{
893 /* do nothing */
894 NS_LOG_FUNCTION(this);
895}
896
897bool
899{
900 NS_LOG_FUNCTION(this << a << b);
901 uint8_t addrA[16];
902 uint8_t addrB[16];
903 unsigned int i = 0;
904
905 a.GetBytes(addrA);
906 b.GetBytes(addrB);
907
908 /* a little bit ugly... */
909 for (i = 0; i < 16; i++)
910 {
911 if ((addrA[i] & m_prefix[i]) != (addrB[i] & m_prefix[i]))
912 {
913 return false;
914 }
915 }
916 return true;
917}
918
919void
920Ipv6Prefix::Print(std::ostream& os) const
921{
922 NS_LOG_FUNCTION(this << &os);
923
924 os << "/" << (unsigned int)GetPrefixLength();
925}
926
929{
931 static Ipv6Prefix prefix((uint8_t)128);
932 return prefix;
933}
934
937{
939 static Ipv6Prefix ones((uint8_t)128);
940 return ones;
941}
942
945{
947 static Ipv6Prefix prefix((uint8_t)0);
948 return prefix;
949}
950
951void
952Ipv6Prefix::GetBytes(uint8_t buf[16]) const
953{
954 NS_LOG_FUNCTION(this << &buf);
955 memcpy(buf, m_prefix, 16);
956}
957
960{
961 uint8_t prefixBytes[16];
962 memcpy(prefixBytes, m_prefix, 16);
963
964 Ipv6Address convertedPrefix = Ipv6Address(prefixBytes);
965 return convertedPrefix;
966}
967
968uint8_t
970{
971 NS_LOG_FUNCTION(this);
972 return m_prefixLength;
973}
974
975void
976Ipv6Prefix::SetPrefixLength(uint8_t prefixLength)
977{
978 NS_LOG_FUNCTION(this);
979 m_prefixLength = prefixLength;
980}
981
982uint8_t
984{
985 NS_LOG_FUNCTION(this);
986
987 uint8_t prefixLength = 0;
988 bool stop = false;
989
990 for (int8_t i = 15; i >= 0 && !stop; i--)
991 {
992 uint8_t mask = m_prefix[i];
993
994 for (uint8_t j = 0; j < 8 && !stop; j++)
995 {
996 if ((mask & 1) == 0)
997 {
998 mask = mask >> 1;
999 prefixLength++;
1000 }
1001 else
1002 {
1003 stop = true;
1004 }
1005 }
1006 }
1007
1008 return 128 - prefixLength;
1009}
1010
1011std::ostream&
1012operator<<(std::ostream& os, const Ipv6Prefix& prefix)
1013{
1014 prefix.Print(os);
1015 return os;
1016}
1017
1018std::istream&
1019operator>>(std::istream& is, Ipv6Prefix& prefix)
1020{
1021 std::string str;
1022 is >> str;
1023 prefix = Ipv6Prefix(str.c_str());
1024 return is;
1025}
1026
1027size_t
1029{
1030 uint8_t buf[16];
1031
1032 x.GetBytes(buf);
1033
1034 return lookuphash(buf, sizeof(buf), 0);
1035}
1036
1039
1040} /* namespace ns3 */
a polymophic address class
Definition: address.h:101
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition: address.cc:146
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
static Ipv4Address Deserialize(const uint8_t buf[4])
size_t operator()(const Ipv6Address &x) const
Returns the hash of an IPv6 address.
Describes an IPv6 address.
Definition: ipv6-address.h:49
static uint8_t GetType()
Return the Type of address.
bool IsLinkLocal() const
If the IPv6 address is a link-local address (fe80::/64).
bool HasPrefix(const Ipv6Prefix &prefix) const
Compares an address and a prefix.
bool IsSolicitedMulticast() const
If the IPv6 address is a Solicited multicast address.
static Ipv6Address GetAllNodesMulticast()
Get the "all nodes multicast" address.
void Print(std::ostream &os) const
Print this address to the given output stream.
static Ipv6Address MakeSolicitedAddress(Ipv6Address addr)
Make the solicited IPv6 address.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
bool IsDocumentation() const
If the IPv6 address is a documentation address (2001:DB8::/32).
static Ipv6Address GetAllHostsMulticast()
Get the "all hosts multicast" address.
bool IsAllNodesMulticast() const
If the IPv6 address is "all nodes multicast" (ff02::1/8).
bool IsLinkLocalMulticast() const
If the IPv6 address is link-local multicast (ff02::/16).
static Ipv6Address Deserialize(const uint8_t buf[16])
Deserialize this address.
static Ipv6Address GetZero()
Get the 0 (::) Ipv6Address.
static Ipv6Address MakeAutoconfiguredAddress(Address addr, Ipv6Address prefix)
Make the autoconfigured IPv6 address from a Mac address.
~Ipv6Address()
Destructor.
bool IsMulticast() const
If the IPv6 address is multicast (ff00::/8).
void GetBytes(uint8_t buf[16]) const
Get the bytes corresponding to the address.
bool IsIpv4MappedAddress() const
If the address is an IPv4-mapped address.
void Set(const char *address)
Sets an Ipv6Address by parsing the input C-string.
void Serialize(uint8_t buf[16]) const
Serialize this address to a 16-byte buffer.
Ipv6Address()
Default constructor.
Address ConvertTo() const
convert the IPv6Address object to an Address object.
bool IsAny() const
If the IPv6 address is the "Any" address.
static Ipv6Address GetAllRoutersMulticast()
Get the "all routers multicast" address.
bool IsLocalhost() const
If the IPv6 address is localhost (::1).
bool m_initialized
IPv6 address has been explicitly initialized to a valid value.
Definition: ipv6-address.h:417
uint8_t m_address[16]
The address representation on 128 bits (16 bytes).
Definition: ipv6-address.h:416
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.
bool IsInitialized() const
static Ipv6Address GetOnes()
Get the "all-1" IPv6 address (ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff).
static Ipv6Address MakeAutoconfiguredLinkLocalAddress(Address mac)
Make the autoconfigured link-local IPv6 address from a Mac address.
Ipv4Address GetIpv4MappedAddress() const
Return the Ipv4 address.
static bool IsMatchingType(const Address &address)
If the Address matches the type.
static Ipv6Address MakeIpv4MappedAddress(Ipv4Address addr)
Make the Ipv4-mapped IPv6 address.
static Ipv6Address GetLoopback()
Get the loopback address.
Ipv6Address CombinePrefix(const Ipv6Prefix &prefix) const
Combine this address with a prefix.
bool IsAllRoutersMulticast() const
If the IPv6 address is "all routers multicast" (ff02::2/8).
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
uint8_t m_prefixLength
The prefix length.
Definition: ipv6-address.h:594
static Ipv6Prefix GetLoopback()
Get the loopback prefix ( /128).
~Ipv6Prefix()
Destructor.
uint8_t m_prefix[16]
The prefix representation.
Definition: ipv6-address.h:589
void Print(std::ostream &os) const
Print this address to the given output stream.
uint8_t GetPrefixLength() const
Get prefix length.
Ipv6Address ConvertToIpv6Address() const
Convert the Prefix into an IPv6 Address.
static Ipv6Prefix GetZero()
Get the zero prefix ( /0).
bool IsMatch(Ipv6Address a, Ipv6Address b) const
If the Address match the type.
static Ipv6Prefix GetOnes()
Get the "all-1" IPv6 mask (ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff).
void SetPrefixLength(uint8_t prefixLength)
Set prefix length.
Ipv6Prefix()
Default constructor.
uint8_t GetMinimumPrefixLength() const
Get the minimum prefix length, i.e., 128 - the length of the largest sequence trailing zeroes.
void GetBytes(uint8_t buf[16]) const
Get the bytes corresponding to the prefix.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
static bool IsMatchingType(const Address &address)
static Mac16Address ConvertFrom(const Address &address)
void CopyTo(uint8_t buffer[2]) const
an EUI-48 address
Definition: mac48-address.h:46
static bool IsMatchingType(const Address &address)
static Mac48Address ConvertFrom(const Address &address)
void CopyTo(uint8_t buffer[6]) const
an EUI-64 address
Definition: mac64-address.h:46
static bool IsMatchingType(const Address &address)
void CopyTo(uint8_t buffer[8]) const
static Mac64Address ConvertFrom(const Address &address)
A class used for addressing MAC8 MAC's.
Definition: mac8-address.h:44
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
Definition: mac8-address.cc:56
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
Definition: mac8-address.cc:65
void CopyTo(uint8_t *pBuffer) const
Writes address to buffer parameter.
Definition: mac8-address.cc:82
static double zero
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
void mixHashKey(uint32_t &a, uint32_t &b, uint32_t &c)
Mix hash keys in-place for lookuphash.
Definition: ipv6-address.cc:54
static uint32_t lookuphash(unsigned char *k, uint32_t length, uint32_t level)
Get a hash key.
Definition: ipv6-address.cc:93