If regex.h is not present (Windows), use alternative code

This commit is contained in:
Dmitri Tikhonov 2018-05-18 09:18:06 -04:00
parent 07354a9a23
commit 5dc9444531
4 changed files with 47 additions and 6 deletions

View file

@ -14,6 +14,9 @@ CHECK_SYMBOL_EXISTS(
HAVE_IP_DONTFRAG 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) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/test_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/test_config.h)

View file

@ -83,15 +83,26 @@ void
prog_print_common_options (const struct prog *prog, FILE *out) prog_print_common_options (const struct prog *prog, FILE *out)
{ {
fprintf(out, fprintf(out,
#if HAVE_REGEX
" -s SERVER Server address. Takes on the form of host:port, host,\n" " -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" " 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" " 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" " used (see the -H flag). If port is not set, the default\n"
" is 443. Examples:\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" " 127.0.0.1:12345\n"
" ::1:12345\n" " ::1:443\n"
" example.com\n" " example.com\n"
" example.com:8443\n" " example.com:8443\n"
#if HAVE_REGEX
" 8443\n"
#endif
#if LSQUIC_DONTFRAG_SUPPORTED #if LSQUIC_DONTFRAG_SUPPORTED
" -D Set `do not fragment' flag on outgoing UDP packets\n" " -D Set `do not fragment' flag on outgoing UDP packets\n"
#endif #endif

View file

@ -28,7 +28,9 @@
#include <sys/queue.h> #include <sys/queue.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#if HAVE_REGEX
#include <regex.h> #include <regex.h>
#endif
#include <event2/event.h> #include <event2/event.h>
@ -213,13 +215,18 @@ struct service_port *
sport_new (const char *optarg, struct prog *prog) sport_new (const char *optarg, struct prog *prog)
{ {
struct service_port *const sport = malloc(sizeof(*sport)); struct service_port *const sport = malloc(sizeof(*sport));
#if HAVE_REGEX
regex_t re; regex_t re;
regmatch_t matches[5]; regmatch_t matches[5];
int re_code, port, e; int re_code;
const char *host;
const char *port_str; const char *port_str;
struct addrinfo hints, *res = NULL;
char errbuf[80]; char errbuf[80];
#else
char *port_str;
#endif
int port, e;
const char *host;
struct addrinfo hints, *res = NULL;
#if __linux__ #if __linux__
sport->n_dropped = 0; sport->n_dropped = 0;
sport->drop_init = 0; sport->drop_init = 0;
@ -240,6 +247,7 @@ sport_new (const char *optarg, struct prog *prog)
else else
sport->if_name[0] = '\0'; sport->if_name[0] = '\0';
#endif #endif
#if HAVE_REGEX
re_code = regcomp(&re, "^(.*):([0-9][0-9]*)$" re_code = regcomp(&re, "^(.*):([0-9][0-9]*)$"
"|^([0-9][0-9]*)$" "|^([0-9][0-9]*)$"
"|^(..*)$" "|^(..*)$"
@ -281,6 +289,20 @@ sport_new (const char *optarg, struct prog *prog)
port_str = "443"; port_str = "443";
port = 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); assert(host);
LSQ_DEBUG("host: %s; port: %d", host, port); LSQ_DEBUG("host: %s; port: %d", host, port);
if (strlen(host) > sizeof(sport->host) - 1) if (strlen(host) > sizeof(sport->host) - 1)
@ -324,8 +346,10 @@ sport_new (const char *optarg, struct prog *prog)
prog->prog_hostname = sport->host; prog->prog_hostname = sport->host;
} }
#if HAVE_REGEX
if (0 == re_code) if (0 == re_code)
regfree(&re); regfree(&re);
#endif
if (res) if (res)
freeaddrinfo(res); freeaddrinfo(res);
free(addr); free(addr);
@ -333,8 +357,10 @@ sport_new (const char *optarg, struct prog *prog)
return sport; return sport;
err: err:
#if HAVE_REGEX
if (0 == re_code) if (0 == re_code)
regfree(&re); regfree(&re);
#endif
if (res) if (res)
freeaddrinfo(res); freeaddrinfo(res);
free(sport); free(sport);

View file

@ -3,6 +3,7 @@
#cmakedefine HAVE_IP_DONTFRAG 1 #cmakedefine HAVE_IP_DONTFRAG 1
#cmakedefine HAVE_IP_MTU_DISCOVER 1 #cmakedefine HAVE_IP_MTU_DISCOVER 1
#cmakedefine HAVE_REGEX 1
#define LSQUIC_DONTFRAG_SUPPORTED (HAVE_IP_DONTFRAG || HAVE_IP_MTU_DISCOVER) #define LSQUIC_DONTFRAG_SUPPORTED (HAVE_IP_DONTFRAG || HAVE_IP_MTU_DISCOVER)