summaryrefslogtreecommitdiff
path: root/data/libevent/darwindns.diff
blob: e751e180556fec78f1023ed72c5f8e71d6efd953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
diff -ur libevent-2.1.8-stable/evdns.c libevent-2.1.8-stable+ios/evdns.c
--- libevent-2.1.8-stable/evdns.c	2017-01-29 07:51:00.000000000 -1000
+++ libevent-2.1.8-stable+ios/evdns.c	2019-10-29 14:49:24.000000000 -1000
@@ -3896,6 +3896,76 @@
 }
 #endif
 
+#ifdef __APPLE__
+#include <dnsinfo.h>
+
+int
+load_nameservers_from_systemconfiguration(struct evdns_base *base)
+{
+	int found = 0;
+
+	ASSERT_LOCKED(base);
+
+	dns_config_t *dns_config = dns_configuration_copy();
+	if (!dns_config) return -1;
+
+	if (dns_config->n_resolver > 0) {
+		for (int i=0; i<dns_config->n_resolver; i++) {
+			// This should probably support domain specific servers but idk how
+			if (dns_config->resolver[i]->domain == NULL) {
+				char buf[1024];
+				for (int j=0; j<dns_config->resolver[i]->n_nameserver; j++) {
+					struct sockaddr *nameserver = dns_config->resolver[i]->nameserver[j];
+					const char *ns = NULL;
+					if (nameserver->sa_family == AF_INET) {
+						ns = inet_ntop(nameserver->sa_family, &((struct sockaddr_in*)nameserver)->sin_addr, buf, 1024);
+					} else if (nameserver->sa_family == AF_INET6) {
+						ns = inet_ntop(nameserver->sa_family, &((struct sockaddr_in6*)nameserver)->sin6_addr, buf, 1024);
+					}
+					if (ns && evdns_base_nameserver_ip_add(base, ns)) {
+						found=1;
+					}
+				}
+			}
+		}
+	}
+	free(dns_config);
+
+	if (found) {
+		log(EVDNS_LOG_DEBUG,"Found nameservers in systemconfiguration");
+	} else {
+		log(EVDNS_LOG_DEBUG,"Didn't find nameservers in systemconfiguration");
+	}
+
+	return found ? 0 : -1;
+}
+
+int
+evdns_base_config_darwin_nameservers(struct evdns_base *base)
+{
+	if (base == NULL)
+		base = current_base;
+	if (base == NULL)
+		return -1;
+	EVDNS_LOCK(base);
+	int r = evdns_base_resolv_conf_parse_impl(base, DNS_OPTIONS_ALL, "/etc/resolv.conf");
+	int r2 = load_nameservers_from_systemconfiguration(base);
+	EVDNS_UNLOCK(base);
+	return (r==-1 && r2==-1)?-1:0;
+}
+
+int
+evdns_config_darwin_nameservers(void)
+{
+	if (!current_base) {
+		current_base = evdns_base_new(NULL, 1);
+		return current_base == NULL ? -1 : 0;
+	} else {
+		return evdns_base_config_darwin_nameservers(current_base);
+	}
+}
+#endif
+
 struct evdns_base *
 evdns_base_new(struct event_base *event_base, int flags)
 {
@@ -3960,7 +4030,11 @@
 #ifdef _WIN32
 		r = evdns_base_config_windows_nameservers(base);
 #else
+#ifdef __APPLE__
+		r = evdns_base_config_darwin_nameservers(base);
+#else
 		r = evdns_base_resolv_conf_parse(base, DNS_OPTIONS_ALL, "/etc/resolv.conf");
+#endif // __APPLE__
 #endif
 		if (r == -1) {
 			evdns_base_free_and_unlock(base, 0);
diff -ur libevent-2.1.8-stable/include/event2/dns_compat.h libevent-2.1.8-stable+ios/include/event2/dns_compat.h
--- libevent-2.1.8-stable/include/event2/dns_compat.h	2016-10-04 09:55:31.000000000 -1000
+++ libevent-2.1.8-stable+ios/include/event2/dns_compat.h	2019-10-29 14:44:10.000000000 -1000
@@ -329,6 +329,12 @@
 #define EVDNS_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED
 #endif
 
+#ifdef __APPLE__
+int load_nameservers_from_systemconfiguration(struct evdns_base *base);
+int evdns_base_config_darwin_nameservers(struct evdns_base *base);
+int evdns_config_darwin_nameservers(void);
+#endif
+
 #ifdef __cplusplus
 }
 #endif