Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 8a9fd52 | 2016-03-25 17:09:29 -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 | |
| 34 | #include "src/core/transport/chttp2/frame_rst_stream.h" |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 35 | #include "src/core/transport/chttp2/internal.h" |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 36 | |
| 37 | #include <grpc/support/log.h> |
| 38 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include "src/core/transport/chttp2/frame.h" |
| 40 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 41 | gpr_slice grpc_chttp2_rst_stream_create(uint32_t id, uint32_t code) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 42 | gpr_slice slice = gpr_slice_malloc(13); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 43 | uint8_t *p = GPR_SLICE_START_PTR(slice); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | |
| 45 | *p++ = 0; |
| 46 | *p++ = 0; |
| 47 | *p++ = 4; |
| 48 | *p++ = GRPC_CHTTP2_FRAME_RST_STREAM; |
| 49 | *p++ = 0; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 50 | *p++ = (uint8_t)(id >> 24); |
| 51 | *p++ = (uint8_t)(id >> 16); |
| 52 | *p++ = (uint8_t)(id >> 8); |
| 53 | *p++ = (uint8_t)(id); |
| 54 | *p++ = (uint8_t)(code >> 24); |
| 55 | *p++ = (uint8_t)(code >> 16); |
| 56 | *p++ = (uint8_t)(code >> 8); |
| 57 | *p++ = (uint8_t)(code); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | |
| 59 | return slice; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 60 | } |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 61 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 62 | grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_begin_frame( |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 63 | grpc_chttp2_rst_stream_parser *parser, uint32_t length, uint8_t flags) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 64 | if (length != 4) { |
| 65 | gpr_log(GPR_ERROR, "invalid rst_stream: length=%d, flags=%02x", length, |
| 66 | flags); |
| 67 | return GRPC_CHTTP2_CONNECTION_ERROR; |
| 68 | } |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 69 | parser->byte = 0; |
| 70 | return GRPC_CHTTP2_PARSE_OK; |
| 71 | } |
| 72 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 73 | grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_parse( |
| 74 | grpc_exec_ctx *exec_ctx, void *parser, |
| 75 | grpc_chttp2_transport_parsing *transport_parsing, |
| 76 | grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 77 | uint8_t *const beg = GPR_SLICE_START_PTR(slice); |
| 78 | uint8_t *const end = GPR_SLICE_END_PTR(slice); |
| 79 | uint8_t *cur = beg; |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 80 | grpc_chttp2_rst_stream_parser *p = parser; |
| 81 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | while (p->byte != 4 && cur != end) { |
| 83 | p->reason_bytes[p->byte] = *cur; |
| 84 | cur++; |
| 85 | p->byte++; |
| 86 | } |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | if (p->byte == 4) { |
| 89 | GPR_ASSERT(is_last); |
| 90 | stream_parsing->received_close = 1; |
| 91 | stream_parsing->saw_rst_stream = 1; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 92 | stream_parsing->rst_stream_reason = (((uint32_t)p->reason_bytes[0]) << 24) | |
| 93 | (((uint32_t)p->reason_bytes[1]) << 16) | |
| 94 | (((uint32_t)p->reason_bytes[2]) << 8) | |
| 95 | (((uint32_t)p->reason_bytes[3])); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 96 | } |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 97 | |
| 98 | return GRPC_CHTTP2_PARSE_OK; |
| 99 | } |