blob: 3b4aa623f27e682fa1c52301ff0e2903cfb79fa7 [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_rst_stream.h"
Craig Tiller3719f072015-06-12 17:19:51 -070035#include "src/core/transport/chttp2/internal.h"
Craig Tiller7a290982015-05-19 12:49:54 -070036
37#include <grpc/support/log.h>
38
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include "src/core/transport/chttp2/frame.h"
40
Craig Tiller7536af02015-12-22 13:49:30 -080041gpr_slice grpc_chttp2_rst_stream_create(uint32_t id, uint32_t code) {
Craig Tillera82950e2015-09-22 12:33:20 -070042 gpr_slice slice = gpr_slice_malloc(13);
Craig Tiller7536af02015-12-22 13:49:30 -080043 uint8_t *p = GPR_SLICE_START_PTR(slice);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044
45 *p++ = 0;
46 *p++ = 0;
47 *p++ = 4;
48 *p++ = GRPC_CHTTP2_FRAME_RST_STREAM;
49 *p++ = 0;
Craig Tiller7536af02015-12-22 13:49:30 -080050 *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 Nobleb7ebd3b2014-11-26 16:33:03 -080058
59 return slice;
Craig Tiller190d3602015-02-18 09:23:38 -080060}
Craig Tiller7a290982015-05-19 12:49:54 -070061
Craig Tillera82950e2015-09-22 12:33:20 -070062grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_begin_frame(
Craig Tiller7536af02015-12-22 13:49:30 -080063 grpc_chttp2_rst_stream_parser *parser, uint32_t length, uint8_t flags) {
Craig Tillera82950e2015-09-22 12:33:20 -070064 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 Tiller7a290982015-05-19 12:49:54 -070069 parser->byte = 0;
70 return GRPC_CHTTP2_PARSE_OK;
71}
72
Craig Tillera82950e2015-09-22 12:33:20 -070073grpc_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 Tiller7536af02015-12-22 13:49:30 -080077 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 Tiller7a290982015-05-19 12:49:54 -070080 grpc_chttp2_rst_stream_parser *p = parser;
81
Craig Tillera82950e2015-09-22 12:33:20 -070082 while (p->byte != 4 && cur != end) {
83 p->reason_bytes[p->byte] = *cur;
84 cur++;
85 p->byte++;
86 }
Craig Tiller7a290982015-05-19 12:49:54 -070087
Craig Tillera82950e2015-09-22 12:33:20 -070088 if (p->byte == 4) {
89 GPR_ASSERT(is_last);
90 stream_parsing->received_close = 1;
91 stream_parsing->saw_rst_stream = 1;
Craig Tiller7536af02015-12-22 13:49:30 -080092 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 Tillera82950e2015-09-22 12:33:20 -070096 }
Craig Tiller7a290982015-05-19 12:49:54 -070097
98 return GRPC_CHTTP2_PARSE_OK;
99}