1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/* Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Bruno Haible <bruno@clisp.org>, 2002.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <net/if.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <net/route.h>
#include <net/if_dl.h>
#include <alloca.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
typedef int (*if_fn) (void *private, unsigned int index, const char *name);
/* Iterate through all present interfaces.
Call FN once for every interface, returning immediately if FN returns
a nonzero value. */
static void
if_iterate (if_fn fn, void *private)
{
int request[6] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
char *buf;
size_t bufsize = 512;
char *bufend;
char *p;
/* Call the kernel function sysctl_iflist() in /usr/src/sys/net/rtsock.c. */
for (;;)
{
buf = alloca (bufsize);
if (__sysctl (request, 6, buf, &bufsize, NULL, 0) >= 0)
break;
if (errno != ENOMEM)
return;
bufsize *= 2;
}
bufend = buf + bufsize;
for (p = buf; p < bufend; )
{
struct if_msghdr *msg = (struct if_msghdr *) p;
if (msg->ifm_version != RTM_VERSION)
abort ();
switch (msg->ifm_type)
{
case RTM_IFINFO:
if (msg->ifm_addrs & RTA_IFP)
{
unsigned int index;
struct sockaddr_dl *sdl;
char namebuf[IFNAMSIZ + 1];
size_t namelen;
index = msg->ifm_index;
if (index == 0)
abort ();
/* FIXME: 'struct if_msghdr' contains a 'struct if_data' which in turns
contains 'unsigned long' values. Their size therefore depends on
the running kernel (32 or 64 bits). This should be fixed in the
compat layer of the kernel. Meanwhile just workaround the bug here/ */
#if 0
sdl = (struct sockaddr_dl *) (msg + 1);
#else
sdl = (struct sockaddr_dl *) (p + msg->ifm_msglen - sizeof(struct sockaddr_dl) - 2);
#endif
namelen = sdl->sdl_nlen;
/* Avoid overflowing namebuf[]. */
if (namelen > IFNAMSIZ)
namelen = IFNAMSIZ;
memcpy (namebuf, sdl->sdl_data, namelen);
namebuf[namelen] = '\0';
/* Call FN now. */
if (fn (private, index, namebuf))
return;
}
break;
case RTM_NEWADDR:
break;
default:
abort ();
}
p += msg->ifm_msglen;
}
}
/* ------------------------------------------------------------------------- */
struct nametoindex_locals
{
const char *name;
unsigned int index;
};
static int
nametoindex_aux (void *private, unsigned int index, const char *name)
{
struct nametoindex_locals *l = (struct nametoindex_locals *) private;
if (strcmp (name, l->name) == 0)
{
l->index = index;
return 1;
}
return 0;
}
/* Return the index of an interface given by name. */
unsigned int
__if_nametoindex (const char *ifname)
{
struct nametoindex_locals l;
l.name = ifname;
l.index = 0;
if_iterate (nametoindex_aux, &l);
return l.index;
}
libc_hidden_def (__if_nametoindex)
weak_alias (__if_nametoindex, if_nametoindex)
libc_hidden_weak (if_nametoindex)
/* ------------------------------------------------------------------------- */
struct indextoname_locals
{
unsigned int index;
char *name;
char *retval;
};
static int
indextoname_aux (void *private, unsigned int index, const char *name)
{
struct indextoname_locals *l = (struct indextoname_locals *) private;
if (index == l->index)
{
strncpy (l->name, name, IF_NAMESIZE);
l->retval = l->name;
return 1;
}
__set_errno (ENXIO);
return 0;
}
/* Return the name of an interface given by name. */
char *
__if_indextoname (unsigned int ifindex, char *ifname)
{
struct indextoname_locals l;
l.index = ifindex;
l.name = ifname;
l.retval = NULL;
if_iterate (indextoname_aux, &l);
return l.retval;
}
weak_alias (__if_indextoname, if_indextoname)
libc_hidden_weak (if_indextoname)
/* ------------------------------------------------------------------------- */
struct nameindex_locals
{
/* Resizable array of 'struct if_nameindex'. */
struct if_nameindex *s_array;
size_t s_len;
size_t s_allocated;
/* Resizable array of char. */
char *c_array;
size_t c_len;
size_t c_allocated;
/* Out-of-memory indicator. */
int oom;
};
static void
add_s (struct nameindex_locals *l, unsigned int index, char *name)
{
if (l->s_len == l->s_allocated)
{
size_t new_allocated = 2 * l->s_allocated + 1;
struct if_nameindex *new_array =
(struct if_nameindex *)
realloc (l->s_array, new_allocated * sizeof (struct if_nameindex));
if (new_array == NULL)
{
l->oom = 1;
return;
}
l->s_array = new_array;
l->s_allocated = new_allocated;
}
/* Now l->s_len < l->s_allocated. */
l->s_array[l->s_len].if_index = index;
l->s_array[l->s_len].if_name = name;
l->s_len++;
}
static __inline size_t
add_c (struct nameindex_locals *l, const char *name)
{
size_t n = strlen (name) + 1;
size_t result_offset;
if (l->c_len + n > l->c_allocated)
{
size_t new_allocated =
(l->c_len + n < 2 * l->c_allocated + 1
? 2 * l->c_allocated + 1
: l->c_len + n);
char *new_array = (char *) realloc (l->c_array, new_allocated);
if (new_array == NULL)
{
l->oom = 1;
return 0;
}
l->c_array = new_array;
l->c_allocated = new_allocated;
}
/* Now l->c_len + n <= l->c_allocated. */
result_offset = l->c_len;
memcpy (l->c_array + l->c_len, name, n);
l->c_len += n;
return result_offset;
}
static int
nameindex_aux (void *private, unsigned int index, const char *name)
{
struct nameindex_locals *l = (struct nameindex_locals *) private;
size_t name_offset = add_c (l, name);
if (!l->oom)
{
add_s (l, index, (char *) NULL + name_offset);
if (!l->oom)
return 0;
}
return 1;
}
/* Return an array of 'struct if_nameindex', one for each present
interface. */
struct if_nameindex *
__if_nameindex (void)
{
struct nameindex_locals l;
l.s_array = NULL; l.s_len = 0; l.s_allocated = 0;
l.c_array = NULL; l.c_len = 0; l.c_allocated = 0;
l.oom = 0;
if_iterate (nameindex_aux, &l);
if (!l.oom)
{
/* Convert all offsets to real pointers. */
struct if_nameindex *p;
struct if_nameindex *p_end;
for (p = l.s_array, p_end = p + l.s_len; p < p_end; p++)
p->if_name = l.c_array + (p->if_name - (char *) NULL);
/* Add a terminating entry. */
add_s (&l, 0, NULL);
}
if (l.oom)
{
free (l.s_array);
free (l.c_array);
__set_errno (ENOMEM);
return NULL;
}
return l.s_array;
}
weak_alias (__if_nameindex, if_nameindex)
libc_hidden_weak (if_nameindex)
/* ------------------------------------------------------------------------- */
/* Free an array returned by if_nameindex(). */
void
__if_freenameindex (struct if_nameindex *ifn)
{
if (ifn != NULL)
{
/* Free c_array. */
free (ifn[0].if_name);
/* Free s_array. */
free (ifn);
}
}
libc_hidden_def (__if_freenameindex)
weak_alias (__if_freenameindex, if_freenameindex)
libc_hidden_weak (if_freenameindex)
|