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 switch (network->type) {
182 case CONNMAN_NETWORK_TYPE_UNKNOWN:
183 case CONNMAN_NETWORK_TYPE_VENDOR:
185 case CONNMAN_NETWORK_TYPE_ETHERNET:
186 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
187 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
188 case CONNMAN_NETWORK_TYPE_CELLULAR:
189 case CONNMAN_NETWORK_TYPE_WIFI:
190 case CONNMAN_NETWORK_TYPE_WIMAX:
191 if (network->group != NULL) {
192 __connman_service_remove_from_network(network);
194 g_free(network->group);
195 network->group = NULL;
200 if (network->driver->remove)
201 network->driver->remove(network);
203 network->driver = NULL;
206 static void network_change(struct connman_network *network)
208 DBG("network %p name %s", network, network->name);
210 if (network->connected == FALSE)
213 connman_device_set_disconnected(network->device, TRUE);
215 if (network->driver && network->driver->disconnect) {
216 network->driver->disconnect(network);
220 network->connected = FALSE;
223 static void probe_driver(struct connman_network_driver *driver)
227 DBG("driver %p name %s", driver, driver->name);
229 for (list = network_list; list != NULL; list = list->next) {
230 struct connman_network *network = list->data;
232 if (network->driver != NULL)
235 if (driver->type != network->type)
238 if (driver->probe(network) < 0)
241 network->driver = driver;
245 static void remove_driver(struct connman_network_driver *driver)
249 DBG("driver %p name %s", driver, driver->name);
251 for (list = network_list; list != NULL; list = list->next) {
252 struct connman_network *network = list->data;
254 if (network->driver == driver)
255 network_remove(network);
259 static gint compare_priority(gconstpointer a, gconstpointer b)
261 const struct connman_network_driver *driver1 = a;
262 const struct connman_network_driver *driver2 = b;
264 return driver2->priority - driver1->priority;
268 * connman_network_driver_register:
269 * @driver: network driver definition
271 * Register a new network driver
273 * Returns: %0 on success
275 int connman_network_driver_register(struct connman_network_driver *driver)
279 DBG("driver %p name %s", driver, driver->name);
281 for (list = driver_list; list; list = list->next) {
282 struct connman_network_driver *tmp = list->data;
284 if (tmp->type == driver->type)
289 driver_list = g_slist_insert_sorted(driver_list, driver,
292 probe_driver(driver);
298 * connman_network_driver_unregister:
299 * @driver: network driver definition
301 * Remove a previously registered network driver
303 void connman_network_driver_unregister(struct connman_network_driver *driver)
305 DBG("driver %p name %s", driver, driver->name);
307 driver_list = g_slist_remove(driver_list, driver);
309 remove_driver(driver);
312 static void network_destruct(struct connman_network *network)
314 DBG("network %p name %s", network, network->name);
316 g_free(network->wifi.ssid);
317 g_free(network->wifi.mode);
318 g_free(network->wifi.security);
319 g_free(network->wifi.passphrase);
320 g_free(network->wifi.agent_passphrase);
321 g_free(network->wifi.eap);
322 g_free(network->wifi.identity);
323 g_free(network->wifi.agent_identity);
324 g_free(network->wifi.ca_cert_path);
325 g_free(network->wifi.client_cert_path);
326 g_free(network->wifi.private_key_path);
327 g_free(network->wifi.private_key_passphrase);
328 g_free(network->wifi.phase2_auth);
329 g_free(network->wifi.pin_wps);
331 g_free(network->path);
332 g_free(network->group);
333 g_free(network->node);
334 g_free(network->name);
335 g_free(network->identifier);
337 network->device = NULL;
343 * connman_network_create:
344 * @identifier: network identifier (for example an unqiue name)
346 * Allocate a new network and assign the #identifier to it.
348 * Returns: a newly-allocated #connman_network structure
350 struct connman_network *connman_network_create(const char *identifier,
351 enum connman_network_type type)
353 struct connman_network *network;
356 DBG("identifier %s type %d", identifier, type);
358 network = g_try_new0(struct connman_network, 1);
362 DBG("network %p", network);
364 network->refcount = 1;
366 ident = g_strdup(identifier);
373 network->type = type;
374 network->identifier = ident;
376 network_list = g_slist_append(network_list, network);
382 * connman_network_ref:
383 * @network: network structure
385 * Increase reference counter of network
387 struct connman_network *connman_network_ref(struct connman_network *network)
389 DBG("network %p name %s refcount %d", network, network->name,
390 g_atomic_int_get(&network->refcount) + 1);
392 g_atomic_int_inc(&network->refcount);
398 * connman_network_unref:
399 * @network: network structure
401 * Decrease reference counter of network
403 void connman_network_unref(struct connman_network *network)
405 DBG("network %p name %s refcount %d", network, network->name,
406 g_atomic_int_get(&network->refcount) - 1);
408 if (g_atomic_int_dec_and_test(&network->refcount) == FALSE)
411 network_list = g_slist_remove(network_list, network);
413 network_destruct(network);
416 const char *__connman_network_get_type(struct connman_network *network)
418 return type2string(network->type);
422 * connman_network_get_type:
423 * @network: network structure
425 * Get type of network
427 enum connman_network_type connman_network_get_type(struct connman_network *network)
429 return network->type;
433 * connman_network_get_identifier:
434 * @network: network structure
436 * Get identifier of network
438 const char *connman_network_get_identifier(struct connman_network *network)
440 return network->identifier;
444 * connman_network_set_index:
445 * @network: network structure
446 * @index: index number
448 * Set index number of network
450 void connman_network_set_index(struct connman_network *network, int index)
452 struct connman_service *service;
453 struct connman_ipconfig *ipconfig;
455 service = __connman_service_lookup_from_network(network);
459 ipconfig = __connman_service_get_ip4config(service);
461 DBG("index %d service %p ip4config %p", network->index,
464 if (network->index < 0 && ipconfig == NULL) {
466 ipconfig = __connman_service_get_ip4config(service);
467 if (ipconfig == NULL)
469 * This is needed for plugins that havent set their
470 * ipconfig layer yet, due to not being able to get
471 * a network index prior to creating a service.
473 __connman_service_create_ip4config(service, index);
475 __connman_ipconfig_set_index(ipconfig, index);
478 /* If index changed, the index of ipconfig must be reset. */
479 if (ipconfig == NULL)
482 __connman_ipconfig_set_index(ipconfig, index);
486 network->index = index;
490 * connman_network_get_index:
491 * @network: network structure
493 * Get index number of network
495 int connman_network_get_index(struct connman_network *network)
497 return network->index;
501 * connman_network_set_group:
502 * @network: network structure
505 * Set group name for automatic clustering
507 void connman_network_set_group(struct connman_network *network,
510 switch (network->type) {
511 case CONNMAN_NETWORK_TYPE_UNKNOWN:
512 case CONNMAN_NETWORK_TYPE_VENDOR:
514 case CONNMAN_NETWORK_TYPE_ETHERNET:
515 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
516 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
517 case CONNMAN_NETWORK_TYPE_CELLULAR:
518 case CONNMAN_NETWORK_TYPE_WIFI:
519 case CONNMAN_NETWORK_TYPE_WIMAX:
523 if (g_strcmp0(network->group, group) == 0) {
525 __connman_service_update_from_network(network);
529 if (network->group != NULL) {
530 __connman_service_remove_from_network(network);
532 g_free(network->group);
535 network->group = g_strdup(group);
537 if (network->group != NULL)
538 network_probe(network);
542 * connman_network_get_group:
543 * @network: network structure
545 * Get group name for automatic clustering
547 const char *connman_network_get_group(struct connman_network *network)
549 return network->group;
552 const char *__connman_network_get_ident(struct connman_network *network)
554 if (network->device == NULL)
557 return connman_device_get_ident(network->device);
560 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
562 switch (network->type) {
563 case CONNMAN_NETWORK_TYPE_UNKNOWN:
564 case CONNMAN_NETWORK_TYPE_VENDOR:
565 case CONNMAN_NETWORK_TYPE_ETHERNET:
566 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
567 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
568 case CONNMAN_NETWORK_TYPE_CELLULAR:
569 case CONNMAN_NETWORK_TYPE_WIMAX:
571 case CONNMAN_NETWORK_TYPE_WIFI:
572 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
574 if (network->strength > 0 && network->strength < 20)
582 connman_bool_t connman_network_get_connecting(struct connman_network *network)
584 return network->connecting;
588 * connman_network_set_available:
589 * @network: network structure
590 * @available: availability state
592 * Change availability state of network (in range)
594 int connman_network_set_available(struct connman_network *network,
595 connman_bool_t available)
597 DBG("network %p available %d", network, available);
599 if (network->available == available)
602 network->available = available;
608 * connman_network_get_available:
609 * @network: network structure
611 * Get network available setting
613 connman_bool_t connman_network_get_available(struct connman_network *network)
615 return network->available;
619 * connman_network_set_associating:
620 * @network: network structure
621 * @associating: associating state
623 * Change associating state of network
625 int connman_network_set_associating(struct connman_network *network,
626 connman_bool_t associating)
628 DBG("network %p associating %d", network, associating);
630 if (network->associating == associating)
633 network->associating = associating;
635 if (associating == TRUE) {
636 struct connman_service *service;
638 service = __connman_service_lookup_from_network(network);
639 __connman_service_ipconfig_indicate_state(service,
640 CONNMAN_SERVICE_STATE_ASSOCIATION,
641 CONNMAN_IPCONFIG_TYPE_IPV4);
642 __connman_service_ipconfig_indicate_state(service,
643 CONNMAN_SERVICE_STATE_ASSOCIATION,
644 CONNMAN_IPCONFIG_TYPE_IPV6);
650 static void set_associate_error(struct connman_network *network)
652 struct connman_service *service;
654 if (network->associating == FALSE)
657 network->associating = FALSE;
659 service = __connman_service_lookup_from_network(network);
661 __connman_service_ipconfig_indicate_state(service,
662 CONNMAN_SERVICE_STATE_FAILURE,
663 CONNMAN_IPCONFIG_TYPE_IPV4);
666 static void set_configure_error(struct connman_network *network)
668 struct connman_service *service;
670 network->connecting = FALSE;
672 service = __connman_service_lookup_from_network(network);
674 __connman_service_ipconfig_indicate_state(service,
675 CONNMAN_SERVICE_STATE_FAILURE,
676 CONNMAN_IPCONFIG_TYPE_IPV4);
679 static void set_invalid_key_error(struct connman_network *network)
681 struct connman_service *service;
683 service = __connman_service_lookup_from_network(network);
685 __connman_service_indicate_error(service,
686 CONNMAN_SERVICE_ERROR_INVALID_KEY);
689 static void set_connect_error(struct connman_network *network)
691 struct connman_service *service;
693 service = __connman_service_lookup_from_network(network);
695 __connman_service_indicate_error(service,
696 CONNMAN_SERVICE_ERROR_CONNECT_FAILED);
699 void connman_network_set_ipv4_method(struct connman_network *network,
700 enum connman_ipconfig_method method)
702 struct connman_service *service;
703 struct connman_ipconfig *ipconfig;
705 service = __connman_service_lookup_from_network(network);
709 ipconfig = __connman_service_get_ip4config(service);
710 if (ipconfig == NULL)
713 connman_ipconfig_set_method(ipconfig, method);
716 void connman_network_set_ipv6_method(struct connman_network *network,
717 enum connman_ipconfig_method method)
719 struct connman_service *service;
720 struct connman_ipconfig *ipconfig;
722 service = __connman_service_lookup_from_network(network);
726 ipconfig = __connman_service_get_ip6config(service);
727 if (ipconfig == NULL)
730 connman_ipconfig_set_method(ipconfig, method);
733 void connman_network_set_error(struct connman_network *network,
734 enum connman_network_error error)
736 DBG("nework %p, error %d", network, error);
738 network->connecting = FALSE;
741 case CONNMAN_NETWORK_ERROR_UNKNOWN:
743 case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
744 set_associate_error(network);
746 case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
747 set_configure_error(network);
749 case CONNMAN_NETWORK_ERROR_INVALID_KEY:
750 set_invalid_key_error(network);
752 case CONNMAN_NETWORK_ERROR_CONNECT_FAIL:
753 set_connect_error(network);
757 network_change(network);
760 void connman_network_clear_error(struct connman_network *network)
762 struct connman_service *service;
764 DBG("network %p", network);
769 if (network->connecting == TRUE || network->associating == TRUE)
772 service = __connman_service_lookup_from_network(network);
773 __connman_service_clear_error(service);
776 static void set_configuration(struct connman_network *network)
778 struct connman_service *service;
780 DBG("network %p", network);
782 __connman_device_set_network(network->device, network);
784 connman_device_set_disconnected(network->device, FALSE);
786 service = __connman_service_lookup_from_network(network);
787 __connman_service_ipconfig_indicate_state(service,
788 CONNMAN_SERVICE_STATE_CONFIGURATION,
789 CONNMAN_IPCONFIG_TYPE_IPV4);
792 static void dhcp_success(struct connman_network *network)
794 struct connman_service *service;
795 struct connman_ipconfig *ipconfig_ipv4;
798 service = __connman_service_lookup_from_network(network);
802 connman_network_set_associating(network, FALSE);
804 network->connecting = FALSE;
806 ipconfig_ipv4 = __connman_service_get_ip4config(service);
807 err = __connman_ipconfig_address_add(ipconfig_ipv4);
811 err = __connman_ipconfig_gateway_add(ipconfig_ipv4);
818 connman_network_set_error(network,
819 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
822 static void dhcp_failure(struct connman_network *network)
824 struct connman_service *service;
826 service = __connman_service_lookup_from_network(network);
830 __connman_service_ipconfig_indicate_state(service,
831 CONNMAN_SERVICE_STATE_IDLE,
832 CONNMAN_IPCONFIG_TYPE_IPV4);
835 static void dhcp_callback(struct connman_network *network,
836 connman_bool_t success)
838 DBG("success %d", success);
841 dhcp_success(network);
843 dhcp_failure(network);
846 static int set_connected_fixed(struct connman_network *network)
848 struct connman_service *service;
849 struct connman_ipconfig *ipconfig_ipv4;
854 service = __connman_service_lookup_from_network(network);
856 ipconfig_ipv4 = __connman_service_get_ip4config(service);
858 set_configuration(network);
860 network->connecting = FALSE;
862 connman_network_set_associating(network, FALSE);
864 err = __connman_ipconfig_address_add(ipconfig_ipv4);
868 err = __connman_ipconfig_gateway_add(ipconfig_ipv4);
875 connman_network_set_error(network,
876 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
881 static void set_connected_manual(struct connman_network *network)
883 struct connman_service *service;
884 struct connman_ipconfig *ipconfig;
887 DBG("network %p", network);
889 service = __connman_service_lookup_from_network(network);
891 ipconfig = __connman_service_get_ip4config(service);
893 if (__connman_ipconfig_get_local(ipconfig) == NULL)
894 __connman_service_read_ip4config(service);
896 set_configuration(network);
898 err = __connman_ipconfig_address_add(ipconfig);
902 err = __connman_ipconfig_gateway_add(ipconfig);
906 network->connecting = FALSE;
908 connman_network_set_associating(network, FALSE);
913 connman_network_set_error(network,
914 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
918 static int set_connected_dhcp(struct connman_network *network)
922 DBG("network %p", network);
924 set_configuration(network);
926 err = __connman_dhcp_start(network, dhcp_callback);
928 connman_error("Can not request DHCP lease");
935 static int manual_ipv6_set(struct connman_network *network,
936 struct connman_ipconfig *ipconfig_ipv6)
938 struct connman_service *service;
941 DBG("network %p ipv6 %p", network, ipconfig_ipv6);
943 service = __connman_service_lookup_from_network(network);
947 if (__connman_ipconfig_get_local(ipconfig_ipv6) == NULL)
948 __connman_service_read_ip6config(service);
950 err = __connman_ipconfig_address_add(ipconfig_ipv6);
952 connman_network_set_error(network,
953 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
957 err = __connman_ipconfig_gateway_add(ipconfig_ipv6);
961 __connman_connection_gateway_activate(service,
962 CONNMAN_IPCONFIG_TYPE_IPV6);
964 __connman_device_set_network(network->device, network);
966 connman_device_set_disconnected(network->device, FALSE);
968 network->connecting = FALSE;
973 static void autoconf_ipv6_set(struct connman_network *network)
975 DBG("network %p", network);
977 __connman_device_set_network(network->device, network);
979 connman_device_set_disconnected(network->device, FALSE);
981 /* XXX: Append IPv6 nameservers here */
983 network->connecting = FALSE;
986 static gboolean set_connected(gpointer user_data)
988 struct connman_network *network = user_data;
989 struct connman_service *service;
990 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
991 enum connman_ipconfig_method ipv4_method, ipv6_method;
993 service = __connman_service_lookup_from_network(network);
995 ipconfig_ipv4 = __connman_service_get_ip4config(service);
996 ipconfig_ipv6 = __connman_service_get_ip6config(service);
998 DBG("service %p ipv4 %p ipv6 %p", service, ipconfig_ipv4,
1001 ipv4_method = __connman_ipconfig_get_method(ipconfig_ipv4);
1002 ipv6_method = __connman_ipconfig_get_method(ipconfig_ipv6);
1004 DBG("method ipv4 %d ipv6 %d", ipv4_method, ipv6_method);
1005 DBG("network connected %d", network->connected);
1007 if (network->connected == TRUE) {
1010 switch (ipv6_method) {
1011 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1012 case CONNMAN_IPCONFIG_METHOD_OFF:
1014 case CONNMAN_IPCONFIG_METHOD_AUTO:
1015 autoconf_ipv6_set(network);
1017 case CONNMAN_IPCONFIG_METHOD_FIXED:
1018 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1019 ret = manual_ipv6_set(network, ipconfig_ipv6);
1021 connman_network_set_error(network,
1022 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1026 case CONNMAN_IPCONFIG_METHOD_DHCP:
1030 switch (ipv4_method) {
1031 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1032 case CONNMAN_IPCONFIG_METHOD_OFF:
1033 case CONNMAN_IPCONFIG_METHOD_AUTO:
1035 case CONNMAN_IPCONFIG_METHOD_FIXED:
1036 if (set_connected_fixed(network) < 0) {
1037 connman_network_set_error(network,
1038 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1042 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1043 set_connected_manual(network);
1045 case CONNMAN_IPCONFIG_METHOD_DHCP:
1046 if (set_connected_dhcp(network) < 0) {
1047 connman_network_set_error(network,
1048 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1054 struct connman_service *service;
1055 enum connman_service_state state;
1057 __connman_device_set_network(network->device, NULL);
1059 service = __connman_service_lookup_from_network(network);
1061 switch (ipv4_method) {
1062 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1063 case CONNMAN_IPCONFIG_METHOD_OFF:
1064 case CONNMAN_IPCONFIG_METHOD_AUTO:
1065 case CONNMAN_IPCONFIG_METHOD_FIXED:
1066 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1068 case CONNMAN_IPCONFIG_METHOD_DHCP:
1069 __connman_dhcp_stop(network);
1074 * We only set the disconnect state if we were not in idle
1075 * or in failure. It does not make sense to go to disconnect
1076 * state if we were not connected.
1078 state = __connman_service_ipconfig_get_state(service,
1079 CONNMAN_IPCONFIG_TYPE_IPV4);
1080 if (state != CONNMAN_SERVICE_STATE_IDLE &&
1081 state != CONNMAN_SERVICE_STATE_FAILURE)
1082 __connman_service_ipconfig_indicate_state(service,
1083 CONNMAN_SERVICE_STATE_DISCONNECT,
1084 CONNMAN_IPCONFIG_TYPE_IPV4);
1086 state = __connman_service_ipconfig_get_state(service,
1087 CONNMAN_IPCONFIG_TYPE_IPV6);
1088 if (state != CONNMAN_SERVICE_STATE_IDLE &&
1089 state != CONNMAN_SERVICE_STATE_FAILURE)
1090 __connman_service_ipconfig_indicate_state(service,
1091 CONNMAN_SERVICE_STATE_DISCONNECT,
1092 CONNMAN_IPCONFIG_TYPE_IPV6);
1094 __connman_connection_gateway_remove(service,
1095 CONNMAN_IPCONFIG_TYPE_ALL);
1097 __connman_ipconfig_address_unset(ipconfig_ipv4);
1098 __connman_ipconfig_address_unset(ipconfig_ipv6);
1101 * Special handling for IPv6 autoconfigured address.
1102 * The simplest way to remove autoconfigured routes is to
1103 * disable IPv6 temporarily so that kernel will do the cleanup
1106 if (ipv6_method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1107 __connman_ipconfig_disable_ipv6(ipconfig_ipv6);
1108 __connman_ipconfig_enable_ipv6(ipconfig_ipv6);
1111 __connman_service_ipconfig_indicate_state(service,
1112 CONNMAN_SERVICE_STATE_IDLE,
1113 CONNMAN_IPCONFIG_TYPE_IPV4);
1115 __connman_service_ipconfig_indicate_state(service,
1116 CONNMAN_SERVICE_STATE_IDLE,
1117 CONNMAN_IPCONFIG_TYPE_IPV6);
1120 network->connecting = FALSE;
1122 connman_network_set_associating(network, FALSE);
1128 * connman_network_set_connected:
1129 * @network: network structure
1130 * @connected: connected state
1132 * Change connected state of network
1134 int connman_network_set_connected(struct connman_network *network,
1135 connman_bool_t connected)
1137 DBG("network %p connected %d", network, connected);
1139 if ((network->connecting == TRUE || network->associating == TRUE) &&
1140 connected == FALSE) {
1141 connman_network_set_error(network,
1142 CONNMAN_NETWORK_ERROR_CONNECT_FAIL);
1143 __connman_network_disconnect(network);
1146 if (network->connected == connected)
1149 network->connected = connected;
1151 set_connected(network);
1157 * connman_network_get_connected:
1158 * @network: network structure
1160 * Get network connection status
1162 connman_bool_t connman_network_get_connected(struct connman_network *network)
1164 return network->connected;
1168 * connman_network_get_associating:
1169 * @network: network structure
1171 * Get network associating status
1173 connman_bool_t connman_network_get_associating(struct connman_network *network)
1175 return network->associating;
1179 * __connman_network_connect:
1180 * @network: network structure
1184 int __connman_network_connect(struct connman_network *network)
1188 DBG("network %p", network);
1190 if (network->connected == TRUE)
1193 if (network->connecting == TRUE || network->associating == TRUE)
1196 if (network->driver == NULL)
1199 if (network->driver->connect == NULL)
1202 if (network->device == NULL)
1205 network->connecting = TRUE;
1207 __connman_device_disconnect(network->device);
1209 err = network->driver->connect(network);
1211 if (err == -EINPROGRESS)
1212 connman_network_set_associating(network, TRUE);
1214 network->connecting = FALSE;
1220 network->connected = TRUE;
1221 set_connected(network);
1227 * __connman_network_disconnect:
1228 * @network: network structure
1230 * Disconnect network
1232 int __connman_network_disconnect(struct connman_network *network)
1236 DBG("network %p", network);
1238 if (network->connected == FALSE && network->connecting == FALSE &&
1239 network->associating == FALSE)
1242 if (network->driver == NULL)
1245 if (network->driver->disconnect == NULL)
1248 network->connecting = FALSE;
1250 err = network->driver->disconnect(network);
1252 connman_network_set_connected(network, FALSE);
1253 set_connected(network);
1259 static int manual_ipv4_set(struct connman_network *network,
1260 struct connman_ipconfig *ipconfig)
1262 struct connman_service *service;
1265 service = __connman_service_lookup_from_network(network);
1266 if (service == NULL)
1269 err = __connman_ipconfig_address_add(ipconfig);
1271 connman_network_set_error(network,
1272 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1276 return __connman_ipconfig_gateway_add(ipconfig);
1279 int __connman_network_clear_ipconfig(struct connman_network *network,
1280 struct connman_ipconfig *ipconfig)
1282 struct connman_service *service;
1283 enum connman_ipconfig_method method;
1284 enum connman_ipconfig_type type;
1286 service = __connman_service_lookup_from_network(network);
1287 if (service == NULL)
1290 method = __connman_ipconfig_get_method(ipconfig);
1291 type = __connman_ipconfig_get_config_type(ipconfig);
1294 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1295 case CONNMAN_IPCONFIG_METHOD_OFF:
1296 case CONNMAN_IPCONFIG_METHOD_FIXED:
1297 case CONNMAN_IPCONFIG_METHOD_AUTO:
1299 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1300 __connman_ipconfig_address_remove(ipconfig);
1302 case CONNMAN_IPCONFIG_METHOD_DHCP:
1303 __connman_dhcp_stop(network);
1307 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1308 __connman_service_ipconfig_indicate_state(service,
1309 CONNMAN_SERVICE_STATE_CONFIGURATION,
1310 CONNMAN_IPCONFIG_TYPE_IPV6);
1311 else if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1312 __connman_service_ipconfig_indicate_state(service,
1313 CONNMAN_SERVICE_STATE_CONFIGURATION,
1314 CONNMAN_IPCONFIG_TYPE_IPV4);
1319 int __connman_network_set_ipconfig(struct connman_network *network,
1320 struct connman_ipconfig *ipconfig_ipv4,
1321 struct connman_ipconfig *ipconfig_ipv6)
1323 enum connman_ipconfig_method method;
1326 if (network == NULL)
1329 if (ipconfig_ipv6) {
1330 method = __connman_ipconfig_get_method(ipconfig_ipv6);
1333 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1334 case CONNMAN_IPCONFIG_METHOD_OFF:
1336 case CONNMAN_IPCONFIG_METHOD_AUTO:
1337 autoconf_ipv6_set(network);
1339 case CONNMAN_IPCONFIG_METHOD_FIXED:
1340 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1341 ret = manual_ipv6_set(network, ipconfig_ipv6);
1343 connman_network_set_error(network,
1344 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1348 case CONNMAN_IPCONFIG_METHOD_DHCP:
1353 if (ipconfig_ipv4) {
1354 method = __connman_ipconfig_get_method(ipconfig_ipv4);
1357 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1358 case CONNMAN_IPCONFIG_METHOD_OFF:
1359 case CONNMAN_IPCONFIG_METHOD_FIXED:
1360 case CONNMAN_IPCONFIG_METHOD_AUTO:
1362 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1363 return manual_ipv4_set(network, ipconfig_ipv4);
1364 case CONNMAN_IPCONFIG_METHOD_DHCP:
1365 return __connman_dhcp_start(network, dhcp_callback);
1372 int connman_network_set_ipaddress(struct connman_network *network,
1373 struct connman_ipaddress *ipaddress)
1375 struct connman_service *service;
1376 struct connman_ipconfig *ipconfig = NULL;
1378 DBG("network %p", network);
1380 service = __connman_service_lookup_from_network(network);
1381 if (service == NULL)
1384 ipconfig = __connman_service_get_ipconfig(service, ipaddress->family);
1385 if (ipconfig == NULL)
1388 __connman_ipconfig_set_local(ipconfig, ipaddress->local);
1389 __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
1390 __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1391 __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1392 __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1397 int connman_network_set_nameservers(struct connman_network *network,
1398 const char *nameservers)
1400 struct connman_service *service;
1401 char **nameservers_array = NULL;
1404 DBG("network %p nameservers %s", network, nameservers);
1406 service = __connman_service_lookup_from_network(network);
1407 if (service == NULL)
1410 __connman_service_nameserver_clear(service);
1412 if (nameservers != NULL)
1413 nameservers_array = g_strsplit(nameservers, " ", 0);
1415 for (i = 0; nameservers_array[i] != NULL; i++) {
1416 __connman_service_nameserver_append(service,
1417 nameservers_array[i]);
1420 g_strfreev(nameservers_array);
1425 int connman_network_set_domain(struct connman_network *network,
1428 struct connman_service *service;
1430 DBG("network %p domain %s", network, domain);
1432 service = __connman_service_lookup_from_network(network);
1433 if (service == NULL)
1436 __connman_service_set_domainname(service, domain);
1442 * connman_network_set_name:
1443 * @network: network structure
1446 * Set display name value for network
1448 int connman_network_set_name(struct connman_network *network,
1451 DBG("network %p name %s", network, name);
1453 g_free(network->name);
1454 network->name = g_strdup(name);
1460 * connman_network_set_strength:
1461 * @network: network structure
1462 * @strength: strength value
1464 * Set signal strength value for network
1467 int connman_network_set_strength(struct connman_network *network,
1468 connman_uint8_t strength)
1470 DBG("network %p strengh %d", network, strength);
1472 network->strength = strength;
1477 connman_uint8_t connman_network_get_strength(struct connman_network *network)
1479 return network->strength;
1482 int connman_network_set_frequency(struct connman_network *network,
1483 connman_uint16_t frequency)
1485 DBG("network %p frequency %d", network, frequency);
1487 network->frequency = frequency;
1492 connman_uint16_t connman_network_get_frequency(struct connman_network *network)
1494 return network->frequency;
1497 int connman_network_set_wifi_channel(struct connman_network *network,
1498 connman_uint16_t channel)
1500 DBG("network %p wifi channel %d", network, channel);
1502 network->wifi.channel = channel;
1507 connman_uint16_t connman_network_get_wifi_channel(struct connman_network *network)
1509 return network->wifi.channel;
1513 * connman_network_set_roaming:
1514 * @network: network structure
1515 * @roaming: roaming state
1517 * Set roaming state for network
1519 int connman_network_set_roaming(struct connman_network *network,
1520 connman_bool_t roaming)
1522 DBG("network %p roaming %d", network, roaming);
1524 network->roaming = roaming;
1530 * connman_network_set_string:
1531 * @network: network structure
1532 * @key: unique identifier
1533 * @value: string value
1535 * Set string value for specific key
1537 int connman_network_set_string(struct connman_network *network,
1538 const char *key, const char *value)
1540 DBG("network %p key %s value %s", network, key, value);
1542 if (g_strcmp0(key, "Name") == 0)
1543 return connman_network_set_name(network, value);
1545 if (g_str_equal(key, "Path") == TRUE) {
1546 g_free(network->path);
1547 network->path = g_strdup(value);
1548 } else if (g_str_equal(key, "Node") == TRUE) {
1549 g_free(network->node);
1550 network->node = g_strdup(value);
1551 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1552 g_free(network->wifi.mode);
1553 network->wifi.mode = g_strdup(value);
1554 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1555 g_free(network->wifi.security);
1556 network->wifi.security = g_strdup(value);
1557 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1558 g_free(network->wifi.passphrase);
1559 network->wifi.passphrase = g_strdup(value);
1560 } else if (g_str_equal(key, "WiFi.AgentPassphrase") == TRUE) {
1561 g_free(network->wifi.agent_passphrase);
1562 network->wifi.agent_passphrase = g_strdup(value);
1563 } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1564 g_free(network->wifi.eap);
1565 network->wifi.eap = g_strdup(value);
1566 } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1567 g_free(network->wifi.identity);
1568 network->wifi.identity = g_strdup(value);
1569 } else if (g_str_equal(key, "WiFi.AgentIdentity") == TRUE) {
1570 g_free(network->wifi.agent_identity);
1571 network->wifi.agent_identity = g_strdup(value);
1572 } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1573 g_free(network->wifi.ca_cert_path);
1574 network->wifi.ca_cert_path = g_strdup(value);
1575 } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1576 g_free(network->wifi.client_cert_path);
1577 network->wifi.client_cert_path = g_strdup(value);
1578 } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1579 g_free(network->wifi.private_key_path);
1580 network->wifi.private_key_path = g_strdup(value);
1581 } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1582 g_free(network->wifi.private_key_passphrase);
1583 network->wifi.private_key_passphrase = g_strdup(value);
1584 } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1585 g_free(network->wifi.phase2_auth);
1586 network->wifi.phase2_auth = g_strdup(value);
1587 } else if (g_str_equal(key, "WiFi.PinWPS") == TRUE) {
1588 g_free(network->wifi.pin_wps);
1589 network->wifi.pin_wps = g_strdup(value);
1598 * connman_network_get_string:
1599 * @network: network structure
1600 * @key: unique identifier
1602 * Get string value for specific key
1604 const char *connman_network_get_string(struct connman_network *network,
1607 DBG("network %p key %s", network, key);
1609 if (g_str_equal(key, "Path") == TRUE)
1610 return network->path;
1611 else if (g_str_equal(key, "Name") == TRUE)
1612 return network->name;
1613 else if (g_str_equal(key, "Node") == TRUE)
1614 return network->node;
1615 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1616 return network->wifi.mode;
1617 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1618 return network->wifi.security;
1619 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1620 return network->wifi.passphrase;
1621 else if (g_str_equal(key, "WiFi.AgentPassphrase") == TRUE)
1622 return network->wifi.agent_passphrase;
1623 else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1624 return network->wifi.eap;
1625 else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1626 return network->wifi.identity;
1627 else if (g_str_equal(key, "WiFi.AgentIdentity") == TRUE)
1628 return network->wifi.agent_identity;
1629 else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1630 return network->wifi.ca_cert_path;
1631 else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1632 return network->wifi.client_cert_path;
1633 else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1634 return network->wifi.private_key_path;
1635 else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1636 return network->wifi.private_key_passphrase;
1637 else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1638 return network->wifi.phase2_auth;
1639 else if (g_str_equal(key, "WiFi.PinWPS") == TRUE)
1640 return network->wifi.pin_wps;
1646 * connman_network_set_bool:
1647 * @network: network structure
1648 * @key: unique identifier
1649 * @value: boolean value
1651 * Set boolean value for specific key
1653 int connman_network_set_bool(struct connman_network *network,
1654 const char *key, connman_bool_t value)
1656 DBG("network %p key %s value %d", network, key, value);
1658 if (g_strcmp0(key, "Roaming") == 0)
1659 return connman_network_set_roaming(network, value);
1660 else if (g_strcmp0(key, "WiFi.WPS") == 0)
1661 network->wifi.wps = value;
1662 else if (g_strcmp0(key, "WiFi.UseWPS") == 0)
1663 network->wifi.use_wps = value;
1669 * connman_network_get_bool:
1670 * @network: network structure
1671 * @key: unique identifier
1673 * Get boolean value for specific key
1675 connman_bool_t connman_network_get_bool(struct connman_network *network,
1678 DBG("network %p key %s", network, key);
1680 if (g_str_equal(key, "Roaming") == TRUE)
1681 return network->roaming;
1682 else if (g_str_equal(key, "WiFi.WPS") == TRUE)
1683 return network->wifi.wps;
1684 else if (g_str_equal(key, "WiFi.UseWPS") == TRUE)
1685 return network->wifi.use_wps;
1691 * connman_network_set_blob:
1692 * @network: network structure
1693 * @key: unique identifier
1697 * Set binary blob value for specific key
1699 int connman_network_set_blob(struct connman_network *network,
1700 const char *key, const void *data, unsigned int size)
1702 DBG("network %p key %s size %d", network, key, size);
1704 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1705 g_free(network->wifi.ssid);
1706 network->wifi.ssid = g_try_malloc(size);
1707 if (network->wifi.ssid != NULL) {
1708 memcpy(network->wifi.ssid, data, size);
1709 network->wifi.ssid_len = size;
1711 network->wifi.ssid_len = 0;
1720 * connman_network_get_blob:
1721 * @network: network structure
1722 * @key: unique identifier
1723 * @size: pointer to blob size
1725 * Get binary blob value for specific key
1727 const void *connman_network_get_blob(struct connman_network *network,
1728 const char *key, unsigned int *size)
1730 DBG("network %p key %s", network, key);
1732 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1734 *size = network->wifi.ssid_len;
1735 return network->wifi.ssid;
1741 void __connman_network_set_device(struct connman_network *network,
1742 struct connman_device *device)
1744 if (network->device == device)
1747 if (network->device != NULL)
1748 network_remove(network);
1750 network->device = device;
1752 if (network->device != NULL)
1753 network_probe(network);
1757 * connman_network_get_device:
1758 * @network: network structure
1760 * Get parent device of network
1762 struct connman_device *connman_network_get_device(struct connman_network *network)
1764 return network->device;
1768 * connman_network_get_data:
1769 * @network: network structure
1771 * Get private network data pointer
1773 void *connman_network_get_data(struct connman_network *network)
1775 return network->driver_data;
1779 * connman_network_set_data:
1780 * @network: network structure
1781 * @data: data pointer
1783 * Set private network data pointer
1785 void connman_network_set_data(struct connman_network *network, void *data)
1787 network->driver_data = data;
1790 void connman_network_update(struct connman_network *network)
1792 switch (network->type) {
1793 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1794 case CONNMAN_NETWORK_TYPE_VENDOR:
1796 case CONNMAN_NETWORK_TYPE_ETHERNET:
1797 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1798 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1799 case CONNMAN_NETWORK_TYPE_CELLULAR:
1800 case CONNMAN_NETWORK_TYPE_WIFI:
1801 case CONNMAN_NETWORK_TYPE_WIMAX:
1805 if (network->group != NULL)
1806 __connman_service_update_from_network(network);
1809 int __connman_network_init(void)
1816 void __connman_network_cleanup(void)