OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
10 #include <winsock2.h> | 11 #include <winsock2.h> |
11 #elif defined(OS_POSIX) | 12 #elif defined(OS_POSIX) |
12 #include <netinet/in.h> | 13 #include <netinet/in.h> |
13 #endif | 14 #endif |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 | 17 |
17 IPEndPoint::IPEndPoint() : port_(0) {} | 18 IPEndPoint::IPEndPoint() : port_(0) {} |
18 | 19 |
(...skipping 28 matching lines...) Expand all Loading... |
47 DCHECK(address); | 48 DCHECK(address); |
48 DCHECK(address_length); | 49 DCHECK(address_length); |
49 switch (address_.size()) { | 50 switch (address_.size()) { |
50 case kIPv4AddressSize: { | 51 case kIPv4AddressSize: { |
51 if (*address_length < sizeof(struct sockaddr_in)) | 52 if (*address_length < sizeof(struct sockaddr_in)) |
52 return false; | 53 return false; |
53 *address_length = sizeof(struct sockaddr_in); | 54 *address_length = sizeof(struct sockaddr_in); |
54 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address); | 55 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address); |
55 memset(addr, 0, sizeof(struct sockaddr_in)); | 56 memset(addr, 0, sizeof(struct sockaddr_in)); |
56 addr->sin_family = AF_INET; | 57 addr->sin_family = AF_INET; |
57 addr->sin_port = htons(port_); | 58 addr->sin_port = base::HostToNet16(port_); |
58 memcpy(&addr->sin_addr, &address_[0], kIPv4AddressSize); | 59 memcpy(&addr->sin_addr, &address_[0], kIPv4AddressSize); |
59 break; | 60 break; |
60 } | 61 } |
61 case kIPv6AddressSize: { | 62 case kIPv6AddressSize: { |
62 if (*address_length < sizeof(struct sockaddr_in6)) | 63 if (*address_length < sizeof(struct sockaddr_in6)) |
63 return false; | 64 return false; |
64 *address_length = sizeof(struct sockaddr_in6); | 65 *address_length = sizeof(struct sockaddr_in6); |
65 struct sockaddr_in6* addr6 = | 66 struct sockaddr_in6* addr6 = |
66 reinterpret_cast<struct sockaddr_in6*>(address); | 67 reinterpret_cast<struct sockaddr_in6*>(address); |
67 memset(addr6, 0, sizeof(struct sockaddr_in6)); | 68 memset(addr6, 0, sizeof(struct sockaddr_in6)); |
68 addr6->sin6_family = AF_INET6; | 69 addr6->sin6_family = AF_INET6; |
69 addr6->sin6_port = htons(port_); | 70 addr6->sin6_port = base::HostToNet16(port_); |
70 memcpy(&addr6->sin6_addr, &address_[0], kIPv6AddressSize); | 71 memcpy(&addr6->sin6_addr, &address_[0], kIPv6AddressSize); |
71 break; | 72 break; |
72 } | 73 } |
73 default: { | 74 default: { |
74 NOTREACHED() << "Bad IP address"; | 75 NOTREACHED() << "Bad IP address"; |
75 break; | 76 break; |
76 } | 77 } |
77 } | 78 } |
78 return true; | 79 return true; |
79 } | 80 } |
80 | 81 |
81 bool IPEndPoint::FromSockAddr(const struct sockaddr* address, | 82 bool IPEndPoint::FromSockAddr(const struct sockaddr* address, |
82 size_t address_length) { | 83 size_t address_length) { |
83 DCHECK(address); | 84 DCHECK(address); |
84 switch (address->sa_family) { | 85 switch (address->sa_family) { |
85 case AF_INET: { | 86 case AF_INET: { |
86 if (address_length < sizeof(struct sockaddr_in)) | 87 if (address_length < sizeof(struct sockaddr_in)) |
87 return false; | 88 return false; |
88 const struct sockaddr_in* addr = | 89 const struct sockaddr_in* addr = |
89 reinterpret_cast<const struct sockaddr_in*>(address); | 90 reinterpret_cast<const struct sockaddr_in*>(address); |
90 port_ = ntohs(addr->sin_port); | 91 port_ = base::NetToHost16(addr->sin_port); |
91 const char* bytes = reinterpret_cast<const char*>(&addr->sin_addr); | 92 const char* bytes = reinterpret_cast<const char*>(&addr->sin_addr); |
92 address_.assign(&bytes[0], &bytes[kIPv4AddressSize]); | 93 address_.assign(&bytes[0], &bytes[kIPv4AddressSize]); |
93 break; | 94 break; |
94 } | 95 } |
95 case AF_INET6: { | 96 case AF_INET6: { |
96 if (address_length < sizeof(struct sockaddr_in6)) | 97 if (address_length < sizeof(struct sockaddr_in6)) |
97 return false; | 98 return false; |
98 const struct sockaddr_in6* addr = | 99 const struct sockaddr_in6* addr = |
99 reinterpret_cast<const struct sockaddr_in6*>(address); | 100 reinterpret_cast<const struct sockaddr_in6*>(address); |
100 port_ = ntohs(addr->sin6_port); | 101 port_ = base::NetToHost16(addr->sin6_port); |
101 const char* bytes = reinterpret_cast<const char*>(&addr->sin6_addr); | 102 const char* bytes = reinterpret_cast<const char*>(&addr->sin6_addr); |
102 address_.assign(&bytes[0], &bytes[kIPv6AddressSize]); | 103 address_.assign(&bytes[0], &bytes[kIPv6AddressSize]); |
103 break; | 104 break; |
104 } | 105 } |
105 default: { | 106 default: { |
106 NOTREACHED() << "Bad IP address"; | 107 NOTREACHED() << "Bad IP address"; |
107 break; | 108 break; |
108 } | 109 } |
109 } | 110 } |
110 return true; | 111 return true; |
(...skipping 18 matching lines...) Expand all Loading... |
129 return address_ < that.address_; | 130 return address_ < that.address_; |
130 } | 131 } |
131 return port_ < that.port_; | 132 return port_ < that.port_; |
132 } | 133 } |
133 | 134 |
134 bool IPEndPoint::operator==(const IPEndPoint& that) const { | 135 bool IPEndPoint::operator==(const IPEndPoint& that) const { |
135 return address_ == that.address_ && port_ == that.port_; | 136 return address_ == that.address_ && port_ == that.port_; |
136 } | 137 } |
137 | 138 |
138 } // namespace net | 139 } // namespace net |
OLD | NEW |