| 1 |
/* |
| 2 |
Copyright (C) 2001-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <errno.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <sys/socket.h> |
| 28 |
|
| 29 |
/* Solaris at least earlier 2.6 and before does not include |
| 30 |
the ioctl definitions if BSD_COMP is not set. */ |
| 31 |
#if defined __svr4__ |
| 32 |
# define BSD_COMP 1 |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <sys/ioctl.h> |
| 36 |
#include <net/if.h> |
| 37 |
|
| 38 |
#ifndef HAVE_STRUCT_IF_NAMEINDEX |
| 39 |
unsigned int |
| 40 |
if_nametoindex (const char *ifname) |
| 41 |
{ |
| 42 |
int result = 0; |
| 43 |
# ifdef SIOCGIFINDEX |
| 44 |
{ |
| 45 |
int fd = socket (AF_INET, SOCK_DGRAM, 0); |
| 46 |
if (fd >= 0) |
| 47 |
{ |
| 48 |
int rc; |
| 49 |
struct ifreq ifr; |
| 50 |
strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); |
| 51 |
ifr.ifr_name[IFNAMSIZ - 1] = '\0'; |
| 52 |
rc = ioctl (fd, SIOCGIFINDEX, &ifr); |
| 53 |
close (fd); |
| 54 |
if (rc == 0) |
| 55 |
return ifr.ifr_index; |
| 56 |
} |
| 57 |
} |
| 58 |
# endif |
| 59 |
{ |
| 60 |
struct if_nameindex *idx; |
| 61 |
idx = if_nameindex (); |
| 62 |
if (idx != NULL) |
| 63 |
{ |
| 64 |
struct if_nameindex *p; |
| 65 |
for (p = idx; p->if_index || p->if_name; ++p) |
| 66 |
{ |
| 67 |
if (strncmp (p->if_name, ifname, IFNAMSIZ) == 0) |
| 68 |
{ |
| 69 |
result = p->if_index; |
| 70 |
break; |
| 71 |
} |
| 72 |
} |
| 73 |
if_freenameindex (idx); |
| 74 |
} |
| 75 |
return result; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void |
| 80 |
if_freenameindex (struct if_nameindex *ifn) |
| 81 |
{ |
| 82 |
struct if_nameindex *ptr = ifn; |
| 83 |
if (!ifn) |
| 84 |
return; |
| 85 |
while (ptr->if_name || ptr->if_index) |
| 86 |
{ |
| 87 |
free (ptr->if_name); |
| 88 |
++ptr; |
| 89 |
} |
| 90 |
free (ifn); |
| 91 |
} |
| 92 |
|
| 93 |
struct if_nameindex * |
| 94 |
if_nameindex (void) |
| 95 |
{ |
| 96 |
# if defined SIOCGIFCONF |
| 97 |
int fd = socket (AF_INET, SOCK_DGRAM, 0); |
| 98 |
struct ifconf ifc; |
| 99 |
unsigned int i = 0; |
| 100 |
int rq_len, last_len; |
| 101 |
struct if_nameindex *idx = NULL; |
| 102 |
struct ifreq *ifr, *end, *cur; |
| 103 |
|
| 104 |
if (fd < 0) |
| 105 |
return NULL; |
| 106 |
|
| 107 |
/* A first estimate. */ |
| 108 |
rq_len = 4 * sizeof (struct ifreq); |
| 109 |
|
| 110 |
ifc.ifc_buf = NULL; |
| 111 |
ifc.ifc_len = 0; |
| 112 |
/* Read all the interfaces out of the kernel. */ |
| 113 |
do |
| 114 |
{ |
| 115 |
last_len = ifc.ifc_len; |
| 116 |
ifc.ifc_buf = alloca (ifc.ifc_len = rq_len); |
| 117 |
if (ifc.ifc_buf == NULL || ioctl (fd, SIOCGIFCONF, &ifc) < 0) |
| 118 |
{ |
| 119 |
close (fd); |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
rq_len *= 2; |
| 123 |
} |
| 124 |
while (ifc.ifc_len != last_len); |
| 125 |
|
| 126 |
i = 0; |
| 127 |
ifr = (struct ifreq *) ifc.ifc_req; |
| 128 |
end = (struct ifreq *) (ifr + ifc.ifc_len); |
| 129 |
while (ifr < end) |
| 130 |
{ |
| 131 |
cur = ifr; |
| 132 |
|
| 133 |
++ifr; |
| 134 |
|
| 135 |
/* We ignore the other families .. OK ? */ |
| 136 |
if (cur->ifr_addr.sa_family != AF_INET) |
| 137 |
continue; |
| 138 |
|
| 139 |
/* Make Room safely. */ |
| 140 |
{ |
| 141 |
struct if_nameindex *tidx = NULL; |
| 142 |
tidx = realloc (idx, (i + 1) * sizeof (*idx)); |
| 143 |
if (tidx == NULL) |
| 144 |
{ |
| 145 |
if_freenameindex (idx); |
| 146 |
close (fd); |
| 147 |
errno = ENOBUFS; |
| 148 |
return NULL; |
| 149 |
} |
| 150 |
idx = tidx; |
| 151 |
} |
| 152 |
|
| 153 |
/* FIXME: We did not deal with duplicates or interface aliases. */ |
| 154 |
|
| 155 |
idx[i].if_name = strdup (cur->ifr_name); |
| 156 |
if (idx[i].if_name == NULL) |
| 157 |
{ |
| 158 |
if_freenameindex (idx); |
| 159 |
close (fd); |
| 160 |
errno = ENOBUFS; |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
|
| 164 |
# if defined SIOCGIFINDEX |
| 165 |
if (ioctl (fd, SIOCGIFINDEX, cur) >= 0) |
| 166 |
idx[i].if_index = cur->ifr_index; |
| 167 |
else |
| 168 |
# endif |
| 169 |
idx[i].if_index = i + 1; |
| 170 |
i++; |
| 171 |
} |
| 172 |
|
| 173 |
/* Terminate the array with an empty solt. */ |
| 174 |
{ |
| 175 |
struct if_nameindex *tidx = NULL; |
| 176 |
tidx = realloc (idx, (i + 1) * sizeof (*idx)); |
| 177 |
if (tidx == NULL) |
| 178 |
{ |
| 179 |
if_freenameindex (idx); |
| 180 |
close (fd); |
| 181 |
errno = ENOBUFS; |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
idx = tidx; |
| 185 |
} |
| 186 |
idx[i].if_index = 0; |
| 187 |
idx[i].if_name = NULL; |
| 188 |
|
| 189 |
close (fd); |
| 190 |
return idx; |
| 191 |
|
| 192 |
# else |
| 193 |
errno = ENOSYS; |
| 194 |
return NULL; |
| 195 |
# endif |
| 196 |
} |
| 197 |
#endif |