6#if defined(DART_HOST_OS_FUCHSIA)
22#if defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR)
24#define LOG_ERR(msg, ...) \
27 Syslog::PrintErr("Dart Socket ERROR: %s:%d: " msg, __FILE__, __LINE__, \
31#if defined(SOCKET_LOG_INFO)
32#define LOG_INFO(msg, ...) \
33 Syslog::Print("Dart Socket INFO: %s:%d: " msg, __FILE__, __LINE__, \
36#define LOG_INFO(msg, ...)
39#define LOG_ERR(msg, ...)
40#define LOG_INFO(msg, ...)
51 udp_receive_buffer_(nullptr) {}
53void Socket::SetClosedFd() {
57void Socket::CloseFd() {
59 IOHandle* handle =
reinterpret_cast<IOHandle*
>(fd_);
66 LOG_INFO(
"Create: calling socket(SOCK_STREAM)\n");
69 LOG_ERR(
"Create: socket(SOCK_STREAM) failed\n");
72 LOG_INFO(
"Create: socket(SOCK_STREAM) -> fd %ld\n", fd);
73 if (!FDUtils::SetCloseOnExec(fd)) {
74 LOG_ERR(
"Create: FDUtils::SetCloseOnExec(%ld) failed\n", fd);
75 FDUtils::SaveErrorAndClose(fd);
78 IOHandle* io_handle =
new IOHandle(fd);
79 return reinterpret_cast<intptr_t
>(io_handle);
82static intptr_t Connect(intptr_t fd,
const RawAddr&
addr) {
83 IOHandle* handle =
reinterpret_cast<IOHandle*
>(fd);
84 LOG_INFO(
"Connect: calling connect(%ld)\n", handle->fd());
86 connect(handle->fd(), &
addr.addr, SocketAddress::GetAddrLength(
addr)));
87 if ((
result == 0) || (errno == EINPROGRESS)) {
88 return reinterpret_cast<intptr_t
>(handle);
90 LOG_ERR(
"Connect: connect(%ld) failed\n", handle->fd());
91 FDUtils::SaveErrorAndClose(handle->fd());
96intptr_t Socket::CreateConnect(
const RawAddr&
addr) {
101 IOHandle* handle =
reinterpret_cast<IOHandle*
>(fd);
102 if (!FDUtils::SetNonBlocking(handle->fd())) {
103 LOG_ERR(
"CreateConnect: FDUtils::SetNonBlocking(%ld) failed\n",
105 FDUtils::SaveErrorAndClose(handle->fd());
109 return Connect(fd,
addr);
112intptr_t Socket::CreateUnixDomainConnect(
const RawAddr&
addr) {
118intptr_t Socket::CreateBindConnect(
const RawAddr&
addr,
119 const RawAddr& source_addr) {
124intptr_t Socket::CreateUnixDomainBindConnect(
const RawAddr&
addr,
125 const RawAddr& source_addr) {
131intptr_t Socket::CreateBindDatagram(
const RawAddr&
addr,
139intptr_t ServerSocket::CreateBindListen(
const RawAddr&
addr,
142 LOG_INFO(
"ServerSocket::CreateBindListen: calling socket(SOCK_STREAM)\n");
145 LOG_ERR(
"ServerSocket::CreateBindListen: socket() failed\n");
148 LOG_INFO(
"ServerSocket::CreateBindListen: socket(SOCK_STREAM) -> %ld\n", fd);
150 if (!FDUtils::SetCloseOnExec(fd)) {
151 LOG_ERR(
"ServerSocket::CreateBindListen: SetCloseOnExec(%ld) failed\n", fd);
152 FDUtils::SaveErrorAndClose(fd);
156 LOG_INFO(
"ServerSocket::CreateBindListen: calling setsockopt(%ld)\n", fd);
159 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)));
161 if (
addr.ss.ss_family == AF_INET6) {
162 optval = v6_only ? 1 : 0;
163 LOG_INFO(
"ServerSocket::CreateBindListen: calling setsockopt(%ld)\n", fd);
165 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval,
sizeof(optval)));
168 LOG_INFO(
"ServerSocket::CreateBindListen: calling bind(%ld)\n", fd);
170 bind(fd, &
addr.addr, SocketAddress::GetAddrLength(
addr))) < 0) {
171 LOG_ERR(
"ServerSocket::CreateBindListen: bind(%ld) failed\n", fd);
172 FDUtils::SaveErrorAndClose(fd);
175 LOG_INFO(
"ServerSocket::CreateBindListen: bind(%ld) succeeded\n", fd);
177 IOHandle* io_handle =
new IOHandle(fd);
180 if ((SocketAddress::GetAddrPort(
addr) == 0) &&
181 (SocketBase::GetPort(
reinterpret_cast<intptr_t
>(io_handle)) == 65535)) {
184 intptr_t new_fd = CreateBindListen(
addr, backlog, v6_only);
185 FDUtils::SaveErrorAndClose(fd);
186 io_handle->Release();
190 LOG_INFO(
"ServerSocket::CreateBindListen: calling listen(%ld)\n", fd);
192 LOG_ERR(
"ServerSocket::CreateBindListen: listen failed(%ld)\n", fd);
193 FDUtils::SaveErrorAndClose(fd);
194 io_handle->Release();
197 LOG_INFO(
"ServerSocket::CreateBindListen: listen(%ld) succeeded\n", fd);
199 if (!FDUtils::SetNonBlocking(fd)) {
200 LOG_ERR(
"CreateBindListen: FDUtils::SetNonBlocking(%ld) failed\n", fd);
201 FDUtils::SaveErrorAndClose(fd);
202 io_handle->Release();
205 return reinterpret_cast<intptr_t
>(io_handle);
208intptr_t ServerSocket::CreateUnixDomainBindListen(
const RawAddr&
addr,
215bool ServerSocket::StartAccept(intptr_t fd) {
220static bool IsTemporaryAcceptError(
int error) {
223 return (
error == EAGAIN) || (
error == ENETDOWN) || (
error == EPROTO) ||
225 (
error == EHOSTUNREACH) || (
error == EOPNOTSUPP) ||
226 (
error == ENETUNREACH);
229intptr_t ServerSocket::Accept(intptr_t fd) {
230 IOHandle* listen_handle =
reinterpret_cast<IOHandle*
>(fd);
232 struct sockaddr clientaddr;
233 socklen_t addrlen =
sizeof(clientaddr);
234 LOG_INFO(
"ServerSocket::Accept: calling accept(%ld)\n", fd);
235 socket = listen_handle->Accept(&clientaddr, &addrlen);
237 if (IsTemporaryAcceptError(errno)) {
241 ASSERT(kTemporaryFailure != -1);
242 socket = kTemporaryFailure;
244 LOG_ERR(
"ServerSocket::Accept: accept(%ld) failed\n", fd);
247 IOHandle* io_handle =
new IOHandle(socket);
248 LOG_INFO(
"ServerSocket::Accept: accept(%ld) -> socket %ld\n", fd, socket);
249 if (!FDUtils::SetCloseOnExec(socket)) {
250 LOG_ERR(
"FDUtils::SetCloseOnExec(%ld) failed\n", socket);
251 FDUtils::SaveErrorAndClose(socket);
252 io_handle->Release();
255 if (!FDUtils::SetNonBlocking(socket)) {
256 LOG_ERR(
"FDUtils::SetNonBlocking(%ld) failed\n", socket);
257 FDUtils::SaveErrorAndClose(socket);
258 io_handle->Release();
261 socket =
reinterpret_cast<intptr_t
>(io_handle);
static sk_sp< Effect > Create()
const uint8_t uint32_t uint32_t GError ** error
DART_EXPORT Dart_Port Dart_GetMainPortId()
#define NO_RETRY_EXPECTED(expression)
#define VOID_NO_RETRY_EXPECTED(expression)