lsquic_shsk_stream.c revision 5392f7a3
15392f7a3SLiteSpeed Tech/* Copyright (c) 2017 - 2019 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 <sys/socket.h>
165392f7a3SLiteSpeed Tech#include <netdb.h>
175392f7a3SLiteSpeed Tech#include <time.h>
185392f7a3SLiteSpeed Tech
195392f7a3SLiteSpeed Tech#include "lsquic_int_types.h"
205392f7a3SLiteSpeed Tech#include "lsquic.h"
215392f7a3SLiteSpeed Tech
225392f7a3SLiteSpeed Tech#include "lsquic_str.h"
235392f7a3SLiteSpeed Tech#include "lsquic_mm.h"
245392f7a3SLiteSpeed Tech#include "lsquic_engine_public.h"
255392f7a3SLiteSpeed Tech#include "lsquic_hash.h"
265392f7a3SLiteSpeed Tech#include "lsquic_conn.h"
275392f7a3SLiteSpeed Tech#include "lsquic_shsk_stream.h"
285392f7a3SLiteSpeed Tech
295392f7a3SLiteSpeed Tech#define LSQUIC_LOGGER_MODULE LSQLM_HSK_ADAPTER
305392f7a3SLiteSpeed Tech#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(s_hsk->lconn)
315392f7a3SLiteSpeed Tech#include "lsquic_logger.h"
325392f7a3SLiteSpeed Tech
335392f7a3SLiteSpeed Tech
345392f7a3SLiteSpeed Tech
355392f7a3SLiteSpeed Techstatic lsquic_stream_ctx_t *
365392f7a3SLiteSpeed Techhsk_server_on_new_stream (void *stream_if_ctx, lsquic_stream_t *stream)
375392f7a3SLiteSpeed Tech{
385392f7a3SLiteSpeed Tech    struct server_hsk_ctx *const s_hsk = stream_if_ctx;
395392f7a3SLiteSpeed Tech    LSQ_DEBUG("stream created");
405392f7a3SLiteSpeed Tech
415392f7a3SLiteSpeed Tech    lsquic_stream_wantread(stream, 1);
425392f7a3SLiteSpeed Tech
435392f7a3SLiteSpeed Tech    /* Note that we return the same thing we're passed.  This structure lives
445392f7a3SLiteSpeed Tech     * inside struct full_conn.
455392f7a3SLiteSpeed Tech     */
465392f7a3SLiteSpeed Tech    return (lsquic_stream_ctx_t *) s_hsk;
475392f7a3SLiteSpeed Tech}
485392f7a3SLiteSpeed Tech
495392f7a3SLiteSpeed Tech
505392f7a3SLiteSpeed Techstatic void
515392f7a3SLiteSpeed Techhsk_server_on_read (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
525392f7a3SLiteSpeed Tech{
535392f7a3SLiteSpeed Tech    struct server_hsk_ctx *const s_hsk = (struct server_hsk_ctx *) ctx;
545392f7a3SLiteSpeed Tech    struct lsquic_mm *const mm = &s_hsk->enpub->enp_mm;
555392f7a3SLiteSpeed Tech    ssize_t nread;
565392f7a3SLiteSpeed Tech    unsigned char *buf;
575392f7a3SLiteSpeed Tech
585392f7a3SLiteSpeed Tech    buf = lsquic_mm_get_4k(mm);
595392f7a3SLiteSpeed Tech    if (!buf)
605392f7a3SLiteSpeed Tech    {
615392f7a3SLiteSpeed Tech        LSQ_WARN("could not allocate buffer: %s", strerror(errno));
625392f7a3SLiteSpeed Tech        return;
635392f7a3SLiteSpeed Tech    }
645392f7a3SLiteSpeed Tech    nread = lsquic_stream_read(stream, buf, 4 * 1024);
655392f7a3SLiteSpeed Tech    lsquic_mm_put_4k(mm, buf);
665392f7a3SLiteSpeed Tech
675392f7a3SLiteSpeed Tech    if (!(s_hsk->flags & SHC_WARNED))
685392f7a3SLiteSpeed Tech    {
695392f7a3SLiteSpeed Tech        LSQ_WARN("read %zd bytes from stream: what are we to do with them?  "
705392f7a3SLiteSpeed Tech                 "Further warnings suppressed", nread);
715392f7a3SLiteSpeed Tech        s_hsk->flags |= SHC_WARNED;
725392f7a3SLiteSpeed Tech    }
735392f7a3SLiteSpeed Tech    else
745392f7a3SLiteSpeed Tech        LSQ_DEBUG("read %zd bytes from stream", nread);
755392f7a3SLiteSpeed Tech}
765392f7a3SLiteSpeed Tech
775392f7a3SLiteSpeed Tech
785392f7a3SLiteSpeed Techstatic void
795392f7a3SLiteSpeed Techhsk_server_on_write (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
805392f7a3SLiteSpeed Tech{
815392f7a3SLiteSpeed Tech    assert(0);      /* This function is never called */
825392f7a3SLiteSpeed Tech}
835392f7a3SLiteSpeed Tech
845392f7a3SLiteSpeed Tech
855392f7a3SLiteSpeed Techstatic void
865392f7a3SLiteSpeed Techhsk_server_on_close (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
875392f7a3SLiteSpeed Tech{
885392f7a3SLiteSpeed Tech    struct server_hsk_ctx *s_hsk = (struct server_hsk_ctx *) ctx;
895392f7a3SLiteSpeed Tech    LSQ_DEBUG("stream closed");
905392f7a3SLiteSpeed Tech}
915392f7a3SLiteSpeed Tech
925392f7a3SLiteSpeed Tech
935392f7a3SLiteSpeed Techconst struct lsquic_stream_if lsquic_server_hsk_stream_if =
945392f7a3SLiteSpeed Tech{
955392f7a3SLiteSpeed Tech    .on_new_stream = hsk_server_on_new_stream,
965392f7a3SLiteSpeed Tech    .on_read       = hsk_server_on_read,
975392f7a3SLiteSpeed Tech    .on_write      = hsk_server_on_write,
985392f7a3SLiteSpeed Tech    .on_close      = hsk_server_on_close,
995392f7a3SLiteSpeed Tech};
100