X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fsptps_test.c;h=3406a97ebd1e8db918d82cdc3d875763385cafc7;hb=04d8a8e34e1fe7f33f1946863b36a24ee358175f;hp=0f75c242a0bedf07c579e63718f9d44853bd4b07;hpb=2d268fc06bc01419e7f7f32d4da1b560e72e4ad3;p=tinc diff --git a/src/sptps_test.c b/src/sptps_test.c index 0f75c242..3406a97e 100644 --- a/src/sptps_test.c +++ b/src/sptps_test.c @@ -25,19 +25,39 @@ #include +#ifdef HAVE_MINGW +#include +#endif + #include "crypto.h" #include "ecdsa.h" #include "sptps.h" #include "utils.h" +#ifndef MAX +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#endif + +#ifndef HAVE_MINGW +#define closesocket(s) close(s) +#endif + // Symbols necessary to link with logger.o bool send_request(void *c, const char *msg, ...) { + (void)c; + (void)msg; return false; } + struct list_t *connection_list = NULL; + bool send_meta(void *c, const char *msg, int len) { + (void)c; + (void)msg; + (void)len; return false; } + char *logfilename = NULL; bool do_detach = false; struct timeval now; @@ -51,16 +71,17 @@ static int out = 1; static 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]; bin2hex(data, hex, len); if(verbose) { - fprintf(stderr, "Sending %d bytes of data:\n%s\n", (int)len, hex); + fprintf(stderr, "Sending %zu bytes of data:\n%s\n", len, hex); } const int *sock = handle; - if(send(*sock, data, len, 0) != len) { + if((size_t)send(*sock, data, len, 0) != len) { return false; } @@ -68,6 +89,8 @@ static bool send_data(void *handle, uint8_t type, const void *data, size_t len) } static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) { + (void)handle; + if(verbose) { fprintf(stderr, "Received type %d record of %u bytes:\n", type, len); } @@ -114,6 +137,155 @@ static void usage() { fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n"); } +#ifdef HAVE_MINGW + +int stdin_sock_fd = -1; + +// Windows does not allow calling select() on anything but sockets. Therefore, +// to keep the same code as on other operating systems, we have to put a +// 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) { + struct sockaddr_in sa; + socklen_t sa_size = sizeof(sa); + + while(true) { + int peer_fd = accept(stdin_sock_fd, (struct sockaddr *) &sa, &sa_size); + + if(peer_fd < 0) { + fprintf(stderr, "accept() failed: %s\n", strerror(errno)); + continue; + } + + if(verbose) { + fprintf(stderr, "New connection received from :%d\n", ntohs(sa.sin_port)); + } + + uint8_t buf[1024]; + ssize_t nread; + + while((nread = read(STDIN_FILENO, buf, sizeof(buf))) > 0) { + if(verbose) { + fprintf(stderr, "Read %lld bytes from input\n", nread); + } + + uint8_t *start = buf; + ssize_t nleft = nread; + + while(nleft) { + ssize_t nsend = send(peer_fd, start, nleft, 0); + + if(nsend < 0) { + if(sockwouldblock(sockerrno)) { + continue; + } + + break; + } + + start += nsend; + nleft -= nsend; + } + + if(nleft) { + fprintf(stderr, "Could not send data: %s\n", strerror(errno)); + break; + } + + if(verbose) { + fprintf(stderr, "Sent %lld bytes to peer\n", nread); + } + } + + closesocket(peer_fd); + } + + closesocket(stdin_sock_fd); + stdin_sock_fd = -1; +} + +int start_input_reader() { + if(stdin_sock_fd != -1) { + fprintf(stderr, "stdin thread can only be started once.\n"); + return -1; + } + + stdin_sock_fd = socket(AF_INET, SOCK_STREAM, 0); + + if(stdin_sock_fd < 0) { + fprintf(stderr, "Could not create server socket: %s\n", strerror(errno)); + return -1; + } + + struct sockaddr_in serv_sa; + + memset(&serv_sa, 0, sizeof(serv_sa)); + + serv_sa.sin_family = AF_INET; + + serv_sa.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1 + + int res = bind(stdin_sock_fd, (struct sockaddr *)&serv_sa, sizeof(serv_sa)); + + if(res < 0) { + fprintf(stderr, "Could not bind socket: %s\n", strerror(errno)); + goto server_err; + } + + if(listen(stdin_sock_fd, 1) < 0) { + fprintf(stderr, "Could not listen: %s\n", strerror(errno)); + goto server_err; + } + + struct sockaddr_in connect_sa; + + socklen_t addr_len = sizeof(connect_sa); + + if(getsockname(stdin_sock_fd, (struct sockaddr *)&connect_sa, &addr_len) < 0) { + fprintf(stderr, "Could not determine the address of the stdin thread socket\n"); + goto server_err; + } + + if(verbose) { + 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)); + goto server_err; + } + + int client_fd = socket(AF_INET, SOCK_STREAM, 0); + + if(client_fd < 0) { + fprintf(stderr, "Could not create client socket: %s\n", strerror(errno)); + return -1; + } + + if(connect(client_fd, (struct sockaddr *)&connect_sa, sizeof(connect_sa)) < 0) { + fprintf(stderr, "Could not connect: %s\n", strerror(errno)); + closesocket(client_fd); + return -1; + } + + return client_fd; + +server_err: + + if(stdin_sock_fd != -1) { + closesocket(stdin_sock_fd); + stdin_sock_fd = -1; + } + + return -1; +} + +#endif // HAVE_MINGW + int main(int argc, char *argv[]) { program_name = argv[0]; bool initiator = false; @@ -124,7 +296,6 @@ int main(int argc, char *argv[]) { int packetloss = 0; int r; int option_index = 0; - ecdsa_t *mykey = NULL, *hiskey = NULL; bool quit = false; while((r = getopt_long(argc, argv, "dqrstwL:W:v46", long_options, &option_index)) != EOF) { @@ -208,7 +379,7 @@ int main(int argc, char *argv[]) { initiator = true; } - srand(time(NULL)); + srand(getpid()); #ifdef HAVE_LINUX @@ -261,6 +432,7 @@ int main(int argc, char *argv[]) { if(sock < 0) { fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno)); + freeaddrinfo(ai); return 1; } @@ -268,14 +440,24 @@ int main(int argc, char *argv[]) { setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one)); if(initiator) { - if(connect(sock, ai->ai_addr, ai->ai_addrlen)) { + int res = connect(sock, ai->ai_addr, ai->ai_addrlen); + + freeaddrinfo(ai); + ai = NULL; + + if(res) { 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)) { + int res = bind(sock, ai->ai_addr, ai->ai_addrlen); + + freeaddrinfo(ai); + ai = NULL; + + if(res) { fprintf(stderr, "Could not bind socket: %s\n", sockstrerror(sockerrno)); return 1; } @@ -297,7 +479,7 @@ int main(int argc, char *argv[]) { } else { fprintf(stderr, "Listening...\n"); - char buf[65536]; + uint8_t buf[65536]; struct sockaddr addr; socklen_t addrlen = sizeof(addr); @@ -324,6 +506,8 @@ int main(int argc, char *argv[]) { return 1; } + ecdsa_t *mykey = NULL; + if(!(mykey = ecdsa_read_pem_private_key(fp))) { return 1; } @@ -334,10 +518,14 @@ int main(int argc, char *argv[]) { if(!fp) { fprintf(stderr, "Could not open %s: %s\n", argv[2], strerror(errno)); + free(mykey); return 1; } + ecdsa_t *hiskey = NULL; + if(!(hiskey = ecdsa_read_pem_public_key(fp))) { + free(mykey); return 1; } @@ -350,40 +538,71 @@ int main(int argc, char *argv[]) { sptps_t s; if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record)) { + free(mykey); + free(hiskey); return 1; } +#ifdef HAVE_MINGW + + if(!readonly) { + in = start_input_reader(); + + if(in < 0) { + fprintf(stderr, "Could not init stdin reader thread\n"); + free(mykey); + free(hiskey); + return 1; + } + } + +#endif + + int max_fd = MAX(sock, in); + while(true) { if(writeonly && readonly) { break; } char buf[65535] = ""; + size_t readsize = datagram ? 1460u : sizeof(buf); fd_set fds; FD_ZERO(&fds); -#ifndef HAVE_MINGW if(!readonly && s.instate) { FD_SET(in, &fds); } -#endif FD_SET(sock, &fds); - if(select(sock + 1, &fds, NULL, NULL, NULL) <= 0) { + if(select(max_fd + 1, &fds, NULL, NULL, NULL) <= 0) { + free(mykey); + free(hiskey); return 1; } if(FD_ISSET(in, &fds)) { - ssize_t len = read(in, buf, sizeof(buf)); +#ifdef HAVE_MINGW + ssize_t len = recv(in, buf, readsize, 0); +#else + ssize_t len = read(in, buf, readsize); +#endif if(len < 0) { fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno)); + free(mykey); + free(hiskey); return 1; } if(len == 0) { +#ifdef HAVE_MINGW + shutdown(in, SD_SEND); + closesocket(in); +#endif + if(quit) { break; } @@ -404,7 +623,9 @@ int main(int argc, char *argv[]) { if(len > 1) { sptps_send_record(&s, 0, buf, len); } - } else if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof(buf) : len)) { + } else if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof(buf) : (size_t)len)) { + free(mykey); + free(hiskey); return 1; } } @@ -414,6 +635,8 @@ int main(int argc, char *argv[]) { if(len < 0) { fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno)); + free(mykey); + free(hiskey); return 1; } @@ -425,7 +648,7 @@ int main(int argc, char *argv[]) { if(verbose) { char hex[len * 2 + 1]; bin2hex(buf, hex, len); - fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex); + fprintf(stderr, "Received %zd bytes of data:\n%s\n", len, hex); } if(packetloss && (rand() % 100) < packetloss) { @@ -443,19 +666,28 @@ int main(int argc, char *argv[]) { if(!done) { if(!datagram) { + free(mykey); + free(hiskey); return 1; } } bufp += done; - len -= done; + len -= (ssize_t) done; } } } - if(!sptps_stop(&s)) { + bool stopped = sptps_stop(&s); + + free(mykey); + free(hiskey); + + if(!stopped) { return 1; } + closesocket(sock); + return 0; }