Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / mach / hurd / if_index.c
blob9d0ca62fa12a8f47fdc4a936a8055feb7eca7e06
1 /* Find network interface names and index numbers. Hurd version.
2 Copyright (C) 2000-2014 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/>. */
19 #include <error.h>
20 #include <net/if.h>
21 #include <string.h>
22 #include <sys/ioctl.h>
23 #include <unistd.h>
25 #include <hurd.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. */
31 unsigned int
32 if_nametoindex (const char *ifname)
34 struct ifreq ifr;
35 int fd = __opensock ();
37 if (fd < 0)
38 return 0;
40 strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
41 if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
43 int saved_errno = errno;
44 __close (fd);
45 if (saved_errno == EINVAL || saved_errno == ENOTTY)
46 __set_errno (ENOSYS);
47 return 0;
49 __close (fd);
50 return ifr.ifr_ifindex;
52 libc_hidden_def (if_nametoindex)
54 /* Free the structure IFN returned by if_nameindex. */
55 void
56 if_freenameindex (struct if_nameindex *ifn)
58 struct if_nameindex *ptr = ifn;
59 while (ptr->if_name || ptr->if_index)
61 free (ptr->if_name);
62 ++ptr;
64 free (ifn);
67 /* Return an array of if_nameindex structures, one for each network
68 interface present, plus one indicating the end of the array. On
69 error, return NULL. */
70 struct if_nameindex *
71 if_nameindex (void)
73 error_t err = 0;
74 char data[2048];
75 file_t server;
76 int fd = __opensock ();
77 struct ifconf ifc;
78 unsigned int nifs, i;
79 struct if_nameindex *idx = NULL;
81 ifc.ifc_buf = data;
83 if (fd < 0)
84 return NULL;
86 server = _hurd_socket_server (PF_INET, 0);
87 if (server == MACH_PORT_NULL)
88 nifs = 0;
89 else
91 size_t len = sizeof data;
92 err = __pfinet_siocgifconf (server, -1, &ifc.ifc_buf, &len);
93 if (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED)
95 /* On the first use of the socket server during the operation,
96 allow for the old server port dying. */
97 server = _hurd_socket_server (PF_INET, 1);
98 if (server == MACH_PORT_NULL)
99 goto out;
100 err = __pfinet_siocgifconf (server, -1, &ifc.ifc_buf, &len);
102 if (err)
103 goto out;
105 ifc.ifc_len = len;
106 nifs = len / sizeof (struct ifreq);
109 idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
110 if (idx == NULL)
112 err = ENOBUFS;
113 goto out;
116 for (i = 0; i < nifs; ++i)
118 struct ifreq *ifr = &ifc.ifc_req[i];
119 idx[i].if_name = __strdup (ifr->ifr_name);
120 if (idx[i].if_name == NULL
121 || __ioctl (fd, SIOCGIFINDEX, ifr) < 0)
123 unsigned int j;
124 err = errno;
126 for (j = 0; j < i; ++j)
127 free (idx[j].if_name);
128 free (idx);
129 idx = NULL;
131 if (err == EINVAL)
132 err = ENOSYS;
133 else if (err == ENOMEM)
134 err = ENOBUFS;
135 goto out;
137 idx[i].if_index = ifr->ifr_ifindex;
140 idx[i].if_index = 0;
141 idx[i].if_name = NULL;
143 out:
144 __close (fd);
145 if (data != ifc.ifc_buf)
146 __vm_deallocate (__mach_task_self (), (vm_address_t) ifc.ifc_buf,
147 ifc.ifc_len);
148 __set_errno (err);
149 return idx;
152 /* Store the name of the interface corresponding to index IFINDEX in
153 IFNAME (which has space for at least IFNAMSIZ characters). Return
154 IFNAME, or NULL on error. */
155 char *
156 if_indextoname (unsigned int ifindex, char *ifname)
158 struct ifreq ifr;
159 int fd = __opensock ();
161 if (fd < 0)
162 return NULL;
164 ifr.ifr_ifindex = ifindex;
165 if (__ioctl (fd, SIOCGIFNAME, &ifr) < 0)
167 int saved_errno = errno;
168 __close (fd);
169 if (saved_errno == EINVAL || saved_errno == ENOTTY)
170 __set_errno (ENOSYS);
171 else if (saved_errno == ENODEV)
172 __set_errno (ENXIO);
173 return NULL;
175 __close (fd);
176 return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
178 libc_hidden_def (if_indextoname)
180 #if 0
181 void
182 internal_function
183 __protocol_available (int *have_inet, int *have_inet6)
185 *have_inet = _hurd_socket_server (PF_INET, 0) != MACH_PORT_NULL;
186 *have_inet6 = _hurd_socket_server (PF_INET6, 0) != MACH_PORT_NULL;
188 #endif