ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/device-tincd.C
Revision: 1.2
Committed: Tue Oct 14 03:59:32 2003 UTC (20 years, 7 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +12 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2     device-tincd.C -- include one of the tincd low level implementations.
3    
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8    
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13    
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17     */
18    
19     #include <cstdio>
20     #include <cstring>
21     #include <cerrno>
22    
23     #include <sys/types.h>
24     #include <sys/stat.h>
25     #include <sys/ioctl.h>
26     #include <unistd.h>
27     #include <syslog.h>
28     #include <fcntl.h>
29    
30     #include "conf.h"
31    
32 pcg 1.2 /* make the tincd sources feel comfortable in our environment. */
33     /* this was reasonably easy to do. */
34 pcg 1.1 #define routing_mode 1
35     #define RMODE_ROUTER 0
36    
37 pcg 1.2 #define LOG_TO_L(level) \
38 pcg 1.1 (level) == LOG_ERR ? L_ERR \
39     : (level) == LOG_DEBUG ? L_DEBUG \
40     : (level) == LOG_WARNING ? L_WARN \
41     : (level) == LOG_INFO ? L_INFO \
42 pcg 1.2 : L_NOTICE
43    
44     #if __STDC_VERSION__ > 199900
45     # define logger(level, ...) slog (LOG_TO_L(level), __VA_ARGS__)
46     #elif __GNUC__
47     # define logger(level, args...) slog (LOG_TO_L(level), ## args)
48     #else
49     # error either need ISO-C 99 compliant compiler or gcc.
50     #endif
51 pcg 1.1
52     #define ifdebug(subsys) if (0)
53    
54     #define cp()
55     #define lookup_config(config_tree,key) (key)
56    
57     #define MTU MAXSIZE
58    
59     // BIGGEST hack of 'em all
60     // will be casted to data_packet, due to structural similarity
61     struct vpn_packet_t : net_packet {
62     u8 data[MAXSIZE];
63     };
64    
65     static tap_device *self;
66    
67     static bool overwrite_mac;
68    
69     static bool
70     get_config_string(const char *key, char **res)
71     {
72     if (!strcmp (key, "Interface"))
73     *res = conf.ifname;
74     else if (!strcmp (key, "Device"))
75     *res = 0;
76     else
77     {
78     slog (L_ERR, _("tincd layer asking for unknown config '%s'"), key);
79     *res = 0;
80     }
81    
82     return *res;
83     }
84    
85     #define netname conf.ifname
86    
87     #if IF_linux
88     # include "tincd/linux/device.c"
89     #elif IF_freebsd
90     # include "tincd/freebsd/device.c"
91     #elif IF_netbsd
92     # include "tincd/netbsd/device.c"
93     #elif IF_solaris
94     # include "tincd/solaris/device.c"
95     #elif IF_cygwin
96     # include "tincd/cygwin/device.c"
97     #elif IF_mingw
98     # include "tincd/mingw/device.c"
99     #elif IF_darwin
100     # include "tincd/darwin/device.c"
101     #elif IF_raw_socket
102     # include "tincd/raw_socket/device.c"
103     #else
104     # error No interface implementation for your IFTYPE/IFSUBTYPE combination.
105     #endif
106    
107     const char *
108     tap_device::info ()
109     {
110     return _("tincd compatibility layer");
111     }
112    
113     tap_device::tap_device ()
114     {
115     self = this;
116    
117     if (setup_device ())
118     {
119     //slog (L_DEBUG, _("%s is a %s"), device, info ());
120     fd = device_fd;
121     strcpy (ifrname, iface);
122     }
123     else
124     {
125     slog (L_ERR, _("error while configuring tincd device (%s/%s)"), device, info ());
126     exit (1);
127     }
128     }
129    
130     tap_device::~tap_device ()
131     {
132     close_device ();
133     }
134    
135     tap_packet *
136     tap_device::recv ()
137     {
138     tap_packet *pkt = new tap_packet;
139    
140     if (!read_packet (reinterpret_cast<vpn_packet_t *>(pkt)))
141     {
142     delete pkt;
143     slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE,
144     strerror (errno));
145     return 0;
146     }
147    
148     return pkt;
149     }
150    
151     void
152     tap_device::send (tap_packet *pkt)
153     {
154     if (!write_packet (reinterpret_cast<vpn_packet_t *>(pkt)))
155     slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE,
156     strerror (errno));
157     }
158    
159