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

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

Issue 1932363003: Remove net::IPAddressNumber. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 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
« no previous file with comments | « net/base/ip_endpoint.h ('k') | net/cert/x509_certificate_ios.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #if defined(OS_WIN) 9 #if defined(OS_WIN)
10 #include <winsock2.h> 10 #include <winsock2.h>
(...skipping 26 matching lines...) Expand all
37 socklen_t sock_addr_len, 37 socklen_t sock_addr_len,
38 const uint8_t** address, 38 const uint8_t** address,
39 size_t* address_len, 39 size_t* address_len,
40 uint16_t* port) { 40 uint16_t* port) {
41 if (sock_addr->sa_family == AF_INET) { 41 if (sock_addr->sa_family == AF_INET) {
42 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in))) 42 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in)))
43 return false; 43 return false;
44 const struct sockaddr_in* addr = 44 const struct sockaddr_in* addr =
45 reinterpret_cast<const struct sockaddr_in*>(sock_addr); 45 reinterpret_cast<const struct sockaddr_in*>(sock_addr);
46 *address = reinterpret_cast<const uint8_t*>(&addr->sin_addr); 46 *address = reinterpret_cast<const uint8_t*>(&addr->sin_addr);
47 *address_len = kIPv4AddressSize; 47 *address_len = IPAddress::kIPv4AddressSize;
48 if (port) 48 if (port)
49 *port = base::NetToHost16(addr->sin_port); 49 *port = base::NetToHost16(addr->sin_port);
50 return true; 50 return true;
51 } 51 }
52 52
53 if (sock_addr->sa_family == AF_INET6) { 53 if (sock_addr->sa_family == AF_INET6) {
54 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6))) 54 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6)))
55 return false; 55 return false;
56 const struct sockaddr_in6* addr = 56 const struct sockaddr_in6* addr =
57 reinterpret_cast<const struct sockaddr_in6*>(sock_addr); 57 reinterpret_cast<const struct sockaddr_in6*>(sock_addr);
58 *address = reinterpret_cast<const uint8_t*>(&addr->sin6_addr); 58 *address = reinterpret_cast<const uint8_t*>(&addr->sin6_addr);
59 *address_len = kIPv6AddressSize; 59 *address_len = IPAddress::kIPv6AddressSize;
60 if (port) 60 if (port)
61 *port = base::NetToHost16(addr->sin6_port); 61 *port = base::NetToHost16(addr->sin6_port);
62 return true; 62 return true;
63 } 63 }
64 64
65 #if defined(OS_WIN) 65 #if defined(OS_WIN)
66 if (sock_addr->sa_family == AF_BTH) { 66 if (sock_addr->sa_family == AF_BTH) {
67 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH))) 67 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH)))
68 return false; 68 return false;
69 const SOCKADDR_BTH* addr = reinterpret_cast<const SOCKADDR_BTH*>(sock_addr); 69 const SOCKADDR_BTH* addr = reinterpret_cast<const SOCKADDR_BTH*>(sock_addr);
70 *address = reinterpret_cast<const uint8_t*>(&addr->btAddr); 70 *address = reinterpret_cast<const uint8_t*>(&addr->btAddr);
71 *address_len = kBluetoothAddressSize; 71 *address_len = kBluetoothAddressSize;
72 if (port) 72 if (port)
73 *port = static_cast<uint16_t>(addr->port); 73 *port = static_cast<uint16_t>(addr->port);
74 return true; 74 return true;
75 } 75 }
76 #endif 76 #endif
77 77
78 return false; // Unrecognized |sa_family|. 78 return false; // Unrecognized |sa_family|.
79 } 79 }
80 80
81 } // namespace 81 } // namespace
82 82
83 IPEndPoint::IPEndPoint() : port_(0) {} 83 IPEndPoint::IPEndPoint() : port_(0) {}
84 84
85 IPEndPoint::~IPEndPoint() {} 85 IPEndPoint::~IPEndPoint() {}
86 86
87 IPEndPoint::IPEndPoint(const IPAddressNumber& address, uint16_t port)
88 : address_(address), port_(port) {
89 }
90
91 IPEndPoint::IPEndPoint(const IPAddress& address, uint16_t port) 87 IPEndPoint::IPEndPoint(const IPAddress& address, uint16_t port)
92 : address_(address), port_(port) {} 88 : address_(address), port_(port) {}
93 89
94 IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) { 90 IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) {
95 address_ = endpoint.address_; 91 address_ = endpoint.address_;
96 port_ = endpoint.port_; 92 port_ = endpoint.port_;
97 } 93 }
98 94
99 AddressFamily IPEndPoint::GetFamily() const { 95 AddressFamily IPEndPoint::GetFamily() const {
100 return GetAddressFamily(address_); 96 return GetAddressFamily(address_);
101 } 97 }
102 98
103 int IPEndPoint::GetSockAddrFamily() const { 99 int IPEndPoint::GetSockAddrFamily() const {
104 switch (address_.size()) { 100 switch (address_.size()) {
105 case kIPv4AddressSize: 101 case IPAddress::kIPv4AddressSize:
106 return AF_INET; 102 return AF_INET;
107 case kIPv6AddressSize: 103 case IPAddress::kIPv6AddressSize:
108 return AF_INET6; 104 return AF_INET6;
109 default: 105 default:
110 NOTREACHED() << "Bad IP address"; 106 NOTREACHED() << "Bad IP address";
111 return AF_UNSPEC; 107 return AF_UNSPEC;
112 } 108 }
113 } 109 }
114 110
115 bool IPEndPoint::ToSockAddr(struct sockaddr* address, 111 bool IPEndPoint::ToSockAddr(struct sockaddr* address,
116 socklen_t* address_length) const { 112 socklen_t* address_length) const {
117 DCHECK(address); 113 DCHECK(address);
118 DCHECK(address_length); 114 DCHECK(address_length);
119 switch (address_.size()) { 115 switch (address_.size()) {
120 case kIPv4AddressSize: { 116 case IPAddress::kIPv4AddressSize: {
121 if (*address_length < kSockaddrInSize) 117 if (*address_length < kSockaddrInSize)
122 return false; 118 return false;
123 *address_length = kSockaddrInSize; 119 *address_length = kSockaddrInSize;
124 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address); 120 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address);
125 memset(addr, 0, sizeof(struct sockaddr_in)); 121 memset(addr, 0, sizeof(struct sockaddr_in));
126 addr->sin_family = AF_INET; 122 addr->sin_family = AF_INET;
127 addr->sin_port = base::HostToNet16(port_); 123 addr->sin_port = base::HostToNet16(port_);
128 memcpy(&addr->sin_addr, &address_.bytes()[0], kIPv4AddressSize); 124 memcpy(&addr->sin_addr, address_.bytes().data(),
125 IPAddress::kIPv4AddressSize);
129 break; 126 break;
130 } 127 }
131 case kIPv6AddressSize: { 128 case IPAddress::kIPv6AddressSize: {
132 if (*address_length < kSockaddrIn6Size) 129 if (*address_length < kSockaddrIn6Size)
133 return false; 130 return false;
134 *address_length = kSockaddrIn6Size; 131 *address_length = kSockaddrIn6Size;
135 struct sockaddr_in6* addr6 = 132 struct sockaddr_in6* addr6 =
136 reinterpret_cast<struct sockaddr_in6*>(address); 133 reinterpret_cast<struct sockaddr_in6*>(address);
137 memset(addr6, 0, sizeof(struct sockaddr_in6)); 134 memset(addr6, 0, sizeof(struct sockaddr_in6));
138 addr6->sin6_family = AF_INET6; 135 addr6->sin6_family = AF_INET6;
139 addr6->sin6_port = base::HostToNet16(port_); 136 addr6->sin6_port = base::HostToNet16(port_);
140 memcpy(&addr6->sin6_addr, &address_.bytes()[0], kIPv6AddressSize); 137 memcpy(&addr6->sin6_addr, address_.bytes().data(),
138 IPAddress::kIPv6AddressSize);
141 break; 139 break;
142 } 140 }
143 default: 141 default:
144 return false; 142 return false;
145 } 143 }
146 return true; 144 return true;
147 } 145 }
148 146
149 bool IPEndPoint::FromSockAddr(const struct sockaddr* sock_addr, 147 bool IPEndPoint::FromSockAddr(const struct sockaddr* sock_addr,
150 socklen_t sock_addr_len) { 148 socklen_t sock_addr_len) {
(...skipping 26 matching lines...) Expand all
177 return address_.size() < other.address_.size(); 175 return address_.size() < other.address_.size();
178 } 176 }
179 return std::tie(address_, port_) < std::tie(other.address_, other.port_); 177 return std::tie(address_, port_) < std::tie(other.address_, other.port_);
180 } 178 }
181 179
182 bool IPEndPoint::operator==(const IPEndPoint& other) const { 180 bool IPEndPoint::operator==(const IPEndPoint& other) const {
183 return address_ == other.address_ && port_ == other.port_; 181 return address_ == other.address_ && port_ == other.port_;
184 } 182 }
185 183
186 } // namespace net 184 } // namespace net
OLDNEW
« no previous file with comments | « net/base/ip_endpoint.h ('k') | net/cert/x509_certificate_ios.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698