5 * Copyright (C) 2007-2008 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
27 #include <arpa/inet.h>
33 #define GROUP_CONFIG "Config"
35 char *__connman_iface_find_passphrase(struct connman_iface *iface,
39 gchar *pathname, *result = NULL;
44 if (iface->identifier == NULL)
47 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
52 keyfile = g_key_file_new();
54 g_key_file_set_list_separator(keyfile, ',');
56 if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
59 if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
62 list = g_key_file_get_string_list(keyfile, GROUP_CONFIG,
63 "KnownNetworks", &list_len, NULL);
64 for (i = 0; i < list_len; i++) {
65 if (g_str_equal(list[i], network) == TRUE) {
66 result = g_key_file_get_string(keyfile, network,
69 result = g_strdup("");
77 g_key_file_free(keyfile);
84 int __connman_iface_load(struct connman_iface *iface)
87 gchar *pathname, *str;
91 DBG("iface %p", iface);
93 if (iface->identifier == NULL)
96 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
101 keyfile = g_key_file_new();
103 g_key_file_set_list_separator(keyfile, ',');
105 if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
108 if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
111 str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Policy", NULL);
113 iface->policy = __connman_iface_string2policy(str);
117 list = g_key_file_get_string_list(keyfile, GROUP_CONFIG,
118 "KnownNetworks", &list_len, NULL);
122 str = g_key_file_get_string(keyfile, GROUP_CONFIG,
123 "LastNetwork", NULL);
125 g_free(iface->network.identifier);
126 iface->network.identifier = str;
128 str = g_key_file_get_string(keyfile,
129 iface->network.identifier, "PSK", NULL);
131 g_free(iface->network.passphrase);
132 iface->network.passphrase = str;
137 g_key_file_free(keyfile);
144 static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
148 DBG("iface %p", iface);
150 str = __connman_iface_policy2string(iface->policy);
151 g_key_file_set_string(keyfile, GROUP_CONFIG, "Policy", str);
153 if (iface->network.identifier != NULL) {
154 g_key_file_set_string(keyfile, GROUP_CONFIG,
155 "LastNetwork", iface->network.identifier);
157 g_key_file_remove_key(keyfile, GROUP_CONFIG,
158 "LastNetwork", NULL);
160 if (iface->network.identifier != NULL)
161 g_key_file_set_string(keyfile, iface->network.identifier,
162 "PSK", iface->network.passphrase);
165 int __connman_iface_store(struct connman_iface *iface)
168 gchar *pathname, *data = NULL;
171 DBG("iface %p", iface);
173 if (iface->identifier == NULL)
176 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
178 if (pathname == NULL)
181 keyfile = g_key_file_new();
183 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
187 if (g_key_file_load_from_data(keyfile, data, length,
188 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
195 do_update(keyfile, iface);
197 data = g_key_file_to_data(keyfile, &length, NULL);
199 g_file_set_contents(pathname, data, length, NULL);
204 g_key_file_free(keyfile);
211 int __connman_iface_store_current_network(struct connman_iface *iface)
214 gchar *pathname, *data = NULL;
217 DBG("iface %p", iface);
219 if (iface->identifier == NULL)
222 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
224 if (pathname == NULL)
227 keyfile = g_key_file_new();
229 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
233 if (g_key_file_load_from_data(keyfile, data, length,
234 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
241 g_key_file_set_string(keyfile, GROUP_CONFIG,
242 "LastNetwork", iface->network.identifier);
244 data = g_key_file_to_data(keyfile, &length, NULL);
246 g_file_set_contents(pathname, data, length, NULL);
251 g_key_file_free(keyfile);
258 int __connman_iface_load_networks(struct connman_iface *iface)
266 if (iface->identifier == NULL)
269 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
271 if (pathname == NULL)
274 keyfile = g_key_file_new();
276 g_key_file_set_list_separator(keyfile, ',');
278 if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
281 if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
284 list = g_key_file_get_string_list(keyfile, GROUP_CONFIG,
285 "KnownNetworks", &list_len, NULL);
286 for (i = 0; i < list_len; i++) {
289 DBG("Known network %s", list[i]);
291 psk = g_key_file_get_string(keyfile, list[i], "PSK", NULL);
293 __connman_iface_add_network(iface, list[i], psk);
299 g_key_file_free(keyfile);