5 * Copyright (C) 2007-2012 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 #include <connman/storage.h>
34 #include <connman/setting.h>
38 #define CONNECT_TIMEOUT 120
40 static DBusConnection *connection = NULL;
42 static GSequence *service_list = NULL;
43 static GHashTable *service_hash = NULL;
44 static GSList *counter_list = NULL;
45 static unsigned int autoconnect_timeout = 0;
46 static struct connman_service *current_default = NULL;
48 struct connman_stats {
50 connman_bool_t enabled;
51 struct connman_stats_data data_last;
52 struct connman_stats_data data;
56 struct connman_stats_counter {
57 connman_bool_t append_all;
58 struct connman_stats stats;
59 struct connman_stats stats_roaming;
62 struct connman_service {
64 int session_usage_count;
67 enum connman_service_type type;
68 enum connman_service_security security;
69 enum connman_service_state state;
70 enum connman_service_state state_ipv4;
71 enum connman_service_state state_ipv6;
72 enum connman_service_error error;
73 connman_uint8_t strength;
74 connman_bool_t favorite;
75 connman_bool_t immutable;
76 connman_bool_t hidden;
77 connman_bool_t ignore;
78 connman_bool_t autoconnect;
79 connman_bool_t userconnect;
84 char *agent_passphrase;
85 connman_bool_t roaming;
86 struct connman_ipconfig *ipconfig_ipv4;
87 struct connman_ipconfig *ipconfig_ipv6;
88 struct connman_network *network;
89 struct connman_provider *provider;
91 char **nameservers_config;
92 char **nameservers_auto;
96 char **timeservers_config;
97 /* 802.1x settings from the config files */
100 char *agent_identity;
102 char *client_cert_file;
103 char *private_key_file;
104 char *private_key_passphrase;
106 DBusMessage *pending;
108 struct connman_stats stats;
109 struct connman_stats stats_roaming;
110 GHashTable *counter_table;
111 enum connman_service_proxy_method proxy;
112 enum connman_service_proxy_method proxy_config;
117 int online_check_count;
118 connman_bool_t do_split_routing;
119 connman_bool_t new_service;
124 struct connman_service *service;
127 static void compare_path(gpointer value, gpointer user_data)
129 struct connman_service *service = value;
130 struct find_data *data = user_data;
132 if (data->service != NULL)
135 if (g_strcmp0(service->path, data->path) == 0)
136 data->service = service;
139 static struct connman_service *find_service(const char *path)
141 struct find_data data = { .path = path, .service = NULL };
143 DBG("path %s", path);
145 g_sequence_foreach(service_list, compare_path, &data);
150 const char *__connman_service_type2string(enum connman_service_type type)
153 case CONNMAN_SERVICE_TYPE_UNKNOWN:
155 case CONNMAN_SERVICE_TYPE_SYSTEM:
157 case CONNMAN_SERVICE_TYPE_ETHERNET:
159 case CONNMAN_SERVICE_TYPE_WIFI:
161 case CONNMAN_SERVICE_TYPE_WIMAX:
163 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
165 case CONNMAN_SERVICE_TYPE_CELLULAR:
167 case CONNMAN_SERVICE_TYPE_GPS:
169 case CONNMAN_SERVICE_TYPE_VPN:
171 case CONNMAN_SERVICE_TYPE_GADGET:
178 enum connman_service_type __connman_service_string2type(const char *str)
181 return CONNMAN_SERVICE_TYPE_UNKNOWN;
183 if (strcmp(str, "ethernet") == 0)
184 return CONNMAN_SERVICE_TYPE_ETHERNET;
185 if (strcmp(str, "gadget") == 0)
186 return CONNMAN_SERVICE_TYPE_GADGET;
187 if (strcmp(str, "wifi") == 0)
188 return CONNMAN_SERVICE_TYPE_WIFI;
189 if (strcmp(str, "cellular") == 0)
190 return CONNMAN_SERVICE_TYPE_CELLULAR;
191 if (strcmp(str, "bluetooth") == 0)
192 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
193 if (strcmp(str, "wimax") == 0)
194 return CONNMAN_SERVICE_TYPE_WIMAX;
195 if (strcmp(str, "vpn") == 0)
196 return CONNMAN_SERVICE_TYPE_VPN;
197 if (strcmp(str, "gps") == 0)
198 return CONNMAN_SERVICE_TYPE_GPS;
199 if (strcmp(str, "system") == 0)
200 return CONNMAN_SERVICE_TYPE_SYSTEM;
202 return CONNMAN_SERVICE_TYPE_UNKNOWN;
205 static const char *security2string(enum connman_service_security security)
208 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
210 case CONNMAN_SERVICE_SECURITY_NONE:
212 case CONNMAN_SERVICE_SECURITY_WEP:
214 case CONNMAN_SERVICE_SECURITY_PSK:
215 case CONNMAN_SERVICE_SECURITY_WPA:
216 case CONNMAN_SERVICE_SECURITY_RSN:
218 case CONNMAN_SERVICE_SECURITY_8021X:
225 static const char *state2string(enum connman_service_state state)
228 case CONNMAN_SERVICE_STATE_UNKNOWN:
230 case CONNMAN_SERVICE_STATE_IDLE:
232 case CONNMAN_SERVICE_STATE_ASSOCIATION:
233 return "association";
234 case CONNMAN_SERVICE_STATE_CONFIGURATION:
235 return "configuration";
236 case CONNMAN_SERVICE_STATE_READY:
238 case CONNMAN_SERVICE_STATE_ONLINE:
240 case CONNMAN_SERVICE_STATE_DISCONNECT:
242 case CONNMAN_SERVICE_STATE_FAILURE:
249 static const char *error2string(enum connman_service_error error)
252 case CONNMAN_SERVICE_ERROR_UNKNOWN:
254 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
255 return "out-of-range";
256 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
257 return "pin-missing";
258 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
259 return "dhcp-failed";
260 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
261 return "connect-failed";
262 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
263 return "login-failed";
264 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
265 return "auth-failed";
266 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
267 return "invalid-key";
273 static enum connman_service_error string2error(const char *error)
275 if (g_strcmp0(error, "dhcp-failed") == 0)
276 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
277 else if (g_strcmp0(error, "pin-missing") == 0)
278 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
279 else if (g_strcmp0(error, "invalid-key") == 0)
280 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
282 return CONNMAN_SERVICE_ERROR_UNKNOWN;
285 static const char *proxymethod2string(enum connman_service_proxy_method method)
288 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
290 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
292 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
294 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
301 static enum connman_service_proxy_method string2proxymethod(const char *method)
303 if (g_strcmp0(method, "direct") == 0)
304 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
305 else if (g_strcmp0(method, "auto") == 0)
306 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
307 else if (g_strcmp0(method, "manual") == 0)
308 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
310 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
313 static int service_load(struct connman_service *service)
316 GError *error = NULL;
319 connman_bool_t autoconnect;
320 unsigned int ssid_len;
323 DBG("service %p", service);
325 keyfile = connman_storage_load_service(service->identifier);
326 if (keyfile == NULL) {
327 service->new_service = TRUE;
330 service->new_service = FALSE;
332 switch (service->type) {
333 case CONNMAN_SERVICE_TYPE_UNKNOWN:
334 case CONNMAN_SERVICE_TYPE_SYSTEM:
335 case CONNMAN_SERVICE_TYPE_GPS:
336 case CONNMAN_SERVICE_TYPE_GADGET:
338 case CONNMAN_SERVICE_TYPE_VPN:
339 service->do_split_routing = g_key_file_get_boolean(keyfile,
340 service->identifier, "SplitRouting", NULL);
342 case CONNMAN_SERVICE_TYPE_WIFI:
343 if (service->name == NULL) {
346 name = g_key_file_get_string(keyfile,
347 service->identifier, "Name", NULL);
349 g_free(service->name);
350 service->name = name;
353 if (service->network != NULL)
354 connman_network_set_name(service->network,
358 if (service->network &&
359 connman_network_get_blob(service->network,
360 "WiFi.SSID", &ssid_len) == NULL) {
363 hex_ssid = g_key_file_get_string(keyfile,
367 if (hex_ssid != NULL) {
369 unsigned int i, j = 0, hex;
370 size_t hex_ssid_len = strlen(hex_ssid);
372 ssid = g_try_malloc0(hex_ssid_len / 2);
379 for (i = 0; i < hex_ssid_len; i += 2) {
380 sscanf(hex_ssid + i, "%02x", &hex);
384 connman_network_set_blob(service->network,
385 "WiFi.SSID", ssid, hex_ssid_len / 2);
392 case CONNMAN_SERVICE_TYPE_WIMAX:
393 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
394 case CONNMAN_SERVICE_TYPE_CELLULAR:
395 service->favorite = g_key_file_get_boolean(keyfile,
396 service->identifier, "Favorite", NULL);
398 str = g_key_file_get_string(keyfile,
399 service->identifier, "Failure", NULL);
401 if (service->favorite == FALSE)
402 service->state_ipv4 = service->state_ipv6 =
403 CONNMAN_SERVICE_STATE_FAILURE;
404 service->error = string2error(str);
409 case CONNMAN_SERVICE_TYPE_ETHERNET:
410 autoconnect = g_key_file_get_boolean(keyfile,
411 service->identifier, "AutoConnect", &error);
413 service->autoconnect = autoconnect;
414 g_clear_error(&error);
418 str = g_key_file_get_string(keyfile,
419 service->identifier, "Modified", NULL);
421 g_time_val_from_iso8601(str, &service->modified);
425 str = g_key_file_get_string(keyfile,
426 service->identifier, "Passphrase", NULL);
428 g_free(service->passphrase);
429 service->passphrase = str;
432 if (service->ipconfig_ipv4 != NULL)
433 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
434 service->identifier, "IPv4.");
436 if (service->ipconfig_ipv6 != NULL)
437 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
438 service->identifier, "IPv6.");
440 service->nameservers_config = g_key_file_get_string_list(keyfile,
441 service->identifier, "Nameservers", &length, NULL);
442 if (service->nameservers_config != NULL && length == 0) {
443 g_strfreev(service->nameservers_config);
444 service->nameservers_config = NULL;
447 service->timeservers_config = g_key_file_get_string_list(keyfile,
448 service->identifier, "Timeservers", &length, NULL);
449 if (service->timeservers_config != NULL && length == 0) {
450 g_strfreev(service->timeservers_config);
451 service->timeservers_config = NULL;
454 service->domains = g_key_file_get_string_list(keyfile,
455 service->identifier, "Domains", &length, NULL);
456 if (service->domains != NULL && length == 0) {
457 g_strfreev(service->domains);
458 service->domains = NULL;
461 str = g_key_file_get_string(keyfile,
462 service->identifier, "Proxy.Method", NULL);
464 service->proxy_config = string2proxymethod(str);
468 service->proxies = g_key_file_get_string_list(keyfile,
469 service->identifier, "Proxy.Servers", &length, NULL);
470 if (service->proxies != NULL && length == 0) {
471 g_strfreev(service->proxies);
472 service->proxies = NULL;
475 service->excludes = g_key_file_get_string_list(keyfile,
476 service->identifier, "Proxy.Excludes", &length, NULL);
477 if (service->excludes != NULL && length == 0) {
478 g_strfreev(service->excludes);
479 service->excludes = NULL;
482 str = g_key_file_get_string(keyfile,
483 service->identifier, "Proxy.URL", NULL);
485 g_free(service->pac);
490 g_key_file_free(keyfile);
495 static int service_save(struct connman_service *service)
500 const char *cst_str = NULL;
503 DBG("service %p new %d", service, service->new_service);
505 if (service->new_service == TRUE)
508 keyfile = __connman_storage_open_service(service->identifier);
512 if (service->name != NULL)
513 g_key_file_set_string(keyfile, service->identifier,
514 "Name", service->name);
516 switch (service->type) {
517 case CONNMAN_SERVICE_TYPE_UNKNOWN:
518 case CONNMAN_SERVICE_TYPE_SYSTEM:
519 case CONNMAN_SERVICE_TYPE_GPS:
520 case CONNMAN_SERVICE_TYPE_GADGET:
522 case CONNMAN_SERVICE_TYPE_VPN:
523 g_key_file_set_boolean(keyfile, service->identifier,
524 "SplitRouting", service->do_split_routing);
526 case CONNMAN_SERVICE_TYPE_WIFI:
527 if (service->network) {
528 const unsigned char *ssid;
529 unsigned int ssid_len = 0;
531 ssid = connman_network_get_blob(service->network,
532 "WiFi.SSID", &ssid_len);
534 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
535 char *identifier = service->identifier;
539 str = g_string_sized_new(ssid_len * 2);
545 for (i = 0; i < ssid_len; i++)
546 g_string_append_printf(str,
549 g_key_file_set_string(keyfile, identifier,
552 g_string_free(str, TRUE);
555 freq = connman_network_get_frequency(service->network);
556 g_key_file_set_integer(keyfile, service->identifier,
561 case CONNMAN_SERVICE_TYPE_WIMAX:
562 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
563 case CONNMAN_SERVICE_TYPE_CELLULAR:
564 g_key_file_set_boolean(keyfile, service->identifier,
565 "Favorite", service->favorite);
567 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
568 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
569 const char *failure = error2string(service->error);
571 g_key_file_set_string(keyfile,
575 g_key_file_remove_key(keyfile, service->identifier,
580 case CONNMAN_SERVICE_TYPE_ETHERNET:
581 if (service->favorite == TRUE)
582 g_key_file_set_boolean(keyfile, service->identifier,
583 "AutoConnect", service->autoconnect);
587 str = g_time_val_to_iso8601(&service->modified);
589 g_key_file_set_string(keyfile, service->identifier,
594 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
595 g_key_file_set_string(keyfile, service->identifier,
596 "Passphrase", service->passphrase);
598 g_key_file_remove_key(keyfile, service->identifier,
601 if (service->ipconfig_ipv4 != NULL)
602 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
603 service->identifier, "IPv4.");
605 if (service->ipconfig_ipv6 != NULL)
606 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
607 service->identifier, "IPv6.");
609 if (service->nameservers_config != NULL) {
610 guint len = g_strv_length(service->nameservers_config);
612 g_key_file_set_string_list(keyfile, service->identifier,
614 (const gchar **) service->nameservers_config, len);
616 g_key_file_remove_key(keyfile, service->identifier,
617 "Nameservers", NULL);
619 if (service->timeservers_config != NULL) {
620 guint len = g_strv_length(service->timeservers_config);
622 g_key_file_set_string_list(keyfile, service->identifier,
624 (const gchar **) service->timeservers_config, len);
626 g_key_file_remove_key(keyfile, service->identifier,
627 "Timeservers", NULL);
629 if (service->domains != NULL) {
630 guint len = g_strv_length(service->domains);
632 g_key_file_set_string_list(keyfile, service->identifier,
634 (const gchar **) service->domains, len);
636 g_key_file_remove_key(keyfile, service->identifier,
639 cst_str = proxymethod2string(service->proxy_config);
641 g_key_file_set_string(keyfile, service->identifier,
642 "Proxy.Method", cst_str);
644 if (service->proxies != NULL) {
645 guint len = g_strv_length(service->proxies);
647 g_key_file_set_string_list(keyfile, service->identifier,
649 (const gchar **) service->proxies, len);
651 g_key_file_remove_key(keyfile, service->identifier,
652 "Proxy.Servers", NULL);
654 if (service->excludes != NULL) {
655 guint len = g_strv_length(service->excludes);
657 g_key_file_set_string_list(keyfile, service->identifier,
659 (const gchar **) service->excludes, len);
661 g_key_file_remove_key(keyfile, service->identifier,
662 "Proxy.Excludes", NULL);
664 if (service->pac != NULL && strlen(service->pac) > 0)
665 g_key_file_set_string(keyfile, service->identifier,
666 "Proxy.URL", service->pac);
668 g_key_file_remove_key(keyfile, service->identifier,
672 __connman_storage_save_service(keyfile, service->identifier);
674 g_key_file_free(keyfile);
679 static enum connman_service_state combine_state(
680 enum connman_service_state state_a,
681 enum connman_service_state state_b)
683 enum connman_service_state result;
685 if (state_a == state_b) {
690 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
695 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
700 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
705 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
710 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
715 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
720 if (state_a == CONNMAN_SERVICE_STATE_READY) {
725 if (state_b == CONNMAN_SERVICE_STATE_READY) {
730 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
735 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
740 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
745 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
750 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
755 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
760 result = CONNMAN_SERVICE_STATE_FAILURE;
766 static connman_bool_t is_connecting_state(struct connman_service *service,
767 enum connman_service_state state)
770 case CONNMAN_SERVICE_STATE_UNKNOWN:
771 case CONNMAN_SERVICE_STATE_IDLE:
772 case CONNMAN_SERVICE_STATE_FAILURE:
773 if (service->network != NULL)
774 return connman_network_get_connecting(service->network);
775 case CONNMAN_SERVICE_STATE_DISCONNECT:
776 case CONNMAN_SERVICE_STATE_READY:
777 case CONNMAN_SERVICE_STATE_ONLINE:
779 case CONNMAN_SERVICE_STATE_ASSOCIATION:
780 case CONNMAN_SERVICE_STATE_CONFIGURATION:
787 static connman_bool_t is_connected_state(const struct connman_service *service,
788 enum connman_service_state state)
791 case CONNMAN_SERVICE_STATE_UNKNOWN:
792 case CONNMAN_SERVICE_STATE_IDLE:
793 case CONNMAN_SERVICE_STATE_ASSOCIATION:
794 case CONNMAN_SERVICE_STATE_CONFIGURATION:
795 case CONNMAN_SERVICE_STATE_DISCONNECT:
796 case CONNMAN_SERVICE_STATE_FAILURE:
798 case CONNMAN_SERVICE_STATE_READY:
799 case CONNMAN_SERVICE_STATE_ONLINE:
806 static connman_bool_t is_idle_state(const struct connman_service *service,
807 enum connman_service_state state)
810 case CONNMAN_SERVICE_STATE_UNKNOWN:
811 case CONNMAN_SERVICE_STATE_ASSOCIATION:
812 case CONNMAN_SERVICE_STATE_CONFIGURATION:
813 case CONNMAN_SERVICE_STATE_READY:
814 case CONNMAN_SERVICE_STATE_ONLINE:
815 case CONNMAN_SERVICE_STATE_DISCONNECT:
816 case CONNMAN_SERVICE_STATE_FAILURE:
818 case CONNMAN_SERVICE_STATE_IDLE:
825 static connman_bool_t is_connecting(struct connman_service *service)
827 return is_connecting_state(service, service->state);
830 static connman_bool_t is_connected(struct connman_service *service)
832 return is_connected_state(service, service->state);
835 static void update_nameservers(struct connman_service *service)
839 if (service->ipconfig_ipv4)
840 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
841 else if (service->ipconfig_ipv6)
842 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
849 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
850 case CONNMAN_SERVICE_STATE_UNKNOWN:
851 case CONNMAN_SERVICE_STATE_IDLE:
852 case CONNMAN_SERVICE_STATE_ASSOCIATION:
853 case CONNMAN_SERVICE_STATE_CONFIGURATION:
855 case CONNMAN_SERVICE_STATE_FAILURE:
856 case CONNMAN_SERVICE_STATE_DISCONNECT:
857 connman_resolver_remove_all(ifname);
859 case CONNMAN_SERVICE_STATE_READY:
860 case CONNMAN_SERVICE_STATE_ONLINE:
864 if (service->nameservers_config != NULL) {
867 for (i = 0; service->nameservers_config[i] != NULL; i++) {
868 connman_resolver_append(ifname, NULL,
869 service->nameservers_config[i]);
871 } else if (service->nameservers != NULL) {
874 for (i = 0; service->nameservers[i] != NULL; i++) {
875 connman_resolver_append(ifname, NULL,
876 service->nameservers[i]);
880 if (service->domains != NULL) {
883 for (i = 0; service->domains[i]; i++)
884 connman_resolver_append(ifname, service->domains[i],
886 } else if (service->domainname != NULL)
887 connman_resolver_append(ifname, service->domainname, NULL);
889 connman_resolver_flush();
893 * The is_auto variable is set to true when IPv6 autoconf nameservers are
894 * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
895 * for details) and not through service.c
897 int __connman_service_nameserver_append(struct connman_service *service,
898 const char *nameserver, gboolean is_auto)
903 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
905 if (nameserver == NULL)
909 nameservers = service->nameservers_auto;
911 nameservers = service->nameservers;
913 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
914 if (g_strcmp0(nameservers[i], nameserver) == 0)
917 if (nameservers != NULL) {
918 len = g_strv_length(nameservers);
919 nameservers = g_try_renew(char *, nameservers, len + 2);
922 nameservers = g_try_new0(char *, len + 2);
925 if (nameservers == NULL)
928 nameservers[len] = g_strdup(nameserver);
929 if (nameservers[len] == NULL)
932 nameservers[len + 1] = NULL;
934 if (is_auto == TRUE) {
935 service->nameservers_auto = nameservers;
937 service->nameservers = nameservers;
938 update_nameservers(service);
944 int __connman_service_nameserver_remove(struct connman_service *service,
945 const char *nameserver, gboolean is_auto)
947 char **servers, **nameservers;
948 gboolean found = FALSE;
951 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
953 if (nameserver == NULL)
957 nameservers = service->nameservers_auto;
959 nameservers = service->nameservers;
961 if (nameservers == NULL)
964 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
965 if (g_strcmp0(nameservers[i], nameserver) == 0) {
973 len = g_strv_length(nameservers);
976 g_strfreev(nameservers);
978 service->nameservers_auto = NULL;
980 service->nameservers = NULL;
985 servers = g_try_new0(char *, len);
989 for (i = 0, j = 0; i < len; i++) {
990 if (g_strcmp0(nameservers[i], nameserver) != 0) {
991 servers[j] = g_strdup(nameservers[i]);
992 if (servers[j] == NULL)
997 servers[len - 1] = NULL;
999 g_strfreev(nameservers);
1000 nameservers = servers;
1002 if (is_auto == TRUE) {
1003 service->nameservers_auto = nameservers;
1005 service->nameservers = nameservers;
1006 update_nameservers(service);
1012 void __connman_service_nameserver_clear(struct connman_service *service)
1014 g_strfreev(service->nameservers);
1015 service->nameservers = NULL;
1017 update_nameservers(service);
1020 static void add_nameserver_route(int family, int index, char *nameserver,
1025 if (connman_inet_compare_subnet(index, nameserver) == TRUE)
1028 if (connman_inet_add_host_route(index, nameserver, gw) < 0)
1029 /* For P-t-P link the above route add will fail */
1030 connman_inet_add_host_route(index, nameserver, NULL);
1034 if (connman_inet_add_ipv6_host_route(index, nameserver,
1036 connman_inet_add_ipv6_host_route(index, nameserver,
1042 static void nameserver_add_routes(int index, char **nameservers,
1046 struct addrinfo hints;
1047 struct addrinfo *addr;
1049 for (i = 0; nameservers[i] != NULL; i++) {
1050 memset(&hints, 0, sizeof(struct addrinfo));
1051 hints.ai_flags = AI_NUMERICHOST;
1054 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1055 if (ret == EAI_NONAME)
1056 family = AF_INET; /* use the IPv4 as a default */
1060 family = addr->ai_family;
1062 add_nameserver_route(family, index, nameservers[i], gw);
1068 static void nameserver_del_routes(int index, char **nameservers,
1069 enum connman_ipconfig_type type)
1072 struct addrinfo hints;
1073 struct addrinfo *addr;
1075 for (i = 0; nameservers[i] != NULL; i++) {
1076 memset(&hints, 0, sizeof(struct addrinfo));
1077 hints.ai_flags = AI_NUMERICHOST;
1080 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1081 if (ret == EAI_NONAME)
1082 family = AF_INET; /* use the IPv4 as a default */
1086 family = addr->ai_family;
1090 if (type != CONNMAN_IPCONFIG_TYPE_IPV6)
1091 connman_inet_del_host_route(index,
1095 if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
1096 connman_inet_del_ipv6_host_route(index,
1105 void __connman_service_nameserver_add_routes(struct connman_service *service,
1110 if (service == NULL)
1113 if (service->network != NULL)
1114 index = connman_network_get_index(service->network);
1115 else if (service->provider != NULL)
1116 index = connman_provider_get_index(service->provider);
1118 if (service->nameservers_config != NULL) {
1120 * Configured nameserver takes preference over the
1121 * discoverd nameserver gathered from DHCP, VPN, etc.
1123 nameserver_add_routes(index, service->nameservers_config, gw);
1124 } else if (service->nameservers != NULL) {
1126 * We add nameservers host routes for nameservers that
1127 * are not on our subnet. For those who are, the subnet
1128 * route will be installed by the time the dns proxy code
1129 * tries to reach them. The subnet route is installed
1130 * when setting the interface IP address.
1132 nameserver_add_routes(index, service->nameservers, gw);
1136 void __connman_service_nameserver_del_routes(struct connman_service *service,
1137 enum connman_ipconfig_type type)
1141 if (service == NULL)
1144 if (service->network != NULL)
1145 index = connman_network_get_index(service->network);
1146 else if (service->provider != NULL)
1147 index = connman_provider_get_index(service->provider);
1149 if (service->nameservers_config != NULL)
1150 nameserver_del_routes(index, service->nameservers_config,
1152 else if (service->nameservers != NULL)
1153 nameserver_del_routes(index, service->nameservers, type);
1156 static struct connman_stats *stats_get(struct connman_service *service)
1158 if (service->roaming == TRUE)
1159 return &service->stats_roaming;
1161 return &service->stats;
1164 static connman_bool_t stats_enabled(struct connman_service *service)
1166 struct connman_stats *stats = stats_get(service);
1168 return stats->enabled;
1171 static void stats_start(struct connman_service *service)
1173 struct connman_stats *stats = stats_get(service);
1175 DBG("service %p", service);
1177 if (stats->timer == NULL)
1180 stats->enabled = TRUE;
1181 stats->data_last.time = stats->data.time;
1183 g_timer_start(stats->timer);
1186 static void stats_stop(struct connman_service *service)
1188 struct connman_stats *stats = stats_get(service);
1189 unsigned int seconds;
1191 DBG("service %p", service);
1193 if (stats->timer == NULL)
1196 if (stats->enabled == FALSE)
1199 g_timer_stop(stats->timer);
1201 seconds = g_timer_elapsed(stats->timer, NULL);
1202 stats->data.time = stats->data_last.time + seconds;
1204 stats->enabled = FALSE;
1207 static void reset_stats(struct connman_service *service)
1209 DBG("service %p", service);
1212 service->stats.valid = FALSE;
1214 service->stats.data.rx_packets = 0;
1215 service->stats.data.tx_packets = 0;
1216 service->stats.data.rx_bytes = 0;
1217 service->stats.data.tx_bytes = 0;
1218 service->stats.data.rx_errors = 0;
1219 service->stats.data.tx_errors = 0;
1220 service->stats.data.rx_dropped = 0;
1221 service->stats.data.tx_dropped = 0;
1222 service->stats.data.time = 0;
1223 service->stats.data_last.time = 0;
1225 g_timer_reset(service->stats.timer);
1228 service->stats_roaming.valid = FALSE;
1230 service->stats_roaming.data.rx_packets = 0;
1231 service->stats_roaming.data.tx_packets = 0;
1232 service->stats_roaming.data.rx_bytes = 0;
1233 service->stats_roaming.data.tx_bytes = 0;
1234 service->stats_roaming.data.rx_errors = 0;
1235 service->stats_roaming.data.tx_errors = 0;
1236 service->stats_roaming.data.rx_dropped = 0;
1237 service->stats_roaming.data.tx_dropped = 0;
1238 service->stats_roaming.data.time = 0;
1239 service->stats_roaming.data_last.time = 0;
1241 g_timer_reset(service->stats_roaming.timer);
1244 struct connman_service *__connman_service_get_default(void)
1246 struct connman_service *service;
1247 GSequenceIter *iter;
1249 iter = g_sequence_get_begin_iter(service_list);
1251 if (g_sequence_iter_is_end(iter) == TRUE)
1254 service = g_sequence_get(iter);
1256 if (is_connected(service) == FALSE)
1262 static void default_changed(void)
1264 struct connman_service *service = __connman_service_get_default();
1266 if (service == current_default)
1269 __connman_service_timeserver_changed(current_default, NULL);
1271 current_default = service;
1273 __connman_notifier_default_changed(service);
1276 static void state_changed(struct connman_service *service)
1280 __connman_notifier_service_state_changed(service, service->state);
1282 str = state2string(service->state);
1286 connman_dbus_property_changed_basic(service->path,
1287 CONNMAN_SERVICE_INTERFACE, "State",
1288 DBUS_TYPE_STRING, &str);
1291 static void strength_changed(struct connman_service *service)
1293 if (service->strength == 0)
1296 connman_dbus_property_changed_basic(service->path,
1297 CONNMAN_SERVICE_INTERFACE, "Strength",
1298 DBUS_TYPE_BYTE, &service->strength);
1301 static void favorite_changed(struct connman_service *service)
1303 if (service->path == NULL)
1306 connman_dbus_property_changed_basic(service->path,
1307 CONNMAN_SERVICE_INTERFACE, "Favorite",
1308 DBUS_TYPE_BOOLEAN, &service->favorite);
1311 static void immutable_changed(struct connman_service *service)
1313 if (service->path == NULL)
1316 connman_dbus_property_changed_basic(service->path,
1317 CONNMAN_SERVICE_INTERFACE, "Immutable",
1318 DBUS_TYPE_BOOLEAN, &service->immutable);
1321 static void roaming_changed(struct connman_service *service)
1323 if (service->path == NULL)
1326 connman_dbus_property_changed_basic(service->path,
1327 CONNMAN_SERVICE_INTERFACE, "Roaming",
1328 DBUS_TYPE_BOOLEAN, &service->roaming);
1331 static void autoconnect_changed(struct connman_service *service)
1333 if (service->path == NULL)
1336 connman_dbus_property_changed_basic(service->path,
1337 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1338 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1341 static void append_security(DBusMessageIter *iter, void *user_data)
1343 struct connman_service *service = user_data;
1346 str = security2string(service->security);
1348 dbus_message_iter_append_basic(iter,
1349 DBUS_TYPE_STRING, &str);
1352 * Some access points incorrectly advertise WPS even when they
1353 * are configured as open or no security, so filter
1356 if (service->wps == TRUE) {
1357 switch (service->security) {
1358 case CONNMAN_SERVICE_SECURITY_PSK:
1359 case CONNMAN_SERVICE_SECURITY_WPA:
1360 case CONNMAN_SERVICE_SECURITY_RSN:
1362 dbus_message_iter_append_basic(iter,
1363 DBUS_TYPE_STRING, &str);
1365 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1366 case CONNMAN_SERVICE_SECURITY_NONE:
1367 case CONNMAN_SERVICE_SECURITY_WEP:
1368 case CONNMAN_SERVICE_SECURITY_8021X:
1374 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1376 struct connman_service *service = user_data;
1378 if (service->ipconfig_ipv4 != NULL)
1379 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1381 else if (service->ipconfig_ipv6 != NULL)
1382 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1386 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1388 struct connman_service *service = user_data;
1390 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1391 state2string(service->state_ipv4));
1393 if (is_connected_state(service, service->state_ipv4) == FALSE)
1396 if (service->ipconfig_ipv4 != NULL)
1397 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1400 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1402 struct connman_service *service = user_data;
1404 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1405 state2string(service->state_ipv6));
1407 if (is_connected_state(service, service->state_ipv6) == FALSE)
1410 if (service->ipconfig_ipv6 != NULL)
1411 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1412 service->ipconfig_ipv4);
1415 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1417 struct connman_service *service = user_data;
1419 if (service->ipconfig_ipv4 != NULL)
1420 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1424 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1426 struct connman_service *service = user_data;
1428 if (service->ipconfig_ipv6 != NULL)
1429 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1433 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1438 servers = *nameservers;
1440 for (i = 0; servers[i] != NULL; i++) {
1441 dbus_message_iter_append_basic(iter,
1442 DBUS_TYPE_STRING, &servers[i]);
1446 static void append_dns(DBusMessageIter *iter, void *user_data)
1448 struct connman_service *service = user_data;
1450 if (is_connected(service) == FALSE)
1453 if (service->nameservers_config != NULL) {
1454 append_nameserver(iter, &service->nameservers_config);
1457 if (service->nameservers != NULL)
1458 append_nameserver(iter, &service->nameservers);
1460 if (service->nameservers_auto != NULL)
1461 append_nameserver(iter, &service->nameservers_auto);
1465 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1467 struct connman_service *service = user_data;
1470 if (service->nameservers_config == NULL)
1473 for (i = 0; service->nameservers_config[i]; i++) {
1474 dbus_message_iter_append_basic(iter,
1476 &service->nameservers_config[i]);
1480 static void append_ts(DBusMessageIter *iter, void *user_data)
1482 GSList *list = user_data;
1484 while (list != NULL) {
1485 char *timeserver = list->data;
1487 if (timeserver != NULL)
1488 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
1491 list = g_slist_next(list);
1495 static void append_tsconfig(DBusMessageIter *iter, void *user_data)
1497 struct connman_service *service = user_data;
1500 if (service->timeservers_config == NULL)
1503 for (i = 0; service->timeservers_config[i]; i++) {
1504 dbus_message_iter_append_basic(iter,
1506 &service->timeservers_config[i]);
1510 static void append_domain(DBusMessageIter *iter, void *user_data)
1512 struct connman_service *service = user_data;
1514 if (is_connected(service) == FALSE &&
1515 is_connecting(service) == FALSE)
1518 if (service->domainname == NULL)
1521 dbus_message_iter_append_basic(iter,
1522 DBUS_TYPE_STRING, &service->domainname);
1525 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1527 struct connman_service *service = user_data;
1530 if (service->domains == NULL)
1533 for (i = 0; service->domains[i]; i++)
1534 dbus_message_iter_append_basic(iter,
1535 DBUS_TYPE_STRING, &service->domains[i]);
1538 static void append_proxies(DBusMessageIter *iter, void *user_data)
1540 struct connman_service *service = user_data;
1543 if (service->proxies == NULL)
1546 for (i = 0; service->proxies[i]; i++)
1547 dbus_message_iter_append_basic(iter,
1548 DBUS_TYPE_STRING, &service->proxies[i]);
1551 static void append_excludes(DBusMessageIter *iter, void *user_data)
1553 struct connman_service *service = user_data;
1556 if (service->excludes == NULL)
1559 for (i = 0; service->excludes[i]; i++)
1560 dbus_message_iter_append_basic(iter,
1561 DBUS_TYPE_STRING, &service->excludes[i]);
1564 static void append_proxy(DBusMessageIter *iter, void *user_data)
1566 struct connman_service *service = user_data;
1567 enum connman_service_proxy_method proxy;
1568 const char *pac = NULL;
1569 const char *method = proxymethod2string(
1570 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1574 if (is_connected(service) == FALSE)
1577 proxy = connman_service_get_proxy_method(service);
1580 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1582 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1584 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1585 connman_dbus_dict_append_array(iter, "Servers",
1586 DBUS_TYPE_STRING, append_proxies,
1589 connman_dbus_dict_append_array(iter, "Excludes",
1590 DBUS_TYPE_STRING, append_excludes,
1593 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1594 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1595 if (service->ipconfig_ipv4 != NULL)
1596 pac = __connman_ipconfig_get_proxy_autoconfig(
1597 service->ipconfig_ipv4);
1598 else if (service->ipconfig_ipv6 != NULL)
1599 pac = __connman_ipconfig_get_proxy_autoconfig(
1600 service->ipconfig_ipv6);
1602 if (service->pac == NULL && pac == NULL)
1605 if (service->pac != NULL)
1608 connman_dbus_dict_append_basic(iter, "URL",
1609 DBUS_TYPE_STRING, &pac);
1613 method = proxymethod2string(proxy);
1616 connman_dbus_dict_append_basic(iter, "Method",
1617 DBUS_TYPE_STRING, &method);
1620 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1622 struct connman_service *service = user_data;
1625 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1628 switch (service->proxy_config) {
1629 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1631 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1633 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1634 if (service->proxies != NULL)
1635 connman_dbus_dict_append_array(iter, "Servers",
1637 append_proxies, service);
1639 if (service->excludes != NULL)
1640 connman_dbus_dict_append_array(iter, "Excludes",
1642 append_excludes, service);
1644 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1645 if (service->pac != NULL)
1646 connman_dbus_dict_append_basic(iter, "URL",
1647 DBUS_TYPE_STRING, &service->pac);
1651 method = proxymethod2string(service->proxy_config);
1653 connman_dbus_dict_append_basic(iter, "Method",
1654 DBUS_TYPE_STRING, &method);
1657 static void append_provider(DBusMessageIter *iter, void *user_data)
1659 struct connman_service *service = user_data;
1661 DBG("%p %p", service, service->provider);
1663 if (is_connected(service) == FALSE)
1666 if (service->provider != NULL)
1667 __connman_provider_append_properties(service->provider, iter);
1671 static void settings_changed(struct connman_service *service,
1672 struct connman_ipconfig *ipconfig)
1674 connman_dbus_property_changed_dict(service->path,
1675 CONNMAN_SERVICE_INTERFACE, "IPv4",
1676 append_ipv4, service);
1678 connman_dbus_property_changed_dict(service->path,
1679 CONNMAN_SERVICE_INTERFACE, "IPv6",
1680 append_ipv6, service);
1682 __connman_notifier_ipconfig_changed(service, ipconfig);
1685 static void ipv4_configuration_changed(struct connman_service *service)
1687 connman_dbus_property_changed_dict(service->path,
1688 CONNMAN_SERVICE_INTERFACE,
1689 "IPv4.Configuration",
1694 static void ipv6_configuration_changed(struct connman_service *service)
1696 connman_dbus_property_changed_dict(service->path,
1697 CONNMAN_SERVICE_INTERFACE,
1698 "IPv6.Configuration",
1703 static void dns_changed(struct connman_service *service)
1705 connman_dbus_property_changed_array(service->path,
1706 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1707 DBUS_TYPE_STRING, append_dns, service);
1710 static void dns_configuration_changed(struct connman_service *service)
1712 connman_dbus_property_changed_array(service->path,
1713 CONNMAN_SERVICE_INTERFACE,
1714 "Nameservers.Configuration",
1715 DBUS_TYPE_STRING, append_dnsconfig, service);
1717 dns_changed(service);
1720 static void domain_changed(struct connman_service *service)
1722 connman_dbus_property_changed_array(service->path,
1723 CONNMAN_SERVICE_INTERFACE, "Domains",
1724 DBUS_TYPE_STRING, append_domain, service);
1727 static void domain_configuration_changed(struct connman_service *service)
1729 connman_dbus_property_changed_array(service->path,
1730 CONNMAN_SERVICE_INTERFACE,
1731 "Domains.Configuration",
1732 DBUS_TYPE_STRING, append_domainconfig, service);
1735 static void proxy_changed(struct connman_service *service)
1737 connman_dbus_property_changed_dict(service->path,
1738 CONNMAN_SERVICE_INTERFACE, "Proxy",
1739 append_proxy, service);
1742 static void proxy_configuration_changed(struct connman_service *service)
1744 connman_dbus_property_changed_dict(service->path,
1745 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1746 append_proxyconfig, service);
1748 proxy_changed(service);
1751 static void timeservers_configuration_changed(struct connman_service *service)
1753 connman_dbus_property_changed_array(service->path,
1754 CONNMAN_SERVICE_INTERFACE,
1755 "Timeservers.Configuration",
1757 append_tsconfig, service);
1760 static void link_changed(struct connman_service *service)
1762 connman_dbus_property_changed_dict(service->path,
1763 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1764 append_ethernet, service);
1767 static void stats_append_counters(DBusMessageIter *dict,
1768 struct connman_stats_data *stats,
1769 struct connman_stats_data *counters,
1770 connman_bool_t append_all)
1772 if (counters->rx_packets != stats->rx_packets || append_all) {
1773 counters->rx_packets = stats->rx_packets;
1774 connman_dbus_dict_append_basic(dict, "RX.Packets",
1775 DBUS_TYPE_UINT32, &stats->rx_packets);
1778 if (counters->tx_packets != stats->tx_packets || append_all) {
1779 counters->tx_packets = stats->tx_packets;
1780 connman_dbus_dict_append_basic(dict, "TX.Packets",
1781 DBUS_TYPE_UINT32, &stats->tx_packets);
1784 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1785 counters->rx_bytes = stats->rx_bytes;
1786 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1787 DBUS_TYPE_UINT32, &stats->rx_bytes);
1790 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1791 counters->tx_bytes = stats->tx_bytes;
1792 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1793 DBUS_TYPE_UINT32, &stats->tx_bytes);
1796 if (counters->rx_errors != stats->rx_errors || append_all) {
1797 counters->rx_errors = stats->rx_errors;
1798 connman_dbus_dict_append_basic(dict, "RX.Errors",
1799 DBUS_TYPE_UINT32, &stats->rx_errors);
1802 if (counters->tx_errors != stats->tx_errors || append_all) {
1803 counters->tx_errors = stats->tx_errors;
1804 connman_dbus_dict_append_basic(dict, "TX.Errors",
1805 DBUS_TYPE_UINT32, &stats->tx_errors);
1808 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1809 counters->rx_dropped = stats->rx_dropped;
1810 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1811 DBUS_TYPE_UINT32, &stats->rx_dropped);
1814 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1815 counters->tx_dropped = stats->tx_dropped;
1816 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1817 DBUS_TYPE_UINT32, &stats->tx_dropped);
1820 if (counters->time != stats->time || append_all) {
1821 counters->time = stats->time;
1822 connman_dbus_dict_append_basic(dict, "Time",
1823 DBUS_TYPE_UINT32, &stats->time);
1827 static void stats_append(struct connman_service *service,
1828 const char *counter,
1829 struct connman_stats_counter *counters,
1830 connman_bool_t append_all)
1832 DBusMessageIter array, dict;
1835 DBG("service %p counter %s", service, counter);
1837 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1841 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1842 &service->path, DBUS_TYPE_INVALID);
1844 dbus_message_iter_init_append(msg, &array);
1847 connman_dbus_dict_open(&array, &dict);
1849 stats_append_counters(&dict, &service->stats.data,
1850 &counters->stats.data, append_all);
1852 connman_dbus_dict_close(&array, &dict);
1854 /* roaming counter */
1855 connman_dbus_dict_open(&array, &dict);
1857 stats_append_counters(&dict, &service->stats_roaming.data,
1858 &counters->stats_roaming.data, append_all);
1860 connman_dbus_dict_close(&array, &dict);
1862 __connman_counter_send_usage(counter, msg);
1865 static void stats_update(struct connman_service *service,
1866 unsigned int rx_packets, unsigned int tx_packets,
1867 unsigned int rx_bytes, unsigned int tx_bytes,
1868 unsigned int rx_errors, unsigned int tx_errors,
1869 unsigned int rx_dropped, unsigned int tx_dropped)
1871 struct connman_stats *stats = stats_get(service);
1872 struct connman_stats_data *data_last = &stats->data_last;
1873 struct connman_stats_data *data = &stats->data;
1874 unsigned int seconds;
1876 DBG("service %p", service);
1878 if (stats->valid == TRUE) {
1880 rx_packets - data_last->rx_packets;
1882 tx_packets - data_last->tx_packets;
1884 rx_bytes - data_last->rx_bytes;
1886 tx_bytes - data_last->tx_bytes;
1888 rx_errors - data_last->rx_errors;
1890 tx_errors - data_last->tx_errors;
1892 rx_dropped - data_last->rx_dropped;
1894 tx_dropped - data_last->tx_dropped;
1896 stats->valid = TRUE;
1899 data_last->rx_packets = rx_packets;
1900 data_last->tx_packets = tx_packets;
1901 data_last->rx_bytes = rx_bytes;
1902 data_last->tx_bytes = tx_bytes;
1903 data_last->rx_errors = rx_errors;
1904 data_last->tx_errors = tx_errors;
1905 data_last->rx_dropped = rx_dropped;
1906 data_last->tx_dropped = tx_dropped;
1908 seconds = g_timer_elapsed(stats->timer, NULL);
1909 stats->data.time = stats->data_last.time + seconds;
1912 void __connman_service_notify(struct connman_service *service,
1913 unsigned int rx_packets, unsigned int tx_packets,
1914 unsigned int rx_bytes, unsigned int tx_bytes,
1915 unsigned int rx_errors, unsigned int tx_errors,
1916 unsigned int rx_dropped, unsigned int tx_dropped)
1918 GHashTableIter iter;
1919 gpointer key, value;
1920 const char *counter;
1921 struct connman_stats_counter *counters;
1922 struct connman_stats_data *data;
1925 if (service == NULL)
1928 if (is_connected(service) == FALSE)
1931 stats_update(service,
1932 rx_packets, tx_packets,
1934 rx_errors, tx_errors,
1935 rx_dropped, tx_dropped);
1937 data = &stats_get(service)->data;
1938 err = __connman_stats_update(service, service->roaming, data);
1940 connman_error("Failed to store statistics for %s",
1941 service->identifier);
1943 g_hash_table_iter_init(&iter, service->counter_table);
1944 while (g_hash_table_iter_next(&iter, &key, &value)) {
1948 stats_append(service, counter, counters, counters->append_all);
1949 counters->append_all = FALSE;
1953 int __connman_service_counter_register(const char *counter)
1955 struct connman_service *service;
1956 GSequenceIter *iter;
1957 struct connman_stats_counter *counters;
1959 DBG("counter %s", counter);
1961 counter_list = g_slist_append(counter_list, (gpointer)counter);
1963 iter = g_sequence_get_begin_iter(service_list);
1965 while (g_sequence_iter_is_end(iter) == FALSE) {
1966 service = g_sequence_get(iter);
1968 counters = g_try_new0(struct connman_stats_counter, 1);
1969 if (counters == NULL)
1972 counters->append_all = TRUE;
1974 g_hash_table_replace(service->counter_table, (gpointer)counter,
1977 iter = g_sequence_iter_next(iter);
1983 void __connman_service_counter_unregister(const char *counter)
1985 struct connman_service *service;
1986 GSequenceIter *iter;
1988 DBG("counter %s", counter);
1990 iter = g_sequence_get_begin_iter(service_list);
1992 while (g_sequence_iter_is_end(iter) == FALSE) {
1993 service = g_sequence_get(iter);
1995 g_hash_table_remove(service->counter_table, counter);
1997 iter = g_sequence_iter_next(iter);
2000 counter_list = g_slist_remove(counter_list, counter);
2003 GSequence *__connman_service_get_list(struct connman_session *session,
2004 service_match_cb service_match,
2005 create_service_entry_cb create_service_entry,
2006 GDestroyNotify destroy_service_entry)
2009 GSequenceIter *iter;
2010 struct connman_service *service;
2011 struct service_entry *entry;
2013 list = g_sequence_new(destroy_service_entry);
2017 iter = g_sequence_get_begin_iter(service_list);
2019 while (g_sequence_iter_is_end(iter) == FALSE) {
2020 service = g_sequence_get(iter);
2022 if (service_match(session, service) == TRUE) {
2023 entry = create_service_entry(service, service->name,
2028 g_sequence_append(list, entry);
2031 iter = g_sequence_iter_next(iter);
2037 void __connman_service_session_inc(struct connman_service *service)
2039 DBG("service %p ref count %d", service,
2040 service->session_usage_count + 1);
2042 __sync_fetch_and_add(&service->session_usage_count, 1);
2045 connman_bool_t __connman_service_session_dec(struct connman_service *service)
2047 DBG("service %p ref count %d", service,
2048 service->session_usage_count - 1);
2050 if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
2056 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2057 struct connman_service *service)
2062 str = __connman_service_type2string(service->type);
2064 connman_dbus_dict_append_basic(dict, "Type",
2065 DBUS_TYPE_STRING, &str);
2067 connman_dbus_dict_append_array(dict, "Security",
2068 DBUS_TYPE_STRING, append_security, service);
2070 str = state2string(service->state);
2072 connman_dbus_dict_append_basic(dict, "State",
2073 DBUS_TYPE_STRING, &str);
2075 str = error2string(service->error);
2077 connman_dbus_dict_append_basic(dict, "Error",
2078 DBUS_TYPE_STRING, &str);
2080 if (service->strength > 0)
2081 connman_dbus_dict_append_basic(dict, "Strength",
2082 DBUS_TYPE_BYTE, &service->strength);
2084 connman_dbus_dict_append_basic(dict, "Favorite",
2085 DBUS_TYPE_BOOLEAN, &service->favorite);
2087 connman_dbus_dict_append_basic(dict, "Immutable",
2088 DBUS_TYPE_BOOLEAN, &service->immutable);
2090 if (service->favorite == TRUE)
2091 connman_dbus_dict_append_basic(dict, "AutoConnect",
2092 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2094 connman_dbus_dict_append_basic(dict, "AutoConnect",
2095 DBUS_TYPE_BOOLEAN, &service->favorite);
2097 if (service->name != NULL)
2098 connman_dbus_dict_append_basic(dict, "Name",
2099 DBUS_TYPE_STRING, &service->name);
2101 switch (service->type) {
2102 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2103 case CONNMAN_SERVICE_TYPE_SYSTEM:
2104 case CONNMAN_SERVICE_TYPE_GPS:
2105 case CONNMAN_SERVICE_TYPE_VPN:
2106 case CONNMAN_SERVICE_TYPE_GADGET:
2108 case CONNMAN_SERVICE_TYPE_CELLULAR:
2109 connman_dbus_dict_append_basic(dict, "Roaming",
2110 DBUS_TYPE_BOOLEAN, &service->roaming);
2112 connman_dbus_dict_append_dict(dict, "Ethernet",
2113 append_ethernet, service);
2115 case CONNMAN_SERVICE_TYPE_WIFI:
2116 case CONNMAN_SERVICE_TYPE_ETHERNET:
2117 case CONNMAN_SERVICE_TYPE_WIMAX:
2118 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2119 connman_dbus_dict_append_dict(dict, "Ethernet",
2120 append_ethernet, service);
2124 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2126 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2127 append_ipv4config, service);
2129 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2131 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2132 append_ipv6config, service);
2134 connman_dbus_dict_append_array(dict, "Nameservers",
2135 DBUS_TYPE_STRING, append_dns, service);
2137 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2138 DBUS_TYPE_STRING, append_dnsconfig, service);
2140 if (service->state == CONNMAN_SERVICE_STATE_READY ||
2141 service->state == CONNMAN_SERVICE_STATE_ONLINE)
2142 list = __connman_timeserver_get_all(service);
2146 connman_dbus_dict_append_array(dict, "Timeservers",
2147 DBUS_TYPE_STRING, append_ts, list);
2149 g_slist_free_full(list, g_free);
2151 connman_dbus_dict_append_array(dict, "Timeservers.Configuration",
2152 DBUS_TYPE_STRING, append_tsconfig, service);
2154 connman_dbus_dict_append_array(dict, "Domains",
2155 DBUS_TYPE_STRING, append_domain, service);
2157 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2158 DBUS_TYPE_STRING, append_domainconfig, service);
2160 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2162 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2163 append_proxyconfig, service);
2165 connman_dbus_dict_append_dict(dict, "Provider",
2166 append_provider, service);
2169 static void append_struct_service(DBusMessageIter *iter,
2170 connman_dbus_append_cb_t function,
2171 struct connman_service *service)
2173 DBusMessageIter entry, dict;
2175 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2177 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2180 connman_dbus_dict_open(&entry, &dict);
2181 if (function != NULL)
2182 function(&dict, service);
2183 connman_dbus_dict_close(&entry, &dict);
2185 dbus_message_iter_close_container(iter, &entry);
2188 static void append_dict_properties(DBusMessageIter *dict, void *user_data)
2190 struct connman_service *service = user_data;
2192 append_properties(dict, TRUE, service);
2195 static void append_struct(gpointer value, gpointer user_data)
2197 struct connman_service *service = value;
2198 DBusMessageIter *iter = user_data;
2200 if (service->path == NULL)
2203 append_struct_service(iter, append_dict_properties, service);
2206 void __connman_service_list_struct(DBusMessageIter *iter)
2208 g_sequence_foreach(service_list, append_struct, iter);
2211 connman_bool_t __connman_service_is_hidden(struct connman_service *service)
2213 return service->hidden;
2217 __connman_service_is_split_routing(struct connman_service *service)
2219 return service->do_split_routing;
2222 int __connman_service_get_index(struct connman_service *service)
2224 if (service == NULL)
2227 if (service->network != NULL)
2228 return connman_network_get_index(service->network);
2229 else if (service->provider != NULL)
2230 return connman_provider_get_index(service->provider);
2235 void __connman_service_set_domainname(struct connman_service *service,
2236 const char *domainname)
2238 if (service == NULL || service->hidden == TRUE)
2241 g_free(service->domainname);
2242 service->domainname = g_strdup(domainname);
2244 domain_changed(service);
2247 const char *connman_service_get_domainname(struct connman_service *service)
2249 if (service == NULL)
2252 if (service->domains != NULL)
2253 return service->domains[0];
2255 return service->domainname;
2258 char **connman_service_get_nameservers(struct connman_service *service)
2260 if (service == NULL)
2263 if (service->nameservers_config != NULL)
2264 return g_strdupv(service->nameservers_config);
2265 else if (service->nameservers != NULL ||
2266 service->nameservers_auto != NULL) {
2267 int len = 0, len_auto = 0, i;
2270 if (service->nameservers != NULL)
2271 len = g_strv_length(service->nameservers);
2272 if (service->nameservers_auto != NULL)
2273 len_auto = g_strv_length(service->nameservers_auto);
2275 nameservers = g_try_new0(char *, len + len_auto + 1);
2276 if (nameservers == NULL)
2279 for (i = 0; i < len; i++)
2280 nameservers[i] = g_strdup(service->nameservers[i]);
2282 for (i = 0; i < len_auto; i++)
2283 nameservers[i + len] =
2284 g_strdup(service->nameservers_auto[i]);
2292 char **connman_service_get_timeservers_config(struct connman_service *service)
2294 if (service == NULL)
2297 return service->timeservers_config;
2300 char **connman_service_get_timeservers(struct connman_service *service)
2302 if (service == NULL)
2305 if (service->timeservers != NULL)
2306 return service->timeservers;
2311 void connman_service_set_proxy_method(struct connman_service *service,
2312 enum connman_service_proxy_method method)
2314 if (service == NULL || service->hidden == TRUE)
2317 service->proxy = method;
2319 proxy_changed(service);
2321 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2322 __connman_notifier_proxy_changed(service);
2325 enum connman_service_proxy_method connman_service_get_proxy_method(
2326 struct connman_service *service)
2328 if (service == NULL)
2329 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2331 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2332 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2333 service->pac == NULL)
2334 return service->proxy;
2336 return service->proxy_config;
2339 return service->proxy;
2342 char **connman_service_get_proxy_servers(struct connman_service *service)
2344 return g_strdupv(service->proxies);
2347 char **connman_service_get_proxy_excludes(struct connman_service *service)
2349 return g_strdupv(service->excludes);
2352 const char *connman_service_get_proxy_url(struct connman_service *service)
2354 if (service == NULL)
2357 return service->pac;
2360 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2363 if (service == NULL || service->hidden == TRUE)
2366 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2368 if (service->ipconfig_ipv4) {
2369 if (__connman_ipconfig_set_proxy_autoconfig(
2370 service->ipconfig_ipv4, url) < 0)
2372 } else if (service->ipconfig_ipv6) {
2373 if (__connman_ipconfig_set_proxy_autoconfig(
2374 service->ipconfig_ipv6, url) < 0)
2379 proxy_changed(service);
2381 __connman_notifier_proxy_changed(service);
2384 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2386 if (service == NULL)
2389 if (service->ipconfig_ipv4)
2390 return __connman_ipconfig_get_proxy_autoconfig(
2391 service->ipconfig_ipv4);
2392 else if (service->ipconfig_ipv6)
2393 return __connman_ipconfig_get_proxy_autoconfig(
2394 service->ipconfig_ipv6);
2398 int __connman_service_timeserver_append(struct connman_service *service,
2399 const char *timeserver)
2403 DBG("service %p timeserver %s", service, timeserver);
2405 if (timeserver == NULL)
2408 if (service->timeservers != NULL) {
2411 for (i = 0; service->timeservers[i] != NULL; i++)
2412 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2415 len = g_strv_length(service->timeservers);
2416 service->timeservers = g_try_renew(char *, service->timeservers,
2420 service->timeservers = g_try_new0(char *, len + 2);
2423 if (service->timeservers == NULL)
2426 service->timeservers[len] = g_strdup(timeserver);
2427 service->timeservers[len + 1] = NULL;
2432 int __connman_service_timeserver_remove(struct connman_service *service,
2433 const char *timeserver)
2436 int len, i, j, found = 0;
2438 DBG("service %p timeserver %s", service, timeserver);
2440 if (timeserver == NULL)
2443 if (service->timeservers == NULL)
2446 for (i = 0; service->timeservers != NULL &&
2447 service->timeservers[i] != NULL; i++)
2448 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2456 len = g_strv_length(service->timeservers);
2459 g_strfreev(service->timeservers);
2460 service->timeservers = NULL;
2465 servers = g_try_new0(char *, len);
2466 if (servers == NULL)
2469 for (i = 0, j = 0; i < len; i++) {
2470 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2471 servers[j] = g_strdup(service->timeservers[i]);
2472 if (servers[j] == NULL)
2477 servers[len - 1] = NULL;
2479 g_strfreev(service->timeservers);
2480 service->timeservers = servers;
2485 void __connman_service_timeserver_changed(struct connman_service *service,
2488 if (service == NULL)
2491 connman_dbus_property_changed_array(service->path,
2492 CONNMAN_SERVICE_INTERFACE, "Timeservers",
2493 DBUS_TYPE_STRING, append_ts, ts_list);
2496 void __connman_service_set_pac(struct connman_service *service,
2499 if (service->hidden == TRUE)
2501 g_free(service->pac);
2502 service->pac = g_strdup(pac);
2504 proxy_changed(service);
2507 void __connman_service_set_identity(struct connman_service *service,
2508 const char *identity)
2510 if (service->immutable || service->hidden == TRUE)
2513 g_free(service->identity);
2514 service->identity = g_strdup(identity);
2516 if (service->network != NULL)
2517 connman_network_set_string(service->network,
2522 void __connman_service_set_agent_identity(struct connman_service *service,
2523 const char *agent_identity)
2525 if (service->hidden == TRUE)
2527 g_free(service->agent_identity);
2528 service->agent_identity = g_strdup(agent_identity);
2530 if (service->network != NULL)
2531 connman_network_set_string(service->network,
2532 "WiFi.AgentIdentity",
2533 service->agent_identity);
2536 static int check_passphrase(enum connman_service_security security,
2537 const char *passphrase)
2542 if (passphrase == NULL)
2545 length = strlen(passphrase);
2548 case CONNMAN_SERVICE_SECURITY_PSK:
2549 case CONNMAN_SERVICE_SECURITY_WPA:
2550 case CONNMAN_SERVICE_SECURITY_RSN:
2551 /* A raw key is always 64 bytes length,
2552 * its content is in hex representation.
2553 * A PSK key must be between [8..63].
2556 for (i = 0; i < 64; i++)
2557 if (!isxdigit((unsigned char)
2560 } else if (length < 8 || length > 63)
2563 case CONNMAN_SERVICE_SECURITY_WEP:
2564 /* length of WEP key is 10 or 26
2565 * length of WEP passphrase is 5 or 13
2567 if (length == 10 || length == 26) {
2568 for (i = 0; i < length; i++)
2569 if (!isxdigit((unsigned char)
2572 } else if (length != 5 && length != 13)
2575 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2576 case CONNMAN_SERVICE_SECURITY_NONE:
2577 case CONNMAN_SERVICE_SECURITY_8021X:
2584 int __connman_service_set_passphrase(struct connman_service *service,
2585 const char *passphrase)
2589 if (service->immutable == TRUE || service->hidden == TRUE)
2592 err = check_passphrase(service->security, passphrase);
2595 g_free(service->passphrase);
2596 service->passphrase = g_strdup(passphrase);
2598 if (service->network != NULL)
2599 connman_network_set_string(service->network,
2601 service->passphrase);
2602 service_save(service);
2608 void __connman_service_set_agent_passphrase(struct connman_service *service,
2609 const char *agent_passphrase)
2611 if (service->hidden == TRUE)
2613 g_free(service->agent_passphrase);
2614 service->agent_passphrase = g_strdup(agent_passphrase);
2616 if (service->network != NULL)
2617 connman_network_set_string(service->network,
2618 "WiFi.AgentPassphrase",
2619 service->agent_passphrase);
2622 static DBusMessage *get_properties(DBusConnection *conn,
2623 DBusMessage *msg, void *user_data)
2625 struct connman_service *service = user_data;
2627 DBusMessageIter array, dict;
2629 DBG("service %p", service);
2631 reply = dbus_message_new_method_return(msg);
2635 dbus_message_iter_init_append(reply, &array);
2637 connman_dbus_dict_open(&array, &dict);
2638 append_properties(&dict, FALSE, service);
2639 connman_dbus_dict_close(&array, &dict);
2644 static int update_proxy_configuration(struct connman_service *service,
2645 DBusMessageIter *array)
2647 DBusMessageIter dict;
2648 enum connman_service_proxy_method method;
2649 GString *servers_str = NULL;
2650 GString *excludes_str = NULL;
2651 const char *url = NULL;
2653 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2655 dbus_message_iter_recurse(array, &dict);
2657 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2658 DBusMessageIter entry, variant;
2662 dbus_message_iter_recurse(&dict, &entry);
2664 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2667 dbus_message_iter_get_basic(&entry, &key);
2668 dbus_message_iter_next(&entry);
2670 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2673 dbus_message_iter_recurse(&entry, &variant);
2675 type = dbus_message_iter_get_arg_type(&variant);
2677 if (g_str_equal(key, "Method") == TRUE) {
2680 if (type != DBUS_TYPE_STRING)
2683 dbus_message_iter_get_basic(&variant, &val);
2684 method = string2proxymethod(val);
2685 } else if (g_str_equal(key, "URL") == TRUE) {
2686 if (type != DBUS_TYPE_STRING)
2689 dbus_message_iter_get_basic(&variant, &url);
2690 } else if (g_str_equal(key, "Servers") == TRUE) {
2691 DBusMessageIter str_array;
2693 if (type != DBUS_TYPE_ARRAY)
2696 servers_str = g_string_new(NULL);
2697 if (servers_str == NULL)
2700 dbus_message_iter_recurse(&variant, &str_array);
2702 while (dbus_message_iter_get_arg_type(&str_array) ==
2706 dbus_message_iter_get_basic(&str_array, &val);
2708 if (servers_str->len > 0)
2709 g_string_append_printf(servers_str,
2712 g_string_append(servers_str, val);
2714 dbus_message_iter_next(&str_array);
2716 } else if (g_str_equal(key, "Excludes") == TRUE) {
2717 DBusMessageIter str_array;
2719 if (type != DBUS_TYPE_ARRAY)
2722 excludes_str = g_string_new(NULL);
2723 if (excludes_str == NULL)
2726 dbus_message_iter_recurse(&variant, &str_array);
2728 while (dbus_message_iter_get_arg_type(&str_array) ==
2732 dbus_message_iter_get_basic(&str_array, &val);
2734 if (excludes_str->len > 0)
2735 g_string_append_printf(excludes_str,
2738 g_string_append(excludes_str, val);
2740 dbus_message_iter_next(&str_array);
2744 dbus_message_iter_next(&dict);
2748 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2750 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2751 if (servers_str == NULL && service->proxies == NULL)
2754 if (servers_str != NULL) {
2755 g_strfreev(service->proxies);
2757 if (servers_str->len > 0)
2758 service->proxies = g_strsplit_set(
2759 servers_str->str, " ", 0);
2761 service->proxies = NULL;
2764 if (excludes_str != NULL) {
2765 g_strfreev(service->excludes);
2767 if (excludes_str->len > 0)
2768 service->excludes = g_strsplit_set(
2769 excludes_str->str, " ", 0);
2771 service->excludes = NULL;
2774 if (service->proxies == NULL)
2775 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2778 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2779 g_free(service->pac);
2781 if (url != NULL && strlen(url) > 0)
2782 service->pac = g_strdup(url);
2784 service->pac = NULL;
2786 /* if we are connected:
2787 - if service->pac == NULL
2788 - if __connman_ipconfig_get_proxy_autoconfig(
2789 service->ipconfig) == NULL
2790 --> We should start WPAD */
2793 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2797 if (servers_str != NULL)
2798 g_string_free(servers_str, TRUE);
2800 if (excludes_str != NULL)
2801 g_string_free(excludes_str, TRUE);
2803 service->proxy_config = method;
2808 if (servers_str != NULL)
2809 g_string_free(servers_str, TRUE);
2811 if (excludes_str != NULL)
2812 g_string_free(excludes_str, TRUE);
2817 static int set_ipconfig(struct connman_service *service,
2818 struct connman_ipconfig *ipconfig,
2819 DBusMessageIter *array,
2820 enum connman_service_state state,
2821 enum connman_service_state *new_state)
2823 enum connman_ipconfig_method old_method;
2824 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2825 enum connman_ipconfig_type type;
2828 if (ipconfig == NULL)
2831 old_method = __connman_ipconfig_get_method(ipconfig);
2833 if (is_connecting_state(service, state) ||
2834 is_connected_state(service, state))
2835 __connman_network_clear_ipconfig(service->network, ipconfig);
2837 err = __connman_ipconfig_set_config(ipconfig, array);
2838 method = __connman_ipconfig_get_method(ipconfig);
2839 type = __connman_ipconfig_get_config_type(ipconfig);
2841 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2842 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2843 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2844 *new_state = service->state_ipv4 =
2845 CONNMAN_SERVICE_STATE_CONFIGURATION;
2846 __connman_ipconfig_enable(ipconfig);
2849 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2850 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2851 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2852 *new_state = service->state_ipv6;
2853 __connman_ipconfig_enable(ipconfig);
2857 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2858 type, method, state2string(*new_state));
2863 static DBusMessage *set_property(DBusConnection *conn,
2864 DBusMessage *msg, void *user_data)
2866 struct connman_service *service = user_data;
2867 DBusMessageIter iter, value;
2871 DBG("service %p", service);
2873 if (dbus_message_iter_init(msg, &iter) == FALSE)
2874 return __connman_error_invalid_arguments(msg);
2876 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
2877 return __connman_error_invalid_arguments(msg);
2879 dbus_message_iter_get_basic(&iter, &name);
2880 dbus_message_iter_next(&iter);
2882 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
2883 return __connman_error_invalid_arguments(msg);
2885 dbus_message_iter_recurse(&iter, &value);
2887 type = dbus_message_iter_get_arg_type(&value);
2889 if (g_str_equal(name, "AutoConnect") == TRUE) {
2890 connman_bool_t autoconnect;
2892 if (type != DBUS_TYPE_BOOLEAN)
2893 return __connman_error_invalid_arguments(msg);
2895 if (service->favorite == FALSE)
2896 return __connman_error_invalid_service(msg);
2898 dbus_message_iter_get_basic(&value, &autoconnect);
2900 if (service->autoconnect == autoconnect)
2901 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2903 service->autoconnect = autoconnect;
2905 autoconnect_changed(service);
2907 service_save(service);
2908 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2909 DBusMessageIter entry;
2914 if (type != DBUS_TYPE_ARRAY)
2915 return __connman_error_invalid_arguments(msg);
2917 str = g_string_new(NULL);
2919 return __connman_error_invalid_arguments(msg);
2921 index = connman_network_get_index(service->network);
2922 gw = __connman_ipconfig_get_gateway_from_index(index,
2923 CONNMAN_IPCONFIG_TYPE_ALL);
2925 if (gw && strlen(gw))
2926 __connman_service_nameserver_del_routes(service,
2927 CONNMAN_IPCONFIG_TYPE_ALL);
2929 dbus_message_iter_recurse(&value, &entry);
2931 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2933 dbus_message_iter_get_basic(&entry, &val);
2934 dbus_message_iter_next(&entry);
2936 g_string_append_printf(str, " %s", val);
2938 g_string_append(str, val);
2941 g_strfreev(service->nameservers_config);
2944 service->nameservers_config =
2945 g_strsplit_set(str->str, " ", 0);
2947 service->nameservers_config = NULL;
2950 g_string_free(str, TRUE);
2952 if (gw && strlen(gw))
2953 __connman_service_nameserver_add_routes(service, gw);
2955 update_nameservers(service);
2956 dns_configuration_changed(service);
2958 service_save(service);
2959 } else if (g_str_equal(name, "Timeservers.Configuration") == TRUE) {
2960 DBusMessageIter entry;
2961 GSList *list = NULL;
2964 if (type != DBUS_TYPE_ARRAY)
2965 return __connman_error_invalid_arguments(msg);
2967 dbus_message_iter_recurse(&value, &entry);
2969 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2973 dbus_message_iter_get_basic(&entry, &val);
2975 new_head = __connman_timeserver_add_list(list, val);
2976 if (list != new_head) {
2981 dbus_message_iter_next(&entry);
2984 g_strfreev(service->timeservers_config);
2985 service->timeservers_config = NULL;
2988 service->timeservers_config = g_new0(char *, count+1);
2990 while (list != NULL) {
2992 service->timeservers_config[count] = list->data;
2993 list = g_slist_delete_link(list, list);
2997 service_save(service);
2998 timeservers_configuration_changed(service);
3000 __connman_timeserver_sync(service);
3001 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
3002 DBusMessageIter entry;
3005 if (type != DBUS_TYPE_ARRAY)
3006 return __connman_error_invalid_arguments(msg);
3008 str = g_string_new(NULL);
3010 return __connman_error_invalid_arguments(msg);
3012 dbus_message_iter_recurse(&value, &entry);
3014 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3016 dbus_message_iter_get_basic(&entry, &val);
3017 dbus_message_iter_next(&entry);
3019 g_string_append_printf(str, " %s", val);
3021 g_string_append(str, val);
3024 g_strfreev(service->domains);
3027 service->domains = g_strsplit_set(str->str, " ", 0);
3029 service->domains = NULL;
3031 g_string_free(str, TRUE);
3033 update_nameservers(service);
3034 domain_configuration_changed(service);
3036 service_save(service);
3037 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
3040 if (type != DBUS_TYPE_ARRAY)
3041 return __connman_error_invalid_arguments(msg);
3043 err = update_proxy_configuration(service, &value);
3046 return __connman_error_failed(msg, -err);
3048 proxy_configuration_changed(service);
3050 __connman_notifier_proxy_changed(service);
3052 service_save(service);
3053 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
3054 g_str_equal(name, "IPv6.Configuration")) {
3056 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
3057 enum connman_service_state state =
3058 CONNMAN_SERVICE_STATE_UNKNOWN;
3063 if (service->ipconfig_ipv4 == NULL &&
3064 service->ipconfig_ipv6 == NULL)
3065 return __connman_error_invalid_property(msg);
3067 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
3068 ipv4 = service->ipconfig_ipv4;
3069 err = set_ipconfig(service, ipv4, &value,
3070 service->state_ipv4, &state);
3072 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
3073 ipv6 = service->ipconfig_ipv6;
3074 err = set_ipconfig(service, ipv6, &value,
3075 service->state_ipv6, &state);
3079 if (is_connected_state(service, state) ||
3080 is_connecting_state(service, state))
3081 __connman_network_set_ipconfig(service->network,
3083 return __connman_error_failed(msg, -err);
3087 ipv4_configuration_changed(service);
3089 ipv6_configuration_changed(service);
3091 if (is_connecting(service) || is_connected(service))
3092 __connman_network_set_ipconfig(service->network,
3095 service_save(service);
3097 return __connman_error_invalid_property(msg);
3099 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3102 static void set_idle(struct connman_service *service)
3104 service->state = service->state_ipv4 = service->state_ipv6 =
3105 CONNMAN_SERVICE_STATE_IDLE;
3106 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3107 state_changed(service);
3110 static DBusMessage *clear_property(DBusConnection *conn,
3111 DBusMessage *msg, void *user_data)
3113 struct connman_service *service = user_data;
3116 DBG("service %p", service);
3118 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
3121 if (g_str_equal(name, "Error") == TRUE) {
3124 g_get_current_time(&service->modified);
3125 service_save(service);
3127 return __connman_error_invalid_property(msg);
3129 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3132 static connman_bool_t is_ignore(struct connman_service *service)
3134 if (service->autoconnect == FALSE)
3137 if (service->roaming == TRUE)
3140 if (service->ignore == TRUE)
3143 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3149 struct preferred_tech_data {
3150 GSequence *preferred_list;
3151 enum connman_service_type type;
3154 static void preferred_tech_add_by_type(gpointer data, gpointer user_data)
3156 struct connman_service *service = data;
3157 struct preferred_tech_data *tech_data = user_data;
3159 if (service->type == tech_data->type) {
3160 g_sequence_append(tech_data->preferred_list, service);
3162 DBG("type %d service %p %s", tech_data->type, service,
3167 static GSequence* preferred_tech_list_get(GSequence *list)
3169 unsigned int *tech_array;
3170 struct preferred_tech_data tech_data;
3173 tech_array = connman_setting_get_uint_list("PreferredTechnologies");
3174 if (tech_array == NULL)
3177 tech_data.preferred_list = g_sequence_new(NULL);
3179 for (i = 0; tech_array[i] != 0; i += 1) {
3180 tech_data.type = tech_array[i];
3181 g_sequence_foreach(service_list, preferred_tech_add_by_type,
3185 return tech_data.preferred_list;
3188 static connman_bool_t auto_connect_service(GSequenceIter* iter,
3189 connman_bool_t preferred)
3191 struct connman_service *service = NULL;
3193 while (g_sequence_iter_is_end(iter) == FALSE) {
3194 service = g_sequence_get(iter);
3196 if (service->pending != NULL)
3199 if (is_connecting(service) == TRUE)
3202 if (service->favorite == FALSE) {
3203 if (preferred == TRUE)
3208 if (is_connected(service) == TRUE) {
3209 if (preferred == TRUE && service->state !=
3210 CONNMAN_SERVICE_STATE_ONLINE)
3215 if (is_ignore(service) == FALSE && service->state ==
3216 CONNMAN_SERVICE_STATE_IDLE)
3222 iter = g_sequence_iter_next(iter);
3225 if (service != NULL) {
3227 DBG("service %p %s %s", service, service->name,
3228 (preferred == TRUE)? "preferred": "auto");
3230 service->userconnect = FALSE;
3231 __connman_service_connect(service);
3237 static gboolean run_auto_connect(gpointer data)
3239 GSequenceIter *iter = NULL;
3240 GSequence *preferred_tech;
3242 autoconnect_timeout = 0;
3246 preferred_tech = preferred_tech_list_get(service_list);
3247 if (preferred_tech != NULL)
3248 iter = g_sequence_get_begin_iter(preferred_tech);
3250 if (iter == NULL || auto_connect_service(iter, TRUE) == FALSE)
3251 iter = g_sequence_get_begin_iter(service_list);
3254 auto_connect_service(iter, FALSE);
3256 if (preferred_tech != NULL)
3257 g_sequence_free(preferred_tech);
3262 void __connman_service_auto_connect(void)
3266 if (__connman_session_mode() == TRUE) {
3267 DBG("Session mode enabled: auto connect disabled");
3271 if (autoconnect_timeout != 0)
3274 autoconnect_timeout = g_timeout_add_seconds(0, run_auto_connect, NULL);
3277 static void remove_timeout(struct connman_service *service)
3279 if (service->timeout > 0) {
3280 g_source_remove(service->timeout);
3281 service->timeout = 0;
3285 static void reply_pending(struct connman_service *service, int error)
3287 remove_timeout(service);
3289 if (service->pending != NULL) {
3293 reply = __connman_error_failed(service->pending,
3296 g_dbus_send_message(connection, reply);
3300 sender = dbus_message_get_interface(service->pending);
3302 DBG("sender %s", sender);
3304 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
3305 g_dbus_send_reply(connection, service->pending,
3306 DBUS_TYPE_OBJECT_PATH, &service->path,
3309 g_dbus_send_reply(connection, service->pending,
3313 dbus_message_unref(service->pending);
3314 service->pending = NULL;
3318 static gboolean connect_timeout(gpointer user_data)
3320 struct connman_service *service = user_data;
3321 connman_bool_t autoconnect = FALSE;
3323 DBG("service %p", service);
3325 service->timeout = 0;
3327 if (service->network != NULL)
3328 __connman_network_disconnect(service->network);
3330 __connman_ipconfig_disable(service->ipconfig_ipv4);
3331 __connman_ipconfig_disable(service->ipconfig_ipv6);
3333 __connman_stats_service_unregister(service);
3335 if (service->pending != NULL) {
3338 reply = __connman_error_operation_timeout(service->pending);
3340 g_dbus_send_message(connection, reply);
3342 dbus_message_unref(service->pending);
3343 service->pending = NULL;
3347 __connman_service_ipconfig_indicate_state(service,
3348 CONNMAN_SERVICE_STATE_FAILURE,
3349 CONNMAN_IPCONFIG_TYPE_IPV4);
3350 __connman_service_ipconfig_indicate_state(service,
3351 CONNMAN_SERVICE_STATE_FAILURE,
3352 CONNMAN_IPCONFIG_TYPE_IPV6);
3354 if (autoconnect == TRUE && service->userconnect == FALSE)
3355 __connman_service_auto_connect();
3360 static void set_reconnect_state(struct connman_service *service,
3361 connman_bool_t reconnect)
3363 struct connman_device *device;
3365 if (service->network == NULL)
3368 device = connman_network_get_device(service->network);
3372 __connman_device_set_reconnect(device, reconnect);
3375 static connman_bool_t get_reconnect_state(struct connman_service *service)
3377 struct connman_device *device;
3379 if (service->network == NULL)
3382 device = connman_network_get_device(service->network);
3386 return __connman_device_get_reconnect(device);
3389 static DBusMessage *connect_service(DBusConnection *conn,
3390 DBusMessage *msg, void *user_data)
3392 struct connman_service *service = user_data;
3393 GSequenceIter *iter;
3396 DBG("service %p", service);
3398 if (service->pending != NULL)
3399 return __connman_error_in_progress(msg);
3401 iter = g_sequence_get_begin_iter(service_list);
3403 while (g_sequence_iter_is_end(iter) == FALSE) {
3404 struct connman_service *temp = g_sequence_get(iter);
3406 if (service->type == temp->type && is_connecting(temp) == TRUE)
3407 return __connman_error_in_progress(msg);
3409 iter = g_sequence_iter_next(iter);
3412 service->ignore = FALSE;
3414 service->userconnect = TRUE;
3416 service->pending = dbus_message_ref(msg);
3418 set_reconnect_state(service, FALSE);
3420 err = __connman_service_connect(service);
3422 if (service->pending == NULL)
3425 if (err != -EINPROGRESS) {
3426 dbus_message_unref(service->pending);
3427 service->pending = NULL;
3429 return __connman_error_failed(msg, -err);
3435 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3438 static DBusMessage *disconnect_service(DBusConnection *conn,
3439 DBusMessage *msg, void *user_data)
3441 struct connman_service *service = user_data;
3444 DBG("service %p", service);
3446 reply_pending(service, ECONNABORTED);
3448 service->ignore = TRUE;
3450 set_reconnect_state(service, FALSE);
3452 err = __connman_service_disconnect(service);
3454 if (err != -EINPROGRESS)
3455 return __connman_error_failed(msg, -err);
3460 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3463 static DBusMessage *remove_service(DBusConnection *conn,
3464 DBusMessage *msg, void *user_data)
3466 struct connman_service *service = user_data;
3468 DBG("service %p", service);
3470 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3471 return __connman_error_not_supported(msg);
3473 if (service->immutable == TRUE || service->hidden == TRUE)
3474 return __connman_error_not_supported(msg);
3476 if (service->favorite == FALSE && service->state !=
3477 CONNMAN_SERVICE_STATE_FAILURE)
3478 return __connman_error_not_supported(msg);
3480 set_reconnect_state(service, FALSE);
3482 __connman_service_disconnect(service);
3484 g_free(service->passphrase);
3485 service->passphrase = NULL;
3489 __connman_service_set_favorite(service, FALSE);
3490 service_save(service);
3492 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3495 static gboolean check_suitable_state(enum connman_service_state a,
3496 enum connman_service_state b)
3499 * Special check so that "ready" service can be moved before
3502 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3503 b == CONNMAN_SERVICE_STATE_READY) ||
3504 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3505 a == CONNMAN_SERVICE_STATE_READY))
3511 static void downgrade_state(struct connman_service *service)
3513 if (service == NULL)
3516 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3517 service->state_ipv6);
3519 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3520 __connman_service_ipconfig_indicate_state(service,
3521 CONNMAN_SERVICE_STATE_READY,
3522 CONNMAN_IPCONFIG_TYPE_IPV4);
3524 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3525 __connman_service_ipconfig_indicate_state(service,
3526 CONNMAN_SERVICE_STATE_READY,
3527 CONNMAN_IPCONFIG_TYPE_IPV6);
3530 static void apply_relevant_default_downgrade(struct connman_service *service)
3532 struct connman_service *def_service;
3534 def_service = __connman_service_get_default();
3535 if (def_service == NULL)
3538 if (def_service == service &&
3539 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3540 def_service->state = CONNMAN_SERVICE_STATE_READY;
3543 static void switch_default_service(struct connman_service *default_service,
3544 struct connman_service *downgrade_service)
3546 GSequenceIter *src, *dst;
3548 apply_relevant_default_downgrade(default_service);
3549 src = g_hash_table_lookup(service_hash, downgrade_service->identifier);
3550 dst = g_hash_table_lookup(service_hash, default_service->identifier);
3551 g_sequence_move(src, dst);
3552 downgrade_state(downgrade_service);
3555 static DBusMessage *move_service(DBusConnection *conn,
3556 DBusMessage *msg, void *user_data,
3559 struct connman_service *service = user_data;
3560 struct connman_service *target;
3562 enum connman_ipconfig_method target4, target6;
3563 enum connman_ipconfig_method service4, service6;
3565 DBG("service %p", service);
3567 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3570 if (service->favorite == FALSE)
3571 return __connman_error_not_supported(msg);
3573 target = find_service(path);
3574 if (target == NULL || target->favorite == FALSE || target == service)
3575 return __connman_error_invalid_service(msg);
3577 if (target->type == CONNMAN_SERVICE_TYPE_VPN) {
3579 * We only allow VPN route splitting if there are
3580 * routes defined for a given VPN.
3582 if (__connman_provider_check_routes(target->provider)
3584 connman_info("Cannot move service. "
3585 "No routes defined for provider %s",
3586 __connman_provider_get_ident(target->provider));
3587 return __connman_error_invalid_service(msg);
3590 target->do_split_routing = TRUE;
3592 target->do_split_routing = FALSE;
3594 service->do_split_routing = FALSE;
3596 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3597 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3598 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3599 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3601 DBG("target %s method %d/%d state %d/%d split %d", target->identifier,
3602 target4, target6, target->state_ipv4, target->state_ipv6,
3603 target->do_split_routing);
3605 DBG("service %s method %d/%d state %d/%d", service->identifier,
3607 service->state_ipv4, service->state_ipv6);
3610 * If method is OFF, then we do not need to check the corresponding
3613 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3614 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3615 if (check_suitable_state(target->state_ipv6,
3616 service->state_ipv6) == FALSE)
3617 return __connman_error_invalid_service(msg);
3621 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3622 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3623 if (check_suitable_state(target->state_ipv4,
3624 service->state_ipv4) == FALSE)
3625 return __connman_error_invalid_service(msg);
3629 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3630 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3631 if (check_suitable_state(target->state_ipv6,
3632 service->state_ipv6) == FALSE)
3633 return __connman_error_invalid_service(msg);
3637 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3638 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3639 if (check_suitable_state(target->state_ipv4,
3640 service->state_ipv4) == FALSE)
3641 return __connman_error_invalid_service(msg);
3645 g_get_current_time(&service->modified);
3646 service_save(service);
3647 service_save(target);
3650 * If the service which goes down is the default service and is
3651 * online, we downgrade directly its state to ready so:
3652 * the service which goes up, needs to recompute its state which
3653 * is triggered via downgrading it - if relevant - to state ready.
3656 switch_default_service(target, service);
3658 switch_default_service(service, target);
3660 __connman_connection_update_gateway();
3662 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3665 static DBusMessage *move_before(DBusConnection *conn,
3666 DBusMessage *msg, void *user_data)
3668 return move_service(conn, msg, user_data, TRUE);
3671 static DBusMessage *move_after(DBusConnection *conn,
3672 DBusMessage *msg, void *user_data)
3674 return move_service(conn, msg, user_data, FALSE);
3677 static DBusMessage *reset_counters(DBusConnection *conn,
3678 DBusMessage *msg, void *user_data)
3680 struct connman_service *service = user_data;
3682 reset_stats(service);
3684 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3687 static struct _services_notify {
3693 static void service_append_added_foreach(gpointer data, gpointer user_data)
3695 struct connman_service *service = data;
3696 DBusMessageIter *iter = user_data;
3698 if (service == NULL || service->path == NULL) {
3699 DBG("service %p or path is NULL", service);
3703 if (g_hash_table_lookup(services_notify->add, service->path) != NULL) {
3704 DBG("new %s", service->path);
3706 append_struct(service, iter);
3707 g_hash_table_remove(services_notify->add, service->path);
3709 DBG("changed %s", service->path);
3711 append_struct_service(iter, NULL, service);
3715 static void service_append_ordered(DBusMessageIter *iter, void *user_data)
3717 if (service_list != NULL)
3718 g_sequence_foreach(service_list,
3719 service_append_added_foreach, iter);
3722 static void append_removed(gpointer key, gpointer value, gpointer user_data)
3724 char *objpath = key;
3725 DBusMessageIter *iter = user_data;
3727 DBG("removed %s", objpath);
3728 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath);
3731 static gboolean service_send_changed(gpointer data)
3733 DBusMessage *signal;
3734 DBusMessageIter iter, array;
3738 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
3739 CONNMAN_MANAGER_INTERFACE, "ServicesChanged");
3743 __connman_dbus_append_objpath_dict_array(signal,
3744 service_append_ordered, NULL);
3746 dbus_message_iter_init_append(signal, &iter);
3747 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
3748 DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
3750 g_hash_table_foreach(services_notify->remove, append_removed, &array);
3752 dbus_message_iter_close_container(&iter, &array);
3754 dbus_connection_send(connection, signal, NULL);
3755 dbus_message_unref(signal);
3757 g_hash_table_remove_all(services_notify->remove);
3758 g_hash_table_remove_all(services_notify->add);
3760 services_notify->id = 0;
3764 static void service_schedule_changed(void)
3766 if (services_notify->id != 0)
3769 services_notify->id = g_timeout_add(100, service_send_changed, NULL);
3772 static void service_schedule_added(struct connman_service *service)
3774 DBG("service %p", service);
3776 g_hash_table_remove(services_notify->remove, service->path);
3777 g_hash_table_replace(services_notify->add, service->path, service);
3779 service_schedule_changed();
3782 static void service_schedule_removed(struct connman_service *service)
3784 DBG("service %p %s", service, service->path);
3786 if (service == NULL || service->path == NULL) {
3787 DBG("service %p or path is NULL", service);
3791 g_hash_table_remove(services_notify->add, service->path);
3792 g_hash_table_replace(services_notify->remove, g_strdup(service->path),
3795 service_schedule_changed();
3798 static GDBusMethodTable service_methods[] = {
3799 { "GetProperties", "", "a{sv}", get_properties },
3800 { "SetProperty", "sv", "", set_property },
3801 { "ClearProperty", "s", "", clear_property },
3802 { "Connect", "", "", connect_service,
3803 G_DBUS_METHOD_FLAG_ASYNC },
3804 { "Disconnect", "", "", disconnect_service },
3805 { "Remove", "", "", remove_service },
3806 { "MoveBefore", "o", "", move_before },
3807 { "MoveAfter", "o", "", move_after },
3808 { "ResetCounters", "", "", reset_counters },
3812 static GDBusSignalTable service_signals[] = {
3813 { "PropertyChanged", "sv" },
3817 static void service_free(gpointer user_data)
3819 struct connman_service *service = user_data;
3820 char *path = service->path;
3822 DBG("service %p", service);
3824 reply_pending(service, ENOENT);
3826 g_hash_table_remove(service_hash, service->identifier);
3828 __connman_notifier_service_remove(service);
3829 service_schedule_removed(service);
3831 stats_stop(service);
3833 service->path = NULL;
3836 __connman_connection_update_gateway();
3838 g_dbus_unregister_interface(connection, path,
3839 CONNMAN_SERVICE_INTERFACE);
3843 g_hash_table_destroy(service->counter_table);
3845 if (service->network != NULL) {
3846 __connman_network_disconnect(service->network);
3847 connman_network_unref(service->network);
3848 service->network = NULL;
3851 if (service->provider != NULL)
3852 connman_provider_unref(service->provider);
3854 if (service->ipconfig_ipv4 != NULL) {
3855 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3856 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3857 __connman_ipconfig_unref(service->ipconfig_ipv4);
3858 service->ipconfig_ipv4 = NULL;
3861 if (service->ipconfig_ipv6 != NULL) {
3862 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3863 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3864 __connman_ipconfig_unref(service->ipconfig_ipv6);
3865 service->ipconfig_ipv6 = NULL;
3868 g_strfreev(service->timeservers);
3869 g_strfreev(service->timeservers_config);
3870 g_strfreev(service->nameservers);
3871 g_strfreev(service->nameservers_config);
3872 g_strfreev(service->nameservers_auto);
3873 g_strfreev(service->domains);
3874 g_strfreev(service->proxies);
3875 g_strfreev(service->excludes);
3877 g_free(service->domainname);
3878 g_free(service->pac);
3879 g_free(service->name);
3880 g_free(service->passphrase);
3881 g_free(service->agent_passphrase);
3882 g_free(service->identifier);
3883 g_free(service->eap);
3884 g_free(service->identity);
3885 g_free(service->agent_identity);
3886 g_free(service->ca_cert_file);
3887 g_free(service->client_cert_file);
3888 g_free(service->private_key_file);
3889 g_free(service->private_key_passphrase);
3890 g_free(service->phase2);
3892 if (service->stats.timer != NULL)
3893 g_timer_destroy(service->stats.timer);
3894 if (service->stats_roaming.timer != NULL)
3895 g_timer_destroy(service->stats_roaming.timer);
3900 static void stats_init(struct connman_service *service)
3903 service->stats.valid = FALSE;
3904 service->stats.enabled = FALSE;
3905 service->stats.timer = g_timer_new();
3908 service->stats_roaming.valid = FALSE;
3909 service->stats_roaming.enabled = FALSE;
3910 service->stats_roaming.timer = g_timer_new();
3913 static void service_initialize(struct connman_service *service)
3915 DBG("service %p", service);
3917 service->refcount = 1;
3918 service->session_usage_count = 0;
3920 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3921 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3923 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3924 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3925 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3927 service->favorite = FALSE;
3928 service->immutable = FALSE;
3929 service->hidden = FALSE;
3931 service->ignore = FALSE;
3933 service->userconnect = FALSE;
3937 stats_init(service);
3939 service->provider = NULL;
3941 service->wps = FALSE;
3945 * connman_service_create:
3947 * Allocate a new service.
3949 * Returns: a newly-allocated #connman_service structure
3951 struct connman_service *connman_service_create(void)
3954 struct connman_stats_counter *counters;
3955 const char *counter;
3957 struct connman_service *service;
3959 service = g_try_new0(struct connman_service, 1);
3960 if (service == NULL)
3963 DBG("service %p", service);
3965 service->counter_table = g_hash_table_new_full(g_str_hash,
3966 g_str_equal, NULL, g_free);
3968 for (list = counter_list; list; list = list->next) {
3969 counter = list->data;
3971 counters = g_try_new0(struct connman_stats_counter, 1);
3972 if (counters == NULL) {
3973 g_hash_table_destroy(service->counter_table);
3978 counters->append_all = TRUE;
3980 g_hash_table_replace(service->counter_table, (gpointer)counter,
3984 service_initialize(service);
3990 * connman_service_ref:
3991 * @service: service structure
3993 * Increase reference counter of service
3995 struct connman_service *
3996 connman_service_ref_debug(struct connman_service *service,
3997 const char *file, int line, const char *caller)
3999 DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
4000 file, line, caller);
4002 __sync_fetch_and_add(&service->refcount, 1);
4008 * connman_service_unref:
4009 * @service: service structure
4011 * Decrease reference counter of service and release service if no
4014 void connman_service_unref_debug(struct connman_service *service,
4015 const char *file, int line, const char *caller)
4017 GSequenceIter *iter;
4019 DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
4020 file, line, caller);
4022 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
4025 iter = g_hash_table_lookup(service_hash, service->identifier);
4027 reply_pending(service, ECONNABORTED);
4029 __connman_service_disconnect(service);
4031 g_sequence_remove(iter);
4033 service_free(service);
4037 static gint service_compare(gconstpointer a, gconstpointer b,
4040 struct connman_service *service_a = (void *) a;
4041 struct connman_service *service_b = (void *) b;
4042 enum connman_service_state state_a, state_b;
4044 state_a = service_a->state;
4045 state_b = service_b->state;
4047 if (state_a != state_b) {
4048 gboolean a_connected = is_connected(service_a);
4049 gboolean b_connected = is_connected(service_b);
4051 if (a_connected == TRUE && b_connected == TRUE) {
4052 /* We prefer online over ready state */
4053 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
4056 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
4060 if (a_connected == TRUE)
4062 if (b_connected == TRUE)
4065 if (is_connecting(service_a) == TRUE)
4067 if (is_connecting(service_b) == TRUE)
4071 if (service_a->order > service_b->order)
4074 if (service_a->order < service_b->order)
4077 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
4080 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
4083 if (service_a->type != service_b->type) {
4084 switch (service_a->type) {
4085 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4086 case CONNMAN_SERVICE_TYPE_SYSTEM:
4087 case CONNMAN_SERVICE_TYPE_ETHERNET:
4088 case CONNMAN_SERVICE_TYPE_GPS:
4089 case CONNMAN_SERVICE_TYPE_VPN:
4090 case CONNMAN_SERVICE_TYPE_GADGET:
4092 case CONNMAN_SERVICE_TYPE_WIFI:
4094 case CONNMAN_SERVICE_TYPE_WIMAX:
4095 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4096 case CONNMAN_SERVICE_TYPE_CELLULAR:
4101 return (gint) service_b->strength - (gint) service_a->strength;
4105 * connman_service_get_type:
4106 * @service: service structure
4108 * Get the type of service
4110 enum connman_service_type connman_service_get_type(struct connman_service *service)
4112 if (service == NULL)
4113 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4115 return service->type;
4119 * connman_service_get_interface:
4120 * @service: service structure
4122 * Get network interface of service
4124 char *connman_service_get_interface(struct connman_service *service)
4128 if (service == NULL)
4131 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
4132 if (service->ipconfig_ipv4)
4133 index = __connman_ipconfig_get_index(
4134 service->ipconfig_ipv4);
4135 else if (service->ipconfig_ipv6)
4136 index = __connman_ipconfig_get_index(
4137 service->ipconfig_ipv6);
4141 return connman_inet_ifname(index);
4144 if (service->network == NULL)
4147 index = connman_network_get_index(service->network);
4149 return connman_inet_ifname(index);
4153 * connman_service_get_network:
4154 * @service: service structure
4156 * Get the service network
4158 struct connman_network *
4159 __connman_service_get_network(struct connman_service *service)
4161 if (service == NULL)
4164 return service->network;
4167 struct connman_ipconfig *
4168 __connman_service_get_ip4config(struct connman_service *service)
4170 if (service == NULL)
4173 return service->ipconfig_ipv4;
4176 struct connman_ipconfig *
4177 __connman_service_get_ip6config(struct connman_service *service)
4179 if (service == NULL)
4182 return service->ipconfig_ipv6;
4185 struct connman_ipconfig *
4186 __connman_service_get_ipconfig(struct connman_service *service, int family)
4188 if (family == AF_INET)
4189 return __connman_service_get_ip4config(service);
4190 else if (family == AF_INET6)
4191 return __connman_service_get_ip6config(service);
4197 connman_bool_t __connman_service_is_connected_state(struct connman_service *service,
4198 enum connman_ipconfig_type type)
4200 if (service == NULL)
4204 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
4206 case CONNMAN_IPCONFIG_TYPE_IPV4:
4207 return is_connected_state(service, service->state_ipv4);
4208 case CONNMAN_IPCONFIG_TYPE_IPV6:
4209 return is_connected_state(service, service->state_ipv6);
4214 enum connman_service_security __connman_service_get_security(struct connman_service *service)
4216 if (service == NULL)
4217 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4219 return service->security;
4222 const char *__connman_service_get_phase2(struct connman_service *service)
4224 if (service == NULL)
4227 return service->phase2;
4230 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
4232 if (service == NULL)
4235 return service->wps;
4239 * __connman_service_set_favorite:
4240 * @service: service structure
4241 * @favorite: favorite value
4243 * Change the favorite setting of service
4245 int __connman_service_set_favorite(struct connman_service *service,
4246 connman_bool_t favorite)
4248 GSequenceIter *iter;
4250 if (service->hidden == TRUE)
4252 iter = g_hash_table_lookup(service_hash, service->identifier);
4256 if (service->favorite == favorite)
4259 service->favorite = favorite;
4260 service->order = __connman_service_get_order(service);
4262 favorite_changed(service);
4264 if (g_sequence_get_length(service_list) > 1) {
4265 g_sequence_sort_changed(iter, service_compare, NULL);
4266 service_schedule_changed();
4269 __connman_connection_update_gateway();
4274 int __connman_service_set_immutable(struct connman_service *service,
4275 connman_bool_t immutable)
4277 if (service->hidden == TRUE)
4279 service->immutable = immutable;
4281 immutable_changed(service);
4286 void __connman_service_set_string(struct connman_service *service,
4287 const char *key, const char *value)
4289 if (service->hidden == TRUE)
4291 if (g_str_equal(key, "EAP") == TRUE) {
4292 g_free(service->eap);
4293 service->eap = g_strdup(value);
4294 } else if (g_str_equal(key, "Identity") == TRUE) {
4295 g_free(service->identity);
4296 service->identity = g_strdup(value);
4297 } else if (g_str_equal(key, "CACertFile") == TRUE) {
4298 g_free(service->ca_cert_file);
4299 service->ca_cert_file = g_strdup(value);
4300 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
4301 g_free(service->client_cert_file);
4302 service->client_cert_file = g_strdup(value);
4303 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
4304 g_free(service->private_key_file);
4305 service->private_key_file = g_strdup(value);
4306 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
4307 g_free(service->private_key_passphrase);
4308 service->private_key_passphrase = g_strdup(value);
4309 } else if (g_str_equal(key, "Phase2") == TRUE) {
4310 g_free(service->phase2);
4311 service->phase2 = g_strdup(value);
4312 } else if (g_str_equal(key, "Passphrase") == TRUE) {
4313 g_free(service->passphrase);
4314 service->passphrase = g_strdup(value);
4318 void __connman_service_set_userconnect(struct connman_service *service,
4319 connman_bool_t userconnect)
4321 if (service != NULL)
4322 service->userconnect = userconnect;
4325 static void service_complete(struct connman_service *service)
4327 reply_pending(service, EIO);
4329 if (service->userconnect == FALSE)
4330 __connman_service_auto_connect();
4332 g_get_current_time(&service->modified);
4333 service_save(service);
4336 static void report_error_cb(struct connman_service *service,
4337 gboolean retry, void *user_data)
4340 __connman_service_connect(service);
4342 service_complete(service);
4343 __connman_connection_update_gateway();
4347 int __connman_service_add_passphrase(struct connman_service *service,
4348 const gchar *passphrase)
4352 switch (service->security) {
4353 case CONNMAN_SERVICE_SECURITY_WEP:
4354 case CONNMAN_SERVICE_SECURITY_PSK:
4355 err = __connman_service_set_passphrase(service, passphrase);
4357 case CONNMAN_SERVICE_SECURITY_8021X:
4358 __connman_service_set_agent_passphrase(service,
4361 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4362 case CONNMAN_SERVICE_SECURITY_NONE:
4363 case CONNMAN_SERVICE_SECURITY_WPA:
4364 case CONNMAN_SERVICE_SECURITY_RSN:
4365 DBG("service security '%s' (%d) not handled",
4366 security2string(service->security),
4374 static int check_wpspin(struct connman_service *service, const char *wpspin)
4382 length = strlen(wpspin);
4384 /* If 0, it will mean user wants to use PBC method */
4386 connman_network_set_string(service->network,
4387 "WiFi.PinWPS", NULL);
4391 /* A WPS PIN is always 8 chars length,
4392 * its content is in digit representation.
4397 for (i = 0; i < 8; i++)
4398 if (!isdigit((unsigned char) wpspin[i]))
4401 connman_network_set_string(service->network, "WiFi.PinWPS", wpspin);
4406 static void request_input_cb (struct connman_service *service,
4407 connman_bool_t values_received,
4408 const char *name, int name_len,
4409 const char *identity, const char *passphrase,
4410 gboolean wps, const char *wpspin,
4411 const char *error, void *user_data)
4413 struct connman_device *device;
4416 DBG ("RequestInput return, %p", service);
4418 if (error != NULL) {
4419 DBG("error: %s", error);
4421 if (g_strcmp0(error,
4422 "net.connman.Agent.Error.Canceled") == 0) {
4428 if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
4429 device = connman_network_get_device(service->network);
4430 __connman_device_request_hidden_scan(device,
4432 identity, passphrase);
4435 if (values_received == FALSE || service->hidden == TRUE) {
4440 if (wps == TRUE && service->network != NULL) {
4441 err = check_wpspin(service, wpspin);
4445 connman_network_set_bool(service->network, "WiFi.UseWPS", wps);
4448 if (identity != NULL)
4449 __connman_service_set_agent_identity(service, identity);
4451 if (passphrase != NULL)
4452 err = __connman_service_add_passphrase(service, passphrase);
4456 __connman_service_connect(service);
4458 /* Never cache agent provided credentials */
4459 __connman_service_set_agent_identity(service, NULL);
4460 __connman_service_set_agent_passphrase(service, NULL);
4461 } else if (err == -ENOKEY) {
4462 __connman_service_indicate_error(service,
4463 CONNMAN_SERVICE_ERROR_INVALID_KEY);
4464 __connman_agent_report_error(service,
4465 error2string(service->error),
4466 report_error_cb, NULL);
4467 } else if (err == -EINVAL) {
4468 service_complete(service);
4469 __connman_connection_update_gateway();
4473 static void downgrade_connected_services(void)
4475 struct connman_service *up_service;
4476 GSequenceIter *iter;
4478 iter = g_sequence_get_begin_iter(service_list);
4479 while (g_sequence_iter_is_end(iter) == FALSE) {
4480 up_service = g_sequence_get(iter);
4482 if (is_connected(up_service) == FALSE) {
4483 iter = g_sequence_iter_next(iter);
4487 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4490 downgrade_state(up_service);
4492 iter = g_sequence_iter_next(iter);
4496 static int service_update_preferred_order(struct connman_service *default_service,
4497 struct connman_service *new_service,
4498 enum connman_service_state new_state)
4500 unsigned int *tech_array;
4503 if (default_service == NULL || default_service == new_service ||
4504 default_service->state != new_state )
4507 tech_array = connman_setting_get_uint_list("PreferredTechnologies");
4508 if (tech_array != NULL) {
4510 for (i = 0; tech_array[i] != 0; i += 1) {
4511 if (default_service->type == tech_array[i])
4514 if (new_service->type == tech_array[i]) {
4515 switch_default_service(new_service,
4526 static int service_indicate_state(struct connman_service *service)
4528 enum connman_service_state old_state, new_state;
4529 struct connman_service *def_service;
4531 GSequenceIter *iter;
4533 if (service == NULL)
4536 old_state = service->state;
4537 new_state = combine_state(service->state_ipv4, service->state_ipv6);
4539 DBG("service %p old %s - new %s/%s => %s",
4541 state2string(old_state),
4542 state2string(service->state_ipv4),
4543 state2string(service->state_ipv6),
4544 state2string(new_state));
4546 if (old_state == new_state)
4549 def_service = __connman_service_get_default();
4551 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4552 result = service_update_preferred_order(def_service,
4553 service, new_state);
4554 if (result == -EALREADY)
4556 if (result == -EAGAIN)
4557 __connman_service_auto_connect();
4560 if (old_state == CONNMAN_SERVICE_STATE_ONLINE)
4561 __connman_notifier_leave_online(service->type);
4563 service->state = new_state;
4564 state_changed(service);
4566 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4567 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4568 reply_pending(service, ECONNABORTED);
4570 __connman_service_disconnect(service);
4573 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4574 if (service->new_service == FALSE &&
4575 __connman_stats_service_register(service) == 0) {
4577 * For new services the statistics are updated after
4578 * we have successfully connected.
4580 __connman_stats_get(service, FALSE,
4581 &service->stats.data);
4582 __connman_stats_get(service, TRUE,
4583 &service->stats_roaming.data);
4587 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4588 connman_bool_t reconnect;
4590 reconnect = get_reconnect_state(service);
4591 if (reconnect == TRUE)
4592 __connman_service_auto_connect();
4595 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4596 enum connman_ipconfig_method method;
4598 if (service->new_service == TRUE &&
4599 __connman_stats_service_register(service) == 0) {
4601 * This is normally done after configuring state
4602 * but for new service do this after we have connected
4605 __connman_stats_get(service, FALSE,
4606 &service->stats.data);
4607 __connman_stats_get(service, TRUE,
4608 &service->stats_roaming.data);
4611 service->new_service = FALSE;
4613 service_update_preferred_order(def_service, service, new_state);
4615 set_reconnect_state(service, TRUE);
4617 __connman_service_set_favorite(service, TRUE);
4619 reply_pending(service, 0);
4621 service->userconnect = FALSE;
4623 g_get_current_time(&service->modified);
4624 service_save(service);
4626 update_nameservers(service);
4627 dns_changed(service);
4628 domain_changed(service);
4630 __connman_notifier_connect(service->type);
4632 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4633 connman_network_get_bool(service->network,
4634 "WiFi.UseWPS") == TRUE) {
4637 pass = connman_network_get_string(service->network,
4640 __connman_service_set_passphrase(service, pass);
4642 connman_network_set_bool(service->network,
4643 "WiFi.UseWPS", FALSE);
4648 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4649 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4650 __connman_ipconfig_disable_ipv6(
4651 service->ipconfig_ipv6);
4653 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4654 def_service = __connman_service_get_default();
4656 if (__connman_notifier_is_connected() == FALSE &&
4657 def_service != NULL &&
4658 def_service->provider != NULL)
4659 __connman_provider_disconnect(def_service->provider);
4663 __connman_wispr_stop(service);
4665 __connman_wpad_stop(service);
4667 update_nameservers(service);
4668 dns_changed(service);
4669 domain_changed(service);
4671 __connman_notifier_disconnect(service->type);
4674 * Previous services which are connected and which states
4675 * are set to online should reset relevantly ipconfig_state
4676 * to ready so wispr/portal will be rerun on those
4678 downgrade_connected_services();
4681 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4682 if (service->userconnect == TRUE &&
4683 __connman_agent_report_error(service,
4684 error2string(service->error),
4685 report_error_cb, NULL) == -EIO)
4687 service_complete(service);
4689 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4691 iter = g_hash_table_lookup(service_hash, service->identifier);
4692 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
4693 g_sequence_sort_changed(iter, service_compare, NULL);
4694 service_schedule_changed();
4697 __connman_connection_update_gateway();
4699 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4700 __connman_notifier_enter_online(service->type);
4707 int __connman_service_indicate_error(struct connman_service *service,
4708 enum connman_service_error error)
4710 DBG("service %p error %d", service, error);
4712 if (service == NULL)
4715 service->error = error;
4717 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4718 __connman_service_set_passphrase(service, NULL);
4720 __connman_service_ipconfig_indicate_state(service,
4721 CONNMAN_SERVICE_STATE_FAILURE,
4722 CONNMAN_IPCONFIG_TYPE_IPV4);
4723 __connman_service_ipconfig_indicate_state(service,
4724 CONNMAN_SERVICE_STATE_FAILURE,
4725 CONNMAN_IPCONFIG_TYPE_IPV6);
4729 int __connman_service_clear_error(struct connman_service *service)
4731 DBG("service %p", service);
4733 if (service == NULL)
4736 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4739 service->state_ipv4 = service->state_ipv6 =
4740 CONNMAN_SERVICE_STATE_UNKNOWN;
4741 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4743 if (service->favorite == TRUE)
4744 set_reconnect_state(service, TRUE);
4746 __connman_service_ipconfig_indicate_state(service,
4747 CONNMAN_SERVICE_STATE_IDLE,
4748 CONNMAN_IPCONFIG_TYPE_IPV6);
4751 * Toggling the IPv6 state to IDLE could trigger the auto connect
4752 * machinery and consequently the IPv4 state.
4754 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4755 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4758 return __connman_service_ipconfig_indicate_state(service,
4759 CONNMAN_SERVICE_STATE_IDLE,
4760 CONNMAN_IPCONFIG_TYPE_IPV4);
4763 int __connman_service_indicate_default(struct connman_service *service)
4765 DBG("service %p", service);
4772 enum connman_service_state __connman_service_ipconfig_get_state(
4773 struct connman_service *service,
4774 enum connman_ipconfig_type type)
4776 if (service == NULL)
4777 return CONNMAN_SERVICE_STATE_UNKNOWN;
4779 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4780 return service->state_ipv4;
4782 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4783 return service->state_ipv6;
4785 return CONNMAN_SERVICE_STATE_UNKNOWN;
4788 static void check_proxy_setup(struct connman_service *service)
4791 * We start WPAD if we haven't got a PAC URL from DHCP and
4792 * if our proxy manual configuration is either empty or set
4793 * to AUTO with an empty URL.
4796 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4799 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4800 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4801 service->pac != NULL))
4804 if (__connman_wpad_start(service) < 0) {
4805 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4806 __connman_notifier_proxy_changed(service);
4813 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4817 * How many networks are connected at the same time. If more than 1,
4818 * then set the rp_filter setting properly (loose mode routing) so that network
4819 * connectivity works ok. This is only done for IPv4 networks as IPv6
4820 * does not have rp_filter knob.
4822 static int connected_networks_count;
4823 static int original_rp_filter;
4825 static void service_rp_filter(struct connman_service *service,
4828 enum connman_ipconfig_method method;
4830 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4833 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4834 case CONNMAN_IPCONFIG_METHOD_OFF:
4835 case CONNMAN_IPCONFIG_METHOD_AUTO:
4837 case CONNMAN_IPCONFIG_METHOD_FIXED:
4838 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4839 case CONNMAN_IPCONFIG_METHOD_DHCP:
4843 if (connected == TRUE) {
4844 if (connected_networks_count == 1) {
4846 filter_value = __connman_ipconfig_set_rp_filter();
4847 if (filter_value < 0)
4850 original_rp_filter = filter_value;
4852 connected_networks_count++;
4855 if (connected_networks_count == 2)
4856 __connman_ipconfig_unset_rp_filter(original_rp_filter);
4858 connected_networks_count--;
4859 if (connected_networks_count < 0)
4860 connected_networks_count = 0;
4863 DBG("%s %s ipconfig %p method %d count %d filter %d",
4864 connected ? "connected" : "disconnected", service->identifier,
4865 service->ipconfig_ipv4, method,
4866 connected_networks_count, original_rp_filter);
4869 static gboolean redo_wispr(gpointer user_data)
4871 struct connman_service *service = user_data;
4875 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
4880 int __connman_service_online_check_failed(struct connman_service *service,
4881 enum connman_ipconfig_type type)
4883 DBG("service %p type %d count %d", service, type,
4884 service->online_check_count);
4886 /* currently we only retry IPv6 stuff */
4887 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 ||
4888 service->online_check_count != 1) {
4889 connman_warn("Online check failed for %p %s", service,
4894 service->online_check_count = 0;
4897 * We set the timeout to 1 sec so that we have a chance to get
4898 * necessary IPv6 router advertisement messages that might have
4901 g_timeout_add_seconds(1, redo_wispr, service);
4906 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4907 enum connman_service_state new_state,
4908 enum connman_ipconfig_type type)
4910 struct connman_ipconfig *ipconfig = NULL;
4911 enum connman_service_state old_state;
4914 if (service == NULL)
4917 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4918 old_state = service->state_ipv4;
4919 ipconfig = service->ipconfig_ipv4;
4920 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4921 old_state = service->state_ipv6;
4922 ipconfig = service->ipconfig_ipv6;
4925 if (ipconfig == NULL)
4929 if (old_state == new_state)
4932 DBG("service %p (%s) state %d (%s) type %d (%s)",
4933 service, service ? service->identifier : NULL,
4934 new_state, state2string(new_state),
4935 type, __connman_ipconfig_type2string(type));
4937 switch (new_state) {
4938 case CONNMAN_SERVICE_STATE_UNKNOWN:
4939 case CONNMAN_SERVICE_STATE_IDLE:
4940 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4943 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4945 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4946 __connman_ipconfig_enable(ipconfig);
4948 case CONNMAN_SERVICE_STATE_READY:
4949 update_nameservers(service);
4951 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4952 check_proxy_setup(service);
4953 service_rp_filter(service, TRUE);
4955 service->online_check_count = 1;
4956 __connman_wispr_start(service, type);
4959 case CONNMAN_SERVICE_STATE_ONLINE:
4961 case CONNMAN_SERVICE_STATE_DISCONNECT:
4962 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4965 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4966 service_rp_filter(service, FALSE);
4969 case CONNMAN_SERVICE_STATE_FAILURE:
4973 /* We keep that state */
4974 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4975 service->state_ipv4 = new_state;
4976 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4977 service->state_ipv6 = new_state;
4979 ret = service_indicate_state(service);
4982 * If the ipconfig method is OFF, then we set the state to IDLE
4983 * so that it will not affect the combined state in the future.
4985 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4986 enum connman_ipconfig_method method;
4987 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4988 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4989 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4990 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4991 ret = service_indicate_state(service);
4994 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4995 enum connman_ipconfig_method method;
4996 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4997 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4998 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4999 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5000 ret = service_indicate_state(service);
5007 static connman_bool_t prepare_network(struct connman_service *service)
5009 enum connman_network_type type;
5010 unsigned int ssid_len;
5012 type = connman_network_get_type(service->network);
5015 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5016 case CONNMAN_NETWORK_TYPE_VENDOR:
5018 case CONNMAN_NETWORK_TYPE_WIFI:
5019 if (connman_network_get_blob(service->network, "WiFi.SSID",
5023 if (service->passphrase != NULL)
5024 connman_network_set_string(service->network,
5025 "WiFi.Passphrase", service->passphrase);
5027 case CONNMAN_NETWORK_TYPE_ETHERNET:
5028 case CONNMAN_NETWORK_TYPE_WIMAX:
5029 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5030 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5031 case CONNMAN_NETWORK_TYPE_CELLULAR:
5038 static void prepare_8021x(struct connman_service *service)
5040 if (service->eap != NULL)
5041 connman_network_set_string(service->network, "WiFi.EAP",
5044 if (service->identity != NULL)
5045 connman_network_set_string(service->network, "WiFi.Identity",
5048 if (service->ca_cert_file != NULL)
5049 connman_network_set_string(service->network, "WiFi.CACertFile",
5050 service->ca_cert_file);
5052 if (service->client_cert_file != NULL)
5053 connman_network_set_string(service->network,
5054 "WiFi.ClientCertFile",
5055 service->client_cert_file);
5057 if (service->private_key_file != NULL)
5058 connman_network_set_string(service->network,
5059 "WiFi.PrivateKeyFile",
5060 service->private_key_file);
5062 if (service->private_key_passphrase != NULL)
5063 connman_network_set_string(service->network,
5064 "WiFi.PrivateKeyPassphrase",
5065 service->private_key_passphrase);
5067 if (service->phase2 != NULL)
5068 connman_network_set_string(service->network, "WiFi.Phase2",
5072 static int service_connect(struct connman_service *service)
5076 if (service->hidden == TRUE)
5079 switch (service->type) {
5080 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5081 case CONNMAN_SERVICE_TYPE_SYSTEM:
5082 case CONNMAN_SERVICE_TYPE_GPS:
5083 case CONNMAN_SERVICE_TYPE_GADGET:
5085 case CONNMAN_SERVICE_TYPE_ETHERNET:
5086 case CONNMAN_SERVICE_TYPE_WIMAX:
5087 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5088 case CONNMAN_SERVICE_TYPE_CELLULAR:
5089 case CONNMAN_SERVICE_TYPE_VPN:
5091 case CONNMAN_SERVICE_TYPE_WIFI:
5092 switch (service->security) {
5093 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5094 case CONNMAN_SERVICE_SECURITY_NONE:
5096 case CONNMAN_SERVICE_SECURITY_WEP:
5097 case CONNMAN_SERVICE_SECURITY_PSK:
5098 case CONNMAN_SERVICE_SECURITY_WPA:
5099 case CONNMAN_SERVICE_SECURITY_RSN:
5100 if (service->passphrase == NULL) {
5101 if (service->network == NULL)
5104 if (service->wps == FALSE ||
5105 connman_network_get_bool(
5107 "WiFi.UseWPS") == FALSE)
5111 case CONNMAN_SERVICE_SECURITY_8021X:
5112 if (service->eap == NULL)
5116 * never request credentials if using EAP-TLS
5117 * (EAP-TLS networks need to be fully provisioned)
5119 if (g_str_equal(service->eap, "tls") == TRUE)
5123 * Return -ENOKEY if either identity or passphrase is
5124 * missing. Agent provided credentials can be used as
5125 * fallback if needed.
5127 if ((service->identity == NULL &&
5128 service->agent_identity == NULL) ||
5129 (service->passphrase == NULL &&
5130 service->agent_passphrase == NULL))
5138 if (service->network != NULL) {
5139 if (prepare_network(service) == FALSE)
5142 switch (service->security) {
5143 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5144 case CONNMAN_SERVICE_SECURITY_NONE:
5145 case CONNMAN_SERVICE_SECURITY_WEP:
5146 case CONNMAN_SERVICE_SECURITY_PSK:
5147 case CONNMAN_SERVICE_SECURITY_WPA:
5148 case CONNMAN_SERVICE_SECURITY_RSN:
5150 case CONNMAN_SERVICE_SECURITY_8021X:
5151 prepare_8021x(service);
5155 if (__connman_stats_service_register(service) == 0) {
5156 __connman_stats_get(service, FALSE,
5157 &service->stats.data);
5158 __connman_stats_get(service, TRUE,
5159 &service->stats_roaming.data);
5162 if (service->ipconfig_ipv4)
5163 __connman_ipconfig_enable(service->ipconfig_ipv4);
5164 if (service->ipconfig_ipv6)
5165 __connman_ipconfig_enable(service->ipconfig_ipv6);
5167 err = __connman_network_connect(service->network);
5168 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5169 service->provider != NULL)
5170 err = __connman_provider_connect(service->provider);
5175 if (err != -EINPROGRESS) {
5176 __connman_ipconfig_disable(service->ipconfig_ipv4);
5177 __connman_ipconfig_disable(service->ipconfig_ipv6);
5178 __connman_stats_service_unregister(service);
5186 int __connman_service_connect(struct connman_service *service)
5190 DBG("service %p state %s", service, state2string(service->state));
5192 if (is_connected(service) == TRUE)
5195 if (is_connecting(service) == TRUE)
5198 switch (service->type) {
5199 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5200 case CONNMAN_SERVICE_TYPE_SYSTEM:
5201 case CONNMAN_SERVICE_TYPE_GPS:
5202 case CONNMAN_SERVICE_TYPE_GADGET:
5205 err = service_connect(service);
5211 if (err == -EINPROGRESS) {
5212 if (service->timeout == 0)
5213 service->timeout = g_timeout_add_seconds(
5214 CONNECT_TIMEOUT, connect_timeout, service);
5216 return -EINPROGRESS;
5219 __connman_service_ipconfig_indicate_state(service,
5220 CONNMAN_SERVICE_STATE_FAILURE,
5221 CONNMAN_IPCONFIG_TYPE_IPV4);
5222 __connman_service_ipconfig_indicate_state(service,
5223 CONNMAN_SERVICE_STATE_FAILURE,
5224 CONNMAN_IPCONFIG_TYPE_IPV6);
5226 if (service->network != NULL)
5227 __connman_network_disconnect(service->network);
5228 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5229 service->provider != NULL)
5230 __connman_provider_disconnect(service->provider);
5232 if (service->userconnect == TRUE) {
5233 if (err == -ENOKEY || err == -EPERM) {
5234 if (__connman_agent_request_passphrase_input(service,
5237 return -EINPROGRESS;
5239 reply_pending(service, -err);
5245 int __connman_service_disconnect(struct connman_service *service)
5249 DBG("service %p", service);
5251 if (service->network != NULL) {
5252 err = __connman_network_disconnect(service->network);
5253 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5254 service->provider != NULL)
5255 err = __connman_provider_disconnect(service->provider);
5259 if (err < 0 && err != -EINPROGRESS)
5262 __connman_6to4_remove(service->ipconfig_ipv4);
5264 if (service->ipconfig_ipv4)
5265 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
5268 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
5271 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
5272 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
5274 __connman_ipconfig_disable(service->ipconfig_ipv4);
5275 __connman_ipconfig_disable(service->ipconfig_ipv6);
5277 __connman_stats_service_unregister(service);
5282 int __connman_service_disconnect_all(void)
5284 GSequenceIter *iter;
5285 GSList *services = NULL, *list;
5289 iter = g_sequence_get_begin_iter(service_list);
5291 while (g_sequence_iter_is_end(iter) == FALSE) {
5292 struct connman_service *service = g_sequence_get(iter);
5294 services = g_slist_prepend(services, service);
5296 iter = g_sequence_iter_next(iter);
5299 for (list = services; list != NULL; list = list->next) {
5300 struct connman_service *service = list->data;
5302 service->ignore = TRUE;
5304 set_reconnect_state(service, FALSE);
5306 __connman_service_disconnect(service);
5316 * lookup_by_identifier:
5317 * @identifier: service identifier
5319 * Look up a service by identifier (reference count will not be increased)
5321 static struct connman_service *lookup_by_identifier(const char *identifier)
5323 GSequenceIter *iter;
5325 iter = g_hash_table_lookup(service_hash, identifier);
5327 return g_sequence_get(iter);
5332 static void provision_changed(gpointer value, gpointer user_data)
5334 struct connman_service *service = value;
5335 char *path = user_data;
5337 __connman_config_provision_service_ident(service, path);
5340 void __connman_service_provision_changed(const char *ident)
5342 g_sequence_foreach(service_list, provision_changed, (void *)ident);
5346 * __connman_service_get:
5347 * @identifier: service identifier
5349 * Look up a service by identifier or create a new one if not found
5351 static struct connman_service *service_get(const char *identifier)
5353 struct connman_service *service;
5354 GSequenceIter *iter;
5356 iter = g_hash_table_lookup(service_hash, identifier);
5358 service = g_sequence_get(iter);
5359 if (service != NULL)
5360 connman_service_ref(service);
5364 service = connman_service_create();
5365 if (service == NULL)
5368 DBG("service %p", service);
5370 service->identifier = g_strdup(identifier);
5372 iter = g_sequence_insert_sorted(service_list, service,
5373 service_compare, NULL);
5375 g_hash_table_insert(service_hash, service->identifier, iter);
5380 static int service_register(struct connman_service *service)
5382 GSequenceIter *iter;
5384 DBG("service %p", service);
5386 if (service->path != NULL)
5389 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
5390 service->identifier);
5392 DBG("path %s", service->path);
5394 __connman_config_provision_service(service);
5396 service_load(service);
5398 g_dbus_register_interface(connection, service->path,
5399 CONNMAN_SERVICE_INTERFACE,
5400 service_methods, service_signals,
5401 NULL, service, NULL);
5403 iter = g_hash_table_lookup(service_hash, service->identifier);
5404 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
5405 g_sequence_sort_changed(iter, service_compare, NULL);
5406 service_schedule_changed();
5409 __connman_connection_update_gateway();
5414 static void service_up(struct connman_ipconfig *ipconfig)
5416 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5418 DBG("%s up", __connman_ipconfig_get_ifname(ipconfig));
5420 link_changed(service);
5422 service->stats.valid = FALSE;
5423 service->stats_roaming.valid = FALSE;
5426 static void service_down(struct connman_ipconfig *ipconfig)
5428 DBG("%s down", __connman_ipconfig_get_ifname(ipconfig));
5431 static void service_lower_up(struct connman_ipconfig *ipconfig)
5433 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5435 DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig));
5437 stats_start(service);
5440 static void service_lower_down(struct connman_ipconfig *ipconfig)
5442 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5444 DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig));
5446 if (is_idle_state(service, service->state_ipv4) == FALSE)
5447 __connman_ipconfig_disable(service->ipconfig_ipv4);
5449 if (is_idle_state(service, service->state_ipv6) == FALSE)
5450 __connman_ipconfig_disable(service->ipconfig_ipv6);
5452 stats_stop(service);
5453 service_save(service);
5456 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5458 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5459 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5460 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5462 DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig));
5464 type = __connman_ipconfig_get_config_type(ipconfig);
5465 method = __connman_ipconfig_get_method(ipconfig);
5467 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5470 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5471 method == CONNMAN_IPCONFIG_METHOD_AUTO)
5472 __connman_service_ipconfig_indicate_state(service,
5473 CONNMAN_SERVICE_STATE_READY,
5474 CONNMAN_IPCONFIG_TYPE_IPV6);
5476 settings_changed(service, ipconfig);
5479 static void service_ip_release(struct connman_ipconfig *ipconfig)
5481 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5482 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5483 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5485 DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig));
5487 type = __connman_ipconfig_get_config_type(ipconfig);
5488 method = __connman_ipconfig_get_method(ipconfig);
5490 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5493 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5494 method == CONNMAN_IPCONFIG_METHOD_OFF)
5495 __connman_service_ipconfig_indicate_state(service,
5496 CONNMAN_SERVICE_STATE_DISCONNECT,
5497 CONNMAN_IPCONFIG_TYPE_IPV6);
5499 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5500 method == CONNMAN_IPCONFIG_METHOD_OFF)
5501 __connman_service_ipconfig_indicate_state(service,
5502 CONNMAN_SERVICE_STATE_DISCONNECT,
5503 CONNMAN_IPCONFIG_TYPE_IPV4);
5505 settings_changed(service, ipconfig);
5508 static const struct connman_ipconfig_ops service_ops = {
5510 .down = service_down,
5511 .lower_up = service_lower_up,
5512 .lower_down = service_lower_down,
5513 .ip_bound = service_ip_bound,
5514 .ip_release = service_ip_release,
5517 static void setup_ip4config(struct connman_service *service, int index,
5518 enum connman_ipconfig_method method)
5520 service->ipconfig_ipv4 = __connman_ipconfig_create(index,
5521 CONNMAN_IPCONFIG_TYPE_IPV4);
5522 if (service->ipconfig_ipv4 == NULL)
5525 __connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5527 __connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5529 __connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5532 static void setup_ip6config(struct connman_service *service, int index)
5534 service->ipconfig_ipv6 = __connman_ipconfig_create(index,
5535 CONNMAN_IPCONFIG_TYPE_IPV6);
5536 if (service->ipconfig_ipv6 == NULL)
5539 __connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5541 __connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5544 void __connman_service_read_ip4config(struct connman_service *service)
5548 if (service->ipconfig_ipv4 == NULL)
5551 keyfile = connman_storage_load_service(service->identifier);
5552 if (keyfile == NULL)
5555 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5556 service->identifier, "IPv4.");
5558 g_key_file_free(keyfile);
5561 void __connman_service_create_ip4config(struct connman_service *service,
5564 DBG("ipv4 %p", service->ipconfig_ipv4);
5566 if (service->ipconfig_ipv4 != NULL)
5569 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5570 __connman_service_read_ip4config(service);
5573 void __connman_service_read_ip6config(struct connman_service *service)
5577 if (service->ipconfig_ipv6 == NULL)
5580 keyfile = connman_storage_load_service(service->identifier);
5581 if (keyfile == NULL)
5584 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5585 service->identifier, "IPv6.");
5587 g_key_file_free(keyfile);
5590 void __connman_service_create_ip6config(struct connman_service *service,
5593 DBG("ipv6 %p", service->ipconfig_ipv6);
5595 if (service->ipconfig_ipv6 != NULL)
5598 setup_ip6config(service, index);
5600 __connman_service_read_ip6config(service);
5604 * __connman_service_lookup_from_network:
5605 * @network: network structure
5607 * Look up a service by network (reference count will not be increased)
5609 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5611 struct connman_service *service;
5612 const char *ident, *group;
5615 DBG("network %p", network);
5617 if (network == NULL)
5620 ident = __connman_network_get_ident(network);
5624 group = connman_network_get_group(network);
5628 name = g_strdup_printf("%s_%s_%s",
5629 __connman_network_get_type(network), ident, group);
5630 service = lookup_by_identifier(name);
5636 struct connman_service *__connman_service_lookup_from_index(int index)
5638 struct connman_service *service;
5639 GSequenceIter *iter;
5641 iter = g_sequence_get_begin_iter(service_list);
5643 while (g_sequence_iter_is_end(iter) == FALSE) {
5644 service = g_sequence_get(iter);
5646 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
5650 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
5654 iter = g_sequence_iter_next(iter);
5660 const char *__connman_service_get_ident(struct connman_service *service)
5662 return service->identifier;
5665 const char *__connman_service_get_path(struct connman_service *service)
5667 return service->path;
5670 unsigned int __connman_service_get_order(struct connman_service *service)
5672 GSequenceIter *iter;
5674 if (service == NULL)
5677 if (service->favorite == FALSE) {
5682 iter = g_hash_table_lookup(service_hash, service->identifier);
5684 if (g_sequence_iter_get_position(iter) == 0)
5686 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5687 service->do_split_routing == FALSE)
5688 service->order = 10;
5693 DBG("service %p name %s order %d split %d", service, service->name,
5694 service->order, service->do_split_routing);
5697 return service->order;
5700 void __connman_service_update_ordering(void)
5702 GSequenceIter *iter;
5704 iter = g_sequence_get_begin_iter(service_list);
5705 if (iter != NULL && g_sequence_get_length(service_list) > 1)
5706 g_sequence_sort_changed(iter, service_compare, NULL);
5709 static enum connman_service_type convert_network_type(struct connman_network *network)
5711 enum connman_network_type type = connman_network_get_type(network);
5714 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5715 case CONNMAN_NETWORK_TYPE_VENDOR:
5717 case CONNMAN_NETWORK_TYPE_ETHERNET:
5718 return CONNMAN_SERVICE_TYPE_ETHERNET;
5719 case CONNMAN_NETWORK_TYPE_WIFI:
5720 return CONNMAN_SERVICE_TYPE_WIFI;
5721 case CONNMAN_NETWORK_TYPE_WIMAX:
5722 return CONNMAN_SERVICE_TYPE_WIMAX;
5723 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5724 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5725 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5726 case CONNMAN_NETWORK_TYPE_CELLULAR:
5727 return CONNMAN_SERVICE_TYPE_CELLULAR;
5730 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5733 static enum connman_service_security convert_wifi_security(const char *security)
5735 if (security == NULL)
5736 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5737 else if (g_str_equal(security, "none") == TRUE)
5738 return CONNMAN_SERVICE_SECURITY_NONE;
5739 else if (g_str_equal(security, "wep") == TRUE)
5740 return CONNMAN_SERVICE_SECURITY_WEP;
5741 else if (g_str_equal(security, "psk") == TRUE)
5742 return CONNMAN_SERVICE_SECURITY_PSK;
5743 else if (g_str_equal(security, "ieee8021x") == TRUE)
5744 return CONNMAN_SERVICE_SECURITY_8021X;
5745 else if (g_str_equal(security, "wpa") == TRUE)
5746 return CONNMAN_SERVICE_SECURITY_WPA;
5747 else if (g_str_equal(security, "rsn") == TRUE)
5748 return CONNMAN_SERVICE_SECURITY_RSN;
5750 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5753 static void update_from_network(struct connman_service *service,
5754 struct connman_network *network)
5756 connman_uint8_t strength = service->strength;
5757 GSequenceIter *iter;
5760 DBG("service %p network %p", service, network);
5762 if (is_connected(service) == TRUE)
5765 if (is_connecting(service) == TRUE)
5768 str = connman_network_get_string(network, "Name");
5770 g_free(service->name);
5771 service->name = g_strdup(str);
5772 service->hidden = FALSE;
5774 g_free(service->name);
5775 service->name = NULL;
5776 service->hidden = TRUE;
5779 service->strength = connman_network_get_strength(network);
5780 service->roaming = connman_network_get_bool(network, "Roaming");
5782 if (service->strength == 0) {
5784 * Filter out 0-values; it's unclear what they mean
5785 * and they cause anomalous sorting of the priority list.
5787 service->strength = strength;
5790 str = connman_network_get_string(network, "WiFi.Security");
5791 service->security = convert_wifi_security(str);
5793 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5794 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5796 if (service->strength > strength && service->network != NULL) {
5797 connman_network_unref(service->network);
5798 service->network = connman_network_ref(network);
5800 strength_changed(service);
5803 if (service->network == NULL)
5804 service->network = connman_network_ref(network);
5806 iter = g_hash_table_lookup(service_hash, service->identifier);
5807 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
5808 g_sequence_sort_changed(iter, service_compare, NULL);
5809 service_schedule_changed();
5814 * __connman_service_create_from_network:
5815 * @network: network structure
5817 * Look up service by network and if not found, create one
5819 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5821 struct connman_service *service;
5822 struct connman_device *device;
5823 const char *ident, *group;
5825 unsigned int *auto_connect_types;
5828 DBG("network %p", network);
5830 if (network == NULL)
5833 ident = __connman_network_get_ident(network);
5837 group = connman_network_get_group(network);
5841 name = g_strdup_printf("%s_%s_%s",
5842 __connman_network_get_type(network), ident, group);
5843 service = service_get(name);
5846 if (service == NULL)
5849 if (__connman_network_get_weakness(network) == TRUE)
5852 if (service->path != NULL) {
5853 update_from_network(service, network);
5854 __connman_connection_update_gateway();
5858 service->type = convert_network_type(network);
5860 auto_connect_types = connman_setting_get_uint_list("DefaultAutoConnectTechnologies");
5861 service->autoconnect = FALSE;
5862 for (i = 0; auto_connect_types != NULL &&
5863 auto_connect_types[i] != 0; i++) {
5864 if (service->type == auto_connect_types[i]) {
5865 service->autoconnect = TRUE;
5870 switch (service->type) {
5871 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5872 case CONNMAN_SERVICE_TYPE_SYSTEM:
5873 case CONNMAN_SERVICE_TYPE_WIMAX:
5874 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5875 case CONNMAN_SERVICE_TYPE_GPS:
5876 case CONNMAN_SERVICE_TYPE_VPN:
5877 case CONNMAN_SERVICE_TYPE_GADGET:
5878 case CONNMAN_SERVICE_TYPE_WIFI:
5879 case CONNMAN_SERVICE_TYPE_CELLULAR:
5881 case CONNMAN_SERVICE_TYPE_ETHERNET:
5882 service->favorite = TRUE;
5886 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5887 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5889 update_from_network(service, network);
5891 index = connman_network_get_index(network);
5893 if (service->ipconfig_ipv4 == NULL)
5894 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5896 if (service->ipconfig_ipv6 == NULL)
5897 setup_ip6config(service, index);
5899 service_register(service);
5901 if (service->favorite == TRUE) {
5902 device = connman_network_get_device(service->network);
5903 if (device && __connman_device_scanning(device) == FALSE)
5904 __connman_service_auto_connect();
5907 __connman_notifier_service_add(service, service->name);
5908 service_schedule_added(service);
5913 void __connman_service_update_from_network(struct connman_network *network)
5915 connman_bool_t need_sort = FALSE;
5916 struct connman_service *service;
5917 connman_uint8_t strength;
5918 connman_bool_t roaming;
5919 GSequenceIter *iter;
5921 connman_bool_t stats_enable;
5923 DBG("network %p", network);
5925 service = __connman_service_lookup_from_network(network);
5926 if (service == NULL)
5929 if (service->network == NULL)
5932 name = connman_network_get_string(service->network, "Name");
5933 if (g_strcmp0(service->name, name) != 0) {
5934 g_free(service->name);
5935 service->name = g_strdup(name);
5936 connman_dbus_property_changed_basic(service->path,
5937 CONNMAN_SERVICE_INTERFACE, "Name",
5938 DBUS_TYPE_STRING, &service->name);
5941 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5942 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5944 strength = connman_network_get_strength(service->network);
5945 if (strength == service->strength)
5948 service->strength = strength;
5951 strength_changed(service);
5954 roaming = connman_network_get_bool(service->network, "Roaming");
5955 if (roaming == service->roaming)
5958 stats_enable = stats_enabled(service);
5959 if (stats_enable == TRUE)
5960 stats_stop(service);
5962 service->roaming = roaming;
5965 if (stats_enable == TRUE)
5966 stats_start(service);
5968 roaming_changed(service);
5971 if (need_sort == TRUE) {
5972 iter = g_hash_table_lookup(service_hash, service->identifier);
5973 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
5974 g_sequence_sort_changed(iter, service_compare, NULL);
5975 service_schedule_changed();
5980 void __connman_service_remove_from_network(struct connman_network *network)
5982 struct connman_service *service;
5984 DBG("network %p", network);
5986 service = __connman_service_lookup_from_network(network);
5987 if (service == NULL)
5990 service->ignore = TRUE;
5992 __connman_connection_gateway_remove(service,
5993 CONNMAN_IPCONFIG_TYPE_ALL);
5995 connman_service_unref(service);
5999 * __connman_service_create_from_provider:
6000 * @provider: provider structure
6002 * Look up service by provider and if not found, create one
6004 struct connman_service *
6005 __connman_service_create_from_provider(struct connman_provider *provider)
6007 struct connman_service *service;
6008 const char *ident, *str;
6010 int index = connman_provider_get_index(provider);
6012 DBG("provider %p", provider);
6014 ident = __connman_provider_get_ident(provider);
6018 name = g_strdup_printf("vpn_%s", ident);
6019 service = service_get(name);
6022 if (service == NULL)
6025 service->type = CONNMAN_SERVICE_TYPE_VPN;
6026 service->provider = connman_provider_ref(provider);
6027 service->autoconnect = FALSE;
6028 service->userconnect = TRUE;
6030 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
6031 service->state = combine_state(service->state_ipv4, service->state_ipv6);
6033 str = connman_provider_get_string(provider, "Name");
6035 g_free(service->name);
6036 service->name = g_strdup(str);
6037 service->hidden = FALSE;
6039 g_free(service->name);
6040 service->name = NULL;
6041 service->hidden = TRUE;
6044 service->strength = 0;
6046 if (service->ipconfig_ipv4 == NULL)
6047 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
6049 if (service->ipconfig_ipv6 == NULL)
6050 setup_ip6config(service, index);
6052 service_register(service);
6054 __connman_notifier_service_add(service, service->name);
6055 service_schedule_added(service);
6060 int __connman_service_init(void)
6064 connection = connman_dbus_get_connection();
6066 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
6069 service_list = g_sequence_new(service_free);
6071 services_notify = g_new0(struct _services_notify, 1);
6072 services_notify->remove = g_hash_table_new_full(g_str_hash,
6073 g_str_equal, g_free, NULL);
6074 services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
6079 void __connman_service_cleanup(void)
6085 if (autoconnect_timeout != 0) {
6086 g_source_remove(autoconnect_timeout);
6087 autoconnect_timeout = 0;
6090 list = service_list;
6091 service_list = NULL;
6092 g_sequence_free(list);
6094 g_hash_table_destroy(service_hash);
6095 service_hash = NULL;
6097 g_slist_free(counter_list);
6098 counter_list = NULL;
6100 if (services_notify->id != 0) {
6101 g_source_remove(services_notify->id);
6102 service_send_changed(NULL);
6103 g_hash_table_destroy(services_notify->remove);
6104 g_hash_table_destroy(services_notify->add);
6106 g_free(services_notify);
6108 dbus_connection_unref(connection);