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_ping.h"
35 : #include "src/core/transport/chttp2/internal.h"
36 :
37 : #include <string.h>
38 :
39 : #include <grpc/support/alloc.h>
40 : #include <grpc/support/log.h>
41 :
42 0 : gpr_slice grpc_chttp2_ping_create(gpr_uint8 ack, gpr_uint8 *opaque_8bytes) {
43 0 : gpr_slice slice = gpr_slice_malloc(9 + 8);
44 0 : gpr_uint8 *p = GPR_SLICE_START_PTR(slice);
45 :
46 0 : *p++ = 0;
47 0 : *p++ = 0;
48 0 : *p++ = 8;
49 0 : *p++ = GRPC_CHTTP2_FRAME_PING;
50 0 : *p++ = ack ? 1 : 0;
51 0 : *p++ = 0;
52 0 : *p++ = 0;
53 0 : *p++ = 0;
54 0 : *p++ = 0;
55 0 : memcpy(p, opaque_8bytes, 8);
56 :
57 0 : return slice;
58 : }
59 :
60 0 : grpc_chttp2_parse_error grpc_chttp2_ping_parser_begin_frame(
61 : grpc_chttp2_ping_parser *parser, gpr_uint32 length, gpr_uint8 flags) {
62 0 : if (flags & 0xfe || length != 8) {
63 0 : gpr_log(GPR_ERROR, "invalid ping: length=%d, flags=%02x", length, flags);
64 0 : return GRPC_CHTTP2_CONNECTION_ERROR;
65 : }
66 0 : parser->byte = 0;
67 0 : parser->is_ack = flags;
68 0 : return GRPC_CHTTP2_PARSE_OK;
69 : }
70 :
71 0 : grpc_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) {
75 0 : gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice);
76 0 : gpr_uint8 *const end = GPR_SLICE_END_PTR(slice);
77 0 : gpr_uint8 *cur = beg;
78 0 : grpc_chttp2_ping_parser *p = parser;
79 : grpc_chttp2_outstanding_ping *ping;
80 :
81 0 : while (p->byte != 8 && cur != end) {
82 0 : p->opaque_8bytes[p->byte] = *cur;
83 0 : cur++;
84 0 : p->byte++;
85 : }
86 :
87 0 : if (p->byte == 8) {
88 0 : GPR_ASSERT(is_last);
89 0 : if (p->is_ack) {
90 0 : for (ping = transport_parsing->pings.next;
91 0 : ping != &transport_parsing->pings; ping = ping->next) {
92 0 : if (0 == memcmp(p->opaque_8bytes, ping->id, 8)) {
93 0 : grpc_exec_ctx_enqueue(exec_ctx, ping->on_recv, 1);
94 : }
95 0 : ping->next->prev = ping->prev;
96 0 : ping->prev->next = ping->next;
97 0 : gpr_free(ping);
98 : }
99 : } else {
100 0 : gpr_slice_buffer_add(&transport_parsing->qbuf,
101 0 : grpc_chttp2_ping_create(1, p->opaque_8bytes));
102 : }
103 : }
104 :
105 0 : return GRPC_CHTTP2_PARSE_OK;
106 : }
|