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,
365 GSList *list, *matches = NULL;
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 ||
376 g_strcmp0(entry->interface, interface) != 0 ||
377 g_strcmp0(entry->domain, domain) != 0 ||
378 g_strcmp0(entry->server, server) != 0)
381 matches = g_slist_append(matches, entry);
385 remove_entries(matches);
387 return append_resolver(interface, domain, server, 0, 0);
391 * connman_resolver_append_lifetime:
392 * @interface: network interface
393 * @domain: domain limitation
394 * @server: server address
395 * @timeout: server lifetime in seconds
397 * Append resolver server address to current list
399 int connman_resolver_append_lifetime(const char *interface, const char *domain,
400 const char *server, unsigned int lifetime)
403 unsigned int interval;
405 DBG("interface %s domain %s server %s lifetime %d",
406 interface, domain, server, lifetime);
408 if (server == NULL && domain == NULL)
411 for (list = entry_list; list; list = list->next) {
412 struct entry_data *entry = list->data;
414 if (entry->timeout == 0 ||
415 g_strcmp0(entry->interface, interface) != 0 ||
416 g_strcmp0(entry->domain, domain) != 0 ||
417 g_strcmp0(entry->server, server) != 0)
420 g_source_remove(entry->timeout);
423 resolver_expire_cb(entry);
427 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
429 DBG("RDNSS start interface %s domain %s "
430 "server %s lifetime threshold %d",
431 interface, domain, server, interval);
433 entry->timeout = g_timeout_add_seconds(interval,
434 resolver_refresh_cb, entry);
438 return append_resolver(interface, domain, server, lifetime, 0);
442 * connman_resolver_remove:
443 * @interface: network interface
444 * @domain: domain limitation
445 * @server: server address
447 * Remover resolver server address from current list
449 int connman_resolver_remove(const char *interface, const char *domain,
452 GSList *list, *matches = NULL;
454 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 (interface != NULL &&
463 g_strcmp0(entry->interface, interface) != 0)
466 if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
469 if (g_strcmp0(entry->server, server) != 0)
472 matches = g_slist_append(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_append(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 * We remove the server, and then re-create so that it will
549 * use proper source addresses when sending DNS queries.
551 __connman_dnsproxy_remove(entry->interface, entry->domain,
554 * Remove also the resolver timer for the old server entry.
555 * A new timer will be set for the new server entry
556 * when the next Router Advertisement message arrives
557 * with RDNSS/DNSSL settings.
559 g_source_remove(entry->timeout);
561 __connman_dnsproxy_append(entry->interface, entry->domain,
568 static void free_entry(gpointer data)
570 struct entry_data *entry = data;
571 g_free(entry->interface);
572 g_free(entry->domain);
573 g_free(entry->server);
577 static void free_resolvfile(gpointer data)
579 struct resolvfile_entry *entry = data;
580 g_free(entry->interface);
581 g_free(entry->domain);
582 g_free(entry->server);
586 int __connman_resolver_init(connman_bool_t dnsproxy)
591 DBG("dnsproxy %d", dnsproxy);
593 if (dnsproxy == FALSE)
596 if (__connman_dnsproxy_init() < 0) {
597 /* Fall back to resolv.conf */
601 dnsproxy_enabled = TRUE;
603 ns = connman_setting_get_string_list("FallbackNameservers");
604 for (i = 0; ns != NULL && ns[i] != NULL; i += 1) {
605 DBG("server %s", ns[i]);
606 append_resolver(NULL, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
612 void __connman_resolver_cleanup(void)
616 if (dnsproxy_enabled == TRUE)
617 __connman_dnsproxy_cleanup();
622 for (list = resolvfile_list; list; list = g_list_next(list))
623 free_resolvfile(list->data);
624 g_list_free(resolvfile_list);
625 resolvfile_list = NULL;
627 for (slist = entry_list; slist; slist = g_slist_next(slist))
628 free_entry(slist->data);
629 g_slist_free(entry_list);