xref: /titanic_51/usr/src/cmd/ipf/lib/common/getifname.c (revision c40f76e346ad844b9326c2049644b7b1d1a93e48)
1 /*
2  * Copyright (C) 2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright 2006 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 "qif.h"
14 #include "kmem.h"
15 
16 /*
17  * Given a pointer to an interface in the kernel, return a pointer to a
18  * string which is the interface name.
19  */
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 	char *ifname;
32 	qif_t qif;
33 
34 	if ((void *)ptr == (void *)-1)
35 		return "!";
36 	if (ptr == NULL)
37 		return "-";
38 
39 	if (kmemcpy((char *)&qif, (u_long)ptr, sizeof(qif)) == -1)
40 		return "X";
41 	ifname = strdup(qif.qf_name);
42 	if ((ifname != NULL) && (*ifname == '\0')) {
43 		free(ifname);
44 		return "!";
45 	}
46 	return ifname;
47 #else
48 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
49     defined(__OpenBSD__) || \
50     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
51 #else
52 	char buf[32];
53 	int len;
54 # endif
55 	struct ifnet netif;
56 
57 	if ((void *)ptr == (void *)-1)
58 		return "!";
59 	if (ptr == NULL)
60 		return "-";
61 
62 	if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1)
63 		return "X";
64 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
65     defined(__OpenBSD__) || defined(linux) || \
66     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
67 	return strdup(netif.if_xname);
68 # else
69 	if (kstrncpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1)
70 		return "X";
71 	if (netif.if_unit < 10)
72 		len = 2;
73 	else if (netif.if_unit < 1000)
74 		len = 3;
75 	else if (netif.if_unit < 10000)
76 		len = 4;
77 	else
78 		len = 5;
79 	buf[sizeof(buf) - len] = '\0';
80 	sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000);
81 	return strdup(buf);
82 # endif
83 #endif
84 }
85