2023-06-08 19:18:38 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a temporary test server for QUIC. It will eventually be replaced
|
|
|
|
* by s_server and removed once we have full QUIC server support.
|
|
|
|
*/
|
|
|
|
|
2023-08-08 00:34:50 +08:00
|
|
|
#include <stdio.h>
|
2023-06-08 19:18:38 +08:00
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include "internal/e_os.h"
|
|
|
|
#include "internal/sockets.h"
|
|
|
|
#include "internal/quic_tserver.h"
|
2023-11-09 18:27:13 +08:00
|
|
|
#include "internal/quic_stream_map.h"
|
2023-06-08 19:18:38 +08:00
|
|
|
#include "internal/time.h"
|
|
|
|
|
2023-08-08 00:34:50 +08:00
|
|
|
static BIO *bio_err = NULL;
|
|
|
|
|
2023-06-08 19:18:38 +08:00
|
|
|
static void wait_for_activity(QUIC_TSERVER *qtserv)
|
|
|
|
{
|
|
|
|
fd_set readfds, writefds;
|
|
|
|
fd_set *readfdsp = NULL, *writefdsp = NULL;
|
|
|
|
struct timeval timeout, *timeoutp = NULL;
|
|
|
|
int width;
|
|
|
|
int sock;
|
|
|
|
BIO *bio = ossl_quic_tserver_get0_rbio(qtserv);
|
|
|
|
OSSL_TIME deadline;
|
|
|
|
|
|
|
|
BIO_get_fd(bio, &sock);
|
|
|
|
|
|
|
|
if (ossl_quic_tserver_get_net_read_desired(qtserv)) {
|
|
|
|
readfdsp = &readfds;
|
|
|
|
FD_ZERO(readfdsp);
|
|
|
|
openssl_fdset(sock, readfdsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ossl_quic_tserver_get_net_write_desired(qtserv)) {
|
|
|
|
writefdsp = &writefds;
|
|
|
|
FD_ZERO(writefdsp);
|
|
|
|
openssl_fdset(sock, writefdsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
deadline = ossl_quic_tserver_get_deadline(qtserv);
|
|
|
|
|
|
|
|
if (!ossl_time_is_infinite(deadline)) {
|
|
|
|
timeout = ossl_time_to_timeval(ossl_time_subtract(deadline,
|
|
|
|
ossl_time_now()));
|
|
|
|
timeoutp = &timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
width = sock + 1;
|
|
|
|
|
|
|
|
if (readfdsp == NULL && writefdsp == NULL && timeoutp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
select(width, readfdsp, writefdsp, NULL, timeoutp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function to create a BIO connected to the server */
|
|
|
|
static BIO *create_dgram_bio(int family, const char *hostname, const char *port)
|
|
|
|
{
|
|
|
|
int sock = -1;
|
|
|
|
BIO_ADDRINFO *res;
|
|
|
|
const BIO_ADDRINFO *ai = NULL;
|
|
|
|
BIO *bio;
|
|
|
|
|
|
|
|
if (BIO_sock_init() != 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup IP address info for the server.
|
|
|
|
*/
|
|
|
|
if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_SERVER, family, SOCK_DGRAM,
|
|
|
|
0, &res))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through all the possible addresses for the server and find one
|
|
|
|
* we can create and start listening on
|
|
|
|
*/
|
|
|
|
for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) {
|
|
|
|
/* Create the UDP socket */
|
|
|
|
sock = BIO_socket(BIO_ADDRINFO_family(ai), SOCK_DGRAM, 0, 0);
|
|
|
|
if (sock == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Start listening on the socket */
|
|
|
|
if (!BIO_listen(sock, BIO_ADDRINFO_address(ai), 0)) {
|
|
|
|
BIO_closesocket(sock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set to non-blocking mode */
|
|
|
|
if (!BIO_socket_nbio(sock, 1)) {
|
|
|
|
BIO_closesocket(sock);
|
|
|
|
continue;
|
|
|
|
}
|
Fix quicserver binding when duplicate entries exist
In testing the quic demos, I found that the quicserver refused to start for me,
indicating an inability to bind a socket to listen on
The problem turned out to be that getaddrinfo on my system was returning
multiple entries, due to the fact that /etc/host maps the localhost host name to
both ipv4 (127.0.0.1) and ipv6 (::1), but returns the latter as an ipv4 mapped
address (specifying family == AF_INET)
It seems like the proper fix would be to modify the /etc/hosts file to not make
that mapping, and indeed that works. However, since several distribution ship
with this setup, it seems like it is worthwhile to manage it in the server code.
its also that some other application may be bound to a given address/port
leading to failure, which I think could be considered erroneous, as any failure
for the full addrinfo list in quicserver would lead to a complete failure
Fix this by modifying the create_dgram_bio function to count the number of
sockets is successfully binds/listens on, skipping any failures, and only exit
the application if the number of bound sockets is zero.
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22559)
2023-10-31 01:47:05 +08:00
|
|
|
|
|
|
|
break; /* stop searching if we found an addr */
|
2023-06-08 19:18:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the address information resources we allocated earlier */
|
|
|
|
BIO_ADDRINFO_free(res);
|
|
|
|
|
Fix quicserver binding when duplicate entries exist
In testing the quic demos, I found that the quicserver refused to start for me,
indicating an inability to bind a socket to listen on
The problem turned out to be that getaddrinfo on my system was returning
multiple entries, due to the fact that /etc/host maps the localhost host name to
both ipv4 (127.0.0.1) and ipv6 (::1), but returns the latter as an ipv4 mapped
address (specifying family == AF_INET)
It seems like the proper fix would be to modify the /etc/hosts file to not make
that mapping, and indeed that works. However, since several distribution ship
with this setup, it seems like it is worthwhile to manage it in the server code.
its also that some other application may be bound to a given address/port
leading to failure, which I think could be considered erroneous, as any failure
for the full addrinfo list in quicserver would lead to a complete failure
Fix this by modifying the create_dgram_bio function to count the number of
sockets is successfully binds/listens on, skipping any failures, and only exit
the application if the number of bound sockets is zero.
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22559)
2023-10-31 01:47:05 +08:00
|
|
|
/* If we didn't bind any sockets, fail */
|
|
|
|
if (ai == NULL)
|
2023-06-08 19:18:38 +08:00
|
|
|
return NULL;
|
|
|
|
|
2023-09-06 19:36:43 +08:00
|
|
|
/* Create a BIO to wrap the socket */
|
2023-06-08 19:18:38 +08:00
|
|
|
bio = BIO_new(BIO_s_datagram());
|
2023-09-06 19:36:43 +08:00
|
|
|
if (bio == NULL) {
|
2023-06-08 19:18:38 +08:00
|
|
|
BIO_closesocket(sock);
|
2023-09-06 19:36:43 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-06-08 19:18:38 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Associate the newly created BIO with the underlying socket. By
|
|
|
|
* passing BIO_CLOSE here the socket will be automatically closed when
|
|
|
|
* the BIO is freed. Alternatively you can use BIO_NOCLOSE, in which
|
|
|
|
* case you must close the socket explicitly when it is no longer
|
|
|
|
* needed.
|
|
|
|
*/
|
|
|
|
BIO_set_fd(bio, sock, BIO_CLOSE);
|
|
|
|
|
|
|
|
return bio;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
2023-08-17 21:32:53 +08:00
|
|
|
BIO_printf(bio_err, "quicserver [-6][-trace] hostname port certfile keyfile\n");
|
2023-06-08 19:18:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QUIC_TSERVER_ARGS tserver_args = {0};
|
|
|
|
QUIC_TSERVER *qtserv = NULL;
|
2023-08-17 21:32:53 +08:00
|
|
|
int ipv6 = 0, trace = 0;
|
2023-06-08 19:18:38 +08:00
|
|
|
int argnext = 1;
|
|
|
|
BIO *bio = NULL;
|
|
|
|
char *hostname, *port, *certfile, *keyfile;
|
|
|
|
int ret = EXIT_FAILURE;
|
|
|
|
unsigned char reqbuf[1024];
|
|
|
|
size_t numbytes, reqbytes = 0;
|
|
|
|
const char reqterm[] = {
|
|
|
|
'\r', '\n', '\r', '\n'
|
|
|
|
};
|
2023-08-14 23:32:44 +08:00
|
|
|
const char *response[] = {
|
|
|
|
"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n<!DOCTYPE html>\n<html>\n<body>Hello world</body>\n</html>\n",
|
|
|
|
"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n<!DOCTYPE html>\n<html>\n<body>Hello again</body>\n</html>\n",
|
|
|
|
"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n<!DOCTYPE html>\n<html>\n<body>Another response</body>\n</html>\n",
|
|
|
|
"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n<!DOCTYPE html>\n<html>\n<body>A message</body>\n</html>\n",
|
|
|
|
};
|
2023-06-08 19:18:38 +08:00
|
|
|
unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' };
|
|
|
|
int first = 1;
|
2023-08-14 23:32:44 +08:00
|
|
|
uint64_t streamid;
|
|
|
|
size_t respnum = 0;
|
2023-06-08 19:18:38 +08:00
|
|
|
|
2023-08-08 00:34:50 +08:00
|
|
|
bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
|
|
|
|
if (argc == 0 || bio_err == NULL)
|
|
|
|
goto end2;
|
2023-06-08 19:18:38 +08:00
|
|
|
|
|
|
|
while (argnext < argc) {
|
|
|
|
if (argv[argnext][0] != '-')
|
|
|
|
break;
|
|
|
|
if (strcmp(argv[argnext], "-6") == 0) {
|
|
|
|
ipv6 = 1;
|
2023-08-17 21:32:53 +08:00
|
|
|
} else if(strcmp(argv[argnext], "-trace") == 0) {
|
|
|
|
trace = 1;
|
2023-06-08 19:18:38 +08:00
|
|
|
} else {
|
2023-08-08 00:34:50 +08:00
|
|
|
BIO_printf(bio_err, "Unrecognised argument %s\n", argv[argnext]);
|
2023-06-08 19:18:38 +08:00
|
|
|
usage();
|
2023-08-08 00:34:50 +08:00
|
|
|
goto end2;
|
2023-06-08 19:18:38 +08:00
|
|
|
}
|
|
|
|
argnext++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - argnext != 4) {
|
|
|
|
usage();
|
2023-08-08 00:34:50 +08:00
|
|
|
goto end2;
|
2023-06-08 19:18:38 +08:00
|
|
|
}
|
|
|
|
hostname = argv[argnext++];
|
|
|
|
port = argv[argnext++];
|
|
|
|
certfile = argv[argnext++];
|
|
|
|
keyfile = argv[argnext++];
|
|
|
|
|
|
|
|
bio = create_dgram_bio(ipv6 ? AF_INET6 : AF_INET, hostname, port);
|
|
|
|
if (bio == NULL || !BIO_up_ref(bio)) {
|
2023-08-08 00:34:50 +08:00
|
|
|
BIO_printf(bio_err, "Unable to create server socket\n");
|
|
|
|
goto end2;
|
2023-06-08 19:18:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tserver_args.libctx = NULL;
|
|
|
|
tserver_args.net_rbio = bio;
|
|
|
|
tserver_args.net_wbio = bio;
|
|
|
|
tserver_args.alpn = alpn;
|
|
|
|
tserver_args.alpnlen = sizeof(alpn);
|
2023-07-28 16:22:38 +08:00
|
|
|
tserver_args.ctx = NULL;
|
2023-06-08 19:18:38 +08:00
|
|
|
|
|
|
|
qtserv = ossl_quic_tserver_new(&tserver_args, certfile, keyfile);
|
|
|
|
if (qtserv == NULL) {
|
2023-08-08 00:34:50 +08:00
|
|
|
BIO_printf(bio_err, "Failed to create the QUIC_TSERVER\n");
|
2023-06-08 19:18:38 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2023-08-08 00:34:50 +08:00
|
|
|
BIO_printf(bio_err, "Starting quicserver\n");
|
|
|
|
BIO_printf(bio_err,
|
|
|
|
"Note that this utility will be removed in a future OpenSSL version.\n");
|
|
|
|
BIO_printf(bio_err,
|
|
|
|
"For test purposes only. Not for use in a production environment.\n");
|
2023-06-08 19:18:38 +08:00
|
|
|
|
|
|
|
/* Ownership of the BIO is passed to qtserv */
|
|
|
|
bio = NULL;
|
|
|
|
|
2023-08-17 21:32:53 +08:00
|
|
|
if (trace)
|
2023-09-05 03:50:18 +08:00
|
|
|
#ifndef OPENSSL_NO_SSL_TRACE
|
2023-08-17 21:32:53 +08:00
|
|
|
ossl_quic_tserver_set_msg_callback(qtserv, SSL_trace, bio_err);
|
2023-09-05 03:50:18 +08:00
|
|
|
#else
|
|
|
|
BIO_printf(bio_err,
|
|
|
|
"Warning: -trace specified but no SSL tracing support present\n");
|
|
|
|
#endif
|
2023-08-17 21:32:53 +08:00
|
|
|
|
2023-08-14 23:32:44 +08:00
|
|
|
/* Wait for handshake to complete */
|
|
|
|
ossl_quic_tserver_tick(qtserv);
|
|
|
|
while(!ossl_quic_tserver_is_handshake_confirmed(qtserv)) {
|
|
|
|
wait_for_activity(qtserv);
|
2023-06-08 19:18:38 +08:00
|
|
|
ossl_quic_tserver_tick(qtserv);
|
2023-10-31 00:30:35 +08:00
|
|
|
if (ossl_quic_tserver_is_terminated(qtserv)) {
|
|
|
|
BIO_printf(bio_err, "Failed waiting for handshake completion\n");
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto end;
|
|
|
|
}
|
2023-08-14 23:32:44 +08:00
|
|
|
}
|
2023-06-08 19:18:38 +08:00
|
|
|
|
2023-08-14 23:32:44 +08:00
|
|
|
for (;; respnum++) {
|
|
|
|
if (respnum >= OSSL_NELEM(response))
|
|
|
|
goto end;
|
|
|
|
/* Wait for an incoming stream */
|
|
|
|
do {
|
|
|
|
streamid = ossl_quic_tserver_pop_incoming_stream(qtserv);
|
|
|
|
if (streamid == UINT64_MAX)
|
|
|
|
wait_for_activity(qtserv);
|
|
|
|
ossl_quic_tserver_tick(qtserv);
|
|
|
|
if (ossl_quic_tserver_is_terminated(qtserv)) {
|
|
|
|
/* Assume we finished everything the clients wants from us */
|
|
|
|
ret = EXIT_SUCCESS;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} while(streamid == UINT64_MAX);
|
|
|
|
|
|
|
|
/* Read the request */
|
|
|
|
do {
|
|
|
|
if (first)
|
|
|
|
first = 0;
|
|
|
|
else
|
|
|
|
wait_for_activity(qtserv);
|
|
|
|
|
|
|
|
ossl_quic_tserver_tick(qtserv);
|
2023-10-31 00:30:35 +08:00
|
|
|
if (ossl_quic_tserver_is_terminated(qtserv)) {
|
|
|
|
BIO_printf(bio_err, "Failed reading request\n");
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto end;
|
|
|
|
}
|
2023-08-14 23:32:44 +08:00
|
|
|
|
|
|
|
if (ossl_quic_tserver_read(qtserv, streamid, reqbuf + reqbytes,
|
|
|
|
sizeof(reqbuf) - reqbytes,
|
|
|
|
&numbytes)) {
|
|
|
|
if (numbytes > 0)
|
|
|
|
fwrite(reqbuf + reqbytes, 1, numbytes, stdout);
|
|
|
|
reqbytes += numbytes;
|
|
|
|
}
|
|
|
|
} while (reqbytes < sizeof(reqterm)
|
|
|
|
|| memcmp(reqbuf + reqbytes - sizeof(reqterm), reqterm,
|
|
|
|
sizeof(reqterm)) != 0);
|
|
|
|
|
|
|
|
if ((streamid & QUIC_STREAM_DIR_UNI) != 0) {
|
|
|
|
/*
|
|
|
|
* Incoming stream was uni-directional. Create a server initiated
|
|
|
|
* uni-directional stream for the response.
|
|
|
|
*/
|
|
|
|
if (!ossl_quic_tserver_stream_new(qtserv, 1, &streamid)) {
|
|
|
|
BIO_printf(bio_err, "Failed creating response stream\n");
|
|
|
|
goto end;
|
|
|
|
}
|
2023-06-08 19:18:38 +08:00
|
|
|
}
|
|
|
|
|
2023-08-14 23:32:44 +08:00
|
|
|
/* Send the response */
|
2023-06-08 19:18:38 +08:00
|
|
|
|
|
|
|
ossl_quic_tserver_tick(qtserv);
|
2023-08-14 23:32:44 +08:00
|
|
|
if (!ossl_quic_tserver_write(qtserv, streamid,
|
|
|
|
(unsigned char *)response[respnum],
|
|
|
|
strlen(response[respnum]), &numbytes))
|
|
|
|
goto end;
|
2023-06-08 19:18:38 +08:00
|
|
|
|
2023-08-14 23:32:44 +08:00
|
|
|
if (!ossl_quic_tserver_conclude(qtserv, streamid))
|
|
|
|
goto end;
|
|
|
|
}
|
2023-06-08 19:18:38 +08:00
|
|
|
|
|
|
|
end:
|
|
|
|
/* Free twice because we did an up-ref */
|
|
|
|
BIO_free(bio);
|
2023-08-08 00:34:50 +08:00
|
|
|
end2:
|
2023-06-08 19:18:38 +08:00
|
|
|
BIO_free(bio);
|
|
|
|
ossl_quic_tserver_free(qtserv);
|
2023-08-08 00:34:50 +08:00
|
|
|
BIO_free(bio_err);
|
2023-06-08 19:18:38 +08:00
|
|
|
return ret;
|
|
|
|
}
|