1 /* 2 * Copyright (c) 2000 - 2001 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifdef HAVE_CONFIG_H 35 #include <config.h> 36 RCSID("$Id: getifaddrs.c,v 1.4 2001/01/28 23:02:46 assar Exp $"); 37 #endif 38 #include "roken.h" 39 40 #ifdef __osf__ 41 /* hate */ 42 struct rtentry; 43 struct mbuf; 44 #endif 45 #ifdef HAVE_NET_IF_H 46 #include <net/if.h> 47 #endif 48 49 #ifdef HAVE_SYS_SOCKIO_H 50 #include <sys/sockio.h> 51 #endif /* HAVE_SYS_SOCKIO_H */ 52 53 #ifdef HAVE_NETINET_IN6_VAR_H 54 #include <netinet/in6_var.h> 55 #endif /* HAVE_NETINET_IN6_VAR_H */ 56 57 #include <ifaddrs.h> 58 59 static int 60 getifaddrs2(struct ifaddrs **ifap, 61 int af, int siocgifconf, int siocgifflags, 62 size_t ifreq_sz) 63 { 64 int ret; 65 int fd; 66 size_t buf_size; 67 char *buf; 68 struct ifconf ifconf; 69 int num, j = 0; 70 char *p; 71 size_t sz; 72 struct sockaddr sa_zero; 73 struct ifreq *ifr; 74 75 struct ifaddrs *start, **end = &start; 76 77 buf = NULL; 78 79 memset (&sa_zero, 0, sizeof(sa_zero)); 80 fd = socket(af, SOCK_DGRAM, 0); 81 if (fd < 0) 82 return -1; 83 84 buf_size = 8192; 85 for (;;) { 86 buf = calloc(1, buf_size); 87 if (buf == NULL) { 88 ret = ENOMEM; 89 goto error_out; 90 } 91 ifconf.ifc_len = buf_size; 92 ifconf.ifc_buf = buf; 93 94 /* 95 * Solaris returns EINVAL when the buffer is too small. 96 */ 97 if (ioctl (fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) { 98 ret = errno; 99 goto error_out; 100 } 101 /* 102 * Can the difference between a full and a overfull buf 103 * be determined? 104 */ 105 106 if (ifconf.ifc_len < buf_size) 107 break; 108 free (buf); 109 buf_size *= 2; 110 } 111 112 num = ifconf.ifc_len / ifreq_sz; 113 j = 0; 114 for (p = ifconf.ifc_buf; 115 p < ifconf.ifc_buf + ifconf.ifc_len; 116 p += sz) { 117 struct ifreq ifreq; 118 struct sockaddr *sa; 119 size_t salen; 120 121 ifr = (struct ifreq *)p; 122 sa = &ifr->ifr_addr; 123 124 sz = ifreq_sz; 125 salen = sizeof(struct sockaddr); 126 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 127 salen = sa->sa_len; 128 sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len); 129 #endif 130 #ifdef SA_LEN 131 salen = SA_LEN(sa); 132 sz = max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa)); 133 #endif 134 memset (&ifreq, 0, sizeof(ifreq)); 135 memcpy (ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name)); 136 137 if (ioctl(fd, siocgifflags, &ifreq) < 0) { 138 ret = errno; 139 goto error_out; 140 } 141 142 *end = malloc(sizeof(**end)); 143 144 (*end)->ifa_next = NULL; 145 (*end)->ifa_name = strdup(ifr->ifr_name); 146 (*end)->ifa_flags = ifreq.ifr_flags; 147 (*end)->ifa_addr = malloc(salen); 148 memcpy((*end)->ifa_addr, sa, salen); 149 (*end)->ifa_netmask = NULL; 150 151 #if 0 152 /* fix these when we actually need them */ 153 if(ifreq.ifr_flags & IFF_BROADCAST) { 154 (*end)->ifa_broadaddr = malloc(sizeof(ifr->ifr_broadaddr)); 155 memcpy((*end)->ifa_broadaddr, &ifr->ifr_broadaddr, 156 sizeof(ifr->ifr_broadaddr)); 157 } else if(ifreq.ifr_flags & IFF_POINTOPOINT) { 158 (*end)->ifa_dstaddr = malloc(sizeof(ifr->ifr_dstaddr)); 159 memcpy((*end)->ifa_dstaddr, &ifr->ifr_dstaddr, 160 sizeof(ifr->ifr_dstaddr)); 161 } else 162 (*end)->ifa_dstaddr = NULL; 163 #else 164 (*end)->ifa_dstaddr = NULL; 165 #endif 166 167 (*end)->ifa_data = NULL; 168 169 end = &(*end)->ifa_next; 170 171 } 172 *ifap = start; 173 free(buf); 174 return 0; 175 error_out: 176 free(buf); 177 errno = ret; 178 return -1; 179 } 180 181 int 182 getifaddrs(struct ifaddrs **ifap) 183 { 184 int ret = -1; 185 errno = ENXIO; 186 #if defined(AF_INET6) && defined(SIOCGIF6CONF) && defined(SIOCGIF6FLAGS) 187 if (ret) 188 ret = getifaddrs2 (ifap, AF_INET6, SIOCGIF6CONF, SIOCGIF6FLAGS, 189 sizeof(struct in6_ifreq)); 190 #endif 191 #if defined(HAVE_IPV6) && defined(SIOCGIFCONF) 192 if (ret) 193 ret = getifaddrs2 (ifap, AF_INET6, SIOCGIFCONF, SIOCGIFFLAGS, 194 sizeof(struct ifreq)); 195 #endif 196 #if defined(AF_INET) && defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS) 197 if (ret) 198 ret = getifaddrs2 (ifap, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS, 199 sizeof(struct ifreq)); 200 #endif 201 return ret; 202 } 203 204 void 205 freeifaddrs(struct ifaddrs *ifp) 206 { 207 struct ifaddrs *p, *q; 208 209 for(p = ifp; p; ) { 210 free(p->ifa_name); 211 if(p->ifa_addr) 212 free(p->ifa_addr); 213 if(p->ifa_dstaddr) 214 free(p->ifa_dstaddr); 215 if(p->ifa_netmask) 216 free(p->ifa_netmask); 217 if(p->ifa_data) 218 free(p->ifa_data); 219 q = p; 220 p = p->ifa_next; 221 free(q); 222 } 223 } 224 225 #ifdef TEST 226 227 void 228 print_addr(const char *s, struct sockaddr *sa) 229 { 230 int i; 231 printf(" %s=%d/", s, sa->sa_family); 232 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 233 for(i = 0; i < sa->sa_len - ((long)sa->sa_data - (long)&sa->sa_family); i++) 234 printf("%02x", ((unsigned char*)sa->sa_data)[i]); 235 #else 236 for(i = 0; i < sizeof(sa->sa_data); i++) 237 printf("%02x", ((unsigned char*)sa->sa_data)[i]); 238 #endif 239 printf("\n"); 240 } 241 242 void 243 print_ifaddrs(struct ifaddrs *x) 244 { 245 struct ifaddrs *p; 246 247 for(p = x; p; p = p->ifa_next) { 248 printf("%s\n", p->ifa_name); 249 printf(" flags=%x\n", p->ifa_flags); 250 if(p->ifa_addr) 251 print_addr("addr", p->ifa_addr); 252 if(p->ifa_dstaddr) 253 print_addr("dstaddr", p->ifa_dstaddr); 254 if(p->ifa_netmask) 255 print_addr("netmask", p->ifa_netmask); 256 printf(" %p\n", p->ifa_data); 257 } 258 } 259 260 int 261 main() 262 { 263 struct ifaddrs *a = NULL, *b; 264 getifaddrs2(&a, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS, sizeof(struct ifreq)); 265 print_ifaddrs(a); 266 printf("---\n"); 267 getifaddrs(&b); 268 print_ifaddrs(b); 269 return 0; 270 } 271 #endif 272