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
39
41{
42 NS_LOG_FUNCTION(this << str);
43 NS_ASSERT_MSG(strlen(str) <= 5, "Mac16Address: illegal string (too long) " << str);
44
45 unsigned int bytes[2];
46 int charsRead = 0;
47
48 int i = sscanf(str, "%02x:%02x%n", bytes, bytes + 1, &charsRead);
49 NS_ASSERT_MSG(i == 2 && !str[charsRead], "Mac16Address: illegal string " << str);
50
51 std::copy(std::begin(bytes), std::end(bytes), std::begin(m_address));
52}
53
55{
56 NS_LOG_FUNCTION(this);
57 m_address[1] = addr & 0xFF;
58 m_address[0] = (addr >> 8) & 0xFF;
59}
60
61void
62Mac16Address::CopyFrom(const uint8_t buffer[2])
63{
64 NS_LOG_FUNCTION(this << &buffer);
65 memcpy(m_address, buffer, 2);
66}
67
68void
69Mac16Address::CopyTo(uint8_t buffer[2]) const
70{
71 NS_LOG_FUNCTION(this << &buffer);
72 memcpy(buffer, m_address, 2);
73}
74
75bool
77{
78 NS_LOG_FUNCTION(&address);
79 return address.CheckCompatible(GetType(), 2);
80}
81
82Mac16Address::operator Address() const
83{
84 return ConvertTo();
85}
86
89{
90 NS_LOG_FUNCTION(address);
91 NS_ASSERT(address.CheckCompatible(GetType(), 2));
92 Mac16Address retval;
93 address.CopyTo(retval.m_address);
94 return retval;
95}
96
99{
100 NS_LOG_FUNCTION(this);
101 return Address(GetType(), m_address, 2);
102}
103
104uint16_t
106{
107 uint16_t addr = m_address[1] & (0xFF);
108 addr |= (m_address[0] << 8) & (0xFF << 8);
109
110 return addr;
111}
112
115{
117
118 if (m_allocationIndex == 0)
119 {
121 }
122
124 Mac16Address address;
125 address.m_address[0] = (m_allocationIndex >> 8) & 0xff;
126 address.m_address[1] = m_allocationIndex & 0xff;
127 return address;
128}
129
130void
132{
135}
136
137uint8_t
139{
141
142 static uint8_t type = Address::Register();
143 return type;
144}
145
148{
150
151 static Mac16Address broadcast("ff:ff");
152 return broadcast;
153}
154
157{
158 NS_LOG_FUNCTION(address);
159
160 uint8_t ipv6AddrBuf[16];
161 address.GetBytes(ipv6AddrBuf);
162
163 uint8_t addrBuf[2];
164
165 addrBuf[0] = 0x80 | (ipv6AddrBuf[14] & 0x1F);
166 addrBuf[1] = ipv6AddrBuf[15];
167
168 Mac16Address multicastAddr;
169 multicastAddr.CopyFrom(addrBuf);
170
171 return multicastAddr;
172}
173
174bool
176{
177 NS_LOG_FUNCTION(this);
178 return m_address[0] == 0xff && m_address[1] == 0xff;
179}
180
181bool
183{
184 NS_LOG_FUNCTION(this);
185 uint8_t val = m_address[0];
186 val >>= 5;
187 return val == 0x4;
188}
189
190std::ostream&
191operator<<(std::ostream& os, const Mac16Address& address)
192{
193 uint8_t ad[2];
194 address.CopyTo(ad);
195
196 os.setf(std::ios::hex, std::ios::basefield);
197 os.fill('0');
198 for (uint8_t i = 0; i < 1; i++)
199 {
200 os << std::setw(2) << (uint32_t)ad[i] << ":";
201 }
202 // Final byte not suffixed by ":"
203 os << std::setw(2) << (uint32_t)ad[1];
204 os.setf(std::ios::dec, std::ios::basefield);
205 os.fill(' ');
206 return os;
207}
208
209std::istream&
210operator>>(std::istream& is, Mac16Address& address)
211{
212 std::string v;
213 is >> v;
214
215 std::string::size_type col = 0;
216 for (uint8_t i = 0; i < 2; ++i)
217 {
218 std::string tmp;
219 std::string::size_type next;
220 next = v.find(':', col);
221 if (next == std::string::npos)
222 {
223 tmp = v.substr(col, v.size() - col);
224 address.m_address[i] = std::stoul(tmp, nullptr, 16);
225 break;
226 }
227 else
228 {
229 tmp = v.substr(col, next - col);
230 address.m_address[i] = std::stoul(tmp, nullptr, 16);
231 col = next + 1;
232 }
233 }
234 return is;
235}
236
237} // namespace ns3
a polymophic address class
Definition: address.h:101
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)
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....
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:622
#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_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#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:159
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183