blob: 03b665c9cbf3f826bfd69aca3a2f109e35ef923a [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller8a9fd522016-03-25 17:09:29 -07003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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_window_update.h"
Craig Tiller3719f072015-06-12 17:19:51 -070035#include "src/core/transport/chttp2/internal.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <grpc/support/log.h>
38
Craig Tiller7536af02015-12-22 13:49:30 -080039gpr_slice grpc_chttp2_window_update_create(uint32_t id,
40 uint32_t window_update) {
Craig Tillera82950e2015-09-22 12:33:20 -070041 gpr_slice slice = gpr_slice_malloc(13);
Craig Tiller7536af02015-12-22 13:49:30 -080042 uint8_t *p = GPR_SLICE_START_PTR(slice);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043
Craig Tillera82950e2015-09-22 12:33:20 -070044 GPR_ASSERT(window_update);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045
46 *p++ = 0;
47 *p++ = 0;
48 *p++ = 4;
49 *p++ = GRPC_CHTTP2_FRAME_WINDOW_UPDATE;
50 *p++ = 0;
Craig Tiller7536af02015-12-22 13:49:30 -080051 *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 Nobleb7ebd3b2014-11-26 16:33:03 -080059
60 return slice;
61}
62
Craig Tillera82950e2015-09-22 12:33:20 -070063grpc_chttp2_parse_error grpc_chttp2_window_update_parser_begin_frame(
Craig Tiller7536af02015-12-22 13:49:30 -080064 grpc_chttp2_window_update_parser *parser, uint32_t length, uint8_t flags) {
Craig Tillera82950e2015-09-22 12:33:20 -070065 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 Nobleb7ebd3b2014-11-26 16:33:03 -080070 parser->byte = 0;
71 parser->amount = 0;
72 return GRPC_CHTTP2_PARSE_OK;
73}
74
Craig Tillera82950e2015-09-22 12:33:20 -070075grpc_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 Tiller7536af02015-12-22 13:49:30 -080079 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 Nobleb7ebd3b2014-11-26 16:33:03 -080082 grpc_chttp2_window_update_parser *p = parser;
83
Craig Tillera82950e2015-09-22 12:33:20 -070084 while (p->byte != 4 && cur != end) {
Craig Tiller7536af02015-12-22 13:49:30 -080085 p->amount |= ((uint32_t)*cur) << (8 * (3 - p->byte));
Craig Tillera82950e2015-09-22 12:33:20 -070086 cur++;
87 p->byte++;
88 }
Craig Tiller3719f072015-06-12 17:19:51 -070089
Craig Tillera82950e2015-09-22 12:33:20 -070090 if (p->byte == 4) {
Craig Tiller7536af02015-12-22 13:49:30 -080091 uint32_t received_update = p->amount;
Craig Tiller9d35a1f2015-11-02 14:16:12 -080092 if (received_update == 0 || (received_update & 0x80000000u)) {
Craig Tillera82950e2015-09-22 12:33:20 -070093 gpr_log(GPR_ERROR, "invalid window update bytes: %d", p->amount);
94 return GRPC_CHTTP2_CONNECTION_ERROR;
Craig Tiller3719f072015-06-12 17:19:51 -070095 }
Craig Tillera82950e2015-09-22 12:33:20 -070096 GPR_ASSERT(is_last);
97
98 if (transport_parsing->incoming_stream_id != 0) {
99 if (stream_parsing != NULL) {
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800100 GRPC_CHTTP2_FLOW_CREDIT_STREAM("parse", transport_parsing,
101 stream_parsing, outgoing_window,
102 received_update);
Craig Tillera82950e2015-09-22 12:33:20 -0700103 grpc_chttp2_list_add_parsing_seen_stream(transport_parsing,
104 stream_parsing);
105 }
106 } else {
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800107 GRPC_CHTTP2_FLOW_CREDIT_TRANSPORT("parse", transport_parsing,
108 outgoing_window, received_update);
Craig Tillera82950e2015-09-22 12:33:20 -0700109 }
110 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111
112 return GRPC_CHTTP2_PARSE_OK;
Craig Tiller190d3602015-02-18 09:23:38 -0800113}