Rizin
unix-like reverse engineering framework and cli tools
bsd-ifaddrs.c File Reference
#include "uv.h"
#include "internal.h"
#include <errno.h>
#include <stddef.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <net/if_dl.h>

Go to the source code of this file.

Functions

static int uv__ifaddr_exclude (struct ifaddrs *ent, int exclude_type)
 
int uv_interface_addresses (uv_interface_address_t **addresses, int *count)
 
void uv_free_interface_addresses (uv_interface_address_t *addresses, int count)
 

Function Documentation

◆ uv__ifaddr_exclude()

static int uv__ifaddr_exclude ( struct ifaddrs ent,
int  exclude_type 
)
static

Definition at line 38 of file bsd-ifaddrs.c.

38  {
39  if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)))
40  return 1;
41  if (ent->ifa_addr == NULL)
42  return 1;
43 #if !defined(__CYGWIN__) && !defined(__MSYS__)
44  /*
45  * If `exclude_type` is `UV__EXCLUDE_IFPHYS`, just see whether `sa_family`
46  * equals to `AF_LINK` or not. Otherwise, the result depends on the operation
47  * system with `AF_LINK` or `PF_INET`.
48  */
49  if (exclude_type == UV__EXCLUDE_IFPHYS)
50  return (ent->ifa_addr->sa_family != AF_LINK);
51 #endif
52 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__) || \
53  defined(__HAIKU__)
54  /*
55  * On BSD getifaddrs returns information related to the raw underlying
56  * devices. We're not interested in this information.
57  */
58  if (ent->ifa_addr->sa_family == AF_LINK)
59  return 1;
60 #elif defined(__NetBSD__) || defined(__OpenBSD__)
61  if (ent->ifa_addr->sa_family != PF_INET &&
62  ent->ifa_addr->sa_family != PF_INET6)
63  return 1;
64 #endif
65  return 0;
66 }
#define NULL
Definition: cris-opc.c:27
#define PF_INET
Definition: sftypes.h:248
struct sockaddr * ifa_addr
unsigned int ifa_flags
@ UV__EXCLUDE_IFPHYS
Definition: internal.h:142

References ifaddrs::ifa_addr, ifaddrs::ifa_flags, NULL, PF_INET, and UV__EXCLUDE_IFPHYS.

Referenced by uv_interface_addresses().

◆ uv_free_interface_addresses()

void uv_free_interface_addresses ( uv_interface_address_t addresses,
int  count 
)

Definition at line 154 of file bsd-ifaddrs.c.

155  {
156  int i;
157 
158  for (i = 0; i < count; i++) {
159  uv__free(addresses[i].name);
160  }
161 
162  uv__free(addresses);
163 }
lzma_index ** i
Definition: index.h:629
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
Definition: z80asm.h:102
void uv__free(void *ptr)
Definition: uv-common.c:81

References count, i, and uv__free().

◆ uv_interface_addresses()

int uv_interface_addresses ( uv_interface_address_t **  addresses,
int count 
)

Definition at line 68 of file bsd-ifaddrs.c.

68  {
69  struct ifaddrs* addrs;
70  struct ifaddrs* ent;
71  uv_interface_address_t* address;
72 #if !(defined(__CYGWIN__) || defined(__MSYS__))
73  int i;
74 #endif
75 
76  *count = 0;
77  *addresses = NULL;
78 
79  if (getifaddrs(&addrs) != 0)
80  return UV__ERR(errno);
81 
82  /* Count the number of interfaces */
83  for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
85  continue;
86  (*count)++;
87  }
88 
89  if (*count == 0) {
90  freeifaddrs(addrs);
91  return 0;
92  }
93 
94  /* Make sure the memory is initiallized to zero using calloc() */
95  *addresses = uv__calloc(*count, sizeof(**addresses));
96 
97  if (*addresses == NULL) {
98  freeifaddrs(addrs);
99  return UV_ENOMEM;
100  }
101 
102  address = *addresses;
103 
104  for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
106  continue;
107 
108  address->name = uv__strdup(ent->ifa_name);
109 
110  if (ent->ifa_addr->sa_family == AF_INET6) {
111  address->address.address6 = *((struct sockaddr_in6*) ent->ifa_addr);
112  } else {
113  address->address.address4 = *((struct sockaddr_in*) ent->ifa_addr);
114  }
115 
116  if (ent->ifa_netmask == NULL) {
117  memset(&address->netmask, 0, sizeof(address->netmask));
118  } else if (ent->ifa_netmask->sa_family == AF_INET6) {
119  address->netmask.netmask6 = *((struct sockaddr_in6*) ent->ifa_netmask);
120  } else {
121  address->netmask.netmask4 = *((struct sockaddr_in*) ent->ifa_netmask);
122  }
123 
124  address->is_internal = !!(ent->ifa_flags & IFF_LOOPBACK);
125 
126  address++;
127  }
128 
129 #if !(defined(__CYGWIN__) || defined(__MSYS__))
130  /* Fill in physical addresses for each interface */
131  for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
133  continue;
134 
135  address = *addresses;
136 
137  for (i = 0; i < *count; i++) {
138  if (strcmp(address->name, ent->ifa_name) == 0) {
139  struct sockaddr_dl* sa_addr;
140  sa_addr = (struct sockaddr_dl*)(ent->ifa_addr);
141  memcpy(address->phys_addr, LLADDR(sa_addr), sizeof(address->phys_addr));
142  }
143  address++;
144  }
145  }
146 #endif
147 
148  freeifaddrs(addrs);
149 
150  return 0;
151 }
__BEGIN_DECLS int getifaddrs(struct ifaddrs **ifap)
void freeifaddrs(struct ifaddrs *ifa)
static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type)
Definition: bsd-ifaddrs.c:38
#define UV__ERR(x)
Definition: errno.h:29
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define AF_INET6
Definition: sftypes.h:295
char * ifa_name
struct ifaddrs * ifa_next
struct sockaddr * ifa_netmask
struct sockaddr_in6 netmask6
Definition: uv.h:1106
struct sockaddr_in6 address6
Definition: uv.h:1102
struct sockaddr_in netmask4
Definition: uv.h:1105
union uv_interface_address_s::@399 netmask
union uv_interface_address_s::@398 address
struct sockaddr_in address4
Definition: uv.h:1101
char phys_addr[6]
Definition: uv.h:1098
@ UV__EXCLUDE_IFADDR
Definition: internal.h:143
char * uv__strdup(const char *s)
Definition: uv-common.c:55
void * uv__calloc(size_t count, size_t size)
Definition: uv-common.c:92

References uv_interface_address_s::address, uv_interface_address_s::address4, uv_interface_address_s::address6, AF_INET6, count, freeifaddrs(), getifaddrs(), i, ifaddrs::ifa_addr, ifaddrs::ifa_flags, ifaddrs::ifa_name, ifaddrs::ifa_netmask, ifaddrs::ifa_next, uv_interface_address_s::is_internal, memcpy(), memset(), uv_interface_address_s::name, uv_interface_address_s::netmask, uv_interface_address_s::netmask4, uv_interface_address_s::netmask6, NULL, uv_interface_address_s::phys_addr, uv__calloc(), UV__ERR, UV__EXCLUDE_IFADDR, UV__EXCLUDE_IFPHYS, uv__ifaddr_exclude(), and uv__strdup().