comparison src/common.c @ 7:fd7c60905b13

BSD support
author ivo <Ivo@UCIS.nl>
date Wed, 13 Oct 2010 21:07:44 +0200
parents cf9b44b46be5
children 6d86596d8884
comparison
equal deleted inserted replaced
6:cf9b44b46be5 7:fd7c60905b13
29 #include <fcntl.h> 29 #include <fcntl.h>
30 #ifndef HAVE_NETINET_IN_H 30 #ifndef HAVE_NETINET_IN_H
31 #include <netinet/in.h> 31 #include <netinet/in.h>
32 #endif 32 #endif
33 #include <sys/ioctl.h> 33 #include <sys/ioctl.h>
34 #include <linux/if.h> 34 #include <net/if.h>
35 #include <linux/if_tun.h> 35 #include <sys/socket.h>
36 #include <linux/if_ether.h>
37 #include <poll.h> 36 #include <poll.h>
38 #include <netdb.h> 37 #include <netdb.h>
39 #include <stdlib.h> 38 #include <stdlib.h>
39 #ifdef linux
40 #include <linux/if_tun.h>
41 #include <linux/if_ether.h>
42 #else
43 #define ETH_FRAME_LEN 1514
44 #include <net/if_tun.h>
45 #endif
40 46
41 #define MAX_PACKET_LEN (ETH_FRAME_LEN+4) //Some space for optional packet information 47 #define MAX_PACKET_LEN (ETH_FRAME_LEN+4) //Some space for optional packet information
42 48
43 struct qtsession; 49 struct qtsession;
44 struct qtproto { 50 struct qtproto {
130 return sfd; 136 return sfd;
131 } 137 }
132 138
133 int init_tuntap() { 139 int init_tuntap() {
134 char* envval; 140 char* envval;
135 fprintf(stderr, "Initializing tap device...\n"); 141 fprintf(stderr, "Initializing tun/tap device...\n");
136 int ttfd; //Tap device file descriptor 142 int ttfd; //Tap device file descriptor
137 struct ifreq ifr; //required for tun/tap setup 143 #ifdef linux
138 memset(&ifr, 0, sizeof(ifr)); 144 struct ifreq ifr; //required for tun/tap setup
139 if ((ttfd = open("/dev/net/tun", O_RDWR)) < 0) return errorexitp("Could not open tap device file"); 145 memset(&ifr, 0, sizeof(ifr));
140 if (envval = getconf("INTERFACE")) strcpy(ifr.ifr_name, envval); 146 if ((ttfd = open("/dev/net/tun", O_RDWR)) < 0) return errorexitp("Could not open tun/tap device file");
141 ifr.ifr_flags = getconf("TUN_MODE") ? IFF_TUN : IFF_TAP; 147 if (envval = getconf("INTERFACE")) strcpy(ifr.ifr_name, envval);
142 ifr.ifr_flags |= getconf("USE_PI") ? 0 : IFF_NO_PI; 148 ifr.ifr_flags = getconf("TUN_MODE") ? IFF_TUN : IFF_TAP;
143 if (ioctl(ttfd, TUNSETIFF, (void *)&ifr) < 0) return errorexitp("TUNSETIFF ioctl failed"); 149 ifr.ifr_flags |= getconf("USE_PI") ? 0 : IFF_NO_PI;
150 if (ioctl(ttfd, TUNSETIFF, (void *)&ifr) < 0) return errorexitp("TUNSETIFF ioctl failed");
151 #else
152 if (!(envval = getconf("INTERFACE"))) envval = "/dev/tun0";
153 if ((ttfd = open(envval, O_RDWR)) < 0) return errorexitp("Could not open tun device file");
154 #endif
144 return ttfd; 155 return ttfd;
145 } 156 }
146 157
147 void hex2bin(unsigned char* dest, unsigned char* src, int count) { 158 void hex2bin(unsigned char* dest, unsigned char* src, int count) {
148 int i; 159 int i;