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
33 static unsigned int hidden_counter = 0;
35 struct connman_network {
36 struct connman_element element;
37 enum connman_network_type type;
38 enum connman_network_protocol protocol;
39 connman_bool_t available;
40 connman_bool_t connected;
41 connman_bool_t roaming;
42 connman_bool_t hidden;
43 connman_uint8_t strength;
44 connman_uint16_t frequency;
50 struct connman_ipconfig *ipconfig;
52 struct connman_network_driver *driver;
55 connman_bool_t registered;
56 connman_bool_t connecting;
57 connman_bool_t associating;
59 struct connman_device *device;
65 unsigned short channel;
71 char *client_cert_path;
72 char *private_key_path;
73 char *private_key_passphrase;
78 static const char *type2string(enum connman_network_type type)
81 case CONNMAN_NETWORK_TYPE_UNKNOWN:
82 case CONNMAN_NETWORK_TYPE_VENDOR:
84 case CONNMAN_NETWORK_TYPE_ETHERNET:
86 case CONNMAN_NETWORK_TYPE_WIFI:
88 case CONNMAN_NETWORK_TYPE_WIMAX:
90 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
91 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
93 case CONNMAN_NETWORK_TYPE_CELLULAR:
100 static DBusMessage *get_properties(DBusConnection *conn,
101 DBusMessage *msg, void *data)
103 struct connman_network *network = data;
105 DBusMessageIter array, dict;
107 DBG("conn %p", conn);
109 if (__connman_security_check_privilege(msg,
110 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
111 return __connman_error_permission_denied(msg);
113 reply = dbus_message_new_method_return(msg);
117 dbus_message_iter_init_append(reply, &array);
119 connman_dbus_dict_open(&array, &dict);
121 if (network->device) {
122 const char *path = connman_device_get_path(network->device);
124 connman_dbus_dict_append_basic(&dict, "Device",
125 DBUS_TYPE_OBJECT_PATH, &path);
128 if (network->address != NULL)
129 connman_dbus_dict_append_basic(&dict, "Address",
130 DBUS_TYPE_STRING, &network->address);
132 if (network->name != NULL)
133 connman_dbus_dict_append_basic(&dict, "Name",
134 DBUS_TYPE_STRING, &network->name);
136 connman_dbus_dict_append_basic(&dict, "Connected",
137 DBUS_TYPE_BOOLEAN, &network->connected);
139 if (network->strength > 0)
140 connman_dbus_dict_append_basic(&dict, "Strength",
141 DBUS_TYPE_BYTE, &network->strength);
143 if (network->frequency > 0)
144 connman_dbus_dict_append_basic(&dict, "Frequency",
145 DBUS_TYPE_UINT16, &network->frequency);
147 if (network->wifi.ssid != NULL && network->wifi.ssid_len > 0)
148 connman_dbus_dict_append_fixed_array(&dict, "WiFi.SSID",
149 DBUS_TYPE_BYTE, &network->wifi.ssid,
150 network->wifi.ssid_len);
152 if (network->wifi.mode != NULL)
153 connman_dbus_dict_append_basic(&dict, "WiFi.Mode",
154 DBUS_TYPE_STRING, &network->wifi.mode);
156 if (network->wifi.channel > 0)
157 connman_dbus_dict_append_basic(&dict, "WiFi.Channel",
158 DBUS_TYPE_UINT16, &network->wifi.channel);
160 if (network->wifi.security != NULL) {
161 connman_dbus_dict_append_basic(&dict, "WiFi.Security",
162 DBUS_TYPE_STRING, &network->wifi.security);
164 if (g_strcmp0(network->wifi.security, "ieee8021x") == 0 &&
165 network->wifi.eap != NULL)
166 connman_dbus_dict_append_basic(&dict, "WiFi.EAP",
167 DBUS_TYPE_STRING, &network->wifi.eap);
171 if (network->wifi.passphrase != NULL &&
172 __connman_security_check_privilege(msg,
173 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
174 connman_dbus_dict_append_basic(&dict, "WiFi.Passphrase",
175 DBUS_TYPE_STRING, &network->wifi.passphrase);
177 connman_dbus_dict_close(&array, &dict);
182 static GDBusMethodTable network_methods[] = {
183 { "GetProperties", "", "a{sv}", get_properties },
187 static GDBusSignalTable network_signals[] = {
188 { "PropertyChanged", "sv" },
192 static DBusConnection *connection;
194 static void append_networks(DBusMessageIter *iter, void *user_data)
196 struct connman_device *device = user_data;
198 __connman_element_list((struct connman_element *) device,
199 CONNMAN_ELEMENT_TYPE_NETWORK, iter);
202 static void emit_networks_signal(struct connman_device *device)
204 const char *path = connman_device_get_path(device);
206 connman_dbus_property_changed_array(path,
207 CONNMAN_DEVICE_INTERFACE, "Networks",
208 DBUS_TYPE_OBJECT_PATH, append_networks, device);
211 static int register_interface(struct connman_element *element)
213 struct connman_network *network = element->network;
215 DBG("element %p name %s", element, element->name);
217 if (g_dbus_register_interface(connection, element->path,
218 CONNMAN_NETWORK_INTERFACE,
219 network_methods, network_signals,
220 NULL, network, NULL) == FALSE) {
221 connman_error("Failed to register %s network", element->path);
225 network->registered = TRUE;
227 emit_networks_signal(network->device);
232 static void unregister_interface(struct connman_element *element)
234 struct connman_network * network = element->network;
236 DBG("element %p name %s", element, element->name);
238 network->registered = FALSE;
240 if (network->device != NULL)
241 emit_networks_signal(network->device);
243 g_dbus_unregister_interface(connection, element->path,
244 CONNMAN_NETWORK_INTERFACE);
247 connman_bool_t __connman_network_has_driver(struct connman_network *network)
249 if (network == NULL || network->driver == NULL)
252 return network->registered;
255 static GSList *driver_list = NULL;
257 static gint compare_priority(gconstpointer a, gconstpointer b)
259 const struct connman_network_driver *driver1 = a;
260 const struct connman_network_driver *driver2 = b;
262 return driver2->priority - driver1->priority;
266 * connman_network_driver_register:
267 * @driver: network driver definition
269 * Register a new network driver
271 * Returns: %0 on success
273 int connman_network_driver_register(struct connman_network_driver *driver)
275 DBG("driver %p name %s", driver, driver->name);
277 driver_list = g_slist_insert_sorted(driver_list, driver,
284 * connman_network_driver_unregister:
285 * @driver: network driver definition
287 * Remove a previously registered network driver
289 void connman_network_driver_unregister(struct connman_network_driver *driver)
291 DBG("driver %p name %s", driver, driver->name);
293 driver_list = g_slist_remove(driver_list, driver);
296 static void network_destruct(struct connman_element *element)
298 struct connman_network *network = element->network;
300 DBG("element %p name %s", element, element->name);
302 g_free(network->wifi.ssid);
303 g_free(network->wifi.mode);
304 g_free(network->wifi.security);
305 g_free(network->wifi.passphrase);
307 g_free(network->group);
308 g_free(network->node);
309 g_free(network->name);
310 g_free(network->address);
311 g_free(network->identifier);
313 if (network->ipconfig) {
314 connman_ipconfig_unref(network->ipconfig);
315 network->ipconfig = NULL;
318 network->device = NULL;
322 * connman_network_create:
323 * @identifier: network identifier (for example an unqiue name)
325 * Allocate a new network and assign the #identifier to it.
327 * Returns: a newly-allocated #connman_network structure
329 struct connman_network *connman_network_create(const char *identifier,
330 enum connman_network_type type)
332 struct connman_network *network;
333 connman_uint8_t strength = 0;
337 DBG("identifier %s type %d", identifier, type);
339 network = g_try_new0(struct connman_network, 1);
343 DBG("network %p", network);
345 __connman_element_initialize(&network->element);
347 //temp = connman_dbus_encode_string(identifier);
348 if (identifier == NULL) {
349 temp = g_strdup_printf("hidden_%d", hidden_counter++);
350 network->hidden = TRUE;
352 temp = g_strdup(identifier);
359 network->element.name = temp;
360 network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
362 network->element.network = network;
363 network->element.destruct = network_destruct;
365 str = type2string(type);
367 connman_element_set_string(&network->element, "Type", str);
369 connman_element_set_uint8(&network->element, "Strength", strength);
371 network->type = type;
372 network->identifier = g_strdup(temp);
378 * connman_network_ref:
379 * @network: network structure
381 * Increase reference counter of network
383 struct connman_network *connman_network_ref(struct connman_network *network)
385 if (connman_element_ref(&network->element) == NULL)
392 * connman_network_unref:
393 * @network: network structure
395 * Decrease reference counter of network
397 void connman_network_unref(struct connman_network *network)
399 connman_element_unref(&network->element);
402 const char *__connman_network_get_type(struct connman_network *network)
404 return type2string(network->type);
408 * connman_network_get_type:
409 * @network: network structure
411 * Get type of network
413 enum connman_network_type connman_network_get_type(struct connman_network *network)
415 return network->type;
419 * connman_network_get_identifier:
420 * @network: network structure
422 * Get identifier of network
424 const char *connman_network_get_identifier(struct connman_network *network)
426 return network->identifier;
430 * connman_network_get_path:
431 * @network: network structure
433 * Get path name of network
435 const char *connman_network_get_path(struct connman_network *network)
437 return network->element.path;
441 * connman_network_set_index:
442 * @network: network structure
443 * @index: index number
445 * Set index number of network
447 void connman_network_set_index(struct connman_network *network, int index)
449 struct connman_service *service;
450 struct connman_ipconfig *ipconfig;
452 service = __connman_service_lookup_from_network(network);
456 ipconfig = __connman_service_get_ipconfig(service);
458 if (network->element.index < 0 && ipconfig == NULL)
460 * This is needed for plugins that havent set their ipconfig
461 * layer yet, due to not being able to get a network index
462 * prior to creating a service.
464 __connman_service_create_ipconfig(service, index);
466 /* If index changed, the index of ipconfig must be reset. */
467 if (ipconfig == NULL)
470 __connman_ipconfig_set_index(ipconfig, index);
474 network->element.index = index;
478 * connman_network_get_index:
479 * @network: network structure
481 * Get index number of network
483 int connman_network_get_index(struct connman_network *network)
485 return network->element.index;
489 * connman_network_get_element:
490 * @network: network structure
492 * Get connman_element of network
494 struct connman_element *connman_network_get_element(
495 struct connman_network *network)
497 return &network->element;
501 * connman_network_set_protocol:
502 * @network: network structure
503 * @protocol: network protocol
505 * Change protocol of network
507 void connman_network_set_protocol(struct connman_network *network,
508 enum connman_network_protocol protocol)
510 network->protocol = protocol;
514 * connman_network_set_group:
515 * @network: network structure
518 * Set group name for automatic clustering
520 void connman_network_set_group(struct connman_network *network,
523 switch (network->type) {
524 case CONNMAN_NETWORK_TYPE_UNKNOWN:
525 case CONNMAN_NETWORK_TYPE_VENDOR:
527 case CONNMAN_NETWORK_TYPE_ETHERNET:
528 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
529 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
530 case CONNMAN_NETWORK_TYPE_CELLULAR:
531 case CONNMAN_NETWORK_TYPE_WIFI:
532 case CONNMAN_NETWORK_TYPE_WIMAX:
536 if (g_strcmp0(network->group, group) == 0) {
537 if (group != NULL && network->registered)
538 __connman_profile_update_network(network);
542 if (network->group != NULL) {
543 if (network->registered)
544 __connman_profile_remove_network(network);
546 g_free(network->group);
549 network->group = g_strdup(group);
551 if (network->group != NULL) {
552 if (network->registered)
553 __connman_profile_add_network(network);
558 * connman_network_get_group:
559 * @network: network structure
561 * Get group name for automatic clustering
563 const char *connman_network_get_group(struct connman_network *network)
565 return network->group;
568 const char *__connman_network_get_ident(struct connman_network *network)
570 if (network->device == NULL)
573 return __connman_device_get_ident(network->device);
576 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
578 switch (network->type) {
579 case CONNMAN_NETWORK_TYPE_UNKNOWN:
580 case CONNMAN_NETWORK_TYPE_VENDOR:
581 case CONNMAN_NETWORK_TYPE_ETHERNET:
582 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
583 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
584 case CONNMAN_NETWORK_TYPE_CELLULAR:
585 case CONNMAN_NETWORK_TYPE_WIMAX:
587 case CONNMAN_NETWORK_TYPE_WIFI:
588 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
590 if (network->strength > 0 && network->strength < 20)
598 connman_bool_t __connman_network_get_connecting(struct connman_network *network)
600 return network->connecting;
604 * connman_network_set_available:
605 * @network: network structure
606 * @available: availability state
608 * Change availability state of network (in range)
610 int connman_network_set_available(struct connman_network *network,
611 connman_bool_t available)
613 DBG("network %p available %d", network, available);
615 if (network->available == available)
618 network->available = available;
624 * connman_network_get_available:
625 * @network: network structure
627 * Get network available setting
629 connman_bool_t connman_network_get_available(struct connman_network *network)
631 if (network->hidden == TRUE)
634 return network->available;
638 * connman_network_set_associating:
639 * @network: network structure
640 * @associating: associating state
642 * Change associating state of network
644 int connman_network_set_associating(struct connman_network *network,
645 connman_bool_t associating)
647 DBG("network %p associating %d", network, associating);
649 if (network->associating == associating)
652 network->associating = associating;
654 if (associating == TRUE) {
655 struct connman_service *service;
657 service = __connman_service_lookup_from_network(network);
658 __connman_service_indicate_state(service,
659 CONNMAN_SERVICE_STATE_ASSOCIATION);
665 static void set_associate_error(struct connman_network *network)
667 struct connman_service *service;
669 if (network->associating == FALSE)
672 network->associating = FALSE;
674 service = __connman_service_lookup_from_network(network);
676 __connman_service_indicate_state(service,
677 CONNMAN_SERVICE_STATE_FAILURE);
680 static void set_configure_error(struct connman_network *network)
682 struct connman_service *service;
684 network->connecting = FALSE;
686 service = __connman_service_lookup_from_network(network);
688 __connman_service_indicate_state(service,
689 CONNMAN_SERVICE_STATE_FAILURE);
692 void connman_network_set_method(struct connman_network *network,
693 enum connman_ipconfig_method method)
695 struct connman_service *service;
696 struct connman_ipconfig *ipconfig;
698 network->element.ipv4.method = method;
700 service = __connman_service_lookup_from_network(network);
704 ipconfig = __connman_service_get_ipconfig(service);
705 if (ipconfig == NULL)
708 connman_ipconfig_set_method(ipconfig, method);
711 void connman_network_set_error(struct connman_network *network,
712 enum connman_network_error error)
714 DBG("nework %p, error %d", network, error);
716 network->connecting = FALSE;
719 case CONNMAN_NETWORK_ERROR_UNKNOWN:
721 case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
722 set_associate_error(network);
724 case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
725 set_configure_error(network);
730 static void set_configuration(struct connman_network *network)
732 struct connman_service *service;
734 DBG("network %p", network);
736 __connman_device_increase_connections(network->device);
738 __connman_device_set_network(network->device, network);
740 connman_device_set_disconnected(network->device, FALSE);
742 service = __connman_service_lookup_from_network(network);
743 __connman_service_indicate_state(service,
744 CONNMAN_SERVICE_STATE_CONFIGURATION);
747 static int set_connected_fixed(struct connman_network *network)
749 struct connman_service *service;
750 struct connman_element *parent, *element;
754 service = __connman_service_lookup_from_network(network);
756 parent = connman_network_get_element(network);
758 set_configuration(network);
760 if (parent->ipv4.address == NULL)
763 if (parent->ipv4.netmask == NULL)
766 element = connman_element_create(NULL);
767 if (element == NULL) {
768 connman_error("Can not create connman_element");
772 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
773 element->index = parent->index;
775 if (connman_element_register(element, parent) < 0) {
776 connman_error("Can not register connman_element");
780 network->connecting = FALSE;
782 connman_network_set_associating(network, FALSE);
787 static void set_connected_manual(struct connman_network *network)
789 struct connman_service *service;
790 struct connman_ipconfig *ipconfig;
791 const char *nameserver = NULL;
794 DBG("network %p", network);
796 service = __connman_service_lookup_from_network(network);
798 ipconfig = __connman_service_get_ipconfig(service);
800 set_configuration(network);
802 err = __connman_ipconfig_set_address(ipconfig);
804 connman_network_set_error(network,
805 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
809 connman_element_get_value(&network->element,
810 CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
811 if (nameserver != NULL)
812 __connman_service_append_nameserver(service, nameserver);
814 __connman_ipconfig_set_gateway(ipconfig, &network->element);
816 network->connecting = FALSE;
818 connman_network_set_associating(network, FALSE);
820 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
823 static int set_connected_dhcp(struct connman_network *network)
825 struct connman_element *element;
828 DBG("network %p", network);
830 if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
833 element = connman_element_create(NULL);
837 element->type = CONNMAN_ELEMENT_TYPE_DHCP;
838 element->index = network->element.index;
840 error = connman_element_register(element, &network->element);
842 connman_element_unref(element);
846 set_configuration(network);
851 static int manual_ipv6_set(struct connman_network *network,
852 struct connman_ipconfig *ipconfig_ipv6)
854 struct connman_service *service;
857 service = __connman_service_lookup_from_network(network);
861 err = __connman_ipconfig_set_address(ipconfig_ipv6);
863 connman_network_set_error(network,
864 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
869 * READY state will be indicated by IPV4 setting
870 * gateway will be set by IPV4 setting
876 static gboolean set_connected(gpointer user_data)
878 struct connman_network *network = user_data;
879 struct connman_service *service;
880 struct connman_ipconfig *ipconfig;
881 enum connman_ipconfig_method method;
883 service = __connman_service_lookup_from_network(network);
885 ipconfig = __connman_service_get_ipconfig(service);
887 method = __connman_ipconfig_get_method(ipconfig);
889 DBG("method %d", method);
891 if (network->connected == TRUE) {
892 enum connman_ipconfig_method ipv6_method;
893 struct connman_ipconfig *ipv6config;
896 ipv6config = connman_ipconfig_get_ipv6config(ipconfig);
897 ipv6_method = __connman_ipconfig_get_method(ipv6config);
898 switch (ipv6_method) {
899 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
900 case CONNMAN_IPCONFIG_METHOD_OFF:
902 case CONNMAN_IPCONFIG_METHOD_FIXED:
903 case CONNMAN_IPCONFIG_METHOD_MANUAL:
904 ret = manual_ipv6_set(network, ipv6config);
906 connman_network_set_error(network,
907 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
911 case CONNMAN_IPCONFIG_METHOD_DHCP:
916 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
917 case CONNMAN_IPCONFIG_METHOD_OFF:
919 case CONNMAN_IPCONFIG_METHOD_FIXED:
920 if (set_connected_fixed(network) < 0) {
921 connman_network_set_error(network,
922 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
926 case CONNMAN_IPCONFIG_METHOD_MANUAL:
927 set_connected_manual(network);
929 case CONNMAN_IPCONFIG_METHOD_DHCP:
930 if (set_connected_dhcp(network) < 0) {
931 connman_network_set_error(network,
932 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
938 struct connman_service *service;
940 connman_element_unregister_children(&network->element);
942 __connman_device_set_network(network->device, NULL);
943 network->hidden = FALSE;
945 __connman_device_decrease_connections(network->device);
947 service = __connman_service_lookup_from_network(network);
949 __connman_service_indicate_state(service,
950 CONNMAN_SERVICE_STATE_IDLE);
953 network->connecting = FALSE;
955 connman_network_set_associating(network, FALSE);
961 * connman_network_set_connected:
962 * @network: network structure
963 * @connected: connected state
965 * Change connected state of network
967 int connman_network_set_connected(struct connman_network *network,
968 connman_bool_t connected)
970 DBG("network %p connected %d", network, connected);
972 if ((network->connecting == TRUE || network->associating == TRUE) &&
973 connected == FALSE) {
974 connman_element_set_error(&network->element,
975 CONNMAN_ELEMENT_ERROR_CONNECT_FAILED);
976 __connman_network_disconnect(network);
979 if (network->connected == connected)
982 network->connected = connected;
984 if (network->registered == FALSE) {
985 g_idle_add(set_connected, network);
989 connman_dbus_property_changed_basic(network->element.path,
990 CONNMAN_NETWORK_INTERFACE, "Connected",
991 DBUS_TYPE_BOOLEAN, &connected);
993 set_connected(network);
999 * connman_network_get_connected:
1000 * @network: network structure
1002 * Get network connection status
1004 connman_bool_t connman_network_get_connected(struct connman_network *network)
1006 return network->connected;
1010 * connman_network_get_associating:
1011 * @network: network structure
1013 * Get network associating status
1015 connman_bool_t connman_network_get_associating(struct connman_network *network)
1017 return network->associating;
1021 * __connman_network_connect:
1022 * @network: network structure
1026 int __connman_network_connect(struct connman_network *network)
1028 struct connman_service *service;
1031 DBG("network %p", network);
1033 if (network->connected == TRUE)
1036 if (network->connecting == TRUE || network->associating == TRUE)
1039 if (network->driver == NULL)
1042 if (network->driver->connect == NULL)
1045 __connman_device_disconnect(network->device);
1047 network->connecting = TRUE;
1049 service = __connman_service_lookup_from_network(network);
1051 err = network->driver->connect(network);
1053 if (err == -EINPROGRESS)
1054 connman_network_set_associating(network, TRUE);
1056 network->connecting = FALSE;
1057 network->hidden = FALSE;
1063 network->connected = TRUE;
1064 set_connected(network);
1070 * __connman_network_disconnect:
1071 * @network: network structure
1073 * Disconnect network
1075 int __connman_network_disconnect(struct connman_network *network)
1079 DBG("network %p", network);
1081 if (network->connected == FALSE && network->connecting == FALSE &&
1082 network->associating == FALSE)
1085 if (network->driver == NULL)
1088 if (network->driver->disconnect == NULL)
1091 network->connecting = FALSE;
1093 err = network->driver->disconnect(network);
1095 connman_network_set_connected(network, FALSE);
1096 set_connected(network);
1102 static int dhcp_start(struct connman_network *network)
1104 struct connman_element *element;
1107 if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
1110 element = connman_element_create(NULL);
1111 if (element == NULL)
1114 element->type = CONNMAN_ELEMENT_TYPE_DHCP;
1115 element->index = network->element.index;
1117 error = connman_element_register(element, &network->element);
1119 connman_element_unref(element);
1126 static int dhcp_stop(struct connman_network *network)
1128 if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
1131 connman_element_unregister_children_type(&network->element,
1132 CONNMAN_ELEMENT_TYPE_CONNECTION);
1133 connman_element_unregister_children_type(&network->element,
1134 CONNMAN_ELEMENT_TYPE_IPV4);
1135 connman_element_unregister_children_type(&network->element,
1136 CONNMAN_ELEMENT_TYPE_DHCP);
1141 static int manual_ipv4_set(struct connman_network *network,
1142 struct connman_ipconfig *ipconfig)
1144 struct connman_service *service;
1147 service = __connman_service_lookup_from_network(network);
1148 if (service == NULL)
1151 err = __connman_ipconfig_set_address(ipconfig);
1153 connman_network_set_error(network,
1154 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1158 __connman_ipconfig_set_gateway(ipconfig, &network->element);
1160 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
1165 int __connman_network_clear_ipconfig(struct connman_network *network,
1166 struct connman_ipconfig *ipconfig)
1168 struct connman_service *service;
1169 enum connman_ipconfig_method method;
1171 service = __connman_service_lookup_from_network(network);
1172 if (service == NULL)
1175 method = __connman_ipconfig_get_method(ipconfig);
1178 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1179 case CONNMAN_IPCONFIG_METHOD_OFF:
1180 case CONNMAN_IPCONFIG_METHOD_FIXED:
1182 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1183 connman_element_unregister_children_type(&network->element,
1184 CONNMAN_ELEMENT_TYPE_CONNECTION);
1185 __connman_ipconfig_clear_address(ipconfig);
1187 case CONNMAN_IPCONFIG_METHOD_DHCP:
1192 __connman_service_indicate_state(service,
1193 CONNMAN_SERVICE_STATE_CONFIGURATION);
1198 int __connman_network_set_ipconfig(struct connman_network *network,
1199 struct connman_ipconfig *ipconfig)
1201 struct connman_ipconfig *ipv6config;
1202 enum connman_ipconfig_method method;
1205 ipv6config = connman_ipconfig_get_ipv6config(ipconfig);
1206 method = __connman_ipconfig_get_method(ipv6config);
1208 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1209 case CONNMAN_IPCONFIG_METHOD_OFF:
1211 case CONNMAN_IPCONFIG_METHOD_FIXED:
1212 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1213 ret = manual_ipv6_set(network, ipv6config);
1215 connman_network_set_error(network,
1216 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1220 case CONNMAN_IPCONFIG_METHOD_DHCP:
1224 method = __connman_ipconfig_get_method(ipconfig);
1227 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1228 case CONNMAN_IPCONFIG_METHOD_OFF:
1229 case CONNMAN_IPCONFIG_METHOD_FIXED:
1231 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1232 return manual_ipv4_set(network, ipconfig);
1233 case CONNMAN_IPCONFIG_METHOD_DHCP:
1234 return dhcp_start(network);
1241 * connman_network_set_address:
1242 * @network: network structure
1243 * @address: binary address value
1244 * @size: binary address length
1246 * Set unique address value for network
1248 int connman_network_set_address(struct connman_network *network,
1249 const void *address, unsigned int size)
1251 const unsigned char *addr_octet = address;
1254 DBG("network %p size %d", network, size);
1259 str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
1260 addr_octet[0], addr_octet[1], addr_octet[2],
1261 addr_octet[3], addr_octet[4], addr_octet[5]);
1265 g_free(network->address);
1266 network->address = str;
1268 return connman_element_set_string(&network->element,
1269 "Address", network->address);
1273 * connman_network_set_name:
1274 * @network: network structure
1277 * Set display name value for network
1279 int connman_network_set_name(struct connman_network *network,
1282 DBG("network %p name %s", network, name);
1284 g_free(network->name);
1285 network->name = g_strdup(name);
1287 return connman_element_set_string(&network->element, "Name", name);
1291 * connman_network_set_strength:
1292 * @network: network structure
1293 * @strength: strength value
1295 * Set signal strength value for network
1297 int connman_network_set_strength(struct connman_network *network,
1298 connman_uint8_t strength)
1300 DBG("network %p strengh %d", network, strength);
1302 network->strength = strength;
1304 return connman_element_set_uint8(&network->element,
1305 "Strength", strength);
1309 * connman_network_set_roaming:
1310 * @network: network structure
1311 * @roaming: roaming state
1313 * Set roaming state for network
1315 int connman_network_set_roaming(struct connman_network *network,
1316 connman_bool_t roaming)
1318 DBG("network %p roaming %d", network, roaming);
1320 network->roaming = roaming;
1322 return connman_element_set_bool(&network->element,
1323 "Roaming", roaming);
1327 * connman_network_set_string:
1328 * @network: network structure
1329 * @key: unique identifier
1330 * @value: string value
1332 * Set string value for specific key
1334 int connman_network_set_string(struct connman_network *network,
1335 const char *key, const char *value)
1339 DBG("network %p key %s value %s", network, key, value);
1341 if (g_strcmp0(key, "Name") == 0)
1342 return connman_network_set_name(network, value);
1344 if (g_str_equal(key, "Address") == TRUE) {
1345 g_free(network->address);
1346 network->address = g_strdup(value);
1347 } else if (g_str_equal(key, "Node") == TRUE) {
1348 g_free(network->node);
1349 network->node = g_strdup(value);
1350 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1351 g_free(network->wifi.mode);
1352 network->wifi.mode = g_strdup(value);
1353 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1354 g_free(network->wifi.security);
1355 network->wifi.security = g_strdup(value);
1356 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1357 g_free(network->wifi.passphrase);
1358 network->wifi.passphrase = g_strdup(value);
1359 } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1360 g_free(network->wifi.eap);
1361 network->wifi.eap = g_strdup(value);
1362 } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1363 g_free(network->wifi.identity);
1364 network->wifi.identity = g_strdup(value);
1365 } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1366 g_free(network->wifi.ca_cert_path);
1367 network->wifi.ca_cert_path = g_strdup(value);
1368 } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1369 g_free(network->wifi.client_cert_path);
1370 network->wifi.client_cert_path = g_strdup(value);
1371 } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1372 g_free(network->wifi.private_key_path);
1373 network->wifi.private_key_path = g_strdup(value);
1374 } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1375 g_free(network->wifi.private_key_passphrase);
1376 network->wifi.private_key_passphrase = g_strdup(value);
1377 } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1378 g_free(network->wifi.phase2_auth);
1379 network->wifi.phase2_auth = g_strdup(value);
1382 err = connman_element_set_string(&network->element, key, value);
1386 if (network->driver == NULL)
1389 if (network->driver->setup)
1390 return network->driver->setup(network, key);
1396 * connman_network_get_string:
1397 * @network: network structure
1398 * @key: unique identifier
1400 * Get string value for specific key
1402 const char *connman_network_get_string(struct connman_network *network,
1405 DBG("network %p key %s", network, key);
1407 if (g_str_equal(key, "Address") == TRUE)
1408 return network->address;
1409 else if (g_str_equal(key, "Name") == TRUE)
1410 return network->name;
1411 else if (g_str_equal(key, "Node") == TRUE)
1412 return network->node;
1413 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1414 return network->wifi.mode;
1415 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1416 return network->wifi.security;
1417 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1418 return network->wifi.passphrase;
1419 else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1420 return network->wifi.eap;
1421 else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1422 return network->wifi.identity;
1423 else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1424 return network->wifi.ca_cert_path;
1425 else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1426 return network->wifi.client_cert_path;
1427 else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1428 return network->wifi.private_key_path;
1429 else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1430 return network->wifi.private_key_passphrase;
1431 else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1432 return network->wifi.phase2_auth;
1434 return connman_element_get_string(&network->element, key);
1438 * connman_network_set_bool:
1439 * @network: network structure
1440 * @key: unique identifier
1441 * @value: boolean value
1443 * Set boolean value for specific key
1445 int connman_network_set_bool(struct connman_network *network,
1446 const char *key, connman_bool_t value)
1448 DBG("network %p key %s value %d", network, key, value);
1450 if (g_strcmp0(key, "Roaming") == 0)
1451 return connman_network_set_roaming(network, value);
1453 return connman_element_set_bool(&network->element, key, value);
1457 * connman_network_get_bool:
1458 * @network: network structure
1459 * @key: unique identifier
1461 * Get boolean value for specific key
1463 connman_bool_t connman_network_get_bool(struct connman_network *network,
1466 DBG("network %p key %s", network, key);
1468 if (g_str_equal(key, "Roaming") == TRUE)
1469 return network->roaming;
1471 return connman_element_get_bool(&network->element, key);
1475 * connman_network_set_uint8:
1476 * @network: network structure
1477 * @key: unique identifier
1478 * @value: integer value
1480 * Set integer value for specific key
1482 int connman_network_set_uint8(struct connman_network *network,
1483 const char *key, connman_uint8_t value)
1485 DBG("network %p key %s value %d", network, key, value);
1487 if (g_strcmp0(key, "Strength") == 0)
1488 return connman_network_set_strength(network, value);
1490 return connman_element_set_uint8(&network->element, key, value);
1494 * connman_network_get_uint8:
1495 * @network: network structure
1496 * @key: unique identifier
1498 * Get integer value for specific key
1500 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1503 DBG("network %p key %s", network, key);
1505 if (g_str_equal(key, "Strength") == TRUE)
1506 return network->strength;
1508 return connman_element_get_uint8(&network->element, key);
1512 * connman_network_set_uint16:
1513 * @network: network structure
1514 * @key: unique identifier
1515 * @value: integer value
1517 * Set integer value for specific key
1519 int connman_network_set_uint16(struct connman_network *network,
1520 const char *key, connman_uint16_t value)
1522 DBG("network %p key %s value %d", network, key, value);
1524 if (g_str_equal(key, "Frequency") == TRUE)
1525 network->frequency = value;
1526 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1527 network->wifi.channel = value;
1533 * connman_network_get_uint16:
1534 * @network: network structure
1535 * @key: unique identifier
1537 * Get integer value for specific key
1539 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1542 DBG("network %p key %s", network, key);
1544 if (g_str_equal(key, "Frequency") == TRUE)
1545 return network->frequency;
1546 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1547 return network->wifi.channel;
1553 * connman_network_set_blob:
1554 * @network: network structure
1555 * @key: unique identifier
1559 * Set binary blob value for specific key
1561 int connman_network_set_blob(struct connman_network *network,
1562 const char *key, const void *data, unsigned int size)
1564 DBG("network %p key %s size %d", network, key, size);
1566 if (g_strcmp0(key, "Address") == 0)
1567 return connman_network_set_address(network, data, size);
1569 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1570 g_free(network->wifi.ssid);
1571 network->wifi.ssid = g_try_malloc(size);
1572 if (network->wifi.ssid != NULL) {
1573 memcpy(network->wifi.ssid, data, size);
1574 network->wifi.ssid_len = size;
1576 network->wifi.ssid_len = 0;
1579 return connman_element_set_blob(&network->element, key, data, size);
1583 * connman_network_get_blob:
1584 * @network: network structure
1585 * @key: unique identifier
1586 * @size: pointer to blob size
1588 * Get binary blob value for specific key
1590 const void *connman_network_get_blob(struct connman_network *network,
1591 const char *key, unsigned int *size)
1593 DBG("network %p key %s", network, key);
1595 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1597 *size = network->wifi.ssid_len;
1598 return network->wifi.ssid;
1601 return connman_element_get_blob(&network->element, key, size);
1604 void __connman_network_set_device(struct connman_network *network,
1605 struct connman_device *device)
1607 network->device = device;
1611 * connman_network_get_device:
1612 * @network: network structure
1614 * Get parent device of network
1616 struct connman_device *connman_network_get_device(struct connman_network *network)
1618 return network->device;
1622 * connman_network_get_data:
1623 * @network: network structure
1625 * Get private network data pointer
1627 void *connman_network_get_data(struct connman_network *network)
1629 return network->driver_data;
1633 * connman_network_set_data:
1634 * @network: network structure
1635 * @data: data pointer
1637 * Set private network data pointer
1639 void connman_network_set_data(struct connman_network *network, void *data)
1641 network->driver_data = data;
1644 void connman_network_update(struct connman_network *network)
1646 switch (network->type) {
1647 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1648 case CONNMAN_NETWORK_TYPE_VENDOR:
1650 case CONNMAN_NETWORK_TYPE_ETHERNET:
1651 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1652 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1653 case CONNMAN_NETWORK_TYPE_CELLULAR:
1654 case CONNMAN_NETWORK_TYPE_WIFI:
1655 case CONNMAN_NETWORK_TYPE_WIMAX:
1659 if (network->group != NULL)
1660 __connman_service_update_from_network(network);
1665 static gboolean match_driver(struct connman_network *network,
1666 struct connman_network_driver *driver)
1668 if (network->type == driver->type ||
1669 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1675 static int network_probe(struct connman_element *element)
1677 struct connman_network *network = element->network;
1681 DBG("element %p name %s", element, element->name);
1683 if (network == NULL)
1686 for (list = driver_list; list; list = list->next) {
1687 struct connman_network_driver *driver = list->data;
1689 if (match_driver(network, driver) == FALSE)
1692 DBG("driver %p name %s", driver, driver->name);
1694 if (driver->probe(network) == 0) {
1695 network->driver = driver;
1700 if (network->driver == NULL)
1703 err = register_interface(element);
1705 if (network->driver->remove)
1706 network->driver->remove(network);
1710 switch (network->type) {
1711 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1712 case CONNMAN_NETWORK_TYPE_VENDOR:
1714 case CONNMAN_NETWORK_TYPE_ETHERNET:
1715 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1716 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1717 case CONNMAN_NETWORK_TYPE_CELLULAR:
1718 case CONNMAN_NETWORK_TYPE_WIFI:
1719 case CONNMAN_NETWORK_TYPE_WIMAX:
1720 if (network->group != NULL)
1721 __connman_profile_add_network(network);
1728 static void network_remove(struct connman_element *element)
1730 struct connman_network *network = element->network;
1732 DBG("element %p name %s", element, element->name);
1734 if (network == NULL)
1737 if (network->driver == NULL)
1740 switch (network->type) {
1741 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1742 case CONNMAN_NETWORK_TYPE_VENDOR:
1744 case CONNMAN_NETWORK_TYPE_ETHERNET:
1745 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1746 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1747 case CONNMAN_NETWORK_TYPE_CELLULAR:
1748 case CONNMAN_NETWORK_TYPE_WIFI:
1749 case CONNMAN_NETWORK_TYPE_WIMAX:
1750 if (network->group != NULL) {
1751 if (network->registered)
1752 __connman_profile_remove_network(network);
1754 g_free(network->group);
1755 network->group = NULL;
1760 unregister_interface(element);
1762 if (network->driver->remove)
1763 network->driver->remove(network);
1766 static void network_change(struct connman_element *element)
1768 struct connman_network *network = element->network;
1770 DBG("element %p name %s", element, element->name);
1772 if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1775 if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1778 if (network->connected == FALSE)
1781 connman_element_unregister_children(element);
1783 connman_device_set_disconnected(network->device, TRUE);
1785 if (network->driver && network->driver->disconnect) {
1786 network->driver->disconnect(network);
1790 network->connected = FALSE;
1793 static struct connman_driver network_driver = {
1795 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
1796 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1797 .probe = network_probe,
1798 .remove = network_remove,
1799 .change = network_change,
1802 int __connman_network_init(void)
1806 connection = connman_dbus_get_connection();
1808 return connman_driver_register(&network_driver);
1811 void __connman_network_cleanup(void)
1815 connman_driver_unregister(&network_driver);
1817 dbus_connection_unref(connection);