xref: /titanic_52/usr/src/cmd/ipf/lib/common/getifname.c (revision 8fd04b8338ed5093ec2d1e668fa620b7de44c177)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
11 
12 #include "ipf.h"
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  * The same code is used to run in two different environments: in ipfstat
20  * and in ipftest.  In ipftest, kmemcpy is wrapper for bcopy but in ipfstat,
21  * it is used as an interface to libkvm.
22  */
23 char *getifname(ptr)
24 struct ifnet *ptr;
25 {
26 # if SOLARIS
27 #  include <sys/mutex.h>
28 #  include <sys/condvar.h>
29 # endif
30 # ifdef __hpux
31 #  include "compat.h"
32 # endif
33 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
34     defined(__OpenBSD__) || \
35     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
36 #else
37 	char buf[32];
38 	int len;
39 # endif
40 	struct ifnet netif;
41 #define SOLARIS_PFHOOKS	1
42 # ifdef SOLARIS_PFHOOKS
43 	if ((opts & OPT_DONOTHING) == 0)
44 		return "@";
45 # endif
46 
47 	if ((void *)ptr == (void *)-1)
48 		return "!";
49 	if (ptr == NULL)
50 		return "-";
51 
52 	if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1)
53 		return "X";
54 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
55     defined(__OpenBSD__) || defined(linux) || \
56     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
57 	return strdup(netif.if_xname);
58 # else
59 	if (kstrncpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1)
60 		return "X";
61 	if (netif.if_unit < 10)
62 		len = 2;
63 	else if (netif.if_unit < 1000)
64 		len = 3;
65 	else if (netif.if_unit < 10000)
66 		len = 4;
67 	else
68 		len = 5;
69 	buf[sizeof(buf) - len] = '\0';
70 	sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000);
71 	return strdup(buf);
72 # endif
73 }
74