- More s/vertex/edge/g
[tinc] / src / freebsd / device.c
1 /*
2     device.c -- Interaction with FreeBSD tap device
3     Copyright (C) 2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2001 Guus Sliepen <guus@sliepen.warande.net>
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.1 2001/10/12 15:22:59 guus Exp $
21 */
22
23 #define DEFAULT_DEVICE "/dev/tap0"
24
25 int device_fd = -1;
26 int device_type;
27 char *device_fname;
28 char *device_info;
29
30 int device_total_in = 0;
31 int device_total_out = 0;
32
33 *
34   open the local ethertap device
35 */
36 int setup_device(void)
37 {
38   struct ifreq ifr;
39
40 cp
41   if(!get_config_string(lookup_config(config_tree, "Device"), &device_fname)))
42     device_fname = DEFAULT_DEVICE;
43
44 cp
45   if((device_fd = open(device_fname, O_RDWR | O_NONBLOCK)) < 0)
46     {
47       syslog(LOG_ERR, _("Could not open %s: %m"), device_fname);
48       return -1;
49     }
50 cp
51   device_fd = device_fd;
52
53   /* Set default MAC address for ethertap devices */
54
55   mymac.type = SUBNET_MAC;
56   mymac.net.mac.address.x[0] = 0xfe;
57   mymac.net.mac.address.x[1] = 0xfd;
58   mymac.net.mac.address.x[2] = 0x00;
59   mymac.net.mac.address.x[3] = 0x00;
60   mymac.net.mac.address.x[4] = 0x00;
61   mymac.net.mac.address.x[5] = 0x00;
62
63   device_info = _("FreeBSD tap device");
64
65   syslog(LOG_INFO, _("%s is a %s"), device_fname, device_info);
66 cp
67   return 0;
68 }
69
70 /*
71   read, encrypt and send data that is
72   available through the ethertap device
73 */
74 int read_packet(vpn_packet_t *packet)
75 {
76   int lenin;
77 cp
78   if((lenin = read(device_fd, packet->data, MTU)) <= 0)
79     {
80       syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
81       return -1;
82     }
83
84   packet->len = lenin;
85
86   device_total_in += packet->len;
87
88   if(debug_lvl >= DEBUG_TRAFFIC)
89     syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"),
90            packet->len, device_info);
91
92   return 0;
93 cp
94 }
95
96 int write_packet(vpn_packet_t *packet)
97 {
98 cp
99   if(debug_lvl >= DEBUG_TRAFFIC)
100     syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
101            packet->len, device_info);
102
103   if(write(device_fd, packet->data, packet->len) < 0)
104     {
105       syslog(LOG_ERR, _("Error while writing to %s %s: %m"), device_info, device_fname);
106       return -1;
107     }
108
109   device_total_out += packet->len;
110 cp
111 }