blob: 14ca3942641da367b55ffe25c529ddb36c67df58 [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
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/transport/chttp2/frame_ping.h"
35#include "src/core/lib/transport/chttp2/internal.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <string.h>
38
Craig Tiller3719f072015-06-12 17:19:51 -070039#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/support/log.h>
41
Craig Tiller7536af02015-12-22 13:49:30 -080042gpr_slice grpc_chttp2_ping_create(uint8_t ack, uint8_t *opaque_8bytes) {
Craig Tillera82950e2015-09-22 12:33:20 -070043 gpr_slice slice = gpr_slice_malloc(9 + 8);
Craig Tiller7536af02015-12-22 13:49:30 -080044 uint8_t *p = GPR_SLICE_START_PTR(slice);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045
46 *p++ = 0;
47 *p++ = 0;
48 *p++ = 8;
49 *p++ = GRPC_CHTTP2_FRAME_PING;
50 *p++ = ack ? 1 : 0;
51 *p++ = 0;
52 *p++ = 0;
53 *p++ = 0;
54 *p++ = 0;
Craig Tillera82950e2015-09-22 12:33:20 -070055 memcpy(p, opaque_8bytes, 8);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056
57 return slice;
58}
59
Craig Tillera82950e2015-09-22 12:33:20 -070060grpc_chttp2_parse_error grpc_chttp2_ping_parser_begin_frame(
Craig Tiller7536af02015-12-22 13:49:30 -080061 grpc_chttp2_ping_parser *parser, uint32_t length, uint8_t flags) {
Craig Tillera82950e2015-09-22 12:33:20 -070062 if (flags & 0xfe || length != 8) {
63 gpr_log(GPR_ERROR, "invalid ping: length=%d, flags=%02x", length, flags);
64 return GRPC_CHTTP2_CONNECTION_ERROR;
65 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066 parser->byte = 0;
67 parser->is_ack = flags;
68 return GRPC_CHTTP2_PARSE_OK;
69}
70
Craig Tillera82950e2015-09-22 12:33:20 -070071grpc_chttp2_parse_error grpc_chttp2_ping_parser_parse(
72 grpc_exec_ctx *exec_ctx, void *parser,
73 grpc_chttp2_transport_parsing *transport_parsing,
74 grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
Craig Tiller7536af02015-12-22 13:49:30 -080075 uint8_t *const beg = GPR_SLICE_START_PTR(slice);
76 uint8_t *const end = GPR_SLICE_END_PTR(slice);
77 uint8_t *cur = beg;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 grpc_chttp2_ping_parser *p = parser;
Craig Tiller3719f072015-06-12 17:19:51 -070079
Craig Tillera82950e2015-09-22 12:33:20 -070080 while (p->byte != 8 && cur != end) {
81 p->opaque_8bytes[p->byte] = *cur;
82 cur++;
83 p->byte++;
84 }
Craig Tiller45724b32015-09-22 10:42:19 -070085
Craig Tillera82950e2015-09-22 12:33:20 -070086 if (p->byte == 8) {
87 GPR_ASSERT(is_last);
88 if (p->is_ack) {
Craig Tiller28bf8912015-12-07 16:07:04 -080089 grpc_chttp2_ack_ping(exec_ctx, transport_parsing, p->opaque_8bytes);
Craig Tillera82950e2015-09-22 12:33:20 -070090 } else {
91 gpr_slice_buffer_add(&transport_parsing->qbuf,
92 grpc_chttp2_ping_create(1, p->opaque_8bytes));
Craig Tiller45724b32015-09-22 10:42:19 -070093 }
Craig Tillera82950e2015-09-22 12:33:20 -070094 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095
96 return GRPC_CHTTP2_PARSE_OK;
Craig Tiller190d3602015-02-18 09:23:38 -080097}