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 <ostream>
18#include <stdint.h>
19
20namespace ns3
21{
22
23class Address;
24
25/**
26 * @ingroup address
27 *
28 * This class can contain 16 bit addresses.
29 *
30 * @see attribute_Mac16Address
31 */
33{
34 public:
35 Mac16Address() = default;
36 /**
37 * @param str a string representing the new Mac16Address
38 *
39 * The format of the string is "xx:xx"
40 */
41 Mac16Address(const char* str);
42
43 /**
44 * @param addr The 16 bit unsigned integer used to create a Mac16Address object.
45 *
46 * Create a Mac16Address from an 16 bit unsigned integer.
47 */
48 Mac16Address(uint16_t addr);
49
50 /**
51 * @param buffer address in network order
52 *
53 * Copy the input address to our internal buffer.
54 */
55 void CopyFrom(const uint8_t buffer[2]);
56
57 /**
58 * @param buffer address in network order
59 *
60 * Copy the internal address to the input buffer.
61 */
62 void CopyTo(uint8_t buffer[2]) const;
63
64 /**
65 * @returns a new Address instance
66 *
67 * Convert an instance of this class to a polymorphic Address instance.
68 */
69 operator Address() const;
70
71 /**
72 * @param address a polymorphic address
73 * @returns a new Mac16Address from the polymorphic address
74 *
75 * This function performs a type check and asserts if the
76 * type of the input address is not compatible with an
77 * Mac16Address.
78 */
79 static Mac16Address ConvertFrom(const Address& address);
80
81 /**
82 * @returns a new Address instance
83 *
84 * Convert an instance of this class to a polymorphic Address instance.
85 */
86 Address ConvertTo() const;
87
88 /**
89 * @return the mac address in a 16 bit unsigned integer
90 *
91 * Convert an instance of this class to a 16 bit unsigned integer.
92 */
93 uint16_t ConvertToInt() const;
94
95 /**
96 * @param address address to test
97 * @returns true if the address matches, false otherwise.
98 */
99 static bool IsMatchingType(const Address& address);
100
101 /**
102 * Allocate a new Mac16Address.
103 * @returns newly allocated mac16Address
104 */
105 static Mac16Address Allocate();
106
107 /**
108 * Reset the Mac16Address allocation index.
109 *
110 * This function resets (to zero) the global integer
111 * that is used for unique address allocation.
112 * It is automatically called whenever
113 * @code
114 * SimulatorDestroy ();
115 * @endcode
116 * is called. It may also be optionally called
117 * by user code if there is a need to force a reset
118 * of this allocation index.
119 */
120 static void ResetAllocationIndex();
121
122 /**
123 * @returns the broadcast address (0xFFFF)
124 */
125 static Mac16Address GetBroadcast();
126
127 /**
128 * Returns the multicast address associated with an IPv6 address
129 * according to RFC 4944 Section 9.
130 * An IPv6 packet with a multicast destination address (DST),
131 * consisting of the sixteen octets DST[1] through DST[16], is
132 * transmitted to the following 802.15.4 16-bit multicast address:
133
134 \verbatim
135 0 1
136 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
137 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138 |1 0 0|DST[15]* | DST[16] |
139 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140 \endverbatim
141
142 * Here, DST[15]* refers to the last 5 bits in octet DST[15], that is,
143 * bits 3-7 within DST[15]. The initial 3-bit pattern of "100" follows
144 * the 16-bit address format for multicast addresses (Section 12).
145 *
146 * @param address base IPv6 address
147 * @returns the multicast 16-bit address.
148 */
149
150 static Mac16Address GetMulticast(Ipv6Address address);
151
152 /**
153 * Checks if the address is a broadcast address according
154 * to 802.15.4 scheme (i.e., 0xFFFF).
155 *
156 * @returns true if the address is 0xFFFF
157 */
158 bool IsBroadcast() const;
159
160 /**
161 * Checks if the address is a multicast address according
162 * to RFC 4944 Section 9 (i.e., if its first 3 bits are 100).
163 *
164 * @returns true if the address is in the range 0x8000 - 0x9FFF
165 */
166 bool IsMulticast() const;
167
168 private:
169 /**
170 * @brief Return the Type of address.
171 * @return type of address
172 */
173 static uint8_t GetType();
174
175 /**
176 * @brief Equal to operator.
177 *
178 * @param a the first operand
179 * @param b the first operand
180 * @returns true if the operands are equal
181 */
182 friend bool operator==(const Mac16Address& a, const Mac16Address& b);
183
184 /**
185 * @brief Not equal to operator.
186 *
187 * @param a the first operand
188 * @param b the first operand
189 * @returns true if the operands are not equal
190 */
191 friend bool operator!=(const Mac16Address& a, const Mac16Address& b);
192
193 /**
194 * @brief Less than operator.
195 *
196 * @param a the first operand
197 * @param b the first operand
198 * @returns true if the operand a is less than operand b
199 */
200 friend bool operator<(const Mac16Address& a, const Mac16Address& b);
201
202 /**
203 * @brief Stream insertion operator.
204 *
205 * @param os the stream
206 * @param address the address
207 * @returns a reference to the stream
208 */
209 friend std::ostream& operator<<(std::ostream& os, const Mac16Address& address);
210
211 /**
212 * @brief Stream extraction operator.
213 *
214 * @param is the stream
215 * @param address the address
216 * @returns a reference to the stream
217 */
218 friend std::istream& operator>>(std::istream& is, Mac16Address& address);
219
220 static uint64_t m_allocationIndex; //!< Address allocation index
221 uint8_t m_address[2]{0}; //!< Address value
222};
223
225
226inline bool
228{
229 return memcmp(a.m_address, b.m_address, 2) == 0;
230}
231
232inline bool
234{
235 return memcmp(a.m_address, b.m_address, 2) != 0;
236}
237
238inline bool
239operator<(const Mac16Address& a, const Mac16Address& b)
240{
241 return memcmp(a.m_address, b.m_address, 2) < 0;
242}
243
244std::ostream& operator<<(std::ostream& os, const Mac16Address& address);
245std::istream& operator>>(std::istream& is, Mac16Address& address);
246
247} // namespace ns3
248
249#endif /* MAC16_ADDRESS_H */
a polymophic address class
Definition address.h:90
Describes an IPv6 address.
This class can contain 16 bit addresses.
friend bool operator<(const Mac16Address &a, const Mac16Address &b)
Less than operator.
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)
static uint64_t m_allocationIndex
Address allocation index.
friend bool operator!=(const Mac16Address &a, const Mac16Address &b)
Not equal to operator.
static Mac16Address ConvertFrom(const Address &address)
friend std::istream & operator>>(std::istream &is, Mac16Address &address)
Stream extraction operator.
Mac16Address()=default
uint16_t ConvertToInt() const
void CopyTo(uint8_t buffer[2]) const
friend bool operator==(const Mac16Address &a, const Mac16Address &b)
Equal to operator.
static Mac16Address Allocate()
Allocate a new Mac16Address.
Address ConvertTo() const
void CopyFrom(const uint8_t buffer[2])
bool IsMulticast() const
Checks if the address is a multicast address according to RFC 4944 Section 9 (i.e....
uint8_t m_address[2]
Address value.
static void ResetAllocationIndex()
Reset the Mac16Address allocation index.
static Mac16Address GetBroadcast()
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.
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition callback.h:658
bool operator==(const EventId &a, const EventId &b)
Definition event-id.h:155
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
bool operator<(const EventId &a, const EventId &b)
Definition event-id.h:168