mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:07:34 +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:
parent
780d2a08c4
commit
5e938868a2
7 changed files with 80 additions and 88 deletions
|
@ -51,7 +51,7 @@ bool IPv4Socket::get_address(sockaddr* address, socklen_t* address_size)
|
||||||
// FIXME: Look into what fallback behavior we should have here.
|
// FIXME: Look into what fallback behavior we should have here.
|
||||||
if (*address_size != sizeof(sockaddr_in))
|
if (*address_size != sizeof(sockaddr_in))
|
||||||
return false;
|
return false;
|
||||||
memcpy(address, &m_destination_address, sizeof(sockaddr_in));
|
memcpy(address, &m_peer_address, sizeof(sockaddr_in));
|
||||||
*address_size = sizeof(sockaddr_in);
|
*address_size = sizeof(sockaddr_in);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -65,10 +65,10 @@ KResult IPv4Socket::bind(const sockaddr* address, socklen_t address_size)
|
||||||
return KResult(-EINVAL);
|
return KResult(-EINVAL);
|
||||||
|
|
||||||
auto& ia = *(const sockaddr_in*)address;
|
auto& ia = *(const sockaddr_in*)address;
|
||||||
m_source_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
m_local_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
||||||
m_source_port = ntohs(ia.sin_port);
|
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();
|
return protocol_bind();
|
||||||
}
|
}
|
||||||
|
@ -82,8 +82,8 @@ KResult IPv4Socket::connect(FileDescriptor& descriptor, const sockaddr* address,
|
||||||
return KResult(-EINVAL);
|
return KResult(-EINVAL);
|
||||||
|
|
||||||
auto& ia = *(const sockaddr_in*)address;
|
auto& ia = *(const sockaddr_in*)address;
|
||||||
m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
m_peer_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
||||||
m_destination_port = ntohs(ia.sin_port);
|
m_peer_port = ntohs(ia.sin_port);
|
||||||
|
|
||||||
return protocol_connect(descriptor, should_block);
|
return protocol_connect(descriptor, should_block);
|
||||||
}
|
}
|
||||||
|
@ -122,14 +122,14 @@ bool IPv4Socket::can_write(FileDescriptor&) const
|
||||||
return is_connected();
|
return is_connected();
|
||||||
}
|
}
|
||||||
|
|
||||||
int IPv4Socket::allocate_source_port_if_needed()
|
int IPv4Socket::allocate_local_port_if_needed()
|
||||||
{
|
{
|
||||||
if (m_source_port)
|
if (m_local_port)
|
||||||
return m_source_port;
|
return m_local_port;
|
||||||
int port = protocol_allocate_source_port();
|
int port = protocol_allocate_local_port();
|
||||||
if (port < 0)
|
if (port < 0)
|
||||||
return port;
|
return port;
|
||||||
m_source_port = (word)port;
|
m_local_port = (word)port;
|
||||||
return 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;
|
auto& ia = *(const sockaddr_in*)addr;
|
||||||
m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
m_peer_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
||||||
m_destination_port = ntohs(ia.sin_port);
|
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)
|
if (!adapter)
|
||||||
return -EHOSTUNREACH;
|
return -EHOSTUNREACH;
|
||||||
|
|
||||||
int rc = allocate_source_port_if_needed();
|
int rc = allocate_local_port_if_needed();
|
||||||
if (rc < 0)
|
if (rc < 0)
|
||||||
return rc;
|
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) {
|
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;
|
return data_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,26 +175,21 @@ ssize_t IPv4Socket::recvfrom(FileDescriptor& descriptor, void* buffer, size_t bu
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
#ifdef IPV4_SOCKET_DEBUG
|
#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
|
#endif
|
||||||
|
|
||||||
IPv4Address peer_address;
|
ReceivedPacket packet;
|
||||||
word peer_port = 0;
|
|
||||||
ByteBuffer packet_buffer;
|
|
||||||
{
|
{
|
||||||
LOCKER(lock());
|
LOCKER(lock());
|
||||||
if (!m_receive_queue.is_empty()) {
|
if (!m_receive_queue.is_empty()) {
|
||||||
auto packet = m_receive_queue.take_first();
|
packet = m_receive_queue.take_first();
|
||||||
packet_buffer = packet.data;
|
|
||||||
peer_address = packet.source_address;
|
|
||||||
peer_port = packet.source_port;
|
|
||||||
m_can_read = !m_receive_queue.is_empty();
|
m_can_read = !m_receive_queue.is_empty();
|
||||||
#ifdef IPV4_SOCKET_DEBUG
|
#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
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (packet_buffer.is_null()) {
|
if (packet.data.is_null()) {
|
||||||
if (protocol_is_disconnected()) {
|
if (protocol_is_disconnected()) {
|
||||||
kprintf("IPv4Socket{%p} is protocol-disconnected, returning 0 in recvfrom!\n", this);
|
kprintf("IPv4Socket{%p} is protocol-disconnected, returning 0 in recvfrom!\n", this);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -210,23 +205,20 @@ ssize_t IPv4Socket::recvfrom(FileDescriptor& descriptor, void* buffer, size_t bu
|
||||||
}
|
}
|
||||||
ASSERT(m_can_read);
|
ASSERT(m_can_read);
|
||||||
ASSERT(!m_receive_queue.is_empty());
|
ASSERT(!m_receive_queue.is_empty());
|
||||||
auto packet = m_receive_queue.take_first();
|
packet = m_receive_queue.take_first();
|
||||||
packet_buffer = packet.data;
|
|
||||||
peer_address = packet.source_address;
|
|
||||||
peer_port = packet.source_port;
|
|
||||||
m_can_read = !m_receive_queue.is_empty();
|
m_can_read = !m_receive_queue.is_empty();
|
||||||
#ifdef IPV4_SOCKET_DEBUG
|
#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
|
#endif
|
||||||
}
|
}
|
||||||
ASSERT(!packet_buffer.is_null());
|
ASSERT(!packet.data.is_null());
|
||||||
auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
|
auto& ipv4_packet = *(const IPv4Packet*)(packet.data.pointer());
|
||||||
|
|
||||||
if (addr) {
|
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;
|
auto& ia = *(sockaddr_in*)addr;
|
||||||
memcpy(&ia.sin_addr, &peer_address, sizeof(IPv4Address));
|
memcpy(&ia.sin_addr, &packet.peer_address, sizeof(IPv4Address));
|
||||||
ia.sin_port = htons(peer_port);
|
ia.sin_port = htons(packet.peer_port);
|
||||||
ia.sin_family = AF_INET;
|
ia.sin_family = AF_INET;
|
||||||
ASSERT(addr_length);
|
ASSERT(addr_length);
|
||||||
*addr_length = sizeof(sockaddr_in);
|
*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 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)
|
void IPv4Socket::did_receive(const IPv4Address& source_address, word source_port, ByteBuffer&& packet)
|
||||||
|
|
|
@ -32,27 +32,27 @@ public:
|
||||||
virtual ssize_t sendto(FileDescriptor&, const void*, size_t, int, const sockaddr*, socklen_t) override;
|
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;
|
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;
|
const IPv4Address& local_address() const;
|
||||||
word source_port() const { return m_source_port; }
|
word local_port() const { return m_local_port; }
|
||||||
void set_source_port(word port) { m_source_port = port; }
|
void set_local_port(word port) { m_local_port = port; }
|
||||||
|
|
||||||
const IPv4Address& destination_address() const { return m_destination_address; }
|
const IPv4Address& peer_address() const { return m_peer_address; }
|
||||||
word destination_port() const { return m_destination_port; }
|
word peer_port() const { return m_peer_port; }
|
||||||
void set_destination_port(word port) { m_destination_port = port; }
|
void set_peer_port(word port) { m_peer_port = port; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
IPv4Socket(int type, int protocol);
|
IPv4Socket(int type, int protocol);
|
||||||
virtual const char* class_name() const override { return "IPv4Socket"; }
|
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 KResult protocol_bind() { return KSuccess; }
|
||||||
virtual int protocol_receive(const ByteBuffer&, void*, size_t, int, sockaddr*, socklen_t*) { return -ENOTIMPL; }
|
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 int protocol_send(const void*, int) { return -ENOTIMPL; }
|
||||||
virtual KResult protocol_connect(FileDescriptor&, ShouldBlock) { return KSuccess; }
|
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; }
|
virtual bool protocol_is_disconnected() const { return false; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -61,22 +61,22 @@ private:
|
||||||
bool m_bound { false };
|
bool m_bound { false };
|
||||||
int m_attached_fds { 0 };
|
int m_attached_fds { 0 };
|
||||||
|
|
||||||
IPv4Address m_source_address;
|
IPv4Address m_local_address;
|
||||||
IPv4Address m_destination_address;
|
IPv4Address m_peer_address;
|
||||||
|
|
||||||
DoubleBuffer m_for_client;
|
DoubleBuffer m_for_client;
|
||||||
DoubleBuffer m_for_server;
|
DoubleBuffer m_for_server;
|
||||||
|
|
||||||
struct ReceivedPacket {
|
struct ReceivedPacket {
|
||||||
IPv4Address source_address;
|
IPv4Address peer_address;
|
||||||
word source_port;
|
word peer_port;
|
||||||
ByteBuffer data;
|
ByteBuffer data;
|
||||||
};
|
};
|
||||||
|
|
||||||
SinglyLinkedList<ReceivedPacket> m_receive_queue;
|
SinglyLinkedList<ReceivedPacket> m_receive_queue;
|
||||||
|
|
||||||
word m_source_port { 0 };
|
word m_local_port { 0 };
|
||||||
word m_destination_port { 0 };
|
word m_peer_port { 0 };
|
||||||
|
|
||||||
dword m_bytes_received { 0 };
|
dword m_bytes_received { 0 };
|
||||||
|
|
||||||
|
|
|
@ -276,7 +276,7 @@ void handle_udp(const EthernetFrameHeader& eth, int frame_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
ASSERT(socket->type() == SOCK_DGRAM);
|
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()));
|
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->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()) {
|
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());
|
kprintf("handle_tcp: ack/seq mismatch: got %u, wanted %u\n", tcp_packet.ack_number(), socket->sequence_number());
|
||||||
|
|
|
@ -36,7 +36,7 @@ TCPSocket::TCPSocket(int protocol)
|
||||||
TCPSocket::~TCPSocket()
|
TCPSocket::~TCPSocket()
|
||||||
{
|
{
|
||||||
LOCKER(sockets_by_port().lock());
|
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)
|
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)
|
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)
|
if (!adapter)
|
||||||
return -EHOSTUNREACH;
|
return -EHOSTUNREACH;
|
||||||
send_tcp_packet(TCPFlags::PUSH | TCPFlags::ACK, data, data_length);
|
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)
|
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?
|
// 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);
|
ASSERT(adapter);
|
||||||
|
|
||||||
auto buffer = ByteBuffer::create_zeroed(sizeof(TCPPacket) + payload_size);
|
auto buffer = ByteBuffer::create_zeroed(sizeof(TCPPacket) + payload_size);
|
||||||
auto& tcp_packet = *(TCPPacket*)(buffer.pointer());
|
auto& tcp_packet = *(TCPPacket*)(buffer.pointer());
|
||||||
ASSERT(source_port());
|
ASSERT(local_port());
|
||||||
tcp_packet.set_source_port(source_port());
|
tcp_packet.set_source_port(local_port());
|
||||||
tcp_packet.set_destination_port(destination_port());
|
tcp_packet.set_destination_port(peer_port());
|
||||||
tcp_packet.set_window_size(1024);
|
tcp_packet.set_window_size(1024);
|
||||||
tcp_packet.set_sequence_number(m_sequence_number);
|
tcp_packet.set_sequence_number(m_sequence_number);
|
||||||
tcp_packet.set_data_offset(sizeof(TCPPacket) / sizeof(dword));
|
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);
|
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",
|
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(),
|
adapter->ipv4_address().to_string().characters(),
|
||||||
source_port(),
|
local_port(),
|
||||||
destination_address().to_string().characters(),
|
peer_address().to_string().characters(),
|
||||||
destination_port(),
|
peer_port(),
|
||||||
tcp_packet.has_syn() ? "SYN" : "",
|
tcp_packet.has_syn() ? "SYN" : "",
|
||||||
tcp_packet.has_ack() ? "ACK" : "",
|
tcp_packet.has_ack() ? "ACK" : "",
|
||||||
tcp_packet.sequence_number(),
|
tcp_packet.sequence_number(),
|
||||||
tcp_packet.ack_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)
|
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)
|
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)
|
if (!adapter)
|
||||||
return KResult(-EHOSTUNREACH);
|
return KResult(-EHOSTUNREACH);
|
||||||
|
|
||||||
allocate_source_port_if_needed();
|
allocate_local_port_if_needed();
|
||||||
|
|
||||||
m_sequence_number = 0;
|
m_sequence_number = 0;
|
||||||
m_ack_number = 0;
|
m_ack_number = 0;
|
||||||
|
@ -171,7 +171,7 @@ KResult TCPSocket::protocol_connect(FileDescriptor& descriptor, ShouldBlock shou
|
||||||
return KResult(-EINPROGRESS);
|
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 first_ephemeral_port = 32768;
|
||||||
static const word last_ephemeral_port = 60999;
|
static const word last_ephemeral_port = 60999;
|
||||||
|
@ -182,7 +182,7 @@ int TCPSocket::protocol_allocate_source_port()
|
||||||
for (word port = first_scan_port;;) {
|
for (word port = first_scan_port;;) {
|
||||||
auto it = sockets_by_port().resource().find(port);
|
auto it = sockets_by_port().resource().find(port);
|
||||||
if (it == sockets_by_port().resource().end()) {
|
if (it == sockets_by_port().resource().end()) {
|
||||||
set_source_port(port);
|
set_local_port(port);
|
||||||
sockets_by_port().resource().set(port, this);
|
sockets_by_port().resource().set(port, this);
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
@ -203,8 +203,8 @@ bool TCPSocket::protocol_is_disconnected() const
|
||||||
KResult TCPSocket::protocol_bind()
|
KResult TCPSocket::protocol_bind()
|
||||||
{
|
{
|
||||||
LOCKER(sockets_by_port().lock());
|
LOCKER(sockets_by_port().lock());
|
||||||
if (sockets_by_port().resource().contains(source_port()))
|
if (sockets_by_port().resource().contains(local_port()))
|
||||||
return KResult(-EADDRINUSE);
|
return KResult(-EADDRINUSE);
|
||||||
sockets_by_port().resource().set(source_port(), this);
|
sockets_by_port().resource().set(local_port(), this);
|
||||||
return KSuccess;
|
return KSuccess;
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,12 +31,12 @@ private:
|
||||||
explicit TCPSocket(int protocol);
|
explicit TCPSocket(int protocol);
|
||||||
virtual const char* class_name() const override { return "TCPSocket"; }
|
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_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 int protocol_send(const void*, int) override;
|
||||||
virtual KResult protocol_connect(FileDescriptor&, ShouldBlock) 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 bool protocol_is_disconnected() const override;
|
||||||
virtual KResult protocol_bind() override;
|
virtual KResult protocol_bind() override;
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ UDPSocket::UDPSocket(int protocol)
|
||||||
UDPSocket::~UDPSocket()
|
UDPSocket::~UDPSocket()
|
||||||
{
|
{
|
||||||
LOCKER(sockets_by_port().lock());
|
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)
|
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)
|
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)
|
if (!adapter)
|
||||||
return -EHOSTUNREACH;
|
return -EHOSTUNREACH;
|
||||||
auto buffer = ByteBuffer::create_zeroed(sizeof(UDPPacket) + data_length);
|
auto buffer = ByteBuffer::create_zeroed(sizeof(UDPPacket) + data_length);
|
||||||
auto& udp_packet = *(UDPPacket*)(buffer.pointer());
|
auto& udp_packet = *(UDPPacket*)(buffer.pointer());
|
||||||
udp_packet.set_source_port(source_port());
|
udp_packet.set_source_port(local_port());
|
||||||
udp_packet.set_destination_port(destination_port());
|
udp_packet.set_destination_port(peer_port());
|
||||||
udp_packet.set_length(sizeof(UDPPacket) + data_length);
|
udp_packet.set_length(sizeof(UDPPacket) + data_length);
|
||||||
memcpy(udp_packet.payload(), data, data_length);
|
memcpy(udp_packet.payload(), data, data_length);
|
||||||
kprintf("sending as udp packet from %s:%u to %s:%u!\n",
|
kprintf("sending as udp packet from %s:%u to %s:%u!\n",
|
||||||
adapter->ipv4_address().to_string().characters(),
|
adapter->ipv4_address().to_string().characters(),
|
||||||
source_port(),
|
local_port(),
|
||||||
destination_address().to_string().characters(),
|
peer_address().to_string().characters(),
|
||||||
destination_port());
|
peer_port());
|
||||||
adapter->send_ipv4(MACAddress(), destination_address(), IPv4Protocol::UDP, move(buffer));
|
adapter->send_ipv4(MACAddress(), peer_address(), IPv4Protocol::UDP, move(buffer));
|
||||||
return data_length;
|
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 first_ephemeral_port = 32768;
|
||||||
static const word last_ephemeral_port = 60999;
|
static const word last_ephemeral_port = 60999;
|
||||||
|
@ -88,7 +88,7 @@ int UDPSocket::protocol_allocate_source_port()
|
||||||
for (word port = first_scan_port;;) {
|
for (word port = first_scan_port;;) {
|
||||||
auto it = sockets_by_port().resource().find(port);
|
auto it = sockets_by_port().resource().find(port);
|
||||||
if (it == sockets_by_port().resource().end()) {
|
if (it == sockets_by_port().resource().end()) {
|
||||||
set_source_port(port);
|
set_local_port(port);
|
||||||
sockets_by_port().resource().set(port, this);
|
sockets_by_port().resource().set(port, this);
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
@ -104,8 +104,8 @@ int UDPSocket::protocol_allocate_source_port()
|
||||||
KResult UDPSocket::protocol_bind()
|
KResult UDPSocket::protocol_bind()
|
||||||
{
|
{
|
||||||
LOCKER(sockets_by_port().lock());
|
LOCKER(sockets_by_port().lock());
|
||||||
if (sockets_by_port().resource().contains(source_port()))
|
if (sockets_by_port().resource().contains(local_port()))
|
||||||
return KResult(-EADDRINUSE);
|
return KResult(-EADDRINUSE);
|
||||||
sockets_by_port().resource().set(source_port(), this);
|
sockets_by_port().resource().set(local_port(), this);
|
||||||
return KSuccess;
|
return KSuccess;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,17 +9,17 @@ public:
|
||||||
static Retained<UDPSocket> create(int protocol);
|
static Retained<UDPSocket> create(int protocol);
|
||||||
virtual ~UDPSocket() override;
|
virtual ~UDPSocket() override;
|
||||||
|
|
||||||
static Lockable<HashMap<word, UDPSocket*>>& sockets_by_port();
|
|
||||||
static UDPSocketHandle from_port(word);
|
static UDPSocketHandle from_port(word);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit UDPSocket(int protocol);
|
explicit UDPSocket(int protocol);
|
||||||
virtual const char* class_name() const override { return "UDPSocket"; }
|
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_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 int protocol_send(const void*, int) override;
|
||||||
virtual KResult protocol_connect(FileDescriptor&, ShouldBlock) override { return KSuccess; }
|
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;
|
virtual KResult protocol_bind() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue