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
32 #define CONNECT_TIMEOUT 120
34 static DBusConnection *connection = NULL;
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
38 static GSList *counter_list = NULL;
40 struct connman_stats {
42 connman_bool_t enabled;
43 struct connman_stats_data data_last;
44 struct connman_stats_data data;
48 struct connman_stats_counter {
49 connman_bool_t append_all;
50 struct connman_stats stats;
51 struct connman_stats stats_roaming;
54 struct connman_service {
58 enum connman_service_type type;
59 enum connman_service_mode mode;
60 enum connman_service_security security;
61 enum connman_service_state state_ipv4;
62 enum connman_service_state state_ipv6;
63 enum connman_service_error error;
64 connman_uint8_t strength;
65 connman_bool_t favorite;
66 connman_bool_t immutable;
67 connman_bool_t hidden;
68 connman_bool_t ignore;
69 connman_bool_t autoconnect;
70 connman_bool_t userconnect;
76 connman_bool_t roaming;
77 connman_bool_t login_required;
78 struct connman_ipconfig *ipconfig_ipv4;
79 struct connman_ipconfig *ipconfig_ipv6;
80 struct connman_network *network;
81 struct connman_provider *provider;
83 char **nameservers_config;
86 /* 802.1x settings from the config files */
90 char *client_cert_file;
91 char *private_key_file;
92 char *private_key_passphrase;
96 struct connman_location *location;
97 struct connman_stats stats;
98 struct connman_stats stats_roaming;
99 GHashTable *counter_table;
100 enum connman_service_proxy_method proxy;
101 enum connman_service_proxy_method proxy_config;
108 static void append_path(gpointer value, gpointer user_data)
110 struct connman_service *service = value;
111 DBusMessageIter *iter = user_data;
113 if (service->path == NULL || service->hidden == TRUE)
116 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
120 void __connman_service_list(DBusMessageIter *iter, void *user_data)
122 g_sequence_foreach(service_list, append_path, iter);
127 struct connman_service *service;
130 static void compare_path(gpointer value, gpointer user_data)
132 struct connman_service *service = value;
133 struct find_data *data = user_data;
135 if (data->service != NULL)
138 if (g_strcmp0(service->path, data->path) == 0)
139 data->service = service;
142 static struct connman_service *find_service(const char *path)
144 struct find_data data = { .path = path, .service = NULL };
146 DBG("path %s", path);
148 g_sequence_foreach(service_list, compare_path, &data);
153 const char *__connman_service_type2string(enum connman_service_type type)
156 case CONNMAN_SERVICE_TYPE_UNKNOWN:
158 case CONNMAN_SERVICE_TYPE_SYSTEM:
160 case CONNMAN_SERVICE_TYPE_ETHERNET:
162 case CONNMAN_SERVICE_TYPE_WIFI:
164 case CONNMAN_SERVICE_TYPE_WIMAX:
166 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
168 case CONNMAN_SERVICE_TYPE_CELLULAR:
170 case CONNMAN_SERVICE_TYPE_GPS:
172 case CONNMAN_SERVICE_TYPE_VPN:
174 case CONNMAN_SERVICE_TYPE_GADGET:
181 static const char *mode2string(enum connman_service_mode mode)
184 case CONNMAN_SERVICE_MODE_UNKNOWN:
186 case CONNMAN_SERVICE_MODE_MANAGED:
188 case CONNMAN_SERVICE_MODE_ADHOC:
190 case CONNMAN_SERVICE_MODE_GPRS:
192 case CONNMAN_SERVICE_MODE_EDGE:
194 case CONNMAN_SERVICE_MODE_UMTS:
201 static const char *security2string(enum connman_service_security security)
204 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
206 case CONNMAN_SERVICE_SECURITY_NONE:
208 case CONNMAN_SERVICE_SECURITY_WEP:
210 case CONNMAN_SERVICE_SECURITY_PSK:
212 case CONNMAN_SERVICE_SECURITY_8021X:
214 case CONNMAN_SERVICE_SECURITY_WPA:
216 case CONNMAN_SERVICE_SECURITY_RSN:
223 static const char *state2string(enum connman_service_state state)
226 case CONNMAN_SERVICE_STATE_UNKNOWN:
228 case CONNMAN_SERVICE_STATE_IDLE:
230 case CONNMAN_SERVICE_STATE_ASSOCIATION:
231 return "association";
232 case CONNMAN_SERVICE_STATE_CONFIGURATION:
233 return "configuration";
234 case CONNMAN_SERVICE_STATE_READY:
236 case CONNMAN_SERVICE_STATE_ONLINE:
238 case CONNMAN_SERVICE_STATE_DISCONNECT:
240 case CONNMAN_SERVICE_STATE_FAILURE:
247 static const char *error2string(enum connman_service_error error)
250 case CONNMAN_SERVICE_ERROR_UNKNOWN:
252 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
253 return "out-of-range";
254 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
255 return "pin-missing";
256 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
257 return "dhcp-failed";
258 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
259 return "connect-failed";
260 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
261 return "login-failed";
262 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
263 return "auth-failed";
264 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
265 return "invalid-key";
271 static enum connman_service_error string2error(const char *error)
273 if (g_strcmp0(error, "dhcp-failed") == 0)
274 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
275 else if (g_strcmp0(error, "pin-missing") == 0)
276 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
277 else if (g_strcmp0(error, "invalid-key") == 0)
278 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
280 return CONNMAN_SERVICE_ERROR_UNKNOWN;
283 static const char *proxymethod2string(enum connman_service_proxy_method method)
286 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
288 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
290 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
292 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
299 static enum connman_service_proxy_method string2proxymethod(const char *method)
301 if (g_strcmp0(method, "direct") == 0)
302 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
303 else if (g_strcmp0(method, "auto") == 0)
304 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
305 else if (g_strcmp0(method, "manual") == 0)
306 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
308 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
311 static enum connman_service_state combine_state(
312 enum connman_service_state state_a,
313 enum connman_service_state state_b)
315 enum connman_service_state result;
317 if (state_a == state_b) {
322 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
327 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
332 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
337 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
342 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
343 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
344 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
345 state_b == CONNMAN_SERVICE_STATE_READY)
352 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
353 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
354 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
355 state_a == CONNMAN_SERVICE_STATE_READY)
362 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
363 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
364 state_b == CONNMAN_SERVICE_STATE_READY)
371 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
372 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
373 state_a == CONNMAN_SERVICE_STATE_READY)
380 if (state_a == CONNMAN_SERVICE_STATE_READY) {
381 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
388 if (state_b == CONNMAN_SERVICE_STATE_READY) {
389 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
396 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
401 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
406 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
411 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
416 result = CONNMAN_SERVICE_STATE_FAILURE;
422 static connman_bool_t is_connecting_state(struct connman_service *service,
423 enum connman_service_state state)
426 case CONNMAN_SERVICE_STATE_UNKNOWN:
427 case CONNMAN_SERVICE_STATE_IDLE:
428 case CONNMAN_SERVICE_STATE_FAILURE:
429 if (service->network != NULL)
430 return __connman_network_get_connecting(
432 case CONNMAN_SERVICE_STATE_DISCONNECT:
433 case CONNMAN_SERVICE_STATE_READY:
434 case CONNMAN_SERVICE_STATE_ONLINE:
436 case CONNMAN_SERVICE_STATE_ASSOCIATION:
437 case CONNMAN_SERVICE_STATE_CONFIGURATION:
444 static connman_bool_t is_connected_state(const struct connman_service *service,
445 enum connman_service_state state)
448 case CONNMAN_SERVICE_STATE_UNKNOWN:
449 case CONNMAN_SERVICE_STATE_IDLE:
450 case CONNMAN_SERVICE_STATE_ASSOCIATION:
451 case CONNMAN_SERVICE_STATE_CONFIGURATION:
452 case CONNMAN_SERVICE_STATE_DISCONNECT:
453 case CONNMAN_SERVICE_STATE_FAILURE:
455 case CONNMAN_SERVICE_STATE_READY:
456 case CONNMAN_SERVICE_STATE_ONLINE:
463 static connman_bool_t is_connecting(struct connman_service *service)
465 enum connman_service_state state;
467 state = combine_state(service->state_ipv4, service->state_ipv6);
469 return is_connecting_state(service, state);
472 static connman_bool_t is_connected(struct connman_service *service)
474 enum connman_service_state state;
476 state = combine_state(service->state_ipv4, service->state_ipv6);
478 return is_connected_state(service, state);
481 static void update_nameservers(struct connman_service *service)
485 if (service->ipconfig_ipv4)
486 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
487 else if (service->ipconfig_ipv6)
488 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
495 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
496 case CONNMAN_SERVICE_STATE_UNKNOWN:
497 case CONNMAN_SERVICE_STATE_IDLE:
498 case CONNMAN_SERVICE_STATE_ASSOCIATION:
499 case CONNMAN_SERVICE_STATE_CONFIGURATION:
501 case CONNMAN_SERVICE_STATE_FAILURE:
502 case CONNMAN_SERVICE_STATE_DISCONNECT:
503 connman_resolver_remove_all(ifname);
505 case CONNMAN_SERVICE_STATE_READY:
506 case CONNMAN_SERVICE_STATE_ONLINE:
510 connman_resolver_remove_all(ifname);
512 if (service->nameservers_config != NULL) {
515 for (i = 0; service->nameservers_config[i] != NULL; i++) {
516 connman_resolver_append(ifname, NULL,
517 service->nameservers_config[i]);
519 } else if (service->nameservers != NULL) {
522 for (i = 0; service->nameservers[i] != NULL; i++) {
523 connman_resolver_append(ifname, NULL,
524 service->nameservers[i]);
528 if (service->domains != NULL) {
531 for (i = 0; service->domains[i]; i++)
532 connman_resolver_append(ifname, service->domains[i],
534 } else if (service->domainname != NULL)
535 connman_resolver_append(ifname, service->domainname, NULL);
537 connman_resolver_flush();
540 int __connman_service_nameserver_append(struct connman_service *service,
541 const char *nameserver)
545 DBG("service %p nameserver %s", service, nameserver);
547 if (nameserver == NULL)
550 if (service->nameservers != NULL) {
551 len = g_strv_length(service->nameservers);
552 service->nameservers = g_try_renew(char *, service->nameservers,
556 service->nameservers = g_try_new0(char *, len + 2);
559 if (service->nameservers == NULL)
562 service->nameservers[len] = g_strdup(nameserver);
563 service->nameservers[len + 1] = NULL;
565 update_nameservers(service);
570 int __connman_service_nameserver_remove(struct connman_service *service,
571 const char *nameserver)
576 DBG("service %p nameserver %s", service, nameserver);
578 if (nameserver == NULL)
581 if (service->nameservers == NULL)
584 len = g_strv_length(service->nameservers);
586 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
589 g_strfreev(service->nameservers);
590 service->nameservers = NULL;
595 servers = g_try_new0(char *, len - 1);
599 for (i = 0, j = 0; i < len; i++) {
600 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
601 servers[j] = g_strdup(service->nameservers[i]);
605 servers[len - 2] = NULL;
607 g_strfreev(service->nameservers);
608 service->nameservers = servers;
610 update_nameservers(service);
615 static void nameserver_add_routes(int index, char **nameservers,
620 for (i = 0; nameservers[i] != NULL; i++) {
621 if (connman_inet_compare_subnet(index, nameservers[i]))
624 connman_inet_add_host_route(index, nameservers[i], gw);
628 static void nameserver_del_routes(int index, char **nameservers)
632 for (i = 0; nameservers[i] != NULL; i++)
633 connman_inet_del_host_route(index, nameservers[i]);
636 void __connman_service_nameserver_add_routes(struct connman_service *service,
644 if (service->network != NULL)
645 index = connman_network_get_index(service->network);
646 else if (service->provider != NULL)
647 index = connman_provider_get_index(service->provider);
649 if (service->nameservers_config != NULL) {
651 * Configured nameserver takes preference over the
652 * discoverd nameserver gathered from DHCP, VPN, etc.
654 nameserver_add_routes(index, service->nameservers_config, gw);
655 } else if (service->nameservers != NULL) {
657 * We add nameservers host routes for nameservers that
658 * are not on our subnet. For those who are, the subnet
659 * route will be installed by the time the dns proxy code
660 * tries to reach them. The subnet route is installed
661 * when setting the interface IP address.
663 nameserver_add_routes(index, service->nameservers, gw);
667 void __connman_service_nameserver_del_routes(struct connman_service *service)
674 if (service->network != NULL)
675 index = connman_network_get_index(service->network);
676 else if (service->provider != NULL)
677 index = connman_provider_get_index(service->provider);
679 if (service->nameservers_config != NULL)
680 nameserver_del_routes(index, service->nameservers_config);
681 else if (service->nameservers != NULL)
682 nameserver_del_routes(index, service->nameservers);
685 static struct connman_stats *stats_get(struct connman_service *service)
687 if (service->roaming == TRUE)
688 return &service->stats_roaming;
690 return &service->stats;
693 static connman_bool_t stats_enabled(struct connman_service *service)
695 struct connman_stats *stats = stats_get(service);
697 return stats->enabled;
700 static void stats_start(struct connman_service *service)
702 struct connman_stats *stats = stats_get(service);
704 DBG("service %p", service);
706 if (stats->timer == NULL)
709 stats->enabled = TRUE;
710 stats->data_last.time = stats->data.time;
712 g_timer_start(stats->timer);
715 static void stats_stop(struct connman_service *service)
717 struct connman_stats *stats = stats_get(service);
718 unsigned int seconds;
720 DBG("service %p", service);
722 if (stats->timer == NULL)
725 if (stats->enabled == FALSE)
728 g_timer_stop(stats->timer);
730 seconds = g_timer_elapsed(stats->timer, NULL);
731 stats->data.time = stats->data_last.time + seconds;
733 stats->enabled = FALSE;
736 static void reset_stats(struct connman_service *service)
738 DBG("service %p", service);
741 service->stats.valid = FALSE;
743 service->stats.data.rx_packets = 0;
744 service->stats.data.tx_packets = 0;
745 service->stats.data.rx_bytes = 0;
746 service->stats.data.tx_bytes = 0;
747 service->stats.data.rx_errors = 0;
748 service->stats.data.tx_errors = 0;
749 service->stats.data.rx_dropped = 0;
750 service->stats.data.tx_dropped = 0;
751 service->stats.data.time = 0;
752 service->stats.data_last.time = 0;
754 g_timer_reset(service->stats.timer);
757 service->stats_roaming.valid = FALSE;
759 service->stats_roaming.data.rx_packets = 0;
760 service->stats_roaming.data.tx_packets = 0;
761 service->stats_roaming.data.rx_bytes = 0;
762 service->stats_roaming.data.tx_bytes = 0;
763 service->stats_roaming.data.rx_errors = 0;
764 service->stats_roaming.data.tx_errors = 0;
765 service->stats_roaming.data.rx_dropped = 0;
766 service->stats_roaming.data.tx_dropped = 0;
767 service->stats_roaming.data.time = 0;
768 service->stats_roaming.data_last.time = 0;
770 g_timer_reset(service->stats_roaming.timer);
773 static struct connman_service *get_default(void)
775 struct connman_service *service;
778 iter = g_sequence_get_begin_iter(service_list);
780 if (g_sequence_iter_is_end(iter) == TRUE)
783 service = g_sequence_get(iter);
785 if (is_connected(service) == FALSE)
791 static void default_changed(void)
793 struct connman_service *service = get_default();
795 __connman_notifier_default_changed(service);
798 const char *__connman_service_default(void)
800 struct connman_service *service;
802 service = get_default();
806 return __connman_service_type2string(service->type);
809 static void mode_changed(struct connman_service *service)
813 str = mode2string(service->mode);
817 connman_dbus_property_changed_basic(service->path,
818 CONNMAN_SERVICE_INTERFACE, "Mode",
819 DBUS_TYPE_STRING, &str);
822 static void state_changed(struct connman_service *service)
826 str = state2string(combine_state(service->state_ipv4,
827 service->state_ipv6));
831 connman_dbus_property_changed_basic(service->path,
832 CONNMAN_SERVICE_INTERFACE, "State",
833 DBUS_TYPE_STRING, &str);
836 static void strength_changed(struct connman_service *service)
838 if (service->strength == 0)
841 connman_dbus_property_changed_basic(service->path,
842 CONNMAN_SERVICE_INTERFACE, "Strength",
843 DBUS_TYPE_BYTE, &service->strength);
846 static void favorite_changed(struct connman_service *service)
848 if (service->path == NULL)
851 connman_dbus_property_changed_basic(service->path,
852 CONNMAN_SERVICE_INTERFACE, "Favorite",
853 DBUS_TYPE_BOOLEAN, &service->favorite);
856 static void immutable_changed(struct connman_service *service)
858 if (service->path == NULL)
861 connman_dbus_property_changed_basic(service->path,
862 CONNMAN_SERVICE_INTERFACE, "Immutable",
863 DBUS_TYPE_BOOLEAN, &service->immutable);
866 static void roaming_changed(struct connman_service *service)
868 if (service->path == NULL)
871 connman_dbus_property_changed_basic(service->path,
872 CONNMAN_SERVICE_INTERFACE, "Roaming",
873 DBUS_TYPE_BOOLEAN, &service->roaming);
876 static void autoconnect_changed(struct connman_service *service)
878 if (service->path == NULL)
881 connman_dbus_property_changed_basic(service->path,
882 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
883 DBUS_TYPE_BOOLEAN, &service->autoconnect);
886 static void passphrase_changed(struct connman_service *service)
888 dbus_bool_t required;
890 switch (service->type) {
891 case CONNMAN_SERVICE_TYPE_UNKNOWN:
892 case CONNMAN_SERVICE_TYPE_SYSTEM:
893 case CONNMAN_SERVICE_TYPE_ETHERNET:
894 case CONNMAN_SERVICE_TYPE_WIMAX:
895 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
896 case CONNMAN_SERVICE_TYPE_CELLULAR:
897 case CONNMAN_SERVICE_TYPE_GPS:
898 case CONNMAN_SERVICE_TYPE_VPN:
899 case CONNMAN_SERVICE_TYPE_GADGET:
901 case CONNMAN_SERVICE_TYPE_WIFI:
904 switch (service->security) {
905 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
906 case CONNMAN_SERVICE_SECURITY_NONE:
908 case CONNMAN_SERVICE_SECURITY_WEP:
909 case CONNMAN_SERVICE_SECURITY_PSK:
910 case CONNMAN_SERVICE_SECURITY_WPA:
911 case CONNMAN_SERVICE_SECURITY_RSN:
912 if (service->passphrase == NULL)
915 case CONNMAN_SERVICE_SECURITY_8021X:
921 connman_dbus_property_changed_basic(service->path,
922 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
923 DBUS_TYPE_BOOLEAN, &required);
926 static void login_changed(struct connman_service *service)
928 dbus_bool_t required = service->login_required;
930 if (service->path == NULL)
933 connman_dbus_property_changed_basic(service->path,
934 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
935 DBUS_TYPE_BOOLEAN, &required);
938 static void append_security(DBusMessageIter *iter, void *user_data)
940 struct connman_service *service = user_data;
943 str = security2string(service->security);
945 dbus_message_iter_append_basic(iter,
946 DBUS_TYPE_STRING, &str);
949 if (service->wps == TRUE)
950 dbus_message_iter_append_basic(iter,
951 DBUS_TYPE_STRING, &str);
954 static void append_ethernet(DBusMessageIter *iter, void *user_data)
956 struct connman_service *service = user_data;
958 if (service->ipconfig_ipv4 != NULL)
959 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
961 else if (service->ipconfig_ipv6 != NULL)
962 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
966 static void append_ipv4(DBusMessageIter *iter, void *user_data)
968 struct connman_service *service = user_data;
970 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
971 state2string(service->state_ipv4));
973 if (is_connected_state(service, service->state_ipv4) == FALSE)
976 if (service->ipconfig_ipv4 != NULL)
977 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
980 static void append_ipv6(DBusMessageIter *iter, void *user_data)
982 struct connman_service *service = user_data;
984 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
985 state2string(service->state_ipv6));
987 if (is_connected_state(service, service->state_ipv6) == FALSE)
990 if (service->ipconfig_ipv6 != NULL)
991 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter);
994 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
996 struct connman_service *service = user_data;
998 if (service->ipconfig_ipv4 != NULL)
999 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1003 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1005 struct connman_service *service = user_data;
1007 if (service->ipconfig_ipv6 != NULL)
1008 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1012 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1017 servers = *nameservers;
1019 for (i = 0; servers[i] != NULL; i++) {
1020 dbus_message_iter_append_basic(iter,
1021 DBUS_TYPE_STRING, &servers[i]);
1025 static void append_dns(DBusMessageIter *iter, void *user_data)
1027 struct connman_service *service = user_data;
1029 if (is_connected(service) == FALSE)
1032 if (service->nameservers_config != NULL) {
1033 append_nameserver(iter, &service->nameservers_config);
1035 } else if (service->nameservers != NULL) {
1036 append_nameserver(iter, &service->nameservers);
1041 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1043 struct connman_service *service = user_data;
1046 if (service->nameservers_config == NULL)
1049 for (i = 0; service->nameservers_config[i]; i++) {
1050 dbus_message_iter_append_basic(iter,
1052 &service->nameservers_config[i]);
1056 static void append_domain(DBusMessageIter *iter, void *user_data)
1058 struct connman_service *service = user_data;
1060 if (is_connected(service) == FALSE &&
1061 is_connecting(service) == FALSE)
1064 if (service->domainname == NULL)
1067 dbus_message_iter_append_basic(iter,
1068 DBUS_TYPE_STRING, &service->domainname);
1071 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1073 struct connman_service *service = user_data;
1076 if (service->domains == NULL)
1079 for (i = 0; service->domains[i]; i++)
1080 dbus_message_iter_append_basic(iter,
1081 DBUS_TYPE_STRING, &service->domains[i]);
1084 static void append_proxies(DBusMessageIter *iter, void *user_data)
1086 struct connman_service *service = user_data;
1089 if (service->proxies == NULL)
1092 for (i = 0; service->proxies[i]; i++)
1093 dbus_message_iter_append_basic(iter,
1094 DBUS_TYPE_STRING, &service->proxies[i]);
1097 static void append_excludes(DBusMessageIter *iter, void *user_data)
1099 struct connman_service *service = user_data;
1102 if (service->excludes == NULL)
1105 for (i = 0; service->excludes[i]; i++)
1106 dbus_message_iter_append_basic(iter,
1107 DBUS_TYPE_STRING, &service->excludes[i]);
1110 static void append_proxy(DBusMessageIter *iter, void *user_data)
1112 struct connman_service *service = user_data;
1113 enum connman_service_proxy_method proxy;
1114 const char *pac = NULL;
1115 const char *method = proxymethod2string(
1116 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1120 if (is_connected(service) == FALSE)
1123 proxy = connman_service_get_proxy_method(service);
1126 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1128 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1130 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1131 connman_dbus_dict_append_array(iter, "Servers",
1132 DBUS_TYPE_STRING, append_proxies,
1135 connman_dbus_dict_append_array(iter, "Excludes",
1136 DBUS_TYPE_STRING, append_excludes,
1139 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1140 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1141 if (service->ipconfig_ipv4 != NULL)
1142 pac = __connman_ipconfig_get_proxy_autoconfig(
1143 service->ipconfig_ipv4);
1144 else if (service->ipconfig_ipv6 != NULL)
1145 pac = __connman_ipconfig_get_proxy_autoconfig(
1146 service->ipconfig_ipv6);
1148 if (service->pac == NULL && pac == NULL)
1151 if (service->pac != NULL)
1154 connman_dbus_dict_append_basic(iter, "URL",
1155 DBUS_TYPE_STRING, &pac);
1159 method = proxymethod2string(proxy);
1162 connman_dbus_dict_append_basic(iter, "Method",
1163 DBUS_TYPE_STRING, &method);
1166 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1168 struct connman_service *service = user_data;
1171 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1174 switch (service->proxy_config) {
1175 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1177 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1179 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1180 if (service->proxies != NULL)
1181 connman_dbus_dict_append_array(iter, "Servers",
1183 append_proxies, service);
1185 if (service->excludes != NULL)
1186 connman_dbus_dict_append_array(iter, "Excludes",
1188 append_excludes, service);
1190 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1191 if (service->pac != NULL)
1192 connman_dbus_dict_append_basic(iter, "URL",
1193 DBUS_TYPE_STRING, &service->pac);
1197 method = proxymethod2string(service->proxy_config);
1199 connman_dbus_dict_append_basic(iter, "Method",
1200 DBUS_TYPE_STRING, &method);
1203 static void append_provider(DBusMessageIter *iter, void *user_data)
1205 struct connman_service *service = user_data;
1207 DBG("%p %p", service, service->provider);
1209 if (is_connected(service) == FALSE)
1212 if (service->provider != NULL)
1213 __connman_provider_append_properties(service->provider, iter);
1217 static void settings_changed(struct connman_service *service)
1219 connman_dbus_property_changed_dict(service->path,
1220 CONNMAN_SERVICE_INTERFACE, "IPv4",
1221 append_ipv4, service);
1223 connman_dbus_property_changed_dict(service->path,
1224 CONNMAN_SERVICE_INTERFACE, "IPv6",
1225 append_ipv6, service);
1228 static void ipv4_configuration_changed(struct connman_service *service)
1230 connman_dbus_property_changed_dict(service->path,
1231 CONNMAN_SERVICE_INTERFACE,
1232 "IPv4.Configuration",
1237 static void ipv6_configuration_changed(struct connman_service *service)
1239 connman_dbus_property_changed_dict(service->path,
1240 CONNMAN_SERVICE_INTERFACE,
1241 "IPv6.Configuration",
1246 static void dns_changed(struct connman_service *service)
1248 connman_dbus_property_changed_array(service->path,
1249 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1250 DBUS_TYPE_STRING, append_dns, service);
1253 static void dns_configuration_changed(struct connman_service *service)
1255 connman_dbus_property_changed_array(service->path,
1256 CONNMAN_SERVICE_INTERFACE,
1257 "Nameservers.Configuration",
1258 DBUS_TYPE_STRING, append_dnsconfig, service);
1260 dns_changed(service);
1263 static void domain_changed(struct connman_service *service)
1265 connman_dbus_property_changed_array(service->path,
1266 CONNMAN_SERVICE_INTERFACE, "Domains",
1267 DBUS_TYPE_STRING, append_domain, service);
1270 static void domain_configuration_changed(struct connman_service *service)
1272 connman_dbus_property_changed_array(service->path,
1273 CONNMAN_SERVICE_INTERFACE,
1274 "Domains.Configuration",
1275 DBUS_TYPE_STRING, append_domainconfig, service);
1278 static void proxy_changed(struct connman_service *service)
1280 connman_dbus_property_changed_dict(service->path,
1281 CONNMAN_SERVICE_INTERFACE, "Proxy",
1282 append_proxy, service);
1285 static void proxy_configuration_changed(struct connman_service *service)
1287 connman_dbus_property_changed_dict(service->path,
1288 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1289 append_proxyconfig, service);
1291 proxy_changed(service);
1294 static void link_changed(struct connman_service *service)
1296 connman_dbus_property_changed_dict(service->path,
1297 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1298 append_ethernet, service);
1301 static void stats_append_counters(DBusMessageIter *dict,
1302 struct connman_stats_data *stats,
1303 struct connman_stats_data *counters,
1304 connman_bool_t append_all)
1306 if (counters->rx_packets != stats->rx_packets || append_all) {
1307 counters->rx_packets = stats->rx_packets;
1308 connman_dbus_dict_append_basic(dict, "RX.Packets",
1309 DBUS_TYPE_UINT32, &stats->rx_packets);
1312 if (counters->tx_packets != stats->tx_packets || append_all) {
1313 counters->tx_packets = stats->tx_packets;
1314 connman_dbus_dict_append_basic(dict, "TX.Packets",
1315 DBUS_TYPE_UINT32, &stats->tx_packets);
1318 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1319 counters->rx_bytes = stats->rx_bytes;
1320 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1321 DBUS_TYPE_UINT32, &stats->rx_bytes);
1324 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1325 counters->tx_bytes = stats->tx_bytes;
1326 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1327 DBUS_TYPE_UINT32, &stats->tx_bytes);
1330 if (counters->rx_errors != stats->rx_errors || append_all) {
1331 counters->rx_errors = stats->rx_errors;
1332 connman_dbus_dict_append_basic(dict, "RX.Errors",
1333 DBUS_TYPE_UINT32, &stats->rx_errors);
1336 if (counters->tx_errors != stats->tx_errors || append_all) {
1337 counters->tx_errors = stats->tx_errors;
1338 connman_dbus_dict_append_basic(dict, "TX.Errors",
1339 DBUS_TYPE_UINT32, &stats->tx_errors);
1342 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1343 counters->rx_dropped = stats->rx_dropped;
1344 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1345 DBUS_TYPE_UINT32, &stats->rx_dropped);
1348 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1349 counters->tx_dropped = stats->tx_dropped;
1350 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1351 DBUS_TYPE_UINT32, &stats->tx_dropped);
1354 if (counters->time != stats->time || append_all) {
1355 counters->time = stats->time;
1356 connman_dbus_dict_append_basic(dict, "Time",
1357 DBUS_TYPE_UINT32, &stats->time);
1361 static void stats_append(struct connman_service *service,
1362 const char *counter,
1363 struct connman_stats_counter *counters,
1364 connman_bool_t append_all)
1366 DBusMessageIter array, dict;
1369 DBG("service %p counter %s", service, counter);
1371 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1375 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1376 &service->path, DBUS_TYPE_INVALID);
1378 dbus_message_iter_init_append(msg, &array);
1381 connman_dbus_dict_open(&array, &dict);
1383 stats_append_counters(&dict, &service->stats.data,
1384 &counters->stats.data, append_all);
1386 connman_dbus_dict_close(&array, &dict);
1388 /* roaming counter */
1389 connman_dbus_dict_open(&array, &dict);
1391 stats_append_counters(&dict, &service->stats_roaming.data,
1392 &counters->stats_roaming.data, append_all);
1394 connman_dbus_dict_close(&array, &dict);
1396 __connman_counter_send_usage(counter, msg);
1399 static void stats_update(struct connman_service *service,
1400 unsigned int rx_packets, unsigned int tx_packets,
1401 unsigned int rx_bytes, unsigned int tx_bytes,
1402 unsigned int rx_errors, unsigned int tx_errors,
1403 unsigned int rx_dropped, unsigned int tx_dropped)
1405 struct connman_stats *stats = stats_get(service);
1406 struct connman_stats_data *data_last = &stats->data_last;
1407 struct connman_stats_data *data = &stats->data;
1408 unsigned int seconds;
1410 DBG("service %p", service);
1412 if (stats->valid == TRUE) {
1414 rx_packets - data_last->rx_packets;
1416 tx_packets - data_last->tx_packets;
1418 rx_bytes - data_last->rx_bytes;
1420 tx_bytes - data_last->tx_bytes;
1422 rx_errors - data_last->rx_errors;
1424 tx_errors - data_last->tx_errors;
1426 rx_dropped - data_last->rx_dropped;
1428 tx_dropped - data_last->tx_dropped;
1430 stats->valid = TRUE;
1433 data_last->rx_packets = rx_packets;
1434 data_last->tx_packets = tx_packets;
1435 data_last->rx_bytes = rx_bytes;
1436 data_last->tx_bytes = tx_bytes;
1437 data_last->rx_errors = rx_errors;
1438 data_last->tx_errors = tx_errors;
1439 data_last->rx_dropped = rx_dropped;
1440 data_last->tx_dropped = tx_dropped;
1442 seconds = g_timer_elapsed(stats->timer, NULL);
1443 stats->data.time = stats->data_last.time + seconds;
1446 void __connman_service_notify(struct connman_service *service,
1447 unsigned int rx_packets, unsigned int tx_packets,
1448 unsigned int rx_bytes, unsigned int tx_bytes,
1449 unsigned int rx_errors, unsigned int tx_errors,
1450 unsigned int rx_dropped, unsigned int tx_dropped)
1452 GHashTableIter iter;
1453 gpointer key, value;
1454 const char *counter;
1455 struct connman_stats_counter *counters;
1456 struct connman_stats_data *data;
1459 if (service == NULL)
1462 if (is_connected(service) == FALSE)
1465 stats_update(service,
1466 rx_packets, tx_packets,
1468 rx_errors, tx_errors,
1469 rx_dropped, tx_dropped);
1471 data = &stats_get(service)->data;
1472 err = __connman_stats_update(service, service->roaming, data);
1474 connman_error("Failed to store statistics for %s",
1475 service->identifier);
1477 g_hash_table_iter_init(&iter, service->counter_table);
1478 while (g_hash_table_iter_next(&iter, &key, &value)) {
1482 stats_append(service, counter, counters, counters->append_all);
1483 counters->append_all = FALSE;
1487 int __connman_service_counter_register(const char *counter)
1489 struct connman_service *service;
1490 GSequenceIter *iter;
1491 struct connman_stats_counter *counters;
1493 DBG("counter %s", counter);
1495 counter_list = g_slist_append(counter_list, (gpointer)counter);
1497 iter = g_sequence_get_begin_iter(service_list);
1499 while (g_sequence_iter_is_end(iter) == FALSE) {
1500 service = g_sequence_get(iter);
1502 counters = g_try_new0(struct connman_stats_counter, 1);
1503 if (counters == NULL)
1506 counters->append_all = TRUE;
1508 g_hash_table_replace(service->counter_table, (gpointer)counter,
1511 iter = g_sequence_iter_next(iter);
1517 void __connman_service_counter_unregister(const char *counter)
1519 struct connman_service *service;
1520 GSequenceIter *iter;
1522 DBG("counter %s", counter);
1524 iter = g_sequence_get_begin_iter(service_list);
1526 while (g_sequence_iter_is_end(iter) == FALSE) {
1527 service = g_sequence_get(iter);
1529 g_hash_table_remove(service->counter_table, counter);
1531 iter = g_sequence_iter_next(iter);
1534 counter_list = g_slist_remove(counter_list, counter);
1537 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1538 struct connman_service *service)
1540 dbus_bool_t required;
1543 str = __connman_service_type2string(service->type);
1545 connman_dbus_dict_append_basic(dict, "Type",
1546 DBUS_TYPE_STRING, &str);
1548 str = mode2string(service->mode);
1550 connman_dbus_dict_append_basic(dict, "Mode",
1551 DBUS_TYPE_STRING, &str);
1553 connman_dbus_dict_append_array(dict, "Security",
1554 DBUS_TYPE_STRING, append_security, service);
1556 str = state2string(combine_state(service->state_ipv4,
1557 service->state_ipv6));
1559 connman_dbus_dict_append_basic(dict, "State",
1560 DBUS_TYPE_STRING, &str);
1562 str = error2string(service->error);
1564 connman_dbus_dict_append_basic(dict, "Error",
1565 DBUS_TYPE_STRING, &str);
1567 if (service->strength > 0)
1568 connman_dbus_dict_append_basic(dict, "Strength",
1569 DBUS_TYPE_BYTE, &service->strength);
1571 connman_dbus_dict_append_basic(dict, "Favorite",
1572 DBUS_TYPE_BOOLEAN, &service->favorite);
1574 connman_dbus_dict_append_basic(dict, "Immutable",
1575 DBUS_TYPE_BOOLEAN, &service->immutable);
1577 if (service->favorite == TRUE)
1578 connman_dbus_dict_append_basic(dict, "AutoConnect",
1579 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1581 connman_dbus_dict_append_basic(dict, "AutoConnect",
1582 DBUS_TYPE_BOOLEAN, &service->favorite);
1584 if (service->name != NULL)
1585 connman_dbus_dict_append_basic(dict, "Name",
1586 DBUS_TYPE_STRING, &service->name);
1588 connman_dbus_dict_append_basic(dict, "LoginRequired",
1589 DBUS_TYPE_BOOLEAN, &service->login_required);
1591 switch (service->type) {
1592 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1593 case CONNMAN_SERVICE_TYPE_SYSTEM:
1594 case CONNMAN_SERVICE_TYPE_GPS:
1595 case CONNMAN_SERVICE_TYPE_VPN:
1596 case CONNMAN_SERVICE_TYPE_GADGET:
1598 case CONNMAN_SERVICE_TYPE_CELLULAR:
1599 connman_dbus_dict_append_basic(dict, "Roaming",
1600 DBUS_TYPE_BOOLEAN, &service->roaming);
1603 connman_dbus_dict_append_basic(dict, "SetupRequired",
1604 DBUS_TYPE_BOOLEAN, &required);
1605 connman_dbus_dict_append_dict(dict, "Ethernet",
1606 append_ethernet, service);
1608 case CONNMAN_SERVICE_TYPE_WIFI:
1609 if (service->passphrase != NULL && limited == FALSE)
1610 connman_dbus_dict_append_basic(dict, "Passphrase",
1611 DBUS_TYPE_STRING, &service->passphrase);
1615 switch (service->security) {
1616 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1617 case CONNMAN_SERVICE_SECURITY_NONE:
1619 case CONNMAN_SERVICE_SECURITY_WEP:
1620 case CONNMAN_SERVICE_SECURITY_PSK:
1621 case CONNMAN_SERVICE_SECURITY_WPA:
1622 case CONNMAN_SERVICE_SECURITY_RSN:
1623 if (service->passphrase == NULL)
1626 case CONNMAN_SERVICE_SECURITY_8021X:
1630 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1631 DBUS_TYPE_BOOLEAN, &required);
1634 case CONNMAN_SERVICE_TYPE_ETHERNET:
1635 case CONNMAN_SERVICE_TYPE_WIMAX:
1636 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1637 connman_dbus_dict_append_dict(dict, "Ethernet",
1638 append_ethernet, service);
1642 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1644 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1645 append_ipv4config, service);
1647 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1649 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1650 append_ipv6config, service);
1652 connman_dbus_dict_append_array(dict, "Nameservers",
1653 DBUS_TYPE_STRING, append_dns, service);
1655 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1656 DBUS_TYPE_STRING, append_dnsconfig, service);
1658 connman_dbus_dict_append_array(dict, "Domains",
1659 DBUS_TYPE_STRING, append_domain, service);
1661 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1662 DBUS_TYPE_STRING, append_domainconfig, service);
1664 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1666 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1667 append_proxyconfig, service);
1669 connman_dbus_dict_append_dict(dict, "Provider",
1670 append_provider, service);
1673 static void append_struct(gpointer value, gpointer user_data)
1675 struct connman_service *service = value;
1676 DBusMessageIter *iter = user_data;
1677 DBusMessageIter entry, dict;
1679 if (service->path == NULL || service->hidden == TRUE)
1682 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1684 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1687 connman_dbus_dict_open(&entry, &dict);
1688 append_properties(&dict, TRUE, service);
1689 connman_dbus_dict_close(&entry, &dict);
1691 dbus_message_iter_close_container(iter, &entry);
1694 void __connman_service_list_struct(DBusMessageIter *iter)
1696 g_sequence_foreach(service_list, append_struct, iter);
1699 int __connman_service_get_index(struct connman_service *service)
1701 if (service == NULL)
1704 if (service->network != NULL)
1705 return connman_network_get_index(service->network);
1706 else if (service->provider != NULL)
1707 return connman_provider_get_index(service->provider);
1712 void __connman_service_set_domainname(struct connman_service *service,
1713 const char *domainname)
1715 if (service == NULL)
1718 g_free(service->domainname);
1719 service->domainname = g_strdup(domainname);
1721 domain_changed(service);
1724 const char *connman_service_get_domainname(struct connman_service *service)
1726 if (service == NULL)
1729 if (service->domains != NULL)
1730 return service->domains[0];
1732 return service->domainname;
1735 char **connman_service_get_nameservers(struct connman_service *service)
1737 if (service == NULL)
1740 if (service->nameservers_config != NULL)
1741 return service->nameservers_config;
1742 else if (service->nameservers != NULL)
1743 return service->nameservers;
1748 void connman_service_set_proxy_method(struct connman_service *service,
1749 enum connman_service_proxy_method method)
1751 if (service == NULL)
1754 service->proxy = method;
1756 proxy_changed(service);
1758 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1759 __connman_notifier_proxy_changed(service);
1762 enum connman_service_proxy_method connman_service_get_proxy_method(
1763 struct connman_service *service)
1765 if (service == NULL)
1766 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1768 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1769 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1770 service->pac == NULL)
1771 return service->proxy;
1773 return service->proxy_config;
1776 return service->proxy;
1779 char **connman_service_get_proxy_servers(struct connman_service *service)
1781 return g_strdupv(service->proxies);
1784 char **connman_service_get_proxy_excludes(struct connman_service *service)
1786 return g_strdupv(service->excludes);
1789 const char *connman_service_get_proxy_url(struct connman_service *service)
1791 if (service == NULL)
1794 return service->pac;
1797 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1800 if (service == NULL)
1803 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1805 if (service->ipconfig_ipv4) {
1806 if (__connman_ipconfig_set_proxy_autoconfig(
1807 service->ipconfig_ipv4, url) < 0)
1809 } else if (service->ipconfig_ipv6) {
1810 if (__connman_ipconfig_set_proxy_autoconfig(
1811 service->ipconfig_ipv6, url) < 0)
1816 proxy_changed(service);
1818 __connman_notifier_proxy_changed(service);
1821 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1823 if (service == NULL)
1826 if (service->ipconfig_ipv4)
1827 return __connman_ipconfig_get_proxy_autoconfig(
1828 service->ipconfig_ipv4);
1829 else if (service->ipconfig_ipv6)
1830 return __connman_ipconfig_get_proxy_autoconfig(
1831 service->ipconfig_ipv6);
1835 void __connman_service_set_passphrase(struct connman_service *service,
1836 const char* passphrase)
1838 if (service->immutable == TRUE)
1841 g_free(service->passphrase);
1842 service->passphrase = g_strdup(passphrase);
1844 passphrase_changed(service);
1846 if (service->network != NULL)
1847 connman_network_set_string(service->network,
1849 service->passphrase);
1851 __connman_storage_save_service(service);
1854 static DBusMessage *get_properties(DBusConnection *conn,
1855 DBusMessage *msg, void *user_data)
1857 struct connman_service *service = user_data;
1859 DBusMessageIter array, dict;
1861 DBG("service %p", service);
1863 reply = dbus_message_new_method_return(msg);
1867 dbus_message_iter_init_append(reply, &array);
1869 connman_dbus_dict_open(&array, &dict);
1870 append_properties(&dict, FALSE, service);
1871 connman_dbus_dict_close(&array, &dict);
1876 static int update_proxy_configuration(struct connman_service *service,
1877 DBusMessageIter *array)
1879 DBusMessageIter dict;
1880 enum connman_service_proxy_method method;
1881 GString *servers_str = NULL;
1882 GString *excludes_str = NULL;
1883 const char *url = NULL;
1885 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1887 dbus_message_iter_recurse(array, &dict);
1889 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1890 DBusMessageIter entry, variant;
1894 dbus_message_iter_recurse(&dict, &entry);
1896 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1899 dbus_message_iter_get_basic(&entry, &key);
1900 dbus_message_iter_next(&entry);
1902 if (dbus_message_iter_get_arg_type(&entry) !=
1906 dbus_message_iter_recurse(&entry, &variant);
1907 type = dbus_message_iter_get_arg_type(&variant);
1909 if (g_str_equal(key, "Method") == TRUE) {
1912 if (type != DBUS_TYPE_STRING)
1915 dbus_message_iter_get_basic(&variant, &val);
1916 method = string2proxymethod(val);
1917 } else if (g_str_equal(key, "URL") == TRUE) {
1918 if (type != DBUS_TYPE_STRING)
1921 dbus_message_iter_get_basic(&variant, &url);
1922 } else if (g_str_equal(key, "Servers") == TRUE) {
1923 DBusMessageIter str_array;
1925 if (type != DBUS_TYPE_ARRAY)
1928 servers_str = g_string_new(NULL);
1929 if (servers_str == NULL)
1932 dbus_message_iter_recurse(&variant, &str_array);
1934 while (dbus_message_iter_get_arg_type(&str_array) ==
1938 dbus_message_iter_get_basic(&str_array, &val);
1940 if (servers_str->len > 0)
1941 g_string_append_printf(servers_str,
1944 g_string_append(servers_str, val);
1946 dbus_message_iter_next(&str_array);
1948 } else if (g_str_equal(key, "Excludes") == TRUE) {
1949 DBusMessageIter str_array;
1951 if (type != DBUS_TYPE_ARRAY)
1954 excludes_str = g_string_new(NULL);
1955 if (excludes_str == NULL)
1958 dbus_message_iter_recurse(&variant, &str_array);
1960 while (dbus_message_iter_get_arg_type(&str_array) ==
1964 dbus_message_iter_get_basic(&str_array, &val);
1966 if (excludes_str->len > 0)
1967 g_string_append_printf(excludes_str,
1970 g_string_append(excludes_str, val);
1972 dbus_message_iter_next(&str_array);
1976 dbus_message_iter_next(&dict);
1980 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1982 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1983 if (servers_str == NULL && service->proxies == NULL)
1986 if (servers_str != NULL) {
1987 g_strfreev(service->proxies);
1989 if (servers_str->len > 0)
1990 service->proxies = g_strsplit_set(
1991 servers_str->str, " ", 0);
1993 service->proxies = NULL;
1996 if (excludes_str != NULL) {
1997 g_strfreev(service->excludes);
1999 if (excludes_str->len > 0)
2000 service->excludes = g_strsplit_set(
2001 excludes_str->str, " ", 0);
2003 service->excludes = NULL;
2006 if (service->proxies == NULL)
2007 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2010 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2011 g_free(service->pac);
2013 if (url != NULL && strlen(url) > 0)
2014 service->pac = g_strdup(url);
2016 service->pac = NULL;
2018 /* if we are connected:
2019 - if service->pac == NULL
2020 - if __connman_ipconfig_get_proxy_autoconfig(
2021 service->ipconfig) == NULL
2022 --> We should start WPAD */
2025 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2029 if (servers_str != NULL)
2030 g_string_free(servers_str, TRUE);
2032 if (excludes_str != NULL)
2033 g_string_free(excludes_str, TRUE);
2035 service->proxy_config = method;
2040 if (servers_str != NULL)
2041 g_string_free(servers_str, TRUE);
2043 if (excludes_str != NULL)
2044 g_string_free(excludes_str, TRUE);
2049 static int set_ipconfig(struct connman_service *service,
2050 struct connman_ipconfig *ipconfig,
2051 DBusMessageIter *array,
2052 enum connman_service_state state,
2053 enum connman_service_state *new_state)
2055 enum connman_ipconfig_method old_method;
2056 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2057 enum connman_ipconfig_type type;
2060 old_method = __connman_ipconfig_get_method(ipconfig);
2062 if (is_connecting_state(service, state) ||
2063 is_connected_state(service, state))
2064 __connman_network_clear_ipconfig(service->network, ipconfig);
2066 err = __connman_ipconfig_set_config(ipconfig, array);
2067 method = __connman_ipconfig_get_method(ipconfig);
2068 type = __connman_ipconfig_get_config_type(ipconfig);
2070 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2071 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2072 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2073 *new_state = service->state_ipv4 =
2074 CONNMAN_SERVICE_STATE_CONFIGURATION;
2075 __connman_ipconfig_enable(ipconfig);
2078 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2079 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2080 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2081 *new_state = service->state_ipv6;
2082 __connman_ipconfig_enable(ipconfig);
2086 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2087 type, method, state2string(*new_state));
2092 static DBusMessage *set_property(DBusConnection *conn,
2093 DBusMessage *msg, void *user_data)
2095 struct connman_service *service = user_data;
2096 DBusMessageIter iter, value;
2100 DBG("service %p", service);
2102 if (dbus_message_iter_init(msg, &iter) == FALSE)
2103 return __connman_error_invalid_arguments(msg);
2105 dbus_message_iter_get_basic(&iter, &name);
2106 dbus_message_iter_next(&iter);
2107 dbus_message_iter_recurse(&iter, &value);
2109 type = dbus_message_iter_get_arg_type(&value);
2111 if (g_str_equal(name, "AutoConnect") == TRUE) {
2112 connman_bool_t autoconnect;
2114 if (type != DBUS_TYPE_BOOLEAN)
2115 return __connman_error_invalid_arguments(msg);
2117 if (service->favorite == FALSE)
2118 return __connman_error_invalid_service(msg);
2120 dbus_message_iter_get_basic(&value, &autoconnect);
2122 if (service->autoconnect == autoconnect)
2123 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2125 service->autoconnect = autoconnect;
2127 autoconnect_changed(service);
2129 __connman_storage_save_service(service);
2130 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2131 const char *passphrase;
2133 if (type != DBUS_TYPE_STRING)
2134 return __connman_error_invalid_arguments(msg);
2136 if (service->immutable == TRUE)
2137 return __connman_error_not_supported(msg);
2139 dbus_message_iter_get_basic(&value, &passphrase);
2141 __connman_service_set_passphrase(service, passphrase);
2142 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2143 DBusMessageIter entry;
2148 if (type != DBUS_TYPE_ARRAY)
2149 return __connman_error_invalid_arguments(msg);
2151 str = g_string_new(NULL);
2153 return __connman_error_invalid_arguments(msg);
2155 index = connman_network_get_index(service->network);
2156 gw = __connman_ipconfig_get_gateway_from_index(index);
2158 if (gw && strlen(gw))
2159 __connman_service_nameserver_del_routes(service);
2161 dbus_message_iter_recurse(&value, &entry);
2163 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2165 dbus_message_iter_get_basic(&entry, &val);
2166 dbus_message_iter_next(&entry);
2168 g_string_append_printf(str, " %s", val);
2170 g_string_append(str, val);
2173 g_strfreev(service->nameservers_config);
2176 service->nameservers_config =
2177 g_strsplit_set(str->str, " ", 0);
2179 service->nameservers_config = NULL;
2182 g_string_free(str, TRUE);
2184 if (gw && strlen(gw))
2185 __connman_service_nameserver_add_routes(service, gw);
2187 update_nameservers(service);
2188 dns_configuration_changed(service);
2190 __connman_storage_save_service(service);
2191 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2192 DBusMessageIter entry;
2195 if (type != DBUS_TYPE_ARRAY)
2196 return __connman_error_invalid_arguments(msg);
2198 str = g_string_new(NULL);
2200 return __connman_error_invalid_arguments(msg);
2202 dbus_message_iter_recurse(&value, &entry);
2204 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2206 dbus_message_iter_get_basic(&entry, &val);
2207 dbus_message_iter_next(&entry);
2209 g_string_append_printf(str, " %s", val);
2211 g_string_append(str, val);
2214 g_strfreev(service->domains);
2217 service->domains = g_strsplit_set(str->str, " ", 0);
2219 service->domains = NULL;
2221 g_string_free(str, TRUE);
2223 update_nameservers(service);
2224 domain_configuration_changed(service);
2226 __connman_storage_save_service(service);
2227 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2230 if (type != DBUS_TYPE_ARRAY)
2231 return __connman_error_invalid_arguments(msg);
2233 err = update_proxy_configuration(service, &value);
2236 return __connman_error_failed(msg, -err);
2238 proxy_configuration_changed(service);
2240 __connman_storage_save_service(service);
2241 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2242 g_str_equal(name, "IPv6.Configuration")) {
2244 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2245 enum connman_service_state state =
2246 CONNMAN_SERVICE_STATE_UNKNOWN;
2251 if (service->ipconfig_ipv4 == NULL &&
2252 service->ipconfig_ipv6 == NULL)
2253 return __connman_error_invalid_property(msg);
2255 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2256 ipv4 = service->ipconfig_ipv4;
2257 err = set_ipconfig(service, ipv4, &value,
2258 service->state_ipv4, &state);
2260 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2261 ipv6 = service->ipconfig_ipv6;
2262 err = set_ipconfig(service, ipv6, &value,
2263 service->state_ipv6, &state);
2267 if (is_connected_state(service, state) ||
2268 is_connecting_state(service, state))
2269 __connman_network_set_ipconfig(service->network,
2271 return __connman_error_failed(msg, -err);
2275 ipv4_configuration_changed(service);
2277 ipv6_configuration_changed(service);
2279 if (is_connecting(service) || is_connected(service))
2280 __connman_network_set_ipconfig(service->network,
2283 __connman_storage_save_service(service);
2285 return __connman_error_invalid_property(msg);
2287 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2290 static void set_idle(struct connman_service *service)
2292 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
2293 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2294 state_changed(service);
2297 static DBusMessage *clear_property(DBusConnection *conn,
2298 DBusMessage *msg, void *user_data)
2300 struct connman_service *service = user_data;
2303 DBG("service %p", service);
2305 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2308 if (g_str_equal(name, "Error") == TRUE) {
2311 g_get_current_time(&service->modified);
2312 __connman_storage_save_service(service);
2313 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2314 if (service->immutable == TRUE)
2315 return __connman_error_not_supported(msg);
2317 g_free(service->passphrase);
2318 service->passphrase = NULL;
2320 passphrase_changed(service);
2322 __connman_storage_save_service(service);
2324 return __connman_error_invalid_property(msg);
2326 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2329 static connman_bool_t is_ignore(struct connman_service *service)
2331 if (service->autoconnect == FALSE)
2334 if (service->roaming == TRUE)
2337 if (service->ignore == TRUE)
2340 if (combine_state(service->state_ipv4, service->state_ipv6) ==
2341 CONNMAN_SERVICE_STATE_FAILURE)
2347 void __connman_service_auto_connect(void)
2349 struct connman_service *service = NULL;
2350 GSequenceIter *iter;
2354 iter = g_sequence_get_begin_iter(service_list);
2356 while (g_sequence_iter_is_end(iter) == FALSE) {
2357 service = g_sequence_get(iter);
2359 if (service->pending != NULL)
2362 if (is_connecting(service) == TRUE)
2365 if (service->favorite == FALSE)
2368 if (is_connected(service) == TRUE)
2371 if (is_ignore(service) == FALSE &&
2372 combine_state(service->state_ipv4,
2373 service->state_ipv6) ==
2374 CONNMAN_SERVICE_STATE_IDLE)
2379 iter = g_sequence_iter_next(iter);
2382 if (service != NULL) {
2383 service->userconnect = FALSE;
2384 __connman_service_connect(service);
2388 static void remove_timeout(struct connman_service *service)
2390 if (service->timeout > 0) {
2391 g_source_remove(service->timeout);
2392 service->timeout = 0;
2396 static void reply_pending(struct connman_service *service, int error)
2398 remove_timeout(service);
2400 if (service->pending != NULL) {
2404 reply = __connman_error_failed(service->pending,
2407 g_dbus_send_message(connection, reply);
2409 g_dbus_send_reply(connection, service->pending,
2412 dbus_message_unref(service->pending);
2413 service->pending = NULL;
2417 static gboolean connect_timeout(gpointer user_data)
2419 struct connman_service *service = user_data;
2420 connman_bool_t autoconnect = FALSE;
2422 DBG("service %p", service);
2424 service->timeout = 0;
2426 if (service->network != NULL)
2427 __connman_network_disconnect(service->network);
2429 __connman_ipconfig_disable(service->ipconfig_ipv4);
2430 __connman_ipconfig_disable(service->ipconfig_ipv6);
2432 __connman_stats_service_unregister(service);
2434 if (service->pending != NULL) {
2437 reply = __connman_error_operation_timeout(service->pending);
2439 g_dbus_send_message(connection, reply);
2441 dbus_message_unref(service->pending);
2442 service->pending = NULL;
2446 __connman_service_indicate_state(service,
2447 CONNMAN_SERVICE_STATE_FAILURE,
2448 CONNMAN_IPCONFIG_TYPE_IPV4);
2449 __connman_service_indicate_state(service,
2450 CONNMAN_SERVICE_STATE_FAILURE,
2451 CONNMAN_IPCONFIG_TYPE_IPV6);
2453 if (autoconnect == TRUE && service->userconnect == FALSE)
2454 __connman_service_auto_connect();
2459 static void set_reconnect_state(struct connman_service *service,
2460 connman_bool_t reconnect)
2462 struct connman_device *device;
2464 if (service->network == NULL)
2467 device = connman_network_get_device(service->network);
2471 __connman_device_set_reconnect(device, reconnect);
2474 static connman_bool_t get_reconnect_state(struct connman_service *service)
2476 struct connman_device *device;
2478 if (service->network == NULL)
2481 device = connman_network_get_device(service->network);
2485 return __connman_device_get_reconnect(device);
2488 static void request_input_cb (struct connman_service *service,
2489 const char *passphrase, void *user_data)
2491 DBG ("RequestInput return, %p", service);
2493 if (passphrase == NULL && service->wps == FALSE)
2496 if (passphrase != NULL)
2497 __connman_service_set_passphrase(service, passphrase);
2499 __connman_service_connect(service);
2502 struct connman_service *
2503 __connman_service_connect_type(enum connman_service_type type)
2505 struct connman_service *service;
2506 GSequenceIter *iter;
2509 DBG("type %d", type);
2512 * We go through the already sorted service list.
2513 * We pick the first one matching our type, or just
2514 * the first available one if we have no type.
2516 iter = g_sequence_get_begin_iter(service_list);
2517 if (g_sequence_iter_is_end(iter))
2519 service = g_sequence_get(iter);
2522 * If the first service is connected or about to be
2523 * connected, we return it, regardless of the type.
2525 if ((g_sequence_iter_is_end(iter) == FALSE) &&
2526 (is_connecting(service) == TRUE ||
2527 is_connected(service) == TRUE))
2530 while (g_sequence_iter_is_end(iter) == FALSE) {
2531 if (service->type == type ||
2532 type == CONNMAN_SERVICE_TYPE_UNKNOWN)
2535 iter = g_sequence_iter_next(iter);
2536 service = g_sequence_get(iter);
2539 if (g_sequence_iter_is_end(iter))
2542 service->ignore = FALSE;
2544 service->userconnect = TRUE;
2546 set_reconnect_state(service, FALSE);
2548 err = __connman_service_connect(service);
2551 if ( __connman_agent_request_input(service,
2556 if (err != -EINPROGRESS)
2563 static DBusMessage *connect_service(DBusConnection *conn,
2564 DBusMessage *msg, void *user_data)
2566 struct connman_service *service = user_data;
2567 GSequenceIter *iter;
2570 DBG("service %p", service);
2572 if (service->pending != NULL)
2573 return __connman_error_in_progress(msg);
2575 iter = g_sequence_get_begin_iter(service_list);
2577 while (g_sequence_iter_is_end(iter) == FALSE) {
2578 struct connman_service *temp = g_sequence_get(iter);
2580 if (service->type == temp->type && is_connecting(temp) == TRUE)
2581 return __connman_error_in_progress(msg);
2583 iter = g_sequence_iter_next(iter);
2586 service->ignore = FALSE;
2588 service->userconnect = TRUE;
2590 service->pending = dbus_message_ref(msg);
2592 set_reconnect_state(service, FALSE);
2594 err = __connman_service_connect(service);
2596 if (err == -ENOKEY) {
2597 if (__connman_agent_request_input(service,
2603 if (service->pending == NULL)
2606 if (err != -EINPROGRESS) {
2607 dbus_message_unref(service->pending);
2608 service->pending = NULL;
2610 return __connman_error_failed(msg, -err);
2616 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2619 static DBusMessage *disconnect_service(DBusConnection *conn,
2620 DBusMessage *msg, void *user_data)
2622 struct connman_service *service = user_data;
2625 DBG("service %p", service);
2627 reply_pending(service, ECONNABORTED);
2629 service->ignore = TRUE;
2631 set_reconnect_state(service, FALSE);
2633 err = __connman_service_disconnect(service);
2635 if (err != -EINPROGRESS)
2636 return __connman_error_failed(msg, -err);
2641 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2644 static DBusMessage *remove_service(DBusConnection *conn,
2645 DBusMessage *msg, void *user_data)
2647 struct connman_service *service = user_data;
2649 DBG("service %p", service);
2651 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2652 return __connman_error_not_supported(msg);
2654 if (service->immutable == TRUE)
2655 return __connman_error_not_supported(msg);
2657 if (service->favorite == FALSE &&
2658 combine_state(service->state_ipv4, service->state_ipv6) !=
2659 CONNMAN_SERVICE_STATE_FAILURE)
2660 return __connman_error_not_supported(msg);
2662 if (service->network != NULL) {
2663 set_reconnect_state(service, FALSE);
2665 __connman_network_disconnect(service->network);
2668 g_hash_table_destroy(service->counter_table);
2670 g_free(service->passphrase);
2671 service->passphrase = NULL;
2673 passphrase_changed(service);
2677 __connman_service_set_favorite(service, FALSE);
2678 __connman_storage_save_service(service);
2680 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2683 static DBusMessage *move_service(DBusConnection *conn,
2684 DBusMessage *msg, void *user_data,
2687 struct connman_service *service = user_data;
2688 struct connman_service *target;
2690 GSequenceIter *src, *dst;
2692 DBG("service %p", service);
2694 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2697 if (service->favorite == FALSE)
2698 return __connman_error_not_supported(msg);
2700 target = find_service(path);
2701 if (target == NULL || target->favorite == FALSE || target == service)
2702 return __connman_error_invalid_service(msg);
2704 DBG("target %s", target->identifier);
2706 if (target->state_ipv4 != service->state_ipv4 &&
2707 target->state_ipv6 != service->state_ipv6)
2708 return __connman_error_invalid_service(msg);
2710 g_get_current_time(&service->modified);
2711 __connman_storage_save_service(service);
2713 src = g_hash_table_lookup(service_hash, service->identifier);
2714 dst = g_hash_table_lookup(service_hash, target->identifier);
2716 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2718 __connman_profile_changed(FALSE);
2720 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2723 static DBusMessage *move_before(DBusConnection *conn,
2724 DBusMessage *msg, void *user_data)
2726 return move_service(conn, msg, user_data, TRUE);
2729 static DBusMessage *move_after(DBusConnection *conn,
2730 DBusMessage *msg, void *user_data)
2732 return move_service(conn, msg, user_data, FALSE);
2735 static DBusMessage *reset_counters(DBusConnection *conn,
2736 DBusMessage *msg, void *user_data)
2738 struct connman_service *service = user_data;
2740 reset_stats(service);
2742 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2745 static GDBusMethodTable service_methods[] = {
2746 { "GetProperties", "", "a{sv}", get_properties },
2747 { "SetProperty", "sv", "", set_property },
2748 { "ClearProperty", "s", "", clear_property },
2749 { "Connect", "", "", connect_service,
2750 G_DBUS_METHOD_FLAG_ASYNC },
2751 { "Disconnect", "", "", disconnect_service },
2752 { "Remove", "", "", remove_service },
2753 { "MoveBefore", "o", "", move_before },
2754 { "MoveAfter", "o", "", move_after },
2755 { "ResetCounters", "", "", reset_counters },
2759 static GDBusSignalTable service_signals[] = {
2760 { "PropertyChanged", "sv" },
2764 static void service_free(gpointer user_data)
2766 struct connman_service *service = user_data;
2767 char *path = service->path;
2769 DBG("service %p", service);
2771 reply_pending(service, ENOENT);
2773 g_hash_table_remove(service_hash, service->identifier);
2775 stats_stop(service);
2776 __connman_storage_save_service(service);
2778 service->path = NULL;
2781 __connman_profile_changed(FALSE);
2783 g_dbus_unregister_interface(connection, path,
2784 CONNMAN_SERVICE_INTERFACE);
2788 if (service->network != NULL)
2789 connman_network_unref(service->network);
2791 if (service->provider != NULL)
2792 connman_provider_unref(service->provider);
2794 if (service->ipconfig_ipv4 != NULL) {
2795 connman_ipconfig_unref(service->ipconfig_ipv4);
2796 service->ipconfig_ipv4 = NULL;
2799 if (service->ipconfig_ipv6 != NULL) {
2800 connman_ipconfig_unref(service->ipconfig_ipv6);
2801 service->ipconfig_ipv6 = NULL;
2804 if (service->location != NULL)
2805 connman_location_unref(service->location);
2807 g_strfreev(service->nameservers);
2808 g_strfreev(service->nameservers_config);
2809 g_strfreev(service->domains);
2810 g_strfreev(service->proxies);
2811 g_strfreev(service->excludes);
2813 g_free(service->domainname);
2814 g_free(service->pac);
2815 g_free(service->profile);
2816 g_free(service->name);
2817 g_free(service->passphrase);
2818 g_free(service->identifier);
2819 g_free(service->eap);
2820 g_free(service->identity);
2821 g_free(service->ca_cert_file);
2822 g_free(service->client_cert_file);
2823 g_free(service->private_key_file);
2824 g_free(service->private_key_passphrase);
2825 g_free(service->phase2);
2827 if (service->stats.timer != NULL)
2828 g_timer_destroy(service->stats.timer);
2829 if (service->stats_roaming.timer != NULL)
2830 g_timer_destroy(service->stats_roaming.timer);
2836 * __connman_service_put:
2837 * @service: service structure
2839 * Release service if no longer needed
2841 void __connman_service_put(struct connman_service *service)
2843 DBG("service %p", service);
2845 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2846 GSequenceIter *iter;
2848 iter = g_hash_table_lookup(service_hash, service->identifier);
2850 reply_pending(service, ECONNABORTED);
2852 __connman_service_disconnect(service);
2854 g_sequence_remove(iter);
2856 service_free(service);
2860 static void stats_init(struct connman_service *service)
2863 service->stats.valid = FALSE;
2864 service->stats.enabled = FALSE;
2865 service->stats.timer = g_timer_new();
2868 service->stats_roaming.valid = FALSE;
2869 service->stats_roaming.enabled = FALSE;
2870 service->stats_roaming.timer = g_timer_new();
2873 static void service_initialize(struct connman_service *service)
2875 DBG("service %p", service);
2877 service->refcount = 1;
2879 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
2880 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
2881 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
2883 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
2884 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
2886 service->favorite = FALSE;
2887 service->immutable = FALSE;
2888 service->hidden = FALSE;
2890 service->ignore = FALSE;
2892 service->userconnect = FALSE;
2896 stats_init(service);
2898 service->provider = NULL;
2900 service->wps = FALSE;
2904 * connman_service_create:
2906 * Allocate a new service.
2908 * Returns: a newly-allocated #connman_service structure
2910 struct connman_service *connman_service_create(void)
2913 struct connman_stats_counter *counters;
2914 const char *counter;
2916 struct connman_service *service;
2918 service = g_try_new0(struct connman_service, 1);
2919 if (service == NULL)
2922 DBG("service %p", service);
2924 service->counter_table = g_hash_table_new_full(g_str_hash,
2925 g_str_equal, NULL, g_free);
2927 for (list = counter_list; list; list = list->next) {
2928 counter = list->data;
2930 counters = g_try_new0(struct connman_stats_counter, 1);
2931 if (counters == NULL) {
2932 g_hash_table_destroy(service->counter_table);
2937 counters->append_all = TRUE;
2939 g_hash_table_replace(service->counter_table, (gpointer)counter,
2943 service_initialize(service);
2945 service->location = __connman_location_create(service);
2950 struct connman_location *__connman_service_get_location(struct connman_service *service)
2952 return service->location;
2956 * connman_service_ref:
2957 * @service: service structure
2959 * Increase reference counter of service
2961 struct connman_service *connman_service_ref(struct connman_service *service)
2965 g_atomic_int_inc(&service->refcount);
2971 * connman_service_unref:
2972 * @service: service structure
2974 * Decrease reference counter of service
2976 void connman_service_unref(struct connman_service *service)
2978 __connman_service_put(service);
2981 static gint service_compare(gconstpointer a, gconstpointer b,
2984 struct connman_service *service_a = (void *) a;
2985 struct connman_service *service_b = (void *) b;
2986 enum connman_service_state state_a, state_b;
2988 state_a = combine_state(service_a->state_ipv4, service_a->state_ipv6);
2989 state_b = combine_state(service_b->state_ipv4, service_b->state_ipv6);
2991 if (state_a != state_b) {
2992 if (is_connected(service_a) == TRUE)
2994 if (is_connected(service_b) == TRUE)
2997 if (is_connecting(service_a) == TRUE)
2999 if (is_connecting(service_b) == TRUE)
3003 if (service_a->order > service_b->order)
3006 if (service_a->order < service_b->order)
3009 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3012 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3015 if (service_a->type != service_b->type) {
3016 switch (service_a->type) {
3017 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3018 case CONNMAN_SERVICE_TYPE_SYSTEM:
3019 case CONNMAN_SERVICE_TYPE_ETHERNET:
3020 case CONNMAN_SERVICE_TYPE_GPS:
3021 case CONNMAN_SERVICE_TYPE_VPN:
3022 case CONNMAN_SERVICE_TYPE_GADGET:
3024 case CONNMAN_SERVICE_TYPE_WIFI:
3026 case CONNMAN_SERVICE_TYPE_WIMAX:
3027 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3028 case CONNMAN_SERVICE_TYPE_CELLULAR:
3033 return (gint) service_b->strength - (gint) service_a->strength;
3037 * connman_service_get_type:
3038 * @service: service structure
3040 * Get the type of service
3042 enum connman_service_type connman_service_get_type(struct connman_service *service)
3044 if (service == NULL)
3045 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3047 return service->type;
3051 * connman_service_get_interface:
3052 * @service: service structure
3054 * Get network interface of service
3056 char *connman_service_get_interface(struct connman_service *service)
3060 if (service == NULL)
3063 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3064 if (service->ipconfig_ipv4)
3065 index = connman_ipconfig_get_index(
3066 service->ipconfig_ipv4);
3067 else if (service->ipconfig_ipv6)
3068 index = connman_ipconfig_get_index(
3069 service->ipconfig_ipv6);
3073 return connman_inet_ifname(index);
3076 if (service->network == NULL)
3079 index = connman_network_get_index(service->network);
3081 return connman_inet_ifname(index);
3085 * connman_service_get_network:
3086 * @service: service structure
3088 * Get the service network
3090 struct connman_network *
3091 __connman_service_get_network(struct connman_service *service)
3093 if (service == NULL)
3096 return service->network;
3099 struct connman_ipconfig *
3100 __connman_service_get_ip4config(struct connman_service *service)
3102 if (service == NULL)
3105 return service->ipconfig_ipv4;
3108 struct connman_ipconfig *
3109 __connman_service_get_ip6config(struct connman_service *service)
3111 if (service == NULL)
3114 return service->ipconfig_ipv6;
3117 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3119 if (service == NULL)
3120 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3122 return service->security;
3125 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3127 if (service == NULL)
3130 return service->wps;
3134 * __connman_service_set_favorite:
3135 * @service: service structure
3136 * @favorite: favorite value
3138 * Change the favorite setting of service
3140 int __connman_service_set_favorite(struct connman_service *service,
3141 connman_bool_t favorite)
3143 GSequenceIter *iter;
3145 iter = g_hash_table_lookup(service_hash, service->identifier);
3149 if (service->favorite == favorite)
3152 service->favorite = favorite;
3154 favorite_changed(service);
3156 g_sequence_sort_changed(iter, service_compare, NULL);
3158 __connman_profile_changed(FALSE);
3163 int __connman_service_set_immutable(struct connman_service *service,
3164 connman_bool_t immutable)
3166 service->immutable = immutable;
3168 immutable_changed(service);
3173 void __connman_service_set_string(struct connman_service *service,
3174 const char *key, const char *value)
3176 if (g_str_equal(key, "EAP") == TRUE) {
3177 g_free(service->eap);
3178 service->eap = g_strdup(value);
3179 } else if (g_str_equal(key, "Identity") == TRUE) {
3180 g_free(service->identity);
3181 service->identity = g_strdup(value);
3182 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3183 g_free(service->ca_cert_file);
3184 service->ca_cert_file = g_strdup(value);
3185 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3186 g_free(service->client_cert_file);
3187 service->client_cert_file = g_strdup(value);
3188 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3189 g_free(service->private_key_file);
3190 service->private_key_file = g_strdup(value);
3191 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3192 g_free(service->private_key_passphrase);
3193 service->private_key_passphrase = g_strdup(value);
3194 } else if (g_str_equal(key, "Phase2") == TRUE) {
3195 g_free(service->phase2);
3196 service->phase2 = g_strdup(value);
3197 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3198 g_free(service->passphrase);
3199 service->passphrase = g_strdup(value);
3203 static void service_complete(struct connman_service *service)
3205 reply_pending(service, EIO);
3207 if (service->userconnect == FALSE)
3208 __connman_service_auto_connect();
3210 g_get_current_time(&service->modified);
3211 __connman_storage_save_service(service);
3214 static void report_error_cb(struct connman_service *service,
3215 gboolean retry, void *user_data)
3218 __connman_service_connect(service);
3220 service_complete(service);
3221 __connman_profile_changed(FALSE);
3222 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3226 int __connman_service_indicate_state(struct connman_service *service,
3227 enum connman_service_state new_state,
3228 enum connman_ipconfig_type type)
3230 enum connman_service_state service_state,
3231 state = CONNMAN_SERVICE_STATE_IDLE;
3232 GSequenceIter *iter;
3234 if (service == NULL)
3237 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3238 if (service->state_ipv4 == new_state)
3240 state = combine_state(new_state, service->state_ipv6);
3241 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3242 if (service->state_ipv6 == new_state)
3244 state = combine_state(new_state, service->state_ipv4);
3248 service_state = combine_state(service->state_ipv4,
3249 service->state_ipv6);
3251 DBG("service %p state %s/%s => %s new %s/%d => %s",
3253 state2string(service->state_ipv4),
3254 state2string(service->state_ipv6),
3255 state2string(service_state),
3256 state2string(new_state),
3258 state2string(state));
3260 if (service_state == CONNMAN_SERVICE_STATE_FAILURE &&
3261 state == CONNMAN_SERVICE_STATE_IDLE)
3264 if (service_state == CONNMAN_SERVICE_STATE_IDLE &&
3265 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3268 if (state == CONNMAN_SERVICE_STATE_IDLE &&
3269 service_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3270 state_changed(service);
3272 reply_pending(service, ECONNABORTED);
3274 __connman_service_disconnect(service);
3277 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3278 if (__connman_stats_service_register(service) == 0) {
3279 __connman_stats_get(service, FALSE,
3280 &service->stats.data);
3281 __connman_stats_get(service, TRUE,
3282 &service->stats_roaming.data);
3285 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
3286 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3287 __connman_ipconfig_enable(service->ipconfig_ipv4);
3288 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
3289 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3290 __connman_ipconfig_enable(service->ipconfig_ipv6);
3293 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3294 service->state_ipv4 = new_state;
3295 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3296 service->state_ipv6 = new_state;
3298 if (state == service_state)
3301 state_changed(service);
3303 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3304 if (service->login_required == TRUE) {
3305 service->login_required = FALSE;
3306 login_changed(service);
3309 connman_timeserver_sync();
3312 if (state == CONNMAN_SERVICE_STATE_IDLE) {
3313 connman_bool_t reconnect;
3315 reconnect = get_reconnect_state(service);
3316 if (reconnect == TRUE)
3317 __connman_service_auto_connect();
3320 if (state == CONNMAN_SERVICE_STATE_READY) {
3321 enum connman_service_proxy_method proxy_config;
3322 enum connman_ipconfig_method method;
3324 set_reconnect_state(service, TRUE);
3326 __connman_service_set_favorite(service, TRUE);
3328 reply_pending(service, 0);
3330 service->userconnect = FALSE;
3332 g_get_current_time(&service->modified);
3333 __connman_storage_save_service(service);
3335 update_nameservers(service);
3336 dns_changed(service);
3337 domain_changed(service);
3339 proxy_config = service->proxy_config;
3342 * We start WPAD if we haven't got a PAC URL from DHCP and
3343 * if our proxy manual configuration is either empty or set
3344 * to AUTO with an empty URL.
3346 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3347 (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3349 CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3350 service->pac == NULL)))
3351 if (__connman_wpad_start(service) < 0) {
3353 CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3355 __connman_notifier_proxy_changed(service);
3358 __connman_notifier_connect(service->type);
3360 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3361 connman_network_get_bool(service->network,
3362 "WiFi.UseWPS") == TRUE) {
3365 pass = connman_network_get_string(service->network,
3368 __connman_service_set_passphrase(service, pass);
3370 connman_network_set_bool(service->network,
3371 "WiFi.UseWPS", FALSE);
3376 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3377 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3378 __connman_ipconfig_disable_ipv6(
3379 service->ipconfig_ipv6);
3381 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3382 __connman_location_finish(service);
3386 __connman_wpad_stop(service);
3388 update_nameservers(service);
3389 dns_changed(service);
3390 domain_changed(service);
3392 __connman_notifier_disconnect(service->type);
3395 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3396 if (service->userconnect == TRUE &&
3397 __connman_agent_report_error(service,
3398 error2string(service->error),
3399 report_error_cb, NULL) == -EIO)
3401 service_complete(service);
3403 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3405 iter = g_hash_table_lookup(service_hash, service->identifier);
3407 g_sequence_sort_changed(iter, service_compare, NULL);
3409 __connman_profile_changed(FALSE);
3411 service_state = combine_state(service->state_ipv4,
3412 service->state_ipv6);
3413 if (service_state == CONNMAN_SERVICE_STATE_ONLINE)
3416 if (service_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3417 struct connman_service *def_service = get_default();
3419 if (__connman_notifier_count_connected() == 0 &&
3420 def_service != NULL &&
3421 def_service->provider != NULL)
3422 __connman_provider_disconnect(def_service->provider);
3425 if (service_state == CONNMAN_SERVICE_STATE_IDLE ||
3426 service_state == CONNMAN_SERVICE_STATE_FAILURE)
3427 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3432 int __connman_service_indicate_error(struct connman_service *service,
3433 enum connman_service_error error)
3435 DBG("service %p error %d", service, error);
3437 if (service == NULL)
3440 service->error = error;
3442 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3443 __connman_service_set_passphrase(service, NULL);
3445 return __connman_service_indicate_state(service,
3446 CONNMAN_SERVICE_STATE_FAILURE,
3447 CONNMAN_IPCONFIG_TYPE_IPV4);
3450 int __connman_service_clear_error(struct connman_service *service)
3452 enum connman_service_state state;
3454 DBG("service %p", service);
3456 if (service == NULL)
3459 state = combine_state(service->state_ipv4, service->state_ipv6);
3461 if (state != CONNMAN_SERVICE_STATE_FAILURE)
3464 service->state_ipv4 = service->state_ipv6 =
3465 CONNMAN_SERVICE_STATE_UNKNOWN;
3466 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3468 if (service->favorite == TRUE)
3469 set_reconnect_state(service, TRUE);
3471 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
3472 CONNMAN_IPCONFIG_TYPE_IPV6);
3474 return __connman_service_indicate_state(service,
3475 CONNMAN_SERVICE_STATE_IDLE,
3476 CONNMAN_IPCONFIG_TYPE_IPV4);
3479 int __connman_service_indicate_default(struct connman_service *service)
3481 DBG("service %p", service);
3485 __connman_location_detect(service);
3490 int __connman_service_request_login(struct connman_service *service)
3492 DBG("service %p", service);
3494 if (service == NULL)
3497 service->login_required = TRUE;
3498 login_changed(service);
3503 static connman_bool_t prepare_network(struct connman_service *service)
3505 enum connman_network_type type;
3506 unsigned int ssid_len;
3508 type = connman_network_get_type(service->network);
3511 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3512 case CONNMAN_NETWORK_TYPE_VENDOR:
3514 case CONNMAN_NETWORK_TYPE_WIFI:
3515 if (connman_network_get_blob(service->network, "WiFi.SSID",
3519 if (service->passphrase != NULL)
3520 connman_network_set_string(service->network,
3521 "WiFi.Passphrase", service->passphrase);
3523 case CONNMAN_NETWORK_TYPE_ETHERNET:
3524 case CONNMAN_NETWORK_TYPE_WIMAX:
3525 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3526 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3527 case CONNMAN_NETWORK_TYPE_CELLULAR:
3534 static void prepare_8021x(struct connman_service *service)
3536 if (service->eap != NULL)
3537 connman_network_set_string(service->network, "WiFi.EAP",
3540 if (service->identity != NULL)
3541 connman_network_set_string(service->network, "WiFi.Identity",
3544 if (service->ca_cert_file != NULL)
3545 connman_network_set_string(service->network, "WiFi.CACertFile",
3546 service->ca_cert_file);
3548 if (service->client_cert_file != NULL)
3549 connman_network_set_string(service->network,
3550 "WiFi.ClientCertFile",
3551 service->client_cert_file);
3553 if (service->private_key_file != NULL)
3554 connman_network_set_string(service->network,
3555 "WiFi.PrivateKeyFile",
3556 service->private_key_file);
3558 if (service->private_key_passphrase != NULL)
3559 connman_network_set_string(service->network,
3560 "WiFi.PrivateKeyPassphrase",
3561 service->private_key_passphrase);
3563 if (service->phase2 != NULL)
3564 connman_network_set_string(service->network, "WiFi.Phase2",
3568 static int service_connect(struct connman_service *service)
3572 switch (service->type) {
3573 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3574 case CONNMAN_SERVICE_TYPE_SYSTEM:
3575 case CONNMAN_SERVICE_TYPE_GPS:
3576 case CONNMAN_SERVICE_TYPE_GADGET:
3578 case CONNMAN_SERVICE_TYPE_ETHERNET:
3579 case CONNMAN_SERVICE_TYPE_WIMAX:
3580 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3581 case CONNMAN_SERVICE_TYPE_CELLULAR:
3582 case CONNMAN_SERVICE_TYPE_VPN:
3584 case CONNMAN_SERVICE_TYPE_WIFI:
3585 switch (service->security) {
3586 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3587 case CONNMAN_SERVICE_SECURITY_NONE:
3589 case CONNMAN_SERVICE_SECURITY_WEP:
3590 case CONNMAN_SERVICE_SECURITY_PSK:
3591 case CONNMAN_SERVICE_SECURITY_WPA:
3592 case CONNMAN_SERVICE_SECURITY_RSN:
3593 if (service->passphrase == NULL) {
3594 if (service->network == NULL)
3597 if (service->wps == FALSE ||
3598 connman_network_get_bool(
3600 "WiFi.UseWPS") == FALSE)
3604 case CONNMAN_SERVICE_SECURITY_8021X:
3610 if (service->network != NULL) {
3611 if (prepare_network(service) == FALSE)
3614 switch (service->security) {
3615 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3616 case CONNMAN_SERVICE_SECURITY_NONE:
3617 case CONNMAN_SERVICE_SECURITY_WEP:
3618 case CONNMAN_SERVICE_SECURITY_PSK:
3619 case CONNMAN_SERVICE_SECURITY_WPA:
3620 case CONNMAN_SERVICE_SECURITY_RSN:
3622 case CONNMAN_SERVICE_SECURITY_8021X:
3623 prepare_8021x(service);
3627 if (__connman_stats_service_register(service) == 0) {
3628 __connman_stats_get(service, FALSE,
3629 &service->stats.data);
3630 __connman_stats_get(service, TRUE,
3631 &service->stats_roaming.data);
3634 if (service->ipconfig_ipv4)
3635 __connman_ipconfig_enable(service->ipconfig_ipv4);
3636 if (service->ipconfig_ipv6)
3637 __connman_ipconfig_enable(service->ipconfig_ipv6);
3639 err = __connman_network_connect(service->network);
3640 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3641 service->provider != NULL)
3642 err = __connman_provider_connect(service->provider);
3647 if (err != -EINPROGRESS) {
3648 __connman_ipconfig_disable(service->ipconfig_ipv4);
3649 __connman_ipconfig_disable(service->ipconfig_ipv6);
3650 __connman_stats_service_unregister(service);
3658 int __connman_service_connect(struct connman_service *service)
3660 enum connman_service_state state;
3663 state = combine_state(service->state_ipv4, service->state_ipv6);
3665 DBG("service %p state %s", service, state2string(state));
3667 if (is_connected(service) == TRUE)
3670 if (is_connecting(service) == TRUE)
3673 switch (service->type) {
3674 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3675 case CONNMAN_SERVICE_TYPE_SYSTEM:
3676 case CONNMAN_SERVICE_TYPE_GPS:
3677 case CONNMAN_SERVICE_TYPE_GADGET:
3680 err = service_connect(service);
3686 if (err == -EINPROGRESS) {
3687 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
3688 connect_timeout, service);
3690 return -EINPROGRESS;
3696 if (service->userconnect == TRUE)
3697 reply_pending(service, err);
3699 __connman_service_indicate_state(service,
3700 CONNMAN_SERVICE_STATE_FAILURE,
3701 CONNMAN_IPCONFIG_TYPE_IPV4);
3702 __connman_service_indicate_state(service,
3703 CONNMAN_SERVICE_STATE_FAILURE,
3704 CONNMAN_IPCONFIG_TYPE_IPV6);
3709 int __connman_service_disconnect(struct connman_service *service)
3713 DBG("service %p", service);
3715 if (service->network != NULL) {
3716 err = __connman_network_disconnect(service->network);
3717 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3718 service->provider != NULL)
3719 err = __connman_provider_disconnect(service->provider);
3723 if (err < 0 && err != -EINPROGRESS)
3726 if (service->ipconfig_ipv4)
3727 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3730 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3733 __connman_ipconfig_clear_address(service->ipconfig_ipv4);
3734 __connman_ipconfig_clear_address(service->ipconfig_ipv6);
3736 __connman_ipconfig_disable(service->ipconfig_ipv4);
3737 __connman_ipconfig_disable(service->ipconfig_ipv6);
3739 __connman_stats_service_unregister(service);
3745 * __connman_service_lookup:
3746 * @pattern: search pattern
3747 * @path: return object path
3749 * Look up a service path from a search pattern
3751 int __connman_service_lookup(const char *pattern, const char **path)
3753 GHashTableIter iter;
3754 gpointer key, value;
3756 g_hash_table_iter_init(&iter, service_hash);
3758 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
3759 GSequenceIter *iter = value;
3760 struct connman_service *service = g_sequence_get(iter);
3762 if (g_strcmp0(service->identifier, pattern) == 0 ||
3763 g_strcmp0(service->name, pattern) == 0) {
3764 *path = (const char *) service->path;
3773 * lookup_by_identifier:
3774 * @identifier: service identifier
3776 * Look up a service by identifier (reference count will not be increased)
3778 static struct connman_service *lookup_by_identifier(const char *identifier)
3780 GSequenceIter *iter;
3782 iter = g_hash_table_lookup(service_hash, identifier);
3784 return g_sequence_get(iter);
3789 static struct connman_network *create_hidden_wifi(struct connman_device *device,
3790 const char *ssid, const char *mode, const char *security)
3792 struct connman_network *network;
3795 unsigned int i, ssid_len;
3797 ssid_len = strlen(ssid);
3801 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
3802 if (network == NULL)
3805 connman_network_set_blob(network, "WiFi.SSID",
3806 (unsigned char *) ssid, ssid_len);
3808 connman_network_set_string(network, "WiFi.Mode", mode);
3809 connman_network_set_string(network, "WiFi.Security", security);
3811 name = g_try_malloc0(ssid_len + 1);
3813 connman_network_unref(network);
3817 for (i = 0; i < ssid_len; i++) {
3818 if (g_ascii_isprint(ssid[i]))
3824 connman_network_set_name(network, name);
3828 index = connman_device_get_index(device);
3829 connman_network_set_index(network, index);
3831 if (connman_device_add_network(device, network) < 0) {
3832 connman_network_unref(network);
3836 connman_network_set_available(network, TRUE);
3841 int __connman_service_create_and_connect(DBusMessage *msg)
3843 struct connman_service *service;
3844 struct connman_network *network;
3845 struct connman_device *device;
3846 DBusMessageIter iter, array;
3847 const char *mode = "managed", *security = "none", *group_security;
3848 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
3849 unsigned int ssid_len = 0;
3852 gboolean created = FALSE;
3855 dbus_message_iter_init(msg, &iter);
3856 dbus_message_iter_recurse(&iter, &array);
3858 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
3859 DBusMessageIter entry, value;
3862 dbus_message_iter_recurse(&array, &entry);
3863 dbus_message_iter_get_basic(&entry, &key);
3865 dbus_message_iter_next(&entry);
3866 dbus_message_iter_recurse(&entry, &value);
3868 switch (dbus_message_iter_get_arg_type(&value)) {
3869 case DBUS_TYPE_STRING:
3870 if (g_str_equal(key, "Type") == TRUE)
3871 dbus_message_iter_get_basic(&value, &type);
3872 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
3873 g_str_equal(key, "Mode") == TRUE)
3874 dbus_message_iter_get_basic(&value, &mode);
3875 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
3876 g_str_equal(key, "Security") == TRUE)
3877 dbus_message_iter_get_basic(&value, &security);
3878 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
3879 g_str_equal(key, "Passphrase") == TRUE)
3880 dbus_message_iter_get_basic(&value, &passphrase);
3881 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
3882 g_str_equal(key, "SSID") == TRUE)
3883 dbus_message_iter_get_basic(&value, &ssid);
3886 dbus_message_iter_next(&array);
3892 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
3898 ssid_len = strlen(ssid);
3902 if (g_strcmp0(security, "none") != 0 &&
3903 g_strcmp0(security, "wep") != 0 &&
3904 g_strcmp0(security, "psk") != 0 &&
3905 g_strcmp0(security, "wpa") != 0 &&
3906 g_strcmp0(security, "rsn") != 0 &&
3907 g_strcmp0(security, "ieee8021x") != 0)
3910 device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
3914 ident = connman_device_get_ident(device);
3919 if (!g_strcmp0(security, "wpa") ||
3920 !g_strcmp0(security, "rsn"))
3921 group_security = "psk";
3923 group_security = security;
3925 group = connman_wifi_build_group_name((unsigned char *) ssid,
3926 ssid_len, mode, group_security);
3930 name = g_strdup_printf("%s_%s_%s", type, ident, group);
3932 service = lookup_by_identifier(name);
3934 if (service != NULL)
3937 network = create_hidden_wifi(device, ssid, mode, security);
3938 if (network != NULL) {
3939 connman_network_set_group(network, group);
3943 service = lookup_by_identifier(name);
3949 if (service == NULL) {
3954 if (is_connected(service) == TRUE) {
3959 if (is_connecting(service) == TRUE) {
3964 set_reconnect_state(service, FALSE);
3966 __connman_device_disconnect(device);
3968 if (passphrase != NULL) {
3969 g_free(service->passphrase);
3970 service->passphrase = g_strdup(passphrase);
3973 service->userconnect = TRUE;
3975 err = __connman_service_connect(service);
3976 if (err < 0 && err != -EINPROGRESS)
3979 g_dbus_send_reply(connection, msg,
3980 DBUS_TYPE_OBJECT_PATH, &service->path,
3986 if (service != NULL && created == TRUE) {
3987 struct connman_network *network = service->network;
3989 if (network != NULL) {
3990 connman_network_set_available(network, FALSE);
3991 __connman_device_cleanup_networks(device);
3993 __connman_service_put(service);
4000 * __connman_service_get:
4001 * @identifier: service identifier
4003 * Look up a service by identifier or create a new one if not found
4005 static struct connman_service *service_get(const char *identifier)
4007 struct connman_service *service;
4008 GSequenceIter *iter;
4010 iter = g_hash_table_lookup(service_hash, identifier);
4012 service = g_sequence_get(iter);
4013 if (service != NULL)
4014 connman_service_ref(service);
4018 service = connman_service_create();
4019 if (service == NULL)
4022 DBG("service %p", service);
4024 service->identifier = g_strdup(identifier);
4026 service->profile = g_strdup(__connman_profile_active_ident());
4028 iter = g_sequence_insert_sorted(service_list, service,
4029 service_compare, NULL);
4031 g_hash_table_insert(service_hash, service->identifier, iter);
4036 static int service_register(struct connman_service *service)
4038 const char *path = __connman_profile_active_path();
4039 GSequenceIter *iter;
4041 DBG("service %p", service);
4043 if (service->path != NULL)
4046 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4048 DBG("path %s", service->path);
4050 __connman_config_provision_service(service);
4052 __connman_storage_load_service(service);
4054 g_dbus_register_interface(connection, service->path,
4055 CONNMAN_SERVICE_INTERFACE,
4056 service_methods, service_signals,
4057 NULL, service, NULL);
4059 iter = g_hash_table_lookup(service_hash, service->identifier);
4061 g_sequence_sort_changed(iter, service_compare, NULL);
4063 __connman_profile_changed(TRUE);
4068 static void service_up(struct connman_ipconfig *ipconfig)
4070 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4072 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
4074 link_changed(service);
4076 service->stats.valid = FALSE;
4077 service->stats_roaming.valid = FALSE;
4080 static void service_down(struct connman_ipconfig *ipconfig)
4082 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
4085 static void service_lower_up(struct connman_ipconfig *ipconfig)
4087 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4089 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4091 stats_start(service);
4094 static void service_lower_down(struct connman_ipconfig *ipconfig)
4096 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4098 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4100 stats_stop(service);
4101 __connman_storage_save_service(service);
4104 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4106 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4107 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4108 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4110 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4112 type = __connman_ipconfig_get_config_type(ipconfig);
4113 method = __connman_ipconfig_get_method(ipconfig);
4115 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4118 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4119 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4120 __connman_service_indicate_state(service,
4121 CONNMAN_SERVICE_STATE_READY,
4122 CONNMAN_IPCONFIG_TYPE_IPV6);
4124 settings_changed(service);
4127 static void service_ip_release(struct connman_ipconfig *ipconfig)
4129 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4130 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4131 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4133 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4135 type = __connman_ipconfig_get_config_type(ipconfig);
4136 method = __connman_ipconfig_get_method(ipconfig);
4138 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4141 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4142 method == CONNMAN_IPCONFIG_METHOD_OFF)
4143 __connman_service_indicate_state(service,
4144 CONNMAN_SERVICE_STATE_DISCONNECT,
4145 CONNMAN_IPCONFIG_TYPE_IPV6);
4147 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4148 method == CONNMAN_IPCONFIG_METHOD_OFF)
4149 __connman_service_indicate_state(service,
4150 CONNMAN_SERVICE_STATE_DISCONNECT,
4151 CONNMAN_IPCONFIG_TYPE_IPV4);
4153 settings_changed(service);
4156 static const struct connman_ipconfig_ops service_ops = {
4158 .down = service_down,
4159 .lower_up = service_lower_up,
4160 .lower_down = service_lower_down,
4161 .ip_bound = service_ip_bound,
4162 .ip_release = service_ip_release,
4165 static void setup_ip4config(struct connman_service *service, int index)
4170 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4171 CONNMAN_IPCONFIG_TYPE_IPV4);
4172 if (service->ipconfig_ipv4 == NULL)
4175 connman_ipconfig_set_method(service->ipconfig_ipv4,
4176 CONNMAN_IPCONFIG_METHOD_DHCP);
4178 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4180 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4183 static void setup_ip6config(struct connman_service *service, int index)
4188 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4189 CONNMAN_IPCONFIG_TYPE_IPV6);
4190 if (service->ipconfig_ipv6 == NULL)
4193 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4195 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4198 void __connman_service_create_ip4config(struct connman_service *service,
4201 const char *ident = service->profile;
4204 DBG("ipv4 %p", service->ipconfig_ipv4);
4206 if (service->ipconfig_ipv4 != NULL)
4209 setup_ip4config(service, index);
4214 keyfile = __connman_storage_open_profile(ident);
4215 if (keyfile == NULL)
4218 if (service->ipconfig_ipv4)
4219 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4220 service->identifier, "IPv4.");
4221 g_key_file_free(keyfile);
4224 void __connman_service_create_ip6config(struct connman_service *service,
4227 const char *ident = service->profile;
4230 DBG("ipv6 %p", service->ipconfig_ipv6);
4232 if (service->ipconfig_ipv6 != NULL)
4235 setup_ip6config(service, index);
4240 keyfile = __connman_storage_open_profile(ident);
4241 if (keyfile == NULL)
4244 if (service->ipconfig_ipv6 != NULL)
4245 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4246 service->identifier, "IPv6.");
4248 g_key_file_free(keyfile);
4252 * __connman_service_lookup_from_network:
4253 * @network: network structure
4255 * Look up a service by network (reference count will not be increased)
4257 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4259 struct connman_service *service;
4260 const char *ident, *group;
4263 DBG("network %p", network);
4265 ident = __connman_network_get_ident(network);
4269 group = connman_network_get_group(network);
4273 name = g_strdup_printf("%s_%s_%s",
4274 __connman_network_get_type(network), ident, group);
4275 service = lookup_by_identifier(name);
4281 struct connman_service *__connman_service_lookup_from_index(int index)
4283 struct connman_service *service;
4284 GSequenceIter *iter;
4286 iter = g_sequence_get_begin_iter(service_list);
4288 while (g_sequence_iter_is_end(iter) == FALSE) {
4289 service = g_sequence_get(iter);
4291 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4295 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4299 iter = g_sequence_iter_next(iter);
4305 const char *__connman_service_get_ident(struct connman_service *service)
4307 return service->identifier;
4310 const char *__connman_service_get_path(struct connman_service *service)
4312 return service->path;
4315 unsigned int __connman_service_get_order(struct connman_service *service)
4317 GSequenceIter *iter;
4319 if (service == NULL)
4322 if (service->favorite == FALSE) {
4327 iter = g_hash_table_lookup(service_hash, service->identifier);
4329 if (g_sequence_iter_get_position(iter) == 0)
4331 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4332 service->order = 10;
4338 return service->order;
4341 static enum connman_service_type convert_network_type(struct connman_network *network)
4343 enum connman_network_type type = connman_network_get_type(network);
4346 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4347 case CONNMAN_NETWORK_TYPE_VENDOR:
4349 case CONNMAN_NETWORK_TYPE_ETHERNET:
4350 return CONNMAN_SERVICE_TYPE_ETHERNET;
4351 case CONNMAN_NETWORK_TYPE_WIFI:
4352 return CONNMAN_SERVICE_TYPE_WIFI;
4353 case CONNMAN_NETWORK_TYPE_WIMAX:
4354 return CONNMAN_SERVICE_TYPE_WIMAX;
4355 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4356 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4357 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4358 case CONNMAN_NETWORK_TYPE_CELLULAR:
4359 return CONNMAN_SERVICE_TYPE_CELLULAR;
4362 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4365 static enum connman_service_mode convert_wifi_mode(const char *mode)
4368 return CONNMAN_SERVICE_MODE_UNKNOWN;
4369 else if (g_str_equal(mode, "managed") == TRUE)
4370 return CONNMAN_SERVICE_MODE_MANAGED;
4371 else if (g_str_equal(mode, "adhoc") == TRUE)
4372 return CONNMAN_SERVICE_MODE_ADHOC;
4374 return CONNMAN_SERVICE_MODE_UNKNOWN;
4377 static enum connman_service_security convert_wifi_security(const char *security)
4379 if (security == NULL)
4380 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4381 else if (g_str_equal(security, "none") == TRUE)
4382 return CONNMAN_SERVICE_SECURITY_NONE;
4383 else if (g_str_equal(security, "wep") == TRUE)
4384 return CONNMAN_SERVICE_SECURITY_WEP;
4385 else if (g_str_equal(security, "psk") == TRUE)
4386 return CONNMAN_SERVICE_SECURITY_PSK;
4387 else if (g_str_equal(security, "ieee8021x") == TRUE)
4388 return CONNMAN_SERVICE_SECURITY_8021X;
4389 else if (g_str_equal(security, "wpa") == TRUE)
4390 return CONNMAN_SERVICE_SECURITY_WPA;
4391 else if (g_str_equal(security, "rsn") == TRUE)
4392 return CONNMAN_SERVICE_SECURITY_RSN;
4394 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4397 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
4402 return CONNMAN_SERVICE_MODE_GPRS;
4404 return CONNMAN_SERVICE_MODE_EDGE;
4409 return CONNMAN_SERVICE_MODE_UMTS;
4412 return CONNMAN_SERVICE_MODE_UNKNOWN;
4415 static void update_from_network(struct connman_service *service,
4416 struct connman_network *network)
4418 connman_uint8_t strength = service->strength;
4419 GSequenceIter *iter;
4422 DBG("service %p network %p", service, network);
4424 if (is_connected(service) == TRUE)
4427 if (is_connecting(service) == TRUE)
4430 str = connman_network_get_string(network, "Name");
4432 g_free(service->name);
4433 service->name = g_strdup(str);
4434 service->hidden = FALSE;
4436 g_free(service->name);
4437 service->name = NULL;
4438 service->hidden = TRUE;
4441 service->strength = connman_network_get_uint8(network, "Strength");
4442 service->roaming = connman_network_get_bool(network, "Roaming");
4444 if (service->strength == 0) {
4446 * Filter out 0-values; it's unclear what they mean
4447 * and they cause anomalous sorting of the priority list.
4449 service->strength = strength;
4452 str = connman_network_get_string(network, "WiFi.Mode");
4453 service->mode = convert_wifi_mode(str);
4455 str = connman_network_get_string(network, "WiFi.Security");
4456 service->security = convert_wifi_security(str);
4458 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
4459 connman_uint8_t value = connman_network_get_uint8(network,
4462 service->mode = convert_cellular_mode(value);
4463 } else if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4464 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4466 if (service->strength > strength && service->network != NULL) {
4467 connman_network_unref(service->network);
4468 service->network = connman_network_ref(network);
4470 strength_changed(service);
4473 if (service->network == NULL)
4474 service->network = connman_network_ref(network);
4476 iter = g_hash_table_lookup(service_hash, service->identifier);
4478 g_sequence_sort_changed(iter, service_compare, NULL);
4482 * __connman_service_create_from_network:
4483 * @network: network structure
4485 * Look up service by network and if not found, create one
4487 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4489 struct connman_service *service;
4490 struct connman_device *device;
4491 const char *ident, *group;
4495 DBG("network %p", network);
4497 ident = __connman_network_get_ident(network);
4501 group = connman_network_get_group(network);
4505 name = g_strdup_printf("%s_%s_%s",
4506 __connman_network_get_type(network), ident, group);
4507 service = service_get(name);
4510 if (service == NULL)
4513 if (__connman_network_get_weakness(network) == TRUE)
4516 if (service->path != NULL) {
4517 update_from_network(service, network);
4518 __connman_profile_changed(TRUE);
4522 service->type = convert_network_type(network);
4524 switch (service->type) {
4525 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4526 case CONNMAN_SERVICE_TYPE_SYSTEM:
4527 case CONNMAN_SERVICE_TYPE_ETHERNET:
4528 case CONNMAN_SERVICE_TYPE_WIMAX:
4529 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4530 case CONNMAN_SERVICE_TYPE_GPS:
4531 case CONNMAN_SERVICE_TYPE_VPN:
4532 case CONNMAN_SERVICE_TYPE_GADGET:
4533 service->autoconnect = FALSE;
4535 case CONNMAN_SERVICE_TYPE_WIFI:
4536 case CONNMAN_SERVICE_TYPE_CELLULAR:
4537 service->autoconnect = TRUE;
4541 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4543 update_from_network(service, network);
4545 index = connman_network_get_index(network);
4547 if (service->ipconfig_ipv4 == NULL)
4548 setup_ip4config(service, index);
4550 if (service->ipconfig_ipv6 == NULL)
4551 setup_ip6config(service, index);
4553 service_register(service);
4555 if (service->favorite == TRUE) {
4556 device = connman_network_get_device(service->network);
4557 if (device && __connman_device_scanning(device) == FALSE)
4558 __connman_service_auto_connect();
4564 void __connman_service_update_from_network(struct connman_network *network)
4566 struct connman_service *service;
4567 enum connman_service_mode mode;
4568 connman_uint8_t strength, value;
4569 connman_bool_t roaming;
4570 GSequenceIter *iter;
4572 connman_bool_t stats_enable;
4574 DBG("network %p", network);
4576 service = __connman_service_lookup_from_network(network);
4577 if (service == NULL)
4580 if (service->network == NULL)
4583 name = connman_network_get_string(service->network, "Name");
4584 if (g_strcmp0(service->name, name) != 0) {
4585 g_free(service->name);
4586 service->name = g_strdup(name);
4587 connman_dbus_property_changed_basic(service->path,
4588 CONNMAN_SERVICE_INTERFACE, "Name",
4589 DBUS_TYPE_STRING, &service->name);
4592 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4593 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4595 strength = connman_network_get_uint8(service->network, "Strength");
4596 if (strength == service->strength)
4599 service->strength = strength;
4601 strength_changed(service);
4604 roaming = connman_network_get_bool(service->network, "Roaming");
4605 if (roaming == service->roaming)
4608 stats_enable = stats_enabled(service);
4609 if (stats_enable == TRUE)
4610 stats_stop(service);
4612 service->roaming = roaming;
4614 if (stats_enable == TRUE)
4615 stats_start(service);
4617 roaming_changed(service);
4619 iter = g_hash_table_lookup(service_hash, service->identifier);
4621 g_sequence_sort_changed(iter, service_compare, NULL);
4624 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
4627 value = connman_network_get_uint8(service->network, "Cellular.Mode");
4628 mode = convert_cellular_mode(value);
4630 if (mode == service->mode)
4633 service->mode = mode;
4635 mode_changed(service);
4638 void __connman_service_remove_from_network(struct connman_network *network)
4640 struct connman_service *service;
4642 DBG("network %p", network);
4644 service = __connman_service_lookup_from_network(network);
4645 if (service == NULL)
4648 __connman_service_put(service);
4652 * __connman_service_create_from_provider:
4653 * @provider: provider structure
4655 * Look up service by provider and if not found, create one
4657 struct connman_service *
4658 __connman_service_create_from_provider(struct connman_provider *provider)
4660 struct connman_service *service;
4661 const char *ident, *str;
4663 int index = connman_provider_get_index(provider);
4665 DBG("provider %p", provider);
4667 ident = __connman_provider_get_ident(provider);
4671 name = g_strdup_printf("vpn_%s", ident);
4672 service = service_get(name);
4675 if (service == NULL)
4678 service->type = CONNMAN_SERVICE_TYPE_VPN;
4679 service->provider = connman_provider_ref(provider);
4680 service->autoconnect = FALSE;
4682 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4684 str = connman_provider_get_string(provider, "Name");
4686 g_free(service->name);
4687 service->name = g_strdup(str);
4688 service->hidden = FALSE;
4690 g_free(service->name);
4691 service->name = NULL;
4692 service->hidden = TRUE;
4695 service->strength = 0;
4697 if (service->ipconfig_ipv4 == NULL)
4698 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4699 CONNMAN_IPCONFIG_TYPE_IPV4);
4700 if (service->ipconfig_ipv4 == NULL)
4703 connman_ipconfig_set_method(service->ipconfig_ipv4,
4704 CONNMAN_IPCONFIG_METHOD_MANUAL);
4705 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4706 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4708 if (service->ipconfig_ipv6 == NULL)
4709 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4710 CONNMAN_IPCONFIG_TYPE_IPV6);
4711 if (service->ipconfig_ipv6 == NULL)
4714 connman_ipconfig_set_method(service->ipconfig_ipv6,
4715 CONNMAN_IPCONFIG_METHOD_OFF);
4716 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4717 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4719 service_register(service);
4724 static int service_load(struct connman_service *service)
4726 const char *ident = service->profile;
4728 GError *error = NULL;
4729 gchar *pathname, *data = NULL;
4732 connman_bool_t autoconnect;
4733 unsigned int ssid_len;
4736 DBG("service %p", service);
4741 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4742 if (pathname == NULL)
4745 keyfile = g_key_file_new();
4747 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
4754 if (g_key_file_load_from_data(keyfile, data, length,
4755 0, NULL) == FALSE) {
4762 switch (service->type) {
4763 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4764 case CONNMAN_SERVICE_TYPE_SYSTEM:
4765 case CONNMAN_SERVICE_TYPE_ETHERNET:
4766 case CONNMAN_SERVICE_TYPE_GPS:
4767 case CONNMAN_SERVICE_TYPE_VPN:
4768 case CONNMAN_SERVICE_TYPE_GADGET:
4770 case CONNMAN_SERVICE_TYPE_WIFI:
4771 if (service->name == NULL) {
4774 name = g_key_file_get_string(keyfile,
4775 service->identifier, "Name", NULL);
4777 g_free(service->name);
4778 service->name = name;
4781 if (service->network != NULL)
4782 connman_network_set_name(service->network,
4786 if (service->network &&
4787 connman_network_get_blob(service->network,
4788 "WiFi.SSID", &ssid_len) == NULL) {
4791 hex_ssid = g_key_file_get_string(keyfile,
4792 service->identifier,
4795 if (hex_ssid != NULL) {
4797 unsigned int i, j = 0, hex;
4798 size_t hex_ssid_len = strlen(hex_ssid);
4800 ssid = g_try_malloc0(hex_ssid_len / 2);
4807 for (i = 0; i < hex_ssid_len; i += 2) {
4808 sscanf(hex_ssid + i, "%02x", &hex);
4812 connman_network_set_blob(service->network,
4813 "WiFi.SSID", ssid, hex_ssid_len / 2);
4820 case CONNMAN_SERVICE_TYPE_WIMAX:
4821 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4822 case CONNMAN_SERVICE_TYPE_CELLULAR:
4823 service->favorite = g_key_file_get_boolean(keyfile,
4824 service->identifier, "Favorite", NULL);
4826 autoconnect = g_key_file_get_boolean(keyfile,
4827 service->identifier, "AutoConnect", &error);
4829 service->autoconnect = autoconnect;
4830 g_clear_error(&error);
4832 str = g_key_file_get_string(keyfile,
4833 service->identifier, "Failure", NULL);
4835 if (service->favorite == FALSE)
4836 service->state_ipv4 = service->state_ipv6 =
4837 CONNMAN_SERVICE_STATE_FAILURE;
4838 service->error = string2error(str);
4843 str = g_key_file_get_string(keyfile,
4844 service->identifier, "Modified", NULL);
4846 g_time_val_from_iso8601(str, &service->modified);
4850 str = g_key_file_get_string(keyfile,
4851 service->identifier, "Passphrase", NULL);
4853 g_free(service->passphrase);
4854 service->passphrase = str;
4857 if (service->ipconfig_ipv4 != NULL)
4858 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4859 service->identifier, "IPv4.");
4861 if (service->ipconfig_ipv6 != NULL)
4862 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4863 service->identifier, "IPv6.");
4865 service->nameservers_config = g_key_file_get_string_list(keyfile,
4866 service->identifier, "Nameservers", &length, NULL);
4867 if (service->nameservers_config != NULL && length == 0) {
4868 g_strfreev(service->nameservers_config);
4869 service->nameservers_config = NULL;
4872 service->domains = g_key_file_get_string_list(keyfile,
4873 service->identifier, "Domains", &length, NULL);
4874 if (service->domains != NULL && length == 0) {
4875 g_strfreev(service->domains);
4876 service->domains = NULL;
4879 str = g_key_file_get_string(keyfile,
4880 service->identifier, "Proxy.Method", NULL);
4882 service->proxy_config = string2proxymethod(str);
4886 service->proxies = g_key_file_get_string_list(keyfile,
4887 service->identifier, "Proxy.Servers", &length, NULL);
4888 if (service->proxies != NULL && length == 0) {
4889 g_strfreev(service->proxies);
4890 service->proxies = NULL;
4893 service->excludes = g_key_file_get_string_list(keyfile,
4894 service->identifier, "Proxy.Excludes", &length, NULL);
4895 if (service->excludes != NULL && length == 0) {
4896 g_strfreev(service->excludes);
4897 service->excludes = NULL;
4900 str = g_key_file_get_string(keyfile,
4901 service->identifier, "Proxy.URL", NULL);
4903 g_free(service->pac);
4908 g_key_file_free(keyfile);
4913 static int service_save(struct connman_service *service)
4915 const char *ident = service->profile;
4917 gchar *pathname, *data = NULL;
4920 const char *cst_str = NULL;
4923 DBG("service %p", service);
4928 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4929 if (pathname == NULL)
4932 keyfile = g_key_file_new();
4934 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
4938 if (g_key_file_load_from_data(keyfile, data, length,
4946 if (service->name != NULL)
4947 g_key_file_set_string(keyfile, service->identifier,
4948 "Name", service->name);
4950 switch (service->type) {
4951 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4952 case CONNMAN_SERVICE_TYPE_SYSTEM:
4953 case CONNMAN_SERVICE_TYPE_ETHERNET:
4954 case CONNMAN_SERVICE_TYPE_GPS:
4955 case CONNMAN_SERVICE_TYPE_VPN:
4956 case CONNMAN_SERVICE_TYPE_GADGET:
4958 case CONNMAN_SERVICE_TYPE_WIFI:
4959 if (service->network) {
4960 const unsigned char *ssid;
4961 unsigned int ssid_len = 0;
4963 ssid = connman_network_get_blob(service->network,
4964 "WiFi.SSID", &ssid_len);
4966 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
4967 char *identifier = service->identifier;
4971 str = g_string_sized_new(ssid_len * 2);
4977 for (i = 0; i < ssid_len; i++)
4978 g_string_append_printf(str,
4981 g_key_file_set_string(keyfile, identifier,
4984 g_string_free(str, TRUE);
4989 case CONNMAN_SERVICE_TYPE_WIMAX:
4990 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4991 case CONNMAN_SERVICE_TYPE_CELLULAR:
4992 g_key_file_set_boolean(keyfile, service->identifier,
4993 "Favorite", service->favorite);
4995 if (service->favorite == TRUE)
4996 g_key_file_set_boolean(keyfile, service->identifier,
4997 "AutoConnect", service->autoconnect);
4999 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5000 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5001 const char *failure = error2string(service->error);
5002 if (failure != NULL)
5003 g_key_file_set_string(keyfile,
5004 service->identifier,
5005 "Failure", failure);
5007 g_key_file_remove_key(keyfile, service->identifier,
5013 str = g_time_val_to_iso8601(&service->modified);
5015 g_key_file_set_string(keyfile, service->identifier,
5020 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5021 g_key_file_set_string(keyfile, service->identifier,
5022 "Passphrase", service->passphrase);
5024 g_key_file_remove_key(keyfile, service->identifier,
5025 "Passphrase", NULL);
5027 if (service->ipconfig_ipv4 != NULL)
5028 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5029 service->identifier, "IPv4.");
5031 if (service->ipconfig_ipv6 != NULL)
5032 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5033 service->identifier, "IPv6.");
5035 if (service->nameservers_config != NULL) {
5036 guint len = g_strv_length(service->nameservers_config);
5038 g_key_file_set_string_list(keyfile, service->identifier,
5040 (const gchar **) service->nameservers_config, len);
5042 g_key_file_remove_key(keyfile, service->identifier,
5043 "Nameservers", NULL);
5045 if (service->domains != NULL) {
5046 guint len = g_strv_length(service->domains);
5048 g_key_file_set_string_list(keyfile, service->identifier,
5050 (const gchar **) service->domains, len);
5052 g_key_file_remove_key(keyfile, service->identifier,
5055 cst_str = proxymethod2string(service->proxy_config);
5056 if (cst_str != NULL)
5057 g_key_file_set_string(keyfile, service->identifier,
5058 "Proxy.Method", cst_str);
5060 if (service->proxies != NULL) {
5061 guint len = g_strv_length(service->proxies);
5063 g_key_file_set_string_list(keyfile, service->identifier,
5065 (const gchar **) service->proxies, len);
5067 g_key_file_remove_key(keyfile, service->identifier,
5068 "Proxy.Servers", NULL);
5070 if (service->excludes != NULL) {
5071 guint len = g_strv_length(service->excludes);
5073 g_key_file_set_string_list(keyfile, service->identifier,
5075 (const gchar **) service->excludes, len);
5077 g_key_file_remove_key(keyfile, service->identifier,
5078 "Proxy.Excludes", NULL);
5080 if (service->pac != NULL && strlen(service->pac) > 0)
5081 g_key_file_set_string(keyfile, service->identifier,
5082 "Proxy.URL", service->pac);
5084 g_key_file_remove_key(keyfile, service->identifier,
5087 data = g_key_file_to_data(keyfile, &length, NULL);
5089 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5090 connman_error("Failed to store service information");
5095 g_key_file_free(keyfile);
5102 static struct connman_storage service_storage = {
5104 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5105 .service_load = service_load,
5106 .service_save = service_save,
5109 int __connman_service_init(void)
5113 connection = connman_dbus_get_connection();
5115 if (connman_storage_register(&service_storage) < 0)
5116 connman_error("Failed to register service storage");
5118 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5121 service_list = g_sequence_new(service_free);
5126 void __connman_service_cleanup(void)
5130 g_sequence_free(service_list);
5131 service_list = NULL;
5133 g_hash_table_destroy(service_hash);
5134 service_hash = NULL;
5136 g_slist_free(counter_list);
5137 counter_list = NULL;
5139 connman_storage_unregister(&service_storage);
5141 dbus_connection_unref(connection);