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"
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;
20 QuicSocketAddressCoder::QuicSocketAddressCoder() {
23 QuicSocketAddressCoder::QuicSocketAddressCoder(const IPEndPoint
& address
)
27 QuicSocketAddressCoder::~QuicSocketAddressCoder() {
30 string
QuicSocketAddressCoder::Encode() const {
32 uint16 address_family
;
33 switch (address_
.GetSockAddrFamily()) {
35 address_family
= kIPv4
;
38 address_family
= kIPv6
;
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
));
51 bool QuicSocketAddressCoder::Decode(const char* data
, size_t length
) {
52 uint16 address_family
;
53 if (length
< sizeof(address_family
)) {
56 memcpy(&address_family
, data
, sizeof(address_family
));
57 data
+= sizeof(address_family
);
58 length
-= sizeof(address_family
);
61 switch (address_family
) {
63 ip_length
= kIPv4AddressSize
;
66 ip_length
= kIPv6AddressSize
;
71 if (length
< ip_length
) {
74 IPAddressNumber
ip(ip_length
);
75 memcpy(&ip
[0], data
, ip_length
);
80 if (length
!= sizeof(port
)) {
83 memcpy(&port
, data
, length
);
85 address_
= IPEndPoint(ip
, port
);