ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/gvpe/src/tincd/raw_socket/device.c
Revision: 1.2
Committed: Thu Mar 17 23:59:38 2005 UTC (21 years, 7 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_9, rel-1_8, rel-2_01, rel-3_0, rel-2_2, rel-2_0, rel-2_21, rel-2_22, rel-2_25, HEAD
Changes since 1.1: +13 -27 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2     device.c -- raw socket
3 pcg 1.2 Copyright (C) 2002-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
4     2002-2004 Guus Sliepen <guus@tinc-vpn.org>
5 pcg 1.1
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 pcg 1.2 $Id: device.c 1405 2004-11-08 22:11:33Z guus $
21 pcg 1.1 */
22    
23     #include <netpacket/packet.h>
24 pcg 1.2 #include <netinet/ether.h>
25 pcg 1.1
26     int device_fd = -1;
27     char *device;
28 pcg 1.2 char *iface;
29 pcg 1.1 char ifrname[IFNAMSIZ];
30     char *device_info;
31    
32 pcg 1.2 static int device_total_in = 0;
33     static int device_total_out = 0;
34 pcg 1.1
35     bool setup_device(void)
36     {
37     struct ifreq ifr;
38     struct sockaddr_ll sa;
39    
40     cp();
41    
42     if(!get_config_string
43 pcg 1.2 (lookup_config(config_tree, "Interface"), &iface))
44     iface = "eth0";
45 pcg 1.1
46     if(!get_config_string(lookup_config(config_tree, "Device"), &device))
47 pcg 1.2 device = iface;
48 pcg 1.1
49     device_info = _("raw socket");
50    
51     if((device_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
52     logger(LOG_ERR, _("Could not open %s: %s"), device_info,
53     strerror(errno));
54     return false;
55     }
56    
57     memset(&ifr, 0, sizeof(ifr));
58 pcg 1.2 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
59 pcg 1.1 if(ioctl(device_fd, SIOCGIFINDEX, &ifr)) {
60     close(device_fd);
61 pcg 1.2 logger(LOG_ERR, _("Can't find interface %s: %s"), iface,
62 pcg 1.1 strerror(errno));
63     return false;
64     }
65    
66     memset(&sa, '0', sizeof(sa));
67     sa.sll_family = AF_PACKET;
68     sa.sll_protocol = htons(ETH_P_ALL);
69     sa.sll_ifindex = ifr.ifr_ifindex;
70    
71     if(bind(device_fd, (struct sockaddr *) &sa, (socklen_t) sizeof(sa))) {
72 pcg 1.2 logger(LOG_ERR, _("Could not bind %s to %s: %s"), device, iface, strerror(errno));
73 pcg 1.1 return false;
74     }
75    
76     logger(LOG_INFO, _("%s is a %s"), device, device_info);
77    
78     return true;
79     }
80    
81     void close_device(void)
82     {
83     cp();
84    
85     close(device_fd);
86     }
87    
88     bool read_packet(vpn_packet_t *packet)
89     {
90     int lenin;
91    
92     cp();
93    
94     if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
95     logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
96     device, strerror(errno));
97     return false;
98     }
99    
100     packet->len = lenin;
101    
102     device_total_in += packet->len;
103    
104     ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
105     device_info);
106    
107     return true;
108     }
109    
110     bool write_packet(vpn_packet_t *packet)
111     {
112     cp();
113    
114     ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
115     packet->len, device_info);
116    
117     if(write(device_fd, packet->data, packet->len) < 0) {
118     logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
119     strerror(errno));
120     return false;
121     }
122    
123     device_total_out += packet->len;
124    
125     return true;
126     }
127    
128     void dump_device_stats(void)
129     {
130     cp();
131    
132     logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
133     logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
134     logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
135     }