ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/tincd/freebsd/device.c
Revision: 1.1
Committed: Tue Oct 14 03:22:09 2003 UTC (20 years, 9 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_9, rel-1_8, rel-2_01, poll-based-iom, rel-3_0, VPE_1_2, rel-2_2, rel-2_0, VPE_1_4, VPE_1_6, rel-1_7, VPE-1_6_1, rel-2_21, rel-2_22, rel-2_25, HEAD
Log Message:
*** empty log message ***

File Contents

# Content
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.13 2003/07/22 20:55:21 guus Exp $
21 */
22
23
24
25 #define DEFAULT_DEVICE "/dev/tap0"
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 = _("FreeBSD tap 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, MTU)) <= 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->len = lenin;
76
77 device_total_in += packet->len;
78
79 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
80 packet->len, device_info);
81
82 return true;
83 }
84
85 bool write_packet(vpn_packet_t *packet)
86 {
87 cp();
88
89 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
90 packet->len, device_info);
91
92 if(write(device_fd, packet->data, packet->len) < 0) {
93 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
94 device, strerror(errno));
95 return false;
96 }
97
98 device_total_out += packet->len;
99
100 return true;
101 }
102
103 void dump_device_stats(void)
104 {
105 cp();
106
107 logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
108 logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
109 logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
110 }