handles errors from tun and dns(d)

This commit is contained in:
Bjorn Andersson 2006-06-11 14:42:19 +00:00
parent b9a14dcae2
commit 02763e9a84
4 changed files with 23 additions and 16 deletions

10
dns.c
View file

@ -70,7 +70,7 @@ open_dns(const char *host, const char *domain)
fd = socket(AF_INET, SOCK_DGRAM, 0); fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0) { if(fd < 0) {
warn("socket"); warn("socket");
return 0; return -1;
} }
flag = 1; flag = 1;
@ -81,7 +81,7 @@ open_dns(const char *host, const char *domain)
if(bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { if(bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
warn("bind"); warn("bind");
return 0; return -1;
} }
printf("Opened UDP socket\n"); printf("Opened UDP socket\n");
@ -91,7 +91,7 @@ open_dns(const char *host, const char *domain)
h = gethostbyname(host); h = gethostbyname(host);
if (!h) { if (!h) {
printf("Could not resolve name %s, exiting\n", host); printf("Could not resolve name %s, exiting\n", host);
exit(9); return -1;
} }
bzero(&peer, sizeof(peer)); bzero(&peer, sizeof(peer));
peer.sin_family = AF_INET; peer.sin_family = AF_INET;
@ -124,7 +124,7 @@ open_dnsd(const char *domain)
fd = socket(AF_INET, SOCK_DGRAM, 0); fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0) { if(fd < 0) {
warn("socket"); warn("socket");
return 0; return -1;
} }
flag = 1; flag = 1;
@ -135,7 +135,7 @@ open_dnsd(const char *domain)
if(bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { if(bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
warn("bind"); warn("bind");
return 0; return -1;
} }
printf("Opened UDP socket\n"); printf("Opened UDP socket\n");

View file

@ -185,8 +185,10 @@ main(int argc, char **argv)
} }
} }
tun_fd = open_tun(); if ((tun_fd = open_tun()) == -1)
dns_fd = open_dns(argv[0], argv[1]); goto cleanup1;
if ((dns_fd = open_dns(argv[0], argv[1])) == -1)
goto cleanup2;
signal(SIGINT, sigint); signal(SIGINT, sigint);
@ -215,7 +217,9 @@ main(int argc, char **argv)
printf("Closing tunnel\n"); printf("Closing tunnel\n");
cleanup2:
close_dns(dns_fd); close_dns(dns_fd);
cleanup1:
close_tun(tun_fd); close_tun(tun_fd);
return 0; return 0;

View file

@ -187,8 +187,11 @@ main(int argc, char **argv)
} }
} }
tun_fd = open_tun(); if ((tun_fd = open_tun()) == -1)
dnsd_fd = open_dnsd(argv[0]); goto cleanup1;
if ((dnsd_fd = open_dnsd(argv[0])) == -1)
goto cleanup2;
if (newroot) { if (newroot) {
if (chroot(newroot) != 0 || chdir("/") != 0) if (chroot(newroot) != 0 || chdir("/") != 0)
@ -214,7 +217,9 @@ main(int argc, char **argv)
tunnel(tun_fd, dnsd_fd); tunnel(tun_fd, dnsd_fd);
cleanup2:
close_dnsd(dnsd_fd); close_dnsd(dnsd_fd);
cleanup1:
close_tun(tun_fd); close_tun(tun_fd);
return 0; return 0;

12
tun.c
View file

@ -49,7 +49,7 @@ open_tun()
if ((tun_fd = open(tun_device, O_RDWR)) < 0) { if ((tun_fd = open(tun_device, O_RDWR)) < 0) {
warn("open_tun: %s: %s", tun_device, strerror(errno)); warn("open_tun: %s: %s", tun_device, strerror(errno));
exit(1); return -1;
} }
bzero(&ifreq, sizeof(ifreq)); bzero(&ifreq, sizeof(ifreq));
@ -66,14 +66,13 @@ open_tun()
if (errno != EBUSY) { if (errno != EBUSY) {
warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno)); warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno));
return 0; return -1;
} }
} }
warn("open_tun: Couldn't set interface name.\n"); warn("open_tun: Couldn't set interface name.\n");
exit(1);
return 0; return -1;
} }
#else /* BSD */ #else /* BSD */
@ -88,7 +87,7 @@ open_tun()
if (tun_device != NULL) { if (tun_device != NULL) {
if ((tun_fd = open(tun_device, O_RDWR)) < 0) { if ((tun_fd = open(tun_device, O_RDWR)) < 0) {
warn("open_tun: %s: %s", tun_device, strerror(errno)); warn("open_tun: %s: %s", tun_device, strerror(errno));
exit(1); return -1;
} }
} else { } else {
for (i = 0; i < TUN_MAX_TRY; i++) { for (i = 0; i < TUN_MAX_TRY; i++) {
@ -104,10 +103,9 @@ open_tun()
} }
warn("open_tun: Failed to open tunneling device."); warn("open_tun: Failed to open tunneling device.");
exit(1);
} }
return 0; return -1;
} }
#endif /* LINUX */ #endif /* LINUX */