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>
33 #define IFF_LOWER_UP 0x10000
40 struct connman_ipconfig {
43 enum connman_ipconfig_type type;
45 struct connman_ipconfig *origin;
47 const struct connman_ipconfig_ops *ops;
50 enum connman_ipconfig_method method;
51 struct connman_ipaddress *address;
52 struct connman_ipaddress *system;
55 struct connman_ipdevice {
77 struct connman_ipconfig *config_ipv4;
78 struct connman_ipconfig *config_ipv6;
80 struct connman_ipconfig_driver *driver_ipv4;
81 struct connman_ipconfig *driver_config_ipv4;
83 struct connman_ipconfig_driver *driver_ipv6;
84 struct connman_ipconfig *driver_config_ipv6;
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,
156 const char *address, const char *gateway,
157 unsigned char prefix_length)
159 if (ipaddress == NULL)
162 if (check_ipv6_address(address) == FALSE)
165 if (check_ipv6_address(gateway) == FALSE)
168 DBG("prefix_len %d address %s gateway %s",
169 prefix_length, address, gateway);
171 ipaddress->prefixlen = prefix_length;
173 g_free(ipaddress->local);
174 ipaddress->local = g_strdup(address);
176 g_free(ipaddress->gateway);
177 ipaddress->gateway = g_strdup(gateway);
182 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
183 const char *address, const char *netmask, const char *gateway)
185 if (ipaddress == NULL)
188 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
190 g_free(ipaddress->local);
191 ipaddress->local = g_strdup(address);
193 g_free(ipaddress->gateway);
194 ipaddress->gateway = g_strdup(gateway);
197 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
199 if (ipaddress == NULL)
202 ipaddress->prefixlen = 0;
204 g_free(ipaddress->local);
205 ipaddress->local = NULL;
207 g_free(ipaddress->peer);
208 ipaddress->peer = NULL;
210 g_free(ipaddress->broadcast);
211 ipaddress->broadcast = NULL;
213 g_free(ipaddress->gateway);
214 ipaddress->gateway = NULL;
217 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
218 struct connman_ipaddress *source)
220 if (ipaddress == NULL || source == NULL)
223 ipaddress->family = source->family;
224 ipaddress->prefixlen = source->prefixlen;
226 g_free(ipaddress->local);
227 ipaddress->local = g_strdup(source->local);
229 g_free(ipaddress->peer);
230 ipaddress->peer = g_strdup(source->peer);
232 g_free(ipaddress->broadcast);
233 ipaddress->broadcast = g_strdup(source->broadcast);
235 g_free(ipaddress->gateway);
236 ipaddress->gateway = g_strdup(source->gateway);
239 static void free_address_list(struct connman_ipdevice *ipdevice)
243 for (list = ipdevice->address_list; list; list = list->next) {
244 struct connman_ipaddress *ipaddress = list->data;
246 connman_ipaddress_free(ipaddress);
250 g_slist_free(ipdevice->address_list);
251 ipdevice->address_list = NULL;
254 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
255 unsigned char prefixlen, const char *local)
259 for (list = ipdevice->address_list; list; list = list->next) {
260 struct connman_ipaddress *ipaddress = list->data;
262 if (g_strcmp0(ipaddress->local, local) == 0 &&
263 ipaddress->prefixlen == prefixlen)
270 static const char *type2str(unsigned short type)
275 case ARPHRD_LOOPBACK:
288 static const char *scope2str(unsigned char scope)
300 static void free_ipdevice(gpointer data)
302 struct connman_ipdevice *ipdevice = data;
304 connman_info("%s {remove} index %d", ipdevice->ifname,
307 if (ipdevice->config_ipv4 != NULL) {
308 connman_ipconfig_unref(ipdevice->config_ipv4);
309 ipdevice->config_ipv4 = NULL;
312 if (ipdevice->config_ipv6 != NULL) {
313 connman_ipconfig_unref(ipdevice->config_ipv6);
314 ipdevice->config_ipv6 = NULL;
317 free_address_list(ipdevice);
318 g_free(ipdevice->ipv4_gateway);
319 g_free(ipdevice->ipv6_gateway);
320 g_free(ipdevice->pac);
322 g_free(ipdevice->address);
323 g_free(ipdevice->ifname);
327 static GSList *driver_list = NULL;
329 static gint compare_priority(gconstpointer a, gconstpointer b)
331 const struct connman_ipconfig_driver *driver1 = a;
332 const struct connman_ipconfig_driver *driver2 = b;
334 return driver2->priority - driver1->priority;
338 * connman_ipconfig_driver_register:
339 * @driver: IP configuration driver
341 * Register a new IP configuration driver
343 * Returns: %0 on success
345 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
347 DBG("driver %p name %s", driver, driver->name);
349 driver_list = g_slist_insert_sorted(driver_list, driver,
356 * connman_ipconfig_driver_unregister:
357 * @driver: IP configuration driver
359 * Remove a previously registered IP configuration driver.
361 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
363 DBG("driver %p name %s", driver, driver->name);
365 driver_list = g_slist_remove(driver_list, driver);
368 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
371 int is_dhcpv4 = 0, is_dhcpv6 = 0;
374 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
375 ipdevice->config_ipv6);
377 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
380 if (ipdevice->driver_ipv4 != NULL && ipdevice->driver_ipv6 != NULL)
383 if (ipdevice->config_ipv4) {
384 switch (ipdevice->config_ipv4->method) {
385 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
386 case CONNMAN_IPCONFIG_METHOD_OFF:
387 case CONNMAN_IPCONFIG_METHOD_FIXED:
388 case CONNMAN_IPCONFIG_METHOD_MANUAL:
390 case CONNMAN_IPCONFIG_METHOD_DHCP:
396 if (ipdevice->config_ipv6) {
397 switch (ipdevice->config_ipv6->method) {
398 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
399 case CONNMAN_IPCONFIG_METHOD_OFF:
400 case CONNMAN_IPCONFIG_METHOD_FIXED:
401 case CONNMAN_IPCONFIG_METHOD_MANUAL:
403 case CONNMAN_IPCONFIG_METHOD_DHCP:
409 if (is_dhcpv4 && ipdevice->config_ipv4) {
410 ipdevice->driver_config_ipv4 = connman_ipconfig_clone(
411 ipdevice->config_ipv4);
412 if (ipdevice->driver_config_ipv4 == NULL)
416 if (is_dhcpv6 && ipdevice->config_ipv6) {
417 ipdevice->driver_config_ipv6 = connman_ipconfig_clone(
418 ipdevice->config_ipv6);
419 if (ipdevice->driver_config_ipv6 == NULL)
423 for (list = driver_list; list; list = list->next) {
424 struct connman_ipconfig_driver *driver = list->data;
426 if (is_dhcpv4 && ipdevice->driver_ipv4 != NULL) {
427 if (!driver->request(ipdevice->driver_config_ipv4)) {
428 ipdevice->driver_ipv4 = driver;
433 if (is_dhcpv6 && ipdevice->driver_ipv6 != NULL) {
434 if (!driver->request(ipdevice->driver_config_ipv6)) {
435 ipdevice->driver_ipv6 = driver;
444 if (ipdevice->driver_ipv4 == NULL) {
445 connman_ipconfig_unref(ipdevice->driver_config_ipv4);
446 ipdevice->driver_config_ipv4 = NULL;
449 if (ipdevice->driver_ipv6 == NULL) {
450 connman_ipconfig_unref(ipdevice->driver_config_ipv6);
451 ipdevice->driver_config_ipv6 = NULL;
455 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
457 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
458 ipdevice->config_ipv6);
460 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
463 if (ipdevice->driver_ipv4 == NULL && ipdevice->driver_ipv6 == NULL)
466 if (ipdevice->driver_ipv4) {
467 ipdevice->driver_ipv4->release(ipdevice->driver_config_ipv4);
468 ipdevice->driver_ipv4 = NULL;
471 if (ipdevice->driver_ipv6) {
472 ipdevice->driver_ipv6->release(ipdevice->driver_config_ipv6);
473 ipdevice->driver_ipv6 = NULL;
476 if (ipdevice->driver_config_ipv4) {
477 connman_ipconfig_unref(ipdevice->driver_config_ipv4);
478 ipdevice->driver_config_ipv4 = NULL;
481 if (ipdevice->driver_config_ipv6) {
482 connman_ipconfig_unref(ipdevice->driver_config_ipv6);
483 ipdevice->driver_config_ipv6 = NULL;
486 if (ipdevice->config_ipv4)
487 connman_inet_clear_address(ipdevice->index,
488 ipdevice->config_ipv4->address);
490 if (ipdevice->config_ipv6)
491 connman_inet_clear_ipv6_address(ipdevice->index,
492 ipdevice->config_ipv6->address->local,
493 ipdevice->config_ipv6->address->prefixlen);
496 static void update_stats(struct connman_ipdevice *ipdevice,
497 struct rtnl_link_stats *stats)
499 struct connman_service *service;
501 if (stats->rx_packets == 0 && stats->tx_packets == 0)
504 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
505 stats->rx_packets, stats->rx_bytes);
506 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
507 stats->tx_packets, stats->tx_bytes);
509 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
512 if (ipdevice->config_ipv4)
513 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
514 else if (ipdevice->config_ipv6)
515 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
522 ipdevice->rx_packets = stats->rx_packets;
523 ipdevice->tx_packets = stats->tx_packets;
524 ipdevice->rx_bytes = stats->rx_bytes;
525 ipdevice->tx_bytes = stats->tx_bytes;
526 ipdevice->rx_errors = stats->rx_errors;
527 ipdevice->tx_errors = stats->tx_errors;
528 ipdevice->rx_dropped = stats->rx_dropped;
529 ipdevice->tx_dropped = stats->tx_dropped;
531 __connman_service_notify(service,
532 ipdevice->rx_packets, ipdevice->tx_packets,
533 ipdevice->rx_bytes, ipdevice->tx_bytes,
534 ipdevice->rx_errors, ipdevice->tx_errors,
535 ipdevice->rx_dropped, ipdevice->tx_dropped);
538 void __connman_ipconfig_newlink(int index, unsigned short type,
539 unsigned int flags, const char *address,
541 struct rtnl_link_stats *stats)
543 struct connman_ipdevice *ipdevice;
546 gboolean up = FALSE, down = FALSE;
547 gboolean lower_up = FALSE, lower_down = FALSE;
549 DBG("index %d", index);
551 if (type == ARPHRD_LOOPBACK)
554 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
555 if (ipdevice != NULL)
558 ipdevice = g_try_new0(struct connman_ipdevice, 1);
559 if (ipdevice == NULL)
562 ipdevice->index = index;
563 ipdevice->ifname = connman_inet_ifname(index);
564 ipdevice->type = type;
566 ipdevice->address = g_strdup(address);
568 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
570 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
571 index, type, type2str(type));
576 update_stats(ipdevice, stats);
578 if (flags == ipdevice->flags)
581 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
588 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
589 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
590 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
591 (IFF_RUNNING | IFF_LOWER_UP))
593 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
597 ipdevice->flags = flags;
599 str = g_string_new(NULL);
604 g_string_append(str, "UP");
606 g_string_append(str, "DOWN");
608 if (flags & IFF_RUNNING)
609 g_string_append(str, ",RUNNING");
611 if (flags & IFF_LOWER_UP)
612 g_string_append(str, ",LOWER_UP");
614 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
617 g_string_free(str, TRUE);
619 for (list = g_list_first(ipconfig_list); list;
620 list = g_list_next(list)) {
621 struct connman_ipconfig *ipconfig = list->data;
623 if (index != ipconfig->index)
626 if (ipconfig->ops == NULL)
629 if (up == TRUE && ipconfig->ops->up)
630 ipconfig->ops->up(ipconfig);
631 if (lower_up == TRUE && ipconfig->ops->lower_up)
632 ipconfig->ops->lower_up(ipconfig);
634 if (lower_down == TRUE && ipconfig->ops->lower_down)
635 ipconfig->ops->lower_down(ipconfig);
636 if (down == TRUE && ipconfig->ops->down)
637 ipconfig->ops->down(ipconfig);
641 __connman_ipconfig_lower_up(ipdevice);
643 __connman_ipconfig_lower_down(ipdevice);
646 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
648 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 update_stats(ipdevice, stats);
659 for (list = g_list_first(ipconfig_list); list;
660 list = g_list_next(list)) {
661 struct connman_ipconfig *ipconfig = list->data;
663 if (index != ipconfig->index)
666 ipconfig->index = -1;
668 if (ipconfig->ops == NULL)
671 if (ipconfig->ops->lower_down)
672 ipconfig->ops->lower_down(ipconfig);
673 if (ipconfig->ops->down)
674 ipconfig->ops->down(ipconfig);
677 __connman_ipconfig_lower_down(ipdevice);
679 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
682 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
684 const struct connman_ipaddress *addr1 = a;
685 const struct connman_ipaddress *addr2 = b;
687 if (addr1->prefixlen != addr2->prefixlen)
688 return addr2->prefixlen - addr1->prefixlen;
690 return g_strcmp0(addr1->local, addr2->local);
693 void __connman_ipconfig_newaddr(int index, int family, const char *label,
694 unsigned char prefixlen, const char *address)
696 struct connman_ipdevice *ipdevice;
697 struct connman_ipaddress *ipaddress;
700 DBG("index %d", index);
702 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
703 if (ipdevice == NULL)
706 ipaddress = connman_ipaddress_alloc(family);
707 if (ipaddress == NULL)
710 ipaddress->prefixlen = prefixlen;
711 ipaddress->local = g_strdup(address);
713 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
714 check_duplicate_address)) {
715 connman_ipaddress_free(ipaddress);
719 ipdevice->address_list = g_slist_append(ipdevice->address_list,
722 connman_info("%s {add} address %s/%u label %s family %d",
723 ipdevice->ifname, address, prefixlen, label, family);
725 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
726 connman_ipaddress_copy(ipdevice->config_ipv4->system,
729 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
730 connman_ipaddress_copy(ipdevice->config_ipv6->system,
735 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
738 for (list = g_list_first(ipconfig_list); list;
739 list = g_list_next(list)) {
740 struct connman_ipconfig *ipconfig = list->data;
742 if (index != ipconfig->index)
745 if (ipconfig->ops == NULL)
748 if (ipconfig->ops->ip_bound)
749 ipconfig->ops->ip_bound(ipconfig);
753 void __connman_ipconfig_deladdr(int index, int family, const char *label,
754 unsigned char prefixlen, const char *address)
756 struct connman_ipdevice *ipdevice;
757 struct connman_ipaddress *ipaddress;
760 DBG("index %d", index);
762 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
763 if (ipdevice == NULL)
766 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
767 if (ipaddress == NULL)
770 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
773 connman_ipaddress_free(ipaddress);
775 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
776 address, prefixlen, label);
778 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
781 if (g_slist_length(ipdevice->address_list) > 0)
784 for (list = g_list_first(ipconfig_list); list;
785 list = g_list_next(list)) {
786 struct connman_ipconfig *ipconfig = list->data;
788 if (index != ipconfig->index)
791 if (ipconfig->ops == NULL)
794 if (ipconfig->ops->ip_release)
795 ipconfig->ops->ip_release(ipconfig);
799 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
800 const char *dst, const char *gateway)
802 struct connman_ipdevice *ipdevice;
804 DBG("index %d", index);
806 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
807 if (ipdevice == NULL)
810 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 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 =
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 =
836 for (list = ipdevice->address_list; list; list = list->next) {
837 struct connman_ipaddress *ipaddress = list->data;
839 g_free(ipaddress->gateway);
840 ipaddress->gateway = g_strdup(gateway);
843 for (config_list = g_list_first(ipconfig_list); config_list;
844 config_list = g_list_next(config_list)) {
845 struct connman_ipconfig *ipconfig = config_list->data;
847 if (index != ipconfig->index)
850 if (ipconfig->ops == NULL)
853 if (ipconfig->ops->ip_bound)
854 ipconfig->ops->ip_bound(ipconfig);
858 connman_info("%s {add} route %s gw %s scope %u <%s>",
859 ipdevice->ifname, dst, gateway,
860 scope, scope2str(scope));
863 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
864 const char *dst, const char *gateway)
866 struct connman_ipdevice *ipdevice;
868 DBG("index %d", index);
870 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
871 if (ipdevice == NULL)
874 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
878 if (family == AF_INET6) {
879 g_free(ipdevice->ipv6_gateway);
880 ipdevice->ipv6_gateway = NULL;
882 if (ipdevice->config_ipv6 != NULL &&
883 ipdevice->config_ipv6->system != NULL) {
884 g_free(ipdevice->config_ipv6->system->gateway);
885 ipdevice->config_ipv6->system->gateway = NULL;
888 g_free(ipdevice->ipv4_gateway);
889 ipdevice->ipv4_gateway = NULL;
891 if (ipdevice->config_ipv4 != NULL &&
892 ipdevice->config_ipv4->system != NULL) {
893 g_free(ipdevice->config_ipv4->system->gateway);
894 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 unsigned short __connman_ipconfig_get_type(int index)
945 struct connman_ipdevice *ipdevice;
947 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
948 if (ipdevice == NULL)
951 return ipdevice->type;
954 unsigned int __connman_ipconfig_get_flags(int index)
956 struct connman_ipdevice *ipdevice;
958 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
959 if (ipdevice == NULL)
962 return ipdevice->flags;
965 const char *__connman_ipconfig_get_gateway(int index)
967 struct connman_ipdevice *ipdevice;
969 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
970 if (ipdevice == NULL)
973 if (ipdevice->ipv4_gateway != NULL)
974 return ipdevice->ipv4_gateway;
976 if (ipdevice->config_ipv4 != NULL &&
977 ipdevice->config_ipv4->address != NULL)
978 return ipdevice->config_ipv4->address->gateway;
980 if (ipdevice->ipv6_gateway != NULL)
981 return ipdevice->ipv6_gateway;
983 if (ipdevice->config_ipv6 != NULL &&
984 ipdevice->config_ipv6->address != NULL)
985 return ipdevice->config_ipv6->address->gateway;
990 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
992 ipconfig->index = index;
995 static struct connman_ipconfig *create_ipv6config(int index)
997 struct connman_ipconfig *ipv6config;
999 DBG("index %d", index);
1001 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1002 if (ipv6config == NULL)
1005 ipv6config->index = index;
1006 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1007 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1009 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1010 if (ipv6config->address == NULL) {
1015 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1017 DBG("ipconfig %p", ipv6config);
1023 * connman_ipconfig_create:
1025 * Allocate a new ipconfig structure.
1027 * Returns: a newly-allocated #connman_ipconfig structure
1029 struct connman_ipconfig *connman_ipconfig_create(int index,
1030 enum connman_ipconfig_type type)
1032 struct connman_ipconfig *ipconfig;
1034 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1035 return create_ipv6config(index);
1037 DBG("index %d", index);
1039 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1040 if (ipconfig == NULL)
1043 ipconfig->refcount = 1;
1045 ipconfig->index = index;
1046 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1048 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1049 if (ipconfig->address == NULL) {
1054 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1056 DBG("ipconfig %p", ipconfig);
1062 * connman_ipconfig_clone:
1064 * Clone an ipconfig structure and create new reference.
1066 * Returns: a newly-allocated #connman_ipconfig structure
1068 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
1070 struct connman_ipconfig *ipconfig_clone;
1072 DBG("ipconfig %p", ipconfig);
1074 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
1075 if (ipconfig_clone == NULL)
1078 ipconfig_clone->refcount = 1;
1080 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
1082 ipconfig_clone->index = -1;
1084 return ipconfig_clone;
1088 * connman_ipconfig_ref:
1089 * @ipconfig: ipconfig structure
1091 * Increase reference counter of ipconfig
1093 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1095 g_atomic_int_inc(&ipconfig->refcount);
1101 * connman_ipconfig_unref:
1102 * @ipconfig: ipconfig structure
1104 * Decrease reference counter of ipconfig
1106 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1109 g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1110 __connman_ipconfig_disable(ipconfig);
1112 connman_ipconfig_set_ops(ipconfig, NULL);
1114 if (ipconfig->origin != NULL) {
1115 connman_ipconfig_unref(ipconfig->origin);
1116 ipconfig->origin = NULL;
1119 connman_ipaddress_free(ipconfig->system);
1120 connman_ipaddress_free(ipconfig->address);
1126 * connman_ipconfig_get_data:
1127 * @ipconfig: ipconfig structure
1129 * Get private data pointer
1131 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1133 return ipconfig->ops_data;
1137 * connman_ipconfig_set_data:
1138 * @ipconfig: ipconfig structure
1139 * @data: data pointer
1141 * Set private data pointer
1143 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1145 ipconfig->ops_data = data;
1149 * connman_ipconfig_get_index:
1150 * @ipconfig: ipconfig structure
1152 * Get interface index
1154 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1156 if (ipconfig == NULL)
1159 if (ipconfig->origin != NULL)
1160 return ipconfig->origin->index;
1162 return ipconfig->index;
1166 * connman_ipconfig_get_ifname:
1167 * @ipconfig: ipconfig structure
1169 * Get interface name
1171 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1173 struct connman_ipdevice *ipdevice;
1175 if (ipconfig == NULL)
1178 if (ipconfig->index < 0)
1181 ipdevice = g_hash_table_lookup(ipdevice_hash,
1182 GINT_TO_POINTER(ipconfig->index));
1183 if (ipdevice == NULL)
1186 return ipdevice->ifname;
1190 * connman_ipconfig_set_ops:
1191 * @ipconfig: ipconfig structure
1192 * @ops: operation callbacks
1194 * Set the operation callbacks
1196 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1197 const struct connman_ipconfig_ops *ops)
1199 ipconfig->ops = ops;
1202 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1203 struct connman_ipconfig *ipconfig)
1205 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1212 * connman_ipconfig_set_method:
1213 * @ipconfig: ipconfig structure
1214 * @method: configuration method
1216 * Set the configuration method
1218 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1219 enum connman_ipconfig_method method)
1221 ipconfig->method = method;
1226 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1228 if (ipconfig == NULL)
1229 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1231 return ipconfig->method;
1235 * connman_ipconfig_bind:
1236 * @ipconfig: ipconfig structure
1237 * @ipaddress: ipaddress structure
1239 * Bind IP address details to configuration
1241 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1242 struct connman_ipaddress *ipaddress)
1244 struct connman_ipconfig *origin;
1246 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1248 connman_ipaddress_copy(origin->address, ipaddress);
1250 connman_inet_set_address(origin->index, origin->address);
1253 void __connman_ipconfig_set_element_ipv6_gateway(
1254 struct connman_ipconfig *ipconfig,
1255 struct connman_element *element)
1257 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1258 element->ipv6.gateway = ipconfig->address->gateway;
1262 * FIXME: The element soulution should be removed in the future
1263 * Set IPv4 and IPv6 gateway
1265 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1266 struct connman_element *parent)
1268 struct connman_element *connection;
1270 connection = connman_element_create(NULL);
1272 DBG("ipconfig %p", ipconfig);
1274 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1275 connection->index = ipconfig->index;
1277 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1278 connection->ipv4.gateway = ipconfig->address->gateway;
1279 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1280 connection->ipv6.gateway = ipconfig->address->gateway;
1282 if (connman_element_register(connection, parent) < 0)
1283 connman_element_unref(connection);
1288 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1292 switch (ipconfig->method) {
1293 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1294 case CONNMAN_IPCONFIG_METHOD_OFF:
1295 case CONNMAN_IPCONFIG_METHOD_FIXED:
1296 case CONNMAN_IPCONFIG_METHOD_DHCP:
1298 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1299 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1300 return connman_inet_set_address(ipconfig->index,
1302 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1303 return connman_inet_set_ipv6_address(
1304 ipconfig->index, ipconfig->address);
1310 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1314 if (ipconfig == NULL)
1317 DBG("method %d", ipconfig->method);
1319 switch (ipconfig->method) {
1320 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1321 case CONNMAN_IPCONFIG_METHOD_OFF:
1322 case CONNMAN_IPCONFIG_METHOD_FIXED:
1323 case CONNMAN_IPCONFIG_METHOD_DHCP:
1325 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1326 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1327 return connman_inet_clear_address(ipconfig->index,
1329 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1330 return connman_inet_clear_ipv6_address(
1332 ipconfig->address->local,
1333 ipconfig->address->prefixlen);
1339 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1342 struct connman_ipdevice *ipdevice;
1344 DBG("ipconfig %p", ipconfig);
1346 if (ipconfig == NULL || ipconfig->index < 0)
1349 ipdevice = g_hash_table_lookup(ipdevice_hash,
1350 GINT_TO_POINTER(ipconfig->index));
1351 if (ipdevice == NULL)
1354 g_free(ipdevice->pac);
1355 ipdevice->pac = g_strdup(url);
1360 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1362 struct connman_ipdevice *ipdevice;
1364 DBG("ipconfig %p", ipconfig);
1366 if (ipconfig == NULL || ipconfig->index < 0)
1369 ipdevice = g_hash_table_lookup(ipdevice_hash,
1370 GINT_TO_POINTER(ipconfig->index));
1371 if (ipdevice == NULL)
1374 return ipdevice->pac;
1377 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1379 struct connman_ipdevice *ipdevice;
1380 gboolean up = FALSE, down = FALSE;
1381 gboolean lower_up = FALSE, lower_down = FALSE;
1382 enum connman_ipconfig_type type;
1384 DBG("ipconfig %p", ipconfig);
1386 if (ipconfig == NULL || ipconfig->index < 0)
1389 ipdevice = g_hash_table_lookup(ipdevice_hash,
1390 GINT_TO_POINTER(ipconfig->index));
1391 if (ipdevice == NULL)
1394 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1395 if (ipdevice->config_ipv4 == ipconfig)
1397 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1398 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1399 if (ipdevice->config_ipv6 == ipconfig)
1401 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1405 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1406 ipdevice->config_ipv4 != NULL) {
1407 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1409 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1411 connman_ipconfig_unref(ipdevice->config_ipv4);
1414 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1415 ipdevice->config_ipv6 != NULL) {
1416 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1418 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1420 connman_ipconfig_unref(ipdevice->config_ipv6);
1423 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1424 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1425 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1426 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1428 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1430 if (ipdevice->flags & IFF_UP)
1435 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1436 (IFF_RUNNING | IFF_LOWER_UP))
1438 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1441 if (up == TRUE && ipconfig->ops->up)
1442 ipconfig->ops->up(ipconfig);
1443 if (lower_up == TRUE && ipconfig->ops->lower_up)
1444 ipconfig->ops->lower_up(ipconfig);
1446 if (lower_down == TRUE && ipconfig->ops->lower_down)
1447 ipconfig->ops->lower_down(ipconfig);
1448 if (down == TRUE && ipconfig->ops->down)
1449 ipconfig->ops->down(ipconfig);
1454 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1456 struct connman_ipdevice *ipdevice;
1458 DBG("ipconfig %p", ipconfig);
1460 if (ipconfig == NULL || ipconfig->index < 0)
1463 ipdevice = g_hash_table_lookup(ipdevice_hash,
1464 GINT_TO_POINTER(ipconfig->index));
1465 if (ipdevice == NULL)
1468 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1471 if (ipdevice->config_ipv4 == ipconfig) {
1472 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1474 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1475 connman_ipconfig_unref(ipdevice->config_ipv4);
1476 ipdevice->config_ipv4 = NULL;
1480 if (ipdevice->config_ipv6 == ipconfig) {
1481 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1483 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1484 connman_ipconfig_unref(ipdevice->config_ipv6);
1485 ipdevice->config_ipv6 = NULL;
1492 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1495 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1497 case CONNMAN_IPCONFIG_METHOD_OFF:
1499 case CONNMAN_IPCONFIG_METHOD_FIXED:
1501 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1503 case CONNMAN_IPCONFIG_METHOD_DHCP:
1510 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1512 if (g_strcmp0(method, "off") == 0)
1513 return CONNMAN_IPCONFIG_METHOD_OFF;
1514 else if (g_strcmp0(method, "fixed") == 0)
1515 return CONNMAN_IPCONFIG_METHOD_FIXED;
1516 else if (g_strcmp0(method, "manual") == 0)
1517 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1518 else if (g_strcmp0(method, "dhcp") == 0)
1519 return CONNMAN_IPCONFIG_METHOD_DHCP;
1521 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1524 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1525 DBusMessageIter *iter)
1531 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1534 str = __connman_ipconfig_method2string(ipconfig->method);
1538 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1540 if (ipconfig->system == NULL)
1543 if (ipconfig->system->local != NULL) {
1545 struct in_addr netmask;
1548 connman_dbus_dict_append_basic(iter, "Address",
1549 DBUS_TYPE_STRING, &ipconfig->system->local);
1551 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1552 netmask.s_addr = htonl(addr);
1553 mask = inet_ntoa(netmask);
1554 connman_dbus_dict_append_basic(iter, "Netmask",
1555 DBUS_TYPE_STRING, &mask);
1558 if (ipconfig->system->gateway != NULL)
1559 connman_dbus_dict_append_basic(iter, "Gateway",
1560 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1563 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1564 DBusMessageIter *iter)
1570 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1573 str = __connman_ipconfig_method2string(ipconfig->method);
1577 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1579 if (ipconfig->system == NULL)
1582 if (ipconfig->system->local != NULL) {
1583 connman_dbus_dict_append_basic(iter, "Address",
1584 DBUS_TYPE_STRING, &ipconfig->system->local);
1585 connman_dbus_dict_append_basic(iter, "PrefixLength",
1587 &ipconfig->system->prefixlen);
1590 if (ipconfig->system->gateway != NULL)
1591 connman_dbus_dict_append_basic(iter, "Gateway",
1592 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1595 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1596 DBusMessageIter *iter)
1602 str = __connman_ipconfig_method2string(ipconfig->method);
1606 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1608 switch (ipconfig->method) {
1609 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1610 case CONNMAN_IPCONFIG_METHOD_OFF:
1611 case CONNMAN_IPCONFIG_METHOD_DHCP:
1613 case CONNMAN_IPCONFIG_METHOD_FIXED:
1614 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1618 if (ipconfig->address == NULL)
1621 if (ipconfig->address->local != NULL) {
1622 connman_dbus_dict_append_basic(iter, "Address",
1623 DBUS_TYPE_STRING, &ipconfig->address->local);
1624 connman_dbus_dict_append_basic(iter, "PrefixLength",
1626 &ipconfig->address->prefixlen);
1629 if (ipconfig->address->gateway != NULL)
1630 connman_dbus_dict_append_basic(iter, "Gateway",
1631 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1634 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1635 DBusMessageIter *iter)
1641 str = __connman_ipconfig_method2string(ipconfig->method);
1645 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1647 switch (ipconfig->method) {
1648 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1649 case CONNMAN_IPCONFIG_METHOD_OFF:
1650 case CONNMAN_IPCONFIG_METHOD_FIXED:
1651 case CONNMAN_IPCONFIG_METHOD_DHCP:
1653 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1657 if (ipconfig->address == NULL)
1660 if (ipconfig->address->local != NULL) {
1662 struct in_addr netmask;
1665 connman_dbus_dict_append_basic(iter, "Address",
1666 DBUS_TYPE_STRING, &ipconfig->address->local);
1668 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1669 netmask.s_addr = htonl(addr);
1670 mask = inet_ntoa(netmask);
1671 connman_dbus_dict_append_basic(iter, "Netmask",
1672 DBUS_TYPE_STRING, &mask);
1675 if (ipconfig->address->gateway != NULL)
1676 connman_dbus_dict_append_basic(iter, "Gateway",
1677 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1680 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1681 enum connman_ipconfig_type type, DBusMessageIter *array)
1683 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1684 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1685 *prefix_length_string = NULL;
1686 int prefix_length = 0;
1687 DBusMessageIter dict;
1689 DBG("ipconfig %p type %d", ipconfig, type);
1691 if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1692 type != CONNMAN_IPCONFIG_TYPE_IPV6)
1695 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1698 dbus_message_iter_recurse(array, &dict);
1700 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1701 DBusMessageIter entry;
1705 dbus_message_iter_recurse(&dict, &entry);
1707 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1710 dbus_message_iter_get_basic(&entry, &key);
1711 dbus_message_iter_next(&entry);
1713 type = dbus_message_iter_get_arg_type(&entry);
1715 if (g_str_equal(key, "Method") == TRUE) {
1718 if (type != DBUS_TYPE_STRING)
1721 dbus_message_iter_get_basic(&entry, &str);
1722 method = __connman_ipconfig_string2method(str);
1723 } else if (g_str_equal(key, "Address") == TRUE) {
1724 if (type != DBUS_TYPE_STRING)
1727 dbus_message_iter_get_basic(&entry, &address);
1728 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1729 if (type != DBUS_TYPE_STRING)
1732 dbus_message_iter_get_basic(&entry,
1733 &prefix_length_string);
1735 prefix_length = atoi(prefix_length_string);
1736 if (prefix_length < 0 || prefix_length > 128)
1739 } else if (g_str_equal(key, "Netmask") == TRUE) {
1740 if (type != DBUS_TYPE_STRING)
1743 dbus_message_iter_get_basic(&entry, &netmask);
1744 } else if (g_str_equal(key, "Gateway") == TRUE) {
1745 if (type != DBUS_TYPE_STRING)
1748 dbus_message_iter_get_basic(&entry, &gateway);
1750 dbus_message_iter_next(&dict);
1753 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1754 method, address, netmask, gateway, prefix_length);
1757 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1758 case CONNMAN_IPCONFIG_METHOD_OFF:
1759 case CONNMAN_IPCONFIG_METHOD_FIXED:
1762 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1763 if (address == NULL)
1766 ipconfig->method = method;
1768 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1769 connman_ipaddress_set_ipv4(ipconfig->address,
1770 address, netmask, gateway);
1772 return connman_ipaddress_set_ipv6(
1773 ipconfig->address, address,
1774 gateway, prefix_length);
1777 case CONNMAN_IPCONFIG_METHOD_DHCP:
1778 if (ipconfig->method == method)
1781 ipconfig->method = method;
1788 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1789 DBusMessageIter *iter)
1791 struct connman_ipdevice *ipdevice;
1792 const char *method = "auto";
1794 connman_dbus_dict_append_basic(iter, "Method",
1795 DBUS_TYPE_STRING, &method);
1797 ipdevice = g_hash_table_lookup(ipdevice_hash,
1798 GINT_TO_POINTER(ipconfig->index));
1799 if (ipdevice == NULL)
1802 if (ipdevice->ifname != NULL)
1803 connman_dbus_dict_append_basic(iter, "Interface",
1804 DBUS_TYPE_STRING, &ipdevice->ifname);
1806 if (ipdevice->address != NULL)
1807 connman_dbus_dict_append_basic(iter, "Address",
1808 DBUS_TYPE_STRING, &ipdevice->address);
1810 if (ipdevice->mtu > 0)
1811 connman_dbus_dict_append_basic(iter, "MTU",
1812 DBUS_TYPE_UINT16, &ipdevice->mtu);
1815 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1816 GKeyFile *keyfile, const char *identifier, const char *prefix)
1821 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1823 key = g_strdup_printf("%smethod", prefix);
1824 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1825 if (method == NULL) {
1826 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1827 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1829 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1831 ipconfig->method = __connman_ipconfig_string2method(method);
1834 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1835 ipconfig->address->prefixlen = g_key_file_get_integer(
1836 keyfile, identifier, key, NULL);
1839 key = g_strdup_printf("%slocal_address", prefix);
1840 ipconfig->address->local = g_key_file_get_string(
1841 keyfile, identifier, key, NULL);
1844 key = g_strdup_printf("%speer_address", prefix);
1845 ipconfig->address->peer = g_key_file_get_string(
1846 keyfile, identifier, key, NULL);
1849 key = g_strdup_printf("%sbroadcast_address", prefix);
1850 ipconfig->address->broadcast = g_key_file_get_string(
1851 keyfile, identifier, key, NULL);
1854 key = g_strdup_printf("%sgateway", prefix);
1855 ipconfig->address->gateway = g_key_file_get_string(
1856 keyfile, identifier, key, NULL);
1862 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1863 GKeyFile *keyfile, const char *identifier, const char *prefix)
1868 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1870 method = __connman_ipconfig_method2string(ipconfig->method);
1872 key = g_strdup_printf("%smethod", prefix);
1873 g_key_file_set_string(keyfile, identifier, key, method);
1876 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1877 g_key_file_set_integer(keyfile, identifier,
1878 key, ipconfig->address->prefixlen);
1881 key = g_strdup_printf("%slocal_address", prefix);
1882 if (ipconfig->address->local != NULL)
1883 g_key_file_set_string(keyfile, identifier,
1884 key, ipconfig->address->local);
1887 key = g_strdup_printf("%speer_address", prefix);
1888 if (ipconfig->address->peer != NULL)
1889 g_key_file_set_string(keyfile, identifier,
1890 key, ipconfig->address->peer);
1893 key = g_strdup_printf("%sbroadcast_address", prefix);
1894 if (ipconfig->address->broadcast != NULL)
1895 g_key_file_set_string(keyfile, identifier,
1896 key, ipconfig->address->broadcast);
1899 key = g_strdup_printf("%sgateway", prefix);
1900 if (ipconfig->address->gateway != NULL)
1901 g_key_file_set_string(keyfile, identifier,
1902 key, ipconfig->address->gateway);
1908 int __connman_ipconfig_init(void)
1912 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1913 NULL, free_ipdevice);
1918 void __connman_ipconfig_cleanup(void)
1922 g_hash_table_destroy(ipdevice_hash);
1923 ipdevice_hash = NULL;