Mercurial > hg > quicktun
annotate src/common.c @ 47:e896392f7e03
Added support for connecting over IPv6
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Fri, 24 May 2013 17:14:10 +0200 |
parents | 54d28a81ca99 |
children | b4de2326e9f7 |
rev | line source |
---|---|
0 | 1 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. |
2 Redistribution and use in source and binary forms, with or without modification, are | |
3 permitted provided that the following conditions are met: | |
4 | |
5 1. Redistributions of source code must retain the above copyright notice, this list of | |
6 conditions and the following disclaimer. | |
7 | |
8 2. Redistributions in binary form must reproduce the above copyright notice, this list | |
9 of conditions and the following disclaimer in the documentation and/or other materials | |
10 provided with the distribution. | |
11 | |
12 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
13 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
14 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR | |
15 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
16 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
17 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
18 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
19 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
20 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
21 | |
22 The views and conclusions contained in the software and documentation are those of the | |
23 authors and should not be interpreted as representing official policies, either expressed | |
24 or implied, of Ivo Smits.*/ | |
25 | |
26 #include <stdio.h> | |
27 #include <stdlib.h> | |
28 #include <string.h> | |
29 #include <fcntl.h> | |
36
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
30 #include <pwd.h> |
0 | 31 #ifndef HAVE_NETINET_IN_H |
32 #include <netinet/in.h> | |
33 #endif | |
34 #include <sys/ioctl.h> | |
7 | 35 #include <sys/socket.h> |
0 | 36 #include <poll.h> |
37 #include <netdb.h> | |
38 #include <stdlib.h> | |
8
6d86596d8884
Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents:
7
diff
changeset
|
39 #include <net/if.h> |
7 | 40 #ifdef linux |
41 #include <linux/if_tun.h> | |
42 #include <linux/if_ether.h> | |
43 #else | |
44 #define ETH_FRAME_LEN 1514 | |
45 #include <net/if_tun.h> | |
13 | 46 #ifdef SOLARIS |
47 #include <sys/stropts.h> | |
48 #include <sys/sockio.h> | |
49 #endif | |
7 | 50 #endif |
0 | 51 |
52 #define MAX_PACKET_LEN (ETH_FRAME_LEN+4) //Some space for optional packet information | |
53 | |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
54 typedef union { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
55 struct sockaddr any; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
56 struct sockaddr_in ip4; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
57 struct sockaddr_in6 ip6; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
58 } sockaddr_any; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
59 |
0 | 60 struct qtsession; |
61 struct qtproto { | |
62 int encrypted; | |
63 int buffersize_raw; | |
64 int buffersize_enc; | |
65 int offset_raw; | |
66 int offset_enc; | |
67 int (*encode)(struct qtsession* sess, char* raw, char* enc, int len); | |
68 int (*decode)(struct qtsession* sess, char* enc, char* raw, int len); | |
69 int (*init)(struct qtsession* sess); | |
70 int protocol_data_size; | |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
71 void (*idle)(struct qtsession* sess); |
0 | 72 }; |
73 struct qtsession { | |
74 struct qtproto protocol; | |
75 void* protocol_data; | |
76 int fd_socket; | |
77 int fd_dev; | |
78 int remote_float; | |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
79 sockaddr_any remote_addr; |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
80 int use_pi; |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
81 int poll_timeout; |
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
82 void (*sendnetworkpacket)(struct qtsession* sess, char* msg, int len); |
0 | 83 }; |
84 | |
85 #ifdef COMBINED_BINARY | |
86 extern char* (*getconf)(const char*); | |
87 extern int errorexit(const char*); | |
88 extern int errorexitp(const char*); | |
89 extern void print_header(); | |
90 extern void hex2bin(unsigned char*, unsigned char*, int); | |
22
38d495566d1c
Re-added some debugging messages to nacltai protocol code, enabled by the DEBUG environment variable
Ivo Smits <Ivo@UCIS.nl>
parents:
15
diff
changeset
|
91 extern int debug; |
0 | 92 #else |
93 | |
94 char* (*getconf)(const char*) = getenv; | |
22
38d495566d1c
Re-added some debugging messages to nacltai protocol code, enabled by the DEBUG environment variable
Ivo Smits <Ivo@UCIS.nl>
parents:
15
diff
changeset
|
95 int debug = 0; |
38
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
96 static int gargc = 0; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
97 static char** gargv = NULL; |
0 | 98 |
99 int errorexit(const char* text) { | |
100 fprintf(stderr, "%s\n", text); | |
101 return -1; | |
102 } | |
103 int errorexitp(const char* text) { | |
104 perror(text); | |
105 return -1; | |
106 } | |
107 | |
108 void print_header() { | |
38
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
109 fprintf(stderr, "UCIS QuickTun "QT_VERSION" (c) 2010-2013 Ivo Smits <Ivo@UCIS.nl>\n"); |
22
38d495566d1c
Re-added some debugging messages to nacltai protocol code, enabled by the DEBUG environment variable
Ivo Smits <Ivo@UCIS.nl>
parents:
15
diff
changeset
|
110 fprintf(stderr, "More information: http://wiki.ucis.nl/QuickTun\n"); |
0 | 111 } |
112 | |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
113 static int is_all_zero(void* buf, int size) { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
114 int i; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
115 char* bb = (char*)buf; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
116 for (i = 0; i < size; i++) if (bb[i] != 0) return 0; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
117 return 1; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
118 } |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
119 static int sockaddr_is_zero_address(sockaddr_any* sa) { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
120 int af = sa->any.sa_family; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
121 if (af == AF_INET) return is_all_zero(&sa->ip4.sin_addr, sizeof(struct in_addr)); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
122 if (af == AF_INET6) return is_all_zero(&sa->ip6.sin6_addr, sizeof(struct in6_addr)); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
123 return is_all_zero(sa, sizeof(sockaddr_any)); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
124 } |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
125 static int sockaddr_set_port(sockaddr_any* sa, int port) { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
126 port = htons(port); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
127 int af = sa->any.sa_family; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
128 if (af == AF_INET) sa->ip4.sin_port = port; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
129 else if (af == AF_INET6) sa->ip6.sin6_port = port; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
130 else return errorexit("Unknown address family"); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
131 return 0; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
132 } |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
133 static int sockaddr_equal(sockaddr_any* a, sockaddr_any* b) { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
134 if (a->any.sa_family != b->any.sa_family) return 0; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
135 if (a->any.sa_family == AF_INET) return a->ip4.sin_port == b->ip4.sin_port && a->ip4.sin_addr.s_addr == b->ip4.sin_addr.s_addr; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
136 if (a->any.sa_family == AF_INET6) return a->ip6.sin6_port == b->ip6.sin6_port && memcmp(&a->ip6.sin6_addr, &b->ip6.sin6_addr, sizeof(struct in6_addr)) == 0 && a->ip6.sin6_scope_id == b->ip6.sin6_scope_id; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
137 return memcmp(a, b, sizeof(sockaddr_any)) == 0; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
138 } |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
139 static void sockaddr_to_string(sockaddr_any* sa, char* str, int strbuflen) { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
140 if (sa->any.sa_family == AF_INET) { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
141 if (!inet_ntop(AF_INET, &sa->ip4.sin_addr, str, strbuflen)) str[0] = 0; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
142 int i = strlen(str); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
143 snprintf(str + i, strbuflen - i, ":%u", ntohs(sa->ip4.sin_port)); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
144 } else if (sa->any.sa_family == AF_INET6) { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
145 if (!inet_ntop(AF_INET6, &sa->ip6.sin6_addr, str, strbuflen)) str[0] = 0; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
146 int i = strlen(str); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
147 snprintf(str + i, strbuflen - i, "%%%d:%u", sa->ip6.sin6_scope_id, ntohs(sa->ip6.sin6_port)); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
148 } else { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
149 strncpy(str, "Unknown AF", strbuflen); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
150 } |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
151 str[strbuflen - 1] = 0; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
152 } |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
153 |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
154 static int init_udp(struct qtsession* session) { |
0 | 155 char* envval; |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
156 fprintf(stderr, "Initializing UDP socket...\n"); |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
157 struct addrinfo *ai_local = NULL, *ai_remote = NULL; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
158 unsigned short af = 0; |
0 | 159 if (envval = getconf("LOCAL_ADDRESS")) { |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
160 if (getaddrinfo(envval, NULL, NULL, &ai_local)) return errorexitp("getaddrinfo(LOCAL_ADDRESS)"); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
161 if (!ai_local) return errorexit("LOCAL_ADDRESS lookup failed"); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
162 if (ai_local->ai_addrlen > sizeof(sockaddr_any)) return errorexit("Resolved LOCAL_ADDRESS is too big"); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
163 af = ai_local->ai_family; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
164 } |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
165 if (envval = getconf("REMOTE_ADDRESS")) { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
166 if (getaddrinfo(envval, NULL, NULL, &ai_remote)) return errorexitp("getaddrinfo(REMOTE_ADDRESS)"); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
167 if (!ai_remote) return errorexit("REMOTE_ADDRESS lookup failed"); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
168 if (ai_remote->ai_addrlen > sizeof(sockaddr_any)) return errorexit("Resolved REMOTE_ADDRESS is too big"); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
169 if (af && af != ai_remote->ai_family) return errorexit("Address families do not match"); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
170 af = ai_remote->ai_family; |
0 | 171 } |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
172 if (!af) af = AF_INET; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
173 int sfd = socket(af, SOCK_DGRAM, IPPROTO_UDP); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
174 if (sfd < 0) return errorexitp("Could not create UDP socket"); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
175 sockaddr_any udpaddr; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
176 memset(&udpaddr, 0, sizeof(udpaddr)); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
177 udpaddr.any.sa_family = af; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
178 if (ai_local) memcpy(&udpaddr, ai_local->ai_addr, ai_local->ai_addrlen); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
179 int port = 2998; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
180 if (envval = getconf("LOCAL_PORT")) port = atoi(envval); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
181 if (sockaddr_set_port(&udpaddr, port)) return -1; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
182 if (bind(sfd, (struct sockaddr*)&udpaddr, sizeof(udpaddr))) return errorexitp("Could not bind socket"); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
183 memset(&udpaddr, 0, sizeof(udpaddr)); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
184 udpaddr.any.sa_family = af; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
185 if (ai_remote) memcpy(&udpaddr, ai_remote->ai_addr, ai_remote->ai_addrlen); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
186 if (!ai_remote || sockaddr_is_zero_address(&udpaddr)) { |
0 | 187 session->remote_float = 1; |
188 } else { | |
12
e4b60d041491
Make sure that the session buffer is zero
Ivo Smits <Ivo@UCIS.nl>
parents:
9
diff
changeset
|
189 session->remote_float = getconf("REMOTE_FLOAT") ? 1 : 0; |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
190 port = 2998; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
191 if (envval = getconf("REMOTE_PORT")) port = atoi(envval); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
192 if (sockaddr_set_port(&udpaddr, port)) return -1; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
193 session->remote_addr = udpaddr; |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
194 if (session->remote_float) { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
195 session->remote_float = 2; |
2
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
196 } else { |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
197 if (connect(sfd, (struct sockaddr*)&udpaddr, sizeof(udpaddr))) return errorexitp("Could not connect socket"); |
0 | 198 } |
199 } | |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
200 if (ai_local) freeaddrinfo(ai_local); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
201 if (ai_remote) freeaddrinfo(ai_remote); |
0 | 202 session->fd_socket = sfd; |
203 return sfd; | |
204 } | |
205 | |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
206 static int init_tuntap(struct qtsession* session) { |
0 | 207 char* envval; |
7 | 208 fprintf(stderr, "Initializing tun/tap device...\n"); |
0 | 209 int ttfd; //Tap device file descriptor |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
210 int tunmode = 0; |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
211 if (envval = getconf("TUN_MODE")) tunmode = atoi(envval); |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
212 session->use_pi = 0; |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
213 if (tunmode && (envval = getconf("USE_PI"))) session->use_pi = atoi(envval); |
32
51c6d2fc712f
Fixes contributed by Daniel Dickinson <daniel@cshore.neomailbox.net>
Ivo Smits <Ivo@UCIS.nl>
parents:
30
diff
changeset
|
214 #if defined linux |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
215 struct ifreq ifr; //required for tun/tap setup |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
216 memset(&ifr, 0, sizeof(ifr)); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
217 if ((ttfd = open("/dev/net/tun", O_RDWR)) < 0) return errorexitp("Could not open tun/tap device file"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
218 if (envval = getconf("INTERFACE")) strcpy(ifr.ifr_name, envval); |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
219 ifr.ifr_flags = tunmode ? IFF_TUN : IFF_TAP; |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
220 if (!session->use_pi) ifr.ifr_flags |= IFF_NO_PI; |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
221 if (ioctl(ttfd, TUNSETIFF, (void *)&ifr) < 0) return errorexitp("TUNSETIFF ioctl failed"); |
32
51c6d2fc712f
Fixes contributed by Daniel Dickinson <daniel@cshore.neomailbox.net>
Ivo Smits <Ivo@UCIS.nl>
parents:
30
diff
changeset
|
222 #elif defined SOLARIS |
13 | 223 int ip_fd = -1, if_fd = -1, ppa = 0; |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
224 if ((ttfd = open("/dev/tun", O_RDWR)) < 0) return errorexitp("Could not open tun device file"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
225 if ((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) return errorexitp("Could not open /dev/ip"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
226 if ((envval = getconf("INTERFACE"))) { |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
227 while (*envval && !isdigit((int)*envval)) envval++; |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
228 ppa = atoi(envval); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
229 } |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
230 if ((ppa = ioctl(ttfd, TUNNEWPPA, ppa)) < 0) return errorexitp("Could not assign new PPA"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
231 if ((if_fd = open("/dev/tun", O_RDWR, 0)) < 0) return errorexitp("Could not open tun device file again"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
232 if (ioctl(if_fd, I_PUSH, "ip") < 0) return errorexitp("Could not push IP module"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
233 if (ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0) return errorexitp("Could not set PPA"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
234 if (ioctl(ip_fd, I_LINK, if_fd) < 0) return errorexitp("Could not link TUN device to IP"); |
7 | 235 #else |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
236 if (!(envval = getconf("INTERFACE"))) envval = "/dev/tun0"; |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
237 if ((ttfd = open(envval, O_RDWR)) < 0) return errorexitp("Could not open tun device file"); |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
238 if (tunmode) { |
33
422f3582bd38
Possible fix for tun mode on FreeBSD
Ivo Smits <Ivo@UCIS.nl>
parents:
32
diff
changeset
|
239 int i = IFF_POINTOPOINT | IFF_MULTICAST; |
34
b876afa5a72a
Fixed the build script and FreeBSD tun mode
Ivo Smits <Ivo@UCIS.nl>
parents:
33
diff
changeset
|
240 ioctl(ttfd, TUNSIFMODE, &i); |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
241 i = session->use_pi ? 1 : 0; |
34
b876afa5a72a
Fixed the build script and FreeBSD tun mode
Ivo Smits <Ivo@UCIS.nl>
parents:
33
diff
changeset
|
242 ioctl(ttfd, TUNSIFHEAD, &i); |
33
422f3582bd38
Possible fix for tun mode on FreeBSD
Ivo Smits <Ivo@UCIS.nl>
parents:
32
diff
changeset
|
243 } |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
244 #endif |
39
47a34fe75c57
Added option to run a script after opening the tun device
Ivo Smits <Ivo@UCIS.nl>
parents:
38
diff
changeset
|
245 if (envval = getconf("TUN_UP_SCRIPT")) system(envval); |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
246 session->fd_dev = ttfd; |
0 | 247 return ttfd; |
248 } | |
249 | |
250 void hex2bin(unsigned char* dest, unsigned char* src, int count) { | |
251 int i; | |
252 for (i = 0; i < count; i++) { | |
253 if (*src >= '0' && *src <= '9') *dest = *src - '0'; | |
254 else if (*src >= 'a' && * src <='f') *dest = *src - 'a' + 10; | |
255 else if (*src >= 'A' && * src <='F') *dest = *src - 'A' + 10; | |
256 src++; *dest = *dest << 4; | |
257 if (*src >= '0' && *src <= '9') *dest += *src - '0'; | |
258 else if (*src >= 'a' && *src <= 'f') *dest += *src - 'a' + 10; | |
259 else if (*src >= 'A' && *src <= 'F') *dest += *src - 'A' + 10; | |
260 src++; dest++; | |
261 } | |
262 } | |
263 | |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
264 static int drop_privileges() { |
36
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
265 char* envval; |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
266 if (envval = getconf("SETUID")) { |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
267 if (setgroups(0, NULL) == -1) return errorexitp("setgroups"); |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
268 struct passwd *pw = getpwnam(envval); |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
269 if (!pw) return errorexitp("getpwnam"); |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
270 if (setgid(pw->pw_gid) == -1) return errorexitp("setgid"); |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
271 if (setuid(pw->pw_uid) == -1) return errorexitp("setuid"); |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
272 } |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
273 chdir("/"); |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
274 } |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
275 |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
276 static void qtsendnetworkpacket(struct qtsession* session, char* msg, int len) { |
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
277 if (session->remote_float == 0) { |
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
278 len = write(session->fd_socket, msg, len); |
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
279 } else if (session->remote_float == 2) { |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
280 len = sendto(session->fd_socket, msg, len, 0, (struct sockaddr*)&session->remote_addr, sizeof(sockaddr_any)); |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
281 } |
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
282 } |
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
283 |
0 | 284 int qtrun(struct qtproto* p) { |
22
38d495566d1c
Re-added some debugging messages to nacltai protocol code, enabled by the DEBUG environment variable
Ivo Smits <Ivo@UCIS.nl>
parents:
15
diff
changeset
|
285 if (getconf("DEBUG")) debug = 1; |
0 | 286 struct qtsession session; |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
287 session.poll_timeout = -1; |
0 | 288 session.protocol = *p; |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
289 |
29
7c5e5be876bb
Small fix to error handling during initialization
Ivo Smits <Ivo@UCIS.nl>
parents:
28
diff
changeset
|
290 if (init_udp(&session) < 0) return -1; |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
291 int sfd = session.fd_socket; |
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
292 |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
293 session.sendnetworkpacket = qtsendnetworkpacket; |
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
294 |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
295 if (init_tuntap(&session) < 0) return -1; |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
296 int ttfd = session.fd_dev; |
0 | 297 |
298 char protocol_data[p->protocol_data_size]; | |
12
e4b60d041491
Make sure that the session buffer is zero
Ivo Smits <Ivo@UCIS.nl>
parents:
9
diff
changeset
|
299 memset(protocol_data, 0, p->protocol_data_size); |
0 | 300 session.protocol_data = &protocol_data; |
27
5ba185ca7102
Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents:
22
diff
changeset
|
301 if (p->init && p->init(&session) < 0) return -1; |
0 | 302 |
36
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
303 if (drop_privileges() < 0) return -1; |
1fe62a94c28a
Added option SETUID to drop privileges
Ivo Smits <Ivo@UCIS.nl>
parents:
35
diff
changeset
|
304 |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
305 fprintf(stderr, "The tunnel is now operational!\n"); |
0 | 306 |
307 struct pollfd fds[2]; | |
308 fds[0].fd = ttfd; | |
309 fds[0].events = POLLIN; | |
310 fds[1].fd = sfd; | |
311 fds[1].events = POLLIN; | |
312 | |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
313 int pi_length = 0; |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
314 if (session.use_pi == 2) pi_length = 4; |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
315 |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
316 char buffer_raw_a[p->buffersize_raw + pi_length]; |
0 | 317 char buffer_enc_a[p->buffersize_enc]; |
318 char* buffer_raw = buffer_raw_a; | |
319 char* buffer_enc = buffer_enc_a; | |
320 | |
321 while (1) { | |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
322 int len = poll(fds, 2, session.poll_timeout); |
0 | 323 if (len < 0) return errorexitp("poll error"); |
324 else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) return errorexit("poll error on tap device"); | |
325 else if (fds[1].revents & (POLLHUP | POLLNVAL)) return errorexit("poll error on udp socket"); | |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
326 if (len == 0 && p->idle) p->idle(&session); |
0 | 327 if (fds[0].revents & POLLIN) { |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
328 len = read(ttfd, buffer_raw + p->offset_raw, p->buffersize_raw + pi_length); |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
329 if (len < pi_length) errorexit("read packet smaller than header from tun device"); |
0 | 330 if (session.remote_float == 0 || session.remote_float == 2) { |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
331 len = p->encode(&session, buffer_raw + pi_length, buffer_enc, len - pi_length); |
0 | 332 if (len < 0) return len; |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
333 if (len == 0) continue; //encoding is not yet possible |
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
334 qtsendnetworkpacket(&session, buffer_enc + p->offset_enc, len); |
0 | 335 } |
336 } | |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
337 if (fds[1].revents & POLLERR) { |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
338 int out; |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
339 len = sizeof(out); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
340 getsockopt(sfd, SOL_SOCKET, SO_ERROR, &out, &len); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
341 fprintf(stderr, "Received error %d on udp socket\n", out); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
342 } |
0 | 343 if (fds[1].revents & POLLIN) { |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
344 sockaddr_any recvaddr; |
0 | 345 socklen_t recvaddr_len = sizeof(recvaddr); |
346 if (session.remote_float == 0) { | |
347 len = read(sfd, buffer_enc + p->offset_enc, p->buffersize_enc); | |
348 } else { | |
349 len = recvfrom(sfd, buffer_enc + p->offset_enc, p->buffersize_enc, 0, (struct sockaddr*)&recvaddr, &recvaddr_len); | |
350 } | |
351 if (len < 0) { | |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
352 long long out; |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
353 len = sizeof(out); |
0 | 354 getsockopt(sfd, SOL_SOCKET, SO_ERROR, &out, &len); |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
355 fprintf(stderr, "Received end of file on udp socket (error %lld)\n", out); |
0 | 356 } else { |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
357 len = p->decode(&session, buffer_enc, buffer_raw + pi_length, len); |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
358 if (len < 0) continue; |
47
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
359 if (session.remote_float != 0 && !sockaddr_equal(&session.remote_addr, &recvaddr)) { |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
360 char epname[INET6_ADDRSTRLEN + 1 + 2 + 1 + 5]; //addr%scope:port |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
361 sockaddr_to_string(&recvaddr, epname, sizeof(epname)); |
e896392f7e03
Added support for connecting over IPv6
Ivo Smits <Ivo@UCIS.nl>
parents:
41
diff
changeset
|
362 fprintf(stderr, "Remote endpoint has changed to %s\n", epname); |
0 | 363 session.remote_addr = recvaddr; |
364 session.remote_float = 2; | |
365 } | |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
366 if (len > 0 && session.use_pi == 2) { |
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
367 int ipver = (buffer_raw[p->offset_raw + pi_length] >> 4) & 0xf; |
35
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
368 int pihdr = 0; |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
369 #if defined linux |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
370 if (ipver == 4) pihdr = 0x0000 | (0x0008 << 16); //little endian: flags and protocol are swapped |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
371 else if (ipver == 6) pihdr = 0x0000 | (0xdd86 << 16); |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
372 #else |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
373 if (ipver == 4) pihdr = htonl(AF_INET); |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
374 else if (ipver == 6) pihdr = htonl(AF_INET6); |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
375 #endif |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
376 *(int*)(buffer_raw + p->offset_raw) = ipver; |
a1ec0d6b6f13
Added USE_PI=2 setting to automatically add/remove packet information header
Ivo Smits <Ivo@UCIS.nl>
parents:
34
diff
changeset
|
377 } |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
378 if (len > 0) write(ttfd, buffer_raw + p->offset_raw, len + pi_length); |
0 | 379 } |
380 } | |
381 } | |
382 return 0; | |
383 } | |
38
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
384 |
41
54d28a81ca99
Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents:
39
diff
changeset
|
385 static char* getconfcmdargs(const char* name) { |
38
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
386 int i; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
387 for (i = 1; i < gargc - 2; i++) { |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
388 if (strcmp(gargv[i], "-c")) continue; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
389 if (strcmp(gargv[i + 1], name)) continue; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
390 return gargv[i + 2]; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
391 } |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
392 return NULL; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
393 } |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
394 |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
395 int qtprocessargs(int argc, char** argv) { |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
396 int i; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
397 for (i = 1; i < argc; i++) { |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
398 char* a = argv[i]; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
399 if (!strcmp(a, "-h") || !strcmp(a, "--help")) { |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
400 return errorexit("Please read the documentation at http://wiki.ucis.nl/QuickTun"); |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
401 } else if (!strcmp(a, "-v") || !strcmp(a, "--version")) { |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
402 return errorexit("UCIS QuickTun "QT_VERSION); |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
403 } else if (!strcmp(a, "-c")) { |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
404 gargc = argc; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
405 gargv = argv; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
406 getconf = getconfcmdargs; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
407 i += 2; |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
408 } else { |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
409 return errorexit("Unexpected command line argument"); |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
410 } |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
411 } |
d9f5caa13898
Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents:
36
diff
changeset
|
412 } |
0 | 413 #endif |
30
6f0e6b7dc088
Fixed build script to support multiple abis on one machine, bugfix in code, minor improvements
Ivo Smits <Ivo@UCIS.nl>
parents:
29
diff
changeset
|
414 |