Move socket error interpretation to utils.h.

This commit is contained in:
Guus Sliepen 2009-10-25 01:40:07 +02:00
parent c11dc8079b
commit e00b44cb98
5 changed files with 34 additions and 54 deletions

View file

@ -27,6 +27,17 @@ extern void bin2hex(char *src, char *dst, int length);
#ifdef HAVE_MINGW #ifdef HAVE_MINGW
extern const char *winerror(int); extern const char *winerror(int);
#define strerror(x) ((x)>0?strerror(x):winerror(GetLastError())) #define strerror(x) ((x)>0?strerror(x):winerror(GetLastError()))
#define sockerrno WSAGetLastError()
#define sockstrerror(x) winerror(x)
#define sockwouldblock(x) ((x) == WSAEWOULDBLOCK || (x) == WSAEINTR)
#define sockmsgsize(x) ((x) == WSAEMSGSIZE)
#define sockinprogress(x) ((x) == WSAEINPROGRESS || (x) == WSAEWOULDBLOCK)
#else
#define sockerrno errno
#define sockstrerror(x) strerror(x)
#define sockwouldblock(x) ((x) == EWOULDBLOCK || (x) == EINTR)
#define sockmsgsize(x) ((x) == EMSGSIZE)
#define sockinprogress(x) ((x) == EINPROGRESS)
#endif #endif
extern unsigned int bitfield_to_int(void *bitfield, size_t size); extern unsigned int bitfield_to_int(void *bitfield, size_t size);

View file

@ -94,15 +94,13 @@ bool flush_meta(connection_t *c) {
c->name, c->hostname); c->name, c->hostname);
} else if(errno == EINTR) { } else if(errno == EINTR) {
continue; continue;
#ifdef EWOULDBLOCK } else if(sockwouldblock(sockerrno)) {
} else if(errno == EWOULDBLOCK) {
ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s) would block", ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s) would block",
c->outbuflen, c->name, c->hostname); c->outbuflen, c->name, c->hostname);
return true; return true;
#endif
} else { } else {
logger(LOG_ERR, "Flushing meta data to %s (%s) failed: %s", c->name, logger(LOG_ERR, "Flushing meta data to %s (%s) failed: %s", c->name,
c->hostname, strerror(errno)); c->hostname, sockstrerror(sockerrno));
} }
return false; return false;
@ -149,11 +147,11 @@ bool receive_meta(connection_t *c) {
if(!lenin || !errno) { if(!lenin || !errno) {
ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)", ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
c->name, c->hostname); c->name, c->hostname);
} else if(errno == EINTR) } else if(sockwouldblock(sockerrno))
return true; return true;
else else
logger(LOG_ERR, "Metadata socket read error for %s (%s): %s", logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
c->name, c->hostname, strerror(errno)); c->name, c->hostname, sockstrerror(sockerrno));
return false; return false;
} }

View file

@ -303,7 +303,7 @@ static void check_network_activity(fd_set * readset, fd_set * writeset) {
else { else {
ifdebug(CONNECTIONS) logger(LOG_DEBUG, ifdebug(CONNECTIONS) logger(LOG_DEBUG,
"Error while connecting to %s (%s): %s", "Error while connecting to %s (%s): %s",
c->name, c->hostname, strerror(result)); c->name, c->hostname, sockstrerror(result));
closesocket(c->socket); closesocket(c->socket);
do_outgoing_connection(c); do_outgoing_connection(c);
continue; continue;
@ -369,9 +369,8 @@ int main_loop(void) {
#endif #endif
if(r < 0) { if(r < 0) {
if(errno != EINTR && errno != EAGAIN) { if(!sockwouldblock(sockerrno)) {
logger(LOG_ERR, "Error while waiting for input: %s", logger(LOG_ERR, "Error while waiting for input: %s", sockstrerror(sockerrno));
strerror(errno));
dump_connections(); dump_connections();
return 1; return 1;
} }

View file

@ -46,18 +46,6 @@
#include "utils.h" #include "utils.h"
#include "xalloc.h" #include "xalloc.h"
#ifdef HAVE_MINGW
#include <winerror.h>
#define EMSGSIZE WSAEMSGSIZE
#define EAGAIN WSATRY_AGAIN
#define EINTR WSAEINTR
#define sockstrerror(x) winerror(x)
#define sockerrno WSAGetLastError()
#else
#define sockstrerror(x) strerror(x)
#define sockerrno errno
#endif
int keylifetime = 0; int keylifetime = 0;
int keyexpires = 0; int keyexpires = 0;
static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS]; static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
@ -457,8 +445,8 @@ static void send_udppacket(node_t *n, vpn_packet_t *origpkt) {
} }
#endif #endif
if((sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) { if(sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa)) < 0 && !sockwouldblock(sockerrno)) {
if(sockerrno == EMSGSIZE) { if(sockmsgsize(sockerrno)) {
if(n->maxmtu >= origlen) if(n->maxmtu >= origlen)
n->maxmtu = origlen - 1; n->maxmtu = origlen - 1;
if(n->mtu >= origlen) if(n->mtu >= origlen)
@ -570,7 +558,7 @@ void handle_incoming_vpn_data(int sock) {
pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen); pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
if(pkt.len < 0) { if(pkt.len < 0) {
if(sockerrno != EAGAIN && sockerrno != EINTR) if(!sockwouldblock(sockerrno))
logger(LOG_ERR, "Receiving packet failed: %s", sockstrerror(sockerrno)); logger(LOG_ERR, "Receiving packet failed: %s", sockstrerror(sockerrno));
return; return;
} }

View file

@ -36,10 +36,6 @@
#include <assert.h> #include <assert.h>
#ifdef WSAEINPROGRESS
#define EINPROGRESS WSAEINPROGRESS
#endif
/* Needed on Mac OS/X */ /* Needed on Mac OS/X */
#ifndef SOL_TCP #ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP #define SOL_TCP IPPROTO_TCP
@ -68,7 +64,7 @@ static void configure_tcp(connection_t *c) {
unsigned long arg = 1; unsigned long arg = 1;
if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) { if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
logger(LOG_ERR, "ioctlsocket for %s: WSA error %d", c->hostname, WSAGetLastError()); logger(LOG_ERR, "ioctlsocket for %s: %d", c->hostname, sockstrerror(sockerrno));
} }
#endif #endif
@ -157,8 +153,7 @@ static bool bind_to_address(connection_t *c) {
if(status) { if(status) {
logger(LOG_ERR, "Can't bind to %s/tcp: %s", node, logger(LOG_ERR, "Can't bind to %s/tcp: %s", node, sockstrerror(sockerrno));
strerror(errno));
} else ifdebug(CONNECTIONS) { } else ifdebug(CONNECTIONS) {
logger(LOG_DEBUG, "Successfully bound outgoing " logger(LOG_DEBUG, "Successfully bound outgoing "
"TCP socket to %s", node); "TCP socket to %s", node);
@ -179,7 +174,7 @@ int setup_listen_socket(const sockaddr_t *sa) {
nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP); nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
if(nfd < 0) { if(nfd < 0) {
ifdebug(STATUS) logger(LOG_ERR, "Creating metasocket failed: %s", strerror(errno)); ifdebug(STATUS) logger(LOG_ERR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
return -1; return -1;
} }
@ -204,7 +199,7 @@ int setup_listen_socket(const sockaddr_t *sa) {
if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) { if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
closesocket(nfd); closesocket(nfd);
logger(LOG_ERR, "Can't bind to interface %s: %s", iface, logger(LOG_ERR, "Can't bind to interface %s: %s", iface,
strerror(errno)); strerror(sockerrno));
return -1; return -1;
} }
#else #else
@ -215,16 +210,14 @@ int setup_listen_socket(const sockaddr_t *sa) {
if(bind(nfd, &sa->sa, SALEN(sa->sa))) { if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
closesocket(nfd); closesocket(nfd);
addrstr = sockaddr2hostname(sa); addrstr = sockaddr2hostname(sa);
logger(LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, logger(LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
strerror(errno));
free(addrstr); free(addrstr);
return -1; return -1;
} }
if(listen(nfd, 3)) { if(listen(nfd, 3)) {
closesocket(nfd); closesocket(nfd);
logger(LOG_ERR, "System call `%s' failed: %s", "listen", logger(LOG_ERR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
strerror(errno));
return -1; return -1;
} }
@ -239,7 +232,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) {
nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP); nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
if(nfd < 0) { if(nfd < 0) {
logger(LOG_ERR, "Creating UDP socket failed: %s", strerror(errno)); logger(LOG_ERR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
return -1; return -1;
} }
@ -259,8 +252,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) {
unsigned long arg = 1; unsigned long arg = 1;
if(ioctlsocket(nfd, FIONBIO, &arg) != 0) { if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
closesocket(nfd); closesocket(nfd);
logger(LOG_ERR, "Call to `%s' failed: WSA error %d", "ioctlsocket", logger(LOG_ERR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
WSAGetLastError());
return -1; return -1;
} }
} }
@ -301,8 +293,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) {
if(bind(nfd, &sa->sa, SALEN(sa->sa))) { if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
closesocket(nfd); closesocket(nfd);
addrstr = sockaddr2hostname(sa); addrstr = sockaddr2hostname(sa);
logger(LOG_ERR, "Can't bind to %s/udp: %s", addrstr, logger(LOG_ERR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
strerror(errno));
free(addrstr); free(addrstr);
return -1; return -1;
} }
@ -393,9 +384,7 @@ begin:
c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP); c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
if(c->socket == -1) { if(c->socket == -1) {
ifdebug(CONNECTIONS) logger(LOG_ERR, "Creating socket for %s failed: %s", c->hostname, ifdebug(CONNECTIONS) logger(LOG_ERR, "Creating socket for %s failed: %s", c->hostname, sockstrerror(sockerrno));
strerror(errno));
goto begin; goto begin;
} }
@ -417,18 +406,14 @@ begin:
result = connect(c->socket, &c->address.sa, SALEN(c->address.sa)); result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
if(result == -1) { if(result == -1) {
if(errno == EINPROGRESS if(sockinprogress(sockerrno)) {
#if defined(WIN32) && !defined(O_NONBLOCK)
|| WSAGetLastError() == WSAEWOULDBLOCK
#endif
) {
c->status.connecting = true; c->status.connecting = true;
return; return;
} }
closesocket(c->socket); closesocket(c->socket);
ifdebug(CONNECTIONS) logger(LOG_ERR, "%s: %s", c->hostname, strerror(errno)); ifdebug(CONNECTIONS) logger(LOG_ERR, "%s: %s", c->hostname, sockstrerror(sockerrno));
goto begin; goto begin;
} }
@ -493,8 +478,7 @@ bool handle_new_meta_connection(int sock) {
fd = accept(sock, &sa.sa, &len); fd = accept(sock, &sa.sa, &len);
if(fd < 0) { if(fd < 0) {
logger(LOG_ERR, "Accepting a new connection failed: %s", logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
strerror(errno));
return false; return false;
} }