5 * Copyright (C) 2007 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 DBG("known network %s", list[i]);
67 if (g_str_equal(list[i], network) == TRUE) {
68 DBG("found network %s", network);
70 result = g_key_file_get_string(keyfile, network,
73 result = g_strdup("");
81 g_key_file_free(keyfile);
88 int __connman_iface_load(struct connman_iface *iface)
91 gchar *pathname, *str;
95 DBG("iface %p", iface);
97 if (iface->identifier == NULL)
100 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
102 if (pathname == NULL)
105 keyfile = g_key_file_new();
107 g_key_file_set_list_separator(keyfile, ',');
109 if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
112 if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
115 str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Policy", NULL);
117 iface->policy = __connman_iface_string2policy(str);
121 list = g_key_file_get_string_list(keyfile, GROUP_CONFIG,
122 "KnownNetworks", &list_len, NULL);
126 str = g_key_file_get_string(keyfile, GROUP_CONFIG,
127 "LastNetwork", NULL);
129 g_free(iface->network.identifier);
130 iface->network.identifier = str;
132 str = g_key_file_get_string(keyfile,
133 iface->network.identifier, "PSK", NULL);
135 g_free(iface->network.passphrase);
136 iface->network.passphrase = str;
141 g_key_file_free(keyfile);
148 static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
152 DBG("iface %p", iface);
154 str = __connman_iface_policy2string(iface->policy);
155 g_key_file_set_string(keyfile, GROUP_CONFIG, "Policy", str);
157 if (iface->network.identifier != NULL) {
158 g_key_file_set_string(keyfile, GROUP_CONFIG,
159 "LastNetwork", iface->network.identifier);
161 g_key_file_remove_key(keyfile, GROUP_CONFIG,
162 "LastNetwork", NULL);
164 if (iface->network.identifier != NULL)
165 g_key_file_set_string(keyfile, iface->network.identifier,
166 "PSK", iface->network.passphrase);
169 int __connman_iface_store(struct connman_iface *iface)
172 gchar *pathname, *data = NULL;
175 DBG("iface %p", iface);
177 if (iface->identifier == NULL)
180 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
182 if (pathname == NULL)
185 keyfile = g_key_file_new();
187 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
191 if (g_key_file_load_from_data(keyfile, data, length,
192 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
199 do_update(keyfile, iface);
201 data = g_key_file_to_data(keyfile, &length, NULL);
203 g_file_set_contents(pathname, data, length, NULL);
208 g_key_file_free(keyfile);
215 int __connman_iface_store_current_network(struct connman_iface *iface)
218 gchar *pathname, *data = NULL;
221 DBG("iface %p", iface);
223 if (iface->identifier == NULL)
226 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
228 if (pathname == NULL)
231 keyfile = g_key_file_new();
233 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
237 if (g_key_file_load_from_data(keyfile, data, length,
238 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
245 g_key_file_set_string(keyfile, GROUP_CONFIG,
246 "LastNetwork", iface->network.identifier);
248 data = g_key_file_to_data(keyfile, &length, NULL);
250 g_file_set_contents(pathname, data, length, NULL);
255 g_key_file_free(keyfile);