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

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

Powered by Google App Engine
This is Rietveld 408576698