lsquic_shsk_stream.c revision a74702c6
1/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * Stream/crypto handshake adapter for the server side.  Since on the server
4 * side, the handshake logic is handled in mini conn, this adapter does not
5 * have much to do.  If peer sends any data on this stream, the adapter
6 * throws the data out and warns.
7 */
8
9#include <assert.h>
10#include <errno.h>
11#include <stdarg.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/queue.h>
15#include <time.h>
16
17#ifndef WIN32
18#include <netdb.h>
19#include <sys/socket.h>
20#endif
21
22#include "lsquic_int_types.h"
23#include "lsquic.h"
24
25#include "lsquic_str.h"
26#include "lsquic_mm.h"
27#include "lsquic_engine_public.h"
28#include "lsquic_hash.h"
29#include "lsquic_conn.h"
30#include "lsquic_shsk_stream.h"
31
32#define LSQUIC_LOGGER_MODULE LSQLM_HSK_ADAPTER
33#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(s_hsk->lconn)
34#include "lsquic_logger.h"
35
36
37
38static lsquic_stream_ctx_t *
39hsk_server_on_new_stream (void *stream_if_ctx, lsquic_stream_t *stream)
40{
41    struct server_hsk_ctx *const s_hsk = stream_if_ctx;
42    LSQ_DEBUG("stream created");
43
44    lsquic_stream_wantread(stream, 1);
45
46    /* Note that we return the same thing we're passed.  This structure lives
47     * inside struct full_conn.
48     */
49    return (lsquic_stream_ctx_t *) s_hsk;
50}
51
52
53static void
54hsk_server_on_read (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
55{
56    struct server_hsk_ctx *const s_hsk = (struct server_hsk_ctx *) ctx;
57    struct lsquic_mm *const mm = &s_hsk->enpub->enp_mm;
58    ssize_t nread;
59    unsigned char *buf;
60
61    buf = lsquic_mm_get_4k(mm);
62    if (!buf)
63    {
64        LSQ_WARN("could not allocate buffer: %s", strerror(errno));
65        return;
66    }
67    nread = lsquic_stream_read(stream, buf, 4 * 1024);
68    lsquic_mm_put_4k(mm, buf);
69
70    if (!(s_hsk->flags & SHC_WARNED))
71    {
72        LSQ_WARN("read %zd bytes from stream: what are we to do with them?  "
73                 "Further warnings suppressed", nread);
74        s_hsk->flags |= SHC_WARNED;
75    }
76    else
77        LSQ_DEBUG("read %zd bytes from stream", nread);
78}
79
80
81static void
82hsk_server_on_write (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
83{
84    assert(0);      /* This function is never called */
85}
86
87
88static void
89hsk_server_on_close (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
90{
91    struct server_hsk_ctx *s_hsk = (struct server_hsk_ctx *) ctx;
92    LSQ_DEBUG("stream closed");
93}
94
95
96const struct lsquic_stream_if lsquic_server_hsk_stream_if =
97{
98    .on_new_stream = hsk_server_on_new_stream,
99    .on_read       = hsk_server_on_read,
100    .on_write      = hsk_server_on_write,
101    .on_close      = hsk_server_on_close,
102};
103