9
0
Fork 0
barebox/arch/linux/lib/tap.c

42 lines
830 B
C
Raw Normal View History

2007-07-05 16:01:52 +00:00
#include <stdio.h>
2007-07-05 16:01:42 +00:00
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <string.h>
int tap_alloc(char *dev)
{
struct ifreq ifr;
int fd, err;
2007-07-05 16:01:52 +00:00
if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
perror("could not open /dev/net/tun");
2007-07-05 16:01:42 +00:00
return -1;
2007-07-05 16:01:52 +00:00
}
2007-07-05 16:01:42 +00:00
memset(&ifr, 0, sizeof(ifr));
/* Flags: IFF_TUN - TUN device (no Ethernet headers)
* IFF_TAP - TAP device
*
* IFF_NO_PI - Do not provide packet information
*/
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (*dev)
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
2007-07-05 16:01:52 +00:00
perror("could not get tap device");
2007-07-05 16:01:42 +00:00
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}