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
27 #include <net/if_arp.h>
30 #define IFF_LOWER_UP 0x10000
37 struct connman_ipconfig {
41 struct connman_ipconfig *origin;
43 const struct connman_ipconfig_ops *ops;
46 enum connman_ipconfig_method method;
47 struct connman_ipaddress *address;
48 struct connman_ipaddress *system;
54 struct connman_ipdevice {
63 struct connman_ipconfig *config;
65 struct connman_ipconfig_driver *driver;
66 struct connman_ipconfig *driver_config;
69 static GHashTable *ipdevice_hash = NULL;
70 static GList *ipconfig_list = NULL;
72 struct connman_ipaddress *connman_ipaddress_alloc(void)
74 struct connman_ipaddress *ipaddress;
76 ipaddress = g_try_new0(struct connman_ipaddress, 1);
77 if (ipaddress == NULL)
83 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
85 if (ipaddress == NULL)
88 g_free(ipaddress->broadcast);
89 g_free(ipaddress->peer);
90 g_free(ipaddress->local);
94 static unsigned char netmask2prefixlen(const char *netmask)
96 unsigned char bits = 0;
97 in_addr_t mask = inet_network(netmask);
98 in_addr_t host = ~mask;
100 /* a valid netmask must be 2^n - 1 */
101 if ((host & (host + 1)) != 0)
104 for (; mask; mask <<= 1)
110 void connman_ipaddress_set(struct connman_ipaddress *ipaddress,
111 const char *address, const char *netmask)
113 if (ipaddress == NULL)
117 ipaddress->prefixlen = netmask2prefixlen(netmask);
119 ipaddress->prefixlen = 32;
121 g_free(ipaddress->local);
122 ipaddress->local = g_strdup(address);
125 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
127 if (ipaddress == NULL)
130 ipaddress->prefixlen = 0;
132 g_free(ipaddress->local);
133 ipaddress->local = NULL;
135 g_free(ipaddress->peer);
136 ipaddress->peer = NULL;
138 g_free(ipaddress->broadcast);
139 ipaddress->broadcast = NULL;
142 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
143 struct connman_ipaddress *source)
145 if (ipaddress == NULL || source == NULL)
148 ipaddress->prefixlen = source->prefixlen;
150 g_free(ipaddress->local);
151 ipaddress->local = g_strdup(source->local);
153 g_free(ipaddress->peer);
154 ipaddress->peer = g_strdup(source->peer);
156 g_free(ipaddress->broadcast);
157 ipaddress->broadcast = g_strdup(source->broadcast);
160 static void free_address_list(struct connman_ipdevice *ipdevice)
164 for (list = ipdevice->address_list; list; list = list->next) {
165 struct connman_ipaddress *ipaddress = list->data;
167 connman_ipaddress_free(ipaddress);
171 g_slist_free(ipdevice->address_list);
172 ipdevice->address_list = NULL;
175 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
176 unsigned char prefixlen, const char *local)
180 for (list = ipdevice->address_list; list; list = list->next) {
181 struct connman_ipaddress *ipaddress = list->data;
183 if (g_strcmp0(ipaddress->local, local) == 0 &&
184 ipaddress->prefixlen == prefixlen)
191 static const char *type2str(unsigned short type)
196 case ARPHRD_LOOPBACK:
209 static const char *scope2str(unsigned char scope)
221 static void free_ipdevice(gpointer data)
223 struct connman_ipdevice *ipdevice = data;
225 connman_info("%s {remove} index %d", ipdevice->ifname,
228 if (ipdevice->config != NULL)
229 connman_ipconfig_unref(ipdevice->config);
231 free_address_list(ipdevice);
232 g_free(ipdevice->gateway);
234 g_free(ipdevice->ifname);
238 static GSList *driver_list = NULL;
240 static gint compare_priority(gconstpointer a, gconstpointer b)
242 const struct connman_ipconfig_driver *driver1 = a;
243 const struct connman_ipconfig_driver *driver2 = b;
245 return driver2->priority - driver1->priority;
249 * connman_ipconfig_driver_register:
250 * @driver: IP configuration driver
252 * Register a new IP configuration driver
254 * Returns: %0 on success
256 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
258 DBG("driver %p name %s", driver, driver->name);
260 driver_list = g_slist_insert_sorted(driver_list, driver,
267 * connman_ipconfig_driver_unregister:
268 * @driver: IP configuration driver
270 * Remove a previously registered IP configuration driver.
272 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
274 DBG("driver %p name %s", driver, driver->name);
276 driver_list = g_slist_remove(driver_list, driver);
279 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
283 DBG("ipconfig %p", ipdevice->config);
285 if (ipdevice->config == NULL)
288 switch (ipdevice->config->method) {
289 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
290 case CONNMAN_IPCONFIG_METHOD_OFF:
291 case CONNMAN_IPCONFIG_METHOD_MANUAL:
293 case CONNMAN_IPCONFIG_METHOD_DHCP:
297 if (ipdevice->driver != NULL)
300 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
301 if (ipdevice->driver_config == NULL)
304 for (list = driver_list; list; list = list->next) {
305 struct connman_ipconfig_driver *driver = list->data;
307 if (driver->request(ipdevice->driver_config) == 0) {
308 ipdevice->driver = driver;
313 if (ipdevice->driver == NULL) {
314 connman_ipconfig_unref(ipdevice->driver_config);
315 ipdevice->driver_config = NULL;
319 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
321 DBG("ipconfig %p", ipdevice->config);
323 if (ipdevice->config == NULL)
326 if (ipdevice->driver == NULL)
329 ipdevice->driver->release(ipdevice->driver_config);
331 ipdevice->driver = NULL;
333 connman_ipconfig_unref(ipdevice->driver_config);
334 ipdevice->driver_config = NULL;
336 connman_inet_clear_address(ipdevice->index);
339 void __connman_ipconfig_newlink(int index, unsigned short type,
340 unsigned int flags, const char *address,
343 struct connman_ipdevice *ipdevice;
346 gboolean up = FALSE, down = FALSE;
347 gboolean lower_up = FALSE, lower_down = FALSE;
350 DBG("index %d", index);
352 if (type == ARPHRD_LOOPBACK)
355 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
356 if (ipdevice != NULL)
359 ifname = connman_inet_ifname(index);
361 if (__connman_element_device_isfiltered(ifname) == TRUE) {
362 connman_info("Ignoring interface %s (filtered)", ifname);
367 ipdevice = g_try_new0(struct connman_ipdevice, 1);
368 if (ipdevice == NULL) {
373 ipdevice->index = index;
374 ipdevice->ifname = ifname;
375 ipdevice->type = type;
377 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
379 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
380 index, type, type2str(type));
383 if (ipdevice->config != NULL) {
384 g_free(ipdevice->config->eth);
385 ipdevice->config->eth = g_strdup(address);
386 ipdevice->config->mtu = mtu;
389 if (flags == ipdevice->flags)
392 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
399 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
400 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
401 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
402 (IFF_RUNNING | IFF_LOWER_UP))
404 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
408 ipdevice->flags = flags;
410 str = g_string_new(NULL);
415 g_string_append(str, "UP");
417 g_string_append(str, "DOWN");
419 if (flags & IFF_RUNNING)
420 g_string_append(str, ",RUNNING");
422 if (flags & IFF_LOWER_UP)
423 g_string_append(str, ",LOWER_UP");
425 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
428 g_string_free(str, TRUE);
430 for (list = g_list_first(ipconfig_list); list;
431 list = g_list_next(list)) {
432 struct connman_ipconfig *ipconfig = list->data;
434 if (index != ipconfig->index)
437 if (up == TRUE && ipconfig->ops->up)
438 ipconfig->ops->up(ipconfig);
439 if (lower_up == TRUE && ipconfig->ops->lower_up)
440 ipconfig->ops->lower_up(ipconfig);
442 if (lower_down == TRUE && ipconfig->ops->lower_down)
443 ipconfig->ops->lower_down(ipconfig);
444 if (down == TRUE && ipconfig->ops->down)
445 ipconfig->ops->down(ipconfig);
449 __connman_ipconfig_lower_up(ipdevice);
451 __connman_ipconfig_lower_down(ipdevice);
454 void __connman_ipconfig_dellink(int index)
456 struct connman_ipdevice *ipdevice;
459 DBG("index %d", index);
461 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
462 if (ipdevice == NULL)
465 for (list = g_list_first(ipconfig_list); list;
466 list = g_list_next(list)) {
467 struct connman_ipconfig *ipconfig = list->data;
469 if (index != ipconfig->index)
472 ipconfig->index = -1;
474 if (ipconfig->ops->lower_down)
475 ipconfig->ops->lower_down(ipconfig);
476 if (ipconfig->ops->down)
477 ipconfig->ops->down(ipconfig);
480 __connman_ipconfig_lower_down(ipdevice);
482 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
485 void __connman_ipconfig_newaddr(int index, const char *label,
486 unsigned char prefixlen, const char *address)
488 struct connman_ipdevice *ipdevice;
489 struct connman_ipaddress *ipaddress;
492 DBG("index %d", index);
494 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
495 if (ipdevice == NULL)
498 ipaddress = connman_ipaddress_alloc();
499 if (ipaddress == NULL)
502 ipaddress->prefixlen = prefixlen;
503 ipaddress->local = g_strdup(address);
505 ipdevice->address_list = g_slist_append(ipdevice->address_list,
508 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
509 address, prefixlen, label);
511 if (ipdevice->config != NULL)
512 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
514 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
517 if (g_slist_length(ipdevice->address_list) > 1)
520 for (list = g_list_first(ipconfig_list); list;
521 list = g_list_next(list)) {
522 struct connman_ipconfig *ipconfig = list->data;
524 if (index != ipconfig->index)
527 if (ipconfig->ops->ip_bound)
528 ipconfig->ops->ip_bound(ipconfig);
532 void __connman_ipconfig_deladdr(int index, const char *label,
533 unsigned char prefixlen, const char *address)
535 struct connman_ipdevice *ipdevice;
536 struct connman_ipaddress *ipaddress;
539 DBG("index %d", index);
541 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
542 if (ipdevice == NULL)
545 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
546 if (ipaddress == NULL)
549 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
552 connman_ipaddress_free(ipaddress);
554 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
555 address, prefixlen, label);
557 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
560 if (g_slist_length(ipdevice->address_list) > 0)
563 for (list = g_list_first(ipconfig_list); list;
564 list = g_list_next(list)) {
565 struct connman_ipconfig *ipconfig = list->data;
567 if (index != ipconfig->index)
570 if (ipconfig->ops->ip_release)
571 ipconfig->ops->ip_release(ipconfig);
575 void __connman_ipconfig_newroute(int index, unsigned char scope,
576 const char *dst, const char *gateway)
578 struct connman_ipdevice *ipdevice;
580 DBG("index %d", index);
582 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
583 if (ipdevice == NULL)
586 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
587 g_free(ipdevice->gateway);
588 ipdevice->gateway = g_strdup(gateway);
591 connman_info("%s {add} route %s gw %s scope %u <%s>",
592 ipdevice->ifname, dst, gateway,
593 scope, scope2str(scope));
596 void __connman_ipconfig_delroute(int index, unsigned char scope,
597 const char *dst, const char *gateway)
599 struct connman_ipdevice *ipdevice;
601 DBG("index %d", index);
603 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
604 if (ipdevice == NULL)
607 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
608 g_free(ipdevice->gateway);
609 ipdevice->gateway = NULL;
612 connman_info("%s {del} route %s gw %s scope %u <%s>",
613 ipdevice->ifname, dst, gateway,
614 scope, scope2str(scope));
617 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
622 keys = g_hash_table_get_keys(ipdevice_hash);
626 for (list = g_list_first(keys); list; list = g_list_next(list)) {
627 int index = GPOINTER_TO_INT(list->data);
629 function(index, user_data);
635 unsigned short __connman_ipconfig_get_type(int index)
637 struct connman_ipdevice *ipdevice;
639 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
640 if (ipdevice == NULL)
643 return ipdevice->type;
646 unsigned int __connman_ipconfig_get_flags(int index)
648 struct connman_ipdevice *ipdevice;
650 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
651 if (ipdevice == NULL)
654 return ipdevice->flags;
657 const char *__connman_ipconfig_get_gateway(int index)
659 struct connman_ipdevice *ipdevice;
661 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
662 if (ipdevice == NULL)
665 return ipdevice->gateway;
669 * connman_ipconfig_create:
671 * Allocate a new ipconfig structure.
673 * Returns: a newly-allocated #connman_ipconfig structure
675 struct connman_ipconfig *connman_ipconfig_create(int index)
677 struct connman_ipconfig *ipconfig;
679 DBG("index %d", index);
681 ipconfig = g_try_new0(struct connman_ipconfig, 1);
682 if (ipconfig == NULL)
685 ipconfig->refcount = 1;
687 ipconfig->index = index;
689 ipconfig->address = connman_ipaddress_alloc();
690 if (ipconfig->address == NULL) {
695 ipconfig->system = connman_ipaddress_alloc();
697 DBG("ipconfig %p", ipconfig);
703 * connman_ipconfig_clone:
705 * Clone an ipconfig structure and create new reference.
707 * Returns: a newly-allocated #connman_ipconfig structure
709 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
711 struct connman_ipconfig *ipconfig_clone;
713 DBG("ipconfig %p", ipconfig);
715 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
716 if (ipconfig_clone == NULL)
719 ipconfig_clone->refcount = 1;
721 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
723 ipconfig_clone->index = -1;
725 return ipconfig_clone;
729 * connman_ipconfig_ref:
730 * @ipconfig: ipconfig structure
732 * Increase reference counter of ipconfig
734 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
736 g_atomic_int_inc(&ipconfig->refcount);
742 * connman_ipconfig_unref:
743 * @ipconfig: ipconfig structure
745 * Decrease reference counter of ipconfig
747 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
749 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
750 connman_ipconfig_set_ops(ipconfig, NULL);
752 if (ipconfig->origin != NULL) {
753 connman_ipconfig_unref(ipconfig->origin);
754 ipconfig->origin = NULL;
757 connman_ipaddress_free(ipconfig->system);
758 connman_ipaddress_free(ipconfig->address);
759 g_free(ipconfig->eth);
765 * connman_ipconfig_get_data:
766 * @ipconfig: ipconfig structure
768 * Get private data pointer
770 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
772 return ipconfig->ops_data;
776 * connman_ipconfig_set_data:
777 * @ipconfig: ipconfig structure
778 * @data: data pointer
780 * Set private data pointer
782 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
784 ipconfig->ops_data = data;
788 * connman_ipconfig_get_index:
789 * @ipconfig: ipconfig structure
791 * Get interface index
793 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
795 if (ipconfig->origin != NULL)
796 return ipconfig->origin->index;
798 return ipconfig->index;
802 * connman_ipconfig_get_ifname:
803 * @ipconfig: ipconfig structure
807 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
809 struct connman_ipdevice *ipdevice;
811 if (ipconfig->index < 0)
814 ipdevice = g_hash_table_lookup(ipdevice_hash,
815 GINT_TO_POINTER(ipconfig->index));
816 if (ipdevice == NULL)
819 return ipdevice->ifname;
823 * connman_ipconfig_set_ops:
824 * @ipconfig: ipconfig structure
825 * @ops: operation callbacks
827 * Set the operation callbacks
829 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
830 const struct connman_ipconfig_ops *ops)
832 if (ipconfig->ops != NULL)
833 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
838 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
842 * connman_ipconfig_set_method:
843 * @ipconfig: ipconfig structure
844 * @method: configuration method
846 * Set the configuration method
848 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
849 enum connman_ipconfig_method method)
851 ipconfig->method = method;
857 * connman_ipconfig_bind:
858 * @ipconfig: ipconfig structure
859 * @ipaddress: ipaddress structure
861 * Bind IP address details to configuration
863 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
864 struct connman_ipaddress *ipaddress)
866 struct connman_ipconfig *origin;
868 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
870 connman_ipaddress_copy(origin->address, ipaddress);
872 connman_inet_set_address(origin->index, origin->address);
875 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
877 struct connman_ipdevice *ipdevice;
879 DBG("ipconfig %p", ipconfig);
881 if (ipconfig == NULL || ipconfig->index < 0)
884 ipdevice = g_hash_table_lookup(ipdevice_hash,
885 GINT_TO_POINTER(ipconfig->index));
886 if (ipdevice == NULL)
889 if (ipdevice->config != NULL) {
890 connman_ipaddress_clear(ipdevice->config->system);
892 connman_ipconfig_unref(ipdevice->config);
895 ipdevice->config = connman_ipconfig_ref(ipconfig);
900 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
902 struct connman_ipdevice *ipdevice;
904 DBG("ipconfig %p", ipconfig);
906 if (ipconfig == NULL || ipconfig->index < 0)
909 ipdevice = g_hash_table_lookup(ipdevice_hash,
910 GINT_TO_POINTER(ipconfig->index));
911 if (ipdevice == NULL)
914 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
917 connman_ipaddress_clear(ipdevice->config->system);
919 connman_ipconfig_unref(ipdevice->config);
920 ipdevice->config = NULL;
925 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
928 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
930 case CONNMAN_IPCONFIG_METHOD_OFF:
932 case CONNMAN_IPCONFIG_METHOD_MANUAL:
934 case CONNMAN_IPCONFIG_METHOD_DHCP:
941 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
943 if (g_strcmp0(method, "off") == 0)
944 return CONNMAN_IPCONFIG_METHOD_OFF;
945 else if (g_strcmp0(method, "manual") == 0)
946 return CONNMAN_IPCONFIG_METHOD_MANUAL;
947 else if (g_strcmp0(method, "dhcp") == 0)
948 return CONNMAN_IPCONFIG_METHOD_DHCP;
950 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
953 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
954 DBusMessageIter *iter)
958 str = __connman_ipconfig_method2string(ipconfig->method);
962 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
964 if (ipconfig->system == NULL)
967 if (ipconfig->system->local != NULL) {
969 struct in_addr netmask;
972 connman_dbus_dict_append_basic(iter, "Address",
973 DBUS_TYPE_STRING, &ipconfig->system->local);
975 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
976 netmask.s_addr = htonl(addr);
977 mask = inet_ntoa(netmask);
978 connman_dbus_dict_append_basic(iter, "Netmask",
979 DBUS_TYPE_STRING, &mask);
983 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
984 DBusMessageIter *iter)
988 str = __connman_ipconfig_method2string(ipconfig->method);
992 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
994 switch (ipconfig->method) {
995 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
996 case CONNMAN_IPCONFIG_METHOD_OFF:
997 case CONNMAN_IPCONFIG_METHOD_DHCP:
999 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1003 if (ipconfig->address == NULL)
1006 if (ipconfig->address->local != NULL) {
1008 struct in_addr netmask;
1011 connman_dbus_dict_append_basic(iter, "Address",
1012 DBUS_TYPE_STRING, &ipconfig->address->local);
1014 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1015 netmask.s_addr = htonl(addr);
1016 mask = inet_ntoa(netmask);
1017 connman_dbus_dict_append_basic(iter, "Netmask",
1018 DBUS_TYPE_STRING, &mask);
1022 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1023 DBusMessageIter *array)
1025 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1026 const char *address = NULL, *netmask = NULL;
1027 DBusMessageIter dict;
1029 DBG("ipconfig %p", ipconfig);
1031 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1034 dbus_message_iter_recurse(array, &dict);
1036 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1037 DBusMessageIter entry;
1041 dbus_message_iter_recurse(&dict, &entry);
1043 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1046 dbus_message_iter_get_basic(&entry, &key);
1047 dbus_message_iter_next(&entry);
1049 type = dbus_message_iter_get_arg_type(&entry);
1051 if (g_str_equal(key, "Method") == TRUE) {
1054 if (type != DBUS_TYPE_STRING)
1057 dbus_message_iter_get_basic(&entry, &str);
1058 method = __connman_ipconfig_string2method(str);
1059 } else if (g_str_equal(key, "Address") == TRUE) {
1060 if (type != DBUS_TYPE_STRING)
1063 dbus_message_iter_get_basic(&entry, &address);
1064 } else if (g_str_equal(key, "Netmask") == TRUE) {
1065 if (type != DBUS_TYPE_STRING)
1068 dbus_message_iter_get_basic(&entry, &netmask);
1071 dbus_message_iter_next(&dict);
1074 DBG("method %d address %s netmask %s", method, address, netmask);
1077 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1078 case CONNMAN_IPCONFIG_METHOD_OFF:
1081 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1082 if (address == NULL)
1085 ipconfig->method = method;
1086 connman_ipaddress_set(ipconfig->address, address, netmask);
1089 case CONNMAN_IPCONFIG_METHOD_DHCP:
1090 if (ipconfig->method == method)
1093 ipconfig->method = method;
1100 int __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1101 DBusMessageIter *iter)
1103 const char *method = "auto";
1105 connman_dbus_dict_append_basic(iter, "Method",
1106 DBUS_TYPE_STRING, &method);
1108 if (ipconfig->eth != NULL)
1109 connman_dbus_dict_append_basic(iter, "Address",
1110 DBUS_TYPE_STRING, &ipconfig->eth);
1112 if (ipconfig->mtu > 0)
1113 connman_dbus_dict_append_basic(iter, "MTU",
1114 DBUS_TYPE_UINT16, &ipconfig->mtu);
1119 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1120 GKeyFile *keyfile, const char *identifier, const char *prefix)
1122 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1127 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1128 GKeyFile *keyfile, const char *identifier, const char *prefix)
1130 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1135 int __connman_ipconfig_init(void)
1139 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1140 NULL, free_ipdevice);
1145 void __connman_ipconfig_cleanup(void)
1149 g_hash_table_destroy(ipdevice_hash);
1150 ipdevice_hash = NULL;