mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:38:11 +00:00
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.. :^)
This commit is contained in:
parent
d34ad44f90
commit
d04fcccc90
6 changed files with 23 additions and 38 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue