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 int __connman_iface_load(struct connman_iface *iface)
38 gchar *pathname, *str;
40 DBG("iface %p", iface);
42 if (iface->identifier == NULL)
45 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
50 keyfile = g_key_file_new();
52 if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
55 if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
58 str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Policy", NULL);
60 iface->policy = __connman_iface_string2policy(str);
65 str = g_key_file_get_string(keyfile, iface->identifier,
66 "Network.ESSID", NULL);
68 g_free(iface->network.essid);
69 iface->network.essid = str;
70 if (iface->driver->set_network)
71 iface->driver->set_network(iface, str);
74 str = g_key_file_get_string(keyfile, iface->identifier,
77 iface->ipv4.method = __connman_ipv4_string2method(str);
81 str = g_key_file_get_string(keyfile, iface->identifier,
82 "IPv4.Address", NULL);
84 iface->ipv4.address.s_addr = inet_addr(str);
88 str = g_key_file_get_string(keyfile, iface->identifier,
89 "IPv4.Netmask", NULL);
91 iface->ipv4.netmask.s_addr = inet_addr(str);
95 str = g_key_file_get_string(keyfile, iface->identifier,
96 "IPv4.Gateway", NULL);
98 iface->ipv4.gateway.s_addr = inet_addr(str);
104 g_key_file_free(keyfile);
111 static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
115 DBG("iface %p", iface);
117 str = __connman_iface_policy2string(iface->policy);
118 g_key_file_set_string(keyfile, GROUP_CONFIG, "Policy", str);
121 if (iface->network.essid != NULL) {
122 g_key_file_set_string(keyfile, iface->identifier,
123 "Network.ESSID", iface->network.essid);
125 g_key_file_remove_key(keyfile, iface->identifier,
126 "Network.ESSID", NULL);
128 if (iface->ipv4.method != CONNMAN_IPV4_METHOD_UNKNOWN) {
129 str = __connman_ipv4_method2string(iface->ipv4.method);
130 g_key_file_set_string(keyfile, iface->identifier,
133 g_key_file_remove_key(keyfile, iface->identifier,
134 "IPv4.Method", NULL);
136 if (iface->ipv4.address.s_addr != INADDR_ANY) {
137 str = inet_ntoa(iface->ipv4.address);
138 g_key_file_set_string(keyfile, iface->identifier,
139 "IPv4.Address", str);
141 g_key_file_remove_key(keyfile, iface->identifier,
142 "IPv4.Address", NULL);
144 if (iface->ipv4.netmask.s_addr != INADDR_ANY) {
145 str = inet_ntoa(iface->ipv4.netmask);
146 g_key_file_set_string(keyfile, iface->identifier,
147 "IPv4.Netmask", str);
149 g_key_file_remove_key(keyfile, iface->identifier,
150 "IPv4.Netmask", NULL);
152 if (iface->ipv4.gateway.s_addr != INADDR_ANY) {
153 str = inet_ntoa(iface->ipv4.gateway);
154 g_key_file_set_string(keyfile, iface->identifier,
155 "IPv4.Gateway", str);
157 g_key_file_remove_key(keyfile, iface->identifier,
158 "IPv4.Gateway", NULL);
162 int __connman_iface_store(struct connman_iface *iface)
165 gchar *pathname, *data = NULL;
168 DBG("iface %p", iface);
170 if (iface->identifier == NULL)
173 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
175 if (pathname == NULL)
178 keyfile = g_key_file_new();
180 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
184 if (g_key_file_load_from_data(keyfile, data, length,
185 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
192 do_update(keyfile, iface);
194 data = g_key_file_to_data(keyfile, &length, NULL);
196 g_file_set_contents(pathname, data, length, NULL);
201 g_key_file_free(keyfile);