Clean up URLFetcher unit tests, part 8.
[chromium-blink-merge.git] / net / quic / quic_socket_address_coder.cc
blob77e595f2d65f27403ad374557241688c031ad7ea
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 using std::string;
9 namespace net {
11 namespace {
13 // For convenience, the values of these constants match the values of AF_INET
14 // and AF_INET6 on Linux.
15 const uint16 kIPv4 = 2;
16 const uint16 kIPv6 = 10;
18 } // namespace
20 QuicSocketAddressCoder::QuicSocketAddressCoder() {
23 QuicSocketAddressCoder::QuicSocketAddressCoder(const IPEndPoint& address)
24 : address_(address) {
27 QuicSocketAddressCoder::~QuicSocketAddressCoder() {
30 string QuicSocketAddressCoder::Encode() const {
31 string serialized;
32 uint16 address_family;
33 switch (address_.GetSockAddrFamily()) {
34 case AF_INET:
35 address_family = kIPv4;
36 break;
37 case AF_INET6:
38 address_family = kIPv6;
39 break;
40 default:
41 return serialized;
43 serialized.append(reinterpret_cast<const char*>(&address_family),
44 sizeof(address_family));
45 serialized.append(IPAddressToPackedString(address_.address()));
46 uint16 port = address_.port();
47 serialized.append(reinterpret_cast<const char*>(&port), sizeof(port));
48 return serialized;
51 bool QuicSocketAddressCoder::Decode(const char* data, size_t length) {
52 uint16 address_family;
53 if (length < sizeof(address_family)) {
54 return false;
56 memcpy(&address_family, data, sizeof(address_family));
57 data += sizeof(address_family);
58 length -= sizeof(address_family);
60 size_t ip_length;
61 switch (address_family) {
62 case kIPv4:
63 ip_length = kIPv4AddressSize;
64 break;
65 case kIPv6:
66 ip_length = kIPv6AddressSize;
67 break;
68 default:
69 return false;
71 if (length < ip_length) {
72 return false;
74 IPAddressNumber ip(ip_length);
75 memcpy(&ip[0], data, ip_length);
76 data += ip_length;
77 length -= ip_length;
79 uint16 port;
80 if (length != sizeof(port)) {
81 return false;
83 memcpy(&port, data, length);
85 address_ = IPEndPoint(ip, port);
86 return true;
89 } // namespace net