getifname.c revision 145519
1/* $FreeBSD: head/contrib/ipfilter/lib/getifname.c 145519 2005-04-25 18:20:15Z darrenr $ */ 2 3#include "ipf.h" 4 5#include "kmem.h" 6 7/* 8 * Given a pointer to an interface in the kernel, return a pointer to a 9 * string which is the interface name. 10 */ 11char *getifname(ptr) 12struct ifnet *ptr; 13{ 14#if SOLARIS || defined(__hpux) 15# if SOLARIS 16# include <sys/mutex.h> 17# include <sys/condvar.h> 18# endif 19# ifdef __hpux 20# include "compat.h" 21# endif 22# include "../pfil/qif.h" 23 char *ifname; 24 qif_t qif; 25 26 if ((void *)ptr == (void *)-1) 27 return "!"; 28 if (ptr == NULL) 29 return "-"; 30 31 if (kmemcpy((char *)&qif, (u_long)ptr, sizeof(qif)) == -1) 32 return "X"; 33 ifname = strdup(qif.qf_name); 34 if ((ifname != NULL) && (*ifname == '\0')) { 35 free(ifname); 36 return "!"; 37 } 38 return ifname; 39#else 40# if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \ 41 defined(__OpenBSD__) || \ 42 (defined(__FreeBSD__) && (__FreeBSD_version >= 501113)) 43#else 44 char buf[32]; 45 int len; 46# endif 47 struct ifnet netif; 48 49 if ((void *)ptr == (void *)-1) 50 return "!"; 51 if (ptr == NULL) 52 return "-"; 53 54 if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1) 55 return "X"; 56# if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \ 57 defined(__OpenBSD__) || defined(linux) || \ 58 (defined(__FreeBSD__) && (__FreeBSD_version >= 501113)) 59 return strdup(netif.if_xname); 60# else 61 if (kstrncpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1) 62 return "X"; 63 if (netif.if_unit < 10) 64 len = 2; 65 else if (netif.if_unit < 1000) 66 len = 3; 67 else if (netif.if_unit < 10000) 68 len = 4; 69 else 70 len = 5; 71 buf[sizeof(buf) - len] = '\0'; 72 sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000); 73 return strdup(buf); 74# endif 75#endif 76} 77