Ver Fonte

LookupServer: Use a CFile for loading /etc/hosts.

This fixes an issue with the lines having extra bytes at the end due after
converting from ByteBuffer to String.
Andreas Kling há 6 anos atrás
pai
commit
8cb0c765ca
1 ficheiros alterados com 36 adições e 27 exclusões
  1. 36 27
      Servers/LookupServer/main.cpp

+ 36 - 27
Servers/LookupServer/main.cpp

@@ -7,6 +7,7 @@
 #include <AK/StringBuilder.h>
 #include <Kernel/Net/IPv4.h>
 #include <LibCore/CConfigFile.h>
+#include <LibCore/CFile.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <stdio.h>
@@ -25,27 +26,22 @@
 
 #define C_IN 1
 
+static HashMap<String, String> dns_custom_hostnames;
+
 static Vector<String> lookup(const String& hostname, bool& did_timeout, const String& DNS_IP, unsigned short record_type);
 static String parse_dns_name(const byte*, int& offset, int max_offset);
 
-int main(int argc, char** argv)
+static void load_etc_hosts()
 {
-    (void)argc;
-    (void)argv;
-
-    unlink("/tmp/.LookupServer-socket");
-
-    auto config = CConfigFile::get_for_system("LookupServer");
-    dbgprintf("LookupServer: Using network config file at %s.\n",
-        config->file_name().characters());
-    auto DNS_IP = config->read_entry("DNS", "IPAddress", "127.0.0.53");
-
-    dbgprintf("LookupServer: Loading hosts from /etc/hosts:\n");
-    HashMap<String, String> 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);
+    dbgprintf("LookupServer: Loading hosts from /etc/hosts\n");
+    CFile file("/etc/hosts");
+    if (!file.open(CIODevice::ReadOnly))
+        return;
+    while (!file.eof()) {
+        auto line = file.read_line(1024);
+        if (line.is_empty())
+            break;
+        auto str_line = String((const char*)line.pointer(), line.size() - 1, Chomp);
         auto fields = str_line.split('\t');
 
         auto sections = fields[0].split('.');
@@ -55,25 +51,38 @@ int main(int argc, char** argv)
             (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);
 
+        auto name = fields[1];
         dns_custom_hostnames.set(name, addr.to_string());
 
-        IPv4Address addr2 {
+        IPv4Address reverse_addr {
             (byte)atoi(sections[3].characters()),
             (byte)atoi(sections[2].characters()),
             (byte)atoi(sections[1].characters()),
             (byte)atoi(sections[0].characters()),
         };
-        auto sb = StringBuilder();
-        sb.append(addr2.to_string());
-        sb.append(".in-addr.arpa");
-        dns_custom_hostnames.set(sb.to_string(), name);
+        StringBuilder builder;
+        builder.append(reverse_addr.to_string());
+        builder.append(".in-addr.arpa");
+        dns_custom_hostnames.set(builder.to_string(), name);
     }
 
+}
+
+int main(int argc, char** argv)
+{
+    (void)argc;
+    (void)argv;
+
+    unlink("/tmp/.LookupServer-socket");
+
+    auto config = CConfigFile::get_for_system("LookupServer");
+    dbgprintf("LookupServer: Using network config file at %s.\n",
+        config->file_name().characters());
+    auto DNS_IP = config->read_entry("DNS", "IPAddress", "127.0.0.53");
+
+    load_etc_hosts();
+
     int server_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
     if (server_fd < 0) {
         perror("socket");
@@ -145,7 +154,7 @@ int main(int argc, char** argv)
         Vector<String> responses;
 
         for (auto& key : dns_custom_hostnames.keys()) {
-            dbgprintf("Known Hostname: %s\n", key.characters());
+            dbgprintf("Known hostname: '%s'\n", key.characters());
         }
         if (dns_custom_hostnames.contains(hostname)) {
             responses.append(dns_custom_hostnames.get(hostname));