@@ -275,9 +275,9 @@ public static EthernetAddress fromInterface()
275
275
while (en .hasMoreElements ()) {
276
276
NetworkInterface nint = en .nextElement ();
277
277
if (!nint .isLoopback ()) {
278
- EthernetAddress addr = fromInterface ( nint );
279
- if (addr != null ) {
280
- return addr ;
278
+ byte [] data = nint . getHardwareAddress ( );
279
+ if (( data != null ) && ( data . length == 6 ) ) {
280
+ return new EthernetAddress ( data ) ;
281
281
}
282
282
}
283
283
}
@@ -287,83 +287,6 @@ public static EthernetAddress fromInterface()
287
287
return null ;
288
288
}
289
289
290
- /**
291
- * A factory method to return the ethernet address of a specified network interface.
292
- *
293
- * @since 4.1
294
- */
295
- public static EthernetAddress fromInterface (NetworkInterface nint )
296
- {
297
- if (nint != null ) {
298
- try {
299
- byte [] data = nint .getHardwareAddress ();
300
- if (data != null && data .length == 6 ) {
301
- return new EthernetAddress (data );
302
- }
303
- } catch (SocketException e ) {
304
- // could not get address
305
- }
306
- }
307
- return null ;
308
- }
309
-
310
- /**
311
- * A factory method that will try to determine the ethernet address of
312
- * the network interface that connects to the default network gateway.
313
- * To do this it will try to open a connection to one of the root DNS
314
- * servers, or barring that, to address 1.1.1.1, or finally if that also
315
- * fails then to IPv6 address "1::1". If a connection can be opened then
316
- * the interface through which that connection is routed is determined
317
- * to be the default egress interface, and the corresponding address of
318
- * that interface will be returned. If all attempts are unsuccessful,
319
- * null will be returned.
320
- *
321
- * @since 4.1
322
- */
323
- public static EthernetAddress fromEgressInterface ()
324
- {
325
- String roots = "abcdefghijklm" ;
326
- int index = new Random ().nextInt (roots .length ());
327
- String name = roots .charAt (index ) + ".root-servers.net" ;
328
- // Specify standard/default port DNS uses; more robust on some platforms
329
- // (MacOS/JDK 17), see:
330
- // https://github.com/cowtowncoder/java-uuid-generator/pull/59
331
- InetSocketAddress externalAddress = new InetSocketAddress (name , 53 );
332
- if (externalAddress .isUnresolved ()) {
333
- externalAddress = new InetSocketAddress ("1.1.1.1" , 0 );
334
- }
335
- EthernetAddress ifAddr = fromEgressInterface (externalAddress );
336
- if (ifAddr == null ) {
337
- return fromEgressInterface (new InetSocketAddress ("1::1" , 0 ));
338
- } else {
339
- return ifAddr ;
340
- }
341
- }
342
-
343
- /**
344
- * A factory method to return the address of the interface used to route
345
- * traffic to the specified IP address.
346
- *
347
- * @since 4.1
348
- */
349
- public static EthernetAddress fromEgressInterface (InetSocketAddress externalSocketAddress )
350
- {
351
- DatagramSocket socket = null ;
352
- try {
353
- socket = new DatagramSocket ();
354
- socket .connect (externalSocketAddress );
355
- InetAddress localAddress = socket .getLocalAddress ();
356
- NetworkInterface egressIf = NetworkInterface .getByInetAddress (localAddress );
357
- return fromInterface (egressIf );
358
- } catch (SocketException e ) {
359
- return null ;
360
- } finally {
361
- if (socket != null ) {
362
- socket .close ();
363
- }
364
- }
365
- }
366
-
367
290
/**
368
291
* Factory method that can be used to construct a random multicast
369
292
* address; to be used in cases where there is no "real" ethernet
0 commit comments