Kernel: recvfrom() should treat the address arguments as outparams.

This commit is contained in:
Andreas Kling 2019-03-13 14:47:21 +01:00
parent b59d588c04
commit 19a51132f5
Notes: sideshowbarker 2024-07-19 15:04:22 +09:00
10 changed files with 32 additions and 28 deletions

View file

@ -43,7 +43,7 @@ bool IPv4Socket::get_address(sockaddr* address, socklen_t* address_size)
// FIXME: Look into what fallback behavior we should have here.
if (*address_size != sizeof(sockaddr_in))
return false;
memcpy(address, &m_peer_address, sizeof(sockaddr_in));
memcpy(address, &m_destination_address, sizeof(sockaddr_in));
*address_size = sizeof(sockaddr_in);
return true;
}
@ -117,34 +117,31 @@ ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, cons
return -EAFNOSUPPORT;
}
auto peer_address = IPv4Address((const byte*)&((const sockaddr_in*)addr)->sin_addr.s_addr);
auto& ia = *(const sockaddr_in*)addr;
m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
m_destination_port = ia.sin_port;
kprintf("sendto: peer_address=%s\n", peer_address.to_string().characters());
kprintf("sendto: destination=%s:%u\n", m_destination_address.to_string().characters(), m_destination_port);
// FIXME: If we can't find the right MAC address, block until it's available?
// I feel like this should happen in a layer below this code.
MACAddress mac_address;
adapter->send_ipv4(mac_address, peer_address, (IPv4Protocol)protocol(), ByteBuffer::copy((const byte*)data, data_length));
adapter->send_ipv4(mac_address, m_destination_address, (IPv4Protocol)protocol(), ByteBuffer::copy((const byte*)data, data_length));
return data_length;
}
ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, const sockaddr* addr, socklen_t addr_length)
ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
{
(void)flags;
if (addr_length != sizeof(sockaddr_in))
return -EINVAL;
// FIXME: Find the adapter some better way!
auto* adapter = NetworkAdapter::from_ipv4_address(IPv4Address(192, 168, 5, 2));
if (!adapter) {
// FIXME: Figure out which error code to return.
ASSERT_NOT_REACHED();
}
if (addr->sa_family != AF_INET) {
kprintf("recvfrom: Bad address family: %u is not AF_INET!\n", addr->sa_family);
return -EAFNOSUPPORT;
}
if (*addr_length < sizeof(sockaddr_in))
return -EINVAL;
*addr_length = sizeof(sockaddr_in);
auto peer_address = IPv4Address((const byte*)&((const sockaddr_in*)addr)->sin_addr.s_addr);
#ifdef IPV4_SOCKET_DEBUG
kprintf("recvfrom: peer_address=%s\n", peer_address.to_string().characters());
@ -177,6 +174,9 @@ ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, cons
ASSERT(!packet_buffer.is_null());
auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
auto& ia = *(sockaddr_in*)addr;
memcpy(&ia.sin_addr, &m_destination_address, sizeof(IPv4Address));
if (type() == SOCK_RAW) {
ASSERT(buffer_length >= ipv4_packet.payload_size());
memcpy(buffer, ipv4_packet.payload(), ipv4_packet.payload_size());
@ -187,6 +187,7 @@ ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, cons
auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
ASSERT(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
ASSERT(buffer_length >= (udp_packet.length() - sizeof(UDPPacket)));
ia.sin_port = udp_packet.destination_port();
memcpy(buffer, udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket));
return udp_packet.length() - sizeof(UDPPacket);
}

View file

@ -23,7 +23,7 @@ public:
virtual ssize_t write(SocketRole, const byte*, ssize_t) override;
virtual bool can_write(SocketRole) const override;
virtual ssize_t sendto(const void*, size_t, int, const sockaddr*, socklen_t) override;
virtual ssize_t recvfrom(void*, size_t, int flags, const sockaddr*, socklen_t) override;
virtual ssize_t recvfrom(void*, size_t, int flags, sockaddr*, socklen_t*) override;
void did_receive(ByteBuffer&&);
@ -38,7 +38,7 @@ private:
bool m_bound { false };
int m_attached_fds { 0 };
IPv4Address m_peer_address;
IPv4Address m_destination_address;
DoubleBuffer m_for_client;
DoubleBuffer m_for_server;

View file

@ -169,7 +169,7 @@ ssize_t LocalSocket::sendto(const void*, size_t, int, const sockaddr*, socklen_t
ASSERT_NOT_REACHED();
}
ssize_t LocalSocket::recvfrom(void*, size_t, int flags, const sockaddr*, socklen_t)
ssize_t LocalSocket::recvfrom(void*, size_t, int flags, sockaddr*, socklen_t*)
{
ASSERT_NOT_REACHED();
}

View file

@ -20,7 +20,7 @@ public:
virtual ssize_t write(SocketRole, const byte*, ssize_t) override;
virtual bool can_write(SocketRole) const override;
virtual ssize_t sendto(const void*, size_t, int, const sockaddr*, socklen_t) override;
virtual ssize_t recvfrom(void*, size_t, int flags, const sockaddr*, socklen_t) override;
virtual ssize_t recvfrom(void*, size_t, int flags, sockaddr*, socklen_t*) override;
private:
explicit LocalSocket(int type);

View file

@ -2554,12 +2554,14 @@ ssize_t Process::sys$recvfrom(const Syscall::SC_recvfrom_params* params)
void* buffer = params->buffer;
size_t buffer_length = params->buffer_length;
int flags = params->flags;
auto* addr = (const sockaddr*)params->addr;
auto addr_length = (socklen_t)params->addr_length;
auto* addr = (sockaddr*)params->addr;
auto* addr_length = (socklen_t*)params->addr_length;
if (!validate_write(buffer, buffer_length))
return -EFAULT;
if (!validate_read(addr, addr_length))
if (!validate_read_typed(addr_length))
return -EFAULT;
if (!validate_read(addr, *addr_length))
return -EFAULT;
auto* descriptor = file_descriptor(sockfd);
if (!descriptor)
@ -2567,7 +2569,7 @@ ssize_t Process::sys$recvfrom(const Syscall::SC_recvfrom_params* params)
if (!descriptor->is_socket())
return -ENOTSOCK;
auto& socket = *descriptor->socket();
kprintf("recvfrom %p (%u), flags=%u, addr: %p (%u)\n", buffer, buffer_length, flags, addr, addr_length);
kprintf("recvfrom %p (%u), flags=%u, addr: %p (%u)\n", buffer, buffer_length, flags, addr, *addr_length);
return socket.recvfrom(buffer, buffer_length, flags, addr, addr_length);
}

View file

@ -36,7 +36,7 @@ public:
virtual ssize_t write(SocketRole, const byte*, ssize_t) = 0;
virtual bool can_write(SocketRole) const = 0;
virtual ssize_t sendto(const void*, size_t, int flags, const sockaddr*, socklen_t) = 0;
virtual ssize_t recvfrom(void*, size_t, int flags, const sockaddr*, socklen_t) = 0;
virtual ssize_t recvfrom(void*, size_t, int flags, sockaddr*, socklen_t*) = 0;
KResult setsockopt(int level, int option, const void*, socklen_t);
KResult getsockopt(int level, int option, void*, socklen_t*);

View file

@ -146,8 +146,8 @@ struct SC_recvfrom_params {
void* buffer;
size_t buffer_length;
int flags;
const void* addr; // const sockaddr*
size_t addr_length; // socklen_t
void* addr; // sockaddr*
void* addr_length; // socklen_t*
};
struct SC_getsockopt_params {

View file

@ -41,7 +41,7 @@ ssize_t sendto(int sockfd, const void* data, size_t data_length, int flags, cons
__RETURN_WITH_ERRNO(rc, rc, -1);
}
ssize_t recvfrom(int sockfd, void* buffer, size_t buffer_length, int flags, const struct sockaddr* addr, socklen_t addr_length)
ssize_t recvfrom(int sockfd, void* buffer, size_t buffer_length, int flags, struct sockaddr* addr, socklen_t* addr_length)
{
Syscall::SC_recvfrom_params params { sockfd, buffer, buffer_length, flags, addr, addr_length };
int rc = syscall(SC_recvfrom, &params);

View file

@ -57,7 +57,7 @@ int listen(int sockfd, int backlog);
int accept(int sockfd, sockaddr*, socklen_t*);
int connect(int sockfd, const sockaddr*, socklen_t);
ssize_t sendto(int sockfd, const void*, size_t, int flags, const struct sockaddr*, socklen_t);
ssize_t recvfrom(int sockfd, void*, size_t, int flags, const struct sockaddr*, socklen_t);
ssize_t recvfrom(int sockfd, void*, size_t, int flags, struct sockaddr*, socklen_t*);
int getsockopt(int sockfd, int level, int option, void*, socklen_t*);
int setsockopt(int sockfd, int level, int option, const void*, socklen_t);

View file

@ -89,7 +89,8 @@ int main(int argc, char** argv)
}
for (;;) {
rc = recvfrom(fd, &pong_packet, sizeof(PingPacket), 0, (const struct sockaddr*)&peer_address, sizeof(sockaddr_in));
socklen_t peer_address_size = sizeof(peer_address);
rc = recvfrom(fd, &pong_packet, sizeof(PingPacket), 0, (struct sockaddr*)&peer_address, &peer_address_size);
if (rc < 0) {
if (errno == EAGAIN) {
printf("Request (seq=%u) timed out.\n", ntohs(ping_packet.header.un.echo.sequence));