X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=test%2Fsplice.c;h=118d01185f7997753b5ded9d03f4404827d8131f;hb=b18d5b572992debb04a7851e43143711319243a0;hp=53451481178a5b5958faa9d6ec4f89dad7ac55a4;hpb=46f3eba7755089ff68fdc137b0754cae2fa523eb;p=tinc diff --git a/test/splice.c b/test/splice.c index 53451481..118d0118 100644 --- a/test/splice.c +++ b/test/splice.c @@ -17,16 +17,26 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include -#include -#include -#include -#include -#include -#include +#include "../src/system.h" #ifdef HAVE_MINGW -extern const char *winerror(int); +static const char *winerror(int err) { + static char buf[1024], *ptr; + + ptr = buf + snprintf(buf, sizeof(buf), "(%d) ", err); + + if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) { + strncpy(buf, "(unable to format errormessage)", sizeof(buf)); + }; + + if((ptr = strchr(buf, '\r'))) { + *ptr = '\0'; + } + + return buf; +} + #define strerror(x) ((x)>0?strerror(x):winerror(GetLastError())) #define sockerrno WSAGetLastError() #define sockstrerror(x) winerror(x) @@ -60,15 +70,16 @@ int main(int argc, char *argv[]) { int sock[2]; char buf[1024]; - struct addrinfo *ai, hint; - memset(&hint, 0, sizeof(hint)); + const struct addrinfo hint = { + .ai_family = AF_UNSPEC, + .ai_socktype = SOCK_STREAM, + .ai_protocol = IPPROTO_TCP, + .ai_flags = 0, + }; - hint.ai_family = AF_UNSPEC; - hint.ai_socktype = SOCK_STREAM; - hint.ai_protocol = IPPROTO_TCP; - hint.ai_flags = 0; + for(int i = 0; i < 2; i++) { + struct addrinfo *ai; - for (int i = 0; i < 2; i++) { if(getaddrinfo(argv[2 + 3 * i], argv[3 + 3 * i], &hint, &ai) || !ai) { fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno)); return 1; @@ -78,26 +89,31 @@ int main(int argc, char *argv[]) { if(sock[i] == -1) { fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno)); + freeaddrinfo(ai); return 1; } if(connect(sock[i], ai->ai_addr, ai->ai_addrlen)) { fprintf(stderr, "Could not connect to %s: %s\n", argv[i + 3 * i], sockstrerror(sockerrno)); + freeaddrinfo(ai); return 1; } + freeaddrinfo(ai); + fprintf(stderr, "Connected to %s\n", argv[1 + 3 * i]); /* Pretend to be the other one */ int len = snprintf(buf, sizeof buf, "0 %s %s\n", argv[4 - 3 * i], protocol); - if (send(sock[i], buf, len, 0) != len) { + + if(send(sock[i], buf, len, 0) != len) { fprintf(stderr, "Error sending data to %s: %s\n", argv[1 + 3 * i], sockstrerror(sockerrno)); return 1; } /* Ignore the response */ do { - if (recv(sock[i], buf, 1, 0) != 1) { + if(recv(sock[i], buf, 1, 0) != 1) { fprintf(stderr, "Error reading data from %s: %s\n", argv[1 + 3 * i], sockstrerror(sockerrno)); return 1; } @@ -118,7 +134,7 @@ int main(int argc, char *argv[]) { return 1; } - for(int i = 0; i < 2; i++ ) { + for(int i = 0; i < 2; i++) { if(FD_ISSET(sock[i], &fds)) { ssize_t len = recv(sock[i], buf, sizeof buf, 0);