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
37 #define RESOLVER_FLAG_PUBLIC (1 << 0)
40 * Threshold for RDNSS lifetime. Will be used to trigger RS
41 * before RDNSS entries actually expire
43 #define RESOLVER_LIFETIME_REFRESH_THRESHOLD 0.8
50 unsigned int lifetime;
54 static GSList *entry_list = NULL;
55 static connman_bool_t dnsproxy_enabled = FALSE;
57 struct resolvfile_entry {
63 static GList *resolvfile_list = NULL;
65 static void resolvfile_remove_entries(GList *entries)
69 for (list = entries; list; list = list->next) {
70 struct resolvfile_entry *entry = list->data;
72 resolvfile_list = g_list_remove(resolvfile_list, entry);
74 g_free(entry->server);
75 g_free(entry->domain);
76 g_free(entry->interface);
83 static int resolvfile_export(void)
91 content = g_string_new("# Generated by Connection Manager\n");
94 * Domains and nameservers are added in reverse so that the most
95 * recently appended entry is the primary one. No more than
96 * MAXDNSRCH/MAXNS entries are used.
99 for (count = 0, list = g_list_last(resolvfile_list);
100 list && (count < MAXDNSRCH);
101 list = g_list_previous(list)) {
102 struct resolvfile_entry *entry = list->data;
108 g_string_append_printf(content, "search ");
110 g_string_append_printf(content, "%s ", entry->domain);
115 g_string_append_printf(content, "\n");
117 for (count = 0, list = g_list_last(resolvfile_list);
118 list && (count < MAXNS);
119 list = g_list_previous(list)) {
120 struct resolvfile_entry *entry = list->data;
125 g_string_append_printf(content, "nameserver %s\n",
130 old_umask = umask(022);
132 fd = open("/etc/resolv.conf", O_RDWR | O_CREAT | O_CLOEXEC,
133 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
139 if (ftruncate(fd, 0) < 0) {
146 if (write(fd, content->str, content->len) < 0)
153 g_string_free(content, TRUE);
159 int __connman_resolvfile_append(const char *interface, const char *domain,
162 struct resolvfile_entry *entry;
164 DBG("interface %s server %s", interface, server);
166 if (interface == NULL)
169 entry = g_try_new0(struct resolvfile_entry, 1);
173 entry->interface = g_strdup(interface);
174 entry->domain = g_strdup(domain);
175 entry->server = g_strdup(server);
177 resolvfile_list = g_list_append(resolvfile_list, entry);
179 return resolvfile_export();
182 int __connman_resolvfile_remove(const char *interface, const char *domain,
185 GList *list, *matches = NULL;
187 DBG("interface %s server %s", interface, server);
189 for (list = resolvfile_list; list; list = g_list_next(list)) {
190 struct resolvfile_entry *entry = list->data;
192 if (interface != NULL &&
193 g_strcmp0(entry->interface, interface) != 0)
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->interface, entry->domain,
223 __connman_resolvfile_remove(entry->interface, entry->domain,
228 g_source_remove(entry->timeout);
229 g_free(entry->server);
230 g_free(entry->domain);
231 g_free(entry->interface);
235 g_slist_free(entries);
238 static gboolean resolver_expire_cb(gpointer user_data)
240 struct entry_data *entry = user_data;
244 DBG("interface %s domain %s server %s",
245 entry->interface, entry->domain, entry->server);
247 list = g_slist_append(NULL, entry);
249 index = connman_inet_ifindex(entry->interface);
251 struct connman_service *service;
252 service = __connman_service_lookup_from_index(index);
254 __connman_service_nameserver_remove(service,
255 entry->server, TRUE);
258 remove_entries(list);
263 static gboolean resolver_refresh_cb(gpointer user_data)
265 struct entry_data *entry = user_data;
267 unsigned int interval;
268 struct connman_service *service = NULL;
270 /* Round up what we have left from lifetime */
271 interval = entry->lifetime *
272 (1 - RESOLVER_LIFETIME_REFRESH_THRESHOLD) + 1.0;
274 DBG("RDNSS start interface %s domain %s "
275 "server %s remaining lifetime %d",
276 entry->interface, entry->domain,
277 entry->server, interval);
279 entry->timeout = g_timeout_add_seconds(interval,
280 resolver_expire_cb, entry);
282 index = connman_inet_ifindex(entry->interface);
284 service = __connman_service_lookup_from_index(index);
285 if (service != NULL) {
287 * Send Router Solicitation to refresh RDNSS entries
288 * before their lifetime expires
290 __connman_refresh_rs_ipv6(
291 __connman_service_get_network(service),
298 static int append_resolver(const char *interface, const char *domain,
299 const char *server, unsigned int lifetime,
302 struct entry_data *entry;
303 unsigned int interval;
305 DBG("interface %s domain %s server %s lifetime %d flags %d",
306 interface, domain, server, lifetime, flags);
308 if (server == NULL && domain == NULL)
311 entry = g_try_new0(struct entry_data, 1);
315 entry->interface = g_strdup(interface);
316 entry->domain = g_strdup(domain);
317 entry->server = g_strdup(server);
318 entry->flags = flags;
319 entry->lifetime = lifetime;
322 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
324 DBG("RDNSS start interface %s domain %s "
325 "server %s lifetime threshold %d",
326 interface, domain, server, interval);
328 entry->timeout = g_timeout_add_seconds(interval,
329 resolver_refresh_cb, entry);
332 * We update the service only for those nameservers
333 * that are automagically added via netlink (lifetime > 0)
335 index = connman_inet_ifindex(interface);
336 if (server != NULL && index >= 0) {
337 struct connman_service *service;
338 service = __connman_service_lookup_from_index(index);
340 __connman_service_nameserver_append(service,
344 entry_list = g_slist_append(entry_list, entry);
346 if (dnsproxy_enabled == TRUE)
347 __connman_dnsproxy_append(interface, domain, server);
349 __connman_resolvfile_append(interface, domain, server);
355 * connman_resolver_append:
356 * @interface: network interface
357 * @domain: domain limitation
358 * @server: server address
360 * Append resolver server address to current list
362 int connman_resolver_append(const char *interface, const char *domain,
367 DBG("interface %s domain %s server %s", interface, domain, server);
369 if (server == NULL && domain == NULL)
372 for (list = entry_list; list; list = list->next) {
373 struct entry_data *entry = list->data;
375 if (entry->timeout > 0)
378 if (g_strcmp0(entry->interface, interface) == 0 &&
379 g_strcmp0(entry->domain, domain) == 0 &&
380 g_strcmp0(entry->server, server) == 0)
384 return append_resolver(interface, domain, server, 0, 0);
388 * connman_resolver_append_lifetime:
389 * @interface: network interface
390 * @domain: domain limitation
391 * @server: server address
392 * @timeout: server lifetime in seconds
394 * Append resolver server address to current list
396 int connman_resolver_append_lifetime(const char *interface, const char *domain,
397 const char *server, unsigned int lifetime)
400 unsigned int interval;
402 DBG("interface %s domain %s server %s lifetime %d",
403 interface, domain, server, lifetime);
405 if (server == NULL && domain == NULL)
408 for (list = entry_list; list; list = list->next) {
409 struct entry_data *entry = list->data;
411 if (entry->timeout == 0 ||
412 g_strcmp0(entry->interface, interface) != 0 ||
413 g_strcmp0(entry->domain, domain) != 0 ||
414 g_strcmp0(entry->server, server) != 0)
417 g_source_remove(entry->timeout);
420 resolver_expire_cb(entry);
424 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
426 DBG("RDNSS start interface %s domain %s "
427 "server %s lifetime threshold %d",
428 interface, domain, server, interval);
430 entry->timeout = g_timeout_add_seconds(interval,
431 resolver_refresh_cb, entry);
435 return append_resolver(interface, domain, server, lifetime, 0);
439 * connman_resolver_remove:
440 * @interface: network interface
441 * @domain: domain limitation
442 * @server: server address
444 * Remover resolver server address from current list
446 int connman_resolver_remove(const char *interface, const char *domain,
449 GSList *list, *matches = NULL;
451 DBG("interface %s domain %s server %s", interface, domain, server);
453 for (list = entry_list; list; list = list->next) {
454 struct entry_data *entry = list->data;
456 if (g_strcmp0(entry->interface, interface) != 0)
459 if (g_strcmp0(entry->domain, domain) != 0)
462 if (g_strcmp0(entry->server, server) != 0)
465 matches = g_slist_append(matches, entry);
472 remove_entries(matches);
478 * connman_resolver_remove_all:
479 * @interface: network interface
481 * Remove all resolver server address for the specified interface
483 int connman_resolver_remove_all(const char *interface)
485 GSList *list, *matches = NULL;
487 DBG("interface %s", interface);
489 if (interface == NULL)
492 for (list = entry_list; list; list = list->next) {
493 struct entry_data *entry = list->data;
495 if (g_strcmp0(entry->interface, interface) != 0)
498 matches = g_slist_append(matches, entry);
504 remove_entries(matches);
510 * connman_resolver_flush:
512 * Flush pending resolver requests
514 void connman_resolver_flush(void)
516 if (dnsproxy_enabled == TRUE)
517 __connman_dnsproxy_flush();
522 int __connman_resolver_redo_servers(const char *interface)
526 if (dnsproxy_enabled == FALSE)
529 DBG("interface %s", interface);
531 if (interface == NULL)
534 for (list = entry_list; list; list = list->next) {
535 struct entry_data *entry = list->data;
537 if (entry->timeout == 0 ||
538 g_strcmp0(entry->interface, interface) != 0)
542 * We remove the server, and then re-create so that it will
543 * use proper source addresses when sending DNS queries.
545 __connman_dnsproxy_remove(entry->interface, entry->domain,
548 * Remove also the resolver timer for the old server entry.
549 * A new timer will be set for the new server entry
550 * when the next Router Advertisement message arrives
551 * with RDNSS/DNSSL settings.
553 g_source_remove(entry->timeout);
555 __connman_dnsproxy_append(entry->interface, entry->domain,
562 static void free_entry(gpointer data)
564 struct entry_data *entry = data;
565 g_free(entry->interface);
566 g_free(entry->domain);
567 g_free(entry->server);
571 static void free_resolvfile(gpointer data)
573 struct resolvfile_entry *entry = data;
574 g_free(entry->interface);
575 g_free(entry->domain);
576 g_free(entry->server);
580 int __connman_resolver_init(connman_bool_t dnsproxy)
585 DBG("dnsproxy %d", dnsproxy);
587 if (dnsproxy == FALSE)
590 if (__connman_dnsproxy_init() < 0) {
591 /* Fall back to resolv.conf */
595 dnsproxy_enabled = TRUE;
597 ns = connman_setting_get_string_list("FallbackNameservers");
598 for (i = 0; ns != NULL && ns[i] != NULL; i += 1) {
599 DBG("server %s", ns[i]);
600 append_resolver(NULL, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
606 void __connman_resolver_cleanup(void)
610 if (dnsproxy_enabled == TRUE)
611 __connman_dnsproxy_cleanup();
616 for (list = resolvfile_list; list; list = g_list_next(list))
617 free_resolvfile(list->data);
618 g_list_free(resolvfile_list);
619 resolvfile_list = NULL;
621 for (slist = entry_list; slist; slist = g_slist_next(slist))
622 free_entry(slist->data);
623 g_slist_free(entry_list);