lsquic_chsk_stream.c revision 83287402
1/* Copyright (c) 2017 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * Stream/crypto handshake adapter for the client side. 4 * 5 * The client composes CHLO, writes it to the stream, and wait for the 6 * server response, which it processes. 7 */ 8 9#include <assert.h> 10#include <errno.h> 11#include <stdarg.h> 12#include <stdlib.h> 13#include <string.h> 14 15#include "lsquic_int_types.h" 16#include "lsquic.h" 17 18#include "lsquic_str.h" 19#include "lsquic_handshake.h" 20#include "lsquic_chsk_stream.h" 21#include "lsquic_ver_neg.h" 22#include "lsquic_conn.h" 23#include "lsquic_mm.h" 24 25#define LSQUIC_LOGGER_MODULE LSQLM_HSK_ADAPTER 26#define LSQUIC_LOG_CONN_ID lsquic_conn_id(c_hsk->lconn) 27#include "lsquic_logger.h" 28 29 30static lsquic_stream_ctx_t * 31hsk_client_on_new_stream (void *stream_if_ctx, lsquic_stream_t *stream) 32{ 33 struct client_hsk_ctx *const c_hsk = stream_if_ctx; 34 35 LSQ_DEBUG("stream created"); 36 37 38 lsquic_stream_wantwrite(stream, 1); 39 40 return (void *) c_hsk; 41} 42 43 44static void 45hsk_client_on_read (lsquic_stream_t *stream, struct lsquic_stream_ctx *sh) 46{ 47 struct client_hsk_ctx *const c_hsk = (struct client_hsk_ctx *) sh; 48 ssize_t nread; 49 int s; 50 51 if (!c_hsk->buf_in) 52 { 53 c_hsk->buf_in = lsquic_mm_get_16k(c_hsk->mm); 54 if (!c_hsk->buf_in) 55 { 56 LSQ_WARN("could not get buffer: %s", strerror(errno)); 57 lsquic_stream_wantread(stream, 0); 58 lsquic_conn_close(c_hsk->lconn); 59 return; 60 } 61 c_hsk->buf_sz = 16 * 1024; 62 c_hsk->buf_off = 0; 63 } 64 65 nread = lsquic_stream_read(stream, c_hsk->buf_in + c_hsk->buf_off, 66 c_hsk->buf_sz - c_hsk->buf_off); 67 if (nread <= 0) 68 { 69 if (nread < 0) 70 LSQ_INFO("Could not read from handshake stream: %s", 71 strerror(errno)); 72 else 73 LSQ_INFO("Handshake stream closed (odd)"); 74 lsquic_mm_put_16k(c_hsk->mm, c_hsk->buf_in); 75 c_hsk->buf_in = NULL; 76 lsquic_stream_wantread(stream, 0); 77 lsquic_conn_close(c_hsk->lconn); 78 return; 79 } 80 c_hsk->buf_off += nread; 81 82 s = c_hsk->lconn->cn_esf->esf_handle_chlo_reply(c_hsk->lconn->cn_enc_session, 83 c_hsk->buf_in, c_hsk->buf_off); 84 LSQ_DEBUG("lsquic_enc_session_handle_chlo_reply returned %d", s); 85 switch (s) 86 { 87 case DATA_NOT_ENOUGH: 88 if (c_hsk->buf_off < c_hsk->buf_sz) 89 LSQ_INFO("not enough server response has arrived, continue " 90 "buffering"); 91 else 92 { 93 LSQ_INFO("read in %u bytes of server response, and it is still " 94 "not enough: giving up", c_hsk->buf_off); 95 lsquic_mm_put_16k(c_hsk->mm, c_hsk->buf_in); 96 c_hsk->buf_in = NULL; 97 lsquic_stream_wantread(stream, 0); 98 lsquic_conn_close(c_hsk->lconn); 99 } 100 break; 101 case DATA_NO_ERROR: 102 lsquic_mm_put_16k(c_hsk->mm, c_hsk->buf_in); 103 c_hsk->buf_in = NULL; 104 lsquic_stream_wantread(stream, 0); 105 if (c_hsk->lconn->cn_esf->esf_is_hsk_done(c_hsk->lconn->cn_enc_session)) 106 { 107 LSQ_DEBUG("handshake is complete, inform connection"); 108 c_hsk->lconn->cn_if->ci_handshake_done(c_hsk->lconn); 109 } 110 else 111 { 112 LSQ_DEBUG("handshake not yet complete, will generate another " 113 "message"); 114 lsquic_stream_wantwrite(stream, 1); 115 } 116 break; 117 default: 118 LSQ_WARN("lsquic_enc_session_handle_chlo_reply returned unknown value %d", s); 119 case DATA_FORMAT_ERROR: 120 LSQ_INFO("lsquic_enc_session_handle_chlo_reply returned an error"); 121 break; 122 } 123} 124 125 126/* In this function, we assume that we can write the whole message in one 127 * shot. Otherwise, this is an error. 128 */ 129static void 130hsk_client_on_write (lsquic_stream_t *stream, struct lsquic_stream_ctx *sh) 131{ 132 struct client_hsk_ctx *const c_hsk = (struct client_hsk_ctx *) sh; 133 unsigned char *buf; 134 size_t len; 135 ssize_t nw; 136 137 lsquic_stream_wantwrite(stream, 0); 138 139 buf = lsquic_mm_get_4k(c_hsk->mm); 140 if (!buf) 141 { 142 LSQ_WARN("cannot allocate buffer: %s", strerror(errno)); 143 lsquic_conn_close(c_hsk->lconn); 144 return; 145 } 146 len = 4 * 1024; 147 148 if (0 != c_hsk->lconn->cn_esf->esf_gen_chlo(c_hsk->lconn->cn_enc_session, 149 c_hsk->ver_neg->vn_ver, buf, &len)) 150 { 151 LSQ_WARN("cannot create CHLO message"); 152 lsquic_mm_put_4k(c_hsk->mm, buf); 153 lsquic_conn_close(c_hsk->lconn); 154 return; 155 } 156 157 nw = lsquic_stream_write(stream, buf, len); 158 lsquic_mm_put_4k(c_hsk->mm, buf); 159 160 if (nw < 0) 161 LSQ_INFO("error writing to stream: %s", strerror(errno)); 162 else if ((size_t) nw == len) 163 { 164 LSQ_INFO("wrote %zd bytes of CHLO to stream", nw); 165 lsquic_stream_flush(stream); 166 lsquic_stream_wantread(stream, 1); 167 } 168 else 169 LSQ_INFO("could only write %zd bytes to stream instead of %zd", 170 nw, len); 171} 172 173 174static void 175hsk_client_on_close (lsquic_stream_t *stream, struct lsquic_stream_ctx *sh) 176{ 177 struct client_hsk_ctx *const c_hsk = (struct client_hsk_ctx *) sh; 178 if (c_hsk->buf_in) 179 lsquic_mm_put_16k(c_hsk->mm, c_hsk->buf_in); 180 LSQ_DEBUG("stream closed"); 181} 182 183 184const struct lsquic_stream_if lsquic_client_hsk_stream_if = 185{ 186 .on_new_stream = hsk_client_on_new_stream, 187 .on_read = hsk_client_on_read, 188 .on_write = hsk_client_on_write, 189 .on_close = hsk_client_on_close, 190}; 191