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
28 #include <net/if_arp.h>
29 #include <linux/if_link.h>
34 #define IFF_LOWER_UP 0x10000
41 struct connman_ipconfig {
44 enum connman_ipconfig_type type;
46 struct connman_ipconfig *origin;
48 const struct connman_ipconfig_ops *ops;
51 enum connman_ipconfig_method method;
52 struct connman_ipaddress *address;
53 struct connman_ipaddress *system;
55 int ipv6_privacy_config;
58 struct connman_ipdevice {
80 struct connman_ipconfig *config_ipv4;
81 struct connman_ipconfig *config_ipv6;
83 gboolean ipv6_enabled;
87 static GHashTable *ipdevice_hash = NULL;
88 static GList *ipconfig_list = NULL;
90 struct connman_ipaddress *connman_ipaddress_alloc(int family)
92 struct connman_ipaddress *ipaddress;
94 ipaddress = g_try_new0(struct connman_ipaddress, 1);
95 if (ipaddress == NULL)
98 ipaddress->family = family;
99 ipaddress->prefixlen = 0;
100 ipaddress->local = NULL;
101 ipaddress->peer = NULL;
102 ipaddress->broadcast = NULL;
103 ipaddress->gateway = NULL;
108 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
110 if (ipaddress == NULL)
113 g_free(ipaddress->broadcast);
114 g_free(ipaddress->peer);
115 g_free(ipaddress->local);
116 g_free(ipaddress->gateway);
120 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
129 mask = inet_network(netmask);
132 /* a valid netmask must be 2^n - 1 */
133 if ((host & (host + 1)) != 0)
137 for (; mask; mask <<= 1)
143 static gboolean check_ipv6_address(const char *address)
145 unsigned char buf[sizeof(struct in6_addr)];
148 err = inet_pton(AF_INET6, address, buf);
155 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
157 unsigned char prefix_length,
160 if (ipaddress == NULL)
163 if (check_ipv6_address(address) == FALSE)
166 if (check_ipv6_address(gateway) == FALSE)
169 DBG("prefix_len %d address %s gateway %s",
170 prefix_length, address, gateway);
172 ipaddress->family = AF_INET6;
174 ipaddress->prefixlen = prefix_length;
176 g_free(ipaddress->local);
177 ipaddress->local = g_strdup(address);
179 g_free(ipaddress->gateway);
180 ipaddress->gateway = g_strdup(gateway);
185 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
186 const char *address, const char *netmask, const char *gateway)
188 if (ipaddress == NULL)
191 ipaddress->family = AF_INET;
193 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
195 g_free(ipaddress->local);
196 ipaddress->local = g_strdup(address);
198 g_free(ipaddress->gateway);
199 ipaddress->gateway = g_strdup(gateway);
204 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
207 if (ipaddress == NULL)
210 g_free(ipaddress->peer);
211 ipaddress->peer = g_strdup(peer);
214 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
216 if (ipaddress == NULL)
219 ipaddress->prefixlen = 0;
221 g_free(ipaddress->local);
222 ipaddress->local = NULL;
224 g_free(ipaddress->peer);
225 ipaddress->peer = NULL;
227 g_free(ipaddress->broadcast);
228 ipaddress->broadcast = NULL;
230 g_free(ipaddress->gateway);
231 ipaddress->gateway = NULL;
234 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
235 struct connman_ipaddress *source)
237 if (ipaddress == NULL || source == NULL)
240 ipaddress->family = source->family;
241 ipaddress->prefixlen = source->prefixlen;
243 g_free(ipaddress->local);
244 ipaddress->local = g_strdup(source->local);
246 g_free(ipaddress->peer);
247 ipaddress->peer = g_strdup(source->peer);
249 g_free(ipaddress->broadcast);
250 ipaddress->broadcast = g_strdup(source->broadcast);
252 g_free(ipaddress->gateway);
253 ipaddress->gateway = g_strdup(source->gateway);
256 static void free_address_list(struct connman_ipdevice *ipdevice)
260 for (list = ipdevice->address_list; list; list = list->next) {
261 struct connman_ipaddress *ipaddress = list->data;
263 connman_ipaddress_free(ipaddress);
267 g_slist_free(ipdevice->address_list);
268 ipdevice->address_list = NULL;
271 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
272 unsigned char prefixlen, const char *local)
276 for (list = ipdevice->address_list; list; list = list->next) {
277 struct connman_ipaddress *ipaddress = list->data;
279 if (g_strcmp0(ipaddress->local, local) == 0 &&
280 ipaddress->prefixlen == prefixlen)
287 static const char *type2str(unsigned short type)
292 case ARPHRD_LOOPBACK:
305 static const char *scope2str(unsigned char scope)
317 static gboolean get_ipv6_state(gchar *ifname)
322 gboolean enabled = FALSE;
325 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
327 path = g_strdup_printf(
328 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
333 f = fopen(path, "r");
338 if (fscanf(f, "%d", &disabled) > 0)
346 static void set_ipv6_state(gchar *ifname, gboolean enable)
352 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
354 path = g_strdup_printf(
355 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
360 f = fopen(path, "r+");
375 static int get_ipv6_privacy(gchar *ifname)
384 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
390 f = fopen(path, "r");
397 if (fscanf(f, "%d", &value) <= 0)
405 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
406 * The privacy extension is described in RFC 3041 and RFC 4941
408 static void set_ipv6_privacy(gchar *ifname, int value)
416 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
425 f = fopen(path, "r+");
432 fprintf(f, "%d", value);
436 static void free_ipdevice(gpointer data)
438 struct connman_ipdevice *ipdevice = data;
440 connman_info("%s {remove} index %d", ipdevice->ifname,
443 if (ipdevice->config_ipv4 != NULL) {
444 connman_ipconfig_unref(ipdevice->config_ipv4);
445 ipdevice->config_ipv4 = NULL;
448 if (ipdevice->config_ipv6 != NULL) {
449 connman_ipconfig_unref(ipdevice->config_ipv6);
450 ipdevice->config_ipv6 = NULL;
453 free_address_list(ipdevice);
454 g_free(ipdevice->ipv4_gateway);
455 g_free(ipdevice->ipv6_gateway);
456 g_free(ipdevice->pac);
458 g_free(ipdevice->address);
460 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
461 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
463 g_free(ipdevice->ifname);
467 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
469 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
470 ipdevice->config_ipv6);
473 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
475 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
476 ipdevice->config_ipv6);
478 if (ipdevice->config_ipv4)
479 connman_inet_clear_address(ipdevice->index,
480 ipdevice->config_ipv4->address);
482 if (ipdevice->config_ipv6)
483 connman_inet_clear_ipv6_address(ipdevice->index,
484 ipdevice->config_ipv6->address->local,
485 ipdevice->config_ipv6->address->prefixlen);
488 static void update_stats(struct connman_ipdevice *ipdevice,
489 struct rtnl_link_stats *stats)
491 struct connman_service *service;
493 if (stats->rx_packets == 0 && stats->tx_packets == 0)
496 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
497 stats->rx_packets, stats->rx_bytes);
498 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
499 stats->tx_packets, stats->tx_bytes);
501 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
504 if (ipdevice->config_ipv4)
505 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
506 else if (ipdevice->config_ipv6)
507 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
514 ipdevice->rx_packets = stats->rx_packets;
515 ipdevice->tx_packets = stats->tx_packets;
516 ipdevice->rx_bytes = stats->rx_bytes;
517 ipdevice->tx_bytes = stats->tx_bytes;
518 ipdevice->rx_errors = stats->rx_errors;
519 ipdevice->tx_errors = stats->tx_errors;
520 ipdevice->rx_dropped = stats->rx_dropped;
521 ipdevice->tx_dropped = stats->tx_dropped;
523 __connman_service_notify(service,
524 ipdevice->rx_packets, ipdevice->tx_packets,
525 ipdevice->rx_bytes, ipdevice->tx_bytes,
526 ipdevice->rx_errors, ipdevice->tx_errors,
527 ipdevice->rx_dropped, ipdevice->tx_dropped);
530 void __connman_ipconfig_newlink(int index, unsigned short type,
531 unsigned int flags, const char *address,
533 struct rtnl_link_stats *stats)
535 struct connman_ipdevice *ipdevice;
538 gboolean up = FALSE, down = FALSE;
539 gboolean lower_up = FALSE, lower_down = FALSE;
541 DBG("index %d", index);
543 if (type == ARPHRD_LOOPBACK)
546 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
547 if (ipdevice != NULL)
550 ipdevice = g_try_new0(struct connman_ipdevice, 1);
551 if (ipdevice == NULL)
554 ipdevice->index = index;
555 ipdevice->ifname = connman_inet_ifname(index);
556 ipdevice->type = type;
558 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
559 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
561 ipdevice->address = g_strdup(address);
563 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
565 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
566 index, type, type2str(type));
571 update_stats(ipdevice, stats);
573 if (flags == ipdevice->flags)
576 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
583 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
584 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
585 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
586 (IFF_RUNNING | IFF_LOWER_UP))
588 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
592 ipdevice->flags = flags;
594 str = g_string_new(NULL);
599 g_string_append(str, "UP");
601 g_string_append(str, "DOWN");
603 if (flags & IFF_RUNNING)
604 g_string_append(str, ",RUNNING");
606 if (flags & IFF_LOWER_UP)
607 g_string_append(str, ",LOWER_UP");
609 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
612 g_string_free(str, TRUE);
614 for (list = g_list_first(ipconfig_list); list;
615 list = g_list_next(list)) {
616 struct connman_ipconfig *ipconfig = list->data;
618 if (index != ipconfig->index)
621 if (ipconfig->ops == NULL)
624 if (up == TRUE && ipconfig->ops->up)
625 ipconfig->ops->up(ipconfig);
626 if (lower_up == TRUE && ipconfig->ops->lower_up)
627 ipconfig->ops->lower_up(ipconfig);
629 if (lower_down == TRUE && ipconfig->ops->lower_down)
630 ipconfig->ops->lower_down(ipconfig);
631 if (down == TRUE && ipconfig->ops->down)
632 ipconfig->ops->down(ipconfig);
636 __connman_ipconfig_lower_up(ipdevice);
638 __connman_ipconfig_lower_down(ipdevice);
641 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
643 struct connman_ipdevice *ipdevice;
646 DBG("index %d", index);
648 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
649 if (ipdevice == NULL)
652 update_stats(ipdevice, stats);
654 for (list = g_list_first(ipconfig_list); list;
655 list = g_list_next(list)) {
656 struct connman_ipconfig *ipconfig = list->data;
658 if (index != ipconfig->index)
661 ipconfig->index = -1;
663 if (ipconfig->ops == NULL)
666 if (ipconfig->ops->lower_down)
667 ipconfig->ops->lower_down(ipconfig);
668 if (ipconfig->ops->down)
669 ipconfig->ops->down(ipconfig);
672 __connman_ipconfig_lower_down(ipdevice);
674 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
677 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
679 const struct connman_ipaddress *addr1 = a;
680 const struct connman_ipaddress *addr2 = b;
682 if (addr1->prefixlen != addr2->prefixlen)
683 return addr2->prefixlen - addr1->prefixlen;
685 return g_strcmp0(addr1->local, addr2->local);
688 void __connman_ipconfig_newaddr(int index, int family, const char *label,
689 unsigned char prefixlen, const char *address)
691 struct connman_ipdevice *ipdevice;
692 struct connman_ipaddress *ipaddress;
695 DBG("index %d", index);
697 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
698 if (ipdevice == NULL)
701 ipaddress = connman_ipaddress_alloc(family);
702 if (ipaddress == NULL)
705 ipaddress->prefixlen = prefixlen;
706 ipaddress->local = g_strdup(address);
708 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
709 check_duplicate_address)) {
710 connman_ipaddress_free(ipaddress);
714 ipdevice->address_list = g_slist_append(ipdevice->address_list,
717 connman_info("%s {add} address %s/%u label %s family %d",
718 ipdevice->ifname, address, prefixlen, label, family);
720 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
721 connman_ipaddress_copy(ipdevice->config_ipv4->system,
724 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
725 connman_ipaddress_copy(ipdevice->config_ipv6->system,
730 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
733 for (list = g_list_first(ipconfig_list); list;
734 list = g_list_next(list)) {
735 struct connman_ipconfig *ipconfig = list->data;
737 if (index != ipconfig->index)
740 if (ipconfig->ops == NULL)
743 if (ipconfig->ops->ip_bound)
744 ipconfig->ops->ip_bound(ipconfig);
748 void __connman_ipconfig_deladdr(int index, int family, const char *label,
749 unsigned char prefixlen, const char *address)
751 struct connman_ipdevice *ipdevice;
752 struct connman_ipaddress *ipaddress;
755 DBG("index %d", index);
757 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
758 if (ipdevice == NULL)
761 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
762 if (ipaddress == NULL)
765 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
768 connman_ipaddress_clear(ipaddress);
771 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
772 address, prefixlen, label);
774 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
777 if (g_slist_length(ipdevice->address_list) > 0)
780 for (list = g_list_first(ipconfig_list); list;
781 list = g_list_next(list)) {
782 struct connman_ipconfig *ipconfig = list->data;
784 if (index != ipconfig->index)
787 if (ipconfig->ops == NULL)
790 if (ipconfig->ops->ip_release)
791 ipconfig->ops->ip_release(ipconfig);
795 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
796 const char *dst, const char *gateway)
798 struct connman_ipdevice *ipdevice;
800 DBG("index %d", index);
802 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
803 if (ipdevice == NULL)
806 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
807 g_strcmp0(dst, "::") == 0)) {
811 if (family == AF_INET6) {
812 g_free(ipdevice->ipv6_gateway);
813 ipdevice->ipv6_gateway = g_strdup(gateway);
815 if (ipdevice->config_ipv6 != NULL &&
816 ipdevice->config_ipv6->system != NULL) {
817 g_free(ipdevice->config_ipv6->system->gateway);
818 ipdevice->config_ipv6->system->gateway =
821 } else if (family == AF_INET) {
822 g_free(ipdevice->ipv4_gateway);
823 ipdevice->ipv4_gateway = g_strdup(gateway);
825 if (ipdevice->config_ipv4 != NULL &&
826 ipdevice->config_ipv4->system != NULL) {
827 g_free(ipdevice->config_ipv4->system->gateway);
828 ipdevice->config_ipv4->system->gateway =
834 for (list = ipdevice->address_list; list; list = list->next) {
835 struct connman_ipaddress *ipaddress = list->data;
837 g_free(ipaddress->gateway);
838 ipaddress->gateway = g_strdup(gateway);
841 for (config_list = g_list_first(ipconfig_list); config_list;
842 config_list = g_list_next(config_list)) {
843 struct connman_ipconfig *ipconfig = config_list->data;
845 if (index != ipconfig->index)
848 if (ipconfig->ops == NULL)
851 if (ipconfig->ops->ip_bound)
852 ipconfig->ops->ip_bound(ipconfig);
856 connman_info("%s {add} route %s gw %s scope %u <%s>",
857 ipdevice->ifname, dst, gateway,
858 scope, scope2str(scope));
861 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
862 const char *dst, const char *gateway)
864 struct connman_ipdevice *ipdevice;
866 DBG("index %d", index);
868 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
869 if (ipdevice == NULL)
872 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
873 g_strcmp0(dst, "::") == 0)) {
877 if (family == AF_INET6) {
878 g_free(ipdevice->ipv6_gateway);
879 ipdevice->ipv6_gateway = NULL;
881 if (ipdevice->config_ipv6 != NULL &&
882 ipdevice->config_ipv6->system != NULL) {
883 g_free(ipdevice->config_ipv6->system->gateway);
884 ipdevice->config_ipv6->system->gateway = NULL;
886 } else if (family == AF_INET) {
887 g_free(ipdevice->ipv4_gateway);
888 ipdevice->ipv4_gateway = NULL;
890 if (ipdevice->config_ipv4 != NULL &&
891 ipdevice->config_ipv4->system != NULL) {
892 g_free(ipdevice->config_ipv4->system->gateway);
893 ipdevice->config_ipv4->system->gateway = NULL;
898 for (list = ipdevice->address_list; list; list = list->next) {
899 struct connman_ipaddress *ipaddress = list->data;
901 g_free(ipaddress->gateway);
902 ipaddress->gateway = NULL;
905 for (config_list = g_list_first(ipconfig_list); config_list;
906 config_list = g_list_next(config_list)) {
907 struct connman_ipconfig *ipconfig = config_list->data;
909 if (index != ipconfig->index)
912 if (ipconfig->ops == NULL)
915 if (ipconfig->ops->ip_release)
916 ipconfig->ops->ip_release(ipconfig);
920 connman_info("%s {del} route %s gw %s scope %u <%s>",
921 ipdevice->ifname, dst, gateway,
922 scope, scope2str(scope));
925 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
930 keys = g_hash_table_get_keys(ipdevice_hash);
934 for (list = g_list_first(keys); list; list = g_list_next(list)) {
935 int index = GPOINTER_TO_INT(list->data);
937 function(index, user_data);
943 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
944 struct connman_ipconfig *ipconfig)
946 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
949 unsigned short __connman_ipconfig_get_type_from_index(int index)
951 struct connman_ipdevice *ipdevice;
953 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
954 if (ipdevice == NULL)
957 return ipdevice->type;
960 unsigned int __connman_ipconfig_get_flags_from_index(int index)
962 struct connman_ipdevice *ipdevice;
964 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
965 if (ipdevice == NULL)
968 return ipdevice->flags;
971 const char *__connman_ipconfig_get_gateway_from_index(int index)
973 struct connman_ipdevice *ipdevice;
975 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
976 if (ipdevice == NULL)
979 if (ipdevice->ipv4_gateway != NULL)
980 return ipdevice->ipv4_gateway;
982 if (ipdevice->config_ipv4 != NULL &&
983 ipdevice->config_ipv4->address != NULL)
984 return ipdevice->config_ipv4->address->gateway;
986 if (ipdevice->ipv6_gateway != NULL)
987 return ipdevice->ipv6_gateway;
989 if (ipdevice->config_ipv6 != NULL &&
990 ipdevice->config_ipv6->address != NULL)
991 return ipdevice->config_ipv6->address->gateway;
996 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
998 ipconfig->index = index;
1001 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1003 if (ipconfig->address == NULL)
1006 return ipconfig->address->local;
1009 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1011 if (ipconfig->address == NULL)
1014 g_free(ipconfig->address->local);
1015 ipconfig->address->local = g_strdup(address);
1018 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1020 if (ipconfig->address == NULL)
1023 return ipconfig->address->peer;
1026 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1028 if (ipconfig->address == NULL)
1031 g_free(ipconfig->address->peer);
1032 ipconfig->address->peer = g_strdup(address);
1035 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1037 if (ipconfig->address == NULL)
1040 return ipconfig->address->broadcast;
1043 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1045 if (ipconfig->address == NULL)
1048 g_free(ipconfig->address->broadcast);
1049 ipconfig->address->broadcast = g_strdup(broadcast);
1052 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1054 if (ipconfig->address == NULL)
1057 return ipconfig->address->gateway;
1060 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1064 if (ipconfig->address == NULL)
1066 g_free(ipconfig->address->gateway);
1067 ipconfig->address->gateway = g_strdup(gateway);
1070 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1072 struct connman_service *service;
1076 if (ipconfig->address == NULL)
1079 service = __connman_service_lookup_from_index(ipconfig->index);
1080 if (service == NULL)
1083 __connman_connection_gateway_remove(service);
1085 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1086 return __connman_connection_gateway_add(service, NULL,
1087 ipconfig->address->gateway,
1088 ipconfig->address->peer);
1089 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1090 return __connman_connection_gateway_add(service,
1091 ipconfig->address->gateway,
1092 NULL, ipconfig->address->peer);
1098 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1100 struct connman_service *service;
1104 service = __connman_service_lookup_from_index(ipconfig->index);
1105 if (service != NULL)
1106 __connman_connection_gateway_remove(service);
1109 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1111 if (ipconfig->address == NULL)
1114 return ipconfig->address->prefixlen;
1117 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1119 if (ipconfig->address == NULL)
1122 ipconfig->address->prefixlen = prefixlen;
1125 static struct connman_ipconfig *create_ipv6config(int index)
1127 struct connman_ipconfig *ipv6config;
1129 DBG("index %d", index);
1131 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1132 if (ipv6config == NULL)
1135 ipv6config->refcount = 1;
1137 ipv6config->index = index;
1138 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1139 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1140 ipv6config->ipv6_privacy_config = 0;
1142 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1143 if (ipv6config->address == NULL) {
1148 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1150 DBG("ipconfig %p", ipv6config);
1156 * connman_ipconfig_create:
1158 * Allocate a new ipconfig structure.
1160 * Returns: a newly-allocated #connman_ipconfig structure
1162 struct connman_ipconfig *connman_ipconfig_create(int index,
1163 enum connman_ipconfig_type type)
1165 struct connman_ipconfig *ipconfig;
1167 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1168 return create_ipv6config(index);
1170 DBG("index %d", index);
1172 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1173 if (ipconfig == NULL)
1176 ipconfig->refcount = 1;
1178 ipconfig->index = index;
1179 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1181 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1182 if (ipconfig->address == NULL) {
1187 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1189 DBG("ipconfig %p", ipconfig);
1196 * connman_ipconfig_ref:
1197 * @ipconfig: ipconfig structure
1199 * Increase reference counter of ipconfig
1201 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1203 DBG("ipconfig %p refcount %d", ipconfig,
1204 g_atomic_int_get(&ipconfig->refcount) + 1);
1206 g_atomic_int_inc(&ipconfig->refcount);
1212 * connman_ipconfig_unref:
1213 * @ipconfig: ipconfig structure
1215 * Decrease reference counter of ipconfig
1217 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1219 if (ipconfig == NULL)
1222 DBG("ipconfig %p refcount %d", ipconfig,
1223 g_atomic_int_get(&ipconfig->refcount) - 1);
1225 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1226 __connman_ipconfig_disable(ipconfig);
1228 connman_ipconfig_set_ops(ipconfig, NULL);
1230 if (ipconfig->origin != NULL) {
1231 connman_ipconfig_unref(ipconfig->origin);
1232 ipconfig->origin = NULL;
1235 connman_ipaddress_free(ipconfig->system);
1236 connman_ipaddress_free(ipconfig->address);
1242 * connman_ipconfig_get_data:
1243 * @ipconfig: ipconfig structure
1245 * Get private data pointer
1247 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1249 if (ipconfig == NULL)
1252 return ipconfig->ops_data;
1256 * connman_ipconfig_set_data:
1257 * @ipconfig: ipconfig structure
1258 * @data: data pointer
1260 * Set private data pointer
1262 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1264 ipconfig->ops_data = data;
1268 * connman_ipconfig_get_index:
1269 * @ipconfig: ipconfig structure
1271 * Get interface index
1273 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1275 if (ipconfig == NULL)
1278 if (ipconfig->origin != NULL)
1279 return ipconfig->origin->index;
1281 return ipconfig->index;
1285 * connman_ipconfig_get_ifname:
1286 * @ipconfig: ipconfig structure
1288 * Get interface name
1290 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1292 struct connman_ipdevice *ipdevice;
1294 if (ipconfig == NULL)
1297 if (ipconfig->index < 0)
1300 ipdevice = g_hash_table_lookup(ipdevice_hash,
1301 GINT_TO_POINTER(ipconfig->index));
1302 if (ipdevice == NULL)
1305 return ipdevice->ifname;
1309 * connman_ipconfig_set_ops:
1310 * @ipconfig: ipconfig structure
1311 * @ops: operation callbacks
1313 * Set the operation callbacks
1315 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1316 const struct connman_ipconfig_ops *ops)
1318 ipconfig->ops = ops;
1322 * connman_ipconfig_set_method:
1323 * @ipconfig: ipconfig structure
1324 * @method: configuration method
1326 * Set the configuration method
1328 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1329 enum connman_ipconfig_method method)
1331 ipconfig->method = method;
1336 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1338 if (ipconfig == NULL)
1339 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1341 return ipconfig->method;
1344 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1348 switch (ipconfig->method) {
1349 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1350 case CONNMAN_IPCONFIG_METHOD_OFF:
1351 case CONNMAN_IPCONFIG_METHOD_AUTO:
1353 case CONNMAN_IPCONFIG_METHOD_FIXED:
1354 case CONNMAN_IPCONFIG_METHOD_DHCP:
1355 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1356 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1357 return connman_inet_set_address(ipconfig->index,
1359 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1360 return connman_inet_set_ipv6_address(
1361 ipconfig->index, ipconfig->address);
1367 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1373 if (ipconfig == NULL)
1376 DBG("method %d", ipconfig->method);
1378 switch (ipconfig->method) {
1379 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1380 case CONNMAN_IPCONFIG_METHOD_OFF:
1381 case CONNMAN_IPCONFIG_METHOD_AUTO:
1383 case CONNMAN_IPCONFIG_METHOD_FIXED:
1384 case CONNMAN_IPCONFIG_METHOD_DHCP:
1385 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1386 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1387 err = connman_inet_clear_address(ipconfig->index,
1389 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1390 err = connman_inet_clear_ipv6_address(
1392 ipconfig->address->local,
1393 ipconfig->address->prefixlen);
1397 connman_ipaddress_clear(ipconfig->address);
1405 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1408 struct connman_ipdevice *ipdevice;
1410 DBG("ipconfig %p", ipconfig);
1412 if (ipconfig == NULL || ipconfig->index < 0)
1415 ipdevice = g_hash_table_lookup(ipdevice_hash,
1416 GINT_TO_POINTER(ipconfig->index));
1417 if (ipdevice == NULL)
1420 g_free(ipdevice->pac);
1421 ipdevice->pac = g_strdup(url);
1426 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1428 struct connman_ipdevice *ipdevice;
1430 DBG("ipconfig %p", ipconfig);
1432 if (ipconfig == NULL || ipconfig->index < 0)
1435 ipdevice = g_hash_table_lookup(ipdevice_hash,
1436 GINT_TO_POINTER(ipconfig->index));
1437 if (ipdevice == NULL)
1440 return ipdevice->pac;
1443 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1445 struct connman_ipdevice *ipdevice;
1449 ipdevice = g_hash_table_lookup(ipdevice_hash,
1450 GINT_TO_POINTER(ipconfig->index));
1451 if (ipdevice == NULL)
1454 set_ipv6_state(ipdevice->ifname, FALSE);
1457 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1459 struct connman_ipdevice *ipdevice;
1463 ipdevice = g_hash_table_lookup(ipdevice_hash,
1464 GINT_TO_POINTER(ipconfig->index));
1465 if (ipdevice == NULL)
1468 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1469 set_ipv6_privacy(ipdevice->ifname,
1470 ipconfig->ipv6_privacy_config);
1472 set_ipv6_state(ipdevice->ifname, TRUE);
1475 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1477 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1480 disable_ipv6(ipconfig);
1483 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1485 struct connman_ipdevice *ipdevice;
1486 gboolean up = FALSE, down = FALSE;
1487 gboolean lower_up = FALSE, lower_down = FALSE;
1488 enum connman_ipconfig_type type;
1490 DBG("ipconfig %p", ipconfig);
1492 if (ipconfig == NULL || ipconfig->index < 0)
1495 ipdevice = g_hash_table_lookup(ipdevice_hash,
1496 GINT_TO_POINTER(ipconfig->index));
1497 if (ipdevice == NULL)
1500 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1501 if (ipdevice->config_ipv4 == ipconfig)
1503 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1504 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1505 if (ipdevice->config_ipv6 == ipconfig)
1507 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1508 enable_ipv6(ipconfig);
1512 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1513 ipdevice->config_ipv4 != NULL) {
1514 ipconfig_list = g_list_remove(ipconfig_list,
1515 ipdevice->config_ipv4);
1517 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1519 connman_ipconfig_unref(ipdevice->config_ipv4);
1522 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1523 ipdevice->config_ipv6 != NULL) {
1524 ipconfig_list = g_list_remove(ipconfig_list,
1525 ipdevice->config_ipv6);
1527 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1529 connman_ipconfig_unref(ipdevice->config_ipv6);
1532 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1533 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1534 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1535 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1537 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1539 if (ipdevice->flags & IFF_UP)
1544 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1545 (IFF_RUNNING | IFF_LOWER_UP))
1547 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1550 if (up == TRUE && ipconfig->ops->up)
1551 ipconfig->ops->up(ipconfig);
1552 if (lower_up == TRUE && ipconfig->ops->lower_up)
1553 ipconfig->ops->lower_up(ipconfig);
1555 if (lower_down == TRUE && ipconfig->ops->lower_down)
1556 ipconfig->ops->lower_down(ipconfig);
1557 if (down == TRUE && ipconfig->ops->down)
1558 ipconfig->ops->down(ipconfig);
1563 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1565 struct connman_ipdevice *ipdevice;
1567 DBG("ipconfig %p", ipconfig);
1569 if (ipconfig == NULL || ipconfig->index < 0)
1572 ipdevice = g_hash_table_lookup(ipdevice_hash,
1573 GINT_TO_POINTER(ipconfig->index));
1574 if (ipdevice == NULL)
1577 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1580 if (ipdevice->config_ipv4 == ipconfig) {
1581 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1583 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1584 connman_ipconfig_unref(ipdevice->config_ipv4);
1585 ipdevice->config_ipv4 = NULL;
1589 if (ipdevice->config_ipv6 == ipconfig) {
1590 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1592 if (ipdevice->config_ipv6->method ==
1593 CONNMAN_IPCONFIG_METHOD_AUTO)
1594 disable_ipv6(ipdevice->config_ipv6);
1596 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1597 connman_ipconfig_unref(ipdevice->config_ipv6);
1598 ipdevice->config_ipv6 = NULL;
1605 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1608 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1610 case CONNMAN_IPCONFIG_METHOD_OFF:
1612 case CONNMAN_IPCONFIG_METHOD_FIXED:
1614 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1616 case CONNMAN_IPCONFIG_METHOD_DHCP:
1618 case CONNMAN_IPCONFIG_METHOD_AUTO:
1625 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1627 if (g_strcmp0(method, "off") == 0)
1628 return CONNMAN_IPCONFIG_METHOD_OFF;
1629 else if (g_strcmp0(method, "fixed") == 0)
1630 return CONNMAN_IPCONFIG_METHOD_FIXED;
1631 else if (g_strcmp0(method, "manual") == 0)
1632 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1633 else if (g_strcmp0(method, "dhcp") == 0)
1634 return CONNMAN_IPCONFIG_METHOD_DHCP;
1635 else if (g_strcmp0(method, "auto") == 0)
1636 return CONNMAN_IPCONFIG_METHOD_AUTO;
1638 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1641 static const char *privacy2string(int privacy)
1645 else if (privacy == 1)
1647 else if (privacy > 1)
1653 static int string2privacy(const char *privacy)
1655 if (g_strcmp0(privacy, "disabled") == 0)
1657 else if (g_strcmp0(privacy, "enabled") == 0)
1659 else if (g_strcmp0(privacy, "prefered") == 0)
1665 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1666 DBusMessageIter *iter)
1672 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1675 str = __connman_ipconfig_method2string(ipconfig->method);
1679 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1681 if (ipconfig->system == NULL)
1684 if (ipconfig->system->local != NULL) {
1686 struct in_addr netmask;
1689 connman_dbus_dict_append_basic(iter, "Address",
1690 DBUS_TYPE_STRING, &ipconfig->system->local);
1692 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1693 netmask.s_addr = htonl(addr);
1694 mask = inet_ntoa(netmask);
1695 connman_dbus_dict_append_basic(iter, "Netmask",
1696 DBUS_TYPE_STRING, &mask);
1699 if (ipconfig->system->gateway != NULL)
1700 connman_dbus_dict_append_basic(iter, "Gateway",
1701 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1704 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1705 DBusMessageIter *iter,
1706 struct connman_ipconfig *ipconfig_ipv4)
1708 const char *str, *privacy;
1712 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1715 str = __connman_ipconfig_method2string(ipconfig->method);
1719 if (ipconfig_ipv4 != NULL &&
1720 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1721 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1725 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1727 if (ipconfig->system == NULL)
1730 if (ipconfig->system->local != NULL) {
1731 connman_dbus_dict_append_basic(iter, "Address",
1732 DBUS_TYPE_STRING, &ipconfig->system->local);
1733 connman_dbus_dict_append_basic(iter, "PrefixLength",
1735 &ipconfig->system->prefixlen);
1738 if (ipconfig->system->gateway != NULL)
1739 connman_dbus_dict_append_basic(iter, "Gateway",
1740 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1742 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1743 connman_dbus_dict_append_basic(iter, "Privacy",
1744 DBUS_TYPE_STRING, &privacy);
1747 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1748 DBusMessageIter *iter)
1750 const char *str, *privacy;
1754 str = __connman_ipconfig_method2string(ipconfig->method);
1758 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1760 switch (ipconfig->method) {
1761 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1762 case CONNMAN_IPCONFIG_METHOD_OFF:
1763 case CONNMAN_IPCONFIG_METHOD_DHCP:
1765 case CONNMAN_IPCONFIG_METHOD_FIXED:
1766 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1767 case CONNMAN_IPCONFIG_METHOD_AUTO:
1771 if (ipconfig->address == NULL)
1774 if (ipconfig->address->local != NULL) {
1775 connman_dbus_dict_append_basic(iter, "Address",
1776 DBUS_TYPE_STRING, &ipconfig->address->local);
1777 connman_dbus_dict_append_basic(iter, "PrefixLength",
1779 &ipconfig->address->prefixlen);
1782 if (ipconfig->address->gateway != NULL)
1783 connman_dbus_dict_append_basic(iter, "Gateway",
1784 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1786 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1787 connman_dbus_dict_append_basic(iter, "Privacy",
1788 DBUS_TYPE_STRING, &privacy);
1791 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1792 DBusMessageIter *iter)
1798 str = __connman_ipconfig_method2string(ipconfig->method);
1802 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1804 switch (ipconfig->method) {
1805 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1806 case CONNMAN_IPCONFIG_METHOD_OFF:
1807 case CONNMAN_IPCONFIG_METHOD_FIXED:
1808 case CONNMAN_IPCONFIG_METHOD_DHCP:
1809 case CONNMAN_IPCONFIG_METHOD_AUTO:
1811 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1815 if (ipconfig->address == NULL)
1818 if (ipconfig->address->local != NULL) {
1820 struct in_addr netmask;
1823 connman_dbus_dict_append_basic(iter, "Address",
1824 DBUS_TYPE_STRING, &ipconfig->address->local);
1826 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1827 netmask.s_addr = htonl(addr);
1828 mask = inet_ntoa(netmask);
1829 connman_dbus_dict_append_basic(iter, "Netmask",
1830 DBUS_TYPE_STRING, &mask);
1833 if (ipconfig->address->gateway != NULL)
1834 connman_dbus_dict_append_basic(iter, "Gateway",
1835 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1838 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1839 DBusMessageIter *array)
1841 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1842 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1843 *prefix_length_string = NULL, *privacy_string = NULL;
1844 int prefix_length = 0, privacy = 0;
1845 DBusMessageIter dict;
1847 DBG("ipconfig %p", ipconfig);
1849 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1852 dbus_message_iter_recurse(array, &dict);
1854 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1855 DBusMessageIter entry;
1859 dbus_message_iter_recurse(&dict, &entry);
1861 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1864 dbus_message_iter_get_basic(&entry, &key);
1865 dbus_message_iter_next(&entry);
1867 type = dbus_message_iter_get_arg_type(&entry);
1869 if (g_str_equal(key, "Method") == TRUE) {
1872 if (type != DBUS_TYPE_STRING)
1875 dbus_message_iter_get_basic(&entry, &str);
1876 method = __connman_ipconfig_string2method(str);
1877 } else if (g_str_equal(key, "Address") == TRUE) {
1878 if (type != DBUS_TYPE_STRING)
1881 dbus_message_iter_get_basic(&entry, &address);
1882 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1883 if (type != DBUS_TYPE_STRING)
1886 dbus_message_iter_get_basic(&entry,
1887 &prefix_length_string);
1889 prefix_length = atoi(prefix_length_string);
1890 if (prefix_length < 0 || prefix_length > 128)
1893 } else if (g_str_equal(key, "Netmask") == TRUE) {
1894 if (type != DBUS_TYPE_STRING)
1897 dbus_message_iter_get_basic(&entry, &netmask);
1898 } else if (g_str_equal(key, "Gateway") == TRUE) {
1899 if (type != DBUS_TYPE_STRING)
1902 dbus_message_iter_get_basic(&entry, &gateway);
1903 } else if (g_str_equal(key, "Privacy") == TRUE) {
1904 if (type != DBUS_TYPE_STRING)
1907 dbus_message_iter_get_basic(&entry, &privacy_string);
1908 privacy = string2privacy(privacy_string);
1910 dbus_message_iter_next(&dict);
1913 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
1915 method, address, netmask, gateway, prefix_length,
1919 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1920 case CONNMAN_IPCONFIG_METHOD_FIXED:
1923 case CONNMAN_IPCONFIG_METHOD_OFF:
1924 ipconfig->method = method;
1925 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1926 disable_ipv6(ipconfig);
1929 case CONNMAN_IPCONFIG_METHOD_AUTO:
1930 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1933 ipconfig->method = method;
1934 if (privacy_string != NULL)
1935 ipconfig->ipv6_privacy_config = privacy;
1936 enable_ipv6(ipconfig);
1939 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1940 if (address == NULL)
1943 ipconfig->method = method;
1945 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1946 connman_ipaddress_set_ipv4(ipconfig->address,
1947 address, netmask, gateway);
1949 return connman_ipaddress_set_ipv6(
1950 ipconfig->address, address,
1951 prefix_length, gateway);
1954 case CONNMAN_IPCONFIG_METHOD_DHCP:
1955 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1958 ipconfig->method = method;
1965 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1966 DBusMessageIter *iter)
1968 struct connman_ipdevice *ipdevice;
1969 const char *method = "auto";
1971 connman_dbus_dict_append_basic(iter, "Method",
1972 DBUS_TYPE_STRING, &method);
1974 ipdevice = g_hash_table_lookup(ipdevice_hash,
1975 GINT_TO_POINTER(ipconfig->index));
1976 if (ipdevice == NULL)
1979 if (ipdevice->ifname != NULL)
1980 connman_dbus_dict_append_basic(iter, "Interface",
1981 DBUS_TYPE_STRING, &ipdevice->ifname);
1983 if (ipdevice->address != NULL)
1984 connman_dbus_dict_append_basic(iter, "Address",
1985 DBUS_TYPE_STRING, &ipdevice->address);
1987 if (ipdevice->mtu > 0)
1988 connman_dbus_dict_append_basic(iter, "MTU",
1989 DBUS_TYPE_UINT16, &ipdevice->mtu);
1992 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1993 GKeyFile *keyfile, const char *identifier, const char *prefix)
1998 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2000 key = g_strdup_printf("%smethod", prefix);
2001 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2002 if (method == NULL) {
2003 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2004 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2006 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2008 ipconfig->method = __connman_ipconfig_string2method(method);
2010 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2011 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2013 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2014 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2015 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2017 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2018 privacy = g_key_file_get_string(keyfile, identifier,
2020 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2024 __connman_ipconfig_enable(ipconfig);
2025 enable_ipv6(ipconfig);
2032 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2033 ipconfig->address->prefixlen = g_key_file_get_integer(
2034 keyfile, identifier, key, NULL);
2037 key = g_strdup_printf("%slocal_address", prefix);
2038 ipconfig->address->local = g_key_file_get_string(
2039 keyfile, identifier, key, NULL);
2042 key = g_strdup_printf("%speer_address", prefix);
2043 ipconfig->address->peer = g_key_file_get_string(
2044 keyfile, identifier, key, NULL);
2047 key = g_strdup_printf("%sbroadcast_address", prefix);
2048 ipconfig->address->broadcast = g_key_file_get_string(
2049 keyfile, identifier, key, NULL);
2052 key = g_strdup_printf("%sgateway", prefix);
2053 ipconfig->address->gateway = g_key_file_get_string(
2054 keyfile, identifier, key, NULL);
2060 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2061 GKeyFile *keyfile, const char *identifier, const char *prefix)
2066 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2068 method = __connman_ipconfig_method2string(ipconfig->method);
2070 key = g_strdup_printf("%smethod", prefix);
2071 g_key_file_set_string(keyfile, identifier, key, method);
2074 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2075 const char *privacy;
2076 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2077 key = g_strdup_printf("%sprivacy", prefix);
2078 g_key_file_set_string(keyfile, identifier, key, privacy);
2082 switch (ipconfig->method) {
2083 case CONNMAN_IPCONFIG_METHOD_FIXED:
2084 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2086 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2087 case CONNMAN_IPCONFIG_METHOD_OFF:
2088 case CONNMAN_IPCONFIG_METHOD_DHCP:
2089 case CONNMAN_IPCONFIG_METHOD_AUTO:
2093 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2094 g_key_file_set_integer(keyfile, identifier,
2095 key, ipconfig->address->prefixlen);
2098 key = g_strdup_printf("%slocal_address", prefix);
2099 if (ipconfig->address->local != NULL)
2100 g_key_file_set_string(keyfile, identifier,
2101 key, ipconfig->address->local);
2104 key = g_strdup_printf("%speer_address", prefix);
2105 if (ipconfig->address->peer != NULL)
2106 g_key_file_set_string(keyfile, identifier,
2107 key, ipconfig->address->peer);
2110 key = g_strdup_printf("%sbroadcast_address", prefix);
2111 if (ipconfig->address->broadcast != NULL)
2112 g_key_file_set_string(keyfile, identifier,
2113 key, ipconfig->address->broadcast);
2116 key = g_strdup_printf("%sgateway", prefix);
2117 if (ipconfig->address->gateway != NULL)
2118 g_key_file_set_string(keyfile, identifier,
2119 key, ipconfig->address->gateway);
2125 int __connman_ipconfig_init(void)
2129 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2130 NULL, free_ipdevice);
2135 void __connman_ipconfig_cleanup(void)
2139 g_hash_table_destroy(ipdevice_hash);
2140 ipdevice_hash = NULL;