b85923b41253c2edae80e47073e5b79ee1bf23ac
[tinc] / src / freebsd / device.c
1 /*
2     device.c -- Interaction with FreeBSD tap 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.12 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/tap0"
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 = _("FreeBSD tap 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, MTU)) <= 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->len = lenin;
82
83         device_total_in += packet->len;
84
85         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
86                            packet->len, device_info);
87
88         return 0;
89 }
90
91 int write_packet(vpn_packet_t *packet)
92 {
93         cp();
94
95         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
96                            packet->len, device_info);
97
98         if(write(device_fd, packet->data, packet->len) < 0) {
99                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
100                            device, strerror(errno));
101                 return -1;
102         }
103
104         device_total_out += packet->len;
105 }
106
107 void dump_device_stats(void)
108 {
109         cp();
110
111         logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
112         logger(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
113         logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
114 }