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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/transport/chttp2/frame_window_update.h" |
| 35 | #include "src/core/lib/transport/chttp2/internal.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 37 | #include <grpc/support/log.h> |
| 38 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 39 | gpr_slice grpc_chttp2_window_update_create(uint32_t id, |
| 40 | uint32_t window_update) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 41 | gpr_slice slice = gpr_slice_malloc(13); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 42 | uint8_t *p = GPR_SLICE_START_PTR(slice); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 44 | GPR_ASSERT(window_update); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | |
| 46 | *p++ = 0; |
| 47 | *p++ = 0; |
| 48 | *p++ = 4; |
| 49 | *p++ = GRPC_CHTTP2_FRAME_WINDOW_UPDATE; |
| 50 | *p++ = 0; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 51 | *p++ = (uint8_t)(id >> 24); |
| 52 | *p++ = (uint8_t)(id >> 16); |
| 53 | *p++ = (uint8_t)(id >> 8); |
| 54 | *p++ = (uint8_t)(id); |
| 55 | *p++ = (uint8_t)(window_update >> 24); |
| 56 | *p++ = (uint8_t)(window_update >> 16); |
| 57 | *p++ = (uint8_t)(window_update >> 8); |
| 58 | *p++ = (uint8_t)(window_update); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | |
| 60 | return slice; |
| 61 | } |
| 62 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 63 | grpc_chttp2_parse_error grpc_chttp2_window_update_parser_begin_frame( |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 64 | grpc_chttp2_window_update_parser *parser, uint32_t length, uint8_t flags) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 65 | if (flags || length != 4) { |
| 66 | gpr_log(GPR_ERROR, "invalid window update: length=%d, flags=%02x", length, |
| 67 | flags); |
| 68 | return GRPC_CHTTP2_CONNECTION_ERROR; |
| 69 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | parser->byte = 0; |
| 71 | parser->amount = 0; |
| 72 | return GRPC_CHTTP2_PARSE_OK; |
| 73 | } |
| 74 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 75 | grpc_chttp2_parse_error grpc_chttp2_window_update_parser_parse( |
| 76 | grpc_exec_ctx *exec_ctx, void *parser, |
| 77 | grpc_chttp2_transport_parsing *transport_parsing, |
| 78 | grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 79 | uint8_t *const beg = GPR_SLICE_START_PTR(slice); |
| 80 | uint8_t *const end = GPR_SLICE_END_PTR(slice); |
| 81 | uint8_t *cur = beg; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | grpc_chttp2_window_update_parser *p = parser; |
| 83 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 84 | while (p->byte != 4 && cur != end) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 85 | p->amount |= ((uint32_t)*cur) << (8 * (3 - p->byte)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | cur++; |
| 87 | p->byte++; |
| 88 | } |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 89 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | if (p->byte == 4) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 91 | uint32_t received_update = p->amount; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 92 | if (received_update == 0 || (received_update & 0x80000000u)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | gpr_log(GPR_ERROR, "invalid window update bytes: %d", p->amount); |
| 94 | return GRPC_CHTTP2_CONNECTION_ERROR; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 95 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 96 | GPR_ASSERT(is_last); |
| 97 | |
| 98 | if (transport_parsing->incoming_stream_id != 0) { |
| 99 | if (stream_parsing != NULL) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 100 | GRPC_CHTTP2_FLOW_CREDIT_STREAM("parse", transport_parsing, |
| 101 | stream_parsing, outgoing_window, |
| 102 | received_update); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 103 | grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, |
| 104 | stream_parsing); |
| 105 | } |
| 106 | } else { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 107 | GRPC_CHTTP2_FLOW_CREDIT_TRANSPORT("parse", transport_parsing, |
| 108 | outgoing_window, received_update); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 111 | |
| 112 | return GRPC_CHTTP2_PARSE_OK; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 113 | } |