Kernel/LibC: Implement getsockname for ipv4 sockets

This commit is contained in:
2024-06-17 20:54:45 +03:00
parent 511fc870a1
commit cad55a4da5
11 changed files with 64 additions and 1 deletions

View File

@@ -164,6 +164,14 @@ namespace Kernel
return recvfrom_impl(buffer, address, address_len);
};
BAN::ErrorOr<void> Inode::getsockname(sockaddr* address, socklen_t* address_len)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return getsockname_impl(address, address_len);
}
BAN::ErrorOr<size_t> Inode::read(off_t offset, BAN::ByteSpan buffer)
{
LockGuard _(m_mutex);

View File

@@ -139,6 +139,28 @@ namespace Kernel
return {};
}
BAN::ErrorOr<void> IPv4Layer::get_socket_address(BAN::RefPtr<NetworkSocket> socket, sockaddr* address, socklen_t* address_len)
{
if (*address_len < (socklen_t)sizeof(sockaddr_in))
return BAN::Error::from_errno(ENOBUFS);
sockaddr_in* in_addr = reinterpret_cast<sockaddr_in*>(address);
SpinLockGuard _(m_bound_socket_lock);
for (auto& [bound_port, bound_socket] : m_bound_sockets)
{
if (socket != bound_socket.lock())
continue;
// FIXME: sockets should have bound address
in_addr->sin_family = AF_INET;
in_addr->sin_port = bound_port;
in_addr->sin_addr.s_addr = INADDR_ANY;
return {};
}
return {};
}
BAN::ErrorOr<size_t> IPv4Layer::sendto(NetworkSocket& socket, BAN::ConstByteSpan buffer, const sockaddr* address, socklen_t address_len)
{
if (address->sa_family != AF_INET)

View File

@@ -100,4 +100,10 @@ namespace Kernel
}
}
BAN::ErrorOr<void> NetworkSocket::getsockname_impl(sockaddr* address, socklen_t* address_len)
{
TRY(m_network_layer.get_socket_address(this, address, address_len));
return {};
}
}

View File

@@ -911,6 +911,20 @@ namespace Kernel
return TRY(m_open_file_descriptors.socket(domain, type, protocol));
}
BAN::ErrorOr<long> Process::sys_getsockname(int socket, sockaddr* address, socklen_t* address_len)
{
LockGuard _(m_process_lock);
TRY(validate_pointer_access(address_len, sizeof(address_len)));
TRY(validate_pointer_access(address, *address_len));
auto inode = TRY(m_open_file_descriptors.inode_of(socket));
if (!inode->mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
TRY(inode->getsockname(address, address_len));
return 0;
}
BAN::ErrorOr<long> Process::sys_accept(int socket, sockaddr* address, socklen_t* address_len)
{
if (address && !address_len)