summaryrefslogtreecommitdiff
path: root/Kernel/Net/IPv4Socket.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-02-07 23:42:28 +0100
committerAndreas Kling <kling@serenityos.org>2020-02-07 23:43:32 +0100
commitd04fcccc90dfa99031e5e16c2b2be7a31e613520 (patch)
treebdcc5a39f8150129303fe529cfa2481ffd4a5876 /Kernel/Net/IPv4Socket.cpp
parentd34ad44f90fbfc9f4166d8326994aa86458a18d5 (diff)
downloadserenity-d04fcccc90dfa99031e5e16c2b2be7a31e613520.zip
Kernel: Truncate addresses stored by getsockname() and getpeername()
If there's not enough space in the output buffer for the whole sockaddr we now simply truncate the address instead of returning EINVAL. This patch also makes getpeername() actually return the peer address rather than the local address.. :^)
Diffstat (limited to 'Kernel/Net/IPv4Socket.cpp')
-rw-r--r--Kernel/Net/IPv4Socket.cpp24
1 files changed, 6 insertions, 18 deletions
diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp
index 368557bb28..ad90567848 100644
--- a/Kernel/Net/IPv4Socket.cpp
+++ b/Kernel/Net/IPv4Socket.cpp
@@ -82,30 +82,18 @@ IPv4Socket::~IPv4Socket()
all_sockets().resource().remove(this);
}
-bool IPv4Socket::get_local_address(sockaddr* address, socklen_t* address_size)
+void IPv4Socket::get_local_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;
- auto& ia = (sockaddr_in&)*address;
- ia.sin_family = AF_INET;
- ia.sin_port = htons(m_local_port);
- memcpy(&ia.sin_addr, &m_local_address, sizeof(IPv4Address));
+ sockaddr_in local_address = { AF_INET, htons(m_local_port), { m_local_address.to_in_addr_t() }, { 0 } };
+ memcpy(address, &local_address, min(static_cast<size_t>(*address_size), sizeof(sockaddr_in)));
*address_size = sizeof(sockaddr_in);
- return true;
}
-bool IPv4Socket::get_peer_address(sockaddr* address, socklen_t* address_size)
+void IPv4Socket::get_peer_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;
- auto& ia = (sockaddr_in&)*address;
- ia.sin_family = AF_INET;
- ia.sin_port = htons(m_peer_port);
- memcpy(&ia.sin_addr, &m_peer_address, sizeof(IPv4Address));
+ sockaddr_in peer_address = { AF_INET, htons(m_peer_port), { m_peer_address.to_in_addr_t() }, { 0 } };
+ memcpy(address, &peer_address, min(static_cast<size_t>(*address_size), sizeof(sockaddr_in)));
*address_size = sizeof(sockaddr_in);
- return true;
}
KResult IPv4Socket::bind(const sockaddr* user_address, socklen_t address_size)