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