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

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

Issue 1408803010: Add IPAddress class as a replacement for the IPAddressNumber typedef. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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
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/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/sys_byteorder.h"
10 #if defined(OS_WIN) 9 #if defined(OS_WIN)
11 #include <winsock2.h> 10 #include <winsock2.h>
12 #elif defined(OS_POSIX) 11 #elif defined(OS_POSIX)
13 #include <netinet/in.h> 12 #include <netinet/in.h>
14 #endif 13 #endif
15 #include "net/base/net_util.h" 14 #include "net/base/net_util.h"
16 15
17 namespace net { 16 namespace net {
18 17
19 namespace {
20 // By definition, socklen_t is large enough to hold both sizes.
21 const socklen_t kSockaddrInSize = sizeof(struct sockaddr_in);
22 const socklen_t kSockaddrIn6Size = sizeof(struct sockaddr_in6);
23 }
24
25 IPEndPoint::IPEndPoint() : port_(0) {} 18 IPEndPoint::IPEndPoint() : port_(0) {}
26 19
27 IPEndPoint::~IPEndPoint() {} 20 IPEndPoint::~IPEndPoint() {}
28 21
29 IPEndPoint::IPEndPoint(const IPAddressNumber& address, uint16_t port) 22 IPEndPoint::IPEndPoint(const IPAddressNumber& address, uint16_t port)
30 : address_(address), port_(port) { 23 : address_(IPAddress::FromLegacy(address)), port_(port) {}
31 } 24
25 IPEndPoint::IPEndPoint(const IPAddress& address, uint16_t port)
26 : address_(address), port_(port) {}
32 27
33 IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) { 28 IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) {
34 address_ = endpoint.address_; 29 address_ = endpoint.address_;
35 port_ = endpoint.port_; 30 port_ = endpoint.port_;
36 } 31 }
37 32
38 AddressFamily IPEndPoint::GetFamily() const { 33 AddressFamily IPEndPoint::GetFamily() const {
39 return GetAddressFamily(address_); 34 return address_.GetAddressFamily();
40 } 35 }
41 36
42 int IPEndPoint::GetSockAddrFamily() const { 37 int IPEndPoint::GetSockAddrFamily() const {
43 switch (address_.size()) { 38 switch (address_.size()) {
44 case kIPv4AddressSize: 39 case IPAddress::kIPv4AddressSize:
45 return AF_INET; 40 return AF_INET;
46 case kIPv6AddressSize: 41 case IPAddress::kIPv6AddressSize:
47 return AF_INET6; 42 return AF_INET6;
48 default: 43 default:
49 NOTREACHED() << "Bad IP address"; 44 NOTREACHED() << "Bad IP address";
50 return AF_UNSPEC; 45 return AF_UNSPEC;
51 } 46 }
52 } 47 }
53 48
54 bool IPEndPoint::ToSockAddr(struct sockaddr* address, 49 bool IPEndPoint::ToSockAddr(struct sockaddr* address,
55 socklen_t* address_length) const { 50 socklen_t* address_length) const {
56 DCHECK(address); 51 DCHECK(address);
57 DCHECK(address_length); 52 DCHECK(address_length);
58 switch (address_.size()) { 53 return address_.ToSockAddrWithPort(address, address_length, port_);
59 case kIPv4AddressSize: {
60 if (*address_length < kSockaddrInSize)
61 return false;
62 *address_length = kSockaddrInSize;
63 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address);
64 memset(addr, 0, sizeof(struct sockaddr_in));
65 addr->sin_family = AF_INET;
66 addr->sin_port = base::HostToNet16(port_);
67 memcpy(&addr->sin_addr, &address_[0], kIPv4AddressSize);
68 break;
69 }
70 case kIPv6AddressSize: {
71 if (*address_length < kSockaddrIn6Size)
72 return false;
73 *address_length = kSockaddrIn6Size;
74 struct sockaddr_in6* addr6 =
75 reinterpret_cast<struct sockaddr_in6*>(address);
76 memset(addr6, 0, sizeof(struct sockaddr_in6));
77 addr6->sin6_family = AF_INET6;
78 addr6->sin6_port = base::HostToNet16(port_);
79 memcpy(&addr6->sin6_addr, &address_[0], kIPv6AddressSize);
80 break;
81 }
82 default:
83 return false;
84 }
85 return true;
86 } 54 }
87 55
88 bool IPEndPoint::FromSockAddr(const struct sockaddr* sock_addr, 56 bool IPEndPoint::FromSockAddr(const struct sockaddr* sock_addr,
89 socklen_t sock_addr_len) { 57 socklen_t sock_addr_len) {
90 DCHECK(sock_addr); 58 DCHECK(sock_addr);
91 59
92 const uint8_t* address; 60 const uint8_t* address;
93 size_t address_len; 61 size_t address_len;
94 uint16_t port; 62 uint16_t port;
95 if (!GetIPAddressFromSockAddr(sock_addr, sock_addr_len, &address, 63 if (!GetIPAddressFromSockAddr(sock_addr, sock_addr_len, &address,
96 &address_len, &port)) { 64 &address_len, &port)) {
97 return false; 65 return false;
98 } 66 }
99 67
100 address_.assign(address, address + address_len); 68 std::vector<unsigned char> vec(address, address + address_len);
69 IPAddress ip_address(vec);
70 address_ = ip_address;
101 port_ = port; 71 port_ = port;
102 return true; 72 return true;
103 } 73 }
104 74
105 std::string IPEndPoint::ToString() const { 75 std::string IPEndPoint::ToString() const {
106 return IPAddressToStringWithPort(address_, port_); 76 return address_.ToStringWithPort(port_);
107 } 77 }
108 78
109 std::string IPEndPoint::ToStringWithoutPort() const { 79 std::string IPEndPoint::ToStringWithoutPort() const {
110 return IPAddressToString(address_); 80 return address_.ToString();
111 } 81 }
112 82
113 bool IPEndPoint::operator<(const IPEndPoint& that) const { 83 bool IPEndPoint::operator<(const IPEndPoint& that) const {
114 // Sort IPv4 before IPv6. 84 if (address_ == that.address_) {
115 if (address_.size() != that.address_.size()) { 85 return port_ < that.port_;
116 return address_.size() < that.address_.size();
117 } 86 }
118 if (address_ != that.address_) { 87 return address_ < that.address_;
119 return address_ < that.address_;
120 }
121 return port_ < that.port_;
122 } 88 }
123 89
124 bool IPEndPoint::operator==(const IPEndPoint& that) const { 90 bool IPEndPoint::operator==(const IPEndPoint& that) const {
125 return address_ == that.address_ && port_ == that.port_; 91 return address_ == that.address_ && port_ == that.port_;
126 } 92 }
127 93
128 } // namespace net 94 } // namespace net
OLDNEW
« net/base/ip_address.h ('K') | « net/base/ip_endpoint.h ('k') | net/net.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698