5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #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;
123 struct connman_service *service;
126 static void compare_path(gpointer value, gpointer user_data)
128 struct connman_service *service = value;
129 struct find_data *data = user_data;
131 if (data->service != NULL)
134 if (g_strcmp0(service->path, data->path) == 0)
135 data->service = service;
138 static struct connman_service *find_service(const char *path)
140 struct find_data data = { .path = path, .service = NULL };
142 DBG("path %s", path);
144 g_sequence_foreach(service_list, compare_path, &data);
149 const char *__connman_service_type2string(enum connman_service_type type)
152 case CONNMAN_SERVICE_TYPE_UNKNOWN:
154 case CONNMAN_SERVICE_TYPE_SYSTEM:
156 case CONNMAN_SERVICE_TYPE_ETHERNET:
158 case CONNMAN_SERVICE_TYPE_WIFI:
160 case CONNMAN_SERVICE_TYPE_WIMAX:
162 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
164 case CONNMAN_SERVICE_TYPE_CELLULAR:
166 case CONNMAN_SERVICE_TYPE_GPS:
168 case CONNMAN_SERVICE_TYPE_VPN:
170 case CONNMAN_SERVICE_TYPE_GADGET:
177 enum connman_service_type __connman_service_string2type(const char *str)
180 return CONNMAN_SERVICE_TYPE_UNKNOWN;
182 if (strcmp(str, "ethernet") == 0)
183 return CONNMAN_SERVICE_TYPE_ETHERNET;
184 if (strcmp(str, "gadget") == 0)
185 return CONNMAN_SERVICE_TYPE_GADGET;
186 if (strcmp(str, "wifi") == 0)
187 return CONNMAN_SERVICE_TYPE_WIFI;
188 if (strcmp(str, "cellular") == 0)
189 return CONNMAN_SERVICE_TYPE_CELLULAR;
190 if (strcmp(str, "bluetooth") == 0)
191 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
192 if (strcmp(str, "wimax") == 0)
193 return CONNMAN_SERVICE_TYPE_WIMAX;
194 if (strcmp(str, "vpn") == 0)
195 return CONNMAN_SERVICE_TYPE_VPN;
196 if (strcmp(str, "gps") == 0)
197 return CONNMAN_SERVICE_TYPE_GPS;
198 if (strcmp(str, "system") == 0)
199 return CONNMAN_SERVICE_TYPE_SYSTEM;
201 return CONNMAN_SERVICE_TYPE_UNKNOWN;
204 static const char *security2string(enum connman_service_security security)
207 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
209 case CONNMAN_SERVICE_SECURITY_NONE:
211 case CONNMAN_SERVICE_SECURITY_WEP:
213 case CONNMAN_SERVICE_SECURITY_PSK:
214 case CONNMAN_SERVICE_SECURITY_WPA:
215 case CONNMAN_SERVICE_SECURITY_RSN:
217 case CONNMAN_SERVICE_SECURITY_8021X:
224 static const char *state2string(enum connman_service_state state)
227 case CONNMAN_SERVICE_STATE_UNKNOWN:
229 case CONNMAN_SERVICE_STATE_IDLE:
231 case CONNMAN_SERVICE_STATE_ASSOCIATION:
232 return "association";
233 case CONNMAN_SERVICE_STATE_CONFIGURATION:
234 return "configuration";
235 case CONNMAN_SERVICE_STATE_READY:
237 case CONNMAN_SERVICE_STATE_ONLINE:
239 case CONNMAN_SERVICE_STATE_DISCONNECT:
241 case CONNMAN_SERVICE_STATE_FAILURE:
248 static const char *error2string(enum connman_service_error error)
251 case CONNMAN_SERVICE_ERROR_UNKNOWN:
253 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
254 return "out-of-range";
255 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
256 return "pin-missing";
257 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
258 return "dhcp-failed";
259 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
260 return "connect-failed";
261 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
262 return "login-failed";
263 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
264 return "auth-failed";
265 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
266 return "invalid-key";
272 static enum connman_service_error string2error(const char *error)
274 if (g_strcmp0(error, "dhcp-failed") == 0)
275 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
276 else if (g_strcmp0(error, "pin-missing") == 0)
277 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
278 else if (g_strcmp0(error, "invalid-key") == 0)
279 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
281 return CONNMAN_SERVICE_ERROR_UNKNOWN;
284 static const char *proxymethod2string(enum connman_service_proxy_method method)
287 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
289 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
291 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
293 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
300 static enum connman_service_proxy_method string2proxymethod(const char *method)
302 if (g_strcmp0(method, "direct") == 0)
303 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
304 else if (g_strcmp0(method, "auto") == 0)
305 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
306 else if (g_strcmp0(method, "manual") == 0)
307 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
309 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
312 static int service_load(struct connman_service *service)
315 GError *error = NULL;
318 connman_bool_t autoconnect;
319 unsigned int ssid_len;
322 DBG("service %p", service);
324 keyfile = connman_storage_load_service(service->identifier);
328 switch (service->type) {
329 case CONNMAN_SERVICE_TYPE_UNKNOWN:
330 case CONNMAN_SERVICE_TYPE_SYSTEM:
331 case CONNMAN_SERVICE_TYPE_GPS:
332 case CONNMAN_SERVICE_TYPE_GADGET:
334 case CONNMAN_SERVICE_TYPE_VPN:
335 service->do_split_routing = g_key_file_get_boolean(keyfile,
336 service->identifier, "SplitRouting", NULL);
338 case CONNMAN_SERVICE_TYPE_WIFI:
339 if (service->name == NULL) {
342 name = g_key_file_get_string(keyfile,
343 service->identifier, "Name", NULL);
345 g_free(service->name);
346 service->name = name;
349 if (service->network != NULL)
350 connman_network_set_name(service->network,
354 if (service->network &&
355 connman_network_get_blob(service->network,
356 "WiFi.SSID", &ssid_len) == NULL) {
359 hex_ssid = g_key_file_get_string(keyfile,
363 if (hex_ssid != NULL) {
365 unsigned int i, j = 0, hex;
366 size_t hex_ssid_len = strlen(hex_ssid);
368 ssid = g_try_malloc0(hex_ssid_len / 2);
375 for (i = 0; i < hex_ssid_len; i += 2) {
376 sscanf(hex_ssid + i, "%02x", &hex);
380 connman_network_set_blob(service->network,
381 "WiFi.SSID", ssid, hex_ssid_len / 2);
388 case CONNMAN_SERVICE_TYPE_WIMAX:
389 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
390 case CONNMAN_SERVICE_TYPE_CELLULAR:
391 service->favorite = g_key_file_get_boolean(keyfile,
392 service->identifier, "Favorite", NULL);
394 str = g_key_file_get_string(keyfile,
395 service->identifier, "Failure", NULL);
397 if (service->favorite == FALSE)
398 service->state_ipv4 = service->state_ipv6 =
399 CONNMAN_SERVICE_STATE_FAILURE;
400 service->error = string2error(str);
405 case CONNMAN_SERVICE_TYPE_ETHERNET:
406 autoconnect = g_key_file_get_boolean(keyfile,
407 service->identifier, "AutoConnect", &error);
409 service->autoconnect = autoconnect;
410 g_clear_error(&error);
414 str = g_key_file_get_string(keyfile,
415 service->identifier, "Modified", NULL);
417 g_time_val_from_iso8601(str, &service->modified);
421 str = g_key_file_get_string(keyfile,
422 service->identifier, "Passphrase", NULL);
424 g_free(service->passphrase);
425 service->passphrase = str;
428 if (service->ipconfig_ipv4 != NULL)
429 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
430 service->identifier, "IPv4.");
432 if (service->ipconfig_ipv6 != NULL)
433 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
434 service->identifier, "IPv6.");
436 service->nameservers_config = g_key_file_get_string_list(keyfile,
437 service->identifier, "Nameservers", &length, NULL);
438 if (service->nameservers_config != NULL && length == 0) {
439 g_strfreev(service->nameservers_config);
440 service->nameservers_config = NULL;
443 service->timeservers_config = g_key_file_get_string_list(keyfile,
444 service->identifier, "Timeservers", &length, NULL);
445 if (service->timeservers_config != NULL && length == 0) {
446 g_strfreev(service->timeservers_config);
447 service->timeservers_config = NULL;
450 service->domains = g_key_file_get_string_list(keyfile,
451 service->identifier, "Domains", &length, NULL);
452 if (service->domains != NULL && length == 0) {
453 g_strfreev(service->domains);
454 service->domains = NULL;
457 str = g_key_file_get_string(keyfile,
458 service->identifier, "Proxy.Method", NULL);
460 service->proxy_config = string2proxymethod(str);
464 service->proxies = g_key_file_get_string_list(keyfile,
465 service->identifier, "Proxy.Servers", &length, NULL);
466 if (service->proxies != NULL && length == 0) {
467 g_strfreev(service->proxies);
468 service->proxies = NULL;
471 service->excludes = g_key_file_get_string_list(keyfile,
472 service->identifier, "Proxy.Excludes", &length, NULL);
473 if (service->excludes != NULL && length == 0) {
474 g_strfreev(service->excludes);
475 service->excludes = NULL;
478 str = g_key_file_get_string(keyfile,
479 service->identifier, "Proxy.URL", NULL);
481 g_free(service->pac);
486 g_key_file_free(keyfile);
491 static int service_save(struct connman_service *service)
496 const char *cst_str = NULL;
499 DBG("service %p", service);
501 keyfile = __connman_storage_open_service(service->identifier);
505 if (service->name != NULL)
506 g_key_file_set_string(keyfile, service->identifier,
507 "Name", service->name);
509 switch (service->type) {
510 case CONNMAN_SERVICE_TYPE_UNKNOWN:
511 case CONNMAN_SERVICE_TYPE_SYSTEM:
512 case CONNMAN_SERVICE_TYPE_GPS:
513 case CONNMAN_SERVICE_TYPE_GADGET:
515 case CONNMAN_SERVICE_TYPE_VPN:
516 g_key_file_set_boolean(keyfile, service->identifier,
517 "SplitRouting", service->do_split_routing);
519 case CONNMAN_SERVICE_TYPE_WIFI:
520 if (service->network) {
521 const unsigned char *ssid;
522 unsigned int ssid_len = 0;
524 ssid = connman_network_get_blob(service->network,
525 "WiFi.SSID", &ssid_len);
527 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
528 char *identifier = service->identifier;
532 str = g_string_sized_new(ssid_len * 2);
538 for (i = 0; i < ssid_len; i++)
539 g_string_append_printf(str,
542 g_key_file_set_string(keyfile, identifier,
545 g_string_free(str, TRUE);
548 freq = connman_network_get_frequency(service->network);
549 g_key_file_set_integer(keyfile, service->identifier,
554 case CONNMAN_SERVICE_TYPE_WIMAX:
555 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
556 case CONNMAN_SERVICE_TYPE_CELLULAR:
557 g_key_file_set_boolean(keyfile, service->identifier,
558 "Favorite", service->favorite);
560 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
561 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
562 const char *failure = error2string(service->error);
564 g_key_file_set_string(keyfile,
568 g_key_file_remove_key(keyfile, service->identifier,
573 case CONNMAN_SERVICE_TYPE_ETHERNET:
574 if (service->favorite == TRUE)
575 g_key_file_set_boolean(keyfile, service->identifier,
576 "AutoConnect", service->autoconnect);
580 str = g_time_val_to_iso8601(&service->modified);
582 g_key_file_set_string(keyfile, service->identifier,
587 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
588 g_key_file_set_string(keyfile, service->identifier,
589 "Passphrase", service->passphrase);
591 g_key_file_remove_key(keyfile, service->identifier,
594 if (service->ipconfig_ipv4 != NULL)
595 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
596 service->identifier, "IPv4.");
598 if (service->ipconfig_ipv6 != NULL)
599 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
600 service->identifier, "IPv6.");
602 if (service->nameservers_config != NULL) {
603 guint len = g_strv_length(service->nameservers_config);
605 g_key_file_set_string_list(keyfile, service->identifier,
607 (const gchar **) service->nameservers_config, len);
609 g_key_file_remove_key(keyfile, service->identifier,
610 "Nameservers", NULL);
612 if (service->timeservers_config != NULL) {
613 guint len = g_strv_length(service->timeservers_config);
615 g_key_file_set_string_list(keyfile, service->identifier,
617 (const gchar **) service->timeservers_config, len);
619 g_key_file_remove_key(keyfile, service->identifier,
620 "Timeservers", NULL);
622 if (service->domains != NULL) {
623 guint len = g_strv_length(service->domains);
625 g_key_file_set_string_list(keyfile, service->identifier,
627 (const gchar **) service->domains, len);
629 g_key_file_remove_key(keyfile, service->identifier,
632 cst_str = proxymethod2string(service->proxy_config);
634 g_key_file_set_string(keyfile, service->identifier,
635 "Proxy.Method", cst_str);
637 if (service->proxies != NULL) {
638 guint len = g_strv_length(service->proxies);
640 g_key_file_set_string_list(keyfile, service->identifier,
642 (const gchar **) service->proxies, len);
644 g_key_file_remove_key(keyfile, service->identifier,
645 "Proxy.Servers", NULL);
647 if (service->excludes != NULL) {
648 guint len = g_strv_length(service->excludes);
650 g_key_file_set_string_list(keyfile, service->identifier,
652 (const gchar **) service->excludes, len);
654 g_key_file_remove_key(keyfile, service->identifier,
655 "Proxy.Excludes", NULL);
657 if (service->pac != NULL && strlen(service->pac) > 0)
658 g_key_file_set_string(keyfile, service->identifier,
659 "Proxy.URL", service->pac);
661 g_key_file_remove_key(keyfile, service->identifier,
665 __connman_storage_save_service(keyfile, service->identifier);
667 g_key_file_free(keyfile);
672 static enum connman_service_state combine_state(
673 enum connman_service_state state_a,
674 enum connman_service_state state_b)
676 enum connman_service_state result;
678 if (state_a == state_b) {
683 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
688 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
693 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
698 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
703 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
708 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
713 if (state_a == CONNMAN_SERVICE_STATE_READY) {
718 if (state_b == CONNMAN_SERVICE_STATE_READY) {
723 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
728 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
733 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
738 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
743 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
748 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
753 result = CONNMAN_SERVICE_STATE_FAILURE;
759 static connman_bool_t is_connecting_state(struct connman_service *service,
760 enum connman_service_state state)
763 case CONNMAN_SERVICE_STATE_UNKNOWN:
764 case CONNMAN_SERVICE_STATE_IDLE:
765 case CONNMAN_SERVICE_STATE_FAILURE:
766 if (service->network != NULL)
767 return connman_network_get_connecting(service->network);
768 case CONNMAN_SERVICE_STATE_DISCONNECT:
769 case CONNMAN_SERVICE_STATE_READY:
770 case CONNMAN_SERVICE_STATE_ONLINE:
772 case CONNMAN_SERVICE_STATE_ASSOCIATION:
773 case CONNMAN_SERVICE_STATE_CONFIGURATION:
780 static connman_bool_t is_connected_state(const struct connman_service *service,
781 enum connman_service_state state)
784 case CONNMAN_SERVICE_STATE_UNKNOWN:
785 case CONNMAN_SERVICE_STATE_IDLE:
786 case CONNMAN_SERVICE_STATE_ASSOCIATION:
787 case CONNMAN_SERVICE_STATE_CONFIGURATION:
788 case CONNMAN_SERVICE_STATE_DISCONNECT:
789 case CONNMAN_SERVICE_STATE_FAILURE:
791 case CONNMAN_SERVICE_STATE_READY:
792 case CONNMAN_SERVICE_STATE_ONLINE:
799 static connman_bool_t is_idle_state(const struct connman_service *service,
800 enum connman_service_state state)
803 case CONNMAN_SERVICE_STATE_UNKNOWN:
804 case CONNMAN_SERVICE_STATE_ASSOCIATION:
805 case CONNMAN_SERVICE_STATE_CONFIGURATION:
806 case CONNMAN_SERVICE_STATE_READY:
807 case CONNMAN_SERVICE_STATE_ONLINE:
808 case CONNMAN_SERVICE_STATE_DISCONNECT:
809 case CONNMAN_SERVICE_STATE_FAILURE:
811 case CONNMAN_SERVICE_STATE_IDLE:
818 static connman_bool_t is_connecting(struct connman_service *service)
820 return is_connecting_state(service, service->state);
823 static connman_bool_t is_connected(struct connman_service *service)
825 return is_connected_state(service, service->state);
828 static void update_nameservers(struct connman_service *service)
832 if (service->ipconfig_ipv4)
833 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
834 else if (service->ipconfig_ipv6)
835 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
842 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
843 case CONNMAN_SERVICE_STATE_UNKNOWN:
844 case CONNMAN_SERVICE_STATE_IDLE:
845 case CONNMAN_SERVICE_STATE_ASSOCIATION:
846 case CONNMAN_SERVICE_STATE_CONFIGURATION:
848 case CONNMAN_SERVICE_STATE_FAILURE:
849 case CONNMAN_SERVICE_STATE_DISCONNECT:
850 connman_resolver_remove_all(ifname);
852 case CONNMAN_SERVICE_STATE_READY:
853 case CONNMAN_SERVICE_STATE_ONLINE:
857 if (service->nameservers_config != NULL) {
860 for (i = 0; service->nameservers_config[i] != NULL; i++) {
861 connman_resolver_append(ifname, NULL,
862 service->nameservers_config[i]);
864 } else if (service->nameservers != NULL) {
867 for (i = 0; service->nameservers[i] != NULL; i++) {
868 connman_resolver_append(ifname, NULL,
869 service->nameservers[i]);
873 if (service->domains != NULL) {
876 for (i = 0; service->domains[i]; i++)
877 connman_resolver_append(ifname, service->domains[i],
879 } else if (service->domainname != NULL)
880 connman_resolver_append(ifname, service->domainname, NULL);
882 connman_resolver_flush();
886 * The is_auto variable is set to true when IPv6 autoconf nameservers are
887 * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
888 * for details) and not through service.c
890 int __connman_service_nameserver_append(struct connman_service *service,
891 const char *nameserver, gboolean is_auto)
896 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
898 if (nameserver == NULL)
902 nameservers = service->nameservers_auto;
904 nameservers = service->nameservers;
906 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
907 if (g_strcmp0(nameservers[i], nameserver) == 0)
910 if (nameservers != NULL) {
911 len = g_strv_length(nameservers);
912 nameservers = g_try_renew(char *, nameservers, len + 2);
915 nameservers = g_try_new0(char *, len + 2);
918 if (nameservers == NULL)
921 nameservers[len] = g_strdup(nameserver);
922 if (nameservers[len] == NULL)
925 nameservers[len + 1] = NULL;
927 if (is_auto == TRUE) {
928 service->nameservers_auto = nameservers;
930 service->nameservers = nameservers;
931 update_nameservers(service);
937 int __connman_service_nameserver_remove(struct connman_service *service,
938 const char *nameserver, gboolean is_auto)
940 char **servers, **nameservers;
941 gboolean found = FALSE;
944 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
946 if (nameserver == NULL)
950 nameservers = service->nameservers_auto;
952 nameservers = service->nameservers;
954 if (nameservers == NULL)
957 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
958 if (g_strcmp0(nameservers[i], nameserver) == 0) {
966 len = g_strv_length(nameservers);
969 g_strfreev(nameservers);
971 service->nameservers_auto = NULL;
973 service->nameservers = NULL;
978 servers = g_try_new0(char *, len);
982 for (i = 0, j = 0; i < len; i++) {
983 if (g_strcmp0(nameservers[i], nameserver) != 0) {
984 servers[j] = g_strdup(nameservers[i]);
985 if (servers[j] == NULL)
990 servers[len - 1] = NULL;
992 g_strfreev(nameservers);
993 nameservers = servers;
995 if (is_auto == TRUE) {
996 service->nameservers_auto = nameservers;
998 service->nameservers = nameservers;
999 update_nameservers(service);
1005 void __connman_service_nameserver_clear(struct connman_service *service)
1007 g_strfreev(service->nameservers);
1008 service->nameservers = NULL;
1010 update_nameservers(service);
1013 static void add_nameserver_route(int family, int index, char *nameserver,
1018 if (connman_inet_compare_subnet(index, nameserver) == TRUE)
1021 if (connman_inet_add_host_route(index, nameserver, gw) < 0)
1022 /* For P-t-P link the above route add will fail */
1023 connman_inet_add_host_route(index, nameserver, NULL);
1027 if (connman_inet_add_ipv6_host_route(index, nameserver,
1029 connman_inet_add_ipv6_host_route(index, nameserver,
1035 static void nameserver_add_routes(int index, char **nameservers,
1039 struct addrinfo hints;
1040 struct addrinfo *addr;
1042 for (i = 0; nameservers[i] != NULL; i++) {
1043 memset(&hints, 0, sizeof(struct addrinfo));
1044 hints.ai_flags = AI_NUMERICHOST;
1047 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1048 if (ret == EAI_NONAME)
1049 family = AF_INET; /* use the IPv4 as a default */
1053 family = addr->ai_family;
1055 add_nameserver_route(family, index, nameservers[i], gw);
1061 static void nameserver_del_routes(int index, char **nameservers,
1062 enum connman_ipconfig_type type)
1065 struct addrinfo hints;
1066 struct addrinfo *addr;
1068 for (i = 0; nameservers[i] != NULL; i++) {
1069 memset(&hints, 0, sizeof(struct addrinfo));
1070 hints.ai_flags = AI_NUMERICHOST;
1073 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1074 if (ret == EAI_NONAME)
1075 family = AF_INET; /* use the IPv4 as a default */
1079 family = addr->ai_family;
1083 if (type != CONNMAN_IPCONFIG_TYPE_IPV6)
1084 connman_inet_del_host_route(index,
1088 if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
1089 connman_inet_del_ipv6_host_route(index,
1098 void __connman_service_nameserver_add_routes(struct connman_service *service,
1103 if (service == NULL)
1106 if (service->network != NULL)
1107 index = connman_network_get_index(service->network);
1108 else if (service->provider != NULL)
1109 index = connman_provider_get_index(service->provider);
1111 if (service->nameservers_config != NULL) {
1113 * Configured nameserver takes preference over the
1114 * discoverd nameserver gathered from DHCP, VPN, etc.
1116 nameserver_add_routes(index, service->nameservers_config, gw);
1117 } else if (service->nameservers != NULL) {
1119 * We add nameservers host routes for nameservers that
1120 * are not on our subnet. For those who are, the subnet
1121 * route will be installed by the time the dns proxy code
1122 * tries to reach them. The subnet route is installed
1123 * when setting the interface IP address.
1125 nameserver_add_routes(index, service->nameservers, gw);
1129 void __connman_service_nameserver_del_routes(struct connman_service *service,
1130 enum connman_ipconfig_type type)
1134 if (service == NULL)
1137 if (service->network != NULL)
1138 index = connman_network_get_index(service->network);
1139 else if (service->provider != NULL)
1140 index = connman_provider_get_index(service->provider);
1142 if (service->nameservers_config != NULL)
1143 nameserver_del_routes(index, service->nameservers_config,
1145 else if (service->nameservers != NULL)
1146 nameserver_del_routes(index, service->nameservers, type);
1149 static struct connman_stats *stats_get(struct connman_service *service)
1151 if (service->roaming == TRUE)
1152 return &service->stats_roaming;
1154 return &service->stats;
1157 static connman_bool_t stats_enabled(struct connman_service *service)
1159 struct connman_stats *stats = stats_get(service);
1161 return stats->enabled;
1164 static void stats_start(struct connman_service *service)
1166 struct connman_stats *stats = stats_get(service);
1168 DBG("service %p", service);
1170 if (stats->timer == NULL)
1173 stats->enabled = TRUE;
1174 stats->data_last.time = stats->data.time;
1176 g_timer_start(stats->timer);
1179 static void stats_stop(struct connman_service *service)
1181 struct connman_stats *stats = stats_get(service);
1182 unsigned int seconds;
1184 DBG("service %p", service);
1186 if (stats->timer == NULL)
1189 if (stats->enabled == FALSE)
1192 g_timer_stop(stats->timer);
1194 seconds = g_timer_elapsed(stats->timer, NULL);
1195 stats->data.time = stats->data_last.time + seconds;
1197 stats->enabled = FALSE;
1200 static void reset_stats(struct connman_service *service)
1202 DBG("service %p", service);
1205 service->stats.valid = FALSE;
1207 service->stats.data.rx_packets = 0;
1208 service->stats.data.tx_packets = 0;
1209 service->stats.data.rx_bytes = 0;
1210 service->stats.data.tx_bytes = 0;
1211 service->stats.data.rx_errors = 0;
1212 service->stats.data.tx_errors = 0;
1213 service->stats.data.rx_dropped = 0;
1214 service->stats.data.tx_dropped = 0;
1215 service->stats.data.time = 0;
1216 service->stats.data_last.time = 0;
1218 g_timer_reset(service->stats.timer);
1221 service->stats_roaming.valid = FALSE;
1223 service->stats_roaming.data.rx_packets = 0;
1224 service->stats_roaming.data.tx_packets = 0;
1225 service->stats_roaming.data.rx_bytes = 0;
1226 service->stats_roaming.data.tx_bytes = 0;
1227 service->stats_roaming.data.rx_errors = 0;
1228 service->stats_roaming.data.tx_errors = 0;
1229 service->stats_roaming.data.rx_dropped = 0;
1230 service->stats_roaming.data.tx_dropped = 0;
1231 service->stats_roaming.data.time = 0;
1232 service->stats_roaming.data_last.time = 0;
1234 g_timer_reset(service->stats_roaming.timer);
1237 struct connman_service *__connman_service_get_default(void)
1239 struct connman_service *service;
1240 GSequenceIter *iter;
1242 iter = g_sequence_get_begin_iter(service_list);
1244 if (g_sequence_iter_is_end(iter) == TRUE)
1247 service = g_sequence_get(iter);
1249 if (is_connected(service) == FALSE)
1255 static void default_changed(void)
1257 struct connman_service *service = __connman_service_get_default();
1259 if (service == current_default)
1262 current_default = service;
1264 __connman_notifier_default_changed(service);
1267 static void state_changed(struct connman_service *service)
1271 __connman_notifier_service_state_changed(service, service->state);
1273 str = state2string(service->state);
1277 connman_dbus_property_changed_basic(service->path,
1278 CONNMAN_SERVICE_INTERFACE, "State",
1279 DBUS_TYPE_STRING, &str);
1282 static void strength_changed(struct connman_service *service)
1284 if (service->strength == 0)
1287 connman_dbus_property_changed_basic(service->path,
1288 CONNMAN_SERVICE_INTERFACE, "Strength",
1289 DBUS_TYPE_BYTE, &service->strength);
1292 static void favorite_changed(struct connman_service *service)
1294 if (service->path == NULL)
1297 connman_dbus_property_changed_basic(service->path,
1298 CONNMAN_SERVICE_INTERFACE, "Favorite",
1299 DBUS_TYPE_BOOLEAN, &service->favorite);
1302 static void immutable_changed(struct connman_service *service)
1304 if (service->path == NULL)
1307 connman_dbus_property_changed_basic(service->path,
1308 CONNMAN_SERVICE_INTERFACE, "Immutable",
1309 DBUS_TYPE_BOOLEAN, &service->immutable);
1312 static void roaming_changed(struct connman_service *service)
1314 if (service->path == NULL)
1317 connman_dbus_property_changed_basic(service->path,
1318 CONNMAN_SERVICE_INTERFACE, "Roaming",
1319 DBUS_TYPE_BOOLEAN, &service->roaming);
1322 static void autoconnect_changed(struct connman_service *service)
1324 if (service->path == NULL)
1327 connman_dbus_property_changed_basic(service->path,
1328 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1329 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1332 static void append_security(DBusMessageIter *iter, void *user_data)
1334 struct connman_service *service = user_data;
1337 str = security2string(service->security);
1339 dbus_message_iter_append_basic(iter,
1340 DBUS_TYPE_STRING, &str);
1343 * Some access points incorrectly advertise WPS even when they
1344 * are configured as open or no security, so filter
1347 if (service->wps == TRUE) {
1348 switch (service->security) {
1349 case CONNMAN_SERVICE_SECURITY_PSK:
1350 case CONNMAN_SERVICE_SECURITY_WPA:
1351 case CONNMAN_SERVICE_SECURITY_RSN:
1353 dbus_message_iter_append_basic(iter,
1354 DBUS_TYPE_STRING, &str);
1356 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1357 case CONNMAN_SERVICE_SECURITY_NONE:
1358 case CONNMAN_SERVICE_SECURITY_WEP:
1359 case CONNMAN_SERVICE_SECURITY_8021X:
1365 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1367 struct connman_service *service = user_data;
1369 if (service->ipconfig_ipv4 != NULL)
1370 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1372 else if (service->ipconfig_ipv6 != NULL)
1373 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1377 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1379 struct connman_service *service = user_data;
1381 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1382 state2string(service->state_ipv4));
1384 if (is_connected_state(service, service->state_ipv4) == FALSE)
1387 if (service->ipconfig_ipv4 != NULL)
1388 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1391 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1393 struct connman_service *service = user_data;
1395 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1396 state2string(service->state_ipv6));
1398 if (is_connected_state(service, service->state_ipv6) == FALSE)
1401 if (service->ipconfig_ipv6 != NULL)
1402 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1403 service->ipconfig_ipv4);
1406 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1408 struct connman_service *service = user_data;
1410 if (service->ipconfig_ipv4 != NULL)
1411 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1415 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1417 struct connman_service *service = user_data;
1419 if (service->ipconfig_ipv6 != NULL)
1420 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1424 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1429 servers = *nameservers;
1431 for (i = 0; servers[i] != NULL; i++) {
1432 dbus_message_iter_append_basic(iter,
1433 DBUS_TYPE_STRING, &servers[i]);
1437 static void append_dns(DBusMessageIter *iter, void *user_data)
1439 struct connman_service *service = user_data;
1441 if (is_connected(service) == FALSE)
1444 if (service->nameservers_config != NULL) {
1445 append_nameserver(iter, &service->nameservers_config);
1448 if (service->nameservers != NULL)
1449 append_nameserver(iter, &service->nameservers);
1451 if (service->nameservers_auto != NULL)
1452 append_nameserver(iter, &service->nameservers_auto);
1456 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1458 struct connman_service *service = user_data;
1461 if (service->nameservers_config == NULL)
1464 for (i = 0; service->nameservers_config[i]; i++) {
1465 dbus_message_iter_append_basic(iter,
1467 &service->nameservers_config[i]);
1471 static void append_tsconfig(DBusMessageIter *iter, void *user_data)
1473 struct connman_service *service = user_data;
1476 if (service->timeservers_config == NULL)
1479 for (i = 0; service->timeservers_config[i]; i++) {
1480 dbus_message_iter_append_basic(iter,
1482 &service->timeservers_config[i]);
1486 static void append_domain(DBusMessageIter *iter, void *user_data)
1488 struct connman_service *service = user_data;
1490 if (is_connected(service) == FALSE &&
1491 is_connecting(service) == FALSE)
1494 if (service->domainname == NULL)
1497 dbus_message_iter_append_basic(iter,
1498 DBUS_TYPE_STRING, &service->domainname);
1501 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1503 struct connman_service *service = user_data;
1506 if (service->domains == NULL)
1509 for (i = 0; service->domains[i]; i++)
1510 dbus_message_iter_append_basic(iter,
1511 DBUS_TYPE_STRING, &service->domains[i]);
1514 static void append_proxies(DBusMessageIter *iter, void *user_data)
1516 struct connman_service *service = user_data;
1519 if (service->proxies == NULL)
1522 for (i = 0; service->proxies[i]; i++)
1523 dbus_message_iter_append_basic(iter,
1524 DBUS_TYPE_STRING, &service->proxies[i]);
1527 static void append_excludes(DBusMessageIter *iter, void *user_data)
1529 struct connman_service *service = user_data;
1532 if (service->excludes == NULL)
1535 for (i = 0; service->excludes[i]; i++)
1536 dbus_message_iter_append_basic(iter,
1537 DBUS_TYPE_STRING, &service->excludes[i]);
1540 static void append_proxy(DBusMessageIter *iter, void *user_data)
1542 struct connman_service *service = user_data;
1543 enum connman_service_proxy_method proxy;
1544 const char *pac = NULL;
1545 const char *method = proxymethod2string(
1546 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1550 if (is_connected(service) == FALSE)
1553 proxy = connman_service_get_proxy_method(service);
1556 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1558 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1560 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1561 connman_dbus_dict_append_array(iter, "Servers",
1562 DBUS_TYPE_STRING, append_proxies,
1565 connman_dbus_dict_append_array(iter, "Excludes",
1566 DBUS_TYPE_STRING, append_excludes,
1569 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1570 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1571 if (service->ipconfig_ipv4 != NULL)
1572 pac = __connman_ipconfig_get_proxy_autoconfig(
1573 service->ipconfig_ipv4);
1574 else if (service->ipconfig_ipv6 != NULL)
1575 pac = __connman_ipconfig_get_proxy_autoconfig(
1576 service->ipconfig_ipv6);
1578 if (service->pac == NULL && pac == NULL)
1581 if (service->pac != NULL)
1584 connman_dbus_dict_append_basic(iter, "URL",
1585 DBUS_TYPE_STRING, &pac);
1589 method = proxymethod2string(proxy);
1592 connman_dbus_dict_append_basic(iter, "Method",
1593 DBUS_TYPE_STRING, &method);
1596 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1598 struct connman_service *service = user_data;
1601 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1604 switch (service->proxy_config) {
1605 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1607 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1609 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1610 if (service->proxies != NULL)
1611 connman_dbus_dict_append_array(iter, "Servers",
1613 append_proxies, service);
1615 if (service->excludes != NULL)
1616 connman_dbus_dict_append_array(iter, "Excludes",
1618 append_excludes, service);
1620 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1621 if (service->pac != NULL)
1622 connman_dbus_dict_append_basic(iter, "URL",
1623 DBUS_TYPE_STRING, &service->pac);
1627 method = proxymethod2string(service->proxy_config);
1629 connman_dbus_dict_append_basic(iter, "Method",
1630 DBUS_TYPE_STRING, &method);
1633 static void append_provider(DBusMessageIter *iter, void *user_data)
1635 struct connman_service *service = user_data;
1637 DBG("%p %p", service, service->provider);
1639 if (is_connected(service) == FALSE)
1642 if (service->provider != NULL)
1643 __connman_provider_append_properties(service->provider, iter);
1647 static void settings_changed(struct connman_service *service,
1648 struct connman_ipconfig *ipconfig)
1650 connman_dbus_property_changed_dict(service->path,
1651 CONNMAN_SERVICE_INTERFACE, "IPv4",
1652 append_ipv4, service);
1654 connman_dbus_property_changed_dict(service->path,
1655 CONNMAN_SERVICE_INTERFACE, "IPv6",
1656 append_ipv6, service);
1658 __connman_notifier_ipconfig_changed(service, ipconfig);
1661 static void ipv4_configuration_changed(struct connman_service *service)
1663 connman_dbus_property_changed_dict(service->path,
1664 CONNMAN_SERVICE_INTERFACE,
1665 "IPv4.Configuration",
1670 static void ipv6_configuration_changed(struct connman_service *service)
1672 connman_dbus_property_changed_dict(service->path,
1673 CONNMAN_SERVICE_INTERFACE,
1674 "IPv6.Configuration",
1679 static void dns_changed(struct connman_service *service)
1681 connman_dbus_property_changed_array(service->path,
1682 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1683 DBUS_TYPE_STRING, append_dns, service);
1686 static void dns_configuration_changed(struct connman_service *service)
1688 connman_dbus_property_changed_array(service->path,
1689 CONNMAN_SERVICE_INTERFACE,
1690 "Nameservers.Configuration",
1691 DBUS_TYPE_STRING, append_dnsconfig, service);
1693 dns_changed(service);
1696 static void domain_changed(struct connman_service *service)
1698 connman_dbus_property_changed_array(service->path,
1699 CONNMAN_SERVICE_INTERFACE, "Domains",
1700 DBUS_TYPE_STRING, append_domain, service);
1703 static void domain_configuration_changed(struct connman_service *service)
1705 connman_dbus_property_changed_array(service->path,
1706 CONNMAN_SERVICE_INTERFACE,
1707 "Domains.Configuration",
1708 DBUS_TYPE_STRING, append_domainconfig, service);
1711 static void proxy_changed(struct connman_service *service)
1713 connman_dbus_property_changed_dict(service->path,
1714 CONNMAN_SERVICE_INTERFACE, "Proxy",
1715 append_proxy, service);
1718 static void proxy_configuration_changed(struct connman_service *service)
1720 connman_dbus_property_changed_dict(service->path,
1721 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1722 append_proxyconfig, service);
1724 proxy_changed(service);
1727 static void link_changed(struct connman_service *service)
1729 connman_dbus_property_changed_dict(service->path,
1730 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1731 append_ethernet, service);
1734 static void stats_append_counters(DBusMessageIter *dict,
1735 struct connman_stats_data *stats,
1736 struct connman_stats_data *counters,
1737 connman_bool_t append_all)
1739 if (counters->rx_packets != stats->rx_packets || append_all) {
1740 counters->rx_packets = stats->rx_packets;
1741 connman_dbus_dict_append_basic(dict, "RX.Packets",
1742 DBUS_TYPE_UINT32, &stats->rx_packets);
1745 if (counters->tx_packets != stats->tx_packets || append_all) {
1746 counters->tx_packets = stats->tx_packets;
1747 connman_dbus_dict_append_basic(dict, "TX.Packets",
1748 DBUS_TYPE_UINT32, &stats->tx_packets);
1751 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1752 counters->rx_bytes = stats->rx_bytes;
1753 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1754 DBUS_TYPE_UINT32, &stats->rx_bytes);
1757 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1758 counters->tx_bytes = stats->tx_bytes;
1759 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1760 DBUS_TYPE_UINT32, &stats->tx_bytes);
1763 if (counters->rx_errors != stats->rx_errors || append_all) {
1764 counters->rx_errors = stats->rx_errors;
1765 connman_dbus_dict_append_basic(dict, "RX.Errors",
1766 DBUS_TYPE_UINT32, &stats->rx_errors);
1769 if (counters->tx_errors != stats->tx_errors || append_all) {
1770 counters->tx_errors = stats->tx_errors;
1771 connman_dbus_dict_append_basic(dict, "TX.Errors",
1772 DBUS_TYPE_UINT32, &stats->tx_errors);
1775 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1776 counters->rx_dropped = stats->rx_dropped;
1777 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1778 DBUS_TYPE_UINT32, &stats->rx_dropped);
1781 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1782 counters->tx_dropped = stats->tx_dropped;
1783 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1784 DBUS_TYPE_UINT32, &stats->tx_dropped);
1787 if (counters->time != stats->time || append_all) {
1788 counters->time = stats->time;
1789 connman_dbus_dict_append_basic(dict, "Time",
1790 DBUS_TYPE_UINT32, &stats->time);
1794 static void stats_append(struct connman_service *service,
1795 const char *counter,
1796 struct connman_stats_counter *counters,
1797 connman_bool_t append_all)
1799 DBusMessageIter array, dict;
1802 DBG("service %p counter %s", service, counter);
1804 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1808 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1809 &service->path, DBUS_TYPE_INVALID);
1811 dbus_message_iter_init_append(msg, &array);
1814 connman_dbus_dict_open(&array, &dict);
1816 stats_append_counters(&dict, &service->stats.data,
1817 &counters->stats.data, append_all);
1819 connman_dbus_dict_close(&array, &dict);
1821 /* roaming counter */
1822 connman_dbus_dict_open(&array, &dict);
1824 stats_append_counters(&dict, &service->stats_roaming.data,
1825 &counters->stats_roaming.data, append_all);
1827 connman_dbus_dict_close(&array, &dict);
1829 __connman_counter_send_usage(counter, msg);
1832 static void stats_update(struct connman_service *service,
1833 unsigned int rx_packets, unsigned int tx_packets,
1834 unsigned int rx_bytes, unsigned int tx_bytes,
1835 unsigned int rx_errors, unsigned int tx_errors,
1836 unsigned int rx_dropped, unsigned int tx_dropped)
1838 struct connman_stats *stats = stats_get(service);
1839 struct connman_stats_data *data_last = &stats->data_last;
1840 struct connman_stats_data *data = &stats->data;
1841 unsigned int seconds;
1843 DBG("service %p", service);
1845 if (stats->valid == TRUE) {
1847 rx_packets - data_last->rx_packets;
1849 tx_packets - data_last->tx_packets;
1851 rx_bytes - data_last->rx_bytes;
1853 tx_bytes - data_last->tx_bytes;
1855 rx_errors - data_last->rx_errors;
1857 tx_errors - data_last->tx_errors;
1859 rx_dropped - data_last->rx_dropped;
1861 tx_dropped - data_last->tx_dropped;
1863 stats->valid = TRUE;
1866 data_last->rx_packets = rx_packets;
1867 data_last->tx_packets = tx_packets;
1868 data_last->rx_bytes = rx_bytes;
1869 data_last->tx_bytes = tx_bytes;
1870 data_last->rx_errors = rx_errors;
1871 data_last->tx_errors = tx_errors;
1872 data_last->rx_dropped = rx_dropped;
1873 data_last->tx_dropped = tx_dropped;
1875 seconds = g_timer_elapsed(stats->timer, NULL);
1876 stats->data.time = stats->data_last.time + seconds;
1879 void __connman_service_notify(struct connman_service *service,
1880 unsigned int rx_packets, unsigned int tx_packets,
1881 unsigned int rx_bytes, unsigned int tx_bytes,
1882 unsigned int rx_errors, unsigned int tx_errors,
1883 unsigned int rx_dropped, unsigned int tx_dropped)
1885 GHashTableIter iter;
1886 gpointer key, value;
1887 const char *counter;
1888 struct connman_stats_counter *counters;
1889 struct connman_stats_data *data;
1892 if (service == NULL)
1895 if (is_connected(service) == FALSE)
1898 stats_update(service,
1899 rx_packets, tx_packets,
1901 rx_errors, tx_errors,
1902 rx_dropped, tx_dropped);
1904 data = &stats_get(service)->data;
1905 err = __connman_stats_update(service, service->roaming, data);
1907 connman_error("Failed to store statistics for %s",
1908 service->identifier);
1910 g_hash_table_iter_init(&iter, service->counter_table);
1911 while (g_hash_table_iter_next(&iter, &key, &value)) {
1915 stats_append(service, counter, counters, counters->append_all);
1916 counters->append_all = FALSE;
1920 int __connman_service_counter_register(const char *counter)
1922 struct connman_service *service;
1923 GSequenceIter *iter;
1924 struct connman_stats_counter *counters;
1926 DBG("counter %s", counter);
1928 counter_list = g_slist_append(counter_list, (gpointer)counter);
1930 iter = g_sequence_get_begin_iter(service_list);
1932 while (g_sequence_iter_is_end(iter) == FALSE) {
1933 service = g_sequence_get(iter);
1935 counters = g_try_new0(struct connman_stats_counter, 1);
1936 if (counters == NULL)
1939 counters->append_all = TRUE;
1941 g_hash_table_replace(service->counter_table, (gpointer)counter,
1944 iter = g_sequence_iter_next(iter);
1950 void __connman_service_counter_unregister(const char *counter)
1952 struct connman_service *service;
1953 GSequenceIter *iter;
1955 DBG("counter %s", counter);
1957 iter = g_sequence_get_begin_iter(service_list);
1959 while (g_sequence_iter_is_end(iter) == FALSE) {
1960 service = g_sequence_get(iter);
1962 g_hash_table_remove(service->counter_table, counter);
1964 iter = g_sequence_iter_next(iter);
1967 counter_list = g_slist_remove(counter_list, counter);
1970 GSequence *__connman_service_get_list(struct connman_session *session,
1971 service_match_cb service_match,
1972 create_service_entry_cb create_service_entry,
1973 GDestroyNotify destroy_service_entry)
1976 GSequenceIter *iter;
1977 struct connman_service *service;
1978 struct service_entry *entry;
1980 list = g_sequence_new(destroy_service_entry);
1984 iter = g_sequence_get_begin_iter(service_list);
1986 while (g_sequence_iter_is_end(iter) == FALSE) {
1987 service = g_sequence_get(iter);
1989 if (service_match(session, service) == TRUE) {
1990 entry = create_service_entry(service, service->name,
1995 g_sequence_append(list, entry);
1998 iter = g_sequence_iter_next(iter);
2004 void __connman_service_session_inc(struct connman_service *service)
2006 DBG("service %p ref count %d", service,
2007 service->session_usage_count + 1);
2009 __sync_fetch_and_add(&service->session_usage_count, 1);
2012 connman_bool_t __connman_service_session_dec(struct connman_service *service)
2014 DBG("service %p ref count %d", service,
2015 service->session_usage_count - 1);
2017 if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
2023 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2024 struct connman_service *service)
2028 str = __connman_service_type2string(service->type);
2030 connman_dbus_dict_append_basic(dict, "Type",
2031 DBUS_TYPE_STRING, &str);
2033 connman_dbus_dict_append_array(dict, "Security",
2034 DBUS_TYPE_STRING, append_security, service);
2036 str = state2string(service->state);
2038 connman_dbus_dict_append_basic(dict, "State",
2039 DBUS_TYPE_STRING, &str);
2041 str = error2string(service->error);
2043 connman_dbus_dict_append_basic(dict, "Error",
2044 DBUS_TYPE_STRING, &str);
2046 if (service->strength > 0)
2047 connman_dbus_dict_append_basic(dict, "Strength",
2048 DBUS_TYPE_BYTE, &service->strength);
2050 connman_dbus_dict_append_basic(dict, "Favorite",
2051 DBUS_TYPE_BOOLEAN, &service->favorite);
2053 connman_dbus_dict_append_basic(dict, "Immutable",
2054 DBUS_TYPE_BOOLEAN, &service->immutable);
2056 if (service->favorite == TRUE)
2057 connman_dbus_dict_append_basic(dict, "AutoConnect",
2058 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2060 connman_dbus_dict_append_basic(dict, "AutoConnect",
2061 DBUS_TYPE_BOOLEAN, &service->favorite);
2063 if (service->name != NULL)
2064 connman_dbus_dict_append_basic(dict, "Name",
2065 DBUS_TYPE_STRING, &service->name);
2067 switch (service->type) {
2068 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2069 case CONNMAN_SERVICE_TYPE_SYSTEM:
2070 case CONNMAN_SERVICE_TYPE_GPS:
2071 case CONNMAN_SERVICE_TYPE_VPN:
2072 case CONNMAN_SERVICE_TYPE_GADGET:
2074 case CONNMAN_SERVICE_TYPE_CELLULAR:
2075 connman_dbus_dict_append_basic(dict, "Roaming",
2076 DBUS_TYPE_BOOLEAN, &service->roaming);
2078 connman_dbus_dict_append_dict(dict, "Ethernet",
2079 append_ethernet, service);
2081 case CONNMAN_SERVICE_TYPE_WIFI:
2082 case CONNMAN_SERVICE_TYPE_ETHERNET:
2083 case CONNMAN_SERVICE_TYPE_WIMAX:
2084 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2085 connman_dbus_dict_append_dict(dict, "Ethernet",
2086 append_ethernet, service);
2090 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2092 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2093 append_ipv4config, service);
2095 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2097 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2098 append_ipv6config, service);
2100 connman_dbus_dict_append_array(dict, "Nameservers",
2101 DBUS_TYPE_STRING, append_dns, service);
2103 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2104 DBUS_TYPE_STRING, append_dnsconfig, service);
2106 connman_dbus_dict_append_array(dict, "Timeservers.Configuration",
2107 DBUS_TYPE_STRING, append_tsconfig, service);
2109 connman_dbus_dict_append_array(dict, "Domains",
2110 DBUS_TYPE_STRING, append_domain, service);
2112 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2113 DBUS_TYPE_STRING, append_domainconfig, service);
2115 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2117 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2118 append_proxyconfig, service);
2120 connman_dbus_dict_append_dict(dict, "Provider",
2121 append_provider, service);
2124 static void append_struct_service(DBusMessageIter *iter,
2125 connman_dbus_append_cb_t function,
2126 struct connman_service *service)
2128 DBusMessageIter entry, dict;
2130 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2132 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2135 connman_dbus_dict_open(&entry, &dict);
2136 if (function != NULL)
2137 function(&dict, service);
2138 connman_dbus_dict_close(&entry, &dict);
2140 dbus_message_iter_close_container(iter, &entry);
2143 static void append_dict_properties(DBusMessageIter *dict, void *user_data)
2145 struct connman_service *service = user_data;
2147 append_properties(dict, TRUE, service);
2150 static void append_struct(gpointer value, gpointer user_data)
2152 struct connman_service *service = value;
2153 DBusMessageIter *iter = user_data;
2155 if (service->path == NULL)
2158 append_struct_service(iter, append_dict_properties, service);
2161 void __connman_service_list_struct(DBusMessageIter *iter)
2163 g_sequence_foreach(service_list, append_struct, iter);
2166 connman_bool_t __connman_service_is_hidden(struct connman_service *service)
2168 return service->hidden;
2172 __connman_service_is_split_routing(struct connman_service *service)
2174 return service->do_split_routing;
2177 int __connman_service_get_index(struct connman_service *service)
2179 if (service == NULL)
2182 if (service->network != NULL)
2183 return connman_network_get_index(service->network);
2184 else if (service->provider != NULL)
2185 return connman_provider_get_index(service->provider);
2190 void __connman_service_set_domainname(struct connman_service *service,
2191 const char *domainname)
2193 if (service == NULL || service->hidden == TRUE)
2196 g_free(service->domainname);
2197 service->domainname = g_strdup(domainname);
2199 domain_changed(service);
2202 const char *connman_service_get_domainname(struct connman_service *service)
2204 if (service == NULL)
2207 if (service->domains != NULL)
2208 return service->domains[0];
2210 return service->domainname;
2213 char **connman_service_get_nameservers(struct connman_service *service)
2215 if (service == NULL)
2218 if (service->nameservers_config != NULL)
2219 return g_strdupv(service->nameservers_config);
2220 else if (service->nameservers != NULL ||
2221 service->nameservers_auto != NULL) {
2222 int len = 0, len_auto = 0, i;
2225 if (service->nameservers != NULL)
2226 len = g_strv_length(service->nameservers);
2227 if (service->nameservers_auto != NULL)
2228 len_auto = g_strv_length(service->nameservers_auto);
2230 nameservers = g_try_new0(char *, len + len_auto + 1);
2231 if (nameservers == NULL)
2234 for (i = 0; i < len; i++)
2235 nameservers[i] = g_strdup(service->nameservers[i]);
2237 for (i = 0; i < len_auto; i++)
2238 nameservers[i + len] =
2239 g_strdup(service->nameservers_auto[i]);
2247 char **connman_service_get_timeservers_config(struct connman_service *service)
2249 if (service == NULL)
2252 return service->timeservers_config;
2255 char **connman_service_get_timeservers(struct connman_service *service)
2257 if (service == NULL)
2260 if (service->timeservers != NULL)
2261 return service->timeservers;
2266 void connman_service_set_proxy_method(struct connman_service *service,
2267 enum connman_service_proxy_method method)
2269 if (service == NULL || service->hidden == TRUE)
2272 service->proxy = method;
2274 proxy_changed(service);
2276 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2277 __connman_notifier_proxy_changed(service);
2280 enum connman_service_proxy_method connman_service_get_proxy_method(
2281 struct connman_service *service)
2283 if (service == NULL)
2284 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2286 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2287 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2288 service->pac == NULL)
2289 return service->proxy;
2291 return service->proxy_config;
2294 return service->proxy;
2297 char **connman_service_get_proxy_servers(struct connman_service *service)
2299 return g_strdupv(service->proxies);
2302 char **connman_service_get_proxy_excludes(struct connman_service *service)
2304 return g_strdupv(service->excludes);
2307 const char *connman_service_get_proxy_url(struct connman_service *service)
2309 if (service == NULL)
2312 return service->pac;
2315 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2318 if (service == NULL || service->hidden == TRUE)
2321 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2323 if (service->ipconfig_ipv4) {
2324 if (__connman_ipconfig_set_proxy_autoconfig(
2325 service->ipconfig_ipv4, url) < 0)
2327 } else if (service->ipconfig_ipv6) {
2328 if (__connman_ipconfig_set_proxy_autoconfig(
2329 service->ipconfig_ipv6, url) < 0)
2334 proxy_changed(service);
2336 __connman_notifier_proxy_changed(service);
2339 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2341 if (service == NULL)
2344 if (service->ipconfig_ipv4)
2345 return __connman_ipconfig_get_proxy_autoconfig(
2346 service->ipconfig_ipv4);
2347 else if (service->ipconfig_ipv6)
2348 return __connman_ipconfig_get_proxy_autoconfig(
2349 service->ipconfig_ipv6);
2353 int __connman_service_timeserver_append(struct connman_service *service,
2354 const char *timeserver)
2358 DBG("service %p timeserver %s", service, timeserver);
2360 if (timeserver == NULL)
2363 if (service->timeservers != NULL) {
2366 for (i = 0; service->timeservers[i] != NULL; i++)
2367 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2370 len = g_strv_length(service->timeservers);
2371 service->timeservers = g_try_renew(char *, service->timeservers,
2375 service->timeservers = g_try_new0(char *, len + 2);
2378 if (service->timeservers == NULL)
2381 service->timeservers[len] = g_strdup(timeserver);
2382 service->timeservers[len + 1] = NULL;
2387 int __connman_service_timeserver_remove(struct connman_service *service,
2388 const char *timeserver)
2391 int len, i, j, found = 0;
2393 DBG("service %p timeserver %s", service, timeserver);
2395 if (timeserver == NULL)
2398 if (service->timeservers == NULL)
2401 for (i = 0; service->timeservers != NULL &&
2402 service->timeservers[i] != NULL; i++)
2403 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2411 len = g_strv_length(service->timeservers);
2414 g_strfreev(service->timeservers);
2415 service->timeservers = NULL;
2420 servers = g_try_new0(char *, len);
2421 if (servers == NULL)
2424 for (i = 0, j = 0; i < len; i++) {
2425 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2426 servers[j] = g_strdup(service->timeservers[i]);
2427 if (servers[j] == NULL)
2432 servers[len - 1] = NULL;
2434 g_strfreev(service->timeservers);
2435 service->timeservers = servers;
2440 void __connman_service_set_pac(struct connman_service *service,
2443 if (service->hidden == TRUE)
2445 g_free(service->pac);
2446 service->pac = g_strdup(pac);
2448 proxy_changed(service);
2451 void __connman_service_set_identity(struct connman_service *service,
2452 const char *identity)
2454 if (service->immutable || service->hidden == TRUE)
2457 g_free(service->identity);
2458 service->identity = g_strdup(identity);
2460 if (service->network != NULL)
2461 connman_network_set_string(service->network,
2466 void __connman_service_set_agent_identity(struct connman_service *service,
2467 const char *agent_identity)
2469 if (service->hidden == TRUE)
2471 g_free(service->agent_identity);
2472 service->agent_identity = g_strdup(agent_identity);
2474 if (service->network != NULL)
2475 connman_network_set_string(service->network,
2476 "WiFi.AgentIdentity",
2477 service->agent_identity);
2480 static int check_passphrase(enum connman_service_security security,
2481 const char *passphrase)
2486 if (passphrase == NULL)
2489 length = strlen(passphrase);
2492 case CONNMAN_SERVICE_SECURITY_PSK:
2493 case CONNMAN_SERVICE_SECURITY_WPA:
2494 case CONNMAN_SERVICE_SECURITY_RSN:
2495 /* A raw key is always 64 bytes length,
2496 * its content is in hex representation.
2497 * A PSK key must be between [8..63].
2500 for (i = 0; i < 64; i++)
2501 if (!isxdigit((unsigned char)
2504 } else if (length < 8 || length > 63)
2507 case CONNMAN_SERVICE_SECURITY_WEP:
2508 /* length of WEP key is 10 or 26
2509 * length of WEP passphrase is 5 or 13
2511 if (length == 10 || length == 26) {
2512 for (i = 0; i < length; i++)
2513 if (!isxdigit((unsigned char)
2516 } else if (length != 5 && length != 13)
2519 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2520 case CONNMAN_SERVICE_SECURITY_NONE:
2521 case CONNMAN_SERVICE_SECURITY_8021X:
2528 int __connman_service_set_passphrase(struct connman_service *service,
2529 const char *passphrase)
2533 if (service->immutable == TRUE || service->hidden == TRUE)
2536 err = check_passphrase(service->security, passphrase);
2539 g_free(service->passphrase);
2540 service->passphrase = g_strdup(passphrase);
2542 if (service->network != NULL)
2543 connman_network_set_string(service->network,
2545 service->passphrase);
2546 service_save(service);
2552 void __connman_service_set_agent_passphrase(struct connman_service *service,
2553 const char *agent_passphrase)
2555 if (service->hidden == TRUE)
2557 g_free(service->agent_passphrase);
2558 service->agent_passphrase = g_strdup(agent_passphrase);
2560 if (service->network != NULL)
2561 connman_network_set_string(service->network,
2562 "WiFi.AgentPassphrase",
2563 service->agent_passphrase);
2566 static DBusMessage *get_properties(DBusConnection *conn,
2567 DBusMessage *msg, void *user_data)
2569 struct connman_service *service = user_data;
2571 DBusMessageIter array, dict;
2573 DBG("service %p", service);
2575 reply = dbus_message_new_method_return(msg);
2579 dbus_message_iter_init_append(reply, &array);
2581 connman_dbus_dict_open(&array, &dict);
2582 append_properties(&dict, FALSE, service);
2583 connman_dbus_dict_close(&array, &dict);
2588 static int update_proxy_configuration(struct connman_service *service,
2589 DBusMessageIter *array)
2591 DBusMessageIter dict;
2592 enum connman_service_proxy_method method;
2593 GString *servers_str = NULL;
2594 GString *excludes_str = NULL;
2595 const char *url = NULL;
2597 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2599 dbus_message_iter_recurse(array, &dict);
2601 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2602 DBusMessageIter entry, variant;
2606 dbus_message_iter_recurse(&dict, &entry);
2608 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2611 dbus_message_iter_get_basic(&entry, &key);
2612 dbus_message_iter_next(&entry);
2614 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2617 dbus_message_iter_recurse(&entry, &variant);
2619 type = dbus_message_iter_get_arg_type(&variant);
2621 if (g_str_equal(key, "Method") == TRUE) {
2624 if (type != DBUS_TYPE_STRING)
2627 dbus_message_iter_get_basic(&variant, &val);
2628 method = string2proxymethod(val);
2629 } else if (g_str_equal(key, "URL") == TRUE) {
2630 if (type != DBUS_TYPE_STRING)
2633 dbus_message_iter_get_basic(&variant, &url);
2634 } else if (g_str_equal(key, "Servers") == TRUE) {
2635 DBusMessageIter str_array;
2637 if (type != DBUS_TYPE_ARRAY)
2640 servers_str = g_string_new(NULL);
2641 if (servers_str == NULL)
2644 dbus_message_iter_recurse(&variant, &str_array);
2646 while (dbus_message_iter_get_arg_type(&str_array) ==
2650 dbus_message_iter_get_basic(&str_array, &val);
2652 if (servers_str->len > 0)
2653 g_string_append_printf(servers_str,
2656 g_string_append(servers_str, val);
2658 dbus_message_iter_next(&str_array);
2660 } else if (g_str_equal(key, "Excludes") == TRUE) {
2661 DBusMessageIter str_array;
2663 if (type != DBUS_TYPE_ARRAY)
2666 excludes_str = g_string_new(NULL);
2667 if (excludes_str == NULL)
2670 dbus_message_iter_recurse(&variant, &str_array);
2672 while (dbus_message_iter_get_arg_type(&str_array) ==
2676 dbus_message_iter_get_basic(&str_array, &val);
2678 if (excludes_str->len > 0)
2679 g_string_append_printf(excludes_str,
2682 g_string_append(excludes_str, val);
2684 dbus_message_iter_next(&str_array);
2688 dbus_message_iter_next(&dict);
2692 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2694 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2695 if (servers_str == NULL && service->proxies == NULL)
2698 if (servers_str != NULL) {
2699 g_strfreev(service->proxies);
2701 if (servers_str->len > 0)
2702 service->proxies = g_strsplit_set(
2703 servers_str->str, " ", 0);
2705 service->proxies = NULL;
2708 if (excludes_str != NULL) {
2709 g_strfreev(service->excludes);
2711 if (excludes_str->len > 0)
2712 service->excludes = g_strsplit_set(
2713 excludes_str->str, " ", 0);
2715 service->excludes = NULL;
2718 if (service->proxies == NULL)
2719 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2722 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2723 g_free(service->pac);
2725 if (url != NULL && strlen(url) > 0)
2726 service->pac = g_strdup(url);
2728 service->pac = NULL;
2730 /* if we are connected:
2731 - if service->pac == NULL
2732 - if __connman_ipconfig_get_proxy_autoconfig(
2733 service->ipconfig) == NULL
2734 --> We should start WPAD */
2737 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2741 if (servers_str != NULL)
2742 g_string_free(servers_str, TRUE);
2744 if (excludes_str != NULL)
2745 g_string_free(excludes_str, TRUE);
2747 service->proxy_config = method;
2752 if (servers_str != NULL)
2753 g_string_free(servers_str, TRUE);
2755 if (excludes_str != NULL)
2756 g_string_free(excludes_str, TRUE);
2761 static int set_ipconfig(struct connman_service *service,
2762 struct connman_ipconfig *ipconfig,
2763 DBusMessageIter *array,
2764 enum connman_service_state state,
2765 enum connman_service_state *new_state)
2767 enum connman_ipconfig_method old_method;
2768 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2769 enum connman_ipconfig_type type;
2772 if (ipconfig == NULL)
2775 old_method = __connman_ipconfig_get_method(ipconfig);
2777 if (is_connecting_state(service, state) ||
2778 is_connected_state(service, state))
2779 __connman_network_clear_ipconfig(service->network, ipconfig);
2781 err = __connman_ipconfig_set_config(ipconfig, array);
2782 method = __connman_ipconfig_get_method(ipconfig);
2783 type = __connman_ipconfig_get_config_type(ipconfig);
2785 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2786 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2787 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2788 *new_state = service->state_ipv4 =
2789 CONNMAN_SERVICE_STATE_CONFIGURATION;
2790 __connman_ipconfig_enable(ipconfig);
2793 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2794 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2795 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2796 *new_state = service->state_ipv6;
2797 __connman_ipconfig_enable(ipconfig);
2801 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2802 type, method, state2string(*new_state));
2807 static DBusMessage *set_property(DBusConnection *conn,
2808 DBusMessage *msg, void *user_data)
2810 struct connman_service *service = user_data;
2811 DBusMessageIter iter, value;
2815 DBG("service %p", service);
2817 if (dbus_message_iter_init(msg, &iter) == FALSE)
2818 return __connman_error_invalid_arguments(msg);
2820 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
2821 return __connman_error_invalid_arguments(msg);
2823 dbus_message_iter_get_basic(&iter, &name);
2824 dbus_message_iter_next(&iter);
2826 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
2827 return __connman_error_invalid_arguments(msg);
2829 dbus_message_iter_recurse(&iter, &value);
2831 type = dbus_message_iter_get_arg_type(&value);
2833 if (g_str_equal(name, "AutoConnect") == TRUE) {
2834 connman_bool_t autoconnect;
2836 if (type != DBUS_TYPE_BOOLEAN)
2837 return __connman_error_invalid_arguments(msg);
2839 if (service->favorite == FALSE)
2840 return __connman_error_invalid_service(msg);
2842 dbus_message_iter_get_basic(&value, &autoconnect);
2844 if (service->autoconnect == autoconnect)
2845 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2847 service->autoconnect = autoconnect;
2849 autoconnect_changed(service);
2851 service_save(service);
2852 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2853 DBusMessageIter entry;
2858 if (type != DBUS_TYPE_ARRAY)
2859 return __connman_error_invalid_arguments(msg);
2861 str = g_string_new(NULL);
2863 return __connman_error_invalid_arguments(msg);
2865 index = connman_network_get_index(service->network);
2866 gw = __connman_ipconfig_get_gateway_from_index(index);
2868 if (gw && strlen(gw))
2869 __connman_service_nameserver_del_routes(service,
2870 CONNMAN_IPCONFIG_TYPE_ALL);
2872 dbus_message_iter_recurse(&value, &entry);
2874 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2876 dbus_message_iter_get_basic(&entry, &val);
2877 dbus_message_iter_next(&entry);
2879 g_string_append_printf(str, " %s", val);
2881 g_string_append(str, val);
2884 g_strfreev(service->nameservers_config);
2887 service->nameservers_config =
2888 g_strsplit_set(str->str, " ", 0);
2890 service->nameservers_config = NULL;
2893 g_string_free(str, TRUE);
2895 if (gw && strlen(gw))
2896 __connman_service_nameserver_add_routes(service, gw);
2898 update_nameservers(service);
2899 dns_configuration_changed(service);
2901 service_save(service);
2902 } else if (g_str_equal(name, "Timeservers.Configuration") == TRUE) {
2903 DBusMessageIter entry;
2904 GSList *list = NULL;
2907 if (type != DBUS_TYPE_ARRAY)
2908 return __connman_error_invalid_arguments(msg);
2910 dbus_message_iter_recurse(&value, &entry);
2912 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2914 dbus_message_iter_get_basic(&entry, &val);
2916 list = g_slist_prepend(list, strdup(val));
2919 dbus_message_iter_next(&entry);
2922 g_strfreev(service->timeservers_config);
2923 service->timeservers_config = NULL;
2926 service->timeservers_config = g_new0(char *, count+1);
2928 while (list != NULL) {
2930 service->timeservers_config[count] = list->data;
2931 list = g_slist_delete_link(list, list);
2935 service_save(service);
2937 __connman_timeserver_sync(service);
2938 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2939 DBusMessageIter entry;
2942 if (type != DBUS_TYPE_ARRAY)
2943 return __connman_error_invalid_arguments(msg);
2945 str = g_string_new(NULL);
2947 return __connman_error_invalid_arguments(msg);
2949 dbus_message_iter_recurse(&value, &entry);
2951 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2953 dbus_message_iter_get_basic(&entry, &val);
2954 dbus_message_iter_next(&entry);
2956 g_string_append_printf(str, " %s", val);
2958 g_string_append(str, val);
2961 g_strfreev(service->domains);
2964 service->domains = g_strsplit_set(str->str, " ", 0);
2966 service->domains = NULL;
2968 g_string_free(str, TRUE);
2970 update_nameservers(service);
2971 domain_configuration_changed(service);
2973 service_save(service);
2974 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2977 if (type != DBUS_TYPE_ARRAY)
2978 return __connman_error_invalid_arguments(msg);
2980 err = update_proxy_configuration(service, &value);
2983 return __connman_error_failed(msg, -err);
2985 proxy_configuration_changed(service);
2987 __connman_notifier_proxy_changed(service);
2989 service_save(service);
2990 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2991 g_str_equal(name, "IPv6.Configuration")) {
2993 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2994 enum connman_service_state state =
2995 CONNMAN_SERVICE_STATE_UNKNOWN;
3000 if (service->ipconfig_ipv4 == NULL &&
3001 service->ipconfig_ipv6 == NULL)
3002 return __connman_error_invalid_property(msg);
3004 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
3005 ipv4 = service->ipconfig_ipv4;
3006 err = set_ipconfig(service, ipv4, &value,
3007 service->state_ipv4, &state);
3009 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
3010 ipv6 = service->ipconfig_ipv6;
3011 err = set_ipconfig(service, ipv6, &value,
3012 service->state_ipv6, &state);
3016 if (is_connected_state(service, state) ||
3017 is_connecting_state(service, state))
3018 __connman_network_set_ipconfig(service->network,
3020 return __connman_error_failed(msg, -err);
3024 ipv4_configuration_changed(service);
3026 ipv6_configuration_changed(service);
3028 if (is_connecting(service) || is_connected(service))
3029 __connman_network_set_ipconfig(service->network,
3032 service_save(service);
3034 return __connman_error_invalid_property(msg);
3036 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3039 static void set_idle(struct connman_service *service)
3041 service->state = service->state_ipv4 = service->state_ipv6 =
3042 CONNMAN_SERVICE_STATE_IDLE;
3043 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3044 state_changed(service);
3047 static DBusMessage *clear_property(DBusConnection *conn,
3048 DBusMessage *msg, void *user_data)
3050 struct connman_service *service = user_data;
3053 DBG("service %p", service);
3055 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
3058 if (g_str_equal(name, "Error") == TRUE) {
3061 g_get_current_time(&service->modified);
3062 service_save(service);
3064 return __connman_error_invalid_property(msg);
3066 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3069 static connman_bool_t is_ignore(struct connman_service *service)
3071 if (service->autoconnect == FALSE)
3074 if (service->roaming == TRUE)
3077 if (service->ignore == TRUE)
3080 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3086 struct preferred_tech_data {
3087 GSequence *preferred_list;
3088 enum connman_service_type type;
3091 static void preferred_tech_add_by_type(gpointer data, gpointer user_data)
3093 struct connman_service *service = data;
3094 struct preferred_tech_data *tech_data = user_data;
3096 if (service->type == tech_data->type) {
3097 g_sequence_append(tech_data->preferred_list, service);
3099 DBG("type %d service %p %s", tech_data->type, service,
3104 static GSequence* preferred_tech_list_get(GSequence *list)
3106 unsigned int *tech_array;
3107 struct preferred_tech_data tech_data;
3110 tech_array = connman_setting_get_uint_list("PreferredTechnologies");
3111 if (tech_array == NULL)
3114 tech_data.preferred_list = g_sequence_new(NULL);
3116 for (i = 0; tech_array[i] != 0; i += 1) {
3117 tech_data.type = tech_array[i];
3118 g_sequence_foreach(service_list, preferred_tech_add_by_type,
3122 return tech_data.preferred_list;
3125 static connman_bool_t auto_connect_service(GSequenceIter* iter,
3126 connman_bool_t preferred)
3128 struct connman_service *service = NULL;
3130 while (g_sequence_iter_is_end(iter) == FALSE) {
3131 service = g_sequence_get(iter);
3133 if (service->pending != NULL)
3136 if (is_connecting(service) == TRUE)
3139 if (service->favorite == FALSE) {
3140 if (preferred == TRUE)
3145 if (is_connected(service) == TRUE) {
3146 if (preferred == TRUE && service->state !=
3147 CONNMAN_SERVICE_STATE_ONLINE)
3152 if (is_ignore(service) == FALSE && service->state ==
3153 CONNMAN_SERVICE_STATE_IDLE)
3159 iter = g_sequence_iter_next(iter);
3162 if (service != NULL) {
3164 DBG("service %p %s %s", service, service->name,
3165 (preferred == TRUE)? "preferred": "auto");
3167 service->userconnect = FALSE;
3168 __connman_service_connect(service);
3174 static gboolean run_auto_connect(gpointer data)
3176 GSequenceIter *iter = NULL;
3177 GSequence *preferred_tech;
3179 autoconnect_timeout = 0;
3183 preferred_tech = preferred_tech_list_get(service_list);
3184 if (preferred_tech != NULL)
3185 iter = g_sequence_get_begin_iter(preferred_tech);
3187 if (iter == NULL || auto_connect_service(iter, TRUE) == FALSE)
3188 iter = g_sequence_get_begin_iter(service_list);
3191 auto_connect_service(iter, FALSE);
3193 if (preferred_tech != NULL)
3194 g_sequence_free(preferred_tech);
3199 void __connman_service_auto_connect(void)
3203 if (__connman_session_mode() == TRUE) {
3204 DBG("Session mode enabled: auto connect disabled");
3208 if (autoconnect_timeout != 0)
3211 autoconnect_timeout = g_timeout_add_seconds(0, run_auto_connect, NULL);
3214 static void remove_timeout(struct connman_service *service)
3216 if (service->timeout > 0) {
3217 g_source_remove(service->timeout);
3218 service->timeout = 0;
3222 static void reply_pending(struct connman_service *service, int error)
3224 remove_timeout(service);
3226 if (service->pending != NULL) {
3230 reply = __connman_error_failed(service->pending,
3233 g_dbus_send_message(connection, reply);
3237 sender = dbus_message_get_interface(service->pending);
3239 DBG("sender %s", sender);
3241 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
3242 g_dbus_send_reply(connection, service->pending,
3243 DBUS_TYPE_OBJECT_PATH, &service->path,
3246 g_dbus_send_reply(connection, service->pending,
3250 dbus_message_unref(service->pending);
3251 service->pending = NULL;
3255 static gboolean connect_timeout(gpointer user_data)
3257 struct connman_service *service = user_data;
3258 connman_bool_t autoconnect = FALSE;
3260 DBG("service %p", service);
3262 service->timeout = 0;
3264 if (service->network != NULL)
3265 __connman_network_disconnect(service->network);
3267 __connman_ipconfig_disable(service->ipconfig_ipv4);
3268 __connman_ipconfig_disable(service->ipconfig_ipv6);
3270 __connman_stats_service_unregister(service);
3272 if (service->pending != NULL) {
3275 reply = __connman_error_operation_timeout(service->pending);
3277 g_dbus_send_message(connection, reply);
3279 dbus_message_unref(service->pending);
3280 service->pending = NULL;
3284 __connman_service_ipconfig_indicate_state(service,
3285 CONNMAN_SERVICE_STATE_FAILURE,
3286 CONNMAN_IPCONFIG_TYPE_IPV4);
3287 __connman_service_ipconfig_indicate_state(service,
3288 CONNMAN_SERVICE_STATE_FAILURE,
3289 CONNMAN_IPCONFIG_TYPE_IPV6);
3291 if (autoconnect == TRUE && service->userconnect == FALSE)
3292 __connman_service_auto_connect();
3297 static void set_reconnect_state(struct connman_service *service,
3298 connman_bool_t reconnect)
3300 struct connman_device *device;
3302 if (service->network == NULL)
3305 device = connman_network_get_device(service->network);
3309 __connman_device_set_reconnect(device, reconnect);
3312 static connman_bool_t get_reconnect_state(struct connman_service *service)
3314 struct connman_device *device;
3316 if (service->network == NULL)
3319 device = connman_network_get_device(service->network);
3323 return __connman_device_get_reconnect(device);
3326 static DBusMessage *connect_service(DBusConnection *conn,
3327 DBusMessage *msg, void *user_data)
3329 struct connman_service *service = user_data;
3330 GSequenceIter *iter;
3333 DBG("service %p", service);
3335 if (service->pending != NULL)
3336 return __connman_error_in_progress(msg);
3338 iter = g_sequence_get_begin_iter(service_list);
3340 while (g_sequence_iter_is_end(iter) == FALSE) {
3341 struct connman_service *temp = g_sequence_get(iter);
3343 if (service->type == temp->type && is_connecting(temp) == TRUE)
3344 return __connman_error_in_progress(msg);
3346 iter = g_sequence_iter_next(iter);
3349 service->ignore = FALSE;
3351 service->userconnect = TRUE;
3353 service->pending = dbus_message_ref(msg);
3355 set_reconnect_state(service, FALSE);
3357 err = __connman_service_connect(service);
3359 if (service->pending == NULL)
3362 if (err != -EINPROGRESS) {
3363 dbus_message_unref(service->pending);
3364 service->pending = NULL;
3366 return __connman_error_failed(msg, -err);
3372 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3375 static DBusMessage *disconnect_service(DBusConnection *conn,
3376 DBusMessage *msg, void *user_data)
3378 struct connman_service *service = user_data;
3381 DBG("service %p", service);
3383 reply_pending(service, ECONNABORTED);
3385 service->ignore = TRUE;
3387 set_reconnect_state(service, FALSE);
3389 err = __connman_service_disconnect(service);
3391 if (err != -EINPROGRESS)
3392 return __connman_error_failed(msg, -err);
3397 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3400 static DBusMessage *remove_service(DBusConnection *conn,
3401 DBusMessage *msg, void *user_data)
3403 struct connman_service *service = user_data;
3405 DBG("service %p", service);
3407 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3408 return __connman_error_not_supported(msg);
3410 if (service->immutable == TRUE || service->hidden == TRUE)
3411 return __connman_error_not_supported(msg);
3413 if (service->favorite == FALSE && service->state !=
3414 CONNMAN_SERVICE_STATE_FAILURE)
3415 return __connman_error_not_supported(msg);
3417 set_reconnect_state(service, FALSE);
3419 __connman_service_disconnect(service);
3421 g_free(service->passphrase);
3422 service->passphrase = NULL;
3426 __connman_service_set_favorite(service, FALSE);
3427 service_save(service);
3429 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3432 static gboolean check_suitable_state(enum connman_service_state a,
3433 enum connman_service_state b)
3436 * Special check so that "ready" service can be moved before
3439 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3440 b == CONNMAN_SERVICE_STATE_READY) ||
3441 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3442 a == CONNMAN_SERVICE_STATE_READY))
3448 static void downgrade_state(struct connman_service *service)
3450 if (service == NULL)
3453 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3454 service->state_ipv6);
3456 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3457 __connman_service_ipconfig_indicate_state(service,
3458 CONNMAN_SERVICE_STATE_READY,
3459 CONNMAN_IPCONFIG_TYPE_IPV4);
3461 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3462 __connman_service_ipconfig_indicate_state(service,
3463 CONNMAN_SERVICE_STATE_READY,
3464 CONNMAN_IPCONFIG_TYPE_IPV6);
3467 static void apply_relevant_default_downgrade(struct connman_service *service)
3469 struct connman_service *def_service;
3471 def_service = __connman_service_get_default();
3472 if (def_service == NULL)
3475 if (def_service == service &&
3476 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3477 def_service->state = CONNMAN_SERVICE_STATE_READY;
3480 static void switch_default_service(struct connman_service *default_service,
3481 struct connman_service *downgrade_service)
3483 GSequenceIter *src, *dst;
3485 apply_relevant_default_downgrade(default_service);
3486 src = g_hash_table_lookup(service_hash, downgrade_service->identifier);
3487 dst = g_hash_table_lookup(service_hash, default_service->identifier);
3488 g_sequence_move(src, dst);
3489 downgrade_state(downgrade_service);
3492 static DBusMessage *move_service(DBusConnection *conn,
3493 DBusMessage *msg, void *user_data,
3496 struct connman_service *service = user_data;
3497 struct connman_service *target;
3499 enum connman_ipconfig_method target4, target6;
3500 enum connman_ipconfig_method service4, service6;
3502 DBG("service %p", service);
3504 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3507 if (service->favorite == FALSE)
3508 return __connman_error_not_supported(msg);
3510 target = find_service(path);
3511 if (target == NULL || target->favorite == FALSE || target == service)
3512 return __connman_error_invalid_service(msg);
3514 if (target->type == CONNMAN_SERVICE_TYPE_VPN) {
3516 * We only allow VPN route splitting if there are
3517 * routes defined for a given VPN.
3519 if (__connman_provider_check_routes(target->provider)
3521 connman_info("Cannot move service. "
3522 "No routes defined for provider %s",
3523 __connman_provider_get_ident(target->provider));
3524 return __connman_error_invalid_service(msg);
3527 target->do_split_routing = TRUE;
3529 target->do_split_routing = FALSE;
3531 service->do_split_routing = FALSE;
3533 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3534 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3535 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3536 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3538 DBG("target %s method %d/%d state %d/%d split %d", target->identifier,
3539 target4, target6, target->state_ipv4, target->state_ipv6,
3540 target->do_split_routing);
3542 DBG("service %s method %d/%d state %d/%d", service->identifier,
3544 service->state_ipv4, service->state_ipv6);
3547 * If method is OFF, then we do not need to check the corresponding
3550 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3551 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3552 if (check_suitable_state(target->state_ipv6,
3553 service->state_ipv6) == FALSE)
3554 return __connman_error_invalid_service(msg);
3558 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3559 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3560 if (check_suitable_state(target->state_ipv4,
3561 service->state_ipv4) == FALSE)
3562 return __connman_error_invalid_service(msg);
3566 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3567 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3568 if (check_suitable_state(target->state_ipv6,
3569 service->state_ipv6) == FALSE)
3570 return __connman_error_invalid_service(msg);
3574 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3575 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3576 if (check_suitable_state(target->state_ipv4,
3577 service->state_ipv4) == FALSE)
3578 return __connman_error_invalid_service(msg);
3582 g_get_current_time(&service->modified);
3583 service_save(service);
3584 service_save(target);
3587 * If the service which goes down is the default service and is
3588 * online, we downgrade directly its state to ready so:
3589 * the service which goes up, needs to recompute its state which
3590 * is triggered via downgrading it - if relevant - to state ready.
3593 switch_default_service(target, service);
3595 switch_default_service(service, target);
3597 __connman_connection_update_gateway();
3599 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3602 static DBusMessage *move_before(DBusConnection *conn,
3603 DBusMessage *msg, void *user_data)
3605 return move_service(conn, msg, user_data, TRUE);
3608 static DBusMessage *move_after(DBusConnection *conn,
3609 DBusMessage *msg, void *user_data)
3611 return move_service(conn, msg, user_data, FALSE);
3614 static DBusMessage *reset_counters(DBusConnection *conn,
3615 DBusMessage *msg, void *user_data)
3617 struct connman_service *service = user_data;
3619 reset_stats(service);
3621 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3624 static struct _services_notify {
3630 static void service_append_added_foreach(gpointer data, gpointer user_data)
3632 struct connman_service *service = data;
3633 DBusMessageIter *iter = user_data;
3635 if (service == NULL || service->path == NULL) {
3636 DBG("service %p or path is NULL", service);
3640 if (g_hash_table_lookup(services_notify->add, service->path) != NULL) {
3641 DBG("new %s", service->path);
3643 append_struct(service, iter);
3644 g_hash_table_remove(services_notify->add, service->path);
3646 DBG("changed %s", service->path);
3648 append_struct_service(iter, NULL, service);
3652 static void service_append_ordered(DBusMessageIter *iter, void *user_data)
3654 if (service_list != NULL)
3655 g_sequence_foreach(service_list,
3656 service_append_added_foreach, iter);
3659 static void append_removed(gpointer key, gpointer value, gpointer user_data)
3661 char *objpath = key;
3662 DBusMessageIter *iter = user_data;
3664 DBG("removed %s", objpath);
3665 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath);
3668 static gboolean service_send_changed(gpointer data)
3670 DBusMessage *signal;
3671 DBusMessageIter iter, array;
3675 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
3676 CONNMAN_MANAGER_INTERFACE, "ServicesChanged");
3680 __connman_dbus_append_objpath_dict_array(signal,
3681 service_append_ordered, NULL);
3683 dbus_message_iter_init_append(signal, &iter);
3684 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
3685 DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
3687 g_hash_table_foreach(services_notify->remove, append_removed, &array);
3689 dbus_message_iter_close_container(&iter, &array);
3691 dbus_connection_send(connection, signal, NULL);
3692 dbus_message_unref(signal);
3694 g_hash_table_remove_all(services_notify->remove);
3695 g_hash_table_remove_all(services_notify->add);
3697 services_notify->id = 0;
3701 static void service_schedule_changed(void)
3703 if (services_notify->id != 0)
3706 services_notify->id = g_timeout_add(100, service_send_changed, NULL);
3709 static void service_schedule_added(struct connman_service *service)
3711 DBG("service %p", service);
3713 g_hash_table_remove(services_notify->remove, service->path);
3714 g_hash_table_replace(services_notify->add, service->path, service);
3716 service_schedule_changed();
3719 static void service_schedule_removed(struct connman_service *service)
3721 DBG("service %p %s", service, service->path);
3723 if (service == NULL || service->path == NULL) {
3724 DBG("service %p or path is NULL", service);
3728 g_hash_table_remove(services_notify->add, service->path);
3729 g_hash_table_replace(services_notify->remove, g_strdup(service->path),
3732 service_schedule_changed();
3735 static GDBusMethodTable service_methods[] = {
3736 { "GetProperties", "", "a{sv}", get_properties },
3737 { "SetProperty", "sv", "", set_property },
3738 { "ClearProperty", "s", "", clear_property },
3739 { "Connect", "", "", connect_service,
3740 G_DBUS_METHOD_FLAG_ASYNC },
3741 { "Disconnect", "", "", disconnect_service },
3742 { "Remove", "", "", remove_service },
3743 { "MoveBefore", "o", "", move_before },
3744 { "MoveAfter", "o", "", move_after },
3745 { "ResetCounters", "", "", reset_counters },
3749 static GDBusSignalTable service_signals[] = {
3750 { "PropertyChanged", "sv" },
3754 static void service_free(gpointer user_data)
3756 struct connman_service *service = user_data;
3757 char *path = service->path;
3759 DBG("service %p", service);
3761 reply_pending(service, ENOENT);
3763 g_hash_table_remove(service_hash, service->identifier);
3765 __connman_notifier_service_remove(service);
3766 service_schedule_removed(service);
3768 stats_stop(service);
3770 service->path = NULL;
3773 __connman_connection_update_gateway();
3775 g_dbus_unregister_interface(connection, path,
3776 CONNMAN_SERVICE_INTERFACE);
3780 g_hash_table_destroy(service->counter_table);
3782 if (service->network != NULL) {
3783 __connman_network_disconnect(service->network);
3784 connman_network_unref(service->network);
3785 service->network = NULL;
3788 if (service->provider != NULL)
3789 connman_provider_unref(service->provider);
3791 if (service->ipconfig_ipv4 != NULL) {
3792 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3793 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3794 __connman_ipconfig_unref(service->ipconfig_ipv4);
3795 service->ipconfig_ipv4 = NULL;
3798 if (service->ipconfig_ipv6 != NULL) {
3799 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3800 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3801 __connman_ipconfig_unref(service->ipconfig_ipv6);
3802 service->ipconfig_ipv6 = NULL;
3805 g_strfreev(service->timeservers);
3806 g_strfreev(service->timeservers_config);
3807 g_strfreev(service->nameservers);
3808 g_strfreev(service->nameservers_config);
3809 g_strfreev(service->nameservers_auto);
3810 g_strfreev(service->domains);
3811 g_strfreev(service->proxies);
3812 g_strfreev(service->excludes);
3814 g_free(service->domainname);
3815 g_free(service->pac);
3816 g_free(service->name);
3817 g_free(service->passphrase);
3818 g_free(service->agent_passphrase);
3819 g_free(service->identifier);
3820 g_free(service->eap);
3821 g_free(service->identity);
3822 g_free(service->agent_identity);
3823 g_free(service->ca_cert_file);
3824 g_free(service->client_cert_file);
3825 g_free(service->private_key_file);
3826 g_free(service->private_key_passphrase);
3827 g_free(service->phase2);
3829 if (service->stats.timer != NULL)
3830 g_timer_destroy(service->stats.timer);
3831 if (service->stats_roaming.timer != NULL)
3832 g_timer_destroy(service->stats_roaming.timer);
3837 static void stats_init(struct connman_service *service)
3840 service->stats.valid = FALSE;
3841 service->stats.enabled = FALSE;
3842 service->stats.timer = g_timer_new();
3845 service->stats_roaming.valid = FALSE;
3846 service->stats_roaming.enabled = FALSE;
3847 service->stats_roaming.timer = g_timer_new();
3850 static void service_initialize(struct connman_service *service)
3852 DBG("service %p", service);
3854 service->refcount = 1;
3855 service->session_usage_count = 0;
3857 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3858 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3860 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3861 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3862 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3864 service->favorite = FALSE;
3865 service->immutable = FALSE;
3866 service->hidden = FALSE;
3868 service->ignore = FALSE;
3870 service->userconnect = FALSE;
3874 stats_init(service);
3876 service->provider = NULL;
3878 service->wps = FALSE;
3882 * connman_service_create:
3884 * Allocate a new service.
3886 * Returns: a newly-allocated #connman_service structure
3888 struct connman_service *connman_service_create(void)
3891 struct connman_stats_counter *counters;
3892 const char *counter;
3894 struct connman_service *service;
3896 service = g_try_new0(struct connman_service, 1);
3897 if (service == NULL)
3900 DBG("service %p", service);
3902 service->counter_table = g_hash_table_new_full(g_str_hash,
3903 g_str_equal, NULL, g_free);
3905 for (list = counter_list; list; list = list->next) {
3906 counter = list->data;
3908 counters = g_try_new0(struct connman_stats_counter, 1);
3909 if (counters == NULL) {
3910 g_hash_table_destroy(service->counter_table);
3915 counters->append_all = TRUE;
3917 g_hash_table_replace(service->counter_table, (gpointer)counter,
3921 service_initialize(service);
3927 * connman_service_ref:
3928 * @service: service structure
3930 * Increase reference counter of service
3932 struct connman_service *
3933 connman_service_ref_debug(struct connman_service *service,
3934 const char *file, int line, const char *caller)
3936 DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
3937 file, line, caller);
3939 __sync_fetch_and_add(&service->refcount, 1);
3945 * connman_service_unref:
3946 * @service: service structure
3948 * Decrease reference counter of service and release service if no
3951 void connman_service_unref_debug(struct connman_service *service,
3952 const char *file, int line, const char *caller)
3954 GSequenceIter *iter;
3956 DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
3957 file, line, caller);
3959 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
3962 iter = g_hash_table_lookup(service_hash, service->identifier);
3964 reply_pending(service, ECONNABORTED);
3966 __connman_service_disconnect(service);
3968 g_sequence_remove(iter);
3970 service_free(service);
3974 static gint service_compare(gconstpointer a, gconstpointer b,
3977 struct connman_service *service_a = (void *) a;
3978 struct connman_service *service_b = (void *) b;
3979 enum connman_service_state state_a, state_b;
3981 state_a = service_a->state;
3982 state_b = service_b->state;
3984 if (state_a != state_b) {
3985 gboolean a_connected = is_connected(service_a);
3986 gboolean b_connected = is_connected(service_b);
3988 if (a_connected == TRUE && b_connected == TRUE) {
3989 /* We prefer online over ready state */
3990 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3993 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3997 if (a_connected == TRUE)
3999 if (b_connected == TRUE)
4002 if (is_connecting(service_a) == TRUE)
4004 if (is_connecting(service_b) == TRUE)
4008 if (service_a->order > service_b->order)
4011 if (service_a->order < service_b->order)
4014 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
4017 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
4020 if (service_a->type != service_b->type) {
4021 switch (service_a->type) {
4022 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4023 case CONNMAN_SERVICE_TYPE_SYSTEM:
4024 case CONNMAN_SERVICE_TYPE_ETHERNET:
4025 case CONNMAN_SERVICE_TYPE_GPS:
4026 case CONNMAN_SERVICE_TYPE_VPN:
4027 case CONNMAN_SERVICE_TYPE_GADGET:
4029 case CONNMAN_SERVICE_TYPE_WIFI:
4031 case CONNMAN_SERVICE_TYPE_WIMAX:
4032 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4033 case CONNMAN_SERVICE_TYPE_CELLULAR:
4038 return (gint) service_b->strength - (gint) service_a->strength;
4042 * connman_service_get_type:
4043 * @service: service structure
4045 * Get the type of service
4047 enum connman_service_type connman_service_get_type(struct connman_service *service)
4049 if (service == NULL)
4050 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4052 return service->type;
4056 * connman_service_get_interface:
4057 * @service: service structure
4059 * Get network interface of service
4061 char *connman_service_get_interface(struct connman_service *service)
4065 if (service == NULL)
4068 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
4069 if (service->ipconfig_ipv4)
4070 index = __connman_ipconfig_get_index(
4071 service->ipconfig_ipv4);
4072 else if (service->ipconfig_ipv6)
4073 index = __connman_ipconfig_get_index(
4074 service->ipconfig_ipv6);
4078 return connman_inet_ifname(index);
4081 if (service->network == NULL)
4084 index = connman_network_get_index(service->network);
4086 return connman_inet_ifname(index);
4090 * connman_service_get_network:
4091 * @service: service structure
4093 * Get the service network
4095 struct connman_network *
4096 __connman_service_get_network(struct connman_service *service)
4098 if (service == NULL)
4101 return service->network;
4104 struct connman_ipconfig *
4105 __connman_service_get_ip4config(struct connman_service *service)
4107 if (service == NULL)
4110 return service->ipconfig_ipv4;
4113 struct connman_ipconfig *
4114 __connman_service_get_ip6config(struct connman_service *service)
4116 if (service == NULL)
4119 return service->ipconfig_ipv6;
4122 struct connman_ipconfig *
4123 __connman_service_get_ipconfig(struct connman_service *service, int family)
4125 if (family == AF_INET)
4126 return __connman_service_get_ip4config(service);
4127 else if (family == AF_INET6)
4128 return __connman_service_get_ip6config(service);
4134 connman_bool_t __connman_service_is_connected_state(struct connman_service *service,
4135 enum connman_ipconfig_type type)
4137 if (service == NULL)
4141 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
4143 case CONNMAN_IPCONFIG_TYPE_IPV4:
4144 return is_connected_state(service, service->state_ipv4);
4145 case CONNMAN_IPCONFIG_TYPE_IPV6:
4146 return is_connected_state(service, service->state_ipv6);
4151 enum connman_service_security __connman_service_get_security(struct connman_service *service)
4153 if (service == NULL)
4154 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4156 return service->security;
4159 const char *__connman_service_get_phase2(struct connman_service *service)
4161 if (service == NULL)
4164 return service->phase2;
4167 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
4169 if (service == NULL)
4172 return service->wps;
4176 * __connman_service_set_favorite:
4177 * @service: service structure
4178 * @favorite: favorite value
4180 * Change the favorite setting of service
4182 int __connman_service_set_favorite(struct connman_service *service,
4183 connman_bool_t favorite)
4185 GSequenceIter *iter;
4187 if (service->hidden == TRUE)
4189 iter = g_hash_table_lookup(service_hash, service->identifier);
4193 if (service->favorite == favorite)
4196 service->favorite = favorite;
4197 service->order = __connman_service_get_order(service);
4199 favorite_changed(service);
4201 g_sequence_sort_changed(iter, service_compare, NULL);
4202 service_schedule_changed();
4204 __connman_connection_update_gateway();
4209 int __connman_service_set_immutable(struct connman_service *service,
4210 connman_bool_t immutable)
4212 if (service->hidden == TRUE)
4214 service->immutable = immutable;
4216 immutable_changed(service);
4221 void __connman_service_set_string(struct connman_service *service,
4222 const char *key, const char *value)
4224 if (service->hidden == TRUE)
4226 if (g_str_equal(key, "EAP") == TRUE) {
4227 g_free(service->eap);
4228 service->eap = g_strdup(value);
4229 } else if (g_str_equal(key, "Identity") == TRUE) {
4230 g_free(service->identity);
4231 service->identity = g_strdup(value);
4232 } else if (g_str_equal(key, "CACertFile") == TRUE) {
4233 g_free(service->ca_cert_file);
4234 service->ca_cert_file = g_strdup(value);
4235 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
4236 g_free(service->client_cert_file);
4237 service->client_cert_file = g_strdup(value);
4238 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
4239 g_free(service->private_key_file);
4240 service->private_key_file = g_strdup(value);
4241 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
4242 g_free(service->private_key_passphrase);
4243 service->private_key_passphrase = g_strdup(value);
4244 } else if (g_str_equal(key, "Phase2") == TRUE) {
4245 g_free(service->phase2);
4246 service->phase2 = g_strdup(value);
4247 } else if (g_str_equal(key, "Passphrase") == TRUE) {
4248 g_free(service->passphrase);
4249 service->passphrase = g_strdup(value);
4253 static void service_complete(struct connman_service *service)
4255 reply_pending(service, EIO);
4257 if (service->userconnect == FALSE)
4258 __connman_service_auto_connect();
4260 g_get_current_time(&service->modified);
4261 service_save(service);
4264 static void report_error_cb(struct connman_service *service,
4265 gboolean retry, void *user_data)
4268 __connman_service_connect(service);
4270 service_complete(service);
4271 __connman_connection_update_gateway();
4272 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4276 int __connman_service_add_passphrase(struct connman_service *service,
4277 const gchar *passphrase)
4281 switch (service->security) {
4282 case CONNMAN_SERVICE_SECURITY_WEP:
4283 case CONNMAN_SERVICE_SECURITY_PSK:
4284 err = __connman_service_set_passphrase(service, passphrase);
4286 case CONNMAN_SERVICE_SECURITY_8021X:
4287 __connman_service_set_agent_passphrase(service,
4290 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4291 case CONNMAN_SERVICE_SECURITY_NONE:
4292 case CONNMAN_SERVICE_SECURITY_WPA:
4293 case CONNMAN_SERVICE_SECURITY_RSN:
4294 DBG("service security '%s' (%d) not handled",
4295 security2string(service->security),
4303 static int check_wpspin(const char *wpspin)
4310 /* A WPS PIN is always 8 chars length,
4311 * its content is in digit representation.
4313 if (strlen(wpspin) != 8)
4316 for (i = 0; i < 8; i++)
4317 if (!isdigit((unsigned char) wpspin[i]))
4323 static void request_input_cb (struct connman_service *service,
4324 connman_bool_t values_received,
4325 const char *name, int name_len,
4326 const char *identity, const char *passphrase,
4327 gboolean wps, const char *wpspin,
4330 struct connman_device *device;
4333 DBG ("RequestInput return, %p", service);
4335 if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
4336 device = connman_network_get_device(service->network);
4337 __connman_device_request_hidden_scan(device,
4339 identity, passphrase);
4342 if (values_received == FALSE || service->hidden == TRUE) {
4343 service_complete(service);
4344 __connman_connection_update_gateway();
4345 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4349 err = check_wpspin(wpspin);
4352 if (service->network != NULL) {
4353 connman_network_set_bool(service->network, "Wifi.UseWPS", wps);
4354 connman_network_set_string(service->network, "Wifi.PinWPS",
4358 if (identity != NULL)
4359 __connman_service_set_agent_identity(service, identity);
4361 if (passphrase != NULL)
4362 err = __connman_service_add_passphrase(service, passphrase);
4366 __connman_service_connect(service);
4368 /* Never cache agent provided credentials */
4369 __connman_service_set_agent_identity(service, NULL);
4370 __connman_service_set_agent_passphrase(service, NULL);
4371 } else if (err == -ENOKEY) {
4372 __connman_service_indicate_error(service,
4373 CONNMAN_SERVICE_ERROR_INVALID_KEY);
4374 __connman_agent_report_error(service,
4375 error2string(service->error),
4376 report_error_cb, NULL);
4380 static void downgrade_connected_services(void)
4382 struct connman_service *up_service;
4383 GSequenceIter *iter;
4385 iter = g_sequence_get_begin_iter(service_list);
4386 while (g_sequence_iter_is_end(iter) == FALSE) {
4387 up_service = g_sequence_get(iter);
4389 if (is_connected(up_service) == FALSE) {
4390 iter = g_sequence_iter_next(iter);
4394 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4397 downgrade_state(up_service);
4399 iter = g_sequence_iter_next(iter);
4403 static int service_update_preferred_order(struct connman_service *default_service,
4404 struct connman_service *new_service,
4405 enum connman_service_state new_state)
4407 unsigned int *tech_array;
4410 if (default_service == NULL || default_service == new_service ||
4411 default_service->state != new_state )
4414 tech_array = connman_setting_get_uint_list("PreferredTechnologies");
4415 if (tech_array != NULL) {
4417 for (i = 0; tech_array[i] != 0; i += 1) {
4418 if (default_service->type == tech_array[i])
4421 if (new_service->type == tech_array[i]) {
4422 switch_default_service(new_service,
4433 static int service_indicate_state(struct connman_service *service)
4435 enum connman_service_state old_state, new_state;
4436 struct connman_service *def_service;
4438 GSequenceIter *iter;
4440 if (service == NULL)
4443 old_state = service->state;
4444 new_state = combine_state(service->state_ipv4, service->state_ipv6);
4446 DBG("service %p old %s - new %s/%s => %s",
4448 state2string(old_state),
4449 state2string(service->state_ipv4),
4450 state2string(service->state_ipv6),
4451 state2string(new_state));
4453 if (old_state == new_state)
4456 def_service = __connman_service_get_default();
4458 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4459 result = service_update_preferred_order(def_service,
4460 service, new_state);
4461 if (result == -EALREADY)
4463 if (result == -EAGAIN)
4464 __connman_service_auto_connect();
4467 service->state = new_state;
4468 state_changed(service);
4470 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4471 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4472 reply_pending(service, ECONNABORTED);
4474 __connman_service_disconnect(service);
4477 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4478 if (__connman_stats_service_register(service) == 0) {
4479 __connman_stats_get(service, FALSE,
4480 &service->stats.data);
4481 __connman_stats_get(service, TRUE,
4482 &service->stats_roaming.data);
4486 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4487 connman_bool_t reconnect;
4489 reconnect = get_reconnect_state(service);
4490 if (reconnect == TRUE)
4491 __connman_service_auto_connect();
4493 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4496 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4497 enum connman_ipconfig_method method;
4499 service_update_preferred_order(def_service, service, new_state);
4501 set_reconnect_state(service, TRUE);
4503 __connman_service_set_favorite(service, TRUE);
4505 reply_pending(service, 0);
4507 service->userconnect = FALSE;
4509 g_get_current_time(&service->modified);
4510 service_save(service);
4512 update_nameservers(service);
4513 dns_changed(service);
4514 domain_changed(service);
4516 __connman_notifier_connect(service->type);
4518 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4519 connman_network_get_bool(service->network,
4520 "WiFi.UseWPS") == TRUE) {
4523 pass = connman_network_get_string(service->network,
4526 __connman_service_set_passphrase(service, pass);
4528 connman_network_set_bool(service->network,
4529 "WiFi.UseWPS", FALSE);
4534 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4535 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4536 __connman_ipconfig_disable_ipv6(
4537 service->ipconfig_ipv6);
4539 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4540 def_service = __connman_service_get_default();
4542 if (__connman_notifier_count_connected() == 0 &&
4543 def_service != NULL &&
4544 def_service->provider != NULL)
4545 __connman_provider_disconnect(def_service->provider);
4549 __connman_wispr_stop(service);
4551 __connman_wpad_stop(service);
4553 update_nameservers(service);
4554 dns_changed(service);
4555 domain_changed(service);
4557 __connman_notifier_disconnect(service->type, old_state);
4560 * Previous services which are connected and which states
4561 * are set to online should reset relevantly ipconfig_state
4562 * to ready so wispr/portal will be rerun on those
4564 downgrade_connected_services();
4567 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4568 if (service->userconnect == TRUE &&
4569 __connman_agent_report_error(service,
4570 error2string(service->error),
4571 report_error_cb, NULL) == -EIO)
4573 service_complete(service);
4575 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4577 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4579 iter = g_hash_table_lookup(service_hash, service->identifier);
4581 g_sequence_sort_changed(iter, service_compare, NULL);
4582 service_schedule_changed();
4585 __connman_connection_update_gateway();
4587 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4593 int __connman_service_indicate_error(struct connman_service *service,
4594 enum connman_service_error error)
4596 DBG("service %p error %d", service, error);
4598 if (service == NULL)
4601 service->error = error;
4603 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4604 __connman_service_set_passphrase(service, NULL);
4606 __connman_service_ipconfig_indicate_state(service,
4607 CONNMAN_SERVICE_STATE_FAILURE,
4608 CONNMAN_IPCONFIG_TYPE_IPV4);
4609 __connman_service_ipconfig_indicate_state(service,
4610 CONNMAN_SERVICE_STATE_FAILURE,
4611 CONNMAN_IPCONFIG_TYPE_IPV6);
4615 int __connman_service_clear_error(struct connman_service *service)
4617 DBG("service %p", service);
4619 if (service == NULL)
4622 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4625 service->state_ipv4 = service->state_ipv6 =
4626 CONNMAN_SERVICE_STATE_UNKNOWN;
4627 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4629 if (service->favorite == TRUE)
4630 set_reconnect_state(service, TRUE);
4632 __connman_service_ipconfig_indicate_state(service,
4633 CONNMAN_SERVICE_STATE_IDLE,
4634 CONNMAN_IPCONFIG_TYPE_IPV6);
4637 * Toggling the IPv6 state to IDLE could trigger the auto connect
4638 * machinery and consequently the IPv4 state.
4640 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4641 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4644 return __connman_service_ipconfig_indicate_state(service,
4645 CONNMAN_SERVICE_STATE_IDLE,
4646 CONNMAN_IPCONFIG_TYPE_IPV4);
4649 int __connman_service_indicate_default(struct connman_service *service)
4651 struct connman_service *current = __connman_service_get_default();
4653 DBG("service %p default %p", service, current);
4655 if (current == service)
4663 enum connman_service_state __connman_service_ipconfig_get_state(
4664 struct connman_service *service,
4665 enum connman_ipconfig_type type)
4667 if (service == NULL)
4668 return CONNMAN_SERVICE_STATE_UNKNOWN;
4670 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4671 return service->state_ipv4;
4673 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4674 return service->state_ipv6;
4676 return CONNMAN_SERVICE_STATE_UNKNOWN;
4679 static void check_proxy_setup(struct connman_service *service)
4682 * We start WPAD if we haven't got a PAC URL from DHCP and
4683 * if our proxy manual configuration is either empty or set
4684 * to AUTO with an empty URL.
4687 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4690 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4691 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4692 service->pac != NULL))
4695 if (__connman_wpad_start(service) < 0) {
4696 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4697 __connman_notifier_proxy_changed(service);
4704 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4708 * How many networks are connected at the same time. If more than 1,
4709 * then set the rp_filter setting properly (loose mode routing) so that network
4710 * connectivity works ok. This is only done for IPv4 networks as IPv6
4711 * does not have rp_filter knob.
4713 static int connected_networks_count;
4714 static int original_rp_filter;
4716 static void service_rp_filter(struct connman_service *service,
4719 enum connman_ipconfig_method method;
4721 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4724 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4725 case CONNMAN_IPCONFIG_METHOD_OFF:
4726 case CONNMAN_IPCONFIG_METHOD_AUTO:
4728 case CONNMAN_IPCONFIG_METHOD_FIXED:
4729 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4730 case CONNMAN_IPCONFIG_METHOD_DHCP:
4734 if (connected == TRUE) {
4735 if (connected_networks_count == 1) {
4737 filter_value = __connman_ipconfig_set_rp_filter();
4738 if (filter_value < 0)
4741 original_rp_filter = filter_value;
4743 connected_networks_count++;
4746 if (connected_networks_count == 2)
4747 __connman_ipconfig_unset_rp_filter(original_rp_filter);
4749 connected_networks_count--;
4750 if (connected_networks_count < 0)
4751 connected_networks_count = 0;
4754 DBG("%s %s ipconfig %p method %d count %d filter %d",
4755 connected ? "connected" : "disconnected", service->identifier,
4756 service->ipconfig_ipv4, method,
4757 connected_networks_count, original_rp_filter);
4760 static gboolean redo_wispr(gpointer user_data)
4762 struct connman_service *service = user_data;
4766 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
4771 int __connman_service_online_check_failed(struct connman_service *service,
4772 enum connman_ipconfig_type type)
4774 DBG("service %p type %d count %d", service, type,
4775 service->online_check_count);
4777 /* currently we only retry IPv6 stuff */
4778 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 ||
4779 service->online_check_count != 1) {
4780 __connman_service_auto_connect();
4784 service->online_check_count = 0;
4787 * We set the timeout to 1 sec so that we have a chance to get
4788 * necessary IPv6 router advertisement messages that might have
4791 g_timeout_add_seconds(1, redo_wispr, service);
4796 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4797 enum connman_service_state new_state,
4798 enum connman_ipconfig_type type)
4800 struct connman_ipconfig *ipconfig = NULL;
4801 enum connman_service_state old_state;
4804 if (service == NULL)
4807 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4808 old_state = service->state_ipv4;
4809 ipconfig = service->ipconfig_ipv4;
4810 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4811 old_state = service->state_ipv6;
4812 ipconfig = service->ipconfig_ipv6;
4815 if (ipconfig == NULL)
4819 if (old_state == new_state)
4822 DBG("service %p (%s) state %d (%s) type %d (%s)",
4823 service, service ? service->identifier : NULL,
4824 new_state, state2string(new_state),
4825 type, __connman_ipconfig_type2string(type));
4827 switch (new_state) {
4828 case CONNMAN_SERVICE_STATE_UNKNOWN:
4829 case CONNMAN_SERVICE_STATE_IDLE:
4830 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4833 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4835 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4836 __connman_ipconfig_enable(ipconfig);
4838 case CONNMAN_SERVICE_STATE_READY:
4839 update_nameservers(service);
4841 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4842 check_proxy_setup(service);
4843 service_rp_filter(service, TRUE);
4845 service->online_check_count = 1;
4846 __connman_wispr_start(service, type);
4849 case CONNMAN_SERVICE_STATE_ONLINE:
4851 case CONNMAN_SERVICE_STATE_DISCONNECT:
4852 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4855 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4856 service_rp_filter(service, FALSE);
4859 case CONNMAN_SERVICE_STATE_FAILURE:
4863 /* We keep that state */
4864 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4865 service->state_ipv4 = new_state;
4866 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4867 service->state_ipv6 = new_state;
4869 ret = service_indicate_state(service);
4872 * If the ipconfig method is OFF, then we set the state to IDLE
4873 * so that it will not affect the combined state in the future.
4875 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4876 enum connman_ipconfig_method method;
4877 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4878 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4879 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4880 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4881 ret = service_indicate_state(service);
4884 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4885 enum connman_ipconfig_method method;
4886 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4887 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4888 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4889 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4890 ret = service_indicate_state(service);
4897 static connman_bool_t prepare_network(struct connman_service *service)
4899 enum connman_network_type type;
4900 unsigned int ssid_len;
4902 type = connman_network_get_type(service->network);
4905 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4906 case CONNMAN_NETWORK_TYPE_VENDOR:
4908 case CONNMAN_NETWORK_TYPE_WIFI:
4909 if (connman_network_get_blob(service->network, "WiFi.SSID",
4913 if (service->passphrase != NULL)
4914 connman_network_set_string(service->network,
4915 "WiFi.Passphrase", service->passphrase);
4917 case CONNMAN_NETWORK_TYPE_ETHERNET:
4918 case CONNMAN_NETWORK_TYPE_WIMAX:
4919 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4920 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4921 case CONNMAN_NETWORK_TYPE_CELLULAR:
4928 static void prepare_8021x(struct connman_service *service)
4930 if (service->eap != NULL)
4931 connman_network_set_string(service->network, "WiFi.EAP",
4934 if (service->identity != NULL)
4935 connman_network_set_string(service->network, "WiFi.Identity",
4938 if (service->ca_cert_file != NULL)
4939 connman_network_set_string(service->network, "WiFi.CACertFile",
4940 service->ca_cert_file);
4942 if (service->client_cert_file != NULL)
4943 connman_network_set_string(service->network,
4944 "WiFi.ClientCertFile",
4945 service->client_cert_file);
4947 if (service->private_key_file != NULL)
4948 connman_network_set_string(service->network,
4949 "WiFi.PrivateKeyFile",
4950 service->private_key_file);
4952 if (service->private_key_passphrase != NULL)
4953 connman_network_set_string(service->network,
4954 "WiFi.PrivateKeyPassphrase",
4955 service->private_key_passphrase);
4957 if (service->phase2 != NULL)
4958 connman_network_set_string(service->network, "WiFi.Phase2",
4962 static int service_connect(struct connman_service *service)
4966 if (service->hidden == TRUE)
4969 switch (service->type) {
4970 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4971 case CONNMAN_SERVICE_TYPE_SYSTEM:
4972 case CONNMAN_SERVICE_TYPE_GPS:
4973 case CONNMAN_SERVICE_TYPE_GADGET:
4975 case CONNMAN_SERVICE_TYPE_ETHERNET:
4976 case CONNMAN_SERVICE_TYPE_WIMAX:
4977 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4978 case CONNMAN_SERVICE_TYPE_CELLULAR:
4979 case CONNMAN_SERVICE_TYPE_VPN:
4981 case CONNMAN_SERVICE_TYPE_WIFI:
4982 switch (service->security) {
4983 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4984 case CONNMAN_SERVICE_SECURITY_NONE:
4986 case CONNMAN_SERVICE_SECURITY_WEP:
4987 case CONNMAN_SERVICE_SECURITY_PSK:
4988 case CONNMAN_SERVICE_SECURITY_WPA:
4989 case CONNMAN_SERVICE_SECURITY_RSN:
4990 if (service->passphrase == NULL) {
4991 if (service->network == NULL)
4994 if (service->wps == FALSE ||
4995 connman_network_get_bool(
4997 "WiFi.UseWPS") == FALSE)
5001 case CONNMAN_SERVICE_SECURITY_8021X:
5002 if (service->eap == NULL)
5006 * never request credentials if using EAP-TLS
5007 * (EAP-TLS networks need to be fully provisioned)
5009 if (g_str_equal(service->eap, "tls") == TRUE)
5013 * Return -ENOKEY if either identity or passphrase is
5014 * missing. Agent provided credentials can be used as
5015 * fallback if needed.
5017 if ((service->identity == NULL &&
5018 service->agent_identity == NULL) ||
5019 (service->passphrase == NULL &&
5020 service->agent_passphrase == NULL))
5028 if (service->network != NULL) {
5029 if (prepare_network(service) == FALSE)
5032 switch (service->security) {
5033 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5034 case CONNMAN_SERVICE_SECURITY_NONE:
5035 case CONNMAN_SERVICE_SECURITY_WEP:
5036 case CONNMAN_SERVICE_SECURITY_PSK:
5037 case CONNMAN_SERVICE_SECURITY_WPA:
5038 case CONNMAN_SERVICE_SECURITY_RSN:
5040 case CONNMAN_SERVICE_SECURITY_8021X:
5041 prepare_8021x(service);
5045 if (__connman_stats_service_register(service) == 0) {
5046 __connman_stats_get(service, FALSE,
5047 &service->stats.data);
5048 __connman_stats_get(service, TRUE,
5049 &service->stats_roaming.data);
5052 if (service->ipconfig_ipv4)
5053 __connman_ipconfig_enable(service->ipconfig_ipv4);
5054 if (service->ipconfig_ipv6)
5055 __connman_ipconfig_enable(service->ipconfig_ipv6);
5057 err = __connman_network_connect(service->network);
5058 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5059 service->provider != NULL)
5060 err = __connman_provider_connect(service->provider);
5065 if (err != -EINPROGRESS) {
5066 __connman_ipconfig_disable(service->ipconfig_ipv4);
5067 __connman_ipconfig_disable(service->ipconfig_ipv6);
5068 __connman_stats_service_unregister(service);
5076 int __connman_service_connect(struct connman_service *service)
5080 DBG("service %p state %s", service, state2string(service->state));
5082 if (is_connected(service) == TRUE)
5085 if (is_connecting(service) == TRUE)
5088 switch (service->type) {
5089 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5090 case CONNMAN_SERVICE_TYPE_SYSTEM:
5091 case CONNMAN_SERVICE_TYPE_GPS:
5092 case CONNMAN_SERVICE_TYPE_GADGET:
5095 err = service_connect(service);
5101 if (err == -EINPROGRESS) {
5102 if (service->timeout == 0)
5103 service->timeout = g_timeout_add_seconds(
5104 CONNECT_TIMEOUT, connect_timeout, service);
5106 return -EINPROGRESS;
5109 __connman_service_ipconfig_indicate_state(service,
5110 CONNMAN_SERVICE_STATE_FAILURE,
5111 CONNMAN_IPCONFIG_TYPE_IPV4);
5112 __connman_service_ipconfig_indicate_state(service,
5113 CONNMAN_SERVICE_STATE_FAILURE,
5114 CONNMAN_IPCONFIG_TYPE_IPV6);
5116 if (service->network != NULL)
5117 __connman_network_disconnect(service->network);
5118 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5119 service->provider != NULL)
5120 __connman_provider_disconnect(service->provider);
5122 if (service->userconnect == TRUE) {
5123 if (err == -ENOKEY || err == -EPERM) {
5124 if (__connman_agent_request_passphrase_input(service,
5127 return -EINPROGRESS;
5129 reply_pending(service, -err);
5135 int __connman_service_disconnect(struct connman_service *service)
5139 DBG("service %p", service);
5141 if (service->network != NULL) {
5142 err = __connman_network_disconnect(service->network);
5143 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5144 service->provider != NULL)
5145 err = __connman_provider_disconnect(service->provider);
5149 if (err < 0 && err != -EINPROGRESS)
5152 __connman_6to4_remove(service->ipconfig_ipv4);
5154 if (service->ipconfig_ipv4)
5155 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
5158 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
5161 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
5162 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
5164 __connman_ipconfig_disable(service->ipconfig_ipv4);
5165 __connman_ipconfig_disable(service->ipconfig_ipv6);
5167 __connman_stats_service_unregister(service);
5172 int __connman_service_disconnect_all(void)
5174 GSequenceIter *iter;
5175 GSList *services = NULL, *list;
5179 iter = g_sequence_get_begin_iter(service_list);
5181 while (g_sequence_iter_is_end(iter) == FALSE) {
5182 struct connman_service *service = g_sequence_get(iter);
5184 services = g_slist_prepend(services, service);
5186 iter = g_sequence_iter_next(iter);
5189 for (list = services; list != NULL; list = list->next) {
5190 struct connman_service *service = list->data;
5192 service->ignore = TRUE;
5194 set_reconnect_state(service, FALSE);
5196 __connman_service_disconnect(service);
5206 * lookup_by_identifier:
5207 * @identifier: service identifier
5209 * Look up a service by identifier (reference count will not be increased)
5211 static struct connman_service *lookup_by_identifier(const char *identifier)
5213 GSequenceIter *iter;
5215 iter = g_hash_table_lookup(service_hash, identifier);
5217 return g_sequence_get(iter);
5222 static void provision_changed(gpointer value, gpointer user_data)
5224 struct connman_service *service = value;
5225 char *path = user_data;
5227 __connman_config_provision_service_ident(service, path);
5230 void __connman_service_provision_changed(const char *ident)
5232 g_sequence_foreach(service_list, provision_changed, (void *)ident);
5236 * __connman_service_get:
5237 * @identifier: service identifier
5239 * Look up a service by identifier or create a new one if not found
5241 static struct connman_service *service_get(const char *identifier)
5243 struct connman_service *service;
5244 GSequenceIter *iter;
5246 iter = g_hash_table_lookup(service_hash, identifier);
5248 service = g_sequence_get(iter);
5249 if (service != NULL)
5250 connman_service_ref(service);
5254 service = connman_service_create();
5255 if (service == NULL)
5258 DBG("service %p", service);
5260 service->identifier = g_strdup(identifier);
5262 iter = g_sequence_insert_sorted(service_list, service,
5263 service_compare, NULL);
5265 g_hash_table_insert(service_hash, service->identifier, iter);
5270 static int service_register(struct connman_service *service)
5272 GSequenceIter *iter;
5274 DBG("service %p", service);
5276 if (service->path != NULL)
5279 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
5280 service->identifier);
5282 DBG("path %s", service->path);
5284 __connman_config_provision_service(service);
5286 service_load(service);
5288 g_dbus_register_interface(connection, service->path,
5289 CONNMAN_SERVICE_INTERFACE,
5290 service_methods, service_signals,
5291 NULL, service, NULL);
5293 iter = g_hash_table_lookup(service_hash, service->identifier);
5295 g_sequence_sort_changed(iter, service_compare, NULL);
5296 service_schedule_changed();
5299 __connman_connection_update_gateway();
5304 static void service_up(struct connman_ipconfig *ipconfig)
5306 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5308 DBG("%s up", __connman_ipconfig_get_ifname(ipconfig));
5310 link_changed(service);
5312 service->stats.valid = FALSE;
5313 service->stats_roaming.valid = FALSE;
5316 static void service_down(struct connman_ipconfig *ipconfig)
5318 DBG("%s down", __connman_ipconfig_get_ifname(ipconfig));
5321 static void service_lower_up(struct connman_ipconfig *ipconfig)
5323 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5325 DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig));
5327 stats_start(service);
5330 static void service_lower_down(struct connman_ipconfig *ipconfig)
5332 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5334 DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig));
5336 if (is_idle_state(service, service->state_ipv4) == FALSE)
5337 __connman_ipconfig_disable(service->ipconfig_ipv4);
5339 if (is_idle_state(service, service->state_ipv6) == FALSE)
5340 __connman_ipconfig_disable(service->ipconfig_ipv6);
5342 stats_stop(service);
5343 service_save(service);
5346 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5348 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5349 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5350 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5352 DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig));
5354 type = __connman_ipconfig_get_config_type(ipconfig);
5355 method = __connman_ipconfig_get_method(ipconfig);
5357 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5360 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5361 method == CONNMAN_IPCONFIG_METHOD_AUTO)
5362 __connman_service_ipconfig_indicate_state(service,
5363 CONNMAN_SERVICE_STATE_READY,
5364 CONNMAN_IPCONFIG_TYPE_IPV6);
5366 settings_changed(service, ipconfig);
5369 static void service_ip_release(struct connman_ipconfig *ipconfig)
5371 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5372 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5373 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5375 DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig));
5377 type = __connman_ipconfig_get_config_type(ipconfig);
5378 method = __connman_ipconfig_get_method(ipconfig);
5380 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5383 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5384 method == CONNMAN_IPCONFIG_METHOD_OFF)
5385 __connman_service_ipconfig_indicate_state(service,
5386 CONNMAN_SERVICE_STATE_DISCONNECT,
5387 CONNMAN_IPCONFIG_TYPE_IPV6);
5389 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5390 method == CONNMAN_IPCONFIG_METHOD_OFF)
5391 __connman_service_ipconfig_indicate_state(service,
5392 CONNMAN_SERVICE_STATE_DISCONNECT,
5393 CONNMAN_IPCONFIG_TYPE_IPV4);
5395 settings_changed(service, ipconfig);
5398 static const struct connman_ipconfig_ops service_ops = {
5400 .down = service_down,
5401 .lower_up = service_lower_up,
5402 .lower_down = service_lower_down,
5403 .ip_bound = service_ip_bound,
5404 .ip_release = service_ip_release,
5407 static void setup_ip4config(struct connman_service *service, int index,
5408 enum connman_ipconfig_method method)
5410 service->ipconfig_ipv4 = __connman_ipconfig_create(index,
5411 CONNMAN_IPCONFIG_TYPE_IPV4);
5412 if (service->ipconfig_ipv4 == NULL)
5415 __connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5417 __connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5419 __connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5422 static void setup_ip6config(struct connman_service *service, int index)
5424 service->ipconfig_ipv6 = __connman_ipconfig_create(index,
5425 CONNMAN_IPCONFIG_TYPE_IPV6);
5426 if (service->ipconfig_ipv6 == NULL)
5429 __connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5431 __connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5434 void __connman_service_read_ip4config(struct connman_service *service)
5438 if (service->ipconfig_ipv4 == NULL)
5441 keyfile = connman_storage_load_service(service->identifier);
5442 if (keyfile == NULL)
5445 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5446 service->identifier, "IPv4.");
5448 g_key_file_free(keyfile);
5451 void __connman_service_create_ip4config(struct connman_service *service,
5454 DBG("ipv4 %p", service->ipconfig_ipv4);
5456 if (service->ipconfig_ipv4 != NULL)
5459 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5460 __connman_service_read_ip4config(service);
5463 void __connman_service_read_ip6config(struct connman_service *service)
5467 if (service->ipconfig_ipv6 == NULL)
5470 keyfile = connman_storage_load_service(service->identifier);
5471 if (keyfile == NULL)
5474 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5475 service->identifier, "IPv6.");
5477 g_key_file_free(keyfile);
5480 void __connman_service_create_ip6config(struct connman_service *service,
5483 DBG("ipv6 %p", service->ipconfig_ipv6);
5485 if (service->ipconfig_ipv6 != NULL)
5488 setup_ip6config(service, index);
5490 __connman_service_read_ip6config(service);
5494 * __connman_service_lookup_from_network:
5495 * @network: network structure
5497 * Look up a service by network (reference count will not be increased)
5499 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5501 struct connman_service *service;
5502 const char *ident, *group;
5505 DBG("network %p", network);
5507 if (network == NULL)
5510 ident = __connman_network_get_ident(network);
5514 group = connman_network_get_group(network);
5518 name = g_strdup_printf("%s_%s_%s",
5519 __connman_network_get_type(network), ident, group);
5520 service = lookup_by_identifier(name);
5526 struct connman_service *__connman_service_lookup_from_index(int index)
5528 struct connman_service *service;
5529 GSequenceIter *iter;
5531 iter = g_sequence_get_begin_iter(service_list);
5533 while (g_sequence_iter_is_end(iter) == FALSE) {
5534 service = g_sequence_get(iter);
5536 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
5540 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
5544 iter = g_sequence_iter_next(iter);
5550 const char *__connman_service_get_ident(struct connman_service *service)
5552 return service->identifier;
5555 const char *__connman_service_get_path(struct connman_service *service)
5557 return service->path;
5560 unsigned int __connman_service_get_order(struct connman_service *service)
5562 GSequenceIter *iter;
5564 if (service == NULL)
5567 if (service->favorite == FALSE) {
5572 iter = g_hash_table_lookup(service_hash, service->identifier);
5574 if (g_sequence_iter_get_position(iter) == 0)
5576 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5577 service->do_split_routing == FALSE)
5578 service->order = 10;
5583 DBG("service %p name %s order %d split %d", service, service->name,
5584 service->order, service->do_split_routing);
5587 return service->order;
5590 void __connman_service_update_ordering(void)
5592 GSequenceIter *iter;
5594 iter = g_sequence_get_begin_iter(service_list);
5596 g_sequence_sort_changed(iter, service_compare, NULL);
5599 static enum connman_service_type convert_network_type(struct connman_network *network)
5601 enum connman_network_type type = connman_network_get_type(network);
5604 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5605 case CONNMAN_NETWORK_TYPE_VENDOR:
5607 case CONNMAN_NETWORK_TYPE_ETHERNET:
5608 return CONNMAN_SERVICE_TYPE_ETHERNET;
5609 case CONNMAN_NETWORK_TYPE_WIFI:
5610 return CONNMAN_SERVICE_TYPE_WIFI;
5611 case CONNMAN_NETWORK_TYPE_WIMAX:
5612 return CONNMAN_SERVICE_TYPE_WIMAX;
5613 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5614 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5615 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5616 case CONNMAN_NETWORK_TYPE_CELLULAR:
5617 return CONNMAN_SERVICE_TYPE_CELLULAR;
5620 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5623 static enum connman_service_security convert_wifi_security(const char *security)
5625 if (security == NULL)
5626 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5627 else if (g_str_equal(security, "none") == TRUE)
5628 return CONNMAN_SERVICE_SECURITY_NONE;
5629 else if (g_str_equal(security, "wep") == TRUE)
5630 return CONNMAN_SERVICE_SECURITY_WEP;
5631 else if (g_str_equal(security, "psk") == TRUE)
5632 return CONNMAN_SERVICE_SECURITY_PSK;
5633 else if (g_str_equal(security, "ieee8021x") == TRUE)
5634 return CONNMAN_SERVICE_SECURITY_8021X;
5635 else if (g_str_equal(security, "wpa") == TRUE)
5636 return CONNMAN_SERVICE_SECURITY_WPA;
5637 else if (g_str_equal(security, "rsn") == TRUE)
5638 return CONNMAN_SERVICE_SECURITY_RSN;
5640 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5643 static void update_from_network(struct connman_service *service,
5644 struct connman_network *network)
5646 connman_uint8_t strength = service->strength;
5647 GSequenceIter *iter;
5650 DBG("service %p network %p", service, network);
5652 if (is_connected(service) == TRUE)
5655 if (is_connecting(service) == TRUE)
5658 str = connman_network_get_string(network, "Name");
5660 g_free(service->name);
5661 service->name = g_strdup(str);
5662 service->hidden = FALSE;
5664 g_free(service->name);
5665 service->name = NULL;
5666 service->hidden = TRUE;
5669 service->strength = connman_network_get_strength(network);
5670 service->roaming = connman_network_get_bool(network, "Roaming");
5672 if (service->strength == 0) {
5674 * Filter out 0-values; it's unclear what they mean
5675 * and they cause anomalous sorting of the priority list.
5677 service->strength = strength;
5680 str = connman_network_get_string(network, "WiFi.Security");
5681 service->security = convert_wifi_security(str);
5683 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5684 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5686 if (service->strength > strength && service->network != NULL) {
5687 connman_network_unref(service->network);
5688 service->network = connman_network_ref(network);
5690 strength_changed(service);
5693 if (service->network == NULL)
5694 service->network = connman_network_ref(network);
5696 iter = g_hash_table_lookup(service_hash, service->identifier);
5698 g_sequence_sort_changed(iter, service_compare, NULL);
5699 service_schedule_changed();
5704 * __connman_service_create_from_network:
5705 * @network: network structure
5707 * Look up service by network and if not found, create one
5709 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5711 struct connman_service *service;
5712 struct connman_device *device;
5713 const char *ident, *group;
5715 unsigned int *auto_connect_types;
5718 DBG("network %p", network);
5720 if (network == NULL)
5723 ident = __connman_network_get_ident(network);
5727 group = connman_network_get_group(network);
5731 name = g_strdup_printf("%s_%s_%s",
5732 __connman_network_get_type(network), ident, group);
5733 service = service_get(name);
5736 if (service == NULL)
5739 if (__connman_network_get_weakness(network) == TRUE)
5742 if (service->path != NULL) {
5743 update_from_network(service, network);
5744 __connman_connection_update_gateway();
5748 service->type = convert_network_type(network);
5750 auto_connect_types = connman_setting_get_uint_list("DefaultAutoConnectTechnologies");
5751 service->autoconnect = FALSE;
5752 for (i = 0; auto_connect_types != NULL &&
5753 auto_connect_types[i] != 0; i++) {
5754 if (service->type == auto_connect_types[i]) {
5755 service->autoconnect = TRUE;
5760 switch (service->type) {
5761 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5762 case CONNMAN_SERVICE_TYPE_SYSTEM:
5763 case CONNMAN_SERVICE_TYPE_WIMAX:
5764 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5765 case CONNMAN_SERVICE_TYPE_GPS:
5766 case CONNMAN_SERVICE_TYPE_VPN:
5767 case CONNMAN_SERVICE_TYPE_GADGET:
5768 case CONNMAN_SERVICE_TYPE_WIFI:
5769 case CONNMAN_SERVICE_TYPE_CELLULAR:
5771 case CONNMAN_SERVICE_TYPE_ETHERNET:
5772 service->favorite = TRUE;
5776 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5777 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5779 update_from_network(service, network);
5781 index = connman_network_get_index(network);
5783 if (service->ipconfig_ipv4 == NULL)
5784 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5786 if (service->ipconfig_ipv6 == NULL)
5787 setup_ip6config(service, index);
5789 service_register(service);
5791 if (service->favorite == TRUE) {
5792 device = connman_network_get_device(service->network);
5793 if (device && __connman_device_scanning(device) == FALSE)
5794 __connman_service_auto_connect();
5797 __connman_notifier_service_add(service, service->name);
5798 service_schedule_added(service);
5803 void __connman_service_update_from_network(struct connman_network *network)
5805 connman_bool_t need_sort = FALSE;
5806 struct connman_service *service;
5807 connman_uint8_t strength;
5808 connman_bool_t roaming;
5809 GSequenceIter *iter;
5811 connman_bool_t stats_enable;
5813 DBG("network %p", network);
5815 service = __connman_service_lookup_from_network(network);
5816 if (service == NULL)
5819 if (service->network == NULL)
5822 name = connman_network_get_string(service->network, "Name");
5823 if (g_strcmp0(service->name, name) != 0) {
5824 g_free(service->name);
5825 service->name = g_strdup(name);
5826 connman_dbus_property_changed_basic(service->path,
5827 CONNMAN_SERVICE_INTERFACE, "Name",
5828 DBUS_TYPE_STRING, &service->name);
5831 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5832 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5834 strength = connman_network_get_strength(service->network);
5835 if (strength == service->strength)
5838 service->strength = strength;
5841 strength_changed(service);
5844 roaming = connman_network_get_bool(service->network, "Roaming");
5845 if (roaming == service->roaming)
5848 stats_enable = stats_enabled(service);
5849 if (stats_enable == TRUE)
5850 stats_stop(service);
5852 service->roaming = roaming;
5855 if (stats_enable == TRUE)
5856 stats_start(service);
5858 roaming_changed(service);
5861 if (need_sort == TRUE) {
5862 iter = g_hash_table_lookup(service_hash, service->identifier);
5864 g_sequence_sort_changed(iter, service_compare, NULL);
5865 service_schedule_changed();
5870 void __connman_service_remove_from_network(struct connman_network *network)
5872 struct connman_service *service;
5874 DBG("network %p", network);
5876 service = __connman_service_lookup_from_network(network);
5877 if (service == NULL)
5880 service->ignore = TRUE;
5882 __connman_connection_gateway_remove(service,
5883 CONNMAN_IPCONFIG_TYPE_ALL);
5885 connman_service_unref(service);
5889 * __connman_service_create_from_provider:
5890 * @provider: provider structure
5892 * Look up service by provider and if not found, create one
5894 struct connman_service *
5895 __connman_service_create_from_provider(struct connman_provider *provider)
5897 struct connman_service *service;
5898 const char *ident, *str;
5900 int index = connman_provider_get_index(provider);
5902 DBG("provider %p", provider);
5904 ident = __connman_provider_get_ident(provider);
5908 name = g_strdup_printf("vpn_%s", ident);
5909 service = service_get(name);
5912 if (service == NULL)
5915 service->type = CONNMAN_SERVICE_TYPE_VPN;
5916 service->provider = connman_provider_ref(provider);
5917 service->autoconnect = FALSE;
5918 service->userconnect = TRUE;
5920 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5921 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5923 str = connman_provider_get_string(provider, "Name");
5925 g_free(service->name);
5926 service->name = g_strdup(str);
5927 service->hidden = FALSE;
5929 g_free(service->name);
5930 service->name = NULL;
5931 service->hidden = TRUE;
5934 service->strength = 0;
5936 if (service->ipconfig_ipv4 == NULL)
5937 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5939 if (service->ipconfig_ipv6 == NULL)
5940 setup_ip6config(service, index);
5942 service_register(service);
5944 __connman_notifier_service_add(service, service->name);
5945 service_schedule_added(service);
5950 int __connman_service_init(void)
5954 connection = connman_dbus_get_connection();
5956 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5959 service_list = g_sequence_new(service_free);
5961 services_notify = g_new0(struct _services_notify, 1);
5962 services_notify->remove = g_hash_table_new_full(g_str_hash,
5963 g_str_equal, g_free, NULL);
5964 services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
5969 void __connman_service_cleanup(void)
5975 if (autoconnect_timeout != 0) {
5976 g_source_remove(autoconnect_timeout);
5977 autoconnect_timeout = 0;
5980 list = service_list;
5981 service_list = NULL;
5982 g_sequence_free(list);
5984 g_hash_table_destroy(service_hash);
5985 service_hash = NULL;
5987 g_slist_free(counter_list);
5988 counter_list = NULL;
5990 if (services_notify->id != 0) {
5991 g_source_remove(services_notify->id);
5992 service_send_changed(NULL);
5993 g_hash_table_destroy(services_notify->remove);
5994 g_hash_table_destroy(services_notify->add);
5996 g_free(services_notify);
5998 dbus_connection_unref(connection);