xref: /illumos-gate/usr/src/cmd/ipf/lib/common/getifname.c (revision 7c478bd9)
1 #include "ipf.h"
2 #include "qif.h"
3 
4 #include "kmem.h"
5 
6 /*
7  * Given a pointer to an interface in the kernel, return a pointer to a
8  * string which is the interface name.
9  */
10 char *getifname(ptr)
11 struct ifnet *ptr;
12 {
13 #if SOLARIS
14 	char *ifname;
15 	s_ill_t ill;
16 
17 	if ((void *)ptr == (void *)-1)
18 		return "!";
19 	if (ptr == NULL)
20 		return "-";
21 
22 	if (kmemcpy((char *)&ill, (u_long)ptr, sizeof(ill)) == -1)
23 		return "X";
24 	ifname = malloc(sizeof(ill.ill_name) + 1);
25 	strncpy(ifname, ill.ill_name, sizeof(ill.ill_name));
26 	ifname[sizeof(ill.ill_name)] = '\0';
27 	return ifname;
28 #else
29 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
30     defined(__OpenBSD__)
31 #else
32 	char buf[32];
33 	int len;
34 # endif
35 	struct ifnet netif;
36 
37 	if ((void *)ptr == (void *)-1)
38 		return "!";
39 	if (ptr == NULL)
40 		return "-";
41 
42 	if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1)
43 		return "X";
44 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
45     defined(__OpenBSD__)
46 	return strdup(netif.if_xname);
47 # else
48 	if (kstrncpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1)
49 		return "X";
50 	if (netif.if_unit < 10)
51 		len = 2;
52 	else if (netif.if_unit < 1000)
53 		len = 3;
54 	else if (netif.if_unit < 10000)
55 		len = 4;
56 	else
57 		len = 5;
58 	buf[sizeof(buf) - len] = '\0';
59 	sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000);
60 	return strdup(buf);
61 # endif
62 #endif
63 }
64