X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fsptps_test.c;h=38e8b50da1a16e9593692a736689a0f496faf9b0;hb=bc4df95a48857aa4ab65fb47eabd48c48d650ca0;hp=baca66b318f7e3c23d8ff181cd5532bac0191b07;hpb=373b0c12d9d0e8a3b449fd18be704e28dd6403e1;p=tinc diff --git a/src/sptps_test.c b/src/sptps_test.c index baca66b3..38e8b50d 100644 --- a/src/sptps_test.c +++ b/src/sptps_test.c @@ -23,12 +23,6 @@ #include #endif -#include - -#ifdef HAVE_WINDOWS -#include -#endif - #include "crypto.h" #include "ecdsa.h" #include "meta.h" @@ -36,6 +30,7 @@ #include "sptps.h" #include "utils.h" #include "names.h" +#include "random.h" #ifndef HAVE_WINDOWS #define closesocket(s) close(s) @@ -70,7 +65,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) { @@ -123,21 +118,43 @@ static bool receive_record(void *handle, uint8_t type, const void *data, uint16_ return true; } +typedef enum option_t { + OPT_BAD_OPTION = '?', + OPT_LONG_OPTION = 0, + + // Short options + OPT_DATAGRAM = 'd', + OPT_QUIT_ON_EOF = 'q', + OPT_READONLY = 'r', + OPT_WRITEONLY = 'w', + OPT_PACKET_LOSS = 'L', + OPT_REPLAY_WINDOW = 'W', + OPT_SPECIAL_CHAR = 's', + OPT_TUN = 't', + OPT_VERBOSE = 'v', + OPT_IPV4 = '4', + OPT_IPV6 = '6', + + // Long options + OPT_HELP = 255, +} option_t; + static struct option const long_options[] = { - {"datagram", no_argument, NULL, 'd'}, - {"quit", no_argument, NULL, 'q'}, - {"readonly", no_argument, NULL, 'r'}, - {"writeonly", no_argument, NULL, 'w'}, - {"packet-loss", required_argument, NULL, 'L'}, - {"replay-window", required_argument, NULL, 'W'}, - {"special", no_argument, NULL, 's'}, - {"verbose", required_argument, NULL, 'v'}, - {"help", no_argument, NULL, 1}, - {NULL, 0, NULL, 0} + {"datagram", no_argument, NULL, OPT_DATAGRAM}, + {"quit", no_argument, NULL, OPT_QUIT_ON_EOF}, + {"readonly", no_argument, NULL, OPT_READONLY}, + {"writeonly", no_argument, NULL, OPT_WRITEONLY}, + {"packet-loss", required_argument, NULL, OPT_PACKET_LOSS}, + {"replay-window", required_argument, NULL, OPT_REPLAY_WINDOW}, + {"special", no_argument, NULL, OPT_SPECIAL_CHAR}, + {"tun", no_argument, NULL, OPT_TUN}, + {"verbose", required_argument, NULL, OPT_VERBOSE}, + {"help", no_argument, NULL, OPT_HELP}, + {NULL, 0, NULL, 0} }; static void usage(void) { - static const char *message = + fprintf(stderr, "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n" "\n" "Valid options are:\n" @@ -155,9 +172,8 @@ static void usage(void) { " -4 Use IPv4.\n" " -6 Use IPv6.\n" "\n" - "Report bugs to tinc@tinc-vpn.org.\n"; - - fprintf(stderr, message, program_name); + "Report bugs to tinc@tinc-vpn.org.\n", + program_name); } #ifdef HAVE_WINDOWS @@ -169,7 +185,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); @@ -226,7 +242,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) { @@ -275,11 +291,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 +323,20 @@ server_err: #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; @@ -323,23 +349,27 @@ int main(int argc, char *argv[]) { bool quit = false; while((r = getopt_long(argc, argv, "dqrstwL:W:v46", long_options, &option_index)) != EOF) { - switch(r) { - case 0: /* long option */ + switch((option_t) r) { + case OPT_LONG_OPTION: break; - case 'd': /* datagram mode */ + case OPT_BAD_OPTION: + usage(); + return 1; + + case OPT_DATAGRAM: datagram = true; break; - case 'q': /* close connection on EOF from stdin */ + case OPT_QUIT_ON_EOF: quit = true; break; - case 'r': /* read only */ + case OPT_READONLY: readonly = true; break; - case 't': /* read only */ + case OPT_TUN: #ifdef HAVE_LINUX tun = true; #else @@ -349,39 +379,35 @@ int main(int argc, char *argv[]) { #endif break; - case 'w': /* write only */ + case OPT_WRITEONLY: writeonly = true; break; - case 'L': /* packet loss rate */ + case OPT_PACKET_LOSS: packetloss = atoi(optarg); break; - case 'W': /* replay window size */ + case OPT_REPLAY_WINDOW: sptps_replaywin = atoi(optarg); break; - case 'v': /* be verbose */ + case OPT_VERBOSE: verbose = true; break; - case 's': /* special character handling */ + case OPT_SPECIAL_CHAR: special = true; break; - case '?': /* wrong options */ - usage(); - return 1; - - case '4': /* IPv4 */ + case OPT_IPV4: addressfamily = AF_INET; break; - case '6': /* IPv6 */ + case OPT_IPV6: addressfamily = AF_INET6; break; - case 1: /* help */ + case OPT_HELP: usage(); return 0; @@ -490,7 +516,7 @@ int main(int argc, char *argv[]) { return 1; } - fprintf(stderr, "Listening...\n"); + print_listening_msg(sock); sock = accept(sock, NULL, NULL); @@ -499,7 +525,7 @@ int main(int argc, char *argv[]) { return 1; } } else { - fprintf(stderr, "Listening...\n"); + print_listening_msg(sock); char buf[65536]; struct sockaddr addr; @@ -519,9 +545,6 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Connected\n"); } - crypto_init(); - prng_init(); - FILE *fp = fopen(argv[1], "r"); if(!fp) { @@ -541,14 +564,14 @@ int main(int argc, char *argv[]) { if(!fp) { fprintf(stderr, "Could not open %s: %s\n", argv[2], strerror(errno)); - free(mykey); + ecdsa_free(mykey); return 1; } ecdsa_t *hiskey = NULL; if(!(hiskey = ecdsa_read_pem_public_key(fp))) { - free(mykey); + ecdsa_free(mykey); return 1; } @@ -561,8 +584,8 @@ 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); + ecdsa_free(mykey); + ecdsa_free(hiskey); return 1; } @@ -573,8 +596,8 @@ int main(int argc, char *argv[]) { if(in < 0) { fprintf(stderr, "Could not init stdin reader thread\n"); - free(mykey); - free(hiskey); + ecdsa_free(mykey); + ecdsa_free(hiskey); return 1; } } @@ -601,8 +624,8 @@ int main(int argc, char *argv[]) { FD_SET(sock, &fds); if(select(max_fd + 1, &fds, NULL, NULL, NULL) <= 0) { - free(mykey); - free(hiskey); + ecdsa_free(mykey); + ecdsa_free(hiskey); return 1; } @@ -615,8 +638,8 @@ int main(int argc, char *argv[]) { if(len < 0) { fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno)); - free(mykey); - free(hiskey); + ecdsa_free(mykey); + ecdsa_free(hiskey); return 1; } @@ -647,8 +670,8 @@ int main(int argc, char *argv[]) { 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) : (size_t)len)) { - free(mykey); - free(hiskey); + ecdsa_free(mykey); + ecdsa_free(hiskey); return 1; } } @@ -658,8 +681,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); + ecdsa_free(mykey); + ecdsa_free(hiskey); return 1; } @@ -669,7 +692,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); } @@ -689,8 +712,8 @@ int main(int argc, char *argv[]) { if(!done) { if(!datagram) { - free(mykey); - free(hiskey); + ecdsa_free(mykey); + ecdsa_free(hiskey); return 1; } } @@ -703,14 +726,21 @@ int main(int argc, char *argv[]) { bool stopped = sptps_stop(&s); - free(mykey); - free(hiskey); + ecdsa_free(mykey); + ecdsa_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; }