1 /* Find network interface names and index numbers. Hurd version.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
22 #include <sys/ioctl.h>
26 #include <hurd/ioctl.h>
27 #include <hurd/pfinet.h>
29 /* Return the interface index corresponding to interface IFNAME.
30 On error, return 0. */
32 __if_nametoindex (const char *ifname
)
35 int fd
= __opensock ();
40 strncpy (ifr
.ifr_name
, ifname
, IFNAMSIZ
);
41 if (__ioctl (fd
, SIOCGIFINDEX
, &ifr
) < 0)
43 int saved_errno
= errno
;
45 if (saved_errno
== EINVAL
|| saved_errno
== ENOTTY
)
50 return ifr
.ifr_ifindex
;
52 libc_hidden_def (__if_nametoindex
)
53 weak_alias (__if_nametoindex
, if_nametoindex
)
54 libc_hidden_weak (if_nametoindex
)
56 /* Free the structure IFN returned by if_nameindex. */
58 __if_freenameindex (struct if_nameindex
*ifn
)
60 struct if_nameindex
*ptr
= ifn
;
61 while (ptr
->if_name
|| ptr
->if_index
)
68 libc_hidden_def (__if_freenameindex
)
69 weak_alias (__if_freenameindex
, if_freenameindex
)
70 libc_hidden_weak (if_freenameindex
)
72 /* Return an array of if_nameindex structures, one for each network
73 interface present, plus one indicating the end of the array. On
74 error, return NULL. */
81 int fd
= __opensock ();
84 struct if_nameindex
*idx
= NULL
;
91 server
= _hurd_socket_server (PF_INET
, 0);
92 if (server
== MACH_PORT_NULL
)
96 size_t len
= sizeof data
;
97 err
= __pfinet_siocgifconf (server
, -1, &ifc
.ifc_buf
, &len
);
98 if (err
== MACH_SEND_INVALID_DEST
|| err
== MIG_SERVER_DIED
)
100 /* On the first use of the socket server during the operation,
101 allow for the old server port dying. */
102 server
= _hurd_socket_server (PF_INET
, 1);
103 if (server
== MACH_PORT_NULL
)
105 err
= __pfinet_siocgifconf (server
, -1, &ifc
.ifc_buf
, &len
);
111 nifs
= len
/ sizeof (struct ifreq
);
114 idx
= malloc ((nifs
+ 1) * sizeof (struct if_nameindex
));
121 for (i
= 0; i
< nifs
; ++i
)
123 struct ifreq
*ifr
= &ifc
.ifc_req
[i
];
124 idx
[i
].if_name
= __strdup (ifr
->ifr_name
);
125 if (idx
[i
].if_name
== NULL
126 || __ioctl (fd
, SIOCGIFINDEX
, ifr
) < 0)
131 for (j
= 0; j
< i
; ++j
)
132 free (idx
[j
].if_name
);
138 else if (err
== ENOMEM
)
142 idx
[i
].if_index
= ifr
->ifr_ifindex
;
146 idx
[i
].if_name
= NULL
;
150 if (data
!= ifc
.ifc_buf
)
151 __vm_deallocate (__mach_task_self (), (vm_address_t
) ifc
.ifc_buf
,
156 weak_alias (__if_nameindex
, if_nameindex
)
157 libc_hidden_weak (if_nameindex
)
159 /* Store the name of the interface corresponding to index IFINDEX in
160 IFNAME (which has space for at least IFNAMSIZ characters). Return
161 IFNAME, or NULL on error. */
163 __if_indextoname (unsigned int ifindex
, char *ifname
)
166 int fd
= __opensock ();
171 ifr
.ifr_ifindex
= ifindex
;
172 if (__ioctl (fd
, SIOCGIFNAME
, &ifr
) < 0)
174 int saved_errno
= errno
;
176 if (saved_errno
== EINVAL
|| saved_errno
== ENOTTY
)
177 __set_errno (ENOSYS
);
178 else if (saved_errno
== ENODEV
)
183 return strncpy (ifname
, ifr
.ifr_name
, IFNAMSIZ
);
185 weak_alias (__if_indextoname
, if_indextoname
)
186 libc_hidden_weak (if_indextoname
)
190 __protocol_available (int *have_inet
, int *have_inet6
)
192 *have_inet
= _hurd_socket_server (PF_INET
, 0) != MACH_PORT_NULL
;
193 *have_inet6
= _hurd_socket_server (PF_INET6
, 0) != MACH_PORT_NULL
;