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 int __connman_iface_load(struct connman_iface *iface)
36 gchar *pathname, *str;
38 DBG("iface %p", iface);
40 if (iface->identifier == NULL)
43 pathname = g_strdup_printf("%s/interfaces.conf", STORAGEDIR);
47 keyfile = g_key_file_new();
49 if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
52 if (g_key_file_has_group(keyfile, iface->identifier) == FALSE)
55 str = g_key_file_get_string(keyfile, iface->identifier,
58 iface->policy = __connman_iface_string2policy(str);
62 str = g_key_file_get_string(keyfile, iface->identifier,
65 iface->ipv4.method = __connman_ipv4_string2method(str);
69 str = g_key_file_get_string(keyfile, iface->identifier,
70 "IPv4.Address", NULL);
72 iface->ipv4.address.s_addr = inet_addr(str);
76 str = g_key_file_get_string(keyfile, iface->identifier,
77 "IPv4.Netmask", NULL);
79 iface->ipv4.netmask.s_addr = inet_addr(str);
83 str = g_key_file_get_string(keyfile, iface->identifier,
84 "IPv4.Gateway", NULL);
86 iface->ipv4.gateway.s_addr = inet_addr(str);
91 g_key_file_free(keyfile);
98 static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
103 DBG("iface %p", iface);
105 comment = g_key_file_get_comment(keyfile,
106 iface->identifier, NULL, NULL);
107 if (comment == NULL || *comment == '\0') {
108 if (iface->device.product != NULL)
109 g_key_file_set_comment(keyfile, iface->identifier,
110 NULL, iface->device.product, NULL);
114 str = __connman_iface_policy2string(iface->policy);
115 g_key_file_set_string(keyfile, iface->identifier, "Policy", str);
117 if (iface->ipv4.method != CONNMAN_IPV4_METHOD_UNKNOWN) {
118 str = __connman_ipv4_method2string(iface->ipv4.method);
119 g_key_file_set_string(keyfile, iface->identifier,
122 g_key_file_remove_key(keyfile, iface->identifier,
123 "IPv4.Method", NULL);
125 if (iface->ipv4.address.s_addr != INADDR_ANY) {
126 str = inet_ntoa(iface->ipv4.address);
127 g_key_file_set_string(keyfile, iface->identifier,
128 "IPv4.Address", str);
130 g_key_file_remove_key(keyfile, iface->identifier,
131 "IPv4.Address", NULL);
133 if (iface->ipv4.netmask.s_addr != INADDR_ANY) {
134 str = inet_ntoa(iface->ipv4.netmask);
135 g_key_file_set_string(keyfile, iface->identifier,
136 "IPv4.Netmask", str);
138 g_key_file_remove_key(keyfile, iface->identifier,
139 "IPv4.Netmask", NULL);
141 if (iface->ipv4.gateway.s_addr != INADDR_ANY) {
142 str = inet_ntoa(iface->ipv4.gateway);
143 g_key_file_set_string(keyfile, iface->identifier,
144 "IPv4.Gateway", str);
146 g_key_file_remove_key(keyfile, iface->identifier,
147 "IPv4.Gateway", NULL);
150 int __connman_iface_store(struct connman_iface *iface)
153 gchar *pathname, *data = NULL;
156 DBG("iface %p", iface);
158 if (iface->identifier == NULL)
161 pathname = g_strdup_printf("%s/interfaces.conf", STORAGEDIR);
162 if (pathname == NULL)
165 keyfile = g_key_file_new();
167 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
171 if (g_key_file_load_from_data(keyfile, data, length,
172 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
178 do_update(keyfile, iface);
180 data = g_key_file_to_data(keyfile, &length, NULL);
182 g_file_set_contents(pathname, data, length, NULL);
187 g_key_file_free(keyfile);