| OLD | NEW |
| 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_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
| 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 #include <ifaddrs.h> // NOLINT | 15 #include <ifaddrs.h> // NOLINT |
| 16 | 16 |
| 17 #include "bin/fdutils.h" | 17 #include "bin/fdutils.h" |
| 18 #include "bin/file.h" | 18 #include "bin/file.h" |
| 19 #include "bin/log.h" | 19 #include "bin/log.h" |
| 20 #include "bin/socket.h" | 20 #include "bin/socket.h" |
| 21 | 21 |
| 22 | 22 |
| 23 namespace dart { | 23 namespace dart { |
| 24 namespace bin { | 24 namespace bin { |
| 25 | 25 |
| 26 SocketAddress::SocketAddress(struct sockaddr* sockaddr) { | 26 SocketAddress::SocketAddress(struct sockaddr* sa) { |
| 27 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 27 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| 28 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr); | 28 socklen_t salen = GetAddrLength(reinterpret_cast<RawAddr*>(sa)); |
| 29 const char* result; | 29 if (TEMP_FAILURE_RETRY(getnameinfo(sa, |
| 30 if (sockaddr->sa_family == AF_INET) { | 30 salen, |
| 31 result = inet_ntop(sockaddr->sa_family, | 31 as_string_, |
| 32 &raw->in.sin_addr, | 32 INET6_ADDRSTRLEN, |
| 33 as_string_, | 33 NULL, |
| 34 INET_ADDRSTRLEN); | 34 0, |
| 35 } else { | 35 NI_NUMERICHOST)) != 0) { |
| 36 ASSERT(sockaddr->sa_family == AF_INET6); | 36 as_string_[0] = 0; |
| 37 result = inet_ntop(sockaddr->sa_family, | |
| 38 &raw->in6.sin6_addr, | |
| 39 as_string_, | |
| 40 INET6_ADDRSTRLEN); | |
| 41 } | 37 } |
| 42 if (result == NULL) as_string_[0] = 0; | 38 memmove(reinterpret_cast<void *>(&addr_), sa, salen); |
| 43 memmove(reinterpret_cast<void *>(&addr_), | |
| 44 sockaddr, | |
| 45 GetAddrLength(raw)); | |
| 46 } | 39 } |
| 47 | 40 |
| 48 | 41 |
| 49 bool Socket::Initialize() { | 42 bool Socket::Initialize() { |
| 50 // Nothing to do on Mac OS. | 43 // Nothing to do on Mac OS. |
| 51 return true; | 44 return true; |
| 52 } | 45 } |
| 53 | 46 |
| 54 | 47 |
| 55 intptr_t Socket::Create(RawAddr addr) { | 48 intptr_t Socket::Create(RawAddr addr) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 if (TEMP_FAILURE_RETRY( | 144 if (TEMP_FAILURE_RETRY( |
| 152 getpeername(fd, | 145 getpeername(fd, |
| 153 &raw.addr, | 146 &raw.addr, |
| 154 &size))) { | 147 &size))) { |
| 155 const int kBufferSize = 1024; | 148 const int kBufferSize = 1024; |
| 156 char error_message[kBufferSize]; | 149 char error_message[kBufferSize]; |
| 157 strerror_r(errno, error_message, kBufferSize); | 150 strerror_r(errno, error_message, kBufferSize); |
| 158 Log::PrintErr("Error getpeername: %s\n", error_message); | 151 Log::PrintErr("Error getpeername: %s\n", error_message); |
| 159 return false; | 152 return false; |
| 160 } | 153 } |
| 161 const void* src; | 154 if (TEMP_FAILURE_RETRY(getnameinfo(&raw.addr, |
| 162 if (raw.ss.ss_family == AF_INET6) { | 155 size, |
| 163 src = reinterpret_cast<const void*>(&raw.in6.sin6_addr); | 156 host, |
| 164 } else { | 157 INET6_ADDRSTRLEN, |
| 165 src = reinterpret_cast<const void*>(&raw.in.sin_addr); | 158 NULL, |
| 166 } | 159 0, |
| 167 if (inet_ntop(raw.ss.ss_family, | 160 NI_NUMERICHOST)) != 0) { |
| 168 src, | |
| 169 host, | |
| 170 INET_ADDRSTRLEN) == NULL) { | |
| 171 const int kBufferSize = 1024; | 161 const int kBufferSize = 1024; |
| 172 char error_message[kBufferSize]; | 162 char error_message[kBufferSize]; |
| 173 strerror_r(errno, error_message, kBufferSize); | 163 strerror_r(errno, error_message, kBufferSize); |
| 174 Log::PrintErr("Error inet_ntop: %s\n", error_message); | 164 Log::PrintErr("Error getnameinfo: %s\n", error_message); |
| 175 return false; | 165 return false; |
| 176 } | 166 } |
| 177 *port = SocketAddress::GetAddrPort(&raw); | 167 *port = SocketAddress::GetAddrPort(&raw); |
| 178 return true; | 168 return true; |
| 179 } | 169 } |
| 180 | 170 |
| 181 | 171 |
| 182 void Socket::GetError(intptr_t fd, OSError* os_error) { | 172 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 183 int len = sizeof(errno); | 173 int len = sizeof(errno); |
| 184 getsockopt(fd, | 174 getsockopt(fd, |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 IPPROTO_TCP, | 407 IPPROTO_TCP, |
| 418 TCP_NODELAY, | 408 TCP_NODELAY, |
| 419 reinterpret_cast<char *>(&on), | 409 reinterpret_cast<char *>(&on), |
| 420 sizeof(on))) == 0; | 410 sizeof(on))) == 0; |
| 421 } | 411 } |
| 422 | 412 |
| 423 } // namespace bin | 413 } // namespace bin |
| 424 } // namespace dart | 414 } // namespace dart |
| 425 | 415 |
| 426 #endif // defined(TARGET_OS_MACOS) | 416 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |