X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fsptps_test.c;h=50057e29ed76b9445590d8e4cad82ce8f3dd9066;hb=9a012e485a2ed5ea5a28903d93bc625767bb20b2;hp=0f62af01dbaf318f559597aaa14b1da3c53a457f;hpb=28b7a53b693f6b4e70218a926e68a36ece54cda1;p=tinc diff --git a/src/sptps_test.c b/src/sptps_test.c index 0f62af01..50057e29 100644 --- a/src/sptps_test.c +++ b/src/sptps_test.c @@ -23,20 +23,15 @@ #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" -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS #define closesocket(s) close(s) #endif @@ -56,7 +51,6 @@ bool send_meta(struct connection_t *c, const void *msg, size_t len) { return false; } -char *logfilename = NULL; bool do_detach = false; struct timeval now; @@ -70,7 +64,7 @@ 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) { @@ -136,8 +130,6 @@ static struct option const long_options[] = { {NULL, 0, NULL, 0} }; -const char *program_name; - static void usage(void) { static const char *message = "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n" @@ -162,7 +154,7 @@ static void usage(void) { fprintf(stderr, message, program_name); } -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS int stdin_sock_fd = -1; @@ -171,7 +163,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. -static void *stdin_reader_thread(void *arg) { +static DWORD WINAPI stdin_reader_thread(LPVOID arg) { struct sockaddr_in sa; socklen_t sa_size = sizeof(sa); @@ -228,7 +220,7 @@ static void *stdin_reader_thread(void *arg) { closesocket(stdin_sock_fd); stdin_sock_fd = -1; - return NULL; + return 0; } static int start_input_reader(void) { @@ -277,11 +269,8 @@ static int start_input_reader(void) { 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; } @@ -310,7 +299,20 @@ server_err: return -1; } -#endif // HAVE_MINGW +#endif // HAVE_WINDOWS + +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); +} int main(int argc, char *argv[]) { program_name = argv[0]; @@ -430,7 +432,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)) { @@ -492,7 +494,7 @@ int main(int argc, char *argv[]) { return 1; } - fprintf(stderr, "Listening...\n"); + print_listening_msg(sock); sock = accept(sock, NULL, NULL); @@ -501,7 +503,7 @@ int main(int argc, char *argv[]) { return 1; } } else { - fprintf(stderr, "Listening...\n"); + print_listening_msg(sock); char buf[65536]; struct sockaddr addr; @@ -568,7 +570,7 @@ int main(int argc, char *argv[]) { return 1; } -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS if(!readonly) { in = start_input_reader(); @@ -609,7 +611,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); @@ -623,7 +625,7 @@ int main(int argc, char *argv[]) { } if(len == 0) { -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS shutdown(in, SD_SEND); closesocket(in); #endif @@ -671,7 +673,7 @@ 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 %ld bytes of data:\n%s\n", (long)len, hex); }