5 * Copyright (C) 2007-2009 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
29 struct connman_resolver *resolver;
35 static GSList *entry_list = NULL;
36 static GSList *resolver_list = NULL;
38 static void remove_entries(GSList *entries)
42 for (list = entries; list; list = list->next) {
43 struct entry_data *entry = list->data;
44 struct connman_resolver *resolver = entry->resolver;
46 entry_list = g_slist_remove(entry_list, entry);
49 resolver->remove(entry->interface, entry->domain,
52 g_free(entry->server);
53 g_free(entry->domain);
54 g_free(entry->interface);
58 g_slist_free(entries);
61 static gint compare_priority(gconstpointer a, gconstpointer b)
63 const struct connman_resolver *resolver1 = a;
64 const struct connman_resolver *resolver2 = b;
66 return resolver2->priority - resolver1->priority;
70 * connman_resolver_register:
71 * @resolver: resolver module
73 * Register a new resolver module
75 * Returns: %0 on success
77 int connman_resolver_register(struct connman_resolver *resolver)
81 DBG("resolver %p name %s", resolver, resolver->name);
83 resolver_list = g_slist_insert_sorted(resolver_list, resolver,
86 if (resolver->append == NULL)
89 for (list = entry_list; list; list = list->next) {
90 struct entry_data *entry = list->data;
95 if (resolver->append(entry->interface, entry->domain,
97 entry->resolver = resolver;
104 * connman_resolver_unregister:
105 * @resolver: resolver module
107 * Remove a previously registered resolver module
109 void connman_resolver_unregister(struct connman_resolver *resolver)
111 GSList *list, *matches = NULL;
113 DBG("resolver %p name %s", resolver, resolver->name);
115 resolver_list = g_slist_remove(resolver_list, resolver);
117 for (list = entry_list; list; list = list->next) {
118 struct entry_data *entry = list->data;
120 if (entry->resolver != resolver)
123 matches = g_slist_append(matches, entry);
126 remove_entries(matches);
130 * connman_resolver_append:
131 * @interface: network interface
132 * @domain: domain limitation
133 * @server: server address
135 * Append resolver server address to current list
137 int connman_resolver_append(const char *interface, const char *domain,
140 struct entry_data *entry;
143 DBG("interface %s domain %s server %s", interface, domain, server);
145 entry = g_try_new0(struct entry_data, 1);
149 entry->interface = g_strdup(interface);
150 entry->domain = g_strdup(domain);
151 entry->server = g_strdup(server);
153 entry_list = g_slist_append(entry_list, entry);
155 for (list = resolver_list; list; list = list->next) {
156 struct connman_resolver *resolver = list->data;
158 if (resolver->append == NULL)
161 if (resolver->append(interface, domain, server) == 0) {
162 entry->resolver = resolver;
171 * connman_resolver_remove_all:
172 * @interface: network interface
174 * Remove all resolver server address for the specified interface
176 int connman_resolver_remove_all(const char *interface)
178 GSList *list, *matches = NULL;
180 DBG("interface %s", interface);
182 for (list = entry_list; list; list = list->next) {
183 struct entry_data *entry = list->data;
185 if (g_str_equal(entry->interface, interface) == FALSE)
188 matches = g_slist_append(matches, entry);
191 remove_entries(matches);
196 static int selftest_append(const char *interface, const char *domain,
199 DBG("server %s", server);
204 static int selftest_remove(const char *interface, const char *domain,
207 DBG("server %s", server);
212 static struct connman_resolver selftest_resolver = {
214 .priority = CONNMAN_RESOLVER_PRIORITY_HIGH + 42,
215 .append = selftest_append,
216 .remove = selftest_remove,
219 int __connman_resolver_selftest(void)
221 connman_resolver_append("wlan0", "lwn.net", "192.168.0.1");
223 connman_resolver_register(&selftest_resolver);
225 connman_resolver_append("eth0", "moblin.org", "192.168.42.1");
226 connman_resolver_append("wlan0", "lwn.net", "192.168.0.2");
228 connman_resolver_remove_all("wlan0");
230 connman_resolver_unregister(&selftest_resolver);