5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #define RESOLVER_FLAG_PUBLIC (1 << 0)
41 * Threshold for RDNSS lifetime. Will be used to trigger RS
42 * before RDNSS entries actually expire
44 #define RESOLVER_LIFETIME_REFRESH_THRESHOLD 0.8
52 unsigned int lifetime;
56 static GSList *entry_list = NULL;
57 static bool dnsproxy_enabled = false;
59 struct resolvfile_entry {
65 static GList *resolvfile_list = NULL;
67 static void resolvfile_remove_entries(GList *entries)
71 for (list = entries; list; list = list->next) {
72 struct resolvfile_entry *entry = list->data;
74 resolvfile_list = g_list_remove(resolvfile_list, entry);
76 g_free(entry->server);
77 g_free(entry->domain);
84 static int resolvfile_export(void)
92 content = g_string_new("# Generated by Connection Manager\n");
95 * Domains and nameservers are added in reverse so that the most
96 * recently appended entry is the primary one. No more than
97 * MAXDNSRCH/MAXNS entries are used.
100 for (count = 0, list = g_list_last(resolvfile_list);
101 list && (count < MAXDNSRCH);
102 list = g_list_previous(list)) {
103 struct resolvfile_entry *entry = list->data;
109 g_string_append_printf(content, "search ");
111 g_string_append_printf(content, "%s ", entry->domain);
116 g_string_append_printf(content, "\n");
118 for (count = 0, list = g_list_last(resolvfile_list);
119 list && (count < MAXNS);
120 list = g_list_previous(list)) {
121 struct resolvfile_entry *entry = list->data;
126 g_string_append_printf(content, "nameserver %s\n",
131 old_umask = umask(022);
133 fd = open("/etc/resolv.conf", O_RDWR | O_CREAT | O_CLOEXEC,
134 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
140 if (ftruncate(fd, 0) < 0) {
147 if (write(fd, content->str, content->len) < 0)
154 g_string_free(content, TRUE);
160 int __connman_resolvfile_append(int index, const char *domain,
163 struct resolvfile_entry *entry;
165 DBG("index %d server %s", index, server);
170 entry = g_try_new0(struct resolvfile_entry, 1);
174 entry->index = index;
175 entry->domain = g_strdup(domain);
176 entry->server = g_strdup(server);
178 resolvfile_list = g_list_append(resolvfile_list, entry);
180 return resolvfile_export();
183 int __connman_resolvfile_remove(int index, const char *domain,
186 GList *list, *matches = NULL;
188 DBG("index %d server %s", index, server);
190 for (list = resolvfile_list; list; list = g_list_next(list)) {
191 struct resolvfile_entry *entry = list->data;
193 if (index >= 0 && entry->index != index)
196 if (domain && g_strcmp0(entry->domain, domain) != 0)
199 if (g_strcmp0(entry->server, server) != 0)
202 matches = g_list_append(matches, entry);
205 resolvfile_remove_entries(matches);
207 return resolvfile_export();
210 static void append_fallback_nameservers(void)
214 for (list = entry_list; list; list = list->next) {
215 struct entry_data *entry = list->data;
217 if (entry->index >= 0 && entry->server)
221 for (list = entry_list; list; list = list->next) {
222 struct entry_data *entry = list->data;
224 if (entry->index != -1 || !entry->server)
227 DBG("index %d server %s", entry->index, entry->server);
229 if (dnsproxy_enabled) {
230 __connman_dnsproxy_append(entry->index, entry->domain,
233 __connman_resolvfile_append(entry->index,
234 entry->domain, entry->server);
239 static void remove_fallback_nameservers(void)
243 for (list = entry_list; list; list = list->next) {
244 struct entry_data *entry = list->data;
246 if (entry->index >= 0 || !entry->server)
249 DBG("index %d server %s", entry->index, entry->server);
251 if (dnsproxy_enabled) {
252 __connman_dnsproxy_remove(entry->index, entry->domain,
255 __connman_resolvfile_remove(entry->index,
256 entry->domain, entry->server);
261 static void remove_entries(GSList *entries)
265 for (list = entries; list; list = list->next) {
266 struct entry_data *entry = list->data;
268 entry_list = g_slist_remove(entry_list, entry);
270 if (dnsproxy_enabled) {
271 __connman_dnsproxy_remove(entry->index, entry->domain,
274 __connman_resolvfile_remove(entry->index, entry->domain,
279 g_source_remove(entry->timeout);
280 g_free(entry->server);
281 g_free(entry->domain);
285 g_slist_free(entries);
287 append_fallback_nameservers();
290 static gboolean resolver_expire_cb(gpointer user_data)
292 struct entry_data *entry = user_data;
295 DBG("index %d domain %s server %s",
296 entry->index, entry->domain, entry->server);
298 list = g_slist_prepend(NULL, entry);
300 if (entry->index >= 0) {
301 struct connman_service *service;
302 service = __connman_service_lookup_from_index(entry->index);
304 __connman_service_nameserver_remove(service,
305 entry->server, true);
308 remove_entries(list);
313 static gboolean resolver_refresh_cb(gpointer user_data)
315 struct entry_data *entry = user_data;
316 unsigned int interval;
317 struct connman_service *service = NULL;
319 /* Round up what we have left from lifetime */
320 interval = entry->lifetime *
321 (1 - RESOLVER_LIFETIME_REFRESH_THRESHOLD) + 1.0;
323 DBG("RDNSS start index %d domain %s "
324 "server %s remaining lifetime %d",
325 entry->index, entry->domain,
326 entry->server, interval);
328 entry->timeout = g_timeout_add_seconds(interval,
329 resolver_expire_cb, entry);
331 if (entry->index >= 0) {
332 service = __connman_service_lookup_from_index(entry->index);
335 * Send Router Solicitation to refresh RDNSS entries
336 * before their lifetime expires
338 __connman_network_refresh_rs_ipv6(
339 __connman_service_get_network(service),
346 static int append_resolver(int index, const char *domain,
347 const char *server, unsigned int lifetime,
350 struct entry_data *entry;
351 unsigned int interval;
353 DBG("index %d domain %s server %s lifetime %d flags %d",
354 index, domain, server, lifetime, flags);
356 if (!server && !domain)
360 if (g_strcmp0(server, "0.0.0.0") == 0)
364 entry = g_try_new0(struct entry_data, 1);
368 entry->index = index;
369 entry->domain = g_strdup(domain);
370 entry->server = g_strdup(server);
371 entry->flags = flags;
372 entry->lifetime = lifetime;
375 entry->family = connman_inet_check_ipaddress(server);
378 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
380 DBG("RDNSS start index %d domain %s "
381 "server %s lifetime threshold %d",
382 index, domain, server, interval);
384 entry->timeout = g_timeout_add_seconds(interval,
385 resolver_refresh_cb, entry);
388 * We update the service only for those nameservers
389 * that are automagically added via netlink (lifetime > 0)
391 if (server && entry->index >= 0) {
392 struct connman_service *service;
393 service = __connman_service_lookup_from_index(entry->index);
395 __connman_service_nameserver_append(service,
400 if (entry->index >= 0 && entry->server)
401 remove_fallback_nameservers();
403 entry_list = g_slist_append(entry_list, entry);
405 if (dnsproxy_enabled)
406 __connman_dnsproxy_append(entry->index, domain, server);
408 __connman_resolvfile_append(entry->index, domain, server);
414 * connman_resolver_append:
415 * @index: network interface index
416 * @domain: domain limitation
417 * @server: server address
419 * Append resolver server address to current list
421 int connman_resolver_append(int index, const char *domain,
426 DBG("index %d domain %s server %s", index, domain, server);
428 if (!server && !domain)
431 for (list = entry_list; list; list = list->next) {
432 struct entry_data *entry = list->data;
434 if (entry->timeout > 0)
437 if (entry->index == index &&
438 g_strcmp0(entry->domain, domain) == 0 &&
439 g_strcmp0(entry->server, server) == 0) {
440 if (dnsproxy_enabled)
441 __connman_dnsproxy_append(entry->index, domain,
448 return append_resolver(index, domain, server, 0, 0);
452 * connman_resolver_append_lifetime:
453 * @index: network interface index
454 * @domain: domain limitation
455 * @server: server address
456 * @timeout: server lifetime in seconds
458 * Append resolver server address to current list
460 int connman_resolver_append_lifetime(int index, const char *domain,
461 const char *server, unsigned int lifetime)
464 unsigned int interval;
466 DBG("index %d domain %s server %s lifetime %d",
467 index, domain, server, lifetime);
469 if (!server && !domain)
472 for (list = entry_list; list; list = list->next) {
473 struct entry_data *entry = list->data;
475 if (entry->timeout == 0 ||
476 entry->index != index ||
477 g_strcmp0(entry->domain, domain) != 0 ||
478 g_strcmp0(entry->server, server) != 0)
481 g_source_remove(entry->timeout);
484 resolver_expire_cb(entry);
488 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
490 DBG("RDNSS start index %d domain %s "
491 "server %s lifetime threshold %d",
492 index, domain, server, interval);
494 entry->timeout = g_timeout_add_seconds(interval,
495 resolver_refresh_cb, entry);
499 return append_resolver(index, domain, server, lifetime, 0);
503 * connman_resolver_remove:
504 * @index: network interface index
505 * @domain: domain limitation
506 * @server: server address
508 * Remover resolver server address from current list
510 int connman_resolver_remove(int index, const char *domain, const char *server)
512 GSList *list, *matches = NULL;
514 DBG("index %d domain %s server %s", index, domain, server);
516 for (list = entry_list; list; list = list->next) {
517 struct entry_data *entry = list->data;
519 if (entry->index != index)
522 if (g_strcmp0(entry->domain, domain) != 0)
525 if (g_strcmp0(entry->server, server) != 0)
528 matches = g_slist_prepend(matches, entry);
535 remove_entries(matches);
541 * connman_resolver_remove_all:
542 * @index: network interface index
544 * Remove all resolver server address for the specified interface index
546 int connman_resolver_remove_all(int index)
548 GSList *list, *matches = NULL;
550 DBG("index %d", index);
555 for (list = entry_list; list; list = list->next) {
556 struct entry_data *entry = list->data;
558 if (entry->index != index)
561 matches = g_slist_prepend(matches, entry);
567 remove_entries(matches);
572 int __connman_resolver_redo_servers(int index)
576 if (!dnsproxy_enabled)
579 DBG("index %d", index);
584 for (list = entry_list; list; list = list->next) {
585 struct entry_data *entry = list->data;
587 if (entry->timeout == 0 || entry->index != index)
591 * This function must only check IPv6 server addresses so
592 * do not remove IPv4 name servers unnecessarily.
594 if (entry->family != AF_INET6)
598 * We remove the server, and then re-create so that it will
599 * use proper source addresses when sending DNS queries.
601 __connman_dnsproxy_remove(entry->index, entry->domain,
604 __connman_dnsproxy_append(entry->index, entry->domain,
611 static void free_entry(gpointer data)
613 struct entry_data *entry = data;
614 g_free(entry->domain);
615 g_free(entry->server);
619 static void free_resolvfile(gpointer data)
621 struct resolvfile_entry *entry = data;
622 g_free(entry->domain);
623 g_free(entry->server);
627 int __connman_resolver_init(gboolean dnsproxy)
632 DBG("dnsproxy %d", dnsproxy);
637 if (__connman_dnsproxy_init() < 0) {
638 /* Fall back to resolv.conf */
642 dnsproxy_enabled = true;
644 ns = connman_setting_get_string_list("FallbackNameservers");
645 for (i = 0; ns && ns[i]; i += 1) {
646 DBG("server %s", ns[i]);
647 append_resolver(-1, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
653 void __connman_resolver_cleanup(void)
657 if (dnsproxy_enabled)
658 __connman_dnsproxy_cleanup();
663 for (list = resolvfile_list; list; list = g_list_next(list))
664 free_resolvfile(list->data);
665 g_list_free(resolvfile_list);
666 resolvfile_list = NULL;
668 for (slist = entry_list; slist; slist = g_slist_next(slist))
669 free_entry(slist->data);
670 g_slist_free(entry_list);