Define logger(), cleans up source code and allows us to write log entries
[oweals/tinc.git] / src / freebsd / device.c
1 /*
2     device.c -- Interaction with FreeBSD tap device
3     Copyright (C) 2001-2002 Ivo Timmermans <ivo@o2w.nl>,
4                   2001-2002 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/06 22:11:34 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <fcntl.h>
32 #include <net/if.h>
33 #include <unistd.h>
34 #include <string.h>
35
36 #include <utils.h>
37 #include "conf.h"
38 #include "net.h"
39 #include "logger.h"
40
41 #include "system.h"
42
43 #define DEFAULT_DEVICE "/dev/tap0"
44
45 int device_fd = -1;
46 int device_type;
47 char *device;
48 char *interface;
49 char *device_info;
50 int device_total_in = 0;
51 int device_total_out = 0;
52
53 /*
54   open the local ethertap device
55 */
56 int setup_device(void)
57 {
58         cp();
59
60         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
61                 device = DEFAULT_DEVICE;
62
63         if(!get_config_string(lookup_config(config_tree, "Interface"), &interface))
64                 interface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
65
66         if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
67                 logger(DEBUG_ALWAYS, LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
68                 return -1;
69         }
70
71         device_info = _("FreeBSD tap device");
72
73         logger(DEBUG_ALWAYS, LOG_INFO, _("%s is a %s"), device, device_info);
74
75         return 0;
76 }
77
78 void close_device(void)
79 {
80         cp();
81
82         close(device_fd);
83 }
84
85 /*
86   read, encrypt and send data that is
87   available through the ethertap device
88 */
89 int read_packet(vpn_packet_t *packet)
90 {
91         int lenin;
92
93         cp();
94
95         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
96                 logger(DEBUG_ALWAYS, LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
97                            device, strerror(errno));
98                 return -1;
99         }
100
101         packet->len = lenin;
102
103         device_total_in += packet->len;
104
105         logger(DEBUG_TRAFFIC, LOG_DEBUG, _("Read packet of %d bytes from %s"),
106                            packet->len, device_info);
107
108         return 0;
109 }
110
111 int write_packet(vpn_packet_t *packet)
112 {
113         cp();
114
115         logger(DEBUG_TRAFFIC, LOG_DEBUG, _("Writing packet of %d bytes to %s"),
116                            packet->len, device_info);
117
118         if(write(device_fd, packet->data, packet->len) < 0) {
119                 logger(DEBUG_ALWAYS, LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
120                            device, strerror(errno));
121                 return -1;
122         }
123
124         device_total_out += packet->len;
125 }
126
127 void dump_device_stats(void)
128 {
129         cp();
130
131         logger(DEBUG_ALWAYS, LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
132         logger(DEBUG_ALWAYS, LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
133         logger(DEBUG_ALWAYS, LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
134 }