xref: /titanic_50/usr/src/cmd/ipf/lib/common/getifname.c (revision 24bb1048b1197ebc9afe761e4098573aeedfe8c9)
1 /*
2  * Copyright (C) 2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
7  * Use is subject to license terms.
8  */
9 
10 #include "ipf.h"
11 #include "kmem.h"
12 
13 /*
14  * Given a pointer to an interface in the kernel, return a pointer to a
15  * string which is the interface name.
16  *
17  * The same code is used to run in two different environments: in ipfstat
18  * and in ipftest.  In ipftest, kmemcpy is wrapper for bcopy but in ipfstat,
19  * it is used as an interface to libkvm.
20  */
21 char *getifname(ptr)
22 struct ifnet *ptr;
23 {
24 # ifdef SOLARIS
25 #  include <sys/mutex.h>
26 #  include <sys/condvar.h>
27 # endif
28 # ifdef __hpux
29 #  include "compat.h"
30 # endif
31 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
32     defined(__OpenBSD__) || \
33     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
34 #else
35 	char buf[32];
36 	int len;
37 # endif
38 	struct ifnet netif;
39 #define SOLARIS_PFHOOKS	1
40 # ifdef SOLARIS_PFHOOKS
41 	if ((opts & OPT_DONOTHING) == 0)
42 		return "@";
43 # endif
44 
45 	if ((void *)ptr == (void *)-1)
46 		return "!";
47 	if (ptr == NULL)
48 		return "-";
49 
50 	if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1)
51 		return "X";
52 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
53     defined(__OpenBSD__) || defined(linux) || \
54     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
55 	return strdup(netif.if_xname);
56 # else
57 	if (kstrncpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1)
58 		return "X";
59 	if (netif.if_unit < 10)
60 		len = 2;
61 	else if (netif.if_unit < 1000)
62 		len = 3;
63 	else if (netif.if_unit < 10000)
64 		len = 4;
65 	else
66 		len = 5;
67 	buf[sizeof(buf) - len] = '\0';
68 	sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000);
69 	return strdup(buf);
70 # endif
71 }
72