1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:48:10 +00:00

IPv4: Rename source/destination in socket classes to local/peer.

It was way too ambiguous who's the source and who's the destination, and it
didn't really follow a logical pattern. "Local port" vs "Peer port" is super
obvious, so let's call it that.
This commit is contained in:
Andreas Kling 2019-05-04 16:40:34 +02:00
parent 780d2a08c4
commit 5e938868a2
7 changed files with 80 additions and 88 deletions

View file

@ -51,7 +51,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_destination_address, sizeof(sockaddr_in));
memcpy(address, &m_peer_address, sizeof(sockaddr_in));
*address_size = sizeof(sockaddr_in);
return true;
}
@ -65,10 +65,10 @@ KResult IPv4Socket::bind(const sockaddr* address, socklen_t address_size)
return KResult(-EINVAL);
auto& ia = *(const sockaddr_in*)address;
m_source_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
m_source_port = ntohs(ia.sin_port);
m_local_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
m_local_port = ntohs(ia.sin_port);
dbgprintf("IPv4Socket::bind %s{%p} to port %u\n", class_name(), this, m_source_port);
dbgprintf("IPv4Socket::bind %s{%p} to port %u\n", class_name(), this, m_local_port);
return protocol_bind();
}
@ -82,8 +82,8 @@ KResult IPv4Socket::connect(FileDescriptor& descriptor, const sockaddr* address,
return KResult(-EINVAL);
auto& ia = *(const sockaddr_in*)address;
m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
m_destination_port = ntohs(ia.sin_port);
m_peer_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
m_peer_port = ntohs(ia.sin_port);
return protocol_connect(descriptor, should_block);
}
@ -122,14 +122,14 @@ bool IPv4Socket::can_write(FileDescriptor&) const
return is_connected();
}
int IPv4Socket::allocate_source_port_if_needed()
int IPv4Socket::allocate_local_port_if_needed()
{
if (m_source_port)
return m_source_port;
int port = protocol_allocate_source_port();
if (m_local_port)
return m_local_port;
int port = protocol_allocate_local_port();
if (port < 0)
return port;
m_source_port = (word)port;
m_local_port = (word)port;
return port;
}
@ -146,22 +146,22 @@ ssize_t IPv4Socket::sendto(FileDescriptor&, const void* data, size_t data_length
}
auto& ia = *(const sockaddr_in*)addr;
m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
m_destination_port = ntohs(ia.sin_port);
m_peer_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
m_peer_port = ntohs(ia.sin_port);
}
auto* adapter = adapter_for_route_to(m_destination_address);
auto* adapter = adapter_for_route_to(m_peer_address);
if (!adapter)
return -EHOSTUNREACH;
int rc = allocate_source_port_if_needed();
int rc = allocate_local_port_if_needed();
if (rc < 0)
return rc;
kprintf("sendto: destination=%s:%u\n", m_destination_address.to_string().characters(), m_destination_port);
kprintf("sendto: destination=%s:%u\n", m_peer_address.to_string().characters(), m_peer_port);
if (type() == SOCK_RAW) {
adapter->send_ipv4(MACAddress(), m_destination_address, (IPv4Protocol)protocol(), ByteBuffer::copy(data, data_length));
adapter->send_ipv4(MACAddress(), m_peer_address, (IPv4Protocol)protocol(), ByteBuffer::copy(data, data_length));
return data_length;
}
@ -175,26 +175,21 @@ ssize_t IPv4Socket::recvfrom(FileDescriptor& descriptor, void* buffer, size_t bu
return -EINVAL;
#ifdef IPV4_SOCKET_DEBUG
kprintf("recvfrom: type=%d, source_port=%u\n", type(), source_port());
kprintf("recvfrom: type=%d, local_port=%u\n", type(), local_port());
#endif
IPv4Address peer_address;
word peer_port = 0;
ByteBuffer packet_buffer;
ReceivedPacket packet;
{
LOCKER(lock());
if (!m_receive_queue.is_empty()) {
auto packet = m_receive_queue.take_first();
packet_buffer = packet.data;
peer_address = packet.source_address;
peer_port = packet.source_port;
packet = m_receive_queue.take_first();
m_can_read = !m_receive_queue.is_empty();
#ifdef IPV4_SOCKET_DEBUG
kprintf("IPv4Socket(%p): recvfrom without blocking %d bytes, packets in queue: %d\n", this, packet_buffer.size(), m_receive_queue.size_slow());
kprintf("IPv4Socket(%p): recvfrom without blocking %d bytes, packets in queue: %d\n", this, packet.data.size(), m_receive_queue.size_slow());
#endif
}
}
if (packet_buffer.is_null()) {
if (packet.data.is_null()) {
if (protocol_is_disconnected()) {
kprintf("IPv4Socket{%p} is protocol-disconnected, returning 0 in recvfrom!\n", this);
return 0;
@ -210,23 +205,20 @@ ssize_t IPv4Socket::recvfrom(FileDescriptor& descriptor, void* buffer, size_t bu
}
ASSERT(m_can_read);
ASSERT(!m_receive_queue.is_empty());
auto packet = m_receive_queue.take_first();
packet_buffer = packet.data;
peer_address = packet.source_address;
peer_port = packet.source_port;
packet = m_receive_queue.take_first();
m_can_read = !m_receive_queue.is_empty();
#ifdef IPV4_SOCKET_DEBUG
kprintf("IPv4Socket(%p): recvfrom with blocking %d bytes, packets in queue: %d\n", this, packet_buffer.size(), m_receive_queue.size_slow());
kprintf("IPv4Socket(%p): recvfrom with blocking %d bytes, packets in queue: %d\n", this, packet.data.size(), m_receive_queue.size_slow());
#endif
}
ASSERT(!packet_buffer.is_null());
auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
ASSERT(!packet.data.is_null());
auto& ipv4_packet = *(const IPv4Packet*)(packet.data.pointer());
if (addr) {
dbgprintf("Incoming packet is from: %s:%u\n", peer_address.to_string().characters(), peer_port);
dbgprintf("Incoming packet is from: %s:%u\n", packet.peer_address.to_string().characters(), packet.peer_port);
auto& ia = *(sockaddr_in*)addr;
memcpy(&ia.sin_addr, &peer_address, sizeof(IPv4Address));
ia.sin_port = htons(peer_port);
memcpy(&ia.sin_addr, &packet.peer_address, sizeof(IPv4Address));
ia.sin_port = htons(packet.peer_port);
ia.sin_family = AF_INET;
ASSERT(addr_length);
*addr_length = sizeof(sockaddr_in);
@ -238,7 +230,7 @@ ssize_t IPv4Socket::recvfrom(FileDescriptor& descriptor, void* buffer, size_t bu
return ipv4_packet.payload_size();
}
return protocol_receive(packet_buffer, buffer, buffer_length, flags, addr, addr_length);
return protocol_receive(packet.data, buffer, buffer_length, flags, addr, addr_length);
}
void IPv4Socket::did_receive(const IPv4Address& source_address, word source_port, ByteBuffer&& packet)