2024-06-12 14:47:58 +08:00
|
|
|
#include "detection/dns/dns.h"
|
|
|
|
|
|
2026-01-06 09:48:38 +08:00
|
|
|
#include "common/io.h"
|
|
|
|
|
#include "common/mallocHelper.h"
|
|
|
|
|
#include "common/stringUtils.h"
|
|
|
|
|
#include "common/debug.h"
|
2024-06-12 14:47:58 +08:00
|
|
|
|
2025-02-05 02:08:28 +01:00
|
|
|
#ifdef __HAIKU__
|
|
|
|
|
#define RESOLV_CONF "/system/settings/network/resolv.conf"
|
|
|
|
|
#else
|
|
|
|
|
#define RESOLV_CONF "/etc/resolv.conf"
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-03-28 09:44:51 +08:00
|
|
|
static const char* detectDnsFromConf(const char* path, FFDNSOptions* options, FFlist* results)
|
2024-06-12 14:47:58 +08:00
|
|
|
{
|
2025-03-28 09:44:51 +08:00
|
|
|
FF_DEBUG("Attempting to read DNS config from %s", path);
|
|
|
|
|
|
|
|
|
|
FF_AUTO_CLOSE_FILE FILE* file = fopen(path, "r");
|
2024-06-12 15:34:27 +08:00
|
|
|
if (!file)
|
2025-03-28 09:44:51 +08:00
|
|
|
{
|
2026-03-26 19:54:10 +08:00
|
|
|
FF_DEBUG("Failed to open %s: %s", path, strerror(errno));
|
2025-03-28 09:44:51 +08:00
|
|
|
return "fopen(path, r) failed";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (results->length > 0)
|
|
|
|
|
{
|
|
|
|
|
FF_DEBUG("Clearing existing DNS entries (%u entries)", results->length);
|
|
|
|
|
FF_LIST_FOR_EACH(FFstrbuf, item, *results)
|
|
|
|
|
ffStrbufDestroy(item);
|
|
|
|
|
ffListClear(results);
|
|
|
|
|
}
|
2024-06-12 14:47:58 +08:00
|
|
|
|
2024-06-12 15:34:27 +08:00
|
|
|
FF_AUTO_FREE char* line = NULL;
|
|
|
|
|
size_t len = 0;
|
2024-06-12 14:47:58 +08:00
|
|
|
|
2024-06-12 15:34:27 +08:00
|
|
|
while (getline(&line, &len, file) != -1)
|
2024-06-12 14:47:58 +08:00
|
|
|
{
|
2024-06-12 19:52:25 +08:00
|
|
|
if (ffStrStartsWith(line, "nameserver"))
|
2024-06-12 15:34:27 +08:00
|
|
|
{
|
2024-10-04 15:58:17 +08:00
|
|
|
char* nameserver = line + strlen("nameserver");
|
2024-06-12 19:52:25 +08:00
|
|
|
while (*nameserver == ' ' || *nameserver == '\t')
|
|
|
|
|
nameserver++;
|
|
|
|
|
if (*nameserver == '\0') continue;
|
|
|
|
|
|
2024-10-04 15:58:17 +08:00
|
|
|
char* comment = strchr(nameserver, '#');
|
|
|
|
|
if (comment) *comment = '\0';
|
|
|
|
|
|
2024-06-12 19:52:25 +08:00
|
|
|
if ((ffStrContainsC(nameserver, ':') && !(options->showType & FF_DNS_TYPE_IPV6_BIT)) ||
|
|
|
|
|
(ffStrContainsC(nameserver, '.') && !(options->showType & FF_DNS_TYPE_IPV4_BIT)))
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-06-12 15:34:27 +08:00
|
|
|
FFstrbuf* item = (FFstrbuf*) ffListAdd(results);
|
2024-06-12 19:52:25 +08:00
|
|
|
ffStrbufInitS(item, nameserver);
|
2024-06-12 15:34:27 +08:00
|
|
|
ffStrbufTrimRightSpace(item);
|
2025-03-28 09:44:51 +08:00
|
|
|
FF_DEBUG("Found DNS server: %s", item->chars);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_DEBUG("Found %u DNS servers in %s", results->length, path);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* ffDetectDNS(FFDNSOptions* options, FFlist* results)
|
|
|
|
|
{
|
|
|
|
|
FF_DEBUG("Starting DNS detection");
|
|
|
|
|
|
|
|
|
|
const char* error = detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT RESOLV_CONF, options, results);
|
|
|
|
|
if (error != NULL)
|
|
|
|
|
{
|
|
|
|
|
FF_DEBUG("Error detecting DNS: %s", error);
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if __linux__ && !__ANDROID__
|
|
|
|
|
// Handle different DNS management services
|
|
|
|
|
if (results->length == 1)
|
|
|
|
|
{
|
2026-01-09 16:18:25 +08:00
|
|
|
const FFstrbuf* firstEntry = FF_LIST_FIRST(FFstrbuf, *results);
|
2025-03-28 09:44:51 +08:00
|
|
|
|
|
|
|
|
if (ffStrbufEqualS(firstEntry, "127.0.0.53"))
|
|
|
|
|
{
|
|
|
|
|
FF_DEBUG("Detected systemd-resolved (127.0.0.53), checking actual DNS servers");
|
|
|
|
|
// Managed by systemd-resolved
|
|
|
|
|
if (detectDnsFromConf("/run/systemd/resolve/resolv.conf", options, results) == NULL)
|
|
|
|
|
return NULL;
|
2024-06-12 15:34:27 +08:00
|
|
|
}
|
2025-03-28 09:44:51 +08:00
|
|
|
else if (ffStrbufEqualS(firstEntry, "127.0.0.1"))
|
|
|
|
|
{
|
|
|
|
|
FF_DEBUG("Detected possible NetworkManager (127.0.0.1), checking actual DNS servers");
|
|
|
|
|
// Managed by NetworkManager
|
|
|
|
|
if (detectDnsFromConf("/var/run/NetworkManager/resolv.conf", options, results) == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check other possible DNS configuration files
|
|
|
|
|
if (results->length == 0)
|
|
|
|
|
{
|
|
|
|
|
FF_DEBUG("No DNS servers found, trying alternative config files");
|
|
|
|
|
|
|
|
|
|
// Try resolvconf
|
|
|
|
|
FF_DEBUG("Trying resolvconf configuration");
|
|
|
|
|
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/run/resolvconf/resolv.conf", options, results) == NULL && results->length > 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
// Try dnsmasq
|
|
|
|
|
FF_DEBUG("Trying dnsmasq configuration");
|
|
|
|
|
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/var/run/dnsmasq/resolv.conf", options, results) == NULL && results->length > 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
// Try openresolv
|
|
|
|
|
FF_DEBUG("Trying openresolv configuration");
|
|
|
|
|
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/etc/resolv.conf.openresolv", options, results) == NULL && results->length > 0)
|
|
|
|
|
return NULL;
|
2024-06-12 14:47:58 +08:00
|
|
|
}
|
2025-03-28 09:44:51 +08:00
|
|
|
#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
|
// Handle BSD-specific DNS configurations
|
|
|
|
|
if (results->length == 0)
|
|
|
|
|
{
|
|
|
|
|
FF_DEBUG("No DNS servers found, trying BSD-specific config files");
|
|
|
|
|
|
|
|
|
|
// FreeBSD and other BSDs may use resolvconf service
|
|
|
|
|
FF_DEBUG("Trying BSD resolvconf configuration");
|
|
|
|
|
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/var/run/resolvconf/resolv.conf", options, results) == NULL && results->length > 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
// Some BSDs store DNS configuration here
|
|
|
|
|
FF_DEBUG("Trying BSD nameserver configuration");
|
|
|
|
|
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/var/run/nameserver", options, results) == NULL && results->length > 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
// Try common BSD paths
|
|
|
|
|
FF_DEBUG("Trying BSD common paths");
|
|
|
|
|
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/etc/nameserver", options, results) == NULL && results->length > 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
FF_DEBUG("DNS detection completed with %u servers found", results->length);
|
2024-06-12 14:47:58 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|