From 5dc9444531300d486a1198a064bc9075514988b7 Mon Sep 17 00:00:00 2001 From: Dmitri Tikhonov Date: Fri, 18 May 2018 09:18:06 -0400 Subject: [PATCH] If regex.h is not present (Windows), use alternative code --- test/CMakeLists.txt | 3 +++ test/prog.c | 17 ++++++++++++++--- test/test_common.c | 32 +++++++++++++++++++++++++++++--- test/test_config.h.in | 1 + 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 00f829d..1451a88 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,6 +14,9 @@ CHECK_SYMBOL_EXISTS( HAVE_IP_DONTFRAG ) +INCLUDE(CheckIncludeFiles) + +CHECK_INCLUDE_FILES(regex.h HAVE_REGEX) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/test_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/test_config.h) diff --git a/test/prog.c b/test/prog.c index 129174c..b4afa45 100644 --- a/test/prog.c +++ b/test/prog.c @@ -83,15 +83,26 @@ void prog_print_common_options (const struct prog *prog, FILE *out) { fprintf(out, +#if HAVE_REGEX " -s SERVER Server address. Takes on the form of host:port, host,\n" " or port. If host is not an IPv4 or IPv6 address, it is\n" " resolved. If host is not set, the value of SNI is\n" -" use (see the -H flag). If port is not set, the default\n" -" is 443. Examples:\n" +" used (see the -H flag). If port is not set, the default\n" +" is 443.\n" +#else +" -s SERVER Server address. Takes on the form of host:port or host.\n" +" If host is not an IPv4 or IPv6 address, it is resolved.\n" +" If port is not set, the default is 443. If -s is not\n" +" specified, the value of SNI is used (see the -H flag).\n" +#endif +" Examples:\n" " 127.0.0.1:12345\n" -" ::1:12345\n" +" ::1:443\n" " example.com\n" " example.com:8443\n" +#if HAVE_REGEX +" 8443\n" +#endif #if LSQUIC_DONTFRAG_SUPPORTED " -D Set `do not fragment' flag on outgoing UDP packets\n" #endif diff --git a/test/test_common.c b/test/test_common.c index 326f115..7e1a967 100644 --- a/test/test_common.c +++ b/test/test_common.c @@ -28,7 +28,9 @@ #include #include #include +#if HAVE_REGEX #include +#endif #include @@ -213,13 +215,18 @@ struct service_port * sport_new (const char *optarg, struct prog *prog) { struct service_port *const sport = malloc(sizeof(*sport)); +#if HAVE_REGEX regex_t re; regmatch_t matches[5]; - int re_code, port, e; - const char *host; + int re_code; const char *port_str; - struct addrinfo hints, *res = NULL; char errbuf[80]; +#else + char *port_str; +#endif + int port, e; + const char *host; + struct addrinfo hints, *res = NULL; #if __linux__ sport->n_dropped = 0; sport->drop_init = 0; @@ -240,6 +247,7 @@ sport_new (const char *optarg, struct prog *prog) else sport->if_name[0] = '\0'; #endif +#if HAVE_REGEX re_code = regcomp(&re, "^(.*):([0-9][0-9]*)$" "|^([0-9][0-9]*)$" "|^(..*)$" @@ -281,6 +289,20 @@ sport_new (const char *optarg, struct prog *prog) port_str = "443"; port = 443; } +#else + host = addr; + port_str = strrchr(addr, ':'); + if (port_str) + { + *port_str++ = '\0'; + port = atoi(port_str); + } + else + { + port_str = "443"; + port = 443; + } +#endif assert(host); LSQ_DEBUG("host: %s; port: %d", host, port); if (strlen(host) > sizeof(sport->host) - 1) @@ -324,8 +346,10 @@ sport_new (const char *optarg, struct prog *prog) prog->prog_hostname = sport->host; } +#if HAVE_REGEX if (0 == re_code) regfree(&re); +#endif if (res) freeaddrinfo(res); free(addr); @@ -333,8 +357,10 @@ sport_new (const char *optarg, struct prog *prog) return sport; err: +#if HAVE_REGEX if (0 == re_code) regfree(&re); +#endif if (res) freeaddrinfo(res); free(sport); diff --git a/test/test_config.h.in b/test/test_config.h.in index 1dba138..0d2868a 100644 --- a/test/test_config.h.in +++ b/test/test_config.h.in @@ -3,6 +3,7 @@ #cmakedefine HAVE_IP_DONTFRAG 1 #cmakedefine HAVE_IP_MTU_DISCOVER 1 +#cmakedefine HAVE_REGEX 1 #define LSQUIC_DONTFRAG_SUPPORTED (HAVE_IP_DONTFRAG || HAVE_IP_MTU_DISCOVER)