X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fsptps_test.c;h=249f2e4f05f3e3c8cbe02d4f6bf5daf99c650823;hb=90cde91141ec61be4354d8deab21edb8fdf01022;hp=5e785b3af96e0e9dafb4f9a156da3b8939cbd6bb;hpb=3b6d366005b6fc23c705b3156e365316f6ab776c;p=tinc diff --git a/src/sptps_test.c b/src/sptps_test.c index 5e785b3a..249f2e4f 100644 --- a/src/sptps_test.c +++ b/src/sptps_test.c @@ -1,6 +1,6 @@ /* sptps_test.c -- Simple Peer-to-Peer Security test program - Copyright (C) 2011-2014 Guus Sliepen + Copyright (C) 2011-2022 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,38 +23,35 @@ #include #endif -#include - -#ifdef HAVE_MINGW -#include -#endif - #include "crypto.h" #include "ecdsa.h" +#include "meta.h" +#include "protocol.h" #include "sptps.h" #include "utils.h" +#include "names.h" +#include "random.h" -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS #define closesocket(s) close(s) #endif // Symbols necessary to link with logger.o -bool send_request(void *c, const char *msg, ...) { +bool send_request(struct connection_t *c, const char *msg, ...) { (void)c; (void)msg; return false; } -struct list_t *connection_list = NULL; +list_t connection_list; -bool send_meta(void *c, const char *msg, int len) { +bool send_meta(struct connection_t *c, const void *msg, size_t len) { (void)c; (void)msg; (void)len; return false; } -char *logfilename = NULL; bool do_detach = false; struct timeval now; @@ -64,21 +61,30 @@ static bool readonly; static bool writeonly; static int in = 0; static int out = 1; -static int addressfamily = AF_UNSPEC; +int addressfamily = AF_UNSPEC; static bool send_data(void *handle, uint8_t type, const void *data, size_t len) { (void)type; - char hex[len * 2 + 1]; + char *hex = alloca(len * 2 + 1); bin2hex(data, hex, len); if(verbose) { - fprintf(stderr, "Sending %zu bytes of data:\n%s\n", len, hex); + fprintf(stderr, "Sending %lu bytes of data:\n%s\n", (unsigned long)len, hex); } const int *sock = handle; + const char *p = data; - if((size_t)send(*sock, data, len, 0) != len) { - return false; + while(len) { + ssize_t sent = send(*sock, p, len, 0); + + if(sent <= 0) { + fprintf(stderr, "Error sending data: %s\n", strerror(errno)); + return false; + } + + p += sent; + len -= sent; } return true; @@ -91,8 +97,22 @@ static bool receive_record(void *handle, uint8_t type, const void *data, uint16_ fprintf(stderr, "Received type %d record of %u bytes:\n", type, len); } - if(!writeonly) { - write(out, data, len); + if(writeonly) { + return true; + } + + const char *p = data; + + while(len) { + ssize_t written = write(out, p, len); + + if(written <= 0) { + fprintf(stderr, "Error writing received data: %s\n", strerror(errno)); + return false; + } + + p += written; + len -= written; } return true; @@ -111,11 +131,11 @@ static struct option const long_options[] = { {NULL, 0, NULL, 0} }; -const char *program_name; - -static void usage() { - fprintf(stderr, "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n\n", program_name); - fprintf(stderr, "Valid options are:\n" +static void usage(void) { + static const char *message = + "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n" + "\n" + "Valid options are:\n" " -d, --datagram Enable datagram mode.\n" " -q, --quit Quit when EOF occurs on stdin.\n" " -r, --readonly Only send data from the socket to stdout.\n" @@ -129,11 +149,13 @@ static void usage() { " -v, --verbose Display debug messages.\n" " -4 Use IPv4.\n" " -6 Use IPv6.\n" - "\n"); - fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n"); + "\n" + "Report bugs to tinc@tinc-vpn.org.\n"; + + fprintf(stderr, message, program_name); } -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS int stdin_sock_fd = -1; @@ -142,7 +164,7 @@ int stdin_sock_fd = -1; // separate thread between the stdin and the sptps loop way below. This thread // reads stdin and sends its content to the main thread through a TCP socket, // which can be properly select()'ed. -void *stdin_reader_thread(void *arg) { +static DWORD WINAPI stdin_reader_thread(LPVOID arg) { struct sockaddr_in sa; socklen_t sa_size = sizeof(sa); @@ -158,7 +180,7 @@ void *stdin_reader_thread(void *arg) { fprintf(stderr, "New connection received from :%d\n", ntohs(sa.sin_port)); } - uint8_t buf[1024]; + char buf[1024]; ssize_t nread; while((nread = read(STDIN_FILENO, buf, sizeof(buf))) > 0) { @@ -166,7 +188,7 @@ void *stdin_reader_thread(void *arg) { fprintf(stderr, "Read %lld bytes from input\n", nread); } - uint8_t *start = buf; + char *start = buf; ssize_t nleft = nread; while(nleft) { @@ -199,9 +221,10 @@ void *stdin_reader_thread(void *arg) { closesocket(stdin_sock_fd); stdin_sock_fd = -1; + return 0; } -int start_input_reader() { +static int start_input_reader(void) { if(stdin_sock_fd != -1) { fprintf(stderr, "stdin thread can only be started once.\n"); return -1; @@ -247,11 +270,8 @@ int start_input_reader() { fprintf(stderr, "stdin thread is listening on :%d\n", ntohs(connect_sa.sin_port)); } - pthread_t th; - int err = pthread_create(&th, NULL, stdin_reader_thread, NULL); - - if(err) { - fprintf(stderr, "Could not start reader thread: %s\n", strerror(err)); + if(!CreateThread(NULL, 0, stdin_reader_thread, NULL, 0, NULL)) { + fprintf(stderr, "Could not start reader thread: %d\n", GetLastError()); goto server_err; } @@ -280,9 +300,22 @@ server_err: return -1; } -#endif // HAVE_MINGW +#endif // HAVE_WINDOWS -int main(int argc, char *argv[]) { +static void print_listening_msg(int sock) { + sockaddr_t sa = {0}; + socklen_t salen = sizeof(sa); + int port = 0; + + if(!getsockname(sock, &sa.sa, &salen)) { + port = ntohs(sa.in.sin_port); + } + + fprintf(stderr, "Listening on %d...\n", port); + fflush(stderr); +} + +static int run_test(int argc, char *argv[]) { program_name = argv[0]; bool initiator = false; bool datagram = false; @@ -375,8 +408,6 @@ int main(int argc, char *argv[]) { initiator = true; } - srand(getpid()); - #ifdef HAVE_LINUX if(tun) { @@ -402,7 +433,7 @@ int main(int argc, char *argv[]) { #endif -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS static struct WSAData wsa_state; if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) { @@ -464,7 +495,7 @@ int main(int argc, char *argv[]) { return 1; } - fprintf(stderr, "Listening...\n"); + print_listening_msg(sock); sock = accept(sock, NULL, NULL); @@ -473,9 +504,9 @@ int main(int argc, char *argv[]) { return 1; } } else { - fprintf(stderr, "Listening...\n"); + print_listening_msg(sock); - uint8_t buf[65536]; + char buf[65536]; struct sockaddr addr; socklen_t addrlen = sizeof(addr); @@ -493,8 +524,6 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Connected\n"); } - crypto_init(); - FILE *fp = fopen(argv[1], "r"); if(!fp) { @@ -539,7 +568,7 @@ int main(int argc, char *argv[]) { return 1; } -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS if(!readonly) { in = start_input_reader(); @@ -580,7 +609,7 @@ int main(int argc, char *argv[]) { } if(FD_ISSET(in, &fds)) { -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS ssize_t len = recv(in, buf, readsize, 0); #else ssize_t len = read(in, buf, readsize); @@ -594,7 +623,7 @@ int main(int argc, char *argv[]) { } if(len == 0) { -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS shutdown(in, SD_SEND); closesocket(in); #endif @@ -642,12 +671,12 @@ int main(int argc, char *argv[]) { } if(verbose) { - char hex[len * 2 + 1]; + char *hex = alloca(len * 2 + 1); bin2hex(buf, hex, len); - fprintf(stderr, "Received %zd bytes of data:\n%s\n", len, hex); + fprintf(stderr, "Received %ld bytes of data:\n%s\n", (long)len, hex); } - if(packetloss && (rand() % 100) < packetloss) { + if(packetloss && (int)prng(100) < packetloss) { if(verbose) { fprintf(stderr, "Dropped.\n"); } @@ -678,12 +707,19 @@ int main(int argc, char *argv[]) { free(mykey); free(hiskey); + closesocket(sock); - if(!stopped) { - return 1; - } + return !stopped; +} - closesocket(sock); +int main(int argc, char *argv[]) { + random_init(); + crypto_init(); + prng_init(); - return 0; + int result = run_test(argc, argv); + + random_exit(); + + return result; }