Flutter Engine
The Flutter Engine
socket_base.h
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_BIN_SOCKET_BASE_H_
6#define RUNTIME_BIN_SOCKET_BASE_H_
7
8#include "platform/globals.h"
9// Declare the OS-specific types ahead of defining the generic class.
10#if defined(DART_HOST_OS_FUCHSIA)
12#elif defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
14#elif defined(DART_HOST_OS_MACOS)
16#elif defined(DART_HOST_OS_WINDOWS)
17#include "bin/socket_base_win.h"
18#else
19#error Unknown target os.
20#endif
21
22#include "bin/builtin.h"
23#include "bin/dartutils.h"
24#include "bin/file.h"
25#include "bin/thread.h"
26#include "bin/utils.h"
27#include "platform/allocation.h"
28#include "platform/hashmap.h"
29
30namespace dart {
31namespace bin {
32
33union RawAddr {
34 struct sockaddr_in in;
35 struct sockaddr_in6 in6;
36 struct sockaddr_un un;
37 struct sockaddr_storage ss;
38 struct sockaddr addr;
39};
40
42 public:
43 enum {
48 };
49
50 enum {
57 };
58
59 // Unix domain socket may be unnamed. In this case addr_.un.sun_path contains
60 // garbage and should not be inspected.
61 explicit SocketAddress(struct sockaddr* sa, bool unnamed_unix_socket = false);
62
64
65 int GetType();
66
67 const char* as_string() const { return as_string_; }
68 const RawAddr& addr() const { return addr_; }
69
70 static intptr_t GetAddrLength(const RawAddr& addr,
71 bool unnamed_unix_socket = false);
72 static intptr_t GetInAddrLength(const RawAddr& addr);
73 static bool AreAddressesEqual(const RawAddr& a, const RawAddr& b);
74 static void GetSockAddr(Dart_Handle obj, RawAddr* addr);
75 static Dart_Handle GetUnixDomainSockAddr(const char* path,
76 Namespace* namespc,
77 RawAddr* addr);
78 static int16_t FromType(int type);
79 static void SetAddrPort(RawAddr* addr, intptr_t port);
80 static intptr_t GetAddrPort(const RawAddr& addr);
81 static Dart_Handle ToTypedData(const RawAddr& addr);
83 static void SetAddrScope(RawAddr* addr, intptr_t scope_id);
84 static intptr_t GetAddrScope(const RawAddr& addr);
85
86 private:
87#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) || \
88 defined(DART_HOST_OS_ANDROID)
89 // Unix domain address is only on Linux, Mac OS and Android now.
90 // unix(7) require sun_path to be 108 bytes on Linux and Android, 104 bytes on
91 // Mac OS.
92 static constexpr intptr_t kMaxUnixPathLength =
93 sizeof(((struct sockaddr_un*)nullptr)->sun_path);
94 char as_string_[kMaxUnixPathLength];
95#else
96 char as_string_[INET6_ADDRSTRLEN];
97#endif // defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) || \
98 // defined(DART_HOST_OS_ANDROID)
99 RawAddr addr_;
100
102};
103
105 public:
106 InterfaceSocketAddress(struct sockaddr* sa,
107 const char* interface_name,
108 intptr_t interface_index)
109 : socket_address_(new SocketAddress(sa)),
110 interface_name_(interface_name),
111 interface_index_(interface_index) {}
112
113 ~InterfaceSocketAddress() { delete socket_address_; }
114
115 SocketAddress* socket_address() const { return socket_address_; }
116 const char* interface_name() const { return interface_name_; }
117 int interface_index() const { return interface_index_; }
118
119 private:
120 SocketAddress* socket_address_;
121 const char* interface_name_;
122 intptr_t interface_index_;
123
124 DISALLOW_COPY_AND_ASSIGN(InterfaceSocketAddress);
125};
126
127template <typename T>
129 public:
130 explicit AddressList(intptr_t count)
131 : count_(count), addresses_(new T*[count_]) {}
132
134 for (intptr_t i = 0; i < count_; i++) {
135 delete addresses_[i];
136 }
137 delete[] addresses_;
138 }
139
140 intptr_t count() const { return count_; }
141 T* GetAt(intptr_t i) const { return addresses_[i]; }
142 void SetAt(intptr_t i, T* addr) { addresses_[i] = addr; }
143
144 private:
145 const intptr_t count_;
146 T** addresses_;
147
148 DISALLOW_COPY_AND_ASSIGN(AddressList);
149};
150
152 public:
154 intptr_t type,
155 void* data,
156 size_t data_length)
157 : level_(level), type_(type), data_(data), data_length_(data_length) {}
158
159 intptr_t level() const { return level_; }
160 intptr_t type() const { return type_; }
161 void* data() const { return data_; }
162 size_t data_length() const { return data_length_; }
163
165
166 private:
167 const intptr_t level_;
168 const intptr_t type_;
169 void* data_;
170 const size_t data_length_;
171
172 DISALLOW_COPY_AND_ASSIGN(SocketControlMessage);
173};
174
175class SocketBase : public AllStatic {
176 public:
181 };
182
186 };
187
188 // TODO(dart:io): Convert these to instance methods where possible.
189 static bool Initialize();
190 static intptr_t Available(intptr_t fd);
191 static intptr_t Read(intptr_t fd,
192 void* buffer,
193 intptr_t num_bytes,
194 SocketOpKind sync);
195 static intptr_t Write(intptr_t fd,
196 const void* buffer,
197 intptr_t num_bytes,
198 SocketOpKind sync);
199
200 // Send data on a socket. The port to send to is specified in the port
201 // component of the passed RawAddr structure. The RawAddr structure is only
202 // used for datagram sockets.
203 static intptr_t SendTo(intptr_t fd,
204 const void* buffer,
205 intptr_t num_bytes,
206 const RawAddr& addr,
207 SocketOpKind sync);
208 static intptr_t SendMessage(intptr_t fd,
209 void* buffer,
210 size_t buffer_num_bytes,
211 SocketControlMessage* messages,
212 intptr_t num_messages,
213 SocketOpKind sync,
214 OSError* p_oserror);
215 static intptr_t RecvFrom(intptr_t fd,
216 void* buffer,
217 intptr_t num_bytes,
218 RawAddr* addr,
219 SocketOpKind sync);
220 static intptr_t ReceiveMessage(intptr_t fd,
221 void* buffer,
222 int64_t* p_buffer_num_bytes,
223 SocketControlMessage** p_messages,
224 SocketOpKind sync,
225 OSError* p_oserror);
226 static bool AvailableDatagram(intptr_t fd, void* buffer, intptr_t num_bytes);
227 // Returns true if the given error-number is because the system was not able
228 // to bind the socket to a specific IP.
229 static bool IsBindError(intptr_t error_number);
230 static intptr_t GetPort(intptr_t fd);
231 static bool GetSocketName(intptr_t fd, SocketAddress* p_sa);
232 static SocketAddress* GetRemotePeer(intptr_t fd, intptr_t* port);
233 static void GetError(intptr_t fd, OSError* os_error);
234 static int GetType(intptr_t fd);
235 static intptr_t GetStdioHandle(intptr_t num);
236 static void Close(intptr_t fd);
237 static bool GetNoDelay(intptr_t fd, bool* enabled);
238 static bool SetNoDelay(intptr_t fd, bool enabled);
239 static bool GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled);
240 static bool SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled);
241 static bool GetMulticastHops(intptr_t fd, intptr_t protocol, int* value);
242 static bool SetMulticastHops(intptr_t fd, intptr_t protocol, int value);
243 static bool GetBroadcast(intptr_t fd, bool* value);
244 static bool SetBroadcast(intptr_t fd, bool value);
245 static bool GetOption(intptr_t fd,
246 int level,
247 int option,
248 char* data,
249 unsigned int* length);
250 static bool SetOption(intptr_t fd,
251 int level,
252 int option,
253 const char* data,
254 int length);
255 static bool JoinMulticast(intptr_t fd,
256 const RawAddr& addr,
257 const RawAddr& interface,
258 int interfaceIndex);
259 static bool LeaveMulticast(intptr_t fd,
260 const RawAddr& addr,
261 const RawAddr& interface,
262 int interfaceIndex);
263
264#if defined(DART_HOST_OS_WINDOWS)
265 static bool HasPendingWrite(intptr_t fd);
266#endif
267
268 // Perform a hostname lookup. Returns a AddressList of SocketAddress's.
270 int type,
271 OSError** os_error);
272
273 static bool ReverseLookup(const RawAddr& addr,
274 char* host,
275 intptr_t host_len,
276 OSError** os_error);
277
278 static bool ParseAddress(int type, const char* address, RawAddr* addr);
279
280 static bool IsValidAddress(const char* address);
281
282 // Convert address from byte representation to human readable string.
283 static bool RawAddrToString(RawAddr* addr, char* str);
284 static bool FormatNumericAddress(const RawAddr& addr, char* address, int len);
285
286 // List interfaces. Returns a AddressList of InterfaceSocketAddress's.
288 int type,
289 OSError** os_error);
290
291 private:
292#if !defined(DART_HOST_OS_WINDOWS)
293 static intptr_t WriteImpl(intptr_t fd,
294 const void* buffer,
295 intptr_t num_bytes,
296 SocketOpKind sync);
297#endif
298
299 DISALLOW_ALLOCATION();
300 DISALLOW_IMPLICIT_CONSTRUCTORS(SocketBase);
301};
302
303} // namespace bin
304} // namespace dart
305
306#endif // RUNTIME_BIN_SOCKET_BASE_H_
GLenum type
intptr_t count() const
Definition: socket_base.h:140
AddressList(intptr_t count)
Definition: socket_base.h:130
void SetAt(intptr_t i, T *addr)
Definition: socket_base.h:142
T * GetAt(intptr_t i) const
Definition: socket_base.h:141
SocketAddress * socket_address() const
Definition: socket_base.h:115
const char * interface_name() const
Definition: socket_base.h:116
InterfaceSocketAddress(struct sockaddr *sa, const char *interface_name, intptr_t interface_index)
Definition: socket_base.h:106
static void SetAddrScope(RawAddr *addr, intptr_t scope_id)
Definition: socket_base.cc:232
static void GetSockAddr(Dart_Handle obj, RawAddr *addr)
Definition: socket_base.cc:105
static int16_t FromType(int type)
Definition: socket_base.cc:163
static intptr_t GetAddrScope(const RawAddr &addr)
Definition: socket_base.cc:237
static void SetAddrPort(RawAddr *addr, intptr_t port)
Definition: socket_base.cc:177
const char * as_string() const
Definition: socket_base.h:67
static CObjectUint8Array * ToCObject(const RawAddr &addr)
Definition: socket_base.cc:220
static bool AreAddressesEqual(const RawAddr &a, const RawAddr &b)
Definition: socket_base.cc:80
static intptr_t GetAddrLength(const RawAddr &addr, bool unnamed_unix_socket=false)
Definition: socket_base.cc:39
static Dart_Handle GetUnixDomainSockAddr(const char *path, Namespace *namespc, RawAddr *addr)
Definition: socket_base.cc:130
static Dart_Handle ToTypedData(const RawAddr &addr)
Definition: socket_base.cc:200
SocketAddress(struct sockaddr *sa, bool unnamed_unix_socket=false)
static intptr_t GetAddrPort(const RawAddr &addr)
Definition: socket_base.cc:187
static intptr_t GetInAddrLength(const RawAddr &addr)
Definition: socket_base.cc:74
const RawAddr & addr() const
Definition: socket_base.h:68
static bool LeaveMulticast(intptr_t fd, const RawAddr &addr, const RawAddr &interface, int interfaceIndex)
static bool AvailableDatagram(intptr_t fd, void *buffer, intptr_t num_bytes)
static int GetType(intptr_t fd)
static AddressList< SocketAddress > * LookupAddress(const char *host, int type, OSError **os_error)
static bool SetMulticastHops(intptr_t fd, intptr_t protocol, int value)
static intptr_t GetStdioHandle(intptr_t num)
static intptr_t ReceiveMessage(intptr_t fd, void *buffer, int64_t *p_buffer_num_bytes, SocketControlMessage **p_messages, SocketOpKind sync, OSError *p_oserror)
static bool SetOption(intptr_t fd, int level, int option, const char *data, int length)
static SocketAddress * GetRemotePeer(intptr_t fd, intptr_t *port)
static bool FormatNumericAddress(const RawAddr &addr, char *address, int len)
static bool SetNoDelay(intptr_t fd, bool enabled)
static bool GetSocketName(intptr_t fd, SocketAddress *p_sa)
static bool ParseAddress(int type, const char *address, RawAddr *addr)
static intptr_t Available(intptr_t fd)
static void Close(intptr_t fd)
static void GetError(intptr_t fd, OSError *os_error)
static intptr_t RecvFrom(intptr_t fd, void *buffer, intptr_t num_bytes, RawAddr *addr, SocketOpKind sync)
static bool IsBindError(intptr_t error_number)
static intptr_t Read(intptr_t fd, void *buffer, intptr_t num_bytes, SocketOpKind sync)
static bool SetBroadcast(intptr_t fd, bool value)
static bool GetNoDelay(intptr_t fd, bool *enabled)
static bool SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled)
static bool IsValidAddress(const char *address)
Definition: socket_base.cc:306
static bool GetOption(intptr_t fd, int level, int option, char *data, unsigned int *length)
static bool ReverseLookup(const RawAddr &addr, char *host, intptr_t host_len, OSError **os_error)
static bool Initialize()
static bool RawAddrToString(RawAddr *addr, char *str)
static intptr_t SendTo(intptr_t fd, const void *buffer, intptr_t num_bytes, const RawAddr &addr, SocketOpKind sync)
static bool GetMulticastHops(intptr_t fd, intptr_t protocol, int *value)
static bool GetMulticastLoop(intptr_t fd, intptr_t protocol, bool *enabled)
static bool JoinMulticast(intptr_t fd, const RawAddr &addr, const RawAddr &interface, int interfaceIndex)
static intptr_t SendMessage(intptr_t fd, void *buffer, size_t buffer_num_bytes, SocketControlMessage *messages, intptr_t num_messages, SocketOpKind sync, OSError *p_oserror)
static AddressList< InterfaceSocketAddress > * ListInterfaces(int type, OSError **os_error)
static bool GetBroadcast(intptr_t fd, bool *value)
static intptr_t Write(intptr_t fd, const void *buffer, intptr_t num_bytes, SocketOpKind sync)
Definition: socket_base.cc:321
static intptr_t GetPort(intptr_t fd)
SocketControlMessage(intptr_t level, intptr_t type, void *data, size_t data_length)
Definition: socket_base.h:153
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
static bool b
struct MyStruct a[10]
uint8_t value
size_t length
Definition: dart_vm.cc:33
static int8_t data[kExtLength]
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service port
Definition: switches.h:87
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service host
Definition: switches.h:74
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:581
#define T
Definition: precompiler.cc:65
struct sockaddr_storage ss
Definition: socket_base.h:37
struct sockaddr addr
Definition: socket_base.h:38
struct sockaddr_un un
Definition: socket_base.h:36
struct sockaddr_in in
Definition: socket_base.h:34
struct sockaddr_in6 in6
Definition: socket_base.h:35