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;
120 connman_bool_t hidden_service;
127 struct connman_service *service;
130 static void compare_path(gpointer value, gpointer user_data)
132 struct connman_service *service = value;
133 struct find_data *data = user_data;
135 if (data->service != NULL)
138 if (g_strcmp0(service->path, data->path) == 0)
139 data->service = service;
142 static struct connman_service *find_service(const char *path)
144 struct find_data data = { .path = path, .service = NULL };
146 DBG("path %s", path);
148 g_sequence_foreach(service_list, compare_path, &data);
153 const char *__connman_service_type2string(enum connman_service_type type)
156 case CONNMAN_SERVICE_TYPE_UNKNOWN:
158 case CONNMAN_SERVICE_TYPE_SYSTEM:
160 case CONNMAN_SERVICE_TYPE_ETHERNET:
162 case CONNMAN_SERVICE_TYPE_WIFI:
164 case CONNMAN_SERVICE_TYPE_WIMAX:
166 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
168 case CONNMAN_SERVICE_TYPE_CELLULAR:
170 case CONNMAN_SERVICE_TYPE_GPS:
172 case CONNMAN_SERVICE_TYPE_VPN:
174 case CONNMAN_SERVICE_TYPE_GADGET:
181 enum connman_service_type __connman_service_string2type(const char *str)
184 return CONNMAN_SERVICE_TYPE_UNKNOWN;
186 if (strcmp(str, "ethernet") == 0)
187 return CONNMAN_SERVICE_TYPE_ETHERNET;
188 if (strcmp(str, "gadget") == 0)
189 return CONNMAN_SERVICE_TYPE_GADGET;
190 if (strcmp(str, "wifi") == 0)
191 return CONNMAN_SERVICE_TYPE_WIFI;
192 if (strcmp(str, "cellular") == 0)
193 return CONNMAN_SERVICE_TYPE_CELLULAR;
194 if (strcmp(str, "bluetooth") == 0)
195 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
196 if (strcmp(str, "wimax") == 0)
197 return CONNMAN_SERVICE_TYPE_WIMAX;
198 if (strcmp(str, "vpn") == 0)
199 return CONNMAN_SERVICE_TYPE_VPN;
200 if (strcmp(str, "gps") == 0)
201 return CONNMAN_SERVICE_TYPE_GPS;
202 if (strcmp(str, "system") == 0)
203 return CONNMAN_SERVICE_TYPE_SYSTEM;
205 return CONNMAN_SERVICE_TYPE_UNKNOWN;
208 static const char *security2string(enum connman_service_security security)
211 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
213 case CONNMAN_SERVICE_SECURITY_NONE:
215 case CONNMAN_SERVICE_SECURITY_WEP:
217 case CONNMAN_SERVICE_SECURITY_PSK:
218 case CONNMAN_SERVICE_SECURITY_WPA:
219 case CONNMAN_SERVICE_SECURITY_RSN:
221 case CONNMAN_SERVICE_SECURITY_8021X:
228 static const char *state2string(enum connman_service_state state)
231 case CONNMAN_SERVICE_STATE_UNKNOWN:
233 case CONNMAN_SERVICE_STATE_IDLE:
235 case CONNMAN_SERVICE_STATE_ASSOCIATION:
236 return "association";
237 case CONNMAN_SERVICE_STATE_CONFIGURATION:
238 return "configuration";
239 case CONNMAN_SERVICE_STATE_READY:
241 case CONNMAN_SERVICE_STATE_ONLINE:
243 case CONNMAN_SERVICE_STATE_DISCONNECT:
245 case CONNMAN_SERVICE_STATE_FAILURE:
252 static const char *error2string(enum connman_service_error error)
255 case CONNMAN_SERVICE_ERROR_UNKNOWN:
257 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
258 return "out-of-range";
259 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
260 return "pin-missing";
261 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
262 return "dhcp-failed";
263 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
264 return "connect-failed";
265 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
266 return "login-failed";
267 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
268 return "auth-failed";
269 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
270 return "invalid-key";
276 static enum connman_service_error string2error(const char *error)
278 if (g_strcmp0(error, "dhcp-failed") == 0)
279 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
280 else if (g_strcmp0(error, "pin-missing") == 0)
281 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
282 else if (g_strcmp0(error, "invalid-key") == 0)
283 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
285 return CONNMAN_SERVICE_ERROR_UNKNOWN;
288 static const char *proxymethod2string(enum connman_service_proxy_method method)
291 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
293 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
295 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
297 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
304 static enum connman_service_proxy_method string2proxymethod(const char *method)
306 if (g_strcmp0(method, "direct") == 0)
307 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
308 else if (g_strcmp0(method, "auto") == 0)
309 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
310 else if (g_strcmp0(method, "manual") == 0)
311 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
313 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
316 static int service_load(struct connman_service *service)
319 GError *error = NULL;
322 connman_bool_t autoconnect;
323 unsigned int ssid_len;
326 DBG("service %p", service);
328 keyfile = connman_storage_load_service(service->identifier);
329 if (keyfile == NULL) {
330 service->new_service = TRUE;
333 service->new_service = FALSE;
335 switch (service->type) {
336 case CONNMAN_SERVICE_TYPE_UNKNOWN:
337 case CONNMAN_SERVICE_TYPE_SYSTEM:
338 case CONNMAN_SERVICE_TYPE_GPS:
339 case CONNMAN_SERVICE_TYPE_GADGET:
341 case CONNMAN_SERVICE_TYPE_VPN:
342 service->do_split_routing = g_key_file_get_boolean(keyfile,
343 service->identifier, "SplitRouting", NULL);
345 case CONNMAN_SERVICE_TYPE_WIFI:
346 if (service->name == NULL) {
349 name = g_key_file_get_string(keyfile,
350 service->identifier, "Name", NULL);
352 g_free(service->name);
353 service->name = name;
356 if (service->network != NULL)
357 connman_network_set_name(service->network,
361 if (service->network &&
362 connman_network_get_blob(service->network,
363 "WiFi.SSID", &ssid_len) == NULL) {
366 hex_ssid = g_key_file_get_string(keyfile,
370 if (hex_ssid != NULL) {
372 unsigned int i, j = 0, hex;
373 size_t hex_ssid_len = strlen(hex_ssid);
375 ssid = g_try_malloc0(hex_ssid_len / 2);
382 for (i = 0; i < hex_ssid_len; i += 2) {
383 sscanf(hex_ssid + i, "%02x", &hex);
387 connman_network_set_blob(service->network,
388 "WiFi.SSID", ssid, hex_ssid_len / 2);
395 case CONNMAN_SERVICE_TYPE_WIMAX:
396 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
397 case CONNMAN_SERVICE_TYPE_CELLULAR:
398 service->favorite = g_key_file_get_boolean(keyfile,
399 service->identifier, "Favorite", NULL);
401 str = g_key_file_get_string(keyfile,
402 service->identifier, "Failure", NULL);
404 if (service->favorite == FALSE)
405 service->state_ipv4 = service->state_ipv6 =
406 CONNMAN_SERVICE_STATE_FAILURE;
407 service->error = string2error(str);
412 case CONNMAN_SERVICE_TYPE_ETHERNET:
413 autoconnect = g_key_file_get_boolean(keyfile,
414 service->identifier, "AutoConnect", &error);
416 service->autoconnect = autoconnect;
417 g_clear_error(&error);
421 str = g_key_file_get_string(keyfile,
422 service->identifier, "Modified", NULL);
424 g_time_val_from_iso8601(str, &service->modified);
428 str = g_key_file_get_string(keyfile,
429 service->identifier, "Passphrase", NULL);
431 g_free(service->passphrase);
432 service->passphrase = str;
435 if (service->ipconfig_ipv4 != NULL)
436 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
437 service->identifier, "IPv4.");
439 if (service->ipconfig_ipv6 != NULL)
440 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
441 service->identifier, "IPv6.");
443 service->nameservers_config = g_key_file_get_string_list(keyfile,
444 service->identifier, "Nameservers", &length, NULL);
445 if (service->nameservers_config != NULL && length == 0) {
446 g_strfreev(service->nameservers_config);
447 service->nameservers_config = NULL;
450 service->timeservers_config = g_key_file_get_string_list(keyfile,
451 service->identifier, "Timeservers", &length, NULL);
452 if (service->timeservers_config != NULL && length == 0) {
453 g_strfreev(service->timeservers_config);
454 service->timeservers_config = NULL;
457 service->domains = g_key_file_get_string_list(keyfile,
458 service->identifier, "Domains", &length, NULL);
459 if (service->domains != NULL && length == 0) {
460 g_strfreev(service->domains);
461 service->domains = NULL;
464 str = g_key_file_get_string(keyfile,
465 service->identifier, "Proxy.Method", NULL);
467 service->proxy_config = string2proxymethod(str);
471 service->proxies = g_key_file_get_string_list(keyfile,
472 service->identifier, "Proxy.Servers", &length, NULL);
473 if (service->proxies != NULL && length == 0) {
474 g_strfreev(service->proxies);
475 service->proxies = NULL;
478 service->excludes = g_key_file_get_string_list(keyfile,
479 service->identifier, "Proxy.Excludes", &length, NULL);
480 if (service->excludes != NULL && length == 0) {
481 g_strfreev(service->excludes);
482 service->excludes = NULL;
485 str = g_key_file_get_string(keyfile,
486 service->identifier, "Proxy.URL", NULL);
488 g_free(service->pac);
492 service->hidden_service = g_key_file_get_boolean(keyfile,
493 service->identifier, "Hidden", NULL);
496 g_key_file_free(keyfile);
501 static int service_save(struct connman_service *service)
506 const char *cst_str = NULL;
509 DBG("service %p new %d", service, service->new_service);
511 if (service->new_service == TRUE)
514 keyfile = __connman_storage_open_service(service->identifier);
518 if (service->name != NULL)
519 g_key_file_set_string(keyfile, service->identifier,
520 "Name", service->name);
522 switch (service->type) {
523 case CONNMAN_SERVICE_TYPE_UNKNOWN:
524 case CONNMAN_SERVICE_TYPE_SYSTEM:
525 case CONNMAN_SERVICE_TYPE_GPS:
526 case CONNMAN_SERVICE_TYPE_GADGET:
528 case CONNMAN_SERVICE_TYPE_VPN:
529 g_key_file_set_boolean(keyfile, service->identifier,
530 "SplitRouting", service->do_split_routing);
532 case CONNMAN_SERVICE_TYPE_WIFI:
533 if (service->network) {
534 const unsigned char *ssid;
535 unsigned int ssid_len = 0;
537 ssid = connman_network_get_blob(service->network,
538 "WiFi.SSID", &ssid_len);
540 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
541 char *identifier = service->identifier;
545 str = g_string_sized_new(ssid_len * 2);
551 for (i = 0; i < ssid_len; i++)
552 g_string_append_printf(str,
555 g_key_file_set_string(keyfile, identifier,
558 g_string_free(str, TRUE);
561 freq = connman_network_get_frequency(service->network);
562 g_key_file_set_integer(keyfile, service->identifier,
567 case CONNMAN_SERVICE_TYPE_WIMAX:
568 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
569 case CONNMAN_SERVICE_TYPE_CELLULAR:
570 g_key_file_set_boolean(keyfile, service->identifier,
571 "Favorite", service->favorite);
573 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
574 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
575 const char *failure = error2string(service->error);
577 g_key_file_set_string(keyfile,
581 g_key_file_remove_key(keyfile, service->identifier,
586 case CONNMAN_SERVICE_TYPE_ETHERNET:
587 if (service->favorite == TRUE)
588 g_key_file_set_boolean(keyfile, service->identifier,
589 "AutoConnect", service->autoconnect);
593 str = g_time_val_to_iso8601(&service->modified);
595 g_key_file_set_string(keyfile, service->identifier,
600 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
601 g_key_file_set_string(keyfile, service->identifier,
602 "Passphrase", service->passphrase);
604 g_key_file_remove_key(keyfile, service->identifier,
607 if (service->ipconfig_ipv4 != NULL)
608 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
609 service->identifier, "IPv4.");
611 if (service->ipconfig_ipv6 != NULL)
612 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
613 service->identifier, "IPv6.");
615 if (service->nameservers_config != NULL) {
616 guint len = g_strv_length(service->nameservers_config);
618 g_key_file_set_string_list(keyfile, service->identifier,
620 (const gchar **) service->nameservers_config, len);
622 g_key_file_remove_key(keyfile, service->identifier,
623 "Nameservers", NULL);
625 if (service->timeservers_config != NULL) {
626 guint len = g_strv_length(service->timeservers_config);
628 g_key_file_set_string_list(keyfile, service->identifier,
630 (const gchar **) service->timeservers_config, len);
632 g_key_file_remove_key(keyfile, service->identifier,
633 "Timeservers", NULL);
635 if (service->domains != NULL) {
636 guint len = g_strv_length(service->domains);
638 g_key_file_set_string_list(keyfile, service->identifier,
640 (const gchar **) service->domains, len);
642 g_key_file_remove_key(keyfile, service->identifier,
645 cst_str = proxymethod2string(service->proxy_config);
647 g_key_file_set_string(keyfile, service->identifier,
648 "Proxy.Method", cst_str);
650 if (service->proxies != NULL) {
651 guint len = g_strv_length(service->proxies);
653 g_key_file_set_string_list(keyfile, service->identifier,
655 (const gchar **) service->proxies, len);
657 g_key_file_remove_key(keyfile, service->identifier,
658 "Proxy.Servers", NULL);
660 if (service->excludes != NULL) {
661 guint len = g_strv_length(service->excludes);
663 g_key_file_set_string_list(keyfile, service->identifier,
665 (const gchar **) service->excludes, len);
667 g_key_file_remove_key(keyfile, service->identifier,
668 "Proxy.Excludes", NULL);
670 if (service->pac != NULL && strlen(service->pac) > 0)
671 g_key_file_set_string(keyfile, service->identifier,
672 "Proxy.URL", service->pac);
674 g_key_file_remove_key(keyfile, service->identifier,
677 if (service->hidden_service == TRUE)
678 g_key_file_set_boolean(keyfile, service->identifier, "Hidden",
681 if (service->config_file != NULL && strlen(service->config_file) > 0)
682 g_key_file_set_string(keyfile, service->identifier,
683 "Config.file", service->config_file);
685 if (service->config_entry != NULL &&
686 strlen(service->config_entry) > 0)
687 g_key_file_set_string(keyfile, service->identifier,
688 "Config.ident", service->config_entry);
691 __connman_storage_save_service(keyfile, service->identifier);
693 g_key_file_free(keyfile);
698 static enum connman_service_state combine_state(
699 enum connman_service_state state_a,
700 enum connman_service_state state_b)
702 enum connman_service_state result;
704 if (state_a == state_b) {
709 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
714 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
719 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
724 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
729 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
734 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
739 if (state_a == CONNMAN_SERVICE_STATE_READY) {
744 if (state_b == CONNMAN_SERVICE_STATE_READY) {
749 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
754 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
759 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
764 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
769 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
774 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
779 result = CONNMAN_SERVICE_STATE_FAILURE;
785 static connman_bool_t is_connecting_state(struct connman_service *service,
786 enum connman_service_state state)
789 case CONNMAN_SERVICE_STATE_UNKNOWN:
790 case CONNMAN_SERVICE_STATE_IDLE:
791 case CONNMAN_SERVICE_STATE_FAILURE:
792 if (service->network != NULL)
793 return connman_network_get_connecting(service->network);
794 case CONNMAN_SERVICE_STATE_DISCONNECT:
795 case CONNMAN_SERVICE_STATE_READY:
796 case CONNMAN_SERVICE_STATE_ONLINE:
798 case CONNMAN_SERVICE_STATE_ASSOCIATION:
799 case CONNMAN_SERVICE_STATE_CONFIGURATION:
806 static connman_bool_t is_connected_state(const struct connman_service *service,
807 enum connman_service_state state)
810 case CONNMAN_SERVICE_STATE_UNKNOWN:
811 case CONNMAN_SERVICE_STATE_IDLE:
812 case CONNMAN_SERVICE_STATE_ASSOCIATION:
813 case CONNMAN_SERVICE_STATE_CONFIGURATION:
814 case CONNMAN_SERVICE_STATE_DISCONNECT:
815 case CONNMAN_SERVICE_STATE_FAILURE:
817 case CONNMAN_SERVICE_STATE_READY:
818 case CONNMAN_SERVICE_STATE_ONLINE:
825 static connman_bool_t is_idle_state(const struct connman_service *service,
826 enum connman_service_state state)
829 case CONNMAN_SERVICE_STATE_UNKNOWN:
830 case CONNMAN_SERVICE_STATE_ASSOCIATION:
831 case CONNMAN_SERVICE_STATE_CONFIGURATION:
832 case CONNMAN_SERVICE_STATE_READY:
833 case CONNMAN_SERVICE_STATE_ONLINE:
834 case CONNMAN_SERVICE_STATE_DISCONNECT:
835 case CONNMAN_SERVICE_STATE_FAILURE:
837 case CONNMAN_SERVICE_STATE_IDLE:
844 static connman_bool_t is_connecting(struct connman_service *service)
846 return is_connecting_state(service, service->state);
849 static connman_bool_t is_connected(struct connman_service *service)
851 return is_connected_state(service, service->state);
854 static const char *nameserver_get_ifname(struct connman_service *service)
858 if (service->ipconfig_ipv4)
859 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
860 else if (service->ipconfig_ipv6)
861 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
868 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
869 case CONNMAN_SERVICE_STATE_UNKNOWN:
870 case CONNMAN_SERVICE_STATE_IDLE:
871 case CONNMAN_SERVICE_STATE_ASSOCIATION:
872 case CONNMAN_SERVICE_STATE_CONFIGURATION:
873 case CONNMAN_SERVICE_STATE_FAILURE:
874 case CONNMAN_SERVICE_STATE_DISCONNECT:
876 case CONNMAN_SERVICE_STATE_READY:
877 case CONNMAN_SERVICE_STATE_ONLINE:
884 static void remove_nameservers(struct connman_service *service,
885 const char* interface, char **ns)
887 const char *ifname = interface;
893 if (interface == NULL)
894 ifname = nameserver_get_ifname(service);
899 for (i = 0; ns[i] != NULL; i++)
900 connman_resolver_remove(ifname, NULL, ns[i]);
903 static void remove_searchdomains(struct connman_service *service,
904 const char *interface, char **sd)
906 const char *ifname = interface;
912 if (interface == NULL)
913 ifname = nameserver_get_ifname(service);
918 for (i = 0; sd[i] != NULL; i++)
919 connman_resolver_remove(ifname, sd[i], NULL);
922 static void update_nameservers(struct connman_service *service)
926 if (service->ipconfig_ipv4)
927 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
928 else if (service->ipconfig_ipv6)
929 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
936 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
937 case CONNMAN_SERVICE_STATE_UNKNOWN:
938 case CONNMAN_SERVICE_STATE_IDLE:
939 case CONNMAN_SERVICE_STATE_ASSOCIATION:
940 case CONNMAN_SERVICE_STATE_CONFIGURATION:
942 case CONNMAN_SERVICE_STATE_FAILURE:
943 case CONNMAN_SERVICE_STATE_DISCONNECT:
944 connman_resolver_remove_all(ifname);
946 case CONNMAN_SERVICE_STATE_READY:
947 case CONNMAN_SERVICE_STATE_ONLINE:
951 if (service->nameservers_config != NULL) {
954 remove_nameservers(service, ifname, service->nameservers);
956 i = g_strv_length(service->nameservers_config);
959 connman_resolver_append(ifname, NULL,
960 service->nameservers_config[i]);
962 } else if (service->nameservers != NULL) {
965 i = g_strv_length(service->nameservers);
968 connman_resolver_append(ifname, NULL,
969 service->nameservers[i]);
973 if (service->domains != NULL) {
974 char *searchdomains[2] = {NULL, NULL};
977 searchdomains[0] = service->domainname;
978 remove_searchdomains(service, ifname, searchdomains);
980 i = g_strv_length(service->domains);
983 connman_resolver_append(ifname, service->domains[i],
986 } else if (service->domainname != NULL)
987 connman_resolver_append(ifname, service->domainname, NULL);
989 connman_resolver_flush();
993 * The is_auto variable is set to true when IPv6 autoconf nameservers are
994 * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
995 * for details) and not through service.c
997 int __connman_service_nameserver_append(struct connman_service *service,
998 const char *nameserver, gboolean is_auto)
1003 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
1005 if (nameserver == NULL)
1008 if (is_auto == TRUE)
1009 nameservers = service->nameservers_auto;
1011 nameservers = service->nameservers;
1013 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
1014 if (g_strcmp0(nameservers[i], nameserver) == 0)
1017 if (nameservers != NULL) {
1018 len = g_strv_length(nameservers);
1019 nameservers = g_try_renew(char *, nameservers, len + 2);
1022 nameservers = g_try_new0(char *, len + 2);
1025 if (nameservers == NULL)
1028 nameservers[len] = g_strdup(nameserver);
1029 if (nameservers[len] == NULL)
1032 nameservers[len + 1] = NULL;
1034 if (is_auto == TRUE) {
1035 service->nameservers_auto = nameservers;
1037 service->nameservers = nameservers;
1038 update_nameservers(service);
1044 int __connman_service_nameserver_remove(struct connman_service *service,
1045 const char *nameserver, gboolean is_auto)
1047 char **servers, **nameservers;
1048 gboolean found = FALSE;
1051 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
1053 if (nameserver == NULL)
1056 if (is_auto == TRUE)
1057 nameservers = service->nameservers_auto;
1059 nameservers = service->nameservers;
1061 if (nameservers == NULL)
1064 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
1065 if (g_strcmp0(nameservers[i], nameserver) == 0) {
1073 len = g_strv_length(nameservers);
1076 g_strfreev(nameservers);
1077 if (is_auto == TRUE)
1078 service->nameservers_auto = NULL;
1080 service->nameservers = NULL;
1085 servers = g_try_new0(char *, len);
1086 if (servers == NULL)
1089 for (i = 0, j = 0; i < len; i++) {
1090 if (g_strcmp0(nameservers[i], nameserver) != 0) {
1091 servers[j] = g_strdup(nameservers[i]);
1092 if (servers[j] == NULL)
1097 servers[len - 1] = NULL;
1099 g_strfreev(nameservers);
1100 nameservers = servers;
1102 if (is_auto == TRUE) {
1103 service->nameservers_auto = nameservers;
1105 service->nameservers = nameservers;
1106 update_nameservers(service);
1112 void __connman_service_nameserver_clear(struct connman_service *service)
1114 g_strfreev(service->nameservers);
1115 service->nameservers = NULL;
1117 update_nameservers(service);
1120 static void add_nameserver_route(int family, int index, char *nameserver,
1125 if (connman_inet_compare_subnet(index, nameserver) == TRUE)
1128 if (connman_inet_add_host_route(index, nameserver, gw) < 0)
1129 /* For P-t-P link the above route add will fail */
1130 connman_inet_add_host_route(index, nameserver, NULL);
1134 if (connman_inet_add_ipv6_host_route(index, nameserver,
1136 connman_inet_add_ipv6_host_route(index, nameserver,
1142 static void nameserver_add_routes(int index, char **nameservers,
1146 struct addrinfo hints;
1147 struct addrinfo *addr;
1149 for (i = 0; nameservers[i] != NULL; i++) {
1150 memset(&hints, 0, sizeof(struct addrinfo));
1151 hints.ai_flags = AI_NUMERICHOST;
1154 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1155 if (ret == EAI_NONAME)
1156 family = AF_INET; /* use the IPv4 as a default */
1160 family = addr->ai_family;
1162 add_nameserver_route(family, index, nameservers[i], gw);
1168 static void nameserver_del_routes(int index, char **nameservers,
1169 enum connman_ipconfig_type type)
1172 struct addrinfo hints;
1173 struct addrinfo *addr;
1175 for (i = 0; nameservers[i] != NULL; i++) {
1176 memset(&hints, 0, sizeof(struct addrinfo));
1177 hints.ai_flags = AI_NUMERICHOST;
1180 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1181 if (ret == EAI_NONAME)
1182 family = AF_INET; /* use the IPv4 as a default */
1186 family = addr->ai_family;
1190 if (type != CONNMAN_IPCONFIG_TYPE_IPV6)
1191 connman_inet_del_host_route(index,
1195 if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
1196 connman_inet_del_ipv6_host_route(index,
1205 void __connman_service_nameserver_add_routes(struct connman_service *service,
1210 if (service == NULL)
1213 if (service->network != NULL)
1214 index = connman_network_get_index(service->network);
1215 else if (service->provider != NULL)
1216 index = connman_provider_get_index(service->provider);
1218 if (service->nameservers_config != NULL) {
1220 * Configured nameserver takes preference over the
1221 * discoverd nameserver gathered from DHCP, VPN, etc.
1223 nameserver_add_routes(index, service->nameservers_config, gw);
1224 } else if (service->nameservers != NULL) {
1226 * We add nameservers host routes for nameservers that
1227 * are not on our subnet. For those who are, the subnet
1228 * route will be installed by the time the dns proxy code
1229 * tries to reach them. The subnet route is installed
1230 * when setting the interface IP address.
1232 nameserver_add_routes(index, service->nameservers, gw);
1236 void __connman_service_nameserver_del_routes(struct connman_service *service,
1237 enum connman_ipconfig_type type)
1241 if (service == NULL)
1244 if (service->network != NULL)
1245 index = connman_network_get_index(service->network);
1246 else if (service->provider != NULL)
1247 index = connman_provider_get_index(service->provider);
1249 if (service->nameservers_config != NULL)
1250 nameserver_del_routes(index, service->nameservers_config,
1252 else if (service->nameservers != NULL)
1253 nameserver_del_routes(index, service->nameservers, type);
1256 static struct connman_stats *stats_get(struct connman_service *service)
1258 if (service->roaming == TRUE)
1259 return &service->stats_roaming;
1261 return &service->stats;
1264 static connman_bool_t stats_enabled(struct connman_service *service)
1266 struct connman_stats *stats = stats_get(service);
1268 return stats->enabled;
1271 static void stats_start(struct connman_service *service)
1273 struct connman_stats *stats = stats_get(service);
1275 DBG("service %p", service);
1277 if (stats->timer == NULL)
1280 stats->enabled = TRUE;
1281 stats->data_last.time = stats->data.time;
1283 g_timer_start(stats->timer);
1286 static void stats_stop(struct connman_service *service)
1288 struct connman_stats *stats = stats_get(service);
1289 unsigned int seconds;
1291 DBG("service %p", service);
1293 if (stats->timer == NULL)
1296 if (stats->enabled == FALSE)
1299 g_timer_stop(stats->timer);
1301 seconds = g_timer_elapsed(stats->timer, NULL);
1302 stats->data.time = stats->data_last.time + seconds;
1304 stats->enabled = FALSE;
1307 static void reset_stats(struct connman_service *service)
1309 DBG("service %p", service);
1312 service->stats.valid = FALSE;
1314 service->stats.data.rx_packets = 0;
1315 service->stats.data.tx_packets = 0;
1316 service->stats.data.rx_bytes = 0;
1317 service->stats.data.tx_bytes = 0;
1318 service->stats.data.rx_errors = 0;
1319 service->stats.data.tx_errors = 0;
1320 service->stats.data.rx_dropped = 0;
1321 service->stats.data.tx_dropped = 0;
1322 service->stats.data.time = 0;
1323 service->stats.data_last.time = 0;
1325 g_timer_reset(service->stats.timer);
1328 service->stats_roaming.valid = FALSE;
1330 service->stats_roaming.data.rx_packets = 0;
1331 service->stats_roaming.data.tx_packets = 0;
1332 service->stats_roaming.data.rx_bytes = 0;
1333 service->stats_roaming.data.tx_bytes = 0;
1334 service->stats_roaming.data.rx_errors = 0;
1335 service->stats_roaming.data.tx_errors = 0;
1336 service->stats_roaming.data.rx_dropped = 0;
1337 service->stats_roaming.data.tx_dropped = 0;
1338 service->stats_roaming.data.time = 0;
1339 service->stats_roaming.data_last.time = 0;
1341 g_timer_reset(service->stats_roaming.timer);
1344 struct connman_service *__connman_service_get_default(void)
1346 struct connman_service *service;
1347 GSequenceIter *iter;
1349 iter = g_sequence_get_begin_iter(service_list);
1351 if (g_sequence_iter_is_end(iter) == TRUE)
1354 service = g_sequence_get(iter);
1356 if (is_connected(service) == FALSE)
1362 static void default_changed(void)
1364 struct connman_service *service = __connman_service_get_default();
1366 if (service == current_default)
1369 __connman_service_timeserver_changed(current_default, NULL);
1371 current_default = service;
1373 __connman_notifier_default_changed(service);
1376 static void state_changed(struct connman_service *service)
1380 __connman_notifier_service_state_changed(service, service->state);
1382 str = state2string(service->state);
1386 connman_dbus_property_changed_basic(service->path,
1387 CONNMAN_SERVICE_INTERFACE, "State",
1388 DBUS_TYPE_STRING, &str);
1391 static void strength_changed(struct connman_service *service)
1393 if (service->strength == 0)
1396 connman_dbus_property_changed_basic(service->path,
1397 CONNMAN_SERVICE_INTERFACE, "Strength",
1398 DBUS_TYPE_BYTE, &service->strength);
1401 static void favorite_changed(struct connman_service *service)
1403 if (service->path == NULL)
1406 connman_dbus_property_changed_basic(service->path,
1407 CONNMAN_SERVICE_INTERFACE, "Favorite",
1408 DBUS_TYPE_BOOLEAN, &service->favorite);
1411 static void immutable_changed(struct connman_service *service)
1413 if (service->path == NULL)
1416 connman_dbus_property_changed_basic(service->path,
1417 CONNMAN_SERVICE_INTERFACE, "Immutable",
1418 DBUS_TYPE_BOOLEAN, &service->immutable);
1421 static void roaming_changed(struct connman_service *service)
1423 if (service->path == NULL)
1426 connman_dbus_property_changed_basic(service->path,
1427 CONNMAN_SERVICE_INTERFACE, "Roaming",
1428 DBUS_TYPE_BOOLEAN, &service->roaming);
1431 static void autoconnect_changed(struct connman_service *service)
1433 if (service->path == NULL)
1436 connman_dbus_property_changed_basic(service->path,
1437 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1438 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1441 static void append_security(DBusMessageIter *iter, void *user_data)
1443 struct connman_service *service = user_data;
1446 str = security2string(service->security);
1448 dbus_message_iter_append_basic(iter,
1449 DBUS_TYPE_STRING, &str);
1452 * Some access points incorrectly advertise WPS even when they
1453 * are configured as open or no security, so filter
1456 if (service->wps == TRUE) {
1457 switch (service->security) {
1458 case CONNMAN_SERVICE_SECURITY_PSK:
1459 case CONNMAN_SERVICE_SECURITY_WPA:
1460 case CONNMAN_SERVICE_SECURITY_RSN:
1462 dbus_message_iter_append_basic(iter,
1463 DBUS_TYPE_STRING, &str);
1465 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1466 case CONNMAN_SERVICE_SECURITY_NONE:
1467 case CONNMAN_SERVICE_SECURITY_WEP:
1468 case CONNMAN_SERVICE_SECURITY_8021X:
1474 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1476 struct connman_service *service = user_data;
1478 if (service->ipconfig_ipv4 != NULL)
1479 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1481 else if (service->ipconfig_ipv6 != NULL)
1482 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1486 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1488 struct connman_service *service = user_data;
1490 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1491 state2string(service->state_ipv4));
1493 if (is_connected_state(service, service->state_ipv4) == FALSE)
1496 if (service->ipconfig_ipv4 != NULL)
1497 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1500 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1502 struct connman_service *service = user_data;
1504 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1505 state2string(service->state_ipv6));
1507 if (is_connected_state(service, service->state_ipv6) == FALSE)
1510 if (service->ipconfig_ipv6 != NULL)
1511 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1512 service->ipconfig_ipv4);
1515 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1517 struct connman_service *service = user_data;
1519 if (service->ipconfig_ipv4 != NULL)
1520 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1524 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1526 struct connman_service *service = user_data;
1528 if (service->ipconfig_ipv6 != NULL)
1529 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1533 static void append_nameservers(DBusMessageIter *iter, char **servers)
1539 for (i = 0; servers[i] != NULL; i++) {
1540 DBG("servers[%d] %s", i, servers[i]);
1541 dbus_message_iter_append_basic(iter,
1542 DBUS_TYPE_STRING, &servers[i]);
1546 static void append_dns(DBusMessageIter *iter, void *user_data)
1548 struct connman_service *service = user_data;
1550 if (is_connected(service) == FALSE)
1553 if (service->nameservers_config != NULL) {
1554 append_nameservers(iter, service->nameservers_config);
1557 if (service->nameservers != NULL)
1558 append_nameservers(iter, service->nameservers);
1560 if (service->nameservers_auto != NULL)
1561 append_nameservers(iter, service->nameservers_auto);
1565 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1567 struct connman_service *service = user_data;
1569 if (service->nameservers_config == NULL)
1572 append_nameservers(iter, service->nameservers_config);
1575 static void append_ts(DBusMessageIter *iter, void *user_data)
1577 GSList *list = user_data;
1579 while (list != NULL) {
1580 char *timeserver = list->data;
1582 if (timeserver != NULL)
1583 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
1586 list = g_slist_next(list);
1590 static void append_tsconfig(DBusMessageIter *iter, void *user_data)
1592 struct connman_service *service = user_data;
1595 if (service->timeservers_config == NULL)
1598 for (i = 0; service->timeservers_config[i]; i++) {
1599 dbus_message_iter_append_basic(iter,
1601 &service->timeservers_config[i]);
1605 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1607 struct connman_service *service = user_data;
1610 if (service->domains == NULL)
1613 for (i = 0; service->domains[i]; i++)
1614 dbus_message_iter_append_basic(iter,
1615 DBUS_TYPE_STRING, &service->domains[i]);
1618 static void append_domain(DBusMessageIter *iter, void *user_data)
1620 struct connman_service *service = user_data;
1622 if (is_connected(service) == FALSE &&
1623 is_connecting(service) == FALSE)
1626 if (service->domains != NULL)
1627 append_domainconfig(iter, user_data);
1628 else if (service->domainname != NULL)
1629 dbus_message_iter_append_basic(iter,
1630 DBUS_TYPE_STRING, &service->domainname);
1633 static void append_proxies(DBusMessageIter *iter, void *user_data)
1635 struct connman_service *service = user_data;
1638 if (service->proxies == NULL)
1641 for (i = 0; service->proxies[i]; i++)
1642 dbus_message_iter_append_basic(iter,
1643 DBUS_TYPE_STRING, &service->proxies[i]);
1646 static void append_excludes(DBusMessageIter *iter, void *user_data)
1648 struct connman_service *service = user_data;
1651 if (service->excludes == NULL)
1654 for (i = 0; service->excludes[i]; i++)
1655 dbus_message_iter_append_basic(iter,
1656 DBUS_TYPE_STRING, &service->excludes[i]);
1659 static void append_proxy(DBusMessageIter *iter, void *user_data)
1661 struct connman_service *service = user_data;
1662 enum connman_service_proxy_method proxy;
1663 const char *pac = NULL;
1664 const char *method = proxymethod2string(
1665 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1669 if (is_connected(service) == FALSE)
1672 proxy = connman_service_get_proxy_method(service);
1675 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1677 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1679 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1680 connman_dbus_dict_append_array(iter, "Servers",
1681 DBUS_TYPE_STRING, append_proxies,
1684 connman_dbus_dict_append_array(iter, "Excludes",
1685 DBUS_TYPE_STRING, append_excludes,
1688 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1689 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1690 if (service->ipconfig_ipv4 != NULL)
1691 pac = __connman_ipconfig_get_proxy_autoconfig(
1692 service->ipconfig_ipv4);
1693 else if (service->ipconfig_ipv6 != NULL)
1694 pac = __connman_ipconfig_get_proxy_autoconfig(
1695 service->ipconfig_ipv6);
1697 if (service->pac == NULL && pac == NULL)
1700 if (service->pac != NULL)
1703 connman_dbus_dict_append_basic(iter, "URL",
1704 DBUS_TYPE_STRING, &pac);
1708 method = proxymethod2string(proxy);
1711 connman_dbus_dict_append_basic(iter, "Method",
1712 DBUS_TYPE_STRING, &method);
1715 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1717 struct connman_service *service = user_data;
1720 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1723 switch (service->proxy_config) {
1724 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1726 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1728 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1729 if (service->proxies != NULL)
1730 connman_dbus_dict_append_array(iter, "Servers",
1732 append_proxies, service);
1734 if (service->excludes != NULL)
1735 connman_dbus_dict_append_array(iter, "Excludes",
1737 append_excludes, service);
1739 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1740 if (service->pac != NULL)
1741 connman_dbus_dict_append_basic(iter, "URL",
1742 DBUS_TYPE_STRING, &service->pac);
1746 method = proxymethod2string(service->proxy_config);
1748 connman_dbus_dict_append_basic(iter, "Method",
1749 DBUS_TYPE_STRING, &method);
1752 static void append_provider(DBusMessageIter *iter, void *user_data)
1754 struct connman_service *service = user_data;
1756 DBG("%p %p", service, service->provider);
1758 if (is_connected(service) == FALSE)
1761 if (service->provider != NULL)
1762 __connman_provider_append_properties(service->provider, iter);
1766 static void settings_changed(struct connman_service *service,
1767 struct connman_ipconfig *ipconfig)
1769 connman_dbus_property_changed_dict(service->path,
1770 CONNMAN_SERVICE_INTERFACE, "IPv4",
1771 append_ipv4, service);
1773 connman_dbus_property_changed_dict(service->path,
1774 CONNMAN_SERVICE_INTERFACE, "IPv6",
1775 append_ipv6, service);
1777 __connman_notifier_ipconfig_changed(service, ipconfig);
1780 static void ipv4_configuration_changed(struct connman_service *service)
1782 connman_dbus_property_changed_dict(service->path,
1783 CONNMAN_SERVICE_INTERFACE,
1784 "IPv4.Configuration",
1789 static void ipv6_configuration_changed(struct connman_service *service)
1791 connman_dbus_property_changed_dict(service->path,
1792 CONNMAN_SERVICE_INTERFACE,
1793 "IPv6.Configuration",
1798 static void dns_changed(struct connman_service *service)
1800 connman_dbus_property_changed_array(service->path,
1801 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1802 DBUS_TYPE_STRING, append_dns, service);
1805 static void dns_configuration_changed(struct connman_service *service)
1807 connman_dbus_property_changed_array(service->path,
1808 CONNMAN_SERVICE_INTERFACE,
1809 "Nameservers.Configuration",
1810 DBUS_TYPE_STRING, append_dnsconfig, service);
1812 dns_changed(service);
1815 static void domain_changed(struct connman_service *service)
1817 connman_dbus_property_changed_array(service->path,
1818 CONNMAN_SERVICE_INTERFACE, "Domains",
1819 DBUS_TYPE_STRING, append_domain, service);
1822 static void domain_configuration_changed(struct connman_service *service)
1824 connman_dbus_property_changed_array(service->path,
1825 CONNMAN_SERVICE_INTERFACE,
1826 "Domains.Configuration",
1827 DBUS_TYPE_STRING, append_domainconfig, service);
1830 static void proxy_changed(struct connman_service *service)
1832 connman_dbus_property_changed_dict(service->path,
1833 CONNMAN_SERVICE_INTERFACE, "Proxy",
1834 append_proxy, service);
1837 static void proxy_configuration_changed(struct connman_service *service)
1839 connman_dbus_property_changed_dict(service->path,
1840 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1841 append_proxyconfig, service);
1843 proxy_changed(service);
1846 static void timeservers_configuration_changed(struct connman_service *service)
1848 connman_dbus_property_changed_array(service->path,
1849 CONNMAN_SERVICE_INTERFACE,
1850 "Timeservers.Configuration",
1852 append_tsconfig, service);
1855 static void link_changed(struct connman_service *service)
1857 connman_dbus_property_changed_dict(service->path,
1858 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1859 append_ethernet, service);
1862 static void stats_append_counters(DBusMessageIter *dict,
1863 struct connman_stats_data *stats,
1864 struct connman_stats_data *counters,
1865 connman_bool_t append_all)
1867 if (counters->rx_packets != stats->rx_packets || append_all) {
1868 counters->rx_packets = stats->rx_packets;
1869 connman_dbus_dict_append_basic(dict, "RX.Packets",
1870 DBUS_TYPE_UINT32, &stats->rx_packets);
1873 if (counters->tx_packets != stats->tx_packets || append_all) {
1874 counters->tx_packets = stats->tx_packets;
1875 connman_dbus_dict_append_basic(dict, "TX.Packets",
1876 DBUS_TYPE_UINT32, &stats->tx_packets);
1879 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1880 counters->rx_bytes = stats->rx_bytes;
1881 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1882 DBUS_TYPE_UINT32, &stats->rx_bytes);
1885 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1886 counters->tx_bytes = stats->tx_bytes;
1887 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1888 DBUS_TYPE_UINT32, &stats->tx_bytes);
1891 if (counters->rx_errors != stats->rx_errors || append_all) {
1892 counters->rx_errors = stats->rx_errors;
1893 connman_dbus_dict_append_basic(dict, "RX.Errors",
1894 DBUS_TYPE_UINT32, &stats->rx_errors);
1897 if (counters->tx_errors != stats->tx_errors || append_all) {
1898 counters->tx_errors = stats->tx_errors;
1899 connman_dbus_dict_append_basic(dict, "TX.Errors",
1900 DBUS_TYPE_UINT32, &stats->tx_errors);
1903 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1904 counters->rx_dropped = stats->rx_dropped;
1905 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1906 DBUS_TYPE_UINT32, &stats->rx_dropped);
1909 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1910 counters->tx_dropped = stats->tx_dropped;
1911 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1912 DBUS_TYPE_UINT32, &stats->tx_dropped);
1915 if (counters->time != stats->time || append_all) {
1916 counters->time = stats->time;
1917 connman_dbus_dict_append_basic(dict, "Time",
1918 DBUS_TYPE_UINT32, &stats->time);
1922 static void stats_append(struct connman_service *service,
1923 const char *counter,
1924 struct connman_stats_counter *counters,
1925 connman_bool_t append_all)
1927 DBusMessageIter array, dict;
1930 DBG("service %p counter %s", service, counter);
1932 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1936 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1937 &service->path, DBUS_TYPE_INVALID);
1939 dbus_message_iter_init_append(msg, &array);
1942 connman_dbus_dict_open(&array, &dict);
1944 stats_append_counters(&dict, &service->stats.data,
1945 &counters->stats.data, append_all);
1947 connman_dbus_dict_close(&array, &dict);
1949 /* roaming counter */
1950 connman_dbus_dict_open(&array, &dict);
1952 stats_append_counters(&dict, &service->stats_roaming.data,
1953 &counters->stats_roaming.data, append_all);
1955 connman_dbus_dict_close(&array, &dict);
1957 __connman_counter_send_usage(counter, msg);
1960 static void stats_update(struct connman_service *service,
1961 unsigned int rx_packets, unsigned int tx_packets,
1962 unsigned int rx_bytes, unsigned int tx_bytes,
1963 unsigned int rx_errors, unsigned int tx_errors,
1964 unsigned int rx_dropped, unsigned int tx_dropped)
1966 struct connman_stats *stats = stats_get(service);
1967 struct connman_stats_data *data_last = &stats->data_last;
1968 struct connman_stats_data *data = &stats->data;
1969 unsigned int seconds;
1971 DBG("service %p", service);
1973 if (stats->valid == TRUE) {
1975 rx_packets - data_last->rx_packets;
1977 tx_packets - data_last->tx_packets;
1979 rx_bytes - data_last->rx_bytes;
1981 tx_bytes - data_last->tx_bytes;
1983 rx_errors - data_last->rx_errors;
1985 tx_errors - data_last->tx_errors;
1987 rx_dropped - data_last->rx_dropped;
1989 tx_dropped - data_last->tx_dropped;
1991 stats->valid = TRUE;
1994 data_last->rx_packets = rx_packets;
1995 data_last->tx_packets = tx_packets;
1996 data_last->rx_bytes = rx_bytes;
1997 data_last->tx_bytes = tx_bytes;
1998 data_last->rx_errors = rx_errors;
1999 data_last->tx_errors = tx_errors;
2000 data_last->rx_dropped = rx_dropped;
2001 data_last->tx_dropped = tx_dropped;
2003 seconds = g_timer_elapsed(stats->timer, NULL);
2004 stats->data.time = stats->data_last.time + seconds;
2007 void __connman_service_notify(struct connman_service *service,
2008 unsigned int rx_packets, unsigned int tx_packets,
2009 unsigned int rx_bytes, unsigned int tx_bytes,
2010 unsigned int rx_errors, unsigned int tx_errors,
2011 unsigned int rx_dropped, unsigned int tx_dropped)
2013 GHashTableIter iter;
2014 gpointer key, value;
2015 const char *counter;
2016 struct connman_stats_counter *counters;
2017 struct connman_stats_data *data;
2020 if (service == NULL)
2023 if (is_connected(service) == FALSE)
2026 stats_update(service,
2027 rx_packets, tx_packets,
2029 rx_errors, tx_errors,
2030 rx_dropped, tx_dropped);
2032 data = &stats_get(service)->data;
2033 err = __connman_stats_update(service, service->roaming, data);
2035 connman_error("Failed to store statistics for %s",
2036 service->identifier);
2038 g_hash_table_iter_init(&iter, service->counter_table);
2039 while (g_hash_table_iter_next(&iter, &key, &value)) {
2043 stats_append(service, counter, counters, counters->append_all);
2044 counters->append_all = FALSE;
2048 int __connman_service_counter_register(const char *counter)
2050 struct connman_service *service;
2051 GSequenceIter *iter;
2052 struct connman_stats_counter *counters;
2054 DBG("counter %s", counter);
2056 counter_list = g_slist_append(counter_list, (gpointer)counter);
2058 iter = g_sequence_get_begin_iter(service_list);
2060 while (g_sequence_iter_is_end(iter) == FALSE) {
2061 service = g_sequence_get(iter);
2063 counters = g_try_new0(struct connman_stats_counter, 1);
2064 if (counters == NULL)
2067 counters->append_all = TRUE;
2069 g_hash_table_replace(service->counter_table, (gpointer)counter,
2072 iter = g_sequence_iter_next(iter);
2078 void __connman_service_counter_unregister(const char *counter)
2080 struct connman_service *service;
2081 GSequenceIter *iter;
2083 DBG("counter %s", counter);
2085 iter = g_sequence_get_begin_iter(service_list);
2087 while (g_sequence_iter_is_end(iter) == FALSE) {
2088 service = g_sequence_get(iter);
2090 g_hash_table_remove(service->counter_table, counter);
2092 iter = g_sequence_iter_next(iter);
2095 counter_list = g_slist_remove(counter_list, counter);
2098 GSequence *__connman_service_get_list(struct connman_session *session,
2099 service_match_cb service_match,
2100 create_service_entry_cb create_service_entry,
2101 GDestroyNotify destroy_service_entry)
2104 GSequenceIter *iter;
2105 struct connman_service *service;
2106 struct service_entry *entry;
2108 list = g_sequence_new(destroy_service_entry);
2112 iter = g_sequence_get_begin_iter(service_list);
2114 while (g_sequence_iter_is_end(iter) == FALSE) {
2115 service = g_sequence_get(iter);
2117 if (service_match(session, service) == TRUE) {
2118 entry = create_service_entry(service, service->name,
2123 g_sequence_append(list, entry);
2126 iter = g_sequence_iter_next(iter);
2132 void __connman_service_session_inc(struct connman_service *service)
2134 DBG("service %p ref count %d", service,
2135 service->session_usage_count + 1);
2137 __sync_fetch_and_add(&service->session_usage_count, 1);
2140 connman_bool_t __connman_service_session_dec(struct connman_service *service)
2142 DBG("service %p ref count %d", service,
2143 service->session_usage_count - 1);
2145 if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
2151 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2152 struct connman_service *service)
2157 str = __connman_service_type2string(service->type);
2159 connman_dbus_dict_append_basic(dict, "Type",
2160 DBUS_TYPE_STRING, &str);
2162 connman_dbus_dict_append_array(dict, "Security",
2163 DBUS_TYPE_STRING, append_security, service);
2165 str = state2string(service->state);
2167 connman_dbus_dict_append_basic(dict, "State",
2168 DBUS_TYPE_STRING, &str);
2170 str = error2string(service->error);
2172 connman_dbus_dict_append_basic(dict, "Error",
2173 DBUS_TYPE_STRING, &str);
2175 if (service->strength > 0)
2176 connman_dbus_dict_append_basic(dict, "Strength",
2177 DBUS_TYPE_BYTE, &service->strength);
2179 connman_dbus_dict_append_basic(dict, "Favorite",
2180 DBUS_TYPE_BOOLEAN, &service->favorite);
2182 connman_dbus_dict_append_basic(dict, "Immutable",
2183 DBUS_TYPE_BOOLEAN, &service->immutable);
2185 if (service->favorite == TRUE)
2186 connman_dbus_dict_append_basic(dict, "AutoConnect",
2187 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2189 connman_dbus_dict_append_basic(dict, "AutoConnect",
2190 DBUS_TYPE_BOOLEAN, &service->favorite);
2192 if (service->name != NULL)
2193 connman_dbus_dict_append_basic(dict, "Name",
2194 DBUS_TYPE_STRING, &service->name);
2196 switch (service->type) {
2197 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2198 case CONNMAN_SERVICE_TYPE_SYSTEM:
2199 case CONNMAN_SERVICE_TYPE_GPS:
2200 case CONNMAN_SERVICE_TYPE_VPN:
2201 case CONNMAN_SERVICE_TYPE_GADGET:
2203 case CONNMAN_SERVICE_TYPE_CELLULAR:
2204 connman_dbus_dict_append_basic(dict, "Roaming",
2205 DBUS_TYPE_BOOLEAN, &service->roaming);
2207 connman_dbus_dict_append_dict(dict, "Ethernet",
2208 append_ethernet, service);
2210 case CONNMAN_SERVICE_TYPE_WIFI:
2211 case CONNMAN_SERVICE_TYPE_ETHERNET:
2212 case CONNMAN_SERVICE_TYPE_WIMAX:
2213 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2214 connman_dbus_dict_append_dict(dict, "Ethernet",
2215 append_ethernet, service);
2219 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2221 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2222 append_ipv4config, service);
2224 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2226 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2227 append_ipv6config, service);
2229 connman_dbus_dict_append_array(dict, "Nameservers",
2230 DBUS_TYPE_STRING, append_dns, service);
2232 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2233 DBUS_TYPE_STRING, append_dnsconfig, service);
2235 if (service->state == CONNMAN_SERVICE_STATE_READY ||
2236 service->state == CONNMAN_SERVICE_STATE_ONLINE)
2237 list = __connman_timeserver_get_all(service);
2241 connman_dbus_dict_append_array(dict, "Timeservers",
2242 DBUS_TYPE_STRING, append_ts, list);
2244 g_slist_free_full(list, g_free);
2246 connman_dbus_dict_append_array(dict, "Timeservers.Configuration",
2247 DBUS_TYPE_STRING, append_tsconfig, service);
2249 connman_dbus_dict_append_array(dict, "Domains",
2250 DBUS_TYPE_STRING, append_domain, service);
2252 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2253 DBUS_TYPE_STRING, append_domainconfig, service);
2255 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2257 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2258 append_proxyconfig, service);
2260 connman_dbus_dict_append_dict(dict, "Provider",
2261 append_provider, service);
2264 static void append_struct_service(DBusMessageIter *iter,
2265 connman_dbus_append_cb_t function,
2266 struct connman_service *service)
2268 DBusMessageIter entry, dict;
2270 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2272 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2275 connman_dbus_dict_open(&entry, &dict);
2276 if (function != NULL)
2277 function(&dict, service);
2278 connman_dbus_dict_close(&entry, &dict);
2280 dbus_message_iter_close_container(iter, &entry);
2283 static void append_dict_properties(DBusMessageIter *dict, void *user_data)
2285 struct connman_service *service = user_data;
2287 append_properties(dict, TRUE, service);
2290 static void append_struct(gpointer value, gpointer user_data)
2292 struct connman_service *service = value;
2293 DBusMessageIter *iter = user_data;
2295 if (service->path == NULL)
2298 append_struct_service(iter, append_dict_properties, service);
2301 void __connman_service_list_struct(DBusMessageIter *iter)
2303 g_sequence_foreach(service_list, append_struct, iter);
2306 connman_bool_t __connman_service_is_hidden(struct connman_service *service)
2308 return service->hidden;
2312 __connman_service_is_split_routing(struct connman_service *service)
2314 return service->do_split_routing;
2317 int __connman_service_get_index(struct connman_service *service)
2319 if (service == NULL)
2322 if (service->network != NULL)
2323 return connman_network_get_index(service->network);
2324 else if (service->provider != NULL)
2325 return connman_provider_get_index(service->provider);
2330 void __connman_service_set_hidden(struct connman_service *service)
2332 if (service == NULL || service->hidden == TRUE)
2335 service->hidden_service = TRUE;
2338 void __connman_service_set_domainname(struct connman_service *service,
2339 const char *domainname)
2341 if (service == NULL || service->hidden == TRUE)
2344 g_free(service->domainname);
2345 service->domainname = g_strdup(domainname);
2347 domain_changed(service);
2350 const char *connman_service_get_domainname(struct connman_service *service)
2352 if (service == NULL)
2355 if (service->domains != NULL)
2356 return service->domains[0];
2358 return service->domainname;
2361 char **connman_service_get_nameservers(struct connman_service *service)
2363 if (service == NULL)
2366 if (service->nameservers_config != NULL)
2367 return g_strdupv(service->nameservers_config);
2368 else if (service->nameservers != NULL ||
2369 service->nameservers_auto != NULL) {
2370 int len = 0, len_auto = 0, i;
2373 if (service->nameservers != NULL)
2374 len = g_strv_length(service->nameservers);
2375 if (service->nameservers_auto != NULL)
2376 len_auto = g_strv_length(service->nameservers_auto);
2378 nameservers = g_try_new0(char *, len + len_auto + 1);
2379 if (nameservers == NULL)
2382 for (i = 0; i < len; i++)
2383 nameservers[i] = g_strdup(service->nameservers[i]);
2385 for (i = 0; i < len_auto; i++)
2386 nameservers[i + len] =
2387 g_strdup(service->nameservers_auto[i]);
2395 char **connman_service_get_timeservers_config(struct connman_service *service)
2397 if (service == NULL)
2400 return service->timeservers_config;
2403 char **connman_service_get_timeservers(struct connman_service *service)
2405 if (service == NULL)
2408 if (service->timeservers != NULL)
2409 return service->timeservers;
2414 void connman_service_set_proxy_method(struct connman_service *service,
2415 enum connman_service_proxy_method method)
2417 if (service == NULL || service->hidden == TRUE)
2420 service->proxy = method;
2422 proxy_changed(service);
2424 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2425 __connman_notifier_proxy_changed(service);
2428 enum connman_service_proxy_method connman_service_get_proxy_method(
2429 struct connman_service *service)
2431 if (service == NULL)
2432 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2434 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2435 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2436 service->pac == NULL)
2437 return service->proxy;
2439 return service->proxy_config;
2442 return service->proxy;
2445 char **connman_service_get_proxy_servers(struct connman_service *service)
2447 return g_strdupv(service->proxies);
2450 char **connman_service_get_proxy_excludes(struct connman_service *service)
2452 return g_strdupv(service->excludes);
2455 const char *connman_service_get_proxy_url(struct connman_service *service)
2457 if (service == NULL)
2460 return service->pac;
2463 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2466 if (service == NULL || service->hidden == TRUE)
2469 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2471 if (service->ipconfig_ipv4) {
2472 if (__connman_ipconfig_set_proxy_autoconfig(
2473 service->ipconfig_ipv4, url) < 0)
2475 } else if (service->ipconfig_ipv6) {
2476 if (__connman_ipconfig_set_proxy_autoconfig(
2477 service->ipconfig_ipv6, url) < 0)
2482 proxy_changed(service);
2484 __connman_notifier_proxy_changed(service);
2487 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2489 if (service == NULL)
2492 if (service->ipconfig_ipv4)
2493 return __connman_ipconfig_get_proxy_autoconfig(
2494 service->ipconfig_ipv4);
2495 else if (service->ipconfig_ipv6)
2496 return __connman_ipconfig_get_proxy_autoconfig(
2497 service->ipconfig_ipv6);
2501 int __connman_service_timeserver_append(struct connman_service *service,
2502 const char *timeserver)
2506 DBG("service %p timeserver %s", service, timeserver);
2508 if (timeserver == NULL)
2511 if (service->timeservers != NULL) {
2514 for (i = 0; service->timeservers[i] != NULL; i++)
2515 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2518 len = g_strv_length(service->timeservers);
2519 service->timeservers = g_try_renew(char *, service->timeservers,
2523 service->timeservers = g_try_new0(char *, len + 2);
2526 if (service->timeservers == NULL)
2529 service->timeservers[len] = g_strdup(timeserver);
2530 service->timeservers[len + 1] = NULL;
2535 int __connman_service_timeserver_remove(struct connman_service *service,
2536 const char *timeserver)
2539 int len, i, j, found = 0;
2541 DBG("service %p timeserver %s", service, timeserver);
2543 if (timeserver == NULL)
2546 if (service->timeservers == NULL)
2549 for (i = 0; service->timeservers != NULL &&
2550 service->timeservers[i] != NULL; i++)
2551 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2559 len = g_strv_length(service->timeservers);
2562 g_strfreev(service->timeservers);
2563 service->timeservers = NULL;
2568 servers = g_try_new0(char *, len);
2569 if (servers == NULL)
2572 for (i = 0, j = 0; i < len; i++) {
2573 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2574 servers[j] = g_strdup(service->timeservers[i]);
2575 if (servers[j] == NULL)
2580 servers[len - 1] = NULL;
2582 g_strfreev(service->timeservers);
2583 service->timeservers = servers;
2588 void __connman_service_timeserver_changed(struct connman_service *service,
2591 if (service == NULL)
2594 connman_dbus_property_changed_array(service->path,
2595 CONNMAN_SERVICE_INTERFACE, "Timeservers",
2596 DBUS_TYPE_STRING, append_ts, ts_list);
2599 void __connman_service_set_pac(struct connman_service *service,
2602 if (service->hidden == TRUE)
2604 g_free(service->pac);
2605 service->pac = g_strdup(pac);
2607 proxy_changed(service);
2610 void __connman_service_set_identity(struct connman_service *service,
2611 const char *identity)
2613 if (service->immutable || service->hidden == TRUE)
2616 g_free(service->identity);
2617 service->identity = g_strdup(identity);
2619 if (service->network != NULL)
2620 connman_network_set_string(service->network,
2625 void __connman_service_set_agent_identity(struct connman_service *service,
2626 const char *agent_identity)
2628 if (service->hidden == TRUE)
2630 g_free(service->agent_identity);
2631 service->agent_identity = g_strdup(agent_identity);
2633 if (service->network != NULL)
2634 connman_network_set_string(service->network,
2635 "WiFi.AgentIdentity",
2636 service->agent_identity);
2639 static int check_passphrase(enum connman_service_security security,
2640 const char *passphrase)
2645 if (passphrase == NULL)
2648 length = strlen(passphrase);
2651 case CONNMAN_SERVICE_SECURITY_PSK:
2652 case CONNMAN_SERVICE_SECURITY_WPA:
2653 case CONNMAN_SERVICE_SECURITY_RSN:
2654 /* A raw key is always 64 bytes length,
2655 * its content is in hex representation.
2656 * A PSK key must be between [8..63].
2659 for (i = 0; i < 64; i++)
2660 if (!isxdigit((unsigned char)
2663 } else if (length < 8 || length > 63)
2666 case CONNMAN_SERVICE_SECURITY_WEP:
2667 /* length of WEP key is 10 or 26
2668 * length of WEP passphrase is 5 or 13
2670 if (length == 10 || length == 26) {
2671 for (i = 0; i < length; i++)
2672 if (!isxdigit((unsigned char)
2675 } else if (length != 5 && length != 13)
2678 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2679 case CONNMAN_SERVICE_SECURITY_NONE:
2680 case CONNMAN_SERVICE_SECURITY_8021X:
2687 int __connman_service_set_passphrase(struct connman_service *service,
2688 const char *passphrase)
2692 if (service->immutable == TRUE || service->hidden == TRUE)
2695 err = check_passphrase(service->security, passphrase);
2698 g_free(service->passphrase);
2699 service->passphrase = g_strdup(passphrase);
2701 if (service->network != NULL)
2702 connman_network_set_string(service->network,
2704 service->passphrase);
2705 service_save(service);
2711 void __connman_service_set_agent_passphrase(struct connman_service *service,
2712 const char *agent_passphrase)
2714 if (service->hidden == TRUE)
2716 g_free(service->agent_passphrase);
2717 service->agent_passphrase = g_strdup(agent_passphrase);
2719 if (service->network != NULL)
2720 connman_network_set_string(service->network,
2721 "WiFi.AgentPassphrase",
2722 service->agent_passphrase);
2725 static DBusMessage *get_properties(DBusConnection *conn,
2726 DBusMessage *msg, void *user_data)
2728 struct connman_service *service = user_data;
2730 DBusMessageIter array, dict;
2732 DBG("service %p", service);
2734 reply = dbus_message_new_method_return(msg);
2738 dbus_message_iter_init_append(reply, &array);
2740 connman_dbus_dict_open(&array, &dict);
2741 append_properties(&dict, FALSE, service);
2742 connman_dbus_dict_close(&array, &dict);
2747 static int update_proxy_configuration(struct connman_service *service,
2748 DBusMessageIter *array)
2750 DBusMessageIter dict;
2751 enum connman_service_proxy_method method;
2752 GString *servers_str = NULL;
2753 GString *excludes_str = NULL;
2754 const char *url = NULL;
2756 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2758 dbus_message_iter_recurse(array, &dict);
2760 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2761 DBusMessageIter entry, variant;
2765 dbus_message_iter_recurse(&dict, &entry);
2767 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2770 dbus_message_iter_get_basic(&entry, &key);
2771 dbus_message_iter_next(&entry);
2773 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2776 dbus_message_iter_recurse(&entry, &variant);
2778 type = dbus_message_iter_get_arg_type(&variant);
2780 if (g_str_equal(key, "Method") == TRUE) {
2783 if (type != DBUS_TYPE_STRING)
2786 dbus_message_iter_get_basic(&variant, &val);
2787 method = string2proxymethod(val);
2788 } else if (g_str_equal(key, "URL") == TRUE) {
2789 if (type != DBUS_TYPE_STRING)
2792 dbus_message_iter_get_basic(&variant, &url);
2793 } else if (g_str_equal(key, "Servers") == TRUE) {
2794 DBusMessageIter str_array;
2796 if (type != DBUS_TYPE_ARRAY)
2799 servers_str = g_string_new(NULL);
2800 if (servers_str == NULL)
2803 dbus_message_iter_recurse(&variant, &str_array);
2805 while (dbus_message_iter_get_arg_type(&str_array) ==
2809 dbus_message_iter_get_basic(&str_array, &val);
2811 if (servers_str->len > 0)
2812 g_string_append_printf(servers_str,
2815 g_string_append(servers_str, val);
2817 dbus_message_iter_next(&str_array);
2819 } else if (g_str_equal(key, "Excludes") == TRUE) {
2820 DBusMessageIter str_array;
2822 if (type != DBUS_TYPE_ARRAY)
2825 excludes_str = g_string_new(NULL);
2826 if (excludes_str == NULL)
2829 dbus_message_iter_recurse(&variant, &str_array);
2831 while (dbus_message_iter_get_arg_type(&str_array) ==
2835 dbus_message_iter_get_basic(&str_array, &val);
2837 if (excludes_str->len > 0)
2838 g_string_append_printf(excludes_str,
2841 g_string_append(excludes_str, val);
2843 dbus_message_iter_next(&str_array);
2847 dbus_message_iter_next(&dict);
2851 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2853 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2854 if (servers_str == NULL && service->proxies == NULL)
2857 if (servers_str != NULL) {
2858 g_strfreev(service->proxies);
2860 if (servers_str->len > 0)
2861 service->proxies = g_strsplit_set(
2862 servers_str->str, " ", 0);
2864 service->proxies = NULL;
2867 if (excludes_str != NULL) {
2868 g_strfreev(service->excludes);
2870 if (excludes_str->len > 0)
2871 service->excludes = g_strsplit_set(
2872 excludes_str->str, " ", 0);
2874 service->excludes = NULL;
2877 if (service->proxies == NULL)
2878 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2881 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2882 g_free(service->pac);
2884 if (url != NULL && strlen(url) > 0)
2885 service->pac = g_strdup(url);
2887 service->pac = NULL;
2889 /* if we are connected:
2890 - if service->pac == NULL
2891 - if __connman_ipconfig_get_proxy_autoconfig(
2892 service->ipconfig) == NULL
2893 --> We should start WPAD */
2896 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2900 if (servers_str != NULL)
2901 g_string_free(servers_str, TRUE);
2903 if (excludes_str != NULL)
2904 g_string_free(excludes_str, TRUE);
2906 service->proxy_config = method;
2911 if (servers_str != NULL)
2912 g_string_free(servers_str, TRUE);
2914 if (excludes_str != NULL)
2915 g_string_free(excludes_str, TRUE);
2920 static int set_ipconfig(struct connman_service *service,
2921 struct connman_ipconfig *ipconfig,
2922 DBusMessageIter *array,
2923 enum connman_service_state state,
2924 enum connman_service_state *new_state)
2926 enum connman_ipconfig_method old_method;
2927 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2928 enum connman_ipconfig_type type;
2931 if (ipconfig == NULL)
2934 old_method = __connman_ipconfig_get_method(ipconfig);
2936 if (is_connecting_state(service, state) ||
2937 is_connected_state(service, state))
2938 __connman_network_clear_ipconfig(service->network, ipconfig);
2940 err = __connman_ipconfig_set_config(ipconfig, array);
2941 method = __connman_ipconfig_get_method(ipconfig);
2942 type = __connman_ipconfig_get_config_type(ipconfig);
2944 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2945 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2946 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2947 *new_state = service->state_ipv4 =
2948 CONNMAN_SERVICE_STATE_CONFIGURATION;
2949 __connman_ipconfig_enable(ipconfig);
2952 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2953 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2954 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2955 *new_state = service->state_ipv6;
2956 __connman_ipconfig_enable(ipconfig);
2960 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2961 type, method, state2string(*new_state));
2966 static DBusMessage *set_property(DBusConnection *conn,
2967 DBusMessage *msg, void *user_data)
2969 struct connman_service *service = user_data;
2970 DBusMessageIter iter, value;
2974 DBG("service %p", service);
2976 if (dbus_message_iter_init(msg, &iter) == FALSE)
2977 return __connman_error_invalid_arguments(msg);
2979 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
2980 return __connman_error_invalid_arguments(msg);
2982 dbus_message_iter_get_basic(&iter, &name);
2983 dbus_message_iter_next(&iter);
2985 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
2986 return __connman_error_invalid_arguments(msg);
2988 dbus_message_iter_recurse(&iter, &value);
2990 type = dbus_message_iter_get_arg_type(&value);
2992 if (g_str_equal(name, "AutoConnect") == TRUE) {
2993 connman_bool_t autoconnect;
2995 if (type != DBUS_TYPE_BOOLEAN)
2996 return __connman_error_invalid_arguments(msg);
2998 if (service->favorite == FALSE)
2999 return __connman_error_invalid_service(msg);
3001 dbus_message_iter_get_basic(&value, &autoconnect);
3003 if (service->autoconnect == autoconnect)
3004 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3006 service->autoconnect = autoconnect;
3008 autoconnect_changed(service);
3010 service_save(service);
3011 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
3012 DBusMessageIter entry;
3017 if (type != DBUS_TYPE_ARRAY)
3018 return __connman_error_invalid_arguments(msg);
3020 str = g_string_new(NULL);
3022 return __connman_error_invalid_arguments(msg);
3024 index = connman_network_get_index(service->network);
3025 gw = __connman_ipconfig_get_gateway_from_index(index,
3026 CONNMAN_IPCONFIG_TYPE_ALL);
3028 if (gw && strlen(gw))
3029 __connman_service_nameserver_del_routes(service,
3030 CONNMAN_IPCONFIG_TYPE_ALL);
3032 dbus_message_iter_recurse(&value, &entry);
3034 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3036 dbus_message_iter_get_basic(&entry, &val);
3037 dbus_message_iter_next(&entry);
3039 g_string_append_printf(str, " %s", val);
3041 g_string_append(str, val);
3044 remove_nameservers(service, NULL, service->nameservers_config);
3045 g_strfreev(service->nameservers_config);
3048 service->nameservers_config =
3049 g_strsplit_set(str->str, " ", 0);
3051 service->nameservers_config = NULL;
3054 g_string_free(str, TRUE);
3056 if (gw && strlen(gw))
3057 __connman_service_nameserver_add_routes(service, gw);
3059 update_nameservers(service);
3060 dns_configuration_changed(service);
3062 service_save(service);
3063 } else if (g_str_equal(name, "Timeservers.Configuration") == TRUE) {
3064 DBusMessageIter entry;
3065 GSList *list = NULL;
3068 if (type != DBUS_TYPE_ARRAY)
3069 return __connman_error_invalid_arguments(msg);
3071 dbus_message_iter_recurse(&value, &entry);
3073 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3077 dbus_message_iter_get_basic(&entry, &val);
3079 new_head = __connman_timeserver_add_list(list, val);
3080 if (list != new_head) {
3085 dbus_message_iter_next(&entry);
3088 g_strfreev(service->timeservers_config);
3089 service->timeservers_config = NULL;
3092 service->timeservers_config = g_new0(char *, count+1);
3094 while (list != NULL) {
3096 service->timeservers_config[count] = list->data;
3097 list = g_slist_delete_link(list, list);
3101 service_save(service);
3102 timeservers_configuration_changed(service);
3104 __connman_timeserver_sync(service);
3105 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
3106 DBusMessageIter entry;
3109 if (type != DBUS_TYPE_ARRAY)
3110 return __connman_error_invalid_arguments(msg);
3112 str = g_string_new(NULL);
3114 return __connman_error_invalid_arguments(msg);
3116 dbus_message_iter_recurse(&value, &entry);
3118 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3120 dbus_message_iter_get_basic(&entry, &val);
3121 dbus_message_iter_next(&entry);
3123 g_string_append_printf(str, " %s", val);
3125 g_string_append(str, val);
3128 remove_searchdomains(service, NULL, service->domains);
3129 g_strfreev(service->domains);
3132 service->domains = g_strsplit_set(str->str, " ", 0);
3134 service->domains = NULL;
3136 g_string_free(str, TRUE);
3138 update_nameservers(service);
3139 domain_configuration_changed(service);
3141 service_save(service);
3142 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
3145 if (type != DBUS_TYPE_ARRAY)
3146 return __connman_error_invalid_arguments(msg);
3148 err = update_proxy_configuration(service, &value);
3151 return __connman_error_failed(msg, -err);
3153 proxy_configuration_changed(service);
3155 __connman_notifier_proxy_changed(service);
3157 service_save(service);
3158 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
3159 g_str_equal(name, "IPv6.Configuration")) {
3161 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
3162 enum connman_service_state state =
3163 CONNMAN_SERVICE_STATE_UNKNOWN;
3168 if (service->ipconfig_ipv4 == NULL &&
3169 service->ipconfig_ipv6 == NULL)
3170 return __connman_error_invalid_property(msg);
3172 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
3173 ipv4 = service->ipconfig_ipv4;
3174 err = set_ipconfig(service, ipv4, &value,
3175 service->state_ipv4, &state);
3177 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
3178 ipv6 = service->ipconfig_ipv6;
3179 err = set_ipconfig(service, ipv6, &value,
3180 service->state_ipv6, &state);
3184 if (is_connected_state(service, state) ||
3185 is_connecting_state(service, state))
3186 __connman_network_set_ipconfig(service->network,
3188 return __connman_error_failed(msg, -err);
3192 ipv4_configuration_changed(service);
3194 ipv6_configuration_changed(service);
3196 if (is_connecting(service) || is_connected(service))
3197 __connman_network_set_ipconfig(service->network,
3200 service_save(service);
3202 return __connman_error_invalid_property(msg);
3204 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3207 static void set_idle(struct connman_service *service)
3209 service->state = service->state_ipv4 = service->state_ipv6 =
3210 CONNMAN_SERVICE_STATE_IDLE;
3211 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3212 state_changed(service);
3215 static DBusMessage *clear_property(DBusConnection *conn,
3216 DBusMessage *msg, void *user_data)
3218 struct connman_service *service = user_data;
3221 DBG("service %p", service);
3223 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
3226 if (g_str_equal(name, "Error") == TRUE) {
3229 g_get_current_time(&service->modified);
3230 service_save(service);
3232 return __connman_error_invalid_property(msg);
3234 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3237 static connman_bool_t is_ignore(struct connman_service *service)
3239 if (service->autoconnect == FALSE)
3242 if (service->roaming == TRUE)
3245 if (service->ignore == TRUE)
3248 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3254 struct preferred_tech_data {
3255 GSequence *preferred_list;
3256 enum connman_service_type type;
3259 static void preferred_tech_add_by_type(gpointer data, gpointer user_data)
3261 struct connman_service *service = data;
3262 struct preferred_tech_data *tech_data = user_data;
3264 if (service->type == tech_data->type) {
3265 g_sequence_append(tech_data->preferred_list, service);
3267 DBG("type %d service %p %s", tech_data->type, service,
3272 static GSequence* preferred_tech_list_get(GSequence *list)
3274 unsigned int *tech_array;
3275 struct preferred_tech_data tech_data;
3278 tech_array = connman_setting_get_uint_list("PreferredTechnologies");
3279 if (tech_array == NULL)
3282 tech_data.preferred_list = g_sequence_new(NULL);
3284 for (i = 0; tech_array[i] != 0; i += 1) {
3285 tech_data.type = tech_array[i];
3286 g_sequence_foreach(service_list, preferred_tech_add_by_type,
3290 return tech_data.preferred_list;
3293 static connman_bool_t auto_connect_service(GSequenceIter* iter,
3294 connman_bool_t preferred)
3296 struct connman_service *service = NULL;
3298 while (g_sequence_iter_is_end(iter) == FALSE) {
3299 service = g_sequence_get(iter);
3301 if (service->pending != NULL)
3304 if (is_connecting(service) == TRUE)
3307 if (service->favorite == FALSE) {
3308 if (preferred == TRUE)
3313 if (is_connected(service) == TRUE) {
3314 if (preferred == TRUE && service->state !=
3315 CONNMAN_SERVICE_STATE_ONLINE)
3320 if (is_ignore(service) == FALSE && service->state ==
3321 CONNMAN_SERVICE_STATE_IDLE)
3327 iter = g_sequence_iter_next(iter);
3330 if (service != NULL) {
3332 DBG("service %p %s %s", service, service->name,
3333 (preferred == TRUE)? "preferred": "auto");
3335 service->userconnect = FALSE;
3336 __connman_service_connect(service);
3342 static gboolean run_auto_connect(gpointer data)
3344 GSequenceIter *iter = NULL;
3345 GSequence *preferred_tech;
3347 autoconnect_timeout = 0;
3351 preferred_tech = preferred_tech_list_get(service_list);
3352 if (preferred_tech != NULL)
3353 iter = g_sequence_get_begin_iter(preferred_tech);
3355 if (iter == NULL || auto_connect_service(iter, TRUE) == FALSE)
3356 iter = g_sequence_get_begin_iter(service_list);
3359 auto_connect_service(iter, FALSE);
3361 if (preferred_tech != NULL)
3362 g_sequence_free(preferred_tech);
3367 void __connman_service_auto_connect(void)
3371 if (__connman_session_mode() == TRUE) {
3372 DBG("Session mode enabled: auto connect disabled");
3376 if (autoconnect_timeout != 0)
3379 autoconnect_timeout = g_timeout_add_seconds(0, run_auto_connect, NULL);
3382 static void remove_timeout(struct connman_service *service)
3384 if (service->timeout > 0) {
3385 g_source_remove(service->timeout);
3386 service->timeout = 0;
3390 static void reply_pending(struct connman_service *service, int error)
3392 remove_timeout(service);
3394 if (service->pending != NULL) {
3398 reply = __connman_error_failed(service->pending,
3401 g_dbus_send_message(connection, reply);
3405 sender = dbus_message_get_interface(service->pending);
3407 DBG("sender %s", sender);
3409 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
3410 g_dbus_send_reply(connection, service->pending,
3411 DBUS_TYPE_OBJECT_PATH, &service->path,
3414 g_dbus_send_reply(connection, service->pending,
3418 dbus_message_unref(service->pending);
3419 service->pending = NULL;
3423 static gboolean connect_timeout(gpointer user_data)
3425 struct connman_service *service = user_data;
3426 connman_bool_t autoconnect = FALSE;
3428 DBG("service %p", service);
3430 service->timeout = 0;
3432 if (service->network != NULL)
3433 __connman_network_disconnect(service->network);
3435 __connman_ipconfig_disable(service->ipconfig_ipv4);
3436 __connman_ipconfig_disable(service->ipconfig_ipv6);
3438 __connman_stats_service_unregister(service);
3440 if (service->pending != NULL) {
3443 reply = __connman_error_operation_timeout(service->pending);
3445 g_dbus_send_message(connection, reply);
3447 dbus_message_unref(service->pending);
3448 service->pending = NULL;
3452 __connman_service_ipconfig_indicate_state(service,
3453 CONNMAN_SERVICE_STATE_FAILURE,
3454 CONNMAN_IPCONFIG_TYPE_IPV4);
3455 __connman_service_ipconfig_indicate_state(service,
3456 CONNMAN_SERVICE_STATE_FAILURE,
3457 CONNMAN_IPCONFIG_TYPE_IPV6);
3459 if (autoconnect == TRUE && service->userconnect == FALSE)
3460 __connman_service_auto_connect();
3465 static void set_reconnect_state(struct connman_service *service,
3466 connman_bool_t reconnect)
3468 struct connman_device *device;
3470 if (service->network == NULL)
3473 device = connman_network_get_device(service->network);
3477 __connman_device_set_reconnect(device, reconnect);
3480 static connman_bool_t get_reconnect_state(struct connman_service *service)
3482 struct connman_device *device;
3484 if (service->network == NULL)
3487 device = connman_network_get_device(service->network);
3491 return __connman_device_get_reconnect(device);
3494 static DBusMessage *connect_service(DBusConnection *conn,
3495 DBusMessage *msg, void *user_data)
3497 struct connman_service *service = user_data;
3498 GSequenceIter *iter;
3501 DBG("service %p", service);
3503 if (service->pending != NULL)
3504 return __connman_error_in_progress(msg);
3506 iter = g_sequence_get_begin_iter(service_list);
3508 while (g_sequence_iter_is_end(iter) == FALSE) {
3509 struct connman_service *temp = g_sequence_get(iter);
3511 if (service->type == temp->type && is_connecting(temp) == TRUE)
3512 return __connman_error_in_progress(msg);
3514 iter = g_sequence_iter_next(iter);
3517 service->ignore = FALSE;
3519 service->userconnect = TRUE;
3521 service->pending = dbus_message_ref(msg);
3523 set_reconnect_state(service, FALSE);
3525 err = __connman_service_connect(service);
3527 if (service->pending == NULL)
3530 if (err != -EINPROGRESS) {
3531 dbus_message_unref(service->pending);
3532 service->pending = NULL;
3534 return __connman_error_failed(msg, -err);
3540 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3543 static DBusMessage *disconnect_service(DBusConnection *conn,
3544 DBusMessage *msg, void *user_data)
3546 struct connman_service *service = user_data;
3549 DBG("service %p", service);
3551 reply_pending(service, ECONNABORTED);
3553 service->ignore = TRUE;
3555 set_reconnect_state(service, FALSE);
3557 err = __connman_service_disconnect(service);
3559 if (err != -EINPROGRESS)
3560 return __connman_error_failed(msg, -err);
3565 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3568 gboolean __connman_service_remove(struct connman_service *service)
3570 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3573 if (service->immutable == TRUE || service->hidden == TRUE)
3576 if (service->favorite == FALSE && service->state !=
3577 CONNMAN_SERVICE_STATE_FAILURE)
3580 set_reconnect_state(service, FALSE);
3582 __connman_service_disconnect(service);
3584 g_free(service->passphrase);
3585 service->passphrase = NULL;
3587 g_free(service->agent_passphrase);
3588 service->agent_passphrase = NULL;
3590 g_free(service->identity);
3591 service->identity = NULL;
3593 g_free(service->agent_identity);
3594 service->agent_identity = NULL;
3596 g_free(service->eap);
3597 service->eap = NULL;
3601 __connman_service_set_favorite(service, FALSE);
3603 service_save(service);
3608 static DBusMessage *remove_service(DBusConnection *conn,
3609 DBusMessage *msg, void *user_data)
3611 struct connman_service *service = user_data;
3613 DBG("service %p", service);
3615 if (__connman_service_remove(service) == FALSE)
3616 return __connman_error_not_supported(msg);
3618 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3621 static gboolean check_suitable_state(enum connman_service_state a,
3622 enum connman_service_state b)
3625 * Special check so that "ready" service can be moved before
3628 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3629 b == CONNMAN_SERVICE_STATE_READY) ||
3630 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3631 a == CONNMAN_SERVICE_STATE_READY))
3637 static void downgrade_state(struct connman_service *service)
3639 if (service == NULL)
3642 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3643 service->state_ipv6);
3645 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3646 __connman_service_ipconfig_indicate_state(service,
3647 CONNMAN_SERVICE_STATE_READY,
3648 CONNMAN_IPCONFIG_TYPE_IPV4);
3650 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3651 __connman_service_ipconfig_indicate_state(service,
3652 CONNMAN_SERVICE_STATE_READY,
3653 CONNMAN_IPCONFIG_TYPE_IPV6);
3656 static void apply_relevant_default_downgrade(struct connman_service *service)
3658 struct connman_service *def_service;
3660 def_service = __connman_service_get_default();
3661 if (def_service == NULL)
3664 if (def_service == service &&
3665 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3666 def_service->state = CONNMAN_SERVICE_STATE_READY;
3669 static void switch_default_service(struct connman_service *default_service,
3670 struct connman_service *downgrade_service)
3672 GSequenceIter *src, *dst;
3674 apply_relevant_default_downgrade(default_service);
3675 src = g_hash_table_lookup(service_hash, downgrade_service->identifier);
3676 dst = g_hash_table_lookup(service_hash, default_service->identifier);
3677 g_sequence_move(src, dst);
3678 downgrade_state(downgrade_service);
3681 static DBusMessage *move_service(DBusConnection *conn,
3682 DBusMessage *msg, void *user_data,
3685 struct connman_service *service = user_data;
3686 struct connman_service *target;
3688 enum connman_ipconfig_method target4, target6;
3689 enum connman_ipconfig_method service4, service6;
3691 DBG("service %p", service);
3693 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3696 if (service->favorite == FALSE)
3697 return __connman_error_not_supported(msg);
3699 target = find_service(path);
3700 if (target == NULL || target->favorite == FALSE || target == service)
3701 return __connman_error_invalid_service(msg);
3703 if (target->type == CONNMAN_SERVICE_TYPE_VPN) {
3705 * We only allow VPN route splitting if there are
3706 * routes defined for a given VPN.
3708 if (__connman_provider_check_routes(target->provider)
3710 connman_info("Cannot move service. "
3711 "No routes defined for provider %s",
3712 __connman_provider_get_ident(target->provider));
3713 return __connman_error_invalid_service(msg);
3716 target->do_split_routing = TRUE;
3718 target->do_split_routing = FALSE;
3720 service->do_split_routing = FALSE;
3722 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3723 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3724 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3725 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3727 DBG("target %s method %d/%d state %d/%d split %d", target->identifier,
3728 target4, target6, target->state_ipv4, target->state_ipv6,
3729 target->do_split_routing);
3731 DBG("service %s method %d/%d state %d/%d", service->identifier,
3733 service->state_ipv4, service->state_ipv6);
3736 * If method is OFF, then we do not need to check the corresponding
3739 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3740 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3741 if (check_suitable_state(target->state_ipv6,
3742 service->state_ipv6) == FALSE)
3743 return __connman_error_invalid_service(msg);
3747 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3748 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3749 if (check_suitable_state(target->state_ipv4,
3750 service->state_ipv4) == FALSE)
3751 return __connman_error_invalid_service(msg);
3755 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3756 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3757 if (check_suitable_state(target->state_ipv6,
3758 service->state_ipv6) == FALSE)
3759 return __connman_error_invalid_service(msg);
3763 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3764 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3765 if (check_suitable_state(target->state_ipv4,
3766 service->state_ipv4) == FALSE)
3767 return __connman_error_invalid_service(msg);
3771 g_get_current_time(&service->modified);
3772 service_save(service);
3773 service_save(target);
3776 * If the service which goes down is the default service and is
3777 * online, we downgrade directly its state to ready so:
3778 * the service which goes up, needs to recompute its state which
3779 * is triggered via downgrading it - if relevant - to state ready.
3782 switch_default_service(target, service);
3784 switch_default_service(service, target);
3786 __connman_connection_update_gateway();
3788 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3791 static DBusMessage *move_before(DBusConnection *conn,
3792 DBusMessage *msg, void *user_data)
3794 return move_service(conn, msg, user_data, TRUE);
3797 static DBusMessage *move_after(DBusConnection *conn,
3798 DBusMessage *msg, void *user_data)
3800 return move_service(conn, msg, user_data, FALSE);
3803 static DBusMessage *reset_counters(DBusConnection *conn,
3804 DBusMessage *msg, void *user_data)
3806 struct connman_service *service = user_data;
3808 reset_stats(service);
3810 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3813 static struct _services_notify {
3819 static void service_append_added_foreach(gpointer data, gpointer user_data)
3821 struct connman_service *service = data;
3822 DBusMessageIter *iter = user_data;
3824 if (service == NULL || service->path == NULL) {
3825 DBG("service %p or path is NULL", service);
3829 if (g_hash_table_lookup(services_notify->add, service->path) != NULL) {
3830 DBG("new %s", service->path);
3832 append_struct(service, iter);
3833 g_hash_table_remove(services_notify->add, service->path);
3835 DBG("changed %s", service->path);
3837 append_struct_service(iter, NULL, service);
3841 static void service_append_ordered(DBusMessageIter *iter, void *user_data)
3843 if (service_list != NULL)
3844 g_sequence_foreach(service_list,
3845 service_append_added_foreach, iter);
3848 static void append_removed(gpointer key, gpointer value, gpointer user_data)
3850 char *objpath = key;
3851 DBusMessageIter *iter = user_data;
3853 DBG("removed %s", objpath);
3854 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath);
3857 static gboolean service_send_changed(gpointer data)
3859 DBusMessage *signal;
3860 DBusMessageIter iter, array;
3864 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
3865 CONNMAN_MANAGER_INTERFACE, "ServicesChanged");
3869 __connman_dbus_append_objpath_dict_array(signal,
3870 service_append_ordered, NULL);
3872 dbus_message_iter_init_append(signal, &iter);
3873 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
3874 DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
3876 g_hash_table_foreach(services_notify->remove, append_removed, &array);
3878 dbus_message_iter_close_container(&iter, &array);
3880 dbus_connection_send(connection, signal, NULL);
3881 dbus_message_unref(signal);
3883 g_hash_table_remove_all(services_notify->remove);
3884 g_hash_table_remove_all(services_notify->add);
3886 services_notify->id = 0;
3890 static void service_schedule_changed(void)
3892 if (services_notify->id != 0)
3895 services_notify->id = g_timeout_add(100, service_send_changed, NULL);
3898 static void service_schedule_added(struct connman_service *service)
3900 DBG("service %p", service);
3902 g_hash_table_remove(services_notify->remove, service->path);
3903 g_hash_table_replace(services_notify->add, service->path, service);
3905 service_schedule_changed();
3908 static void service_schedule_removed(struct connman_service *service)
3910 DBG("service %p %s", service, service->path);
3912 if (service == NULL || service->path == NULL) {
3913 DBG("service %p or path is NULL", service);
3917 g_hash_table_remove(services_notify->add, service->path);
3918 g_hash_table_replace(services_notify->remove, g_strdup(service->path),
3921 service_schedule_changed();
3924 static const GDBusMethodTable service_methods[] = {
3925 { GDBUS_DEPRECATED_METHOD("GetProperties",
3926 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
3928 { GDBUS_METHOD("SetProperty",
3929 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
3930 NULL, set_property) },
3931 { GDBUS_METHOD("ClearProperty",
3932 GDBUS_ARGS({ "name", "s" }), NULL,
3934 { GDBUS_ASYNC_METHOD("Connect", NULL, NULL,
3936 { GDBUS_METHOD("Disconnect", NULL, NULL,
3937 disconnect_service) },
3938 { GDBUS_METHOD("Remove", NULL, NULL, remove_service) },
3939 { GDBUS_METHOD("MoveBefore",
3940 GDBUS_ARGS({ "service", "o" }), NULL,
3942 { GDBUS_METHOD("MoveAfter",
3943 GDBUS_ARGS({ "service", "o" }), NULL,
3945 { GDBUS_METHOD("ResetCounters", NULL, NULL, reset_counters) },
3949 static const GDBusSignalTable service_signals[] = {
3950 { GDBUS_SIGNAL("PropertyChanged",
3951 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
3955 static void service_free(gpointer user_data)
3957 struct connman_service *service = user_data;
3958 char *path = service->path;
3960 DBG("service %p", service);
3962 reply_pending(service, ENOENT);
3964 g_hash_table_remove(service_hash, service->identifier);
3966 __connman_notifier_service_remove(service);
3967 service_schedule_removed(service);
3969 stats_stop(service);
3971 service->path = NULL;
3974 __connman_connection_update_gateway();
3976 g_dbus_unregister_interface(connection, path,
3977 CONNMAN_SERVICE_INTERFACE);
3981 g_hash_table_destroy(service->counter_table);
3983 if (service->network != NULL) {
3984 __connman_network_disconnect(service->network);
3985 connman_network_unref(service->network);
3986 service->network = NULL;
3989 if (service->provider != NULL)
3990 connman_provider_unref(service->provider);
3992 if (service->ipconfig_ipv4 != NULL) {
3993 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3994 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3995 __connman_ipconfig_unref(service->ipconfig_ipv4);
3996 service->ipconfig_ipv4 = NULL;
3999 if (service->ipconfig_ipv6 != NULL) {
4000 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
4001 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
4002 __connman_ipconfig_unref(service->ipconfig_ipv6);
4003 service->ipconfig_ipv6 = NULL;
4006 g_strfreev(service->timeservers);
4007 g_strfreev(service->timeservers_config);
4008 g_strfreev(service->nameservers);
4009 g_strfreev(service->nameservers_config);
4010 g_strfreev(service->nameservers_auto);
4011 g_strfreev(service->domains);
4012 g_strfreev(service->proxies);
4013 g_strfreev(service->excludes);
4015 g_free(service->domainname);
4016 g_free(service->pac);
4017 g_free(service->name);
4018 g_free(service->passphrase);
4019 g_free(service->agent_passphrase);
4020 g_free(service->identifier);
4021 g_free(service->eap);
4022 g_free(service->identity);
4023 g_free(service->agent_identity);
4024 g_free(service->ca_cert_file);
4025 g_free(service->client_cert_file);
4026 g_free(service->private_key_file);
4027 g_free(service->private_key_passphrase);
4028 g_free(service->phase2);
4029 g_free(service->config_file);
4030 g_free(service->config_entry);
4032 if (service->stats.timer != NULL)
4033 g_timer_destroy(service->stats.timer);
4034 if (service->stats_roaming.timer != NULL)
4035 g_timer_destroy(service->stats_roaming.timer);
4040 static void stats_init(struct connman_service *service)
4043 service->stats.valid = FALSE;
4044 service->stats.enabled = FALSE;
4045 service->stats.timer = g_timer_new();
4048 service->stats_roaming.valid = FALSE;
4049 service->stats_roaming.enabled = FALSE;
4050 service->stats_roaming.timer = g_timer_new();
4053 static void service_initialize(struct connman_service *service)
4055 DBG("service %p", service);
4057 service->refcount = 1;
4058 service->session_usage_count = 0;
4060 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
4061 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
4063 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
4064 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
4065 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
4067 service->favorite = FALSE;
4068 service->immutable = FALSE;
4069 service->hidden = FALSE;
4071 service->ignore = FALSE;
4073 service->userconnect = FALSE;
4077 stats_init(service);
4079 service->provider = NULL;
4081 service->wps = FALSE;
4085 * connman_service_create:
4087 * Allocate a new service.
4089 * Returns: a newly-allocated #connman_service structure
4091 struct connman_service *connman_service_create(void)
4094 struct connman_stats_counter *counters;
4095 const char *counter;
4097 struct connman_service *service;
4099 service = g_try_new0(struct connman_service, 1);
4100 if (service == NULL)
4103 DBG("service %p", service);
4105 service->counter_table = g_hash_table_new_full(g_str_hash,
4106 g_str_equal, NULL, g_free);
4108 for (list = counter_list; list; list = list->next) {
4109 counter = list->data;
4111 counters = g_try_new0(struct connman_stats_counter, 1);
4112 if (counters == NULL) {
4113 g_hash_table_destroy(service->counter_table);
4118 counters->append_all = TRUE;
4120 g_hash_table_replace(service->counter_table, (gpointer)counter,
4124 service_initialize(service);
4130 * connman_service_ref:
4131 * @service: service structure
4133 * Increase reference counter of service
4135 struct connman_service *
4136 connman_service_ref_debug(struct connman_service *service,
4137 const char *file, int line, const char *caller)
4139 DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
4140 file, line, caller);
4142 __sync_fetch_and_add(&service->refcount, 1);
4148 * connman_service_unref:
4149 * @service: service structure
4151 * Decrease reference counter of service and release service if no
4154 void connman_service_unref_debug(struct connman_service *service,
4155 const char *file, int line, const char *caller)
4157 GSequenceIter *iter;
4159 DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
4160 file, line, caller);
4162 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
4165 iter = g_hash_table_lookup(service_hash, service->identifier);
4167 reply_pending(service, ECONNABORTED);
4169 __connman_service_disconnect(service);
4171 g_sequence_remove(iter);
4173 service_free(service);
4177 static gint service_compare(gconstpointer a, gconstpointer b,
4180 struct connman_service *service_a = (void *) a;
4181 struct connman_service *service_b = (void *) b;
4182 enum connman_service_state state_a, state_b;
4184 state_a = service_a->state;
4185 state_b = service_b->state;
4187 if (state_a != state_b) {
4188 gboolean a_connected = is_connected(service_a);
4189 gboolean b_connected = is_connected(service_b);
4191 if (a_connected == TRUE && b_connected == TRUE) {
4192 /* We prefer online over ready state */
4193 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
4196 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
4200 if (a_connected == TRUE)
4202 if (b_connected == TRUE)
4205 if (is_connecting(service_a) == TRUE)
4207 if (is_connecting(service_b) == TRUE)
4211 if (service_a->order > service_b->order)
4214 if (service_a->order < service_b->order)
4217 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
4220 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
4223 if (service_a->type != service_b->type) {
4224 switch (service_a->type) {
4225 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4226 case CONNMAN_SERVICE_TYPE_SYSTEM:
4227 case CONNMAN_SERVICE_TYPE_ETHERNET:
4228 case CONNMAN_SERVICE_TYPE_GPS:
4229 case CONNMAN_SERVICE_TYPE_VPN:
4230 case CONNMAN_SERVICE_TYPE_GADGET:
4232 case CONNMAN_SERVICE_TYPE_WIFI:
4234 case CONNMAN_SERVICE_TYPE_WIMAX:
4235 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4236 case CONNMAN_SERVICE_TYPE_CELLULAR:
4241 return (gint) service_b->strength - (gint) service_a->strength;
4245 * connman_service_get_type:
4246 * @service: service structure
4248 * Get the type of service
4250 enum connman_service_type connman_service_get_type(struct connman_service *service)
4252 if (service == NULL)
4253 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4255 return service->type;
4259 * connman_service_get_interface:
4260 * @service: service structure
4262 * Get network interface of service
4264 char *connman_service_get_interface(struct connman_service *service)
4268 if (service == NULL)
4271 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
4272 if (service->ipconfig_ipv4)
4273 index = __connman_ipconfig_get_index(
4274 service->ipconfig_ipv4);
4275 else if (service->ipconfig_ipv6)
4276 index = __connman_ipconfig_get_index(
4277 service->ipconfig_ipv6);
4281 return connman_inet_ifname(index);
4284 if (service->network == NULL)
4287 index = connman_network_get_index(service->network);
4289 return connman_inet_ifname(index);
4293 * connman_service_get_network:
4294 * @service: service structure
4296 * Get the service network
4298 struct connman_network *
4299 __connman_service_get_network(struct connman_service *service)
4301 if (service == NULL)
4304 return service->network;
4307 struct connman_ipconfig *
4308 __connman_service_get_ip4config(struct connman_service *service)
4310 if (service == NULL)
4313 return service->ipconfig_ipv4;
4316 struct connman_ipconfig *
4317 __connman_service_get_ip6config(struct connman_service *service)
4319 if (service == NULL)
4322 return service->ipconfig_ipv6;
4325 struct connman_ipconfig *
4326 __connman_service_get_ipconfig(struct connman_service *service, int family)
4328 if (family == AF_INET)
4329 return __connman_service_get_ip4config(service);
4330 else if (family == AF_INET6)
4331 return __connman_service_get_ip6config(service);
4337 connman_bool_t __connman_service_is_connected_state(struct connman_service *service,
4338 enum connman_ipconfig_type type)
4340 if (service == NULL)
4344 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
4346 case CONNMAN_IPCONFIG_TYPE_IPV4:
4347 return is_connected_state(service, service->state_ipv4);
4348 case CONNMAN_IPCONFIG_TYPE_IPV6:
4349 return is_connected_state(service, service->state_ipv6);
4354 enum connman_service_security __connman_service_get_security(struct connman_service *service)
4356 if (service == NULL)
4357 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4359 return service->security;
4362 const char *__connman_service_get_phase2(struct connman_service *service)
4364 if (service == NULL)
4367 return service->phase2;
4370 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
4372 if (service == NULL)
4375 return service->wps;
4379 * __connman_service_set_favorite:
4380 * @service: service structure
4381 * @favorite: favorite value
4383 * Change the favorite setting of service
4385 int __connman_service_set_favorite(struct connman_service *service,
4386 connman_bool_t favorite)
4388 GSequenceIter *iter;
4390 if (service->hidden == TRUE)
4392 iter = g_hash_table_lookup(service_hash, service->identifier);
4396 if (service->favorite == favorite)
4399 service->favorite = favorite;
4400 service->order = __connman_service_get_order(service);
4402 favorite_changed(service);
4404 if (g_sequence_get_length(service_list) > 1) {
4405 g_sequence_sort_changed(iter, service_compare, NULL);
4406 service_schedule_changed();
4409 __connman_connection_update_gateway();
4414 int __connman_service_set_immutable(struct connman_service *service,
4415 connman_bool_t immutable)
4417 if (service->hidden == TRUE)
4419 service->immutable = immutable;
4421 immutable_changed(service);
4426 void __connman_service_set_string(struct connman_service *service,
4427 const char *key, const char *value)
4429 if (service->hidden == TRUE)
4431 if (g_str_equal(key, "EAP") == TRUE) {
4432 g_free(service->eap);
4433 service->eap = g_strdup(value);
4434 } else if (g_str_equal(key, "Identity") == TRUE) {
4435 g_free(service->identity);
4436 service->identity = g_strdup(value);
4437 } else if (g_str_equal(key, "CACertFile") == TRUE) {
4438 g_free(service->ca_cert_file);
4439 service->ca_cert_file = g_strdup(value);
4440 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
4441 g_free(service->client_cert_file);
4442 service->client_cert_file = g_strdup(value);
4443 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
4444 g_free(service->private_key_file);
4445 service->private_key_file = g_strdup(value);
4446 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
4447 g_free(service->private_key_passphrase);
4448 service->private_key_passphrase = g_strdup(value);
4449 } else if (g_str_equal(key, "Phase2") == TRUE) {
4450 g_free(service->phase2);
4451 service->phase2 = g_strdup(value);
4452 } else if (g_str_equal(key, "Passphrase") == TRUE) {
4453 g_free(service->passphrase);
4454 service->passphrase = g_strdup(value);
4458 void __connman_service_set_userconnect(struct connman_service *service,
4459 connman_bool_t userconnect)
4461 if (service != NULL)
4462 service->userconnect = userconnect;
4465 static void service_complete(struct connman_service *service)
4467 reply_pending(service, EIO);
4469 if (service->userconnect == FALSE)
4470 __connman_service_auto_connect();
4472 g_get_current_time(&service->modified);
4473 service_save(service);
4476 static void report_error_cb(struct connman_service *service,
4477 gboolean retry, void *user_data)
4480 __connman_service_connect(service);
4482 /* It is not relevant to stay on Failure state
4483 * when failing is due to wrong user input */
4484 service->state = CONNMAN_SERVICE_STATE_IDLE;
4486 service_complete(service);
4487 __connman_connection_update_gateway();
4491 int __connman_service_add_passphrase(struct connman_service *service,
4492 const gchar *passphrase)
4496 switch (service->security) {
4497 case CONNMAN_SERVICE_SECURITY_WEP:
4498 case CONNMAN_SERVICE_SECURITY_PSK:
4499 err = __connman_service_set_passphrase(service, passphrase);
4501 case CONNMAN_SERVICE_SECURITY_8021X:
4502 __connman_service_set_agent_passphrase(service,
4505 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4506 case CONNMAN_SERVICE_SECURITY_NONE:
4507 case CONNMAN_SERVICE_SECURITY_WPA:
4508 case CONNMAN_SERVICE_SECURITY_RSN:
4509 DBG("service security '%s' (%d) not handled",
4510 security2string(service->security),
4518 static int check_wpspin(struct connman_service *service, const char *wpspin)
4526 length = strlen(wpspin);
4528 /* If 0, it will mean user wants to use PBC method */
4530 connman_network_set_string(service->network,
4531 "WiFi.PinWPS", NULL);
4535 /* A WPS PIN is always 8 chars length,
4536 * its content is in digit representation.
4541 for (i = 0; i < 8; i++)
4542 if (!isdigit((unsigned char) wpspin[i]))
4545 connman_network_set_string(service->network, "WiFi.PinWPS", wpspin);
4550 static void request_input_cb (struct connman_service *service,
4551 connman_bool_t values_received,
4552 const char *name, int name_len,
4553 const char *identity, const char *passphrase,
4554 gboolean wps, const char *wpspin,
4555 const char *error, void *user_data)
4557 struct connman_device *device;
4560 DBG ("RequestInput return, %p", service);
4562 if (error != NULL) {
4563 DBG("error: %s", error);
4565 if (g_strcmp0(error,
4566 "net.connman.Agent.Error.Canceled") == 0) {
4572 if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
4573 device = connman_network_get_device(service->network);
4574 __connman_device_request_hidden_scan(device,
4576 identity, passphrase);
4579 if (values_received == FALSE || service->hidden == TRUE) {
4584 if (wps == TRUE && service->network != NULL) {
4585 err = check_wpspin(service, wpspin);
4589 connman_network_set_bool(service->network, "WiFi.UseWPS", wps);
4592 if (identity != NULL)
4593 __connman_service_set_agent_identity(service, identity);
4595 if (passphrase != NULL)
4596 err = __connman_service_add_passphrase(service, passphrase);
4600 __connman_service_connect(service);
4602 /* Never cache agent provided credentials */
4603 __connman_service_set_agent_identity(service, NULL);
4604 __connman_service_set_agent_passphrase(service, NULL);
4605 } else if (err == -ENOKEY) {
4606 __connman_service_indicate_error(service,
4607 CONNMAN_SERVICE_ERROR_INVALID_KEY);
4608 __connman_agent_report_error(service,
4609 error2string(service->error),
4610 report_error_cb, NULL);
4612 /* It is not relevant to stay on Failure state
4613 * when failing is due to wrong user input */
4614 service->state = CONNMAN_SERVICE_STATE_IDLE;
4616 service_complete(service);
4617 __connman_connection_update_gateway();
4621 static void downgrade_connected_services(void)
4623 struct connman_service *up_service;
4624 GSequenceIter *iter;
4626 iter = g_sequence_get_begin_iter(service_list);
4627 while (g_sequence_iter_is_end(iter) == FALSE) {
4628 up_service = g_sequence_get(iter);
4630 if (is_connected(up_service) == FALSE) {
4631 iter = g_sequence_iter_next(iter);
4635 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4638 downgrade_state(up_service);
4640 iter = g_sequence_iter_next(iter);
4644 static int service_update_preferred_order(struct connman_service *default_service,
4645 struct connman_service *new_service,
4646 enum connman_service_state new_state)
4648 unsigned int *tech_array;
4651 if (default_service == NULL || default_service == new_service ||
4652 default_service->state != new_state )
4655 tech_array = connman_setting_get_uint_list("PreferredTechnologies");
4656 if (tech_array != NULL) {
4658 for (i = 0; tech_array[i] != 0; i += 1) {
4659 if (default_service->type == tech_array[i])
4662 if (new_service->type == tech_array[i]) {
4663 switch_default_service(new_service,
4674 static int service_indicate_state(struct connman_service *service)
4676 enum connman_service_state old_state, new_state;
4677 struct connman_service *def_service;
4679 GSequenceIter *iter;
4681 if (service == NULL)
4684 old_state = service->state;
4685 new_state = combine_state(service->state_ipv4, service->state_ipv6);
4687 DBG("service %p old %s - new %s/%s => %s",
4689 state2string(old_state),
4690 state2string(service->state_ipv4),
4691 state2string(service->state_ipv6),
4692 state2string(new_state));
4694 if (old_state == new_state)
4697 def_service = __connman_service_get_default();
4699 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4700 result = service_update_preferred_order(def_service,
4701 service, new_state);
4702 if (result == -EALREADY)
4704 if (result == -EAGAIN)
4705 __connman_service_auto_connect();
4708 if (old_state == CONNMAN_SERVICE_STATE_ONLINE)
4709 __connman_notifier_leave_online(service->type);
4711 service->state = new_state;
4712 state_changed(service);
4714 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4715 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4716 reply_pending(service, ECONNABORTED);
4718 __connman_service_disconnect(service);
4721 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4722 if (service->new_service == FALSE &&
4723 __connman_stats_service_register(service) == 0) {
4725 * For new services the statistics are updated after
4726 * we have successfully connected.
4728 __connman_stats_get(service, FALSE,
4729 &service->stats.data);
4730 __connman_stats_get(service, TRUE,
4731 &service->stats_roaming.data);
4735 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4736 connman_bool_t reconnect;
4738 reconnect = get_reconnect_state(service);
4739 if (reconnect == TRUE)
4740 __connman_service_auto_connect();
4743 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4744 enum connman_ipconfig_method method;
4746 if (service->new_service == TRUE &&
4747 __connman_stats_service_register(service) == 0) {
4749 * This is normally done after configuring state
4750 * but for new service do this after we have connected
4753 __connman_stats_get(service, FALSE,
4754 &service->stats.data);
4755 __connman_stats_get(service, TRUE,
4756 &service->stats_roaming.data);
4759 service->new_service = FALSE;
4761 service_update_preferred_order(def_service, service, new_state);
4763 set_reconnect_state(service, TRUE);
4765 __connman_service_set_favorite(service, TRUE);
4767 reply_pending(service, 0);
4769 service->userconnect = FALSE;
4771 g_get_current_time(&service->modified);
4772 service_save(service);
4774 update_nameservers(service);
4775 dns_changed(service);
4776 domain_changed(service);
4778 __connman_notifier_connect(service->type);
4780 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4781 connman_network_get_bool(service->network,
4782 "WiFi.UseWPS") == TRUE) {
4785 pass = connman_network_get_string(service->network,
4788 __connman_service_set_passphrase(service, pass);
4790 connman_network_set_bool(service->network,
4791 "WiFi.UseWPS", FALSE);
4796 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4797 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4798 __connman_ipconfig_disable_ipv6(
4799 service->ipconfig_ipv6);
4801 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4802 def_service = __connman_service_get_default();
4804 if (__connman_notifier_is_connected() == FALSE &&
4805 def_service != NULL &&
4806 def_service->provider != NULL)
4807 __connman_provider_disconnect(def_service->provider);
4811 __connman_wispr_stop(service);
4813 __connman_wpad_stop(service);
4815 update_nameservers(service);
4816 dns_changed(service);
4817 domain_changed(service);
4819 __connman_notifier_disconnect(service->type);
4822 * Previous services which are connected and which states
4823 * are set to online should reset relevantly ipconfig_state
4824 * to ready so wispr/portal will be rerun on those
4826 downgrade_connected_services();
4829 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4830 if (service->userconnect == TRUE &&
4831 __connman_agent_report_error(service,
4832 error2string(service->error),
4833 report_error_cb, NULL) == -EINPROGRESS)
4835 service_complete(service);
4837 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4839 iter = g_hash_table_lookup(service_hash, service->identifier);
4840 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
4841 g_sequence_sort_changed(iter, service_compare, NULL);
4842 service_schedule_changed();
4845 __connman_connection_update_gateway();
4847 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4848 __connman_notifier_enter_online(service->type);
4855 int __connman_service_indicate_error(struct connman_service *service,
4856 enum connman_service_error error)
4858 DBG("service %p error %d", service, error);
4860 if (service == NULL)
4863 service->error = error;
4865 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4866 __connman_service_set_passphrase(service, NULL);
4868 __connman_service_ipconfig_indicate_state(service,
4869 CONNMAN_SERVICE_STATE_FAILURE,
4870 CONNMAN_IPCONFIG_TYPE_IPV4);
4871 __connman_service_ipconfig_indicate_state(service,
4872 CONNMAN_SERVICE_STATE_FAILURE,
4873 CONNMAN_IPCONFIG_TYPE_IPV6);
4877 int __connman_service_clear_error(struct connman_service *service)
4879 DBG("service %p", service);
4881 if (service == NULL)
4884 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4887 service->state_ipv4 = service->state_ipv6 =
4888 CONNMAN_SERVICE_STATE_UNKNOWN;
4889 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4891 if (service->favorite == TRUE)
4892 set_reconnect_state(service, TRUE);
4894 __connman_service_ipconfig_indicate_state(service,
4895 CONNMAN_SERVICE_STATE_IDLE,
4896 CONNMAN_IPCONFIG_TYPE_IPV6);
4899 * Toggling the IPv6 state to IDLE could trigger the auto connect
4900 * machinery and consequently the IPv4 state.
4902 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4903 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4906 return __connman_service_ipconfig_indicate_state(service,
4907 CONNMAN_SERVICE_STATE_IDLE,
4908 CONNMAN_IPCONFIG_TYPE_IPV4);
4911 int __connman_service_indicate_default(struct connman_service *service)
4913 DBG("service %p", service);
4920 enum connman_service_state __connman_service_ipconfig_get_state(
4921 struct connman_service *service,
4922 enum connman_ipconfig_type type)
4924 if (service == NULL)
4925 return CONNMAN_SERVICE_STATE_UNKNOWN;
4927 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4928 return service->state_ipv4;
4930 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4931 return service->state_ipv6;
4933 return CONNMAN_SERVICE_STATE_UNKNOWN;
4936 static void check_proxy_setup(struct connman_service *service)
4939 * We start WPAD if we haven't got a PAC URL from DHCP and
4940 * if our proxy manual configuration is either empty or set
4941 * to AUTO with an empty URL.
4944 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4947 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4948 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4949 service->pac != NULL))
4952 if (__connman_wpad_start(service) < 0) {
4953 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4954 __connman_notifier_proxy_changed(service);
4961 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4965 * How many networks are connected at the same time. If more than 1,
4966 * then set the rp_filter setting properly (loose mode routing) so that network
4967 * connectivity works ok. This is only done for IPv4 networks as IPv6
4968 * does not have rp_filter knob.
4970 static int connected_networks_count;
4971 static int original_rp_filter;
4973 static void service_rp_filter(struct connman_service *service,
4976 enum connman_ipconfig_method method;
4978 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4981 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4982 case CONNMAN_IPCONFIG_METHOD_OFF:
4983 case CONNMAN_IPCONFIG_METHOD_AUTO:
4985 case CONNMAN_IPCONFIG_METHOD_FIXED:
4986 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4987 case CONNMAN_IPCONFIG_METHOD_DHCP:
4991 if (connected == TRUE) {
4992 if (connected_networks_count == 1) {
4994 filter_value = __connman_ipconfig_set_rp_filter();
4995 if (filter_value < 0)
4998 original_rp_filter = filter_value;
5000 connected_networks_count++;
5003 if (connected_networks_count == 2)
5004 __connman_ipconfig_unset_rp_filter(original_rp_filter);
5006 connected_networks_count--;
5007 if (connected_networks_count < 0)
5008 connected_networks_count = 0;
5011 DBG("%s %s ipconfig %p method %d count %d filter %d",
5012 connected ? "connected" : "disconnected", service->identifier,
5013 service->ipconfig_ipv4, method,
5014 connected_networks_count, original_rp_filter);
5017 static gboolean redo_wispr(gpointer user_data)
5019 struct connman_service *service = user_data;
5023 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
5028 int __connman_service_online_check_failed(struct connman_service *service,
5029 enum connman_ipconfig_type type)
5031 DBG("service %p type %d count %d", service, type,
5032 service->online_check_count);
5034 /* currently we only retry IPv6 stuff */
5035 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 ||
5036 service->online_check_count != 1) {
5037 connman_warn("Online check failed for %p %s", service,
5042 service->online_check_count = 0;
5045 * We set the timeout to 1 sec so that we have a chance to get
5046 * necessary IPv6 router advertisement messages that might have
5049 g_timeout_add_seconds(1, redo_wispr, service);
5054 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
5055 enum connman_service_state new_state,
5056 enum connman_ipconfig_type type)
5058 struct connman_ipconfig *ipconfig = NULL;
5059 enum connman_service_state old_state;
5062 if (service == NULL)
5065 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5066 old_state = service->state_ipv4;
5067 ipconfig = service->ipconfig_ipv4;
5068 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
5069 old_state = service->state_ipv6;
5070 ipconfig = service->ipconfig_ipv6;
5073 if (ipconfig == NULL)
5077 if (old_state == new_state)
5080 DBG("service %p (%s) state %d (%s) type %d (%s)",
5081 service, service ? service->identifier : NULL,
5082 new_state, state2string(new_state),
5083 type, __connman_ipconfig_type2string(type));
5085 switch (new_state) {
5086 case CONNMAN_SERVICE_STATE_UNKNOWN:
5087 case CONNMAN_SERVICE_STATE_IDLE:
5088 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
5091 case CONNMAN_SERVICE_STATE_ASSOCIATION:
5093 case CONNMAN_SERVICE_STATE_CONFIGURATION:
5094 __connman_ipconfig_enable(ipconfig);
5096 case CONNMAN_SERVICE_STATE_READY:
5097 update_nameservers(service);
5099 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5100 check_proxy_setup(service);
5101 service_rp_filter(service, TRUE);
5103 service->online_check_count = 1;
5104 __connman_wispr_start(service, type);
5107 case CONNMAN_SERVICE_STATE_ONLINE:
5109 case CONNMAN_SERVICE_STATE_DISCONNECT:
5110 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
5113 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
5114 service_rp_filter(service, FALSE);
5117 case CONNMAN_SERVICE_STATE_FAILURE:
5121 /* We keep that state */
5122 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
5123 service->state_ipv4 = new_state;
5124 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
5125 service->state_ipv6 = new_state;
5127 ret = service_indicate_state(service);
5130 * If the ipconfig method is OFF, then we set the state to IDLE
5131 * so that it will not affect the combined state in the future.
5133 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5134 enum connman_ipconfig_method method;
5135 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
5136 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
5137 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
5138 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
5139 ret = service_indicate_state(service);
5142 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
5143 enum connman_ipconfig_method method;
5144 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
5145 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
5146 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
5147 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5148 ret = service_indicate_state(service);
5155 static connman_bool_t prepare_network(struct connman_service *service)
5157 enum connman_network_type type;
5158 unsigned int ssid_len;
5160 type = connman_network_get_type(service->network);
5163 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5164 case CONNMAN_NETWORK_TYPE_VENDOR:
5166 case CONNMAN_NETWORK_TYPE_WIFI:
5167 if (connman_network_get_blob(service->network, "WiFi.SSID",
5171 if (service->passphrase != NULL)
5172 connman_network_set_string(service->network,
5173 "WiFi.Passphrase", service->passphrase);
5175 case CONNMAN_NETWORK_TYPE_ETHERNET:
5176 case CONNMAN_NETWORK_TYPE_WIMAX:
5177 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5178 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5179 case CONNMAN_NETWORK_TYPE_CELLULAR:
5186 static void prepare_8021x(struct connman_service *service)
5188 if (service->eap != NULL)
5189 connman_network_set_string(service->network, "WiFi.EAP",
5192 if (service->identity != NULL)
5193 connman_network_set_string(service->network, "WiFi.Identity",
5196 if (service->ca_cert_file != NULL)
5197 connman_network_set_string(service->network, "WiFi.CACertFile",
5198 service->ca_cert_file);
5200 if (service->client_cert_file != NULL)
5201 connman_network_set_string(service->network,
5202 "WiFi.ClientCertFile",
5203 service->client_cert_file);
5205 if (service->private_key_file != NULL)
5206 connman_network_set_string(service->network,
5207 "WiFi.PrivateKeyFile",
5208 service->private_key_file);
5210 if (service->private_key_passphrase != NULL)
5211 connman_network_set_string(service->network,
5212 "WiFi.PrivateKeyPassphrase",
5213 service->private_key_passphrase);
5215 if (service->phase2 != NULL)
5216 connman_network_set_string(service->network, "WiFi.Phase2",
5220 static int service_connect(struct connman_service *service)
5224 if (service->hidden == TRUE)
5227 switch (service->type) {
5228 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5229 case CONNMAN_SERVICE_TYPE_SYSTEM:
5230 case CONNMAN_SERVICE_TYPE_GPS:
5231 case CONNMAN_SERVICE_TYPE_GADGET:
5233 case CONNMAN_SERVICE_TYPE_ETHERNET:
5234 case CONNMAN_SERVICE_TYPE_WIMAX:
5235 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5236 case CONNMAN_SERVICE_TYPE_CELLULAR:
5237 case CONNMAN_SERVICE_TYPE_VPN:
5239 case CONNMAN_SERVICE_TYPE_WIFI:
5240 switch (service->security) {
5241 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5242 case CONNMAN_SERVICE_SECURITY_NONE:
5244 case CONNMAN_SERVICE_SECURITY_WEP:
5245 case CONNMAN_SERVICE_SECURITY_PSK:
5246 case CONNMAN_SERVICE_SECURITY_WPA:
5247 case CONNMAN_SERVICE_SECURITY_RSN:
5248 if (service->passphrase == NULL) {
5249 if (service->network == NULL)
5252 if (service->wps == FALSE ||
5253 connman_network_get_bool(
5255 "WiFi.UseWPS") == FALSE)
5259 case CONNMAN_SERVICE_SECURITY_8021X:
5260 if (service->eap == NULL)
5264 * never request credentials if using EAP-TLS
5265 * (EAP-TLS networks need to be fully provisioned)
5267 if (g_str_equal(service->eap, "tls") == TRUE)
5271 * Return -ENOKEY if either identity or passphrase is
5272 * missing. Agent provided credentials can be used as
5273 * fallback if needed.
5275 if ((service->identity == NULL &&
5276 service->agent_identity == NULL) ||
5277 (service->passphrase == NULL &&
5278 service->agent_passphrase == NULL))
5286 if (service->network != NULL) {
5287 if (prepare_network(service) == FALSE)
5290 switch (service->security) {
5291 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5292 case CONNMAN_SERVICE_SECURITY_NONE:
5293 case CONNMAN_SERVICE_SECURITY_WEP:
5294 case CONNMAN_SERVICE_SECURITY_PSK:
5295 case CONNMAN_SERVICE_SECURITY_WPA:
5296 case CONNMAN_SERVICE_SECURITY_RSN:
5298 case CONNMAN_SERVICE_SECURITY_8021X:
5299 prepare_8021x(service);
5303 if (__connman_stats_service_register(service) == 0) {
5304 __connman_stats_get(service, FALSE,
5305 &service->stats.data);
5306 __connman_stats_get(service, TRUE,
5307 &service->stats_roaming.data);
5310 if (service->ipconfig_ipv4)
5311 __connman_ipconfig_enable(service->ipconfig_ipv4);
5312 if (service->ipconfig_ipv6)
5313 __connman_ipconfig_enable(service->ipconfig_ipv6);
5315 err = __connman_network_connect(service->network);
5316 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5317 service->provider != NULL)
5318 err = __connman_provider_connect(service->provider);
5323 if (err != -EINPROGRESS) {
5324 __connman_ipconfig_disable(service->ipconfig_ipv4);
5325 __connman_ipconfig_disable(service->ipconfig_ipv6);
5326 __connman_stats_service_unregister(service);
5334 int __connman_service_connect(struct connman_service *service)
5338 DBG("service %p state %s", service, state2string(service->state));
5340 if (is_connected(service) == TRUE)
5343 if (is_connecting(service) == TRUE)
5346 switch (service->type) {
5347 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5348 case CONNMAN_SERVICE_TYPE_SYSTEM:
5349 case CONNMAN_SERVICE_TYPE_GPS:
5350 case CONNMAN_SERVICE_TYPE_GADGET:
5353 err = service_connect(service);
5359 if (err == -EINPROGRESS) {
5360 if (service->timeout == 0)
5361 service->timeout = g_timeout_add_seconds(
5362 CONNECT_TIMEOUT, connect_timeout, service);
5364 return -EINPROGRESS;
5367 if (service->network != NULL)
5368 __connman_network_disconnect(service->network);
5369 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5370 service->provider != NULL)
5371 __connman_provider_disconnect(service->provider);
5373 if (service->userconnect == TRUE) {
5374 if (err == -ENOKEY || err == -EPERM) {
5375 return __connman_agent_request_passphrase_input(service,
5376 request_input_cb, NULL);
5378 reply_pending(service, -err);
5384 int __connman_service_disconnect(struct connman_service *service)
5388 DBG("service %p", service);
5390 if (service->network != NULL) {
5391 err = __connman_network_disconnect(service->network);
5392 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5393 service->provider != NULL)
5394 err = __connman_provider_disconnect(service->provider);
5398 if (err < 0 && err != -EINPROGRESS)
5401 __connman_6to4_remove(service->ipconfig_ipv4);
5403 if (service->ipconfig_ipv4)
5404 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
5407 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
5410 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
5411 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
5413 __connman_ipconfig_disable(service->ipconfig_ipv4);
5414 __connman_ipconfig_disable(service->ipconfig_ipv6);
5416 __connman_stats_service_unregister(service);
5421 int __connman_service_disconnect_all(void)
5423 GSequenceIter *iter;
5424 GSList *services = NULL, *list;
5428 iter = g_sequence_get_begin_iter(service_list);
5430 while (g_sequence_iter_is_end(iter) == FALSE) {
5431 struct connman_service *service = g_sequence_get(iter);
5433 services = g_slist_prepend(services, service);
5435 iter = g_sequence_iter_next(iter);
5438 for (list = services; list != NULL; list = list->next) {
5439 struct connman_service *service = list->data;
5441 service->ignore = TRUE;
5443 set_reconnect_state(service, FALSE);
5445 __connman_service_disconnect(service);
5455 * lookup_by_identifier:
5456 * @identifier: service identifier
5458 * Look up a service by identifier (reference count will not be increased)
5460 static struct connman_service *lookup_by_identifier(const char *identifier)
5462 GSequenceIter *iter;
5464 iter = g_hash_table_lookup(service_hash, identifier);
5466 return g_sequence_get(iter);
5471 static void provision_changed(gpointer value, gpointer user_data)
5473 struct connman_service *service = value;
5474 char *path = user_data;
5476 __connman_config_provision_service_ident(service, path);
5479 void __connman_service_provision_changed(const char *ident)
5481 g_sequence_foreach(service_list, provision_changed, (void *)ident);
5484 void __connman_service_set_config(struct connman_service *service,
5485 const char *file_id, const char *entry)
5487 if (service == NULL)
5490 g_free(service->config_file);
5491 service->config_file = g_strdup(file_id);
5493 g_free(service->config_entry);
5494 service->config_entry = g_strdup(entry);
5498 * __connman_service_get:
5499 * @identifier: service identifier
5501 * Look up a service by identifier or create a new one if not found
5503 static struct connman_service *service_get(const char *identifier)
5505 struct connman_service *service;
5506 GSequenceIter *iter;
5508 iter = g_hash_table_lookup(service_hash, identifier);
5510 service = g_sequence_get(iter);
5511 if (service != NULL)
5512 connman_service_ref(service);
5516 service = connman_service_create();
5517 if (service == NULL)
5520 DBG("service %p", service);
5522 service->identifier = g_strdup(identifier);
5524 iter = g_sequence_insert_sorted(service_list, service,
5525 service_compare, NULL);
5527 g_hash_table_insert(service_hash, service->identifier, iter);
5532 static int service_register(struct connman_service *service)
5534 GSequenceIter *iter;
5536 DBG("service %p", service);
5538 if (service->path != NULL)
5541 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
5542 service->identifier);
5544 DBG("path %s", service->path);
5546 __connman_config_provision_service(service);
5548 service_load(service);
5550 g_dbus_register_interface(connection, service->path,
5551 CONNMAN_SERVICE_INTERFACE,
5552 service_methods, service_signals,
5553 NULL, service, NULL);
5555 iter = g_hash_table_lookup(service_hash, service->identifier);
5556 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
5557 g_sequence_sort_changed(iter, service_compare, NULL);
5558 service_schedule_changed();
5561 __connman_connection_update_gateway();
5566 static void service_up(struct connman_ipconfig *ipconfig)
5568 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5570 DBG("%s up", __connman_ipconfig_get_ifname(ipconfig));
5572 link_changed(service);
5574 service->stats.valid = FALSE;
5575 service->stats_roaming.valid = FALSE;
5578 static void service_down(struct connman_ipconfig *ipconfig)
5580 DBG("%s down", __connman_ipconfig_get_ifname(ipconfig));
5583 static void service_lower_up(struct connman_ipconfig *ipconfig)
5585 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5587 DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig));
5589 stats_start(service);
5592 static void service_lower_down(struct connman_ipconfig *ipconfig)
5594 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5596 DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig));
5598 if (is_idle_state(service, service->state_ipv4) == FALSE)
5599 __connman_ipconfig_disable(service->ipconfig_ipv4);
5601 if (is_idle_state(service, service->state_ipv6) == FALSE)
5602 __connman_ipconfig_disable(service->ipconfig_ipv6);
5604 stats_stop(service);
5605 service_save(service);
5608 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5610 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5611 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5612 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5614 DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig));
5616 type = __connman_ipconfig_get_config_type(ipconfig);
5617 method = __connman_ipconfig_get_method(ipconfig);
5619 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5622 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5623 method == CONNMAN_IPCONFIG_METHOD_AUTO)
5624 __connman_service_ipconfig_indicate_state(service,
5625 CONNMAN_SERVICE_STATE_READY,
5626 CONNMAN_IPCONFIG_TYPE_IPV6);
5628 settings_changed(service, ipconfig);
5631 static void service_ip_release(struct connman_ipconfig *ipconfig)
5633 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5634 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5635 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5637 DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig));
5639 type = __connman_ipconfig_get_config_type(ipconfig);
5640 method = __connman_ipconfig_get_method(ipconfig);
5642 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5645 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5646 method == CONNMAN_IPCONFIG_METHOD_OFF)
5647 __connman_service_ipconfig_indicate_state(service,
5648 CONNMAN_SERVICE_STATE_DISCONNECT,
5649 CONNMAN_IPCONFIG_TYPE_IPV6);
5651 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5652 method == CONNMAN_IPCONFIG_METHOD_OFF)
5653 __connman_service_ipconfig_indicate_state(service,
5654 CONNMAN_SERVICE_STATE_DISCONNECT,
5655 CONNMAN_IPCONFIG_TYPE_IPV4);
5657 settings_changed(service, ipconfig);
5660 static const struct connman_ipconfig_ops service_ops = {
5662 .down = service_down,
5663 .lower_up = service_lower_up,
5664 .lower_down = service_lower_down,
5665 .ip_bound = service_ip_bound,
5666 .ip_release = service_ip_release,
5669 static void setup_ip4config(struct connman_service *service, int index,
5670 enum connman_ipconfig_method method)
5672 service->ipconfig_ipv4 = __connman_ipconfig_create(index,
5673 CONNMAN_IPCONFIG_TYPE_IPV4);
5674 if (service->ipconfig_ipv4 == NULL)
5677 __connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5679 __connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5681 __connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5684 static void setup_ip6config(struct connman_service *service, int index)
5686 service->ipconfig_ipv6 = __connman_ipconfig_create(index,
5687 CONNMAN_IPCONFIG_TYPE_IPV6);
5688 if (service->ipconfig_ipv6 == NULL)
5691 __connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5693 __connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5696 void __connman_service_read_ip4config(struct connman_service *service)
5700 if (service->ipconfig_ipv4 == NULL)
5703 keyfile = connman_storage_load_service(service->identifier);
5704 if (keyfile == NULL)
5707 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5708 service->identifier, "IPv4.");
5710 g_key_file_free(keyfile);
5713 void __connman_service_create_ip4config(struct connman_service *service,
5716 DBG("ipv4 %p", service->ipconfig_ipv4);
5718 if (service->ipconfig_ipv4 != NULL)
5721 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5722 __connman_service_read_ip4config(service);
5725 void __connman_service_read_ip6config(struct connman_service *service)
5729 if (service->ipconfig_ipv6 == NULL)
5732 keyfile = connman_storage_load_service(service->identifier);
5733 if (keyfile == NULL)
5736 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5737 service->identifier, "IPv6.");
5739 g_key_file_free(keyfile);
5742 void __connman_service_create_ip6config(struct connman_service *service,
5745 DBG("ipv6 %p", service->ipconfig_ipv6);
5747 if (service->ipconfig_ipv6 != NULL)
5750 setup_ip6config(service, index);
5752 __connman_service_read_ip6config(service);
5756 * __connman_service_lookup_from_network:
5757 * @network: network structure
5759 * Look up a service by network (reference count will not be increased)
5761 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5763 struct connman_service *service;
5764 const char *ident, *group;
5767 DBG("network %p", network);
5769 if (network == NULL)
5772 ident = __connman_network_get_ident(network);
5776 group = connman_network_get_group(network);
5780 name = g_strdup_printf("%s_%s_%s",
5781 __connman_network_get_type(network), ident, group);
5782 service = lookup_by_identifier(name);
5788 struct connman_service *__connman_service_lookup_from_index(int index)
5790 struct connman_service *service;
5791 GSequenceIter *iter;
5793 iter = g_sequence_get_begin_iter(service_list);
5795 while (g_sequence_iter_is_end(iter) == FALSE) {
5796 service = g_sequence_get(iter);
5798 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
5802 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
5806 iter = g_sequence_iter_next(iter);
5812 struct connman_service *__connman_service_lookup_from_ident(const char *identifier)
5814 return lookup_by_identifier(identifier);
5817 const char *__connman_service_get_ident(struct connman_service *service)
5819 return service->identifier;
5822 const char *__connman_service_get_path(struct connman_service *service)
5824 return service->path;
5827 unsigned int __connman_service_get_order(struct connman_service *service)
5829 GSequenceIter *iter;
5831 if (service == NULL)
5834 if (service->favorite == FALSE) {
5839 iter = g_hash_table_lookup(service_hash, service->identifier);
5841 if (g_sequence_iter_get_position(iter) == 0)
5843 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5844 service->do_split_routing == FALSE)
5845 service->order = 10;
5850 DBG("service %p name %s order %d split %d", service, service->name,
5851 service->order, service->do_split_routing);
5854 return service->order;
5857 void __connman_service_update_ordering(void)
5859 GSequenceIter *iter;
5861 iter = g_sequence_get_begin_iter(service_list);
5862 if (iter != NULL && g_sequence_get_length(service_list) > 1)
5863 g_sequence_sort_changed(iter, service_compare, NULL);
5866 static enum connman_service_type convert_network_type(struct connman_network *network)
5868 enum connman_network_type type = connman_network_get_type(network);
5871 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5872 case CONNMAN_NETWORK_TYPE_VENDOR:
5874 case CONNMAN_NETWORK_TYPE_ETHERNET:
5875 return CONNMAN_SERVICE_TYPE_ETHERNET;
5876 case CONNMAN_NETWORK_TYPE_WIFI:
5877 return CONNMAN_SERVICE_TYPE_WIFI;
5878 case CONNMAN_NETWORK_TYPE_WIMAX:
5879 return CONNMAN_SERVICE_TYPE_WIMAX;
5880 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5881 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5882 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5883 case CONNMAN_NETWORK_TYPE_CELLULAR:
5884 return CONNMAN_SERVICE_TYPE_CELLULAR;
5887 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5890 static enum connman_service_security convert_wifi_security(const char *security)
5892 if (security == NULL)
5893 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5894 else if (g_str_equal(security, "none") == TRUE)
5895 return CONNMAN_SERVICE_SECURITY_NONE;
5896 else if (g_str_equal(security, "wep") == TRUE)
5897 return CONNMAN_SERVICE_SECURITY_WEP;
5898 else if (g_str_equal(security, "psk") == TRUE)
5899 return CONNMAN_SERVICE_SECURITY_PSK;
5900 else if (g_str_equal(security, "ieee8021x") == TRUE)
5901 return CONNMAN_SERVICE_SECURITY_8021X;
5902 else if (g_str_equal(security, "wpa") == TRUE)
5903 return CONNMAN_SERVICE_SECURITY_WPA;
5904 else if (g_str_equal(security, "rsn") == TRUE)
5905 return CONNMAN_SERVICE_SECURITY_RSN;
5907 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5910 static void update_from_network(struct connman_service *service,
5911 struct connman_network *network)
5913 connman_uint8_t strength = service->strength;
5914 GSequenceIter *iter;
5917 DBG("service %p network %p", service, network);
5919 if (is_connected(service) == TRUE)
5922 if (is_connecting(service) == TRUE)
5925 str = connman_network_get_string(network, "Name");
5927 g_free(service->name);
5928 service->name = g_strdup(str);
5929 service->hidden = FALSE;
5931 g_free(service->name);
5932 service->name = NULL;
5933 service->hidden = TRUE;
5936 service->strength = connman_network_get_strength(network);
5937 service->roaming = connman_network_get_bool(network, "Roaming");
5939 if (service->strength == 0) {
5941 * Filter out 0-values; it's unclear what they mean
5942 * and they cause anomalous sorting of the priority list.
5944 service->strength = strength;
5947 str = connman_network_get_string(network, "WiFi.Security");
5948 service->security = convert_wifi_security(str);
5950 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5951 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5953 if (service->strength > strength && service->network != NULL) {
5954 connman_network_unref(service->network);
5955 service->network = connman_network_ref(network);
5957 strength_changed(service);
5960 if (service->network == NULL)
5961 service->network = connman_network_ref(network);
5963 iter = g_hash_table_lookup(service_hash, service->identifier);
5964 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
5965 g_sequence_sort_changed(iter, service_compare, NULL);
5966 service_schedule_changed();
5971 * __connman_service_create_from_network:
5972 * @network: network structure
5974 * Look up service by network and if not found, create one
5976 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5978 struct connman_service *service;
5979 struct connman_device *device;
5980 const char *ident, *group;
5982 unsigned int *auto_connect_types;
5985 DBG("network %p", network);
5987 if (network == NULL)
5990 ident = __connman_network_get_ident(network);
5994 group = connman_network_get_group(network);
5998 name = g_strdup_printf("%s_%s_%s",
5999 __connman_network_get_type(network), ident, group);
6000 service = service_get(name);
6003 if (service == NULL)
6006 if (__connman_network_get_weakness(network) == TRUE)
6009 if (service->path != NULL) {
6010 update_from_network(service, network);
6011 __connman_connection_update_gateway();
6015 service->type = convert_network_type(network);
6017 auto_connect_types = connman_setting_get_uint_list("DefaultAutoConnectTechnologies");
6018 service->autoconnect = FALSE;
6019 for (i = 0; auto_connect_types != NULL &&
6020 auto_connect_types[i] != 0; i++) {
6021 if (service->type == auto_connect_types[i]) {
6022 service->autoconnect = TRUE;
6027 switch (service->type) {
6028 case CONNMAN_SERVICE_TYPE_UNKNOWN:
6029 case CONNMAN_SERVICE_TYPE_SYSTEM:
6030 case CONNMAN_SERVICE_TYPE_WIMAX:
6031 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
6032 case CONNMAN_SERVICE_TYPE_GPS:
6033 case CONNMAN_SERVICE_TYPE_VPN:
6034 case CONNMAN_SERVICE_TYPE_GADGET:
6035 case CONNMAN_SERVICE_TYPE_WIFI:
6036 case CONNMAN_SERVICE_TYPE_CELLULAR:
6038 case CONNMAN_SERVICE_TYPE_ETHERNET:
6039 service->favorite = TRUE;
6043 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
6044 service->state = combine_state(service->state_ipv4, service->state_ipv6);
6046 update_from_network(service, network);
6048 index = connman_network_get_index(network);
6050 if (service->ipconfig_ipv4 == NULL)
6051 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
6053 if (service->ipconfig_ipv6 == NULL)
6054 setup_ip6config(service, index);
6056 service_register(service);
6058 if (service->favorite == TRUE) {
6059 device = connman_network_get_device(service->network);
6060 if (device && connman_device_get_scanning(device) == FALSE)
6061 __connman_service_auto_connect();
6064 __connman_notifier_service_add(service, service->name);
6065 service_schedule_added(service);
6070 void __connman_service_update_from_network(struct connman_network *network)
6072 connman_bool_t need_sort = FALSE;
6073 struct connman_service *service;
6074 connman_uint8_t strength;
6075 connman_bool_t roaming;
6076 GSequenceIter *iter;
6078 connman_bool_t stats_enable;
6080 DBG("network %p", network);
6082 service = __connman_service_lookup_from_network(network);
6083 if (service == NULL)
6086 if (service->network == NULL)
6089 name = connman_network_get_string(service->network, "Name");
6090 if (g_strcmp0(service->name, name) != 0) {
6091 g_free(service->name);
6092 service->name = g_strdup(name);
6093 connman_dbus_property_changed_basic(service->path,
6094 CONNMAN_SERVICE_INTERFACE, "Name",
6095 DBUS_TYPE_STRING, &service->name);
6098 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
6099 service->wps = connman_network_get_bool(network, "WiFi.WPS");
6101 strength = connman_network_get_strength(service->network);
6102 if (strength == service->strength)
6105 service->strength = strength;
6108 strength_changed(service);
6111 roaming = connman_network_get_bool(service->network, "Roaming");
6112 if (roaming == service->roaming)
6115 stats_enable = stats_enabled(service);
6116 if (stats_enable == TRUE)
6117 stats_stop(service);
6119 service->roaming = roaming;
6122 if (stats_enable == TRUE)
6123 stats_start(service);
6125 roaming_changed(service);
6128 if (need_sort == TRUE) {
6129 iter = g_hash_table_lookup(service_hash, service->identifier);
6130 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
6131 g_sequence_sort_changed(iter, service_compare, NULL);
6132 service_schedule_changed();
6137 void __connman_service_remove_from_network(struct connman_network *network)
6139 struct connman_service *service;
6141 DBG("network %p", network);
6143 service = __connman_service_lookup_from_network(network);
6144 if (service == NULL)
6147 service->ignore = TRUE;
6149 __connman_connection_gateway_remove(service,
6150 CONNMAN_IPCONFIG_TYPE_ALL);
6152 connman_service_unref(service);
6156 * __connman_service_create_from_provider:
6157 * @provider: provider structure
6159 * Look up service by provider and if not found, create one
6161 struct connman_service *
6162 __connman_service_create_from_provider(struct connman_provider *provider)
6164 struct connman_service *service;
6165 const char *ident, *str;
6167 int index = connman_provider_get_index(provider);
6169 DBG("provider %p", provider);
6171 ident = __connman_provider_get_ident(provider);
6175 name = g_strdup_printf("vpn_%s", ident);
6176 service = service_get(name);
6179 if (service == NULL)
6182 service->type = CONNMAN_SERVICE_TYPE_VPN;
6183 service->provider = connman_provider_ref(provider);
6184 service->autoconnect = FALSE;
6185 service->userconnect = TRUE;
6187 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
6188 service->state = combine_state(service->state_ipv4, service->state_ipv6);
6190 str = connman_provider_get_string(provider, "Name");
6192 g_free(service->name);
6193 service->name = g_strdup(str);
6194 service->hidden = FALSE;
6196 g_free(service->name);
6197 service->name = NULL;
6198 service->hidden = TRUE;
6201 service->strength = 0;
6203 if (service->ipconfig_ipv4 == NULL)
6204 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
6206 if (service->ipconfig_ipv6 == NULL)
6207 setup_ip6config(service, index);
6209 service_register(service);
6211 __connman_notifier_service_add(service, service->name);
6212 service_schedule_added(service);
6217 int __connman_service_init(void)
6221 connection = connman_dbus_get_connection();
6223 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
6226 service_list = g_sequence_new(service_free);
6228 services_notify = g_new0(struct _services_notify, 1);
6229 services_notify->remove = g_hash_table_new_full(g_str_hash,
6230 g_str_equal, g_free, NULL);
6231 services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
6236 void __connman_service_cleanup(void)
6242 if (autoconnect_timeout != 0) {
6243 g_source_remove(autoconnect_timeout);
6244 autoconnect_timeout = 0;
6247 list = service_list;
6248 service_list = NULL;
6249 g_sequence_free(list);
6251 g_hash_table_destroy(service_hash);
6252 service_hash = NULL;
6254 g_slist_free(counter_list);
6255 counter_list = NULL;
6257 if (services_notify->id != 0) {
6258 g_source_remove(services_notify->id);
6259 service_send_changed(NULL);
6260 g_hash_table_destroy(services_notify->remove);
6261 g_hash_table_destroy(services_notify->add);
6263 g_free(services_notify);
6265 dbus_connection_unref(connection);