A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-address.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007-2008 Louis Pasteur University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
7 */
8
9#ifndef IPV6_ADDRESS_H
10#define IPV6_ADDRESS_H
11
12#include "ipv4-address.h"
13#include "mac8-address.h"
14
15#include "ns3/address.h"
16#include "ns3/attribute-helper.h"
17#include "ns3/deprecated.h"
18
19#include <array>
20#include <compare>
21#include <cstring>
22#include <ostream>
23#include <stdint.h>
24
25namespace ns3
26{
27
28class Ipv6Prefix;
29class Mac16Address;
30class Mac48Address;
31class Mac64Address;
32
33/**
34 * @ingroup address
35 * @class Ipv6Address
36 * @brief Describes an IPv6 address.
37 * @see Ipv6Prefix
38 * @see attribute_Ipv6Address
39 */
41{
42 public:
43 /**
44 * @brief Default constructor.
45 */
46 Ipv6Address() = default;
47
48 /**
49 * @brief Constructs an Ipv6Address by parsing the input C-string.
50 * @param address the C-string containing the IPv6 address (e.g. 2001:db8:f00d:cafe::1).
51 */
52 Ipv6Address(const char* address);
53
54 /**
55 * @brief Checks if the string contains an Ipv6Address
56 *
57 * Input address is in format:
58 * \c 2001:db8:f00d:cafe::1
59 *
60 * Note: the function uses ``inet_pton`` internally.
61 *
62 * @see Address::CheckCompatible hich has a similar name but which
63 * instead checks the underlying type and length embedded in the Address.
64 *
65 * @param addressStr string containing the address as described above
66 * @return true if the string can be parsed as an IPv6 address
67 */
68 static bool CheckCompatible(const std::string& addressStr);
69
70 /**
71 * @brief Constructs an Ipv6Address by using the input 16 bytes.
72 * @param address the 128-bit address
73 * @warning the parameter must point on a 16 bytes integer array!
74 */
75 Ipv6Address(uint8_t address[16]);
76
77 /**
78 * @brief Sets an Ipv6Address by parsing the input C-string.
79 * @param address the C-string containing the IPv6 address (e.g. 2001:db8:f00d:cafe::1).
80 */
81 void Set(const char* address);
82
83 /**
84 * @brief Set an Ipv6Address by using the input 16 bytes.
85 *
86 * @param address the 128-bit address
87 * @warning the parameter must point on a 16 bytes integer array!
88 */
89 void Set(uint8_t address[16]);
90
91 /**
92 * @brief Serialize this address to a 16-byte buffer.
93 * @param buf the output buffer to which this address gets overwritten with this
94 * Ipv6Address
95 */
96 void Serialize(uint8_t buf[16]) const;
97
98 /**
99 * @brief Deserialize this address.
100 * @param buf buffer to read address from
101 * @return an Ipv6Address
102 */
103 static Ipv6Address Deserialize(const uint8_t buf[16]);
104
105 /**
106 * @brief Make the solicited IPv6 address.
107 * @param addr the IPv6 address
108 * @return Solicited IPv6 address
109 */
111
112 /**
113 * @brief Make the Ipv4-mapped IPv6 address.
114 * @param addr the IPv4 address
115 * @return Ipv4-mapped IPv6 address
116 */
118
119 /**
120 * @brief Return the Ipv4 address.
121 * @return Ipv4 address
122 */
124
125 /**
126 * @brief Make the autoconfigured IPv6 address from a Mac address.
127 *
128 * Actually the MAC supported are: Mac8, Mac16, Mac48, and Mac64.
129 *
130 * @param addr the MAC address.
131 * @param prefix the IPv6 prefix
132 * @return autoconfigured IPv6 address
133 */
135
136 /**
137 * @brief Make the autoconfigured IPv6 address from a Mac address.
138 *
139 * Actually the MAC supported are: Mac8, Mac16, Mac48, and Mac64.
140 *
141 * @param addr the MAC address.
142 * @param prefix the IPv6 prefix
143 * @return autoconfigured IPv6 address
144 */
145
147
148 /**
149 * @brief Make the autoconfigured IPv6 address with Mac16Address.
150 *
151 * The EUI-64 scheme used is based on the \RFC{4944}.
152 *
153 * @param addr the MAC address (16 bits).
154 * @param prefix the IPv6 prefix
155 * @return autoconfigured IPv6 address
156 */
158
159 /**
160 * @brief Make the autoconfigured IPv6 address with Mac48Address.
161 *
162 * The EUI-64 scheme used is based on \RFC{2464}.
163 *
164 * @param addr the MAC address (48 bits).
165 * @param prefix the IPv6 prefix
166 * @return autoconfigured IPv6 address
167 */
169
170 /**
171 * @brief Make the autoconfigured IPv6 address with Mac64Address.
172 * @param addr the MAC address (64 bits).
173 * @param prefix the IPv6 prefix
174 * @return autoconfigured IPv6 address
175 */
177
178 /**
179 * @brief Make the autoconfigured IPv6 address with Mac8Address.
180 *
181 * The EUI-64 scheme used is loosely based on the \RFC{2464}.
182 *
183 * @param addr the Mac8Address address (8 bits).
184 * @param prefix the IPv6 prefix
185 * @return autoconfigured IPv6 address
186 */
188
189 /**
190 * @brief Make the autoconfigured link-local IPv6 address from a Mac address.
191 *
192 * Actually the MAC supported are: Mac8, Mac16, Mac48, and Mac64.
193 *
194 * @param mac the MAC address.
195 * @return autoconfigured link-local IPv6 address
196 */
198
199 /**
200 * @brief Make the autoconfigured link-local IPv6 address with Mac16Address.
201 *
202 * The EUI-64 scheme used is based on the \RFC{4944}.
203 *
204 * @param mac the MAC address (16 bits).
205 * @return autoconfigured link-local IPv6 address
206 */
208
209 /**
210 * @brief Make the autoconfigured link-local IPv6 address with Mac48Address.
211 *
212 * The EUI-64 scheme used is based on \RFC{2464}.
213 *
214 * @param mac the MAC address (48 bits).
215 * @return autoconfigured link-local IPv6 address
216 */
218
219 /**
220 * @brief Make the autoconfigured link-local IPv6 address with Mac64Address.
221 * @param mac the MAC address (64 bits).
222 * @return autoconfigured link-local IPv6 address
223 */
225
226 /**
227 * @brief Make the autoconfigured link-local IPv6 address with Mac8Address.
228 *
229 * The EUI-64 scheme used is loosely based on the \RFC{2464}.
230 *
231 * @param mac the MAC address (8 bits).
232 * @return autoconfigured link-local IPv6 address
233 */
235
236 /**
237 * @brief Print this address to the given output stream.
238 *
239 * The print format is in the typical "2001:660:4701::1".
240 * @param os the output stream to which this Ipv6Address is printed
241 */
242 void Print(std::ostream& os) const;
243
244 /**
245 * @brief If the IPv6 address is localhost (::1).
246 * @return true if localhost, false otherwise
247 */
248 bool IsLocalhost() const;
249
250 /**
251 * @brief If the IPv6 address is multicast (ff00::/8).
252 * @return true if multicast, false otherwise
253 */
254 bool IsMulticast() const;
255
256 /**
257 * @brief If the IPv6 address is link-local multicast (ff02::/16).
258 * @return true if link-local multicast, false otherwise
259 */
260 bool IsLinkLocalMulticast() const;
261
262 /**
263 * @brief If the IPv6 address is "all nodes multicast" (ff02::1/8).
264 * @return true if "all nodes multicast", false otherwise
265 */
266 bool IsAllNodesMulticast() const;
267
268 /**
269 * @brief If the IPv6 address is "all routers multicast" (ff02::2/8).
270 * @return true if "all routers multicast", false otherwise
271 */
272 bool IsAllRoutersMulticast() const;
273
274 /**
275 * @brief If the IPv6 address is a link-local address (fe80::/64).
276 * @return true if the address is link-local, false otherwise
277 */
278 bool IsLinkLocal() const;
279
280 /**
281 * @brief If the IPv6 address is a Solicited multicast address.
282 * @return true if it is, false otherwise
283 */
284 bool IsSolicitedMulticast() const;
285
286 /**
287 * @brief If the IPv6 address is the "Any" address.
288 * @return true if it is, false otherwise
289 */
290 bool IsAny() const;
291
292 /**
293 * @brief If the IPv6 address is a documentation address (2001:DB8::/32).
294 * @return true if the address is documentation, false otherwise
295 */
296 bool IsDocumentation() const;
297
298 /**
299 * @brief Compares an address and a prefix.
300 * @param prefix the prefix to compare with
301 * @return true if the address has the given prefix
302 */
303 bool HasPrefix(const Ipv6Prefix& prefix) const;
304
305 /**
306 * @brief Combine this address with a prefix.
307 * @param prefix a IPv6 prefix
308 * @return an IPv6 address that is this address combined
309 * (bitwise AND) with a prefix, yielding an IPv6 network address.
310 */
311 Ipv6Address CombinePrefix(const Ipv6Prefix& prefix) const;
312
313 /**
314 * @brief If the Address matches the type.
315 * @param address other address
316 * @return true if the type matches, false otherwise
317 */
318 static bool IsMatchingType(const Address& address);
319
320 /**
321 * @brief If the address is an IPv4-mapped address
322 * @return true if address is an IPv4-mapped address, otherwise false.
323 */
324 bool IsIpv4MappedAddress() const;
325
326 /**
327 * @brief Convert to Address object
328 */
329 operator Address() const;
330
331 /**
332 * @brief Convert the Address object into an Ipv6Address ones.
333 * @param address address to convert
334 * @return an Ipv6Address
335 */
336 static Ipv6Address ConvertFrom(const Address& address);
337
338 /**
339 * @brief convert the IPv6Address object to an Address object.
340 * @return the Address object corresponding to this object.
341 */
342 Address ConvertTo() const;
343
344 /**
345 * @return true if address is initialized (i.e., set to something), false otherwise
346 */
347 NS_DEPRECATED_3_47("Use IsAny or std::optional")
348 bool IsInitialized() const;
349
350 /**
351 * @brief Get the 0 (::) Ipv6Address.
352 * @return the :: Ipv6Address representation
353 */
354 static Ipv6Address GetZero();
355
356 /**
357 * @brief Get the "any" (::) Ipv6Address.
358 * @return the "any" (::) Ipv6Address
359 */
360 static Ipv6Address GetAny();
361
362 /**
363 * @brief Get the "all nodes multicast" address.
364 * @return the "ff02::2/8" Ipv6Address representation
365 */
367
368 /**
369 * @brief Get the "all routers multicast" address.
370 * @return the "ff02::2/8" Ipv6Address representation
371 */
373
374 /**
375 * @brief Get the "all hosts multicast" address.
376 * @return the "ff02::3/8" Ipv6Address representation
377 */
379
380 /**
381 * @brief Get the loopback address.
382 * @return the "::1/128" Ipv6Address representation.
383 */
384 static Ipv6Address GetLoopback();
385
386 /**
387 * @brief Get the "all-1" IPv6 address (ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff).
388 * @return all-1 Ipv6Address representation
389 */
390 static Ipv6Address GetOnes();
391
392 /**
393 * @brief Get the bytes corresponding to the address.
394 * @param buf buffer to store the data
395 */
396 void GetBytes(uint8_t buf[16]) const;
397
398 /**
399 * @brief Three-way comparison operator.
400 *
401 * @param other the other address to compare with
402 * @returns comparison result
403 */
404 std::strong_ordering operator<=>(const Ipv6Address& other) const = default;
405
406 private:
407 /**
408 * @brief Return the Type of address.
409 * @return type of address
410 */
411 static uint8_t GetType();
412 /**
413 * @brief The address representation on 128 bits (16 bytes).
414 */
415 std::array<uint8_t, 16> m_address{};
416};
417
418/**
419 * @ingroup address
420 * @class Ipv6Prefix
421 * @brief Describes an IPv6 prefix. It is just a bitmask like Ipv4Mask.
422 * @see Ipv6Address
423 * @see attribute_Ipv6Prefix
424 */
426{
427 public:
428 /**
429 * @brief Default constructor.
430 *
431 * The default prefix is empty, corresponding to /0
432 */
433 Ipv6Prefix() = default;
434
435 /**
436 * @brief Constructs an Ipv6Prefix by using the input 16 bytes.
437 *
438 * The prefix length is calculated as the minimum prefix length, i.e.,
439 * 2001:db8:cafe:: will have a 47 bit prefix length (0xE is 0b1110).
440 *
441 * @param prefix the 128-bit prefix
442 */
443 Ipv6Prefix(uint8_t prefix[16]);
444
445 /**
446 * @brief Constructs an Ipv6Prefix by using the input string.
447 *
448 * The prefix length is calculated as the minimum prefix length, i.e.,
449 * 2001:db8:cafe:: will have a 47 bit prefix length.
450 *
451 * @param prefix the 128-bit prefix
452 */
453 Ipv6Prefix(const char* prefix);
454
455 /**
456 * @brief Constructs an Ipv6Prefix by using the input 16 bytes.
457 * @param prefix the 128-bit prefix
458 * @param prefixLength the prefix length
459 */
460 Ipv6Prefix(uint8_t prefix[16], uint8_t prefixLength);
461
462 /**
463 * @brief Constructs an Ipv6Prefix by using the input string.
464 * @param prefix the 128-bit prefix
465 * @param prefixLength the prefix length
466 */
467 Ipv6Prefix(const char* prefix, uint8_t prefixLength);
468
469 /**
470 * @brief Constructs an Ipv6Prefix by using the input number of bits.
471 * @param prefix number of bits of the prefix (0 - 128)
472 * @note A valid number of bits is between 0 and 128).
473 */
474 Ipv6Prefix(uint8_t prefix);
475
476 /**
477 * @brief Check whether two addresses have the same bits in the prefix
478 * portion of their addresses.
479 *
480 * If the prefix length is 0, this method will return true regardless
481 * of the two address argument values. If the prefix length is 128,
482 * this method will require that the two address arguments are the same.
483 *
484 * @param a first address to compare
485 * @param b second address to compare
486 * @return true if both addresses are equal in their masked bits,
487 * corresponding to the prefix length.
488 */
489 bool IsMatch(Ipv6Address a, Ipv6Address b) const;
490
491 /**
492 * @brief Get the bytes corresponding to the prefix.
493 * @param buf buffer to store the data
494 */
495 void GetBytes(uint8_t buf[16]) const;
496
497 /**
498 * @brief Convert the Prefix into an IPv6 Address.
499 * @return an IPv6 address representing the prefix
500 */
502
503 /**
504 * @brief Get prefix length.
505 * @return prefix length
506 */
507 uint8_t GetPrefixLength() const;
508
509 /**
510 * @brief Set prefix length.
511 * @param prefixLength the prefix length
512 */
513 void SetPrefixLength(uint8_t prefixLength);
514
515 /**
516 * @brief Get the minimum prefix length, i.e., 128 - the length of the largest sequence
517 * trailing zeroes.
518 * @return minimum prefix length
519 */
520 uint8_t GetMinimumPrefixLength() const;
521
522 /**
523 * @brief Print this address to the given output stream.
524 *
525 * The print format is in the typical "2001:660:4701::1".
526 * @param os the output stream to which this Ipv6Address is printed
527 */
528 void Print(std::ostream& os) const;
529
530 /**
531 * @brief Get the loopback prefix ( /128).
532 * @return a Ipv6Prefix corresponding to loopback prefix
533 */
534 static Ipv6Prefix GetLoopback();
535
536 /**
537 * @brief Get the "all-1" IPv6 mask (ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff).
538 * @return /128 Ipv6Prefix representation
539 */
540 static Ipv6Prefix GetOnes();
541
542 /**
543 * @brief Get the zero prefix ( /0).
544 * @return an Ipv6Prefix
545 */
546 static Ipv6Prefix GetZero();
547
548 /**
549 * @brief Three-way comparison operator.
550 *
551 * @param other the other prefix to compare with
552 * @returns comparison result
553 */
554 std::strong_ordering operator<=>(const Ipv6Prefix& other) const = default;
555
556 private:
557 /**
558 * @brief The prefix representation.
559 */
560 std::array<uint8_t, 16> m_prefix{};
561
562 /**
563 * @brief The prefix length.
564 */
565 uint8_t m_prefixLength{0};
566};
567
570
571/**
572 * @brief Stream insertion operator.
573 *
574 * @param os the reference to the output stream
575 * @param address the Ipv6Address
576 * @returns the reference to the output stream
577 */
578std::ostream& operator<<(std::ostream& os, const Ipv6Address& address);
579
580/**
581 * @brief Stream insertion operator.
582 *
583 * @param os the reference to the output stream
584 * @param prefix the Ipv6Prefix
585 * @returns the reference to the output stream
586 */
587std::ostream& operator<<(std::ostream& os, const Ipv6Prefix& prefix);
588
589/**
590 * @brief Stream extraction operator.
591 *
592 * @param is the reference to the input stream
593 * @param address the Ipv6Address
594 * @returns the reference to the input stream
595 */
596std::istream& operator>>(std::istream& is, Ipv6Address& address);
597
598/**
599 * @brief Stream extraction operator.
600 *
601 * @param is the reference to the input stream
602 * @param prefix the Ipv6Prefix
603 * @returns the reference to the input stream
604 */
605std::istream& operator>>(std::istream& is, Ipv6Prefix& prefix);
606
607/**
608 * @class Ipv6AddressHash
609 * @brief Hash function class for IPv6 addresses.
610 */
612{
613 public:
614 /**
615 * @brief Returns the hash of an IPv6 address.
616 * @param x IPv6 address to hash
617 * @returns the hash of the address
618 */
619 size_t operator()(const Ipv6Address& x) const;
620};
621
622} /* namespace ns3 */
623
624#endif /* IPV6_ADDRESS_H */
cairo_uint64_t x
_cairo_uint_96by64_32x64_divrem:
a polymophic address class
Definition address.h:114
Ipv4 addresses are stored in host order in this class.
Hash function class for IPv6 addresses.
size_t operator()(const Ipv6Address &x) const
Returns the hash of an IPv6 address.
Describes an IPv6 address.
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.
Ipv6Address()=default
Default constructor.
bool IsAllNodesMulticast() const
If the IPv6 address is "all nodes multicast" (ff02::1/8).
static bool CheckCompatible(const std::string &addressStr)
Checks if the string contains an Ipv6Address.
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.
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.
std::array< uint8_t, 16 > m_address
The address representation on 128 bits (16 bytes).
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).
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.
uint8_t m_prefixLength
The prefix length.
std::array< uint8_t, 16 > m_prefix
The prefix representation.
static Ipv6Prefix GetLoopback()
Get the loopback prefix ( /128).
std::strong_ordering operator<=>(const Ipv6Prefix &other) const =default
Three-way comparison operator.
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.
Ipv6Prefix()=default
Default constructor.
static Ipv6Prefix GetZero()
Get the zero prefix ( /0).
bool IsMatch(Ipv6Address a, Ipv6Address b) const
Check whether two addresses have the same bits in the prefix portion of their addresses.
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.
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.
an EUI-48 address
an EUI-64 address
A class used for addressing MAC8 MAC's.
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type.
#define NS_DEPRECATED_3_47(msg)
Tag for things deprecated in version ns-3.47.
Definition deprecated.h:91
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:148
std::istream & operator>>(std::istream &is, Angles &a)
Definition angles.cc:172
STL namespace.
#define private