| 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 "bin/builtin.h" | 8 #include "bin/builtin.h" |
| 9 #include "bin/utils.h" | 9 #include "bin/utils.h" |
| 10 | 10 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 enum { | 45 enum { |
| 46 ADDRESS_LOOPBACK_IP_V4, | 46 ADDRESS_LOOPBACK_IP_V4, |
| 47 ADDRESS_LOOPBACK_IP_V6, | 47 ADDRESS_LOOPBACK_IP_V6, |
| 48 ADDRESS_ANY_IP_V4, | 48 ADDRESS_ANY_IP_V4, |
| 49 ADDRESS_ANY_IP_V6, | 49 ADDRESS_ANY_IP_V6, |
| 50 ADDRESS_FIRST = ADDRESS_LOOPBACK_IP_V4, | 50 ADDRESS_FIRST = ADDRESS_LOOPBACK_IP_V4, |
| 51 ADDRESS_LAST = ADDRESS_ANY_IP_V6, | 51 ADDRESS_LAST = ADDRESS_ANY_IP_V6, |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 explicit SocketAddress(struct addrinfo* addrinfo); | 54 explicit SocketAddress(struct sockaddr* sockaddr); |
| 55 |
| 56 ~SocketAddress() {} |
| 55 | 57 |
| 56 int GetType() { | 58 int GetType() { |
| 57 if (addr_.ss.ss_family == AF_INET6) return TYPE_IPV6; | 59 if (addr_.ss.ss_family == AF_INET6) return TYPE_IPV6; |
| 58 return TYPE_IPV4; | 60 return TYPE_IPV4; |
| 59 } | 61 } |
| 60 | 62 |
| 61 const char* as_string() const { return as_string_; } | 63 const char* as_string() const { return as_string_; } |
| 62 const RawAddr& addr() const { return addr_; } | 64 const RawAddr& addr() const { return addr_; } |
| 63 | 65 |
| 64 static intptr_t GetAddrLength(const RawAddr& addr) { | 66 static intptr_t GetAddrLength(const RawAddr* addr) { |
| 65 return addr.ss.ss_family == AF_INET6 ? | 67 return addr->ss.ss_family == AF_INET6 ? |
| 66 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); | 68 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); |
| 67 } | 69 } |
| 68 | 70 |
| 69 static int16_t FromType(int type) { | 71 static int16_t FromType(int type) { |
| 70 if (type == TYPE_ANY) return AF_UNSPEC; | 72 if (type == TYPE_ANY) return AF_UNSPEC; |
| 71 if (type == TYPE_IPV4) return AF_INET; | 73 if (type == TYPE_IPV4) return AF_INET; |
| 72 ASSERT(type == TYPE_IPV6 && "Invalid type"); | 74 ASSERT(type == TYPE_IPV6 && "Invalid type"); |
| 73 return AF_INET6; | 75 return AF_INET6; |
| 74 } | 76 } |
| 75 | 77 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 89 } | 91 } |
| 90 } | 92 } |
| 91 | 93 |
| 92 private: | 94 private: |
| 93 char as_string_[INET6_ADDRSTRLEN]; | 95 char as_string_[INET6_ADDRSTRLEN]; |
| 94 RawAddr addr_; | 96 RawAddr addr_; |
| 95 | 97 |
| 96 DISALLOW_COPY_AND_ASSIGN(SocketAddress); | 98 DISALLOW_COPY_AND_ASSIGN(SocketAddress); |
| 97 }; | 99 }; |
| 98 | 100 |
| 99 class SocketAddresses { | 101 class InterfaceSocketAddress { |
| 100 public: | 102 public: |
| 101 explicit SocketAddresses(intptr_t count) | 103 explicit InterfaceSocketAddress(struct sockaddr* sockaddr, |
| 104 const char* interface_name) |
| 105 : socket_address_(new SocketAddress(sockaddr)), |
| 106 interface_name_(interface_name) {} |
| 107 |
| 108 ~InterfaceSocketAddress() { |
| 109 delete socket_address_; |
| 110 free(const_cast<char*>(interface_name_)); |
| 111 } |
| 112 |
| 113 SocketAddress* socket_address() const { return socket_address_; } |
| 114 const char* interface_name() const { return interface_name_; } |
| 115 |
| 116 private: |
| 117 SocketAddress* socket_address_; |
| 118 const char* interface_name_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(InterfaceSocketAddress); |
| 121 }; |
| 122 |
| 123 template<typename T> |
| 124 class AddressList { |
| 125 public: |
| 126 explicit AddressList(intptr_t count) |
| 102 : count_(count), | 127 : count_(count), |
| 103 addresses_(new SocketAddress*[count_]) {} | 128 addresses_(new T*[count_]) {} |
| 104 | 129 |
| 105 ~SocketAddresses() { | 130 ~AddressList() { |
| 106 for (intptr_t i = 0; i < count_; i++) { | 131 for (intptr_t i = 0; i < count_; i++) { |
| 107 delete addresses_[i]; | 132 delete addresses_[i]; |
| 108 } | 133 } |
| 109 delete[] addresses_; | 134 delete[] addresses_; |
| 110 } | 135 } |
| 111 | 136 |
| 112 intptr_t count() const { return count_; } | 137 intptr_t count() const { return count_; } |
| 113 SocketAddress* GetAt(intptr_t i) const { return addresses_[i]; } | 138 T* GetAt(intptr_t i) const { return addresses_[i]; } |
| 114 void SetAt(intptr_t i, SocketAddress* addr) { addresses_[i] = addr; } | 139 void SetAt(intptr_t i, T* addr) { addresses_[i] = addr; } |
| 115 | 140 |
| 116 private: | 141 private: |
| 117 const intptr_t count_; | 142 const intptr_t count_; |
| 118 SocketAddress** addresses_; | 143 T** addresses_; |
| 119 | 144 |
| 120 DISALLOW_COPY_AND_ASSIGN(SocketAddresses); | 145 DISALLOW_COPY_AND_ASSIGN(AddressList); |
| 121 }; | 146 }; |
| 122 | 147 |
| 123 class Socket { | 148 class Socket { |
| 124 public: | 149 public: |
| 125 enum SocketRequest { | 150 enum SocketRequest { |
| 126 kLookupRequest = 0, | 151 kLookupRequest = 0, |
| 152 kListInterfacesRequest = 1, |
| 127 }; | 153 }; |
| 128 | 154 |
| 129 static bool Initialize(); | 155 static bool Initialize(); |
| 130 static intptr_t Available(intptr_t fd); | 156 static intptr_t Available(intptr_t fd); |
| 131 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); | 157 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); |
| 132 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); | 158 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); |
| 133 static intptr_t Create(RawAddr addr); | 159 static intptr_t Create(RawAddr addr); |
| 134 static intptr_t Connect(intptr_t fd, RawAddr addr, const intptr_t port); | 160 static intptr_t Connect(intptr_t fd, RawAddr addr, const intptr_t port); |
| 135 static intptr_t CreateConnect(RawAddr addr, | 161 static intptr_t CreateConnect(RawAddr addr, |
| 136 const intptr_t port); | 162 const intptr_t port); |
| 137 static intptr_t GetPort(intptr_t fd); | 163 static intptr_t GetPort(intptr_t fd); |
| 138 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); | 164 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); |
| 139 static void GetError(intptr_t fd, OSError* os_error); | 165 static void GetError(intptr_t fd, OSError* os_error); |
| 140 static int GetType(intptr_t fd); | 166 static int GetType(intptr_t fd); |
| 141 static intptr_t GetStdioHandle(int num); | 167 static intptr_t GetStdioHandle(int num); |
| 142 static void Close(intptr_t fd); | 168 static void Close(intptr_t fd); |
| 143 static bool SetNonBlocking(intptr_t fd); | 169 static bool SetNonBlocking(intptr_t fd); |
| 144 static bool SetBlocking(intptr_t fd); | 170 static bool SetBlocking(intptr_t fd); |
| 145 static bool SetNoDelay(intptr_t fd, bool enabled); | 171 static bool SetNoDelay(intptr_t fd, bool enabled); |
| 146 | 172 |
| 147 // Perform a hostname lookup. Returns the SocketAddresses. | 173 // Perform a hostname lookup. Returns a AddressList of SocketAddress's. |
| 148 static SocketAddresses* LookupAddress(const char* host, | 174 static AddressList<SocketAddress>* LookupAddress(const char* host, |
| 149 int type, | 175 int type, |
| 150 OSError** os_error); | 176 OSError** os_error); |
| 177 |
| 178 // List interfaces. Returns a AddressList of InterfaceSocketAddress's. |
| 179 static AddressList<InterfaceSocketAddress>* ListInterfaces( |
| 180 int type, |
| 181 OSError** os_error); |
| 151 | 182 |
| 152 static Dart_Port GetServicePort(); | 183 static Dart_Port GetServicePort(); |
| 153 | 184 |
| 154 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); | 185 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); |
| 155 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); | 186 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); |
| 156 | 187 |
| 157 private: | 188 private: |
| 158 static dart::Mutex mutex_; | 189 static dart::Mutex mutex_; |
| 159 static int service_ports_size_; | 190 static int service_ports_size_; |
| 160 static Dart_Port* service_ports_; | 191 static Dart_Port* service_ports_; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 174 // Returns a positive integer if the call is successful. In case of failure | 205 // Returns a positive integer if the call is successful. In case of failure |
| 175 // it returns: | 206 // it returns: |
| 176 // | 207 // |
| 177 // -1: system error (errno set) | 208 // -1: system error (errno set) |
| 178 // -5: invalid bindAddress | 209 // -5: invalid bindAddress |
| 179 static intptr_t CreateBindListen(RawAddr addr, | 210 static intptr_t CreateBindListen(RawAddr addr, |
| 180 intptr_t port, | 211 intptr_t port, |
| 181 intptr_t backlog, | 212 intptr_t backlog, |
| 182 bool v6_only = false); | 213 bool v6_only = false); |
| 183 | 214 |
| 215 private: |
| 184 DISALLOW_ALLOCATION(); | 216 DISALLOW_ALLOCATION(); |
| 185 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 217 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
| 186 }; | 218 }; |
| 187 | 219 |
| 188 } // namespace bin | 220 } // namespace bin |
| 189 } // namespace dart | 221 } // namespace dart |
| 190 | 222 |
| 191 #endif // BIN_SOCKET_H_ | 223 #endif // BIN_SOCKET_H_ |
| OLD | NEW |