diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-05-04 16:40:34 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-05-04 16:40:34 +0200 |
commit | 5e938868a226cd9d2c2f2736c678f98618f345f5 (patch) | |
tree | 4cae733c660d5bab12b5b6d6018c13a080d60acb /Kernel/Net | |
parent | 780d2a08c488e5e8a61f7508f7cc31f35bdf8b26 (diff) | |
download | serenity-5e938868a226cd9d2c2f2736c678f98618f345f5.zip |
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.
Diffstat (limited to 'Kernel/Net')
-rw-r--r-- | Kernel/Net/IPv4Socket.cpp | 68 | ||||
-rw-r--r-- | Kernel/Net/IPv4Socket.h | 30 | ||||
-rw-r--r-- | Kernel/Net/NetworkTask.cpp | 4 | ||||
-rw-r--r-- | Kernel/Net/TCPSocket.cpp | 34 | ||||
-rw-r--r-- | Kernel/Net/TCPSocket.h | 4 | ||||
-rw-r--r-- | Kernel/Net/UDPSocket.cpp | 24 | ||||
-rw-r--r-- | Kernel/Net/UDPSocket.h | 4 |
7 files changed, 80 insertions, 88 deletions
diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index 697e24f1d9..f6b4b96ed6 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -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) diff --git a/Kernel/Net/IPv4Socket.h b/Kernel/Net/IPv4Socket.h index 47ec8804f3..3d7b145293 100644 --- a/Kernel/Net/IPv4Socket.h +++ b/Kernel/Net/IPv4Socket.h @@ -32,27 +32,27 @@ public: virtual ssize_t sendto(FileDescriptor&, const void*, size_t, int, const sockaddr*, socklen_t) override; virtual ssize_t recvfrom(FileDescriptor&, void*, size_t, int flags, sockaddr*, socklen_t*) override; - void did_receive(const IPv4Address& source_address, word source_port, ByteBuffer&&); + void did_receive(const IPv4Address& peer_address, word peer_port, ByteBuffer&&); - const IPv4Address& source_address() const; - word source_port() const { return m_source_port; } - void set_source_port(word port) { m_source_port = port; } + const IPv4Address& local_address() const; + word local_port() const { return m_local_port; } + void set_local_port(word port) { m_local_port = port; } - const IPv4Address& destination_address() const { return m_destination_address; } - word destination_port() const { return m_destination_port; } - void set_destination_port(word port) { m_destination_port = port; } + const IPv4Address& peer_address() const { return m_peer_address; } + word peer_port() const { return m_peer_port; } + void set_peer_port(word port) { m_peer_port = port; } protected: IPv4Socket(int type, int protocol); virtual const char* class_name() const override { return "IPv4Socket"; } - int allocate_source_port_if_needed(); + int allocate_local_port_if_needed(); virtual KResult protocol_bind() { return KSuccess; } virtual int protocol_receive(const ByteBuffer&, void*, size_t, int, sockaddr*, socklen_t*) { return -ENOTIMPL; } virtual int protocol_send(const void*, int) { return -ENOTIMPL; } virtual KResult protocol_connect(FileDescriptor&, ShouldBlock) { return KSuccess; } - virtual int protocol_allocate_source_port() { return 0; } + virtual int protocol_allocate_local_port() { return 0; } virtual bool protocol_is_disconnected() const { return false; } private: @@ -61,22 +61,22 @@ private: bool m_bound { false }; int m_attached_fds { 0 }; - IPv4Address m_source_address; - IPv4Address m_destination_address; + IPv4Address m_local_address; + IPv4Address m_peer_address; DoubleBuffer m_for_client; DoubleBuffer m_for_server; struct ReceivedPacket { - IPv4Address source_address; - word source_port; + IPv4Address peer_address; + word peer_port; ByteBuffer data; }; SinglyLinkedList<ReceivedPacket> m_receive_queue; - word m_source_port { 0 }; - word m_destination_port { 0 }; + word m_local_port { 0 }; + word m_peer_port { 0 }; dword m_bytes_received { 0 }; diff --git a/Kernel/Net/NetworkTask.cpp b/Kernel/Net/NetworkTask.cpp index 680dfc33c8..bd8406376d 100644 --- a/Kernel/Net/NetworkTask.cpp +++ b/Kernel/Net/NetworkTask.cpp @@ -276,7 +276,7 @@ void handle_udp(const EthernetFrameHeader& eth, int frame_size) } ASSERT(socket->type() == SOCK_DGRAM); - ASSERT(socket->source_port() == udp_packet.destination_port()); + ASSERT(socket->local_port() == udp_packet.destination_port()); socket->did_receive(ipv4_packet.source(), udp_packet.source_port(), ByteBuffer::copy(&ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size())); } @@ -317,7 +317,7 @@ void handle_tcp(const EthernetFrameHeader& eth, int frame_size) } ASSERT(socket->type() == SOCK_STREAM); - ASSERT(socket->source_port() == tcp_packet.destination_port()); + ASSERT(socket->local_port() == tcp_packet.destination_port()); if (tcp_packet.ack_number() != socket->sequence_number()) { kprintf("handle_tcp: ack/seq mismatch: got %u, wanted %u\n", tcp_packet.ack_number(), socket->sequence_number()); diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index 6f73144791..acb3c47569 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -36,7 +36,7 @@ TCPSocket::TCPSocket(int protocol) TCPSocket::~TCPSocket() { LOCKER(sockets_by_port().lock()); - sockets_by_port().resource().remove(source_port()); + sockets_by_port().resource().remove(local_port()); } Retained<TCPSocket> TCPSocket::create(int protocol) @@ -60,7 +60,7 @@ int TCPSocket::protocol_receive(const ByteBuffer& packet_buffer, void* buffer, s int TCPSocket::protocol_send(const void* data, int data_length) { - auto* adapter = adapter_for_route_to(destination_address()); + auto* adapter = adapter_for_route_to(peer_address()); if (!adapter) return -EHOSTUNREACH; send_tcp_packet(TCPFlags::PUSH | TCPFlags::ACK, data, data_length); @@ -70,14 +70,14 @@ int TCPSocket::protocol_send(const void* data, int data_length) void TCPSocket::send_tcp_packet(word flags, const void* payload, int payload_size) { // FIXME: Maybe the socket should be bound to an adapter instead of looking it up every time? - auto* adapter = adapter_for_route_to(destination_address()); + auto* adapter = adapter_for_route_to(peer_address()); ASSERT(adapter); auto buffer = ByteBuffer::create_zeroed(sizeof(TCPPacket) + payload_size); auto& tcp_packet = *(TCPPacket*)(buffer.pointer()); - ASSERT(source_port()); - tcp_packet.set_source_port(source_port()); - tcp_packet.set_destination_port(destination_port()); + ASSERT(local_port()); + tcp_packet.set_source_port(local_port()); + tcp_packet.set_destination_port(peer_port()); tcp_packet.set_window_size(1024); tcp_packet.set_sequence_number(m_sequence_number); tcp_packet.set_data_offset(sizeof(TCPPacket) / sizeof(dword)); @@ -93,18 +93,18 @@ void TCPSocket::send_tcp_packet(word flags, const void* payload, int payload_siz } memcpy(tcp_packet.payload(), payload, payload_size); - tcp_packet.set_checksum(compute_tcp_checksum(adapter->ipv4_address(), destination_address(), tcp_packet, payload_size)); + tcp_packet.set_checksum(compute_tcp_checksum(adapter->ipv4_address(), peer_address(), tcp_packet, payload_size)); kprintf("sending tcp packet from %s:%u to %s:%u with (%s %s) seq_no=%u, ack_no=%u\n", adapter->ipv4_address().to_string().characters(), - source_port(), - destination_address().to_string().characters(), - destination_port(), + local_port(), + peer_address().to_string().characters(), + peer_port(), tcp_packet.has_syn() ? "SYN" : "", tcp_packet.has_ack() ? "ACK" : "", tcp_packet.sequence_number(), tcp_packet.ack_number() ); - adapter->send_ipv4(MACAddress(), destination_address(), IPv4Protocol::TCP, move(buffer)); + adapter->send_ipv4(MACAddress(), peer_address(), IPv4Protocol::TCP, move(buffer)); } NetworkOrdered<word> TCPSocket::compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket& packet, word payload_size) @@ -150,11 +150,11 @@ NetworkOrdered<word> TCPSocket::compute_tcp_checksum(const IPv4Address& source, KResult TCPSocket::protocol_connect(FileDescriptor& descriptor, ShouldBlock should_block) { - auto* adapter = adapter_for_route_to(destination_address()); + auto* adapter = adapter_for_route_to(peer_address()); if (!adapter) return KResult(-EHOSTUNREACH); - allocate_source_port_if_needed(); + allocate_local_port_if_needed(); m_sequence_number = 0; m_ack_number = 0; @@ -171,7 +171,7 @@ KResult TCPSocket::protocol_connect(FileDescriptor& descriptor, ShouldBlock shou return KResult(-EINPROGRESS); } -int TCPSocket::protocol_allocate_source_port() +int TCPSocket::protocol_allocate_local_port() { static const word first_ephemeral_port = 32768; static const word last_ephemeral_port = 60999; @@ -182,7 +182,7 @@ int TCPSocket::protocol_allocate_source_port() for (word port = first_scan_port;;) { auto it = sockets_by_port().resource().find(port); if (it == sockets_by_port().resource().end()) { - set_source_port(port); + set_local_port(port); sockets_by_port().resource().set(port, this); return port; } @@ -203,8 +203,8 @@ bool TCPSocket::protocol_is_disconnected() const KResult TCPSocket::protocol_bind() { LOCKER(sockets_by_port().lock()); - if (sockets_by_port().resource().contains(source_port())) + if (sockets_by_port().resource().contains(local_port())) return KResult(-EADDRINUSE); - sockets_by_port().resource().set(source_port(), this); + sockets_by_port().resource().set(local_port(), this); return KSuccess; } diff --git a/Kernel/Net/TCPSocket.h b/Kernel/Net/TCPSocket.h index a1ebce3a0d..c248b9d9ba 100644 --- a/Kernel/Net/TCPSocket.h +++ b/Kernel/Net/TCPSocket.h @@ -31,12 +31,12 @@ private: explicit TCPSocket(int protocol); virtual const char* class_name() const override { return "TCPSocket"; } - NetworkOrdered<word> compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket&, word payload_size); + static NetworkOrdered<word> compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket&, word payload_size); virtual int protocol_receive(const ByteBuffer&, void* buffer, size_t buffer_size, int flags, sockaddr* addr, socklen_t* addr_length) override; virtual int protocol_send(const void*, int) override; virtual KResult protocol_connect(FileDescriptor&, ShouldBlock) override; - virtual int protocol_allocate_source_port() override; + virtual int protocol_allocate_local_port() override; virtual bool protocol_is_disconnected() const override; virtual KResult protocol_bind() override; diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp index 982bdd8606..ee3597e386 100644 --- a/Kernel/Net/UDPSocket.cpp +++ b/Kernel/Net/UDPSocket.cpp @@ -36,7 +36,7 @@ UDPSocket::UDPSocket(int protocol) UDPSocket::~UDPSocket() { LOCKER(sockets_by_port().lock()); - sockets_by_port().resource().remove(source_port()); + sockets_by_port().resource().remove(local_port()); } Retained<UDPSocket> UDPSocket::create(int protocol) @@ -59,25 +59,25 @@ int UDPSocket::protocol_receive(const ByteBuffer& packet_buffer, void* buffer, s int UDPSocket::protocol_send(const void* data, int data_length) { - auto* adapter = adapter_for_route_to(destination_address()); + auto* adapter = adapter_for_route_to(peer_address()); if (!adapter) return -EHOSTUNREACH; auto buffer = ByteBuffer::create_zeroed(sizeof(UDPPacket) + data_length); auto& udp_packet = *(UDPPacket*)(buffer.pointer()); - udp_packet.set_source_port(source_port()); - udp_packet.set_destination_port(destination_port()); + udp_packet.set_source_port(local_port()); + udp_packet.set_destination_port(peer_port()); udp_packet.set_length(sizeof(UDPPacket) + data_length); memcpy(udp_packet.payload(), data, data_length); kprintf("sending as udp packet from %s:%u to %s:%u!\n", adapter->ipv4_address().to_string().characters(), - source_port(), - destination_address().to_string().characters(), - destination_port()); - adapter->send_ipv4(MACAddress(), destination_address(), IPv4Protocol::UDP, move(buffer)); + local_port(), + peer_address().to_string().characters(), + peer_port()); + adapter->send_ipv4(MACAddress(), peer_address(), IPv4Protocol::UDP, move(buffer)); return data_length; } -int UDPSocket::protocol_allocate_source_port() +int UDPSocket::protocol_allocate_local_port() { static const word first_ephemeral_port = 32768; static const word last_ephemeral_port = 60999; @@ -88,7 +88,7 @@ int UDPSocket::protocol_allocate_source_port() for (word port = first_scan_port;;) { auto it = sockets_by_port().resource().find(port); if (it == sockets_by_port().resource().end()) { - set_source_port(port); + set_local_port(port); sockets_by_port().resource().set(port, this); return port; } @@ -104,8 +104,8 @@ int UDPSocket::protocol_allocate_source_port() KResult UDPSocket::protocol_bind() { LOCKER(sockets_by_port().lock()); - if (sockets_by_port().resource().contains(source_port())) + if (sockets_by_port().resource().contains(local_port())) return KResult(-EADDRINUSE); - sockets_by_port().resource().set(source_port(), this); + sockets_by_port().resource().set(local_port(), this); return KSuccess; } diff --git a/Kernel/Net/UDPSocket.h b/Kernel/Net/UDPSocket.h index 693f6eb349..ea94887754 100644 --- a/Kernel/Net/UDPSocket.h +++ b/Kernel/Net/UDPSocket.h @@ -9,17 +9,17 @@ public: static Retained<UDPSocket> create(int protocol); virtual ~UDPSocket() override; - static Lockable<HashMap<word, UDPSocket*>>& sockets_by_port(); static UDPSocketHandle from_port(word); private: explicit UDPSocket(int protocol); virtual const char* class_name() const override { return "UDPSocket"; } + static Lockable<HashMap<word, UDPSocket*>>& sockets_by_port(); virtual int protocol_receive(const ByteBuffer&, void* buffer, size_t buffer_size, int flags, sockaddr* addr, socklen_t* addr_length) override; virtual int protocol_send(const void*, int) override; virtual KResult protocol_connect(FileDescriptor&, ShouldBlock) override { return KSuccess; } - virtual int protocol_allocate_source_port() override; + virtual int protocol_allocate_local_port() override; virtual KResult protocol_bind() override; }; |