@@ -218,10 +218,7 @@ static int if6_is_up = 0; /* IPv6 interface has been marked up */
218218
219219#endif /* defined(INET6) && defined(SOL2) */
220220
221- #if defined(INET6 ) && defined(SOL2 )
222- static char first_ether_name [LIFNAMSIZ ]; /* Solaris 8 and above */
223- #else
224- static char first_ether_name [IFNAMSIZ ]; /* Before Solaris 8 */
221+ #if !defined(INET6 ) || !defined(SOL2 )
225222#define MAXIFS 256 /* Max # of interfaces */
226223#endif /* defined(INET6) && defined(SOL2) */
227224
@@ -294,13 +291,13 @@ sifppa(fd, ppa)
294291
295292#if defined(SOL2 ) && defined(INET6 )
296293/*
297- * get_first_ethernet - returns the first Ethernet interface name found in
298- * the system, or NULL if none is found
294+ * get_first_ether_hwaddr - get the hardware address for the first
295+ * ethernet-style interface on this system.
299296 *
300297 * NOTE: This is the lifreq version (Solaris 8 and above)
301298 */
302- char *
303- get_first_ethernet ( void )
299+ int
300+ get_first_ether_hwaddr ( u_char * addr )
304301{
305302 struct lifnum lifn ;
306303 struct lifconf lifc ;
@@ -312,7 +309,7 @@ get_first_ethernet(void)
312309
313310 fd = socket (AF_INET , SOCK_DGRAM , 0 );
314311 if (fd < 0 ) {
315- return 0 ;
312+ return -1 ;
316313 }
317314
318315 /*
@@ -323,7 +320,7 @@ get_first_ethernet(void)
323320 if (ioctl (fd , SIOCGLIFNUM , & lifn ) < 0 ) {
324321 close (fd );
325322 error ("could not determine number of interfaces: %m" );
326- return 0 ;
323+ return -1 ;
327324 }
328325
329326 num_ifs = lifn .lifn_count ;
@@ -332,7 +329,7 @@ get_first_ethernet(void)
332329 if (req == NULL ) {
333330 close (fd );
334331 error ("out of memory" );
335- return 0 ;
332+ return -1 ;
336333 }
337334
338335 /*
@@ -346,7 +343,7 @@ get_first_ethernet(void)
346343 close (fd );
347344 free (req );
348345 error ("SIOCGLIFCONF: %m" );
349- return 0 ;
346+ return -1 ;
350347 }
351348
352349 /*
@@ -363,38 +360,38 @@ get_first_ethernet(void)
363360 memset (& lifr , 0 , sizeof (lifr ));
364361 strncpy (lifr .lifr_name , plifreq -> lifr_name , sizeof (lifr .lifr_name ));
365362 if (ioctl (fd , SIOCGLIFFLAGS , & lifr ) < 0 ) {
366- close (fd );
367- free (req );
368363 error ("SIOCGLIFFLAGS: %m" );
369- return 0 ;
364+ break ;
370365 }
371366 fl = lifr .lifr_flags ;
372367
373368 if ((fl & (IFF_UP |IFF_BROADCAST |IFF_POINTOPOINT |IFF_LOOPBACK |IFF_NOARP ))
374369 != (IFF_UP | IFF_BROADCAST ))
375370 continue ;
376371
372+ if (get_if_hwaddr (addr , lifr .lifr_name ) < 0 )
373+ continue ;
374+
377375 found = 1 ;
378376 break ;
379377 }
380378 free (req );
381379 close (fd );
382380
383- if (found ) {
384- strncpy (first_ether_name , lifr .lifr_name , sizeof (first_ether_name ));
385- return (char * )first_ether_name ;
386- } else
387- return NULL ;
381+ if (found )
382+ return 0 ;
383+ else
384+ return -1 ;
388385}
389386#else
390387/*
391- * get_first_ethernet - returns the first Ethernet interface name found in
392- * the system, or NULL if none is found
388+ * get_first_ether_hwaddr - get the hardware address for the first
389+ * ethernet-style interface on this system.
393390 *
394391 * NOTE: This is the ifreq version (before Solaris 8).
395392 */
396- char *
397- get_first_ethernet ( void )
393+ int
394+ get_first_ether_hwaddr ( u_char * addr )
398395{
399396 struct ifconf ifc ;
400397 struct ifreq * pifreq ;
@@ -405,7 +402,7 @@ get_first_ethernet(void)
405402
406403 fd = socket (AF_INET , SOCK_DGRAM , 0 );
407404 if (fd < 0 ) {
408- return 0 ;
405+ return -1 ;
409406 }
410407
411408 /*
@@ -420,7 +417,7 @@ get_first_ethernet(void)
420417 if (req == NULL ) {
421418 close (fd );
422419 error ("out of memory" );
423- return 0 ;
420+ return -1 ;
424421 }
425422
426423 /*
@@ -432,7 +429,7 @@ get_first_ethernet(void)
432429 close (fd );
433430 free (req );
434431 error ("SIOCGIFCONF: %m" );
435- return 0 ;
432+ return -1 ;
436433 }
437434
438435 /*
@@ -449,28 +446,28 @@ get_first_ethernet(void)
449446 memset (& ifr , 0 , sizeof (ifr ));
450447 strncpy (ifr .ifr_name , pifreq -> ifr_name , sizeof (ifr .ifr_name ));
451448 if (ioctl (fd , SIOCGIFFLAGS , & ifr ) < 0 ) {
452- close (fd );
453- free (req );
454449 error ("SIOCGIFFLAGS: %m" );
455- return 0 ;
450+ break ;
456451 }
457452 fl = ifr .ifr_flags ;
458453
459454 if ((fl & (IFF_UP |IFF_BROADCAST |IFF_POINTOPOINT |IFF_LOOPBACK |IFF_NOARP ))
460455 != (IFF_UP | IFF_BROADCAST ))
461456 continue ;
462457
458+ if (get_if_hwaddr (addr , ifr .ifr_name ) < 0 )
459+ continue ;
460+
463461 found = 1 ;
464462 break ;
465463 }
466464 free (req );
467465 close (fd );
468466
469- if (found ) {
470- strncpy (first_ether_name , ifr .ifr_name , sizeof (first_ether_name ));
471- return (char * )first_ether_name ;
472- } else
473- return NULL ;
467+ if (found )
468+ return 0 ;
469+ else
470+ return -1 ;
474471}
475472#endif /* defined(SOL2) && defined(INET6) */
476473
0 commit comments