2000-03-24 20:59:00 +00:00
|
|
|
/* Find network interface names and index numbers. Hurd version.
|
2016-01-04 16:05:18 +00:00
|
|
|
Copyright (C) 2000-2016 Free Software Foundation, Inc.
|
2000-03-24 20:59:00 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
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.
|
2000-03-24 20:59:00 +00:00
|
|
|
|
|
|
|
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
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2001-07-01 12:06:11 +00:00
|
|
|
#include <error.h>
|
2000-03-24 20:59:00 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <string.h>
|
2001-07-01 12:06:11 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2001-07-01 12:06:11 +00:00
|
|
|
#include <hurd.h>
|
|
|
|
#include <hurd/ioctl.h>
|
|
|
|
#include <hurd/pfinet.h>
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2001-07-01 12:06:11 +00:00
|
|
|
/* Return the interface index corresponding to interface IFNAME.
|
|
|
|
On error, return 0. */
|
|
|
|
unsigned int
|
2014-12-16 18:18:49 +00:00
|
|
|
__if_nametoindex (const char *ifname)
|
2001-07-01 12:06:11 +00:00
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd = __opensock ();
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2001-07-01 12:06:11 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return 0;
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2001-07-01 12:06:11 +00:00
|
|
|
strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
|
|
|
|
if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
|
2000-03-24 20:59:00 +00:00
|
|
|
{
|
2001-07-01 12:06:11 +00:00
|
|
|
int saved_errno = errno;
|
|
|
|
__close (fd);
|
|
|
|
if (saved_errno == EINVAL || saved_errno == ENOTTY)
|
|
|
|
__set_errno (ENOSYS);
|
|
|
|
return 0;
|
2000-03-24 20:59:00 +00:00
|
|
|
}
|
2001-07-01 12:06:11 +00:00
|
|
|
__close (fd);
|
|
|
|
return ifr.ifr_ifindex;
|
2000-03-24 20:59:00 +00:00
|
|
|
}
|
2014-12-16 18:18:49 +00:00
|
|
|
libc_hidden_def (__if_nametoindex)
|
|
|
|
weak_alias (__if_nametoindex, if_nametoindex)
|
|
|
|
libc_hidden_weak (if_nametoindex)
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2001-07-01 12:06:11 +00:00
|
|
|
/* Free the structure IFN returned by if_nameindex. */
|
|
|
|
void
|
2014-12-16 18:18:49 +00:00
|
|
|
__if_freenameindex (struct if_nameindex *ifn)
|
2000-03-24 20:59:00 +00:00
|
|
|
{
|
2001-07-01 12:06:11 +00:00
|
|
|
struct if_nameindex *ptr = ifn;
|
|
|
|
while (ptr->if_name || ptr->if_index)
|
2000-03-24 20:59:00 +00:00
|
|
|
{
|
2008-03-19 06:43:34 +00:00
|
|
|
free (ptr->if_name);
|
2001-07-01 12:06:11 +00:00
|
|
|
++ptr;
|
2000-03-24 20:59:00 +00:00
|
|
|
}
|
2001-07-01 12:06:11 +00:00
|
|
|
free (ifn);
|
2000-03-24 20:59:00 +00:00
|
|
|
}
|
2015-11-25 01:35:18 +00:00
|
|
|
libc_hidden_def (__if_freenameindex)
|
2014-12-16 18:18:49 +00:00
|
|
|
weak_alias (__if_freenameindex, if_freenameindex)
|
2015-11-25 01:35:18 +00:00
|
|
|
libc_hidden_weak (if_freenameindex)
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2001-07-01 12:06:11 +00:00
|
|
|
/* Return an array of if_nameindex structures, one for each network
|
|
|
|
interface present, plus one indicating the end of the array. On
|
|
|
|
error, return NULL. */
|
|
|
|
struct if_nameindex *
|
2014-12-16 18:18:49 +00:00
|
|
|
__if_nameindex (void)
|
2000-03-24 20:59:00 +00:00
|
|
|
{
|
2001-07-01 12:06:11 +00:00
|
|
|
error_t err = 0;
|
|
|
|
char data[2048];
|
|
|
|
file_t server;
|
|
|
|
int fd = __opensock ();
|
|
|
|
struct ifconf ifc;
|
|
|
|
unsigned int nifs, i;
|
|
|
|
struct if_nameindex *idx = NULL;
|
|
|
|
|
|
|
|
ifc.ifc_buf = data;
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
server = _hurd_socket_server (PF_INET, 0);
|
|
|
|
if (server == MACH_PORT_NULL)
|
|
|
|
nifs = 0;
|
|
|
|
else
|
2000-03-24 20:59:00 +00:00
|
|
|
{
|
2002-06-13 09:02:29 +00:00
|
|
|
size_t len = sizeof data;
|
|
|
|
err = __pfinet_siocgifconf (server, -1, &ifc.ifc_buf, &len);
|
2001-07-01 12:06:11 +00:00
|
|
|
if (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED)
|
2000-03-24 20:59:00 +00:00
|
|
|
{
|
2001-07-01 12:06:11 +00:00
|
|
|
/* On the first use of the socket server during the operation,
|
|
|
|
allow for the old server port dying. */
|
|
|
|
server = _hurd_socket_server (PF_INET, 1);
|
|
|
|
if (server == MACH_PORT_NULL)
|
|
|
|
goto out;
|
2002-06-13 09:02:29 +00:00
|
|
|
err = __pfinet_siocgifconf (server, -1, &ifc.ifc_buf, &len);
|
2000-03-24 20:59:00 +00:00
|
|
|
}
|
2001-07-01 12:06:11 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2002-06-13 09:02:29 +00:00
|
|
|
ifc.ifc_len = len;
|
|
|
|
nifs = len / sizeof (struct ifreq);
|
2001-07-01 12:06:11 +00:00
|
|
|
}
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2001-07-01 12:06:11 +00:00
|
|
|
idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
|
|
|
|
if (idx == NULL)
|
2000-03-24 20:59:00 +00:00
|
|
|
{
|
2001-07-01 12:06:11 +00:00
|
|
|
err = ENOBUFS;
|
|
|
|
goto out;
|
2000-03-24 20:59:00 +00:00
|
|
|
}
|
2001-07-01 12:06:11 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nifs; ++i)
|
2000-03-24 20:59:00 +00:00
|
|
|
{
|
2001-07-01 12:06:11 +00:00
|
|
|
struct ifreq *ifr = &ifc.ifc_req[i];
|
|
|
|
idx[i].if_name = __strdup (ifr->ifr_name);
|
|
|
|
if (idx[i].if_name == NULL
|
|
|
|
|| __ioctl (fd, SIOCGIFINDEX, ifr) < 0)
|
|
|
|
{
|
|
|
|
unsigned int j;
|
|
|
|
err = errno;
|
|
|
|
|
|
|
|
for (j = 0; j < i; ++j)
|
|
|
|
free (idx[j].if_name);
|
|
|
|
free (idx);
|
|
|
|
idx = NULL;
|
|
|
|
|
|
|
|
if (err == EINVAL)
|
|
|
|
err = ENOSYS;
|
|
|
|
else if (err == ENOMEM)
|
|
|
|
err = ENOBUFS;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
idx[i].if_index = ifr->ifr_ifindex;
|
2000-03-24 20:59:00 +00:00
|
|
|
}
|
|
|
|
|
2001-07-01 12:06:11 +00:00
|
|
|
idx[i].if_index = 0;
|
|
|
|
idx[i].if_name = NULL;
|
|
|
|
|
|
|
|
out:
|
|
|
|
__close (fd);
|
|
|
|
if (data != ifc.ifc_buf)
|
|
|
|
__vm_deallocate (__mach_task_self (), (vm_address_t) ifc.ifc_buf,
|
|
|
|
ifc.ifc_len);
|
|
|
|
__set_errno (err);
|
|
|
|
return idx;
|
2000-03-24 20:59:00 +00:00
|
|
|
}
|
2014-12-16 18:18:49 +00:00
|
|
|
weak_alias (__if_nameindex, if_nameindex)
|
2015-11-25 01:35:18 +00:00
|
|
|
libc_hidden_weak (if_nameindex)
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2001-07-01 12:06:11 +00:00
|
|
|
/* Store the name of the interface corresponding to index IFINDEX in
|
|
|
|
IFNAME (which has space for at least IFNAMSIZ characters). Return
|
|
|
|
IFNAME, or NULL on error. */
|
|
|
|
char *
|
2014-12-16 18:18:49 +00:00
|
|
|
__if_indextoname (unsigned int ifindex, char *ifname)
|
2000-03-24 20:59:00 +00:00
|
|
|
{
|
2001-07-01 12:06:11 +00:00
|
|
|
struct ifreq ifr;
|
|
|
|
int fd = __opensock ();
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ifr.ifr_ifindex = ifindex;
|
|
|
|
if (__ioctl (fd, SIOCGIFNAME, &ifr) < 0)
|
|
|
|
{
|
|
|
|
int saved_errno = errno;
|
|
|
|
__close (fd);
|
|
|
|
if (saved_errno == EINVAL || saved_errno == ENOTTY)
|
|
|
|
__set_errno (ENOSYS);
|
|
|
|
else if (saved_errno == ENODEV)
|
|
|
|
__set_errno (ENXIO);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
__close (fd);
|
|
|
|
return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
|
2000-03-24 20:59:00 +00:00
|
|
|
}
|
2014-12-16 18:18:49 +00:00
|
|
|
weak_alias (__if_indextoname, if_indextoname)
|
|
|
|
libc_hidden_weak (if_indextoname)
|
2000-03-24 20:59:00 +00:00
|
|
|
|
2000-12-27 08:14:22 +00:00
|
|
|
#if 0
|
2000-03-24 20:59:00 +00:00
|
|
|
void
|
|
|
|
internal_function
|
|
|
|
__protocol_available (int *have_inet, int *have_inet6)
|
|
|
|
{
|
|
|
|
*have_inet = _hurd_socket_server (PF_INET, 0) != MACH_PORT_NULL;
|
|
|
|
*have_inet6 = _hurd_socket_server (PF_INET6, 0) != MACH_PORT_NULL;
|
|
|
|
}
|
2000-12-27 08:14:22 +00:00
|
|
|
#endif
|