1 | /* |
1 | /* |
2 | device.c -- Interaction with Linux ethertap and tun/tap device |
2 | device.c -- Interaction with Linux ethertap and tun/tap device |
3 | Copyright (C) 2001-2003 Ivo Timmermans <ivo@o2w.nl>, |
3 | Copyright (C) 2001-2004 Ivo Timmermans <ivo@tinc-vpn.org>, |
4 | 2001-2003 Guus Sliepen <guus@sliepen.eu.org> |
4 | 2001-2004 Guus Sliepen <guus@tinc-vpn.org> |
5 | |
5 | |
6 | This program is free software; you can redistribute it and/or modify |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
7 | it under the terms of the GNU General Public License as published by |
8 | the Free Software Foundation; either version 2 of the License, or |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. |
9 | (at your option) any later version. |
… | |
… | |
15 | |
15 | |
16 | You should have received a copy of the GNU General Public License |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | |
19 | |
20 | $Id: device.c,v 1.1 2003/10/14 03:22:09 pcg Exp $ |
20 | $Id: device.c,v 1.2 2005/03/17 23:59:38 pcg Exp $ |
21 | */ |
21 | */ |
22 | |
|
|
23 | |
22 | |
24 | #ifdef HAVE_TUNTAP |
23 | #ifdef HAVE_TUNTAP |
25 | #ifdef LINUX_IF_TUN_H |
24 | #ifdef LINUX_IF_TUN_H |
26 | #include LINUX_IF_TUN_H |
25 | #include LINUX_IF_TUN_H |
27 | #else |
26 | #else |
… | |
… | |
30 | #define DEFAULT_DEVICE "/dev/net/tun" |
29 | #define DEFAULT_DEVICE "/dev/net/tun" |
31 | #else |
30 | #else |
32 | #define DEFAULT_DEVICE "/dev/tap0" |
31 | #define DEFAULT_DEVICE "/dev/tap0" |
33 | #endif |
32 | #endif |
34 | |
33 | |
35 | |
|
|
36 | typedef enum device_type_t { |
34 | typedef enum device_type_t { |
37 | DEVICE_TYPE_ETHERTAP, |
35 | DEVICE_TYPE_ETHERTAP, |
38 | DEVICE_TYPE_TUN, |
36 | DEVICE_TYPE_TUN, |
39 | DEVICE_TYPE_TAP, |
37 | DEVICE_TYPE_TAP, |
40 | } device_type_t; |
38 | } device_type_t; |
41 | |
39 | |
42 | int device_fd = -1; |
40 | int device_fd = -1; |
43 | device_type_t device_type; |
41 | static device_type_t device_type; |
44 | char *device; |
42 | char *device; |
45 | char *iface; |
43 | char *iface; |
46 | char ifrname[IFNAMSIZ]; |
44 | char ifrname[IFNAMSIZ]; |
47 | char *device_info; |
45 | char *device_info; |
48 | |
46 | |
49 | int device_total_in = 0; |
47 | static int device_total_in = 0; |
50 | int device_total_out = 0; |
48 | static int device_total_out = 0; |
51 | |
49 | |
52 | bool setup_device(void) |
50 | bool setup_device(void) |
53 | { |
51 | { |
54 | struct ifreq ifr; |
52 | struct ifreq ifr; |
55 | |
53 | |
… | |
… | |
86 | } |
84 | } |
87 | |
85 | |
88 | if(iface) |
86 | if(iface) |
89 | strncpy(ifr.ifr_name, iface, IFNAMSIZ); |
87 | strncpy(ifr.ifr_name, iface, IFNAMSIZ); |
90 | |
88 | |
91 | if(!ioctl(device_fd, TUNSETIFF, (void *) &ifr)) { |
89 | if(!ioctl(device_fd, TUNSETIFF, &ifr)) { |
92 | strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); |
90 | strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); |
93 | iface = ifrname; |
91 | iface = ifrname; |
94 | } else if(!ioctl(device_fd, (('T' << 8) | 202), (void *) &ifr)) { |
92 | } else if(!ioctl(device_fd, (('T' << 8) | 202), &ifr)) { |
95 | logger(LOG_WARNING, _("Old ioctl() request was needed for %s"), device); |
93 | logger(LOG_WARNING, _("Old ioctl() request was needed for %s"), device); |
96 | strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); |
94 | strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); |
97 | iface = ifrname; |
95 | iface = ifrname; |
98 | } else |
96 | } else |
99 | #endif |
97 | #endif |