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