[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / quic / quic_socket_address_coder.cc
blob5dba82107f5a2722548e6f08114b1fe5ca8d9829
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.
5 #include "net/quic/quic_socket_address_coder.h"
7 #include "net/base/sys_addrinfo.h"
9 using std::string;
11 namespace net {
13 namespace {
15 // For convenience, the values of these constants match the values of AF_INET
16 // and AF_INET6 on Linux.
17 const uint16 kIPv4 = 2;
18 const uint16 kIPv6 = 10;
20 } // namespace
22 QuicSocketAddressCoder::QuicSocketAddressCoder() {
25 QuicSocketAddressCoder::QuicSocketAddressCoder(const IPEndPoint& address)
26 : address_(address) {
29 QuicSocketAddressCoder::~QuicSocketAddressCoder() {
32 string QuicSocketAddressCoder::Encode() const {
33 string serialized;
34 uint16 address_family;
35 switch (address_.GetSockAddrFamily()) {
36 case AF_INET:
37 address_family = kIPv4;
38 break;
39 case AF_INET6:
40 address_family = kIPv6;
41 break;
42 default:
43 return serialized;
45 serialized.append(reinterpret_cast<const char*>(&address_family),
46 sizeof(address_family));
47 serialized.append(IPAddressToPackedString(address_.address()));
48 uint16 port = address_.port();
49 serialized.append(reinterpret_cast<const char*>(&port), sizeof(port));
50 return serialized;
53 bool QuicSocketAddressCoder::Decode(const char* data, size_t length) {
54 uint16 address_family;
55 if (length < sizeof(address_family)) {
56 return false;
58 memcpy(&address_family, data, sizeof(address_family));
59 data += sizeof(address_family);
60 length -= sizeof(address_family);
62 size_t ip_length;
63 switch (address_family) {
64 case kIPv4:
65 ip_length = kIPv4AddressSize;
66 break;
67 case kIPv6:
68 ip_length = kIPv6AddressSize;
69 break;
70 default:
71 return false;
73 if (length < ip_length) {
74 return false;
76 IPAddressNumber ip(ip_length);
77 memcpy(&ip[0], data, ip_length);
78 data += ip_length;
79 length -= ip_length;
81 uint16 port;
82 if (length != sizeof(port)) {
83 return false;
85 memcpy(&port, data, length);
87 address_ = IPEndPoint(ip, port);
88 return true;
91 } // namespace net