2020-01-09 16:52:25 +00:00
|
|
|
/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */
|
2017-09-22 21:00:03 +00:00
|
|
|
#include <assert.h>
|
2018-03-12 22:25:01 +00:00
|
|
|
#ifndef WIN32
|
2018-05-16 16:14:02 +00:00
|
|
|
#include <arpa/inet.h>
|
2017-09-22 21:00:03 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <signal.h>
|
2018-03-12 22:25:01 +00:00
|
|
|
#endif
|
2019-09-11 15:27:58 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2017-09-22 21:00:03 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-09-11 15:27:58 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2017-09-22 21:00:03 +00:00
|
|
|
#include <sys/queue.h>
|
2018-03-12 22:25:01 +00:00
|
|
|
#ifndef WIN32
|
2017-09-22 21:00:03 +00:00
|
|
|
#include <unistd.h>
|
2018-03-12 22:25:01 +00:00
|
|
|
#else
|
|
|
|
#include <getopt.h>
|
|
|
|
#pragma warning(disable:4028)
|
|
|
|
#endif// WIN32
|
2017-09-22 21:00:03 +00:00
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
|
|
|
#include <lsquic.h>
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
2017-09-22 21:00:03 +00:00
|
|
|
#include "../src/liblsquic/lsquic_hash.h"
|
2019-09-11 15:27:58 +00:00
|
|
|
#include "../src/liblsquic/lsquic_int_types.h"
|
|
|
|
#include "../src/liblsquic/lsquic_util.h"
|
2017-09-22 21:00:03 +00:00
|
|
|
#include "../src/liblsquic/lsquic_logger.h"
|
|
|
|
|
2017-09-26 15:26:05 +00:00
|
|
|
#include "test_config.h"
|
2019-09-11 15:27:58 +00:00
|
|
|
#include "test_cert.h"
|
2017-09-22 21:00:03 +00:00
|
|
|
#include "test_common.h"
|
|
|
|
#include "prog.h"
|
|
|
|
|
|
|
|
static int prog_stopped;
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
static SSL_CTX * get_ssl_ctx (void *);
|
|
|
|
|
2017-09-22 21:00:03 +00:00
|
|
|
static const struct lsquic_packout_mem_if pmi = {
|
|
|
|
.pmi_allocate = pba_allocate,
|
|
|
|
.pmi_release = pba_release,
|
2018-10-16 13:03:33 +00:00
|
|
|
.pmi_return = pba_release,
|
2017-09-22 21:00:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
prog_init (struct prog *prog, unsigned flags,
|
|
|
|
struct sport_head *sports,
|
|
|
|
const struct lsquic_stream_if *stream_if, void *stream_if_ctx)
|
|
|
|
{
|
|
|
|
/* prog-specific initialization: */
|
|
|
|
memset(prog, 0, sizeof(*prog));
|
|
|
|
prog->prog_engine_flags = flags;
|
|
|
|
prog->prog_sports = sports;
|
|
|
|
lsquic_engine_init_settings(&prog->prog_settings, flags);
|
2019-09-11 15:27:58 +00:00
|
|
|
#if ECN_SUPPORTED
|
|
|
|
prog->prog_settings.es_ecn = LSQUIC_DF_ECN;
|
|
|
|
#else
|
|
|
|
prog->prog_settings.es_ecn = 0;
|
|
|
|
#endif
|
2017-09-22 21:00:03 +00:00
|
|
|
|
|
|
|
prog->prog_api.ea_settings = &prog->prog_settings;
|
|
|
|
prog->prog_api.ea_stream_if = stream_if;
|
|
|
|
prog->prog_api.ea_stream_if_ctx = stream_if_ctx;
|
|
|
|
prog->prog_api.ea_packets_out = sport_packets_out;
|
|
|
|
prog->prog_api.ea_packets_out_ctx
|
|
|
|
= prog;
|
|
|
|
prog->prog_api.ea_pmi = &pmi;
|
|
|
|
prog->prog_api.ea_pmi_ctx = &prog->prog_pba;
|
2019-09-11 15:27:58 +00:00
|
|
|
prog->prog_api.ea_get_ssl_ctx = get_ssl_ctx;
|
|
|
|
#if LSQUIC_PREFERRED_ADDR
|
|
|
|
if (getenv("LSQUIC_PREFERRED_ADDR4") || getenv("LSQUIC_PREFERRED_ADDR6"))
|
|
|
|
prog->prog_flags |= PROG_SEARCH_ADDRS;
|
|
|
|
#endif
|
2017-09-22 21:00:03 +00:00
|
|
|
|
|
|
|
/* Non prog-specific initialization: */
|
|
|
|
lsquic_global_init(flags & LSENG_SERVER ? LSQUIC_GLOBAL_SERVER :
|
|
|
|
LSQUIC_GLOBAL_CLIENT);
|
|
|
|
lsquic_log_to_fstream(stderr, LLTS_HHMMSSMS);
|
|
|
|
lsquic_logger_lopt("=notice");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
prog_add_sport (struct prog *prog, const char *arg)
|
|
|
|
{
|
|
|
|
struct service_port *sport;
|
|
|
|
sport = sport_new(arg, prog);
|
|
|
|
if (!sport)
|
|
|
|
return -1;
|
|
|
|
/* Default settings: */
|
|
|
|
sport->sp_flags = prog->prog_dummy_sport.sp_flags;
|
|
|
|
sport->sp_sndbuf = prog->prog_dummy_sport.sp_sndbuf;
|
|
|
|
sport->sp_rcvbuf = prog->prog_dummy_sport.sp_rcvbuf;
|
|
|
|
TAILQ_INSERT_TAIL(prog->prog_sports, sport, next_sport);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
prog_print_common_options (const struct prog *prog, FILE *out)
|
|
|
|
{
|
|
|
|
fprintf(out,
|
2018-05-18 13:18:06 +00:00
|
|
|
#if HAVE_REGEX
|
2019-09-11 15:27:58 +00:00
|
|
|
" -s SVCPORT Service port. Takes on the form of host:port, host,\n"
|
2018-05-16 19:48:43 +00:00
|
|
|
" 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"
|
2018-05-18 13:18:06 +00:00
|
|
|
" used (see the -H flag). If port is not set, the default\n"
|
|
|
|
" is 443.\n"
|
|
|
|
#else
|
2019-09-11 15:27:58 +00:00
|
|
|
" -s SVCPORT Service port. Takes on the form of host:port or host.\n"
|
2018-05-18 13:18:06 +00:00
|
|
|
" If host is not an IPv4 or IPv6 address, it is resolved.\n"
|
2018-05-18 14:24:20 +00:00
|
|
|
" If port is not set, the default is 443.\n"
|
2018-05-18 13:18:06 +00:00
|
|
|
#endif
|
|
|
|
" Examples:\n"
|
2018-05-16 19:48:43 +00:00
|
|
|
" 127.0.0.1:12345\n"
|
2018-05-18 13:18:06 +00:00
|
|
|
" ::1:443\n"
|
2018-05-16 19:48:43 +00:00
|
|
|
" example.com\n"
|
|
|
|
" example.com:8443\n"
|
2018-05-18 13:18:06 +00:00
|
|
|
#if HAVE_REGEX
|
|
|
|
" 8443\n"
|
|
|
|
#endif
|
2019-09-11 15:27:58 +00:00
|
|
|
" If no -s option is given, 0.0.0.0:12345 address\n"
|
|
|
|
" is used.\n"
|
2017-09-26 15:26:05 +00:00
|
|
|
#if LSQUIC_DONTFRAG_SUPPORTED
|
2020-01-28 14:35:09 +00:00
|
|
|
" -D Do not set `do not fragment' flag on outgoing UDP packets\n"
|
2017-09-26 15:26:05 +00:00
|
|
|
#endif
|
2017-09-22 21:00:03 +00:00
|
|
|
" -z BYTES Maximum size of outgoing UDP packets. The default is 1370\n"
|
|
|
|
" bytes for IPv4 socket and 1350 bytes for IPv6 socket\n"
|
|
|
|
" -L LEVEL Log level for all modules. Possible values are `debug',\n"
|
|
|
|
" `info', `notice', `warn', `error', `alert', `emerg',\n"
|
|
|
|
" and `crit'.\n"
|
|
|
|
" -l LEVELS Log levels for modules, e.g.\n"
|
|
|
|
" -l event=info,engine=debug\n"
|
|
|
|
" Can be specified more than once.\n"
|
|
|
|
" -m MAX Maximum number of outgoing packet buffers that can be\n"
|
|
|
|
" assigned at any one time. By default, there is no max.\n"
|
|
|
|
" -y style Timestamp style used in log messages. The following styles\n"
|
|
|
|
" are supported:\n"
|
|
|
|
" 0 No timestamp\n"
|
|
|
|
" 1 Millisecond time (this is the default).\n"
|
|
|
|
" Example: 11:04:05.196\n"
|
|
|
|
" 2 Full date and millisecond time.\n"
|
|
|
|
" Example: 2017-03-21 13:43:46.671\n"
|
|
|
|
" 3 Chrome-like timestamp: date/time.microseconds.\n"
|
|
|
|
" Example: 1223/104613.946956\n"
|
|
|
|
" 4 Microsecond time.\n"
|
|
|
|
" Example: 11:04:05.196308\n"
|
2018-02-26 21:01:16 +00:00
|
|
|
" 5 Full date and microsecond time.\n"
|
|
|
|
" Example: 2017-03-21 13:43:46.671345\n"
|
2017-09-22 21:00:03 +00:00
|
|
|
" -S opt=val Socket options. Supported options:\n"
|
|
|
|
" sndbuf=12345 # Sets SO_SNDBUF\n"
|
|
|
|
" rcvbuf=12345 # Sets SO_RCVBUF\n"
|
2019-09-11 15:27:58 +00:00
|
|
|
" -W Use stock PMI (malloc & free)\n"
|
2017-09-22 21:00:03 +00:00
|
|
|
);
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
#if HAVE_SENDMMSG
|
|
|
|
fprintf(out,
|
|
|
|
" -g Use sendmmsg() to send packets.\n"
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
#if HAVE_RECVMMSG
|
|
|
|
fprintf(out,
|
|
|
|
" -j Use recvmmsg() to receive packets.\n"
|
|
|
|
);
|
|
|
|
#endif
|
2017-09-22 21:00:03 +00:00
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
if (prog->prog_engine_flags & LSENG_SERVER)
|
|
|
|
fprintf(out,
|
|
|
|
" -c CERTSPEC Service specification. The specification is three values\n"
|
|
|
|
" separated by commas. The values are:\n"
|
|
|
|
" * Domain name\n"
|
|
|
|
" * File containing cert in PEM format\n"
|
|
|
|
" * File containing private key in DER or PEM format\n"
|
|
|
|
" Example:\n"
|
|
|
|
" -c www.example.com,/tmp/cert.pem,/tmp/key.pkcs8\n"
|
|
|
|
);
|
|
|
|
else
|
2017-09-22 21:00:03 +00:00
|
|
|
{
|
|
|
|
if (prog->prog_engine_flags & LSENG_HTTP)
|
|
|
|
fprintf(out,
|
2018-05-16 19:48:43 +00:00
|
|
|
" -H host Value of `host' HTTP header. This is also used as SNI\n"
|
|
|
|
" in Client Hello. This option is used to override the\n"
|
|
|
|
" `host' part of the address specified using -s flag.\n"
|
2017-09-22 21:00:03 +00:00
|
|
|
);
|
|
|
|
else
|
|
|
|
fprintf(out,
|
|
|
|
" -H host Value of SNI in CHLO.\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
fprintf(out,
|
|
|
|
" -G dir SSL keys will be logged to files in this directory.\n"
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
2017-09-22 21:00:03 +00:00
|
|
|
|
2019-01-28 20:41:28 +00:00
|
|
|
fprintf(out,
|
2019-02-18 13:40:51 +00:00
|
|
|
" -k Connect UDP socket. Only meant to be used with clients\n"
|
|
|
|
" to pick up ICMP errors.\n"
|
2019-01-28 20:41:28 +00:00
|
|
|
" -i USECS Clock granularity in microseconds. Defaults to %u.\n",
|
|
|
|
LSQUIC_DF_CLOCK_GRANULARITY
|
|
|
|
);
|
2017-09-22 21:00:03 +00:00
|
|
|
fprintf(out,
|
|
|
|
" -h Print this help screen and exit\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
prog_set_opt (struct prog *prog, int opt, const char *arg)
|
|
|
|
{
|
2019-09-11 15:27:58 +00:00
|
|
|
struct stat st;
|
|
|
|
int s;
|
|
|
|
|
2017-09-22 21:00:03 +00:00
|
|
|
switch (opt)
|
|
|
|
{
|
2017-09-26 15:26:05 +00:00
|
|
|
#if LSQUIC_DONTFRAG_SUPPORTED
|
2017-09-22 21:00:03 +00:00
|
|
|
case 'D':
|
|
|
|
{
|
|
|
|
struct service_port *sport = TAILQ_LAST(prog->prog_sports, sport_head);
|
|
|
|
if (!sport)
|
|
|
|
sport = &prog->prog_dummy_sport;
|
2020-01-28 14:35:09 +00:00
|
|
|
sport->sp_flags |= SPORT_FRAGMENT_OK;
|
2017-09-22 21:00:03 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2019-09-11 15:27:58 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_SENDMMSG
|
|
|
|
case 'g':
|
|
|
|
prog->prog_use_sendmmsg = 1;
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
#if HAVE_RECVMMSG
|
|
|
|
case 'j':
|
|
|
|
prog->prog_use_recvmmsg = 1;
|
|
|
|
return 0;
|
2017-09-26 15:26:05 +00:00
|
|
|
#endif
|
2017-09-22 21:00:03 +00:00
|
|
|
case 'm':
|
|
|
|
prog->prog_packout_max = atoi(arg);
|
|
|
|
return 0;
|
|
|
|
case 'z':
|
|
|
|
prog->prog_max_packet_size = atoi(arg);
|
|
|
|
return 0;
|
2019-09-11 15:27:58 +00:00
|
|
|
case 'W':
|
|
|
|
prog->prog_use_stock_pmi = 1;
|
|
|
|
return 0;
|
|
|
|
case 'c':
|
|
|
|
if (prog->prog_engine_flags & LSENG_SERVER)
|
|
|
|
{
|
|
|
|
if (!prog->prog_certs)
|
|
|
|
prog->prog_certs = lsquic_hash_create();
|
|
|
|
return load_cert(prog->prog_certs, arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
2017-09-22 21:00:03 +00:00
|
|
|
case 'H':
|
|
|
|
if (prog->prog_engine_flags & LSENG_SERVER)
|
|
|
|
return -1;
|
|
|
|
prog->prog_hostname = arg;
|
|
|
|
return 0;
|
|
|
|
case 'y':
|
|
|
|
lsquic_log_to_fstream(stderr, atoi(arg));
|
|
|
|
return 0;
|
|
|
|
case 'L':
|
|
|
|
return lsquic_set_log_level(arg);
|
|
|
|
case 'l':
|
|
|
|
return lsquic_logger_lopt(arg);
|
|
|
|
case 'o':
|
|
|
|
return set_engine_option(&prog->prog_settings,
|
|
|
|
&prog->prog_version_cleared, arg);
|
2019-01-28 20:41:28 +00:00
|
|
|
case 'i':
|
|
|
|
prog->prog_settings.es_clock_granularity = atoi(arg);
|
|
|
|
return 0;
|
2017-09-22 21:00:03 +00:00
|
|
|
case 's':
|
|
|
|
if (0 == (prog->prog_engine_flags & LSENG_SERVER) &&
|
|
|
|
!TAILQ_EMPTY(prog->prog_sports))
|
|
|
|
return -1;
|
|
|
|
return prog_add_sport(prog, arg);
|
|
|
|
case 'S':
|
|
|
|
{
|
|
|
|
struct service_port *sport = TAILQ_LAST(prog->prog_sports, sport_head);
|
|
|
|
if (!sport)
|
|
|
|
sport = &prog->prog_dummy_sport;
|
|
|
|
char *const name = strdup(optarg);
|
|
|
|
char *val = strchr(name, '=');
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
free(name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*val = '\0';
|
|
|
|
++val;
|
|
|
|
if (0 == strcasecmp(name, "sndbuf"))
|
|
|
|
{
|
|
|
|
sport->sp_flags |= SPORT_SET_SNDBUF;
|
|
|
|
sport->sp_sndbuf = atoi(val);
|
|
|
|
free(name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (0 == strcasecmp(name, "rcvbuf"))
|
|
|
|
{
|
|
|
|
sport->sp_flags |= SPORT_SET_RCVBUF;
|
|
|
|
sport->sp_rcvbuf = atoi(val);
|
|
|
|
free(name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 13:40:51 +00:00
|
|
|
case 'k':
|
|
|
|
{
|
|
|
|
struct service_port *sport = TAILQ_LAST(prog->prog_sports, sport_head);
|
|
|
|
if (!sport)
|
|
|
|
sport = &prog->prog_dummy_sport;
|
|
|
|
sport->sp_flags |= SPORT_CONNECT;
|
|
|
|
}
|
|
|
|
return 0;
|
2019-09-11 15:27:58 +00:00
|
|
|
case 'G':
|
|
|
|
#ifndef WIN32
|
|
|
|
if (0 == stat(optarg, &st))
|
|
|
|
{
|
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
{
|
|
|
|
LSQ_ERROR("%s is not a directory", optarg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s = mkdir(optarg, 0700);
|
|
|
|
if (s != 0)
|
|
|
|
{
|
|
|
|
LSQ_ERROR("cannot create directory %s: %s", optarg,
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prog->prog_keylog_dir = optarg;
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
LSQ_ERROR("key logging is not supported on Windows");
|
|
|
|
return -1;
|
|
|
|
#endif
|
2017-09-22 21:00:03 +00:00
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct event_base *
|
|
|
|
prog_eb (struct prog *prog)
|
|
|
|
{
|
|
|
|
return prog->prog_eb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2019-02-04 13:59:11 +00:00
|
|
|
prog_connect (struct prog *prog, unsigned char *zero_rtt, size_t zero_rtt_len)
|
2017-09-22 21:00:03 +00:00
|
|
|
{
|
|
|
|
struct service_port *sport;
|
|
|
|
|
|
|
|
sport = TAILQ_FIRST(prog->prog_sports);
|
2019-10-31 16:21:14 +00:00
|
|
|
if (NULL == lsquic_engine_connect(prog->prog_engine, N_LSQVER,
|
2018-05-30 04:15:35 +00:00
|
|
|
(struct sockaddr *) &sport->sp_local_addr,
|
2018-02-26 21:01:16 +00:00
|
|
|
(struct sockaddr *) &sport->sas, sport, NULL,
|
2020-03-02 13:53:41 +00:00
|
|
|
prog->prog_hostname ? prog->prog_hostname
|
|
|
|
/* SNI is required for HTTP */
|
|
|
|
: prog->prog_engine_flags & LSENG_HTTP ? sport->host
|
|
|
|
: NULL,
|
2019-09-11 15:27:58 +00:00
|
|
|
prog->prog_max_packet_size, zero_rtt, zero_rtt_len,
|
|
|
|
sport->sp_token_buf, sport->sp_token_sz))
|
2017-09-22 21:00:03 +00:00
|
|
|
return -1;
|
|
|
|
|
2018-04-09 13:39:38 +00:00
|
|
|
prog_process_conns(prog);
|
2017-09-22 21:00:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
prog_init_client (struct prog *prog)
|
|
|
|
{
|
|
|
|
struct service_port *sport;
|
|
|
|
|
|
|
|
sport = TAILQ_FIRST(prog->prog_sports);
|
|
|
|
if (0 != sport_init_client(sport, prog->prog_engine, prog->prog_eb))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
static SSL_CTX *
|
|
|
|
get_ssl_ctx (void *peer_ctx)
|
|
|
|
{
|
|
|
|
const struct service_port *const sport = peer_ctx;
|
|
|
|
return sport->sp_prog->prog_ssl_ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
prog_init_server (struct prog *prog)
|
|
|
|
{
|
|
|
|
struct service_port *sport;
|
|
|
|
unsigned char ticket_keys[48];
|
|
|
|
|
|
|
|
prog->prog_ssl_ctx = SSL_CTX_new(TLS_method());
|
|
|
|
if (prog->prog_ssl_ctx)
|
|
|
|
{
|
|
|
|
SSL_CTX_set_min_proto_version(prog->prog_ssl_ctx, TLS1_3_VERSION);
|
|
|
|
SSL_CTX_set_max_proto_version(prog->prog_ssl_ctx, TLS1_3_VERSION);
|
|
|
|
SSL_CTX_set_default_verify_paths(prog->prog_ssl_ctx);
|
|
|
|
|
|
|
|
/* This is obviously test code: the key is just an array of NUL bytes */
|
|
|
|
memset(ticket_keys, 0, sizeof(ticket_keys));
|
|
|
|
if (1 != SSL_CTX_set_tlsext_ticket_keys(prog->prog_ssl_ctx,
|
|
|
|
ticket_keys, sizeof(ticket_keys)))
|
|
|
|
{
|
|
|
|
LSQ_ERROR("SSL_CTX_set_tlsext_ticket_keys failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LSQ_WARN("cannot create SSL context");
|
|
|
|
|
|
|
|
TAILQ_FOREACH(sport, prog->prog_sports, next_sport)
|
|
|
|
if (0 != sport_init_server(sport, prog->prog_engine, prog->prog_eb))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-22 21:00:03 +00:00
|
|
|
void
|
2018-04-09 13:39:38 +00:00
|
|
|
prog_process_conns (struct prog *prog)
|
2017-09-22 21:00:03 +00:00
|
|
|
{
|
|
|
|
int diff;
|
2018-04-09 13:39:38 +00:00
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
lsquic_engine_process_conns(prog->prog_engine);
|
2017-09-22 21:00:03 +00:00
|
|
|
|
|
|
|
if (lsquic_engine_earliest_adv_tick(prog->prog_engine, &diff))
|
|
|
|
{
|
2019-01-28 20:41:28 +00:00
|
|
|
if (diff < 0
|
|
|
|
|| (unsigned) diff < prog->prog_settings.es_clock_granularity)
|
2018-04-09 13:39:38 +00:00
|
|
|
{
|
|
|
|
timeout.tv_sec = 0;
|
2019-01-28 20:41:28 +00:00
|
|
|
timeout.tv_usec = prog->prog_settings.es_clock_granularity;
|
2018-04-09 13:39:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timeout.tv_sec = (unsigned) diff / 1000000;
|
|
|
|
timeout.tv_usec = (unsigned) diff % 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prog_is_stopped())
|
|
|
|
event_add(prog->prog_timer, &timeout);
|
2017-09-22 21:00:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
prog_timer_handler (int fd, short what, void *arg)
|
|
|
|
{
|
|
|
|
struct prog *const prog = arg;
|
2018-04-09 13:39:38 +00:00
|
|
|
if (!prog_is_stopped())
|
|
|
|
prog_process_conns(prog);
|
2017-09-22 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
prog_usr1_handler (int fd, short what, void *arg)
|
|
|
|
{
|
|
|
|
LSQ_NOTICE("Got SIGUSR1, stopping engine");
|
|
|
|
prog_stop(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
static void
|
|
|
|
prog_usr2_handler (int fd, short what, void *arg)
|
|
|
|
{
|
|
|
|
struct prog *const prog = arg;
|
|
|
|
|
|
|
|
LSQ_NOTICE("Got SIGUSR2, cool down engine");
|
|
|
|
prog->prog_flags |= PROG_FLAG_COOLDOWN;
|
|
|
|
lsquic_engine_cooldown(prog->prog_engine);
|
2020-01-16 14:22:41 +00:00
|
|
|
prog_process_conns(prog);
|
2019-09-11 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-22 21:00:03 +00:00
|
|
|
int
|
|
|
|
prog_run (struct prog *prog)
|
|
|
|
{
|
2018-03-12 22:25:01 +00:00
|
|
|
#ifndef WIN32
|
2017-09-22 21:00:03 +00:00
|
|
|
prog->prog_usr1 = evsignal_new(prog->prog_eb, SIGUSR1,
|
|
|
|
prog_usr1_handler, prog);
|
|
|
|
evsignal_add(prog->prog_usr1, NULL);
|
2019-09-11 15:27:58 +00:00
|
|
|
prog->prog_usr2 = evsignal_new(prog->prog_eb, SIGUSR2,
|
|
|
|
prog_usr2_handler, prog);
|
|
|
|
evsignal_add(prog->prog_usr2, NULL);
|
2018-03-12 22:25:01 +00:00
|
|
|
#endif
|
2017-09-22 21:00:03 +00:00
|
|
|
|
|
|
|
event_base_loop(prog->prog_eb, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
prog_cleanup (struct prog *prog)
|
|
|
|
{
|
|
|
|
lsquic_engine_destroy(prog->prog_engine);
|
|
|
|
event_base_free(prog->prog_eb);
|
2019-09-11 15:27:58 +00:00
|
|
|
if (!prog->prog_use_stock_pmi)
|
|
|
|
pba_cleanup(&prog->prog_pba);
|
|
|
|
if (prog->prog_ssl_ctx)
|
|
|
|
SSL_CTX_free(prog->prog_ssl_ctx);
|
|
|
|
if (prog->prog_certs)
|
|
|
|
delete_certs(prog->prog_certs);
|
2017-09-22 21:00:03 +00:00
|
|
|
lsquic_global_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
prog_stop (struct prog *prog)
|
|
|
|
{
|
|
|
|
struct service_port *sport;
|
|
|
|
|
|
|
|
prog_stopped = 1;
|
|
|
|
|
|
|
|
while ((sport = TAILQ_FIRST(prog->prog_sports)))
|
|
|
|
{
|
|
|
|
TAILQ_REMOVE(prog->prog_sports, sport, next_sport);
|
|
|
|
sport_destroy(sport);
|
|
|
|
}
|
|
|
|
|
2018-02-26 21:01:16 +00:00
|
|
|
if (prog->prog_timer)
|
|
|
|
{
|
|
|
|
event_del(prog->prog_timer);
|
|
|
|
event_free(prog->prog_timer);
|
|
|
|
prog->prog_timer = NULL;
|
|
|
|
}
|
|
|
|
if (prog->prog_usr1)
|
|
|
|
{
|
|
|
|
event_del(prog->prog_usr1);
|
|
|
|
event_free(prog->prog_usr1);
|
|
|
|
prog->prog_usr1 = NULL;
|
|
|
|
}
|
2019-09-11 15:27:58 +00:00
|
|
|
if (prog->prog_usr2)
|
|
|
|
{
|
|
|
|
event_del(prog->prog_usr2);
|
|
|
|
event_free(prog->prog_usr2);
|
|
|
|
prog->prog_usr2 = NULL;
|
|
|
|
}
|
2017-09-22 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
static void *
|
|
|
|
keylog_open (void *ctx, lsquic_conn_t *conn)
|
|
|
|
{
|
|
|
|
const struct prog *const prog = ctx;
|
|
|
|
const lsquic_cid_t *cid;
|
|
|
|
FILE *fh;
|
|
|
|
int sz;
|
|
|
|
char id_str[MAX_CID_LEN * 2 + 1];
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
|
|
|
cid = lsquic_conn_id(conn);
|
|
|
|
lsquic_hexstr(cid->idbuf, cid->len, id_str, sizeof(id_str));
|
|
|
|
sz = snprintf(path, sizeof(path), "%s/%s.keys", prog->prog_keylog_dir,
|
|
|
|
id_str);
|
|
|
|
if ((size_t) sz >= sizeof(path))
|
|
|
|
{
|
|
|
|
LSQ_WARN("%s: file too long", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fh = fopen(path, "w");
|
|
|
|
if (!fh)
|
|
|
|
LSQ_WARN("could not open %s for writing: %s", path, strerror(errno));
|
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
keylog_log_line (void *handle, const char *line)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = strlen(line);
|
|
|
|
if (len < sizeof("QUIC_") - 1 || strncmp(line, "QUIC_", 5))
|
|
|
|
fputs("QUIC_", handle);
|
|
|
|
fputs(line, handle);
|
|
|
|
fputs("\n", handle);
|
|
|
|
fflush(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
keylog_close (void *handle)
|
|
|
|
{
|
|
|
|
fclose(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const struct lsquic_keylog_if keylog_if =
|
|
|
|
{
|
|
|
|
.kli_open = keylog_open,
|
|
|
|
.kli_log_line = keylog_log_line,
|
|
|
|
.kli_close = keylog_close,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-12-05 13:44:25 +00:00
|
|
|
static struct ssl_ctx_st *
|
|
|
|
no_cert (void *cert_lu_ctx, const struct sockaddr *sa_UNUSED, const char *sni)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
|
2017-09-22 21:00:03 +00:00
|
|
|
int
|
|
|
|
prog_prep (struct prog *prog)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
char err_buf[100];
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
if (prog->prog_keylog_dir)
|
|
|
|
{
|
|
|
|
prog->prog_api.ea_keylog_if = &keylog_if;
|
|
|
|
prog->prog_api.ea_keylog_ctx = prog;
|
|
|
|
}
|
|
|
|
|
2017-09-22 21:00:03 +00:00
|
|
|
if (0 != lsquic_engine_check_settings(prog->prog_api.ea_settings,
|
|
|
|
prog->prog_engine_flags, err_buf, sizeof(err_buf)))
|
|
|
|
{
|
|
|
|
LSQ_ERROR("Error in settings: %s", err_buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
if (!prog->prog_use_stock_pmi)
|
|
|
|
pba_init(&prog->prog_pba, prog->prog_packout_max);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prog->prog_api.ea_pmi = NULL;
|
|
|
|
prog->prog_api.ea_pmi_ctx = NULL;
|
|
|
|
}
|
2017-09-22 21:00:03 +00:00
|
|
|
|
|
|
|
if (TAILQ_EMPTY(prog->prog_sports))
|
|
|
|
{
|
2019-09-11 15:27:58 +00:00
|
|
|
if (prog->prog_hostname)
|
|
|
|
s = prog_add_sport(prog, prog->prog_hostname);
|
|
|
|
else
|
|
|
|
s = prog_add_sport(prog, "0.0.0.0:12345");
|
2017-09-22 21:00:03 +00:00
|
|
|
if (0 != s)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
if (prog->prog_certs)
|
|
|
|
{
|
|
|
|
prog->prog_api.ea_lookup_cert = lookup_cert;
|
|
|
|
prog->prog_api.ea_cert_lu_ctx = prog->prog_certs;
|
|
|
|
}
|
2019-12-05 13:44:25 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (prog->prog_engine_flags & LSENG_SERVER)
|
|
|
|
LSQ_WARN("Not a single service specified. Use -c option.");
|
|
|
|
prog->prog_api.ea_lookup_cert = no_cert;
|
|
|
|
}
|
2017-09-22 21:00:03 +00:00
|
|
|
|
|
|
|
prog->prog_eb = event_base_new();
|
|
|
|
prog->prog_engine = lsquic_engine_new(prog->prog_engine_flags,
|
|
|
|
&prog->prog_api);
|
|
|
|
if (!prog->prog_engine)
|
|
|
|
return -1;
|
|
|
|
|
2018-04-09 13:39:38 +00:00
|
|
|
prog->prog_timer = event_new(prog->prog_eb, -1, 0,
|
|
|
|
prog_timer_handler, prog);
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
if (prog->prog_engine_flags & LSENG_SERVER)
|
|
|
|
s = prog_init_server(prog);
|
|
|
|
else
|
|
|
|
s = prog_init_client(prog);
|
2017-09-22 21:00:03 +00:00
|
|
|
|
|
|
|
if (s != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
prog_is_stopped (void)
|
|
|
|
{
|
|
|
|
return prog_stopped != 0;
|
|
|
|
}
|
2018-05-21 19:02:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
send_unsent (evutil_socket_t fd, short what, void *arg)
|
|
|
|
{
|
|
|
|
struct prog *const prog = arg;
|
|
|
|
assert(prog->prog_send);
|
|
|
|
event_del(prog->prog_send);
|
|
|
|
event_free(prog->prog_send);
|
|
|
|
prog->prog_send = NULL;
|
2018-09-06 18:05:15 +00:00
|
|
|
LSQ_DEBUG("on_write event fires");
|
2018-05-21 19:02:33 +00:00
|
|
|
lsquic_engine_send_unsent_packets(prog->prog_engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
prog_sport_cant_send (struct prog *prog, int fd)
|
|
|
|
{
|
|
|
|
assert(!prog->prog_send);
|
2018-09-06 18:05:15 +00:00
|
|
|
LSQ_DEBUG("cannot send: register on_write event");
|
2018-05-21 19:02:33 +00:00
|
|
|
prog->prog_send = event_new(prog->prog_eb, fd, EV_WRITE, send_unsent, prog);
|
|
|
|
event_add(prog->prog_send, NULL);
|
|
|
|
}
|