1 |
/* |
2 |
device.c -- Interaction with MacOS/X tun device |
3 |
Copyright (C) 2001-2003 Ivo Timmermans <ivo@o2w.nl>, |
4 |
2001-2003 Guus Sliepen <guus@sliepen.eu.org> |
5 |
|
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 |
8 |
the Free Software Foundation; either version 2 of the License, or |
9 |
(at your option) any later version. |
10 |
|
11 |
This program is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
15 |
|
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 |
18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
20 |
$Id: device.c,v 1.1.2.10 2003/07/22 20:55:21 guus Exp $ |
21 |
*/ |
22 |
|
23 |
|
24 |
|
25 |
#define DEFAULT_DEVICE "/dev/tun0" |
26 |
|
27 |
int device_fd = -1; |
28 |
char *device; |
29 |
char *iface; |
30 |
char *device_info; |
31 |
int device_total_in = 0; |
32 |
int device_total_out = 0; |
33 |
|
34 |
bool setup_device(void) |
35 |
{ |
36 |
cp(); |
37 |
|
38 |
if(!get_config_string(lookup_config(config_tree, "Device"), &device)) |
39 |
device = DEFAULT_DEVICE; |
40 |
|
41 |
if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) |
42 |
iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; |
43 |
|
44 |
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { |
45 |
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); |
46 |
return false; |
47 |
} |
48 |
|
49 |
device_info = _("MacOS/X tun device"); |
50 |
|
51 |
logger(LOG_INFO, _("%s is a %s"), device, device_info); |
52 |
|
53 |
return true; |
54 |
} |
55 |
|
56 |
void close_device(void) |
57 |
{ |
58 |
cp(); |
59 |
|
60 |
close(device_fd); |
61 |
} |
62 |
|
63 |
bool read_packet(vpn_packet_t *packet) |
64 |
{ |
65 |
int lenin; |
66 |
|
67 |
cp(); |
68 |
|
69 |
if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) { |
70 |
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, |
71 |
device, strerror(errno)); |
72 |
return false; |
73 |
} |
74 |
|
75 |
packet->data[12] = 0x08; |
76 |
packet->data[13] = 0x00; |
77 |
|
78 |
packet->len = lenin + 14; |
79 |
|
80 |
device_total_in += packet->len; |
81 |
|
82 |
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), |
83 |
packet->len, device_info); |
84 |
|
85 |
return true; |
86 |
} |
87 |
|
88 |
bool write_packet(vpn_packet_t *packet) |
89 |
{ |
90 |
cp(); |
91 |
|
92 |
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"), |
93 |
packet->len, device_info); |
94 |
|
95 |
if(write(device_fd, packet->data + 14, packet->len - 14) < 0) { |
96 |
logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info, |
97 |
device, strerror(errno)); |
98 |
return false; |
99 |
} |
100 |
|
101 |
device_total_out += packet->len; |
102 |
|
103 |
return true; |
104 |
} |
105 |
|
106 |
void dump_device_stats(void) |
107 |
{ |
108 |
cp(); |
109 |
|
110 |
logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device); |
111 |
logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in); |
112 |
logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out); |
113 |
} |