QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame^] | 5 | #include <string> |
| 6 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/quic/core/quic_socket_address_coder.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8 | |
| 9 | namespace quic { |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | // For convenience, the values of these constants match the values of AF_INET |
| 14 | // and AF_INET6 on Linux. |
| 15 | const uint16_t kIPv4 = 2; |
| 16 | const uint16_t kIPv6 = 10; |
| 17 | |
| 18 | } // namespace |
| 19 | |
| 20 | QuicSocketAddressCoder::QuicSocketAddressCoder() {} |
| 21 | |
| 22 | QuicSocketAddressCoder::QuicSocketAddressCoder(const QuicSocketAddress& address) |
| 23 | : address_(address) {} |
| 24 | |
| 25 | QuicSocketAddressCoder::~QuicSocketAddressCoder() {} |
| 26 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 27 | std::string QuicSocketAddressCoder::Encode() const { |
| 28 | std::string serialized; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | uint16_t address_family; |
| 30 | switch (address_.host().address_family()) { |
| 31 | case IpAddressFamily::IP_V4: |
| 32 | address_family = kIPv4; |
| 33 | break; |
| 34 | case IpAddressFamily::IP_V6: |
| 35 | address_family = kIPv6; |
| 36 | break; |
| 37 | default: |
| 38 | return serialized; |
| 39 | } |
| 40 | serialized.append(reinterpret_cast<const char*>(&address_family), |
| 41 | sizeof(address_family)); |
| 42 | serialized.append(address_.host().ToPackedString()); |
| 43 | uint16_t port = address_.port(); |
| 44 | serialized.append(reinterpret_cast<const char*>(&port), sizeof(port)); |
| 45 | return serialized; |
| 46 | } |
| 47 | |
| 48 | bool QuicSocketAddressCoder::Decode(const char* data, size_t length) { |
| 49 | uint16_t address_family; |
| 50 | if (length < sizeof(address_family)) { |
| 51 | return false; |
| 52 | } |
| 53 | memcpy(&address_family, data, sizeof(address_family)); |
| 54 | data += sizeof(address_family); |
| 55 | length -= sizeof(address_family); |
| 56 | |
| 57 | size_t ip_length; |
| 58 | switch (address_family) { |
| 59 | case kIPv4: |
| 60 | ip_length = QuicIpAddress::kIPv4AddressSize; |
| 61 | break; |
| 62 | case kIPv6: |
| 63 | ip_length = QuicIpAddress::kIPv6AddressSize; |
| 64 | break; |
| 65 | default: |
| 66 | return false; |
| 67 | } |
| 68 | if (length < ip_length) { |
| 69 | return false; |
| 70 | } |
| 71 | std::vector<uint8_t> ip(ip_length); |
| 72 | memcpy(&ip[0], data, ip_length); |
| 73 | data += ip_length; |
| 74 | length -= ip_length; |
| 75 | |
| 76 | uint16_t port; |
| 77 | if (length != sizeof(port)) { |
| 78 | return false; |
| 79 | } |
| 80 | memcpy(&port, data, length); |
| 81 | |
| 82 | QuicIpAddress ip_address; |
| 83 | ip_address.FromPackedString(reinterpret_cast<const char*>(&ip[0]), ip_length); |
| 84 | address_ = QuicSocketAddress(ip_address, port); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | } // namespace quic |