two minor bugfixes.
[vde.git] / vde / tuntap.c
blobd8fecf390159fd82850a9daa1460b3f1eb2f6805
1 /* Copyright 2002 Yon Uriarte and Jeff Dike
2 * Licensed under the GPL
3 * This file is part of the original uml_switch code
4 * Modified 2003 Renzo Davoli
5 */
7 #include <config.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <syslog.h>
14 #include <sys/ioctl.h>
15 #include <net/if.h>
16 #include <linux/if_tun.h>
18 #include <port.h>
19 #include <switch.h>
21 void send_tap(int fd, void *packet, int len, void *unused)
23 int n;
25 n = write(fd, packet, len);
26 if(n != len){
27 if(errno != EAGAIN) printlog(LOG_WARNING,"send_tap %s",strerror(errno));
31 int open_tap(char *dev)
33 struct ifreq ifr;
34 int fd;
36 if((fd = open("/dev/net/tun", O_RDWR)) < 0){
37 printlog(LOG_ERR,"Failed to open /dev/net/tun %s",strerror(errno));
38 return(-1);
40 memset(&ifr, 0, sizeof(ifr));
41 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
42 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name) - 1);
43 /*printf("dev=\"%s\", ifr.ifr_name=\"%s\"\n", ifr.ifr_name, dev);*/
44 if(ioctl(fd, TUNSETIFF, (void *) &ifr) < 0){
45 printlog(LOG_ERR,"TUNSETIFF failed %s",strerror(errno));
46 close(fd);
47 return(-1);
49 return(fd);