A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
mac48-address.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2007 INRIA
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
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
*/
20
#include "
mac48-address.h
"
21
#include "ns3/address.h"
22
#include "ns3/assert.h"
23
#include "ns3/log.h"
24
#include <iomanip>
25
#include <iostream>
26
#include <cstring>
27
28
NS_LOG_COMPONENT_DEFINE
(
"Mac48Address"
);
29
30
namespace
ns3 {
31
32
ATTRIBUTE_HELPER_CPP
(Mac48Address);
33
34
#define ASCII_a (0x41)
35
#define ASCII_z (0x5a)
36
#define ASCII_A (0x61)
37
#define ASCII_Z (0x7a)
38
#define ASCII_COLON (0x3a)
39
#define ASCII_ZERO (0x30)
40
41
static
char
42
AsciiToLowCase
(
char
c)
43
{
44
NS_LOG_FUNCTION
(c);
45
if
(c >=
ASCII_a
&& c <=
ASCII_z
) {
46
return
c;
47
}
else
if
(c >=
ASCII_A
&& c <=
ASCII_Z
) {
48
return
c + (
ASCII_a
-
ASCII_A
);
49
}
else
{
50
return
c;
51
}
52
}
53
54
55
Mac48Address::Mac48Address
()
56
{
57
NS_LOG_FUNCTION
(
this
);
58
std::memset (
m_address
, 0, 6);
59
}
60
Mac48Address::Mac48Address
(
const
char
*str)
61
{
62
NS_LOG_FUNCTION
(
this
<< str);
63
int
i = 0;
64
while
(*str != 0 && i < 6)
65
{
66
uint8_t byte = 0;
67
while
(*str !=
ASCII_COLON
&& *str != 0)
68
{
69
byte <<= 4;
70
char
low =
AsciiToLowCase
(*str);
71
if
(low >=
ASCII_a
)
72
{
73
byte |= low -
ASCII_a
+ 10;
74
}
75
else
76
{
77
byte |= low -
ASCII_ZERO
;
78
}
79
str++;
80
}
81
m_address
[i] = byte;
82
i++;
83
if
(*str == 0)
84
{
85
break
;
86
}
87
str++;
88
}
89
NS_ASSERT
(i == 6);
90
}
91
void
92
Mac48Address::CopyFrom
(
const
uint8_t buffer[6])
93
{
94
NS_LOG_FUNCTION
(
this
<< &buffer);
95
std::memcpy (
m_address
, buffer, 6);
96
}
97
void
98
Mac48Address::CopyTo
(uint8_t buffer[6])
const
99
{
100
NS_LOG_FUNCTION
(
this
<< &buffer);
101
std::memcpy (buffer,
m_address
, 6);
102
}
103
104
bool
105
Mac48Address::IsMatchingType
(
const
Address
&address)
106
{
107
NS_LOG_FUNCTION
(&address);
108
return
address.
CheckCompatible
(
GetType
(), 6);
109
}
110
Mac48Address::operator
Address
()
const
111
{
112
return
ConvertTo ();
113
}
114
Address
115
Mac48Address::ConvertTo
(
void
)
const
116
{
117
NS_LOG_FUNCTION
(
this
);
118
return
Address
(
GetType
(),
m_address
, 6);
119
}
120
Mac48Address
121
Mac48Address::ConvertFrom
(
const
Address
&address)
122
{
123
NS_LOG_FUNCTION
(&address);
124
NS_ASSERT
(address.
CheckCompatible
(
GetType
(), 6));
125
Mac48Address
retval;
126
address.
CopyTo
(retval.m_address);
127
return
retval;
128
}
129
Mac48Address
130
Mac48Address::Allocate
(
void
)
131
{
132
NS_LOG_FUNCTION_NOARGS
();
133
static
uint64_t
id
= 0;
134
id
++;
135
Mac48Address
address;
136
address.
m_address
[0] = (
id
>> 40) & 0xff;
137
address.
m_address
[1] = (
id
>> 32) & 0xff;
138
address.
m_address
[2] = (
id
>> 24) & 0xff;
139
address.
m_address
[3] = (
id
>> 16) & 0xff;
140
address.
m_address
[4] = (
id
>> 8) & 0xff;
141
address.
m_address
[5] = (
id
>> 0) & 0xff;
142
return
address;
143
}
144
uint8_t
145
Mac48Address::GetType
(
void
)
146
{
147
NS_LOG_FUNCTION_NOARGS
();
148
static
uint8_t type =
Address::Register
();
149
return
type;
150
}
151
152
bool
153
Mac48Address::IsBroadcast
(
void
)
const
154
{
155
NS_LOG_FUNCTION
(
this
);
156
return
*
this
==
GetBroadcast
();
157
}
158
bool
159
Mac48Address::IsGroup
(
void
)
const
160
{
161
NS_LOG_FUNCTION
(
this
);
162
return
(
m_address
[0] & 0x01) == 0x01;
163
}
164
Mac48Address
165
Mac48Address::GetBroadcast
(
void
)
166
{
167
NS_LOG_FUNCTION_NOARGS
();
168
static
Mac48Address
broadcast =
Mac48Address
(
"ff:ff:ff:ff:ff:ff"
);
169
return
broadcast;
170
}
171
Mac48Address
172
Mac48Address::GetMulticastPrefix
(
void
)
173
{
174
NS_LOG_FUNCTION_NOARGS
();
175
static
Mac48Address
multicast =
Mac48Address
(
"01:00:5e:00:00:00"
);
176
return
multicast;
177
}
178
Mac48Address
179
Mac48Address::GetMulticast6Prefix
(
void
)
180
{
181
NS_LOG_FUNCTION_NOARGS
();
182
static
Mac48Address
multicast =
Mac48Address
(
"33:33:00:00:00:00"
);
183
return
multicast;
184
}
185
Mac48Address
186
Mac48Address::GetMulticast
(
Ipv4Address
multicastGroup)
187
{
188
NS_LOG_FUNCTION
(multicastGroup);
189
Mac48Address
etherAddr =
Mac48Address::GetMulticastPrefix
();
190
//
191
// We now have the multicast address in an abstract 48-bit container. We
192
// need to pull it out so we can play with it. When we're done, we have the
193
// high order bits in etherBuffer[0], etc.
194
//
195
uint8_t etherBuffer[6];
196
etherAddr.
CopyTo
(etherBuffer);
197
198
//
199
// Now we need to pull the raw bits out of the Ipv4 destination address.
200
//
201
uint8_t ipBuffer[4];
202
multicastGroup.
Serialize
(ipBuffer);
203
204
//
205
// RFC 1112 says that an Ipv4 host group address is mapped to an EUI-48
206
// multicast address by placing the low-order 23-bits of the IP address into
207
// the low-order 23 bits of the Ethernet multicast address
208
// 01-00-5E-00-00-00 (hex).
209
//
210
etherBuffer[3] |= ipBuffer[1] & 0x7f;
211
etherBuffer[4] = ipBuffer[2];
212
etherBuffer[5] = ipBuffer[3];
213
214
//
215
// Now, etherBuffer has the desired ethernet multicast address. We have to
216
// suck these bits back into the Mac48Address,
217
//
218
Mac48Address
result;
219
result.
CopyFrom
(etherBuffer);
220
return
result;
221
}
222
Mac48Address
Mac48Address::GetMulticast
(
Ipv6Address
addr)
223
{
224
NS_LOG_FUNCTION
(addr);
225
Mac48Address
etherAddr =
Mac48Address::GetMulticast6Prefix
();
226
uint8_t etherBuffer[6];
227
uint8_t ipBuffer[16];
228
229
/* a MAC multicast IPv6 address is like 33:33 and the four low bytes */
230
/* for 2001:db8::2fff:fe11:ac10 => 33:33:FE:11:AC:10 */
231
etherAddr.
CopyTo
(etherBuffer);
232
addr.
Serialize
(ipBuffer);
233
234
etherBuffer[2] = ipBuffer[12];
235
etherBuffer[3] = ipBuffer[13];
236
etherBuffer[4] = ipBuffer[14];
237
etherBuffer[5] = ipBuffer[15];
238
239
etherAddr.
CopyFrom
(etherBuffer);
240
241
return
etherAddr;
242
}
243
244
std::ostream&
operator<<
(std::ostream& os,
const
Mac48Address
& address)
245
{
246
uint8_t ad[6];
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 < 5; 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[5];
257
os.setf (std::ios::dec, std::ios::basefield);
258
os.fill (
' '
);
259
return
os;
260
}
261
262
static
uint8_t
263
AsInt
(std::string v)
264
{
265
NS_LOG_FUNCTION
(v);
266
std::istringstream iss;
267
iss.str (v);
268
uint32_t retval;
269
iss >> std::hex >> retval >> std::dec;
270
return
retval;
271
}
272
273
std::istream&
operator>>
(std::istream& is,
Mac48Address
& address)
274
{
275
std::string v;
276
is >> v;
277
278
std::string::size_type col = 0;
279
for
(uint8_t i = 0; i < 6; ++i)
280
{
281
std::string tmp;
282
std::string::size_type next;
283
next = v.find (
":"
, col);
284
if
(next == std::string::npos)
285
{
286
tmp = v.substr (col, v.size ()-col);
287
address.
m_address
[i] =
AsInt
(tmp);
288
break
;
289
}
290
else
291
{
292
tmp = v.substr (col, next-col);
293
address.
m_address
[i] =
AsInt
(tmp);
294
col = next + 1;
295
}
296
}
297
return
is;
298
}
299
300
301
}
// namespace ns3
src
network
utils
mac48-address.cc
Generated on Fri Dec 21 2012 19:00:44 for ns-3 by
1.8.1.2