xref: /netbsd-src/external/bsd/ipf/dist/lib/getifname.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1*13885a66Sdarrenr /*	$NetBSD: getifname.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $	*/
2bc4097aaSchristos 
3bc4097aaSchristos /*
4c9d5dc6cSdarrenr  * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos  *
6bc4097aaSchristos  * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos  *
8*13885a66Sdarrenr  * Id: getifname.c,v 1.1.1.2 2012/07/22 13:44:38 darrenr Exp $
9bc4097aaSchristos  */
10bc4097aaSchristos 
11bc4097aaSchristos #include "ipf.h"
12bc4097aaSchristos 
13bc4097aaSchristos #include "kmem.h"
14bc4097aaSchristos 
15bc4097aaSchristos /*
16bc4097aaSchristos  * Given a pointer to an interface in the kernel, return a pointer to a
17bc4097aaSchristos  * string which is the interface name.
18bc4097aaSchristos  */
19bc4097aaSchristos #if 0
20bc4097aaSchristos char *getifname(ptr)
21bc4097aaSchristos 	struct ifnet *ptr;
22bc4097aaSchristos {
23bc4097aaSchristos #if SOLARIS || defined(__hpux)
24bc4097aaSchristos # if SOLARIS
25bc4097aaSchristos #  include <sys/mutex.h>
26bc4097aaSchristos #  include <sys/condvar.h>
27bc4097aaSchristos # endif
28bc4097aaSchristos # ifdef __hpux
29bc4097aaSchristos #  include "compat.h"
30bc4097aaSchristos # endif
31bc4097aaSchristos # include "../pfil/qif.h"
32bc4097aaSchristos 	char *ifname;
33bc4097aaSchristos 	qif_t qif;
34bc4097aaSchristos 
35bc4097aaSchristos 	if ((void *)ptr == (void *)-1)
36bc4097aaSchristos 		return "!";
37bc4097aaSchristos 	if (ptr == NULL)
38bc4097aaSchristos 		return "-";
39bc4097aaSchristos 
40bc4097aaSchristos 	if (kmemcpy((char *)&qif, (u_long)ptr, sizeof(qif)) == -1)
41bc4097aaSchristos 		return "X";
42bc4097aaSchristos 	ifname = strdup(qif.qf_name);
43bc4097aaSchristos 	if ((ifname != NULL) && (*ifname == '\0')) {
44bc4097aaSchristos 		free(ifname);
45bc4097aaSchristos 		return "!";
46bc4097aaSchristos 	}
47bc4097aaSchristos 	return ifname;
48bc4097aaSchristos #else
49bc4097aaSchristos # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
50bc4097aaSchristos     defined(__OpenBSD__) || \
51bc4097aaSchristos     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
52bc4097aaSchristos #else
53bc4097aaSchristos 	char buf[LIFNAMSIZ];
54bc4097aaSchristos 	int len;
55bc4097aaSchristos # endif
56bc4097aaSchristos 	struct ifnet netif;
57bc4097aaSchristos 
58bc4097aaSchristos 	if ((void *)ptr == (void *)-1)
59bc4097aaSchristos 		return "!";
60bc4097aaSchristos 	if (ptr == NULL)
61bc4097aaSchristos 		return "-";
62bc4097aaSchristos 
63bc4097aaSchristos 	if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1)
64bc4097aaSchristos 		return "X";
65bc4097aaSchristos # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
66bc4097aaSchristos     defined(__OpenBSD__) || defined(linux) || \
67bc4097aaSchristos     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
68bc4097aaSchristos 	return strdup(netif.if_xname);
69bc4097aaSchristos # else
70bc4097aaSchristos 	if (kstrncpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1)
71bc4097aaSchristos 		return "X";
72bc4097aaSchristos 	if (netif.if_unit < 10)
73bc4097aaSchristos 		len = 2;
74bc4097aaSchristos 	else if (netif.if_unit < 1000)
75bc4097aaSchristos 		len = 3;
76bc4097aaSchristos 	else if (netif.if_unit < 10000)
77bc4097aaSchristos 		len = 4;
78bc4097aaSchristos 	else
79bc4097aaSchristos 		len = 5;
80bc4097aaSchristos 	buf[sizeof(buf) - len] = '\0';
81bc4097aaSchristos 	sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000);
82bc4097aaSchristos 	return strdup(buf);
83bc4097aaSchristos # endif
84bc4097aaSchristos #endif
85bc4097aaSchristos }
86bc4097aaSchristos #else
getifname(ptr)87bc4097aaSchristos char *getifname(ptr)
88bc4097aaSchristos 	struct ifnet *ptr;
89bc4097aaSchristos {
90c9d5dc6cSdarrenr 	ptr = ptr;
91bc4097aaSchristos 	return "X";
92bc4097aaSchristos }
93bc4097aaSchristos #endif
94