Chromium Code Reviews| 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 } | 100 } |
| 101 return ntohs(socket_address.sin_port); | 101 return ntohs(socket_address.sin_port); |
| 102 } | 102 } |
| 103 | 103 |
| 104 | 104 |
| 105 intptr_t Socket::GetStdioHandle(int num) { | 105 intptr_t Socket::GetStdioHandle(int num) { |
| 106 return static_cast<intptr_t>(num); | 106 return static_cast<intptr_t>(num); |
| 107 } | 107 } |
| 108 | 108 |
| 109 | 109 |
| 110 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { | |
| 111 // Perform a name lookup for an IPv4 address. | |
| 112 struct addrinfo hints; | |
| 113 memset(&hints, 0, sizeof(hints)); | |
| 114 hints.ai_family = AF_INET; | |
| 115 hints.ai_socktype = SOCK_STREAM; | |
| 116 hints.ai_protocol = IPPROTO_TCP; | |
| 117 struct addrinfo* info = NULL; | |
| 118 int status = getaddrinfo(host, 0, &hints, &info); | |
| 119 if (status != 0) { | |
| 120 ASSERT(*os_error == NULL); | |
| 121 *os_error = new OSError(status, | |
| 122 gai_strerror(status), | |
| 123 OSError::kGetAddressInfo); | |
| 124 (*os_error)->set_code(status); | |
| 125 (*os_error)->SetMessage(gai_strerror(status)); | |
| 126 return NULL; | |
| 127 } | |
| 128 // Convert the address into IPv4 dotted decomal notation. | |
|
Mads Ager (google)
2012/03/14 12:51:51
decomal -> decimal
Søren Gjesse
2012/03/19 10:05:43
Done.
| |
| 129 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); | |
| 130 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); | |
| 131 const char* result = inet_ntop(AF_INET, | |
| 132 reinterpret_cast<void *>(&sockaddr->sin_addr), | |
| 133 buffer, | |
| 134 INET_ADDRSTRLEN); | |
| 135 if (result == NULL) { | |
| 136 free(buffer); | |
| 137 return NULL; | |
| 138 } | |
| 139 ASSERT(result == buffer); | |
| 140 return buffer; | |
| 141 } | |
| 142 | |
| 143 | |
| 110 intptr_t ServerSocket::CreateBindListen(const char* host, | 144 intptr_t ServerSocket::CreateBindListen(const char* host, |
| 111 intptr_t port, | 145 intptr_t port, |
| 112 intptr_t backlog) { | 146 intptr_t backlog) { |
| 113 intptr_t fd; | 147 intptr_t fd; |
| 114 struct sockaddr_in server_address; | 148 struct sockaddr_in server_address; |
| 115 | 149 |
| 116 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 150 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
| 117 if (fd < 0) { | 151 if (fd < 0) { |
| 118 fprintf(stderr, "Error CreateBind: %s\n", strerror(errno)); | 152 fprintf(stderr, "Error CreateBind: %s\n", strerror(errno)); |
| 119 return -1; | 153 return -1; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 struct sockaddr clientaddr; | 185 struct sockaddr clientaddr; |
| 152 socklen_t addrlen = sizeof(clientaddr); | 186 socklen_t addrlen = sizeof(clientaddr); |
| 153 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | 187 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
| 154 if (socket < 0) { | 188 if (socket < 0) { |
| 155 fprintf(stderr, "Error Accept: %s\n", strerror(errno)); | 189 fprintf(stderr, "Error Accept: %s\n", strerror(errno)); |
| 156 } else { | 190 } else { |
| 157 FDUtils::SetNonBlocking(socket); | 191 FDUtils::SetNonBlocking(socket); |
| 158 } | 192 } |
| 159 return socket; | 193 return socket; |
| 160 } | 194 } |
| OLD | NEW |