1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
25392f7a3SLiteSpeed Tech/*
35392f7a3SLiteSpeed Tech * Stream/crypto handshake adapter for the server side.  Since on the server
45392f7a3SLiteSpeed Tech * side, the handshake logic is handled in mini conn, this adapter does not
55392f7a3SLiteSpeed Tech * have much to do.  If peer sends any data on this stream, the adapter
65392f7a3SLiteSpeed Tech * throws the data out and warns.
75392f7a3SLiteSpeed Tech */
85392f7a3SLiteSpeed Tech
95392f7a3SLiteSpeed Tech#include <assert.h>
105392f7a3SLiteSpeed Tech#include <errno.h>
115392f7a3SLiteSpeed Tech#include <stdarg.h>
125392f7a3SLiteSpeed Tech#include <stdlib.h>
135392f7a3SLiteSpeed Tech#include <string.h>
145392f7a3SLiteSpeed Tech#include <sys/queue.h>
155392f7a3SLiteSpeed Tech#include <time.h>
165392f7a3SLiteSpeed Tech
17fb3e20e0SDmitri Tikhonov#ifndef WIN32
18fb3e20e0SDmitri Tikhonov#include <netdb.h>
19fb3e20e0SDmitri Tikhonov#include <sys/socket.h>
20fb3e20e0SDmitri Tikhonov#endif
21fb3e20e0SDmitri Tikhonov
225392f7a3SLiteSpeed Tech#include "lsquic_int_types.h"
235392f7a3SLiteSpeed Tech#include "lsquic.h"
245392f7a3SLiteSpeed Tech
255392f7a3SLiteSpeed Tech#include "lsquic_str.h"
265392f7a3SLiteSpeed Tech#include "lsquic_mm.h"
275392f7a3SLiteSpeed Tech#include "lsquic_engine_public.h"
285392f7a3SLiteSpeed Tech#include "lsquic_hash.h"
295392f7a3SLiteSpeed Tech#include "lsquic_conn.h"
305392f7a3SLiteSpeed Tech#include "lsquic_shsk_stream.h"
315392f7a3SLiteSpeed Tech
325392f7a3SLiteSpeed Tech#define LSQUIC_LOGGER_MODULE LSQLM_HSK_ADAPTER
335392f7a3SLiteSpeed Tech#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(s_hsk->lconn)
345392f7a3SLiteSpeed Tech#include "lsquic_logger.h"
355392f7a3SLiteSpeed Tech
365392f7a3SLiteSpeed Tech
375392f7a3SLiteSpeed Tech
385392f7a3SLiteSpeed Techstatic lsquic_stream_ctx_t *
395392f7a3SLiteSpeed Techhsk_server_on_new_stream (void *stream_if_ctx, lsquic_stream_t *stream)
405392f7a3SLiteSpeed Tech{
415392f7a3SLiteSpeed Tech    struct server_hsk_ctx *const s_hsk = stream_if_ctx;
425392f7a3SLiteSpeed Tech    LSQ_DEBUG("stream created");
435392f7a3SLiteSpeed Tech
445392f7a3SLiteSpeed Tech    lsquic_stream_wantread(stream, 1);
455392f7a3SLiteSpeed Tech
465392f7a3SLiteSpeed Tech    /* Note that we return the same thing we're passed.  This structure lives
475392f7a3SLiteSpeed Tech     * inside struct full_conn.
485392f7a3SLiteSpeed Tech     */
495392f7a3SLiteSpeed Tech    return (lsquic_stream_ctx_t *) s_hsk;
505392f7a3SLiteSpeed Tech}
515392f7a3SLiteSpeed Tech
525392f7a3SLiteSpeed Tech
535392f7a3SLiteSpeed Techstatic void
545392f7a3SLiteSpeed Techhsk_server_on_read (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
555392f7a3SLiteSpeed Tech{
565392f7a3SLiteSpeed Tech    struct server_hsk_ctx *const s_hsk = (struct server_hsk_ctx *) ctx;
575392f7a3SLiteSpeed Tech    struct lsquic_mm *const mm = &s_hsk->enpub->enp_mm;
585392f7a3SLiteSpeed Tech    ssize_t nread;
595392f7a3SLiteSpeed Tech    unsigned char *buf;
605392f7a3SLiteSpeed Tech
615392f7a3SLiteSpeed Tech    buf = lsquic_mm_get_4k(mm);
625392f7a3SLiteSpeed Tech    if (!buf)
635392f7a3SLiteSpeed Tech    {
645392f7a3SLiteSpeed Tech        LSQ_WARN("could not allocate buffer: %s", strerror(errno));
655392f7a3SLiteSpeed Tech        return;
665392f7a3SLiteSpeed Tech    }
675392f7a3SLiteSpeed Tech    nread = lsquic_stream_read(stream, buf, 4 * 1024);
685392f7a3SLiteSpeed Tech    lsquic_mm_put_4k(mm, buf);
695392f7a3SLiteSpeed Tech
705392f7a3SLiteSpeed Tech    if (!(s_hsk->flags & SHC_WARNED))
715392f7a3SLiteSpeed Tech    {
725392f7a3SLiteSpeed Tech        LSQ_WARN("read %zd bytes from stream: what are we to do with them?  "
735392f7a3SLiteSpeed Tech                 "Further warnings suppressed", nread);
745392f7a3SLiteSpeed Tech        s_hsk->flags |= SHC_WARNED;
755392f7a3SLiteSpeed Tech    }
765392f7a3SLiteSpeed Tech    else
775392f7a3SLiteSpeed Tech        LSQ_DEBUG("read %zd bytes from stream", nread);
785392f7a3SLiteSpeed Tech}
795392f7a3SLiteSpeed Tech
805392f7a3SLiteSpeed Tech
815392f7a3SLiteSpeed Techstatic void
825392f7a3SLiteSpeed Techhsk_server_on_write (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
835392f7a3SLiteSpeed Tech{
845392f7a3SLiteSpeed Tech    assert(0);      /* This function is never called */
855392f7a3SLiteSpeed Tech}
865392f7a3SLiteSpeed Tech
875392f7a3SLiteSpeed Tech
885392f7a3SLiteSpeed Techstatic void
895392f7a3SLiteSpeed Techhsk_server_on_close (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
905392f7a3SLiteSpeed Tech{
915392f7a3SLiteSpeed Tech    struct server_hsk_ctx *s_hsk = (struct server_hsk_ctx *) ctx;
925392f7a3SLiteSpeed Tech    LSQ_DEBUG("stream closed");
935392f7a3SLiteSpeed Tech}
945392f7a3SLiteSpeed Tech
955392f7a3SLiteSpeed Tech
965392f7a3SLiteSpeed Techconst struct lsquic_stream_if lsquic_server_hsk_stream_if =
975392f7a3SLiteSpeed Tech{
985392f7a3SLiteSpeed Tech    .on_new_stream = hsk_server_on_new_stream,
995392f7a3SLiteSpeed Tech    .on_read       = hsk_server_on_read,
1005392f7a3SLiteSpeed Tech    .on_write      = hsk_server_on_write,
1015392f7a3SLiteSpeed Tech    .on_close      = hsk_server_on_close,
1025392f7a3SLiteSpeed Tech};
103