Skip to content

Commit

Permalink
unix: fix wrong MAC of uv_interface_address
Browse files Browse the repository at this point in the history
fix a wrong `if` in `uv_interface_address` about MAC.

Fixes: nodejs/node#13581
  • Loading branch information
XadillaX committed Jul 17, 2017
1 parent 57f4180 commit f34d7e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
15 changes: 11 additions & 4 deletions src/unix/bsd-ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@
#include <net/if_dl.h>
#endif

static int uv__ifaddr_exclude(struct ifaddrs *ent) {
static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type) {
if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)))
return 1;
if (ent->ifa_addr == NULL)
return 1;
/*
* If `exclude_type` is `UV__EXCLUDE_IFPHYS`, just see whether `sa_family`
* equals to `AF_LINK` or not. Otherwise, the result depends on the operation
* system with `AF_LINK` or `PF_INET`.
*/
if (exclude_type == UV__EXCLUDE_IFPHYS)
return (ent->ifa_addr->sa_family != AF_LINK);
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__)
/*
* On BSD getifaddrs returns information related to the raw underlying
Expand Down Expand Up @@ -63,7 +70,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses, int* count) {

/* Count the number of interfaces */
for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
if (uv__ifaddr_exclude(ent))
if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFADDR))
continue;
(*count)++;
}
Expand All @@ -78,7 +85,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses, int* count) {
address = *addresses;

for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
if (uv__ifaddr_exclude(ent))
if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFADDR))
continue;

address->name = uv__strdup(ent->ifa_name);
Expand All @@ -102,7 +109,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses, int* count) {

/* Fill in physical addresses for each interface */
for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
if (uv__ifaddr_exclude(ent))
if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFPHYS))
continue;

address = *addresses;
Expand Down
6 changes: 6 additions & 0 deletions src/unix/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ enum {
UV_LOOP_BLOCK_SIGPROF = 1
};

/* flags of excluding ifaddr */
enum {
UV__EXCLUDE_IFPHYS,
UV__EXCLUDE_IFADDR
};

typedef enum {
UV_CLOCK_PRECISE = 0, /* Use the highest resolution clock available. */
UV_CLOCK_FAST = 1 /* Use the fastest clock with <= 1ms granularity. */
Expand Down
12 changes: 6 additions & 6 deletions src/unix/linux-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
uv__free(cpu_infos);
}

static int uv__ifaddr_exclude(struct ifaddrs *ent) {
static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type) {
if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)))
return 1;
if (ent->ifa_addr == NULL)
Expand All @@ -847,8 +847,8 @@ static int uv__ifaddr_exclude(struct ifaddrs *ent) {
* devices. We're not interested in this information yet.
*/
if (ent->ifa_addr->sa_family == PF_PACKET)
return 1;
return 0;
return exclude_type;
return !exclude_type;
}

int uv_interface_addresses(uv_interface_address_t** addresses,
Expand All @@ -869,7 +869,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses,

/* Count the number of interfaces */
for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
if (uv__ifaddr_exclude(ent))
if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFADDR))
continue;

(*count)++;
Expand All @@ -887,7 +887,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses,
address = *addresses;

for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
if (uv__ifaddr_exclude(ent))
if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFADDR))
continue;

address->name = uv__strdup(ent->ifa_name);
Expand All @@ -911,7 +911,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses,

/* Fill in physical addresses for each interface */
for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
if (uv__ifaddr_exclude(ent))
if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFPHYS))
continue;

address = *addresses;
Expand Down

0 comments on commit f34d7e2

Please sign in to comment.