A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
mac16-address.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 * Copyright (c) 2011 The Boeing Company
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8#ifndef MAC16_ADDRESS_H
9#define MAC16_ADDRESS_H
10
11#include "ipv4-address.h"
12#include "ipv6-address.h"
13
14#include "ns3/attribute-helper.h"
15#include "ns3/attribute.h"
16
17#include <array>
18#include <compare>
19#include <ostream>
20#include <stdint.h>
21
22namespace ns3
23{
24
25class Address;
26
27/**
28 * @ingroup address
29 *
30 * This class can contain 16 bit addresses.
31 *
32 * @see attribute_Mac16Address
33 */
35{
36 public:
37 /// Address length when converted to an Address [bytes].
38 static constexpr uint8_t ADDRESS_LENGTH = 2;
39
40 Mac16Address() = default;
41 /**
42 * @param str a string representing the new Mac16Address
43 *
44 * The format of the string is "xx:xx"
45 */
46 Mac16Address(const char* str);
47
48 /**
49 * @param addr The 16 bit unsigned integer used to create a Mac16Address object.
50 *
51 * Create a Mac16Address from an 16 bit unsigned integer.
52 */
53 Mac16Address(uint16_t addr);
54
55 /**
56 * @param buffer address in network order
57 *
58 * Copy the input address to our internal buffer.
59 */
60 void CopyFrom(const uint8_t buffer[ADDRESS_LENGTH]);
61
62 /**
63 * @param buffer address in network order
64 *
65 * Copy the internal address to the input buffer.
66 */
67 void CopyTo(uint8_t buffer[ADDRESS_LENGTH]) const;
68
69 /**
70 * @returns a new Address instance
71 *
72 * Convert an instance of this class to a polymorphic Address instance.
73 */
74 operator Address() const;
75
76 /**
77 * @param address a polymorphic address
78 * @returns a new Mac16Address from the polymorphic address
79 *
80 * This function performs a type check and asserts if the
81 * type of the input address is not compatible with an
82 * Mac16Address.
83 */
84 static Mac16Address ConvertFrom(const Address& address);
85
86 /**
87 * @returns a new Address instance
88 *
89 * Convert an instance of this class to a polymorphic Address instance.
90 */
91 Address ConvertTo() const;
92
93 /**
94 * @return the mac address in a 16 bit unsigned integer
95 *
96 * Convert an instance of this class to a 16 bit unsigned integer.
97 */
98 uint16_t ConvertToInt() const;
99
100 /**
101 * @param address address to test
102 * @returns true if the address matches, false otherwise.
103 */
104 static bool IsMatchingType(const Address& address);
105
106 /**
107 * Allocate a new Mac16Address.
108 * @returns newly allocated mac16Address
109 */
110 static Mac16Address Allocate();
111
112 /**
113 * Reset the Mac16Address allocation index.
114 *
115 * This function resets (to zero) the global integer
116 * that is used for unique address allocation.
117 * It is automatically called whenever
118 * @code
119 * SimulatorDestroy ();
120 * @endcode
121 * is called. It may also be optionally called
122 * by user code if there is a need to force a reset
123 * of this allocation index.
124 */
125 static void ResetAllocationIndex();
126
127 /**
128 * @returns the broadcast address (0xFFFF)
129 */
130 static Mac16Address GetBroadcast();
131
132 /**
133 * Returns the multicast address associated with an IPv6 address
134 * according to RFC 4944 Section 9.
135 * An IPv6 packet with a multicast destination address (DST),
136 * consisting of the sixteen octets DST[1] through DST[16], is
137 * transmitted to the following 802.15.4 16-bit multicast address:
138
139 @verbatim
140 0 1
141 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
142 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 |1 0 0|DST[15]* | DST[16] |
144 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 @endverbatim
146
147 * Here, DST[15]* refers to the last 5 bits in octet DST[15], that is,
148 * bits 3-7 within DST[15]. The initial 3-bit pattern of "100" follows
149 * the 16-bit address format for multicast addresses (Section 12).
150 *
151 * @param address base IPv6 address
152 * @returns the multicast 16-bit address.
153 */
154
155 static Mac16Address GetMulticast(Ipv6Address address);
156
157 /**
158 * Checks if the address is a broadcast address according
159 * to 802.15.4 scheme (i.e., 0xFFFF).
160 *
161 * @returns true if the address is 0xFFFF
162 */
163 bool IsBroadcast() const;
164
165 /**
166 * Checks if the address is a multicast address according
167 * to RFC 4944 Section 9 (i.e., if its first 3 bits are 100).
168 *
169 * @returns true if the address is in the range 0x8000 - 0x9FFF
170 */
171 bool IsMulticast() const;
172
173 /**
174 * Spaceship comparison operator. All the other comparison operators
175 * are automatically generated from this one.
176 *
177 * @param other address to compare to this one
178 * @returns The result of the comparison.
179 */
180 constexpr std::strong_ordering operator<=>(const Mac16Address& other) const = default;
181
182 private:
183 /**
184 * @brief Return the Type of address.
185 * @return type of address
186 */
187 static uint8_t GetType();
188
189 /**
190 * @brief Stream insertion operator.
191 *
192 * @param os the stream
193 * @param address the address
194 * @returns a reference to the stream
195 */
196 friend std::ostream& operator<<(std::ostream& os, const Mac16Address& address);
197
198 /**
199 * @brief Stream extraction operator.
200 *
201 * @param is the stream
202 * @param address the address
203 * @returns a reference to the stream
204 */
205 friend std::istream& operator>>(std::istream& is, Mac16Address& address);
206
207 static uint64_t m_allocationIndex; //!< Address allocation index
208 std::array<uint8_t, ADDRESS_LENGTH> m_address{}; //!< Address value
209};
210
212
213std::ostream& operator<<(std::ostream& os, const Mac16Address& address);
214std::istream& operator>>(std::istream& is, Mac16Address& address);
215
216} // namespace ns3
217
218#endif /* MAC16_ADDRESS_H */
a polymophic address class
Definition address.h:114
Describes an IPv6 address.
This class can contain 16 bit addresses.
std::array< uint8_t, ADDRESS_LENGTH > m_address
Address value.
static Mac16Address GetMulticast(Ipv6Address address)
Returns the multicast address associated with an IPv6 address according to RFC 4944 Section 9.
static bool IsMatchingType(const Address &address)
constexpr std::strong_ordering operator<=>(const Mac16Address &other) const =default
Spaceship comparison operator.
static uint64_t m_allocationIndex
Address allocation index.
static Mac16Address ConvertFrom(const Address &address)
friend std::istream & operator>>(std::istream &is, Mac16Address &address)
Stream extraction operator.
Mac16Address()=default
uint16_t ConvertToInt() const
static constexpr uint8_t ADDRESS_LENGTH
Address length when converted to an Address [bytes].
static Mac16Address Allocate()
Allocate a new Mac16Address.
Address ConvertTo() const
bool IsMulticast() const
Checks if the address is a multicast address according to RFC 4944 Section 9 (i.e....
static void ResetAllocationIndex()
Reset the Mac16Address allocation index.
static Mac16Address GetBroadcast()
void CopyFrom(const uint8_t buffer[ADDRESS_LENGTH])
void CopyTo(uint8_t buffer[ADDRESS_LENGTH]) const
bool IsBroadcast() const
Checks if the address is a broadcast address according to 802.15.4 scheme (i.e., 0xFFFF).
static uint8_t GetType()
Return the Type of address.
friend std::ostream & operator<<(std::ostream &os, const Mac16Address &address)
Stream insertion operator.
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type.
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