| 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 #ifndef RUNTIME_BIN_SOCKET_BASE_H_ | 5 #ifndef RUNTIME_BIN_SOCKET_BASE_H_ |
| 6 #define RUNTIME_BIN_SOCKET_BASE_H_ | 6 #define RUNTIME_BIN_SOCKET_BASE_H_ |
| 7 | 7 |
| 8 #if defined(DART_IO_DISABLED) | 8 #if defined(DART_IO_DISABLED) |
| 9 #error "socket_base.h can only be included on builds with IO enabled" | 9 #error "socket_base.h can only be included on builds with IO enabled" |
| 10 #endif | 10 #endif |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 ADDRESS_ANY_IP_V4, | 56 ADDRESS_ANY_IP_V4, |
| 57 ADDRESS_ANY_IP_V6, | 57 ADDRESS_ANY_IP_V6, |
| 58 ADDRESS_FIRST = ADDRESS_LOOPBACK_IP_V4, | 58 ADDRESS_FIRST = ADDRESS_LOOPBACK_IP_V4, |
| 59 ADDRESS_LAST = ADDRESS_ANY_IP_V6, | 59 ADDRESS_LAST = ADDRESS_ANY_IP_V6, |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 explicit SocketAddress(struct sockaddr* sa); | 62 explicit SocketAddress(struct sockaddr* sa); |
| 63 | 63 |
| 64 ~SocketAddress() {} | 64 ~SocketAddress() {} |
| 65 | 65 |
| 66 int GetType() { | 66 int GetType(); |
| 67 if (addr_.ss.ss_family == AF_INET6) { | |
| 68 return TYPE_IPV6; | |
| 69 } | |
| 70 return TYPE_IPV4; | |
| 71 } | |
| 72 | 67 |
| 73 const char* as_string() const { return as_string_; } | 68 const char* as_string() const { return as_string_; } |
| 74 const RawAddr& addr() const { return addr_; } | 69 const RawAddr& addr() const { return addr_; } |
| 75 | 70 |
| 76 static intptr_t GetAddrLength(const RawAddr& addr) { | 71 static intptr_t GetAddrLength(const RawAddr& addr); |
| 77 ASSERT((addr.ss.ss_family == AF_INET) || (addr.ss.ss_family == AF_INET6)); | 72 static intptr_t GetInAddrLength(const RawAddr& addr); |
| 78 return (addr.ss.ss_family == AF_INET6) ? sizeof(struct sockaddr_in6) | 73 static bool AreAddressesEqual(const RawAddr& a, const RawAddr& b); |
| 79 : sizeof(struct sockaddr_in); | 74 static void GetSockAddr(Dart_Handle obj, RawAddr* addr); |
| 80 } | 75 static int16_t FromType(int type); |
| 81 | 76 static void SetAddrPort(RawAddr* addr, intptr_t port); |
| 82 static intptr_t GetInAddrLength(const RawAddr& addr) { | 77 static intptr_t GetAddrPort(const RawAddr& addr); |
| 83 ASSERT((addr.ss.ss_family == AF_INET) || (addr.ss.ss_family == AF_INET6)); | 78 static Dart_Handle ToTypedData(const RawAddr& addr); |
| 84 return (addr.ss.ss_family == AF_INET6) ? sizeof(struct in6_addr) | 79 static CObjectUint8Array* ToCObject(const RawAddr& addr); |
| 85 : sizeof(struct in_addr); | |
| 86 } | |
| 87 | |
| 88 static bool AreAddressesEqual(const RawAddr& a, const RawAddr& b) { | |
| 89 if (a.ss.ss_family == AF_INET) { | |
| 90 if (b.ss.ss_family != AF_INET) { | |
| 91 return false; | |
| 92 } | |
| 93 return memcmp(&a.in.sin_addr, &b.in.sin_addr, sizeof(a.in.sin_addr)) == 0; | |
| 94 } else if (a.ss.ss_family == AF_INET6) { | |
| 95 if (b.ss.ss_family != AF_INET6) { | |
| 96 return false; | |
| 97 } | |
| 98 return memcmp(&a.in6.sin6_addr, &b.in6.sin6_addr, | |
| 99 sizeof(a.in6.sin6_addr)) == 0; | |
| 100 } else { | |
| 101 UNREACHABLE(); | |
| 102 return false; | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 static void GetSockAddr(Dart_Handle obj, RawAddr* addr) { | |
| 107 Dart_TypedData_Type data_type; | |
| 108 uint8_t* data = NULL; | |
| 109 intptr_t len; | |
| 110 Dart_Handle result = Dart_TypedDataAcquireData( | |
| 111 obj, &data_type, reinterpret_cast<void**>(&data), &len); | |
| 112 if (Dart_IsError(result)) { | |
| 113 Dart_PropagateError(result); | |
| 114 } | |
| 115 if ((data_type != Dart_TypedData_kUint8) || | |
| 116 ((len != sizeof(in_addr)) && (len != sizeof(in6_addr)))) { | |
| 117 Dart_PropagateError( | |
| 118 Dart_NewApiError("Unexpected type for socket address")); | |
| 119 } | |
| 120 memset(reinterpret_cast<void*>(addr), 0, sizeof(RawAddr)); | |
| 121 if (len == sizeof(in_addr)) { | |
| 122 addr->in.sin_family = AF_INET; | |
| 123 memmove(reinterpret_cast<void*>(&addr->in.sin_addr), data, len); | |
| 124 } else { | |
| 125 ASSERT(len == sizeof(in6_addr)); | |
| 126 addr->in6.sin6_family = AF_INET6; | |
| 127 memmove(reinterpret_cast<void*>(&addr->in6.sin6_addr), data, len); | |
| 128 } | |
| 129 Dart_TypedDataReleaseData(obj); | |
| 130 } | |
| 131 | |
| 132 static int16_t FromType(int type) { | |
| 133 if (type == TYPE_ANY) { | |
| 134 return AF_UNSPEC; | |
| 135 } | |
| 136 if (type == TYPE_IPV4) { | |
| 137 return AF_INET; | |
| 138 } | |
| 139 ASSERT((type == TYPE_IPV6) && "Invalid type"); | |
| 140 return AF_INET6; | |
| 141 } | |
| 142 | |
| 143 static void SetAddrPort(RawAddr* addr, intptr_t port) { | |
| 144 if (addr->ss.ss_family == AF_INET) { | |
| 145 addr->in.sin_port = htons(port); | |
| 146 } else { | |
| 147 addr->in6.sin6_port = htons(port); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 static intptr_t GetAddrPort(const RawAddr& addr) { | |
| 152 if (addr.ss.ss_family == AF_INET) { | |
| 153 return ntohs(addr.in.sin_port); | |
| 154 } else { | |
| 155 return ntohs(addr.in6.sin6_port); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 static Dart_Handle ToTypedData(const RawAddr& addr) { | |
| 160 int len = GetInAddrLength(addr); | |
| 161 Dart_Handle result = Dart_NewTypedData(Dart_TypedData_kUint8, len); | |
| 162 if (Dart_IsError(result)) { | |
| 163 Dart_PropagateError(result); | |
| 164 } | |
| 165 Dart_Handle err; | |
| 166 if (addr.addr.sa_family == AF_INET6) { | |
| 167 err = Dart_ListSetAsBytes( | |
| 168 result, 0, reinterpret_cast<const uint8_t*>(&addr.in6.sin6_addr), | |
| 169 len); | |
| 170 } else { | |
| 171 err = Dart_ListSetAsBytes( | |
| 172 result, 0, reinterpret_cast<const uint8_t*>(&addr.in.sin_addr), len); | |
| 173 } | |
| 174 if (Dart_IsError(err)) { | |
| 175 Dart_PropagateError(err); | |
| 176 } | |
| 177 return result; | |
| 178 } | |
| 179 | |
| 180 static CObjectUint8Array* ToCObject(const RawAddr& addr) { | |
| 181 int in_addr_len = SocketAddress::GetInAddrLength(addr); | |
| 182 const void* in_addr; | |
| 183 CObjectUint8Array* data = | |
| 184 new CObjectUint8Array(CObject::NewUint8Array(in_addr_len)); | |
| 185 if (addr.addr.sa_family == AF_INET6) { | |
| 186 in_addr = reinterpret_cast<const void*>(&addr.in6.sin6_addr); | |
| 187 } else { | |
| 188 in_addr = reinterpret_cast<const void*>(&addr.in.sin_addr); | |
| 189 } | |
| 190 memmove(data->Buffer(), in_addr, in_addr_len); | |
| 191 return data; | |
| 192 } | |
| 193 | 80 |
| 194 private: | 81 private: |
| 195 char as_string_[INET6_ADDRSTRLEN]; | 82 char as_string_[INET6_ADDRSTRLEN]; |
| 196 RawAddr addr_; | 83 RawAddr addr_; |
| 197 | 84 |
| 198 DISALLOW_COPY_AND_ASSIGN(SocketAddress); | 85 DISALLOW_COPY_AND_ASSIGN(SocketAddress); |
| 199 }; | 86 }; |
| 200 | 87 |
| 201 | |
| 202 class InterfaceSocketAddress { | 88 class InterfaceSocketAddress { |
| 203 public: | 89 public: |
| 204 InterfaceSocketAddress(struct sockaddr* sa, | 90 InterfaceSocketAddress(struct sockaddr* sa, |
| 205 const char* interface_name, | 91 const char* interface_name, |
| 206 intptr_t interface_index) | 92 intptr_t interface_index) |
| 207 : socket_address_(new SocketAddress(sa)), | 93 : socket_address_(new SocketAddress(sa)), |
| 208 interface_name_(interface_name), | 94 interface_name_(interface_name), |
| 209 interface_index_(interface_index) {} | 95 interface_index_(interface_index) {} |
| 210 | 96 |
| 211 ~InterfaceSocketAddress() { delete socket_address_; } | 97 ~InterfaceSocketAddress() { delete socket_address_; } |
| 212 | 98 |
| 213 SocketAddress* socket_address() const { return socket_address_; } | 99 SocketAddress* socket_address() const { return socket_address_; } |
| 214 const char* interface_name() const { return interface_name_; } | 100 const char* interface_name() const { return interface_name_; } |
| 215 int interface_index() const { return interface_index_; } | 101 int interface_index() const { return interface_index_; } |
| 216 | 102 |
| 217 private: | 103 private: |
| 218 SocketAddress* socket_address_; | 104 SocketAddress* socket_address_; |
| 219 const char* interface_name_; | 105 const char* interface_name_; |
| 220 intptr_t interface_index_; | 106 intptr_t interface_index_; |
| 221 | 107 |
| 222 DISALLOW_COPY_AND_ASSIGN(InterfaceSocketAddress); | 108 DISALLOW_COPY_AND_ASSIGN(InterfaceSocketAddress); |
| 223 }; | 109 }; |
| 224 | 110 |
| 225 | |
| 226 template <typename T> | 111 template <typename T> |
| 227 class AddressList { | 112 class AddressList { |
| 228 public: | 113 public: |
| 229 explicit AddressList(intptr_t count) | 114 explicit AddressList(intptr_t count) |
| 230 : count_(count), addresses_(new T*[count_]) {} | 115 : count_(count), addresses_(new T*[count_]) {} |
| 231 | 116 |
| 232 ~AddressList() { | 117 ~AddressList() { |
| 233 for (intptr_t i = 0; i < count_; i++) { | 118 for (intptr_t i = 0; i < count_; i++) { |
| 234 delete addresses_[i]; | 119 delete addresses_[i]; |
| 235 } | 120 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 248 }; | 133 }; |
| 249 | 134 |
| 250 class SocketBase : public AllStatic { | 135 class SocketBase : public AllStatic { |
| 251 public: | 136 public: |
| 252 enum SocketRequest { | 137 enum SocketRequest { |
| 253 kLookupRequest = 0, | 138 kLookupRequest = 0, |
| 254 kListInterfacesRequest = 1, | 139 kListInterfacesRequest = 1, |
| 255 kReverseLookupRequest = 2, | 140 kReverseLookupRequest = 2, |
| 256 }; | 141 }; |
| 257 | 142 |
| 143 enum SocketOpKind { |
| 144 kSync, |
| 145 kAsync, |
| 146 }; |
| 147 |
| 258 // TODO(dart:io): Convert these to instance methods where possible. | 148 // TODO(dart:io): Convert these to instance methods where possible. |
| 259 static bool Initialize(); | 149 static bool Initialize(); |
| 260 static intptr_t Available(intptr_t fd); | 150 static intptr_t Available(intptr_t fd); |
| 261 static intptr_t Read(intptr_t fd, void* buffer, intptr_t num_bytes); | 151 static intptr_t Read(intptr_t fd, |
| 262 static intptr_t Write(intptr_t fd, const void* buffer, intptr_t num_bytes); | 152 void* buffer, |
| 153 intptr_t num_bytes, |
| 154 SocketOpKind sync); |
| 155 static intptr_t Write(intptr_t fd, |
| 156 const void* buffer, |
| 157 intptr_t num_bytes, |
| 158 SocketOpKind sync); |
| 263 // Send data on a socket. The port to send to is specified in the port | 159 // Send data on a socket. The port to send to is specified in the port |
| 264 // component of the passed RawAddr structure. The RawAddr structure is only | 160 // component of the passed RawAddr structure. The RawAddr structure is only |
| 265 // used for datagram sockets. | 161 // used for datagram sockets. |
| 266 static intptr_t SendTo(intptr_t fd, | 162 static intptr_t SendTo(intptr_t fd, |
| 267 const void* buffer, | 163 const void* buffer, |
| 268 intptr_t num_bytes, | 164 intptr_t num_bytes, |
| 269 const RawAddr& addr); | 165 const RawAddr& addr, |
| 166 SocketOpKind sync); |
| 270 static intptr_t RecvFrom(intptr_t fd, | 167 static intptr_t RecvFrom(intptr_t fd, |
| 271 void* buffer, | 168 void* buffer, |
| 272 intptr_t num_bytes, | 169 intptr_t num_bytes, |
| 273 RawAddr* addr); | 170 RawAddr* addr, |
| 171 SocketOpKind sync); |
| 274 // Returns true if the given error-number is because the system was not able | 172 // Returns true if the given error-number is because the system was not able |
| 275 // to bind the socket to a specific IP. | 173 // to bind the socket to a specific IP. |
| 276 static bool IsBindError(intptr_t error_number); | 174 static bool IsBindError(intptr_t error_number); |
| 277 static intptr_t GetPort(intptr_t fd); | 175 static intptr_t GetPort(intptr_t fd); |
| 278 static SocketAddress* GetRemotePeer(intptr_t fd, intptr_t* port); | 176 static SocketAddress* GetRemotePeer(intptr_t fd, intptr_t* port); |
| 279 static void GetError(intptr_t fd, OSError* os_error); | 177 static void GetError(intptr_t fd, OSError* os_error); |
| 280 static int GetType(intptr_t fd); | 178 static int GetType(intptr_t fd); |
| 281 static intptr_t GetStdioHandle(intptr_t num); | 179 static intptr_t GetStdioHandle(intptr_t num); |
| 282 static void Close(intptr_t fd); | 180 static void Close(intptr_t fd); |
| 283 static bool GetNoDelay(intptr_t fd, bool* enabled); | 181 static bool GetNoDelay(intptr_t fd, bool* enabled); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 | 218 |
| 321 private: | 219 private: |
| 322 DISALLOW_ALLOCATION(); | 220 DISALLOW_ALLOCATION(); |
| 323 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketBase); | 221 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketBase); |
| 324 }; | 222 }; |
| 325 | 223 |
| 326 } // namespace bin | 224 } // namespace bin |
| 327 } // namespace dart | 225 } // namespace dart |
| 328 | 226 |
| 329 #endif // RUNTIME_BIN_SOCKET_BASE_H_ | 227 #endif // RUNTIME_BIN_SOCKET_BASE_H_ |
| OLD | NEW |