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 1703142 : gpr_slice grpc_chttp2_window_update_create(gpr_uint32 id,
40 : gpr_uint32 window_update) {
41 1703142 : gpr_slice slice = gpr_slice_malloc(13);
42 1704482 : gpr_uint8 *p = GPR_SLICE_START_PTR(slice);
43 :
44 1704482 : GPR_ASSERT(window_update);
45 :
46 1704482 : *p++ = 0;
47 1704482 : *p++ = 0;
48 1704482 : *p++ = 4;
49 1704482 : *p++ = GRPC_CHTTP2_FRAME_WINDOW_UPDATE;
50 1704482 : *p++ = 0;
51 1704482 : *p++ = (gpr_uint8)(id >> 24);
52 1704482 : *p++ = (gpr_uint8)(id >> 16);
53 1704482 : *p++ = (gpr_uint8)(id >> 8);
54 1704482 : *p++ = (gpr_uint8)(id);
55 1704482 : *p++ = (gpr_uint8)(window_update >> 24);
56 1704482 : *p++ = (gpr_uint8)(window_update >> 16);
57 1704482 : *p++ = (gpr_uint8)(window_update >> 8);
58 1704482 : *p++ = (gpr_uint8)(window_update);
59 :
60 1704482 : return slice;
61 : }
62 :
63 1602866 : 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 1602866 : 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 1602871 : parser->byte = 0;
72 1602871 : parser->amount = 0;
73 1602871 : return GRPC_CHTTP2_PARSE_OK;
74 : }
75 :
76 1605772 : 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 1605772 : gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice);
81 1605772 : gpr_uint8 *const end = GPR_SLICE_END_PTR(slice);
82 1605772 : gpr_uint8 *cur = beg;
83 1605772 : grpc_chttp2_window_update_parser *p = parser;
84 :
85 9623214 : while (p->byte != 4 && cur != end) {
86 6411670 : p->amount |= ((gpr_uint32)*cur) << (8 * (3 - p->byte));
87 6411670 : cur++;
88 6411670 : p->byte++;
89 : }
90 :
91 1605772 : if (p->byte == 4) {
92 1602957 : if (p->amount == 0 || (p->amount & 0x80000000u)) {
93 0 : gpr_log(GPR_ERROR, "invalid window update bytes: %d", p->amount);
94 0 : return GRPC_CHTTP2_CONNECTION_ERROR;
95 : }
96 1602969 : GPR_ASSERT(is_last);
97 :
98 1602969 : if (transport_parsing->incoming_stream_id != 0) {
99 1598869 : if (stream_parsing != NULL) {
100 1596587 : GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("update", transport_parsing,
101 : stream_parsing, outgoing_window_update,
102 : p->amount);
103 1596587 : stream_parsing->outgoing_window_update += p->amount;
104 1596587 : grpc_chttp2_list_add_parsing_seen_stream(transport_parsing,
105 : stream_parsing);
106 : }
107 : } else {
108 4100 : GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT("update", transport_parsing,
109 : outgoing_window_update, p->amount);
110 4100 : transport_parsing->outgoing_window_update += p->amount;
111 : }
112 : }
113 :
114 1605700 : return GRPC_CHTTP2_PARSE_OK;
115 : }
|