Skip to content

Commit

Permalink
LookupServer: Load hostnames
Browse files Browse the repository at this point in the history
  • Loading branch information
alexispurslane authored and awesomekling committed Jun 7, 2019
1 parent 4f62176 commit 70a37f9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Toolchain/Build
Toolchain/Local
.vscode
compile_commands.json
.clang_complete
1 change: 0 additions & 1 deletion Base/etc/LookupServer.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
[DNS]
IPAddress=8.8.8.8

1 change: 1 addition & 0 deletions Base/etc/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
127.0.0.1 localhost
27 changes: 25 additions & 2 deletions Servers/LookupServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,27 @@ int main(int argc, char** argv)
config->file_name().characters());
auto DNS_IP = config->read_entry("DNS", "IPAddress", "127.0.0.53");

HashMap<String, IPv4Address> dns_cache;
dbgprintf("LookupServer: Loading hosts from /etc/hosts:\n");
HashMap<String, IPv4Address> dns_custom_hostnames;
auto* file = fopen("/etc/hosts", "r");
auto linebuf = ByteBuffer::create_uninitialized(256);
while (fgets((char*)linebuf.pointer(), linebuf.size(), file)) {
auto str_line = String::copy(linebuf);
auto fields = str_line.split('\t');
auto sections = fields[0].split('.');
IPv4Address addr {
(byte)atoi(sections[0].characters()),
(byte)atoi(sections[1].characters()),
(byte)atoi(sections[2].characters()),
(byte)atoi(sections[3].characters()),
};
int len = 0;
while ((fields[1][len++]) != -123)
;
auto name = fields[1].substring(0, len - 3);
dbgprintf("LookupServer: Hosts: %s\t%s\n", name.characters(), addr.to_string().characters());
dns_custom_hostnames.set(name, addr);
}

int server_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (server_fd < 0) {
Expand Down Expand Up @@ -111,7 +131,10 @@ int main(int argc, char** argv)

Vector<String> responses;

if (!hostname.is_empty()) {
if (dns_custom_hostnames.contains(hostname)) {
addresses.append(dns_custom_hostnames.get(hostname));
dbgprintf("LookupServer: Found preconfigured host (from /etc/hosts): %s\n", addresses[0].to_string().characters());
} else if (!hostname.is_empty()) {
bool did_timeout;
int retries = 3;
do {
Expand Down

0 comments on commit 70a37f9

Please sign in to comment.