mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:27:45 +00:00
LibCore+LookupServer: Implement and use UDPServer::send
This commit is contained in:
parent
3da0c072f4
commit
0cca6cef95
5 changed files with 25 additions and 6 deletions
|
@ -102,4 +102,18 @@ Optional<u16> UDPServer::local_port() const
|
||||||
return ntohs(address.sin_port);
|
return ntohs(address.sin_port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<size_t> UDPServer::send(ReadonlyBytes buffer, sockaddr_in const& to)
|
||||||
|
{
|
||||||
|
if (m_fd < 0) {
|
||||||
|
return Error::from_errno(EBADF);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto result = ::sendto(m_fd, buffer.data(), buffer.size(), 0, (sockaddr const*)&to, sizeof(to));
|
||||||
|
if (result < 0) {
|
||||||
|
return Error::from_errno(errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,8 @@ public:
|
||||||
return receive(size, saddr);
|
return receive(size, saddr);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ErrorOr<size_t> send(ReadonlyBytes, sockaddr_in const& to);
|
||||||
|
|
||||||
Optional<IPv4Address> local_address() const;
|
Optional<IPv4Address> local_address() const;
|
||||||
Optional<u16> local_port() const;
|
Optional<u16> local_port() const;
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,9 @@ void DNSServer::handle_client()
|
||||||
response.set_code(DNSPacket::Code::NOERROR);
|
response.set_code(DNSPacket::Code::NOERROR);
|
||||||
|
|
||||||
buffer = response.to_byte_buffer();
|
buffer = response.to_byte_buffer();
|
||||||
sendto(fd(), buffer.data(), buffer.size(), 0, (const sockaddr*)&client_address, sizeof(client_address));
|
|
||||||
|
// FIXME: We should be handling errors here.
|
||||||
|
[[maybe_unused]] auto result = send(buffer, client_address);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,16 +100,17 @@ void MulticastDNS::announce()
|
||||||
response.add_answer(answer);
|
response.add_answer(answer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (emit_packet(response) < 0)
|
if (emit_packet(response).is_error())
|
||||||
perror("Failed to emit response packet");
|
perror("Failed to emit response packet");
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t MulticastDNS::emit_packet(const DNSPacket& packet, const sockaddr_in* destination)
|
ErrorOr<size_t> MulticastDNS::emit_packet(const DNSPacket& packet, const sockaddr_in* destination)
|
||||||
{
|
{
|
||||||
auto buffer = packet.to_byte_buffer();
|
auto buffer = packet.to_byte_buffer();
|
||||||
if (!destination)
|
if (!destination)
|
||||||
destination = &mdns_addr;
|
destination = &mdns_addr;
|
||||||
return sendto(fd(), buffer.data(), buffer.size(), 0, (const sockaddr*)destination, sizeof(*destination));
|
|
||||||
|
return send(buffer, *destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<IPv4Address> MulticastDNS::local_addresses() const
|
Vector<IPv4Address> MulticastDNS::local_addresses() const
|
||||||
|
@ -148,7 +149,7 @@ Vector<DNSAnswer> MulticastDNS::lookup(const DNSName& name, DNSRecordType record
|
||||||
request.set_recursion_desired(false);
|
request.set_recursion_desired(false);
|
||||||
request.add_question({ name, record_type, DNSRecordClass::IN, false });
|
request.add_question({ name, record_type, DNSRecordClass::IN, false });
|
||||||
|
|
||||||
if (emit_packet(request) < 0) {
|
if (emit_packet(request).is_error()) {
|
||||||
perror("failed to emit request packet");
|
perror("failed to emit request packet");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ private:
|
||||||
explicit MulticastDNS(Object* parent = nullptr);
|
explicit MulticastDNS(Object* parent = nullptr);
|
||||||
|
|
||||||
void announce();
|
void announce();
|
||||||
ssize_t emit_packet(const DNSPacket&, const sockaddr_in* destination = nullptr);
|
ErrorOr<size_t> emit_packet(const DNSPacket&, const sockaddr_in* destination = nullptr);
|
||||||
|
|
||||||
void handle_packet();
|
void handle_packet();
|
||||||
void handle_query(const DNSPacket&);
|
void handle_query(const DNSPacket&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue