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

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

Issue 2780063002: Pulled a significant portion of Socket implementation into BaseSocket in order to prepare for the s… (Closed)
Patch Set: Minor change to socket_common_unsupported.cc Created 3 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
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 #if !defined(DART_IO_DISABLED) 5 #if !defined(DART_IO_DISABLED)
6 6
7 #include "platform/globals.h" 7 #include "platform/globals.h"
8 #if defined(HOST_OS_LINUX) 8 #if defined(HOST_OS_LINUX)
9 9
10 #include "bin/socket.h" 10 #include "bin/socket.h"
11 #include "bin/socket_linux.h" 11 #include "bin/socket_common_linux.h"
12 12
13 #include <errno.h> // NOLINT 13 #include <errno.h> // NOLINT
14 #include <ifaddrs.h> // NOLINT 14 #include <ifaddrs.h> // NOLINT
15 #include <net/if.h> // NOLINT 15 #include <net/if.h> // NOLINT
16 #include <netinet/tcp.h> // NOLINT 16 #include <netinet/tcp.h> // NOLINT
17 #include <stdio.h> // NOLINT 17 #include <stdio.h> // NOLINT
18 #include <stdlib.h> // NOLINT 18 #include <stdlib.h> // NOLINT
19 #include <string.h> // NOLINT 19 #include <string.h> // NOLINT
20 #include <sys/stat.h> // NOLINT 20 #include <sys/stat.h> // NOLINT
21 #include <unistd.h> // NOLINT 21 #include <unistd.h> // NOLINT
22 22
23 #include "bin/fdutils.h" 23 #include "bin/fdutils.h"
24 #include "bin/file.h" 24 #include "bin/file.h"
25 #include "bin/thread.h" 25 #include "bin/thread.h"
26 #include "platform/signal_blocker.h" 26 #include "platform/signal_blocker.h"
27 27
28 namespace dart { 28 namespace dart {
29 namespace bin { 29 namespace bin {
30 30
31 SocketAddress::SocketAddress(struct sockaddr* sa) { 31 SocketAddress::SocketAddress(struct sockaddr* sa) {
32 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); 32 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
33 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, 33 if (!BaseSocket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa),
34 INET6_ADDRSTRLEN)) { 34 as_string_, INET6_ADDRSTRLEN)) {
35 as_string_[0] = 0; 35 as_string_[0] = 0;
36 } 36 }
37 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); 37 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa));
38 memmove(reinterpret_cast<void*>(&addr_), sa, salen); 38 memmove(reinterpret_cast<void*>(&addr_), sa, salen);
39 } 39 }
40 40
41 41
42 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { 42 bool BaseSocket::FormatNumericAddress(const RawAddr& addr,
43 char* address,
44 int len) {
43 socklen_t salen = SocketAddress::GetAddrLength(addr); 45 socklen_t salen = SocketAddress::GetAddrLength(addr);
44 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL, 46 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL,
45 0, NI_NUMERICHOST) == 0)); 47 0, NI_NUMERICHOST) == 0));
46 } 48 }
47 49
48 50
49 Socket::Socket(intptr_t fd) 51 Socket::Socket(intptr_t fd)
50 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} 52 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {}
51 53
52 54
53 void Socket::SetClosedFd() { 55 void Socket::SetClosedFd() {
54 fd_ = kClosedFd; 56 fd_ = kClosedFd;
55 } 57 }
56 58
57 59
58 bool Socket::Initialize() { 60 bool Socket::Initialize() {
59 // Nothing to do on Linux. 61 // Nothing to do on Linux.
60 return true; 62 return true;
61 } 63 }
62 64
63 65
64 static intptr_t Create(const RawAddr& addr) { 66 static intptr_t Create(const RawAddr& addr) {
65 intptr_t fd; 67 intptr_t fd;
66 fd = NO_RETRY_EXPECTED( 68 intptr_t type = SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC;
67 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)); 69 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, type, 0));
68 if (fd < 0) { 70 if (fd < 0) {
69 return -1; 71 return -1;
70 } 72 }
71 return fd; 73 return fd;
72 } 74 }
73 75
74 76
75 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { 77 static intptr_t Connect(intptr_t fd, const RawAddr& addr) {
76 intptr_t result = TEMP_FAILURE_RETRY( 78 intptr_t result = TEMP_FAILURE_RETRY(
77 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); 79 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr)));
(...skipping 25 matching lines...) Expand all
103 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); 105 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr)));
104 if ((result != 0) && (errno != EINPROGRESS)) { 106 if ((result != 0) && (errno != EINPROGRESS)) {
105 FDUtils::SaveErrorAndClose(fd); 107 FDUtils::SaveErrorAndClose(fd);
106 return -1; 108 return -1;
107 } 109 }
108 110
109 return Connect(fd, addr); 111 return Connect(fd, addr);
110 } 112 }
111 113
112 114
113 bool Socket::IsBindError(intptr_t error_number) { 115 bool BaseSocket::IsBindError(intptr_t error_number) {
114 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || 116 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL ||
115 error_number == EINVAL; 117 error_number == EINVAL;
116 } 118 }
117 119
118 120
119 intptr_t Socket::Available(intptr_t fd) { 121 intptr_t BaseSocket::Available(intptr_t fd) {
120 return FDUtils::AvailableBytes(fd); 122 return FDUtils::AvailableBytes(fd);
121 } 123 }
122 124
123 125
124 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { 126 intptr_t BaseSocket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
125 ASSERT(fd >= 0); 127 ASSERT(fd >= 0);
126 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); 128 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes));
127 ASSERT(EAGAIN == EWOULDBLOCK); 129 ASSERT(EAGAIN == EWOULDBLOCK);
128 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { 130 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) {
129 // If the read would block we need to retry and therefore return 0 131 // If the read would block we need to retry and therefore return 0
130 // as the number of bytes written. 132 // as the number of bytes written.
131 read_bytes = 0; 133 read_bytes = 0;
132 } 134 }
133 return read_bytes; 135 return read_bytes;
134 } 136 }
135 137
136 138
137 intptr_t Socket::RecvFrom(intptr_t fd, 139 intptr_t BaseSocket::RecvFrom(intptr_t fd,
138 void* buffer, 140 void* buffer,
139 intptr_t num_bytes, 141 intptr_t num_bytes,
140 RawAddr* addr) { 142 RawAddr* addr) {
141 ASSERT(fd >= 0); 143 ASSERT(fd >= 0);
142 socklen_t addr_len = sizeof(addr->ss); 144 socklen_t addr_len = sizeof(addr->ss);
143 ssize_t read_bytes = TEMP_FAILURE_RETRY( 145 ssize_t read_bytes = TEMP_FAILURE_RETRY(
144 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); 146 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len));
145 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { 147 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) {
146 // If the read would block we need to retry and therefore return 0 148 // If the read would block we need to retry and therefore return 0
147 // as the number of bytes written. 149 // as the number of bytes written.
148 read_bytes = 0; 150 read_bytes = 0;
149 } 151 }
150 return read_bytes; 152 return read_bytes;
151 } 153 }
152 154
153 155
154 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { 156 intptr_t BaseSocket::Write(intptr_t fd,
157 const void* buffer,
158 intptr_t num_bytes) {
155 ASSERT(fd >= 0); 159 ASSERT(fd >= 0);
156 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); 160 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes));
157 ASSERT(EAGAIN == EWOULDBLOCK); 161 ASSERT(EAGAIN == EWOULDBLOCK);
158 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { 162 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) {
159 // If the would block we need to retry and therefore return 0 as 163 // If the would block we need to retry and therefore return 0 as
160 // the number of bytes written. 164 // the number of bytes written.
161 written_bytes = 0; 165 written_bytes = 0;
162 } 166 }
163 return written_bytes; 167 return written_bytes;
164 } 168 }
165 169
166 170
167 intptr_t Socket::SendTo(intptr_t fd, 171 intptr_t BaseSocket::SendTo(intptr_t fd,
168 const void* buffer, 172 const void* buffer,
169 intptr_t num_bytes, 173 intptr_t num_bytes,
170 const RawAddr& addr) { 174 const RawAddr& addr) {
171 ASSERT(fd >= 0); 175 ASSERT(fd >= 0);
172 ssize_t written_bytes = 176 ssize_t written_bytes =
173 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, 177 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr,
174 SocketAddress::GetAddrLength(addr))); 178 SocketAddress::GetAddrLength(addr)));
175 ASSERT(EAGAIN == EWOULDBLOCK); 179 ASSERT(EAGAIN == EWOULDBLOCK);
176 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { 180 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) {
177 // If the would block we need to retry and therefore return 0 as 181 // If the would block we need to retry and therefore return 0 as
178 // the number of bytes written. 182 // the number of bytes written.
179 written_bytes = 0; 183 written_bytes = 0;
180 } 184 }
181 return written_bytes; 185 return written_bytes;
182 } 186 }
183 187
184 188
185 intptr_t Socket::GetPort(intptr_t fd) { 189 intptr_t BaseSocket::GetPort(intptr_t fd) {
186 ASSERT(fd >= 0); 190 ASSERT(fd >= 0);
187 RawAddr raw; 191 RawAddr raw;
188 socklen_t size = sizeof(raw); 192 socklen_t size = sizeof(raw);
189 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { 193 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) {
190 return 0; 194 return 0;
191 } 195 }
192 return SocketAddress::GetAddrPort(raw); 196 return SocketAddress::GetAddrPort(raw);
193 } 197 }
194 198
195 199
196 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { 200 SocketAddress* BaseSocket::GetRemotePeer(intptr_t fd, intptr_t* port) {
197 ASSERT(fd >= 0); 201 ASSERT(fd >= 0);
198 RawAddr raw; 202 RawAddr raw;
199 socklen_t size = sizeof(raw); 203 socklen_t size = sizeof(raw);
200 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { 204 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) {
201 return NULL; 205 return NULL;
202 } 206 }
203 *port = SocketAddress::GetAddrPort(raw); 207 *port = SocketAddress::GetAddrPort(raw);
204 return new SocketAddress(&raw.addr); 208 return new SocketAddress(&raw.addr);
205 } 209 }
206 210
207 211
208 void Socket::GetError(intptr_t fd, OSError* os_error) { 212 void BaseSocket::GetError(intptr_t fd, OSError* os_error) {
209 int len = sizeof(errno); 213 int len = sizeof(errno);
210 int err = 0; 214 int err = 0;
211 VOID_NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, 215 VOID_NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err,
212 reinterpret_cast<socklen_t*>(&len))); 216 reinterpret_cast<socklen_t*>(&len)));
213 errno = err; 217 errno = err;
214 os_error->SetCodeAndMessage(OSError::kSystem, errno); 218 os_error->SetCodeAndMessage(OSError::kSystem, errno);
215 } 219 }
216 220
217 221
218 int Socket::GetType(intptr_t fd) { 222 int BaseSocket::GetType(intptr_t fd) {
219 struct stat64 buf; 223 struct stat64 buf;
220 int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf)); 224 int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf));
221 if (result == -1) { 225 if (result == -1) {
222 return -1; 226 return -1;
223 } 227 }
224 if (S_ISCHR(buf.st_mode)) { 228 if (S_ISCHR(buf.st_mode)) {
225 return File::kTerminal; 229 return File::kTerminal;
226 } 230 }
227 if (S_ISFIFO(buf.st_mode)) { 231 if (S_ISFIFO(buf.st_mode)) {
228 return File::kPipe; 232 return File::kPipe;
229 } 233 }
230 if (S_ISREG(buf.st_mode)) { 234 if (S_ISREG(buf.st_mode)) {
231 return File::kFile; 235 return File::kFile;
232 } 236 }
233 return File::kOther; 237 return File::kOther;
234 } 238 }
235 239
236 240
237 intptr_t Socket::GetStdioHandle(intptr_t num) { 241 intptr_t BaseSocket::GetStdioHandle(intptr_t num) {
238 return num; 242 return num;
239 } 243 }
240 244
241 245
242 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, 246 AddressList<SocketAddress>* BaseSocket::LookupAddress(const char* host,
243 int type, 247 int type,
244 OSError** os_error) { 248 OSError** os_error) {
245 // Perform a name lookup for a host name. 249 // Perform a name lookup for a host name.
246 struct addrinfo hints; 250 struct addrinfo hints;
247 memset(&hints, 0, sizeof(hints)); 251 memset(&hints, 0, sizeof(hints));
248 hints.ai_family = SocketAddress::FromType(type); 252 hints.ai_family = SocketAddress::FromType(type);
249 hints.ai_socktype = SOCK_STREAM; 253 hints.ai_socktype = SOCK_STREAM;
250 hints.ai_flags = AI_ADDRCONFIG; 254 hints.ai_flags = AI_ADDRCONFIG;
251 hints.ai_protocol = IPPROTO_TCP; 255 hints.ai_protocol = IPPROTO_TCP;
252 struct addrinfo* info = NULL; 256 struct addrinfo* info = NULL;
253 int status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info)); 257 int status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info));
254 if (status != 0) { 258 if (status != 0) {
(...skipping 20 matching lines...) Expand all
275 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { 279 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) {
276 addresses->SetAt(i, new SocketAddress(c->ai_addr)); 280 addresses->SetAt(i, new SocketAddress(c->ai_addr));
277 i++; 281 i++;
278 } 282 }
279 } 283 }
280 freeaddrinfo(info); 284 freeaddrinfo(info);
281 return addresses; 285 return addresses;
282 } 286 }
283 287
284 288
285 bool Socket::ReverseLookup(const RawAddr& addr, 289 bool BaseSocket::ReverseLookup(const RawAddr& addr,
286 char* host, 290 char* host,
287 intptr_t host_len, 291 intptr_t host_len,
288 OSError** os_error) { 292 OSError** os_error) {
289 ASSERT(host_len >= NI_MAXHOST); 293 ASSERT(host_len >= NI_MAXHOST);
290 int status = NO_RETRY_EXPECTED( 294 int status = NO_RETRY_EXPECTED(
291 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, 295 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host,
292 host_len, NULL, 0, NI_NAMEREQD)); 296 host_len, NULL, 0, NI_NAMEREQD));
293 if (status != 0) { 297 if (status != 0) {
294 ASSERT(*os_error == NULL); 298 ASSERT(*os_error == NULL);
295 *os_error = 299 *os_error =
296 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); 300 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo);
297 return false; 301 return false;
298 } 302 }
299 return true; 303 return true;
300 } 304 }
301 305
302 306
303 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { 307 bool BaseSocket::ParseAddress(int type, const char* address, RawAddr* addr) {
304 int result; 308 int result;
305 if (type == SocketAddress::TYPE_IPV4) { 309 if (type == SocketAddress::TYPE_IPV4) {
306 result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr)); 310 result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr));
307 } else { 311 } else {
308 ASSERT(type == SocketAddress::TYPE_IPV6); 312 ASSERT(type == SocketAddress::TYPE_IPV6);
309 result = 313 result =
310 NO_RETRY_EXPECTED(inet_pton(AF_INET6, address, &addr->in6.sin6_addr)); 314 NO_RETRY_EXPECTED(inet_pton(AF_INET6, address, &addr->in6.sin6_addr));
311 } 315 }
312 return (result == 1); 316 return (result == 1);
313 } 317 }
(...skipping 29 matching lines...) Expand all
343 // OpenVPN's virtual device tun0. 347 // OpenVPN's virtual device tun0.
344 return false; 348 return false;
345 } 349 }
346 int family = ifa->ifa_addr->sa_family; 350 int family = ifa->ifa_addr->sa_family;
347 return ((lookup_family == family) || 351 return ((lookup_family == family) ||
348 (((lookup_family == AF_UNSPEC) && 352 (((lookup_family == AF_UNSPEC) &&
349 ((family == AF_INET) || (family == AF_INET6))))); 353 ((family == AF_INET) || (family == AF_INET6)))));
350 } 354 }
351 355
352 356
353 bool Socket::ListInterfacesSupported() { 357 bool BaseSocket::ListInterfacesSupported() {
354 return true; 358 return true;
355 } 359 }
356 360
357 361
358 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( 362 AddressList<InterfaceSocketAddress>* BaseSocket::ListInterfaces(
359 int type, 363 int type,
360 OSError** os_error) { 364 OSError** os_error) {
361 struct ifaddrs* ifaddr; 365 struct ifaddrs* ifaddr;
362 366
363 int status = NO_RETRY_EXPECTED(getifaddrs(&ifaddr)); 367 int status = NO_RETRY_EXPECTED(getifaddrs(&ifaddr));
364 if (status != 0) { 368 if (status != 0) {
365 ASSERT(*os_error == NULL); 369 ASSERT(*os_error == NULL);
366 *os_error = 370 *os_error =
367 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); 371 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo);
368 return NULL; 372 return NULL;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 } 420 }
417 421
418 if (NO_RETRY_EXPECTED( 422 if (NO_RETRY_EXPECTED(
419 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { 423 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) {
420 FDUtils::SaveErrorAndClose(fd); 424 FDUtils::SaveErrorAndClose(fd);
421 return -1; 425 return -1;
422 } 426 }
423 427
424 // Test for invalid socket port 65535 (some browsers disallow it). 428 // Test for invalid socket port 65535 (some browsers disallow it).
425 if ((SocketAddress::GetAddrPort(addr) == 0) && 429 if ((SocketAddress::GetAddrPort(addr) == 0) &&
426 (Socket::GetPort(fd) == 65535)) { 430 (BaseSocket::GetPort(fd) == 65535)) {
427 // Don't close the socket until we have created a new socket, ensuring 431 // Don't close the socket until we have created a new socket, ensuring
428 // that we do not get the bad port number again. 432 // that we do not get the bad port number again.
429 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); 433 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only);
430 FDUtils::SaveErrorAndClose(fd); 434 FDUtils::SaveErrorAndClose(fd);
431 return new_fd; 435 return new_fd;
432 } 436 }
433 437
434 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { 438 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
435 FDUtils::SaveErrorAndClose(fd); 439 FDUtils::SaveErrorAndClose(fd);
436 return -1; 440 return -1;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 } 480 }
477 if (!FDUtils::SetNonBlocking(socket)) { 481 if (!FDUtils::SetNonBlocking(socket)) {
478 FDUtils::SaveErrorAndClose(socket); 482 FDUtils::SaveErrorAndClose(socket);
479 return -1; 483 return -1;
480 } 484 }
481 } 485 }
482 return socket; 486 return socket;
483 } 487 }
484 488
485 489
486 void Socket::Close(intptr_t fd) { 490 void BaseSocket::Close(intptr_t fd) {
487 ASSERT(fd >= 0); 491 ASSERT(fd >= 0);
488 VOID_TEMP_FAILURE_RETRY(close(fd)); 492 VOID_TEMP_FAILURE_RETRY(close(fd));
489 } 493 }
490 494
491 495
492 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { 496 bool BaseSocket::GetNoDelay(intptr_t fd, bool* enabled) {
493 int on; 497 int on;
494 socklen_t len = sizeof(on); 498 socklen_t len = sizeof(on);
495 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, 499 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
496 reinterpret_cast<void*>(&on), &len)); 500 reinterpret_cast<void*>(&on), &len));
497 if (err == 0) { 501 if (err == 0) {
498 *enabled = (on == 1); 502 *enabled = (on == 1);
499 } 503 }
500 return (err == 0); 504 return (err == 0);
501 } 505 }
502 506
503 507
504 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { 508 bool BaseSocket::SetNoDelay(intptr_t fd, bool enabled) {
505 int on = enabled ? 1 : 0; 509 int on = enabled ? 1 : 0;
506 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, 510 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
507 reinterpret_cast<char*>(&on), 511 reinterpret_cast<char*>(&on),
508 sizeof(on))) == 0; 512 sizeof(on))) == 0;
509 } 513 }
510 514
511 515
512 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { 516 bool BaseSocket::GetMulticastLoop(intptr_t fd,
517 intptr_t protocol,
518 bool* enabled) {
513 uint8_t on; 519 uint8_t on;
514 socklen_t len = sizeof(on); 520 socklen_t len = sizeof(on);
515 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 521 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
516 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP 522 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP
517 : IPV6_MULTICAST_LOOP; 523 : IPV6_MULTICAST_LOOP;
518 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, 524 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname,
519 reinterpret_cast<char*>(&on), &len)) == 0) { 525 reinterpret_cast<char*>(&on), &len)) == 0) {
520 *enabled = (on == 1); 526 *enabled = (on == 1);
521 return true; 527 return true;
522 } 528 }
523 return false; 529 return false;
524 } 530 }
525 531
526 532
527 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { 533 bool BaseSocket::SetMulticastLoop(intptr_t fd,
534 intptr_t protocol,
535 bool enabled) {
528 int on = enabled ? 1 : 0; 536 int on = enabled ? 1 : 0;
529 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 537 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
530 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP 538 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP
531 : IPV6_MULTICAST_LOOP; 539 : IPV6_MULTICAST_LOOP;
532 return NO_RETRY_EXPECTED(setsockopt( 540 return NO_RETRY_EXPECTED(setsockopt(
533 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == 541 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) ==
534 0; 542 0;
535 } 543 }
536 544
537 545
538 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { 546 bool BaseSocket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
539 uint8_t v; 547 uint8_t v;
540 socklen_t len = sizeof(v); 548 socklen_t len = sizeof(v);
541 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 549 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
542 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL 550 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL
543 : IPV6_MULTICAST_HOPS; 551 : IPV6_MULTICAST_HOPS;
544 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, 552 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname,
545 reinterpret_cast<char*>(&v), &len)) == 0) { 553 reinterpret_cast<char*>(&v), &len)) == 0) {
546 *value = v; 554 *value = v;
547 return true; 555 return true;
548 } 556 }
549 return false; 557 return false;
550 } 558 }
551 559
552 560
553 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { 561 bool BaseSocket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
554 int v = value; 562 int v = value;
555 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 563 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
556 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL 564 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL
557 : IPV6_MULTICAST_HOPS; 565 : IPV6_MULTICAST_HOPS;
558 return NO_RETRY_EXPECTED(setsockopt( 566 return NO_RETRY_EXPECTED(setsockopt(
559 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; 567 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0;
560 } 568 }
561 569
562 570
563 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { 571 bool BaseSocket::GetBroadcast(intptr_t fd, bool* enabled) {
564 int on; 572 int on;
565 socklen_t len = sizeof(on); 573 socklen_t len = sizeof(on);
566 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, 574 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST,
567 reinterpret_cast<char*>(&on), &len)); 575 reinterpret_cast<char*>(&on), &len));
568 if (err == 0) { 576 if (err == 0) {
569 *enabled = (on == 1); 577 *enabled = (on == 1);
570 } 578 }
571 return (err == 0); 579 return (err == 0);
572 } 580 }
573 581
574 582
575 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { 583 bool BaseSocket::SetBroadcast(intptr_t fd, bool enabled) {
576 int on = enabled ? 1 : 0; 584 int on = enabled ? 1 : 0;
577 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, 585 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
578 reinterpret_cast<char*>(&on), 586 reinterpret_cast<char*>(&on),
579 sizeof(on))) == 0; 587 sizeof(on))) == 0;
580 } 588 }
581 589
582 590
583 bool Socket::JoinMulticast(intptr_t fd, 591 bool BaseSocket::JoinMulticast(intptr_t fd,
584 const RawAddr& addr, 592 const RawAddr& addr,
585 const RawAddr&, 593 const RawAddr&,
586 int interfaceIndex) { 594 int interfaceIndex) {
587 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; 595 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
588 struct group_req mreq; 596 struct group_req mreq;
589 mreq.gr_interface = interfaceIndex; 597 mreq.gr_interface = interfaceIndex;
590 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); 598 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr));
591 return NO_RETRY_EXPECTED( 599 return NO_RETRY_EXPECTED(
592 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; 600 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0;
593 } 601 }
594 602
595 603
596 bool Socket::LeaveMulticast(intptr_t fd, 604 bool BaseSocket::LeaveMulticast(intptr_t fd,
597 const RawAddr& addr, 605 const RawAddr& addr,
598 const RawAddr&, 606 const RawAddr&,
599 int interfaceIndex) { 607 int interfaceIndex) {
600 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; 608 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
601 struct group_req mreq; 609 struct group_req mreq;
602 mreq.gr_interface = interfaceIndex; 610 mreq.gr_interface = interfaceIndex;
603 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); 611 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr));
604 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, 612 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq,
605 sizeof(mreq))) == 0; 613 sizeof(mreq))) == 0;
606 } 614 }
607 615
608 } // namespace bin 616 } // namespace bin
609 } // namespace dart 617 } // namespace dart
610 618
611 #endif // defined(HOST_OS_LINUX) 619 #endif // defined(HOST_OS_LINUX)
612 620
613 #endif // !defined(DART_IO_DISABLED) 621 #endif // !defined(DART_IO_DISABLED)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698