summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2014-06-29 20:12:31 +0000
committerChristian Pointner <equinox@anytun.org>2014-06-29 20:12:31 +0000
commit027198c8793a9f19931b88b5f650ac6176e44f38 (patch)
tree19a64ad9718b27ecc5216f31c94ca252b97bec1e
parentbasic write functionality for unix domain sockets work as well (diff)
added error handling to unix domain sockets
-rw-r--r--src/uanytun.c4
-rw-r--r--src/unixdomain.c29
2 files changed, 23 insertions, 10 deletions
diff --git a/src/uanytun.c b/src/uanytun.c
index b867648..b95996f 100644
--- a/src/uanytun.c
+++ b/src/uanytun.c
@@ -219,7 +219,9 @@ int process_key_exchange(unixdomain_t* sock, fd_set* readyfds)
{
if(FD_ISSET(sock->server_fd_, readyfds)) {
int old_fd = sock->client_fd_;
- unixdomain_accept(sock);
+ if(unixdomain_accept(sock)) {
+ return -1;
+ }
if(old_fd != sock->client_fd_) {
log_printf(INFO, "key exchange: new client");
}
diff --git a/src/unixdomain.c b/src/unixdomain.c
index 599ddc6..6fce43c 100644
--- a/src/unixdomain.c
+++ b/src/unixdomain.c
@@ -83,14 +83,23 @@ int unixdomain_init(unixdomain_t* sock, const char* path)
strncpy(sock->server_addr_.sun_path, path, sizeof(sock->server_addr_.sun_path)-1);
- unlink(sock->server_addr_.sun_path); // remove stale socket
- // TODO: error handling
+ if(unlink(sock->server_addr_.sun_path)) {
+ if(errno != ENOENT)
+ log_printf(WARNING, "unix domain socket '%s' unlink(): %s", sock->server_addr_.sun_path, strerror(errno));
+ }
- bind(sock->server_fd_, (struct sockaddr*)&(sock->server_addr_), sizeof(sock->server_addr_));
- // TODO: error handling
+ if(bind(sock->server_fd_, (struct sockaddr*)&(sock->server_addr_), sizeof(sock->server_addr_))) {
+ log_printf(ERROR, "unix domain socket '%s' bind(): %s", sock->server_addr_.sun_path, strerror(errno));
+ unixdomain_close(sock);
+ return -1;
+ }
+ // TODO: permissions
- listen(sock->server_fd_, 1);
- // TODO: error handling
+ if(listen(sock->server_fd_, 1)) {
+ log_printf(ERROR, "unix domain socket '%s' listen(): %s", sock->server_addr_.sun_path, strerror(errno));
+ unixdomain_close(sock);
+ return -1;
+ }
log_printf(NOTICE, "unixdomain socket listening on: %s", sock->server_addr_.sun_path);
@@ -123,8 +132,10 @@ void unixdomain_close(unixdomain_t* sock)
close(sock->client_fd_);
if(sock->server_fd_ >= 0) {
close(sock->server_fd_);
- unlink(sock->server_addr_.sun_path);
- // TODO: error handling?
+ if(unlink(sock->server_addr_.sun_path)) {
+ if(errno != ENOENT)
+ log_printf(WARNING, "unix domain socket '%s' unlink(): %s", sock->server_addr_.sun_path, strerror(errno));
+ }
}
}
@@ -135,7 +146,7 @@ int unixdomain_accept(unixdomain_t* sock)
int new_client = accept(sock->server_fd_, NULL, NULL);
if(new_client < 0) {
- // TODO: error HANDLING
+ log_printf(ERROR, "unix domain socket '%s' accept(): %s", sock->server_addr_.sun_path, strerror(errno));
return -1;
}