Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 2e19036 | 2016-03-25 14:33:26 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/transport/chttp2/frame_data.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/useful.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 41 | #include "src/core/lib/support/string.h" |
| 42 | #include "src/core/lib/transport/chttp2/internal.h" |
| 43 | #include "src/core/lib/transport/transport.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 45 | grpc_chttp2_parse_error grpc_chttp2_data_parser_init( |
| 46 | grpc_chttp2_data_parser *parser) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | parser->state = GRPC_CHTTP2_DATA_FH_0; |
Craig Tiller | 7be556e | 2015-11-06 12:29:33 -0800 | [diff] [blame] | 48 | parser->parsing_frame = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | return GRPC_CHTTP2_PARSE_OK; |
| 50 | } |
| 51 | |
Craig Tiller | 7be556e | 2015-11-06 12:29:33 -0800 | [diff] [blame] | 52 | void grpc_chttp2_data_parser_destroy(grpc_exec_ctx *exec_ctx, |
| 53 | grpc_chttp2_data_parser *parser) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 54 | grpc_byte_stream *bs; |
Craig Tiller | 7be556e | 2015-11-06 12:29:33 -0800 | [diff] [blame] | 55 | if (parser->parsing_frame) { |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 56 | grpc_chttp2_incoming_byte_stream_finished(exec_ctx, parser->parsing_frame, |
| 57 | 0, 1); |
Craig Tiller | 7be556e | 2015-11-06 12:29:33 -0800 | [diff] [blame] | 58 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 59 | while ( |
| 60 | (bs = grpc_chttp2_incoming_frame_queue_pop(&parser->incoming_frames))) { |
Craig Tiller | 3b66ab9 | 2015-12-09 19:42:22 -0800 | [diff] [blame] | 61 | grpc_byte_stream_destroy(exec_ctx, bs); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 62 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 65 | grpc_chttp2_parse_error grpc_chttp2_data_parser_begin_frame( |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 66 | grpc_chttp2_data_parser *parser, uint8_t flags) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 67 | if (flags & ~GRPC_CHTTP2_DATA_FLAG_END_STREAM) { |
| 68 | gpr_log(GPR_ERROR, "unsupported data flags: 0x%02x", flags); |
| 69 | return GRPC_CHTTP2_STREAM_ERROR; |
| 70 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | if (flags & GRPC_CHTTP2_DATA_FLAG_END_STREAM) { |
| 73 | parser->is_last_frame = 1; |
| 74 | } else { |
| 75 | parser->is_last_frame = 0; |
| 76 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | |
| 78 | return GRPC_CHTTP2_PARSE_OK; |
| 79 | } |
| 80 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 81 | void grpc_chttp2_incoming_frame_queue_merge( |
| 82 | grpc_chttp2_incoming_frame_queue *head_dst, |
| 83 | grpc_chttp2_incoming_frame_queue *tail_src) { |
| 84 | if (tail_src->head == NULL) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | if (head_dst->head == NULL) { |
| 89 | *head_dst = *tail_src; |
| 90 | memset(tail_src, 0, sizeof(*tail_src)); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | head_dst->tail->next_message = tail_src->head; |
| 95 | head_dst->tail = tail_src->tail; |
| 96 | memset(tail_src, 0, sizeof(*tail_src)); |
| 97 | } |
| 98 | |
| 99 | grpc_byte_stream *grpc_chttp2_incoming_frame_queue_pop( |
| 100 | grpc_chttp2_incoming_frame_queue *q) { |
| 101 | grpc_byte_stream *out; |
| 102 | if (q->head == NULL) { |
| 103 | return NULL; |
| 104 | } |
| 105 | out = &q->head->base; |
| 106 | if (q->head == q->tail) { |
| 107 | memset(q, 0, sizeof(*q)); |
| 108 | } else { |
| 109 | q->head = q->head->next_message; |
| 110 | } |
| 111 | return out; |
| 112 | } |
| 113 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 114 | void grpc_chttp2_encode_data(uint32_t id, gpr_slice_buffer *inbuf, |
| 115 | uint32_t write_bytes, int is_eof, |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 116 | gpr_slice_buffer *outbuf) { |
| 117 | gpr_slice hdr; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 118 | uint8_t *p; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 119 | |
| 120 | hdr = gpr_slice_malloc(9); |
| 121 | p = GPR_SLICE_START_PTR(hdr); |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 122 | GPR_ASSERT(write_bytes < (1 << 24)); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 123 | *p++ = (uint8_t)(write_bytes >> 16); |
| 124 | *p++ = (uint8_t)(write_bytes >> 8); |
| 125 | *p++ = (uint8_t)(write_bytes); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 126 | *p++ = GRPC_CHTTP2_FRAME_DATA; |
| 127 | *p++ = is_eof ? GRPC_CHTTP2_DATA_FLAG_END_STREAM : 0; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 128 | *p++ = (uint8_t)(id >> 24); |
| 129 | *p++ = (uint8_t)(id >> 16); |
| 130 | *p++ = (uint8_t)(id >> 8); |
| 131 | *p++ = (uint8_t)(id); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 132 | gpr_slice_buffer_add(outbuf, hdr); |
| 133 | |
| 134 | gpr_slice_buffer_move_first(inbuf, write_bytes, outbuf); |
| 135 | } |
| 136 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | grpc_chttp2_parse_error grpc_chttp2_data_parser_parse( |
| 138 | grpc_exec_ctx *exec_ctx, void *parser, |
| 139 | grpc_chttp2_transport_parsing *transport_parsing, |
| 140 | grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 141 | uint8_t *const beg = GPR_SLICE_START_PTR(slice); |
| 142 | uint8_t *const end = GPR_SLICE_END_PTR(slice); |
| 143 | uint8_t *cur = beg; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 144 | grpc_chttp2_data_parser *p = parser; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 145 | uint32_t message_flags; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 146 | grpc_chttp2_incoming_byte_stream *incoming_byte_stream; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 147 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 148 | if (is_last && p->is_last_frame) { |
| 149 | stream_parsing->received_close = 1; |
| 150 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | if (cur == end) { |
| 153 | return GRPC_CHTTP2_PARSE_OK; |
| 154 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | switch (p->state) { |
| 157 | fh_0: |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 158 | case GRPC_CHTTP2_DATA_FH_0: |
| 159 | p->frame_type = *cur; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 160 | switch (p->frame_type) { |
| 161 | case 0: |
| 162 | p->is_frame_compressed = 0; /* GPR_FALSE */ |
| 163 | break; |
| 164 | case 1: |
| 165 | p->is_frame_compressed = 1; /* GPR_TRUE */ |
| 166 | break; |
| 167 | default: |
| 168 | gpr_log(GPR_ERROR, "Bad GRPC frame type 0x%02x", p->frame_type); |
| 169 | return GRPC_CHTTP2_STREAM_ERROR; |
| 170 | } |
| 171 | if (++cur == end) { |
| 172 | p->state = GRPC_CHTTP2_DATA_FH_1; |
| 173 | return GRPC_CHTTP2_PARSE_OK; |
| 174 | } |
| 175 | /* fallthrough */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 176 | case GRPC_CHTTP2_DATA_FH_1: |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 177 | p->frame_size = ((uint32_t)*cur) << 24; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 178 | if (++cur == end) { |
| 179 | p->state = GRPC_CHTTP2_DATA_FH_2; |
| 180 | return GRPC_CHTTP2_PARSE_OK; |
| 181 | } |
| 182 | /* fallthrough */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 183 | case GRPC_CHTTP2_DATA_FH_2: |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 184 | p->frame_size |= ((uint32_t)*cur) << 16; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 185 | if (++cur == end) { |
| 186 | p->state = GRPC_CHTTP2_DATA_FH_3; |
| 187 | return GRPC_CHTTP2_PARSE_OK; |
| 188 | } |
| 189 | /* fallthrough */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 190 | case GRPC_CHTTP2_DATA_FH_3: |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 191 | p->frame_size |= ((uint32_t)*cur) << 8; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 192 | if (++cur == end) { |
| 193 | p->state = GRPC_CHTTP2_DATA_FH_4; |
| 194 | return GRPC_CHTTP2_PARSE_OK; |
| 195 | } |
| 196 | /* fallthrough */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 197 | case GRPC_CHTTP2_DATA_FH_4: |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 198 | p->frame_size |= ((uint32_t)*cur); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 199 | p->state = GRPC_CHTTP2_DATA_FRAME; |
| 200 | ++cur; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 201 | message_flags = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 202 | if (p->is_frame_compressed) { |
| 203 | message_flags |= GRPC_WRITE_INTERNAL_COMPRESS; |
| 204 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 205 | p->parsing_frame = incoming_byte_stream = |
| 206 | grpc_chttp2_incoming_byte_stream_create( |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 207 | exec_ctx, transport_parsing, stream_parsing, p->frame_size, |
| 208 | message_flags, &p->incoming_frames); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 209 | /* fallthrough */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 210 | case GRPC_CHTTP2_DATA_FRAME: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | if (cur == end) { |
| 212 | grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, |
| 213 | stream_parsing); |
| 214 | return GRPC_CHTTP2_PARSE_OK; |
| 215 | } |
| 216 | grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, |
| 217 | stream_parsing); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 218 | if ((uint32_t)(end - cur) == p->frame_size) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 219 | grpc_chttp2_incoming_byte_stream_push( |
| 220 | exec_ctx, p->parsing_frame, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 221 | gpr_slice_sub(slice, (size_t)(cur - beg), (size_t)(end - beg))); |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 222 | grpc_chttp2_incoming_byte_stream_finished(exec_ctx, p->parsing_frame, 1, |
| 223 | 1); |
Craig Tiller | 7be556e | 2015-11-06 12:29:33 -0800 | [diff] [blame] | 224 | p->parsing_frame = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 225 | p->state = GRPC_CHTTP2_DATA_FH_0; |
| 226 | return GRPC_CHTTP2_PARSE_OK; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 227 | } else if ((uint32_t)(end - cur) > p->frame_size) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 228 | grpc_chttp2_incoming_byte_stream_push( |
| 229 | exec_ctx, p->parsing_frame, |
| 230 | gpr_slice_sub(slice, (size_t)(cur - beg), |
| 231 | (size_t)(cur + p->frame_size - beg))); |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 232 | grpc_chttp2_incoming_byte_stream_finished(exec_ctx, p->parsing_frame, 1, |
| 233 | 1); |
Craig Tiller | 7be556e | 2015-11-06 12:29:33 -0800 | [diff] [blame] | 234 | p->parsing_frame = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 235 | cur += p->frame_size; |
| 236 | goto fh_0; /* loop */ |
| 237 | } else { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 238 | grpc_chttp2_incoming_byte_stream_push( |
| 239 | exec_ctx, p->parsing_frame, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 240 | gpr_slice_sub(slice, (size_t)(cur - beg), (size_t)(end - beg))); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 241 | GPR_ASSERT((size_t)(end - cur) <= p->frame_size); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 242 | p->frame_size -= (uint32_t)(end - cur); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 243 | return GRPC_CHTTP2_PARSE_OK; |
| 244 | } |
| 245 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 246 | |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 247 | GPR_UNREACHABLE_CODE(return GRPC_CHTTP2_CONNECTION_ERROR); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 248 | } |