xref: /titanic_50/usr/src/cmd/ipf/lib/common/getifname.c (revision f56257d84449cae424f3943cec01573a5027af36)
1ab25eeb5Syz155240 /*
2ab25eeb5Syz155240  * Copyright (C) 2003 by Darren Reed.
3ab25eeb5Syz155240  *
4ab25eeb5Syz155240  * See the IPFILTER.LICENCE file for details on licencing.
5ab25eeb5Syz155240  *
6f4b3ec61Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
7ab25eeb5Syz155240  * Use is subject to license terms.
8ab25eeb5Syz155240  */
9ab25eeb5Syz155240 
107c478bd9Sstevel@tonic-gate #include "ipf.h"
117c478bd9Sstevel@tonic-gate #include "kmem.h"
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate /*
147c478bd9Sstevel@tonic-gate  * Given a pointer to an interface in the kernel, return a pointer to a
157c478bd9Sstevel@tonic-gate  * string which is the interface name.
16381a2a9aSdr146992  *
17381a2a9aSdr146992  * The same code is used to run in two different environments: in ipfstat
18381a2a9aSdr146992  * and in ipftest.  In ipftest, kmemcpy is wrapper for bcopy but in ipfstat,
19381a2a9aSdr146992  * it is used as an interface to libkvm.
207c478bd9Sstevel@tonic-gate  */
getifname(ptr)217c478bd9Sstevel@tonic-gate char *getifname(ptr)
227c478bd9Sstevel@tonic-gate struct ifnet *ptr;
237c478bd9Sstevel@tonic-gate {
24*f56257d8SToomas Soome # ifdef SOLARIS
25ab25eeb5Syz155240 #  include <sys/mutex.h>
26ab25eeb5Syz155240 #  include <sys/condvar.h>
27ab25eeb5Syz155240 # endif
28ab25eeb5Syz155240 # ifdef __hpux
29ab25eeb5Syz155240 #  include "compat.h"
30ab25eeb5Syz155240 # endif
317c478bd9Sstevel@tonic-gate # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
32ab25eeb5Syz155240     defined(__OpenBSD__) || \
33ab25eeb5Syz155240     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
347c478bd9Sstevel@tonic-gate #else
357c478bd9Sstevel@tonic-gate 	char buf[32];
367c478bd9Sstevel@tonic-gate 	int len;
377c478bd9Sstevel@tonic-gate # endif
387c478bd9Sstevel@tonic-gate 	struct ifnet netif;
39f4b3ec61Sdh155122 #define SOLARIS_PFHOOKS	1
40381a2a9aSdr146992 # ifdef SOLARIS_PFHOOKS
41381a2a9aSdr146992 	if ((opts & OPT_DONOTHING) == 0)
42381a2a9aSdr146992 		return "@";
43381a2a9aSdr146992 # endif
44381a2a9aSdr146992 
457c478bd9Sstevel@tonic-gate 	if ((void *)ptr == (void *)-1)
467c478bd9Sstevel@tonic-gate 		return "!";
477c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
487c478bd9Sstevel@tonic-gate 		return "-";
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1)
517c478bd9Sstevel@tonic-gate 		return "X";
527c478bd9Sstevel@tonic-gate # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
53ab25eeb5Syz155240     defined(__OpenBSD__) || defined(linux) || \
54ab25eeb5Syz155240     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
557c478bd9Sstevel@tonic-gate 	return strdup(netif.if_xname);
567c478bd9Sstevel@tonic-gate # else
577c478bd9Sstevel@tonic-gate 	if (kstrncpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1)
587c478bd9Sstevel@tonic-gate 		return "X";
597c478bd9Sstevel@tonic-gate 	if (netif.if_unit < 10)
607c478bd9Sstevel@tonic-gate 		len = 2;
617c478bd9Sstevel@tonic-gate 	else if (netif.if_unit < 1000)
627c478bd9Sstevel@tonic-gate 		len = 3;
637c478bd9Sstevel@tonic-gate 	else if (netif.if_unit < 10000)
647c478bd9Sstevel@tonic-gate 		len = 4;
657c478bd9Sstevel@tonic-gate 	else
667c478bd9Sstevel@tonic-gate 		len = 5;
677c478bd9Sstevel@tonic-gate 	buf[sizeof(buf) - len] = '\0';
687c478bd9Sstevel@tonic-gate 	sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000);
697c478bd9Sstevel@tonic-gate 	return strdup(buf);
707c478bd9Sstevel@tonic-gate # endif
717c478bd9Sstevel@tonic-gate }
72