5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <net/if_arp.h>
28 #include <linux/if_link.h>
31 #define IFF_LOWER_UP 0x10000
38 struct connman_ipconfig {
42 struct connman_ipconfig *origin;
44 const struct connman_ipconfig_ops *ops;
47 enum connman_ipconfig_method method;
48 struct connman_ipaddress *address;
49 struct connman_ipaddress *system;
52 struct connman_ipdevice {
65 struct connman_ipconfig *config;
67 struct connman_ipconfig_driver *driver;
68 struct connman_ipconfig *driver_config;
71 static GHashTable *ipdevice_hash = NULL;
72 static GList *ipconfig_list = NULL;
74 struct connman_ipaddress *connman_ipaddress_alloc(void)
76 struct connman_ipaddress *ipaddress;
78 ipaddress = g_try_new0(struct connman_ipaddress, 1);
79 if (ipaddress == NULL)
85 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
87 if (ipaddress == NULL)
90 g_free(ipaddress->broadcast);
91 g_free(ipaddress->peer);
92 g_free(ipaddress->local);
96 static unsigned char netmask2prefixlen(const char *netmask)
98 unsigned char bits = 0;
99 in_addr_t mask = inet_network(netmask);
100 in_addr_t host = ~mask;
102 /* a valid netmask must be 2^n - 1 */
103 if ((host & (host + 1)) != 0)
106 for (; mask; mask <<= 1)
112 void connman_ipaddress_set(struct connman_ipaddress *ipaddress,
113 const char *address, const char *netmask)
115 if (ipaddress == NULL)
119 ipaddress->prefixlen = netmask2prefixlen(netmask);
121 ipaddress->prefixlen = 32;
123 g_free(ipaddress->local);
124 ipaddress->local = g_strdup(address);
127 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
129 if (ipaddress == NULL)
132 ipaddress->prefixlen = 0;
134 g_free(ipaddress->local);
135 ipaddress->local = NULL;
137 g_free(ipaddress->peer);
138 ipaddress->peer = NULL;
140 g_free(ipaddress->broadcast);
141 ipaddress->broadcast = NULL;
144 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
145 struct connman_ipaddress *source)
147 if (ipaddress == NULL || source == NULL)
150 ipaddress->prefixlen = source->prefixlen;
152 g_free(ipaddress->local);
153 ipaddress->local = g_strdup(source->local);
155 g_free(ipaddress->peer);
156 ipaddress->peer = g_strdup(source->peer);
158 g_free(ipaddress->broadcast);
159 ipaddress->broadcast = g_strdup(source->broadcast);
162 static void free_address_list(struct connman_ipdevice *ipdevice)
166 for (list = ipdevice->address_list; list; list = list->next) {
167 struct connman_ipaddress *ipaddress = list->data;
169 connman_ipaddress_free(ipaddress);
173 g_slist_free(ipdevice->address_list);
174 ipdevice->address_list = NULL;
177 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
178 unsigned char prefixlen, const char *local)
182 for (list = ipdevice->address_list; list; list = list->next) {
183 struct connman_ipaddress *ipaddress = list->data;
185 if (g_strcmp0(ipaddress->local, local) == 0 &&
186 ipaddress->prefixlen == prefixlen)
193 static const char *type2str(unsigned short type)
198 case ARPHRD_LOOPBACK:
211 static const char *scope2str(unsigned char scope)
223 static void free_ipdevice(gpointer data)
225 struct connman_ipdevice *ipdevice = data;
227 connman_info("%s {remove} index %d", ipdevice->ifname,
230 if (ipdevice->config != NULL)
231 connman_ipconfig_unref(ipdevice->config);
233 free_address_list(ipdevice);
234 g_free(ipdevice->gateway);
236 g_free(ipdevice->address);
237 g_free(ipdevice->ifname);
241 static GSList *driver_list = NULL;
243 static gint compare_priority(gconstpointer a, gconstpointer b)
245 const struct connman_ipconfig_driver *driver1 = a;
246 const struct connman_ipconfig_driver *driver2 = b;
248 return driver2->priority - driver1->priority;
252 * connman_ipconfig_driver_register:
253 * @driver: IP configuration driver
255 * Register a new IP configuration driver
257 * Returns: %0 on success
259 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
261 DBG("driver %p name %s", driver, driver->name);
263 driver_list = g_slist_insert_sorted(driver_list, driver,
270 * connman_ipconfig_driver_unregister:
271 * @driver: IP configuration driver
273 * Remove a previously registered IP configuration driver.
275 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
277 DBG("driver %p name %s", driver, driver->name);
279 driver_list = g_slist_remove(driver_list, driver);
282 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
286 DBG("ipconfig %p", ipdevice->config);
288 if (ipdevice->config == NULL)
291 switch (ipdevice->config->method) {
292 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
293 case CONNMAN_IPCONFIG_METHOD_OFF:
294 case CONNMAN_IPCONFIG_METHOD_FIXED:
295 case CONNMAN_IPCONFIG_METHOD_MANUAL:
297 case CONNMAN_IPCONFIG_METHOD_DHCP:
301 if (ipdevice->driver != NULL)
304 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
305 if (ipdevice->driver_config == NULL)
308 for (list = driver_list; list; list = list->next) {
309 struct connman_ipconfig_driver *driver = list->data;
311 if (driver->request(ipdevice->driver_config) == 0) {
312 ipdevice->driver = driver;
317 if (ipdevice->driver == NULL) {
318 connman_ipconfig_unref(ipdevice->driver_config);
319 ipdevice->driver_config = NULL;
323 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
325 DBG("ipconfig %p", ipdevice->config);
327 if (ipdevice->config == NULL)
330 if (ipdevice->driver == NULL)
333 ipdevice->driver->release(ipdevice->driver_config);
335 ipdevice->driver = NULL;
337 connman_ipconfig_unref(ipdevice->driver_config);
338 ipdevice->driver_config = NULL;
340 connman_inet_clear_address(ipdevice->index);
343 static void update_stats(struct connman_ipdevice *ipdevice,
344 struct rtnl_link_stats *stats)
346 if (stats->rx_packets == 0 && stats->tx_packets == 0)
349 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
350 stats->rx_packets, stats->rx_bytes);
351 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
352 stats->tx_packets, stats->tx_bytes);
354 if (ipdevice->config == NULL)
357 ipdevice->rx_bytes = stats->rx_bytes;
358 ipdevice->tx_bytes = stats->tx_bytes;
360 __connman_counter_notify(ipdevice->ifname,
361 ipdevice->rx_bytes, ipdevice->tx_bytes);
364 void __connman_ipconfig_newlink(int index, unsigned short type,
365 unsigned int flags, const char *address,
367 struct rtnl_link_stats *stats)
369 struct connman_ipdevice *ipdevice;
372 gboolean up = FALSE, down = FALSE;
373 gboolean lower_up = FALSE, lower_down = FALSE;
376 DBG("index %d", index);
378 if (type == ARPHRD_LOOPBACK)
381 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
382 if (ipdevice != NULL)
385 ifname = connman_inet_ifname(index);
387 if (__connman_element_device_isfiltered(ifname) == TRUE) {
388 connman_info("Ignoring interface %s (filtered)", ifname);
393 ipdevice = g_try_new0(struct connman_ipdevice, 1);
394 if (ipdevice == NULL) {
399 ipdevice->index = index;
400 ipdevice->ifname = ifname;
401 ipdevice->type = type;
403 ipdevice->address = g_strdup(address);
405 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
407 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
408 index, type, type2str(type));
413 update_stats(ipdevice, stats);
415 if (flags == ipdevice->flags)
418 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
425 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
426 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
427 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
428 (IFF_RUNNING | IFF_LOWER_UP))
430 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
434 ipdevice->flags = flags;
436 str = g_string_new(NULL);
441 g_string_append(str, "UP");
443 g_string_append(str, "DOWN");
445 if (flags & IFF_RUNNING)
446 g_string_append(str, ",RUNNING");
448 if (flags & IFF_LOWER_UP)
449 g_string_append(str, ",LOWER_UP");
451 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
454 g_string_free(str, TRUE);
456 for (list = g_list_first(ipconfig_list); list;
457 list = g_list_next(list)) {
458 struct connman_ipconfig *ipconfig = list->data;
460 if (index != ipconfig->index)
463 if (ipconfig->ops == NULL)
466 if (up == TRUE && ipconfig->ops->up)
467 ipconfig->ops->up(ipconfig);
468 if (lower_up == TRUE && ipconfig->ops->lower_up)
469 ipconfig->ops->lower_up(ipconfig);
471 if (lower_down == TRUE && ipconfig->ops->lower_down)
472 ipconfig->ops->lower_down(ipconfig);
473 if (down == TRUE && ipconfig->ops->down)
474 ipconfig->ops->down(ipconfig);
478 __connman_ipconfig_lower_up(ipdevice);
480 __connman_ipconfig_lower_down(ipdevice);
483 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
485 struct connman_ipdevice *ipdevice;
488 DBG("index %d", index);
490 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
491 if (ipdevice == NULL)
494 update_stats(ipdevice, stats);
496 for (list = g_list_first(ipconfig_list); list;
497 list = g_list_next(list)) {
498 struct connman_ipconfig *ipconfig = list->data;
500 if (index != ipconfig->index)
503 ipconfig->index = -1;
505 if (ipconfig->ops == NULL)
508 if (ipconfig->ops->lower_down)
509 ipconfig->ops->lower_down(ipconfig);
510 if (ipconfig->ops->down)
511 ipconfig->ops->down(ipconfig);
514 __connman_ipconfig_lower_down(ipdevice);
516 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
519 void __connman_ipconfig_newaddr(int index, const char *label,
520 unsigned char prefixlen, const char *address)
522 struct connman_ipdevice *ipdevice;
523 struct connman_ipaddress *ipaddress;
526 DBG("index %d", index);
528 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
529 if (ipdevice == NULL)
532 ipaddress = connman_ipaddress_alloc();
533 if (ipaddress == NULL)
536 ipaddress->prefixlen = prefixlen;
537 ipaddress->local = g_strdup(address);
539 ipdevice->address_list = g_slist_append(ipdevice->address_list,
542 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
543 address, prefixlen, label);
545 if (ipdevice->config != NULL)
546 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
548 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
551 if (g_slist_length(ipdevice->address_list) > 1)
554 for (list = g_list_first(ipconfig_list); list;
555 list = g_list_next(list)) {
556 struct connman_ipconfig *ipconfig = list->data;
558 if (index != ipconfig->index)
561 if (ipconfig->ops == NULL)
564 if (ipconfig->ops->ip_bound)
565 ipconfig->ops->ip_bound(ipconfig);
569 void __connman_ipconfig_deladdr(int index, const char *label,
570 unsigned char prefixlen, const char *address)
572 struct connman_ipdevice *ipdevice;
573 struct connman_ipaddress *ipaddress;
576 DBG("index %d", index);
578 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
579 if (ipdevice == NULL)
582 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
583 if (ipaddress == NULL)
586 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
589 connman_ipaddress_free(ipaddress);
591 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
592 address, prefixlen, label);
594 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
597 if (g_slist_length(ipdevice->address_list) > 0)
600 for (list = g_list_first(ipconfig_list); list;
601 list = g_list_next(list)) {
602 struct connman_ipconfig *ipconfig = list->data;
604 if (index != ipconfig->index)
607 if (ipconfig->ops == NULL)
610 if (ipconfig->ops->ip_release)
611 ipconfig->ops->ip_release(ipconfig);
615 void __connman_ipconfig_newroute(int index, unsigned char scope,
616 const char *dst, const char *gateway)
618 struct connman_ipdevice *ipdevice;
620 DBG("index %d", index);
622 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
623 if (ipdevice == NULL)
626 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
627 g_free(ipdevice->gateway);
628 ipdevice->gateway = g_strdup(gateway);
631 connman_info("%s {add} route %s gw %s scope %u <%s>",
632 ipdevice->ifname, dst, gateway,
633 scope, scope2str(scope));
636 void __connman_ipconfig_delroute(int index, unsigned char scope,
637 const char *dst, const char *gateway)
639 struct connman_ipdevice *ipdevice;
641 DBG("index %d", index);
643 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
644 if (ipdevice == NULL)
647 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
648 g_free(ipdevice->gateway);
649 ipdevice->gateway = NULL;
652 connman_info("%s {del} route %s gw %s scope %u <%s>",
653 ipdevice->ifname, dst, gateway,
654 scope, scope2str(scope));
657 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
662 keys = g_hash_table_get_keys(ipdevice_hash);
666 for (list = g_list_first(keys); list; list = g_list_next(list)) {
667 int index = GPOINTER_TO_INT(list->data);
669 function(index, user_data);
675 unsigned short __connman_ipconfig_get_type(int index)
677 struct connman_ipdevice *ipdevice;
679 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
680 if (ipdevice == NULL)
683 return ipdevice->type;
686 unsigned int __connman_ipconfig_get_flags(int index)
688 struct connman_ipdevice *ipdevice;
690 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
691 if (ipdevice == NULL)
694 return ipdevice->flags;
697 const char *__connman_ipconfig_get_gateway(int index)
699 struct connman_ipdevice *ipdevice;
701 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
702 if (ipdevice == NULL)
705 return ipdevice->gateway;
709 * connman_ipconfig_create:
711 * Allocate a new ipconfig structure.
713 * Returns: a newly-allocated #connman_ipconfig structure
715 struct connman_ipconfig *connman_ipconfig_create(int index)
717 struct connman_ipconfig *ipconfig;
719 DBG("index %d", index);
721 ipconfig = g_try_new0(struct connman_ipconfig, 1);
722 if (ipconfig == NULL)
725 ipconfig->refcount = 1;
727 ipconfig->index = index;
729 ipconfig->address = connman_ipaddress_alloc();
730 if (ipconfig->address == NULL) {
735 ipconfig->system = connman_ipaddress_alloc();
737 DBG("ipconfig %p", ipconfig);
743 * connman_ipconfig_clone:
745 * Clone an ipconfig structure and create new reference.
747 * Returns: a newly-allocated #connman_ipconfig structure
749 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
751 struct connman_ipconfig *ipconfig_clone;
753 DBG("ipconfig %p", ipconfig);
755 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
756 if (ipconfig_clone == NULL)
759 ipconfig_clone->refcount = 1;
761 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
763 ipconfig_clone->index = -1;
765 return ipconfig_clone;
769 * connman_ipconfig_ref:
770 * @ipconfig: ipconfig structure
772 * Increase reference counter of ipconfig
774 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
776 g_atomic_int_inc(&ipconfig->refcount);
782 * connman_ipconfig_unref:
783 * @ipconfig: ipconfig structure
785 * Decrease reference counter of ipconfig
787 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
789 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
790 __connman_ipconfig_disable(ipconfig);
792 connman_ipconfig_set_ops(ipconfig, NULL);
794 if (ipconfig->origin != NULL) {
795 connman_ipconfig_unref(ipconfig->origin);
796 ipconfig->origin = NULL;
799 connman_ipaddress_free(ipconfig->system);
800 connman_ipaddress_free(ipconfig->address);
806 * connman_ipconfig_get_data:
807 * @ipconfig: ipconfig structure
809 * Get private data pointer
811 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
813 return ipconfig->ops_data;
817 * connman_ipconfig_set_data:
818 * @ipconfig: ipconfig structure
819 * @data: data pointer
821 * Set private data pointer
823 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
825 ipconfig->ops_data = data;
829 * connman_ipconfig_get_index:
830 * @ipconfig: ipconfig structure
832 * Get interface index
834 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
836 if (ipconfig->origin != NULL)
837 return ipconfig->origin->index;
839 return ipconfig->index;
843 * connman_ipconfig_get_ifname:
844 * @ipconfig: ipconfig structure
848 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
850 struct connman_ipdevice *ipdevice;
852 if (ipconfig->index < 0)
855 ipdevice = g_hash_table_lookup(ipdevice_hash,
856 GINT_TO_POINTER(ipconfig->index));
857 if (ipdevice == NULL)
860 return ipdevice->ifname;
864 * connman_ipconfig_set_ops:
865 * @ipconfig: ipconfig structure
866 * @ops: operation callbacks
868 * Set the operation callbacks
870 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
871 const struct connman_ipconfig_ops *ops)
877 * connman_ipconfig_set_method:
878 * @ipconfig: ipconfig structure
879 * @method: configuration method
881 * Set the configuration method
883 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
884 enum connman_ipconfig_method method)
886 ipconfig->method = method;
891 enum connman_ipconfig_method __connman_ipconfig_get_method(
892 struct connman_ipconfig *ipconfig)
894 if (ipconfig == NULL)
895 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
897 return ipconfig->method;
901 * connman_ipconfig_bind:
902 * @ipconfig: ipconfig structure
903 * @ipaddress: ipaddress structure
905 * Bind IP address details to configuration
907 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
908 struct connman_ipaddress *ipaddress)
910 struct connman_ipconfig *origin;
912 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
914 connman_ipaddress_copy(origin->address, ipaddress);
916 connman_inet_set_address(origin->index, origin->address);
919 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
923 switch (ipconfig->method) {
924 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
925 case CONNMAN_IPCONFIG_METHOD_OFF:
926 case CONNMAN_IPCONFIG_METHOD_FIXED:
927 case CONNMAN_IPCONFIG_METHOD_DHCP:
929 case CONNMAN_IPCONFIG_METHOD_MANUAL:
930 return connman_inet_set_address(ipconfig->index,
937 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
941 switch (ipconfig->method) {
942 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
943 case CONNMAN_IPCONFIG_METHOD_OFF:
944 case CONNMAN_IPCONFIG_METHOD_FIXED:
945 case CONNMAN_IPCONFIG_METHOD_DHCP:
947 case CONNMAN_IPCONFIG_METHOD_MANUAL:
948 return connman_inet_clear_address(ipconfig->index);
955 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
957 struct connman_ipdevice *ipdevice;
959 DBG("ipconfig %p", ipconfig);
961 if (ipconfig == NULL || ipconfig->index < 0)
964 ipdevice = g_hash_table_lookup(ipdevice_hash,
965 GINT_TO_POINTER(ipconfig->index));
966 if (ipdevice == NULL)
969 if (ipdevice->config == ipconfig)
972 if (ipdevice->config != NULL) {
973 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
975 connman_ipaddress_clear(ipdevice->config->system);
977 connman_ipconfig_unref(ipdevice->config);
980 ipdevice->config = connman_ipconfig_ref(ipconfig);
982 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
987 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
989 struct connman_ipdevice *ipdevice;
991 DBG("ipconfig %p", ipconfig);
993 if (ipconfig == NULL || ipconfig->index < 0)
996 ipdevice = g_hash_table_lookup(ipdevice_hash,
997 GINT_TO_POINTER(ipconfig->index));
998 if (ipdevice == NULL)
1001 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1004 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1006 connman_ipaddress_clear(ipdevice->config->system);
1008 connman_ipconfig_unref(ipdevice->config);
1009 ipdevice->config = NULL;
1014 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1017 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1019 case CONNMAN_IPCONFIG_METHOD_OFF:
1021 case CONNMAN_IPCONFIG_METHOD_FIXED:
1023 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1025 case CONNMAN_IPCONFIG_METHOD_DHCP:
1032 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1034 if (g_strcmp0(method, "off") == 0)
1035 return CONNMAN_IPCONFIG_METHOD_OFF;
1036 else if (g_strcmp0(method, "fixed") == 0)
1037 return CONNMAN_IPCONFIG_METHOD_FIXED;
1038 else if (g_strcmp0(method, "manual") == 0)
1039 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1040 else if (g_strcmp0(method, "dhcp") == 0)
1041 return CONNMAN_IPCONFIG_METHOD_DHCP;
1043 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1046 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1047 DBusMessageIter *iter)
1051 str = __connman_ipconfig_method2string(ipconfig->method);
1055 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1057 if (ipconfig->system == NULL)
1060 if (ipconfig->system->local != NULL) {
1062 struct in_addr netmask;
1065 connman_dbus_dict_append_basic(iter, "Address",
1066 DBUS_TYPE_STRING, &ipconfig->system->local);
1068 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1069 netmask.s_addr = htonl(addr);
1070 mask = inet_ntoa(netmask);
1071 connman_dbus_dict_append_basic(iter, "Netmask",
1072 DBUS_TYPE_STRING, &mask);
1076 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1077 DBusMessageIter *iter)
1081 str = __connman_ipconfig_method2string(ipconfig->method);
1085 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1087 switch (ipconfig->method) {
1088 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1089 case CONNMAN_IPCONFIG_METHOD_OFF:
1090 case CONNMAN_IPCONFIG_METHOD_FIXED:
1091 case CONNMAN_IPCONFIG_METHOD_DHCP:
1093 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1097 if (ipconfig->address == NULL)
1100 if (ipconfig->address->local != NULL) {
1102 struct in_addr netmask;
1105 connman_dbus_dict_append_basic(iter, "Address",
1106 DBUS_TYPE_STRING, &ipconfig->address->local);
1108 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1109 netmask.s_addr = htonl(addr);
1110 mask = inet_ntoa(netmask);
1111 connman_dbus_dict_append_basic(iter, "Netmask",
1112 DBUS_TYPE_STRING, &mask);
1116 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1117 DBusMessageIter *array)
1119 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1120 const char *address = NULL, *netmask = NULL;
1121 DBusMessageIter dict;
1123 DBG("ipconfig %p", ipconfig);
1125 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1128 dbus_message_iter_recurse(array, &dict);
1130 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1131 DBusMessageIter entry;
1135 dbus_message_iter_recurse(&dict, &entry);
1137 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1140 dbus_message_iter_get_basic(&entry, &key);
1141 dbus_message_iter_next(&entry);
1143 type = dbus_message_iter_get_arg_type(&entry);
1145 if (g_str_equal(key, "Method") == TRUE) {
1148 if (type != DBUS_TYPE_STRING)
1151 dbus_message_iter_get_basic(&entry, &str);
1152 method = __connman_ipconfig_string2method(str);
1153 } else if (g_str_equal(key, "Address") == TRUE) {
1154 if (type != DBUS_TYPE_STRING)
1157 dbus_message_iter_get_basic(&entry, &address);
1158 } else if (g_str_equal(key, "Netmask") == TRUE) {
1159 if (type != DBUS_TYPE_STRING)
1162 dbus_message_iter_get_basic(&entry, &netmask);
1165 dbus_message_iter_next(&dict);
1168 DBG("method %d address %s netmask %s", method, address, netmask);
1171 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1172 case CONNMAN_IPCONFIG_METHOD_OFF:
1173 case CONNMAN_IPCONFIG_METHOD_FIXED:
1176 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1177 if (address == NULL)
1180 ipconfig->method = method;
1181 connman_ipaddress_set(ipconfig->address, address, netmask);
1184 case CONNMAN_IPCONFIG_METHOD_DHCP:
1185 if (ipconfig->method == method)
1188 ipconfig->method = method;
1195 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1196 DBusMessageIter *iter)
1198 const char *method = "direct";
1200 connman_dbus_dict_append_basic(iter, "Method",
1201 DBUS_TYPE_STRING, &method);
1204 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1205 DBusMessageIter *iter)
1207 struct connman_ipdevice *ipdevice;
1208 const char *method = "auto";
1210 connman_dbus_dict_append_basic(iter, "Method",
1211 DBUS_TYPE_STRING, &method);
1213 ipdevice = g_hash_table_lookup(ipdevice_hash,
1214 GINT_TO_POINTER(ipconfig->index));
1215 if (ipdevice == NULL)
1218 if (ipdevice->ifname != NULL)
1219 connman_dbus_dict_append_basic(iter, "Interface",
1220 DBUS_TYPE_STRING, &ipdevice->ifname);
1222 if (ipdevice->address != NULL)
1223 connman_dbus_dict_append_basic(iter, "Address",
1224 DBUS_TYPE_STRING, &ipdevice->address);
1226 if (ipdevice->mtu > 0)
1227 connman_dbus_dict_append_basic(iter, "MTU",
1228 DBUS_TYPE_UINT16, &ipdevice->mtu);
1231 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1232 GKeyFile *keyfile, const char *identifier, const char *prefix)
1234 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1239 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1240 GKeyFile *keyfile, const char *identifier, const char *prefix)
1242 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1247 int __connman_ipconfig_init(void)
1251 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1252 NULL, free_ipdevice);
1257 void __connman_ipconfig_cleanup(void)
1261 g_hash_table_destroy(ipdevice_hash);
1262 ipdevice_hash = NULL;