5 * Copyright (C) 2007-2010 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>
28 #include <linux/if_link.h>
31 #define IFF_LOWER_UP 0x10000
38 struct connman_ipconfig {
42 struct connman_ipconfig *origin;
44 const struct connman_ipconfig_ops *ops;
47 enum connman_ipconfig_method method;
48 struct connman_ipaddress *address;
49 struct connman_ipaddress *system;
52 struct connman_ipdevice {
65 struct connman_ipconfig *config;
67 struct connman_ipconfig_driver *driver;
68 struct connman_ipconfig *driver_config;
71 static GHashTable *ipdevice_hash = NULL;
72 static GList *ipconfig_list = NULL;
74 struct connman_ipaddress *connman_ipaddress_alloc(void)
76 struct connman_ipaddress *ipaddress;
78 ipaddress = g_try_new0(struct connman_ipaddress, 1);
79 if (ipaddress == NULL)
85 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
87 if (ipaddress == NULL)
90 g_free(ipaddress->broadcast);
91 g_free(ipaddress->peer);
92 g_free(ipaddress->local);
96 static unsigned char netmask2prefixlen(const char *netmask)
98 unsigned char bits = 0;
99 in_addr_t mask = inet_network(netmask);
100 in_addr_t host = ~mask;
102 /* a valid netmask must be 2^n - 1 */
103 if ((host & (host + 1)) != 0)
106 for (; mask; mask <<= 1)
112 void connman_ipaddress_set(struct connman_ipaddress *ipaddress,
113 const char *address, const char *netmask)
115 if (ipaddress == NULL)
119 ipaddress->prefixlen = netmask2prefixlen(netmask);
121 ipaddress->prefixlen = 32;
123 g_free(ipaddress->local);
124 ipaddress->local = g_strdup(address);
127 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
129 if (ipaddress == NULL)
132 ipaddress->prefixlen = 0;
134 g_free(ipaddress->local);
135 ipaddress->local = NULL;
137 g_free(ipaddress->peer);
138 ipaddress->peer = NULL;
140 g_free(ipaddress->broadcast);
141 ipaddress->broadcast = NULL;
144 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
145 struct connman_ipaddress *source)
147 if (ipaddress == NULL || source == NULL)
150 ipaddress->prefixlen = source->prefixlen;
152 g_free(ipaddress->local);
153 ipaddress->local = g_strdup(source->local);
155 g_free(ipaddress->peer);
156 ipaddress->peer = g_strdup(source->peer);
158 g_free(ipaddress->broadcast);
159 ipaddress->broadcast = g_strdup(source->broadcast);
162 static void free_address_list(struct connman_ipdevice *ipdevice)
166 for (list = ipdevice->address_list; list; list = list->next) {
167 struct connman_ipaddress *ipaddress = list->data;
169 connman_ipaddress_free(ipaddress);
173 g_slist_free(ipdevice->address_list);
174 ipdevice->address_list = NULL;
177 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
178 unsigned char prefixlen, const char *local)
182 for (list = ipdevice->address_list; list; list = list->next) {
183 struct connman_ipaddress *ipaddress = list->data;
185 if (g_strcmp0(ipaddress->local, local) == 0 &&
186 ipaddress->prefixlen == prefixlen)
193 static const char *type2str(unsigned short type)
198 case ARPHRD_LOOPBACK:
211 static const char *scope2str(unsigned char scope)
223 static void free_ipdevice(gpointer data)
225 struct connman_ipdevice *ipdevice = data;
227 connman_info("%s {remove} index %d", ipdevice->ifname,
230 if (ipdevice->config != NULL)
231 connman_ipconfig_unref(ipdevice->config);
233 free_address_list(ipdevice);
234 g_free(ipdevice->gateway);
236 g_free(ipdevice->address);
237 g_free(ipdevice->ifname);
241 static GSList *driver_list = NULL;
243 static gint compare_priority(gconstpointer a, gconstpointer b)
245 const struct connman_ipconfig_driver *driver1 = a;
246 const struct connman_ipconfig_driver *driver2 = b;
248 return driver2->priority - driver1->priority;
252 * connman_ipconfig_driver_register:
253 * @driver: IP configuration driver
255 * Register a new IP configuration driver
257 * Returns: %0 on success
259 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
261 DBG("driver %p name %s", driver, driver->name);
263 driver_list = g_slist_insert_sorted(driver_list, driver,
270 * connman_ipconfig_driver_unregister:
271 * @driver: IP configuration driver
273 * Remove a previously registered IP configuration driver.
275 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
277 DBG("driver %p name %s", driver, driver->name);
279 driver_list = g_slist_remove(driver_list, driver);
282 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
286 DBG("ipconfig %p", ipdevice->config);
288 if (ipdevice->config == NULL)
291 switch (ipdevice->config->method) {
292 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
293 case CONNMAN_IPCONFIG_METHOD_OFF:
294 case CONNMAN_IPCONFIG_METHOD_FIXED:
295 case CONNMAN_IPCONFIG_METHOD_MANUAL:
297 case CONNMAN_IPCONFIG_METHOD_DHCP:
301 if (ipdevice->driver != NULL)
304 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
305 if (ipdevice->driver_config == NULL)
308 for (list = driver_list; list; list = list->next) {
309 struct connman_ipconfig_driver *driver = list->data;
311 if (driver->request(ipdevice->driver_config) == 0) {
312 ipdevice->driver = driver;
317 if (ipdevice->driver == NULL) {
318 connman_ipconfig_unref(ipdevice->driver_config);
319 ipdevice->driver_config = NULL;
323 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
325 DBG("ipconfig %p", ipdevice->config);
327 if (ipdevice->config == NULL)
330 if (ipdevice->driver == NULL)
333 ipdevice->driver->release(ipdevice->driver_config);
335 ipdevice->driver = NULL;
337 connman_ipconfig_unref(ipdevice->driver_config);
338 ipdevice->driver_config = NULL;
340 connman_inet_clear_address(ipdevice->index);
343 static void update_stats(struct connman_ipdevice *ipdevice,
344 struct rtnl_link_stats *stats)
346 if (stats->rx_packets == 0 && stats->tx_packets == 0)
349 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
350 stats->rx_packets, stats->rx_bytes);
351 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
352 stats->tx_packets, stats->tx_bytes);
354 if (ipdevice->config == NULL)
357 ipdevice->rx_bytes = stats->rx_bytes;
358 ipdevice->tx_bytes = stats->tx_bytes;
360 __connman_counter_notify(ipdevice->rx_bytes, ipdevice->tx_bytes);
363 void __connman_ipconfig_newlink(int index, unsigned short type,
364 unsigned int flags, const char *address,
366 struct rtnl_link_stats *stats)
368 struct connman_ipdevice *ipdevice;
371 gboolean up = FALSE, down = FALSE;
372 gboolean lower_up = FALSE, lower_down = FALSE;
375 DBG("index %d", index);
377 if (type == ARPHRD_LOOPBACK)
380 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
381 if (ipdevice != NULL)
384 ifname = connman_inet_ifname(index);
386 if (__connman_element_device_isfiltered(ifname) == TRUE) {
387 connman_info("Ignoring interface %s (filtered)", ifname);
392 ipdevice = g_try_new0(struct connman_ipdevice, 1);
393 if (ipdevice == NULL) {
398 ipdevice->index = index;
399 ipdevice->ifname = ifname;
400 ipdevice->type = type;
402 ipdevice->address = g_strdup(address);
404 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
406 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
407 index, type, type2str(type));
412 update_stats(ipdevice, stats);
414 if (flags == ipdevice->flags)
417 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
424 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
425 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
426 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
427 (IFF_RUNNING | IFF_LOWER_UP))
429 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
433 ipdevice->flags = flags;
435 str = g_string_new(NULL);
440 g_string_append(str, "UP");
442 g_string_append(str, "DOWN");
444 if (flags & IFF_RUNNING)
445 g_string_append(str, ",RUNNING");
447 if (flags & IFF_LOWER_UP)
448 g_string_append(str, ",LOWER_UP");
450 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
453 g_string_free(str, TRUE);
455 for (list = g_list_first(ipconfig_list); list;
456 list = g_list_next(list)) {
457 struct connman_ipconfig *ipconfig = list->data;
459 if (index != ipconfig->index)
462 if (ipconfig->ops == NULL)
465 if (up == TRUE && ipconfig->ops->up)
466 ipconfig->ops->up(ipconfig);
467 if (lower_up == TRUE && ipconfig->ops->lower_up)
468 ipconfig->ops->lower_up(ipconfig);
470 if (lower_down == TRUE && ipconfig->ops->lower_down)
471 ipconfig->ops->lower_down(ipconfig);
472 if (down == TRUE && ipconfig->ops->down)
473 ipconfig->ops->down(ipconfig);
477 __connman_ipconfig_lower_up(ipdevice);
479 __connman_ipconfig_lower_down(ipdevice);
482 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
484 struct connman_ipdevice *ipdevice;
487 DBG("index %d", index);
489 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
490 if (ipdevice == NULL)
493 update_stats(ipdevice, stats);
495 for (list = g_list_first(ipconfig_list); list;
496 list = g_list_next(list)) {
497 struct connman_ipconfig *ipconfig = list->data;
499 if (index != ipconfig->index)
502 ipconfig->index = -1;
504 if (ipconfig->ops == NULL)
507 if (ipconfig->ops->lower_down)
508 ipconfig->ops->lower_down(ipconfig);
509 if (ipconfig->ops->down)
510 ipconfig->ops->down(ipconfig);
513 __connman_ipconfig_lower_down(ipdevice);
515 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
518 void __connman_ipconfig_newaddr(int index, const char *label,
519 unsigned char prefixlen, const char *address)
521 struct connman_ipdevice *ipdevice;
522 struct connman_ipaddress *ipaddress;
525 DBG("index %d", index);
527 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
528 if (ipdevice == NULL)
531 ipaddress = connman_ipaddress_alloc();
532 if (ipaddress == NULL)
535 ipaddress->prefixlen = prefixlen;
536 ipaddress->local = g_strdup(address);
538 ipdevice->address_list = g_slist_append(ipdevice->address_list,
541 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
542 address, prefixlen, label);
544 if (ipdevice->config != NULL)
545 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
547 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
550 if (g_slist_length(ipdevice->address_list) > 1)
553 for (list = g_list_first(ipconfig_list); list;
554 list = g_list_next(list)) {
555 struct connman_ipconfig *ipconfig = list->data;
557 if (index != ipconfig->index)
560 if (ipconfig->ops == NULL)
563 if (ipconfig->ops->ip_bound)
564 ipconfig->ops->ip_bound(ipconfig);
568 void __connman_ipconfig_deladdr(int index, const char *label,
569 unsigned char prefixlen, const char *address)
571 struct connman_ipdevice *ipdevice;
572 struct connman_ipaddress *ipaddress;
575 DBG("index %d", index);
577 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
578 if (ipdevice == NULL)
581 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
582 if (ipaddress == NULL)
585 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
588 connman_ipaddress_free(ipaddress);
590 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
591 address, prefixlen, label);
593 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
596 if (g_slist_length(ipdevice->address_list) > 0)
599 for (list = g_list_first(ipconfig_list); list;
600 list = g_list_next(list)) {
601 struct connman_ipconfig *ipconfig = list->data;
603 if (index != ipconfig->index)
606 if (ipconfig->ops == NULL)
609 if (ipconfig->ops->ip_release)
610 ipconfig->ops->ip_release(ipconfig);
614 void __connman_ipconfig_newroute(int index, unsigned char scope,
615 const char *dst, const char *gateway)
617 struct connman_ipdevice *ipdevice;
619 DBG("index %d", index);
621 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
622 if (ipdevice == NULL)
625 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
626 g_free(ipdevice->gateway);
627 ipdevice->gateway = g_strdup(gateway);
630 connman_info("%s {add} route %s gw %s scope %u <%s>",
631 ipdevice->ifname, dst, gateway,
632 scope, scope2str(scope));
635 void __connman_ipconfig_delroute(int index, unsigned char scope,
636 const char *dst, const char *gateway)
638 struct connman_ipdevice *ipdevice;
640 DBG("index %d", index);
642 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
643 if (ipdevice == NULL)
646 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
647 g_free(ipdevice->gateway);
648 ipdevice->gateway = NULL;
651 connman_info("%s {del} route %s gw %s scope %u <%s>",
652 ipdevice->ifname, dst, gateway,
653 scope, scope2str(scope));
656 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
661 keys = g_hash_table_get_keys(ipdevice_hash);
665 for (list = g_list_first(keys); list; list = g_list_next(list)) {
666 int index = GPOINTER_TO_INT(list->data);
668 function(index, user_data);
674 unsigned short __connman_ipconfig_get_type(int index)
676 struct connman_ipdevice *ipdevice;
678 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
679 if (ipdevice == NULL)
682 return ipdevice->type;
685 unsigned int __connman_ipconfig_get_flags(int index)
687 struct connman_ipdevice *ipdevice;
689 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
690 if (ipdevice == NULL)
693 return ipdevice->flags;
696 const char *__connman_ipconfig_get_gateway(int index)
698 struct connman_ipdevice *ipdevice;
700 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
701 if (ipdevice == NULL)
704 return ipdevice->gateway;
708 * connman_ipconfig_create:
710 * Allocate a new ipconfig structure.
712 * Returns: a newly-allocated #connman_ipconfig structure
714 struct connman_ipconfig *connman_ipconfig_create(int index)
716 struct connman_ipconfig *ipconfig;
718 DBG("index %d", index);
720 ipconfig = g_try_new0(struct connman_ipconfig, 1);
721 if (ipconfig == NULL)
724 ipconfig->refcount = 1;
726 ipconfig->index = index;
728 ipconfig->address = connman_ipaddress_alloc();
729 if (ipconfig->address == NULL) {
734 ipconfig->system = connman_ipaddress_alloc();
736 DBG("ipconfig %p", ipconfig);
742 * connman_ipconfig_clone:
744 * Clone an ipconfig structure and create new reference.
746 * Returns: a newly-allocated #connman_ipconfig structure
748 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
750 struct connman_ipconfig *ipconfig_clone;
752 DBG("ipconfig %p", ipconfig);
754 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
755 if (ipconfig_clone == NULL)
758 ipconfig_clone->refcount = 1;
760 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
762 ipconfig_clone->index = -1;
764 return ipconfig_clone;
768 * connman_ipconfig_ref:
769 * @ipconfig: ipconfig structure
771 * Increase reference counter of ipconfig
773 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
775 g_atomic_int_inc(&ipconfig->refcount);
781 * connman_ipconfig_unref:
782 * @ipconfig: ipconfig structure
784 * Decrease reference counter of ipconfig
786 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
788 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
789 __connman_ipconfig_disable(ipconfig);
791 connman_ipconfig_set_ops(ipconfig, NULL);
793 if (ipconfig->origin != NULL) {
794 connman_ipconfig_unref(ipconfig->origin);
795 ipconfig->origin = NULL;
798 connman_ipaddress_free(ipconfig->system);
799 connman_ipaddress_free(ipconfig->address);
805 * connman_ipconfig_get_data:
806 * @ipconfig: ipconfig structure
808 * Get private data pointer
810 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
812 return ipconfig->ops_data;
816 * connman_ipconfig_set_data:
817 * @ipconfig: ipconfig structure
818 * @data: data pointer
820 * Set private data pointer
822 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
824 ipconfig->ops_data = data;
828 * connman_ipconfig_get_index:
829 * @ipconfig: ipconfig structure
831 * Get interface index
833 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
835 if (ipconfig->origin != NULL)
836 return ipconfig->origin->index;
838 return ipconfig->index;
842 * connman_ipconfig_get_ifname:
843 * @ipconfig: ipconfig structure
847 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
849 struct connman_ipdevice *ipdevice;
851 if (ipconfig->index < 0)
854 ipdevice = g_hash_table_lookup(ipdevice_hash,
855 GINT_TO_POINTER(ipconfig->index));
856 if (ipdevice == NULL)
859 return ipdevice->ifname;
863 * connman_ipconfig_set_ops:
864 * @ipconfig: ipconfig structure
865 * @ops: operation callbacks
867 * Set the operation callbacks
869 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
870 const struct connman_ipconfig_ops *ops)
876 * connman_ipconfig_set_method:
877 * @ipconfig: ipconfig structure
878 * @method: configuration method
880 * Set the configuration method
882 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
883 enum connman_ipconfig_method method)
885 ipconfig->method = method;
891 * connman_ipconfig_bind:
892 * @ipconfig: ipconfig structure
893 * @ipaddress: ipaddress structure
895 * Bind IP address details to configuration
897 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
898 struct connman_ipaddress *ipaddress)
900 struct connman_ipconfig *origin;
902 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
904 connman_ipaddress_copy(origin->address, ipaddress);
906 connman_inet_set_address(origin->index, origin->address);
909 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
911 struct connman_ipdevice *ipdevice;
913 DBG("ipconfig %p", ipconfig);
915 if (ipconfig == NULL || ipconfig->index < 0)
918 ipdevice = g_hash_table_lookup(ipdevice_hash,
919 GINT_TO_POINTER(ipconfig->index));
920 if (ipdevice == NULL)
923 if (ipdevice->config == ipconfig)
926 if (ipdevice->config != NULL) {
927 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
929 connman_ipaddress_clear(ipdevice->config->system);
931 connman_ipconfig_unref(ipdevice->config);
934 ipdevice->config = connman_ipconfig_ref(ipconfig);
936 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
941 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
943 struct connman_ipdevice *ipdevice;
945 DBG("ipconfig %p", ipconfig);
947 if (ipconfig == NULL || ipconfig->index < 0)
950 ipdevice = g_hash_table_lookup(ipdevice_hash,
951 GINT_TO_POINTER(ipconfig->index));
952 if (ipdevice == NULL)
955 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
958 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
960 connman_ipaddress_clear(ipdevice->config->system);
962 connman_ipconfig_unref(ipdevice->config);
963 ipdevice->config = NULL;
968 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
971 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
973 case CONNMAN_IPCONFIG_METHOD_OFF:
975 case CONNMAN_IPCONFIG_METHOD_FIXED:
977 case CONNMAN_IPCONFIG_METHOD_MANUAL:
979 case CONNMAN_IPCONFIG_METHOD_DHCP:
986 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
988 if (g_strcmp0(method, "off") == 0)
989 return CONNMAN_IPCONFIG_METHOD_OFF;
990 else if (g_strcmp0(method, "fixed") == 0)
991 return CONNMAN_IPCONFIG_METHOD_FIXED;
992 else if (g_strcmp0(method, "manual") == 0)
993 return CONNMAN_IPCONFIG_METHOD_MANUAL;
994 else if (g_strcmp0(method, "dhcp") == 0)
995 return CONNMAN_IPCONFIG_METHOD_DHCP;
997 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1000 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1001 DBusMessageIter *iter)
1005 str = __connman_ipconfig_method2string(ipconfig->method);
1009 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1011 if (ipconfig->system == NULL)
1014 if (ipconfig->system->local != NULL) {
1016 struct in_addr netmask;
1019 connman_dbus_dict_append_basic(iter, "Address",
1020 DBUS_TYPE_STRING, &ipconfig->system->local);
1022 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1023 netmask.s_addr = htonl(addr);
1024 mask = inet_ntoa(netmask);
1025 connman_dbus_dict_append_basic(iter, "Netmask",
1026 DBUS_TYPE_STRING, &mask);
1030 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1031 DBusMessageIter *iter)
1035 str = __connman_ipconfig_method2string(ipconfig->method);
1039 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1041 switch (ipconfig->method) {
1042 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1043 case CONNMAN_IPCONFIG_METHOD_OFF:
1044 case CONNMAN_IPCONFIG_METHOD_FIXED:
1045 case CONNMAN_IPCONFIG_METHOD_DHCP:
1047 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1051 if (ipconfig->address == NULL)
1054 if (ipconfig->address->local != NULL) {
1056 struct in_addr netmask;
1059 connman_dbus_dict_append_basic(iter, "Address",
1060 DBUS_TYPE_STRING, &ipconfig->address->local);
1062 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1063 netmask.s_addr = htonl(addr);
1064 mask = inet_ntoa(netmask);
1065 connman_dbus_dict_append_basic(iter, "Netmask",
1066 DBUS_TYPE_STRING, &mask);
1070 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1071 DBusMessageIter *array)
1073 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1074 const char *address = NULL, *netmask = NULL;
1075 DBusMessageIter dict;
1077 DBG("ipconfig %p", ipconfig);
1079 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1082 dbus_message_iter_recurse(array, &dict);
1084 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1085 DBusMessageIter entry;
1089 dbus_message_iter_recurse(&dict, &entry);
1091 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1094 dbus_message_iter_get_basic(&entry, &key);
1095 dbus_message_iter_next(&entry);
1097 type = dbus_message_iter_get_arg_type(&entry);
1099 if (g_str_equal(key, "Method") == TRUE) {
1102 if (type != DBUS_TYPE_STRING)
1105 dbus_message_iter_get_basic(&entry, &str);
1106 method = __connman_ipconfig_string2method(str);
1107 } else if (g_str_equal(key, "Address") == TRUE) {
1108 if (type != DBUS_TYPE_STRING)
1111 dbus_message_iter_get_basic(&entry, &address);
1112 } else if (g_str_equal(key, "Netmask") == TRUE) {
1113 if (type != DBUS_TYPE_STRING)
1116 dbus_message_iter_get_basic(&entry, &netmask);
1119 dbus_message_iter_next(&dict);
1122 DBG("method %d address %s netmask %s", method, address, netmask);
1125 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1126 case CONNMAN_IPCONFIG_METHOD_OFF:
1127 case CONNMAN_IPCONFIG_METHOD_FIXED:
1130 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1131 if (address == NULL)
1134 ipconfig->method = method;
1135 connman_ipaddress_set(ipconfig->address, address, netmask);
1138 case CONNMAN_IPCONFIG_METHOD_DHCP:
1139 if (ipconfig->method == method)
1142 ipconfig->method = method;
1149 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1150 DBusMessageIter *iter)
1152 const char *method = "direct";
1154 connman_dbus_dict_append_basic(iter, "Method",
1155 DBUS_TYPE_STRING, &method);
1158 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1159 DBusMessageIter *iter)
1161 struct connman_ipdevice *ipdevice;
1162 const char *method = "auto";
1164 connman_dbus_dict_append_basic(iter, "Method",
1165 DBUS_TYPE_STRING, &method);
1167 ipdevice = g_hash_table_lookup(ipdevice_hash,
1168 GINT_TO_POINTER(ipconfig->index));
1169 if (ipdevice == NULL)
1172 if (ipdevice->ifname != NULL)
1173 connman_dbus_dict_append_basic(iter, "Interface",
1174 DBUS_TYPE_STRING, &ipdevice->ifname);
1176 if (ipdevice->address != NULL)
1177 connman_dbus_dict_append_basic(iter, "Address",
1178 DBUS_TYPE_STRING, &ipdevice->address);
1180 if (ipdevice->mtu > 0)
1181 connman_dbus_dict_append_basic(iter, "MTU",
1182 DBUS_TYPE_UINT16, &ipdevice->mtu);
1185 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1186 GKeyFile *keyfile, const char *identifier, const char *prefix)
1188 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1193 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1194 GKeyFile *keyfile, const char *identifier, const char *prefix)
1196 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1201 int __connman_ipconfig_init(void)
1205 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1206 NULL, free_ipdevice);
1211 void __connman_ipconfig_cleanup(void)
1215 g_hash_table_destroy(ipdevice_hash);
1216 ipdevice_hash = NULL;