Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: net/base/ip_endpoint.cc

Issue 10309002: Reimplements net::AddressList without struct addrinfo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added missing NET_EXPORT to *PortOnAddressList. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/string_number_conversions.h" 8 #include "base/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 {
19 // By definition, socklen_t is large enough to hold both sizes.
20 const socklen_t kSockaddrInSize = sizeof(struct sockaddr_in);
21 const socklen_t kSockaddrIn6Size = sizeof(struct sockaddr_in6);
22 }
23
18 IPEndPoint::IPEndPoint() : port_(0) {} 24 IPEndPoint::IPEndPoint() : port_(0) {}
19 25
20 IPEndPoint::~IPEndPoint() {} 26 IPEndPoint::~IPEndPoint() {}
21 27
22 IPEndPoint::IPEndPoint(const IPAddressNumber& address, int port) 28 IPEndPoint::IPEndPoint(const IPAddressNumber& address, int port)
23 : address_(address), 29 : address_(address),
24 port_(port) {} 30 port_(port) {}
25 31
26 IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) { 32 IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) {
27 address_ = endpoint.address_; 33 address_ = endpoint.address_;
28 port_ = endpoint.port_; 34 port_ = endpoint.port_;
29 } 35 }
30 36
31 int IPEndPoint::GetFamily() const { 37 int IPEndPoint::GetFamily() const {
32 switch (address_.size()) { 38 switch (address_.size()) {
33 case kIPv4AddressSize: { 39 case kIPv4AddressSize:
34 return AF_INET; 40 return AF_INET;
35 } 41 case kIPv6AddressSize:
36 case kIPv6AddressSize: {
37 return AF_INET6; 42 return AF_INET6;
38 } 43 default:
39 default: {
40 NOTREACHED() << "Bad IP address"; 44 NOTREACHED() << "Bad IP address";
41 return AF_UNSPEC; 45 return AF_UNSPEC;
42 }
43 } 46 }
44 } 47 }
45 48
46 bool IPEndPoint::ToSockAddr(struct sockaddr* address, 49 bool IPEndPoint::ToSockAddr(struct sockaddr* address,
47 size_t* address_length) const { 50 socklen_t* address_length) const {
48 DCHECK(address); 51 DCHECK(address);
49 DCHECK(address_length); 52 DCHECK(address_length);
50 switch (address_.size()) { 53 switch (address_.size()) {
51 case kIPv4AddressSize: { 54 case kIPv4AddressSize: {
52 if (*address_length < sizeof(struct sockaddr_in)) 55 if (*address_length < kSockaddrInSize)
53 return false; 56 return false;
54 *address_length = sizeof(struct sockaddr_in); 57 *address_length = kSockaddrInSize;
55 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address); 58 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address);
56 memset(addr, 0, sizeof(struct sockaddr_in)); 59 memset(addr, 0, sizeof(struct sockaddr_in));
57 addr->sin_family = AF_INET; 60 addr->sin_family = AF_INET;
58 addr->sin_port = base::HostToNet16(port_); 61 addr->sin_port = base::HostToNet16(port_);
59 memcpy(&addr->sin_addr, &address_[0], kIPv4AddressSize); 62 memcpy(&addr->sin_addr, &address_[0], kIPv4AddressSize);
60 break; 63 break;
61 } 64 }
62 case kIPv6AddressSize: { 65 case kIPv6AddressSize: {
63 if (*address_length < sizeof(struct sockaddr_in6)) 66 if (*address_length < kSockaddrIn6Size)
64 return false; 67 return false;
65 *address_length = sizeof(struct sockaddr_in6); 68 *address_length = kSockaddrIn6Size;
66 struct sockaddr_in6* addr6 = 69 struct sockaddr_in6* addr6 =
67 reinterpret_cast<struct sockaddr_in6*>(address); 70 reinterpret_cast<struct sockaddr_in6*>(address);
68 memset(addr6, 0, sizeof(struct sockaddr_in6)); 71 memset(addr6, 0, sizeof(struct sockaddr_in6));
69 addr6->sin6_family = AF_INET6; 72 addr6->sin6_family = AF_INET6;
70 addr6->sin6_port = base::HostToNet16(port_); 73 addr6->sin6_port = base::HostToNet16(port_);
71 memcpy(&addr6->sin6_addr, &address_[0], kIPv6AddressSize); 74 memcpy(&addr6->sin6_addr, &address_[0], kIPv6AddressSize);
72 break; 75 break;
73 } 76 }
74 default: { 77 default:
75 NOTREACHED() << "Bad IP address"; 78 return false;
76 break;
77 }
78 } 79 }
79 return true; 80 return true;
80 } 81 }
81 82
82 bool IPEndPoint::FromSockAddr(const struct sockaddr* address, 83 bool IPEndPoint::FromSockAddr(const struct sockaddr* address,
83 size_t address_length) { 84 socklen_t address_length) {
84 DCHECK(address); 85 DCHECK(address);
85 switch (address->sa_family) { 86 switch (address->sa_family) {
86 case AF_INET: { 87 case AF_INET: {
87 if (address_length < sizeof(struct sockaddr_in)) 88 if (address_length < kSockaddrInSize)
88 return false; 89 return false;
89 const struct sockaddr_in* addr = 90 const struct sockaddr_in* addr =
90 reinterpret_cast<const struct sockaddr_in*>(address); 91 reinterpret_cast<const struct sockaddr_in*>(address);
91 port_ = base::NetToHost16(addr->sin_port); 92 port_ = base::NetToHost16(addr->sin_port);
92 const char* bytes = reinterpret_cast<const char*>(&addr->sin_addr); 93 const char* bytes = reinterpret_cast<const char*>(&addr->sin_addr);
93 address_.assign(&bytes[0], &bytes[kIPv4AddressSize]); 94 address_.assign(&bytes[0], &bytes[kIPv4AddressSize]);
94 break; 95 break;
95 } 96 }
96 case AF_INET6: { 97 case AF_INET6: {
97 if (address_length < sizeof(struct sockaddr_in6)) 98 if (address_length < kSockaddrIn6Size)
98 return false; 99 return false;
99 const struct sockaddr_in6* addr = 100 const struct sockaddr_in6* addr =
100 reinterpret_cast<const struct sockaddr_in6*>(address); 101 reinterpret_cast<const struct sockaddr_in6*>(address);
101 port_ = base::NetToHost16(addr->sin6_port); 102 port_ = base::NetToHost16(addr->sin6_port);
102 const char* bytes = reinterpret_cast<const char*>(&addr->sin6_addr); 103 const char* bytes = reinterpret_cast<const char*>(&addr->sin6_addr);
103 address_.assign(&bytes[0], &bytes[kIPv6AddressSize]); 104 address_.assign(&bytes[0], &bytes[kIPv6AddressSize]);
104 break; 105 break;
105 } 106 }
106 default: { 107 default:
107 NOTREACHED() << "Bad IP address"; 108 return false;
108 break;
109 }
110 } 109 }
111 return true; 110 return true;
112 } 111 }
113 112
114 std::string IPEndPoint::ToString() const { 113 std::string IPEndPoint::ToString() const {
115 struct sockaddr_storage addr_storage; 114 SockaddrStorage storage;
116 size_t addr_len = sizeof(addr_storage); 115 if (!ToSockAddr(storage.addr, &storage.addr_len)) {
117 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage);
118 if (!ToSockAddr(addr, &addr_len)) {
119 return ""; 116 return "";
120 } 117 }
121 return NetAddressToStringWithPort(addr, addr_len); 118 return NetAddressToStringWithPort(storage.addr, storage.addr_len);
119 }
120
121 std::string IPEndPoint::ToStringWithoutPort() const {
122 SockaddrStorage storage;
123 if (!ToSockAddr(storage.addr, &storage.addr_len)) {
124 return "";
125 }
126 return NetAddressToString(storage.addr, storage.addr_len);
eroman 2012/05/04 01:08:41 We should change this at some point to not use soc
szym 2012/05/04 02:38:29 On the other hand, having to call getnameinfo to p
122 } 127 }
123 128
124 bool IPEndPoint::operator<(const IPEndPoint& that) const { 129 bool IPEndPoint::operator<(const IPEndPoint& that) const {
125 // Sort IPv4 before IPv6. 130 // Sort IPv4 before IPv6.
126 if (address_.size() != that.address_.size()) { 131 if (address_.size() != that.address_.size()) {
127 return address_.size() < that.address_.size(); 132 return address_.size() < that.address_.size();
128 } 133 }
129 if (address_ != that.address_) { 134 if (address_ != that.address_) {
130 return address_ < that.address_; 135 return address_ < that.address_;
131 } 136 }
132 return port_ < that.port_; 137 return port_ < that.port_;
133 } 138 }
134 139
135 bool IPEndPoint::operator==(const IPEndPoint& that) const { 140 bool IPEndPoint::operator==(const IPEndPoint& that) const {
136 return address_ == that.address_ && port_ == that.port_; 141 return address_ == that.address_ && port_ == that.port_;
137 } 142 }
138 143
139 } // namespace net 144 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698