5 * Copyright (C) 2007-2010 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)
47 static GSList *entry_list = NULL;
48 static connman_bool_t dnsproxy_enabled = FALSE;
50 struct resolvfile_entry {
56 static GList *resolvfile_list = NULL;
58 static void resolvfile_remove_entries(GList *entries)
62 for (list = entries; list; list = list->next) {
63 struct resolvfile_entry *entry = list->data;
65 resolvfile_list = g_list_remove(resolvfile_list, entry);
67 g_free(entry->server);
68 g_free(entry->domain);
69 g_free(entry->interface);
76 static int resolvfile_export(void)
84 content = g_string_new("# Generated by Connection Manager\n");
87 * Domains and nameservers are added in reverse so that the most
88 * recently appended entry is the primary one. No more than
89 * MAXDNSRCH/MAXNS entries are used.
92 for (count = 0, list = g_list_last(resolvfile_list);
93 list && (count < MAXDNSRCH);
94 list = g_list_previous(list)) {
95 struct resolvfile_entry *entry = list->data;
101 g_string_append_printf(content, "search ");
103 g_string_append_printf(content, "%s ", entry->domain);
108 g_string_append_printf(content, "\n");
110 for (count = 0, list = g_list_last(resolvfile_list);
111 list && (count < MAXNS);
112 list = g_list_previous(list)) {
113 struct resolvfile_entry *entry = list->data;
118 g_string_append_printf(content, "nameserver %s\n",
123 old_umask = umask(022);
125 fd = open("/etc/resolv.conf", O_RDWR | O_CREAT | O_CLOEXEC,
126 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
132 if (ftruncate(fd, 0) < 0) {
139 if (write(fd, content->str, content->len) < 0)
146 g_string_free(content, TRUE);
152 int __connman_resolvfile_append(const char *interface, const char *domain,
155 struct resolvfile_entry *entry;
157 DBG("interface %s server %s", interface, server);
159 if (interface == NULL)
162 entry = g_try_new0(struct resolvfile_entry, 1);
166 entry->interface = g_strdup(interface);
167 entry->domain = g_strdup(domain);
168 entry->server = g_strdup(server);
170 resolvfile_list = g_list_append(resolvfile_list, entry);
172 return resolvfile_export();
175 int __connman_resolvfile_remove(const char *interface, const char *domain,
178 GList *list, *matches = NULL;
180 DBG("interface %s server %s", interface, server);
182 for (list = resolvfile_list; list; list = g_list_next(list)) {
183 struct resolvfile_entry *entry = list->data;
185 if (interface != NULL &&
186 g_strcmp0(entry->interface, interface) != 0)
189 if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
192 if (g_strcmp0(entry->server, server) != 0)
195 matches = g_list_append(matches, entry);
198 resolvfile_remove_entries(matches);
200 return resolvfile_export();
203 static void remove_entries(GSList *entries)
207 for (list = entries; list; list = list->next) {
208 struct entry_data *entry = list->data;
210 entry_list = g_slist_remove(entry_list, entry);
212 if (dnsproxy_enabled == TRUE) {
213 __connman_dnsproxy_remove(entry->interface, entry->domain,
216 __connman_resolvfile_remove(entry->interface, entry->domain,
221 g_source_remove(entry->timeout);
222 g_free(entry->server);
223 g_free(entry->domain);
224 g_free(entry->interface);
228 g_slist_free(entries);
231 static gboolean resolver_expire_cb(gpointer user_data)
233 struct entry_data *entry = user_data;
237 DBG("interface %s domain %s server %s",
238 entry->interface, entry->domain, entry->server);
240 list = g_slist_append(NULL, entry);
242 index = connman_inet_ifindex(entry->interface);
244 struct connman_service *service;
245 service = __connman_service_lookup_from_index(index);
247 __connman_service_nameserver_remove(service,
248 entry->server, TRUE);
251 remove_entries(list);
256 static int append_resolver(const char *interface, const char *domain,
257 const char *server, unsigned int lifetime,
260 struct entry_data *entry;
262 DBG("interface %s domain %s server %s lifetime %d flags %d",
263 interface, domain, server, lifetime, flags);
265 if (server == NULL && domain == NULL)
268 entry = g_try_new0(struct entry_data, 1);
272 entry->interface = g_strdup(interface);
273 entry->domain = g_strdup(domain);
274 entry->server = g_strdup(server);
275 entry->flags = flags;
278 entry->timeout = g_timeout_add_seconds(lifetime,
279 resolver_expire_cb, entry);
282 * We update the service only for those nameservers
283 * that are automagically added via netlink (lifetime > 0)
285 index = connman_inet_ifindex(interface);
287 struct connman_service *service;
288 service = __connman_service_lookup_from_index(index);
290 __connman_service_nameserver_append(service,
294 entry_list = g_slist_append(entry_list, entry);
296 if (dnsproxy_enabled == TRUE)
297 __connman_dnsproxy_append(interface, domain, server);
299 __connman_resolvfile_append(interface, domain, server);
305 * connman_resolver_append:
306 * @interface: network interface
307 * @domain: domain limitation
308 * @server: server address
310 * Append resolver server address to current list
312 int connman_resolver_append(const char *interface, const char *domain,
315 GSList *list, *matches = NULL;
317 DBG("interface %s domain %s server %s", interface, domain, server);
322 for (list = entry_list; list; list = list->next) {
323 struct entry_data *entry = list->data;
325 if (entry->timeout > 0 ||
326 g_strcmp0(entry->interface, interface) != 0 ||
327 g_strcmp0(entry->domain, domain) != 0 ||
328 g_strcmp0(entry->server, server) != 0)
331 matches = g_slist_append(matches, entry);
335 remove_entries(matches);
337 return append_resolver(interface, domain, server, 0, 0);
341 * connman_resolver_append_lifetime:
342 * @interface: network interface
343 * @domain: domain limitation
344 * @server: server address
345 * @timeout: server lifetime in seconds
347 * Append resolver server address to current list
349 int connman_resolver_append_lifetime(const char *interface, const char *domain,
350 const char *server, unsigned int lifetime)
354 DBG("interface %s domain %s server %s lifetime %d",
355 interface, domain, server, lifetime);
360 for (list = entry_list; list; list = list->next) {
361 struct entry_data *entry = list->data;
363 if (!entry->timeout ||
364 g_strcmp0(entry->interface, interface) ||
365 g_strcmp0(entry->domain, domain) ||
366 g_strcmp0(entry->server, server))
369 g_source_remove(entry->timeout);
372 resolver_expire_cb(entry);
376 entry->timeout = g_timeout_add_seconds(lifetime,
377 resolver_expire_cb, entry);
381 return append_resolver(interface, domain, server, lifetime, 0);
385 * connman_resolver_remove:
386 * @interface: network interface
387 * @domain: domain limitation
388 * @server: server address
390 * Remover resolver server address from current list
392 int connman_resolver_remove(const char *interface, const char *domain,
395 GSList *list, *matches = NULL;
397 DBG("interface %s domain %s server %s", interface, domain, server);
402 for (list = entry_list; list; list = list->next) {
403 struct entry_data *entry = list->data;
405 if (interface != NULL &&
406 g_strcmp0(entry->interface, interface) != 0)
409 if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
412 if (g_strcmp0(entry->server, server) != 0)
415 matches = g_slist_append(matches, entry);
421 remove_entries(matches);
427 * connman_resolver_remove_all:
428 * @interface: network interface
430 * Remove all resolver server address for the specified interface
432 int connman_resolver_remove_all(const char *interface)
434 GSList *list, *matches = NULL;
436 DBG("interface %s", interface);
438 if (interface == NULL)
441 for (list = entry_list; list; list = list->next) {
442 struct entry_data *entry = list->data;
444 if (g_strcmp0(entry->interface, interface) != 0)
447 matches = g_slist_append(matches, entry);
453 remove_entries(matches);
459 * connman_resolver_append_public_server:
460 * @server: server address
462 * Append public resolver server address to current list
464 int connman_resolver_append_public_server(const char *server)
466 DBG("server %s", server);
468 return append_resolver(NULL, NULL, server, 0, RESOLVER_FLAG_PUBLIC);
472 * connman_resolver_remove_public_server:
473 * @server: server address
475 * Remove public resolver server address to current list
477 int connman_resolver_remove_public_server(const char *server)
479 DBG("server %s", server);
481 return connman_resolver_remove(NULL, NULL, server);
485 * connman_resolver_flush:
487 * Flush pending resolver requests
489 void connman_resolver_flush(void)
491 if (dnsproxy_enabled == TRUE)
492 __connman_dnsproxy_flush();
497 static void free_entry(gpointer data)
499 struct entry_data *entry = data;
500 g_free(entry->interface);
501 g_free(entry->domain);
502 g_free(entry->server);
506 static void free_resolvfile(gpointer data)
508 struct resolvfile_entry *entry = data;
509 g_free(entry->interface);
510 g_free(entry->domain);
511 g_free(entry->server);
515 int __connman_resolver_init(connman_bool_t dnsproxy)
517 DBG("dnsproxy %d", dnsproxy);
519 if (dnsproxy == FALSE)
522 if (__connman_dnsproxy_init() < 0) {
523 /* Fall back to resolv.conf */
527 dnsproxy_enabled = TRUE;
532 void __connman_resolver_cleanup(void)
536 if (dnsproxy_enabled == TRUE)
537 __connman_dnsproxy_cleanup();
542 for (list = resolvfile_list; list; list = g_list_next(list))
543 free_resolvfile(list->data);
544 g_list_free(resolvfile_list);
545 resolvfile_list = NULL;
547 for (slist = entry_list; slist; slist = g_slist_next(slist))
548 free_entry(slist->data);
549 g_slist_free(entry_list);