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 unsigned int hidden_counter = 0;
33 struct connman_network {
34 struct connman_element element;
35 enum connman_network_type type;
36 connman_bool_t available;
37 connman_bool_t connected;
38 connman_bool_t roaming;
39 connman_bool_t hidden;
40 connman_uint8_t strength;
41 connman_uint16_t frequency;
47 struct connman_network_driver *driver;
50 connman_bool_t connecting;
51 connman_bool_t associating;
53 struct connman_device *device;
59 unsigned short channel;
65 char *client_cert_path;
66 char *private_key_path;
67 char *private_key_passphrase;
70 connman_bool_t use_wps;
75 static const char *type2string(enum connman_network_type type)
78 case CONNMAN_NETWORK_TYPE_UNKNOWN:
79 case CONNMAN_NETWORK_TYPE_VENDOR:
81 case CONNMAN_NETWORK_TYPE_ETHERNET:
83 case CONNMAN_NETWORK_TYPE_WIFI:
85 case CONNMAN_NETWORK_TYPE_WIMAX:
87 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
88 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
90 case CONNMAN_NETWORK_TYPE_CELLULAR:
97 connman_bool_t __connman_network_has_driver(struct connman_network *network)
99 if (network == NULL || network->driver == NULL)
105 static GSList *driver_list = NULL;
107 static gint compare_priority(gconstpointer a, gconstpointer b)
109 const struct connman_network_driver *driver1 = a;
110 const struct connman_network_driver *driver2 = b;
112 return driver2->priority - driver1->priority;
116 * connman_network_driver_register:
117 * @driver: network driver definition
119 * Register a new network driver
121 * Returns: %0 on success
123 int connman_network_driver_register(struct connman_network_driver *driver)
127 DBG("driver %p name %s", driver, driver->name);
129 for (list = driver_list; list; list = list->next) {
130 struct connman_network_driver *tmp = list->data;
132 if (tmp->type == driver->type)
137 driver_list = g_slist_insert_sorted(driver_list, driver,
144 * connman_network_driver_unregister:
145 * @driver: network driver definition
147 * Remove a previously registered network driver
149 void connman_network_driver_unregister(struct connman_network_driver *driver)
151 DBG("driver %p name %s", driver, driver->name);
153 driver_list = g_slist_remove(driver_list, driver);
156 static void network_destruct(struct connman_element *element)
158 struct connman_network *network = element->network;
160 DBG("element %p name %s", element, element->name);
162 g_free(network->wifi.ssid);
163 g_free(network->wifi.mode);
164 g_free(network->wifi.security);
165 g_free(network->wifi.passphrase);
166 g_free(network->wifi.eap);
167 g_free(network->wifi.identity);
168 g_free(network->wifi.ca_cert_path);
169 g_free(network->wifi.client_cert_path);
170 g_free(network->wifi.private_key_path);
171 g_free(network->wifi.private_key_passphrase);
172 g_free(network->wifi.phase2_auth);
173 g_free(network->wifi.pin_wps);
175 g_free(network->group);
176 g_free(network->node);
177 g_free(network->name);
178 g_free(network->identifier);
180 network->device = NULL;
184 * connman_network_create:
185 * @identifier: network identifier (for example an unqiue name)
187 * Allocate a new network and assign the #identifier to it.
189 * Returns: a newly-allocated #connman_network structure
191 struct connman_network *connman_network_create(const char *identifier,
192 enum connman_network_type type)
194 struct connman_network *network;
195 connman_uint8_t strength = 0;
199 DBG("identifier %s type %d", identifier, type);
201 network = g_try_new0(struct connman_network, 1);
205 DBG("network %p", network);
207 __connman_element_initialize(&network->element);
209 if (identifier == NULL) {
210 temp = g_strdup_printf("hidden_%d", hidden_counter++);
211 network->hidden = TRUE;
213 temp = g_strdup(identifier);
220 network->element.name = temp;
221 network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
223 network->element.network = network;
224 network->element.destruct = network_destruct;
226 str = type2string(type);
228 connman_element_set_string(&network->element, "Type", str);
230 connman_element_set_uint8(&network->element, "Strength", strength);
232 network->type = type;
233 network->identifier = g_strdup(temp);
239 * connman_network_ref:
240 * @network: network structure
242 * Increase reference counter of network
244 struct connman_network *connman_network_ref(struct connman_network *network)
246 if (connman_element_ref(&network->element) == NULL)
253 * connman_network_unref:
254 * @network: network structure
256 * Decrease reference counter of network
258 void connman_network_unref(struct connman_network *network)
260 connman_element_unref(&network->element);
263 const char *__connman_network_get_type(struct connman_network *network)
265 return type2string(network->type);
269 * connman_network_get_type:
270 * @network: network structure
272 * Get type of network
274 enum connman_network_type connman_network_get_type(struct connman_network *network)
276 return network->type;
280 * connman_network_get_identifier:
281 * @network: network structure
283 * Get identifier of network
285 const char *connman_network_get_identifier(struct connman_network *network)
287 return network->identifier;
291 * connman_network_set_index:
292 * @network: network structure
293 * @index: index number
295 * Set index number of network
297 void connman_network_set_index(struct connman_network *network, int index)
299 struct connman_service *service;
300 struct connman_ipconfig *ipconfig;
302 service = __connman_service_lookup_from_network(network);
306 ipconfig = __connman_service_get_ip4config(service);
308 DBG("index %d service %p ip4config %p", network->element.index,
311 if (network->element.index < 0 && ipconfig == NULL) {
313 ipconfig = __connman_service_get_ip4config(service);
314 if (ipconfig == NULL)
316 * This is needed for plugins that havent set their
317 * ipconfig layer yet, due to not being able to get
318 * a network index prior to creating a service.
320 __connman_service_create_ip4config(service, index);
322 __connman_ipconfig_set_index(ipconfig, index);
325 /* If index changed, the index of ipconfig must be reset. */
326 if (ipconfig == NULL)
329 __connman_ipconfig_set_index(ipconfig, index);
333 network->element.index = index;
337 * connman_network_get_index:
338 * @network: network structure
340 * Get index number of network
342 int connman_network_get_index(struct connman_network *network)
344 return network->element.index;
348 * connman_network_get_element:
349 * @network: network structure
351 * Get connman_element of network
353 struct connman_element *connman_network_get_element(
354 struct connman_network *network)
356 return &network->element;
360 * connman_network_set_group:
361 * @network: network structure
364 * Set group name for automatic clustering
366 void connman_network_set_group(struct connman_network *network,
369 switch (network->type) {
370 case CONNMAN_NETWORK_TYPE_UNKNOWN:
371 case CONNMAN_NETWORK_TYPE_VENDOR:
373 case CONNMAN_NETWORK_TYPE_ETHERNET:
374 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
375 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
376 case CONNMAN_NETWORK_TYPE_CELLULAR:
377 case CONNMAN_NETWORK_TYPE_WIFI:
378 case CONNMAN_NETWORK_TYPE_WIMAX:
382 if (g_strcmp0(network->group, group) == 0) {
384 __connman_profile_update_network(network);
388 if (network->group != NULL) {
389 __connman_profile_remove_network(network);
391 g_free(network->group);
394 network->group = g_strdup(group);
396 if (network->group != NULL)
397 __connman_profile_add_network(network);
401 * connman_network_get_group:
402 * @network: network structure
404 * Get group name for automatic clustering
406 const char *connman_network_get_group(struct connman_network *network)
408 return network->group;
411 const char *__connman_network_get_ident(struct connman_network *network)
413 if (network->device == NULL)
416 return connman_device_get_ident(network->device);
419 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
421 switch (network->type) {
422 case CONNMAN_NETWORK_TYPE_UNKNOWN:
423 case CONNMAN_NETWORK_TYPE_VENDOR:
424 case CONNMAN_NETWORK_TYPE_ETHERNET:
425 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
426 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
427 case CONNMAN_NETWORK_TYPE_CELLULAR:
428 case CONNMAN_NETWORK_TYPE_WIMAX:
430 case CONNMAN_NETWORK_TYPE_WIFI:
431 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
433 if (network->strength > 0 && network->strength < 20)
441 connman_bool_t __connman_network_get_connecting(struct connman_network *network)
443 return network->connecting;
447 * connman_network_set_available:
448 * @network: network structure
449 * @available: availability state
451 * Change availability state of network (in range)
453 int connman_network_set_available(struct connman_network *network,
454 connman_bool_t available)
456 DBG("network %p available %d", network, available);
458 if (network->available == available)
461 network->available = available;
467 * connman_network_get_available:
468 * @network: network structure
470 * Get network available setting
472 connman_bool_t connman_network_get_available(struct connman_network *network)
474 if (network->hidden == TRUE)
477 return network->available;
481 * connman_network_set_associating:
482 * @network: network structure
483 * @associating: associating state
485 * Change associating state of network
487 int connman_network_set_associating(struct connman_network *network,
488 connman_bool_t associating)
490 DBG("network %p associating %d", network, associating);
492 if (network->associating == associating)
495 network->associating = associating;
497 if (associating == TRUE) {
498 struct connman_service *service;
500 service = __connman_service_lookup_from_network(network);
501 __connman_service_indicate_state(service,
502 CONNMAN_SERVICE_STATE_ASSOCIATION,
503 CONNMAN_IPCONFIG_TYPE_IPV4);
509 static void set_associate_error(struct connman_network *network)
511 struct connman_service *service;
513 if (network->associating == FALSE)
516 network->associating = FALSE;
518 service = __connman_service_lookup_from_network(network);
520 __connman_service_indicate_state(service,
521 CONNMAN_SERVICE_STATE_FAILURE,
522 CONNMAN_IPCONFIG_TYPE_IPV4);
525 static void set_configure_error(struct connman_network *network)
527 struct connman_service *service;
529 network->connecting = FALSE;
531 service = __connman_service_lookup_from_network(network);
533 __connman_service_indicate_state(service,
534 CONNMAN_SERVICE_STATE_FAILURE,
535 CONNMAN_IPCONFIG_TYPE_IPV4);
538 static void set_invalid_key_error(struct connman_network *network)
540 struct connman_service *service;
542 service = __connman_service_lookup_from_network(network);
544 __connman_service_indicate_error(service,
545 CONNMAN_SERVICE_ERROR_INVALID_KEY);
548 void connman_network_set_ipv4_method(struct connman_network *network,
549 enum connman_ipconfig_method method)
551 struct connman_service *service;
552 struct connman_ipconfig *ipconfig;
554 network->element.ipv4.method = method;
556 service = __connman_service_lookup_from_network(network);
560 ipconfig = __connman_service_get_ip4config(service);
561 if (ipconfig == NULL)
564 connman_ipconfig_set_method(ipconfig, method);
567 void connman_network_set_ipv6_method(struct connman_network *network,
568 enum connman_ipconfig_method method)
570 struct connman_service *service;
571 struct connman_ipconfig *ipconfig;
573 network->element.ipv6.method = method;
575 service = __connman_service_lookup_from_network(network);
579 ipconfig = __connman_service_get_ip6config(service);
580 if (ipconfig == NULL)
583 connman_ipconfig_set_method(ipconfig, method);
586 void connman_network_set_error(struct connman_network *network,
587 enum connman_network_error error)
589 DBG("nework %p, error %d", network, error);
591 network->connecting = FALSE;
594 case CONNMAN_NETWORK_ERROR_UNKNOWN:
596 case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
597 set_associate_error(network);
599 case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
600 set_configure_error(network);
602 case CONNMAN_NETWORK_ERROR_INVALID_KEY:
603 set_invalid_key_error(network);
608 void connman_network_clear_error(struct connman_network *network)
610 struct connman_service *service;
612 DBG("network %p", network);
617 if (network->connecting == TRUE || network->associating == TRUE)
620 service = __connman_service_lookup_from_network(network);
621 __connman_service_clear_error(service);
624 static void set_configuration(struct connman_network *network)
626 struct connman_service *service;
628 DBG("network %p", network);
630 __connman_device_increase_connections(network->device);
632 __connman_device_set_network(network->device, network);
634 connman_device_set_disconnected(network->device, FALSE);
636 service = __connman_service_lookup_from_network(network);
637 __connman_service_indicate_state(service,
638 CONNMAN_SERVICE_STATE_CONFIGURATION,
639 CONNMAN_IPCONFIG_TYPE_IPV4);
642 static void dhcp_success(struct connman_network *network)
644 struct connman_service *service;
645 struct connman_ipconfig *ipconfig_ipv4;
648 service = __connman_service_lookup_from_network(network);
652 connman_network_set_associating(network, FALSE);
654 network->connecting = FALSE;
656 ipconfig_ipv4 = __connman_service_get_ip4config(service);
657 err = __connman_ipconfig_address_add(ipconfig_ipv4);
661 err = __connman_ipconfig_gateway_add(ipconfig_ipv4);
665 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY,
666 CONNMAN_IPCONFIG_TYPE_IPV4);
671 connman_network_set_error(network,
672 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
675 static void dhcp_failure(struct connman_network *network)
677 struct connman_service *service;
678 struct connman_ipconfig *ipconfig_ipv4;
680 service = __connman_service_lookup_from_network(network);
684 ipconfig_ipv4 = __connman_service_get_ip4config(service);
685 __connman_ipconfig_address_remove(ipconfig_ipv4);
687 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
688 CONNMAN_IPCONFIG_TYPE_IPV4);
691 static void dhcp_callback(struct connman_network *network,
692 connman_bool_t success)
694 DBG("success %d", success);
697 dhcp_success(network);
699 dhcp_failure(network);
702 static int set_connected_fixed(struct connman_network *network)
704 struct connman_service *service;
705 struct connman_element *parent, *element;
709 service = __connman_service_lookup_from_network(network);
711 parent = connman_network_get_element(network);
713 set_configuration(network);
715 if (parent->ipv4.address == NULL)
718 if (parent->ipv4.netmask == NULL)
721 element = connman_element_create(NULL);
722 if (element == NULL) {
723 connman_error("Can not create connman_element");
727 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
728 element->index = parent->index;
730 if (connman_element_register(element, parent) < 0) {
731 connman_error("Can not register connman_element");
735 network->connecting = FALSE;
737 connman_network_set_associating(network, FALSE);
742 static void set_connected_manual(struct connman_network *network)
744 struct connman_service *service;
745 struct connman_ipconfig *ipconfig;
748 DBG("network %p", network);
750 service = __connman_service_lookup_from_network(network);
752 ipconfig = __connman_service_get_ip4config(service);
754 set_configuration(network);
756 err = __connman_ipconfig_address_add(ipconfig);
760 err = __connman_ipconfig_gateway_add(ipconfig);
764 network->connecting = FALSE;
766 connman_network_set_associating(network, FALSE);
768 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY,
769 CONNMAN_IPCONFIG_TYPE_IPV4);
774 connman_network_set_error(network,
775 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
779 static int set_connected_dhcp(struct connman_network *network)
783 DBG("network %p", network);
785 set_configuration(network);
787 err = __connman_dhcp_start(network, dhcp_callback);
789 connman_error("Can not request DHCP lease");
796 static int manual_ipv6_set(struct connman_network *network,
797 struct connman_ipconfig *ipconfig_ipv6)
799 struct connman_service *service;
802 service = __connman_service_lookup_from_network(network);
806 err = __connman_ipconfig_address_add(ipconfig_ipv6);
808 connman_network_set_error(network,
809 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
814 * READY state will be indicated by IPV4 setting
815 * gateway will be set by IPV4 setting
821 static void autoconf_ipv6_set(struct connman_network *network)
823 struct connman_service *service;
824 struct connman_ipconfig *ipconfig;
825 const char *nameserver = NULL;
827 DBG("network %p", network);
829 service = __connman_service_lookup_from_network(network);
831 ipconfig = __connman_service_get_ip6config(service);
833 __connman_device_increase_connections(network->device);
835 __connman_device_set_network(network->device, network);
837 connman_device_set_disconnected(network->device, FALSE);
839 connman_element_get_value(&network->element,
840 CONNMAN_PROPERTY_ID_IPV6_NAMESERVER, &nameserver);
841 if (nameserver != NULL)
842 __connman_service_nameserver_append(service, nameserver);
844 network->connecting = FALSE;
846 __connman_service_indicate_state(service,
847 CONNMAN_SERVICE_STATE_READY,
848 CONNMAN_IPCONFIG_TYPE_IPV6);
851 static gboolean set_connected(gpointer user_data)
853 struct connman_network *network = user_data;
854 struct connman_service *service;
855 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
856 enum connman_ipconfig_method ipv4_method, ipv6_method;
858 service = __connman_service_lookup_from_network(network);
860 ipconfig_ipv4 = __connman_service_get_ip4config(service);
861 ipconfig_ipv6 = __connman_service_get_ip6config(service);
863 DBG("service %p ipv4 %p ipv6 %p", service, ipconfig_ipv4,
866 ipv4_method = __connman_ipconfig_get_method(ipconfig_ipv4);
867 ipv6_method = __connman_ipconfig_get_method(ipconfig_ipv6);
869 DBG("method ipv4 %d ipv6 %d", ipv4_method, ipv6_method);
870 DBG("network connected %d", network->connected);
872 if (network->connected == TRUE) {
875 switch (ipv6_method) {
876 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
877 case CONNMAN_IPCONFIG_METHOD_OFF:
879 case CONNMAN_IPCONFIG_METHOD_AUTO:
880 autoconf_ipv6_set(network);
882 case CONNMAN_IPCONFIG_METHOD_FIXED:
883 case CONNMAN_IPCONFIG_METHOD_MANUAL:
884 ret = manual_ipv6_set(network, ipconfig_ipv6);
886 connman_network_set_error(network,
887 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
891 case CONNMAN_IPCONFIG_METHOD_DHCP:
895 switch (ipv4_method) {
896 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
897 case CONNMAN_IPCONFIG_METHOD_OFF:
898 case CONNMAN_IPCONFIG_METHOD_AUTO:
900 case CONNMAN_IPCONFIG_METHOD_FIXED:
901 if (set_connected_fixed(network) < 0) {
902 connman_network_set_error(network,
903 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
907 case CONNMAN_IPCONFIG_METHOD_MANUAL:
908 set_connected_manual(network);
910 case CONNMAN_IPCONFIG_METHOD_DHCP:
911 if (set_connected_dhcp(network) < 0) {
912 connman_network_set_error(network,
913 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
919 struct connman_service *service;
921 connman_element_unregister_children(&network->element);
923 __connman_device_set_network(network->device, NULL);
924 network->hidden = FALSE;
926 service = __connman_service_lookup_from_network(network);
928 __connman_service_indicate_state(service,
929 CONNMAN_SERVICE_STATE_IDLE,
930 CONNMAN_IPCONFIG_TYPE_IPV4);
932 /* TODO: eventually the IPv6 disconnect state should be handled
935 __connman_service_indicate_state(service,
936 CONNMAN_SERVICE_STATE_DISCONNECT,
937 CONNMAN_IPCONFIG_TYPE_IPV6);
939 __connman_service_indicate_state(service,
940 CONNMAN_SERVICE_STATE_IDLE,
941 CONNMAN_IPCONFIG_TYPE_IPV6);
944 network->connecting = FALSE;
946 connman_network_set_associating(network, FALSE);
952 * connman_network_set_connected:
953 * @network: network structure
954 * @connected: connected state
956 * Change connected state of network
958 int connman_network_set_connected(struct connman_network *network,
959 connman_bool_t connected)
961 DBG("network %p connected %d", network, connected);
963 if ((network->connecting == TRUE || network->associating == TRUE) &&
964 connected == FALSE) {
965 connman_element_set_error(&network->element,
966 CONNMAN_ELEMENT_ERROR_CONNECT_FAILED);
967 __connman_network_disconnect(network);
970 if (network->connected == connected)
973 if (connected == FALSE)
974 __connman_device_decrease_connections(network->device);
976 network->connected = connected;
978 set_connected(network);
984 * connman_network_get_connected:
985 * @network: network structure
987 * Get network connection status
989 connman_bool_t connman_network_get_connected(struct connman_network *network)
991 return network->connected;
995 * connman_network_get_associating:
996 * @network: network structure
998 * Get network associating status
1000 connman_bool_t connman_network_get_associating(struct connman_network *network)
1002 return network->associating;
1006 * __connman_network_connect:
1007 * @network: network structure
1011 int __connman_network_connect(struct connman_network *network)
1013 struct connman_service *service;
1016 DBG("network %p", network);
1018 if (network->connected == TRUE)
1021 if (network->connecting == TRUE || network->associating == TRUE)
1024 if (network->driver == NULL)
1027 if (network->driver->connect == NULL)
1030 if (network->device == NULL)
1033 network->connecting = TRUE;
1035 __connman_device_disconnect(network->device);
1037 service = __connman_service_lookup_from_network(network);
1039 err = network->driver->connect(network);
1041 if (err == -EINPROGRESS)
1042 connman_network_set_associating(network, TRUE);
1044 network->connecting = FALSE;
1045 network->hidden = FALSE;
1051 network->connected = TRUE;
1052 set_connected(network);
1058 * __connman_network_disconnect:
1059 * @network: network structure
1061 * Disconnect network
1063 int __connman_network_disconnect(struct connman_network *network)
1067 DBG("network %p", network);
1069 if (network->connected == FALSE && network->connecting == FALSE &&
1070 network->associating == FALSE)
1073 if (network->driver == NULL)
1076 if (network->driver->disconnect == NULL)
1079 network->connecting = FALSE;
1081 err = network->driver->disconnect(network);
1083 connman_network_set_connected(network, FALSE);
1084 set_connected(network);
1090 static int manual_ipv4_set(struct connman_network *network,
1091 struct connman_ipconfig *ipconfig)
1093 struct connman_service *service;
1096 service = __connman_service_lookup_from_network(network);
1097 if (service == NULL)
1100 err = __connman_ipconfig_address_add(ipconfig);
1102 connman_network_set_error(network,
1103 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1107 __connman_ipconfig_gateway_add(ipconfig);
1109 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY,
1110 CONNMAN_IPCONFIG_TYPE_IPV4);
1115 int __connman_network_clear_ipconfig(struct connman_network *network,
1116 struct connman_ipconfig *ipconfig)
1118 struct connman_service *service;
1119 enum connman_ipconfig_method method;
1120 enum connman_ipconfig_type type;
1122 service = __connman_service_lookup_from_network(network);
1123 if (service == NULL)
1126 method = __connman_ipconfig_get_method(ipconfig);
1127 type = __connman_ipconfig_get_config_type(ipconfig);
1130 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1131 case CONNMAN_IPCONFIG_METHOD_OFF:
1132 case CONNMAN_IPCONFIG_METHOD_FIXED:
1133 case CONNMAN_IPCONFIG_METHOD_AUTO:
1135 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1136 __connman_ipconfig_address_remove(ipconfig);
1138 case CONNMAN_IPCONFIG_METHOD_DHCP:
1139 __connman_dhcp_stop(network);
1143 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1144 __connman_service_indicate_state(service,
1145 CONNMAN_SERVICE_STATE_CONFIGURATION,
1146 CONNMAN_IPCONFIG_TYPE_IPV6);
1147 else if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1148 __connman_service_indicate_state(service,
1149 CONNMAN_SERVICE_STATE_CONFIGURATION,
1150 CONNMAN_IPCONFIG_TYPE_IPV4);
1155 int __connman_network_set_ipconfig(struct connman_network *network,
1156 struct connman_ipconfig *ipconfig_ipv4,
1157 struct connman_ipconfig *ipconfig_ipv6)
1159 enum connman_ipconfig_method method;
1162 if (ipconfig_ipv6) {
1163 method = __connman_ipconfig_get_method(ipconfig_ipv6);
1166 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1167 case CONNMAN_IPCONFIG_METHOD_OFF:
1169 case CONNMAN_IPCONFIG_METHOD_AUTO:
1170 autoconf_ipv6_set(network);
1172 case CONNMAN_IPCONFIG_METHOD_FIXED:
1173 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1174 ret = manual_ipv6_set(network, ipconfig_ipv6);
1176 connman_network_set_error(network,
1177 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1181 case CONNMAN_IPCONFIG_METHOD_DHCP:
1186 if (ipconfig_ipv4) {
1187 method = __connman_ipconfig_get_method(ipconfig_ipv4);
1190 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1191 case CONNMAN_IPCONFIG_METHOD_OFF:
1192 case CONNMAN_IPCONFIG_METHOD_FIXED:
1193 case CONNMAN_IPCONFIG_METHOD_AUTO:
1195 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1196 return manual_ipv4_set(network, ipconfig_ipv4);
1197 case CONNMAN_IPCONFIG_METHOD_DHCP:
1198 return __connman_dhcp_start(network, dhcp_callback);
1205 int connman_network_set_ipaddress(struct connman_network *network,
1206 struct connman_ipaddress *ipaddress)
1208 struct connman_service *service;
1209 struct connman_ipconfig *ipconfig = NULL;
1211 DBG("network %p", network);
1213 service = __connman_service_lookup_from_network(network);
1214 if (service == NULL)
1217 if (ipaddress->family == CONNMAN_IPCONFIG_TYPE_IPV4)
1218 ipconfig = __connman_service_get_ip4config(service);
1219 else if (ipaddress->family == CONNMAN_IPCONFIG_TYPE_IPV6)
1220 ipconfig = __connman_service_get_ip6config(service);
1222 if (ipconfig == NULL)
1225 __connman_ipconfig_set_local(ipconfig, ipaddress->local);
1226 __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
1227 __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1228 __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1229 __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1231 return __connman_ipconfig_gateway_add(ipconfig);
1234 int connman_network_set_pac(struct connman_network *network,
1237 struct connman_service *service;
1239 DBG("network %p pac %s", network, pac);
1241 service = __connman_service_lookup_from_network(network);
1242 if (service == NULL)
1245 __connman_service_set_pac(service, pac);
1250 int connman_network_set_nameservers(struct connman_network *network,
1251 const char *nameservers)
1253 struct connman_service *service;
1254 char **nameservers_array = NULL;
1257 DBG("network %p nameservers %s", network, nameservers);
1259 service = __connman_service_lookup_from_network(network);
1260 if (service == NULL)
1263 __connman_service_nameserver_clear(service);
1265 if (nameservers != NULL)
1266 nameservers_array = g_strsplit(nameservers, " ", 0);
1268 for (i = 0; nameservers_array[i] == NULL; i++) {
1269 __connman_service_nameserver_append(service,
1270 nameservers_array[i]);
1276 int connman_network_set_domain(struct connman_network *network,
1279 struct connman_service *service;
1281 DBG("network %p domain %s", network, domain);
1283 service = __connman_service_lookup_from_network(network);
1284 if (service == NULL)
1287 __connman_service_set_domainname(service, domain);
1293 * connman_network_set_name:
1294 * @network: network structure
1297 * Set display name value for network
1299 int connman_network_set_name(struct connman_network *network,
1302 DBG("network %p name %s", network, name);
1304 g_free(network->name);
1305 network->name = g_strdup(name);
1307 return connman_element_set_string(&network->element, "Name", name);
1311 * connman_network_set_strength:
1312 * @network: network structure
1313 * @strength: strength value
1315 * Set signal strength value for network
1317 int connman_network_set_strength(struct connman_network *network,
1318 connman_uint8_t strength)
1320 DBG("network %p strengh %d", network, strength);
1322 network->strength = strength;
1324 return connman_element_set_uint8(&network->element,
1325 "Strength", strength);
1329 * connman_network_set_roaming:
1330 * @network: network structure
1331 * @roaming: roaming state
1333 * Set roaming state for network
1335 int connman_network_set_roaming(struct connman_network *network,
1336 connman_bool_t roaming)
1338 DBG("network %p roaming %d", network, roaming);
1340 network->roaming = roaming;
1342 return connman_element_set_bool(&network->element,
1343 "Roaming", roaming);
1347 * connman_network_set_string:
1348 * @network: network structure
1349 * @key: unique identifier
1350 * @value: string value
1352 * Set string value for specific key
1354 int connman_network_set_string(struct connman_network *network,
1355 const char *key, const char *value)
1359 DBG("network %p key %s value %s", network, key, value);
1361 if (g_strcmp0(key, "Name") == 0)
1362 return connman_network_set_name(network, value);
1364 if (g_str_equal(key, "Node") == TRUE) {
1365 g_free(network->node);
1366 network->node = g_strdup(value);
1367 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1368 g_free(network->wifi.mode);
1369 network->wifi.mode = g_strdup(value);
1370 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1371 g_free(network->wifi.security);
1372 network->wifi.security = g_strdup(value);
1373 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1374 g_free(network->wifi.passphrase);
1375 network->wifi.passphrase = g_strdup(value);
1376 } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1377 g_free(network->wifi.eap);
1378 network->wifi.eap = g_strdup(value);
1379 } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1380 g_free(network->wifi.identity);
1381 network->wifi.identity = g_strdup(value);
1382 } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1383 g_free(network->wifi.ca_cert_path);
1384 network->wifi.ca_cert_path = g_strdup(value);
1385 } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1386 g_free(network->wifi.client_cert_path);
1387 network->wifi.client_cert_path = g_strdup(value);
1388 } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1389 g_free(network->wifi.private_key_path);
1390 network->wifi.private_key_path = g_strdup(value);
1391 } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1392 g_free(network->wifi.private_key_passphrase);
1393 network->wifi.private_key_passphrase = g_strdup(value);
1394 } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1395 g_free(network->wifi.phase2_auth);
1396 network->wifi.phase2_auth = g_strdup(value);
1397 } else if (g_str_equal(key, "WiFi.PinWPS") == TRUE) {
1398 g_free(network->wifi.pin_wps);
1399 network->wifi.pin_wps = g_strdup(value);
1402 err = connman_element_set_string(&network->element, key, value);
1406 if (network->driver == NULL)
1409 if (network->driver->setup)
1410 return network->driver->setup(network, key);
1416 * connman_network_get_string:
1417 * @network: network structure
1418 * @key: unique identifier
1420 * Get string value for specific key
1422 const char *connman_network_get_string(struct connman_network *network,
1425 DBG("network %p key %s", network, key);
1427 if (g_str_equal(key, "Name") == TRUE)
1428 return network->name;
1429 else if (g_str_equal(key, "Node") == TRUE)
1430 return network->node;
1431 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1432 return network->wifi.mode;
1433 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1434 return network->wifi.security;
1435 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1436 return network->wifi.passphrase;
1437 else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1438 return network->wifi.eap;
1439 else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1440 return network->wifi.identity;
1441 else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1442 return network->wifi.ca_cert_path;
1443 else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1444 return network->wifi.client_cert_path;
1445 else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1446 return network->wifi.private_key_path;
1447 else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1448 return network->wifi.private_key_passphrase;
1449 else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1450 return network->wifi.phase2_auth;
1451 else if (g_str_equal(key, "WiFi.PinWPS") == TRUE)
1452 return network->wifi.pin_wps;
1454 return connman_element_get_string(&network->element, key);
1458 * connman_network_set_bool:
1459 * @network: network structure
1460 * @key: unique identifier
1461 * @value: boolean value
1463 * Set boolean value for specific key
1465 int connman_network_set_bool(struct connman_network *network,
1466 const char *key, connman_bool_t value)
1468 DBG("network %p key %s value %d", network, key, value);
1470 if (g_strcmp0(key, "Roaming") == 0)
1471 return connman_network_set_roaming(network, value);
1472 else if (g_strcmp0(key, "WiFi.WPS") == 0)
1473 network->wifi.wps = value;
1474 else if (g_strcmp0(key, "WiFi.UseWPS") == 0)
1475 network->wifi.use_wps = value;
1477 return connman_element_set_bool(&network->element, key, value);
1481 * connman_network_get_bool:
1482 * @network: network structure
1483 * @key: unique identifier
1485 * Get boolean value for specific key
1487 connman_bool_t connman_network_get_bool(struct connman_network *network,
1490 DBG("network %p key %s", network, key);
1492 if (g_str_equal(key, "Roaming") == TRUE)
1493 return network->roaming;
1494 else if (g_str_equal(key, "WiFi.WPS") == TRUE)
1495 return network->wifi.wps;
1496 else if (g_str_equal(key, "WiFi.UseWPS") == TRUE)
1497 return network->wifi.use_wps;
1499 return connman_element_get_bool(&network->element, key);
1503 * connman_network_set_uint8:
1504 * @network: network structure
1505 * @key: unique identifier
1506 * @value: integer value
1508 * Set integer value for specific key
1510 int connman_network_set_uint8(struct connman_network *network,
1511 const char *key, connman_uint8_t value)
1513 DBG("network %p key %s value %d", network, key, value);
1515 if (g_strcmp0(key, "Strength") == 0)
1516 return connman_network_set_strength(network, value);
1518 return connman_element_set_uint8(&network->element, key, value);
1522 * connman_network_get_uint8:
1523 * @network: network structure
1524 * @key: unique identifier
1526 * Get integer value for specific key
1528 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1531 DBG("network %p key %s", network, key);
1533 if (g_str_equal(key, "Strength") == TRUE)
1534 return network->strength;
1536 return connman_element_get_uint8(&network->element, key);
1540 * connman_network_set_uint16:
1541 * @network: network structure
1542 * @key: unique identifier
1543 * @value: integer value
1545 * Set integer value for specific key
1547 int connman_network_set_uint16(struct connman_network *network,
1548 const char *key, connman_uint16_t value)
1550 DBG("network %p key %s value %d", network, key, value);
1552 if (g_str_equal(key, "Frequency") == TRUE)
1553 network->frequency = value;
1554 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1555 network->wifi.channel = value;
1561 * connman_network_get_uint16:
1562 * @network: network structure
1563 * @key: unique identifier
1565 * Get integer value for specific key
1567 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1570 DBG("network %p key %s", network, key);
1572 if (g_str_equal(key, "Frequency") == TRUE)
1573 return network->frequency;
1574 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1575 return network->wifi.channel;
1581 * connman_network_set_blob:
1582 * @network: network structure
1583 * @key: unique identifier
1587 * Set binary blob value for specific key
1589 int connman_network_set_blob(struct connman_network *network,
1590 const char *key, const void *data, unsigned int size)
1592 DBG("network %p key %s size %d", network, key, size);
1594 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1595 g_free(network->wifi.ssid);
1596 network->wifi.ssid = g_try_malloc(size);
1597 if (network->wifi.ssid != NULL) {
1598 memcpy(network->wifi.ssid, data, size);
1599 network->wifi.ssid_len = size;
1601 network->wifi.ssid_len = 0;
1604 return connman_element_set_blob(&network->element, key, data, size);
1608 * connman_network_get_blob:
1609 * @network: network structure
1610 * @key: unique identifier
1611 * @size: pointer to blob size
1613 * Get binary blob value for specific key
1615 const void *connman_network_get_blob(struct connman_network *network,
1616 const char *key, unsigned int *size)
1618 DBG("network %p key %s", network, key);
1620 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1622 *size = network->wifi.ssid_len;
1623 return network->wifi.ssid;
1626 return connman_element_get_blob(&network->element, key, size);
1629 void __connman_network_set_device(struct connman_network *network,
1630 struct connman_device *device)
1632 network->device = device;
1636 * connman_network_get_device:
1637 * @network: network structure
1639 * Get parent device of network
1641 struct connman_device *connman_network_get_device(struct connman_network *network)
1643 return network->device;
1647 * connman_network_get_data:
1648 * @network: network structure
1650 * Get private network data pointer
1652 void *connman_network_get_data(struct connman_network *network)
1654 return network->driver_data;
1658 * connman_network_set_data:
1659 * @network: network structure
1660 * @data: data pointer
1662 * Set private network data pointer
1664 void connman_network_set_data(struct connman_network *network, void *data)
1666 network->driver_data = data;
1669 void connman_network_update(struct connman_network *network)
1671 switch (network->type) {
1672 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1673 case CONNMAN_NETWORK_TYPE_VENDOR:
1675 case CONNMAN_NETWORK_TYPE_ETHERNET:
1676 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1677 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1678 case CONNMAN_NETWORK_TYPE_CELLULAR:
1679 case CONNMAN_NETWORK_TYPE_WIFI:
1680 case CONNMAN_NETWORK_TYPE_WIMAX:
1684 if (network->group != NULL)
1685 __connman_service_update_from_network(network);
1690 static gboolean match_driver(struct connman_network *network,
1691 struct connman_network_driver *driver)
1693 if (network->type == driver->type ||
1694 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1700 static int network_probe(struct connman_element *element)
1702 struct connman_network *network = element->network;
1705 DBG("element %p name %s", element, element->name);
1707 if (network == NULL)
1710 for (list = driver_list; list; list = list->next) {
1711 struct connman_network_driver *driver = list->data;
1713 if (match_driver(network, driver) == FALSE)
1716 DBG("driver %p name %s", driver, driver->name);
1718 if (driver->probe(network) == 0) {
1719 network->driver = driver;
1724 if (network->driver == NULL)
1727 switch (network->type) {
1728 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1729 case CONNMAN_NETWORK_TYPE_VENDOR:
1731 case CONNMAN_NETWORK_TYPE_ETHERNET:
1732 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1733 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1734 case CONNMAN_NETWORK_TYPE_CELLULAR:
1735 case CONNMAN_NETWORK_TYPE_WIFI:
1736 case CONNMAN_NETWORK_TYPE_WIMAX:
1737 if (network->group != NULL)
1738 __connman_profile_add_network(network);
1745 static void network_remove(struct connman_element *element)
1747 struct connman_network *network = element->network;
1749 DBG("element %p name %s", element, element->name);
1751 if (network == NULL)
1754 if (network->driver == NULL)
1757 switch (network->type) {
1758 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1759 case CONNMAN_NETWORK_TYPE_VENDOR:
1761 case CONNMAN_NETWORK_TYPE_ETHERNET:
1762 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1763 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1764 case CONNMAN_NETWORK_TYPE_CELLULAR:
1765 case CONNMAN_NETWORK_TYPE_WIFI:
1766 case CONNMAN_NETWORK_TYPE_WIMAX:
1767 if (network->group != NULL) {
1768 __connman_profile_remove_network(network);
1770 g_free(network->group);
1771 network->group = NULL;
1776 if (network->driver->remove)
1777 network->driver->remove(network);
1780 static void network_change(struct connman_element *element)
1782 struct connman_network *network = element->network;
1784 DBG("element %p name %s", element, element->name);
1786 if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1789 if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1792 if (network->connected == FALSE)
1795 connman_element_unregister_children(element);
1797 connman_device_set_disconnected(network->device, TRUE);
1799 if (network->driver && network->driver->disconnect) {
1800 network->driver->disconnect(network);
1804 network->connected = FALSE;
1807 static struct connman_driver network_driver = {
1809 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
1810 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1811 .probe = network_probe,
1812 .remove = network_remove,
1813 .change = network_change,
1816 int __connman_network_init(void)
1820 return connman_driver_register(&network_driver);
1823 void __connman_network_cleanup(void)
1827 connman_driver_unregister(&network_driver);