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);
93 g_free(ipaddress->gateway);
97 static unsigned char netmask2prefixlen(const char *netmask)
99 unsigned char bits = 0;
100 in_addr_t mask = inet_network(netmask);
101 in_addr_t host = ~mask;
103 /* a valid netmask must be 2^n - 1 */
104 if ((host & (host + 1)) != 0)
107 for (; mask; mask <<= 1)
113 void connman_ipaddress_set(struct connman_ipaddress *ipaddress,
114 const char *address, const char *netmask, const char *gateway)
116 if (ipaddress == NULL)
120 ipaddress->prefixlen = netmask2prefixlen(netmask);
122 ipaddress->prefixlen = 32;
124 g_free(ipaddress->local);
125 ipaddress->local = g_strdup(address);
127 g_free(ipaddress->gateway);
128 ipaddress->gateway = g_strdup(gateway);
131 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
133 if (ipaddress == NULL)
136 ipaddress->prefixlen = 0;
138 g_free(ipaddress->local);
139 ipaddress->local = NULL;
141 g_free(ipaddress->peer);
142 ipaddress->peer = NULL;
144 g_free(ipaddress->broadcast);
145 ipaddress->broadcast = NULL;
147 g_free(ipaddress->gateway);
148 ipaddress->gateway = NULL;
151 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
152 struct connman_ipaddress *source)
154 if (ipaddress == NULL || source == NULL)
157 ipaddress->prefixlen = source->prefixlen;
159 g_free(ipaddress->local);
160 ipaddress->local = g_strdup(source->local);
162 g_free(ipaddress->peer);
163 ipaddress->peer = g_strdup(source->peer);
165 g_free(ipaddress->broadcast);
166 ipaddress->broadcast = g_strdup(source->broadcast);
168 g_free(ipaddress->gateway);
169 ipaddress->gateway = g_strdup(source->gateway);
172 static void free_address_list(struct connman_ipdevice *ipdevice)
176 for (list = ipdevice->address_list; list; list = list->next) {
177 struct connman_ipaddress *ipaddress = list->data;
179 connman_ipaddress_free(ipaddress);
183 g_slist_free(ipdevice->address_list);
184 ipdevice->address_list = NULL;
187 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
188 unsigned char prefixlen, const char *local)
192 for (list = ipdevice->address_list; list; list = list->next) {
193 struct connman_ipaddress *ipaddress = list->data;
195 if (g_strcmp0(ipaddress->local, local) == 0 &&
196 ipaddress->prefixlen == prefixlen)
203 static const char *type2str(unsigned short type)
208 case ARPHRD_LOOPBACK:
221 static const char *scope2str(unsigned char scope)
233 static void free_ipdevice(gpointer data)
235 struct connman_ipdevice *ipdevice = data;
237 connman_info("%s {remove} index %d", ipdevice->ifname,
240 if (ipdevice->config != NULL)
241 connman_ipconfig_unref(ipdevice->config);
243 free_address_list(ipdevice);
244 g_free(ipdevice->gateway);
246 g_free(ipdevice->address);
247 g_free(ipdevice->ifname);
251 static GSList *driver_list = NULL;
253 static gint compare_priority(gconstpointer a, gconstpointer b)
255 const struct connman_ipconfig_driver *driver1 = a;
256 const struct connman_ipconfig_driver *driver2 = b;
258 return driver2->priority - driver1->priority;
262 * connman_ipconfig_driver_register:
263 * @driver: IP configuration driver
265 * Register a new IP configuration driver
267 * Returns: %0 on success
269 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
271 DBG("driver %p name %s", driver, driver->name);
273 driver_list = g_slist_insert_sorted(driver_list, driver,
280 * connman_ipconfig_driver_unregister:
281 * @driver: IP configuration driver
283 * Remove a previously registered IP configuration driver.
285 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
287 DBG("driver %p name %s", driver, driver->name);
289 driver_list = g_slist_remove(driver_list, driver);
292 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
296 DBG("ipconfig %p", ipdevice->config);
298 if (ipdevice->config == NULL)
301 switch (ipdevice->config->method) {
302 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
303 case CONNMAN_IPCONFIG_METHOD_OFF:
304 case CONNMAN_IPCONFIG_METHOD_FIXED:
305 case CONNMAN_IPCONFIG_METHOD_MANUAL:
307 case CONNMAN_IPCONFIG_METHOD_DHCP:
311 if (ipdevice->driver != NULL)
314 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
315 if (ipdevice->driver_config == NULL)
318 for (list = driver_list; list; list = list->next) {
319 struct connman_ipconfig_driver *driver = list->data;
321 if (driver->request(ipdevice->driver_config) == 0) {
322 ipdevice->driver = driver;
327 if (ipdevice->driver == NULL) {
328 connman_ipconfig_unref(ipdevice->driver_config);
329 ipdevice->driver_config = NULL;
333 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
335 DBG("ipconfig %p", ipdevice->config);
337 if (ipdevice->config == NULL)
340 if (ipdevice->driver == NULL)
343 ipdevice->driver->release(ipdevice->driver_config);
345 ipdevice->driver = NULL;
347 connman_ipconfig_unref(ipdevice->driver_config);
348 ipdevice->driver_config = NULL;
350 connman_inet_clear_address(ipdevice->index);
353 static void update_stats(struct connman_ipdevice *ipdevice,
354 struct rtnl_link_stats *stats)
356 if (stats->rx_packets == 0 && stats->tx_packets == 0)
359 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
360 stats->rx_packets, stats->rx_bytes);
361 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
362 stats->tx_packets, stats->tx_bytes);
364 if (ipdevice->config == NULL)
367 ipdevice->rx_bytes = stats->rx_bytes;
368 ipdevice->tx_bytes = stats->tx_bytes;
370 __connman_counter_notify(ipdevice->ifname,
371 ipdevice->rx_bytes, ipdevice->tx_bytes);
374 void __connman_ipconfig_newlink(int index, unsigned short type,
375 unsigned int flags, const char *address,
377 struct rtnl_link_stats *stats)
379 struct connman_ipdevice *ipdevice;
382 gboolean up = FALSE, down = FALSE;
383 gboolean lower_up = FALSE, lower_down = FALSE;
386 DBG("index %d", index);
388 if (type == ARPHRD_LOOPBACK)
391 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
392 if (ipdevice != NULL)
395 ifname = connman_inet_ifname(index);
397 if (__connman_element_device_isfiltered(ifname) == TRUE) {
398 connman_info("Ignoring interface %s (filtered)", ifname);
403 ipdevice = g_try_new0(struct connman_ipdevice, 1);
404 if (ipdevice == NULL) {
409 ipdevice->index = index;
410 ipdevice->ifname = ifname;
411 ipdevice->type = type;
413 ipdevice->address = g_strdup(address);
415 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
417 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
418 index, type, type2str(type));
423 update_stats(ipdevice, stats);
425 if (flags == ipdevice->flags)
428 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
435 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
436 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
437 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
438 (IFF_RUNNING | IFF_LOWER_UP))
440 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
444 ipdevice->flags = flags;
446 str = g_string_new(NULL);
451 g_string_append(str, "UP");
453 g_string_append(str, "DOWN");
455 if (flags & IFF_RUNNING)
456 g_string_append(str, ",RUNNING");
458 if (flags & IFF_LOWER_UP)
459 g_string_append(str, ",LOWER_UP");
461 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
464 g_string_free(str, TRUE);
466 for (list = g_list_first(ipconfig_list); list;
467 list = g_list_next(list)) {
468 struct connman_ipconfig *ipconfig = list->data;
470 if (index != ipconfig->index)
473 if (ipconfig->ops == NULL)
476 if (up == TRUE && ipconfig->ops->up)
477 ipconfig->ops->up(ipconfig);
478 if (lower_up == TRUE && ipconfig->ops->lower_up)
479 ipconfig->ops->lower_up(ipconfig);
481 if (lower_down == TRUE && ipconfig->ops->lower_down)
482 ipconfig->ops->lower_down(ipconfig);
483 if (down == TRUE && ipconfig->ops->down)
484 ipconfig->ops->down(ipconfig);
488 __connman_ipconfig_lower_up(ipdevice);
490 __connman_ipconfig_lower_down(ipdevice);
493 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
495 struct connman_ipdevice *ipdevice;
498 DBG("index %d", index);
500 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
501 if (ipdevice == NULL)
504 update_stats(ipdevice, stats);
506 for (list = g_list_first(ipconfig_list); list;
507 list = g_list_next(list)) {
508 struct connman_ipconfig *ipconfig = list->data;
510 if (index != ipconfig->index)
513 ipconfig->index = -1;
515 if (ipconfig->ops == NULL)
518 if (ipconfig->ops->lower_down)
519 ipconfig->ops->lower_down(ipconfig);
520 if (ipconfig->ops->down)
521 ipconfig->ops->down(ipconfig);
524 __connman_ipconfig_lower_down(ipdevice);
526 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
529 void __connman_ipconfig_newaddr(int index, const char *label,
530 unsigned char prefixlen, const char *address)
532 struct connman_ipdevice *ipdevice;
533 struct connman_ipaddress *ipaddress;
536 DBG("index %d", index);
538 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
539 if (ipdevice == NULL)
542 ipaddress = connman_ipaddress_alloc();
543 if (ipaddress == NULL)
546 ipaddress->prefixlen = prefixlen;
547 ipaddress->local = g_strdup(address);
549 ipdevice->address_list = g_slist_append(ipdevice->address_list,
552 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
553 address, prefixlen, label);
555 if (ipdevice->config != NULL)
556 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
558 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
561 if (g_slist_length(ipdevice->address_list) > 1)
564 for (list = g_list_first(ipconfig_list); list;
565 list = g_list_next(list)) {
566 struct connman_ipconfig *ipconfig = list->data;
568 if (index != ipconfig->index)
571 if (ipconfig->ops == NULL)
574 if (ipconfig->ops->ip_bound)
575 ipconfig->ops->ip_bound(ipconfig);
579 void __connman_ipconfig_deladdr(int index, const char *label,
580 unsigned char prefixlen, const char *address)
582 struct connman_ipdevice *ipdevice;
583 struct connman_ipaddress *ipaddress;
586 DBG("index %d", index);
588 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
589 if (ipdevice == NULL)
592 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
593 if (ipaddress == NULL)
596 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
599 connman_ipaddress_free(ipaddress);
601 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
602 address, prefixlen, label);
604 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
607 if (g_slist_length(ipdevice->address_list) > 0)
610 for (list = g_list_first(ipconfig_list); list;
611 list = g_list_next(list)) {
612 struct connman_ipconfig *ipconfig = list->data;
614 if (index != ipconfig->index)
617 if (ipconfig->ops == NULL)
620 if (ipconfig->ops->ip_release)
621 ipconfig->ops->ip_release(ipconfig);
625 void __connman_ipconfig_newroute(int index, unsigned char scope,
626 const char *dst, const char *gateway)
628 struct connman_ipdevice *ipdevice;
630 DBG("index %d", index);
632 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
633 if (ipdevice == NULL)
636 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
637 g_free(ipdevice->gateway);
638 ipdevice->gateway = g_strdup(gateway);
641 connman_info("%s {add} route %s gw %s scope %u <%s>",
642 ipdevice->ifname, dst, gateway,
643 scope, scope2str(scope));
646 void __connman_ipconfig_delroute(int index, unsigned char scope,
647 const char *dst, const char *gateway)
649 struct connman_ipdevice *ipdevice;
651 DBG("index %d", index);
653 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
654 if (ipdevice == NULL)
657 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
658 g_free(ipdevice->gateway);
659 ipdevice->gateway = NULL;
662 connman_info("%s {del} route %s gw %s scope %u <%s>",
663 ipdevice->ifname, dst, gateway,
664 scope, scope2str(scope));
667 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
672 keys = g_hash_table_get_keys(ipdevice_hash);
676 for (list = g_list_first(keys); list; list = g_list_next(list)) {
677 int index = GPOINTER_TO_INT(list->data);
679 function(index, user_data);
685 unsigned short __connman_ipconfig_get_type(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->type;
696 unsigned int __connman_ipconfig_get_flags(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->flags;
707 const char *__connman_ipconfig_get_gateway(int index)
709 struct connman_ipdevice *ipdevice;
711 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
712 if (ipdevice == NULL)
715 return ipdevice->gateway;
719 * connman_ipconfig_create:
721 * Allocate a new ipconfig structure.
723 * Returns: a newly-allocated #connman_ipconfig structure
725 struct connman_ipconfig *connman_ipconfig_create(int index)
727 struct connman_ipconfig *ipconfig;
729 DBG("index %d", index);
731 ipconfig = g_try_new0(struct connman_ipconfig, 1);
732 if (ipconfig == NULL)
735 ipconfig->refcount = 1;
737 ipconfig->index = index;
739 ipconfig->address = connman_ipaddress_alloc();
740 if (ipconfig->address == NULL) {
745 ipconfig->system = connman_ipaddress_alloc();
747 DBG("ipconfig %p", ipconfig);
753 * connman_ipconfig_clone:
755 * Clone an ipconfig structure and create new reference.
757 * Returns: a newly-allocated #connman_ipconfig structure
759 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
761 struct connman_ipconfig *ipconfig_clone;
763 DBG("ipconfig %p", ipconfig);
765 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
766 if (ipconfig_clone == NULL)
769 ipconfig_clone->refcount = 1;
771 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
773 ipconfig_clone->index = -1;
775 return ipconfig_clone;
779 * connman_ipconfig_ref:
780 * @ipconfig: ipconfig structure
782 * Increase reference counter of ipconfig
784 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
786 g_atomic_int_inc(&ipconfig->refcount);
792 * connman_ipconfig_unref:
793 * @ipconfig: ipconfig structure
795 * Decrease reference counter of ipconfig
797 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
799 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
800 __connman_ipconfig_disable(ipconfig);
802 connman_ipconfig_set_ops(ipconfig, NULL);
804 if (ipconfig->origin != NULL) {
805 connman_ipconfig_unref(ipconfig->origin);
806 ipconfig->origin = NULL;
809 connman_ipaddress_free(ipconfig->system);
810 connman_ipaddress_free(ipconfig->address);
816 * connman_ipconfig_get_data:
817 * @ipconfig: ipconfig structure
819 * Get private data pointer
821 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
823 return ipconfig->ops_data;
827 * connman_ipconfig_set_data:
828 * @ipconfig: ipconfig structure
829 * @data: data pointer
831 * Set private data pointer
833 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
835 ipconfig->ops_data = data;
839 * connman_ipconfig_get_index:
840 * @ipconfig: ipconfig structure
842 * Get interface index
844 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
846 if (ipconfig->origin != NULL)
847 return ipconfig->origin->index;
849 return ipconfig->index;
853 * connman_ipconfig_get_ifname:
854 * @ipconfig: ipconfig structure
858 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
860 struct connman_ipdevice *ipdevice;
862 if (ipconfig->index < 0)
865 ipdevice = g_hash_table_lookup(ipdevice_hash,
866 GINT_TO_POINTER(ipconfig->index));
867 if (ipdevice == NULL)
870 return ipdevice->ifname;
874 * connman_ipconfig_set_ops:
875 * @ipconfig: ipconfig structure
876 * @ops: operation callbacks
878 * Set the operation callbacks
880 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
881 const struct connman_ipconfig_ops *ops)
887 * connman_ipconfig_set_method:
888 * @ipconfig: ipconfig structure
889 * @method: configuration method
891 * Set the configuration method
893 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
894 enum connman_ipconfig_method method)
896 ipconfig->method = method;
901 enum connman_ipconfig_method __connman_ipconfig_get_method(
902 struct connman_ipconfig *ipconfig)
904 if (ipconfig == NULL)
905 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
907 return ipconfig->method;
911 * connman_ipconfig_bind:
912 * @ipconfig: ipconfig structure
913 * @ipaddress: ipaddress structure
915 * Bind IP address details to configuration
917 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
918 struct connman_ipaddress *ipaddress)
920 struct connman_ipconfig *origin;
922 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
924 connman_ipaddress_copy(origin->address, ipaddress);
926 connman_inet_set_address(origin->index, origin->address);
929 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
933 switch (ipconfig->method) {
934 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
935 case CONNMAN_IPCONFIG_METHOD_OFF:
936 case CONNMAN_IPCONFIG_METHOD_FIXED:
937 case CONNMAN_IPCONFIG_METHOD_DHCP:
939 case CONNMAN_IPCONFIG_METHOD_MANUAL:
940 return connman_inet_set_address(ipconfig->index,
947 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
951 if (ipconfig == NULL)
954 DBG("method %d", ipconfig->method);
956 switch (ipconfig->method) {
957 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
958 case CONNMAN_IPCONFIG_METHOD_OFF:
959 case CONNMAN_IPCONFIG_METHOD_FIXED:
960 case CONNMAN_IPCONFIG_METHOD_DHCP:
962 case CONNMAN_IPCONFIG_METHOD_MANUAL:
963 return connman_inet_clear_address(ipconfig->index);
970 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
972 struct connman_ipdevice *ipdevice;
974 DBG("ipconfig %p", ipconfig);
976 if (ipconfig == NULL || ipconfig->index < 0)
979 ipdevice = g_hash_table_lookup(ipdevice_hash,
980 GINT_TO_POINTER(ipconfig->index));
981 if (ipdevice == NULL)
984 if (ipdevice->config == ipconfig)
987 if (ipdevice->config != NULL) {
988 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
990 connman_ipaddress_clear(ipdevice->config->system);
992 connman_ipconfig_unref(ipdevice->config);
995 ipdevice->config = connman_ipconfig_ref(ipconfig);
997 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1002 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1004 struct connman_ipdevice *ipdevice;
1006 DBG("ipconfig %p", ipconfig);
1008 if (ipconfig == NULL || ipconfig->index < 0)
1011 ipdevice = g_hash_table_lookup(ipdevice_hash,
1012 GINT_TO_POINTER(ipconfig->index));
1013 if (ipdevice == NULL)
1016 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1019 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1021 connman_ipaddress_clear(ipdevice->config->system);
1023 connman_ipconfig_unref(ipdevice->config);
1024 ipdevice->config = NULL;
1029 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1032 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1034 case CONNMAN_IPCONFIG_METHOD_OFF:
1036 case CONNMAN_IPCONFIG_METHOD_FIXED:
1038 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1040 case CONNMAN_IPCONFIG_METHOD_DHCP:
1047 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1049 if (g_strcmp0(method, "off") == 0)
1050 return CONNMAN_IPCONFIG_METHOD_OFF;
1051 else if (g_strcmp0(method, "fixed") == 0)
1052 return CONNMAN_IPCONFIG_METHOD_FIXED;
1053 else if (g_strcmp0(method, "manual") == 0)
1054 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1055 else if (g_strcmp0(method, "dhcp") == 0)
1056 return CONNMAN_IPCONFIG_METHOD_DHCP;
1058 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1061 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1062 DBusMessageIter *iter)
1066 str = __connman_ipconfig_method2string(ipconfig->method);
1070 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1072 if (ipconfig->system == NULL)
1075 if (ipconfig->system->local != NULL) {
1077 struct in_addr netmask;
1080 connman_dbus_dict_append_basic(iter, "Address",
1081 DBUS_TYPE_STRING, &ipconfig->system->local);
1083 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1084 netmask.s_addr = htonl(addr);
1085 mask = inet_ntoa(netmask);
1086 connman_dbus_dict_append_basic(iter, "Netmask",
1087 DBUS_TYPE_STRING, &mask);
1091 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1092 DBusMessageIter *iter)
1096 str = __connman_ipconfig_method2string(ipconfig->method);
1100 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1102 switch (ipconfig->method) {
1103 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1104 case CONNMAN_IPCONFIG_METHOD_OFF:
1105 case CONNMAN_IPCONFIG_METHOD_FIXED:
1106 case CONNMAN_IPCONFIG_METHOD_DHCP:
1108 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1112 if (ipconfig->address == NULL)
1115 if (ipconfig->address->local != NULL) {
1117 struct in_addr netmask;
1120 connman_dbus_dict_append_basic(iter, "Address",
1121 DBUS_TYPE_STRING, &ipconfig->address->local);
1123 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1124 netmask.s_addr = htonl(addr);
1125 mask = inet_ntoa(netmask);
1126 connman_dbus_dict_append_basic(iter, "Netmask",
1127 DBUS_TYPE_STRING, &mask);
1131 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1132 DBusMessageIter *array)
1134 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1135 const char *address = NULL, *netmask = NULL, *gateway = NULL;
1136 DBusMessageIter dict;
1138 DBG("ipconfig %p", ipconfig);
1140 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1143 dbus_message_iter_recurse(array, &dict);
1145 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1146 DBusMessageIter entry;
1150 dbus_message_iter_recurse(&dict, &entry);
1152 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1155 dbus_message_iter_get_basic(&entry, &key);
1156 dbus_message_iter_next(&entry);
1158 type = dbus_message_iter_get_arg_type(&entry);
1160 if (g_str_equal(key, "Method") == TRUE) {
1163 if (type != DBUS_TYPE_STRING)
1166 dbus_message_iter_get_basic(&entry, &str);
1167 method = __connman_ipconfig_string2method(str);
1168 } else if (g_str_equal(key, "Address") == TRUE) {
1169 if (type != DBUS_TYPE_STRING)
1172 dbus_message_iter_get_basic(&entry, &address);
1173 } else if (g_str_equal(key, "Netmask") == TRUE) {
1174 if (type != DBUS_TYPE_STRING)
1177 dbus_message_iter_get_basic(&entry, &netmask);
1178 } else if (g_str_equal(key, "Gateway") == TRUE) {
1179 if (type != DBUS_TYPE_STRING)
1182 dbus_message_iter_get_basic(&entry, &gateway);
1184 dbus_message_iter_next(&dict);
1187 DBG("method %d address %s netmask %s gateway %s",
1188 method, address, netmask, gateway);
1191 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1192 case CONNMAN_IPCONFIG_METHOD_OFF:
1193 case CONNMAN_IPCONFIG_METHOD_FIXED:
1196 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1197 if (address == NULL)
1200 ipconfig->method = method;
1201 connman_ipaddress_set(ipconfig->address,
1202 address, netmask, gateway);
1205 case CONNMAN_IPCONFIG_METHOD_DHCP:
1206 if (ipconfig->method == method)
1209 ipconfig->method = method;
1216 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1217 DBusMessageIter *iter)
1219 const char *method = "direct";
1221 connman_dbus_dict_append_basic(iter, "Method",
1222 DBUS_TYPE_STRING, &method);
1225 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1226 DBusMessageIter *iter)
1228 struct connman_ipdevice *ipdevice;
1229 const char *method = "auto";
1231 connman_dbus_dict_append_basic(iter, "Method",
1232 DBUS_TYPE_STRING, &method);
1234 ipdevice = g_hash_table_lookup(ipdevice_hash,
1235 GINT_TO_POINTER(ipconfig->index));
1236 if (ipdevice == NULL)
1239 if (ipdevice->ifname != NULL)
1240 connman_dbus_dict_append_basic(iter, "Interface",
1241 DBUS_TYPE_STRING, &ipdevice->ifname);
1243 if (ipdevice->address != NULL)
1244 connman_dbus_dict_append_basic(iter, "Address",
1245 DBUS_TYPE_STRING, &ipdevice->address);
1247 if (ipdevice->mtu > 0)
1248 connman_dbus_dict_append_basic(iter, "MTU",
1249 DBUS_TYPE_UINT16, &ipdevice->mtu);
1252 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1253 GKeyFile *keyfile, const char *identifier, const char *prefix)
1258 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1260 key = g_strdup_printf("%smethod", prefix);
1261 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1263 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1265 ipconfig->method = __connman_ipconfig_string2method(method);
1268 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1269 ipconfig->address->prefixlen = g_key_file_get_integer(
1270 keyfile, identifier, key, NULL);
1273 key = g_strdup_printf("%slocal_address", prefix);
1274 ipconfig->address->local = g_key_file_get_string(
1275 keyfile, identifier, key, NULL);
1278 key = g_strdup_printf("%speer_address", prefix);
1279 ipconfig->address->peer = g_key_file_get_string(
1280 keyfile, identifier, key, NULL);
1283 key = g_strdup_printf("%sbroadcast_address", prefix);
1284 ipconfig->address->broadcast = g_key_file_get_string(
1285 keyfile, identifier, key, NULL);
1288 key = g_strdup_printf("%sgateway", prefix);
1289 ipconfig->address->gateway = g_key_file_get_string(
1290 keyfile, identifier, key, NULL);
1296 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1297 GKeyFile *keyfile, const char *identifier, const char *prefix)
1302 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1304 method = __connman_ipconfig_method2string(ipconfig->method);
1306 key = g_strdup_printf("%smethod", prefix);
1307 g_key_file_set_string(keyfile, identifier, key, method);
1310 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1311 g_key_file_set_integer(keyfile, identifier,
1312 key, ipconfig->address->prefixlen);
1315 key = g_strdup_printf("%slocal_address", prefix);
1316 if (ipconfig->address->local != NULL)
1317 g_key_file_set_string(keyfile, identifier,
1318 key, ipconfig->address->local);
1321 key = g_strdup_printf("%speer_address", prefix);
1322 if (ipconfig->address->peer != NULL)
1323 g_key_file_set_string(keyfile, identifier,
1324 key, ipconfig->address->peer);
1327 key = g_strdup_printf("%sbroadcast_address", prefix);
1328 if (ipconfig->address->broadcast != NULL)
1329 g_key_file_set_string(keyfile, identifier,
1330 key, ipconfig->address->broadcast);
1333 key = g_strdup_printf("%sgateway", prefix);
1334 if (ipconfig->address->gateway != NULL)
1335 g_key_file_set_string(keyfile, identifier,
1336 key, ipconfig->address->gateway);
1342 int __connman_ipconfig_init(void)
1346 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1347 NULL, free_ipdevice);
1352 void __connman_ipconfig_cleanup(void)
1356 g_hash_table_destroy(ipdevice_hash);
1357 ipdevice_hash = NULL;