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
31 static GSList *network_list = NULL;
32 static GSList *driver_list = NULL;
34 struct connman_network {
36 enum connman_network_type type;
37 connman_bool_t available;
38 connman_bool_t connected;
39 connman_bool_t roaming;
40 connman_uint8_t strength;
41 connman_uint16_t frequency;
49 struct connman_network_driver *driver;
52 connman_bool_t connecting;
53 connman_bool_t associating;
55 struct connman_device *device;
61 unsigned short channel;
64 char *agent_passphrase;
69 char *client_cert_path;
70 char *private_key_path;
71 char *private_key_passphrase;
74 connman_bool_t use_wps;
84 static const char *type2string(enum connman_network_type type)
87 case CONNMAN_NETWORK_TYPE_UNKNOWN:
88 case CONNMAN_NETWORK_TYPE_VENDOR:
90 case CONNMAN_NETWORK_TYPE_ETHERNET:
92 case CONNMAN_NETWORK_TYPE_WIFI:
94 case CONNMAN_NETWORK_TYPE_WIMAX:
96 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
97 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
99 case CONNMAN_NETWORK_TYPE_CELLULAR:
106 connman_bool_t __connman_network_has_driver(struct connman_network *network)
108 if (network == NULL || network->driver == NULL)
114 static gboolean match_driver(struct connman_network *network,
115 struct connman_network_driver *driver)
117 if (network->type == driver->type ||
118 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
124 static int network_probe(struct connman_network *network)
127 struct connman_network_driver *driver = NULL;
129 DBG("network %p name %s", network, network->name);
131 if (network->driver != NULL)
134 for (list = driver_list; list; list = list->next) {
137 if (match_driver(network, driver) == FALSE)
140 DBG("driver %p name %s", driver, driver->name);
142 if (driver->probe(network) == 0)
151 if (network->group == NULL)
154 switch (network->type) {
155 case CONNMAN_NETWORK_TYPE_UNKNOWN:
156 case CONNMAN_NETWORK_TYPE_VENDOR:
158 case CONNMAN_NETWORK_TYPE_ETHERNET:
159 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
160 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
161 case CONNMAN_NETWORK_TYPE_CELLULAR:
162 case CONNMAN_NETWORK_TYPE_WIFI:
163 case CONNMAN_NETWORK_TYPE_WIMAX:
164 network->driver = driver;
165 if (__connman_service_create_from_network(network) == NULL) {
166 network->driver = NULL;
174 static void network_remove(struct connman_network *network)
176 DBG("network %p name %s", network, network->name);
178 if (network->driver == NULL)
181 connman_network_set_connected(network, FALSE);
183 switch (network->type) {
184 case CONNMAN_NETWORK_TYPE_UNKNOWN:
185 case CONNMAN_NETWORK_TYPE_VENDOR:
187 case CONNMAN_NETWORK_TYPE_ETHERNET:
188 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
189 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
190 case CONNMAN_NETWORK_TYPE_CELLULAR:
191 case CONNMAN_NETWORK_TYPE_WIFI:
192 case CONNMAN_NETWORK_TYPE_WIMAX:
193 if (network->group != NULL) {
194 __connman_service_remove_from_network(network);
196 g_free(network->group);
197 network->group = NULL;
202 if (network->driver->remove)
203 network->driver->remove(network);
205 network->driver = NULL;
208 static void network_change(struct connman_network *network)
210 DBG("network %p name %s", network, network->name);
212 if (network->connected == FALSE)
215 connman_device_set_disconnected(network->device, TRUE);
217 if (network->driver && network->driver->disconnect) {
218 network->driver->disconnect(network);
222 network->connected = FALSE;
225 static void probe_driver(struct connman_network_driver *driver)
229 DBG("driver %p name %s", driver, driver->name);
231 for (list = network_list; list != NULL; list = list->next) {
232 struct connman_network *network = list->data;
234 if (network->driver != NULL)
237 if (driver->type != network->type)
240 if (driver->probe(network) < 0)
243 network->driver = driver;
247 static void remove_driver(struct connman_network_driver *driver)
251 DBG("driver %p name %s", driver, driver->name);
253 for (list = network_list; list != NULL; list = list->next) {
254 struct connman_network *network = list->data;
256 if (network->driver == driver)
257 network_remove(network);
261 static gint compare_priority(gconstpointer a, gconstpointer b)
263 const struct connman_network_driver *driver1 = a;
264 const struct connman_network_driver *driver2 = b;
266 return driver2->priority - driver1->priority;
270 * connman_network_driver_register:
271 * @driver: network driver definition
273 * Register a new network driver
275 * Returns: %0 on success
277 int connman_network_driver_register(struct connman_network_driver *driver)
281 DBG("driver %p name %s", driver, driver->name);
283 for (list = driver_list; list; list = list->next) {
284 struct connman_network_driver *tmp = list->data;
286 if (tmp->type == driver->type)
291 driver_list = g_slist_insert_sorted(driver_list, driver,
294 probe_driver(driver);
300 * connman_network_driver_unregister:
301 * @driver: network driver definition
303 * Remove a previously registered network driver
305 void connman_network_driver_unregister(struct connman_network_driver *driver)
307 DBG("driver %p name %s", driver, driver->name);
309 driver_list = g_slist_remove(driver_list, driver);
311 remove_driver(driver);
314 static void network_destruct(struct connman_network *network)
316 DBG("network %p name %s", network, network->name);
318 g_free(network->wifi.ssid);
319 g_free(network->wifi.mode);
320 g_free(network->wifi.security);
321 g_free(network->wifi.passphrase);
322 g_free(network->wifi.agent_passphrase);
323 g_free(network->wifi.eap);
324 g_free(network->wifi.identity);
325 g_free(network->wifi.agent_identity);
326 g_free(network->wifi.ca_cert_path);
327 g_free(network->wifi.client_cert_path);
328 g_free(network->wifi.private_key_path);
329 g_free(network->wifi.private_key_passphrase);
330 g_free(network->wifi.phase2_auth);
331 g_free(network->wifi.pin_wps);
333 g_free(network->path);
334 g_free(network->group);
335 g_free(network->node);
336 g_free(network->name);
337 g_free(network->identifier);
339 network->device = NULL;
345 * connman_network_create:
346 * @identifier: network identifier (for example an unqiue name)
348 * Allocate a new network and assign the #identifier to it.
350 * Returns: a newly-allocated #connman_network structure
352 struct connman_network *connman_network_create(const char *identifier,
353 enum connman_network_type type)
355 struct connman_network *network;
358 DBG("identifier %s type %d", identifier, type);
360 network = g_try_new0(struct connman_network, 1);
364 DBG("network %p", network);
366 network->refcount = 1;
368 ident = g_strdup(identifier);
375 network->type = type;
376 network->identifier = ident;
378 network_list = g_slist_append(network_list, network);
384 * connman_network_ref:
385 * @network: network structure
387 * Increase reference counter of network
389 struct connman_network *connman_network_ref(struct connman_network *network)
391 DBG("network %p name %s refcount %d", network, network->name,
392 g_atomic_int_get(&network->refcount) + 1);
394 g_atomic_int_inc(&network->refcount);
400 * connman_network_unref:
401 * @network: network structure
403 * Decrease reference counter of network
405 void connman_network_unref(struct connman_network *network)
407 DBG("network %p name %s refcount %d", network, network->name,
408 g_atomic_int_get(&network->refcount) - 1);
410 if (g_atomic_int_dec_and_test(&network->refcount) == FALSE)
413 network_list = g_slist_remove(network_list, network);
415 network_destruct(network);
418 const char *__connman_network_get_type(struct connman_network *network)
420 return type2string(network->type);
424 * connman_network_get_type:
425 * @network: network structure
427 * Get type of network
429 enum connman_network_type connman_network_get_type(struct connman_network *network)
431 return network->type;
435 * connman_network_get_identifier:
436 * @network: network structure
438 * Get identifier of network
440 const char *connman_network_get_identifier(struct connman_network *network)
442 return network->identifier;
446 * connman_network_set_index:
447 * @network: network structure
448 * @index: index number
450 * Set index number of network
452 void connman_network_set_index(struct connman_network *network, int index)
454 struct connman_service *service;
455 struct connman_ipconfig *ipconfig;
457 service = __connman_service_lookup_from_network(network);
461 ipconfig = __connman_service_get_ip4config(service);
463 DBG("index %d service %p ip4config %p", network->index,
466 if (network->index < 0 && ipconfig == NULL) {
468 ipconfig = __connman_service_get_ip4config(service);
469 if (ipconfig == NULL)
471 * This is needed for plugins that havent set their
472 * ipconfig layer yet, due to not being able to get
473 * a network index prior to creating a service.
475 __connman_service_create_ip4config(service, index);
477 __connman_ipconfig_set_index(ipconfig, index);
480 /* If index changed, the index of ipconfig must be reset. */
481 if (ipconfig == NULL)
484 __connman_ipconfig_set_index(ipconfig, index);
488 network->index = index;
492 * connman_network_get_index:
493 * @network: network structure
495 * Get index number of network
497 int connman_network_get_index(struct connman_network *network)
499 return network->index;
503 * connman_network_set_group:
504 * @network: network structure
507 * Set group name for automatic clustering
509 void connman_network_set_group(struct connman_network *network,
512 switch (network->type) {
513 case CONNMAN_NETWORK_TYPE_UNKNOWN:
514 case CONNMAN_NETWORK_TYPE_VENDOR:
516 case CONNMAN_NETWORK_TYPE_ETHERNET:
517 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
518 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
519 case CONNMAN_NETWORK_TYPE_CELLULAR:
520 case CONNMAN_NETWORK_TYPE_WIFI:
521 case CONNMAN_NETWORK_TYPE_WIMAX:
525 if (g_strcmp0(network->group, group) == 0) {
527 __connman_service_update_from_network(network);
531 if (network->group != NULL) {
532 __connman_service_remove_from_network(network);
534 g_free(network->group);
537 network->group = g_strdup(group);
539 if (network->group != NULL)
540 network_probe(network);
544 * connman_network_get_group:
545 * @network: network structure
547 * Get group name for automatic clustering
549 const char *connman_network_get_group(struct connman_network *network)
551 return network->group;
554 const char *__connman_network_get_ident(struct connman_network *network)
556 if (network->device == NULL)
559 return connman_device_get_ident(network->device);
562 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
564 switch (network->type) {
565 case CONNMAN_NETWORK_TYPE_UNKNOWN:
566 case CONNMAN_NETWORK_TYPE_VENDOR:
567 case CONNMAN_NETWORK_TYPE_ETHERNET:
568 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
569 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
570 case CONNMAN_NETWORK_TYPE_CELLULAR:
571 case CONNMAN_NETWORK_TYPE_WIMAX:
573 case CONNMAN_NETWORK_TYPE_WIFI:
574 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
576 if (network->strength > 0 && network->strength < 20)
584 connman_bool_t connman_network_get_connecting(struct connman_network *network)
586 return network->connecting;
590 * connman_network_set_available:
591 * @network: network structure
592 * @available: availability state
594 * Change availability state of network (in range)
596 int connman_network_set_available(struct connman_network *network,
597 connman_bool_t available)
599 DBG("network %p available %d", network, available);
601 if (network->available == available)
604 network->available = available;
610 * connman_network_get_available:
611 * @network: network structure
613 * Get network available setting
615 connman_bool_t connman_network_get_available(struct connman_network *network)
617 return network->available;
621 * connman_network_set_associating:
622 * @network: network structure
623 * @associating: associating state
625 * Change associating state of network
627 int connman_network_set_associating(struct connman_network *network,
628 connman_bool_t associating)
630 DBG("network %p associating %d", network, associating);
632 if (network->associating == associating)
635 network->associating = associating;
637 if (associating == TRUE) {
638 struct connman_service *service;
640 service = __connman_service_lookup_from_network(network);
641 __connman_service_ipconfig_indicate_state(service,
642 CONNMAN_SERVICE_STATE_ASSOCIATION,
643 CONNMAN_IPCONFIG_TYPE_IPV4);
644 __connman_service_ipconfig_indicate_state(service,
645 CONNMAN_SERVICE_STATE_ASSOCIATION,
646 CONNMAN_IPCONFIG_TYPE_IPV6);
652 static void set_associate_error(struct connman_network *network)
654 struct connman_service *service;
656 if (network->associating == FALSE)
659 network->associating = FALSE;
661 service = __connman_service_lookup_from_network(network);
663 __connman_service_ipconfig_indicate_state(service,
664 CONNMAN_SERVICE_STATE_FAILURE,
665 CONNMAN_IPCONFIG_TYPE_IPV4);
668 static void set_configure_error(struct connman_network *network)
670 struct connman_service *service;
672 network->connecting = FALSE;
674 service = __connman_service_lookup_from_network(network);
676 __connman_service_ipconfig_indicate_state(service,
677 CONNMAN_SERVICE_STATE_FAILURE,
678 CONNMAN_IPCONFIG_TYPE_IPV4);
681 static void set_invalid_key_error(struct connman_network *network)
683 struct connman_service *service;
685 service = __connman_service_lookup_from_network(network);
687 __connman_service_indicate_error(service,
688 CONNMAN_SERVICE_ERROR_INVALID_KEY);
691 static void set_connect_error(struct connman_network *network)
693 struct connman_service *service;
695 service = __connman_service_lookup_from_network(network);
697 __connman_service_indicate_error(service,
698 CONNMAN_SERVICE_ERROR_CONNECT_FAILED);
701 void connman_network_set_ipv4_method(struct connman_network *network,
702 enum connman_ipconfig_method method)
704 struct connman_service *service;
705 struct connman_ipconfig *ipconfig;
707 service = __connman_service_lookup_from_network(network);
711 ipconfig = __connman_service_get_ip4config(service);
712 if (ipconfig == NULL)
715 connman_ipconfig_set_method(ipconfig, method);
718 void connman_network_set_ipv6_method(struct connman_network *network,
719 enum connman_ipconfig_method method)
721 struct connman_service *service;
722 struct connman_ipconfig *ipconfig;
724 service = __connman_service_lookup_from_network(network);
728 ipconfig = __connman_service_get_ip6config(service);
729 if (ipconfig == NULL)
732 connman_ipconfig_set_method(ipconfig, method);
735 void connman_network_set_error(struct connman_network *network,
736 enum connman_network_error error)
738 DBG("nework %p, error %d", network, error);
740 network->connecting = FALSE;
743 case CONNMAN_NETWORK_ERROR_UNKNOWN:
745 case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
746 set_associate_error(network);
748 case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
749 set_configure_error(network);
751 case CONNMAN_NETWORK_ERROR_INVALID_KEY:
752 set_invalid_key_error(network);
754 case CONNMAN_NETWORK_ERROR_CONNECT_FAIL:
755 set_connect_error(network);
759 network_change(network);
762 void connman_network_clear_error(struct connman_network *network)
764 struct connman_service *service;
766 DBG("network %p", network);
771 if (network->connecting == TRUE || network->associating == TRUE)
774 service = __connman_service_lookup_from_network(network);
775 __connman_service_clear_error(service);
778 static void set_configuration(struct connman_network *network)
780 struct connman_service *service;
782 DBG("network %p", network);
784 __connman_device_set_network(network->device, network);
786 connman_device_set_disconnected(network->device, FALSE);
788 service = __connman_service_lookup_from_network(network);
789 __connman_service_ipconfig_indicate_state(service,
790 CONNMAN_SERVICE_STATE_CONFIGURATION,
791 CONNMAN_IPCONFIG_TYPE_IPV4);
794 static void dhcp_success(struct connman_network *network)
796 struct connman_service *service;
797 struct connman_ipconfig *ipconfig_ipv4;
800 service = __connman_service_lookup_from_network(network);
804 connman_network_set_associating(network, FALSE);
806 network->connecting = FALSE;
808 ipconfig_ipv4 = __connman_service_get_ip4config(service);
809 err = __connman_ipconfig_address_add(ipconfig_ipv4);
813 err = __connman_ipconfig_gateway_add(ipconfig_ipv4);
820 connman_network_set_error(network,
821 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
824 static void dhcp_failure(struct connman_network *network)
826 struct connman_service *service;
828 service = __connman_service_lookup_from_network(network);
832 __connman_service_ipconfig_indicate_state(service,
833 CONNMAN_SERVICE_STATE_IDLE,
834 CONNMAN_IPCONFIG_TYPE_IPV4);
837 static void dhcp_callback(struct connman_network *network,
838 connman_bool_t success)
840 DBG("success %d", success);
843 dhcp_success(network);
845 dhcp_failure(network);
848 static int set_connected_fixed(struct connman_network *network)
850 struct connman_service *service;
851 struct connman_ipconfig *ipconfig_ipv4;
856 service = __connman_service_lookup_from_network(network);
858 ipconfig_ipv4 = __connman_service_get_ip4config(service);
860 set_configuration(network);
862 network->connecting = FALSE;
864 connman_network_set_associating(network, FALSE);
866 err = __connman_ipconfig_address_add(ipconfig_ipv4);
870 err = __connman_ipconfig_gateway_add(ipconfig_ipv4);
877 connman_network_set_error(network,
878 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
883 static void set_connected_manual(struct connman_network *network)
885 struct connman_service *service;
886 struct connman_ipconfig *ipconfig;
889 DBG("network %p", network);
891 service = __connman_service_lookup_from_network(network);
893 ipconfig = __connman_service_get_ip4config(service);
895 if (__connman_ipconfig_get_local(ipconfig) == NULL)
896 __connman_service_read_ip4config(service);
898 set_configuration(network);
900 err = __connman_ipconfig_address_add(ipconfig);
904 err = __connman_ipconfig_gateway_add(ipconfig);
908 network->connecting = FALSE;
910 connman_network_set_associating(network, FALSE);
915 connman_network_set_error(network,
916 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
920 static int set_connected_dhcp(struct connman_network *network)
924 DBG("network %p", network);
926 set_configuration(network);
928 err = __connman_dhcp_start(network, dhcp_callback);
930 connman_error("Can not request DHCP lease");
937 static int manual_ipv6_set(struct connman_network *network,
938 struct connman_ipconfig *ipconfig_ipv6)
940 struct connman_service *service;
943 DBG("network %p ipv6 %p", network, ipconfig_ipv6);
945 service = __connman_service_lookup_from_network(network);
949 if (__connman_ipconfig_get_local(ipconfig_ipv6) == NULL)
950 __connman_service_read_ip6config(service);
952 err = __connman_ipconfig_address_add(ipconfig_ipv6);
954 connman_network_set_error(network,
955 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
959 err = __connman_ipconfig_gateway_add(ipconfig_ipv6);
963 __connman_connection_gateway_activate(service,
964 CONNMAN_IPCONFIG_TYPE_IPV6);
966 __connman_device_set_network(network->device, network);
968 connman_device_set_disconnected(network->device, FALSE);
970 network->connecting = FALSE;
975 static void autoconf_ipv6_set(struct connman_network *network)
977 DBG("network %p", network);
979 __connman_device_set_network(network->device, network);
981 connman_device_set_disconnected(network->device, FALSE);
983 /* XXX: Append IPv6 nameservers here */
985 network->connecting = FALSE;
988 static gboolean set_connected(gpointer user_data)
990 struct connman_network *network = user_data;
991 struct connman_service *service;
992 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
993 enum connman_ipconfig_method ipv4_method, ipv6_method;
995 service = __connman_service_lookup_from_network(network);
997 ipconfig_ipv4 = __connman_service_get_ip4config(service);
998 ipconfig_ipv6 = __connman_service_get_ip6config(service);
1000 DBG("service %p ipv4 %p ipv6 %p", service, ipconfig_ipv4,
1003 ipv4_method = __connman_ipconfig_get_method(ipconfig_ipv4);
1004 ipv6_method = __connman_ipconfig_get_method(ipconfig_ipv6);
1006 DBG("method ipv4 %d ipv6 %d", ipv4_method, ipv6_method);
1007 DBG("network connected %d", network->connected);
1009 if (network->connected == TRUE) {
1012 switch (ipv6_method) {
1013 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1014 case CONNMAN_IPCONFIG_METHOD_OFF:
1016 case CONNMAN_IPCONFIG_METHOD_AUTO:
1017 autoconf_ipv6_set(network);
1019 case CONNMAN_IPCONFIG_METHOD_FIXED:
1020 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1021 ret = manual_ipv6_set(network, ipconfig_ipv6);
1023 connman_network_set_error(network,
1024 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1028 case CONNMAN_IPCONFIG_METHOD_DHCP:
1032 switch (ipv4_method) {
1033 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1034 case CONNMAN_IPCONFIG_METHOD_OFF:
1035 case CONNMAN_IPCONFIG_METHOD_AUTO:
1037 case CONNMAN_IPCONFIG_METHOD_FIXED:
1038 if (set_connected_fixed(network) < 0) {
1039 connman_network_set_error(network,
1040 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1044 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1045 set_connected_manual(network);
1047 case CONNMAN_IPCONFIG_METHOD_DHCP:
1048 if (set_connected_dhcp(network) < 0) {
1049 connman_network_set_error(network,
1050 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1056 enum connman_service_state state;
1058 __connman_device_set_network(network->device, NULL);
1060 switch (ipv4_method) {
1061 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1062 case CONNMAN_IPCONFIG_METHOD_OFF:
1063 case CONNMAN_IPCONFIG_METHOD_AUTO:
1064 case CONNMAN_IPCONFIG_METHOD_FIXED:
1065 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1067 case CONNMAN_IPCONFIG_METHOD_DHCP:
1068 __connman_dhcp_stop(network);
1073 * We only set the disconnect state if we were not in idle
1074 * or in failure. It does not make sense to go to disconnect
1075 * state if we were not connected.
1077 state = __connman_service_ipconfig_get_state(service,
1078 CONNMAN_IPCONFIG_TYPE_IPV4);
1079 if (state != CONNMAN_SERVICE_STATE_IDLE &&
1080 state != CONNMAN_SERVICE_STATE_FAILURE)
1081 __connman_service_ipconfig_indicate_state(service,
1082 CONNMAN_SERVICE_STATE_DISCONNECT,
1083 CONNMAN_IPCONFIG_TYPE_IPV4);
1085 state = __connman_service_ipconfig_get_state(service,
1086 CONNMAN_IPCONFIG_TYPE_IPV6);
1087 if (state != CONNMAN_SERVICE_STATE_IDLE &&
1088 state != CONNMAN_SERVICE_STATE_FAILURE)
1089 __connman_service_ipconfig_indicate_state(service,
1090 CONNMAN_SERVICE_STATE_DISCONNECT,
1091 CONNMAN_IPCONFIG_TYPE_IPV6);
1093 __connman_connection_gateway_remove(service,
1094 CONNMAN_IPCONFIG_TYPE_ALL);
1096 __connman_ipconfig_address_unset(ipconfig_ipv4);
1097 __connman_ipconfig_address_unset(ipconfig_ipv6);
1100 * Special handling for IPv6 autoconfigured address.
1101 * The simplest way to remove autoconfigured routes is to
1102 * disable IPv6 temporarily so that kernel will do the cleanup
1105 if (ipv6_method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1106 __connman_ipconfig_disable_ipv6(ipconfig_ipv6);
1107 __connman_ipconfig_enable_ipv6(ipconfig_ipv6);
1110 __connman_service_ipconfig_indicate_state(service,
1111 CONNMAN_SERVICE_STATE_IDLE,
1112 CONNMAN_IPCONFIG_TYPE_IPV4);
1114 __connman_service_ipconfig_indicate_state(service,
1115 CONNMAN_SERVICE_STATE_IDLE,
1116 CONNMAN_IPCONFIG_TYPE_IPV6);
1119 network->connecting = FALSE;
1121 connman_network_set_associating(network, FALSE);
1127 * connman_network_set_connected:
1128 * @network: network structure
1129 * @connected: connected state
1131 * Change connected state of network
1133 int connman_network_set_connected(struct connman_network *network,
1134 connman_bool_t connected)
1136 DBG("network %p connected %d", network, connected);
1138 if ((network->connecting == TRUE || network->associating == TRUE) &&
1139 connected == FALSE) {
1140 connman_network_set_error(network,
1141 CONNMAN_NETWORK_ERROR_CONNECT_FAIL);
1142 __connman_network_disconnect(network);
1145 if (network->connected == connected)
1148 network->connected = connected;
1150 set_connected(network);
1156 * connman_network_get_connected:
1157 * @network: network structure
1159 * Get network connection status
1161 connman_bool_t connman_network_get_connected(struct connman_network *network)
1163 return network->connected;
1167 * connman_network_get_associating:
1168 * @network: network structure
1170 * Get network associating status
1172 connman_bool_t connman_network_get_associating(struct connman_network *network)
1174 return network->associating;
1178 * __connman_network_connect:
1179 * @network: network structure
1183 int __connman_network_connect(struct connman_network *network)
1187 DBG("network %p", network);
1189 if (network->connected == TRUE)
1192 if (network->connecting == TRUE || network->associating == TRUE)
1195 if (network->driver == NULL)
1198 if (network->driver->connect == NULL)
1201 if (network->device == NULL)
1204 network->connecting = TRUE;
1206 __connman_device_disconnect(network->device);
1208 err = network->driver->connect(network);
1210 if (err == -EINPROGRESS)
1211 connman_network_set_associating(network, TRUE);
1213 network->connecting = FALSE;
1219 network->connected = TRUE;
1220 set_connected(network);
1226 * __connman_network_disconnect:
1227 * @network: network structure
1229 * Disconnect network
1231 int __connman_network_disconnect(struct connman_network *network)
1235 DBG("network %p", network);
1237 if (network->connected == FALSE && network->connecting == FALSE &&
1238 network->associating == FALSE)
1241 if (network->driver == NULL)
1244 if (network->driver->disconnect == NULL)
1247 network->connecting = FALSE;
1249 err = network->driver->disconnect(network);
1251 connman_network_set_connected(network, FALSE);
1252 set_connected(network);
1258 static int manual_ipv4_set(struct connman_network *network,
1259 struct connman_ipconfig *ipconfig)
1261 struct connman_service *service;
1264 service = __connman_service_lookup_from_network(network);
1265 if (service == NULL)
1268 err = __connman_ipconfig_address_add(ipconfig);
1270 connman_network_set_error(network,
1271 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1275 return __connman_ipconfig_gateway_add(ipconfig);
1278 int __connman_network_clear_ipconfig(struct connman_network *network,
1279 struct connman_ipconfig *ipconfig)
1281 struct connman_service *service;
1282 enum connman_ipconfig_method method;
1283 enum connman_ipconfig_type type;
1285 service = __connman_service_lookup_from_network(network);
1286 if (service == NULL)
1289 method = __connman_ipconfig_get_method(ipconfig);
1290 type = __connman_ipconfig_get_config_type(ipconfig);
1293 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1294 case CONNMAN_IPCONFIG_METHOD_OFF:
1295 case CONNMAN_IPCONFIG_METHOD_FIXED:
1296 case CONNMAN_IPCONFIG_METHOD_AUTO:
1298 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1299 __connman_ipconfig_address_remove(ipconfig);
1301 case CONNMAN_IPCONFIG_METHOD_DHCP:
1302 __connman_dhcp_stop(network);
1306 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1307 __connman_service_ipconfig_indicate_state(service,
1308 CONNMAN_SERVICE_STATE_CONFIGURATION,
1309 CONNMAN_IPCONFIG_TYPE_IPV6);
1310 else if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1311 __connman_service_ipconfig_indicate_state(service,
1312 CONNMAN_SERVICE_STATE_CONFIGURATION,
1313 CONNMAN_IPCONFIG_TYPE_IPV4);
1318 int __connman_network_set_ipconfig(struct connman_network *network,
1319 struct connman_ipconfig *ipconfig_ipv4,
1320 struct connman_ipconfig *ipconfig_ipv6)
1322 enum connman_ipconfig_method method;
1325 if (network == NULL)
1328 if (ipconfig_ipv6) {
1329 method = __connman_ipconfig_get_method(ipconfig_ipv6);
1332 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1333 case CONNMAN_IPCONFIG_METHOD_OFF:
1335 case CONNMAN_IPCONFIG_METHOD_AUTO:
1336 autoconf_ipv6_set(network);
1338 case CONNMAN_IPCONFIG_METHOD_FIXED:
1339 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1340 ret = manual_ipv6_set(network, ipconfig_ipv6);
1342 connman_network_set_error(network,
1343 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1347 case CONNMAN_IPCONFIG_METHOD_DHCP:
1352 if (ipconfig_ipv4) {
1353 method = __connman_ipconfig_get_method(ipconfig_ipv4);
1356 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1357 case CONNMAN_IPCONFIG_METHOD_OFF:
1358 case CONNMAN_IPCONFIG_METHOD_FIXED:
1359 case CONNMAN_IPCONFIG_METHOD_AUTO:
1361 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1362 return manual_ipv4_set(network, ipconfig_ipv4);
1363 case CONNMAN_IPCONFIG_METHOD_DHCP:
1364 return __connman_dhcp_start(network, dhcp_callback);
1371 int connman_network_set_ipaddress(struct connman_network *network,
1372 struct connman_ipaddress *ipaddress)
1374 struct connman_service *service;
1375 struct connman_ipconfig *ipconfig = NULL;
1377 DBG("network %p", network);
1379 service = __connman_service_lookup_from_network(network);
1380 if (service == NULL)
1383 ipconfig = __connman_service_get_ipconfig(service, ipaddress->family);
1384 if (ipconfig == NULL)
1387 __connman_ipconfig_set_local(ipconfig, ipaddress->local);
1388 __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
1389 __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1390 __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1391 __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1396 int connman_network_set_nameservers(struct connman_network *network,
1397 const char *nameservers)
1399 struct connman_service *service;
1400 char **nameservers_array = NULL;
1403 DBG("network %p nameservers %s", network, nameservers);
1405 service = __connman_service_lookup_from_network(network);
1406 if (service == NULL)
1409 __connman_service_nameserver_clear(service);
1411 if (nameservers != NULL)
1412 nameservers_array = g_strsplit(nameservers, " ", 0);
1414 for (i = 0; nameservers_array[i] != NULL; i++) {
1415 __connman_service_nameserver_append(service,
1416 nameservers_array[i]);
1419 g_strfreev(nameservers_array);
1424 int connman_network_set_domain(struct connman_network *network,
1427 struct connman_service *service;
1429 DBG("network %p domain %s", network, domain);
1431 service = __connman_service_lookup_from_network(network);
1432 if (service == NULL)
1435 __connman_service_set_domainname(service, domain);
1441 * connman_network_set_name:
1442 * @network: network structure
1445 * Set display name value for network
1447 int connman_network_set_name(struct connman_network *network,
1450 DBG("network %p name %s", network, name);
1452 g_free(network->name);
1453 network->name = g_strdup(name);
1459 * connman_network_set_strength:
1460 * @network: network structure
1461 * @strength: strength value
1463 * Set signal strength value for network
1466 int connman_network_set_strength(struct connman_network *network,
1467 connman_uint8_t strength)
1469 DBG("network %p strengh %d", network, strength);
1471 network->strength = strength;
1476 connman_uint8_t connman_network_get_strength(struct connman_network *network)
1478 return network->strength;
1481 int connman_network_set_frequency(struct connman_network *network,
1482 connman_uint16_t frequency)
1484 DBG("network %p frequency %d", network, frequency);
1486 network->frequency = frequency;
1491 connman_uint16_t connman_network_get_frequency(struct connman_network *network)
1493 return network->frequency;
1496 int connman_network_set_wifi_channel(struct connman_network *network,
1497 connman_uint16_t channel)
1499 DBG("network %p wifi channel %d", network, channel);
1501 network->wifi.channel = channel;
1506 connman_uint16_t connman_network_get_wifi_channel(struct connman_network *network)
1508 return network->wifi.channel;
1512 * connman_network_set_roaming:
1513 * @network: network structure
1514 * @roaming: roaming state
1516 * Set roaming state for network
1518 int connman_network_set_roaming(struct connman_network *network,
1519 connman_bool_t roaming)
1521 DBG("network %p roaming %d", network, roaming);
1523 network->roaming = roaming;
1529 * connman_network_set_string:
1530 * @network: network structure
1531 * @key: unique identifier
1532 * @value: string value
1534 * Set string value for specific key
1536 int connman_network_set_string(struct connman_network *network,
1537 const char *key, const char *value)
1539 DBG("network %p key %s value %s", network, key, value);
1541 if (g_strcmp0(key, "Name") == 0)
1542 return connman_network_set_name(network, value);
1544 if (g_str_equal(key, "Path") == TRUE) {
1545 g_free(network->path);
1546 network->path = g_strdup(value);
1547 } else if (g_str_equal(key, "Node") == TRUE) {
1548 g_free(network->node);
1549 network->node = g_strdup(value);
1550 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1551 g_free(network->wifi.mode);
1552 network->wifi.mode = g_strdup(value);
1553 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1554 g_free(network->wifi.security);
1555 network->wifi.security = g_strdup(value);
1556 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1557 g_free(network->wifi.passphrase);
1558 network->wifi.passphrase = g_strdup(value);
1559 } else if (g_str_equal(key, "WiFi.AgentPassphrase") == TRUE) {
1560 g_free(network->wifi.agent_passphrase);
1561 network->wifi.agent_passphrase = g_strdup(value);
1562 } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1563 g_free(network->wifi.eap);
1564 network->wifi.eap = g_strdup(value);
1565 } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1566 g_free(network->wifi.identity);
1567 network->wifi.identity = g_strdup(value);
1568 } else if (g_str_equal(key, "WiFi.AgentIdentity") == TRUE) {
1569 g_free(network->wifi.agent_identity);
1570 network->wifi.agent_identity = g_strdup(value);
1571 } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1572 g_free(network->wifi.ca_cert_path);
1573 network->wifi.ca_cert_path = g_strdup(value);
1574 } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1575 g_free(network->wifi.client_cert_path);
1576 network->wifi.client_cert_path = g_strdup(value);
1577 } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1578 g_free(network->wifi.private_key_path);
1579 network->wifi.private_key_path = g_strdup(value);
1580 } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1581 g_free(network->wifi.private_key_passphrase);
1582 network->wifi.private_key_passphrase = g_strdup(value);
1583 } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1584 g_free(network->wifi.phase2_auth);
1585 network->wifi.phase2_auth = g_strdup(value);
1586 } else if (g_str_equal(key, "WiFi.PinWPS") == TRUE) {
1587 g_free(network->wifi.pin_wps);
1588 network->wifi.pin_wps = g_strdup(value);
1597 * connman_network_get_string:
1598 * @network: network structure
1599 * @key: unique identifier
1601 * Get string value for specific key
1603 const char *connman_network_get_string(struct connman_network *network,
1606 DBG("network %p key %s", network, key);
1608 if (g_str_equal(key, "Path") == TRUE)
1609 return network->path;
1610 else if (g_str_equal(key, "Name") == TRUE)
1611 return network->name;
1612 else if (g_str_equal(key, "Node") == TRUE)
1613 return network->node;
1614 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1615 return network->wifi.mode;
1616 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1617 return network->wifi.security;
1618 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1619 return network->wifi.passphrase;
1620 else if (g_str_equal(key, "WiFi.AgentPassphrase") == TRUE)
1621 return network->wifi.agent_passphrase;
1622 else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1623 return network->wifi.eap;
1624 else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1625 return network->wifi.identity;
1626 else if (g_str_equal(key, "WiFi.AgentIdentity") == TRUE)
1627 return network->wifi.agent_identity;
1628 else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1629 return network->wifi.ca_cert_path;
1630 else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1631 return network->wifi.client_cert_path;
1632 else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1633 return network->wifi.private_key_path;
1634 else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1635 return network->wifi.private_key_passphrase;
1636 else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1637 return network->wifi.phase2_auth;
1638 else if (g_str_equal(key, "WiFi.PinWPS") == TRUE)
1639 return network->wifi.pin_wps;
1645 * connman_network_set_bool:
1646 * @network: network structure
1647 * @key: unique identifier
1648 * @value: boolean value
1650 * Set boolean value for specific key
1652 int connman_network_set_bool(struct connman_network *network,
1653 const char *key, connman_bool_t value)
1655 DBG("network %p key %s value %d", network, key, value);
1657 if (g_strcmp0(key, "Roaming") == 0)
1658 return connman_network_set_roaming(network, value);
1659 else if (g_strcmp0(key, "WiFi.WPS") == 0)
1660 network->wifi.wps = value;
1661 else if (g_strcmp0(key, "WiFi.UseWPS") == 0)
1662 network->wifi.use_wps = value;
1668 * connman_network_get_bool:
1669 * @network: network structure
1670 * @key: unique identifier
1672 * Get boolean value for specific key
1674 connman_bool_t connman_network_get_bool(struct connman_network *network,
1677 DBG("network %p key %s", network, key);
1679 if (g_str_equal(key, "Roaming") == TRUE)
1680 return network->roaming;
1681 else if (g_str_equal(key, "WiFi.WPS") == TRUE)
1682 return network->wifi.wps;
1683 else if (g_str_equal(key, "WiFi.UseWPS") == TRUE)
1684 return network->wifi.use_wps;
1690 * connman_network_set_blob:
1691 * @network: network structure
1692 * @key: unique identifier
1696 * Set binary blob value for specific key
1698 int connman_network_set_blob(struct connman_network *network,
1699 const char *key, const void *data, unsigned int size)
1701 DBG("network %p key %s size %d", network, key, size);
1703 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1704 g_free(network->wifi.ssid);
1705 network->wifi.ssid = g_try_malloc(size);
1706 if (network->wifi.ssid != NULL) {
1707 memcpy(network->wifi.ssid, data, size);
1708 network->wifi.ssid_len = size;
1710 network->wifi.ssid_len = 0;
1719 * connman_network_get_blob:
1720 * @network: network structure
1721 * @key: unique identifier
1722 * @size: pointer to blob size
1724 * Get binary blob value for specific key
1726 const void *connman_network_get_blob(struct connman_network *network,
1727 const char *key, unsigned int *size)
1729 DBG("network %p key %s", network, key);
1731 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1733 *size = network->wifi.ssid_len;
1734 return network->wifi.ssid;
1740 void __connman_network_set_device(struct connman_network *network,
1741 struct connman_device *device)
1743 if (network->device == device)
1746 if (network->device != NULL)
1747 network_remove(network);
1749 network->device = device;
1751 if (network->device != NULL)
1752 network_probe(network);
1756 * connman_network_get_device:
1757 * @network: network structure
1759 * Get parent device of network
1761 struct connman_device *connman_network_get_device(struct connman_network *network)
1763 return network->device;
1767 * connman_network_get_data:
1768 * @network: network structure
1770 * Get private network data pointer
1772 void *connman_network_get_data(struct connman_network *network)
1774 return network->driver_data;
1778 * connman_network_set_data:
1779 * @network: network structure
1780 * @data: data pointer
1782 * Set private network data pointer
1784 void connman_network_set_data(struct connman_network *network, void *data)
1786 network->driver_data = data;
1789 void connman_network_update(struct connman_network *network)
1791 switch (network->type) {
1792 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1793 case CONNMAN_NETWORK_TYPE_VENDOR:
1795 case CONNMAN_NETWORK_TYPE_ETHERNET:
1796 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1797 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1798 case CONNMAN_NETWORK_TYPE_CELLULAR:
1799 case CONNMAN_NETWORK_TYPE_WIFI:
1800 case CONNMAN_NETWORK_TYPE_WIMAX:
1804 if (network->group != NULL)
1805 __connman_service_update_from_network(network);
1808 int __connman_network_init(void)
1815 void __connman_network_cleanup(void)