5 * Copyright (C) 2007-2012 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 connman_bool_t 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);
78 g_free(entry->interface);
85 static int resolvfile_export(void)
93 content = g_string_new("# Generated by Connection Manager\n");
96 * Domains and nameservers are added in reverse so that the most
97 * recently appended entry is the primary one. No more than
98 * MAXDNSRCH/MAXNS entries are used.
101 for (count = 0, list = g_list_last(resolvfile_list);
102 list && (count < MAXDNSRCH);
103 list = g_list_previous(list)) {
104 struct resolvfile_entry *entry = list->data;
110 g_string_append_printf(content, "search ");
112 g_string_append_printf(content, "%s ", entry->domain);
117 g_string_append_printf(content, "\n");
119 for (count = 0, list = g_list_last(resolvfile_list);
120 list && (count < MAXNS);
121 list = g_list_previous(list)) {
122 struct resolvfile_entry *entry = list->data;
127 g_string_append_printf(content, "nameserver %s\n",
132 old_umask = umask(022);
134 fd = open("/etc/resolv.conf", O_RDWR | O_CREAT | O_CLOEXEC,
135 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
141 if (ftruncate(fd, 0) < 0) {
148 if (write(fd, content->str, content->len) < 0)
155 g_string_free(content, TRUE);
161 int __connman_resolvfile_append(const char *interface, const char *domain,
164 struct resolvfile_entry *entry;
166 DBG("interface %s server %s", interface, server);
168 if (interface == NULL)
171 entry = g_try_new0(struct resolvfile_entry, 1);
175 entry->interface = g_strdup(interface);
176 entry->domain = g_strdup(domain);
177 entry->server = g_strdup(server);
179 resolvfile_list = g_list_append(resolvfile_list, entry);
181 return resolvfile_export();
184 int __connman_resolvfile_remove(const char *interface, const char *domain,
187 GList *list, *matches = NULL;
189 DBG("interface %s server %s", interface, server);
191 for (list = resolvfile_list; list; list = g_list_next(list)) {
192 struct resolvfile_entry *entry = list->data;
194 if (interface != NULL &&
195 g_strcmp0(entry->interface, interface) != 0)
198 if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
201 if (g_strcmp0(entry->server, server) != 0)
204 matches = g_list_append(matches, entry);
207 resolvfile_remove_entries(matches);
209 return resolvfile_export();
212 static void remove_entries(GSList *entries)
216 for (list = entries; list; list = list->next) {
217 struct entry_data *entry = list->data;
219 entry_list = g_slist_remove(entry_list, entry);
221 if (dnsproxy_enabled == TRUE) {
222 __connman_dnsproxy_remove(entry->interface, entry->domain,
225 __connman_resolvfile_remove(entry->interface, entry->domain,
230 g_source_remove(entry->timeout);
231 g_free(entry->server);
232 g_free(entry->domain);
233 g_free(entry->interface);
237 g_slist_free(entries);
240 static gboolean resolver_expire_cb(gpointer user_data)
242 struct entry_data *entry = user_data;
246 DBG("interface %s domain %s server %s",
247 entry->interface, entry->domain, entry->server);
249 list = g_slist_prepend(NULL, entry);
251 index = connman_inet_ifindex(entry->interface);
253 struct connman_service *service;
254 service = __connman_service_lookup_from_index(index);
256 __connman_service_nameserver_remove(service,
257 entry->server, TRUE);
260 remove_entries(list);
265 static gboolean resolver_refresh_cb(gpointer user_data)
267 struct entry_data *entry = user_data;
269 unsigned int interval;
270 struct connman_service *service = NULL;
272 /* Round up what we have left from lifetime */
273 interval = entry->lifetime *
274 (1 - RESOLVER_LIFETIME_REFRESH_THRESHOLD) + 1.0;
276 DBG("RDNSS start interface %s domain %s "
277 "server %s remaining lifetime %d",
278 entry->interface, entry->domain,
279 entry->server, interval);
281 entry->timeout = g_timeout_add_seconds(interval,
282 resolver_expire_cb, entry);
284 index = connman_inet_ifindex(entry->interface);
286 service = __connman_service_lookup_from_index(index);
287 if (service != NULL) {
289 * Send Router Solicitation to refresh RDNSS entries
290 * before their lifetime expires
292 __connman_refresh_rs_ipv6(
293 __connman_service_get_network(service),
300 static int append_resolver(const char *interface, const char *domain,
301 const char *server, unsigned int lifetime,
304 struct entry_data *entry;
305 unsigned int interval;
307 DBG("interface %s domain %s server %s lifetime %d flags %d",
308 interface, domain, server, lifetime, flags);
310 if (server == NULL && domain == NULL)
313 entry = g_try_new0(struct entry_data, 1);
317 entry->interface = g_strdup(interface);
318 entry->domain = g_strdup(domain);
319 entry->server = g_strdup(server);
320 entry->flags = flags;
321 entry->lifetime = lifetime;
324 entry->family = connman_inet_check_ipaddress(server);
328 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
330 DBG("RDNSS start interface %s domain %s "
331 "server %s lifetime threshold %d",
332 interface, domain, server, interval);
334 entry->timeout = g_timeout_add_seconds(interval,
335 resolver_refresh_cb, entry);
338 * We update the service only for those nameservers
339 * that are automagically added via netlink (lifetime > 0)
341 index = connman_inet_ifindex(interface);
342 if (server != NULL && index >= 0) {
343 struct connman_service *service;
344 service = __connman_service_lookup_from_index(index);
346 __connman_service_nameserver_append(service,
350 entry_list = g_slist_append(entry_list, entry);
352 if (dnsproxy_enabled == TRUE)
353 __connman_dnsproxy_append(interface, domain, server);
355 __connman_resolvfile_append(interface, domain, server);
361 * connman_resolver_append:
362 * @interface: network interface
363 * @domain: domain limitation
364 * @server: server address
366 * Append resolver server address to current list
368 int connman_resolver_append(const char *interface, const char *domain,
373 DBG("interface %s domain %s server %s", interface, domain, server);
375 if (server == NULL && domain == NULL)
378 for (list = entry_list; list; list = list->next) {
379 struct entry_data *entry = list->data;
381 if (entry->timeout > 0)
384 if (g_strcmp0(entry->interface, interface) == 0 &&
385 g_strcmp0(entry->domain, domain) == 0 &&
386 g_strcmp0(entry->server, server) == 0)
390 return append_resolver(interface, domain, server, 0, 0);
394 * connman_resolver_append_lifetime:
395 * @interface: network interface
396 * @domain: domain limitation
397 * @server: server address
398 * @timeout: server lifetime in seconds
400 * Append resolver server address to current list
402 int connman_resolver_append_lifetime(const char *interface, const char *domain,
403 const char *server, unsigned int lifetime)
406 unsigned int interval;
408 DBG("interface %s domain %s server %s lifetime %d",
409 interface, domain, server, lifetime);
411 if (server == NULL && domain == NULL)
414 for (list = entry_list; list; list = list->next) {
415 struct entry_data *entry = list->data;
417 if (entry->timeout == 0 ||
418 g_strcmp0(entry->interface, interface) != 0 ||
419 g_strcmp0(entry->domain, domain) != 0 ||
420 g_strcmp0(entry->server, server) != 0)
423 g_source_remove(entry->timeout);
426 resolver_expire_cb(entry);
430 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
432 DBG("RDNSS start interface %s domain %s "
433 "server %s lifetime threshold %d",
434 interface, domain, server, interval);
436 entry->timeout = g_timeout_add_seconds(interval,
437 resolver_refresh_cb, entry);
441 return append_resolver(interface, domain, server, lifetime, 0);
445 * connman_resolver_remove:
446 * @interface: network interface
447 * @domain: domain limitation
448 * @server: server address
450 * Remover resolver server address from current list
452 int connman_resolver_remove(const char *interface, const char *domain,
455 GSList *list, *matches = NULL;
457 DBG("interface %s domain %s server %s", interface, domain, server);
459 for (list = entry_list; list; list = list->next) {
460 struct entry_data *entry = list->data;
462 if (g_strcmp0(entry->interface, interface) != 0)
465 if (g_strcmp0(entry->domain, domain) != 0)
468 if (g_strcmp0(entry->server, server) != 0)
471 matches = g_slist_prepend(matches, entry);
478 remove_entries(matches);
484 * connman_resolver_remove_all:
485 * @interface: network interface
487 * Remove all resolver server address for the specified interface
489 int connman_resolver_remove_all(const char *interface)
491 GSList *list, *matches = NULL;
493 DBG("interface %s", interface);
495 if (interface == NULL)
498 for (list = entry_list; list; list = list->next) {
499 struct entry_data *entry = list->data;
501 if (g_strcmp0(entry->interface, interface) != 0)
504 matches = g_slist_prepend(matches, entry);
510 remove_entries(matches);
516 * connman_resolver_flush:
518 * Flush pending resolver requests
520 void connman_resolver_flush(void)
522 if (dnsproxy_enabled == TRUE)
523 __connman_dnsproxy_flush();
528 int __connman_resolver_redo_servers(const char *interface)
532 if (dnsproxy_enabled == FALSE)
535 DBG("interface %s", interface);
537 if (interface == NULL)
540 for (list = entry_list; list; list = list->next) {
541 struct entry_data *entry = list->data;
543 if (entry->timeout == 0 ||
544 g_strcmp0(entry->interface, interface) != 0)
548 * This function must only check IPv6 server addresses so
549 * do not remove IPv4 name servers unnecessarily.
551 if (entry->family != AF_INET6)
555 * We remove the server, and then re-create so that it will
556 * use proper source addresses when sending DNS queries.
558 __connman_dnsproxy_remove(entry->interface, entry->domain,
561 * Remove also the resolver timer for the old server entry.
562 * A new timer will be set for the new server entry
563 * when the next Router Advertisement message arrives
564 * with RDNSS/DNSSL settings.
566 g_source_remove(entry->timeout);
568 __connman_dnsproxy_append(entry->interface, entry->domain,
575 static void free_entry(gpointer data)
577 struct entry_data *entry = data;
578 g_free(entry->interface);
579 g_free(entry->domain);
580 g_free(entry->server);
584 static void free_resolvfile(gpointer data)
586 struct resolvfile_entry *entry = data;
587 g_free(entry->interface);
588 g_free(entry->domain);
589 g_free(entry->server);
593 int __connman_resolver_init(connman_bool_t dnsproxy)
598 DBG("dnsproxy %d", dnsproxy);
600 if (dnsproxy == FALSE)
603 if (__connman_dnsproxy_init() < 0) {
604 /* Fall back to resolv.conf */
608 dnsproxy_enabled = TRUE;
610 ns = connman_setting_get_string_list("FallbackNameservers");
611 for (i = 0; ns != NULL && ns[i] != NULL; i += 1) {
612 DBG("server %s", ns[i]);
613 append_resolver(NULL, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
619 void __connman_resolver_cleanup(void)
623 if (dnsproxy_enabled == TRUE)
624 __connman_dnsproxy_cleanup();
629 for (list = resolvfile_list; list; list = g_list_next(list))
630 free_resolvfile(list->data);
631 g_list_free(resolvfile_list);
632 resolvfile_list = NULL;
634 for (slist = entry_list; slist; slist = g_slist_next(slist))
635 free_entry(slist->data);
636 g_slist_free(entry_list);