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"
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;
22 QuicSocketAddressCoder::QuicSocketAddressCoder() {
25 QuicSocketAddressCoder::QuicSocketAddressCoder(const IPEndPoint
& address
)
29 QuicSocketAddressCoder::~QuicSocketAddressCoder() {
32 string
QuicSocketAddressCoder::Encode() const {
34 uint16 address_family
;
35 switch (address_
.GetSockAddrFamily()) {
37 address_family
= kIPv4
;
40 address_family
= kIPv6
;
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
));
53 bool QuicSocketAddressCoder::Decode(const char* data
, size_t length
) {
54 uint16 address_family
;
55 if (length
< sizeof(address_family
)) {
58 memcpy(&address_family
, data
, sizeof(address_family
));
59 data
+= sizeof(address_family
);
60 length
-= sizeof(address_family
);
63 switch (address_family
) {
65 ip_length
= kIPv4AddressSize
;
68 ip_length
= kIPv6AddressSize
;
73 if (length
< ip_length
) {
76 IPAddressNumber
ip(ip_length
);
77 memcpy(&ip
[0], data
, ip_length
);
82 if (length
!= sizeof(port
)) {
85 memcpy(&port
, data
, length
);
87 address_
= IPEndPoint(ip
, port
);