Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 8a9fd52 | 2016-03-25 17:09:29 -0700 | [diff] [blame^] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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" |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 35 | #include "src/core/transport/chttp2/internal.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 37 | #include <string.h> |
| 38 | |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/log.h> |
| 41 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 42 | gpr_slice grpc_chttp2_ping_create(uint8_t ack, uint8_t *opaque_8bytes) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 43 | gpr_slice slice = gpr_slice_malloc(9 + 8); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 44 | uint8_t *p = GPR_SLICE_START_PTR(slice); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | |
| 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 55 | memcpy(p, opaque_8bytes, 8); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | |
| 57 | return slice; |
| 58 | } |
| 59 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 60 | grpc_chttp2_parse_error grpc_chttp2_ping_parser_begin_frame( |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 61 | grpc_chttp2_ping_parser *parser, uint32_t length, uint8_t flags) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 62 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | parser->byte = 0; |
| 67 | parser->is_ack = flags; |
| 68 | return GRPC_CHTTP2_PARSE_OK; |
| 69 | } |
| 70 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 71 | 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) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 75 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | grpc_chttp2_ping_parser *p = parser; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 79 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | while (p->byte != 8 && cur != end) { |
| 81 | p->opaque_8bytes[p->byte] = *cur; |
| 82 | cur++; |
| 83 | p->byte++; |
| 84 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 85 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | if (p->byte == 8) { |
| 87 | GPR_ASSERT(is_last); |
| 88 | if (p->is_ack) { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 89 | grpc_chttp2_ack_ping(exec_ctx, transport_parsing, p->opaque_8bytes); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | } else { |
| 91 | gpr_slice_buffer_add(&transport_parsing->qbuf, |
| 92 | grpc_chttp2_ping_create(1, p->opaque_8bytes)); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 93 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 94 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 95 | |
| 96 | return GRPC_CHTTP2_PARSE_OK; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 97 | } |