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

Side by Side Diff: runtime/bin/socket_macos.cc

Issue 14036017: Revert "Add new InternetAddress class with a static lookup function (including IPv6 results)." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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
« no previous file with comments | « runtime/bin/socket_linux.cc ('k') | runtime/bin/socket_patch.dart » ('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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_MACOS) 6 #if defined(TARGET_OS_MACOS)
7 7
8 #include <errno.h> // NOLINT 8 #include <errno.h> // NOLINT
9 #include <stdio.h> // NOLINT 9 #include <stdio.h> // NOLINT
10 #include <stdlib.h> // NOLINT 10 #include <stdlib.h> // NOLINT
11 #include <string.h> // NOLINT 11 #include <string.h> // NOLINT
12 #include <sys/stat.h> // NOLINT 12 #include <sys/stat.h> // NOLINT
13 #include <unistd.h> // NOLINT 13 #include <unistd.h> // NOLINT
14 #include <netinet/tcp.h> // NOLINT 14 #include <netinet/tcp.h> // NOLINT
15 15
16 #include "bin/fdutils.h" 16 #include "bin/fdutils.h"
17 #include "bin/file.h" 17 #include "bin/file.h"
18 #include "bin/log.h" 18 #include "bin/log.h"
19 #include "bin/socket.h" 19 #include "bin/socket.h"
20 20
21 21
22 #define SOCKADDR_STORAGE_SET_PORT(addr, port) \
23 if (addr.ss_family == AF_INET) { \
24 reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port = htons(port); \
25 } else { \
26 reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port = htons(port); \
27 }
28
29
30 #define SOCKADDR_STORAGE_GET_PORT(addr) \
31 addr.ss_family == AF_INET ? \
32 ntohs(reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port) : \
33 ntohs(reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port)
34
35
36 SocketAddress::SocketAddress(struct addrinfo* addrinfo) {
37 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
38 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(addrinfo->ai_addr);
39 const char* result = inet_ntop(addrinfo->ai_family,
40 &sockaddr->sin_addr,
41 as_string_,
42 INET6_ADDRSTRLEN);
43 if (result == NULL) as_string_[0] = 0;
44 memmove(reinterpret_cast<void *>(&addr_),
45 addrinfo->ai_addr,
46 addrinfo->ai_addrlen);
47 }
48
49
50 bool Socket::Initialize() { 22 bool Socket::Initialize() {
51 // Nothing to do on Mac OS. 23 // Nothing to do on Mac OS.
52 return true; 24 return true;
53 } 25 }
54 26
55 27
56 intptr_t Socket::CreateConnect(sockaddr_storage addr, const intptr_t port) { 28 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) {
57 intptr_t fd; 29 intptr_t fd;
30 struct hostent* server;
31 struct sockaddr_in server_address;
58 32
59 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); 33 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0));
60 if (fd < 0) { 34 if (fd < 0) {
61 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); 35 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno));
62 return -1; 36 return -1;
63 } 37 }
64 38
65 FDUtils::SetCloseOnExec(fd); 39 FDUtils::SetCloseOnExec(fd);
66 Socket::SetNonBlocking(fd); 40 Socket::SetNonBlocking(fd);
67 41
68 SOCKADDR_STORAGE_SET_PORT(addr, port); 42 server = gethostbyname(host);
43 if (server == NULL) {
44 VOID_TEMP_FAILURE_RETRY(close(fd));
45 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno));
46 return -1;
47 }
48
49 server_address.sin_family = AF_INET;
50 server_address.sin_port = htons(port);
51 bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length);
52 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
69 intptr_t result = TEMP_FAILURE_RETRY( 53 intptr_t result = TEMP_FAILURE_RETRY(
70 connect(fd, 54 connect(fd,
71 reinterpret_cast<struct sockaddr*>(&addr), 55 reinterpret_cast<struct sockaddr *>(&server_address),
72 SocketAddress::GetAddrLength(addr))); 56 sizeof(server_address)));
73 if (result == 0 || errno == EINPROGRESS) { 57 if (result == 0 || errno == EINPROGRESS) {
74 return fd; 58 return fd;
75 } 59 }
76 TEMP_FAILURE_RETRY(close(fd));
77 return -1; 60 return -1;
78 } 61 }
79 62
80 63
81 intptr_t Socket::Available(intptr_t fd) { 64 intptr_t Socket::Available(intptr_t fd) {
82 return FDUtils::AvailableBytes(fd); 65 return FDUtils::AvailableBytes(fd);
83 } 66 }
84 67
85 68
86 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { 69 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
(...skipping 17 matching lines...) Expand all
104 // If the would block we need to retry and therefore return 0 as 87 // If the would block we need to retry and therefore return 0 as
105 // the number of bytes written. 88 // the number of bytes written.
106 written_bytes = 0; 89 written_bytes = 0;
107 } 90 }
108 return written_bytes; 91 return written_bytes;
109 } 92 }
110 93
111 94
112 intptr_t Socket::GetPort(intptr_t fd) { 95 intptr_t Socket::GetPort(intptr_t fd) {
113 ASSERT(fd >= 0); 96 ASSERT(fd >= 0);
114 struct sockaddr_storage socket_address; 97 struct sockaddr_in socket_address;
115 socklen_t size = sizeof(socket_address); 98 socklen_t size = sizeof(socket_address);
116 if (TEMP_FAILURE_RETRY( 99 if (TEMP_FAILURE_RETRY(
117 getsockname(fd, 100 getsockname(fd,
118 reinterpret_cast<struct sockaddr *>(&socket_address), 101 reinterpret_cast<struct sockaddr *>(&socket_address),
119 &size))) { 102 &size))) {
120 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); 103 Log::PrintErr("Error getsockname: %s\n", strerror(errno));
121 return 0; 104 return 0;
122 } 105 }
123 return SOCKADDR_STORAGE_GET_PORT(socket_address); 106 return ntohs(socket_address.sin_port);
124 } 107 }
125 108
126 109
127 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { 110 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) {
128 ASSERT(fd >= 0); 111 ASSERT(fd >= 0);
129 struct sockaddr_storage socket_address; 112 struct sockaddr_in socket_address;
130 socklen_t size = sizeof(socket_address); 113 socklen_t size = sizeof(socket_address);
131 if (TEMP_FAILURE_RETRY( 114 if (TEMP_FAILURE_RETRY(
132 getpeername(fd, 115 getpeername(fd,
133 reinterpret_cast<struct sockaddr *>(&socket_address), 116 reinterpret_cast<struct sockaddr *>(&socket_address),
134 &size))) { 117 &size))) {
135 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); 118 Log::PrintErr("Error getpeername: %s\n", strerror(errno));
136 return false; 119 return false;
137 } 120 }
138 const void* src; 121 if (inet_ntop(socket_address.sin_family,
139 if (socket_address.ss_family == AF_INET6) { 122 reinterpret_cast<const void *>(&socket_address.sin_addr),
140 const sockaddr_in6* in6 =
141 reinterpret_cast<const sockaddr_in6*>(&socket_address);
142 src = reinterpret_cast<const void*>(&in6->sin6_addr);
143 } else {
144 const sockaddr_in* in =
145 reinterpret_cast<const sockaddr_in*>(&socket_address);
146 src = reinterpret_cast<const void*>(&in->sin_addr);
147 }
148 if (inet_ntop(socket_address.ss_family,
149 src,
150 host, 123 host,
151 INET_ADDRSTRLEN) == NULL) { 124 INET_ADDRSTRLEN) == NULL) {
152 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); 125 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno));
153 return false; 126 return false;
154 } 127 }
155 *port = SOCKADDR_STORAGE_GET_PORT(socket_address); 128 *port = ntohs(socket_address.sin_port);
156 return true; 129 return true;
157 } 130 }
158 131
159 132
160 void Socket::GetError(intptr_t fd, OSError* os_error) { 133 void Socket::GetError(intptr_t fd, OSError* os_error) {
161 int len = sizeof(errno); 134 int len = sizeof(errno);
162 getsockopt(fd, 135 getsockopt(fd,
163 SOL_SOCKET, 136 SOL_SOCKET,
164 SO_ERROR, 137 SO_ERROR,
165 &errno, 138 &errno,
(...skipping 11 matching lines...) Expand all
177 if (S_ISREG(buf.st_mode)) return File::kFile; 150 if (S_ISREG(buf.st_mode)) return File::kFile;
178 return File::kOther; 151 return File::kOther;
179 } 152 }
180 153
181 154
182 intptr_t Socket::GetStdioHandle(int num) { 155 intptr_t Socket::GetStdioHandle(int num) {
183 return static_cast<intptr_t>(num); 156 return static_cast<intptr_t>(num);
184 } 157 }
185 158
186 159
187 SocketAddresses* Socket::LookupAddress(const char* host, 160 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) {
188 int type, 161 // Perform a name lookup for an IPv4 address.
189 OSError** os_error) {
190 // Perform a name lookup for a host name.
191 struct addrinfo hints; 162 struct addrinfo hints;
192 memset(&hints, 0, sizeof(hints)); 163 memset(&hints, 0, sizeof(hints));
193 hints.ai_family = SocketAddress::FromType(type); 164 hints.ai_family = AF_INET;
194 hints.ai_socktype = SOCK_STREAM; 165 hints.ai_socktype = SOCK_STREAM;
195 hints.ai_flags = 0;
196 hints.ai_protocol = IPPROTO_TCP; 166 hints.ai_protocol = IPPROTO_TCP;
197 struct addrinfo* info = NULL; 167 struct addrinfo* info = NULL;
198 int status = getaddrinfo(host, 0, &hints, &info); 168 int status = getaddrinfo(host, 0, &hints, &info);
199 if (status != 0) { 169 if (status != 0) {
200 ASSERT(*os_error == NULL); 170 ASSERT(*os_error == NULL);
201 *os_error = new OSError(status, 171 *os_error = new OSError(status,
202 gai_strerror(status), 172 gai_strerror(status),
203 OSError::kGetAddressInfo); 173 OSError::kGetAddressInfo);
204 return NULL; 174 return NULL;
205 } 175 }
206 intptr_t count = 0; 176 // Convert the address into IPv4 dotted decimal notation.
207 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { 177 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN));
208 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; 178 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr);
179 const char* result = inet_ntop(AF_INET,
180 reinterpret_cast<void *>(&sockaddr->sin_addr),
181 buffer,
182 INET_ADDRSTRLEN);
183 if (result == NULL) {
184 free(buffer);
185 return NULL;
209 } 186 }
210 SocketAddresses* addresses = new SocketAddresses(count); 187 ASSERT(result == buffer);
211 intptr_t i = 0; 188 return buffer;
212 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
213 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) {
214 addresses->SetAt(i, new SocketAddress(c));
215 i++;
216 }
217 }
218 freeaddrinfo(info);
219 return addresses;
220 } 189 }
221 190
222 191
223 intptr_t ServerSocket::CreateBindListen(sockaddr_storage addr, 192 intptr_t ServerSocket::CreateBindListen(const char* host,
224 intptr_t port, 193 intptr_t port,
225 intptr_t backlog) { 194 intptr_t backlog) {
226 intptr_t fd; 195 intptr_t fd;
196 struct sockaddr_in server_address;
227 197
228 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); 198 in_addr_t s_addr = inet_addr(host);
199 if (s_addr == INADDR_NONE) {
200 return -5;
201 }
202
203 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0));
229 if (fd < 0) return -1; 204 if (fd < 0) return -1;
230 205
231 FDUtils::SetCloseOnExec(fd); 206 FDUtils::SetCloseOnExec(fd);
232 207
233 int optval = 1; 208 int optval = 1;
234 VOID_TEMP_FAILURE_RETRY( 209 VOID_TEMP_FAILURE_RETRY(
235 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); 210 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
236 211
237 if (addr.ss_family == AF_INET6) { 212 server_address.sin_family = AF_INET;
238 optval = 0; 213 server_address.sin_port = htons(port);
239 VOID_TEMP_FAILURE_RETRY( 214 server_address.sin_addr.s_addr = s_addr;
240 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); 215 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
241 }
242 216
243 SOCKADDR_STORAGE_SET_PORT(addr, port);
244 if (TEMP_FAILURE_RETRY( 217 if (TEMP_FAILURE_RETRY(
245 bind(fd, 218 bind(fd,
246 reinterpret_cast<struct sockaddr*>(&addr), 219 reinterpret_cast<struct sockaddr *>(&server_address),
247 SocketAddress::GetAddrLength(addr))) < 0) { 220 sizeof(server_address))) < 0) {
248 VOID_TEMP_FAILURE_RETRY(close(fd)); 221 VOID_TEMP_FAILURE_RETRY(close(fd));
249 return -1; 222 return -1;
250 } 223 }
251 224
252 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { 225 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
253 VOID_TEMP_FAILURE_RETRY(close(fd)); 226 VOID_TEMP_FAILURE_RETRY(close(fd));
254 return -1; 227 return -1;
255 } 228 }
256 229
257 Socket::SetNonBlocking(fd); 230 Socket::SetNonBlocking(fd);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { 277 bool Socket::SetNoDelay(intptr_t fd, bool enabled) {
305 int on = enabled ? 1 : 0; 278 int on = enabled ? 1 : 0;
306 return TEMP_FAILURE_RETRY(setsockopt(fd, 279 return TEMP_FAILURE_RETRY(setsockopt(fd,
307 IPPROTO_TCP, 280 IPPROTO_TCP,
308 TCP_NODELAY, 281 TCP_NODELAY,
309 reinterpret_cast<char *>(&on), 282 reinterpret_cast<char *>(&on),
310 sizeof(on))) == 0; 283 sizeof(on))) == 0;
311 } 284 }
312 285
313 #endif // defined(TARGET_OS_MACOS) 286 #endif // defined(TARGET_OS_MACOS)
OLDNEW
« no previous file with comments | « runtime/bin/socket_linux.cc ('k') | runtime/bin/socket_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698