1100138Sume/* $KAME: if_indextoname.c,v 1.7 2000/11/08 03:09:30 itojun Exp $ */ 2100138Sume 3100138Sume/*- 4100138Sume * Copyright (c) 1997, 2000 5100138Sume * Berkeley Software Design, Inc. All rights reserved. 6100138Sume * 7100138Sume * Redistribution and use in source and binary forms, with or without 8100138Sume * modification, are permitted provided that the following conditions 9100138Sume * are met: 10100138Sume * 1. Redistributions of source code must retain the above copyright 11100138Sume * notice, this list of conditions and the following disclaimer. 12100138Sume * 13100138Sume * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND 14100138Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15100138Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16100138Sume * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE 17100138Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18100138Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19100138Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20100138Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21100138Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22100138Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23100138Sume * SUCH DAMAGE. 24100138Sume * 25100138Sume * BSDI Id: if_indextoname.c,v 2.3 2000/04/17 22:38:05 dab Exp 26100138Sume */ 27100138Sume 28100138Sume#include <sys/cdefs.h> 29100138Sume__FBSDID("$FreeBSD$"); 30100138Sume 31100138Sume#include <sys/types.h> 32100138Sume#include <sys/socket.h> 33100138Sume#include <net/if_dl.h> 34100138Sume#include <net/if.h> 35100138Sume#include <ifaddrs.h> 36100138Sume#include <stdlib.h> 37100138Sume#include <string.h> 38100138Sume#include <errno.h> 39100138Sume 40100138Sume/* 41100138Sume * From RFC 2533: 42100138Sume * 43100138Sume * The second function maps an interface index into its corresponding 44100138Sume * name. 45100138Sume * 46100138Sume * #include <net/if.h> 47100138Sume * 48100138Sume * char *if_indextoname(unsigned int ifindex, char *ifname); 49100138Sume * 50100138Sume * The ifname argument must point to a buffer of at least IF_NAMESIZE 51100138Sume * bytes into which the interface name corresponding to the specified 52100138Sume * index is returned. (IF_NAMESIZE is also defined in <net/if.h> and 53100138Sume * its value includes a terminating null byte at the end of the 54100138Sume * interface name.) This pointer is also the return value of the 55100138Sume * function. If there is no interface corresponding to the specified 56100138Sume * index, NULL is returned, and errno is set to ENXIO, if there was a 57100138Sume * system error (such as running out of memory), if_indextoname returns 58100138Sume * NULL and errno would be set to the proper value (e.g., ENOMEM). 59100138Sume */ 60100138Sume 61100138Sumechar * 62100138Sumeif_indextoname(unsigned int ifindex, char *ifname) 63100138Sume{ 64100138Sume struct ifaddrs *ifaddrs, *ifa; 65100138Sume int error = 0; 66100138Sume 67100138Sume if (getifaddrs(&ifaddrs) < 0) 68100138Sume return(NULL); /* getifaddrs properly set errno */ 69100138Sume 70100138Sume for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { 71100138Sume if (ifa->ifa_addr && 72100138Sume ifa->ifa_addr->sa_family == AF_LINK && 73235640Smarcel ifindex == LLINDEX((struct sockaddr_dl*)ifa->ifa_addr)) 74100138Sume break; 75100138Sume } 76100138Sume 77100138Sume if (ifa == NULL) { 78100138Sume error = ENXIO; 79100138Sume ifname = NULL; 80100138Sume } 81100138Sume else 82100138Sume strncpy(ifname, ifa->ifa_name, IFNAMSIZ); 83100138Sume 84100138Sume freeifaddrs(ifaddrs); 85100138Sume 86100138Sume errno = error; 87100138Sume return(ifname); 88100138Sume} 89