From: Guus Sliepen Date: Fri, 30 Aug 2013 11:04:14 +0000 (+0200) Subject: Add an option to test datagram SPTPS with packet loss. X-Git-Tag: import-tinc-1.1~84 X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=d0aa0817d2387e89555ed090d900f61c56b19caa;p=meshlink Add an option to test datagram SPTPS with packet loss. --- diff --git a/src/sptps_test.c b/src/sptps_test.c index 2ce98044..6f6be4b0 100644 --- a/src/sptps_test.c +++ b/src/sptps_test.c @@ -19,6 +19,8 @@ #include "system.h" +#include + #include "crypto.h" #include "ecdsa.h" #include "sptps.h" @@ -49,24 +51,72 @@ static bool receive_record(void *handle, uint8_t type, const char *data, uint16_ return true; } +static struct option const long_options[] = { + {"datagram", no_argument, NULL, 'd'}, + {"packet-loss", required_argument, NULL, 'l'}, + {"help", no_argument, NULL, 1}, + {NULL, 0, NULL, 0} +}; + +const char *program_name; + +static void usage() { + fprintf(stderr, "Usage: %s [options] my_ecdsa_key_file his_ecdsa_key_file [host] port\n\n", program_name); + fprintf(stderr, "Valid options are:\n" + " -d, --datagram Enable datagram mode.\n" + " -l, --packet-loss RATE Fake packet loss of RATE percent.\n" + "\n"); + fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n"); +} + int main(int argc, char *argv[]) { + program_name = argv[0]; bool initiator = false; bool datagram = false; + int packetloss = 0; + int r; + int option_index = 0; + + while((r = getopt_long(argc, argv, "dl:", long_options, &option_index)) != EOF) { + switch (r) { + case 0: /* long option */ + break; + + case 'd': /* datagram mode */ + datagram = true; + break; + + case 'l': /* packet loss rate */ + packetloss = atoi(optarg); + break; - if(argc > 1 && !strcmp(argv[1], "-d")) { - datagram = true; - argc--; - argv++; + case '?': /* wrong options */ + usage(); + return 1; + + case 1: /* help */ + usage(); + return 0; + + default: + break; + } } - if(argc < 4) { - fprintf(stderr, "Usage: %s [-d] my_ecdsa_key_file his_ecdsa_key_file [host] port\n", argv[0]); + argc -= optind - 1; + argv += optind - 1; + + if(argc < 4 || argc > 5) { + fprintf(stderr, "Wrong number of arguments.\n"); + usage(); return 1; } if(argc > 4) initiator = true; + srand(time(NULL)); + #ifdef HAVE_MINGW static struct WSAData wsa_state; if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) @@ -202,6 +252,10 @@ int main(int argc, char *argv[]) { char hex[len * 2 + 1]; bin2hex(buf, hex, len); fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex); + if((rand() % 100) < packetloss) { + fprintf(stderr, "Dropped.\n"); + continue; + } if(!sptps_receive_data(&s, buf, len)) return 1; }