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
107void
108Mac16Address::CopyFrom(const uint8_t buffer[2])
109{
110 NS_LOG_FUNCTION(this << &buffer);
111 memcpy(m_address, buffer, 2);
112}
113
114void
115Mac16Address::CopyTo(uint8_t buffer[2]) const
116{
117 NS_LOG_FUNCTION(this << &buffer);
118 memcpy(buffer, m_address, 2);
119}
120
121bool
123{
124 NS_LOG_FUNCTION(&address);
125 return address.CheckCompatible(GetType(), 2);
126}
127
128Mac16Address::operator Address() const
129{
130 return ConvertTo();
131}
132
135{
136 NS_LOG_FUNCTION(address);
137 NS_ASSERT(address.CheckCompatible(GetType(), 2));
138 Mac16Address retval;
139 address.CopyTo(retval.m_address);
140 return retval;
141}
142
145{
146 NS_LOG_FUNCTION(this);
147 return Address(GetType(), m_address, 2);
148}
149
152{
154
155 if (m_allocationIndex == 0)
156 {
158 }
159
161 Mac16Address address;
162 address.m_address[0] = (m_allocationIndex >> 8) & 0xff;
163 address.m_address[1] = m_allocationIndex & 0xff;
164 return address;
165}
166
167void
169{
172}
173
174uint8_t
176{
178
179 static uint8_t type = Address::Register();
180 return type;
181}
182
185{
187
188 static Mac16Address broadcast = Mac16Address("ff:ff");
189 return broadcast;
190}
191
194{
195 NS_LOG_FUNCTION(address);
196
197 uint8_t ipv6AddrBuf[16];
198 address.GetBytes(ipv6AddrBuf);
199
200 uint8_t addrBuf[2];
201
202 addrBuf[0] = 0x80 | (ipv6AddrBuf[14] & 0x1F);
203 addrBuf[1] = ipv6AddrBuf[15];
204
205 Mac16Address multicastAddr;
206 multicastAddr.CopyFrom(addrBuf);
207
208 return multicastAddr;
209}
210
211bool
213{
214 NS_LOG_FUNCTION(this);
215 if (m_address[0] == 0xff && m_address[1] == 0xff)
216 {
217 return true;
218 }
219 return false;
220}
221
222bool
224{
225 NS_LOG_FUNCTION(this);
226 uint8_t val = m_address[0];
227 val >>= 5;
228 if (val == 0x4)
229 {
230 return true;
231 }
232 return false;
233}
234
235std::ostream&
236operator<<(std::ostream& os, const Mac16Address& address)
237{
238 uint8_t ad[2];
239 address.CopyTo(ad);
240
241 os.setf(std::ios::hex, std::ios::basefield);
242 os.fill('0');
243 for (uint8_t i = 0; i < 1; i++)
244 {
245 os << std::setw(2) << (uint32_t)ad[i] << ":";
246 }
247 // Final byte not suffixed by ":"
248 os << std::setw(2) << (uint32_t)ad[1];
249 os.setf(std::ios::dec, std::ios::basefield);
250 os.fill(' ');
251 return os;
252}
253
254std::istream&
255operator>>(std::istream& is, Mac16Address& address)
256{
257 std::string v;
258 is >> v;
259
260 std::string::size_type col = 0;
261 for (uint8_t i = 0; i < 2; ++i)
262 {
263 std::string tmp;
264 std::string::size_type next;
265 next = v.find(':', col);
266 if (next == std::string::npos)
267 {
268 tmp = v.substr(col, v.size() - col);
269 address.m_address[i] = std::stoul(tmp, nullptr, 16);
270 break;
271 }
272 else
273 {
274 tmp = v.substr(col, next - col);
275 address.m_address[i] = std::stoul(tmp, nullptr, 16);
276 col = next + 1;
277 }
278 }
279 return is;
280}
281
282} // 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:50
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 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