| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
| 6 | 6 |
| 7 #include "bin/socket_base.h" | 7 #include "bin/socket_base.h" |
| 8 | 8 |
| 9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
| 10 #include "bin/io_buffer.h" | 10 #include "bin/io_buffer.h" |
| 11 #include "bin/isolate_data.h" | 11 #include "bin/isolate_data.h" |
| 12 #include "bin/lockers.h" | 12 #include "bin/lockers.h" |
| 13 #include "bin/thread.h" | 13 #include "bin/thread.h" |
| 14 #include "bin/utils.h" | 14 #include "bin/utils.h" |
| 15 | 15 |
| 16 #include "include/dart_api.h" | 16 #include "include/dart_api.h" |
| 17 | 17 |
| 18 #include "platform/globals.h" | 18 #include "platform/globals.h" |
| 19 #include "platform/utils.h" | 19 #include "platform/utils.h" |
| 20 | 20 |
| 21 namespace dart { | 21 namespace dart { |
| 22 namespace bin { | 22 namespace bin { |
| 23 | 23 |
| 24 int SocketAddress::GetType() { |
| 25 if (addr_.ss.ss_family == AF_INET6) { |
| 26 return TYPE_IPV6; |
| 27 } |
| 28 return TYPE_IPV4; |
| 29 } |
| 30 |
| 31 |
| 32 intptr_t SocketAddress::GetAddrLength(const RawAddr& addr) { |
| 33 ASSERT((addr.ss.ss_family == AF_INET) || (addr.ss.ss_family == AF_INET6)); |
| 34 return (addr.ss.ss_family == AF_INET6) ? sizeof(struct sockaddr_in6) |
| 35 : sizeof(struct sockaddr_in); |
| 36 } |
| 37 |
| 38 |
| 39 intptr_t SocketAddress::GetInAddrLength(const RawAddr& addr) { |
| 40 ASSERT((addr.ss.ss_family == AF_INET) || (addr.ss.ss_family == AF_INET6)); |
| 41 return (addr.ss.ss_family == AF_INET6) ? sizeof(struct in6_addr) |
| 42 : sizeof(struct in_addr); |
| 43 } |
| 44 |
| 45 |
| 46 bool SocketAddress::AreAddressesEqual(const RawAddr& a, const RawAddr& b) { |
| 47 if (a.ss.ss_family == AF_INET) { |
| 48 if (b.ss.ss_family != AF_INET) { |
| 49 return false; |
| 50 } |
| 51 return memcmp(&a.in.sin_addr, &b.in.sin_addr, sizeof(a.in.sin_addr)) == 0; |
| 52 } else if (a.ss.ss_family == AF_INET6) { |
| 53 if (b.ss.ss_family != AF_INET6) { |
| 54 return false; |
| 55 } |
| 56 return memcmp(&a.in6.sin6_addr, &b.in6.sin6_addr, |
| 57 sizeof(a.in6.sin6_addr)) == 0; |
| 58 } else { |
| 59 UNREACHABLE(); |
| 60 return false; |
| 61 } |
| 62 } |
| 63 |
| 64 |
| 65 void SocketAddress::GetSockAddr(Dart_Handle obj, RawAddr* addr) { |
| 66 Dart_TypedData_Type data_type; |
| 67 uint8_t* data = NULL; |
| 68 intptr_t len; |
| 69 Dart_Handle result = Dart_TypedDataAcquireData( |
| 70 obj, &data_type, reinterpret_cast<void**>(&data), &len); |
| 71 if (Dart_IsError(result)) { |
| 72 Dart_PropagateError(result); |
| 73 } |
| 74 if ((data_type != Dart_TypedData_kUint8) || |
| 75 ((len != sizeof(in_addr)) && (len != sizeof(in6_addr)))) { |
| 76 Dart_PropagateError(Dart_NewApiError("Unexpected type for socket address")); |
| 77 } |
| 78 memset(reinterpret_cast<void*>(addr), 0, sizeof(RawAddr)); |
| 79 if (len == sizeof(in_addr)) { |
| 80 addr->in.sin_family = AF_INET; |
| 81 memmove(reinterpret_cast<void*>(&addr->in.sin_addr), data, len); |
| 82 } else { |
| 83 ASSERT(len == sizeof(in6_addr)); |
| 84 addr->in6.sin6_family = AF_INET6; |
| 85 memmove(reinterpret_cast<void*>(&addr->in6.sin6_addr), data, len); |
| 86 } |
| 87 Dart_TypedDataReleaseData(obj); |
| 88 } |
| 89 |
| 90 |
| 91 int16_t SocketAddress::FromType(int type) { |
| 92 if (type == TYPE_ANY) { |
| 93 return AF_UNSPEC; |
| 94 } |
| 95 if (type == TYPE_IPV4) { |
| 96 return AF_INET; |
| 97 } |
| 98 ASSERT((type == TYPE_IPV6) && "Invalid type"); |
| 99 return AF_INET6; |
| 100 } |
| 101 |
| 102 |
| 103 void SocketAddress::SetAddrPort(RawAddr* addr, intptr_t port) { |
| 104 if (addr->ss.ss_family == AF_INET) { |
| 105 addr->in.sin_port = htons(port); |
| 106 } else { |
| 107 addr->in6.sin6_port = htons(port); |
| 108 } |
| 109 } |
| 110 |
| 111 |
| 112 intptr_t SocketAddress::GetAddrPort(const RawAddr& addr) { |
| 113 if (addr.ss.ss_family == AF_INET) { |
| 114 return ntohs(addr.in.sin_port); |
| 115 } else { |
| 116 return ntohs(addr.in6.sin6_port); |
| 117 } |
| 118 } |
| 119 |
| 120 |
| 121 Dart_Handle SocketAddress::ToTypedData(const RawAddr& addr) { |
| 122 int len = GetInAddrLength(addr); |
| 123 Dart_Handle result = Dart_NewTypedData(Dart_TypedData_kUint8, len); |
| 124 if (Dart_IsError(result)) { |
| 125 Dart_PropagateError(result); |
| 126 } |
| 127 Dart_Handle err; |
| 128 if (addr.addr.sa_family == AF_INET6) { |
| 129 err = Dart_ListSetAsBytes( |
| 130 result, 0, reinterpret_cast<const uint8_t*>(&addr.in6.sin6_addr), len); |
| 131 } else { |
| 132 err = Dart_ListSetAsBytes( |
| 133 result, 0, reinterpret_cast<const uint8_t*>(&addr.in.sin_addr), len); |
| 134 } |
| 135 if (Dart_IsError(err)) { |
| 136 Dart_PropagateError(err); |
| 137 } |
| 138 return result; |
| 139 } |
| 140 |
| 141 |
| 142 CObjectUint8Array* SocketAddress::ToCObject(const RawAddr& addr) { |
| 143 int in_addr_len = SocketAddress::GetInAddrLength(addr); |
| 144 const void* in_addr; |
| 145 CObjectUint8Array* data = |
| 146 new CObjectUint8Array(CObject::NewUint8Array(in_addr_len)); |
| 147 if (addr.addr.sa_family == AF_INET6) { |
| 148 in_addr = reinterpret_cast<const void*>(&addr.in6.sin6_addr); |
| 149 } else { |
| 150 in_addr = reinterpret_cast<const void*>(&addr.in.sin_addr); |
| 151 } |
| 152 memmove(data->Buffer(), in_addr, in_addr_len); |
| 153 return data; |
| 154 } |
| 155 |
| 156 |
| 24 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { | 157 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { |
| 25 const char* address = | 158 const char* address = |
| 26 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 159 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 27 ASSERT(address != NULL); | 160 ASSERT(address != NULL); |
| 28 RawAddr raw; | 161 RawAddr raw; |
| 29 memset(&raw, 0, sizeof(raw)); | 162 memset(&raw, 0, sizeof(raw)); |
| 30 int type = strchr(address, ':') == NULL ? SocketAddress::TYPE_IPV4 | 163 int type = strchr(address, ':') == NULL ? SocketAddress::TYPE_IPV4 |
| 31 : SocketAddress::TYPE_IPV6; | 164 : SocketAddress::TYPE_IPV6; |
| 32 if (type == SocketAddress::TYPE_IPV4) { | 165 if (type == SocketAddress::TYPE_IPV4) { |
| 33 raw.addr.sa_family = AF_INET; | 166 raw.addr.sa_family = AF_INET; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 53 intptr_t error_number = | 186 intptr_t error_number = |
| 54 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); | 187 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); |
| 55 bool is_bind_error = SocketBase::IsBindError(error_number); | 188 bool is_bind_error = SocketBase::IsBindError(error_number); |
| 56 Dart_SetReturnValue(args, is_bind_error ? Dart_True() : Dart_False()); | 189 Dart_SetReturnValue(args, is_bind_error ? Dart_True() : Dart_False()); |
| 57 } | 190 } |
| 58 | 191 |
| 59 } // namespace bin | 192 } // namespace bin |
| 60 } // namespace dart | 193 } // namespace dart |
| 61 | 194 |
| 62 #endif // !defined(DART_IO_DISABLED) | 195 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |