35#define LOG(kind, ...) GNUNET_log_from (kind, "util-os-network", __VA_ARGS__)
36#define LOG_STRERROR_FILE(kind, syscall, \
37 filename) GNUNET_log_from_strerror_file (kind, \
43#if ! (HAVE_GETIFADDRS && HAVE_FREEIFADDRS)
65 struct sockaddr_in a4;
66 struct sockaddr_in6 a6;
69 struct sockaddr_in bcaddr;
70 struct sockaddr_in netmask;
71 struct sockaddr_in6 netmask6;
72 struct sockaddr *pass_bcaddr;
73 struct sockaddr *pass_netmask;
79 const char *sbin_ifconfig;
82 if (0 == access (IFCONFIG, X_OK))
83 sbin_ifconfig = IFCONFIG;
86 if (0 == access (
"/sbin/ifconfig", X_OK))
87 sbin_ifconfig =
"/sbin/ifconfig";
88 else if (0 == access (
"/usr/sbin/ifconfig", X_OK))
89 sbin_ifconfig =
"/usr/sbin/ifconfig";
91 sbin_ifconfig =
"ifconfig";
96 f = popen (pcall,
"r");
108 while (NULL != fgets (
line,
sizeof(
line),
f))
110 if (strlen (
line) == 0)
115 if (! isspace (
line[0]))
119 if (ifc[strlen (ifc) - 1] ==
':')
120 ifc[strlen (ifc) - 1] =
'\0';
135 while ((
'\0' != *
start) && (isspace (*
start)))
139 memset (addrstr, 0, 128);
140 memset (netmaskstr, 0, 128);
141 memset (bcstr, 0, 128);
145 (3 == sscanf (
start,
"inet addr:%127s Bcast:%127s Mask:%127s", addrstr,
146 bcstr, netmaskstr)) ||
147 (2 == sscanf (
start,
"inet addr:%127s Mask:%127s", addrstr,
149 (2 == sscanf (
start,
"inet6 addr:%127s %d", addrstr, &prefixlen)) ||
151 (1 == sscanf (
start,
"inet %127s", addrstr)) ||
152 (1 == sscanf (
start,
"inet6 %127s", addrstr)))
155 if (1 == inet_pton (AF_INET, addrstr, &v4))
157 memset (&a4, 0,
sizeof(a4));
158 a4.sin_family = AF_INET;
159#if HAVE_SOCKADDR_IN_SIN_LEN
160 a4.sin_len = (u_char)
sizeof(
struct sockaddr_in);
166 if (1 == inet_pton (AF_INET, bcstr, &v4))
168 memset (&bcaddr, 0,
sizeof(bcaddr));
169 bcaddr.sin_family = AF_INET;
170#if HAVE_SOCKADDR_IN_SIN_LEN
171 bcaddr.sin_len = (u_char)
sizeof(
struct sockaddr_in);
173 bcaddr.sin_addr = v4;
174 pass_bcaddr = (
struct sockaddr *) &bcaddr;
176 if (1 == inet_pton (AF_INET, netmaskstr, &v4))
178 memset (&netmask, 0,
sizeof(netmask));
179 netmask.sin_family = AF_INET;
180#if HAVE_SOCKADDR_IN_SIN_LEN
181 netmask.sin_len = (u_char)
sizeof(
struct sockaddr_in);
183 netmask.sin_addr = v4;
184 pass_netmask = (
struct sockaddr *) &netmask;
189 proc (proc_cls, ifc,(0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE)),
190 (
const struct sockaddr *) &a4,
191 pass_bcaddr, pass_netmask,
sizeof(a4)))
196 if (1 == inet_pton (AF_INET6, addrstr, &v6))
198 memset (&a6, 0,
sizeof(a6));
199 a6.sin6_family = AF_INET6;
200#if HAVE_SOCKADDR_IN_SIN_LEN
201 a6.sin6_len = (u_char)
sizeof(
struct sockaddr_in6);
208 memset (v6.s6_addr, 0,
sizeof(v6.s6_addr));
209 for (i = 0; i < prefixlen; i++)
211 v6.s6_addr[i >> 3] |= 1 << (i & 7);
213 memset (&netmask6, 0,
sizeof(netmask6));
214 netmask6.sin6_family = AF_INET6;
215#if HAVE_SOCKADDR_IN_SIN_LEN
216 netmask6.sin6_len = (u_char)
sizeof(
struct sockaddr_in6);
218 netmask6.sin6_addr = v6;
220 pass_netmask = (
struct sockaddr *) &netmask6;
224 proc (proc_cls, ifc,(0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE)),
225 (
const struct sockaddr *) &a6,
226 NULL, pass_netmask,
sizeof(a6)))
254 struct sockaddr_in a4;
255 struct sockaddr_in6 a6;
258 struct sockaddr_in netmask;
259 struct sockaddr_in6 netmask6;
261 unsigned int prefixlen;
269 if (0 == access (PATH_TO_IP, X_OK))
273 if (0 == access (
"/sbin/ip", X_OK))
275 else if (0 == access (
"/usr/sbin/ip", X_OK))
280 "%s -o add 2> /dev/null",
283 f = popen (pcall,
"r");
292 while (NULL != fgets (
line,
sizeof(
line),
f))
301 memset (ifname, 0, 64);
302 memset (afstr, 0, 6);
303 memset (addrstr, 0, 128);
304 if (4 != sscanf (
line,
305 "%*u: %63s %5s %127s %6u",
312 if ((0 == strcasecmp (
"inet",
314 (1 == inet_pton (AF_INET,
318 memset (&a4, 0,
sizeof(a4));
319 a4.sin_family = AF_INET;
320#if HAVE_SOCKADDR_IN_SIN_LEN
321 a4.sin_len = (u_char)
sizeof(
struct sockaddr_in);
325 memset (&v4.s_addr, 0,
sizeof(v4.s_addr));
326 for (i = 0; i < prefixlen; i++)
327 v4.s_addr |= 1 << (i & 7);
328 memset (&netmask, 0,
sizeof(netmask));
329 netmask.sin_family = AF_INET;
330#if HAVE_SOCKADDR_IN_SIN_LEN
331 netmask.sin_len = (u_char)
sizeof(
struct sockaddr_in);
333 netmask.sin_addr = v4;
338 (0 == strcmp (ifname,
339 GNUNET_DEFAULT_INTERFACE)),
340 (
const struct sockaddr *) &a4,
342 (
const struct sockaddr *) &netmask,
347 if ((0 == strcasecmp (
"inet6",
349 (1 == inet_pton (AF_INET6,
353 memset (&a6, 0,
sizeof(a6));
354 a6.sin6_family = AF_INET6;
355#if HAVE_SOCKADDR_IN_SIN_LEN
356 a6.sin6_len = (u_char)
sizeof(
struct sockaddr_in6);
360 memset (v6.s6_addr, 0,
sizeof(v6.s6_addr));
361 for (i = 0; i < prefixlen; i++)
362 v6.s6_addr[i >> 3] |= 1 << (i & 7);
363 memset (&netmask6, 0,
sizeof(netmask6));
364 netmask6.sin6_family = AF_INET6;
365#if HAVE_SOCKADDR_IN_SIN_LEN
366 netmask6.sin6_len = (u_char)
sizeof(
struct sockaddr_in6);
368 netmask6.sin6_addr = v6;
373 (0 == strcmp (ifname,
374 GNUNET_DEFAULT_INTERFACE)),
375 (
const struct sockaddr *) &a6,
377 (
const struct sockaddr *) &netmask6,
400#if HAVE_GETIFADDRS && HAVE_FREEIFADDRS
401 struct ifaddrs *ifa_first;
402 struct ifaddrs *ifa_ptr;
405 if (getifaddrs (&ifa_first) == 0)
407 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
409 if ((ifa_ptr->ifa_name != NULL) && (ifa_ptr->ifa_addr != NULL) &&
410 ( (ifa_ptr->ifa_flags & IFF_UP) != 0) )
412 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
413 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
415 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
416 alen =
sizeof(
struct sockaddr_in);
418 alen =
sizeof(
struct sockaddr_in6);
420 proc (proc_cls, ifa_ptr->ifa_name,
421 (0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE)),
422 ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr,
423 ifa_ptr->ifa_netmask, alen))
427 freeifaddrs (ifa_first);
439 "Failed to enumerate network interfaces\n");
static int start
Set if we are to start default services (including ARM).
static char * line
Desired phone line (string to be converted to a hash).
static int replace
Replace DID Document Flag.
static const char * sbin_ip
Name and full path of IPTABLES binary.
@ GNUNET_ERROR_TYPE_WARNING
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
void GNUNET_OS_network_interfaces_list(GNUNET_OS_NetworkInterfaceProcessor proc, void *proc_cls)
Enumerate all network interfaces.
enum GNUNET_GenericReturnValue(* GNUNET_OS_NetworkInterfaceProcessor)(void *cls, const char *name, int isDefault, const struct sockaddr *addr, const struct sockaddr *broadcast_addr, const struct sockaddr *netmask, socklen_t addrlen)
Callback function invoked for each interface found.
static int try_ip(GNUNET_OS_NetworkInterfaceProcessor proc, void *proc_cls)
Try to enumerate all network interfaces using 'ip'.
#define LOG_STRERROR_FILE(kind, syscall, filename)
static int try_ifconfig(GNUNET_OS_NetworkInterfaceProcessor proc, void *proc_cls)
Try to enumerate all network interfaces using 'ifconfig'.