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;
1203 * connman_ipconfig_set_method:
1204 * @ipconfig: ipconfig structure
1205 * @method: configuration method
1207 * Set the configuration method
1209 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1210 enum connman_ipconfig_method method)
1212 ipconfig->method = method;
1217 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1219 if (ipconfig == NULL)
1220 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1222 return ipconfig->method;
1226 * connman_ipconfig_bind:
1227 * @ipconfig: ipconfig structure
1228 * @ipaddress: ipaddress structure
1230 * Bind IP address details to configuration
1232 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1233 struct connman_ipaddress *ipaddress)
1235 struct connman_ipconfig *origin;
1237 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1239 connman_ipaddress_copy(origin->address, ipaddress);
1241 connman_inet_set_address(origin->index, origin->address);
1244 void __connman_ipconfig_set_element_ipv6_gateway(
1245 struct connman_ipconfig *ipconfig,
1246 struct connman_element *element)
1248 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1249 element->ipv6.gateway = ipconfig->address->gateway;
1253 * FIXME: The element soulution should be removed in the future
1254 * Set IPv4 and IPv6 gateway
1256 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1257 struct connman_element *parent)
1259 struct connman_element *connection;
1261 connection = connman_element_create(NULL);
1263 DBG("ipconfig %p", ipconfig);
1265 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1266 connection->index = ipconfig->index;
1268 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1269 connection->ipv4.gateway = ipconfig->address->gateway;
1270 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1271 connection->ipv6.gateway = ipconfig->address->gateway;
1273 if (connman_element_register(connection, parent) < 0)
1274 connman_element_unref(connection);
1279 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1283 switch (ipconfig->method) {
1284 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1285 case CONNMAN_IPCONFIG_METHOD_OFF:
1286 case CONNMAN_IPCONFIG_METHOD_FIXED:
1287 case CONNMAN_IPCONFIG_METHOD_DHCP:
1289 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1290 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1291 return connman_inet_set_address(ipconfig->index,
1293 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1294 return connman_inet_set_ipv6_address(
1295 ipconfig->index, ipconfig->address);
1301 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1305 if (ipconfig == NULL)
1308 DBG("method %d", ipconfig->method);
1310 switch (ipconfig->method) {
1311 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1312 case CONNMAN_IPCONFIG_METHOD_OFF:
1313 case CONNMAN_IPCONFIG_METHOD_FIXED:
1314 case CONNMAN_IPCONFIG_METHOD_DHCP:
1316 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1317 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1318 return connman_inet_clear_address(ipconfig->index,
1320 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1321 return connman_inet_clear_ipv6_address(
1323 ipconfig->address->local,
1324 ipconfig->address->prefixlen);
1330 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1333 struct connman_ipdevice *ipdevice;
1335 DBG("ipconfig %p", ipconfig);
1337 if (ipconfig == NULL || ipconfig->index < 0)
1340 ipdevice = g_hash_table_lookup(ipdevice_hash,
1341 GINT_TO_POINTER(ipconfig->index));
1342 if (ipdevice == NULL)
1345 g_free(ipdevice->pac);
1346 ipdevice->pac = g_strdup(url);
1351 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1353 struct connman_ipdevice *ipdevice;
1355 DBG("ipconfig %p", ipconfig);
1357 if (ipconfig == NULL || ipconfig->index < 0)
1360 ipdevice = g_hash_table_lookup(ipdevice_hash,
1361 GINT_TO_POINTER(ipconfig->index));
1362 if (ipdevice == NULL)
1365 return ipdevice->pac;
1368 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1370 struct connman_ipdevice *ipdevice;
1371 gboolean up = FALSE, down = FALSE;
1372 gboolean lower_up = FALSE, lower_down = FALSE;
1373 enum connman_ipconfig_type type;
1375 DBG("ipconfig %p", ipconfig);
1377 if (ipconfig == NULL || ipconfig->index < 0)
1380 ipdevice = g_hash_table_lookup(ipdevice_hash,
1381 GINT_TO_POINTER(ipconfig->index));
1382 if (ipdevice == NULL)
1385 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1386 if (ipdevice->config_ipv4 == ipconfig)
1388 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1389 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1390 if (ipdevice->config_ipv6 == ipconfig)
1392 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1396 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1397 ipdevice->config_ipv4 != NULL) {
1398 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1400 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1402 connman_ipconfig_unref(ipdevice->config_ipv4);
1405 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1406 ipdevice->config_ipv6 != NULL) {
1407 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1409 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1411 connman_ipconfig_unref(ipdevice->config_ipv6);
1414 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1415 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1416 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1417 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1419 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1421 if (ipdevice->flags & IFF_UP)
1426 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1427 (IFF_RUNNING | IFF_LOWER_UP))
1429 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1432 if (up == TRUE && ipconfig->ops->up)
1433 ipconfig->ops->up(ipconfig);
1434 if (lower_up == TRUE && ipconfig->ops->lower_up)
1435 ipconfig->ops->lower_up(ipconfig);
1437 if (lower_down == TRUE && ipconfig->ops->lower_down)
1438 ipconfig->ops->lower_down(ipconfig);
1439 if (down == TRUE && ipconfig->ops->down)
1440 ipconfig->ops->down(ipconfig);
1445 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1447 struct connman_ipdevice *ipdevice;
1449 DBG("ipconfig %p", ipconfig);
1451 if (ipconfig == NULL || ipconfig->index < 0)
1454 ipdevice = g_hash_table_lookup(ipdevice_hash,
1455 GINT_TO_POINTER(ipconfig->index));
1456 if (ipdevice == NULL)
1459 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1462 if (ipdevice->config_ipv4 == ipconfig) {
1463 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1465 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1466 connman_ipconfig_unref(ipdevice->config_ipv4);
1467 ipdevice->config_ipv4 = NULL;
1471 if (ipdevice->config_ipv6 == ipconfig) {
1472 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1474 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1475 connman_ipconfig_unref(ipdevice->config_ipv6);
1476 ipdevice->config_ipv6 = NULL;
1483 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1486 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1488 case CONNMAN_IPCONFIG_METHOD_OFF:
1490 case CONNMAN_IPCONFIG_METHOD_FIXED:
1492 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1494 case CONNMAN_IPCONFIG_METHOD_DHCP:
1501 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1503 if (g_strcmp0(method, "off") == 0)
1504 return CONNMAN_IPCONFIG_METHOD_OFF;
1505 else if (g_strcmp0(method, "fixed") == 0)
1506 return CONNMAN_IPCONFIG_METHOD_FIXED;
1507 else if (g_strcmp0(method, "manual") == 0)
1508 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1509 else if (g_strcmp0(method, "dhcp") == 0)
1510 return CONNMAN_IPCONFIG_METHOD_DHCP;
1512 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1515 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1516 DBusMessageIter *iter)
1522 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1525 str = __connman_ipconfig_method2string(ipconfig->method);
1529 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1531 if (ipconfig->system == NULL)
1534 if (ipconfig->system->local != NULL) {
1536 struct in_addr netmask;
1539 connman_dbus_dict_append_basic(iter, "Address",
1540 DBUS_TYPE_STRING, &ipconfig->system->local);
1542 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1543 netmask.s_addr = htonl(addr);
1544 mask = inet_ntoa(netmask);
1545 connman_dbus_dict_append_basic(iter, "Netmask",
1546 DBUS_TYPE_STRING, &mask);
1549 if (ipconfig->system->gateway != NULL)
1550 connman_dbus_dict_append_basic(iter, "Gateway",
1551 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1554 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1555 DBusMessageIter *iter)
1561 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1564 str = __connman_ipconfig_method2string(ipconfig->method);
1568 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1570 if (ipconfig->system == NULL)
1573 if (ipconfig->system->local != NULL) {
1574 connman_dbus_dict_append_basic(iter, "Address",
1575 DBUS_TYPE_STRING, &ipconfig->system->local);
1576 connman_dbus_dict_append_basic(iter, "PrefixLength",
1578 &ipconfig->system->prefixlen);
1581 if (ipconfig->system->gateway != NULL)
1582 connman_dbus_dict_append_basic(iter, "Gateway",
1583 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1586 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1587 DBusMessageIter *iter)
1593 str = __connman_ipconfig_method2string(ipconfig->method);
1597 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1599 switch (ipconfig->method) {
1600 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1601 case CONNMAN_IPCONFIG_METHOD_OFF:
1602 case CONNMAN_IPCONFIG_METHOD_DHCP:
1604 case CONNMAN_IPCONFIG_METHOD_FIXED:
1605 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1609 if (ipconfig->address == NULL)
1612 if (ipconfig->address->local != NULL) {
1613 connman_dbus_dict_append_basic(iter, "Address",
1614 DBUS_TYPE_STRING, &ipconfig->address->local);
1615 connman_dbus_dict_append_basic(iter, "PrefixLength",
1617 &ipconfig->address->prefixlen);
1620 if (ipconfig->address->gateway != NULL)
1621 connman_dbus_dict_append_basic(iter, "Gateway",
1622 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1625 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1626 DBusMessageIter *iter)
1632 str = __connman_ipconfig_method2string(ipconfig->method);
1636 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1638 switch (ipconfig->method) {
1639 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1640 case CONNMAN_IPCONFIG_METHOD_OFF:
1641 case CONNMAN_IPCONFIG_METHOD_FIXED:
1642 case CONNMAN_IPCONFIG_METHOD_DHCP:
1644 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1648 if (ipconfig->address == NULL)
1651 if (ipconfig->address->local != NULL) {
1653 struct in_addr netmask;
1656 connman_dbus_dict_append_basic(iter, "Address",
1657 DBUS_TYPE_STRING, &ipconfig->address->local);
1659 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1660 netmask.s_addr = htonl(addr);
1661 mask = inet_ntoa(netmask);
1662 connman_dbus_dict_append_basic(iter, "Netmask",
1663 DBUS_TYPE_STRING, &mask);
1666 if (ipconfig->address->gateway != NULL)
1667 connman_dbus_dict_append_basic(iter, "Gateway",
1668 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1671 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1672 enum connman_ipconfig_type type, DBusMessageIter *array)
1674 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1675 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1676 *prefix_length_string = NULL;
1677 int prefix_length = 0;
1678 DBusMessageIter dict;
1680 DBG("ipconfig %p type %d", ipconfig, type);
1682 if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1683 type != CONNMAN_IPCONFIG_TYPE_IPV6)
1686 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1689 dbus_message_iter_recurse(array, &dict);
1691 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1692 DBusMessageIter entry;
1696 dbus_message_iter_recurse(&dict, &entry);
1698 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1701 dbus_message_iter_get_basic(&entry, &key);
1702 dbus_message_iter_next(&entry);
1704 type = dbus_message_iter_get_arg_type(&entry);
1706 if (g_str_equal(key, "Method") == TRUE) {
1709 if (type != DBUS_TYPE_STRING)
1712 dbus_message_iter_get_basic(&entry, &str);
1713 method = __connman_ipconfig_string2method(str);
1714 } else if (g_str_equal(key, "Address") == TRUE) {
1715 if (type != DBUS_TYPE_STRING)
1718 dbus_message_iter_get_basic(&entry, &address);
1719 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1720 if (type != DBUS_TYPE_STRING)
1723 dbus_message_iter_get_basic(&entry,
1724 &prefix_length_string);
1726 prefix_length = atoi(prefix_length_string);
1727 if (prefix_length < 0 || prefix_length > 128)
1730 } else if (g_str_equal(key, "Netmask") == TRUE) {
1731 if (type != DBUS_TYPE_STRING)
1734 dbus_message_iter_get_basic(&entry, &netmask);
1735 } else if (g_str_equal(key, "Gateway") == TRUE) {
1736 if (type != DBUS_TYPE_STRING)
1739 dbus_message_iter_get_basic(&entry, &gateway);
1741 dbus_message_iter_next(&dict);
1744 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1745 method, address, netmask, gateway, prefix_length);
1748 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1749 case CONNMAN_IPCONFIG_METHOD_OFF:
1750 case CONNMAN_IPCONFIG_METHOD_FIXED:
1753 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1754 if (address == NULL)
1757 ipconfig->method = method;
1759 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1760 connman_ipaddress_set_ipv4(ipconfig->address,
1761 address, netmask, gateway);
1763 return connman_ipaddress_set_ipv6(
1764 ipconfig->address, address,
1765 gateway, prefix_length);
1768 case CONNMAN_IPCONFIG_METHOD_DHCP:
1769 if (ipconfig->method == method)
1772 ipconfig->method = method;
1779 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1780 DBusMessageIter *iter)
1782 struct connman_ipdevice *ipdevice;
1783 const char *method = "auto";
1785 connman_dbus_dict_append_basic(iter, "Method",
1786 DBUS_TYPE_STRING, &method);
1788 ipdevice = g_hash_table_lookup(ipdevice_hash,
1789 GINT_TO_POINTER(ipconfig->index));
1790 if (ipdevice == NULL)
1793 if (ipdevice->ifname != NULL)
1794 connman_dbus_dict_append_basic(iter, "Interface",
1795 DBUS_TYPE_STRING, &ipdevice->ifname);
1797 if (ipdevice->address != NULL)
1798 connman_dbus_dict_append_basic(iter, "Address",
1799 DBUS_TYPE_STRING, &ipdevice->address);
1801 if (ipdevice->mtu > 0)
1802 connman_dbus_dict_append_basic(iter, "MTU",
1803 DBUS_TYPE_UINT16, &ipdevice->mtu);
1806 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1807 GKeyFile *keyfile, const char *identifier, const char *prefix)
1812 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1814 key = g_strdup_printf("%smethod", prefix);
1815 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1816 if (method == NULL) {
1817 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1818 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1820 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1822 ipconfig->method = __connman_ipconfig_string2method(method);
1825 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1826 ipconfig->address->prefixlen = g_key_file_get_integer(
1827 keyfile, identifier, key, NULL);
1830 key = g_strdup_printf("%slocal_address", prefix);
1831 ipconfig->address->local = g_key_file_get_string(
1832 keyfile, identifier, key, NULL);
1835 key = g_strdup_printf("%speer_address", prefix);
1836 ipconfig->address->peer = g_key_file_get_string(
1837 keyfile, identifier, key, NULL);
1840 key = g_strdup_printf("%sbroadcast_address", prefix);
1841 ipconfig->address->broadcast = g_key_file_get_string(
1842 keyfile, identifier, key, NULL);
1845 key = g_strdup_printf("%sgateway", prefix);
1846 ipconfig->address->gateway = g_key_file_get_string(
1847 keyfile, identifier, key, NULL);
1853 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1854 GKeyFile *keyfile, const char *identifier, const char *prefix)
1859 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1861 method = __connman_ipconfig_method2string(ipconfig->method);
1863 key = g_strdup_printf("%smethod", prefix);
1864 g_key_file_set_string(keyfile, identifier, key, method);
1867 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1868 g_key_file_set_integer(keyfile, identifier,
1869 key, ipconfig->address->prefixlen);
1872 key = g_strdup_printf("%slocal_address", prefix);
1873 if (ipconfig->address->local != NULL)
1874 g_key_file_set_string(keyfile, identifier,
1875 key, ipconfig->address->local);
1878 key = g_strdup_printf("%speer_address", prefix);
1879 if (ipconfig->address->peer != NULL)
1880 g_key_file_set_string(keyfile, identifier,
1881 key, ipconfig->address->peer);
1884 key = g_strdup_printf("%sbroadcast_address", prefix);
1885 if (ipconfig->address->broadcast != NULL)
1886 g_key_file_set_string(keyfile, identifier,
1887 key, ipconfig->address->broadcast);
1890 key = g_strdup_printf("%sgateway", prefix);
1891 if (ipconfig->address->gateway != NULL)
1892 g_key_file_set_string(keyfile, identifier,
1893 key, ipconfig->address->gateway);
1899 int __connman_ipconfig_init(void)
1903 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1904 NULL, free_ipdevice);
1909 void __connman_ipconfig_cleanup(void)
1913 g_hash_table_destroy(ipdevice_hash);
1914 ipdevice_hash = NULL;