mirror of
https://github.com/RGBCube/serenity
synced 2025-05-19 14:55:08 +00:00
LookupServer: Use CUdpSocket instead of the POSIX API
LibCore's UDP socket is a bit more comfortable to work with. :^)
This commit is contained in:
parent
3c129172d4
commit
e335d730d6
2 changed files with 17 additions and 67 deletions
|
@ -30,20 +30,13 @@
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <Kernel/Net/IPv4.h>
|
|
||||||
#include <LibCore/CConfigFile.h>
|
#include <LibCore/CConfigFile.h>
|
||||||
#include <LibCore/CEventLoop.h>
|
#include <LibCore/CEventLoop.h>
|
||||||
#include <LibCore/CFile.h>
|
#include <LibCore/CFile.h>
|
||||||
#include <LibCore/CLocalServer.h>
|
#include <LibCore/CLocalServer.h>
|
||||||
#include <LibCore/CLocalSocket.h>
|
#include <LibCore/CLocalSocket.h>
|
||||||
#include <arpa/inet.h>
|
#include <LibCore/CUdpSocket.h>
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/select.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
LookupServer::LookupServer()
|
LookupServer::LookupServer()
|
||||||
|
@ -176,35 +169,29 @@ Vector<String> LookupServer::lookup(const String& hostname, bool& did_timeout, u
|
||||||
|
|
||||||
auto buffer = request.to_byte_buffer();
|
auto buffer = request.to_byte_buffer();
|
||||||
|
|
||||||
struct sockaddr_in dst_addr;
|
auto udp_socket = CUdpSocket::construct();
|
||||||
|
udp_socket->set_blocking(true);
|
||||||
|
|
||||||
int fd = make_dns_request_socket(dst_addr);
|
struct timeval timeout {
|
||||||
if (fd < 0)
|
1, 0
|
||||||
return {};
|
};
|
||||||
|
|
||||||
int nsent = sendto(fd, buffer.data(), buffer.size(), 0, (const struct sockaddr*)&dst_addr, sizeof(dst_addr));
|
int rc = setsockopt(udp_socket->fd(), SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||||
if (nsent < 0) {
|
if (rc < 0) {
|
||||||
perror("sendto");
|
perror("setsockopt(SOL_SOCKET, SO_RCVTIMEO)");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
ASSERT(nsent == buffer.size());
|
|
||||||
|
|
||||||
struct sockaddr_in src_addr;
|
if (!udp_socket->connect(m_dns_ip, 53))
|
||||||
socklen_t src_addr_len = sizeof(src_addr);
|
return {};
|
||||||
|
|
||||||
|
if (!udp_socket->write(buffer))
|
||||||
|
return {};
|
||||||
|
|
||||||
u8 response_buffer[4096];
|
u8 response_buffer[4096];
|
||||||
ssize_t nrecv = recvfrom(fd, response_buffer, sizeof(response_buffer) - 1, 0, (struct sockaddr*)&src_addr, &src_addr_len);
|
int nrecv = udp_socket->read(response_buffer, sizeof(response_buffer));
|
||||||
if (nrecv < 0) {
|
if (nrecv == 0)
|
||||||
if (errno == EAGAIN) {
|
|
||||||
did_timeout = true;
|
|
||||||
} else {
|
|
||||||
perror("recvfrom");
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
response_buffer[nrecv] = '\0';
|
|
||||||
|
|
||||||
auto o_response = DNSResponse::from_raw_response(response_buffer, nrecv);
|
auto o_response = DNSResponse::from_raw_response(response_buffer, nrecv);
|
||||||
if (!o_response.has_value())
|
if (!o_response.has_value())
|
||||||
|
@ -254,35 +241,3 @@ Vector<String> LookupServer::lookup(const String& hostname, bool& did_timeout, u
|
||||||
m_lookup_cache.set(hostname, { time(nullptr), record_type, addresses });
|
m_lookup_cache.set(hostname, { time(nullptr), record_type, addresses });
|
||||||
return addresses;
|
return addresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
int LookupServer::make_dns_request_socket(sockaddr_in& dst_addr)
|
|
||||||
{
|
|
||||||
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
||||||
if (fd < 0) {
|
|
||||||
perror("socket");
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
struct timeval timeout {
|
|
||||||
1, 0
|
|
||||||
};
|
|
||||||
int rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
|
||||||
if (rc < 0) {
|
|
||||||
perror("setsockopt(SOL_SOCKET, SO_RCVTIMEO)");
|
|
||||||
close(fd);
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&dst_addr, 0, sizeof(dst_addr));
|
|
||||||
|
|
||||||
dst_addr.sin_family = AF_INET;
|
|
||||||
dst_addr.sin_port = htons(53);
|
|
||||||
rc = inet_pton(AF_INET, m_dns_ip.characters(), &dst_addr.sin_addr);
|
|
||||||
if (rc < 0) {
|
|
||||||
perror("inet_pton");
|
|
||||||
close(fd);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
return fd;
|
|
||||||
}
|
|
||||||
|
|
|
@ -28,10 +28,7 @@
|
||||||
|
|
||||||
#include "DNSRequest.h"
|
#include "DNSRequest.h"
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <AK/RefPtr.h>
|
|
||||||
#include <AK/String.h>
|
|
||||||
#include <LibCore/CObject.h>
|
#include <LibCore/CObject.h>
|
||||||
#include <netinet/in.h>
|
|
||||||
|
|
||||||
class CLocalSocket;
|
class CLocalSocket;
|
||||||
class CLocalServer;
|
class CLocalServer;
|
||||||
|
@ -47,8 +44,6 @@ private:
|
||||||
void service_client(RefPtr<CLocalSocket>);
|
void service_client(RefPtr<CLocalSocket>);
|
||||||
Vector<String> lookup(const String& hostname, bool& did_timeout, unsigned short record_type, ShouldRandomizeCase = ShouldRandomizeCase::Yes);
|
Vector<String> lookup(const String& hostname, bool& did_timeout, unsigned short record_type, ShouldRandomizeCase = ShouldRandomizeCase::Yes);
|
||||||
|
|
||||||
int make_dns_request_socket(sockaddr_in& dst_addr);
|
|
||||||
|
|
||||||
struct CachedLookup {
|
struct CachedLookup {
|
||||||
time_t timestamp { 0 };
|
time_t timestamp { 0 };
|
||||||
unsigned short record_type { 0 };
|
unsigned short record_type { 0 };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue