File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / bpf.c
Revision 1.1.2.2: download - view: text, annotated - select for diffs - revision graph
Mon Jun 24 11:09:55 2013 UTC (11 years ago) by misho
Branches: io5_4
open/close bfp

    1: #include "global.h"
    2: 
    3: 
    4: /*
    5:  * io_get1stiface() - Get first interface of host
    6:  *
    7:  * @szIface = interface string buffer
    8:  * @iflen = size of interface buffer
    9:  * return: -1 error or 0 ok
   10:  */
   11: int
   12: io_get1stiface(char *szIface, int iflen)
   13: {
   14: 	struct ifaddrs *ifa;
   15: 
   16: 	if (!szIface || !iflen)
   17: 		return -1;
   18: 
   19: 	getifaddrs(&ifa);
   20: 	strlcpy(szIface, ifa->ifa_name, iflen);
   21: 	freeifaddrs(ifa);
   22: 	return 0;
   23: }
   24: 
   25: /*
   26:  * io_etherClose() - Close BPF interface
   27:  *
   28:  * @eth = bpf handle
   29:  * return: none
   30:  */
   31: void
   32: io_etherClose(int eth)
   33: {
   34: 	if (eth > STDERR_FILENO)
   35: 		close(eth);
   36: }
   37: 
   38: /*
   39:  * io_etherOpen() - Open BPF interface to device
   40:  *
   41:  * @csIface = interface name
   42:  * @flags = open flags
   43:  * return: -1 error or >-1 bpf handle
   44:  */
   45: int
   46: io_etherOpen(const char *csIface, int flags)
   47: {
   48: 	int n = 1, eth = -1;
   49: 	register int i;
   50: 	char szStr[STRSIZ];
   51: 	struct ifreq ifr;
   52: 
   53: 	for (i = 0; i < BPF_DEV_MAX; i++) {
   54: 		memset(szStr, 0, sizeof szStr);
   55: 		snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
   56: 		eth = open(szStr, flags);
   57: 		if (eth > STDERR_FILENO)
   58: 			break;
   59: 	}
   60: 	if (eth < 3) {
   61: 		LOGERR;
   62: 		return -1;
   63: 	}
   64: 
   65: 	if (ioctl(eth, BIOCIMMEDIATE, &n) == -1) {
   66: 		LOGERR;
   67: 		close(eth);
   68: 		return -1;
   69: 	}
   70: 	n = USHRT_MAX + 1;
   71: 	if (ioctl(eth, BIOCSBLEN, &n) == -1) {
   72: 		LOGERR;
   73: 		close(eth);
   74: 		return -1;
   75: 	}
   76: 	if (csIface)
   77: 		strlcpy(szStr, csIface, sizeof szStr);
   78: 	else if (io_get1stiface(szStr, sizeof szStr) == -1) {
   79: 		close(eth);
   80: 		return -1;
   81: 	}
   82: 	strlcpy(ifr.ifr_name, szStr, sizeof ifr.ifr_name);
   83: 	if (ioctl(eth, BIOCSETIF, &ifr) == -1) {
   84: 		LOGERR;
   85: 		close(eth);
   86: 		return -1;
   87: 	}
   88: 
   89: 	return eth;
   90: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>