Updated to fedora-glibc-20080410T1907
[glibc.git] / sysdeps / mach / hurd / if_index.c
blob4ecda2298c4ad09c98a60d33486dc38232f43346
1 /* Find network interface names and index numbers. Hurd version.
2 Copyright (C) 2000,01,02 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <error.h>
21 #include <net/if.h>
22 #include <string.h>
23 #include <sys/ioctl.h>
24 #include <unistd.h>
26 #include <hurd.h>
27 #include <hurd/ioctl.h>
28 #include <hurd/pfinet.h>
30 /* Return the interface index corresponding to interface IFNAME.
31 On error, return 0. */
32 unsigned int
33 if_nametoindex (const char *ifname)
35 struct ifreq ifr;
36 int fd = __opensock ();
38 if (fd < 0)
39 return 0;
41 strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
42 if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
44 int saved_errno = errno;
45 __close (fd);
46 if (saved_errno == EINVAL || saved_errno == ENOTTY)
47 __set_errno (ENOSYS);
48 return 0;
50 __close (fd);
51 return ifr.ifr_ifindex;
53 libc_hidden_def (if_nametoindex)
55 /* Free the structure IFN returned by if_nameindex. */
56 void
57 if_freenameindex (struct if_nameindex *ifn)
59 struct if_nameindex *ptr = ifn;
60 while (ptr->if_name || ptr->if_index)
62 free (ptr->if_name);
63 ++ptr;
65 free (ifn);
68 /* Return an array of if_nameindex structures, one for each network
69 interface present, plus one indicating the end of the array. On
70 error, return NULL. */
71 struct if_nameindex *
72 if_nameindex (void)
74 error_t err = 0;
75 char data[2048];
76 file_t server;
77 int fd = __opensock ();
78 struct ifconf ifc;
79 unsigned int nifs, i;
80 struct if_nameindex *idx = NULL;
82 ifc.ifc_buf = data;
84 if (fd < 0)
85 return NULL;
87 server = _hurd_socket_server (PF_INET, 0);
88 if (server == MACH_PORT_NULL)
89 nifs = 0;
90 else
92 size_t len = sizeof data;
93 err = __pfinet_siocgifconf (server, -1, &ifc.ifc_buf, &len);
94 if (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED)
96 /* On the first use of the socket server during the operation,
97 allow for the old server port dying. */
98 server = _hurd_socket_server (PF_INET, 1);
99 if (server == MACH_PORT_NULL)
100 goto out;
101 err = __pfinet_siocgifconf (server, -1, &ifc.ifc_buf, &len);
103 if (err)
104 goto out;
106 ifc.ifc_len = len;
107 nifs = len / sizeof (struct ifreq);
110 idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
111 if (idx == NULL)
113 err = ENOBUFS;
114 goto out;
117 for (i = 0; i < nifs; ++i)
119 struct ifreq *ifr = &ifc.ifc_req[i];
120 idx[i].if_name = __strdup (ifr->ifr_name);
121 if (idx[i].if_name == NULL
122 || __ioctl (fd, SIOCGIFINDEX, ifr) < 0)
124 unsigned int j;
125 err = errno;
127 for (j = 0; j < i; ++j)
128 free (idx[j].if_name);
129 free (idx);
130 idx = NULL;
132 if (err == EINVAL)
133 err = ENOSYS;
134 else if (err == ENOMEM)
135 err = ENOBUFS;
136 goto out;
138 idx[i].if_index = ifr->ifr_ifindex;
141 idx[i].if_index = 0;
142 idx[i].if_name = NULL;
144 out:
145 __close (fd);
146 if (data != ifc.ifc_buf)
147 __vm_deallocate (__mach_task_self (), (vm_address_t) ifc.ifc_buf,
148 ifc.ifc_len);
149 __set_errno (err);
150 return idx;
153 /* Store the name of the interface corresponding to index IFINDEX in
154 IFNAME (which has space for at least IFNAMSIZ characters). Return
155 IFNAME, or NULL on error. */
156 char *
157 if_indextoname (unsigned int ifindex, char *ifname)
159 struct ifreq ifr;
160 int fd = __opensock ();
162 if (fd < 0)
163 return NULL;
165 ifr.ifr_ifindex = ifindex;
166 if (__ioctl (fd, SIOCGIFNAME, &ifr) < 0)
168 int saved_errno = errno;
169 __close (fd);
170 if (saved_errno == EINVAL || saved_errno == ENOTTY)
171 __set_errno (ENOSYS);
172 else if (saved_errno == ENODEV)
173 __set_errno (ENXIO);
174 return NULL;
176 __close (fd);
177 return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
179 libc_hidden_def (if_indextoname)
181 #if 0
182 void
183 internal_function
184 __protocol_available (int *have_inet, int *have_inet6)
186 *have_inet = _hurd_socket_server (PF_INET, 0) != MACH_PORT_NULL;
187 *have_inet6 = _hurd_socket_server (PF_INET6, 0) != MACH_PORT_NULL;
189 #endif