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