1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2005 INRIA |
|
4 * All rights reserved. |
|
5 * |
|
6 * This program is free software; you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License version 2 as |
|
8 * published by the Free Software Foundation; |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 * |
|
19 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
|
20 */ |
|
21 |
|
22 #include <iostream> |
|
23 #include <iomanip> |
|
24 #include "ns3/assert.h" |
|
25 #include "mac-address.h" |
|
26 |
|
27 #define ASCII_a (0x41) |
|
28 #define ASCII_z (0x5a) |
|
29 #define ASCII_A (0x61) |
|
30 #define ASCII_Z (0x7a) |
|
31 #define ASCII_COLON (0x3a) |
|
32 #define ASCII_ZERO (0x30) |
|
33 |
|
34 namespace ns3 { |
|
35 |
|
36 // Static variables |
|
37 uint8_t MacAddress::g_nextAddress[MacAddress::MAX_LEN]; |
|
38 |
|
39 static char |
|
40 AsciiToLowCase (char c) |
|
41 { |
|
42 if (c >= ASCII_a && c <= ASCII_z) { |
|
43 return c; |
|
44 } else if (c >= ASCII_A && c <= ASCII_Z) { |
|
45 return c + (ASCII_a - ASCII_A); |
|
46 } else { |
|
47 return c; |
|
48 } |
|
49 } |
|
50 |
|
51 |
|
52 MacAddress::MacAddress () : m_len(0) |
|
53 { |
|
54 for (int i=0; i < MacAddress::MAX_LEN; i++) |
|
55 { |
|
56 m_address[i] = 0; |
|
57 } |
|
58 } |
|
59 |
|
60 MacAddress::MacAddress(uint8_t len) : m_len(len) |
|
61 { |
|
62 NS_ASSERT (len <= MacAddress::MAX_LEN); |
|
63 AdvanceAddress(); |
|
64 memcpy(m_address, g_nextAddress, len); |
|
65 } |
|
66 |
|
67 MacAddress::MacAddress (uint8_t const *address, uint8_t len) |
|
68 { |
|
69 NS_ASSERT (len <= MacAddress::MAX_LEN); |
|
70 for (int i=0; i < len; i++) |
|
71 { |
|
72 m_address[i] = address[i]; |
|
73 } |
|
74 for (int i=len; i < MacAddress::MAX_LEN; i++) |
|
75 { |
|
76 m_address[i] = 0; |
|
77 } |
|
78 m_len = len; |
|
79 } |
|
80 |
|
81 MacAddress::MacAddress (char const *str) |
|
82 { |
|
83 int i = 0; |
|
84 while (*str != 0 && i < MacAddress::MAX_LEN) { |
|
85 uint8_t byte = 0; |
|
86 while (*str != ASCII_COLON && *str != 0) { |
|
87 byte <<= 4; |
|
88 char low = AsciiToLowCase (*str); |
|
89 if (low >= ASCII_a) { |
|
90 byte |= low - ASCII_a + 10; |
|
91 } else { |
|
92 byte |= low - ASCII_ZERO; |
|
93 } |
|
94 str++; |
|
95 } |
|
96 m_address[i] = byte; |
|
97 i++; |
|
98 if (*str == 0) { |
|
99 break; |
|
100 } |
|
101 str++; |
|
102 } |
|
103 m_len = i; |
|
104 } |
|
105 |
|
106 MacAddress::~MacAddress () |
|
107 {} |
|
108 |
|
109 bool |
|
110 MacAddress::IsEqual (MacAddress other) const |
|
111 { |
|
112 if (memcmp(other.m_address, m_address, m_len)) |
|
113 { |
|
114 return false; |
|
115 } |
|
116 else |
|
117 { |
|
118 return true; |
|
119 } |
|
120 } |
|
121 |
|
122 void |
|
123 MacAddress::Print (std::ostream &os) const |
|
124 { |
|
125 int i; |
|
126 if (m_len == 0) |
|
127 { |
|
128 os << "NULL-ADDRESS"; |
|
129 return; |
|
130 } |
|
131 os.setf (std::ios::hex, std::ios::basefield); |
|
132 std::cout.fill('0'); |
|
133 for (i=0; i< (m_len-1); i++) |
|
134 { |
|
135 os << std::setw(2) << (uint32_t)m_address[i] << ":"; |
|
136 } |
|
137 // Final byte not suffixed by ":" |
|
138 os << std::setw(2) << (uint32_t)m_address[i]; |
|
139 os.setf (std::ios::dec, std::ios::basefield); |
|
140 std::cout.fill(' '); |
|
141 } |
|
142 |
|
143 uint8_t |
|
144 MacAddress::GetLength () const |
|
145 { |
|
146 return m_len; |
|
147 } |
|
148 |
|
149 void |
|
150 MacAddress::Peek (uint8_t ad[MacAddress::MAX_LEN]) const |
|
151 { |
|
152 memcpy (ad, m_address, MacAddress::MAX_LEN); |
|
153 } |
|
154 void |
|
155 MacAddress::Set (uint8_t const ad[MacAddress::MAX_LEN], uint8_t len) |
|
156 { |
|
157 memcpy (m_address, ad, MacAddress::MAX_LEN); |
|
158 m_len = len; |
|
159 } |
|
160 |
|
161 // Static methods |
|
162 void MacAddress::AdvanceAddress() |
|
163 { |
|
164 // Advance to next address, little end first |
|
165 for(size_t i = 0; i < MAX_LEN; ++i) |
|
166 { |
|
167 if (++g_nextAddress[i] != 0) break; |
|
168 } |
|
169 } |
|
170 |
|
171 // Non-member operators |
|
172 bool operator == (MacAddress const&a, MacAddress const&b) |
|
173 { |
|
174 return a.IsEqual (b); |
|
175 } |
|
176 |
|
177 bool operator != (MacAddress const&a, MacAddress const&b) |
|
178 { |
|
179 return !a.IsEqual (b); |
|
180 } |
|
181 |
|
182 bool operator < (MacAddress const&a, MacAddress const&b) |
|
183 { |
|
184 uint8_t a_p[MacAddress::MAX_LEN]; |
|
185 uint8_t b_p[MacAddress::MAX_LEN]; |
|
186 a.Peek (a_p); |
|
187 b.Peek (b_p); |
|
188 NS_ASSERT (a.GetLength() == b.GetLength()); |
|
189 for (uint8_t i = 0; i < a.GetLength(); i++) |
|
190 { |
|
191 if (a_p[i] < b_p[i]) |
|
192 { |
|
193 return true; |
|
194 } |
|
195 else if (a_p[i] > b_p[i]) |
|
196 { |
|
197 return false; |
|
198 } |
|
199 } |
|
200 return false; |
|
201 } |
|
202 |
|
203 std::ostream& operator<< (std::ostream& os, MacAddress const& address) |
|
204 { |
|
205 address.Print (os); |
|
206 return os; |
|
207 } |
|
208 |
|
209 |
|
210 }; // namespace ns3 |
|