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

View File

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

View File

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

12
tun.c
View File

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