A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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 NS_LOG_FUNCTION(this);
110 m_address[1] = addr & 0xFF;
111 m_address[0] = (addr >> 8) & 0xFF;
112}
113
114void
115Mac16Address::CopyFrom(const uint8_t buffer[2])
116{
117 NS_LOG_FUNCTION(this << &buffer);
118 memcpy(m_address, buffer, 2);
119}
120
121void
122Mac16Address::CopyTo(uint8_t buffer[2]) const
123{
124 NS_LOG_FUNCTION(this << &buffer);
125 memcpy(buffer, m_address, 2);
126}
127
128bool
130{
131 NS_LOG_FUNCTION(&address);
132 return address.CheckCompatible(GetType(), 2);
133}
134
135Mac16Address::operator Address() const
136{
137 return ConvertTo();
138}
139
142{
143 NS_LOG_FUNCTION(address);
144 NS_ASSERT(address.CheckCompatible(GetType(), 2));
145 Mac16Address retval;
146 address.CopyTo(retval.m_address);
147 return retval;
148}
149
152{
153 NS_LOG_FUNCTION(this);
154 return Address(GetType(), m_address, 2);
155}
156
157uint16_t
159{
160 uint16_t addr = m_address[1] & (0xFF);
161 addr |= (m_address[0] << 8) & (0xFF << 8);
162
163 return addr;
164}
165
168{
170
171 if (m_allocationIndex == 0)
172 {
174 }
175
177 Mac16Address address;
178 address.m_address[0] = (m_allocationIndex >> 8) & 0xff;
179 address.m_address[1] = m_allocationIndex & 0xff;
180 return address;
181}
182
183void
185{
188}
189
190uint8_t
192{
194
195 static uint8_t type = Address::Register();
196 return type;
197}
198
201{
203
204 static Mac16Address broadcast = Mac16Address("ff:ff");
205 return broadcast;
206}
207
210{
211 NS_LOG_FUNCTION(address);
212
213 uint8_t ipv6AddrBuf[16];
214 address.GetBytes(ipv6AddrBuf);
215
216 uint8_t addrBuf[2];
217
218 addrBuf[0] = 0x80 | (ipv6AddrBuf[14] & 0x1F);
219 addrBuf[1] = ipv6AddrBuf[15];
220
221 Mac16Address multicastAddr;
222 multicastAddr.CopyFrom(addrBuf);
223
224 return multicastAddr;
225}
226
227bool
229{
230 NS_LOG_FUNCTION(this);
231 return m_address[0] == 0xff && m_address[1] == 0xff;
232}
233
234bool
236{
237 NS_LOG_FUNCTION(this);
238 uint8_t val = m_address[0];
239 val >>= 5;
240 return val == 0x4;
241}
242
243std::ostream&
244operator<<(std::ostream& os, const Mac16Address& address)
245{
246 uint8_t ad[2];
247 address.CopyTo(ad);
248
249 os.setf(std::ios::hex, std::ios::basefield);
250 os.fill('0');
251 for (uint8_t i = 0; i < 1; i++)
252 {
253 os << std::setw(2) << (uint32_t)ad[i] << ":";
254 }
255 // Final byte not suffixed by ":"
256 os << std::setw(2) << (uint32_t)ad[1];
257 os.setf(std::ios::dec, std::ios::basefield);
258 os.fill(' ');
259 return os;
260}
261
262std::istream&
263operator>>(std::istream& is, Mac16Address& address)
264{
265 std::string v;
266 is >> v;
267
268 std::string::size_type col = 0;
269 for (uint8_t i = 0; i < 2; ++i)
270 {
271 std::string tmp;
272 std::string::size_type next;
273 next = v.find(':', col);
274 if (next == std::string::npos)
275 {
276 tmp = v.substr(col, v.size() - col);
277 address.m_address[i] = std::stoul(tmp, nullptr, 16);
278 break;
279 }
280 else
281 {
282 tmp = v.substr(col, next - col);
283 address.m_address[i] = std::stoul(tmp, nullptr, 16);
284 col = next + 1;
285 }
286 }
287 return is;
288}
289
290} // 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)
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....
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 ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
#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 ",...
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
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
static char AsciiToLowCase(char c)
Converts a char to lower case.
#define ASCII_Z
#define ASCII_A
#define ASCII_z
#define ASCII_COLON
#define ASCII_ZERO
#define ASCII_a