| 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 #ifndef BIN_SOCKET_H_ | 5 #ifndef BIN_SOCKET_H_ |
| 6 #define BIN_SOCKET_H_ | 6 #define BIN_SOCKET_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "platform/globals.h" | 10 #include "platform/globals.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 ~SocketAddress() {} | 59 ~SocketAddress() {} |
| 60 | 60 |
| 61 int GetType() { | 61 int GetType() { |
| 62 if (addr_.ss.ss_family == AF_INET6) return TYPE_IPV6; | 62 if (addr_.ss.ss_family == AF_INET6) return TYPE_IPV6; |
| 63 return TYPE_IPV4; | 63 return TYPE_IPV4; |
| 64 } | 64 } |
| 65 | 65 |
| 66 const char* as_string() const { return as_string_; } | 66 const char* as_string() const { return as_string_; } |
| 67 const RawAddr& addr() const { return addr_; } | 67 const RawAddr& addr() const { return addr_; } |
| 68 | 68 |
| 69 static intptr_t GetAddrLength(const RawAddr* addr) { | 69 static intptr_t GetAddrLength(const RawAddr& addr) { |
| 70 ASSERT(addr->ss.ss_family == AF_INET || addr->ss.ss_family == AF_INET6); | 70 ASSERT(addr.ss.ss_family == AF_INET || addr.ss.ss_family == AF_INET6); |
| 71 return addr->ss.ss_family == AF_INET6 ? | 71 return addr.ss.ss_family == AF_INET6 ? |
| 72 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); | 72 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); |
| 73 } | 73 } |
| 74 | 74 |
| 75 static intptr_t GetInAddrLength(const RawAddr* addr) { | 75 static intptr_t GetInAddrLength(const RawAddr& addr) { |
| 76 ASSERT(addr->ss.ss_family == AF_INET || addr->ss.ss_family == AF_INET6); | 76 ASSERT(addr.ss.ss_family == AF_INET || addr.ss.ss_family == AF_INET6); |
| 77 return addr->ss.ss_family == AF_INET6 ? | 77 return addr.ss.ss_family == AF_INET6 ? |
| 78 sizeof(struct in6_addr) : sizeof(struct in_addr); | 78 sizeof(struct in6_addr) : sizeof(struct in_addr); |
| 79 } | 79 } |
| 80 | 80 |
| 81 static bool AreAddressesEqual(const RawAddr& a, const RawAddr& b) { | 81 static bool AreAddressesEqual(const RawAddr& a, const RawAddr& b) { |
| 82 if (a.ss.ss_family == AF_INET) { | 82 if (a.ss.ss_family == AF_INET) { |
| 83 if (b.ss.ss_family != AF_INET) return false; | 83 if (b.ss.ss_family != AF_INET) return false; |
| 84 return memcmp(&a.in.sin_addr, &b.in.sin_addr, sizeof(a.in.sin_addr)) == 0; | 84 return memcmp(&a.in.sin_addr, &b.in.sin_addr, sizeof(a.in.sin_addr)) == 0; |
| 85 } else if (a.ss.ss_family == AF_INET6) { | 85 } else if (a.ss.ss_family == AF_INET6) { |
| 86 if (b.ss.ss_family != AF_INET6) return false; | 86 if (b.ss.ss_family != AF_INET6) return false; |
| 87 return memcmp(&a.in6.sin6_addr, | 87 return memcmp(&a.in6.sin6_addr, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 } | 125 } |
| 126 | 126 |
| 127 static void SetAddrPort(RawAddr* addr, intptr_t port) { | 127 static void SetAddrPort(RawAddr* addr, intptr_t port) { |
| 128 if (addr->ss.ss_family == AF_INET) { | 128 if (addr->ss.ss_family == AF_INET) { |
| 129 addr->in.sin_port = htons(port); | 129 addr->in.sin_port = htons(port); |
| 130 } else { | 130 } else { |
| 131 addr->in6.sin6_port = htons(port); | 131 addr->in6.sin6_port = htons(port); |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 | 134 |
| 135 static intptr_t GetAddrPort(const RawAddr* addr) { | 135 static intptr_t GetAddrPort(const RawAddr& addr) { |
| 136 if (addr->ss.ss_family == AF_INET) { | 136 if (addr.ss.ss_family == AF_INET) { |
| 137 return ntohs(addr->in.sin_port); | 137 return ntohs(addr.in.sin_port); |
| 138 } else { | 138 } else { |
| 139 return ntohs(addr->in6.sin6_port); | 139 return ntohs(addr.in6.sin6_port); |
| 140 } | 140 } |
| 141 } | 141 } |
| 142 | 142 |
| 143 static Dart_Handle ToTypedData(RawAddr* raw) { | 143 static Dart_Handle ToTypedData(const RawAddr& addr) { |
| 144 int len = GetInAddrLength(raw); | 144 int len = GetInAddrLength(addr); |
| 145 Dart_Handle result = Dart_NewTypedData(Dart_TypedData_kUint8, len); | 145 Dart_Handle result = Dart_NewTypedData(Dart_TypedData_kUint8, len); |
| 146 if (Dart_IsError(result)) Dart_PropagateError(result); | 146 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 147 Dart_Handle err; | 147 Dart_Handle err; |
| 148 if (raw->addr.sa_family == AF_INET6) { | 148 RawAddr& raw = const_cast<RawAddr&>(addr); |
| 149 if (addr.addr.sa_family == AF_INET6) { |
| 149 err = Dart_ListSetAsBytes( | 150 err = Dart_ListSetAsBytes( |
| 150 result, 0, reinterpret_cast<uint8_t*>(&raw->in6.sin6_addr), len); | 151 result, 0, reinterpret_cast<uint8_t*>(&raw.in6.sin6_addr), len); |
| 151 } else { | 152 } else { |
| 152 err = Dart_ListSetAsBytes( | 153 err = Dart_ListSetAsBytes( |
| 153 result, 0, reinterpret_cast<uint8_t*>(&raw->in.sin_addr), len); | 154 result, 0, reinterpret_cast<uint8_t*>(&raw.in.sin_addr), len); |
| 154 } | 155 } |
| 155 if (Dart_IsError(err)) Dart_PropagateError(err); | 156 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 156 return result; | 157 return result; |
| 157 } | 158 } |
| 158 | 159 |
| 159 static CObjectUint8Array* ToCObject(RawAddr* raw) { | 160 static CObjectUint8Array* ToCObject(const RawAddr& addr) { |
| 160 int in_addr_len = SocketAddress::GetInAddrLength(raw); | 161 int in_addr_len = SocketAddress::GetInAddrLength(addr); |
| 161 void* in_addr; | 162 void* in_addr; |
| 163 RawAddr& raw = const_cast<RawAddr&>(addr); |
| 162 CObjectUint8Array* data = | 164 CObjectUint8Array* data = |
| 163 new CObjectUint8Array(CObject::NewUint8Array(in_addr_len)); | 165 new CObjectUint8Array(CObject::NewUint8Array(in_addr_len)); |
| 164 if (raw->addr.sa_family == AF_INET6) { | 166 if (addr.addr.sa_family == AF_INET6) { |
| 165 in_addr = reinterpret_cast<void*>(&raw->in6.sin6_addr); | 167 in_addr = reinterpret_cast<void*>(&raw.in6.sin6_addr); |
| 166 } else { | 168 } else { |
| 167 in_addr = reinterpret_cast<void*>(&raw->in.sin_addr); | 169 in_addr = reinterpret_cast<void*>(&raw.in.sin_addr); |
| 168 } | 170 } |
| 169 memmove(data->Buffer(), in_addr, in_addr_len); | 171 memmove(data->Buffer(), in_addr, in_addr_len); |
| 170 return data; | 172 return data; |
| 171 } | 173 } |
| 172 | 174 |
| 173 private: | 175 private: |
| 174 char as_string_[INET6_ADDRSTRLEN]; | 176 char as_string_[INET6_ADDRSTRLEN]; |
| 175 RawAddr addr_; | 177 RawAddr addr_; |
| 176 | 178 |
| 177 DISALLOW_COPY_AND_ASSIGN(SocketAddress); | 179 DISALLOW_COPY_AND_ASSIGN(SocketAddress); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 enum SocketRequest { | 235 enum SocketRequest { |
| 234 kLookupRequest = 0, | 236 kLookupRequest = 0, |
| 235 kListInterfacesRequest = 1, | 237 kListInterfacesRequest = 1, |
| 236 kReverseLookupRequest = 2, | 238 kReverseLookupRequest = 2, |
| 237 }; | 239 }; |
| 238 | 240 |
| 239 static bool Initialize(); | 241 static bool Initialize(); |
| 240 static intptr_t Available(intptr_t fd); | 242 static intptr_t Available(intptr_t fd); |
| 241 static intptr_t Read(intptr_t fd, void* buffer, intptr_t num_bytes); | 243 static intptr_t Read(intptr_t fd, void* buffer, intptr_t num_bytes); |
| 242 static intptr_t Write(intptr_t fd, const void* buffer, intptr_t num_bytes); | 244 static intptr_t Write(intptr_t fd, const void* buffer, intptr_t num_bytes); |
| 245 // Send data on a socket. The port to send to is specified in the port |
| 246 // component of the passed RawAddr structure. The RawAddr structure is only |
| 247 // used for datagram sockets. |
| 243 static intptr_t SendTo( | 248 static intptr_t SendTo( |
| 244 intptr_t fd, const void* buffer, intptr_t num_bytes, RawAddr addr); | 249 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr); |
| 245 static intptr_t RecvFrom( | 250 static intptr_t RecvFrom( |
| 246 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr); | 251 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr); |
| 247 static intptr_t CreateConnect(const RawAddr& addr, | 252 // Creates a socket which is bound and connected. The port to connect to is |
| 248 const intptr_t port); | 253 // specified as the port component of the passed RawAddr structure. |
| 254 static intptr_t CreateConnect(const RawAddr& addr); |
| 255 // Creates a socket which is bound and connected. The port to connect to is |
| 256 // specified as the port component of the passed RawAddr structure. |
| 249 static intptr_t CreateBindConnect(const RawAddr& addr, | 257 static intptr_t CreateBindConnect(const RawAddr& addr, |
| 250 const intptr_t port, | |
| 251 const RawAddr& source_addr); | 258 const RawAddr& source_addr); |
| 252 static intptr_t CreateBindDatagram( | 259 // Creates a datagram socket which is bound. The port to bind |
| 253 RawAddr* addr, intptr_t port, bool reuseAddress); | 260 // to is specified as the port component of the RawAddr structure. |
| 261 static intptr_t CreateBindDatagram(const RawAddr& addr, bool reuseAddress); |
| 254 static intptr_t GetPort(intptr_t fd); | 262 static intptr_t GetPort(intptr_t fd); |
| 255 static SocketAddress* GetRemotePeer(intptr_t fd, intptr_t* port); | 263 static SocketAddress* GetRemotePeer(intptr_t fd, intptr_t* port); |
| 256 static void GetError(intptr_t fd, OSError* os_error); | 264 static void GetError(intptr_t fd, OSError* os_error); |
| 257 static int GetType(intptr_t fd); | 265 static int GetType(intptr_t fd); |
| 258 static intptr_t GetStdioHandle(intptr_t num); | 266 static intptr_t GetStdioHandle(intptr_t num); |
| 259 static void Close(intptr_t fd); | 267 static void Close(intptr_t fd); |
| 260 static bool GetNoDelay(intptr_t fd, bool* enabled); | 268 static bool GetNoDelay(intptr_t fd, bool* enabled); |
| 261 static bool SetNoDelay(intptr_t fd, bool enabled); | 269 static bool SetNoDelay(intptr_t fd, bool enabled); |
| 262 static bool GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled); | 270 static bool GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled); |
| 263 static bool SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled); | 271 static bool SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled); |
| 264 static bool GetMulticastHops(intptr_t fd, intptr_t protocol, int* value); | 272 static bool GetMulticastHops(intptr_t fd, intptr_t protocol, int* value); |
| 265 static bool SetMulticastHops(intptr_t fd, intptr_t protocol, int value); | 273 static bool SetMulticastHops(intptr_t fd, intptr_t protocol, int value); |
| 266 static bool GetBroadcast(intptr_t fd, bool* value); | 274 static bool GetBroadcast(intptr_t fd, bool* value); |
| 267 static bool SetBroadcast(intptr_t fd, bool value); | 275 static bool SetBroadcast(intptr_t fd, bool value); |
| 268 static bool JoinMulticast( | 276 static bool JoinMulticast(intptr_t fd, |
| 269 intptr_t fd, RawAddr* addr, RawAddr* interface, int interfaceIndex); | 277 const RawAddr& addr, |
| 270 static bool LeaveMulticast( | 278 const RawAddr& interface, |
| 271 intptr_t fd, RawAddr* addr, RawAddr* interface, int interfaceIndex); | 279 int interfaceIndex); |
| 280 static bool LeaveMulticast(intptr_t fd, |
| 281 const RawAddr& addr, |
| 282 const RawAddr& interface, |
| 283 int interfaceIndex); |
| 272 | 284 |
| 273 // Perform a hostname lookup. Returns a AddressList of SocketAddress's. | 285 // Perform a hostname lookup. Returns a AddressList of SocketAddress's. |
| 274 static AddressList<SocketAddress>* LookupAddress(const char* host, | 286 static AddressList<SocketAddress>* LookupAddress(const char* host, |
| 275 int type, | 287 int type, |
| 276 OSError** os_error); | 288 OSError** os_error); |
| 277 | 289 |
| 278 static bool ReverseLookup(RawAddr addr, | 290 static bool ReverseLookup(const RawAddr& addr, |
| 279 char* host, | 291 char* host, |
| 280 intptr_t host_len, | 292 intptr_t host_len, |
| 281 OSError** os_error); | 293 OSError** os_error); |
| 282 | 294 |
| 283 static bool ParseAddress(int type, const char* address, RawAddr* addr); | 295 static bool ParseAddress(int type, const char* address, RawAddr* addr); |
| 284 static bool FormatNumericAddress(RawAddr* addr, char* address, int len); | 296 static bool FormatNumericAddress(const RawAddr& addr, char* address, int len); |
| 285 | 297 |
| 286 // List interfaces. Returns a AddressList of InterfaceSocketAddress's. | 298 // List interfaces. Returns a AddressList of InterfaceSocketAddress's. |
| 287 static AddressList<InterfaceSocketAddress>* ListInterfaces( | 299 static AddressList<InterfaceSocketAddress>* ListInterfaces( |
| 288 int type, | 300 int type, |
| 289 OSError** os_error); | 301 OSError** os_error); |
| 290 | 302 |
| 291 static CObject* LookupRequest(const CObjectArray& request); | 303 static CObject* LookupRequest(const CObjectArray& request); |
| 292 static CObject* ListInterfacesRequest(const CObjectArray& request); | 304 static CObject* ListInterfacesRequest(const CObjectArray& request); |
| 293 static CObject* ReverseLookupRequest(const CObjectArray& request); | 305 static CObject* ReverseLookupRequest(const CObjectArray& request); |
| 294 | 306 |
| 295 static Dart_Port GetServicePort(); | 307 static Dart_Port GetServicePort(); |
| 296 | 308 |
| 297 static void SetSocketIdNativeField(Dart_Handle socket, intptr_t id); | 309 static void SetSocketIdNativeField(Dart_Handle socket, intptr_t id); |
| 298 static intptr_t GetSocketIdNativeField(Dart_Handle socket); | 310 static intptr_t GetSocketIdNativeField(Dart_Handle socket); |
| 299 | 311 |
| 300 private: | 312 private: |
| 301 DISALLOW_ALLOCATION(); | 313 DISALLOW_ALLOCATION(); |
| 302 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); | 314 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); |
| 303 }; | 315 }; |
| 304 | 316 |
| 305 | 317 |
| 306 class ServerSocket { | 318 class ServerSocket { |
| 307 public: | 319 public: |
| 308 static const intptr_t kTemporaryFailure = -2; | 320 static const intptr_t kTemporaryFailure = -2; |
| 309 | 321 |
| 310 static intptr_t Accept(intptr_t fd); | 322 static intptr_t Accept(intptr_t fd); |
| 311 | 323 |
| 324 // Creates a socket which is bound and listens. The port to listen on is |
| 325 // specified in the port component of the passed RawAddr structure. |
| 326 // |
| 312 // Returns a positive integer if the call is successful. In case of failure | 327 // Returns a positive integer if the call is successful. In case of failure |
| 313 // it returns: | 328 // it returns: |
| 314 // | 329 // |
| 315 // -1: system error (errno set) | 330 // -1: system error (errno set) |
| 316 // -5: invalid bindAddress | 331 // -5: invalid bindAddress |
| 317 static intptr_t CreateBindListen(RawAddr addr, | 332 static intptr_t CreateBindListen(const RawAddr& addr, |
| 318 intptr_t port, | |
| 319 intptr_t backlog, | 333 intptr_t backlog, |
| 320 bool v6_only = false); | 334 bool v6_only = false); |
| 321 | 335 |
| 322 // Start accepting on a newly created listening socket. If it was unable to | 336 // Start accepting on a newly created listening socket. If it was unable to |
| 323 // start accepting incoming sockets, the fd is invalidated. | 337 // start accepting incoming sockets, the fd is invalidated. |
| 324 static bool StartAccept(intptr_t fd); | 338 static bool StartAccept(intptr_t fd); |
| 325 | 339 |
| 326 private: | 340 private: |
| 327 DISALLOW_ALLOCATION(); | 341 DISALLOW_ALLOCATION(); |
| 328 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 342 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 | 374 |
| 361 ~ListeningSocketRegistry() { | 375 ~ListeningSocketRegistry() { |
| 362 delete mutex_; | 376 delete mutex_; |
| 363 mutex_ = NULL; | 377 mutex_ = NULL; |
| 364 } | 378 } |
| 365 | 379 |
| 366 // This function should be called from a dart runtime call in order to create | 380 // This function should be called from a dart runtime call in order to create |
| 367 // a new (potentially shared) socket. | 381 // a new (potentially shared) socket. |
| 368 Dart_Handle CreateBindListen(Dart_Handle socket_object, | 382 Dart_Handle CreateBindListen(Dart_Handle socket_object, |
| 369 RawAddr addr, | 383 RawAddr addr, |
| 370 intptr_t port, | |
| 371 intptr_t backlog, | 384 intptr_t backlog, |
| 372 bool v6_only, | 385 bool v6_only, |
| 373 bool shared); | 386 bool shared); |
| 374 | 387 |
| 375 // This should be called from the event handler for every kCloseEvent it gets | 388 // This should be called from the event handler for every kCloseEvent it gets |
| 376 // on listening sockets. | 389 // on listening sockets. |
| 377 // | 390 // |
| 378 // Returns `true` if the last reference has been dropped and the underlying | 391 // Returns `true` if the last reference has been dropped and the underlying |
| 379 // socket can be closed. | 392 // socket can be closed. |
| 380 // | 393 // |
| (...skipping 29 matching lines...) Expand all Loading... |
| 410 | 423 |
| 411 private: | 424 private: |
| 412 DISALLOW_COPY_AND_ASSIGN(ListeningSocketRegistry); | 425 DISALLOW_COPY_AND_ASSIGN(ListeningSocketRegistry); |
| 413 }; | 426 }; |
| 414 | 427 |
| 415 | 428 |
| 416 } // namespace bin | 429 } // namespace bin |
| 417 } // namespace dart | 430 } // namespace dart |
| 418 | 431 |
| 419 #endif // BIN_SOCKET_H_ | 432 #endif // BIN_SOCKET_H_ |
| OLD | NEW |