Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
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_window_update.h"
35 : #include "src/core/transport/chttp2/internal.h"
36 :
37 : #include <grpc/support/log.h>
38 :
39 35397 : gpr_slice grpc_chttp2_window_update_create(gpr_uint32 id,
40 : gpr_uint32 window_update) {
41 35397 : gpr_slice slice = gpr_slice_malloc(13);
42 35396 : gpr_uint8 *p = GPR_SLICE_START_PTR(slice);
43 :
44 35396 : GPR_ASSERT(window_update);
45 :
46 35396 : *p++ = 0;
47 35396 : *p++ = 0;
48 35396 : *p++ = 4;
49 35396 : *p++ = GRPC_CHTTP2_FRAME_WINDOW_UPDATE;
50 35396 : *p++ = 0;
51 35396 : *p++ = (gpr_uint8)(id >> 24);
52 35396 : *p++ = (gpr_uint8)(id >> 16);
53 35396 : *p++ = (gpr_uint8)(id >> 8);
54 35396 : *p++ = (gpr_uint8)(id);
55 35396 : *p++ = (gpr_uint8)(window_update >> 24);
56 35396 : *p++ = (gpr_uint8)(window_update >> 16);
57 35396 : *p++ = (gpr_uint8)(window_update >> 8);
58 35396 : *p++ = (gpr_uint8)(window_update);
59 :
60 35396 : return slice;
61 : }
62 :
63 34802 : grpc_chttp2_parse_error grpc_chttp2_window_update_parser_begin_frame(
64 : grpc_chttp2_window_update_parser *parser, gpr_uint32 length,
65 : gpr_uint8 flags) {
66 34802 : if (flags || length != 4) {
67 0 : gpr_log(GPR_ERROR, "invalid window update: length=%d, flags=%02x", length,
68 : flags);
69 0 : return GRPC_CHTTP2_CONNECTION_ERROR;
70 : }
71 34803 : parser->byte = 0;
72 34803 : parser->amount = 0;
73 34803 : return GRPC_CHTTP2_PARSE_OK;
74 : }
75 :
76 35938 : grpc_chttp2_parse_error grpc_chttp2_window_update_parser_parse(
77 : grpc_exec_ctx *exec_ctx, void *parser,
78 : grpc_chttp2_transport_parsing *transport_parsing,
79 : grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
80 35938 : gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice);
81 35938 : gpr_uint8 *const end = GPR_SLICE_END_PTR(slice);
82 35829 : gpr_uint8 *cur = beg;
83 35829 : grpc_chttp2_window_update_parser *p = parser;
84 :
85 211051 : while (p->byte != 4 && cur != end) {
86 139175 : p->amount |= ((gpr_uint32)*cur) << (8 * (3 - p->byte));
87 139175 : cur++;
88 139175 : p->byte++;
89 : }
90 :
91 35938 : if (p->byte == 4) {
92 34800 : gpr_uint32 received_update = p->amount;
93 34800 : if (received_update == 0 || (received_update & 0x80000000u)) {
94 2 : gpr_log(GPR_ERROR, "invalid window update bytes: %d", p->amount);
95 0 : return GRPC_CHTTP2_CONNECTION_ERROR;
96 : }
97 34798 : GPR_ASSERT(is_last);
98 :
99 34798 : if (transport_parsing->incoming_stream_id != 0) {
100 28866 : if (stream_parsing != NULL) {
101 28846 : GRPC_CHTTP2_FLOW_CREDIT_STREAM("parse", transport_parsing,
102 : stream_parsing, outgoing_window,
103 : received_update);
104 28846 : grpc_chttp2_list_add_parsing_seen_stream(transport_parsing,
105 : stream_parsing);
106 : }
107 : } else {
108 5932 : GRPC_CHTTP2_FLOW_CREDIT_TRANSPORT("parse", transport_parsing,
109 : outgoing_window, received_update);
110 : }
111 : }
112 :
113 35826 : return GRPC_CHTTP2_PARSE_OK;
114 : }
|