OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/ip_endpoint.h" | 5 #include "net/base/ip_endpoint.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" |
10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
11 #include <winsock2.h> | 11 #include <winsock2.h> |
12 #elif defined(OS_POSIX) | 12 #elif defined(OS_POSIX) |
13 #include <netinet/in.h> | 13 #include <netinet/in.h> |
14 #endif | 14 #endif |
15 | 15 |
16 namespace net { | 16 namespace net { |
17 | 17 |
18 namespace { | 18 namespace { |
19 // By definition, socklen_t is large enough to hold both sizes. | 19 // By definition, socklen_t is large enough to hold both sizes. |
20 const socklen_t kSockaddrInSize = sizeof(struct sockaddr_in); | 20 const socklen_t kSockaddrInSize = sizeof(struct sockaddr_in); |
21 const socklen_t kSockaddrIn6Size = sizeof(struct sockaddr_in6); | 21 const socklen_t kSockaddrIn6Size = sizeof(struct sockaddr_in6); |
22 } | 22 } |
23 | 23 |
24 IPEndPoint::IPEndPoint() : port_(0) {} | 24 IPEndPoint::IPEndPoint() : port_(0) { |
| 25 } |
25 | 26 |
26 IPEndPoint::~IPEndPoint() {} | 27 IPEndPoint::~IPEndPoint() { |
| 28 } |
27 | 29 |
28 IPEndPoint::IPEndPoint(const IPAddressNumber& address, int port) | 30 IPEndPoint::IPEndPoint(const IPAddressNumber& address, int port) |
29 : address_(address), | 31 : address_(address), port_(port) { |
30 port_(port) {} | 32 } |
31 | 33 |
32 IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) { | 34 IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) { |
33 address_ = endpoint.address_; | 35 address_ = endpoint.address_; |
34 port_ = endpoint.port_; | 36 port_ = endpoint.port_; |
35 } | 37 } |
36 | 38 |
37 AddressFamily IPEndPoint::GetFamily() const { | 39 AddressFamily IPEndPoint::GetFamily() const { |
38 return GetAddressFamily(address_); | 40 return GetAddressFamily(address_); |
39 } | 41 } |
40 | 42 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 return true; | 86 return true; |
85 } | 87 } |
86 | 88 |
87 bool IPEndPoint::FromSockAddr(const struct sockaddr* sock_addr, | 89 bool IPEndPoint::FromSockAddr(const struct sockaddr* sock_addr, |
88 socklen_t sock_addr_len) { | 90 socklen_t sock_addr_len) { |
89 DCHECK(sock_addr); | 91 DCHECK(sock_addr); |
90 | 92 |
91 const uint8* address; | 93 const uint8* address; |
92 size_t address_len; | 94 size_t address_len; |
93 uint16 port; | 95 uint16 port; |
94 if (!GetIPAddressFromSockAddr(sock_addr, sock_addr_len, &address, | 96 if (!GetIPAddressFromSockAddr( |
95 &address_len, &port)) { | 97 sock_addr, sock_addr_len, &address, &address_len, &port)) { |
96 return false; | 98 return false; |
97 } | 99 } |
98 | 100 |
99 address_.assign(address, address + address_len); | 101 address_.assign(address, address + address_len); |
100 port_ = port; | 102 port_ = port; |
101 return true; | 103 return true; |
102 } | 104 } |
103 | 105 |
104 std::string IPEndPoint::ToString() const { | 106 std::string IPEndPoint::ToString() const { |
105 return IPAddressToStringWithPort(address_, port_); | 107 return IPAddressToStringWithPort(address_, port_); |
(...skipping 12 matching lines...) Expand all Loading... |
118 return address_ < that.address_; | 120 return address_ < that.address_; |
119 } | 121 } |
120 return port_ < that.port_; | 122 return port_ < that.port_; |
121 } | 123 } |
122 | 124 |
123 bool IPEndPoint::operator==(const IPEndPoint& that) const { | 125 bool IPEndPoint::operator==(const IPEndPoint& that) const { |
124 return address_ == that.address_ && port_ == that.port_; | 126 return address_ == that.address_ && port_ == that.port_; |
125 } | 127 } |
126 | 128 |
127 } // namespace net | 129 } // namespace net |
OLD | NEW |