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

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

Issue 11312242: Revised CL for customisable logging (replacing printfs). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 1 month 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
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 <errno.h> 5 #include <errno.h>
6 #include <stdio.h> 6 #include <stdio.h>
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
9 #include <unistd.h> 9 #include <unistd.h>
10 10
11 #include "bin/fdutils.h" 11 #include "bin/fdutils.h"
12 #include "bin/log.h"
12 #include "bin/socket.h" 13 #include "bin/socket.h"
13 14
14 15
15 bool Socket::Initialize() { 16 bool Socket::Initialize() {
16 // Nothing to do on Linux. 17 // Nothing to do on Linux.
17 return true; 18 return true;
18 } 19 }
19 20
20 21
21 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { 22 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) {
22 intptr_t fd; 23 intptr_t fd;
23 struct hostent server; 24 struct hostent server;
24 struct sockaddr_in server_address; 25 struct sockaddr_in server_address;
25 26
26 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); 27 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0));
27 if (fd < 0) { 28 if (fd < 0) {
28 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); 29 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno));
29 return -1; 30 return -1;
30 } 31 }
31 32
32 FDUtils::SetNonBlocking(fd); 33 FDUtils::SetNonBlocking(fd);
33 34
34 static const size_t kTempBufSize = 1024; 35 static const size_t kTempBufSize = 1024;
35 char temp_buf[kTempBufSize]; 36 char temp_buf[kTempBufSize];
36 struct hostent *unused; 37 struct hostent *unused;
37 int err; 38 int err;
38 if (gethostbyname_r( 39 if (gethostbyname_r(
39 host, &server, temp_buf, kTempBufSize, &unused, &err) != 0) { 40 host, &server, temp_buf, kTempBufSize, &unused, &err) != 0) {
40 TEMP_FAILURE_RETRY(close(fd)); 41 TEMP_FAILURE_RETRY(close(fd));
41 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); 42 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno));
42 return -1; 43 return -1;
43 } 44 }
44 45
45 server_address.sin_family = AF_INET; 46 server_address.sin_family = AF_INET;
46 server_address.sin_port = htons(port); 47 server_address.sin_port = htons(port);
47 bcopy(server.h_addr, &server_address.sin_addr.s_addr, server.h_length); 48 bcopy(server.h_addr, &server_address.sin_addr.s_addr, server.h_length);
48 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); 49 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
49 intptr_t result = TEMP_FAILURE_RETRY( 50 intptr_t result = TEMP_FAILURE_RETRY(
50 connect(fd, 51 connect(fd,
51 reinterpret_cast<struct sockaddr *>(&server_address), 52 reinterpret_cast<struct sockaddr *>(&server_address),
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 90
90 91
91 intptr_t Socket::GetPort(intptr_t fd) { 92 intptr_t Socket::GetPort(intptr_t fd) {
92 ASSERT(fd >= 0); 93 ASSERT(fd >= 0);
93 struct sockaddr_in socket_address; 94 struct sockaddr_in socket_address;
94 socklen_t size = sizeof(socket_address); 95 socklen_t size = sizeof(socket_address);
95 if (TEMP_FAILURE_RETRY( 96 if (TEMP_FAILURE_RETRY(
96 getsockname(fd, 97 getsockname(fd,
97 reinterpret_cast<struct sockaddr *>(&socket_address), 98 reinterpret_cast<struct sockaddr *>(&socket_address),
98 &size))) { 99 &size))) {
99 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); 100 Log::PrintErr("Error getsockname: %s\n", strerror(errno));
100 return 0; 101 return 0;
101 } 102 }
102 return ntohs(socket_address.sin_port); 103 return ntohs(socket_address.sin_port);
103 } 104 }
104 105
105 106
106 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { 107 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) {
107 ASSERT(fd >= 0); 108 ASSERT(fd >= 0);
108 struct sockaddr_in socket_address; 109 struct sockaddr_in socket_address;
109 socklen_t size = sizeof(socket_address); 110 socklen_t size = sizeof(socket_address);
110 if (TEMP_FAILURE_RETRY( 111 if (TEMP_FAILURE_RETRY(
111 getpeername(fd, 112 getpeername(fd,
112 reinterpret_cast<struct sockaddr *>(&socket_address), 113 reinterpret_cast<struct sockaddr *>(&socket_address),
113 &size))) { 114 &size))) {
114 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); 115 Log::PrintErr("Error getpeername: %s\n", strerror(errno));
115 return false; 116 return false;
116 } 117 }
117 if (inet_ntop(socket_address.sin_family, 118 if (inet_ntop(socket_address.sin_family,
118 reinterpret_cast<const void *>(&socket_address.sin_addr), 119 reinterpret_cast<const void *>(&socket_address.sin_addr),
119 host, 120 host,
120 INET_ADDRSTRLEN) == NULL) { 121 INET_ADDRSTRLEN) == NULL) {
121 fprintf(stderr, "Error inet_ntop: %s\n", strerror(errno)); 122 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno));
122 return false; 123 return false;
123 } 124 }
124 *port = ntohs(socket_address.sin_port); 125 *port = ntohs(socket_address.sin_port);
125 return true; 126 return true;
126 } 127 }
127 128
128 129
129 void Socket::GetError(intptr_t fd, OSError* os_error) { 130 void Socket::GetError(intptr_t fd, OSError* os_error) {
130 int len = sizeof(errno); 131 int len = sizeof(errno);
131 getsockopt(fd, 132 getsockopt(fd,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 intptr_t fd; 181 intptr_t fd;
181 struct sockaddr_in server_address; 182 struct sockaddr_in server_address;
182 183
183 in_addr_t s_addr = inet_addr(host); 184 in_addr_t s_addr = inet_addr(host);
184 if (s_addr == INADDR_NONE) { 185 if (s_addr == INADDR_NONE) {
185 return -5; 186 return -5;
186 } 187 }
187 188
188 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); 189 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0));
189 if (fd < 0) { 190 if (fd < 0) {
190 fprintf(stderr, "Error CreateBind: %s\n", strerror(errno)); 191 Log::PrintErr("Error CreateBind: %s\n", strerror(errno));
191 return -1; 192 return -1;
192 } 193 }
193 194
194 int optval = 1; 195 int optval = 1;
195 TEMP_FAILURE_RETRY( 196 TEMP_FAILURE_RETRY(
196 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); 197 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
197 198
198 server_address.sin_family = AF_INET; 199 server_address.sin_family = AF_INET;
199 server_address.sin_port = htons(port); 200 server_address.sin_port = htons(port);
200 server_address.sin_addr.s_addr = s_addr; 201 server_address.sin_addr.s_addr = s_addr;
201 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); 202 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
202 203
203 if (TEMP_FAILURE_RETRY( 204 if (TEMP_FAILURE_RETRY(
204 bind(fd, 205 bind(fd,
205 reinterpret_cast<struct sockaddr *>(&server_address), 206 reinterpret_cast<struct sockaddr *>(&server_address),
206 sizeof(server_address))) < 0) { 207 sizeof(server_address))) < 0) {
207 TEMP_FAILURE_RETRY(close(fd)); 208 TEMP_FAILURE_RETRY(close(fd));
208 fprintf(stderr, "Error Bind: %s\n", strerror(errno)); 209 Log::PrintErr("Error Bind: %s\n", strerror(errno));
209 return -1; 210 return -1;
210 } 211 }
211 212
212 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { 213 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) {
213 fprintf(stderr, "Error Listen: %s\n", strerror(errno)); 214 Log::PrintErr("Error Listen: %s\n", strerror(errno));
214 return -1; 215 return -1;
215 } 216 }
216 217
217 FDUtils::SetNonBlocking(fd); 218 FDUtils::SetNonBlocking(fd);
218 return fd; 219 return fd;
219 } 220 }
220 221
221 222
222 static bool IsTemporaryAcceptError(int error) { 223 static bool IsTemporaryAcceptError(int error) {
223 // On Linux a number of protocol errors should be treated as EAGAIN. 224 // On Linux a number of protocol errors should be treated as EAGAIN.
(...skipping 16 matching lines...) Expand all
240 // error. We got woken up from the poll on the listening socket, 241 // error. We got woken up from the poll on the listening socket,
241 // but there is no connection ready to be accepted. 242 // but there is no connection ready to be accepted.
242 ASSERT(kTemporaryFailure != -1); 243 ASSERT(kTemporaryFailure != -1);
243 socket = kTemporaryFailure; 244 socket = kTemporaryFailure;
244 } 245 }
245 } else { 246 } else {
246 FDUtils::SetNonBlocking(socket); 247 FDUtils::SetNonBlocking(socket);
247 } 248 }
248 return socket; 249 return socket;
249 } 250 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698