A Discrete-Event Network Simulator
API
mac16-address.cc
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
20#include "mac16-address.h"
21
22#include "ns3/address.h"
23#include "ns3/assert.h"
24#include "ns3/log.h"
25#include "ns3/simulator.h"
26
27#include <cstring>
28#include <iomanip>
29#include <iostream>
30
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("Mac16Address");
35
37
38#define ASCII_a (0x41)
39#define ASCII_z (0x5a)
40#define ASCII_A (0x61)
41#define ASCII_Z (0x7a)
42#define ASCII_COLON (0x3a)
43#define ASCII_ZERO (0x30)
44
50static char
52{
53 if (c >= ASCII_a && c <= ASCII_z)
54 {
55 return c;
56 }
57 else if (c >= ASCII_A && c <= ASCII_Z)
58 {
59 return c + (ASCII_a - ASCII_A);
60 }
61 else
62 {
63 return c;
64 }
65}
66
68
70{
71 NS_LOG_FUNCTION(this);
72 memset(m_address, 0, 2);
73}
74
76{
77 NS_LOG_FUNCTION(this << str);
78 int i = 0;
79 while (*str != 0 && i < 2)
80 {
81 uint8_t byte = 0;
82 while (*str != ASCII_COLON && *str != 0)
83 {
84 byte <<= 4;
85 char low = AsciiToLowCase(*str);
86 if (low >= ASCII_a)
87 {
88 byte |= low - ASCII_a + 10;
89 }
90 else
91 {
92 byte |= low - ASCII_ZERO;
93 }
94 str++;
95 }
96 m_address[i] = byte;
97 i++;
98 if (*str == 0)
99 {
100 break;
101 }
102 str++;
103 }
104 NS_ASSERT(i == 2);
105}
106
108{
109 m_address[1] = addr & 0xFF;
110 m_address[0] = (addr >> 8) & 0xFF;
111}
112
113void
114Mac16Address::CopyFrom(const uint8_t buffer[2])
115{
116 NS_LOG_FUNCTION(this << &buffer);
117 memcpy(m_address, buffer, 2);
118}
119
120void
121Mac16Address::CopyTo(uint8_t buffer[2]) const
122{
123 NS_LOG_FUNCTION(this << &buffer);
124 memcpy(buffer, m_address, 2);
125}
126
127bool
129{
131 return address.CheckCompatible(GetType(), 2);
132}
133
134Mac16Address::operator Address() const
135{
136 return ConvertTo();
137}
138
141{
143 NS_ASSERT(address.CheckCompatible(GetType(), 2));
144 Mac16Address retval;
145 address.CopyTo(retval.m_address);
146 return retval;
147}
148
151{
152 NS_LOG_FUNCTION(this);
153 return Address(GetType(), m_address, 2);
154}
155
158{
160
161 if (m_allocationIndex == 0)
162 {
164 }
165
168 address.m_address[0] = (m_allocationIndex >> 8) & 0xff;
169 address.m_address[1] = m_allocationIndex & 0xff;
170 return address;
171}
172
173void
175{
178}
179
180uint8_t
182{
184
185 static uint8_t type = Address::Register();
186 return type;
187}
188
191{
193
194 static Mac16Address broadcast = Mac16Address("ff:ff");
195 return broadcast;
196}
197
200{
202
203 uint8_t ipv6AddrBuf[16];
204 address.GetBytes(ipv6AddrBuf);
205
206 uint8_t addrBuf[2];
207
208 addrBuf[0] = 0x80 | (ipv6AddrBuf[14] & 0x1F);
209 addrBuf[1] = ipv6AddrBuf[15];
210
211 Mac16Address multicastAddr;
212 multicastAddr.CopyFrom(addrBuf);
213
214 return multicastAddr;
215}
216
217bool
219{
220 NS_LOG_FUNCTION(this);
221 if (m_address[0] == 0xff && m_address[1] == 0xff)
222 {
223 return true;
224 }
225 return false;
226}
227
228bool
230{
231 NS_LOG_FUNCTION(this);
232 uint8_t val = m_address[0];
233 val >>= 5;
234 if (val == 0x4)
235 {
236 return true;
237 }
238 return false;
239}
240
241std::ostream&
242operator<<(std::ostream& os, const Mac16Address& address)
243{
244 uint8_t ad[2];
245 address.CopyTo(ad);
246
247 os.setf(std::ios::hex, std::ios::basefield);
248 os.fill('0');
249 for (uint8_t i = 0; i < 1; i++)
250 {
251 os << std::setw(2) << (uint32_t)ad[i] << ":";
252 }
253 // Final byte not suffixed by ":"
254 os << std::setw(2) << (uint32_t)ad[1];
255 os.setf(std::ios::dec, std::ios::basefield);
256 os.fill(' ');
257 return os;
258}
259
260std::istream&
261operator>>(std::istream& is, Mac16Address& address)
262{
263 std::string v;
264 is >> v;
265
266 std::string::size_type col = 0;
267 for (uint8_t i = 0; i < 2; ++i)
268 {
269 std::string tmp;
270 std::string::size_type next;
271 next = v.find(':', col);
272 if (next == std::string::npos)
273 {
274 tmp = v.substr(col, v.size() - col);
275 address.m_address[i] = std::stoul(tmp, nullptr, 16);
276 break;
277 }
278 else
279 {
280 tmp = v.substr(col, next - col);
281 address.m_address[i] = std::stoul(tmp, nullptr, 16);
282 col = next + 1;
283 }
284 }
285 return is;
286}
287
288} // namespace ns3
a polymophic address class
Definition: address.h:100
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition: address.cc:146
Describes an IPv6 address.
Definition: ipv6-address.h:49
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
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.
static Mac16Address ConvertFrom(const Address &address)
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....
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.
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition: simulator.h:625
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define ASCII_Z
#define ASCII_A
#define ASCII_z
#define ASCII_COLON
#define ASCII_ZERO
#define ASCII_a
address
Definition: first.py:40
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:129
ATTRIBUTE_HELPER_CPP(Length)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
static char AsciiToLowCase(char c)
Converts a char to lower case.