aboutsummaryrefslogtreecommitdiff
path: root/src/vpn/gnunet-helper-vpn.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vpn/gnunet-helper-vpn.c')
-rw-r--r--src/vpn/gnunet-helper-vpn.c132
1 files changed, 98 insertions, 34 deletions
diff --git a/src/vpn/gnunet-helper-vpn.c b/src/vpn/gnunet-helper-vpn.c
index 0519680eb..02889d65b 100644
--- a/src/vpn/gnunet-helper-vpn.c
+++ b/src/vpn/gnunet-helper-vpn.c
@@ -91,14 +91,18 @@ init_tun (char *dev)
91 91
92 if (-1 == (fd = open ("/dev/net/tun", O_RDWR))) 92 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
93 { 93 {
94 fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun", 94 fprintf (stderr,
95 "Error opening `%s': %s\n",
96 "/dev/net/tun",
95 strerror (errno)); 97 strerror (errno));
96 return -1; 98 return -1;
97 } 99 }
98 100
99 if (fd >= FD_SETSIZE) 101 if (fd >= FD_SETSIZE)
100 { 102 {
101 fprintf (stderr, "File descriptor to large: %d", fd); 103 fprintf (stderr,
104 "File descriptor to large: %d",
105 fd);
102 (void) close (fd); 106 (void) close (fd);
103 return -1; 107 return -1;
104 } 108 }
@@ -107,9 +111,13 @@ init_tun (char *dev)
107 ifr.ifr_flags = IFF_TUN; 111 ifr.ifr_flags = IFF_TUN;
108 112
109 if ('\0' != *dev) 113 if ('\0' != *dev)
110 strncpy (ifr.ifr_name, dev, IFNAMSIZ); 114 strncpy (ifr.ifr_name,
115 dev,
116 IFNAMSIZ);
111 117
112 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr)) 118 if (-1 == ioctl (fd,
119 TUNSETIFF,
120 (void *) &ifr))
113 { 121 {
114 fprintf (stderr, 122 fprintf (stderr,
115 "Error with ioctl on `%s': %s\n", 123 "Error with ioctl on `%s': %s\n",
@@ -131,7 +139,9 @@ init_tun (char *dev)
131 * @param prefix_len the length of the network-prefix 139 * @param prefix_len the length of the network-prefix
132 */ 140 */
133static void 141static void
134set_address6 (const char *dev, const char *address, unsigned long prefix_len) 142set_address6 (const char *dev,
143 const char *address,
144 unsigned long prefix_len)
135{ 145{
136 struct ifreq ifr; 146 struct ifreq ifr;
137 struct in6_ifreq ifr6; 147 struct in6_ifreq ifr6;
@@ -163,10 +173,17 @@ set_address6 (const char *dev, const char *address, unsigned long prefix_len)
163 /* 173 /*
164 * Get the index of the if 174 * Get the index of the if
165 */ 175 */
166 strncpy (ifr.ifr_name, dev, IFNAMSIZ); 176 strncpy (ifr.ifr_name,
167 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr)) 177 dev,
178 IFNAMSIZ);
179 if (-1 == ioctl (fd,
180 SIOGIFINDEX,
181 &ifr))
168 { 182 {
169 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno)); 183 fprintf (stderr,
184 "ioctl failed at %d: %s\n",
185 __LINE__,
186 strerror (errno));
170 (void) close (fd); 187 (void) close (fd);
171 exit (1); 188 exit (1);
172 } 189 }
@@ -179,9 +196,13 @@ set_address6 (const char *dev, const char *address, unsigned long prefix_len)
179 /* 196 /*
180 * Set the address 197 * Set the address
181 */ 198 */
182 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6)) 199 if (-1 == ioctl (fd,
200 SIOCSIFADDR,
201 &ifr6))
183 { 202 {
184 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 203 fprintf (stderr,
204 "ioctl failed at line %d: %s\n",
205 __LINE__,
185 strerror (errno)); 206 strerror (errno));
186 (void) close (fd); 207 (void) close (fd);
187 exit (1); 208 exit (1);
@@ -190,9 +211,13 @@ set_address6 (const char *dev, const char *address, unsigned long prefix_len)
190 /* 211 /*
191 * Get the flags 212 * Get the flags
192 */ 213 */
193 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr)) 214 if (-1 == ioctl (fd,
215 SIOCGIFFLAGS,
216 &ifr))
194 { 217 {
195 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 218 fprintf (stderr,
219 "ioctl failed at line %d: %s\n",
220 __LINE__,
196 strerror (errno)); 221 strerror (errno));
197 (void) close (fd); 222 (void) close (fd);
198 exit (1); 223 exit (1);
@@ -202,9 +227,13 @@ set_address6 (const char *dev, const char *address, unsigned long prefix_len)
202 * Add the UP and RUNNING flags 227 * Add the UP and RUNNING flags
203 */ 228 */
204 ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 229 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
205 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr)) 230 if (-1 == ioctl (fd,
231 SIOCSIFFLAGS,
232 &ifr))
206 { 233 {
207 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 234 fprintf (stderr,
235 "ioctl failed at line %d: %s\n",
236 __LINE__,
208 strerror (errno)); 237 strerror (errno));
209 (void) close (fd); 238 (void) close (fd);
210 exit (1); 239 exit (1);
@@ -212,7 +241,9 @@ set_address6 (const char *dev, const char *address, unsigned long prefix_len)
212 241
213 if (0 != close (fd)) 242 if (0 != close (fd))
214 { 243 {
215 fprintf (stderr, "close failed: %s\n", strerror (errno)); 244 fprintf (stderr,
245 "close failed: %s\n",
246 strerror (errno));
216 exit (1); 247 exit (1);
217 } 248 }
218} 249}
@@ -226,7 +257,9 @@ set_address6 (const char *dev, const char *address, unsigned long prefix_len)
226 * @param mask the netmask 257 * @param mask the netmask
227 */ 258 */
228static void 259static void
229set_address4 (const char *dev, const char *address, const char *mask) 260set_address4 (const char *dev,
261 const char *address,
262 const char *mask)
230{ 263{
231 int fd; 264 int fd;
232 struct sockaddr_in *addr; 265 struct sockaddr_in *addr;
@@ -239,7 +272,9 @@ set_address4 (const char *dev, const char *address, const char *mask)
239 /* 272 /*
240 * Parse the address 273 * Parse the address
241 */ 274 */
242 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr)) 275 if (1 != inet_pton (AF_INET,
276 address,
277 &addr->sin_addr.s_addr))
243 { 278 {
244 fprintf (stderr, 279 fprintf (stderr,
245 "Failed to parse IPv4 address `%s'\n", 280 "Failed to parse IPv4 address `%s'\n",
@@ -274,7 +309,9 @@ set_address4 (const char *dev, const char *address, const char *mask)
274 * Parse the netmask 309 * Parse the netmask
275 */ 310 */
276 addr = (struct sockaddr_in *) &(ifr.ifr_netmask); 311 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
277 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr)) 312 if (1 != inet_pton (AF_INET,
313 mask,
314 &addr->sin_addr.s_addr))
278 { 315 {
279 fprintf (stderr, 316 fprintf (stderr,
280 "Failed to parse IPv4 address mask `%s'\n", 317 "Failed to parse IPv4 address mask `%s'\n",
@@ -301,7 +338,9 @@ set_address4 (const char *dev, const char *address, const char *mask)
301 */ 338 */
302 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr)) 339 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
303 { 340 {
304 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 341 fprintf (stderr,
342 "ioctl failed at line %d: %s\n",
343 __LINE__,
305 strerror (errno)); 344 strerror (errno));
306 (void) close (fd); 345 (void) close (fd);
307 exit (1); 346 exit (1);
@@ -313,7 +352,9 @@ set_address4 (const char *dev, const char *address, const char *mask)
313 ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 352 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
314 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr)) 353 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
315 { 354 {
316 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 355 fprintf (stderr,
356 "ioctl failed at line %d: %s\n",
357 __LINE__,
317 strerror (errno)); 358 strerror (errno));
318 (void) close (fd); 359 (void) close (fd);
319 exit (1); 360 exit (1);
@@ -321,7 +362,9 @@ set_address4 (const char *dev, const char *address, const char *mask)
321 362
322 if (0 != close (fd)) 363 if (0 != close (fd))
323 { 364 {
324 fprintf (stderr, "close failed: %s\n", strerror (errno)); 365 fprintf (stderr,
366 "close failed: %s\n",
367 strerror (errno));
325 (void) close (fd); 368 (void) close (fd);
326 exit (1); 369 exit (1);
327 } 370 }
@@ -399,7 +442,9 @@ run (int fd_tun)
399 { 442 {
400 if (EINTR == errno) 443 if (EINTR == errno)
401 continue; 444 continue;
402 fprintf (stderr, "select failed: %s\n", strerror (errno)); 445 fprintf (stderr,
446 "select failed: %s\n",
447 strerror (errno));
403 exit (1); 448 exit (1);
404 } 449 }
405 450
@@ -440,7 +485,9 @@ run (int fd_tun)
440 } 485 }
441 else if (FD_ISSET (1, &fds_w)) 486 else if (FD_ISSET (1, &fds_w))
442 { 487 {
443 ssize_t written = write (1, buftun_read, buftun_size); 488 ssize_t written = write (1,
489 buftun_read,
490 buftun_size);
444 491
445 if (-1 == written) 492 if (-1 == written)
446 { 493 {
@@ -457,7 +504,8 @@ run (int fd_tun)
457 } 504 }
458 else if (0 == written) 505 else if (0 == written)
459 { 506 {
460 fprintf (stderr, "write returned 0!?\n"); 507 fprintf (stderr,
508 "write returned 0!?\n");
461 exit (1); 509 exit (1);
462 } 510 }
463 else 511 else
@@ -501,7 +549,8 @@ PROCESS_BUFFER:
501 hdr = (struct GNUNET_MessageHeader *) bufin; 549 hdr = (struct GNUNET_MessageHeader *) bufin;
502 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) 550 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
503 { 551 {
504 fprintf (stderr, "protocol violation!\n"); 552 fprintf (stderr,
553 "protocol violation!\n");
505 exit (1); 554 exit (1);
506 } 555 }
507 if (ntohs (hdr->size) > bufin_rpos) 556 if (ntohs (hdr->size) > bufin_rpos)
@@ -513,11 +562,15 @@ PROCESS_BUFFER:
513 } 562 }
514 else if (FD_ISSET (fd_tun, &fds_w)) 563 else if (FD_ISSET (fd_tun, &fds_w))
515 { 564 {
516 ssize_t written = write (fd_tun, bufin_read, bufin_size); 565 ssize_t written = write (fd_tun,
566 bufin_read,
567 bufin_size);
517 568
518 if (-1 == written) 569 if (-1 == written)
519 { 570 {
520 fprintf (stderr, "write-error to tun: %s\n", strerror (errno)); 571 fprintf (stderr,
572 "write-error to tun: %s\n",
573 strerror (errno));
521 shutdown (0, SHUT_RD); 574 shutdown (0, SHUT_RD);
522 shutdown (fd_tun, SHUT_WR); 575 shutdown (fd_tun, SHUT_WR);
523 write_open = 0; 576 write_open = 0;
@@ -570,12 +623,15 @@ main (int argc, char **argv)
570 return 1; 623 return 1;
571 } 624 }
572 625
573 strncpy (dev, argv[1], IFNAMSIZ); 626 strncpy (dev,
627 argv[1],
628 IFNAMSIZ);
574 dev[IFNAMSIZ - 1] = '\0'; 629 dev[IFNAMSIZ - 1] = '\0';
575 630
576 if (-1 == (fd_tun = init_tun (dev))) 631 if (-1 == (fd_tun = init_tun (dev)))
577 { 632 {
578 fprintf (stderr, "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n", 633 fprintf (stderr,
634 "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
579 dev, 635 dev,
580 argv[2], 636 argv[2],
581 argv[3], 637 argv[3],
@@ -591,11 +647,14 @@ main (int argc, char **argv)
591 647
592 if ((prefix_len < 1) || (prefix_len > 127)) 648 if ((prefix_len < 1) || (prefix_len > 127))
593 { 649 {
594 fprintf (stderr, "Fatal: prefix_len out of range\n"); 650 fprintf (stderr,
651 "Fatal: prefix_len out of range\n");
595 return 1; 652 return 1;
596 } 653 }
597 654
598 set_address6 (dev, address, prefix_len); 655 set_address6 (dev,
656 address,
657 prefix_len);
599 } 658 }
600 659
601 if (0 != strcmp (argv[4], "-")) 660 if (0 != strcmp (argv[4], "-"))
@@ -610,14 +669,18 @@ main (int argc, char **argv)
610#ifdef HAVE_SETRESUID 669#ifdef HAVE_SETRESUID
611 if (0 != setresuid (uid, uid, uid)) 670 if (0 != setresuid (uid, uid, uid))
612 { 671 {
613 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno)); 672 fprintf (stderr,
673 "Failed to setresuid: %s\n",
674 strerror (errno));
614 global_ret = 2; 675 global_ret = 2;
615 goto cleanup; 676 goto cleanup;
616 } 677 }
617#else 678#else
618 if (0 != (setuid (uid) | seteuid (uid))) 679 if (0 != (setuid (uid) | seteuid (uid)))
619 { 680 {
620 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno)); 681 fprintf (stderr,
682 "Failed to setuid: %s\n",
683 strerror (errno));
621 global_ret = 2; 684 global_ret = 2;
622 goto cleanup; 685 goto cleanup;
623 } 686 }
@@ -625,7 +688,8 @@ main (int argc, char **argv)
625 688
626 if (SIG_ERR == signal (SIGPIPE, SIG_IGN)) 689 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
627 { 690 {
628 fprintf (stderr, "Failed to protect against SIGPIPE: %s\n", 691 fprintf (stderr,
692 "Failed to protect against SIGPIPE: %s\n",
629 strerror (errno)); 693 strerror (errno));
630 /* no exit, we might as well die with SIGPIPE should it ever happen */ 694 /* no exit, we might as well die with SIGPIPE should it ever happen */
631 } 695 }