forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacAddress.java
More file actions
executable file
·384 lines (342 loc) · 12.4 KB
/
MacAddress.java
File metadata and controls
executable file
·384 lines (342 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.utils.net;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Formatter;
import com.cloud.utils.NumbersUtil;
/**
* copied from the public domain utility from John Burkard.
* @author <a href="mailto:jb@eaio.com">Johann Burkard</a>
* @version 2.1.3
**/
public class MacAddress {
private long _addr = 0;
protected MacAddress() {
}
public MacAddress(long addr) {
_addr = addr;
}
public long toLong() {
return _addr;
}
public byte[] toByteArray() {
byte[] bytes = new byte[6];
bytes[0] = (byte)((_addr >> 40) & 0xff);
bytes[1] = (byte)((_addr >> 32) & 0xff);
bytes[2] = (byte)((_addr >> 24) & 0xff);
bytes[3] = (byte)((_addr >> 16) & 0xff);
bytes[4] = (byte)((_addr >> 8) & 0xff);
bytes[5] = (byte)((_addr >> 0) & 0xff);
return bytes;
}
public String toString(String separator) {
StringBuilder buff = new StringBuilder();
Formatter formatter = new Formatter(buff);
formatter.format("%02x%s%02x%s%02x%s%02x%s%02x%s%02x",
_addr >> 40 & 0xff, separator,
_addr >> 32 & 0xff, separator,
_addr >> 24 & 0xff, separator,
_addr >> 16 & 0xff, separator,
_addr >> 8 & 0xff, separator,
_addr & 0xff);
return buff.toString();
/*
String str = Long.toHexString(_addr);
for (int i = str.length() - 1; i >= 0; i--) {
buff.append(str.charAt(i));
if (separator != null && (str.length() - i) % 2 == 0) {
buff.append(separator);
}
}
return buff.reverse().toString();
*/
}
@Override
public String toString() {
return toString(":");
}
private static MacAddress s_address;
static {
String macAddress = null;
Process p = null;
BufferedReader in = null;
try {
String osname = System.getProperty("os.name");
if (osname.startsWith("Windows")) {
p = Runtime.getRuntime().exec(new String[] { "ipconfig", "/all"}, null);
} else if (osname.startsWith("Solaris") || osname.startsWith("SunOS")) {
// Solaris code must appear before the generic code
String hostName = MacAddress.getFirstLineOfCommand(new String[] { "uname",
"-n"});
if (hostName != null) {
p = Runtime.getRuntime().exec(new String[] { "/usr/sbin/arp", hostName}, null);
}
} else if (new File("/usr/sbin/lanscan").exists()) {
p = Runtime.getRuntime().exec(new String[] { "/usr/sbin/lanscan"}, null);
} else if (new File("/sbin/ifconfig").exists()) {
p = Runtime.getRuntime().exec(new String[] { "/sbin/ifconfig", "-a"}, null);
}
if (p != null) {
in = new BufferedReader(new InputStreamReader(p.getInputStream()), 128);
String l = null;
while ((l = in.readLine()) != null) {
macAddress = MacAddress.parse(l);
if (macAddress != null && MacAddress.parseShort(macAddress) != 0xff)
break;
}
}
} catch (SecurityException ex) {
} catch (IOException ex) {
} finally {
if (p != null) {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
try {
p.getErrorStream().close();
} catch (IOException ex) {
}
try {
p.getOutputStream().close();
} catch (IOException ex) {
}
p.destroy();
}
}
long clockSeqAndNode = 0;
if (macAddress != null) {
if (macAddress.indexOf(':') != -1) {
clockSeqAndNode |= MacAddress.parseLong(macAddress);
} else if (macAddress.startsWith("0x")) {
clockSeqAndNode |= MacAddress.parseLong(macAddress.substring(2));
}
} else {
try {
byte[] local = InetAddress.getLocalHost().getAddress();
clockSeqAndNode |= (local[0] << 24) & 0xFF000000L;
clockSeqAndNode |= (local[1] << 16) & 0xFF0000;
clockSeqAndNode |= (local[2] << 8) & 0xFF00;
clockSeqAndNode |= local[3] & 0xFF;
} catch (UnknownHostException ex) {
clockSeqAndNode |= (long) (Math.random() * 0x7FFFFFFF);
}
}
s_address = new MacAddress(clockSeqAndNode);
}
public static MacAddress getMacAddress() {
return s_address;
}
private static String getFirstLineOfCommand(String[] commands) throws IOException {
Process p = null;
BufferedReader reader = null;
try {
p = Runtime.getRuntime().exec(commands);
reader = new BufferedReader(new InputStreamReader(p.getInputStream()), 128);
return reader.readLine();
} finally {
if (p != null) {
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
}
}
try {
p.getErrorStream().close();
} catch (IOException ex) {
}
try {
p.getOutputStream().close();
} catch (IOException ex) {
}
p.destroy();
}
}
}
/**
* The MAC address parser attempts to find the following patterns:
* <ul>
* <li>.{1,2}:.{1,2}:.{1,2}:.{1,2}:.{1,2}:.{1,2}</li>
* <li>.{1,2}-.{1,2}-.{1,2}-.{1,2}-.{1,2}-.{1,2}</li>
* </ul>
*
* This is copied from the author below. The author encouraged copying
* it.
*
*/
static String parse(String in) {
// lanscan
int hexStart = in.indexOf("0x");
if (hexStart != -1) {
int hexEnd = in.indexOf(' ', hexStart);
if (hexEnd != -1) {
return in.substring(hexStart, hexEnd);
}
}
int octets = 0;
int lastIndex, old, end;
if (in.indexOf('-') > -1) {
in = in.replace('-', ':');
}
lastIndex = in.lastIndexOf(':');
if (lastIndex > in.length() - 2) return null;
end = Math.min(in.length(), lastIndex + 3);
++octets;
old = lastIndex;
while (octets != 5 && lastIndex != -1 && lastIndex > 1) {
lastIndex = in.lastIndexOf(':', --lastIndex);
if (old - lastIndex == 3 || old - lastIndex == 2) {
++octets;
old = lastIndex;
}
}
if (octets == 5 && lastIndex > 1) {
return in.substring(lastIndex - 2, end).trim();
}
return null;
}
public static void main(String[] args) {
MacAddress addr = MacAddress.getMacAddress();
System.out.println("addr in integer is " + addr.toLong());
System.out.println("addr in bytes is " + NumbersUtil.bytesToString(addr.toByteArray(), 0, addr.toByteArray().length));
System.out.println("addr in char is " + addr.toString(":"));
}
/**
* Parses a <code>long</code> from a hex encoded number. This method will skip
* all characters that are not 0-9 and a-f (the String is lower cased first).
* Returns 0 if the String does not contain any interesting characters.
*
* @param s the String to extract a <code>long</code> from, may not be <code>null</code>
* @return a <code>long</code>
* @throws NullPointerException if the String is <code>null</code>
*/
public static long parseLong(String s) throws NullPointerException {
s = s.toLowerCase();
long out = 0;
byte shifts = 0;
char c;
for (int i = 0; i < s.length() && shifts < 16; i++) {
c = s.charAt(i);
if ((c > 47) && (c < 58)) {
out <<= 4;
++shifts;
out |= c - 48;
}
else if ((c > 96) && (c < 103)) {
++shifts;
out <<= 4;
out |= c - 87;
}
}
return out;
}
/**
* Parses an <code>int</code> from a hex encoded number. This method will skip
* all characters that are not 0-9 and a-f (the String is lower cased first).
* Returns 0 if the String does not contain any interesting characters.
*
* @param s the String to extract an <code>int</code> from, may not be <code>null</code>
* @return an <code>int</code>
* @throws NullPointerException if the String is <code>null</code>
*/
public static int parseInt(String s) throws NullPointerException {
s = s.toLowerCase();
int out = 0;
byte shifts = 0;
char c;
for (int i = 0; i < s.length() && shifts < 8; i++) {
c = s.charAt(i);
if ((c > 47) && (c < 58)) {
out <<= 4;
++shifts;
out |= c - 48;
}
else if ((c > 96) && (c < 103)) {
++shifts;
out <<= 4;
out |= c - 87;
}
}
return out;
}
/**
* Parses a <code>short</code> from a hex encoded number. This method will skip
* all characters that are not 0-9 and a-f (the String is lower cased first).
* Returns 0 if the String does not contain any interesting characters.
*
* @param s the String to extract a <code>short</code> from, may not be <code>null</code>
* @return a <code>short</code>
* @throws NullPointerException if the String is <code>null</code>
*/
public static short parseShort(String s) throws NullPointerException {
s = s.toLowerCase();
short out = 0;
byte shifts = 0;
char c;
for (int i = 0; i < s.length() && shifts < 4; i++) {
c = s.charAt(i);
if ((c > 47) && (c < 58)) {
out <<= 4;
++shifts;
out |= c - 48;
}
else if ((c > 96) && (c < 103)) {
++shifts;
out <<= 4;
out |= c - 87;
}
}
return out;
}
/**
* Parses a <code>byte</code> from a hex encoded number. This method will skip
* all characters that are not 0-9 and a-f (the String is lower cased first).
* Returns 0 if the String does not contain any interesting characters.
*
* @param s the String to extract a <code>byte</code> from, may not be <code>null</code>
* @return a <code>byte</code>
* @throws NullPointerException if the String is <code>null</code>
*/
public static byte parseByte(String s) throws NullPointerException {
s = s.toLowerCase();
byte out = 0;
byte shifts = 0;
char c;
for (int i = 0; i < s.length() && shifts < 2; i++) {
c = s.charAt(i);
if ((c > 47) && (c < 58)) {
out <<= 4;
++shifts;
out |= c - 48;
}
else if ((c > 96) && (c < 103)) {
++shifts;
out <<= 4;
out |= c - 87;
}
}
return out;
}
}