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)];
151 err = inet_pton(AF_INET6, address, buf);
158 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
160 unsigned char prefix_length,
163 if (ipaddress == NULL)
166 if (check_ipv6_address(address) == FALSE)
169 if (check_ipv6_address(gateway) == FALSE)
172 DBG("prefix_len %d address %s gateway %s",
173 prefix_length, address, gateway);
175 ipaddress->family = AF_INET6;
177 ipaddress->prefixlen = prefix_length;
179 g_free(ipaddress->local);
180 ipaddress->local = g_strdup(address);
182 g_free(ipaddress->gateway);
183 ipaddress->gateway = g_strdup(gateway);
188 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
189 const char *address, const char *netmask, const char *gateway)
191 if (ipaddress == NULL)
194 ipaddress->family = AF_INET;
196 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
198 g_free(ipaddress->local);
199 ipaddress->local = g_strdup(address);
201 g_free(ipaddress->gateway);
202 ipaddress->gateway = g_strdup(gateway);
207 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
210 if (ipaddress == NULL)
213 g_free(ipaddress->peer);
214 ipaddress->peer = g_strdup(peer);
217 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
219 if (ipaddress == NULL)
222 ipaddress->prefixlen = 0;
224 g_free(ipaddress->local);
225 ipaddress->local = NULL;
227 g_free(ipaddress->peer);
228 ipaddress->peer = NULL;
230 g_free(ipaddress->broadcast);
231 ipaddress->broadcast = NULL;
233 g_free(ipaddress->gateway);
234 ipaddress->gateway = NULL;
237 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
238 struct connman_ipaddress *source)
240 if (ipaddress == NULL || source == NULL)
243 ipaddress->family = source->family;
244 ipaddress->prefixlen = source->prefixlen;
246 g_free(ipaddress->local);
247 ipaddress->local = g_strdup(source->local);
249 g_free(ipaddress->peer);
250 ipaddress->peer = g_strdup(source->peer);
252 g_free(ipaddress->broadcast);
253 ipaddress->broadcast = g_strdup(source->broadcast);
255 g_free(ipaddress->gateway);
256 ipaddress->gateway = g_strdup(source->gateway);
259 static void free_address_list(struct connman_ipdevice *ipdevice)
263 for (list = ipdevice->address_list; list; list = list->next) {
264 struct connman_ipaddress *ipaddress = list->data;
266 connman_ipaddress_free(ipaddress);
270 g_slist_free(ipdevice->address_list);
271 ipdevice->address_list = NULL;
274 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
275 unsigned char prefixlen, const char *local)
279 for (list = ipdevice->address_list; list; list = list->next) {
280 struct connman_ipaddress *ipaddress = list->data;
282 if (g_strcmp0(ipaddress->local, local) == 0 &&
283 ipaddress->prefixlen == prefixlen)
290 static const char *type2str(unsigned short type)
295 case ARPHRD_LOOPBACK:
308 static const char *scope2str(unsigned char scope)
320 static gboolean get_ipv6_state(gchar *ifname)
325 gboolean enabled = FALSE;
328 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
330 path = g_strdup_printf(
331 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
336 f = fopen(path, "r");
341 if (fscanf(f, "%d", &disabled) > 0)
349 static void set_ipv6_state(gchar *ifname, gboolean enable)
355 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
357 path = g_strdup_printf(
358 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
363 f = fopen(path, "r+");
378 static int get_ipv6_privacy(gchar *ifname)
387 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
393 f = fopen(path, "r");
400 if (fscanf(f, "%d", &value) <= 0)
408 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
409 * The privacy extension is described in RFC 3041 and RFC 4941
411 static void set_ipv6_privacy(gchar *ifname, int value)
419 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
428 f = fopen(path, "r+");
435 fprintf(f, "%d", value);
439 static void free_ipdevice(gpointer data)
441 struct connman_ipdevice *ipdevice = data;
443 connman_info("%s {remove} index %d", ipdevice->ifname,
446 if (ipdevice->config_ipv4 != NULL) {
447 connman_ipconfig_unref(ipdevice->config_ipv4);
448 ipdevice->config_ipv4 = NULL;
451 if (ipdevice->config_ipv6 != NULL) {
452 connman_ipconfig_unref(ipdevice->config_ipv6);
453 ipdevice->config_ipv6 = NULL;
456 free_address_list(ipdevice);
457 g_free(ipdevice->ipv4_gateway);
458 g_free(ipdevice->ipv6_gateway);
459 g_free(ipdevice->pac);
461 g_free(ipdevice->address);
463 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
464 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
466 g_free(ipdevice->ifname);
470 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
472 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
473 ipdevice->config_ipv6);
476 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
478 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
479 ipdevice->config_ipv6);
481 if (ipdevice->config_ipv4)
482 connman_inet_clear_address(ipdevice->index,
483 ipdevice->config_ipv4->address);
485 if (ipdevice->config_ipv6)
486 connman_inet_clear_ipv6_address(ipdevice->index,
487 ipdevice->config_ipv6->address->local,
488 ipdevice->config_ipv6->address->prefixlen);
491 static void update_stats(struct connman_ipdevice *ipdevice,
492 struct rtnl_link_stats *stats)
494 struct connman_service *service;
496 if (stats->rx_packets == 0 && stats->tx_packets == 0)
499 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
500 stats->rx_packets, stats->rx_bytes);
501 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
502 stats->tx_packets, stats->tx_bytes);
504 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
507 if (ipdevice->config_ipv4)
508 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
509 else if (ipdevice->config_ipv6)
510 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
517 ipdevice->rx_packets = stats->rx_packets;
518 ipdevice->tx_packets = stats->tx_packets;
519 ipdevice->rx_bytes = stats->rx_bytes;
520 ipdevice->tx_bytes = stats->tx_bytes;
521 ipdevice->rx_errors = stats->rx_errors;
522 ipdevice->tx_errors = stats->tx_errors;
523 ipdevice->rx_dropped = stats->rx_dropped;
524 ipdevice->tx_dropped = stats->tx_dropped;
526 __connman_service_notify(service,
527 ipdevice->rx_packets, ipdevice->tx_packets,
528 ipdevice->rx_bytes, ipdevice->tx_bytes,
529 ipdevice->rx_errors, ipdevice->tx_errors,
530 ipdevice->rx_dropped, ipdevice->tx_dropped);
533 void __connman_ipconfig_newlink(int index, unsigned short type,
534 unsigned int flags, const char *address,
536 struct rtnl_link_stats *stats)
538 struct connman_ipdevice *ipdevice;
541 gboolean up = FALSE, down = FALSE;
542 gboolean lower_up = FALSE, lower_down = FALSE;
544 DBG("index %d", index);
546 if (type == ARPHRD_LOOPBACK)
549 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
550 if (ipdevice != NULL)
553 ipdevice = g_try_new0(struct connman_ipdevice, 1);
554 if (ipdevice == NULL)
557 ipdevice->index = index;
558 ipdevice->ifname = connman_inet_ifname(index);
559 ipdevice->type = type;
561 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
562 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
564 ipdevice->address = g_strdup(address);
566 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
568 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
569 index, type, type2str(type));
574 update_stats(ipdevice, stats);
576 if (flags == ipdevice->flags)
579 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
586 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
587 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
588 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
589 (IFF_RUNNING | IFF_LOWER_UP))
591 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
595 ipdevice->flags = flags;
597 str = g_string_new(NULL);
602 g_string_append(str, "UP");
604 g_string_append(str, "DOWN");
606 if (flags & IFF_RUNNING)
607 g_string_append(str, ",RUNNING");
609 if (flags & IFF_LOWER_UP)
610 g_string_append(str, ",LOWER_UP");
612 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
615 g_string_free(str, TRUE);
617 for (list = g_list_first(ipconfig_list); list;
618 list = g_list_next(list)) {
619 struct connman_ipconfig *ipconfig = list->data;
621 if (index != ipconfig->index)
624 if (ipconfig->ops == NULL)
627 if (up == TRUE && ipconfig->ops->up)
628 ipconfig->ops->up(ipconfig);
629 if (lower_up == TRUE && ipconfig->ops->lower_up)
630 ipconfig->ops->lower_up(ipconfig);
632 if (lower_down == TRUE && ipconfig->ops->lower_down)
633 ipconfig->ops->lower_down(ipconfig);
634 if (down == TRUE && ipconfig->ops->down)
635 ipconfig->ops->down(ipconfig);
639 __connman_ipconfig_lower_up(ipdevice);
641 __connman_ipconfig_lower_down(ipdevice);
644 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
646 struct connman_ipdevice *ipdevice;
649 DBG("index %d", index);
651 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
652 if (ipdevice == NULL)
655 update_stats(ipdevice, stats);
657 for (list = g_list_first(ipconfig_list); list;
658 list = g_list_next(list)) {
659 struct connman_ipconfig *ipconfig = list->data;
661 if (index != ipconfig->index)
664 ipconfig->index = -1;
666 if (ipconfig->ops == NULL)
669 if (ipconfig->ops->lower_down)
670 ipconfig->ops->lower_down(ipconfig);
671 if (ipconfig->ops->down)
672 ipconfig->ops->down(ipconfig);
675 __connman_ipconfig_lower_down(ipdevice);
677 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
680 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
682 const struct connman_ipaddress *addr1 = a;
683 const struct connman_ipaddress *addr2 = b;
685 if (addr1->prefixlen != addr2->prefixlen)
686 return addr2->prefixlen - addr1->prefixlen;
688 return g_strcmp0(addr1->local, addr2->local);
691 void __connman_ipconfig_newaddr(int index, int family, const char *label,
692 unsigned char prefixlen, const char *address)
694 struct connman_ipdevice *ipdevice;
695 struct connman_ipaddress *ipaddress;
698 DBG("index %d", index);
700 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
701 if (ipdevice == NULL)
704 ipaddress = connman_ipaddress_alloc(family);
705 if (ipaddress == NULL)
708 ipaddress->prefixlen = prefixlen;
709 ipaddress->local = g_strdup(address);
711 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
712 check_duplicate_address)) {
713 connman_ipaddress_free(ipaddress);
717 ipdevice->address_list = g_slist_append(ipdevice->address_list,
720 connman_info("%s {add} address %s/%u label %s family %d",
721 ipdevice->ifname, address, prefixlen, label, family);
723 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
724 connman_ipaddress_copy(ipdevice->config_ipv4->system,
727 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
728 connman_ipaddress_copy(ipdevice->config_ipv6->system,
733 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
736 for (list = g_list_first(ipconfig_list); list;
737 list = g_list_next(list)) {
738 struct connman_ipconfig *ipconfig = list->data;
740 if (index != ipconfig->index)
743 if (ipconfig->ops == NULL)
746 if (ipconfig->ops->ip_bound)
747 ipconfig->ops->ip_bound(ipconfig);
751 void __connman_ipconfig_deladdr(int index, int family, const char *label,
752 unsigned char prefixlen, const char *address)
754 struct connman_ipdevice *ipdevice;
755 struct connman_ipaddress *ipaddress;
758 DBG("index %d", index);
760 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
761 if (ipdevice == NULL)
764 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
765 if (ipaddress == NULL)
768 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
771 connman_ipaddress_clear(ipaddress);
774 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
775 address, prefixlen, label);
777 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
780 if (g_slist_length(ipdevice->address_list) > 0)
783 for (list = g_list_first(ipconfig_list); list;
784 list = g_list_next(list)) {
785 struct connman_ipconfig *ipconfig = list->data;
787 if (index != ipconfig->index)
790 if (ipconfig->ops == NULL)
793 if (ipconfig->ops->ip_release)
794 ipconfig->ops->ip_release(ipconfig);
798 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
799 const char *dst, const char *gateway)
801 struct connman_ipdevice *ipdevice;
803 DBG("index %d", index);
805 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
806 if (ipdevice == NULL)
809 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
810 g_strcmp0(dst, "::") == 0)) {
814 if (family == AF_INET6) {
815 g_free(ipdevice->ipv6_gateway);
816 ipdevice->ipv6_gateway = g_strdup(gateway);
818 if (ipdevice->config_ipv6 != NULL &&
819 ipdevice->config_ipv6->system != NULL) {
820 g_free(ipdevice->config_ipv6->system->gateway);
821 ipdevice->config_ipv6->system->gateway =
824 } else if (family == AF_INET) {
825 g_free(ipdevice->ipv4_gateway);
826 ipdevice->ipv4_gateway = g_strdup(gateway);
828 if (ipdevice->config_ipv4 != NULL &&
829 ipdevice->config_ipv4->system != NULL) {
830 g_free(ipdevice->config_ipv4->system->gateway);
831 ipdevice->config_ipv4->system->gateway =
837 for (list = ipdevice->address_list; list; list = list->next) {
838 struct connman_ipaddress *ipaddress = list->data;
840 g_free(ipaddress->gateway);
841 ipaddress->gateway = g_strdup(gateway);
844 for (config_list = g_list_first(ipconfig_list); config_list;
845 config_list = g_list_next(config_list)) {
846 struct connman_ipconfig *ipconfig = config_list->data;
848 if (index != ipconfig->index)
851 if (ipconfig->ops == NULL)
854 if (ipconfig->ops->ip_bound)
855 ipconfig->ops->ip_bound(ipconfig);
859 connman_info("%s {add} route %s gw %s scope %u <%s>",
860 ipdevice->ifname, dst, gateway,
861 scope, scope2str(scope));
864 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
865 const char *dst, const char *gateway)
867 struct connman_ipdevice *ipdevice;
869 DBG("index %d", index);
871 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
872 if (ipdevice == NULL)
875 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
876 g_strcmp0(dst, "::") == 0)) {
880 if (family == AF_INET6) {
881 g_free(ipdevice->ipv6_gateway);
882 ipdevice->ipv6_gateway = NULL;
884 if (ipdevice->config_ipv6 != NULL &&
885 ipdevice->config_ipv6->system != NULL) {
886 g_free(ipdevice->config_ipv6->system->gateway);
887 ipdevice->config_ipv6->system->gateway = NULL;
889 } else if (family == AF_INET) {
890 g_free(ipdevice->ipv4_gateway);
891 ipdevice->ipv4_gateway = NULL;
893 if (ipdevice->config_ipv4 != NULL &&
894 ipdevice->config_ipv4->system != NULL) {
895 g_free(ipdevice->config_ipv4->system->gateway);
896 ipdevice->config_ipv4->system->gateway = NULL;
901 for (list = ipdevice->address_list; list; list = list->next) {
902 struct connman_ipaddress *ipaddress = list->data;
904 g_free(ipaddress->gateway);
905 ipaddress->gateway = NULL;
908 for (config_list = g_list_first(ipconfig_list); config_list;
909 config_list = g_list_next(config_list)) {
910 struct connman_ipconfig *ipconfig = config_list->data;
912 if (index != ipconfig->index)
915 if (ipconfig->ops == NULL)
918 if (ipconfig->ops->ip_release)
919 ipconfig->ops->ip_release(ipconfig);
923 connman_info("%s {del} route %s gw %s scope %u <%s>",
924 ipdevice->ifname, dst, gateway,
925 scope, scope2str(scope));
928 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
933 keys = g_hash_table_get_keys(ipdevice_hash);
937 for (list = g_list_first(keys); list; list = g_list_next(list)) {
938 int index = GPOINTER_TO_INT(list->data);
940 function(index, user_data);
946 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
947 struct connman_ipconfig *ipconfig)
949 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
952 unsigned short __connman_ipconfig_get_type_from_index(int index)
954 struct connman_ipdevice *ipdevice;
956 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
957 if (ipdevice == NULL)
960 return ipdevice->type;
963 unsigned int __connman_ipconfig_get_flags_from_index(int index)
965 struct connman_ipdevice *ipdevice;
967 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
968 if (ipdevice == NULL)
971 return ipdevice->flags;
974 const char *__connman_ipconfig_get_gateway_from_index(int index)
976 struct connman_ipdevice *ipdevice;
978 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
979 if (ipdevice == NULL)
982 if (ipdevice->ipv4_gateway != NULL)
983 return ipdevice->ipv4_gateway;
985 if (ipdevice->config_ipv4 != NULL &&
986 ipdevice->config_ipv4->address != NULL)
987 return ipdevice->config_ipv4->address->gateway;
989 if (ipdevice->ipv6_gateway != NULL)
990 return ipdevice->ipv6_gateway;
992 if (ipdevice->config_ipv6 != NULL &&
993 ipdevice->config_ipv6->address != NULL)
994 return ipdevice->config_ipv6->address->gateway;
999 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1001 ipconfig->index = index;
1004 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1006 if (ipconfig->address == NULL)
1009 return ipconfig->address->local;
1012 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1014 if (ipconfig->address == NULL)
1017 g_free(ipconfig->address->local);
1018 ipconfig->address->local = g_strdup(address);
1021 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1023 if (ipconfig->address == NULL)
1026 return ipconfig->address->peer;
1029 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1031 if (ipconfig->address == NULL)
1034 g_free(ipconfig->address->peer);
1035 ipconfig->address->peer = g_strdup(address);
1038 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1040 if (ipconfig->address == NULL)
1043 return ipconfig->address->broadcast;
1046 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1048 if (ipconfig->address == NULL)
1051 g_free(ipconfig->address->broadcast);
1052 ipconfig->address->broadcast = g_strdup(broadcast);
1055 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1057 if (ipconfig->address == NULL)
1060 return ipconfig->address->gateway;
1063 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1067 if (ipconfig->address == NULL)
1069 g_free(ipconfig->address->gateway);
1070 ipconfig->address->gateway = g_strdup(gateway);
1073 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1075 struct connman_service *service;
1079 if (ipconfig->address == NULL)
1082 service = __connman_service_lookup_from_index(ipconfig->index);
1083 if (service == NULL)
1086 __connman_connection_gateway_remove(service);
1088 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1089 return __connman_connection_gateway_add(service, NULL,
1090 ipconfig->address->gateway,
1091 ipconfig->address->peer);
1092 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1093 return __connman_connection_gateway_add(service,
1094 ipconfig->address->gateway,
1095 NULL, ipconfig->address->peer);
1101 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1103 struct connman_service *service;
1107 service = __connman_service_lookup_from_index(ipconfig->index);
1108 if (service != NULL)
1109 __connman_connection_gateway_remove(service);
1112 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1114 if (ipconfig->address == NULL)
1117 return ipconfig->address->prefixlen;
1120 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1122 if (ipconfig->address == NULL)
1125 ipconfig->address->prefixlen = prefixlen;
1128 static struct connman_ipconfig *create_ipv6config(int index)
1130 struct connman_ipconfig *ipv6config;
1132 DBG("index %d", index);
1134 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1135 if (ipv6config == NULL)
1138 ipv6config->refcount = 1;
1140 ipv6config->index = index;
1141 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1142 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1143 ipv6config->ipv6_privacy_config = 0;
1145 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1146 if (ipv6config->address == NULL) {
1151 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1153 DBG("ipconfig %p", ipv6config);
1159 * connman_ipconfig_create:
1161 * Allocate a new ipconfig structure.
1163 * Returns: a newly-allocated #connman_ipconfig structure
1165 struct connman_ipconfig *connman_ipconfig_create(int index,
1166 enum connman_ipconfig_type type)
1168 struct connman_ipconfig *ipconfig;
1170 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1171 return create_ipv6config(index);
1173 DBG("index %d", index);
1175 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1176 if (ipconfig == NULL)
1179 ipconfig->refcount = 1;
1181 ipconfig->index = index;
1182 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1184 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1185 if (ipconfig->address == NULL) {
1190 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1192 DBG("ipconfig %p", ipconfig);
1199 * connman_ipconfig_ref:
1200 * @ipconfig: ipconfig structure
1202 * Increase reference counter of ipconfig
1204 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1206 DBG("ipconfig %p refcount %d", ipconfig,
1207 g_atomic_int_get(&ipconfig->refcount) + 1);
1209 g_atomic_int_inc(&ipconfig->refcount);
1215 * connman_ipconfig_unref:
1216 * @ipconfig: ipconfig structure
1218 * Decrease reference counter of ipconfig
1220 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1222 if (ipconfig == NULL)
1225 DBG("ipconfig %p refcount %d", ipconfig,
1226 g_atomic_int_get(&ipconfig->refcount) - 1);
1228 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1229 __connman_ipconfig_disable(ipconfig);
1231 connman_ipconfig_set_ops(ipconfig, NULL);
1233 if (ipconfig->origin != NULL) {
1234 connman_ipconfig_unref(ipconfig->origin);
1235 ipconfig->origin = NULL;
1238 connman_ipaddress_free(ipconfig->system);
1239 connman_ipaddress_free(ipconfig->address);
1245 * connman_ipconfig_get_data:
1246 * @ipconfig: ipconfig structure
1248 * Get private data pointer
1250 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1252 if (ipconfig == NULL)
1255 return ipconfig->ops_data;
1259 * connman_ipconfig_set_data:
1260 * @ipconfig: ipconfig structure
1261 * @data: data pointer
1263 * Set private data pointer
1265 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1267 ipconfig->ops_data = data;
1271 * connman_ipconfig_get_index:
1272 * @ipconfig: ipconfig structure
1274 * Get interface index
1276 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1278 if (ipconfig == NULL)
1281 if (ipconfig->origin != NULL)
1282 return ipconfig->origin->index;
1284 return ipconfig->index;
1288 * connman_ipconfig_get_ifname:
1289 * @ipconfig: ipconfig structure
1291 * Get interface name
1293 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1295 struct connman_ipdevice *ipdevice;
1297 if (ipconfig == NULL)
1300 if (ipconfig->index < 0)
1303 ipdevice = g_hash_table_lookup(ipdevice_hash,
1304 GINT_TO_POINTER(ipconfig->index));
1305 if (ipdevice == NULL)
1308 return ipdevice->ifname;
1312 * connman_ipconfig_set_ops:
1313 * @ipconfig: ipconfig structure
1314 * @ops: operation callbacks
1316 * Set the operation callbacks
1318 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1319 const struct connman_ipconfig_ops *ops)
1321 ipconfig->ops = ops;
1325 * connman_ipconfig_set_method:
1326 * @ipconfig: ipconfig structure
1327 * @method: configuration method
1329 * Set the configuration method
1331 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1332 enum connman_ipconfig_method method)
1334 ipconfig->method = method;
1339 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1341 if (ipconfig == NULL)
1342 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1344 return ipconfig->method;
1347 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1351 switch (ipconfig->method) {
1352 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1353 case CONNMAN_IPCONFIG_METHOD_OFF:
1354 case CONNMAN_IPCONFIG_METHOD_AUTO:
1356 case CONNMAN_IPCONFIG_METHOD_FIXED:
1357 case CONNMAN_IPCONFIG_METHOD_DHCP:
1358 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1359 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1360 return connman_inet_set_address(ipconfig->index,
1362 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1363 return connman_inet_set_ipv6_address(
1364 ipconfig->index, ipconfig->address);
1370 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1376 if (ipconfig == NULL)
1379 DBG("method %d", ipconfig->method);
1381 switch (ipconfig->method) {
1382 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1383 case CONNMAN_IPCONFIG_METHOD_OFF:
1384 case CONNMAN_IPCONFIG_METHOD_AUTO:
1386 case CONNMAN_IPCONFIG_METHOD_FIXED:
1387 case CONNMAN_IPCONFIG_METHOD_DHCP:
1388 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1389 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1390 err = connman_inet_clear_address(ipconfig->index,
1392 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1393 err = connman_inet_clear_ipv6_address(
1395 ipconfig->address->local,
1396 ipconfig->address->prefixlen);
1400 connman_ipaddress_clear(ipconfig->address);
1408 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1411 struct connman_ipdevice *ipdevice;
1413 DBG("ipconfig %p", ipconfig);
1415 if (ipconfig == NULL || ipconfig->index < 0)
1418 ipdevice = g_hash_table_lookup(ipdevice_hash,
1419 GINT_TO_POINTER(ipconfig->index));
1420 if (ipdevice == NULL)
1423 g_free(ipdevice->pac);
1424 ipdevice->pac = g_strdup(url);
1429 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1431 struct connman_ipdevice *ipdevice;
1433 DBG("ipconfig %p", ipconfig);
1435 if (ipconfig == NULL || ipconfig->index < 0)
1438 ipdevice = g_hash_table_lookup(ipdevice_hash,
1439 GINT_TO_POINTER(ipconfig->index));
1440 if (ipdevice == NULL)
1443 return ipdevice->pac;
1446 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1448 struct connman_ipdevice *ipdevice;
1452 ipdevice = g_hash_table_lookup(ipdevice_hash,
1453 GINT_TO_POINTER(ipconfig->index));
1454 if (ipdevice == NULL)
1457 set_ipv6_state(ipdevice->ifname, FALSE);
1460 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1462 struct connman_ipdevice *ipdevice;
1466 ipdevice = g_hash_table_lookup(ipdevice_hash,
1467 GINT_TO_POINTER(ipconfig->index));
1468 if (ipdevice == NULL)
1471 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1472 set_ipv6_privacy(ipdevice->ifname,
1473 ipconfig->ipv6_privacy_config);
1475 set_ipv6_state(ipdevice->ifname, TRUE);
1478 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1480 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1483 disable_ipv6(ipconfig);
1486 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1488 struct connman_ipdevice *ipdevice;
1489 gboolean up = FALSE, down = FALSE;
1490 gboolean lower_up = FALSE, lower_down = FALSE;
1491 enum connman_ipconfig_type type;
1493 DBG("ipconfig %p", ipconfig);
1495 if (ipconfig == NULL || ipconfig->index < 0)
1498 ipdevice = g_hash_table_lookup(ipdevice_hash,
1499 GINT_TO_POINTER(ipconfig->index));
1500 if (ipdevice == NULL)
1503 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1504 if (ipdevice->config_ipv4 == ipconfig)
1506 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1507 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1508 if (ipdevice->config_ipv6 == ipconfig)
1510 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1511 enable_ipv6(ipconfig);
1515 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1516 ipdevice->config_ipv4 != NULL) {
1517 ipconfig_list = g_list_remove(ipconfig_list,
1518 ipdevice->config_ipv4);
1520 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1522 connman_ipconfig_unref(ipdevice->config_ipv4);
1525 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1526 ipdevice->config_ipv6 != NULL) {
1527 ipconfig_list = g_list_remove(ipconfig_list,
1528 ipdevice->config_ipv6);
1530 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1532 connman_ipconfig_unref(ipdevice->config_ipv6);
1535 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1536 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1537 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1538 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1540 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1542 if (ipdevice->flags & IFF_UP)
1547 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1548 (IFF_RUNNING | IFF_LOWER_UP))
1550 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1553 if (up == TRUE && ipconfig->ops->up)
1554 ipconfig->ops->up(ipconfig);
1555 if (lower_up == TRUE && ipconfig->ops->lower_up)
1556 ipconfig->ops->lower_up(ipconfig);
1558 if (lower_down == TRUE && ipconfig->ops->lower_down)
1559 ipconfig->ops->lower_down(ipconfig);
1560 if (down == TRUE && ipconfig->ops->down)
1561 ipconfig->ops->down(ipconfig);
1566 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1568 struct connman_ipdevice *ipdevice;
1570 DBG("ipconfig %p", ipconfig);
1572 if (ipconfig == NULL || ipconfig->index < 0)
1575 ipdevice = g_hash_table_lookup(ipdevice_hash,
1576 GINT_TO_POINTER(ipconfig->index));
1577 if (ipdevice == NULL)
1580 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1583 if (ipdevice->config_ipv4 == ipconfig) {
1584 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1586 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1587 connman_ipconfig_unref(ipdevice->config_ipv4);
1588 ipdevice->config_ipv4 = NULL;
1592 if (ipdevice->config_ipv6 == ipconfig) {
1593 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1595 if (ipdevice->config_ipv6->method ==
1596 CONNMAN_IPCONFIG_METHOD_AUTO)
1597 disable_ipv6(ipdevice->config_ipv6);
1599 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1600 connman_ipconfig_unref(ipdevice->config_ipv6);
1601 ipdevice->config_ipv6 = NULL;
1608 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1611 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1613 case CONNMAN_IPCONFIG_METHOD_OFF:
1615 case CONNMAN_IPCONFIG_METHOD_FIXED:
1617 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1619 case CONNMAN_IPCONFIG_METHOD_DHCP:
1621 case CONNMAN_IPCONFIG_METHOD_AUTO:
1628 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1630 if (g_strcmp0(method, "off") == 0)
1631 return CONNMAN_IPCONFIG_METHOD_OFF;
1632 else if (g_strcmp0(method, "fixed") == 0)
1633 return CONNMAN_IPCONFIG_METHOD_FIXED;
1634 else if (g_strcmp0(method, "manual") == 0)
1635 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1636 else if (g_strcmp0(method, "dhcp") == 0)
1637 return CONNMAN_IPCONFIG_METHOD_DHCP;
1638 else if (g_strcmp0(method, "auto") == 0)
1639 return CONNMAN_IPCONFIG_METHOD_AUTO;
1641 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1644 static const char *privacy2string(int privacy)
1648 else if (privacy == 1)
1650 else if (privacy > 1)
1656 static int string2privacy(const char *privacy)
1658 if (g_strcmp0(privacy, "disabled") == 0)
1660 else if (g_strcmp0(privacy, "enabled") == 0)
1662 else if (g_strcmp0(privacy, "prefered") == 0)
1668 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1669 DBusMessageIter *iter)
1675 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1678 str = __connman_ipconfig_method2string(ipconfig->method);
1682 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1684 if (ipconfig->system == NULL)
1687 if (ipconfig->system->local != NULL) {
1689 struct in_addr netmask;
1692 connman_dbus_dict_append_basic(iter, "Address",
1693 DBUS_TYPE_STRING, &ipconfig->system->local);
1695 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1696 netmask.s_addr = htonl(addr);
1697 mask = inet_ntoa(netmask);
1698 connman_dbus_dict_append_basic(iter, "Netmask",
1699 DBUS_TYPE_STRING, &mask);
1702 if (ipconfig->system->gateway != NULL)
1703 connman_dbus_dict_append_basic(iter, "Gateway",
1704 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1707 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1708 DBusMessageIter *iter,
1709 struct connman_ipconfig *ipconfig_ipv4)
1711 const char *str, *privacy;
1715 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1718 str = __connman_ipconfig_method2string(ipconfig->method);
1722 if (ipconfig_ipv4 != NULL &&
1723 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1724 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1728 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1730 if (ipconfig->system == NULL)
1733 if (ipconfig->system->local != NULL) {
1734 connman_dbus_dict_append_basic(iter, "Address",
1735 DBUS_TYPE_STRING, &ipconfig->system->local);
1736 connman_dbus_dict_append_basic(iter, "PrefixLength",
1738 &ipconfig->system->prefixlen);
1741 if (ipconfig->system->gateway != NULL)
1742 connman_dbus_dict_append_basic(iter, "Gateway",
1743 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1745 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1746 connman_dbus_dict_append_basic(iter, "Privacy",
1747 DBUS_TYPE_STRING, &privacy);
1750 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1751 DBusMessageIter *iter)
1753 const char *str, *privacy;
1757 str = __connman_ipconfig_method2string(ipconfig->method);
1761 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1763 switch (ipconfig->method) {
1764 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1765 case CONNMAN_IPCONFIG_METHOD_OFF:
1766 case CONNMAN_IPCONFIG_METHOD_DHCP:
1768 case CONNMAN_IPCONFIG_METHOD_FIXED:
1769 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1770 case CONNMAN_IPCONFIG_METHOD_AUTO:
1774 if (ipconfig->address == NULL)
1777 if (ipconfig->address->local != NULL) {
1778 connman_dbus_dict_append_basic(iter, "Address",
1779 DBUS_TYPE_STRING, &ipconfig->address->local);
1780 connman_dbus_dict_append_basic(iter, "PrefixLength",
1782 &ipconfig->address->prefixlen);
1785 if (ipconfig->address->gateway != NULL)
1786 connman_dbus_dict_append_basic(iter, "Gateway",
1787 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1789 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1790 connman_dbus_dict_append_basic(iter, "Privacy",
1791 DBUS_TYPE_STRING, &privacy);
1794 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1795 DBusMessageIter *iter)
1801 str = __connman_ipconfig_method2string(ipconfig->method);
1805 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1807 switch (ipconfig->method) {
1808 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1809 case CONNMAN_IPCONFIG_METHOD_OFF:
1810 case CONNMAN_IPCONFIG_METHOD_FIXED:
1811 case CONNMAN_IPCONFIG_METHOD_DHCP:
1812 case CONNMAN_IPCONFIG_METHOD_AUTO:
1814 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1818 if (ipconfig->address == NULL)
1821 if (ipconfig->address->local != NULL) {
1823 struct in_addr netmask;
1826 connman_dbus_dict_append_basic(iter, "Address",
1827 DBUS_TYPE_STRING, &ipconfig->address->local);
1829 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1830 netmask.s_addr = htonl(addr);
1831 mask = inet_ntoa(netmask);
1832 connman_dbus_dict_append_basic(iter, "Netmask",
1833 DBUS_TYPE_STRING, &mask);
1836 if (ipconfig->address->gateway != NULL)
1837 connman_dbus_dict_append_basic(iter, "Gateway",
1838 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1841 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1842 DBusMessageIter *array)
1844 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1845 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1846 *prefix_length_string = NULL, *privacy_string = NULL;
1847 int prefix_length = 0, privacy = 0;
1848 DBusMessageIter dict;
1850 DBG("ipconfig %p", ipconfig);
1852 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1855 dbus_message_iter_recurse(array, &dict);
1857 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1858 DBusMessageIter entry;
1862 dbus_message_iter_recurse(&dict, &entry);
1864 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1867 dbus_message_iter_get_basic(&entry, &key);
1868 dbus_message_iter_next(&entry);
1870 type = dbus_message_iter_get_arg_type(&entry);
1872 if (g_str_equal(key, "Method") == TRUE) {
1875 if (type != DBUS_TYPE_STRING)
1878 dbus_message_iter_get_basic(&entry, &str);
1879 method = __connman_ipconfig_string2method(str);
1880 } else if (g_str_equal(key, "Address") == TRUE) {
1881 if (type != DBUS_TYPE_STRING)
1884 dbus_message_iter_get_basic(&entry, &address);
1885 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1886 if (type != DBUS_TYPE_STRING)
1889 dbus_message_iter_get_basic(&entry,
1890 &prefix_length_string);
1892 prefix_length = atoi(prefix_length_string);
1893 if (prefix_length < 0 || prefix_length > 128)
1896 } else if (g_str_equal(key, "Netmask") == TRUE) {
1897 if (type != DBUS_TYPE_STRING)
1900 dbus_message_iter_get_basic(&entry, &netmask);
1901 } else if (g_str_equal(key, "Gateway") == TRUE) {
1902 if (type != DBUS_TYPE_STRING)
1905 dbus_message_iter_get_basic(&entry, &gateway);
1906 } else if (g_str_equal(key, "Privacy") == TRUE) {
1907 if (type != DBUS_TYPE_STRING)
1910 dbus_message_iter_get_basic(&entry, &privacy_string);
1911 privacy = string2privacy(privacy_string);
1913 dbus_message_iter_next(&dict);
1916 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
1918 method, address, netmask, gateway, prefix_length,
1922 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1923 case CONNMAN_IPCONFIG_METHOD_FIXED:
1926 case CONNMAN_IPCONFIG_METHOD_OFF:
1927 ipconfig->method = method;
1928 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1929 disable_ipv6(ipconfig);
1932 case CONNMAN_IPCONFIG_METHOD_AUTO:
1933 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1936 ipconfig->method = method;
1937 if (privacy_string != NULL)
1938 ipconfig->ipv6_privacy_config = privacy;
1939 enable_ipv6(ipconfig);
1942 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1943 if (address == NULL)
1946 ipconfig->method = method;
1948 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1949 connman_ipaddress_set_ipv4(ipconfig->address,
1950 address, netmask, gateway);
1952 return connman_ipaddress_set_ipv6(
1953 ipconfig->address, address,
1954 prefix_length, gateway);
1957 case CONNMAN_IPCONFIG_METHOD_DHCP:
1958 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1961 ipconfig->method = method;
1968 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1969 DBusMessageIter *iter)
1971 struct connman_ipdevice *ipdevice;
1972 const char *method = "auto";
1974 connman_dbus_dict_append_basic(iter, "Method",
1975 DBUS_TYPE_STRING, &method);
1977 ipdevice = g_hash_table_lookup(ipdevice_hash,
1978 GINT_TO_POINTER(ipconfig->index));
1979 if (ipdevice == NULL)
1982 if (ipdevice->ifname != NULL)
1983 connman_dbus_dict_append_basic(iter, "Interface",
1984 DBUS_TYPE_STRING, &ipdevice->ifname);
1986 if (ipdevice->address != NULL)
1987 connman_dbus_dict_append_basic(iter, "Address",
1988 DBUS_TYPE_STRING, &ipdevice->address);
1990 if (ipdevice->mtu > 0)
1991 connman_dbus_dict_append_basic(iter, "MTU",
1992 DBUS_TYPE_UINT16, &ipdevice->mtu);
1995 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1996 GKeyFile *keyfile, const char *identifier, const char *prefix)
2001 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2003 key = g_strdup_printf("%smethod", prefix);
2004 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2005 if (method == NULL) {
2006 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2007 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2009 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2011 ipconfig->method = __connman_ipconfig_string2method(method);
2013 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2014 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2016 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2017 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2018 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2020 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2021 privacy = g_key_file_get_string(keyfile, identifier,
2023 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2027 __connman_ipconfig_enable(ipconfig);
2028 enable_ipv6(ipconfig);
2035 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2036 ipconfig->address->prefixlen = g_key_file_get_integer(
2037 keyfile, identifier, key, NULL);
2040 key = g_strdup_printf("%slocal_address", prefix);
2041 ipconfig->address->local = g_key_file_get_string(
2042 keyfile, identifier, key, NULL);
2045 key = g_strdup_printf("%speer_address", prefix);
2046 ipconfig->address->peer = g_key_file_get_string(
2047 keyfile, identifier, key, NULL);
2050 key = g_strdup_printf("%sbroadcast_address", prefix);
2051 ipconfig->address->broadcast = g_key_file_get_string(
2052 keyfile, identifier, key, NULL);
2055 key = g_strdup_printf("%sgateway", prefix);
2056 ipconfig->address->gateway = g_key_file_get_string(
2057 keyfile, identifier, key, NULL);
2063 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2064 GKeyFile *keyfile, const char *identifier, const char *prefix)
2069 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2071 method = __connman_ipconfig_method2string(ipconfig->method);
2073 key = g_strdup_printf("%smethod", prefix);
2074 g_key_file_set_string(keyfile, identifier, key, method);
2077 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2078 const char *privacy;
2079 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2080 key = g_strdup_printf("%sprivacy", prefix);
2081 g_key_file_set_string(keyfile, identifier, key, privacy);
2085 switch (ipconfig->method) {
2086 case CONNMAN_IPCONFIG_METHOD_FIXED:
2087 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2089 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2090 case CONNMAN_IPCONFIG_METHOD_OFF:
2091 case CONNMAN_IPCONFIG_METHOD_DHCP:
2092 case CONNMAN_IPCONFIG_METHOD_AUTO:
2096 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2097 g_key_file_set_integer(keyfile, identifier,
2098 key, ipconfig->address->prefixlen);
2101 key = g_strdup_printf("%slocal_address", prefix);
2102 if (ipconfig->address->local != NULL)
2103 g_key_file_set_string(keyfile, identifier,
2104 key, ipconfig->address->local);
2107 key = g_strdup_printf("%speer_address", prefix);
2108 if (ipconfig->address->peer != NULL)
2109 g_key_file_set_string(keyfile, identifier,
2110 key, ipconfig->address->peer);
2113 key = g_strdup_printf("%sbroadcast_address", prefix);
2114 if (ipconfig->address->broadcast != NULL)
2115 g_key_file_set_string(keyfile, identifier,
2116 key, ipconfig->address->broadcast);
2119 key = g_strdup_printf("%sgateway", prefix);
2120 if (ipconfig->address->gateway != NULL)
2121 g_key_file_set_string(keyfile, identifier,
2122 key, ipconfig->address->gateway);
2128 int __connman_ipconfig_init(void)
2132 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2133 NULL, free_ipdevice);
2138 void __connman_ipconfig_cleanup(void)
2142 g_hash_table_destroy(ipdevice_hash);
2143 ipdevice_hash = NULL;