Files
fastfetch/src/detection/dns/dns_linux.c
T

144 lines
5.1 KiB
C
Raw Normal View History

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"
2025-02-05 02:08:28 +01:00
#else
#define RESOLV_CONF "/etc/resolv.conf"
2025-02-05 02:08:28 +01:00
#endif
2026-03-29 09:28:49 +08:00
static const char* detectDnsFromConf(const char* path, FFDNSOptions* options, FFlist* results) {
FF_DEBUG("Attempting to read DNS config from %s", path);
FF_AUTO_CLOSE_FILE FILE* file = fopen(path, "r");
2026-03-29 09:28:49 +08:00
if (!file) {
FF_DEBUG("Failed to open %s: %s", path, strerror(errno));
return "fopen(path, r) failed";
}
2026-03-29 09:28:49 +08:00
if (results->length > 0) {
FF_DEBUG("Clearing existing DNS entries (%u entries)", results->length);
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFstrbuf, item, *results) {
ffStrbufDestroy(item);
2026-03-29 09:28:49 +08:00
}
ffListClear(results);
}
2024-06-12 14:47:58 +08:00
FF_AUTO_FREE char* line = NULL;
size_t len = 0;
2024-06-12 14:47:58 +08:00
2026-03-29 09:28:49 +08:00
while (getline(&line, &len, file) != -1) {
if (ffStrStartsWith(line, "nameserver")) {
2024-10-04 15:58:17 +08:00
char* nameserver = line + strlen("nameserver");
2026-03-29 09:28:49 +08:00
while (*nameserver == ' ' || *nameserver == '\t') {
2024-06-12 19:52:25 +08:00
nameserver++;
2026-03-29 09:28:49 +08:00
}
if (*nameserver == '\0') {
continue;
}
2024-06-12 19:52:25 +08:00
2024-10-04 15:58:17 +08:00
char* comment = strchr(nameserver, '#');
2026-03-29 09:28:49 +08:00
if (comment) {
*comment = '\0';
}
2024-10-04 15:58:17 +08:00
2024-06-12 19:52:25 +08:00
if ((ffStrContainsC(nameserver, ':') && !(options->showType & FF_DNS_TYPE_IPV6_BIT)) ||
2026-03-29 09:28:49 +08:00
(ffStrContainsC(nameserver, '.') && !(options->showType & FF_DNS_TYPE_IPV4_BIT))) {
2024-06-12 19:52:25 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-06-12 19:52:25 +08:00
2026-04-14 10:41:18 +08:00
FFstrbuf* item = FF_LIST_ADD(FFstrbuf, *results);
2024-06-12 19:52:25 +08:00
ffStrbufInitS(item, nameserver);
ffStrbufTrimRightSpace(item);
FF_DEBUG("Found DNS server: %s", item->chars);
}
}
FF_DEBUG("Found %u DNS servers in %s", results->length, path);
return NULL;
}
2026-03-29 09:28:49 +08:00
const char* ffDetectDNS(FFDNSOptions* options, FFlist* results) {
FF_DEBUG("Starting DNS detection");
const char* error = detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT RESOLV_CONF, options, results);
2026-03-29 09:28:49 +08:00
if (error != NULL) {
FF_DEBUG("Error detecting DNS: %s", error);
return error;
}
2026-03-29 09:28:49 +08:00
#if __linux__ && !__ANDROID__
// Handle different DNS management services
2026-03-29 09:28:49 +08:00
if (results->length == 1) {
const FFstrbuf* firstEntry = FF_LIST_FIRST(FFstrbuf, *results);
2026-03-29 09:28:49 +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
2026-03-29 09:28:49 +08:00
if (detectDnsFromConf("/run/systemd/resolve/resolv.conf", options, results) == NULL) {
return NULL;
2026-03-29 09:28:49 +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
2026-03-29 09:28:49 +08:00
if (detectDnsFromConf("/var/run/NetworkManager/resolv.conf", options, results) == NULL) {
return NULL;
2026-03-29 09:28:49 +08:00
}
}
}
// Check other possible DNS configuration files
2026-03-29 09:28:49 +08:00
if (results->length == 0) {
FF_DEBUG("No DNS servers found, trying alternative config files");
// Try resolvconf
FF_DEBUG("Trying resolvconf configuration");
2026-03-29 09:28:49 +08:00
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/run/resolvconf/resolv.conf", options, results) == NULL && results->length > 0) {
return NULL;
2026-03-29 09:28:49 +08:00
}
// Try dnsmasq
FF_DEBUG("Trying dnsmasq configuration");
2026-03-29 09:28:49 +08:00
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/var/run/dnsmasq/resolv.conf", options, results) == NULL && results->length > 0) {
return NULL;
2026-03-29 09:28:49 +08:00
}
// Try openresolv
FF_DEBUG("Trying openresolv configuration");
2026-03-29 09:28:49 +08:00
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/etc/resolv.conf.openresolv", options, results) == NULL && results->length > 0) {
return NULL;
2026-03-29 09:28:49 +08:00
}
2024-06-12 14:47:58 +08:00
}
2026-03-29 09:28:49 +08:00
#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
// Handle BSD-specific DNS configurations
2026-03-29 09:28:49 +08:00
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");
2026-03-29 09:28:49 +08:00
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/var/run/resolvconf/resolv.conf", options, results) == NULL && results->length > 0) {
return NULL;
2026-03-29 09:28:49 +08:00
}
// Some BSDs store DNS configuration here
FF_DEBUG("Trying BSD nameserver configuration");
2026-03-29 09:28:49 +08:00
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/var/run/nameserver", options, results) == NULL && results->length > 0) {
return NULL;
2026-03-29 09:28:49 +08:00
}
// Try common BSD paths
FF_DEBUG("Trying BSD common paths");
2026-03-29 09:28:49 +08:00
if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/etc/nameserver", options, results) == NULL && results->length > 0) {
return NULL;
2026-03-29 09:28:49 +08:00
}
}
2026-03-29 09:28:49 +08:00
#endif
FF_DEBUG("DNS detection completed with %u servers found", results->length);
2024-06-12 14:47:58 +08:00
return NULL;
}