lsquic_shsk_stream.c revision 7d09751d
1/* Copyright (c) 2017 - 2020 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 <sys/socket.h>
16#include <netdb.h>
17#include <time.h>
18
19#include "lsquic_int_types.h"
20#include "lsquic.h"
21
22#include "lsquic_str.h"
23#include "lsquic_mm.h"
24#include "lsquic_engine_public.h"
25#include "lsquic_hash.h"
26#include "lsquic_conn.h"
27#include "lsquic_shsk_stream.h"
28
29#define LSQUIC_LOGGER_MODULE LSQLM_HSK_ADAPTER
30#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(s_hsk->lconn)
31#include "lsquic_logger.h"
32
33
34
35static lsquic_stream_ctx_t *
36hsk_server_on_new_stream (void *stream_if_ctx, lsquic_stream_t *stream)
37{
38    struct server_hsk_ctx *const s_hsk = stream_if_ctx;
39    LSQ_DEBUG("stream created");
40
41    lsquic_stream_wantread(stream, 1);
42
43    /* Note that we return the same thing we're passed.  This structure lives
44     * inside struct full_conn.
45     */
46    return (lsquic_stream_ctx_t *) s_hsk;
47}
48
49
50static void
51hsk_server_on_read (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
52{
53    struct server_hsk_ctx *const s_hsk = (struct server_hsk_ctx *) ctx;
54    struct lsquic_mm *const mm = &s_hsk->enpub->enp_mm;
55    ssize_t nread;
56    unsigned char *buf;
57
58    buf = lsquic_mm_get_4k(mm);
59    if (!buf)
60    {
61        LSQ_WARN("could not allocate buffer: %s", strerror(errno));
62        return;
63    }
64    nread = lsquic_stream_read(stream, buf, 4 * 1024);
65    lsquic_mm_put_4k(mm, buf);
66
67    if (!(s_hsk->flags & SHC_WARNED))
68    {
69        LSQ_WARN("read %zd bytes from stream: what are we to do with them?  "
70                 "Further warnings suppressed", nread);
71        s_hsk->flags |= SHC_WARNED;
72    }
73    else
74        LSQ_DEBUG("read %zd bytes from stream", nread);
75}
76
77
78static void
79hsk_server_on_write (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
80{
81    assert(0);      /* This function is never called */
82}
83
84
85static void
86hsk_server_on_close (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
87{
88    struct server_hsk_ctx *s_hsk = (struct server_hsk_ctx *) ctx;
89    LSQ_DEBUG("stream closed");
90}
91
92
93const struct lsquic_stream_if lsquic_server_hsk_stream_if =
94{
95    .on_new_stream = hsk_server_on_new_stream,
96    .on_read       = hsk_server_on_read,
97    .on_write      = hsk_server_on_write,
98    .on_close      = hsk_server_on_close,
99};
100