Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
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
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
173};
174
175class SocketBase : public AllStatic {
176 public:
182
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();
301};
302
303} // namespace bin
304} // namespace dart
305
306#endif // RUNTIME_BIN_SOCKET_BASE_H_
intptr_t count() const
AddressList(intptr_t count)
void SetAt(intptr_t i, T *addr)
T * GetAt(intptr_t i) const
SocketAddress * socket_address() const
const char * interface_name() const
InterfaceSocketAddress(struct sockaddr *sa, const char *interface_name, intptr_t interface_index)
static void SetAddrScope(RawAddr *addr, intptr_t scope_id)
static void GetSockAddr(Dart_Handle obj, RawAddr *addr)
static int16_t FromType(int type)
static intptr_t GetAddrScope(const RawAddr &addr)
static void SetAddrPort(RawAddr *addr, intptr_t port)
const char * as_string() const
Definition socket_base.h:67
static CObjectUint8Array * ToCObject(const RawAddr &addr)
static bool AreAddressesEqual(const RawAddr &a, const RawAddr &b)
static intptr_t GetAddrLength(const RawAddr &addr, bool unnamed_unix_socket=false)
static Dart_Handle GetUnixDomainSockAddr(const char *path, Namespace *namespc, RawAddr *addr)
static Dart_Handle ToTypedData(const RawAddr &addr)
SocketAddress(struct sockaddr *sa, bool unnamed_unix_socket=false)
static intptr_t GetAddrPort(const RawAddr &addr)
static intptr_t GetInAddrLength(const RawAddr &addr)
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)
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)
static intptr_t GetPort(intptr_t fd)
SocketControlMessage(intptr_t level, intptr_t type, void *data, size_t data_length)
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
static bool b
struct MyStruct a[10]
static const uint8_t buffer[]
size_t length
static int8_t data[kExtLength]
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)
Definition globals.h:593
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581
#define T
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