diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-10-05 15:09:28 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-10-05 15:09:28 +0200 |
commit | c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch) | |
tree | cac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/vpn/gnunet-helper-vpn.c | |
parent | fbb71d527c7d6babf269a8fefce1db291b9f7068 (diff) | |
download | gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip |
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/vpn/gnunet-helper-vpn.c')
-rw-r--r-- | src/vpn/gnunet-helper-vpn.c | 893 |
1 files changed, 447 insertions, 446 deletions
diff --git a/src/vpn/gnunet-helper-vpn.c b/src/vpn/gnunet-helper-vpn.c index 4b4073418..38c5c1795 100644 --- a/src/vpn/gnunet-helper-vpn.c +++ b/src/vpn/gnunet-helper-vpn.c | |||
@@ -61,7 +61,8 @@ | |||
61 | /** | 61 | /** |
62 | * This is in linux/include/net/ipv6.h, but not always exported... | 62 | * This is in linux/include/net/ipv6.h, but not always exported... |
63 | */ | 63 | */ |
64 | struct in6_ifreq { | 64 | struct in6_ifreq |
65 | { | ||
65 | struct in6_addr ifr6_addr; | 66 | struct in6_addr ifr6_addr; |
66 | uint32_t ifr6_prefixlen; | 67 | uint32_t ifr6_prefixlen; |
67 | unsigned int ifr6_ifindex; | 68 | unsigned int ifr6_ifindex; |
@@ -77,55 +78,55 @@ struct in6_ifreq { | |||
77 | * @return the fd to the tun or -1 on error | 78 | * @return the fd to the tun or -1 on error |
78 | */ | 79 | */ |
79 | static int | 80 | static int |
80 | init_tun(char *dev) | 81 | init_tun (char *dev) |
81 | { | 82 | { |
82 | struct ifreq ifr; | 83 | struct ifreq ifr; |
83 | int fd; | 84 | int fd; |
84 | 85 | ||
85 | if (NULL == dev) | 86 | if (NULL == dev) |
86 | { | 87 | { |
87 | errno = EINVAL; | 88 | errno = EINVAL; |
88 | return -1; | 89 | return -1; |
89 | } | 90 | } |
90 | 91 | ||
91 | if (-1 == (fd = open("/dev/net/tun", O_RDWR))) | 92 | if (-1 == (fd = open ("/dev/net/tun", O_RDWR))) |
92 | { | 93 | { |
93 | fprintf(stderr, | 94 | fprintf (stderr, |
94 | "Error opening `%s': %s\n", | 95 | "Error opening `%s': %s\n", |
95 | "/dev/net/tun", | 96 | "/dev/net/tun", |
96 | strerror(errno)); | 97 | strerror (errno)); |
97 | return -1; | 98 | return -1; |
98 | } | 99 | } |
99 | 100 | ||
100 | if (fd >= FD_SETSIZE) | 101 | if (fd >= FD_SETSIZE) |
101 | { | 102 | { |
102 | fprintf(stderr, | 103 | fprintf (stderr, |
103 | "File descriptor to large: %d", | 104 | "File descriptor to large: %d", |
104 | fd); | 105 | fd); |
105 | (void)close(fd); | 106 | (void) close (fd); |
106 | return -1; | 107 | return -1; |
107 | } | 108 | } |
108 | 109 | ||
109 | memset(&ifr, 0, sizeof(ifr)); | 110 | memset (&ifr, 0, sizeof(ifr)); |
110 | ifr.ifr_flags = IFF_TUN; | 111 | ifr.ifr_flags = IFF_TUN; |
111 | 112 | ||
112 | if ('\0' != *dev) | 113 | if ('\0' != *dev) |
113 | strncpy(ifr.ifr_name, | 114 | strncpy (ifr.ifr_name, |
114 | dev, | 115 | dev, |
115 | IFNAMSIZ); | 116 | IFNAMSIZ); |
116 | 117 | ||
117 | if (-1 == ioctl(fd, | 118 | if (-1 == ioctl (fd, |
118 | TUNSETIFF, | 119 | TUNSETIFF, |
119 | (void *)&ifr)) | 120 | (void *) &ifr)) |
120 | { | 121 | { |
121 | fprintf(stderr, | 122 | fprintf (stderr, |
122 | "Error with ioctl on `%s': %s\n", | 123 | "Error with ioctl on `%s': %s\n", |
123 | "/dev/net/tun", | 124 | "/dev/net/tun", |
124 | strerror(errno)); | 125 | strerror (errno)); |
125 | (void)close(fd); | 126 | (void) close (fd); |
126 | return -1; | 127 | return -1; |
127 | } | 128 | } |
128 | strcpy(dev, ifr.ifr_name); | 129 | strcpy (dev, ifr.ifr_name); |
129 | return fd; | 130 | return fd; |
130 | } | 131 | } |
131 | 132 | ||
@@ -138,9 +139,9 @@ init_tun(char *dev) | |||
138 | * @param prefix_len the length of the network-prefix | 139 | * @param prefix_len the length of the network-prefix |
139 | */ | 140 | */ |
140 | static void | 141 | static void |
141 | set_address6(const char *dev, | 142 | set_address6 (const char *dev, |
142 | const char *address, | 143 | const char *address, |
143 | unsigned long prefix_len) | 144 | unsigned long prefix_len) |
144 | { | 145 | { |
145 | struct ifreq ifr; | 146 | struct ifreq ifr; |
146 | struct in6_ifreq ifr6; | 147 | struct in6_ifreq ifr6; |
@@ -150,44 +151,44 @@ set_address6(const char *dev, | |||
150 | /* | 151 | /* |
151 | * parse the new address | 152 | * parse the new address |
152 | */ | 153 | */ |
153 | memset(&sa6, 0, sizeof(struct sockaddr_in6)); | 154 | memset (&sa6, 0, sizeof(struct sockaddr_in6)); |
154 | sa6.sin6_family = AF_INET6; | 155 | sa6.sin6_family = AF_INET6; |
155 | if (1 != inet_pton(AF_INET6, address, sa6.sin6_addr.s6_addr)) | 156 | if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr)) |
156 | { | 157 | { |
157 | fprintf(stderr, | 158 | fprintf (stderr, |
158 | "Failed to parse IPv6 address `%s'\n", | 159 | "Failed to parse IPv6 address `%s'\n", |
159 | address); | 160 | address); |
160 | exit(1); | 161 | exit (1); |
161 | } | 162 | } |
162 | 163 | ||
163 | if (-1 == (fd = socket(PF_INET6, SOCK_DGRAM, 0))) | 164 | if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0))) |
164 | { | 165 | { |
165 | fprintf(stderr, | 166 | fprintf (stderr, |
166 | "Error creating socket: %s\n", | 167 | "Error creating socket: %s\n", |
167 | strerror(errno)); | 168 | strerror (errno)); |
168 | exit(1); | 169 | exit (1); |
169 | } | 170 | } |
170 | 171 | ||
171 | memset(&ifr, 0, sizeof(struct ifreq)); | 172 | memset (&ifr, 0, sizeof(struct ifreq)); |
172 | /* | 173 | /* |
173 | * Get the index of the if | 174 | * Get the index of the if |
174 | */ | 175 | */ |
175 | strncpy(ifr.ifr_name, | 176 | strncpy (ifr.ifr_name, |
176 | dev, | 177 | dev, |
177 | IFNAMSIZ); | 178 | IFNAMSIZ); |
178 | if (-1 == ioctl(fd, | 179 | if (-1 == ioctl (fd, |
179 | SIOGIFINDEX, | 180 | SIOGIFINDEX, |
180 | &ifr)) | 181 | &ifr)) |
181 | { | 182 | { |
182 | fprintf(stderr, | 183 | fprintf (stderr, |
183 | "ioctl failed at %d: %s\n", | 184 | "ioctl failed at %d: %s\n", |
184 | __LINE__, | 185 | __LINE__, |
185 | strerror(errno)); | 186 | strerror (errno)); |
186 | (void)close(fd); | 187 | (void) close (fd); |
187 | exit(1); | 188 | exit (1); |
188 | } | 189 | } |
189 | 190 | ||
190 | memset(&ifr6, 0, sizeof(struct in6_ifreq)); | 191 | memset (&ifr6, 0, sizeof(struct in6_ifreq)); |
191 | ifr6.ifr6_addr = sa6.sin6_addr; | 192 | ifr6.ifr6_addr = sa6.sin6_addr; |
192 | ifr6.ifr6_ifindex = ifr.ifr_ifindex; | 193 | ifr6.ifr6_ifindex = ifr.ifr_ifindex; |
193 | ifr6.ifr6_prefixlen = prefix_len; | 194 | ifr6.ifr6_prefixlen = prefix_len; |
@@ -195,56 +196,56 @@ set_address6(const char *dev, | |||
195 | /* | 196 | /* |
196 | * Set the address | 197 | * Set the address |
197 | */ | 198 | */ |
198 | if (-1 == ioctl(fd, | 199 | if (-1 == ioctl (fd, |
199 | SIOCSIFADDR, | 200 | SIOCSIFADDR, |
200 | &ifr6)) | 201 | &ifr6)) |
201 | { | 202 | { |
202 | fprintf(stderr, | 203 | fprintf (stderr, |
203 | "ioctl failed at line %d: %s\n", | 204 | "ioctl failed at line %d: %s\n", |
204 | __LINE__, | 205 | __LINE__, |
205 | strerror(errno)); | 206 | strerror (errno)); |
206 | (void)close(fd); | 207 | (void) close (fd); |
207 | exit(1); | 208 | exit (1); |
208 | } | 209 | } |
209 | 210 | ||
210 | /* | 211 | /* |
211 | * Get the flags | 212 | * Get the flags |
212 | */ | 213 | */ |
213 | if (-1 == ioctl(fd, | 214 | if (-1 == ioctl (fd, |
214 | SIOCGIFFLAGS, | 215 | SIOCGIFFLAGS, |
215 | &ifr)) | 216 | &ifr)) |
216 | { | 217 | { |
217 | fprintf(stderr, | 218 | fprintf (stderr, |
218 | "ioctl failed at line %d: %s\n", | 219 | "ioctl failed at line %d: %s\n", |
219 | __LINE__, | 220 | __LINE__, |
220 | strerror(errno)); | 221 | strerror (errno)); |
221 | (void)close(fd); | 222 | (void) close (fd); |
222 | exit(1); | 223 | exit (1); |
223 | } | 224 | } |
224 | 225 | ||
225 | /* | 226 | /* |
226 | * Add the UP and RUNNING flags | 227 | * Add the UP and RUNNING flags |
227 | */ | 228 | */ |
228 | ifr.ifr_flags |= IFF_UP | IFF_RUNNING; | 229 | ifr.ifr_flags |= IFF_UP | IFF_RUNNING; |
229 | if (-1 == ioctl(fd, | 230 | if (-1 == ioctl (fd, |
230 | SIOCSIFFLAGS, | 231 | SIOCSIFFLAGS, |
231 | &ifr)) | 232 | &ifr)) |
232 | { | 233 | { |
233 | fprintf(stderr, | 234 | fprintf (stderr, |
234 | "ioctl failed at line %d: %s\n", | 235 | "ioctl failed at line %d: %s\n", |
235 | __LINE__, | 236 | __LINE__, |
236 | strerror(errno)); | 237 | strerror (errno)); |
237 | (void)close(fd); | 238 | (void) close (fd); |
238 | exit(1); | 239 | exit (1); |
239 | } | 240 | } |
240 | 241 | ||
241 | if (0 != close(fd)) | 242 | if (0 != close (fd)) |
242 | { | 243 | { |
243 | fprintf(stderr, | 244 | fprintf (stderr, |
244 | "close failed: %s\n", | 245 | "close failed: %s\n", |
245 | strerror(errno)); | 246 | strerror (errno)); |
246 | exit(1); | 247 | exit (1); |
247 | } | 248 | } |
248 | } | 249 | } |
249 | 250 | ||
250 | 251 | ||
@@ -256,117 +257,117 @@ set_address6(const char *dev, | |||
256 | * @param mask the netmask | 257 | * @param mask the netmask |
257 | */ | 258 | */ |
258 | static void | 259 | static void |
259 | set_address4(const char *dev, | 260 | set_address4 (const char *dev, |
260 | const char *address, | 261 | const char *address, |
261 | const char *mask) | 262 | const char *mask) |
262 | { | 263 | { |
263 | int fd; | 264 | int fd; |
264 | struct sockaddr_in *addr; | 265 | struct sockaddr_in *addr; |
265 | struct ifreq ifr; | 266 | struct ifreq ifr; |
266 | 267 | ||
267 | memset(&ifr, 0, sizeof(struct ifreq)); | 268 | memset (&ifr, 0, sizeof(struct ifreq)); |
268 | addr = (struct sockaddr_in *)&(ifr.ifr_addr); | 269 | addr = (struct sockaddr_in *) &(ifr.ifr_addr); |
269 | addr->sin_family = AF_INET; | 270 | addr->sin_family = AF_INET; |
270 | 271 | ||
271 | /* | 272 | /* |
272 | * Parse the address | 273 | * Parse the address |
273 | */ | 274 | */ |
274 | if (1 != inet_pton(AF_INET, | 275 | if (1 != inet_pton (AF_INET, |
275 | address, | 276 | address, |
276 | &addr->sin_addr.s_addr)) | 277 | &addr->sin_addr.s_addr)) |
277 | { | 278 | { |
278 | fprintf(stderr, | 279 | fprintf (stderr, |
279 | "Failed to parse IPv4 address `%s'\n", | 280 | "Failed to parse IPv4 address `%s'\n", |
280 | address); | 281 | address); |
281 | exit(1); | 282 | exit (1); |
282 | } | 283 | } |
283 | 284 | ||
284 | if (-1 == (fd = socket(PF_INET, SOCK_DGRAM, 0))) | 285 | if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0))) |
285 | { | 286 | { |
286 | fprintf(stderr, | 287 | fprintf (stderr, |
287 | "Error creating socket: %s\n", | 288 | "Error creating socket: %s\n", |
288 | strerror(errno)); | 289 | strerror (errno)); |
289 | exit(1); | 290 | exit (1); |
290 | } | 291 | } |
291 | 292 | ||
292 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); | 293 | strncpy (ifr.ifr_name, dev, IFNAMSIZ); |
293 | 294 | ||
294 | /* | 295 | /* |
295 | * Set the address | 296 | * Set the address |
296 | */ | 297 | */ |
297 | if (-1 == ioctl(fd, SIOCSIFADDR, &ifr)) | 298 | if (-1 == ioctl (fd, SIOCSIFADDR, &ifr)) |
298 | { | 299 | { |
299 | fprintf(stderr, | 300 | fprintf (stderr, |
300 | "ioctl failed at %d: %s\n", | 301 | "ioctl failed at %d: %s\n", |
301 | __LINE__, | 302 | __LINE__, |
302 | strerror(errno)); | 303 | strerror (errno)); |
303 | (void)close(fd); | 304 | (void) close (fd); |
304 | exit(1); | 305 | exit (1); |
305 | } | 306 | } |
306 | 307 | ||
307 | /* | 308 | /* |
308 | * Parse the netmask | 309 | * Parse the netmask |
309 | */ | 310 | */ |
310 | addr = (struct sockaddr_in *)&(ifr.ifr_netmask); | 311 | addr = (struct sockaddr_in *) &(ifr.ifr_netmask); |
311 | if (1 != inet_pton(AF_INET, | 312 | if (1 != inet_pton (AF_INET, |
312 | mask, | 313 | mask, |
313 | &addr->sin_addr.s_addr)) | 314 | &addr->sin_addr.s_addr)) |
314 | { | 315 | { |
315 | fprintf(stderr, | 316 | fprintf (stderr, |
316 | "Failed to parse IPv4 address mask `%s'\n", | 317 | "Failed to parse IPv4 address mask `%s'\n", |
317 | mask); | 318 | mask); |
318 | (void)close(fd); | 319 | (void) close (fd); |
319 | exit(1); | 320 | exit (1); |
320 | } | 321 | } |
321 | 322 | ||
322 | /* | 323 | /* |
323 | * Set the netmask | 324 | * Set the netmask |
324 | */ | 325 | */ |
325 | if (-1 == ioctl(fd, SIOCSIFNETMASK, &ifr)) | 326 | if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr)) |
326 | { | 327 | { |
327 | fprintf(stderr, | 328 | fprintf (stderr, |
328 | "ioctl failed at line %d: %s\n", | 329 | "ioctl failed at line %d: %s\n", |
329 | __LINE__, | 330 | __LINE__, |
330 | strerror(errno)); | 331 | strerror (errno)); |
331 | (void)close(fd); | 332 | (void) close (fd); |
332 | exit(1); | 333 | exit (1); |
333 | } | 334 | } |
334 | 335 | ||
335 | /* | 336 | /* |
336 | * Get the flags | 337 | * Get the flags |
337 | */ | 338 | */ |
338 | if (-1 == ioctl(fd, SIOCGIFFLAGS, &ifr)) | 339 | if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr)) |
339 | { | 340 | { |
340 | fprintf(stderr, | 341 | fprintf (stderr, |
341 | "ioctl failed at line %d: %s\n", | 342 | "ioctl failed at line %d: %s\n", |
342 | __LINE__, | 343 | __LINE__, |
343 | strerror(errno)); | 344 | strerror (errno)); |
344 | (void)close(fd); | 345 | (void) close (fd); |
345 | exit(1); | 346 | exit (1); |
346 | } | 347 | } |
347 | 348 | ||
348 | /* | 349 | /* |
349 | * Add the UP and RUNNING flags | 350 | * Add the UP and RUNNING flags |
350 | */ | 351 | */ |
351 | ifr.ifr_flags |= IFF_UP | IFF_RUNNING; | 352 | ifr.ifr_flags |= IFF_UP | IFF_RUNNING; |
352 | if (-1 == ioctl(fd, SIOCSIFFLAGS, &ifr)) | 353 | if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr)) |
353 | { | 354 | { |
354 | fprintf(stderr, | 355 | fprintf (stderr, |
355 | "ioctl failed at line %d: %s\n", | 356 | "ioctl failed at line %d: %s\n", |
356 | __LINE__, | 357 | __LINE__, |
357 | strerror(errno)); | 358 | strerror (errno)); |
358 | (void)close(fd); | 359 | (void) close (fd); |
359 | exit(1); | 360 | exit (1); |
360 | } | 361 | } |
361 | 362 | ||
362 | if (0 != close(fd)) | 363 | if (0 != close (fd)) |
363 | { | 364 | { |
364 | fprintf(stderr, | 365 | fprintf (stderr, |
365 | "close failed: %s\n", | 366 | "close failed: %s\n", |
366 | strerror(errno)); | 367 | strerror (errno)); |
367 | (void)close(fd); | 368 | (void) close (fd); |
368 | exit(1); | 369 | exit (1); |
369 | } | 370 | } |
370 | } | 371 | } |
371 | 372 | ||
372 | 373 | ||
@@ -376,7 +377,7 @@ set_address4(const char *dev, | |||
376 | * @param fd_tun tunnel FD | 377 | * @param fd_tun tunnel FD |
377 | */ | 378 | */ |
378 | static void | 379 | static void |
379 | run(int fd_tun) | 380 | run (int fd_tun) |
380 | { | 381 | { |
381 | /* | 382 | /* |
382 | * The buffer filled by reading from fd_tun | 383 | * The buffer filled by reading from fd_tun |
@@ -403,198 +404,198 @@ run(int fd_tun) | |||
403 | int write_open = 1; | 404 | int write_open = 1; |
404 | 405 | ||
405 | while ((1 == read_open) && (1 == write_open)) | 406 | while ((1 == read_open) && (1 == write_open)) |
407 | { | ||
408 | FD_ZERO (&fds_w); | ||
409 | FD_ZERO (&fds_r); | ||
410 | |||
411 | /* | ||
412 | * We are supposed to read and the buffer is empty | ||
413 | * -> select on read from tun | ||
414 | */ | ||
415 | if (read_open && (0 == buftun_size)) | ||
416 | FD_SET (fd_tun, &fds_r); | ||
417 | |||
418 | /* | ||
419 | * We are supposed to read and the buffer is not empty | ||
420 | * -> select on write to stdout | ||
421 | */ | ||
422 | if (read_open && (0 != buftun_size)) | ||
423 | FD_SET (1, &fds_w); | ||
424 | |||
425 | /* | ||
426 | * We are supposed to write and the buffer is empty | ||
427 | * -> select on read from stdin | ||
428 | */ | ||
429 | if (write_open && (NULL == bufin_read)) | ||
430 | FD_SET (0, &fds_r); | ||
431 | |||
432 | /* | ||
433 | * We are supposed to write and the buffer is not empty | ||
434 | * -> select on write to tun | ||
435 | */ | ||
436 | if (write_open && (NULL != bufin_read)) | ||
437 | FD_SET (fd_tun, &fds_w); | ||
438 | |||
439 | int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL); | ||
440 | |||
441 | if (-1 == r) | ||
442 | { | ||
443 | if (EINTR == errno) | ||
444 | continue; | ||
445 | fprintf (stderr, | ||
446 | "select failed: %s\n", | ||
447 | strerror (errno)); | ||
448 | exit (1); | ||
449 | } | ||
450 | |||
451 | if (r > 0) | ||
406 | { | 452 | { |
407 | FD_ZERO(&fds_w); | 453 | if (FD_ISSET (fd_tun, &fds_r)) |
408 | FD_ZERO(&fds_r); | 454 | { |
409 | 455 | buftun_size = | |
410 | /* | 456 | read (fd_tun, buftun + sizeof(struct GNUNET_MessageHeader), |
411 | * We are supposed to read and the buffer is empty | 457 | MAX_SIZE - sizeof(struct GNUNET_MessageHeader)); |
412 | * -> select on read from tun | 458 | if (-1 == buftun_size) |
413 | */ | ||
414 | if (read_open && (0 == buftun_size)) | ||
415 | FD_SET(fd_tun, &fds_r); | ||
416 | |||
417 | /* | ||
418 | * We are supposed to read and the buffer is not empty | ||
419 | * -> select on write to stdout | ||
420 | */ | ||
421 | if (read_open && (0 != buftun_size)) | ||
422 | FD_SET(1, &fds_w); | ||
423 | |||
424 | /* | ||
425 | * We are supposed to write and the buffer is empty | ||
426 | * -> select on read from stdin | ||
427 | */ | ||
428 | if (write_open && (NULL == bufin_read)) | ||
429 | FD_SET(0, &fds_r); | ||
430 | |||
431 | /* | ||
432 | * We are supposed to write and the buffer is not empty | ||
433 | * -> select on write to tun | ||
434 | */ | ||
435 | if (write_open && (NULL != bufin_read)) | ||
436 | FD_SET(fd_tun, &fds_w); | ||
437 | |||
438 | int r = select(fd_tun + 1, &fds_r, &fds_w, NULL, NULL); | ||
439 | |||
440 | if (-1 == r) | ||
441 | { | 459 | { |
442 | if (EINTR == errno) | 460 | fprintf (stderr, |
443 | continue; | 461 | "read-error: %s\n", |
444 | fprintf(stderr, | 462 | strerror (errno)); |
445 | "select failed: %s\n", | 463 | shutdown (fd_tun, SHUT_RD); |
446 | strerror(errno)); | 464 | shutdown (1, SHUT_WR); |
447 | exit(1); | 465 | read_open = 0; |
466 | buftun_size = 0; | ||
448 | } | 467 | } |
449 | 468 | else if (0 == buftun_size) | |
450 | if (r > 0) | ||
451 | { | 469 | { |
452 | if (FD_ISSET(fd_tun, &fds_r)) | 470 | fprintf (stderr, "EOF on tun\n"); |
453 | { | 471 | shutdown (fd_tun, SHUT_RD); |
454 | buftun_size = | 472 | shutdown (1, SHUT_WR); |
455 | read(fd_tun, buftun + sizeof(struct GNUNET_MessageHeader), | 473 | read_open = 0; |
456 | MAX_SIZE - sizeof(struct GNUNET_MessageHeader)); | 474 | buftun_size = 0; |
457 | if (-1 == buftun_size) | 475 | } |
458 | { | 476 | else |
459 | fprintf(stderr, | 477 | { |
460 | "read-error: %s\n", | 478 | buftun_read = buftun; |
461 | strerror(errno)); | 479 | struct GNUNET_MessageHeader *hdr = |
462 | shutdown(fd_tun, SHUT_RD); | 480 | (struct GNUNET_MessageHeader *) buftun; |
463 | shutdown(1, SHUT_WR); | 481 | buftun_size += sizeof(struct GNUNET_MessageHeader); |
464 | read_open = 0; | 482 | hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); |
465 | buftun_size = 0; | 483 | hdr->size = htons (buftun_size); |
466 | } | 484 | } |
467 | else if (0 == buftun_size) | 485 | } |
468 | { | 486 | else if (FD_ISSET (1, &fds_w)) |
469 | fprintf(stderr, "EOF on tun\n"); | 487 | { |
470 | shutdown(fd_tun, SHUT_RD); | 488 | ssize_t written = write (1, |
471 | shutdown(1, SHUT_WR); | 489 | buftun_read, |
472 | read_open = 0; | 490 | buftun_size); |
473 | buftun_size = 0; | 491 | |
474 | } | 492 | if (-1 == written) |
475 | else | 493 | { |
476 | { | 494 | #if ! DEBUG |
477 | buftun_read = buftun; | 495 | if (errno != EPIPE) |
478 | struct GNUNET_MessageHeader *hdr = | ||
479 | (struct GNUNET_MessageHeader *)buftun; | ||
480 | buftun_size += sizeof(struct GNUNET_MessageHeader); | ||
481 | hdr->type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER); | ||
482 | hdr->size = htons(buftun_size); | ||
483 | } | ||
484 | } | ||
485 | else if (FD_ISSET(1, &fds_w)) | ||
486 | { | ||
487 | ssize_t written = write(1, | ||
488 | buftun_read, | ||
489 | buftun_size); | ||
490 | |||
491 | if (-1 == written) | ||
492 | { | ||
493 | #if !DEBUG | ||
494 | if (errno != EPIPE) | ||
495 | #endif | 496 | #endif |
496 | fprintf(stderr, | 497 | fprintf (stderr, |
497 | "write-error to stdout: %s\n", | 498 | "write-error to stdout: %s\n", |
498 | strerror(errno)); | 499 | strerror (errno)); |
499 | shutdown(fd_tun, SHUT_RD); | 500 | shutdown (fd_tun, SHUT_RD); |
500 | shutdown(1, SHUT_WR); | 501 | shutdown (1, SHUT_WR); |
501 | read_open = 0; | 502 | read_open = 0; |
502 | buftun_size = 0; | 503 | buftun_size = 0; |
503 | } | 504 | } |
504 | else if (0 == written) | 505 | else if (0 == written) |
505 | { | 506 | { |
506 | fprintf(stderr, | 507 | fprintf (stderr, |
507 | "write returned 0!?\n"); | 508 | "write returned 0!?\n"); |
508 | exit(1); | 509 | exit (1); |
509 | } | 510 | } |
510 | else | 511 | else |
511 | { | 512 | { |
512 | buftun_size -= written; | 513 | buftun_size -= written; |
513 | buftun_read += written; | 514 | buftun_read += written; |
514 | } | 515 | } |
515 | } | 516 | } |
516 | 517 | ||
517 | if (FD_ISSET(0, &fds_r)) | 518 | if (FD_ISSET (0, &fds_r)) |
518 | { | 519 | { |
519 | bufin_size = read(0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos); | 520 | bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos); |
520 | if (-1 == bufin_size) | 521 | if (-1 == bufin_size) |
521 | { | 522 | { |
522 | fprintf(stderr, | 523 | fprintf (stderr, |
523 | "read-error: %s\n", | 524 | "read-error: %s\n", |
524 | strerror(errno)); | 525 | strerror (errno)); |
525 | shutdown(0, SHUT_RD); | 526 | shutdown (0, SHUT_RD); |
526 | shutdown(fd_tun, SHUT_WR); | 527 | shutdown (fd_tun, SHUT_WR); |
527 | write_open = 0; | 528 | write_open = 0; |
528 | bufin_size = 0; | 529 | bufin_size = 0; |
529 | } | 530 | } |
530 | else if (0 == bufin_size) | 531 | else if (0 == bufin_size) |
531 | { | 532 | { |
532 | #if DEBUG | 533 | #if DEBUG |
533 | fprintf(stderr, "EOF on stdin\n"); | 534 | fprintf (stderr, "EOF on stdin\n"); |
534 | #endif | 535 | #endif |
535 | shutdown(0, SHUT_RD); | 536 | shutdown (0, SHUT_RD); |
536 | shutdown(fd_tun, SHUT_WR); | 537 | shutdown (fd_tun, SHUT_WR); |
537 | write_open = 0; | 538 | write_open = 0; |
538 | bufin_size = 0; | 539 | bufin_size = 0; |
539 | } | 540 | } |
540 | else | 541 | else |
541 | { | 542 | { |
542 | struct GNUNET_MessageHeader *hdr; | 543 | struct GNUNET_MessageHeader *hdr; |
543 | 544 | ||
544 | PROCESS_BUFFER: | 545 | PROCESS_BUFFER: |
545 | bufin_rpos += bufin_size; | 546 | bufin_rpos += bufin_size; |
546 | if (bufin_rpos < sizeof(struct GNUNET_MessageHeader)) | 547 | if (bufin_rpos < sizeof(struct GNUNET_MessageHeader)) |
547 | continue; | 548 | continue; |
548 | hdr = (struct GNUNET_MessageHeader *)bufin; | 549 | hdr = (struct GNUNET_MessageHeader *) bufin; |
549 | if (ntohs(hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) | 550 | if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) |
550 | { | 551 | { |
551 | fprintf(stderr, | 552 | fprintf (stderr, |
552 | "protocol violation!\n"); | 553 | "protocol violation!\n"); |
553 | exit(1); | 554 | exit (1); |
554 | } | 555 | } |
555 | if (ntohs(hdr->size) > bufin_rpos) | 556 | if (ntohs (hdr->size) > bufin_rpos) |
556 | continue; | 557 | continue; |
557 | bufin_read = bufin + sizeof(struct GNUNET_MessageHeader); | 558 | bufin_read = bufin + sizeof(struct GNUNET_MessageHeader); |
558 | bufin_size = ntohs(hdr->size) - sizeof(struct GNUNET_MessageHeader); | 559 | bufin_size = ntohs (hdr->size) - sizeof(struct GNUNET_MessageHeader); |
559 | bufin_rpos -= bufin_size + sizeof(struct GNUNET_MessageHeader); | 560 | bufin_rpos -= bufin_size + sizeof(struct GNUNET_MessageHeader); |
560 | } | ||
561 | } | ||
562 | else if (FD_ISSET(fd_tun, &fds_w)) | ||
563 | { | ||
564 | ssize_t written = write(fd_tun, | ||
565 | bufin_read, | ||
566 | bufin_size); | ||
567 | |||
568 | if (-1 == written) | ||
569 | { | ||
570 | fprintf(stderr, | ||
571 | "write-error to tun: %s\n", | ||
572 | strerror(errno)); | ||
573 | shutdown(0, SHUT_RD); | ||
574 | shutdown(fd_tun, SHUT_WR); | ||
575 | write_open = 0; | ||
576 | bufin_size = 0; | ||
577 | } | ||
578 | else if (0 == written) | ||
579 | { | ||
580 | fprintf(stderr, "write returned 0!?\n"); | ||
581 | exit(1); | ||
582 | } | ||
583 | else | ||
584 | { | ||
585 | bufin_size -= written; | ||
586 | bufin_read += written; | ||
587 | if (0 == bufin_size) | ||
588 | { | ||
589 | memmove(bufin, bufin_read, bufin_rpos); | ||
590 | bufin_read = NULL; /* start reading again */ | ||
591 | bufin_size = 0; | ||
592 | goto PROCESS_BUFFER; | ||
593 | } | ||
594 | } | ||
595 | } | ||
596 | } | 561 | } |
562 | } | ||
563 | else if (FD_ISSET (fd_tun, &fds_w)) | ||
564 | { | ||
565 | ssize_t written = write (fd_tun, | ||
566 | bufin_read, | ||
567 | bufin_size); | ||
568 | |||
569 | if (-1 == written) | ||
570 | { | ||
571 | fprintf (stderr, | ||
572 | "write-error to tun: %s\n", | ||
573 | strerror (errno)); | ||
574 | shutdown (0, SHUT_RD); | ||
575 | shutdown (fd_tun, SHUT_WR); | ||
576 | write_open = 0; | ||
577 | bufin_size = 0; | ||
578 | } | ||
579 | else if (0 == written) | ||
580 | { | ||
581 | fprintf (stderr, "write returned 0!?\n"); | ||
582 | exit (1); | ||
583 | } | ||
584 | else | ||
585 | { | ||
586 | bufin_size -= written; | ||
587 | bufin_read += written; | ||
588 | if (0 == bufin_size) | ||
589 | { | ||
590 | memmove (bufin, bufin_read, bufin_rpos); | ||
591 | bufin_read = NULL; /* start reading again */ | ||
592 | bufin_size = 0; | ||
593 | goto PROCESS_BUFFER; | ||
594 | } | ||
595 | } | ||
596 | } | ||
597 | } | 597 | } |
598 | } | ||
598 | } | 599 | } |
599 | 600 | ||
600 | 601 | ||
@@ -610,92 +611,92 @@ PROCESS_BUFFER: | |||
610 | * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-" | 611 | * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-" |
611 | */ | 612 | */ |
612 | int | 613 | int |
613 | main(int argc, char **argv) | 614 | main (int argc, char **argv) |
614 | { | 615 | { |
615 | char dev[IFNAMSIZ]; | 616 | char dev[IFNAMSIZ]; |
616 | int fd_tun; | 617 | int fd_tun; |
617 | int global_ret; | 618 | int global_ret; |
618 | 619 | ||
619 | if (6 != argc) | 620 | if (6 != argc) |
620 | { | 621 | { |
621 | fprintf(stderr, "Fatal: must supply 5 arguments!\n"); | 622 | fprintf (stderr, "Fatal: must supply 5 arguments!\n"); |
622 | return 1; | 623 | return 1; |
623 | } | 624 | } |
624 | 625 | ||
625 | strncpy(dev, | 626 | strncpy (dev, |
626 | argv[1], | 627 | argv[1], |
627 | IFNAMSIZ); | 628 | IFNAMSIZ); |
628 | dev[IFNAMSIZ - 1] = '\0'; | 629 | dev[IFNAMSIZ - 1] = '\0'; |
629 | 630 | ||
630 | if (-1 == (fd_tun = init_tun(dev))) | 631 | if (-1 == (fd_tun = init_tun (dev))) |
632 | { | ||
633 | fprintf (stderr, | ||
634 | "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n", | ||
635 | dev, | ||
636 | argv[2], | ||
637 | argv[3], | ||
638 | argv[4], | ||
639 | argv[5]); | ||
640 | return 1; | ||
641 | } | ||
642 | |||
643 | if (0 != strcmp (argv[2], "-")) | ||
644 | { | ||
645 | const char *address = argv[2]; | ||
646 | long prefix_len = atol (argv[3]); | ||
647 | |||
648 | if ((prefix_len < 1) || (prefix_len > 127)) | ||
631 | { | 649 | { |
632 | fprintf(stderr, | 650 | fprintf (stderr, |
633 | "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n", | 651 | "Fatal: prefix_len out of range\n"); |
634 | dev, | 652 | close (fd_tun); |
635 | argv[2], | ||
636 | argv[3], | ||
637 | argv[4], | ||
638 | argv[5]); | ||
639 | return 1; | 653 | return 1; |
640 | } | 654 | } |
641 | 655 | ||
642 | if (0 != strcmp(argv[2], "-")) | 656 | set_address6 (dev, |
643 | { | 657 | address, |
644 | const char *address = argv[2]; | 658 | prefix_len); |
645 | long prefix_len = atol(argv[3]); | 659 | } |
646 | 660 | ||
647 | if ((prefix_len < 1) || (prefix_len > 127)) | 661 | if (0 != strcmp (argv[4], "-")) |
648 | { | 662 | { |
649 | fprintf(stderr, | 663 | const char *address = argv[4]; |
650 | "Fatal: prefix_len out of range\n"); | 664 | const char *mask = argv[5]; |
651 | close(fd_tun); | ||
652 | return 1; | ||
653 | } | ||
654 | 665 | ||
655 | set_address6(dev, | 666 | set_address4 (dev, address, mask); |
656 | address, | 667 | } |
657 | prefix_len); | ||
658 | } | ||
659 | |||
660 | if (0 != strcmp(argv[4], "-")) | ||
661 | { | ||
662 | const char *address = argv[4]; | ||
663 | const char *mask = argv[5]; | ||
664 | 668 | ||
665 | set_address4(dev, address, mask); | 669 | uid_t uid = getuid (); |
666 | } | ||
667 | |||
668 | uid_t uid = getuid(); | ||
669 | #ifdef HAVE_SETRESUID | 670 | #ifdef HAVE_SETRESUID |
670 | if (0 != setresuid(uid, uid, uid)) | 671 | if (0 != setresuid (uid, uid, uid)) |
671 | { | 672 | { |
672 | fprintf(stderr, | 673 | fprintf (stderr, |
673 | "Failed to setresuid: %s\n", | 674 | "Failed to setresuid: %s\n", |
674 | strerror(errno)); | 675 | strerror (errno)); |
675 | global_ret = 2; | 676 | global_ret = 2; |
676 | goto cleanup; | 677 | goto cleanup; |
677 | } | 678 | } |
678 | #else | 679 | #else |
679 | if (0 != (setuid(uid) | seteuid(uid))) | 680 | if (0 != (setuid (uid) | seteuid (uid))) |
680 | { | 681 | { |
681 | fprintf(stderr, | 682 | fprintf (stderr, |
682 | "Failed to setuid: %s\n", | 683 | "Failed to setuid: %s\n", |
683 | strerror(errno)); | 684 | strerror (errno)); |
684 | global_ret = 2; | 685 | global_ret = 2; |
685 | goto cleanup; | 686 | goto cleanup; |
686 | } | 687 | } |
687 | #endif | 688 | #endif |
688 | 689 | ||
689 | if (SIG_ERR == signal(SIGPIPE, SIG_IGN)) | 690 | if (SIG_ERR == signal (SIGPIPE, SIG_IGN)) |
690 | { | 691 | { |
691 | fprintf(stderr, | 692 | fprintf (stderr, |
692 | "Failed to protect against SIGPIPE: %s\n", | 693 | "Failed to protect against SIGPIPE: %s\n", |
693 | strerror(errno)); | 694 | strerror (errno)); |
694 | /* no exit, we might as well die with SIGPIPE should it ever happen */ | 695 | /* no exit, we might as well die with SIGPIPE should it ever happen */ |
695 | } | 696 | } |
696 | run(fd_tun); | 697 | run (fd_tun); |
697 | global_ret = 0; | 698 | global_ret = 0; |
698 | cleanup: | 699 | cleanup: |
699 | close(fd_tun); | 700 | close (fd_tun); |
700 | return global_ret; | 701 | return global_ret; |
701 | } | 702 | } |