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);
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 != NULL && 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 remove_entries(GSList *entries)
214 for (list = entries; list; list = list->next) {
215 struct entry_data *entry = list->data;
217 entry_list = g_slist_remove(entry_list, entry);
219 if (dnsproxy_enabled == TRUE) {
220 __connman_dnsproxy_remove(entry->index, entry->domain,
223 __connman_resolvfile_remove(entry->index, entry->domain,
228 g_source_remove(entry->timeout);
229 g_free(entry->server);
230 g_free(entry->domain);
234 g_slist_free(entries);
237 static gboolean resolver_expire_cb(gpointer user_data)
239 struct entry_data *entry = user_data;
242 DBG("index %d domain %s server %s",
243 entry->index, entry->domain, entry->server);
245 list = g_slist_prepend(NULL, entry);
247 if (entry->index >= 0) {
248 struct connman_service *service;
249 service = __connman_service_lookup_from_index(entry->index);
251 __connman_service_nameserver_remove(service,
252 entry->server, TRUE);
255 remove_entries(list);
260 static gboolean resolver_refresh_cb(gpointer user_data)
262 struct entry_data *entry = user_data;
263 unsigned int interval;
264 struct connman_service *service = NULL;
266 /* Round up what we have left from lifetime */
267 interval = entry->lifetime *
268 (1 - RESOLVER_LIFETIME_REFRESH_THRESHOLD) + 1.0;
270 DBG("RDNSS start index %d domain %s "
271 "server %s remaining lifetime %d",
272 entry->index, entry->domain,
273 entry->server, interval);
275 entry->timeout = g_timeout_add_seconds(interval,
276 resolver_expire_cb, entry);
278 if (entry->index >= 0) {
279 service = __connman_service_lookup_from_index(entry->index);
280 if (service != NULL) {
282 * Send Router Solicitation to refresh RDNSS entries
283 * before their lifetime expires
285 __connman_refresh_rs_ipv6(
286 __connman_service_get_network(service),
293 static int append_resolver(int index, const char *domain,
294 const char *server, unsigned int lifetime,
297 struct entry_data *entry;
298 unsigned int interval;
300 DBG("index %d domain %s server %s lifetime %d flags %d",
301 index, domain, server, lifetime, flags);
303 if (server == NULL && domain == NULL)
306 entry = g_try_new0(struct entry_data, 1);
310 entry->index = index;
311 entry->domain = g_strdup(domain);
312 entry->server = g_strdup(server);
313 entry->flags = flags;
314 entry->lifetime = lifetime;
317 entry->family = connman_inet_check_ipaddress(server);
320 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
322 DBG("RDNSS start index %d domain %s "
323 "server %s lifetime threshold %d",
324 index, domain, server, interval);
326 entry->timeout = g_timeout_add_seconds(interval,
327 resolver_refresh_cb, entry);
330 * We update the service only for those nameservers
331 * that are automagically added via netlink (lifetime > 0)
333 if (server != NULL && entry->index >= 0) {
334 struct connman_service *service;
335 service = __connman_service_lookup_from_index(entry->index);
337 __connman_service_nameserver_append(service,
341 entry_list = g_slist_append(entry_list, entry);
343 if (dnsproxy_enabled == TRUE)
344 __connman_dnsproxy_append(entry->index, domain, server);
346 __connman_resolvfile_append(entry->index, domain, server);
352 * connman_resolver_append:
353 * @index: network interface index
354 * @domain: domain limitation
355 * @server: server address
357 * Append resolver server address to current list
359 int connman_resolver_append(int index, const char *domain,
364 DBG("index %d domain %s server %s", index, domain, server);
366 if (server == NULL && domain == NULL)
369 for (list = entry_list; list; list = list->next) {
370 struct entry_data *entry = list->data;
372 if (entry->timeout > 0)
375 if (entry->index == index &&
376 g_strcmp0(entry->domain, domain) == 0 &&
377 g_strcmp0(entry->server, server) == 0)
381 return append_resolver(index, domain, server, 0, 0);
385 * connman_resolver_append_lifetime:
386 * @index: network interface index
387 * @domain: domain limitation
388 * @server: server address
389 * @timeout: server lifetime in seconds
391 * Append resolver server address to current list
393 int connman_resolver_append_lifetime(int index, const char *domain,
394 const char *server, unsigned int lifetime)
397 unsigned int interval;
399 DBG("index %d domain %s server %s lifetime %d",
400 index, domain, server, lifetime);
402 if (server == NULL && domain == NULL)
405 for (list = entry_list; list; list = list->next) {
406 struct entry_data *entry = list->data;
408 if (entry->timeout == 0 ||
409 entry->index != index ||
410 g_strcmp0(entry->domain, domain) != 0 ||
411 g_strcmp0(entry->server, server) != 0)
414 g_source_remove(entry->timeout);
417 resolver_expire_cb(entry);
421 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
423 DBG("RDNSS start index %d domain %s "
424 "server %s lifetime threshold %d",
425 index, domain, server, interval);
427 entry->timeout = g_timeout_add_seconds(interval,
428 resolver_refresh_cb, entry);
432 return append_resolver(index, domain, server, lifetime, 0);
436 * connman_resolver_remove:
437 * @index: network interface index
438 * @domain: domain limitation
439 * @server: server address
441 * Remover resolver server address from current list
443 int connman_resolver_remove(int index, const char *domain, const char *server)
445 GSList *list, *matches = NULL;
447 DBG("index %d domain %s server %s", index, domain, server);
449 for (list = entry_list; list; list = list->next) {
450 struct entry_data *entry = list->data;
452 if (entry->index != index)
455 if (g_strcmp0(entry->domain, domain) != 0)
458 if (g_strcmp0(entry->server, server) != 0)
461 matches = g_slist_prepend(matches, entry);
468 remove_entries(matches);
474 * connman_resolver_remove_all:
475 * @index: network interface index
477 * Remove all resolver server address for the specified interface index
479 int connman_resolver_remove_all(int index)
481 GSList *list, *matches = NULL;
483 DBG("index %d", index);
488 for (list = entry_list; list; list = list->next) {
489 struct entry_data *entry = list->data;
491 if (entry->index != index)
494 matches = g_slist_prepend(matches, entry);
500 remove_entries(matches);
506 * connman_resolver_flush:
508 * Flush pending resolver requests
510 void connman_resolver_flush(void)
512 if (dnsproxy_enabled == TRUE)
513 __connman_dnsproxy_flush();
518 int __connman_resolver_redo_servers(int index)
522 if (dnsproxy_enabled == FALSE)
525 DBG("index %d", index);
530 for (list = entry_list; list; list = list->next) {
531 struct entry_data *entry = list->data;
533 if (entry->timeout == 0 || entry->index != index)
537 * This function must only check IPv6 server addresses so
538 * do not remove IPv4 name servers unnecessarily.
540 if (entry->family != AF_INET6)
544 * We remove the server, and then re-create so that it will
545 * use proper source addresses when sending DNS queries.
547 __connman_dnsproxy_remove(entry->index, entry->domain,
550 * Remove also the resolver timer for the old server entry.
551 * A new timer will be set for the new server entry
552 * when the next Router Advertisement message arrives
553 * with RDNSS/DNSSL settings.
555 g_source_remove(entry->timeout);
558 __connman_dnsproxy_append(entry->index, entry->domain,
565 static void free_entry(gpointer data)
567 struct entry_data *entry = data;
568 g_free(entry->domain);
569 g_free(entry->server);
573 static void free_resolvfile(gpointer data)
575 struct resolvfile_entry *entry = data;
576 g_free(entry->domain);
577 g_free(entry->server);
581 int __connman_resolver_init(connman_bool_t dnsproxy)
586 DBG("dnsproxy %d", dnsproxy);
588 if (dnsproxy == FALSE)
591 if (__connman_dnsproxy_init() < 0) {
592 /* Fall back to resolv.conf */
596 dnsproxy_enabled = TRUE;
598 ns = connman_setting_get_string_list("FallbackNameservers");
599 for (i = 0; ns != NULL && ns[i] != NULL; i += 1) {
600 DBG("server %s", ns[i]);
601 append_resolver(-1, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
607 void __connman_resolver_cleanup(void)
611 if (dnsproxy_enabled == TRUE)
612 __connman_dnsproxy_cleanup();
617 for (list = resolvfile_list; list; list = g_list_next(list))
618 free_resolvfile(list->data);
619 g_list_free(resolvfile_list);
620 resolvfile_list = NULL;
622 for (slist = entry_list; slist; slist = g_slist_next(slist))
623 free_entry(slist->data);
624 g_slist_free(entry_list);