Skip to content

Commit 952cfa5

Browse files
authored
pppd: Combine get_first_ethernet() and get_if_hwaddr() into one function (#207)
On all places is just needed hardware address for the first ethernet-style interface. So provide it by new get_first_ether_hwaddr() function. Signed-off-by: Pali Rohár <pali@kernel.org>
1 parent 18ad641 commit 952cfa5

5 files changed

Lines changed: 48 additions & 64 deletions

File tree

pppd/ipv6cp.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,18 +1068,10 @@ static int
10681068
ether_to_eui64(eui64_t *p_eui64)
10691069
{
10701070
u_char addr[6];
1071-
char *if_name;
1072-
1073-
if (get_if_hwaddr(addr, devnam) < 0) {
1074-
if ((if_name = get_first_ethernet()) == NULL) {
1075-
error("no persistent id can be found");
1076-
return 0;
1077-
}
1078-
1079-
if (get_if_hwaddr(addr, if_name) < 0) {
1080-
error("could not obtain hardware address for %s", if_name);
1081-
return 0;
1082-
}
1071+
1072+
if (get_if_hwaddr(addr, devnam) < 0 || get_first_ether_hwaddr(addr) < 0) {
1073+
error("ipv6cp: no persistent id can be found");
1074+
return 0;
10831075
}
10841076

10851077
/*

pppd/multilink.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,11 @@ owns_unit(TDB_DATA key, int unit)
430430
static int
431431
get_default_epdisc(struct epdisc *ep)
432432
{
433-
char *p;
434433
struct hostent *hp;
435434
u_int32_t addr;
436435

437436
/* First try for an ethernet MAC address */
438-
p = get_first_ethernet();
439-
if (p != 0 && get_if_hwaddr(ep->value, p) >= 0) {
437+
if (get_first_ether_hwaddr(ep->value) >= 0) {
440438
ep->class = EPD_MAC;
441439
ep->length = 6;
442440
return 1;

pppd/pppd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ int sipxfaddr(int, unsigned long, unsigned char *);
710710
int cipxfaddr(int);
711711
#endif
712712
int get_if_hwaddr(u_char *addr, char *name);
713-
char *get_first_ethernet(void);
713+
int get_first_ether_hwaddr(u_char *addr);
714714
int get_time(struct timeval *);
715715
/* Get current time, monotonic if possible. */
716716

pppd/sys-linux.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,46 +2159,43 @@ get_if_hwaddr(u_char *addr, char *name)
21592159
}
21602160

21612161
/*
2162-
* get_first_ethernet - return the name of the first ethernet-style
2163-
* interface on this system.
2162+
* get_first_ether_hwaddr - get the hardware address for the first
2163+
* ethernet-style interface on this system.
21642164
*/
2165-
static char first_ether_name[IF_NAMESIZE];
2166-
char *
2167-
get_first_ethernet(void)
2165+
int
2166+
get_first_ether_hwaddr(u_char *addr)
21682167
{
21692168
struct if_nameindex *if_ni, *i;
21702169
struct ifreq ifreq;
21712170
int ret, sock_fd;
21722171

21732172
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
21742173
if (sock_fd < 0)
2175-
return NULL;
2174+
return -1;
21762175

21772176
if_ni = if_nameindex();
21782177
if (!if_ni) {
21792178
close(sock_fd);
2180-
return NULL;
2179+
return -1;
21812180
}
21822181

2183-
first_ether_name[0] = 0;
2182+
ret = -1;
21842183

21852184
for (i = if_ni; !(i->if_index == 0 && i->if_name == NULL); i++) {
21862185
memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
21872186
strlcpy(ifreq.ifr_name, i->if_name, sizeof(ifreq.ifr_name));
21882187
ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
21892188
if (ret >= 0 && ifreq.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
2190-
strlcpy(first_ether_name, i->if_name, sizeof(first_ether_name));
2189+
memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
21912190
break;
21922191
}
2192+
ret = -1;
21932193
}
21942194

21952195
if_freenameindex(if_ni);
21962196
close(sock_fd);
21972197

2198-
if (!first_ether_name[0])
2199-
return NULL;
2200-
2201-
return first_ether_name;
2198+
return ret;
22022199
}
22032200

22042201
/********************************************************************

pppd/sys-solaris.c

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)