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
mac64-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 "
mac64-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
(
"Mac64Address"
);
29
30
namespace
ns3 {
31
32
ATTRIBUTE_HELPER_CPP
(Mac64Address);
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
Mac64Address::Mac64Address
()
56
{
57
NS_LOG_FUNCTION
(
this
);
58
std::memset (
m_address
, 0, 8);
59
}
60
Mac64Address::Mac64Address
(
const
char
*str)
61
{
62
NS_LOG_FUNCTION
(
this
<< str);
63
int
i = 0;
64
while
(*str != 0 && i < 8)
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 == 8);
90
}
91
void
92
Mac64Address::CopyFrom
(
const
uint8_t buffer[8])
93
{
94
NS_LOG_FUNCTION
(
this
<< &buffer);
95
std::memcpy (
m_address
, buffer, 8);
96
}
97
void
98
Mac64Address::CopyTo
(uint8_t buffer[8])
const
99
{
100
NS_LOG_FUNCTION
(
this
<< &buffer);
101
std::memcpy (buffer,
m_address
, 8);
102
}
103
104
bool
105
Mac64Address::IsMatchingType
(
const
Address
&
address
)
106
{
107
NS_LOG_FUNCTION
(&address);
108
return
address.
CheckCompatible
(
GetType
(), 8);
109
}
110
Mac64Address::operator
Address
()
const
111
{
112
return
ConvertTo ();
113
}
114
Mac64Address
115
Mac64Address::ConvertFrom
(
const
Address
&
address
)
116
{
117
NS_LOG_FUNCTION
(address);
118
NS_ASSERT
(address.
CheckCompatible
(
GetType
(), 8));
119
Mac64Address
retval;
120
address.
CopyTo
(retval.m_address);
121
return
retval;
122
}
123
124
Address
125
Mac64Address::ConvertTo
(
void
)
const
126
{
127
NS_LOG_FUNCTION
(
this
);
128
return
Address
(
GetType
(),
m_address
, 8);
129
}
130
131
Mac64Address
132
Mac64Address::Allocate
(
void
)
133
{
134
NS_LOG_FUNCTION_NOARGS
();
135
static
uint64_t
id
= 0;
136
id
++;
137
Mac64Address
address
;
138
address.
m_address
[0] = (
id
>> 56) & 0xff;
139
address.
m_address
[1] = (
id
>> 48) & 0xff;
140
address.
m_address
[2] = (
id
>> 40) & 0xff;
141
address.
m_address
[3] = (
id
>> 32) & 0xff;
142
address.
m_address
[4] = (
id
>> 24) & 0xff;
143
address.
m_address
[5] = (
id
>> 16) & 0xff;
144
address.
m_address
[6] = (
id
>> 8) & 0xff;
145
address.
m_address
[7] = (
id
>> 0) & 0xff;
146
return
address
;
147
}
148
uint8_t
149
Mac64Address::GetType
(
void
)
150
{
151
NS_LOG_FUNCTION_NOARGS
();
152
static
uint8_t type =
Address::Register
();
153
return
type;
154
}
155
156
std::ostream&
operator<<
(std::ostream& os,
const
Mac64Address
&
address
)
157
{
158
uint8_t ad[8];
159
address.
CopyTo
(ad);
160
161
os.setf (std::ios::hex, std::ios::basefield);
162
os.fill (
'0'
);
163
for
(uint8_t i=0; i < 7; i++)
164
{
165
os << std::setw (2) << (uint32_t)ad[i] <<
":"
;
166
}
167
// Final byte not suffixed by ":"
168
os << std::setw (2) << (uint32_t)ad[7];
169
os.setf (std::ios::dec, std::ios::basefield);
170
os.fill (
' '
);
171
return
os;
172
}
173
174
static
uint8_t
175
AsInt
(std::string v)
176
{
177
NS_LOG_FUNCTION
(v);
178
std::istringstream iss;
179
iss.str (v);
180
uint32_t retval;
181
iss >> std::hex >> retval >> std::dec;
182
return
retval;
183
}
184
185
std::istream&
operator>>
(std::istream& is,
Mac64Address
&
address
)
186
{
187
std::string v;
188
is >> v;
189
190
std::string::size_type col = 0;
191
for
(uint8_t i = 0; i < 8; ++i)
192
{
193
std::string tmp;
194
std::string::size_type next;
195
next = v.find (
":"
, col);
196
if
(next == std::string::npos)
197
{
198
tmp = v.substr (col, v.size ()-col);
199
address.
m_address
[i] =
AsInt
(tmp);
200
break
;
201
}
202
else
203
{
204
tmp = v.substr (col, next-col);
205
address.
m_address
[i] =
AsInt
(tmp);
206
col = next + 1;
207
}
208
}
209
return
is;
210
}
211
212
}
// namespace ns3
src
network
utils
mac64-address.cc
Generated on Fri Aug 30 2013 01:43:00 for ns-3 by
1.8.1.2