A Discrete-Event Network Simulator
API
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 
38 namespace ns3
39 {
40 
41 NS_LOG_COMPONENT_DEFINE("Ipv6Address");
42 
43 #ifdef __cplusplus
44 extern "C"
45 { /* } */
46 #endif
47 
56  static uint32_t lookuphash(unsigned char* k, uint32_t length, uint32_t level)
57  {
58  NS_LOG_FUNCTION(k << length << level);
59 #define mix(a, b, c) \
60  ({ \
61  (a) -= (b); \
62  (a) -= (c); \
63  (a) ^= ((c) >> 13); \
64  (b) -= (c); \
65  (b) -= (a); \
66  (b) ^= ((a) << 8); \
67  (c) -= (a); \
68  (c) -= (b); \
69  (c) ^= ((b) >> 13); \
70  (a) -= (b); \
71  (a) -= (c); \
72  (a) ^= ((c) >> 12); \
73  (b) -= (c); \
74  (b) -= (a); \
75  (b) ^= ((a) << 16); \
76  (c) -= (a); \
77  (c) -= (b); \
78  (c) ^= ((b) >> 5); \
79  (a) -= (b); \
80  (a) -= (c); \
81  (a) ^= ((c) >> 3); \
82  (b) -= (c); \
83  (b) -= (a); \
84  (b) ^= ((a) << 10); \
85  (c) -= (a); \
86  (c) -= (b); \
87  (c) ^= ((b) >> 15); \
88  })
89 
90  typedef uint32_t ub4; /* unsigned 4-byte quantities */
91  uint32_t a = 0;
92  uint32_t b = 0;
93  uint32_t c = 0;
94  uint32_t len = 0;
95 
96  /* Set up the internal state */
97  len = length;
98  a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
99  c = level; /* the previous hash value */
100 
101  /* handle most of the key */
102  while (len >= 12)
103  {
104  a += (k[0] + ((ub4)k[1] << 8) + ((ub4)k[2] << 16) + ((ub4)k[3] << 24));
105  b += (k[4] + ((ub4)k[5] << 8) + ((ub4)k[6] << 16) + ((ub4)k[7] << 24));
106  c += (k[8] + ((ub4)k[9] << 8) + ((ub4)k[10] << 16) + ((ub4)k[11] << 24));
107  mix(a, b, c);
108  k += 12;
109  len -= 12;
110  }
111 
112  /* handle the last 11 bytes */
113  c += length;
114  switch (len) /* all the case statements fall through */
115  {
116  case 11:
117  c += ((ub4)k[10] << 24);
118  case 10:
119  c += ((ub4)k[9] << 16);
120  case 9:
121  c += ((ub4)k[8] << 8); /* the first byte of c is reserved for the length */
122  case 8:
123  b += ((ub4)k[7] << 24);
124  case 7:
125  b += ((ub4)k[6] << 16);
126  case 6:
127  b += ((ub4)k[5] << 8);
128  case 5:
129  b += k[4];
130  case 4:
131  a += ((ub4)k[3] << 24);
132  case 3:
133  a += ((ub4)k[2] << 16);
134  case 2:
135  a += ((ub4)k[1] << 8);
136  case 1:
137  a += k[0];
138  /* case 0: nothing left to add */
139  }
140  mix(a, b, c);
141 
142 #undef mix
143 
144  /* report the result */
145  return c;
146  }
147 
148 #ifdef __cplusplus
149 }
150 #endif
151 
153 {
154  NS_LOG_FUNCTION(this);
155  memset(m_address, 0x00, 16);
156  m_initialized = false;
157 }
158 
160 {
161  // Do not add function logging here, to avoid stack overflow
162  memcpy(m_address, addr.m_address, 16);
163  m_initialized = true;
164 }
165 
167 {
168  // Do not add function logging here, to avoid stack overflow
169  memcpy(m_address, addr->m_address, 16);
170  m_initialized = true;
171 }
172 
174 {
175  NS_LOG_FUNCTION(this << address);
176 
177  if (inet_pton(AF_INET6, address, m_address) <= 0)
178  {
179  memset(m_address, 0x00, 16);
180  NS_LOG_LOGIC("Error, can not build an IPv6 address from an invalid string: " << address);
181  m_initialized = false;
182  return;
183  }
184  m_initialized = true;
185 }
186 
188 {
189  NS_LOG_FUNCTION(this << &address);
190  /* 128 bit => 16 bytes */
191  memcpy(m_address, address, 16);
192  m_initialized = true;
193 }
194 
196 {
197  /* do nothing */
198  NS_LOG_FUNCTION(this);
199 }
200 
201 void
203 {
204  NS_LOG_FUNCTION(this << address);
205  if (inet_pton(AF_INET6, address, m_address) <= 0)
206  {
207  memset(m_address, 0x00, 16);
208  NS_LOG_LOGIC("Error, can not build an IPv6 address from an invalid string: " << address);
209  m_initialized = false;
210  return;
211  }
212  m_initialized = true;
213 }
214 
215 void
217 {
218  /* 128 bit => 16 bytes */
219  NS_LOG_FUNCTION(this << &address);
220  memcpy(m_address, address, 16);
221  m_initialized = true;
222 }
223 
224 void
225 Ipv6Address::Serialize(uint8_t buf[16]) const
226 {
227  NS_LOG_FUNCTION(this << &buf);
228  memcpy(buf, m_address, 16);
229 }
230 
232 Ipv6Address::Deserialize(const uint8_t buf[16])
233 {
234  NS_LOG_FUNCTION(&buf);
235  Ipv6Address ipv6((uint8_t*)buf);
236  ipv6.m_initialized = true;
237  return ipv6;
238 }
239 
242 {
243  NS_LOG_FUNCTION(addr);
244  uint8_t buf[16] = {
245  0x00,
246  0x00,
247  0x00,
248  0x00,
249  0x00,
250  0x00,
251  0x00,
252  0x00,
253  0x00,
254  0x00,
255  0xff,
256  0xff,
257  0x00,
258  0x00,
259  0x00,
260  0x00,
261  };
262  addr.Serialize(&buf[12]);
263  return Ipv6Address(buf);
264 }
265 
268 {
269  NS_LOG_FUNCTION(this);
270  uint8_t buf[16];
271  Ipv4Address v4Addr;
272 
273  Serialize(buf);
274  v4Addr = Ipv4Address::Deserialize(&buf[12]);
275  return v4Addr;
276 }
277 
280 {
281  Ipv6Address ipv6Addr = Ipv6Address::GetAny();
282 
284  {
286  }
287  else if (Mac48Address::IsMatchingType(addr))
288  {
290  }
291  else if (Mac16Address::IsMatchingType(addr))
292  {
294  }
295  else if (Mac8Address::IsMatchingType(addr))
296  {
298  }
299 
300  if (ipv6Addr.IsAny())
301  {
302  NS_ABORT_MSG("Unknown address type");
303  }
304  return ipv6Addr;
305 }
306 
309 {
310  Ipv6Address ipv6PrefixAddr = Ipv6Address::GetOnes().CombinePrefix(prefix);
311  return MakeAutoconfiguredAddress(addr, ipv6PrefixAddr);
312 }
313 
316 {
317  NS_LOG_FUNCTION(addr << prefix);
318  Ipv6Address ret;
319  uint8_t buf[2];
320  uint8_t buf2[16];
321 
322  addr.CopyTo(buf);
323  prefix.GetBytes(buf2);
324  memset(buf2 + 8, 0, 8);
325 
326  memcpy(buf2 + 14, buf, 2);
327  buf2[11] = 0xff;
328  buf2[12] = 0xfe;
329 
330  ret.Set(buf2);
331  return ret;
332 }
333 
336 {
337  NS_LOG_FUNCTION(addr << prefix);
338  Ipv6Address ret;
339  uint8_t buf[16];
340  uint8_t buf2[16];
341 
342  addr.CopyTo(buf);
343  prefix.GetBytes(buf2);
344 
345  memcpy(buf2 + 8, buf, 3);
346  buf2[11] = 0xff;
347  buf2[12] = 0xfe;
348  memcpy(buf2 + 13, buf + 3, 3);
349  buf2[8] ^= 0x02;
350 
351  ret.Set(buf2);
352  return ret;
353 }
354 
357 {
358  NS_LOG_FUNCTION(addr << prefix);
359  Ipv6Address ret;
360  uint8_t buf[8];
361  uint8_t buf2[16];
362 
363  addr.CopyTo(buf);
364  prefix.GetBytes(buf2);
365 
366  memcpy(buf2 + 8, buf, 8);
367 
368  ret.Set(buf2);
369  return ret;
370 }
371 
374 {
375  NS_LOG_FUNCTION(addr << prefix);
376  Ipv6Address ret;
377  uint8_t buf[2];
378  uint8_t buf2[16];
379 
380  buf[0] = 0;
381  addr.CopyTo(&buf[1]);
382  prefix.GetBytes(buf2);
383  memset(buf2 + 8, 0, 8);
384 
385  memcpy(buf2 + 14, buf, 2);
386  buf2[11] = 0xff;
387  buf2[12] = 0xfe;
388 
389  ret.Set(buf2);
390  return ret;
391 }
392 
395 {
396  Ipv6Address ipv6Addr = Ipv6Address::GetAny();
397 
399  {
401  }
402  else if (Mac48Address::IsMatchingType(addr))
403  {
405  }
406  else if (Mac16Address::IsMatchingType(addr))
407  {
409  }
410  else if (Mac8Address::IsMatchingType(addr))
411  {
413  }
414 
415  if (ipv6Addr.IsAny())
416  {
417  NS_ABORT_MSG("Unknown address type");
418  }
419  return ipv6Addr;
420 }
421 
424 {
425  NS_LOG_FUNCTION(addr);
426  Ipv6Address ret;
427  uint8_t buf[2];
428  uint8_t buf2[16];
429 
430  addr.CopyTo(buf);
431 
432  memset(buf2, 0x00, sizeof(buf2));
433  buf2[0] = 0xfe;
434  buf2[1] = 0x80;
435  memcpy(buf2 + 14, buf, 2);
436  buf2[11] = 0xff;
437  buf2[12] = 0xfe;
438 
439  ret.Set(buf2);
440  return ret;
441 }
442 
445 {
446  NS_LOG_FUNCTION(addr);
447  Ipv6Address ret;
448  uint8_t buf[16];
449  uint8_t buf2[16];
450 
451  addr.CopyTo(buf);
452 
453  memset(buf2, 0x00, sizeof(buf2));
454  buf2[0] = 0xfe;
455  buf2[1] = 0x80;
456  memcpy(buf2 + 8, buf, 3);
457  buf2[11] = 0xff;
458  buf2[12] = 0xfe;
459  memcpy(buf2 + 13, buf + 3, 3);
460  buf2[8] ^= 0x02;
461 
462  ret.Set(buf2);
463  return ret;
464 }
465 
468 {
469  NS_LOG_FUNCTION(addr);
470  Ipv6Address ret;
471  uint8_t buf[8];
472  uint8_t buf2[16];
473 
474  addr.CopyTo(buf);
475 
476  memset(buf2, 0x00, sizeof(buf2));
477  buf2[0] = 0xfe;
478  buf2[1] = 0x80;
479  memcpy(buf2 + 8, buf, 8);
480 
481  ret.Set(buf2);
482  return ret;
483 }
484 
487 {
488  NS_LOG_FUNCTION(addr);
489  Ipv6Address ret;
490  uint8_t buf[2];
491  uint8_t buf2[16];
492 
493  buf[0] = 0;
494  addr.CopyTo(&buf[1]);
495 
496  memset(buf2, 0x00, sizeof(buf2));
497  buf2[0] = 0xfe;
498  buf2[1] = 0x80;
499  memcpy(buf2 + 14, buf, 2);
500  buf2[11] = 0xff;
501  buf2[12] = 0xfe;
502 
503  ret.Set(buf2);
504  return ret;
505 }
506 
509 {
510  NS_LOG_FUNCTION(addr);
511  uint8_t buf[16];
512  uint8_t buf2[16];
513  Ipv6Address ret;
514 
515  addr.Serialize(buf2);
516 
517  memset(buf, 0x00, sizeof(buf));
518  buf[0] = 0xff;
519  buf[1] = 0x02;
520  buf[11] = 0x01;
521  buf[12] = 0xff;
522  buf[13] = buf2[13];
523  buf[14] = buf2[14];
524  buf[15] = buf2[15];
525 
526  ret.Set(buf);
527  return ret;
528 }
529 
530 void
531 Ipv6Address::Print(std::ostream& os) const
532 {
533  NS_LOG_FUNCTION(this << &os);
534 
535  char str[INET6_ADDRSTRLEN];
536 
537  if (inet_ntop(AF_INET6, m_address, str, INET6_ADDRSTRLEN))
538  {
539  os << str;
540  }
541 }
542 
543 bool
545 {
546  NS_LOG_FUNCTION(this);
547  static Ipv6Address localhost("::1");
548  return (*this == localhost);
549 }
550 
551 bool
553 {
554  NS_LOG_FUNCTION(this);
555  return m_address[0] == 0xff;
556 }
557 
558 bool
560 {
561  NS_LOG_FUNCTION(this);
562  return m_address[0] == 0xff && m_address[1] == 0x02;
563 }
564 
565 bool
567 {
568  NS_LOG_FUNCTION(this);
569  static uint8_t v4MappedPrefix[12] =
570  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff};
571  return memcmp(m_address, v4MappedPrefix, sizeof(v4MappedPrefix)) == 0;
572 }
573 
576 {
577  NS_LOG_FUNCTION(this << prefix);
578  Ipv6Address ipv6;
579  uint8_t addr[16];
580  uint8_t pref[16];
581  unsigned int i = 0;
582 
583  memcpy(addr, m_address, 16);
584  ((Ipv6Prefix)prefix).GetBytes(pref);
585 
586  /* a little bit ugly... */
587  for (i = 0; i < 16; i++)
588  {
589  addr[i] = addr[i] & pref[i];
590  }
591  ipv6.Set(addr);
592  return ipv6;
593 }
594 
595 bool
597 {
598  NS_LOG_FUNCTION(this);
599 
600  static Ipv6Address documentation("ff02::1:ff00:0");
601  return CombinePrefix(Ipv6Prefix(104)) == documentation;
602 }
603 
604 bool
606 {
607  NS_LOG_FUNCTION(this);
608  static Ipv6Address allNodesI("ff01::1");
609  static Ipv6Address allNodesL("ff02::1");
610  static Ipv6Address allNodesR("ff03::1");
611  return (*this == allNodesI || *this == allNodesL || *this == allNodesR);
612 }
613 
614 bool
616 {
617  NS_LOG_FUNCTION(this);
618  static Ipv6Address allroutersI("ff01::2");
619  static Ipv6Address allroutersL("ff02::2");
620  static Ipv6Address allroutersR("ff03::2");
621  static Ipv6Address allroutersS("ff05::2");
622  return (*this == allroutersI || *this == allroutersL || *this == allroutersR ||
623  *this == allroutersS);
624 }
625 
626 bool
628 {
629  NS_LOG_FUNCTION(this);
630  static Ipv6Address any("::");
631  return (*this == any);
632 }
633 
634 bool
636 {
637  NS_LOG_FUNCTION(this);
638  static Ipv6Address documentation("2001:db8::0");
639  return CombinePrefix(Ipv6Prefix(32)) == documentation;
640 }
641 
642 bool
643 Ipv6Address::HasPrefix(const Ipv6Prefix& prefix) const
644 {
645  NS_LOG_FUNCTION(this << prefix);
646 
647  Ipv6Address masked = CombinePrefix(prefix);
649 
650  return masked == reference;
651 }
652 
653 bool
655 {
657  return address.CheckCompatible(GetType(), 16);
658 }
659 
660 Ipv6Address::operator Address() const
661 {
662  return ConvertTo();
663 }
664 
665 Address
667 {
668  NS_LOG_FUNCTION(this);
669  uint8_t buf[16];
670  Serialize(buf);
671  return Address(GetType(), buf, 16);
672 }
673 
676 {
678  NS_ASSERT(address.CheckCompatible(GetType(), 16));
679  uint8_t buf[16];
680  address.CopyTo(buf);
681  return Deserialize(buf);
682 }
683 
684 uint8_t
686 {
688  static uint8_t type = Address::Register();
689  return type;
690 }
691 
694 {
696  static Ipv6Address nmc("ff02::1");
697  return nmc;
698 }
699 
702 {
704  static Ipv6Address rmc("ff02::2");
705  return rmc;
706 }
707 
710 {
712  static Ipv6Address hmc("ff02::3");
713  return hmc;
714 }
715 
718 {
720  static Ipv6Address loopback("::1");
721  return loopback;
722 }
723 
726 {
728  static Ipv6Address zero("::");
729  return zero;
730 }
731 
734 {
736  static Ipv6Address any("::");
737  return any;
738 }
739 
742 {
744  static Ipv6Address ones("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
745  return ones;
746 }
747 
748 void
749 Ipv6Address::GetBytes(uint8_t buf[16]) const
750 {
751  NS_LOG_FUNCTION(this << &buf);
752  memcpy(buf, m_address, 16);
753 }
754 
755 bool
757 {
758  NS_LOG_FUNCTION(this);
759  static Ipv6Address linkLocal("fe80::0");
760  return CombinePrefix(Ipv6Prefix(64)) == linkLocal;
761 }
762 
763 bool
765 {
766  NS_LOG_FUNCTION(this);
767  return m_initialized;
768 }
769 
770 std::ostream&
771 operator<<(std::ostream& os, const Ipv6Address& address)
772 {
773  address.Print(os);
774  return os;
775 }
776 
777 std::istream&
778 operator>>(std::istream& is, Ipv6Address& address)
779 {
780  std::string str;
781  is >> str;
782  address = Ipv6Address(str.c_str());
783  return is;
784 }
785 
787 {
788  NS_LOG_FUNCTION(this);
789  memset(m_prefix, 0x00, 16);
790  m_prefixLength = 64;
791 }
792 
793 Ipv6Prefix::Ipv6Prefix(const char* prefix)
794 {
795  NS_LOG_FUNCTION(this << prefix);
796  if (inet_pton(AF_INET6, prefix, m_prefix) <= 0)
797  {
798  NS_ABORT_MSG("Error, can not build an IPv6 prefix from an invalid string: " << prefix);
799  }
801 }
802 
803 Ipv6Prefix::Ipv6Prefix(uint8_t prefix[16])
804 {
805  NS_LOG_FUNCTION(this << &prefix);
806  memcpy(m_prefix, prefix, 16);
808 }
809 
810 Ipv6Prefix::Ipv6Prefix(const char* prefix, uint8_t prefixLength)
811 {
812  NS_LOG_FUNCTION(this << prefix);
813  if (inet_pton(AF_INET6, prefix, m_prefix) <= 0)
814  {
815  NS_ABORT_MSG("Error, can not build an IPv6 prefix from an invalid string: " << prefix);
816  }
817  uint8_t autoLength = GetMinimumPrefixLength();
818  NS_ASSERT_MSG(autoLength <= prefixLength,
819  "Ipv6Prefix: address and prefix are not compatible: " << Ipv6Address(prefix)
820  << "/" << +prefixLength);
821 
822  m_prefixLength = prefixLength;
823 }
824 
825 Ipv6Prefix::Ipv6Prefix(uint8_t prefix[16], uint8_t prefixLength)
826 {
827  NS_LOG_FUNCTION(this << &prefix);
828  memcpy(m_prefix, prefix, 16);
829 
830  uint8_t autoLength = GetMinimumPrefixLength();
831  NS_ASSERT_MSG(autoLength <= prefixLength,
832  "Ipv6Prefix: address and prefix are not compatible: " << Ipv6Address(prefix)
833  << "/" << +prefixLength);
834 
835  m_prefixLength = prefixLength;
836 }
837 
838 Ipv6Prefix::Ipv6Prefix(uint8_t prefix)
839 {
840  NS_LOG_FUNCTION(this << static_cast<uint32_t>(prefix));
841  unsigned int nb = 0;
842  unsigned int mod = 0;
843  unsigned int i = 0;
844 
845  memset(m_prefix, 0x00, 16);
846  m_prefixLength = prefix;
847 
848  NS_ASSERT(prefix <= 128);
849 
850  nb = prefix / 8;
851  mod = prefix % 8;
852 
853  // protect memset with 'nb > 0' check to suppress
854  // __warn_memset_zero_len compiler errors in some gcc>4.5.x
855  if (nb > 0)
856  {
857  memset(m_prefix, 0xff, nb);
858  }
859  if (mod)
860  {
861  m_prefix[nb] = 0xff << (8 - mod);
862  }
863 
864  if (nb < 16)
865  {
866  nb++;
867  for (i = nb; i < 16; i++)
868  {
869  m_prefix[i] = 0x00;
870  }
871  }
872 }
873 
875 {
876  memcpy(m_prefix, prefix.m_prefix, 16);
878 }
879 
881 {
882  memcpy(m_prefix, prefix->m_prefix, 16);
883  m_prefixLength = prefix->m_prefixLength;
884 }
885 
887 {
888  /* do nothing */
889  NS_LOG_FUNCTION(this);
890 }
891 
892 bool
894 {
895  NS_LOG_FUNCTION(this << a << b);
896  uint8_t addrA[16];
897  uint8_t addrB[16];
898  unsigned int i = 0;
899 
900  a.GetBytes(addrA);
901  b.GetBytes(addrB);
902 
903  /* a little bit ugly... */
904  for (i = 0; i < 16; i++)
905  {
906  if ((addrA[i] & m_prefix[i]) != (addrB[i] & m_prefix[i]))
907  {
908  return false;
909  }
910  }
911  return true;
912 }
913 
914 void
915 Ipv6Prefix::Print(std::ostream& os) const
916 {
917  NS_LOG_FUNCTION(this << &os);
918 
919  os << "/" << (unsigned int)GetPrefixLength();
920 }
921 
924 {
926  static Ipv6Prefix prefix((uint8_t)128);
927  return prefix;
928 }
929 
932 {
934  static Ipv6Prefix ones((uint8_t)128);
935  return ones;
936 }
937 
940 {
942  static Ipv6Prefix prefix((uint8_t)0);
943  return prefix;
944 }
945 
946 void
947 Ipv6Prefix::GetBytes(uint8_t buf[16]) const
948 {
949  NS_LOG_FUNCTION(this << &buf);
950  memcpy(buf, m_prefix, 16);
951 }
952 
955 {
956  uint8_t prefixBytes[16];
957  memcpy(prefixBytes, m_prefix, 16);
958 
959  Ipv6Address convertedPrefix = Ipv6Address(prefixBytes);
960  return convertedPrefix;
961 }
962 
963 uint8_t
965 {
966  NS_LOG_FUNCTION(this);
967  return m_prefixLength;
968 }
969 
970 void
971 Ipv6Prefix::SetPrefixLength(uint8_t prefixLength)
972 {
973  NS_LOG_FUNCTION(this);
974  m_prefixLength = prefixLength;
975 }
976 
977 uint8_t
979 {
980  NS_LOG_FUNCTION(this);
981 
982  uint8_t prefixLength = 0;
983  bool stop = false;
984 
985  for (int8_t i = 15; i >= 0 && !stop; i--)
986  {
987  uint8_t mask = m_prefix[i];
988 
989  for (uint8_t j = 0; j < 8 && !stop; j++)
990  {
991  if ((mask & 1) == 0)
992  {
993  mask = mask >> 1;
994  prefixLength++;
995  }
996  else
997  {
998  stop = true;
999  }
1000  }
1001  }
1002 
1003  return 128 - prefixLength;
1004 }
1005 
1006 std::ostream&
1007 operator<<(std::ostream& os, const Ipv6Prefix& prefix)
1008 {
1009  prefix.Print(os);
1010  return os;
1011 }
1012 
1013 std::istream&
1014 operator>>(std::istream& is, Ipv6Prefix& prefix)
1015 {
1016  std::string str;
1017  is >> str;
1018  prefix = Ipv6Prefix(str.c_str());
1019  return is;
1020 }
1021 
1022 size_t
1024 {
1025  uint8_t buf[16];
1026 
1027  x.GetBytes(buf);
1028 
1029  return lookuphash(buf, sizeof(buf), 0);
1030 }
1031 
1034 
1035 } /* 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 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 ",...
#define mix(a, b, c)
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ATTRIBUTE_HELPER_CPP(ValueClassTest)
static uint32_t lookuphash(unsigned char *k, uint32_t length, uint32_t level)
Get a hash key.
Definition: ipv6-address.cc:56
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159