2 * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * This is a temporary test server for QUIC. It will eventually be replaced
12 * by s_server and removed once we have full QUIC server support.
16 #include <openssl/bio.h>
17 #include <openssl/ssl.h>
18 #include <openssl/err.h>
19 #include "internal/e_os.h"
20 #include "internal/sockets.h"
21 #include "internal/quic_tserver.h"
22 #include "internal/quic_stream_map.h"
23 #include "internal/time.h"
25 static BIO *bio_err = NULL;
27 static void wait_for_activity(QUIC_TSERVER *qtserv)
29 fd_set readfds, writefds;
30 fd_set *readfdsp = NULL, *writefdsp = NULL;
31 struct timeval timeout, *timeoutp = NULL;
34 BIO *bio = ossl_quic_tserver_get0_rbio(qtserv);
37 BIO_get_fd(bio, &sock);
39 if (ossl_quic_tserver_get_net_read_desired(qtserv)) {
42 openssl_fdset(sock, readfdsp);
45 if (ossl_quic_tserver_get_net_write_desired(qtserv)) {
46 writefdsp = &writefds;
48 openssl_fdset(sock, writefdsp);
51 deadline = ossl_quic_tserver_get_deadline(qtserv);
53 if (!ossl_time_is_infinite(deadline)) {
54 timeout = ossl_time_to_timeval(ossl_time_subtract(deadline,
61 if (readfdsp == NULL && writefdsp == NULL && timeoutp == NULL)
64 select(width, readfdsp, writefdsp, NULL, timeoutp);
67 /* Helper function to create a BIO connected to the server */
68 static BIO *create_dgram_bio(int family, const char *hostname, const char *port)
72 const BIO_ADDRINFO *ai = NULL;
75 if (BIO_sock_init() != 1)
79 * Lookup IP address info for the server.
81 if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_SERVER, family, SOCK_DGRAM,
86 * Loop through all the possible addresses for the server and find one
87 * we can create and start listening on
89 for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) {
90 /* Create the UDP socket */
91 sock = BIO_socket(BIO_ADDRINFO_family(ai), SOCK_DGRAM, 0, 0);
95 /* Start listening on the socket */
96 if (!BIO_listen(sock, BIO_ADDRINFO_address(ai), 0)) {
97 BIO_closesocket(sock);
101 /* Set to non-blocking mode */
102 if (!BIO_socket_nbio(sock, 1)) {
103 BIO_closesocket(sock);
107 break; /* stop searching if we found an addr */
110 /* Free the address information resources we allocated earlier */
111 BIO_ADDRINFO_free(res);
113 /* If we didn't bind any sockets, fail */
117 /* Create a BIO to wrap the socket */
118 bio = BIO_new(BIO_s_datagram());
120 BIO_closesocket(sock);
125 * Associate the newly created BIO with the underlying socket. By
126 * passing BIO_CLOSE here the socket will be automatically closed when
127 * the BIO is freed. Alternatively you can use BIO_NOCLOSE, in which
128 * case you must close the socket explicitly when it is no longer
131 BIO_set_fd(bio, sock, BIO_CLOSE);
136 static void usage(void)
138 BIO_printf(bio_err, "quicserver [-6][-trace] hostname port certfile keyfile\n");
141 int main(int argc, char *argv[])
143 QUIC_TSERVER_ARGS tserver_args = {0};
144 QUIC_TSERVER *qtserv = NULL;
145 int ipv6 = 0, trace = 0;
148 char *hostname, *port, *certfile, *keyfile;
149 int ret = EXIT_FAILURE;
150 unsigned char reqbuf[1024];
151 size_t numbytes, reqbytes = 0;
152 const char reqterm[] = {
153 '\r', '\n', '\r', '\n'
155 const char *response[] = {
156 "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",
157 "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",
158 "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",
159 "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",
161 unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' };
166 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
167 if (argc == 0 || bio_err == NULL)
170 while (argnext < argc) {
171 if (argv[argnext][0] != '-')
173 if (strcmp(argv[argnext], "-6") == 0) {
175 } else if(strcmp(argv[argnext], "-trace") == 0) {
178 BIO_printf(bio_err, "Unrecognised argument %s\n", argv[argnext]);
185 if (argc - argnext != 4) {
189 hostname = argv[argnext++];
190 port = argv[argnext++];
191 certfile = argv[argnext++];
192 keyfile = argv[argnext++];
194 bio = create_dgram_bio(ipv6 ? AF_INET6 : AF_INET, hostname, port);
195 if (bio == NULL || !BIO_up_ref(bio)) {
196 BIO_printf(bio_err, "Unable to create server socket\n");
200 tserver_args.libctx = NULL;
201 tserver_args.net_rbio = bio;
202 tserver_args.net_wbio = bio;
203 tserver_args.alpn = alpn;
204 tserver_args.alpnlen = sizeof(alpn);
205 tserver_args.ctx = NULL;
207 qtserv = ossl_quic_tserver_new(&tserver_args, certfile, keyfile);
208 if (qtserv == NULL) {
209 BIO_printf(bio_err, "Failed to create the QUIC_TSERVER\n");
213 BIO_printf(bio_err, "Starting quicserver\n");
215 "Note that this utility will be removed in a future OpenSSL version.\n");
217 "For test purposes only. Not for use in a production environment.\n");
219 /* Ownership of the BIO is passed to qtserv */
223 #ifndef OPENSSL_NO_SSL_TRACE
224 ossl_quic_tserver_set_msg_callback(qtserv, SSL_trace, bio_err);
227 "Warning: -trace specified but no SSL tracing support present\n");
230 /* Wait for handshake to complete */
231 ossl_quic_tserver_tick(qtserv);
232 while(!ossl_quic_tserver_is_handshake_confirmed(qtserv)) {
233 wait_for_activity(qtserv);
234 ossl_quic_tserver_tick(qtserv);
235 if (ossl_quic_tserver_is_terminated(qtserv)) {
236 BIO_printf(bio_err, "Failed waiting for handshake completion\n");
243 if (respnum >= OSSL_NELEM(response))
245 /* Wait for an incoming stream */
247 streamid = ossl_quic_tserver_pop_incoming_stream(qtserv);
248 if (streamid == UINT64_MAX)
249 wait_for_activity(qtserv);
250 ossl_quic_tserver_tick(qtserv);
251 if (ossl_quic_tserver_is_terminated(qtserv)) {
252 /* Assume we finished everything the clients wants from us */
256 } while(streamid == UINT64_MAX);
258 /* Read the request */
263 wait_for_activity(qtserv);
265 ossl_quic_tserver_tick(qtserv);
266 if (ossl_quic_tserver_is_terminated(qtserv)) {
267 BIO_printf(bio_err, "Failed reading request\n");
272 if (ossl_quic_tserver_read(qtserv, streamid, reqbuf + reqbytes,
273 sizeof(reqbuf) - reqbytes,
276 fwrite(reqbuf + reqbytes, 1, numbytes, stdout);
277 reqbytes += numbytes;
279 } while (reqbytes < sizeof(reqterm)
280 || memcmp(reqbuf + reqbytes - sizeof(reqterm), reqterm,
281 sizeof(reqterm)) != 0);
283 if ((streamid & QUIC_STREAM_DIR_UNI) != 0) {
285 * Incoming stream was uni-directional. Create a server initiated
286 * uni-directional stream for the response.
288 if (!ossl_quic_tserver_stream_new(qtserv, 1, &streamid)) {
289 BIO_printf(bio_err, "Failed creating response stream\n");
294 /* Send the response */
296 ossl_quic_tserver_tick(qtserv);
297 if (!ossl_quic_tserver_write(qtserv, streamid,
298 (unsigned char *)response[respnum],
299 strlen(response[respnum]), &numbytes))
302 if (!ossl_quic_tserver_conclude(qtserv, streamid))
307 /* Free twice because we did an up-ref */
311 ossl_quic_tserver_free(qtserv);