From: Etienne Dechamps Date: Thu, 26 Jun 2014 19:42:40 +0000 (+0100) Subject: Fix errno references when handling socket errors. X-Git-Tag: release-1.1pre11~97 X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=0c026f3c6dec784c3267ad7e2c4709d5393dc292;p=tinc Fix errno references when handling socket errors. When using socket functions, "sockerrno" is supposed to be used to retrieve the error code as opposed to "errno", so that it is translated to the correct call on Windows (WSAGetLastError() - Windows does not update errno on socket errors). Unfortunately, the use of sockerrno is inconsistent throughout the tinc codebase, as errno is often used incorrectly on socket-related calls. This commit fixes these oversights, which improves socket error handling on Windows. --- diff --git a/src/control.c b/src/control.c index 45627495..dc8890a8 100644 --- a/src/control.c +++ b/src/control.c @@ -178,7 +178,7 @@ bool init_control(void) { #ifndef HAVE_MINGW int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0); if(unix_fd < 0) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(sockerrno)); return false; } @@ -198,12 +198,12 @@ bool init_control(void) { umask(mask); if(result < 0) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(sockerrno)); return false; } if(listen(unix_fd, 3) < 0) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(sockerrno)); return false; } diff --git a/src/event.c b/src/event.c index 5a1e4f5b..27b884c4 100644 --- a/src/event.c +++ b/src/event.c @@ -225,7 +225,7 @@ bool event_loop(void) { #endif if(n < 0) { - if(sockwouldblock(errno)) + if(sockwouldblock(sockerrno)) continue; else return false; diff --git a/src/meta.c b/src/meta.c index 887da4a8..25cca5f0 100644 --- a/src/meta.c +++ b/src/meta.c @@ -142,7 +142,7 @@ bool receive_meta(connection_t *c) { inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0); if(inlen <= 0) { - if(!inlen || !errno) { + if(!inlen || !sockerrno) { logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)", c->name, c->hostname); } else if(sockwouldblock(sockerrno)) diff --git a/src/multicast_device.c b/src/multicast_device.c index ba272eb2..de3ff15e 100644 --- a/src/multicast_device.c +++ b/src/multicast_device.c @@ -164,7 +164,7 @@ static bool read_packet(vpn_packet_t *packet) { if((lenin = recv(device_fd, (void *)packet->data, MTU, 0)) <= 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info, - device, strerror(errno)); + device, sockstrerror(sockerrno)); return false; } @@ -187,7 +187,7 @@ static bool write_packet(vpn_packet_t *packet) { if(sendto(device_fd, (void *)packet->data, packet->len, 0, ai->ai_addr, ai->ai_addrlen) < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, - strerror(errno)); + sockstrerror(sockerrno)); return false; } diff --git a/src/net.c b/src/net.c index 1e6b4c65..b6ddc571 100644 --- a/src/net.c +++ b/src/net.c @@ -465,7 +465,7 @@ int main_loop(void) { #endif if(!event_loop()) { - logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", sockstrerror(sockerrno)); return 1; } diff --git a/src/net_packet.c b/src/net_packet.c index 70b6106b..f69bf98f 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -744,7 +744,7 @@ static void send_udppacket(node_t *n, vpn_packet_t *origpkt) { priority = origpriority; logger(DEBUG_TRAFFIC, LOG_DEBUG, "Setting outgoing packet priority to %d", priority); if(setsockopt(listen_socket[n->sock].udp.fd, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */ - logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "setsockopt", strerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "setsockopt", sockstrerror(sockerrno)); } #endif diff --git a/src/net_setup.c b/src/net_setup.c index eec1711a..e3c543c9 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -417,7 +417,7 @@ char *get_name(void) { return false; } if(gethostname(hostname, sizeof hostname) || !*hostname) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", sockstrerror(sockerrno)); return false; } hostname[31] = 0; @@ -980,7 +980,7 @@ static bool setup_myself(void) { for(int i = 0; i < listen_sockets; i++) { salen = sizeof sa; if(getsockname(i + 3, &sa.sa, &salen) < 0) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno)); return false; } diff --git a/src/net_socket.c b/src/net_socket.c index cc91521d..939aa9c4 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -103,7 +103,7 @@ static bool bind_to_interface(int sd) { status = setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)); if(status) { logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to interface %s: %s", iface, - strerror(errno)); + sockstrerror(sockerrno)); return false; } #else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */ @@ -134,7 +134,7 @@ static bool bind_to_address(connection_t *c) { sa.in6.sin6_port = 0; if(bind(c->socket, &sa.sa, SALEN(sa.sa))) { - logger(DEBUG_CONNECTIONS, LOG_WARNING, "Can't bind outgoing socket: %s", strerror(errno)); + logger(DEBUG_CONNECTIONS, LOG_WARNING, "Can't bind outgoing socket: %s", sockstrerror(sockerrno)); return false; } @@ -179,7 +179,7 @@ int setup_listen_socket(const sockaddr_t *sa) { if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof ifr)) { closesocket(nfd); logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to interface %s: %s", iface, - strerror(sockerrno)); + sockstrerror(sockerrno)); return -1; } #else @@ -247,10 +247,10 @@ int setup_vpn_in_socket(const sockaddr_t *sa) { setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof option); if(udp_rcvbuf && setsockopt(nfd, SOL_SOCKET, SO_RCVBUF, (void *)&udp_rcvbuf, sizeof(udp_rcvbuf))) - logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", udp_rcvbuf, strerror(errno)); + logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", udp_rcvbuf, sockstrerror(sockerrno)); if(udp_sndbuf && setsockopt(nfd, SOL_SOCKET, SO_SNDBUF, (void *)&udp_sndbuf, sizeof(udp_sndbuf))) - logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, strerror(errno)); + logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, sockstrerror(sockerrno)); #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY) if(sa->sa.sa_family == AF_INET6) @@ -330,7 +330,7 @@ static void do_outgoing_pipe(connection_t *c, char *command) { int fd[2]; if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not create socketpair: %s", strerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Could not create socketpair: %s", sockstrerror(sockerrno)); return; } @@ -379,13 +379,13 @@ static void handle_meta_write(connection_t *c) { ssize_t outlen = send(c->socket, c->outbuf.data + c->outbuf.offset, c->outbuf.len - c->outbuf.offset, 0); if(outlen <= 0) { - if(!errno || errno == EPIPE) { + if(!sockerrno || sockerrno == EPIPE) { logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)", c->name, c->hostname); } else if(sockwouldblock(sockerrno)) { logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Sending %d bytes to %s (%s) would block", c->outbuf.len - c->outbuf.offset, c->name, c->hostname); return; } else { - logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not send %d bytes of data to %s (%s): %s", c->outbuf.len - c->outbuf.offset, c->name, c->hostname, strerror(errno)); + logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not send %d bytes of data to %s (%s): %s", c->outbuf.len - c->outbuf.offset, c->name, c->hostname, sockstrerror(sockerrno)); } terminate_connection(c, c->status.active); diff --git a/src/sptps_speed.c b/src/sptps_speed.c index 953d7f5e..1ba00f2c 100644 --- a/src/sptps_speed.c +++ b/src/sptps_speed.c @@ -18,6 +18,7 @@ */ #include "system.h" +#include "utils.h" #include @@ -121,7 +122,7 @@ int main(int argc, char *argv[]) { int fd[2]; if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) { - fprintf(stderr, "Could not create a UNIX socket pair: %s\n", strerror(errno)); + fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno)); return 1; } @@ -174,7 +175,7 @@ int main(int argc, char *argv[]) { close(fd[1]); if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) { - fprintf(stderr, "Could not create a UNIX socket pair: %s\n", strerror(errno)); + fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno)); return 1; } diff --git a/src/sptps_test.c b/src/sptps_test.c index 3ec9f984..1fb7f97c 100644 --- a/src/sptps_test.c +++ b/src/sptps_test.c @@ -210,13 +210,13 @@ int main(int argc, char *argv[]) { hint.ai_flags = initiator ? 0 : AI_PASSIVE; if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) { - fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno)); + fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno)); return 1; } int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if(sock < 0) { - fprintf(stderr, "Could not create socket: %s\n", strerror(errno)); + fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno)); return 1; } @@ -225,26 +225,26 @@ int main(int argc, char *argv[]) { if(initiator) { if(connect(sock, ai->ai_addr, ai->ai_addrlen)) { - fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno)); + fprintf(stderr, "Could not connect to peer: %s\n", sockstrerror(sockerrno)); return 1; } fprintf(stderr, "Connected\n"); } else { if(bind(sock, ai->ai_addr, ai->ai_addrlen)) { - fprintf(stderr, "Could not bind socket: %s\n", strerror(errno)); + fprintf(stderr, "Could not bind socket: %s\n", sockstrerror(sockerrno)); return 1; } if(!datagram) { if(listen(sock, 1)) { - fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno)); + fprintf(stderr, "Could not listen on socket: %s\n", sockstrerror(sockerrno)); return 1; } fprintf(stderr, "Listening...\n"); sock = accept(sock, NULL, NULL); if(sock < 0) { - fprintf(stderr, "Could not accept connection: %s\n", strerror(errno)); + fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno)); return 1; } } else { @@ -255,12 +255,12 @@ int main(int argc, char *argv[]) { socklen_t addrlen = sizeof addr; if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) { - fprintf(stderr, "Could not read from socket: %s\n", strerror(errno)); + fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno)); return 1; } if(connect(sock, &addr, addrlen)) { - fprintf(stderr, "Could not accept connection: %s\n", strerror(errno)); + fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno)); return 1; } } @@ -331,7 +331,7 @@ int main(int argc, char *argv[]) { if(FD_ISSET(sock, &fds)) { ssize_t len = recv(sock, buf, sizeof buf, 0); if(len < 0) { - fprintf(stderr, "Could not read from socket: %s\n", strerror(errno)); + fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno)); return 1; } if(len == 0) { diff --git a/src/tincctl.c b/src/tincctl.c index 9fc749bc..da66c8ff 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -485,7 +485,7 @@ bool recvline(int fd, char *line, size_t len) { while(!(newline = memchr(buffer, '\n', blen))) { int result = recv(fd, buffer + blen, sizeof buffer - blen, 0); - if(result == -1 && errno == EINTR) + if(result == -1 && sockerrno == EINTR) continue; else if(result <= 0) return false; @@ -511,7 +511,7 @@ bool recvdata(int fd, char *data, size_t len) { while(blen < len) { int result = recv(fd, buffer + blen, sizeof buffer - blen, 0); - if(result == -1 && errno == EINTR) + if(result == -1 && sockerrno == EINTR) continue; else if(result <= 0) return false; @@ -543,7 +543,7 @@ bool sendline(int fd, char *format, ...) { while(blen) { int result = send(fd, p, blen, MSG_NOSIGNAL); - if(result == -1 && errno == EINTR) + if(result == -1 && sockerrno == EINTR) continue; else if(result <= 0) return false; @@ -723,7 +723,7 @@ bool connect_tincd(bool verbose) { if(getaddrinfo(host, port, &hints, &res) || !res) { if(verbose) - fprintf(stderr, "Cannot resolve %s port %s: %s", host, port, strerror(errno)); + fprintf(stderr, "Cannot resolve %s port %s: %s", host, port, sockstrerror(sockerrno)); return false; }