5 * Copyright (C) 2007-2009 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 (up == TRUE && ipconfig->ops->up)
439 ipconfig->ops->up(ipconfig);
440 if (lower_up == TRUE && ipconfig->ops->lower_up)
441 ipconfig->ops->lower_up(ipconfig);
443 if (lower_down == TRUE && ipconfig->ops->lower_down)
444 ipconfig->ops->lower_down(ipconfig);
445 if (down == TRUE && ipconfig->ops->down)
446 ipconfig->ops->down(ipconfig);
450 __connman_ipconfig_lower_up(ipdevice);
452 __connman_ipconfig_lower_down(ipdevice);
455 void __connman_ipconfig_dellink(int index)
457 struct connman_ipdevice *ipdevice;
460 DBG("index %d", index);
462 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
463 if (ipdevice == NULL)
466 for (list = g_list_first(ipconfig_list); list;
467 list = g_list_next(list)) {
468 struct connman_ipconfig *ipconfig = list->data;
470 if (index != ipconfig->index)
473 ipconfig->index = -1;
475 if (ipconfig->ops->lower_down)
476 ipconfig->ops->lower_down(ipconfig);
477 if (ipconfig->ops->down)
478 ipconfig->ops->down(ipconfig);
481 __connman_ipconfig_lower_down(ipdevice);
483 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
486 void __connman_ipconfig_newaddr(int index, const char *label,
487 unsigned char prefixlen, const char *address)
489 struct connman_ipdevice *ipdevice;
490 struct connman_ipaddress *ipaddress;
493 DBG("index %d", index);
495 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
496 if (ipdevice == NULL)
499 ipaddress = connman_ipaddress_alloc();
500 if (ipaddress == NULL)
503 ipaddress->prefixlen = prefixlen;
504 ipaddress->local = g_strdup(address);
506 ipdevice->address_list = g_slist_append(ipdevice->address_list,
509 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
510 address, prefixlen, label);
512 if (ipdevice->config != NULL)
513 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
515 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
518 if (g_slist_length(ipdevice->address_list) > 1)
521 for (list = g_list_first(ipconfig_list); list;
522 list = g_list_next(list)) {
523 struct connman_ipconfig *ipconfig = list->data;
525 if (index != ipconfig->index)
528 if (ipconfig->ops->ip_bound)
529 ipconfig->ops->ip_bound(ipconfig);
533 void __connman_ipconfig_deladdr(int index, const char *label,
534 unsigned char prefixlen, const char *address)
536 struct connman_ipdevice *ipdevice;
537 struct connman_ipaddress *ipaddress;
540 DBG("index %d", index);
542 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
543 if (ipdevice == NULL)
546 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
547 if (ipaddress == NULL)
550 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
553 connman_ipaddress_free(ipaddress);
555 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
556 address, prefixlen, label);
558 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
561 if (g_slist_length(ipdevice->address_list) > 0)
564 for (list = g_list_first(ipconfig_list); list;
565 list = g_list_next(list)) {
566 struct connman_ipconfig *ipconfig = list->data;
568 if (index != ipconfig->index)
571 if (ipconfig->ops->ip_release)
572 ipconfig->ops->ip_release(ipconfig);
576 void __connman_ipconfig_newroute(int index, unsigned char scope,
577 const char *dst, const char *gateway)
579 struct connman_ipdevice *ipdevice;
581 DBG("index %d", index);
583 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
584 if (ipdevice == NULL)
587 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
588 g_free(ipdevice->gateway);
589 ipdevice->gateway = g_strdup(gateway);
592 connman_info("%s {add} route %s gw %s scope %u <%s>",
593 ipdevice->ifname, dst, gateway,
594 scope, scope2str(scope));
597 void __connman_ipconfig_delroute(int index, unsigned char scope,
598 const char *dst, const char *gateway)
600 struct connman_ipdevice *ipdevice;
602 DBG("index %d", index);
604 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
605 if (ipdevice == NULL)
608 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
609 g_free(ipdevice->gateway);
610 ipdevice->gateway = NULL;
613 connman_info("%s {del} route %s gw %s scope %u <%s>",
614 ipdevice->ifname, dst, gateway,
615 scope, scope2str(scope));
618 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
623 keys = g_hash_table_get_keys(ipdevice_hash);
627 for (list = g_list_first(keys); list; list = g_list_next(list)) {
628 int index = GPOINTER_TO_INT(list->data);
630 function(index, user_data);
636 unsigned short __connman_ipconfig_get_type(int index)
638 struct connman_ipdevice *ipdevice;
640 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
641 if (ipdevice == NULL)
644 return ipdevice->type;
647 unsigned int __connman_ipconfig_get_flags(int index)
649 struct connman_ipdevice *ipdevice;
651 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
652 if (ipdevice == NULL)
655 return ipdevice->flags;
658 const char *__connman_ipconfig_get_gateway(int index)
660 struct connman_ipdevice *ipdevice;
662 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
663 if (ipdevice == NULL)
666 return ipdevice->gateway;
670 * connman_ipconfig_create:
672 * Allocate a new ipconfig structure.
674 * Returns: a newly-allocated #connman_ipconfig structure
676 struct connman_ipconfig *connman_ipconfig_create(int index)
678 struct connman_ipconfig *ipconfig;
680 DBG("index %d", index);
682 ipconfig = g_try_new0(struct connman_ipconfig, 1);
683 if (ipconfig == NULL)
686 ipconfig->refcount = 1;
688 ipconfig->index = index;
690 ipconfig->address = connman_ipaddress_alloc();
691 if (ipconfig->address == NULL) {
696 ipconfig->system = connman_ipaddress_alloc();
698 DBG("ipconfig %p", ipconfig);
704 * connman_ipconfig_clone:
706 * Clone an ipconfig structure and create new reference.
708 * Returns: a newly-allocated #connman_ipconfig structure
710 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
712 struct connman_ipconfig *ipconfig_clone;
714 DBG("ipconfig %p", ipconfig);
716 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
717 if (ipconfig_clone == NULL)
720 ipconfig_clone->refcount = 1;
722 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
724 ipconfig_clone->index = -1;
726 return ipconfig_clone;
730 * connman_ipconfig_ref:
731 * @ipconfig: ipconfig structure
733 * Increase reference counter of ipconfig
735 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
737 g_atomic_int_inc(&ipconfig->refcount);
743 * connman_ipconfig_unref:
744 * @ipconfig: ipconfig structure
746 * Decrease reference counter of ipconfig
748 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
750 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
751 connman_ipconfig_set_ops(ipconfig, NULL);
753 if (ipconfig->origin != NULL) {
754 connman_ipconfig_unref(ipconfig->origin);
755 ipconfig->origin = NULL;
758 connman_ipaddress_free(ipconfig->system);
759 connman_ipaddress_free(ipconfig->address);
760 g_free(ipconfig->eth);
766 * connman_ipconfig_get_data:
767 * @ipconfig: ipconfig structure
769 * Get private data pointer
771 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
773 return ipconfig->ops_data;
777 * connman_ipconfig_set_data:
778 * @ipconfig: ipconfig structure
779 * @data: data pointer
781 * Set private data pointer
783 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
785 ipconfig->ops_data = data;
789 * connman_ipconfig_get_index:
790 * @ipconfig: ipconfig structure
792 * Get interface index
794 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
796 if (ipconfig->origin != NULL)
797 return ipconfig->origin->index;
799 return ipconfig->index;
803 * connman_ipconfig_get_ifname:
804 * @ipconfig: ipconfig structure
808 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
810 struct connman_ipdevice *ipdevice;
812 if (ipconfig->index < 0)
815 ipdevice = g_hash_table_lookup(ipdevice_hash,
816 GINT_TO_POINTER(ipconfig->index));
817 if (ipdevice == NULL)
820 return ipdevice->ifname;
824 * connman_ipconfig_set_ops:
825 * @ipconfig: ipconfig structure
826 * @ops: operation callbacks
828 * Set the operation callbacks
830 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
831 const struct connman_ipconfig_ops *ops)
833 if (ipconfig->ops != NULL)
834 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
839 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
843 * connman_ipconfig_set_method:
844 * @ipconfig: ipconfig structure
845 * @method: configuration method
847 * Set the configuration method
849 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
850 enum connman_ipconfig_method method)
852 ipconfig->method = method;
858 * connman_ipconfig_bind:
859 * @ipconfig: ipconfig structure
860 * @ipaddress: ipaddress structure
862 * Bind IP address details to configuration
864 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
865 struct connman_ipaddress *ipaddress)
867 struct connman_ipconfig *origin;
869 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
871 connman_ipaddress_copy(origin->address, ipaddress);
873 connman_inet_set_address(origin->index, origin->address);
876 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
878 struct connman_ipdevice *ipdevice;
880 DBG("ipconfig %p", ipconfig);
882 if (ipconfig == NULL || ipconfig->index < 0)
885 ipdevice = g_hash_table_lookup(ipdevice_hash,
886 GINT_TO_POINTER(ipconfig->index));
887 if (ipdevice == NULL)
890 if (ipdevice->config != NULL) {
891 connman_ipaddress_clear(ipdevice->config->system);
893 connman_ipconfig_unref(ipdevice->config);
896 ipdevice->config = connman_ipconfig_ref(ipconfig);
901 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
903 struct connman_ipdevice *ipdevice;
905 DBG("ipconfig %p", ipconfig);
907 if (ipconfig == NULL || ipconfig->index < 0)
910 ipdevice = g_hash_table_lookup(ipdevice_hash,
911 GINT_TO_POINTER(ipconfig->index));
912 if (ipdevice == NULL)
915 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
918 connman_ipaddress_clear(ipdevice->config->system);
920 connman_ipconfig_unref(ipdevice->config);
921 ipdevice->config = NULL;
926 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
929 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
931 case CONNMAN_IPCONFIG_METHOD_OFF:
933 case CONNMAN_IPCONFIG_METHOD_FIXED:
935 case CONNMAN_IPCONFIG_METHOD_MANUAL:
937 case CONNMAN_IPCONFIG_METHOD_DHCP:
944 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
946 if (g_strcmp0(method, "off") == 0)
947 return CONNMAN_IPCONFIG_METHOD_OFF;
948 else if (g_strcmp0(method, "fixed") == 0)
949 return CONNMAN_IPCONFIG_METHOD_FIXED;
950 else if (g_strcmp0(method, "manual") == 0)
951 return CONNMAN_IPCONFIG_METHOD_MANUAL;
952 else if (g_strcmp0(method, "dhcp") == 0)
953 return CONNMAN_IPCONFIG_METHOD_DHCP;
955 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
958 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
959 DBusMessageIter *iter)
963 str = __connman_ipconfig_method2string(ipconfig->method);
967 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
969 if (ipconfig->system == NULL)
972 if (ipconfig->system->local != NULL) {
974 struct in_addr netmask;
977 connman_dbus_dict_append_basic(iter, "Address",
978 DBUS_TYPE_STRING, &ipconfig->system->local);
980 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
981 netmask.s_addr = htonl(addr);
982 mask = inet_ntoa(netmask);
983 connman_dbus_dict_append_basic(iter, "Netmask",
984 DBUS_TYPE_STRING, &mask);
988 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
989 DBusMessageIter *iter)
993 str = __connman_ipconfig_method2string(ipconfig->method);
997 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
999 switch (ipconfig->method) {
1000 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1001 case CONNMAN_IPCONFIG_METHOD_OFF:
1002 case CONNMAN_IPCONFIG_METHOD_FIXED:
1003 case CONNMAN_IPCONFIG_METHOD_DHCP:
1005 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1009 if (ipconfig->address == NULL)
1012 if (ipconfig->address->local != NULL) {
1014 struct in_addr netmask;
1017 connman_dbus_dict_append_basic(iter, "Address",
1018 DBUS_TYPE_STRING, &ipconfig->address->local);
1020 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1021 netmask.s_addr = htonl(addr);
1022 mask = inet_ntoa(netmask);
1023 connman_dbus_dict_append_basic(iter, "Netmask",
1024 DBUS_TYPE_STRING, &mask);
1028 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1029 DBusMessageIter *array)
1031 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1032 const char *address = NULL, *netmask = NULL;
1033 DBusMessageIter dict;
1035 DBG("ipconfig %p", ipconfig);
1037 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1040 dbus_message_iter_recurse(array, &dict);
1042 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1043 DBusMessageIter entry;
1047 dbus_message_iter_recurse(&dict, &entry);
1049 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1052 dbus_message_iter_get_basic(&entry, &key);
1053 dbus_message_iter_next(&entry);
1055 type = dbus_message_iter_get_arg_type(&entry);
1057 if (g_str_equal(key, "Method") == TRUE) {
1060 if (type != DBUS_TYPE_STRING)
1063 dbus_message_iter_get_basic(&entry, &str);
1064 method = __connman_ipconfig_string2method(str);
1065 } else if (g_str_equal(key, "Address") == TRUE) {
1066 if (type != DBUS_TYPE_STRING)
1069 dbus_message_iter_get_basic(&entry, &address);
1070 } else if (g_str_equal(key, "Netmask") == TRUE) {
1071 if (type != DBUS_TYPE_STRING)
1074 dbus_message_iter_get_basic(&entry, &netmask);
1077 dbus_message_iter_next(&dict);
1080 DBG("method %d address %s netmask %s", method, address, netmask);
1083 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1084 case CONNMAN_IPCONFIG_METHOD_OFF:
1085 case CONNMAN_IPCONFIG_METHOD_FIXED:
1088 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1089 if (address == NULL)
1092 ipconfig->method = method;
1093 connman_ipaddress_set(ipconfig->address, address, netmask);
1096 case CONNMAN_IPCONFIG_METHOD_DHCP:
1097 if (ipconfig->method == method)
1100 ipconfig->method = method;
1107 int __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1108 DBusMessageIter *iter)
1110 const char *method = "auto";
1112 connman_dbus_dict_append_basic(iter, "Method",
1113 DBUS_TYPE_STRING, &method);
1115 if (ipconfig->eth != NULL)
1116 connman_dbus_dict_append_basic(iter, "Address",
1117 DBUS_TYPE_STRING, &ipconfig->eth);
1119 if (ipconfig->mtu > 0)
1120 connman_dbus_dict_append_basic(iter, "MTU",
1121 DBUS_TYPE_UINT16, &ipconfig->mtu);
1126 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1127 GKeyFile *keyfile, const char *identifier, const char *prefix)
1129 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1134 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1135 GKeyFile *keyfile, const char *identifier, const char *prefix)
1137 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1142 int __connman_ipconfig_init(void)
1146 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1147 NULL, free_ipdevice);
1152 void __connman_ipconfig_cleanup(void)
1156 g_hash_table_destroy(ipdevice_hash);
1157 ipdevice_hash = NULL;