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