2000-03-24 20:59:00 +00:00
|
|
|
/* Find network interface names and index numbers. Hurd version.
|
2021-01-02 19:32:25 +00:00
|
|
|
Copyright (C) 2000-2021 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
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://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
|
|
|
|
2018-04-03 21:13:13 +00:00
|
|
|
if (strlen (ifname) >= IFNAMSIZ)
|
|
|
|
{
|
|
|
|
__set_errno (ENODEV);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-03 16:06:15 +00:00
|
|
|
|
2018-04-04 00:31:23 +00:00
|
|
|
strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
|
2001-07-01 12:06:11 +00:00
|
|
|
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
|
|
|
|
__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
|