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  if (m_address[0] == 0xff)
556  {
557  return true;
558  }
559  return false;
560 }
561 
562 bool
564 {
565  NS_LOG_FUNCTION(this);
566  if (m_address[0] == 0xff && m_address[1] == 0x02)
567  {
568  return true;
569  }
570  return false;
571 }
572 
573 bool
575 {
576  NS_LOG_FUNCTION(this);
577  static uint8_t v4MappedPrefix[12] =
578  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff};
579  if (memcmp(m_address, v4MappedPrefix, sizeof(v4MappedPrefix)) == 0)
580  {
581  return (true);
582  }
583  return (false);
584 }
585 
588 {
589  NS_LOG_FUNCTION(this << prefix);
590  Ipv6Address ipv6;
591  uint8_t addr[16];
592  uint8_t pref[16];
593  unsigned int i = 0;
594 
595  memcpy(addr, m_address, 16);
596  ((Ipv6Prefix)prefix).GetBytes(pref);
597 
598  /* a little bit ugly... */
599  for (i = 0; i < 16; i++)
600  {
601  addr[i] = addr[i] & pref[i];
602  }
603  ipv6.Set(addr);
604  return ipv6;
605 }
606 
607 bool
609 {
610  NS_LOG_FUNCTION(this);
611 
612  static Ipv6Address documentation("ff02::1:ff00:0");
613  if (CombinePrefix(Ipv6Prefix(104)) == documentation)
614  {
615  return true;
616  }
617  return false;
618 }
619 
620 bool
622 {
623  NS_LOG_FUNCTION(this);
624  static Ipv6Address allNodesI("ff01::1");
625  static Ipv6Address allNodesL("ff02::1");
626  static Ipv6Address allNodesR("ff03::1");
627  return (*this == allNodesI || *this == allNodesL || *this == allNodesR);
628 }
629 
630 bool
632 {
633  NS_LOG_FUNCTION(this);
634  static Ipv6Address allroutersI("ff01::2");
635  static Ipv6Address allroutersL("ff02::2");
636  static Ipv6Address allroutersR("ff03::2");
637  static Ipv6Address allroutersS("ff05::2");
638  return (*this == allroutersI || *this == allroutersL || *this == allroutersR ||
639  *this == allroutersS);
640 }
641 
642 bool
644 {
645  NS_LOG_FUNCTION(this);
646  static Ipv6Address any("::");
647  return (*this == any);
648 }
649 
650 bool
652 {
653  NS_LOG_FUNCTION(this);
654  static Ipv6Address documentation("2001:db8::0");
655  if (CombinePrefix(Ipv6Prefix(32)) == documentation)
656  {
657  return true;
658  }
659  return false;
660 }
661 
662 bool
663 Ipv6Address::HasPrefix(const Ipv6Prefix& prefix) const
664 {
665  NS_LOG_FUNCTION(this << prefix);
666 
667  Ipv6Address masked = CombinePrefix(prefix);
669 
670  return (masked == reference);
671 }
672 
673 bool
675 {
677  return address.CheckCompatible(GetType(), 16);
678 }
679 
680 Ipv6Address::operator Address() const
681 {
682  return ConvertTo();
683 }
684 
685 Address
687 {
688  NS_LOG_FUNCTION(this);
689  uint8_t buf[16];
690  Serialize(buf);
691  return Address(GetType(), buf, 16);
692 }
693 
696 {
698  NS_ASSERT(address.CheckCompatible(GetType(), 16));
699  uint8_t buf[16];
700  address.CopyTo(buf);
701  return Deserialize(buf);
702 }
703 
704 uint8_t
706 {
708  static uint8_t type = Address::Register();
709  return type;
710 }
711 
714 {
716  static Ipv6Address nmc("ff02::1");
717  return nmc;
718 }
719 
722 {
724  static Ipv6Address rmc("ff02::2");
725  return rmc;
726 }
727 
730 {
732  static Ipv6Address hmc("ff02::3");
733  return hmc;
734 }
735 
738 {
740  static Ipv6Address loopback("::1");
741  return loopback;
742 }
743 
746 {
748  static Ipv6Address zero("::");
749  return zero;
750 }
751 
754 {
756  static Ipv6Address any("::");
757  return any;
758 }
759 
762 {
764  static Ipv6Address ones("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
765  return ones;
766 }
767 
768 void
769 Ipv6Address::GetBytes(uint8_t buf[16]) const
770 {
771  NS_LOG_FUNCTION(this << &buf);
772  memcpy(buf, m_address, 16);
773 }
774 
775 bool
777 {
778  NS_LOG_FUNCTION(this);
779  static Ipv6Address linkLocal("fe80::0");
780  if (CombinePrefix(Ipv6Prefix(64)) == linkLocal)
781  {
782  return true;
783  }
784  return false;
785 }
786 
787 bool
789 {
790  NS_LOG_FUNCTION(this);
791  return (m_initialized);
792 }
793 
794 std::ostream&
795 operator<<(std::ostream& os, const Ipv6Address& address)
796 {
797  address.Print(os);
798  return os;
799 }
800 
801 std::istream&
802 operator>>(std::istream& is, Ipv6Address& address)
803 {
804  std::string str;
805  is >> str;
806  address = Ipv6Address(str.c_str());
807  return is;
808 }
809 
811 {
812  NS_LOG_FUNCTION(this);
813  memset(m_prefix, 0x00, 16);
814  m_prefixLength = 64;
815 }
816 
817 Ipv6Prefix::Ipv6Prefix(const char* prefix)
818 {
819  NS_LOG_FUNCTION(this << prefix);
820  if (inet_pton(AF_INET6, prefix, m_prefix) <= 0)
821  {
822  NS_ABORT_MSG("Error, can not build an IPv6 prefix from an invalid string: " << prefix);
823  }
825 }
826 
827 Ipv6Prefix::Ipv6Prefix(uint8_t prefix[16])
828 {
829  NS_LOG_FUNCTION(this << &prefix);
830  memcpy(m_prefix, prefix, 16);
832 }
833 
834 Ipv6Prefix::Ipv6Prefix(const char* prefix, uint8_t prefixLength)
835 {
836  NS_LOG_FUNCTION(this << prefix);
837  if (inet_pton(AF_INET6, prefix, m_prefix) <= 0)
838  {
839  NS_ABORT_MSG("Error, can not build an IPv6 prefix from an invalid string: " << prefix);
840  }
841  uint8_t autoLength = GetMinimumPrefixLength();
842  NS_ASSERT_MSG(autoLength <= prefixLength,
843  "Ipv6Prefix: address and prefix are not compatible: " << Ipv6Address(prefix)
844  << "/" << +prefixLength);
845 
846  m_prefixLength = prefixLength;
847 }
848 
849 Ipv6Prefix::Ipv6Prefix(uint8_t prefix[16], uint8_t prefixLength)
850 {
851  NS_LOG_FUNCTION(this << &prefix);
852  memcpy(m_prefix, prefix, 16);
853 
854  uint8_t autoLength = GetMinimumPrefixLength();
855  NS_ASSERT_MSG(autoLength <= prefixLength,
856  "Ipv6Prefix: address and prefix are not compatible: " << Ipv6Address(prefix)
857  << "/" << +prefixLength);
858 
859  m_prefixLength = prefixLength;
860 }
861 
862 Ipv6Prefix::Ipv6Prefix(uint8_t prefix)
863 {
864  NS_LOG_FUNCTION(this << static_cast<uint32_t>(prefix));
865  unsigned int nb = 0;
866  unsigned int mod = 0;
867  unsigned int i = 0;
868 
869  memset(m_prefix, 0x00, 16);
870  m_prefixLength = prefix;
871 
872  NS_ASSERT(prefix <= 128);
873 
874  nb = prefix / 8;
875  mod = prefix % 8;
876 
877  // protect memset with 'nb > 0' check to suppress
878  // __warn_memset_zero_len compiler errors in some gcc>4.5.x
879  if (nb > 0)
880  {
881  memset(m_prefix, 0xff, nb);
882  }
883  if (mod)
884  {
885  m_prefix[nb] = 0xff << (8 - mod);
886  }
887 
888  if (nb < 16)
889  {
890  nb++;
891  for (i = nb; i < 16; i++)
892  {
893  m_prefix[i] = 0x00;
894  }
895  }
896 }
897 
899 {
900  memcpy(m_prefix, prefix.m_prefix, 16);
902 }
903 
905 {
906  memcpy(m_prefix, prefix->m_prefix, 16);
907  m_prefixLength = prefix->m_prefixLength;
908 }
909 
911 {
912  /* do nothing */
913  NS_LOG_FUNCTION(this);
914 }
915 
916 bool
918 {
919  NS_LOG_FUNCTION(this << a << b);
920  uint8_t addrA[16];
921  uint8_t addrB[16];
922  unsigned int i = 0;
923 
924  a.GetBytes(addrA);
925  b.GetBytes(addrB);
926 
927  /* a little bit ugly... */
928  for (i = 0; i < 16; i++)
929  {
930  if ((addrA[i] & m_prefix[i]) != (addrB[i] & m_prefix[i]))
931  {
932  return false;
933  }
934  }
935  return true;
936 }
937 
938 void
939 Ipv6Prefix::Print(std::ostream& os) const
940 {
941  NS_LOG_FUNCTION(this << &os);
942 
943  os << "/" << (unsigned int)GetPrefixLength();
944 }
945 
948 {
950  static Ipv6Prefix prefix((uint8_t)128);
951  return prefix;
952 }
953 
956 {
958  static Ipv6Prefix ones((uint8_t)128);
959  return ones;
960 }
961 
964 {
966  static Ipv6Prefix prefix((uint8_t)0);
967  return prefix;
968 }
969 
970 void
971 Ipv6Prefix::GetBytes(uint8_t buf[16]) const
972 {
973  NS_LOG_FUNCTION(this << &buf);
974  memcpy(buf, m_prefix, 16);
975 }
976 
979 {
980  uint8_t prefixBytes[16];
981  memcpy(prefixBytes, m_prefix, 16);
982 
983  Ipv6Address convertedPrefix = Ipv6Address(prefixBytes);
984  return convertedPrefix;
985 }
986 
987 uint8_t
989 {
990  NS_LOG_FUNCTION(this);
991  return m_prefixLength;
992 }
993 
994 void
995 Ipv6Prefix::SetPrefixLength(uint8_t prefixLength)
996 {
997  NS_LOG_FUNCTION(this);
998  m_prefixLength = prefixLength;
999 }
1000 
1001 uint8_t
1003 {
1004  NS_LOG_FUNCTION(this);
1005 
1006  uint8_t prefixLength = 0;
1007  bool stop = false;
1008 
1009  for (int8_t i = 15; i >= 0 && !stop; i--)
1010  {
1011  uint8_t mask = m_prefix[i];
1012 
1013  for (uint8_t j = 0; j < 8 && !stop; j++)
1014  {
1015  if ((mask & 1) == 0)
1016  {
1017  mask = mask >> 1;
1018  prefixLength++;
1019  }
1020  else
1021  {
1022  stop = true;
1023  }
1024  }
1025  }
1026 
1027  return 128 - prefixLength;
1028 }
1029 
1030 std::ostream&
1031 operator<<(std::ostream& os, const Ipv6Prefix& prefix)
1032 {
1033  prefix.Print(os);
1034  return os;
1035 }
1036 
1037 std::istream&
1038 operator>>(std::istream& is, Ipv6Prefix& prefix)
1039 {
1040  std::string str;
1041  is >> str;
1042  prefix = Ipv6Prefix(str.c_str());
1043  return is;
1044 }
1045 
1046 size_t
1048 {
1049  uint8_t buf[16];
1050 
1051  x.GetBytes(buf);
1052 
1053  return lookuphash(buf, sizeof(buf), 0);
1054 }
1055 
1058 
1059 } /* namespace ns3 */
a polymophic address class
Definition: address.h:100
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:43
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:50
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:418
uint8_t m_address[16]
The address representation on 128 bits (16 bytes).
Definition: ipv6-address.h:417
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:456
uint8_t m_prefixLength
The prefix length.
Definition: ipv6-address.h:595
static Ipv6Prefix GetLoopback()
Get the loopback prefix ( /128).
~Ipv6Prefix()
Destructor.
uint8_t m_prefix[16]
The prefix representation.
Definition: ipv6-address.h:590
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:61
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
Definition: mac8-address.cc:70
void CopyTo(uint8_t *pBuffer) const
Writes address to buffer parameter.
Definition: mac8-address.cc:87
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:40
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ATTRIBUTE_HELPER_CPP(Length)
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:153
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129