/* * Copyright (c) 2018-2021, Andreas Kling * Copyright (c) 2021, sin-ack * * SPDX-License-Identifier: BSD-2-Clause */ #include #include namespace Core { ErrorOr Socket::create_fd(SocketDomain domain, SocketType type) { int socket_domain; switch (domain) { case SocketDomain::Inet: socket_domain = AF_INET; break; case SocketDomain::Local: socket_domain = AF_LOCAL; break; default: VERIFY_NOT_REACHED(); } int socket_type; switch (type) { case SocketType::Stream: socket_type = SOCK_STREAM; break; case SocketType::Datagram: socket_type = SOCK_DGRAM; break; default: VERIFY_NOT_REACHED(); } // Let's have a safe default of CLOEXEC. :^) #ifdef SOCK_CLOEXEC return System::socket(socket_domain, socket_type | SOCK_CLOEXEC, 0); #else auto fd = TRY(System::socket(socket_domain, socket_type, 0)); TRY(System::fcntl(fd, F_SETFD, FD_CLOEXEC)); return fd; #endif } ErrorOr Socket::resolve_host(DeprecatedString const& host, SocketType type) { int socket_type; switch (type) { case SocketType::Stream: socket_type = SOCK_STREAM; break; case SocketType::Datagram: socket_type = SOCK_DGRAM; break; default: VERIFY_NOT_REACHED(); } struct addrinfo hints = {}; hints.ai_family = AF_UNSPEC; hints.ai_socktype = socket_type; hints.ai_flags = 0; hints.ai_protocol = 0; auto const results = TRY(Core::System::getaddrinfo(host.characters(), nullptr, hints)); for (auto const& result : results.addresses()) { if (result.ai_family == AF_INET) { auto* socket_address = bit_cast(result.ai_addr); NetworkOrdered const network_ordered_address { socket_address->sin_addr.s_addr }; return IPv4Address { network_ordered_address }; } } return Error::from_string_literal("Could not resolve to IPv4 address"); } ErrorOr Socket::connect_local(int fd, DeprecatedString const& path) { auto address = SocketAddress::local(path); auto maybe_sockaddr = address.to_sockaddr_un(); if (!maybe_sockaddr.has_value()) { dbgln("Core::Socket::connect_local: Could not obtain a sockaddr_un"); return Error::from_errno(EINVAL); } auto addr = maybe_sockaddr.release_value(); return System::connect(fd, bit_cast(&addr), sizeof(addr)); } ErrorOr Socket::connect_inet(int fd, SocketAddress const& address) { auto addr = address.to_sockaddr_in(); return System::connect(fd, bit_cast(&addr), sizeof(addr)); } ErrorOr PosixSocketHelper::read(Bytes buffer, int flags) { if (!is_open()) { return Error::from_errno(ENOTCONN); } ssize_t nread = TRY(System::recv(m_fd, buffer.data(), buffer.size(), flags)); m_last_read_was_eof = nread == 0; // If a socket read is EOF, then no more data can be read from it because // the protocol has disconnected. In this case, we can just disable the // notifier if we have one. if (m_last_read_was_eof && m_notifier) m_notifier->set_enabled(false); return buffer.trim(nread); } ErrorOr PosixSocketHelper::write(ReadonlyBytes buffer, int flags) { if (!is_open()) { return Error::from_errno(ENOTCONN); } return TRY(System::send(m_fd, buffer.data(), buffer.size(), flags)); } void PosixSocketHelper::close() { if (!is_open()) { return; } if (m_notifier) m_notifier->set_enabled(false); ErrorOr result; do { result = System::close(m_fd); } while (result.is_error() && result.error().code() == EINTR); VERIFY(!result.is_error()); m_fd = -1; } ErrorOr PosixSocketHelper::can_read_without_blocking(int timeout) const { struct pollfd the_fd = { .fd = m_fd, .events = POLLIN, .revents = 0 }; ErrorOr result { 0 }; do { result = Core::System::poll({ &the_fd, 1 }, timeout); } while (result.is_error() && result.error().code() == EINTR); if (result.is_error()) return result.release_error(); return (the_fd.revents & POLLIN) > 0; } ErrorOr PosixSocketHelper::set_blocking(bool enabled) { int value = enabled ? 0 : 1; return System::ioctl(m_fd, FIONBIO, &value); } ErrorOr PosixSocketHelper::set_close_on_exec(bool enabled) { int flags = TRY(System::fcntl(m_fd, F_GETFD)); if (enabled) flags |= FD_CLOEXEC; else flags &= ~FD_CLOEXEC; TRY(System::fcntl(m_fd, F_SETFD, flags)); return {}; } ErrorOr PosixSocketHelper::set_receive_timeout(Time timeout) { auto timeout_spec = timeout.to_timespec(); return System::setsockopt(m_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout_spec, sizeof(timeout_spec)); } void PosixSocketHelper::setup_notifier() { if (!m_notifier) m_notifier = Core::Notifier::construct(m_fd, Core::Notifier::Read); } ErrorOr> TCPSocket::connect(DeprecatedString const& host, u16 port) { auto ip_address = TRY(resolve_host(host, SocketType::Stream)); return connect(SocketAddress { ip_address, port }); } ErrorOr> TCPSocket::connect(SocketAddress const& address) { auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TCPSocket())); auto fd = TRY(create_fd(SocketDomain::Inet, SocketType::Stream)); socket->m_helper.set_fd(fd); TRY(connect_inet(fd, address)); socket->setup_notifier(); return socket; } ErrorOr> TCPSocket::adopt_fd(int fd) { if (fd < 0) { return Error::from_errno(EBADF); } auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TCPSocket())); socket->m_helper.set_fd(fd); socket->setup_notifier(); return socket; } ErrorOr PosixSocketHelper::pending_bytes() const { if (!is_open()) { return Error::from_errno(ENOTCONN); } int value; TRY(System::ioctl(m_fd, FIONREAD, &value)); return static_cast(value); } ErrorOr> UDPSocket::connect(DeprecatedString const& host, u16 port, Optional