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