OLD | NEW |
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 <arpa/inet.h> | 5 #include <arpa/inet.h> |
6 #include <errno.h> | 6 #include <errno.h> |
7 #include <netdb.h> | 7 #include <netdb.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 getsockname(fd, | 95 getsockname(fd, |
96 reinterpret_cast<struct sockaddr *>(&socket_address), | 96 reinterpret_cast<struct sockaddr *>(&socket_address), |
97 &size))) { | 97 &size))) { |
98 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); | 98 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); |
99 return 0; | 99 return 0; |
100 } | 100 } |
101 return ntohs(socket_address.sin_port); | 101 return ntohs(socket_address.sin_port); |
102 } | 102 } |
103 | 103 |
104 | 104 |
| 105 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 106 int len = sizeof(errno); |
| 107 getsockopt(fd, |
| 108 SOL_SOCKET, |
| 109 SO_ERROR, |
| 110 &errno, |
| 111 reinterpret_cast<socklen_t*>(&len)); |
| 112 os_error->SetCodeAndMessage(OSError::kSystem, errno); |
| 113 } |
| 114 |
| 115 |
105 intptr_t Socket::GetStdioHandle(int num) { | 116 intptr_t Socket::GetStdioHandle(int num) { |
106 return static_cast<intptr_t>(num); | 117 return static_cast<intptr_t>(num); |
107 } | 118 } |
108 | 119 |
109 | 120 |
110 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { | 121 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { |
111 // Perform a name lookup for an IPv4 address. | 122 // Perform a name lookup for an IPv4 address. |
112 struct addrinfo hints; | 123 struct addrinfo hints; |
113 memset(&hints, 0, sizeof(hints)); | 124 memset(&hints, 0, sizeof(hints)); |
114 hints.ai_family = AF_INET; | 125 hints.ai_family = AF_INET; |
115 hints.ai_socktype = SOCK_STREAM; | 126 hints.ai_socktype = SOCK_STREAM; |
116 hints.ai_protocol = IPPROTO_TCP; | 127 hints.ai_protocol = IPPROTO_TCP; |
117 struct addrinfo* info = NULL; | 128 struct addrinfo* info = NULL; |
118 int status = getaddrinfo(host, 0, &hints, &info); | 129 int status = getaddrinfo(host, 0, &hints, &info); |
119 if (status != 0) { | 130 if (status != 0) { |
120 ASSERT(*os_error == NULL); | 131 ASSERT(*os_error == NULL); |
121 *os_error = new OSError(status, | 132 *os_error = new OSError(status, |
122 gai_strerror(status), | 133 gai_strerror(status), |
123 OSError::kGetAddressInfo); | 134 OSError::kGetAddressInfo); |
124 (*os_error)->set_code(status); | |
125 (*os_error)->SetMessage(gai_strerror(status)); | |
126 return NULL; | 135 return NULL; |
127 } | 136 } |
128 // Convert the address into IPv4 dotted decimal notation. | 137 // Convert the address into IPv4 dotted decimal notation. |
129 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); | 138 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); |
130 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); | 139 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); |
131 const char* result = inet_ntop(AF_INET, | 140 const char* result = inet_ntop(AF_INET, |
132 reinterpret_cast<void *>(&sockaddr->sin_addr), | 141 reinterpret_cast<void *>(&sockaddr->sin_addr), |
133 buffer, | 142 buffer, |
134 INET_ADDRSTRLEN); | 143 INET_ADDRSTRLEN); |
135 if (result == NULL) { | 144 if (result == NULL) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 struct sockaddr clientaddr; | 194 struct sockaddr clientaddr; |
186 socklen_t addrlen = sizeof(clientaddr); | 195 socklen_t addrlen = sizeof(clientaddr); |
187 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | 196 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
188 if (socket < 0) { | 197 if (socket < 0) { |
189 fprintf(stderr, "Error Accept: %s\n", strerror(errno)); | 198 fprintf(stderr, "Error Accept: %s\n", strerror(errno)); |
190 } else { | 199 } else { |
191 FDUtils::SetNonBlocking(socket); | 200 FDUtils::SetNonBlocking(socket); |
192 } | 201 } |
193 return socket; | 202 return socket; |
194 } | 203 } |
OLD | NEW |