Update all device.c files.
[tinc] / src / darwin / device.c
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.9 2003/07/18 13:41:36 guus Exp $
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "logger.h"
27 #include "net.h"
28 #include "utils.h"
29
30 #define DEFAULT_DEVICE "/dev/tun0"
31
32 int device_fd = -1;
33 int device_type;
34 char *device;
35 char *iface;
36 char *device_info;
37 int device_total_in = 0;
38 int device_total_out = 0;
39
40 int setup_device(void)
41 {
42         cp();
43
44         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
45                 device = DEFAULT_DEVICE;
46
47         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
48                 iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
49
50         if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
51                 logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
52                 return -1;
53         }
54
55         device_info = _("MacOS/X tun device");
56
57         logger(LOG_INFO, _("%s is a %s"), device, device_info);
58
59         return 0;
60 }
61
62 void close_device(void)
63 {
64         cp();
65
66         close(device_fd);
67 }
68
69 int read_packet(vpn_packet_t *packet)
70 {
71         int lenin;
72
73         cp();
74
75         if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
76                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
77                            device, strerror(errno));
78                 return -1;
79         }
80
81         packet->data[12] = 0x08;
82         packet->data[13] = 0x00;
83
84         packet->len = lenin + 14;
85
86         device_total_in += packet->len;
87
88         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
89                            packet->len, device_info);
90
91         return 0;
92 }
93
94 int write_packet(vpn_packet_t *packet)
95 {
96         cp();
97
98         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
99                            packet->len, device_info);
100
101         if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
102                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
103                            device, strerror(errno));
104                 return -1;
105         }
106
107         device_total_out += packet->len;
108 }
109
110 void dump_device_stats(void)
111 {
112         cp();
113
114         logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
115         logger(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
116         logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
117 }