Flutter Engine
The Flutter Engine
Public Types | Public Member Functions | Static Public Member Functions | List of all members
dart::bin::SocketAddress Class Reference

#include <socket_base.h>

Public Types

enum  { TYPE_ANY = -1 , TYPE_IPV4 , TYPE_IPV6 , TYPE_UNIX }
 
enum  {
  ADDRESS_LOOPBACK_IP_V4 , ADDRESS_LOOPBACK_IP_V6 , ADDRESS_ANY_IP_V4 , ADDRESS_ANY_IP_V6 ,
  ADDRESS_FIRST = ADDRESS_LOOPBACK_IP_V4 , ADDRESS_LAST = ADDRESS_ANY_IP_V6
}
 

Public Member Functions

 SocketAddress (struct sockaddr *sa, bool unnamed_unix_socket=false)
 
 ~SocketAddress ()
 
int GetType ()
 
const char * as_string () const
 
const RawAddraddr () const
 

Static Public Member Functions

static intptr_t GetAddrLength (const RawAddr &addr, bool unnamed_unix_socket=false)
 
static intptr_t GetInAddrLength (const RawAddr &addr)
 
static bool AreAddressesEqual (const RawAddr &a, const RawAddr &b)
 
static void GetSockAddr (Dart_Handle obj, RawAddr *addr)
 
static Dart_Handle GetUnixDomainSockAddr (const char *path, Namespace *namespc, RawAddr *addr)
 
static int16_t FromType (int type)
 
static void SetAddrPort (RawAddr *addr, intptr_t port)
 
static intptr_t GetAddrPort (const RawAddr &addr)
 
static Dart_Handle ToTypedData (const RawAddr &addr)
 
static CObjectUint8ArrayToCObject (const RawAddr &addr)
 
static void SetAddrScope (RawAddr *addr, intptr_t scope_id)
 
static intptr_t GetAddrScope (const RawAddr &addr)
 

Detailed Description

Definition at line 41 of file socket_base.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
TYPE_ANY 
TYPE_IPV4 
TYPE_IPV6 
TYPE_UNIX 

Definition at line 43 of file socket_base.h.

◆ anonymous enum

anonymous enum
Enumerator
ADDRESS_LOOPBACK_IP_V4 
ADDRESS_LOOPBACK_IP_V6 
ADDRESS_ANY_IP_V4 
ADDRESS_ANY_IP_V6 
ADDRESS_FIRST 
ADDRESS_LAST 

Definition at line 50 of file socket_base.h.

Constructor & Destructor Documentation

◆ SocketAddress()

dart::bin::SocketAddress::SocketAddress ( struct sockaddr *  sa,
bool  unnamed_unix_socket = false 
)
explicit

◆ ~SocketAddress()

dart::bin::SocketAddress::~SocketAddress ( )
inline

Definition at line 63 of file socket_base.h.

63{}

Member Function Documentation

◆ addr()

const RawAddr & dart::bin::SocketAddress::addr ( ) const
inline

Definition at line 68 of file socket_base.h.

68{ return addr_; }

◆ AreAddressesEqual()

bool dart::bin::SocketAddress::AreAddressesEqual ( const RawAddr a,
const RawAddr b 
)
static

Definition at line 80 of file socket_base.cc.

80 {
81 if (a.ss.ss_family != b.ss.ss_family) {
82 return false;
83 }
84 if (a.ss.ss_family == AF_INET) {
85 return memcmp(&a.in.sin_addr, &b.in.sin_addr, sizeof(a.in.sin_addr)) == 0;
86 } else if (a.ss.ss_family == AF_INET6) {
87 return memcmp(&a.in6.sin6_addr, &b.in6.sin6_addr,
88 sizeof(a.in6.sin6_addr)) == 0 &&
89 a.in6.sin6_scope_id == b.in6.sin6_scope_id;
90 } else if (a.ss.ss_family == AF_UNIX) {
91 // This is not used anywhere. The comparison of file path is done via
92 // File::AreIdentical().
93 int len = sizeof(a.un.sun_path);
94 for (int i = 0; i < len; i++) {
95 if (a.un.sun_path[i] != b.un.sun_path[i]) return false;
96 if (a.un.sun_path[i] == '\0') return true;
97 }
98 return true;
99 } else {
100 UNREACHABLE();
101 return false;
102 }
103}
#define UNREACHABLE()
Definition: assert.h:248
static bool b
struct MyStruct a[10]

◆ as_string()

const char * dart::bin::SocketAddress::as_string ( ) const
inline

Definition at line 67 of file socket_base.h.

67{ return as_string_; }

◆ FromType()

int16_t dart::bin::SocketAddress::FromType ( int  type)
static

Definition at line 163 of file socket_base.cc.

163 {
164 if (type == TYPE_ANY) {
165 return AF_UNSPEC;
166 }
167 if (type == TYPE_IPV4) {
168 return AF_INET;
169 }
170 if (type == TYPE_UNIX) {
171 return AF_UNIX;
172 }
173 ASSERT((type == TYPE_IPV6) && "Invalid type");
174 return AF_INET6;
175}
GLenum type
#define ASSERT(E)

◆ GetAddrLength()

intptr_t dart::bin::SocketAddress::GetAddrLength ( const RawAddr addr,
bool  unnamed_unix_socket = false 
)
static

Definition at line 39 of file socket_base.cc.

40 {
41 ASSERT((addr.ss.ss_family == AF_INET) || (addr.ss.ss_family == AF_INET6) ||
42 (addr.ss.ss_family == AF_UNIX));
43 switch (addr.ss.ss_family) {
44 case AF_INET6:
45 return sizeof(struct sockaddr_in6);
46 case AF_INET:
47 return sizeof(struct sockaddr_in);
48 case AF_UNIX: {
49 // For an abstract UNIX socket, trailing null bytes in the name are
50 // meaningful. That is, the bytes '\0/tmp/dbus-xxxx' are a different name
51 // than '\0/tmp/dbus-xxxx\0\0\0...'. The length of the address structure
52 // passed to connect() etc. tells those calls how many bytes of the name
53 // to look at. Therefore, when computing the length of the address in
54 // this case, any trailing null bytes are trimmed.
55 // TODO(dart:io): Support abstract UNIX socket addresses that have
56 // trailing null bytes on purpose.
57 // https://github.com/dart-lang/sdk/issues/46158
58 intptr_t nulls = 0;
59 if (!unnamed_unix_socket && addr.un.sun_path[0] == '\0') {
60 intptr_t i = sizeof(addr.un.sun_path) - 1;
61 while (addr.un.sun_path[i] == '\0') {
62 nulls++;
63 i--;
64 }
65 }
66 return sizeof(struct sockaddr_un) - nulls;
67 }
68 default:
70 return 0;
71 }
72}
const RawAddr & addr() const
Definition: socket_base.h:68
struct sockaddr_storage ss
Definition: socket_base.h:37
struct sockaddr_un un
Definition: socket_base.h:36

◆ GetAddrPort()

intptr_t dart::bin::SocketAddress::GetAddrPort ( const RawAddr addr)
static

Definition at line 187 of file socket_base.cc.

187 {
188 if (addr.ss.ss_family == AF_INET) {
189 return ntohs(addr.in.sin_port);
190 } else if (addr.ss.ss_family == AF_INET6) {
191 return ntohs(addr.in6.sin6_port);
192 } else if (addr.ss.ss_family == AF_UNIX) {
193 return 0;
194 } else {
195 UNREACHABLE();
196 return -1;
197 }
198}
struct sockaddr_in in
Definition: socket_base.h:34
struct sockaddr_in6 in6
Definition: socket_base.h:35

◆ GetAddrScope()

intptr_t dart::bin::SocketAddress::GetAddrScope ( const RawAddr addr)
static

Definition at line 237 of file socket_base.cc.

237 {
238 if (addr.addr.sa_family == AF_INET6) {
239 return addr.in6.sin6_scope_id;
240 } else {
241 return 0;
242 }
243}
struct sockaddr addr
Definition: socket_base.h:38

◆ GetInAddrLength()

intptr_t dart::bin::SocketAddress::GetInAddrLength ( const RawAddr addr)
static

Definition at line 74 of file socket_base.cc.

74 {
75 ASSERT((addr.ss.ss_family == AF_INET) || (addr.ss.ss_family == AF_INET6));
76 return (addr.ss.ss_family == AF_INET6) ? sizeof(struct in6_addr)
77 : sizeof(struct in_addr);
78}

◆ GetSockAddr()

void dart::bin::SocketAddress::GetSockAddr ( Dart_Handle  obj,
RawAddr addr 
)
static

Definition at line 105 of file socket_base.cc.

105 {
106 Dart_TypedData_Type data_type;
107 uint8_t* data = nullptr;
108 intptr_t len;
110 obj, &data_type, reinterpret_cast<void**>(&data), &len);
111 if (Dart_IsError(result)) {
113 }
114 if ((data_type != Dart_TypedData_kUint8) ||
115 ((len != sizeof(in_addr)) && (len != sizeof(in6_addr)))) {
116 Dart_PropagateError(Dart_NewApiError("Unexpected type for socket address"));
117 }
118 memset(reinterpret_cast<void*>(addr), 0, sizeof(RawAddr));
119 if (len == sizeof(in_addr)) {
120 addr->in.sin_family = AF_INET;
121 memmove(reinterpret_cast<void*>(&addr->in.sin_addr), data, len);
122 } else {
123 ASSERT(len == sizeof(in6_addr));
124 addr->in6.sin6_family = AF_INET6;
125 memmove(reinterpret_cast<void*>(&addr->in6.sin6_addr), data, len);
126 }
128}
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
Dart_TypedData_Type
Definition: dart_api.h:2612
@ Dart_TypedData_kUint8
Definition: dart_api.h:2615
GAsyncResult * result
DART_EXPORT void Dart_PropagateError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object, Dart_TypedData_Type *type, void **data, intptr_t *len)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_NewApiError(const char *error)
static int8_t data[kExtLength]

◆ GetType()

int dart::bin::SocketAddress::GetType ( )

Definition at line 25 of file socket_base.cc.

25 {
26 switch (addr_.ss.ss_family) {
27 case AF_INET6:
28 return TYPE_IPV6;
29 case AF_INET:
30 return TYPE_IPV4;
31 case AF_UNIX:
32 return TYPE_UNIX;
33 default:
35 return TYPE_ANY;
36 }
37}

◆ GetUnixDomainSockAddr()

Dart_Handle dart::bin::SocketAddress::GetUnixDomainSockAddr ( const char *  path,
Namespace namespc,
RawAddr addr 
)
static

Definition at line 130 of file socket_base.cc.

132 {
133#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
134 NamespaceScope ns(namespc, path);
135 path = ns.path();
136 bool is_abstract = (path[0] == '@');
137 if (is_abstract) {
138 // The following 107 bytes after the leading null byte represents the name
139 // of unix domain socket. Without reseting, even users provide the same path
140 // for bind and connect, they actually represent two different address and
141 // connection will be rejected.
142 bzero(addr->un.sun_path, sizeof(addr->un.sun_path));
143 }
144#endif // defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
145 if (sizeof(path) > sizeof(addr->un.sun_path)) {
146 OSError os_error(-1,
147 "The length of path exceeds the limit. "
148 "Check out man 7 unix page",
150 return DartUtils::NewDartOSError(&os_error);
151 }
152 addr->un.sun_family = AF_UNIX;
153 Utils::SNPrint(addr->un.sun_path, sizeof(addr->un.sun_path), "%s", path);
154#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
155 // In case of abstract namespace, transfer the leading '@' into a null byte.
156 if (is_abstract) {
157 addr->un.sun_path[0] = '\0';
158 }
159#endif // defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
160 return Dart_Null();
161}
static int SNPrint(char *str, size_t size, const char *format,...) PRINTF_ATTRIBUTE(3
static Dart_Handle NewDartOSError()
Definition: dartutils.cc:702
DART_EXPORT Dart_Handle Dart_Null()
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

◆ SetAddrPort()

void dart::bin::SocketAddress::SetAddrPort ( RawAddr addr,
intptr_t  port 
)
static

Definition at line 177 of file socket_base.cc.

177 {
178 if (addr->ss.ss_family == AF_INET) {
179 addr->in.sin_port = htons(port);
180 } else if (addr->ss.ss_family == AF_INET6) {
181 addr->in6.sin6_port = htons(port);
182 } else {
183 UNREACHABLE();
184 }
185}
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

◆ SetAddrScope()

void dart::bin::SocketAddress::SetAddrScope ( RawAddr addr,
intptr_t  scope_id 
)
static

Definition at line 232 of file socket_base.cc.

232 {
233 if (addr->addr.sa_family != AF_INET6) return;
234 addr->in6.sin6_scope_id = scope_id;
235}

◆ ToCObject()

CObjectUint8Array * dart::bin::SocketAddress::ToCObject ( const RawAddr addr)
static

Definition at line 220 of file socket_base.cc.

220 {
221 int in_addr_len = SocketAddress::GetInAddrLength(addr);
222 const void* in_addr;
223 if (addr.addr.sa_family == AF_INET6) {
224 in_addr = reinterpret_cast<const void*>(&addr.in6.sin6_addr);
225 } else {
226 in_addr = reinterpret_cast<const void*>(&addr.in.sin_addr);
227 }
228 CObjectUint8Array* data =
229 new CObjectUint8Array(CObject::NewUint8Array(in_addr, in_addr_len));
230 return data;
231}
static Dart_CObject * NewUint8Array(const void *data, intptr_t length)
Definition: dartutils.cc:945
static intptr_t GetInAddrLength(const RawAddr &addr)
Definition: socket_base.cc:74

◆ ToTypedData()

Dart_Handle dart::bin::SocketAddress::ToTypedData ( const RawAddr addr)
static

Definition at line 200 of file socket_base.cc.

200 {
201 int len = GetInAddrLength(addr);
203 if (Dart_IsError(result)) {
205 }
206 Dart_Handle err;
207 if (addr.addr.sa_family == AF_INET6) {
209 result, 0, reinterpret_cast<const uint8_t*>(&addr.in6.sin6_addr), len);
210 } else {
212 result, 0, reinterpret_cast<const uint8_t*>(&addr.in.sin_addr), len);
213 }
214 if (Dart_IsError(err)) {
216 }
217 return result;
218}
DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type, intptr_t length)
DART_EXPORT Dart_Handle Dart_ListSetAsBytes(Dart_Handle list, intptr_t offset, const uint8_t *native_array, intptr_t length)

The documentation for this class was generated from the following files: