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>
30 #define IFF_LOWER_UP 0x10000
37 struct connman_ipconfig {
41 struct connman_ipconfig *origin;
43 const struct connman_ipconfig_ops *ops;
46 enum connman_ipconfig_method method;
47 struct connman_ipaddress *address;
48 struct connman_ipaddress *system;
54 struct connman_ipdevice {
63 struct connman_ipconfig *config;
65 struct connman_ipconfig_driver *driver;
66 struct connman_ipconfig *driver_config;
69 static GHashTable *ipdevice_hash = NULL;
70 static GList *ipconfig_list = NULL;
72 struct connman_ipaddress *connman_ipaddress_alloc(void)
74 struct connman_ipaddress *ipaddress;
76 ipaddress = g_try_new0(struct connman_ipaddress, 1);
77 if (ipaddress == NULL)
83 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
85 if (ipaddress == NULL)
88 g_free(ipaddress->broadcast);
89 g_free(ipaddress->peer);
90 g_free(ipaddress->local);
94 static unsigned char netmask2prefixlen(const char *netmask)
96 unsigned char bits = 0;
97 in_addr_t mask = inet_network(netmask);
98 in_addr_t host = ~mask;
100 /* a valid netmask must be 2^n - 1 */
101 if ((host & (host + 1)) != 0)
104 for (; mask; mask <<= 1)
110 void connman_ipaddress_set(struct connman_ipaddress *ipaddress,
111 const char *address, const char *netmask)
113 if (ipaddress == NULL)
117 ipaddress->prefixlen = netmask2prefixlen(netmask);
119 ipaddress->prefixlen = 32;
121 g_free(ipaddress->local);
122 ipaddress->local = g_strdup(address);
125 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
127 if (ipaddress == NULL)
130 ipaddress->prefixlen = 0;
132 g_free(ipaddress->local);
133 ipaddress->local = NULL;
135 g_free(ipaddress->peer);
136 ipaddress->peer = NULL;
138 g_free(ipaddress->broadcast);
139 ipaddress->broadcast = NULL;
142 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
143 struct connman_ipaddress *source)
145 if (ipaddress == NULL || source == NULL)
148 ipaddress->prefixlen = source->prefixlen;
150 g_free(ipaddress->local);
151 ipaddress->local = g_strdup(source->local);
153 g_free(ipaddress->peer);
154 ipaddress->peer = g_strdup(source->peer);
156 g_free(ipaddress->broadcast);
157 ipaddress->broadcast = g_strdup(source->broadcast);
160 static void free_address_list(struct connman_ipdevice *ipdevice)
164 for (list = ipdevice->address_list; list; list = list->next) {
165 struct connman_ipaddress *ipaddress = list->data;
167 connman_ipaddress_free(ipaddress);
171 g_slist_free(ipdevice->address_list);
172 ipdevice->address_list = NULL;
175 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
176 unsigned char prefixlen, const char *local)
180 for (list = ipdevice->address_list; list; list = list->next) {
181 struct connman_ipaddress *ipaddress = list->data;
183 if (g_strcmp0(ipaddress->local, local) == 0 &&
184 ipaddress->prefixlen == prefixlen)
191 static const char *type2str(unsigned short type)
196 case ARPHRD_LOOPBACK:
209 static const char *scope2str(unsigned char scope)
221 static void free_ipdevice(gpointer data)
223 struct connman_ipdevice *ipdevice = data;
225 connman_info("%s {remove} index %d", ipdevice->ifname,
228 if (ipdevice->config != NULL)
229 connman_ipconfig_unref(ipdevice->config);
231 free_address_list(ipdevice);
232 g_free(ipdevice->gateway);
234 g_free(ipdevice->ifname);
238 static GSList *driver_list = NULL;
240 static gint compare_priority(gconstpointer a, gconstpointer b)
242 const struct connman_ipconfig_driver *driver1 = a;
243 const struct connman_ipconfig_driver *driver2 = b;
245 return driver2->priority - driver1->priority;
249 * connman_ipconfig_driver_register:
250 * @driver: IP configuration driver
252 * Register a new IP configuration driver
254 * Returns: %0 on success
256 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
258 DBG("driver %p name %s", driver, driver->name);
260 driver_list = g_slist_insert_sorted(driver_list, driver,
267 * connman_ipconfig_driver_unregister:
268 * @driver: IP configuration driver
270 * Remove a previously registered IP configuration driver.
272 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
274 DBG("driver %p name %s", driver, driver->name);
276 driver_list = g_slist_remove(driver_list, driver);
279 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
283 DBG("ipconfig %p", ipdevice->config);
285 if (ipdevice->config == NULL)
288 switch (ipdevice->config->method) {
289 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
290 case CONNMAN_IPCONFIG_METHOD_OFF:
291 case CONNMAN_IPCONFIG_METHOD_FIXED:
292 case CONNMAN_IPCONFIG_METHOD_MANUAL:
294 case CONNMAN_IPCONFIG_METHOD_DHCP:
298 if (ipdevice->driver != NULL)
301 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
302 if (ipdevice->driver_config == NULL)
305 for (list = driver_list; list; list = list->next) {
306 struct connman_ipconfig_driver *driver = list->data;
308 if (driver->request(ipdevice->driver_config) == 0) {
309 ipdevice->driver = driver;
314 if (ipdevice->driver == NULL) {
315 connman_ipconfig_unref(ipdevice->driver_config);
316 ipdevice->driver_config = NULL;
320 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
322 DBG("ipconfig %p", ipdevice->config);
324 if (ipdevice->config == NULL)
327 if (ipdevice->driver == NULL)
330 ipdevice->driver->release(ipdevice->driver_config);
332 ipdevice->driver = NULL;
334 connman_ipconfig_unref(ipdevice->driver_config);
335 ipdevice->driver_config = NULL;
337 connman_inet_clear_address(ipdevice->index);
340 void __connman_ipconfig_newlink(int index, unsigned short type,
341 unsigned int flags, const char *address,
344 struct connman_ipdevice *ipdevice;
347 gboolean up = FALSE, down = FALSE;
348 gboolean lower_up = FALSE, lower_down = FALSE;
351 DBG("index %d", index);
353 if (type == ARPHRD_LOOPBACK)
356 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
357 if (ipdevice != NULL)
360 ifname = connman_inet_ifname(index);
362 if (__connman_element_device_isfiltered(ifname) == TRUE) {
363 connman_info("Ignoring interface %s (filtered)", ifname);
368 ipdevice = g_try_new0(struct connman_ipdevice, 1);
369 if (ipdevice == NULL) {
374 ipdevice->index = index;
375 ipdevice->ifname = ifname;
376 ipdevice->type = type;
378 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
380 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
381 index, type, type2str(type));
384 if (ipdevice->config != NULL) {
385 g_free(ipdevice->config->eth);
386 ipdevice->config->eth = g_strdup(address);
387 ipdevice->config->mtu = mtu;
390 if (flags == ipdevice->flags)
393 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
400 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
401 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
402 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
403 (IFF_RUNNING | IFF_LOWER_UP))
405 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
409 ipdevice->flags = flags;
411 str = g_string_new(NULL);
416 g_string_append(str, "UP");
418 g_string_append(str, "DOWN");
420 if (flags & IFF_RUNNING)
421 g_string_append(str, ",RUNNING");
423 if (flags & IFF_LOWER_UP)
424 g_string_append(str, ",LOWER_UP");
426 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
429 g_string_free(str, TRUE);
431 for (list = g_list_first(ipconfig_list); list;
432 list = g_list_next(list)) {
433 struct connman_ipconfig *ipconfig = list->data;
435 if (index != ipconfig->index)
438 if (ipconfig->ops == NULL)
441 if (up == TRUE && ipconfig->ops->up)
442 ipconfig->ops->up(ipconfig);
443 if (lower_up == TRUE && ipconfig->ops->lower_up)
444 ipconfig->ops->lower_up(ipconfig);
446 if (lower_down == TRUE && ipconfig->ops->lower_down)
447 ipconfig->ops->lower_down(ipconfig);
448 if (down == TRUE && ipconfig->ops->down)
449 ipconfig->ops->down(ipconfig);
453 __connman_ipconfig_lower_up(ipdevice);
455 __connman_ipconfig_lower_down(ipdevice);
458 void __connman_ipconfig_dellink(int index)
460 struct connman_ipdevice *ipdevice;
463 DBG("index %d", index);
465 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
466 if (ipdevice == NULL)
469 for (list = g_list_first(ipconfig_list); list;
470 list = g_list_next(list)) {
471 struct connman_ipconfig *ipconfig = list->data;
473 if (index != ipconfig->index)
476 ipconfig->index = -1;
478 if (ipconfig->ops == NULL)
481 if (ipconfig->ops->lower_down)
482 ipconfig->ops->lower_down(ipconfig);
483 if (ipconfig->ops->down)
484 ipconfig->ops->down(ipconfig);
487 __connman_ipconfig_lower_down(ipdevice);
489 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
492 void __connman_ipconfig_newaddr(int index, const char *label,
493 unsigned char prefixlen, const char *address)
495 struct connman_ipdevice *ipdevice;
496 struct connman_ipaddress *ipaddress;
499 DBG("index %d", index);
501 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
502 if (ipdevice == NULL)
505 ipaddress = connman_ipaddress_alloc();
506 if (ipaddress == NULL)
509 ipaddress->prefixlen = prefixlen;
510 ipaddress->local = g_strdup(address);
512 ipdevice->address_list = g_slist_append(ipdevice->address_list,
515 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
516 address, prefixlen, label);
518 if (ipdevice->config != NULL)
519 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
521 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
524 if (g_slist_length(ipdevice->address_list) > 1)
527 for (list = g_list_first(ipconfig_list); list;
528 list = g_list_next(list)) {
529 struct connman_ipconfig *ipconfig = list->data;
531 if (index != ipconfig->index)
534 if (ipconfig->ops == NULL)
537 if (ipconfig->ops->ip_bound)
538 ipconfig->ops->ip_bound(ipconfig);
542 void __connman_ipconfig_deladdr(int index, const char *label,
543 unsigned char prefixlen, const char *address)
545 struct connman_ipdevice *ipdevice;
546 struct connman_ipaddress *ipaddress;
549 DBG("index %d", index);
551 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
552 if (ipdevice == NULL)
555 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
556 if (ipaddress == NULL)
559 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
562 connman_ipaddress_free(ipaddress);
564 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
565 address, prefixlen, label);
567 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
570 if (g_slist_length(ipdevice->address_list) > 0)
573 for (list = g_list_first(ipconfig_list); list;
574 list = g_list_next(list)) {
575 struct connman_ipconfig *ipconfig = list->data;
577 if (index != ipconfig->index)
580 if (ipconfig->ops == NULL)
583 if (ipconfig->ops->ip_release)
584 ipconfig->ops->ip_release(ipconfig);
588 void __connman_ipconfig_newroute(int index, unsigned char scope,
589 const char *dst, const char *gateway)
591 struct connman_ipdevice *ipdevice;
593 DBG("index %d", index);
595 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
596 if (ipdevice == NULL)
599 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
600 g_free(ipdevice->gateway);
601 ipdevice->gateway = g_strdup(gateway);
604 connman_info("%s {add} route %s gw %s scope %u <%s>",
605 ipdevice->ifname, dst, gateway,
606 scope, scope2str(scope));
609 void __connman_ipconfig_delroute(int index, unsigned char scope,
610 const char *dst, const char *gateway)
612 struct connman_ipdevice *ipdevice;
614 DBG("index %d", index);
616 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
617 if (ipdevice == NULL)
620 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
621 g_free(ipdevice->gateway);
622 ipdevice->gateway = NULL;
625 connman_info("%s {del} route %s gw %s scope %u <%s>",
626 ipdevice->ifname, dst, gateway,
627 scope, scope2str(scope));
630 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
635 keys = g_hash_table_get_keys(ipdevice_hash);
639 for (list = g_list_first(keys); list; list = g_list_next(list)) {
640 int index = GPOINTER_TO_INT(list->data);
642 function(index, user_data);
648 unsigned short __connman_ipconfig_get_type(int index)
650 struct connman_ipdevice *ipdevice;
652 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
653 if (ipdevice == NULL)
656 return ipdevice->type;
659 unsigned int __connman_ipconfig_get_flags(int index)
661 struct connman_ipdevice *ipdevice;
663 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
664 if (ipdevice == NULL)
667 return ipdevice->flags;
670 const char *__connman_ipconfig_get_gateway(int index)
672 struct connman_ipdevice *ipdevice;
674 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
675 if (ipdevice == NULL)
678 return ipdevice->gateway;
682 * connman_ipconfig_create:
684 * Allocate a new ipconfig structure.
686 * Returns: a newly-allocated #connman_ipconfig structure
688 struct connman_ipconfig *connman_ipconfig_create(int index)
690 struct connman_ipconfig *ipconfig;
692 DBG("index %d", index);
694 ipconfig = g_try_new0(struct connman_ipconfig, 1);
695 if (ipconfig == NULL)
698 ipconfig->refcount = 1;
700 ipconfig->index = index;
702 ipconfig->address = connman_ipaddress_alloc();
703 if (ipconfig->address == NULL) {
708 ipconfig->system = connman_ipaddress_alloc();
710 DBG("ipconfig %p", ipconfig);
716 * connman_ipconfig_clone:
718 * Clone an ipconfig structure and create new reference.
720 * Returns: a newly-allocated #connman_ipconfig structure
722 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
724 struct connman_ipconfig *ipconfig_clone;
726 DBG("ipconfig %p", ipconfig);
728 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
729 if (ipconfig_clone == NULL)
732 ipconfig_clone->refcount = 1;
734 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
736 ipconfig_clone->index = -1;
738 return ipconfig_clone;
742 * connman_ipconfig_ref:
743 * @ipconfig: ipconfig structure
745 * Increase reference counter of ipconfig
747 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
749 g_atomic_int_inc(&ipconfig->refcount);
755 * connman_ipconfig_unref:
756 * @ipconfig: ipconfig structure
758 * Decrease reference counter of ipconfig
760 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
762 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
763 __connman_ipconfig_disable(ipconfig);
765 connman_ipconfig_set_ops(ipconfig, NULL);
767 if (ipconfig->origin != NULL) {
768 connman_ipconfig_unref(ipconfig->origin);
769 ipconfig->origin = NULL;
772 connman_ipaddress_free(ipconfig->system);
773 connman_ipaddress_free(ipconfig->address);
774 g_free(ipconfig->eth);
780 * connman_ipconfig_get_data:
781 * @ipconfig: ipconfig structure
783 * Get private data pointer
785 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
787 return ipconfig->ops_data;
791 * connman_ipconfig_set_data:
792 * @ipconfig: ipconfig structure
793 * @data: data pointer
795 * Set private data pointer
797 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
799 ipconfig->ops_data = data;
803 * connman_ipconfig_get_index:
804 * @ipconfig: ipconfig structure
806 * Get interface index
808 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
810 if (ipconfig->origin != NULL)
811 return ipconfig->origin->index;
813 return ipconfig->index;
817 * connman_ipconfig_get_ifname:
818 * @ipconfig: ipconfig structure
822 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
824 struct connman_ipdevice *ipdevice;
826 if (ipconfig->index < 0)
829 ipdevice = g_hash_table_lookup(ipdevice_hash,
830 GINT_TO_POINTER(ipconfig->index));
831 if (ipdevice == NULL)
834 return ipdevice->ifname;
838 * connman_ipconfig_set_ops:
839 * @ipconfig: ipconfig structure
840 * @ops: operation callbacks
842 * Set the operation callbacks
844 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
845 const struct connman_ipconfig_ops *ops)
851 * connman_ipconfig_set_method:
852 * @ipconfig: ipconfig structure
853 * @method: configuration method
855 * Set the configuration method
857 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
858 enum connman_ipconfig_method method)
860 ipconfig->method = method;
866 * connman_ipconfig_bind:
867 * @ipconfig: ipconfig structure
868 * @ipaddress: ipaddress structure
870 * Bind IP address details to configuration
872 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
873 struct connman_ipaddress *ipaddress)
875 struct connman_ipconfig *origin;
877 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
879 connman_ipaddress_copy(origin->address, ipaddress);
881 connman_inet_set_address(origin->index, origin->address);
884 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
886 struct connman_ipdevice *ipdevice;
888 DBG("ipconfig %p", ipconfig);
890 if (ipconfig == NULL || ipconfig->index < 0)
893 ipdevice = g_hash_table_lookup(ipdevice_hash,
894 GINT_TO_POINTER(ipconfig->index));
895 if (ipdevice == NULL)
898 if (ipdevice->config == ipconfig)
901 if (ipdevice->config != NULL) {
902 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
904 connman_ipaddress_clear(ipdevice->config->system);
906 connman_ipconfig_unref(ipdevice->config);
909 ipdevice->config = connman_ipconfig_ref(ipconfig);
911 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
916 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
918 struct connman_ipdevice *ipdevice;
920 DBG("ipconfig %p", ipconfig);
922 if (ipconfig == NULL || ipconfig->index < 0)
925 ipdevice = g_hash_table_lookup(ipdevice_hash,
926 GINT_TO_POINTER(ipconfig->index));
927 if (ipdevice == NULL)
930 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
933 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
935 connman_ipaddress_clear(ipdevice->config->system);
937 connman_ipconfig_unref(ipdevice->config);
938 ipdevice->config = NULL;
943 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
946 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
948 case CONNMAN_IPCONFIG_METHOD_OFF:
950 case CONNMAN_IPCONFIG_METHOD_FIXED:
952 case CONNMAN_IPCONFIG_METHOD_MANUAL:
954 case CONNMAN_IPCONFIG_METHOD_DHCP:
961 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
963 if (g_strcmp0(method, "off") == 0)
964 return CONNMAN_IPCONFIG_METHOD_OFF;
965 else if (g_strcmp0(method, "fixed") == 0)
966 return CONNMAN_IPCONFIG_METHOD_FIXED;
967 else if (g_strcmp0(method, "manual") == 0)
968 return CONNMAN_IPCONFIG_METHOD_MANUAL;
969 else if (g_strcmp0(method, "dhcp") == 0)
970 return CONNMAN_IPCONFIG_METHOD_DHCP;
972 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
975 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
976 DBusMessageIter *iter)
980 str = __connman_ipconfig_method2string(ipconfig->method);
984 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
986 if (ipconfig->system == NULL)
989 if (ipconfig->system->local != NULL) {
991 struct in_addr netmask;
994 connman_dbus_dict_append_basic(iter, "Address",
995 DBUS_TYPE_STRING, &ipconfig->system->local);
997 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
998 netmask.s_addr = htonl(addr);
999 mask = inet_ntoa(netmask);
1000 connman_dbus_dict_append_basic(iter, "Netmask",
1001 DBUS_TYPE_STRING, &mask);
1005 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1006 DBusMessageIter *iter)
1010 str = __connman_ipconfig_method2string(ipconfig->method);
1014 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1016 switch (ipconfig->method) {
1017 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1018 case CONNMAN_IPCONFIG_METHOD_OFF:
1019 case CONNMAN_IPCONFIG_METHOD_FIXED:
1020 case CONNMAN_IPCONFIG_METHOD_DHCP:
1022 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1026 if (ipconfig->address == NULL)
1029 if (ipconfig->address->local != NULL) {
1031 struct in_addr netmask;
1034 connman_dbus_dict_append_basic(iter, "Address",
1035 DBUS_TYPE_STRING, &ipconfig->address->local);
1037 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1038 netmask.s_addr = htonl(addr);
1039 mask = inet_ntoa(netmask);
1040 connman_dbus_dict_append_basic(iter, "Netmask",
1041 DBUS_TYPE_STRING, &mask);
1045 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1046 DBusMessageIter *array)
1048 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1049 const char *address = NULL, *netmask = NULL;
1050 DBusMessageIter dict;
1052 DBG("ipconfig %p", ipconfig);
1054 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1057 dbus_message_iter_recurse(array, &dict);
1059 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1060 DBusMessageIter entry;
1064 dbus_message_iter_recurse(&dict, &entry);
1066 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1069 dbus_message_iter_get_basic(&entry, &key);
1070 dbus_message_iter_next(&entry);
1072 type = dbus_message_iter_get_arg_type(&entry);
1074 if (g_str_equal(key, "Method") == TRUE) {
1077 if (type != DBUS_TYPE_STRING)
1080 dbus_message_iter_get_basic(&entry, &str);
1081 method = __connman_ipconfig_string2method(str);
1082 } else if (g_str_equal(key, "Address") == TRUE) {
1083 if (type != DBUS_TYPE_STRING)
1086 dbus_message_iter_get_basic(&entry, &address);
1087 } else if (g_str_equal(key, "Netmask") == TRUE) {
1088 if (type != DBUS_TYPE_STRING)
1091 dbus_message_iter_get_basic(&entry, &netmask);
1094 dbus_message_iter_next(&dict);
1097 DBG("method %d address %s netmask %s", method, address, netmask);
1100 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1101 case CONNMAN_IPCONFIG_METHOD_OFF:
1102 case CONNMAN_IPCONFIG_METHOD_FIXED:
1105 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1106 if (address == NULL)
1109 ipconfig->method = method;
1110 connman_ipaddress_set(ipconfig->address, address, netmask);
1113 case CONNMAN_IPCONFIG_METHOD_DHCP:
1114 if (ipconfig->method == method)
1117 ipconfig->method = method;
1124 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1125 DBusMessageIter *iter)
1127 const char *method = "direct";
1129 connman_dbus_dict_append_basic(iter, "Method",
1130 DBUS_TYPE_STRING, &method);
1133 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1134 DBusMessageIter *iter)
1136 struct connman_ipdevice *ipdevice;
1137 const char *method = "auto";
1139 connman_dbus_dict_append_basic(iter, "Method",
1140 DBUS_TYPE_STRING, &method);
1142 if (ipconfig->eth != NULL)
1143 connman_dbus_dict_append_basic(iter, "Address",
1144 DBUS_TYPE_STRING, &ipconfig->eth);
1146 if (ipconfig->mtu > 0)
1147 connman_dbus_dict_append_basic(iter, "MTU",
1148 DBUS_TYPE_UINT16, &ipconfig->mtu);
1150 ipdevice = g_hash_table_lookup(ipdevice_hash,
1151 GINT_TO_POINTER(ipconfig->index));
1152 if (ipdevice == NULL)
1155 if (ipdevice->ifname != NULL)
1156 connman_dbus_dict_append_basic(iter, "Interface",
1157 DBUS_TYPE_STRING, &ipdevice->ifname);
1160 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1161 GKeyFile *keyfile, const char *identifier, const char *prefix)
1163 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1168 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1169 GKeyFile *keyfile, const char *identifier, const char *prefix)
1171 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1176 int __connman_ipconfig_init(void)
1180 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1181 NULL, free_ipdevice);
1186 void __connman_ipconfig_cleanup(void)
1190 g_hash_table_destroy(ipdevice_hash);
1191 ipdevice_hash = NULL;