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

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

Issue 13856013: Try to avoid GCC strict-aliasing error (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 | « no previous file | no next file » | 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_LINUX) 6 #if defined(TARGET_OS_LINUX)
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) \ 22 #define SOCKADDR_STORAGE_GET_PORT(addr) \
31 addr.ss_family == AF_INET ? \ 23 addr.ss_family == AF_INET ? \
32 ntohs(reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port) : \ 24 ntohs(reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port) : \
33 ntohs(reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port) 25 ntohs(reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port)
34 26
35 27
28 static void SetPort(sockaddr_storage* addr, int port) {
29 char* ptr = reinterpret_cast<char*>(addr);
30 if (addr->ss_family == AF_INET) {
31 reinterpret_cast<struct sockaddr_in*>(ptr)->sin_port = htons(port);
32 } else { \
33 reinterpret_cast<struct sockaddr_in6*>(ptr)->sin6_port = htons(port);
34 }
35 }
36
37
36 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { 38 SocketAddress::SocketAddress(struct addrinfo* addrinfo) {
37 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); 39 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
38 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(addrinfo->ai_addr); 40 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(addrinfo->ai_addr);
39 const char* result = inet_ntop(addrinfo->ai_family, 41 const char* result = inet_ntop(addrinfo->ai_family,
40 &sockaddr->sin_addr, 42 &sockaddr->sin_addr,
41 as_string_, 43 as_string_,
42 INET6_ADDRSTRLEN); 44 INET6_ADDRSTRLEN);
43 if (result == NULL) as_string_[0] = 0; 45 if (result == NULL) as_string_[0] = 0;
44 memmove(reinterpret_cast<void *>(&addr_), 46 memmove(reinterpret_cast<void *>(&addr_),
45 addrinfo->ai_addr, 47 addrinfo->ai_addr,
(...skipping 12 matching lines...) Expand all
58 60
59 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); 61 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0));
60 if (fd < 0) { 62 if (fd < 0) {
61 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); 63 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno));
62 return -1; 64 return -1;
63 } 65 }
64 66
65 FDUtils::SetCloseOnExec(fd); 67 FDUtils::SetCloseOnExec(fd);
66 Socket::SetNonBlocking(fd); 68 Socket::SetNonBlocking(fd);
67 69
68 SOCKADDR_STORAGE_SET_PORT(addr, port); 70 SetPort(&addr, port);
69 intptr_t result = TEMP_FAILURE_RETRY( 71 intptr_t result = TEMP_FAILURE_RETRY(
70 connect(fd, 72 connect(fd,
71 reinterpret_cast<struct sockaddr*>(&addr), 73 reinterpret_cast<struct sockaddr*>(&addr),
72 SocketAddress::GetAddrLength(addr))); 74 SocketAddress::GetAddrLength(addr)));
73 if (result == 0 || errno == EINPROGRESS) { 75 if (result == 0 || errno == EINPROGRESS) {
74 return fd; 76 return fd;
75 } 77 }
76 TEMP_FAILURE_RETRY(close(fd)); 78 TEMP_FAILURE_RETRY(close(fd));
77 return -1; 79 return -1;
78 } 80 }
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 int optval = 1; 235 int optval = 1;
234 TEMP_FAILURE_RETRY( 236 TEMP_FAILURE_RETRY(
235 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); 237 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
236 238
237 if (addr.ss_family == AF_INET6) { 239 if (addr.ss_family == AF_INET6) {
238 optval = 0; 240 optval = 0;
239 TEMP_FAILURE_RETRY( 241 TEMP_FAILURE_RETRY(
240 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); 242 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval)));
241 } 243 }
242 244
243 SOCKADDR_STORAGE_SET_PORT(addr, port); 245 SetPort(&addr, port);
244 if (TEMP_FAILURE_RETRY( 246 if (TEMP_FAILURE_RETRY(
245 bind(fd, 247 bind(fd,
246 reinterpret_cast<struct sockaddr*>(&addr), 248 reinterpret_cast<struct sockaddr*>(&addr),
247 SocketAddress::GetAddrLength(addr))) < 0) { 249 SocketAddress::GetAddrLength(addr))) < 0) {
248 TEMP_FAILURE_RETRY(close(fd)); 250 TEMP_FAILURE_RETRY(close(fd));
249 return -1; 251 return -1;
250 } 252 }
251 253
252 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { 254 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
253 TEMP_FAILURE_RETRY(close(fd)); 255 TEMP_FAILURE_RETRY(close(fd));
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { 316 bool Socket::SetNoDelay(intptr_t fd, bool enabled) {
315 int on = enabled ? 1 : 0; 317 int on = enabled ? 1 : 0;
316 return TEMP_FAILURE_RETRY(setsockopt(fd, 318 return TEMP_FAILURE_RETRY(setsockopt(fd,
317 SOL_TCP, 319 SOL_TCP,
318 TCP_NODELAY, 320 TCP_NODELAY,
319 reinterpret_cast<char *>(&on), 321 reinterpret_cast<char *>(&on),
320 sizeof(on))) == 0; 322 sizeof(on))) == 0;
321 } 323 }
322 324
323 #endif // defined(TARGET_OS_LINUX) 325 #endif // defined(TARGET_OS_LINUX)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698