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 {
56 gint session_usage_count;
59 enum connman_service_type type;
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 connman_bool_t network_created;
79 struct connman_ipconfig *ipconfig_ipv4;
80 struct connman_ipconfig *ipconfig_ipv6;
81 struct connman_network *network;
82 struct connman_provider *provider;
84 char **nameservers_config;
88 /* 802.1x settings from the config files */
92 char *client_cert_file;
93 char *private_key_file;
94 char *private_key_passphrase;
98 struct connman_location *location;
99 struct connman_stats stats;
100 struct connman_stats stats_roaming;
101 GHashTable *counter_table;
102 enum connman_service_proxy_method proxy;
103 enum connman_service_proxy_method proxy_config;
110 static void append_path(gpointer value, gpointer user_data)
112 struct connman_service *service = value;
113 DBusMessageIter *iter = user_data;
115 if (service->path == NULL || service->hidden == TRUE)
118 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
122 void __connman_service_list(DBusMessageIter *iter, void *user_data)
124 g_sequence_foreach(service_list, append_path, iter);
129 struct connman_service *service;
132 static void compare_path(gpointer value, gpointer user_data)
134 struct connman_service *service = value;
135 struct find_data *data = user_data;
137 if (data->service != NULL)
140 if (g_strcmp0(service->path, data->path) == 0)
141 data->service = service;
144 static struct connman_service *find_service(const char *path)
146 struct find_data data = { .path = path, .service = NULL };
148 DBG("path %s", path);
150 g_sequence_foreach(service_list, compare_path, &data);
155 const char *__connman_service_type2string(enum connman_service_type type)
158 case CONNMAN_SERVICE_TYPE_UNKNOWN:
160 case CONNMAN_SERVICE_TYPE_SYSTEM:
162 case CONNMAN_SERVICE_TYPE_ETHERNET:
164 case CONNMAN_SERVICE_TYPE_WIFI:
166 case CONNMAN_SERVICE_TYPE_WIMAX:
168 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
170 case CONNMAN_SERVICE_TYPE_CELLULAR:
172 case CONNMAN_SERVICE_TYPE_GPS:
174 case CONNMAN_SERVICE_TYPE_VPN:
176 case CONNMAN_SERVICE_TYPE_GADGET:
183 static const char *security2string(enum connman_service_security security)
186 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
188 case CONNMAN_SERVICE_SECURITY_NONE:
190 case CONNMAN_SERVICE_SECURITY_WEP:
192 case CONNMAN_SERVICE_SECURITY_PSK:
194 case CONNMAN_SERVICE_SECURITY_8021X:
196 case CONNMAN_SERVICE_SECURITY_WPA:
198 case CONNMAN_SERVICE_SECURITY_RSN:
205 static const char *state2string(enum connman_service_state state)
208 case CONNMAN_SERVICE_STATE_UNKNOWN:
210 case CONNMAN_SERVICE_STATE_IDLE:
212 case CONNMAN_SERVICE_STATE_ASSOCIATION:
213 return "association";
214 case CONNMAN_SERVICE_STATE_CONFIGURATION:
215 return "configuration";
216 case CONNMAN_SERVICE_STATE_READY:
218 case CONNMAN_SERVICE_STATE_ONLINE:
220 case CONNMAN_SERVICE_STATE_DISCONNECT:
222 case CONNMAN_SERVICE_STATE_FAILURE:
229 static const char *error2string(enum connman_service_error error)
232 case CONNMAN_SERVICE_ERROR_UNKNOWN:
234 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
235 return "out-of-range";
236 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
237 return "pin-missing";
238 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
239 return "dhcp-failed";
240 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
241 return "connect-failed";
242 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
243 return "login-failed";
244 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
245 return "auth-failed";
246 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
247 return "invalid-key";
253 static enum connman_service_error string2error(const char *error)
255 if (g_strcmp0(error, "dhcp-failed") == 0)
256 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
257 else if (g_strcmp0(error, "pin-missing") == 0)
258 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
259 else if (g_strcmp0(error, "invalid-key") == 0)
260 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
262 return CONNMAN_SERVICE_ERROR_UNKNOWN;
265 static const char *proxymethod2string(enum connman_service_proxy_method method)
268 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
270 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
272 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
274 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
281 static enum connman_service_proxy_method string2proxymethod(const char *method)
283 if (g_strcmp0(method, "direct") == 0)
284 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
285 else if (g_strcmp0(method, "auto") == 0)
286 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
287 else if (g_strcmp0(method, "manual") == 0)
288 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
290 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
293 static enum connman_service_state combine_state(
294 enum connman_service_state state_a,
295 enum connman_service_state state_b)
297 enum connman_service_state result;
299 if (state_a == state_b) {
304 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
309 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
314 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
319 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
324 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
325 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
326 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
327 state_b == CONNMAN_SERVICE_STATE_READY)
334 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
335 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
336 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
337 state_a == CONNMAN_SERVICE_STATE_READY)
344 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
345 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
346 state_b == CONNMAN_SERVICE_STATE_READY)
353 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
354 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
355 state_a == CONNMAN_SERVICE_STATE_READY)
362 if (state_a == CONNMAN_SERVICE_STATE_READY) {
363 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
370 if (state_b == CONNMAN_SERVICE_STATE_READY) {
371 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
378 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
383 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
388 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
393 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
398 result = CONNMAN_SERVICE_STATE_FAILURE;
404 static connman_bool_t is_connecting_state(struct connman_service *service,
405 enum connman_service_state state)
408 case CONNMAN_SERVICE_STATE_UNKNOWN:
409 case CONNMAN_SERVICE_STATE_IDLE:
410 case CONNMAN_SERVICE_STATE_FAILURE:
411 if (service->network != NULL)
412 return connman_network_get_connecting(service->network);
413 case CONNMAN_SERVICE_STATE_DISCONNECT:
414 case CONNMAN_SERVICE_STATE_READY:
415 case CONNMAN_SERVICE_STATE_ONLINE:
417 case CONNMAN_SERVICE_STATE_ASSOCIATION:
418 case CONNMAN_SERVICE_STATE_CONFIGURATION:
425 static connman_bool_t is_connected_state(const struct connman_service *service,
426 enum connman_service_state state)
429 case CONNMAN_SERVICE_STATE_UNKNOWN:
430 case CONNMAN_SERVICE_STATE_IDLE:
431 case CONNMAN_SERVICE_STATE_ASSOCIATION:
432 case CONNMAN_SERVICE_STATE_CONFIGURATION:
433 case CONNMAN_SERVICE_STATE_DISCONNECT:
434 case CONNMAN_SERVICE_STATE_FAILURE:
436 case CONNMAN_SERVICE_STATE_READY:
437 case CONNMAN_SERVICE_STATE_ONLINE:
444 static connman_bool_t is_connecting(struct connman_service *service)
446 enum connman_service_state state;
448 state = combine_state(service->state_ipv4, service->state_ipv6);
450 return is_connecting_state(service, state);
453 static connman_bool_t is_connected(struct connman_service *service)
455 enum connman_service_state state;
457 state = combine_state(service->state_ipv4, service->state_ipv6);
459 return is_connected_state(service, state);
462 static void update_nameservers(struct connman_service *service)
466 if (service->ipconfig_ipv4)
467 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
468 else if (service->ipconfig_ipv6)
469 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
476 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
477 case CONNMAN_SERVICE_STATE_UNKNOWN:
478 case CONNMAN_SERVICE_STATE_IDLE:
479 case CONNMAN_SERVICE_STATE_ASSOCIATION:
480 case CONNMAN_SERVICE_STATE_CONFIGURATION:
482 case CONNMAN_SERVICE_STATE_FAILURE:
483 case CONNMAN_SERVICE_STATE_DISCONNECT:
484 connman_resolver_remove_all(ifname);
486 case CONNMAN_SERVICE_STATE_READY:
487 case CONNMAN_SERVICE_STATE_ONLINE:
491 connman_resolver_remove_all(ifname);
493 if (service->nameservers_config != NULL) {
496 for (i = 0; service->nameservers_config[i] != NULL; i++) {
497 connman_resolver_append(ifname, NULL,
498 service->nameservers_config[i]);
500 } else if (service->nameservers != NULL) {
503 for (i = 0; service->nameservers[i] != NULL; i++) {
504 connman_resolver_append(ifname, NULL,
505 service->nameservers[i]);
509 if (service->domains != NULL) {
512 for (i = 0; service->domains[i]; i++)
513 connman_resolver_append(ifname, service->domains[i],
515 } else if (service->domainname != NULL)
516 connman_resolver_append(ifname, service->domainname, NULL);
518 connman_resolver_flush();
521 int __connman_service_nameserver_append(struct connman_service *service,
522 const char *nameserver)
526 DBG("service %p nameserver %s", service, nameserver);
528 if (nameserver == NULL)
531 if (service->nameservers != NULL) {
534 for (i = 0; service->nameservers[i] != NULL; i++)
535 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
538 len = g_strv_length(service->nameservers);
539 service->nameservers = g_try_renew(char *, service->nameservers,
543 service->nameservers = g_try_new0(char *, len + 2);
546 if (service->nameservers == NULL)
549 service->nameservers[len] = g_strdup(nameserver);
550 service->nameservers[len + 1] = NULL;
552 update_nameservers(service);
557 int __connman_service_nameserver_remove(struct connman_service *service,
558 const char *nameserver)
563 DBG("service %p nameserver %s", service, nameserver);
565 if (nameserver == NULL)
568 if (service->nameservers == NULL)
571 len = g_strv_length(service->nameservers);
573 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
576 g_strfreev(service->nameservers);
577 service->nameservers = NULL;
582 servers = g_try_new0(char *, len - 1);
586 for (i = 0, j = 0; i < len; i++) {
587 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
588 servers[j] = g_strdup(service->nameservers[i]);
592 servers[len - 2] = NULL;
594 g_strfreev(service->nameservers);
595 service->nameservers = servers;
597 update_nameservers(service);
602 void __connman_service_nameserver_clear(struct connman_service *service)
604 g_strfreev(service->nameservers);
605 service->nameservers = NULL;
607 update_nameservers(service);
610 static void nameserver_add_routes(int index, char **nameservers,
615 for (i = 0; nameservers[i] != NULL; i++) {
616 if (connman_inet_compare_subnet(index, nameservers[i]))
619 connman_inet_add_host_route(index, nameservers[i], gw);
623 static void nameserver_del_routes(int index, char **nameservers)
627 for (i = 0; nameservers[i] != NULL; i++)
628 connman_inet_del_host_route(index, nameservers[i]);
631 void __connman_service_nameserver_add_routes(struct connman_service *service,
639 if (service->network != NULL)
640 index = connman_network_get_index(service->network);
641 else if (service->provider != NULL)
642 index = connman_provider_get_index(service->provider);
644 if (service->nameservers_config != NULL) {
646 * Configured nameserver takes preference over the
647 * discoverd nameserver gathered from DHCP, VPN, etc.
649 nameserver_add_routes(index, service->nameservers_config, gw);
650 } else if (service->nameservers != NULL) {
652 * We add nameservers host routes for nameservers that
653 * are not on our subnet. For those who are, the subnet
654 * route will be installed by the time the dns proxy code
655 * tries to reach them. The subnet route is installed
656 * when setting the interface IP address.
658 nameserver_add_routes(index, service->nameservers, gw);
662 void __connman_service_nameserver_del_routes(struct connman_service *service)
669 if (service->network != NULL)
670 index = connman_network_get_index(service->network);
671 else if (service->provider != NULL)
672 index = connman_provider_get_index(service->provider);
674 if (service->nameservers_config != NULL)
675 nameserver_del_routes(index, service->nameservers_config);
676 else if (service->nameservers != NULL)
677 nameserver_del_routes(index, service->nameservers);
680 static struct connman_stats *stats_get(struct connman_service *service)
682 if (service->roaming == TRUE)
683 return &service->stats_roaming;
685 return &service->stats;
688 static connman_bool_t stats_enabled(struct connman_service *service)
690 struct connman_stats *stats = stats_get(service);
692 return stats->enabled;
695 static void stats_start(struct connman_service *service)
697 struct connman_stats *stats = stats_get(service);
699 DBG("service %p", service);
701 if (stats->timer == NULL)
704 stats->enabled = TRUE;
705 stats->data_last.time = stats->data.time;
707 g_timer_start(stats->timer);
710 static void stats_stop(struct connman_service *service)
712 struct connman_stats *stats = stats_get(service);
713 unsigned int seconds;
715 DBG("service %p", service);
717 if (stats->timer == NULL)
720 if (stats->enabled == FALSE)
723 g_timer_stop(stats->timer);
725 seconds = g_timer_elapsed(stats->timer, NULL);
726 stats->data.time = stats->data_last.time + seconds;
728 stats->enabled = FALSE;
731 static void reset_stats(struct connman_service *service)
733 DBG("service %p", service);
736 service->stats.valid = FALSE;
738 service->stats.data.rx_packets = 0;
739 service->stats.data.tx_packets = 0;
740 service->stats.data.rx_bytes = 0;
741 service->stats.data.tx_bytes = 0;
742 service->stats.data.rx_errors = 0;
743 service->stats.data.tx_errors = 0;
744 service->stats.data.rx_dropped = 0;
745 service->stats.data.tx_dropped = 0;
746 service->stats.data.time = 0;
747 service->stats.data_last.time = 0;
749 g_timer_reset(service->stats.timer);
752 service->stats_roaming.valid = FALSE;
754 service->stats_roaming.data.rx_packets = 0;
755 service->stats_roaming.data.tx_packets = 0;
756 service->stats_roaming.data.rx_bytes = 0;
757 service->stats_roaming.data.tx_bytes = 0;
758 service->stats_roaming.data.rx_errors = 0;
759 service->stats_roaming.data.tx_errors = 0;
760 service->stats_roaming.data.rx_dropped = 0;
761 service->stats_roaming.data.tx_dropped = 0;
762 service->stats_roaming.data.time = 0;
763 service->stats_roaming.data_last.time = 0;
765 g_timer_reset(service->stats_roaming.timer);
768 static struct connman_service *get_default(void)
770 struct connman_service *service;
773 iter = g_sequence_get_begin_iter(service_list);
775 if (g_sequence_iter_is_end(iter) == TRUE)
778 service = g_sequence_get(iter);
780 if (is_connected(service) == FALSE)
786 static void default_changed(void)
788 struct connman_service *service = get_default();
790 __connman_notifier_default_changed(service);
793 const char *__connman_service_default(void)
795 struct connman_service *service;
797 service = get_default();
801 return __connman_service_type2string(service->type);
804 static void state_changed(struct connman_service *service)
806 enum connman_service_state state;
809 state = combine_state(service->state_ipv4, service->state_ipv6);
811 __connman_notifier_service_state_changed(service, state);
813 str = state2string(state);
817 connman_dbus_property_changed_basic(service->path,
818 CONNMAN_SERVICE_INTERFACE, "State",
819 DBUS_TYPE_STRING, &str);
822 static void strength_changed(struct connman_service *service)
824 if (service->strength == 0)
827 connman_dbus_property_changed_basic(service->path,
828 CONNMAN_SERVICE_INTERFACE, "Strength",
829 DBUS_TYPE_BYTE, &service->strength);
832 static void favorite_changed(struct connman_service *service)
834 if (service->path == NULL)
837 connman_dbus_property_changed_basic(service->path,
838 CONNMAN_SERVICE_INTERFACE, "Favorite",
839 DBUS_TYPE_BOOLEAN, &service->favorite);
842 static void immutable_changed(struct connman_service *service)
844 if (service->path == NULL)
847 connman_dbus_property_changed_basic(service->path,
848 CONNMAN_SERVICE_INTERFACE, "Immutable",
849 DBUS_TYPE_BOOLEAN, &service->immutable);
852 static void roaming_changed(struct connman_service *service)
854 if (service->path == NULL)
857 connman_dbus_property_changed_basic(service->path,
858 CONNMAN_SERVICE_INTERFACE, "Roaming",
859 DBUS_TYPE_BOOLEAN, &service->roaming);
862 static void autoconnect_changed(struct connman_service *service)
864 if (service->path == NULL)
867 connman_dbus_property_changed_basic(service->path,
868 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
869 DBUS_TYPE_BOOLEAN, &service->autoconnect);
872 static void passphrase_changed(struct connman_service *service)
874 dbus_bool_t required;
876 switch (service->type) {
877 case CONNMAN_SERVICE_TYPE_UNKNOWN:
878 case CONNMAN_SERVICE_TYPE_SYSTEM:
879 case CONNMAN_SERVICE_TYPE_ETHERNET:
880 case CONNMAN_SERVICE_TYPE_WIMAX:
881 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
882 case CONNMAN_SERVICE_TYPE_CELLULAR:
883 case CONNMAN_SERVICE_TYPE_GPS:
884 case CONNMAN_SERVICE_TYPE_VPN:
885 case CONNMAN_SERVICE_TYPE_GADGET:
887 case CONNMAN_SERVICE_TYPE_WIFI:
890 switch (service->security) {
891 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
892 case CONNMAN_SERVICE_SECURITY_NONE:
894 case CONNMAN_SERVICE_SECURITY_WEP:
895 case CONNMAN_SERVICE_SECURITY_PSK:
896 case CONNMAN_SERVICE_SECURITY_WPA:
897 case CONNMAN_SERVICE_SECURITY_RSN:
898 if (service->passphrase == NULL)
901 case CONNMAN_SERVICE_SECURITY_8021X:
907 connman_dbus_property_changed_basic(service->path,
908 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
909 DBUS_TYPE_BOOLEAN, &required);
912 static void login_changed(struct connman_service *service)
914 dbus_bool_t required = service->login_required;
916 if (service->path == NULL)
919 connman_dbus_property_changed_basic(service->path,
920 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
921 DBUS_TYPE_BOOLEAN, &required);
924 static void append_security(DBusMessageIter *iter, void *user_data)
926 struct connman_service *service = user_data;
929 str = security2string(service->security);
931 dbus_message_iter_append_basic(iter,
932 DBUS_TYPE_STRING, &str);
935 if (service->wps == TRUE)
936 dbus_message_iter_append_basic(iter,
937 DBUS_TYPE_STRING, &str);
940 static void append_ethernet(DBusMessageIter *iter, void *user_data)
942 struct connman_service *service = user_data;
944 if (service->ipconfig_ipv4 != NULL)
945 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
947 else if (service->ipconfig_ipv6 != NULL)
948 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
952 static void append_ipv4(DBusMessageIter *iter, void *user_data)
954 struct connman_service *service = user_data;
956 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
957 state2string(service->state_ipv4));
959 if (is_connected_state(service, service->state_ipv4) == FALSE)
962 if (service->ipconfig_ipv4 != NULL)
963 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
966 static void append_ipv6(DBusMessageIter *iter, void *user_data)
968 struct connman_service *service = user_data;
970 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
971 state2string(service->state_ipv6));
973 if (is_connected_state(service, service->state_ipv6) == FALSE)
976 if (service->ipconfig_ipv6 != NULL)
977 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
978 service->ipconfig_ipv4);
981 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
983 struct connman_service *service = user_data;
985 if (service->ipconfig_ipv4 != NULL)
986 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
990 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
992 struct connman_service *service = user_data;
994 if (service->ipconfig_ipv6 != NULL)
995 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
999 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1004 servers = *nameservers;
1006 for (i = 0; servers[i] != NULL; i++) {
1007 dbus_message_iter_append_basic(iter,
1008 DBUS_TYPE_STRING, &servers[i]);
1012 static void append_dns(DBusMessageIter *iter, void *user_data)
1014 struct connman_service *service = user_data;
1016 if (is_connected(service) == FALSE)
1019 if (service->nameservers_config != NULL) {
1020 append_nameserver(iter, &service->nameservers_config);
1022 } else if (service->nameservers != NULL) {
1023 append_nameserver(iter, &service->nameservers);
1028 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1030 struct connman_service *service = user_data;
1033 if (service->nameservers_config == NULL)
1036 for (i = 0; service->nameservers_config[i]; i++) {
1037 dbus_message_iter_append_basic(iter,
1039 &service->nameservers_config[i]);
1043 static void append_domain(DBusMessageIter *iter, void *user_data)
1045 struct connman_service *service = user_data;
1047 if (is_connected(service) == FALSE &&
1048 is_connecting(service) == FALSE)
1051 if (service->domainname == NULL)
1054 dbus_message_iter_append_basic(iter,
1055 DBUS_TYPE_STRING, &service->domainname);
1058 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1060 struct connman_service *service = user_data;
1063 if (service->domains == NULL)
1066 for (i = 0; service->domains[i]; i++)
1067 dbus_message_iter_append_basic(iter,
1068 DBUS_TYPE_STRING, &service->domains[i]);
1071 static void append_proxies(DBusMessageIter *iter, void *user_data)
1073 struct connman_service *service = user_data;
1076 if (service->proxies == NULL)
1079 for (i = 0; service->proxies[i]; i++)
1080 dbus_message_iter_append_basic(iter,
1081 DBUS_TYPE_STRING, &service->proxies[i]);
1084 static void append_excludes(DBusMessageIter *iter, void *user_data)
1086 struct connman_service *service = user_data;
1089 if (service->excludes == NULL)
1092 for (i = 0; service->excludes[i]; i++)
1093 dbus_message_iter_append_basic(iter,
1094 DBUS_TYPE_STRING, &service->excludes[i]);
1097 static void append_proxy(DBusMessageIter *iter, void *user_data)
1099 struct connman_service *service = user_data;
1100 enum connman_service_proxy_method proxy;
1101 const char *pac = NULL;
1102 const char *method = proxymethod2string(
1103 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1107 if (is_connected(service) == FALSE)
1110 proxy = connman_service_get_proxy_method(service);
1113 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1115 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1117 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1118 connman_dbus_dict_append_array(iter, "Servers",
1119 DBUS_TYPE_STRING, append_proxies,
1122 connman_dbus_dict_append_array(iter, "Excludes",
1123 DBUS_TYPE_STRING, append_excludes,
1126 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1127 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1128 if (service->ipconfig_ipv4 != NULL)
1129 pac = __connman_ipconfig_get_proxy_autoconfig(
1130 service->ipconfig_ipv4);
1131 else if (service->ipconfig_ipv6 != NULL)
1132 pac = __connman_ipconfig_get_proxy_autoconfig(
1133 service->ipconfig_ipv6);
1135 if (service->pac == NULL && pac == NULL)
1138 if (service->pac != NULL)
1141 connman_dbus_dict_append_basic(iter, "URL",
1142 DBUS_TYPE_STRING, &pac);
1146 method = proxymethod2string(proxy);
1149 connman_dbus_dict_append_basic(iter, "Method",
1150 DBUS_TYPE_STRING, &method);
1153 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1155 struct connman_service *service = user_data;
1158 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1161 switch (service->proxy_config) {
1162 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1164 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1166 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1167 if (service->proxies != NULL)
1168 connman_dbus_dict_append_array(iter, "Servers",
1170 append_proxies, service);
1172 if (service->excludes != NULL)
1173 connman_dbus_dict_append_array(iter, "Excludes",
1175 append_excludes, service);
1177 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1178 if (service->pac != NULL)
1179 connman_dbus_dict_append_basic(iter, "URL",
1180 DBUS_TYPE_STRING, &service->pac);
1184 method = proxymethod2string(service->proxy_config);
1186 connman_dbus_dict_append_basic(iter, "Method",
1187 DBUS_TYPE_STRING, &method);
1190 static void append_provider(DBusMessageIter *iter, void *user_data)
1192 struct connman_service *service = user_data;
1194 DBG("%p %p", service, service->provider);
1196 if (is_connected(service) == FALSE)
1199 if (service->provider != NULL)
1200 __connman_provider_append_properties(service->provider, iter);
1204 static void settings_changed(struct connman_service *service,
1205 struct connman_ipconfig *ipconfig)
1207 connman_dbus_property_changed_dict(service->path,
1208 CONNMAN_SERVICE_INTERFACE, "IPv4",
1209 append_ipv4, service);
1211 connman_dbus_property_changed_dict(service->path,
1212 CONNMAN_SERVICE_INTERFACE, "IPv6",
1213 append_ipv6, service);
1215 __connman_notifier_ipconfig_changed(service, ipconfig);
1218 static void ipv4_configuration_changed(struct connman_service *service)
1220 connman_dbus_property_changed_dict(service->path,
1221 CONNMAN_SERVICE_INTERFACE,
1222 "IPv4.Configuration",
1227 static void ipv6_configuration_changed(struct connman_service *service)
1229 connman_dbus_property_changed_dict(service->path,
1230 CONNMAN_SERVICE_INTERFACE,
1231 "IPv6.Configuration",
1236 static void dns_changed(struct connman_service *service)
1238 connman_dbus_property_changed_array(service->path,
1239 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1240 DBUS_TYPE_STRING, append_dns, service);
1243 static void dns_configuration_changed(struct connman_service *service)
1245 connman_dbus_property_changed_array(service->path,
1246 CONNMAN_SERVICE_INTERFACE,
1247 "Nameservers.Configuration",
1248 DBUS_TYPE_STRING, append_dnsconfig, service);
1250 dns_changed(service);
1253 static void domain_changed(struct connman_service *service)
1255 connman_dbus_property_changed_array(service->path,
1256 CONNMAN_SERVICE_INTERFACE, "Domains",
1257 DBUS_TYPE_STRING, append_domain, service);
1260 static void domain_configuration_changed(struct connman_service *service)
1262 connman_dbus_property_changed_array(service->path,
1263 CONNMAN_SERVICE_INTERFACE,
1264 "Domains.Configuration",
1265 DBUS_TYPE_STRING, append_domainconfig, service);
1268 static void proxy_changed(struct connman_service *service)
1270 connman_dbus_property_changed_dict(service->path,
1271 CONNMAN_SERVICE_INTERFACE, "Proxy",
1272 append_proxy, service);
1275 static void proxy_configuration_changed(struct connman_service *service)
1277 connman_dbus_property_changed_dict(service->path,
1278 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1279 append_proxyconfig, service);
1281 proxy_changed(service);
1284 static void link_changed(struct connman_service *service)
1286 connman_dbus_property_changed_dict(service->path,
1287 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1288 append_ethernet, service);
1291 static void stats_append_counters(DBusMessageIter *dict,
1292 struct connman_stats_data *stats,
1293 struct connman_stats_data *counters,
1294 connman_bool_t append_all)
1296 if (counters->rx_packets != stats->rx_packets || append_all) {
1297 counters->rx_packets = stats->rx_packets;
1298 connman_dbus_dict_append_basic(dict, "RX.Packets",
1299 DBUS_TYPE_UINT32, &stats->rx_packets);
1302 if (counters->tx_packets != stats->tx_packets || append_all) {
1303 counters->tx_packets = stats->tx_packets;
1304 connman_dbus_dict_append_basic(dict, "TX.Packets",
1305 DBUS_TYPE_UINT32, &stats->tx_packets);
1308 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1309 counters->rx_bytes = stats->rx_bytes;
1310 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1311 DBUS_TYPE_UINT32, &stats->rx_bytes);
1314 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1315 counters->tx_bytes = stats->tx_bytes;
1316 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1317 DBUS_TYPE_UINT32, &stats->tx_bytes);
1320 if (counters->rx_errors != stats->rx_errors || append_all) {
1321 counters->rx_errors = stats->rx_errors;
1322 connman_dbus_dict_append_basic(dict, "RX.Errors",
1323 DBUS_TYPE_UINT32, &stats->rx_errors);
1326 if (counters->tx_errors != stats->tx_errors || append_all) {
1327 counters->tx_errors = stats->tx_errors;
1328 connman_dbus_dict_append_basic(dict, "TX.Errors",
1329 DBUS_TYPE_UINT32, &stats->tx_errors);
1332 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1333 counters->rx_dropped = stats->rx_dropped;
1334 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1335 DBUS_TYPE_UINT32, &stats->rx_dropped);
1338 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1339 counters->tx_dropped = stats->tx_dropped;
1340 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1341 DBUS_TYPE_UINT32, &stats->tx_dropped);
1344 if (counters->time != stats->time || append_all) {
1345 counters->time = stats->time;
1346 connman_dbus_dict_append_basic(dict, "Time",
1347 DBUS_TYPE_UINT32, &stats->time);
1351 static void stats_append(struct connman_service *service,
1352 const char *counter,
1353 struct connman_stats_counter *counters,
1354 connman_bool_t append_all)
1356 DBusMessageIter array, dict;
1359 DBG("service %p counter %s", service, counter);
1361 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1365 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1366 &service->path, DBUS_TYPE_INVALID);
1368 dbus_message_iter_init_append(msg, &array);
1371 connman_dbus_dict_open(&array, &dict);
1373 stats_append_counters(&dict, &service->stats.data,
1374 &counters->stats.data, append_all);
1376 connman_dbus_dict_close(&array, &dict);
1378 /* roaming counter */
1379 connman_dbus_dict_open(&array, &dict);
1381 stats_append_counters(&dict, &service->stats_roaming.data,
1382 &counters->stats_roaming.data, append_all);
1384 connman_dbus_dict_close(&array, &dict);
1386 __connman_counter_send_usage(counter, msg);
1389 static void stats_update(struct connman_service *service,
1390 unsigned int rx_packets, unsigned int tx_packets,
1391 unsigned int rx_bytes, unsigned int tx_bytes,
1392 unsigned int rx_errors, unsigned int tx_errors,
1393 unsigned int rx_dropped, unsigned int tx_dropped)
1395 struct connman_stats *stats = stats_get(service);
1396 struct connman_stats_data *data_last = &stats->data_last;
1397 struct connman_stats_data *data = &stats->data;
1398 unsigned int seconds;
1400 DBG("service %p", service);
1402 if (stats->valid == TRUE) {
1404 rx_packets - data_last->rx_packets;
1406 tx_packets - data_last->tx_packets;
1408 rx_bytes - data_last->rx_bytes;
1410 tx_bytes - data_last->tx_bytes;
1412 rx_errors - data_last->rx_errors;
1414 tx_errors - data_last->tx_errors;
1416 rx_dropped - data_last->rx_dropped;
1418 tx_dropped - data_last->tx_dropped;
1420 stats->valid = TRUE;
1423 data_last->rx_packets = rx_packets;
1424 data_last->tx_packets = tx_packets;
1425 data_last->rx_bytes = rx_bytes;
1426 data_last->tx_bytes = tx_bytes;
1427 data_last->rx_errors = rx_errors;
1428 data_last->tx_errors = tx_errors;
1429 data_last->rx_dropped = rx_dropped;
1430 data_last->tx_dropped = tx_dropped;
1432 seconds = g_timer_elapsed(stats->timer, NULL);
1433 stats->data.time = stats->data_last.time + seconds;
1436 void __connman_service_notify(struct connman_service *service,
1437 unsigned int rx_packets, unsigned int tx_packets,
1438 unsigned int rx_bytes, unsigned int tx_bytes,
1439 unsigned int rx_errors, unsigned int tx_errors,
1440 unsigned int rx_dropped, unsigned int tx_dropped)
1442 GHashTableIter iter;
1443 gpointer key, value;
1444 const char *counter;
1445 struct connman_stats_counter *counters;
1446 struct connman_stats_data *data;
1449 if (service == NULL)
1452 if (is_connected(service) == FALSE)
1455 stats_update(service,
1456 rx_packets, tx_packets,
1458 rx_errors, tx_errors,
1459 rx_dropped, tx_dropped);
1461 data = &stats_get(service)->data;
1462 err = __connman_stats_update(service, service->roaming, data);
1464 connman_error("Failed to store statistics for %s",
1465 service->identifier);
1467 g_hash_table_iter_init(&iter, service->counter_table);
1468 while (g_hash_table_iter_next(&iter, &key, &value)) {
1472 stats_append(service, counter, counters, counters->append_all);
1473 counters->append_all = FALSE;
1477 int __connman_service_counter_register(const char *counter)
1479 struct connman_service *service;
1480 GSequenceIter *iter;
1481 struct connman_stats_counter *counters;
1483 DBG("counter %s", counter);
1485 counter_list = g_slist_append(counter_list, (gpointer)counter);
1487 iter = g_sequence_get_begin_iter(service_list);
1489 while (g_sequence_iter_is_end(iter) == FALSE) {
1490 service = g_sequence_get(iter);
1492 counters = g_try_new0(struct connman_stats_counter, 1);
1493 if (counters == NULL)
1496 counters->append_all = TRUE;
1498 g_hash_table_replace(service->counter_table, (gpointer)counter,
1501 iter = g_sequence_iter_next(iter);
1507 void __connman_service_counter_unregister(const char *counter)
1509 struct connman_service *service;
1510 GSequenceIter *iter;
1512 DBG("counter %s", counter);
1514 iter = g_sequence_get_begin_iter(service_list);
1516 while (g_sequence_iter_is_end(iter) == FALSE) {
1517 service = g_sequence_get(iter);
1519 g_hash_table_remove(service->counter_table, counter);
1521 iter = g_sequence_iter_next(iter);
1524 counter_list = g_slist_remove(counter_list, counter);
1527 GSequence *__connman_service_get_list(struct connman_session *session,
1528 service_match_cb service_match,
1529 create_service_entry_cb create_service_entry,
1530 GDestroyNotify destroy_service_entry)
1533 GSequenceIter *iter;
1534 struct connman_service *service;
1535 enum connman_service_state state;
1536 struct service_entry *entry;
1538 list = g_sequence_new(destroy_service_entry);
1542 iter = g_sequence_get_begin_iter(service_list);
1544 while (g_sequence_iter_is_end(iter) == FALSE) {
1545 service = g_sequence_get(iter);
1547 if (service_match(session, service) == TRUE) {
1548 state = combine_state(service->state_ipv4,
1549 service->state_ipv6);
1550 entry = create_service_entry(service, service->name,
1555 g_sequence_append(list, entry);
1558 iter = g_sequence_iter_next(iter);
1564 void __connman_service_session_inc(struct connman_service *service)
1566 DBG("service %p", service);
1568 g_atomic_int_inc(&service->session_usage_count);
1571 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1573 connman_bool_t in_use;
1575 if (g_atomic_int_dec_and_test(&service->session_usage_count) == TRUE)
1580 DBG("service %p last %d", service, in_use);
1585 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1586 struct connman_service *service)
1588 dbus_bool_t required;
1591 str = __connman_service_type2string(service->type);
1593 connman_dbus_dict_append_basic(dict, "Type",
1594 DBUS_TYPE_STRING, &str);
1596 connman_dbus_dict_append_array(dict, "Security",
1597 DBUS_TYPE_STRING, append_security, service);
1599 str = state2string(combine_state(service->state_ipv4,
1600 service->state_ipv6));
1602 connman_dbus_dict_append_basic(dict, "State",
1603 DBUS_TYPE_STRING, &str);
1605 str = error2string(service->error);
1607 connman_dbus_dict_append_basic(dict, "Error",
1608 DBUS_TYPE_STRING, &str);
1610 if (service->strength > 0)
1611 connman_dbus_dict_append_basic(dict, "Strength",
1612 DBUS_TYPE_BYTE, &service->strength);
1614 connman_dbus_dict_append_basic(dict, "Favorite",
1615 DBUS_TYPE_BOOLEAN, &service->favorite);
1617 connman_dbus_dict_append_basic(dict, "Immutable",
1618 DBUS_TYPE_BOOLEAN, &service->immutable);
1620 if (service->favorite == TRUE)
1621 connman_dbus_dict_append_basic(dict, "AutoConnect",
1622 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1624 connman_dbus_dict_append_basic(dict, "AutoConnect",
1625 DBUS_TYPE_BOOLEAN, &service->favorite);
1627 if (service->name != NULL)
1628 connman_dbus_dict_append_basic(dict, "Name",
1629 DBUS_TYPE_STRING, &service->name);
1631 connman_dbus_dict_append_basic(dict, "LoginRequired",
1632 DBUS_TYPE_BOOLEAN, &service->login_required);
1634 switch (service->type) {
1635 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1636 case CONNMAN_SERVICE_TYPE_SYSTEM:
1637 case CONNMAN_SERVICE_TYPE_GPS:
1638 case CONNMAN_SERVICE_TYPE_VPN:
1639 case CONNMAN_SERVICE_TYPE_GADGET:
1641 case CONNMAN_SERVICE_TYPE_CELLULAR:
1642 connman_dbus_dict_append_basic(dict, "Roaming",
1643 DBUS_TYPE_BOOLEAN, &service->roaming);
1646 connman_dbus_dict_append_basic(dict, "SetupRequired",
1647 DBUS_TYPE_BOOLEAN, &required);
1648 connman_dbus_dict_append_dict(dict, "Ethernet",
1649 append_ethernet, service);
1651 case CONNMAN_SERVICE_TYPE_WIFI:
1652 if (service->passphrase != NULL && limited == FALSE)
1653 connman_dbus_dict_append_basic(dict, "Passphrase",
1654 DBUS_TYPE_STRING, &service->passphrase);
1658 switch (service->security) {
1659 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1660 case CONNMAN_SERVICE_SECURITY_NONE:
1662 case CONNMAN_SERVICE_SECURITY_WEP:
1663 case CONNMAN_SERVICE_SECURITY_PSK:
1664 case CONNMAN_SERVICE_SECURITY_WPA:
1665 case CONNMAN_SERVICE_SECURITY_RSN:
1666 if (service->passphrase == NULL)
1669 case CONNMAN_SERVICE_SECURITY_8021X:
1673 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1674 DBUS_TYPE_BOOLEAN, &required);
1677 case CONNMAN_SERVICE_TYPE_ETHERNET:
1678 case CONNMAN_SERVICE_TYPE_WIMAX:
1679 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1680 connman_dbus_dict_append_dict(dict, "Ethernet",
1681 append_ethernet, service);
1685 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1687 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1688 append_ipv4config, service);
1690 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1692 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1693 append_ipv6config, service);
1695 connman_dbus_dict_append_array(dict, "Nameservers",
1696 DBUS_TYPE_STRING, append_dns, service);
1698 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1699 DBUS_TYPE_STRING, append_dnsconfig, service);
1701 connman_dbus_dict_append_array(dict, "Domains",
1702 DBUS_TYPE_STRING, append_domain, service);
1704 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1705 DBUS_TYPE_STRING, append_domainconfig, service);
1707 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1709 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1710 append_proxyconfig, service);
1712 connman_dbus_dict_append_dict(dict, "Provider",
1713 append_provider, service);
1716 static void append_struct(gpointer value, gpointer user_data)
1718 struct connman_service *service = value;
1719 DBusMessageIter *iter = user_data;
1720 DBusMessageIter entry, dict;
1722 if (service->path == NULL || service->hidden == TRUE)
1725 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1727 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1730 connman_dbus_dict_open(&entry, &dict);
1731 append_properties(&dict, TRUE, service);
1732 connman_dbus_dict_close(&entry, &dict);
1734 dbus_message_iter_close_container(iter, &entry);
1737 void __connman_service_list_struct(DBusMessageIter *iter)
1739 g_sequence_foreach(service_list, append_struct, iter);
1742 int __connman_service_get_index(struct connman_service *service)
1744 if (service == NULL)
1747 if (service->network != NULL)
1748 return connman_network_get_index(service->network);
1749 else if (service->provider != NULL)
1750 return connman_provider_get_index(service->provider);
1755 void __connman_service_set_domainname(struct connman_service *service,
1756 const char *domainname)
1758 if (service == NULL)
1761 g_free(service->domainname);
1762 service->domainname = g_strdup(domainname);
1764 domain_changed(service);
1767 const char *connman_service_get_domainname(struct connman_service *service)
1769 if (service == NULL)
1772 if (service->domains != NULL)
1773 return service->domains[0];
1775 return service->domainname;
1778 char **connman_service_get_nameservers(struct connman_service *service)
1780 if (service == NULL)
1783 if (service->nameservers_config != NULL)
1784 return service->nameservers_config;
1785 else if (service->nameservers != NULL)
1786 return service->nameservers;
1791 void connman_service_set_proxy_method(struct connman_service *service,
1792 enum connman_service_proxy_method method)
1794 if (service == NULL)
1797 service->proxy = method;
1799 proxy_changed(service);
1801 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1802 __connman_notifier_proxy_changed(service);
1805 enum connman_service_proxy_method connman_service_get_proxy_method(
1806 struct connman_service *service)
1808 if (service == NULL)
1809 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1811 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1812 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1813 service->pac == NULL)
1814 return service->proxy;
1816 return service->proxy_config;
1819 return service->proxy;
1822 char **connman_service_get_proxy_servers(struct connman_service *service)
1824 return g_strdupv(service->proxies);
1827 char **connman_service_get_proxy_excludes(struct connman_service *service)
1829 return g_strdupv(service->excludes);
1832 const char *connman_service_get_proxy_url(struct connman_service *service)
1834 if (service == NULL)
1837 return service->pac;
1840 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1843 if (service == NULL)
1846 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1848 if (service->ipconfig_ipv4) {
1849 if (__connman_ipconfig_set_proxy_autoconfig(
1850 service->ipconfig_ipv4, url) < 0)
1852 } else if (service->ipconfig_ipv6) {
1853 if (__connman_ipconfig_set_proxy_autoconfig(
1854 service->ipconfig_ipv6, url) < 0)
1859 proxy_changed(service);
1861 __connman_notifier_proxy_changed(service);
1864 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1866 if (service == NULL)
1869 if (service->ipconfig_ipv4)
1870 return __connman_ipconfig_get_proxy_autoconfig(
1871 service->ipconfig_ipv4);
1872 else if (service->ipconfig_ipv6)
1873 return __connman_ipconfig_get_proxy_autoconfig(
1874 service->ipconfig_ipv6);
1878 static void update_timeservers(struct connman_service *service)
1882 if (service->timeservers == NULL)
1885 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
1886 case CONNMAN_SERVICE_STATE_UNKNOWN:
1887 case CONNMAN_SERVICE_STATE_IDLE:
1888 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1889 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1891 case CONNMAN_SERVICE_STATE_FAILURE:
1892 case CONNMAN_SERVICE_STATE_DISCONNECT:
1893 for (i = 0; service->timeservers[i] != NULL; i++)
1894 connman_timeserver_remove(service->timeservers[i]);
1896 case CONNMAN_SERVICE_STATE_READY:
1897 case CONNMAN_SERVICE_STATE_ONLINE:
1901 for (i = 0; service->timeservers[i] != NULL; i++)
1902 connman_timeserver_append(service->timeservers[i]);
1905 int __connman_service_timeserver_append(struct connman_service *service,
1906 const char *timeserver)
1910 DBG("service %p timeserver %s", service, timeserver);
1912 if (timeserver == NULL)
1915 if (service->timeservers != NULL) {
1918 for (i = 0; service->timeservers[i] != NULL; i++)
1919 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1922 len = g_strv_length(service->timeservers);
1923 service->timeservers = g_try_renew(char *, service->timeservers,
1927 service->timeservers = g_try_new0(char *, len + 2);
1930 if (service->timeservers == NULL)
1933 service->timeservers[len] = g_strdup(timeserver);
1934 service->timeservers[len + 1] = NULL;
1936 update_timeservers(service);
1941 int __connman_service_timeserver_remove(struct connman_service *service,
1942 const char *timeserver)
1947 DBG("service %p timeserver %s", service, timeserver);
1949 if (timeserver == NULL)
1952 if (service->timeservers == NULL)
1955 len = g_strv_length(service->timeservers);
1957 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
1960 g_strfreev(service->timeservers);
1961 service->timeservers = NULL;
1966 servers = g_try_new0(char *, len - 1);
1967 if (servers == NULL)
1970 for (i = 0, j = 0; i < len; i++) {
1971 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
1972 servers[j] = g_strdup(service->timeservers[i]);
1976 servers[len - 2] = NULL;
1978 g_strfreev(service->timeservers);
1979 service->timeservers = servers;
1981 update_timeservers(service);
1986 void __connman_service_set_pac(struct connman_service *service,
1992 g_free(service->pac);
1993 service->pac = g_strdup(pac);
1996 void __connman_service_set_identity(struct connman_service *service,
1997 const char *identity)
1999 if (service->immutable)
2002 g_free(service->identity);
2003 service->identity = g_strdup(identity);
2005 if (service->network != NULL)
2006 connman_network_set_string(service->network,
2011 void __connman_service_set_passphrase(struct connman_service *service,
2012 const char* passphrase)
2014 if (service->immutable == TRUE)
2017 g_free(service->passphrase);
2018 service->passphrase = g_strdup(passphrase);
2020 passphrase_changed(service);
2022 if (service->network != NULL)
2023 connman_network_set_string(service->network,
2025 service->passphrase);
2027 __connman_storage_save_service(service);
2030 static DBusMessage *get_properties(DBusConnection *conn,
2031 DBusMessage *msg, void *user_data)
2033 struct connman_service *service = user_data;
2035 DBusMessageIter array, dict;
2037 DBG("service %p", service);
2039 reply = dbus_message_new_method_return(msg);
2043 dbus_message_iter_init_append(reply, &array);
2045 connman_dbus_dict_open(&array, &dict);
2046 append_properties(&dict, FALSE, service);
2047 connman_dbus_dict_close(&array, &dict);
2052 static int update_proxy_configuration(struct connman_service *service,
2053 DBusMessageIter *array)
2055 DBusMessageIter dict;
2056 enum connman_service_proxy_method method;
2057 GString *servers_str = NULL;
2058 GString *excludes_str = NULL;
2059 const char *url = NULL;
2061 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2063 dbus_message_iter_recurse(array, &dict);
2065 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2066 DBusMessageIter entry, variant;
2070 dbus_message_iter_recurse(&dict, &entry);
2072 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2075 dbus_message_iter_get_basic(&entry, &key);
2076 dbus_message_iter_next(&entry);
2078 if (dbus_message_iter_get_arg_type(&entry) !=
2082 dbus_message_iter_recurse(&entry, &variant);
2083 type = dbus_message_iter_get_arg_type(&variant);
2085 if (g_str_equal(key, "Method") == TRUE) {
2088 if (type != DBUS_TYPE_STRING)
2091 dbus_message_iter_get_basic(&variant, &val);
2092 method = string2proxymethod(val);
2093 } else if (g_str_equal(key, "URL") == TRUE) {
2094 if (type != DBUS_TYPE_STRING)
2097 dbus_message_iter_get_basic(&variant, &url);
2098 } else if (g_str_equal(key, "Servers") == TRUE) {
2099 DBusMessageIter str_array;
2101 if (type != DBUS_TYPE_ARRAY)
2104 servers_str = g_string_new(NULL);
2105 if (servers_str == NULL)
2108 dbus_message_iter_recurse(&variant, &str_array);
2110 while (dbus_message_iter_get_arg_type(&str_array) ==
2114 dbus_message_iter_get_basic(&str_array, &val);
2116 if (servers_str->len > 0)
2117 g_string_append_printf(servers_str,
2120 g_string_append(servers_str, val);
2122 dbus_message_iter_next(&str_array);
2124 } else if (g_str_equal(key, "Excludes") == TRUE) {
2125 DBusMessageIter str_array;
2127 if (type != DBUS_TYPE_ARRAY)
2130 excludes_str = g_string_new(NULL);
2131 if (excludes_str == NULL)
2134 dbus_message_iter_recurse(&variant, &str_array);
2136 while (dbus_message_iter_get_arg_type(&str_array) ==
2140 dbus_message_iter_get_basic(&str_array, &val);
2142 if (excludes_str->len > 0)
2143 g_string_append_printf(excludes_str,
2146 g_string_append(excludes_str, val);
2148 dbus_message_iter_next(&str_array);
2152 dbus_message_iter_next(&dict);
2156 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2158 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2159 if (servers_str == NULL && service->proxies == NULL)
2162 if (servers_str != NULL) {
2163 g_strfreev(service->proxies);
2165 if (servers_str->len > 0)
2166 service->proxies = g_strsplit_set(
2167 servers_str->str, " ", 0);
2169 service->proxies = NULL;
2172 if (excludes_str != NULL) {
2173 g_strfreev(service->excludes);
2175 if (excludes_str->len > 0)
2176 service->excludes = g_strsplit_set(
2177 excludes_str->str, " ", 0);
2179 service->excludes = NULL;
2182 if (service->proxies == NULL)
2183 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2186 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2187 g_free(service->pac);
2189 if (url != NULL && strlen(url) > 0)
2190 service->pac = g_strdup(url);
2192 service->pac = NULL;
2194 /* if we are connected:
2195 - if service->pac == NULL
2196 - if __connman_ipconfig_get_proxy_autoconfig(
2197 service->ipconfig) == NULL
2198 --> We should start WPAD */
2201 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2205 if (servers_str != NULL)
2206 g_string_free(servers_str, TRUE);
2208 if (excludes_str != NULL)
2209 g_string_free(excludes_str, TRUE);
2211 service->proxy_config = method;
2216 if (servers_str != NULL)
2217 g_string_free(servers_str, TRUE);
2219 if (excludes_str != NULL)
2220 g_string_free(excludes_str, TRUE);
2225 static int set_ipconfig(struct connman_service *service,
2226 struct connman_ipconfig *ipconfig,
2227 DBusMessageIter *array,
2228 enum connman_service_state state,
2229 enum connman_service_state *new_state)
2231 enum connman_ipconfig_method old_method;
2232 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2233 enum connman_ipconfig_type type;
2236 old_method = __connman_ipconfig_get_method(ipconfig);
2238 if (is_connecting_state(service, state) ||
2239 is_connected_state(service, state))
2240 __connman_network_clear_ipconfig(service->network, ipconfig);
2242 err = __connman_ipconfig_set_config(ipconfig, array);
2243 method = __connman_ipconfig_get_method(ipconfig);
2244 type = __connman_ipconfig_get_config_type(ipconfig);
2246 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2247 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2248 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2249 *new_state = service->state_ipv4 =
2250 CONNMAN_SERVICE_STATE_CONFIGURATION;
2251 __connman_ipconfig_enable(ipconfig);
2254 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2255 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2256 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2257 *new_state = service->state_ipv6;
2258 __connman_ipconfig_enable(ipconfig);
2262 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2263 type, method, state2string(*new_state));
2268 static DBusMessage *set_property(DBusConnection *conn,
2269 DBusMessage *msg, void *user_data)
2271 struct connman_service *service = user_data;
2272 DBusMessageIter iter, value;
2276 DBG("service %p", service);
2278 if (dbus_message_iter_init(msg, &iter) == FALSE)
2279 return __connman_error_invalid_arguments(msg);
2281 dbus_message_iter_get_basic(&iter, &name);
2282 dbus_message_iter_next(&iter);
2283 dbus_message_iter_recurse(&iter, &value);
2285 type = dbus_message_iter_get_arg_type(&value);
2287 if (g_str_equal(name, "AutoConnect") == TRUE) {
2288 connman_bool_t autoconnect;
2290 if (type != DBUS_TYPE_BOOLEAN)
2291 return __connman_error_invalid_arguments(msg);
2293 if (service->favorite == FALSE)
2294 return __connman_error_invalid_service(msg);
2296 dbus_message_iter_get_basic(&value, &autoconnect);
2298 if (service->autoconnect == autoconnect)
2299 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2301 service->autoconnect = autoconnect;
2303 autoconnect_changed(service);
2305 __connman_storage_save_service(service);
2306 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2307 const char *passphrase;
2309 if (type != DBUS_TYPE_STRING)
2310 return __connman_error_invalid_arguments(msg);
2312 if (service->immutable == TRUE)
2313 return __connman_error_not_supported(msg);
2315 dbus_message_iter_get_basic(&value, &passphrase);
2317 __connman_service_set_passphrase(service, passphrase);
2318 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2319 DBusMessageIter entry;
2324 if (type != DBUS_TYPE_ARRAY)
2325 return __connman_error_invalid_arguments(msg);
2327 str = g_string_new(NULL);
2329 return __connman_error_invalid_arguments(msg);
2331 index = connman_network_get_index(service->network);
2332 gw = __connman_ipconfig_get_gateway_from_index(index);
2334 if (gw && strlen(gw))
2335 __connman_service_nameserver_del_routes(service);
2337 dbus_message_iter_recurse(&value, &entry);
2339 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2341 dbus_message_iter_get_basic(&entry, &val);
2342 dbus_message_iter_next(&entry);
2344 g_string_append_printf(str, " %s", val);
2346 g_string_append(str, val);
2349 g_strfreev(service->nameservers_config);
2352 service->nameservers_config =
2353 g_strsplit_set(str->str, " ", 0);
2355 service->nameservers_config = NULL;
2358 g_string_free(str, TRUE);
2360 if (gw && strlen(gw))
2361 __connman_service_nameserver_add_routes(service, gw);
2363 update_nameservers(service);
2364 dns_configuration_changed(service);
2366 __connman_storage_save_service(service);
2367 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2368 DBusMessageIter entry;
2371 if (type != DBUS_TYPE_ARRAY)
2372 return __connman_error_invalid_arguments(msg);
2374 str = g_string_new(NULL);
2376 return __connman_error_invalid_arguments(msg);
2378 dbus_message_iter_recurse(&value, &entry);
2380 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2382 dbus_message_iter_get_basic(&entry, &val);
2383 dbus_message_iter_next(&entry);
2385 g_string_append_printf(str, " %s", val);
2387 g_string_append(str, val);
2390 g_strfreev(service->domains);
2393 service->domains = g_strsplit_set(str->str, " ", 0);
2395 service->domains = NULL;
2397 g_string_free(str, TRUE);
2399 update_nameservers(service);
2400 domain_configuration_changed(service);
2402 __connman_storage_save_service(service);
2403 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2406 if (type != DBUS_TYPE_ARRAY)
2407 return __connman_error_invalid_arguments(msg);
2409 err = update_proxy_configuration(service, &value);
2412 return __connman_error_failed(msg, -err);
2414 proxy_configuration_changed(service);
2416 __connman_storage_save_service(service);
2417 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2418 g_str_equal(name, "IPv6.Configuration")) {
2420 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2421 enum connman_service_state state =
2422 CONNMAN_SERVICE_STATE_UNKNOWN;
2427 if (service->ipconfig_ipv4 == NULL &&
2428 service->ipconfig_ipv6 == NULL)
2429 return __connman_error_invalid_property(msg);
2431 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2432 ipv4 = service->ipconfig_ipv4;
2433 err = set_ipconfig(service, ipv4, &value,
2434 service->state_ipv4, &state);
2436 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2437 ipv6 = service->ipconfig_ipv6;
2438 err = set_ipconfig(service, ipv6, &value,
2439 service->state_ipv6, &state);
2443 if (is_connected_state(service, state) ||
2444 is_connecting_state(service, state))
2445 __connman_network_set_ipconfig(service->network,
2447 return __connman_error_failed(msg, -err);
2451 ipv4_configuration_changed(service);
2453 ipv6_configuration_changed(service);
2455 if (is_connecting(service) || is_connected(service))
2456 __connman_network_set_ipconfig(service->network,
2459 __connman_storage_save_service(service);
2461 return __connman_error_invalid_property(msg);
2463 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2466 static void set_idle(struct connman_service *service)
2468 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
2469 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2470 state_changed(service);
2473 static DBusMessage *clear_property(DBusConnection *conn,
2474 DBusMessage *msg, void *user_data)
2476 struct connman_service *service = user_data;
2479 DBG("service %p", service);
2481 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2484 if (g_str_equal(name, "Error") == TRUE) {
2487 g_get_current_time(&service->modified);
2488 __connman_storage_save_service(service);
2489 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2490 if (service->immutable == TRUE)
2491 return __connman_error_not_supported(msg);
2493 g_free(service->passphrase);
2494 service->passphrase = NULL;
2496 passphrase_changed(service);
2498 __connman_storage_save_service(service);
2500 return __connman_error_invalid_property(msg);
2502 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2505 static connman_bool_t is_ignore(struct connman_service *service)
2507 if (service->autoconnect == FALSE)
2510 if (service->roaming == TRUE)
2513 if (service->ignore == TRUE)
2516 if (combine_state(service->state_ipv4, service->state_ipv6) ==
2517 CONNMAN_SERVICE_STATE_FAILURE)
2523 void __connman_service_auto_connect(void)
2525 struct connman_service *service = NULL;
2526 GSequenceIter *iter;
2530 if (__connman_session_mode() == TRUE) {
2531 DBG("Session mode enabled: auto connect disabled");
2535 iter = g_sequence_get_begin_iter(service_list);
2537 while (g_sequence_iter_is_end(iter) == FALSE) {
2538 service = g_sequence_get(iter);
2540 if (service->pending != NULL)
2543 if (is_connecting(service) == TRUE)
2546 if (service->favorite == FALSE)
2549 if (is_connected(service) == TRUE)
2552 if (is_ignore(service) == FALSE &&
2553 combine_state(service->state_ipv4,
2554 service->state_ipv6) ==
2555 CONNMAN_SERVICE_STATE_IDLE)
2560 iter = g_sequence_iter_next(iter);
2563 if (service != NULL) {
2564 service->userconnect = FALSE;
2565 __connman_service_connect(service);
2569 static void remove_timeout(struct connman_service *service)
2571 if (service->timeout > 0) {
2572 g_source_remove(service->timeout);
2573 service->timeout = 0;
2577 static void reply_pending(struct connman_service *service, int error)
2579 remove_timeout(service);
2581 if (service->pending != NULL) {
2585 reply = __connman_error_failed(service->pending,
2588 g_dbus_send_message(connection, reply);
2592 sender = dbus_message_get_interface(service->pending);
2594 DBG("sender %s", sender);
2596 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2597 g_dbus_send_reply(connection, service->pending,
2598 DBUS_TYPE_OBJECT_PATH, &service->path,
2601 g_dbus_send_reply(connection, service->pending,
2605 dbus_message_unref(service->pending);
2606 service->pending = NULL;
2610 static gboolean connect_timeout(gpointer user_data)
2612 struct connman_service *service = user_data;
2613 connman_bool_t autoconnect = FALSE;
2615 DBG("service %p", service);
2617 service->timeout = 0;
2619 if (service->network != NULL)
2620 __connman_network_disconnect(service->network);
2622 __connman_ipconfig_disable(service->ipconfig_ipv4);
2623 __connman_ipconfig_disable(service->ipconfig_ipv6);
2625 __connman_stats_service_unregister(service);
2627 if (service->pending != NULL) {
2630 reply = __connman_error_operation_timeout(service->pending);
2632 g_dbus_send_message(connection, reply);
2634 dbus_message_unref(service->pending);
2635 service->pending = NULL;
2639 __connman_service_indicate_state(service,
2640 CONNMAN_SERVICE_STATE_FAILURE,
2641 CONNMAN_IPCONFIG_TYPE_IPV4);
2642 __connman_service_indicate_state(service,
2643 CONNMAN_SERVICE_STATE_FAILURE,
2644 CONNMAN_IPCONFIG_TYPE_IPV6);
2646 if (autoconnect == TRUE && service->userconnect == FALSE)
2647 __connman_service_auto_connect();
2652 static void set_reconnect_state(struct connman_service *service,
2653 connman_bool_t reconnect)
2655 struct connman_device *device;
2657 if (service->network == NULL)
2660 device = connman_network_get_device(service->network);
2664 __connman_device_set_reconnect(device, reconnect);
2667 static connman_bool_t get_reconnect_state(struct connman_service *service)
2669 struct connman_device *device;
2671 if (service->network == NULL)
2674 device = connman_network_get_device(service->network);
2678 return __connman_device_get_reconnect(device);
2681 static void request_input_cb (struct connman_service *service,
2682 const char *identity, const char *passphrase,
2685 DBG ("RequestInput return, %p", service);
2687 if (identity == NULL && passphrase == NULL && service->wps == FALSE)
2690 if (identity != NULL)
2691 __connman_service_set_identity(service, identity);
2693 if (passphrase != NULL)
2694 __connman_service_set_passphrase(service, passphrase);
2696 __connman_service_connect(service);
2699 static DBusMessage *connect_service(DBusConnection *conn,
2700 DBusMessage *msg, void *user_data)
2702 struct connman_service *service = user_data;
2703 GSequenceIter *iter;
2706 DBG("service %p", service);
2708 if (service->pending != NULL)
2709 return __connman_error_in_progress(msg);
2711 iter = g_sequence_get_begin_iter(service_list);
2713 while (g_sequence_iter_is_end(iter) == FALSE) {
2714 struct connman_service *temp = g_sequence_get(iter);
2716 if (service->type == temp->type && is_connecting(temp) == TRUE)
2717 return __connman_error_in_progress(msg);
2719 iter = g_sequence_iter_next(iter);
2722 service->ignore = FALSE;
2724 service->userconnect = TRUE;
2726 service->pending = dbus_message_ref(msg);
2728 set_reconnect_state(service, FALSE);
2730 err = __connman_service_connect(service);
2732 if (err == -ENOKEY) {
2733 if (__connman_agent_request_input(service,
2739 if (service->pending == NULL)
2742 if (err != -EINPROGRESS) {
2743 dbus_message_unref(service->pending);
2744 service->pending = NULL;
2746 return __connman_error_failed(msg, -err);
2752 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2755 static DBusMessage *disconnect_service(DBusConnection *conn,
2756 DBusMessage *msg, void *user_data)
2758 struct connman_service *service = user_data;
2761 DBG("service %p", service);
2763 reply_pending(service, ECONNABORTED);
2765 service->ignore = TRUE;
2767 set_reconnect_state(service, FALSE);
2769 err = __connman_service_disconnect(service);
2771 if (err != -EINPROGRESS)
2772 return __connman_error_failed(msg, -err);
2777 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2780 static DBusMessage *remove_service(DBusConnection *conn,
2781 DBusMessage *msg, void *user_data)
2783 struct connman_service *service = user_data;
2785 DBG("service %p", service);
2787 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2788 return __connman_error_not_supported(msg);
2790 if (service->immutable == TRUE)
2791 return __connman_error_not_supported(msg);
2793 if (service->favorite == FALSE &&
2794 combine_state(service->state_ipv4, service->state_ipv6) !=
2795 CONNMAN_SERVICE_STATE_FAILURE)
2796 return __connman_error_not_supported(msg);
2798 if (service->network != NULL) {
2799 set_reconnect_state(service, FALSE);
2801 __connman_network_disconnect(service->network);
2804 g_free(service->passphrase);
2805 service->passphrase = NULL;
2807 passphrase_changed(service);
2811 __connman_service_set_favorite(service, FALSE);
2812 __connman_storage_save_service(service);
2814 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2817 static DBusMessage *move_service(DBusConnection *conn,
2818 DBusMessage *msg, void *user_data,
2821 struct connman_service *service = user_data;
2822 struct connman_service *target;
2824 GSequenceIter *src, *dst;
2826 DBG("service %p", service);
2828 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2831 if (service->favorite == FALSE)
2832 return __connman_error_not_supported(msg);
2834 target = find_service(path);
2835 if (target == NULL || target->favorite == FALSE || target == service)
2836 return __connman_error_invalid_service(msg);
2838 DBG("target %s", target->identifier);
2840 if (target->state_ipv4 != service->state_ipv4 &&
2841 target->state_ipv6 != service->state_ipv6)
2842 return __connman_error_invalid_service(msg);
2844 g_get_current_time(&service->modified);
2845 __connman_storage_save_service(service);
2847 src = g_hash_table_lookup(service_hash, service->identifier);
2848 dst = g_hash_table_lookup(service_hash, target->identifier);
2850 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2852 __connman_profile_changed(FALSE);
2854 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2857 static DBusMessage *move_before(DBusConnection *conn,
2858 DBusMessage *msg, void *user_data)
2860 return move_service(conn, msg, user_data, TRUE);
2863 static DBusMessage *move_after(DBusConnection *conn,
2864 DBusMessage *msg, void *user_data)
2866 return move_service(conn, msg, user_data, FALSE);
2869 static DBusMessage *reset_counters(DBusConnection *conn,
2870 DBusMessage *msg, void *user_data)
2872 struct connman_service *service = user_data;
2874 reset_stats(service);
2876 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2879 static GDBusMethodTable service_methods[] = {
2880 { "GetProperties", "", "a{sv}", get_properties },
2881 { "SetProperty", "sv", "", set_property },
2882 { "ClearProperty", "s", "", clear_property },
2883 { "Connect", "", "", connect_service,
2884 G_DBUS_METHOD_FLAG_ASYNC },
2885 { "Disconnect", "", "", disconnect_service },
2886 { "Remove", "", "", remove_service },
2887 { "MoveBefore", "o", "", move_before },
2888 { "MoveAfter", "o", "", move_after },
2889 { "ResetCounters", "", "", reset_counters },
2893 static GDBusSignalTable service_signals[] = {
2894 { "PropertyChanged", "sv" },
2898 static void service_free(gpointer user_data)
2900 struct connman_service *service = user_data;
2901 char *path = service->path;
2903 DBG("service %p", service);
2905 reply_pending(service, ENOENT);
2907 g_hash_table_remove(service_hash, service->identifier);
2909 __connman_notifier_service_remove(service);
2911 stats_stop(service);
2912 __connman_storage_save_service(service);
2914 service->path = NULL;
2917 __connman_profile_changed(FALSE);
2919 g_dbus_unregister_interface(connection, path,
2920 CONNMAN_SERVICE_INTERFACE);
2924 g_hash_table_destroy(service->counter_table);
2926 if (service->network != NULL) {
2927 if (service->network_created == TRUE)
2928 connman_network_unregister(service->network);
2929 connman_network_unref(service->network);
2932 if (service->provider != NULL)
2933 connman_provider_unref(service->provider);
2935 if (service->ipconfig_ipv4 != NULL) {
2936 connman_ipconfig_unref(service->ipconfig_ipv4);
2937 service->ipconfig_ipv4 = NULL;
2940 if (service->ipconfig_ipv6 != NULL) {
2941 connman_ipconfig_unref(service->ipconfig_ipv6);
2942 service->ipconfig_ipv6 = NULL;
2945 if (service->location != NULL)
2946 connman_location_unref(service->location);
2948 g_strfreev(service->nameservers);
2949 g_strfreev(service->nameservers_config);
2950 g_strfreev(service->domains);
2951 g_strfreev(service->proxies);
2952 g_strfreev(service->excludes);
2954 g_free(service->domainname);
2955 g_free(service->pac);
2956 g_free(service->profile);
2957 g_free(service->name);
2958 g_free(service->passphrase);
2959 g_free(service->identifier);
2960 g_free(service->eap);
2961 g_free(service->identity);
2962 g_free(service->ca_cert_file);
2963 g_free(service->client_cert_file);
2964 g_free(service->private_key_file);
2965 g_free(service->private_key_passphrase);
2966 g_free(service->phase2);
2968 if (service->stats.timer != NULL)
2969 g_timer_destroy(service->stats.timer);
2970 if (service->stats_roaming.timer != NULL)
2971 g_timer_destroy(service->stats_roaming.timer);
2977 * __connman_service_put:
2978 * @service: service structure
2980 * Release service if no longer needed
2982 void __connman_service_put(struct connman_service *service)
2984 DBG("service %p", service);
2986 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2987 GSequenceIter *iter;
2989 iter = g_hash_table_lookup(service_hash, service->identifier);
2991 reply_pending(service, ECONNABORTED);
2993 __connman_service_disconnect(service);
2995 g_sequence_remove(iter);
2997 service_free(service);
3001 static void stats_init(struct connman_service *service)
3004 service->stats.valid = FALSE;
3005 service->stats.enabled = FALSE;
3006 service->stats.timer = g_timer_new();
3009 service->stats_roaming.valid = FALSE;
3010 service->stats_roaming.enabled = FALSE;
3011 service->stats_roaming.timer = g_timer_new();
3014 static void service_initialize(struct connman_service *service)
3016 DBG("service %p", service);
3018 service->refcount = 1;
3019 service->session_usage_count = 0;
3021 service->network_created = FALSE;
3023 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3024 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3026 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3027 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3029 service->favorite = FALSE;
3030 service->immutable = FALSE;
3031 service->hidden = FALSE;
3033 service->ignore = FALSE;
3035 service->userconnect = FALSE;
3039 stats_init(service);
3041 service->provider = NULL;
3043 service->wps = FALSE;
3047 * connman_service_create:
3049 * Allocate a new service.
3051 * Returns: a newly-allocated #connman_service structure
3053 struct connman_service *connman_service_create(void)
3056 struct connman_stats_counter *counters;
3057 const char *counter;
3059 struct connman_service *service;
3061 service = g_try_new0(struct connman_service, 1);
3062 if (service == NULL)
3065 DBG("service %p", service);
3067 service->counter_table = g_hash_table_new_full(g_str_hash,
3068 g_str_equal, NULL, g_free);
3070 for (list = counter_list; list; list = list->next) {
3071 counter = list->data;
3073 counters = g_try_new0(struct connman_stats_counter, 1);
3074 if (counters == NULL) {
3075 g_hash_table_destroy(service->counter_table);
3080 counters->append_all = TRUE;
3082 g_hash_table_replace(service->counter_table, (gpointer)counter,
3086 service_initialize(service);
3088 service->location = __connman_location_create(service);
3093 struct connman_location *__connman_service_get_location(struct connman_service *service)
3095 return service->location;
3099 * connman_service_ref:
3100 * @service: service structure
3102 * Increase reference counter of service
3104 struct connman_service *connman_service_ref(struct connman_service *service)
3108 g_atomic_int_inc(&service->refcount);
3114 * connman_service_unref:
3115 * @service: service structure
3117 * Decrease reference counter of service
3119 void connman_service_unref(struct connman_service *service)
3121 __connman_service_put(service);
3124 static gint service_compare(gconstpointer a, gconstpointer b,
3127 struct connman_service *service_a = (void *) a;
3128 struct connman_service *service_b = (void *) b;
3129 enum connman_service_state state_a, state_b;
3131 state_a = combine_state(service_a->state_ipv4, service_a->state_ipv6);
3132 state_b = combine_state(service_b->state_ipv4, service_b->state_ipv6);
3134 if (state_a != state_b) {
3135 if (is_connected(service_a) == TRUE)
3137 if (is_connected(service_b) == TRUE)
3140 if (is_connecting(service_a) == TRUE)
3142 if (is_connecting(service_b) == TRUE)
3146 if (service_a->order > service_b->order)
3149 if (service_a->order < service_b->order)
3152 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3155 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3158 if (service_a->type != service_b->type) {
3159 switch (service_a->type) {
3160 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3161 case CONNMAN_SERVICE_TYPE_SYSTEM:
3162 case CONNMAN_SERVICE_TYPE_ETHERNET:
3163 case CONNMAN_SERVICE_TYPE_GPS:
3164 case CONNMAN_SERVICE_TYPE_VPN:
3165 case CONNMAN_SERVICE_TYPE_GADGET:
3167 case CONNMAN_SERVICE_TYPE_WIFI:
3169 case CONNMAN_SERVICE_TYPE_WIMAX:
3170 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3171 case CONNMAN_SERVICE_TYPE_CELLULAR:
3176 return (gint) service_b->strength - (gint) service_a->strength;
3180 * connman_service_get_type:
3181 * @service: service structure
3183 * Get the type of service
3185 enum connman_service_type connman_service_get_type(struct connman_service *service)
3187 if (service == NULL)
3188 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3190 return service->type;
3194 * connman_service_get_interface:
3195 * @service: service structure
3197 * Get network interface of service
3199 char *connman_service_get_interface(struct connman_service *service)
3203 if (service == NULL)
3206 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3207 if (service->ipconfig_ipv4)
3208 index = connman_ipconfig_get_index(
3209 service->ipconfig_ipv4);
3210 else if (service->ipconfig_ipv6)
3211 index = connman_ipconfig_get_index(
3212 service->ipconfig_ipv6);
3216 return connman_inet_ifname(index);
3219 if (service->network == NULL)
3222 index = connman_network_get_index(service->network);
3224 return connman_inet_ifname(index);
3228 * connman_service_get_network:
3229 * @service: service structure
3231 * Get the service network
3233 struct connman_network *
3234 __connman_service_get_network(struct connman_service *service)
3236 if (service == NULL)
3239 return service->network;
3242 struct connman_ipconfig *
3243 __connman_service_get_ip4config(struct connman_service *service)
3245 if (service == NULL)
3248 return service->ipconfig_ipv4;
3251 struct connman_ipconfig *
3252 __connman_service_get_ip6config(struct connman_service *service)
3254 if (service == NULL)
3257 return service->ipconfig_ipv6;
3260 struct connman_ipconfig *
3261 __connman_service_get_ipconfig(struct connman_service *service, int family)
3263 if (family == AF_INET)
3264 return __connman_service_get_ip4config(service);
3265 else if (family == AF_INET6)
3266 return __connman_service_get_ip6config(service);
3272 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3274 if (service == NULL)
3275 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3277 return service->security;
3280 const char *__connman_service_get_phase2(struct connman_service *service)
3282 if (service == NULL)
3285 return service->phase2;
3288 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3290 if (service == NULL)
3293 return service->wps;
3297 * __connman_service_set_favorite:
3298 * @service: service structure
3299 * @favorite: favorite value
3301 * Change the favorite setting of service
3303 int __connman_service_set_favorite(struct connman_service *service,
3304 connman_bool_t favorite)
3306 GSequenceIter *iter;
3308 iter = g_hash_table_lookup(service_hash, service->identifier);
3312 if (service->favorite == favorite)
3315 service->favorite = favorite;
3317 favorite_changed(service);
3319 g_sequence_sort_changed(iter, service_compare, NULL);
3321 __connman_profile_changed(FALSE);
3326 connman_bool_t __connman_service_get_immutable(struct connman_service *service)
3328 if (service == NULL)
3331 return service->immutable;
3334 int __connman_service_set_immutable(struct connman_service *service,
3335 connman_bool_t immutable)
3337 service->immutable = immutable;
3339 immutable_changed(service);
3344 void __connman_service_set_string(struct connman_service *service,
3345 const char *key, const char *value)
3347 if (g_str_equal(key, "EAP") == TRUE) {
3348 g_free(service->eap);
3349 service->eap = g_strdup(value);
3350 } else if (g_str_equal(key, "Identity") == TRUE) {
3351 g_free(service->identity);
3352 service->identity = g_strdup(value);
3353 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3354 g_free(service->ca_cert_file);
3355 service->ca_cert_file = g_strdup(value);
3356 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3357 g_free(service->client_cert_file);
3358 service->client_cert_file = g_strdup(value);
3359 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3360 g_free(service->private_key_file);
3361 service->private_key_file = g_strdup(value);
3362 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3363 g_free(service->private_key_passphrase);
3364 service->private_key_passphrase = g_strdup(value);
3365 } else if (g_str_equal(key, "Phase2") == TRUE) {
3366 g_free(service->phase2);
3367 service->phase2 = g_strdup(value);
3368 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3369 g_free(service->passphrase);
3370 service->passphrase = g_strdup(value);
3374 static void service_complete(struct connman_service *service)
3376 reply_pending(service, EIO);
3378 if (service->userconnect == FALSE)
3379 __connman_service_auto_connect();
3381 g_get_current_time(&service->modified);
3382 __connman_storage_save_service(service);
3385 static void report_error_cb(struct connman_service *service,
3386 gboolean retry, void *user_data)
3389 __connman_service_connect(service);
3391 service_complete(service);
3392 __connman_profile_changed(FALSE);
3393 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3397 int __connman_service_set_ipconfig_ready(struct connman_service *service,
3398 enum connman_ipconfig_type type)
3400 enum connman_service_state state;
3403 DBG("service %p (%s) type %d (%s)",
3404 service, service ? service->identifier : NULL,
3405 type, __connman_ipconfig_type2string(type));
3407 if (service == NULL)
3410 state = combine_state(service->state_ipv4, service->state_ipv6);
3412 if (state == CONNMAN_SERVICE_STATE_READY ||
3413 state == CONNMAN_SERVICE_STATE_ONLINE) {
3416 err = __connman_service_indicate_state(service,
3417 CONNMAN_SERVICE_STATE_READY, type);
3423 int __connman_service_indicate_state(struct connman_service *service,
3424 enum connman_service_state new_state,
3425 enum connman_ipconfig_type type)
3427 enum connman_service_state service_state,
3428 state = CONNMAN_SERVICE_STATE_IDLE;
3429 GSequenceIter *iter;
3431 if (service == NULL)
3434 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3435 if (service->state_ipv4 == new_state)
3437 state = combine_state(new_state, service->state_ipv6);
3438 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3439 if (service->state_ipv6 == new_state)
3441 state = combine_state(new_state, service->state_ipv4);
3445 service_state = combine_state(service->state_ipv4,
3446 service->state_ipv6);
3448 DBG("service %p state %s/%s => %s new %s/%d => %s",
3450 state2string(service->state_ipv4),
3451 state2string(service->state_ipv6),
3452 state2string(service_state),
3453 state2string(new_state),
3455 state2string(state));
3457 if (service_state == CONNMAN_SERVICE_STATE_FAILURE &&
3458 state == CONNMAN_SERVICE_STATE_IDLE)
3461 if (service_state == CONNMAN_SERVICE_STATE_IDLE &&
3462 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3465 if (state == CONNMAN_SERVICE_STATE_IDLE &&
3466 service_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3467 state_changed(service);
3469 reply_pending(service, ECONNABORTED);
3471 __connman_service_disconnect(service);
3474 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3475 if (__connman_stats_service_register(service) == 0) {
3476 __connman_stats_get(service, FALSE,
3477 &service->stats.data);
3478 __connman_stats_get(service, TRUE,
3479 &service->stats_roaming.data);
3482 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
3483 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3484 __connman_ipconfig_enable(service->ipconfig_ipv4);
3485 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
3486 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3487 __connman_ipconfig_enable(service->ipconfig_ipv6);
3490 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3491 service->state_ipv4 = new_state;
3492 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3493 service->state_ipv6 = new_state;
3495 if (state == service_state)
3498 state_changed(service);
3500 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3501 if (service->login_required == TRUE) {
3502 service->login_required = FALSE;
3503 login_changed(service);
3506 connman_timeserver_sync();
3509 if (state == CONNMAN_SERVICE_STATE_IDLE) {
3510 connman_bool_t reconnect;
3512 reconnect = get_reconnect_state(service);
3513 if (reconnect == TRUE)
3514 __connman_service_auto_connect();
3517 if (state == CONNMAN_SERVICE_STATE_READY) {
3518 enum connman_service_proxy_method proxy_config;
3519 enum connman_ipconfig_method method;
3521 set_reconnect_state(service, TRUE);
3523 __connman_service_set_favorite(service, TRUE);
3525 reply_pending(service, 0);
3527 service->userconnect = FALSE;
3529 g_get_current_time(&service->modified);
3530 __connman_storage_save_service(service);
3532 update_nameservers(service);
3533 dns_changed(service);
3534 domain_changed(service);
3536 proxy_config = service->proxy_config;
3539 * We start WPAD if we haven't got a PAC URL from DHCP and
3540 * if our proxy manual configuration is either empty or set
3541 * to AUTO with an empty URL.
3543 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3544 (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3546 CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3547 service->pac == NULL)))
3548 if (__connman_wpad_start(service) < 0) {
3550 CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3552 __connman_notifier_proxy_changed(service);
3555 __connman_notifier_connect(service->type);
3557 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3558 connman_network_get_bool(service->network,
3559 "WiFi.UseWPS") == TRUE) {
3562 pass = connman_network_get_string(service->network,
3565 __connman_service_set_passphrase(service, pass);
3567 connman_network_set_bool(service->network,
3568 "WiFi.UseWPS", FALSE);
3573 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3574 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3575 __connman_ipconfig_disable_ipv6(
3576 service->ipconfig_ipv6);
3578 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3579 __connman_location_finish(service);
3583 __connman_wpad_stop(service);
3585 update_nameservers(service);
3586 dns_changed(service);
3587 domain_changed(service);
3589 __connman_notifier_disconnect(service->type);
3592 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3593 if (service->userconnect == TRUE &&
3594 __connman_agent_report_error(service,
3595 error2string(service->error),
3596 report_error_cb, NULL) == -EIO)
3598 service_complete(service);
3600 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3602 iter = g_hash_table_lookup(service_hash, service->identifier);
3604 g_sequence_sort_changed(iter, service_compare, NULL);
3606 __connman_profile_changed(FALSE);
3608 service_state = combine_state(service->state_ipv4,
3609 service->state_ipv6);
3610 if (service_state == CONNMAN_SERVICE_STATE_ONLINE)
3613 if (service_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3614 struct connman_service *def_service = get_default();
3616 if (__connman_notifier_count_connected() == 0 &&
3617 def_service != NULL &&
3618 def_service->provider != NULL)
3619 __connman_provider_disconnect(def_service->provider);
3622 if (service_state == CONNMAN_SERVICE_STATE_IDLE ||
3623 service_state == CONNMAN_SERVICE_STATE_FAILURE)
3624 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3629 int __connman_service_indicate_error(struct connman_service *service,
3630 enum connman_service_error error)
3632 DBG("service %p error %d", service, error);
3634 if (service == NULL)
3637 service->error = error;
3639 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3640 __connman_service_set_passphrase(service, NULL);
3642 return __connman_service_indicate_state(service,
3643 CONNMAN_SERVICE_STATE_FAILURE,
3644 CONNMAN_IPCONFIG_TYPE_IPV4);
3647 int __connman_service_clear_error(struct connman_service *service)
3649 enum connman_service_state state;
3651 DBG("service %p", service);
3653 if (service == NULL)
3656 state = combine_state(service->state_ipv4, service->state_ipv6);
3658 if (state != CONNMAN_SERVICE_STATE_FAILURE)
3661 service->state_ipv4 = service->state_ipv6 =
3662 CONNMAN_SERVICE_STATE_UNKNOWN;
3663 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3665 if (service->favorite == TRUE)
3666 set_reconnect_state(service, TRUE);
3668 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
3669 CONNMAN_IPCONFIG_TYPE_IPV6);
3672 * Toggling the IPv6 state to IDLE could trigger the auto connect
3673 * machinery and consequently the IPv4 state.
3675 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3676 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3679 return __connman_service_indicate_state(service,
3680 CONNMAN_SERVICE_STATE_IDLE,
3681 CONNMAN_IPCONFIG_TYPE_IPV4);
3684 int __connman_service_indicate_default(struct connman_service *service)
3686 DBG("service %p", service);
3690 __connman_location_detect(service);
3695 int __connman_service_request_login(struct connman_service *service)
3697 DBG("service %p", service);
3699 if (service == NULL)
3702 service->login_required = TRUE;
3703 login_changed(service);
3708 static connman_bool_t prepare_network(struct connman_service *service)
3710 enum connman_network_type type;
3711 unsigned int ssid_len;
3713 type = connman_network_get_type(service->network);
3716 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3717 case CONNMAN_NETWORK_TYPE_VENDOR:
3719 case CONNMAN_NETWORK_TYPE_WIFI:
3720 if (connman_network_get_blob(service->network, "WiFi.SSID",
3724 if (service->passphrase != NULL)
3725 connman_network_set_string(service->network,
3726 "WiFi.Passphrase", service->passphrase);
3728 case CONNMAN_NETWORK_TYPE_ETHERNET:
3729 case CONNMAN_NETWORK_TYPE_WIMAX:
3730 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3731 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3732 case CONNMAN_NETWORK_TYPE_CELLULAR:
3739 static void prepare_8021x(struct connman_service *service)
3741 if (service->eap != NULL)
3742 connman_network_set_string(service->network, "WiFi.EAP",
3745 if (service->identity != NULL)
3746 connman_network_set_string(service->network, "WiFi.Identity",
3749 if (service->ca_cert_file != NULL)
3750 connman_network_set_string(service->network, "WiFi.CACertFile",
3751 service->ca_cert_file);
3753 if (service->client_cert_file != NULL)
3754 connman_network_set_string(service->network,
3755 "WiFi.ClientCertFile",
3756 service->client_cert_file);
3758 if (service->private_key_file != NULL)
3759 connman_network_set_string(service->network,
3760 "WiFi.PrivateKeyFile",
3761 service->private_key_file);
3763 if (service->private_key_passphrase != NULL)
3764 connman_network_set_string(service->network,
3765 "WiFi.PrivateKeyPassphrase",
3766 service->private_key_passphrase);
3768 if (service->phase2 != NULL)
3769 connman_network_set_string(service->network, "WiFi.Phase2",
3773 static int service_connect(struct connman_service *service)
3777 switch (service->type) {
3778 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3779 case CONNMAN_SERVICE_TYPE_SYSTEM:
3780 case CONNMAN_SERVICE_TYPE_GPS:
3781 case CONNMAN_SERVICE_TYPE_GADGET:
3783 case CONNMAN_SERVICE_TYPE_ETHERNET:
3784 case CONNMAN_SERVICE_TYPE_WIMAX:
3785 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3786 case CONNMAN_SERVICE_TYPE_CELLULAR:
3787 case CONNMAN_SERVICE_TYPE_VPN:
3789 case CONNMAN_SERVICE_TYPE_WIFI:
3790 switch (service->security) {
3791 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3792 case CONNMAN_SERVICE_SECURITY_NONE:
3794 case CONNMAN_SERVICE_SECURITY_WEP:
3795 case CONNMAN_SERVICE_SECURITY_PSK:
3796 case CONNMAN_SERVICE_SECURITY_WPA:
3797 case CONNMAN_SERVICE_SECURITY_RSN:
3798 if (service->passphrase == NULL) {
3799 if (service->network == NULL)
3802 if (service->wps == FALSE ||
3803 connman_network_get_bool(
3805 "WiFi.UseWPS") == FALSE)
3809 case CONNMAN_SERVICE_SECURITY_8021X:
3810 if (service->eap == NULL)
3814 * never request credentials if using EAP-TLS
3815 * (EAP-TLS networks need to be fully provisioned)
3817 if (g_str_equal(service->eap, "tls") == TRUE)
3820 if (service->immutable != TRUE)
3828 if (service->network != NULL) {
3829 if (prepare_network(service) == FALSE)
3832 switch (service->security) {
3833 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3834 case CONNMAN_SERVICE_SECURITY_NONE:
3835 case CONNMAN_SERVICE_SECURITY_WEP:
3836 case CONNMAN_SERVICE_SECURITY_PSK:
3837 case CONNMAN_SERVICE_SECURITY_WPA:
3838 case CONNMAN_SERVICE_SECURITY_RSN:
3840 case CONNMAN_SERVICE_SECURITY_8021X:
3841 prepare_8021x(service);
3845 if (__connman_stats_service_register(service) == 0) {
3846 __connman_stats_get(service, FALSE,
3847 &service->stats.data);
3848 __connman_stats_get(service, TRUE,
3849 &service->stats_roaming.data);
3852 if (service->ipconfig_ipv4)
3853 __connman_ipconfig_enable(service->ipconfig_ipv4);
3854 if (service->ipconfig_ipv6)
3855 __connman_ipconfig_enable(service->ipconfig_ipv6);
3857 err = __connman_network_connect(service->network);
3858 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3859 service->provider != NULL)
3860 err = __connman_provider_connect(service->provider);
3865 if (err != -EINPROGRESS) {
3866 __connman_ipconfig_disable(service->ipconfig_ipv4);
3867 __connman_ipconfig_disable(service->ipconfig_ipv6);
3868 __connman_stats_service_unregister(service);
3876 int __connman_service_connect(struct connman_service *service)
3878 enum connman_service_state state;
3881 state = combine_state(service->state_ipv4, service->state_ipv6);
3883 DBG("service %p state %s", service, state2string(state));
3885 if (is_connected(service) == TRUE)
3888 if (is_connecting(service) == TRUE)
3891 switch (service->type) {
3892 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3893 case CONNMAN_SERVICE_TYPE_SYSTEM:
3894 case CONNMAN_SERVICE_TYPE_GPS:
3895 case CONNMAN_SERVICE_TYPE_GADGET:
3898 err = service_connect(service);
3904 if (err == -EINPROGRESS) {
3905 if (service->timeout == 0)
3906 service->timeout = g_timeout_add_seconds(
3907 CONNECT_TIMEOUT, connect_timeout, service);
3909 return -EINPROGRESS;
3915 if (service->userconnect == TRUE)
3916 reply_pending(service, err);
3918 __connman_service_indicate_state(service,
3919 CONNMAN_SERVICE_STATE_FAILURE,
3920 CONNMAN_IPCONFIG_TYPE_IPV4);
3921 __connman_service_indicate_state(service,
3922 CONNMAN_SERVICE_STATE_FAILURE,
3923 CONNMAN_IPCONFIG_TYPE_IPV6);
3928 int __connman_service_disconnect(struct connman_service *service)
3932 DBG("service %p", service);
3934 if (service->network != NULL) {
3935 err = __connman_network_disconnect(service->network);
3936 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3937 service->provider != NULL)
3938 err = __connman_provider_disconnect(service->provider);
3942 if (err < 0 && err != -EINPROGRESS)
3945 __connman_6to4_remove(service->ipconfig_ipv4);
3947 if (service->ipconfig_ipv4)
3948 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3951 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3954 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3955 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3957 __connman_ipconfig_disable(service->ipconfig_ipv4);
3958 __connman_ipconfig_disable(service->ipconfig_ipv6);
3960 __connman_stats_service_unregister(service);
3965 int __connman_service_disconnect_all(void)
3967 GSequenceIter *iter;
3971 iter = g_sequence_get_begin_iter(service_list);
3973 while (g_sequence_iter_is_end(iter) == FALSE) {
3974 struct connman_service *service = g_sequence_get(iter);
3976 service->ignore = TRUE;
3978 set_reconnect_state(service, FALSE);
3980 __connman_service_disconnect(service);
3982 iter = g_sequence_iter_next(iter);
3990 * __connman_service_lookup:
3991 * @pattern: search pattern
3992 * @path: return object path
3994 * Look up a service path from a search pattern
3996 int __connman_service_lookup(const char *pattern, const char **path)
3998 GHashTableIter iter;
3999 gpointer key, value;
4001 g_hash_table_iter_init(&iter, service_hash);
4003 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4004 GSequenceIter *iter = value;
4005 struct connman_service *service = g_sequence_get(iter);
4007 if (g_strcmp0(service->identifier, pattern) == 0 ||
4008 g_strcmp0(service->name, pattern) == 0) {
4009 *path = (const char *) service->path;
4018 * lookup_by_identifier:
4019 * @identifier: service identifier
4021 * Look up a service by identifier (reference count will not be increased)
4023 static struct connman_service *lookup_by_identifier(const char *identifier)
4025 GSequenceIter *iter;
4027 iter = g_hash_table_lookup(service_hash, identifier);
4029 return g_sequence_get(iter);
4034 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4035 const char *ssid, const char *mode, const char *security)
4037 struct connman_network *network;
4040 unsigned int i, ssid_len;
4042 ssid_len = strlen(ssid);
4046 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
4047 if (network == NULL)
4050 connman_network_register(network);
4052 connman_network_set_blob(network, "WiFi.SSID",
4053 (unsigned char *) ssid, ssid_len);
4055 connman_network_set_string(network, "WiFi.Mode", mode);
4056 connman_network_set_string(network, "WiFi.Security", security);
4058 name = g_try_malloc0(ssid_len + 1);
4060 connman_network_unregister(network);
4061 connman_network_unref(network);
4065 for (i = 0; i < ssid_len; i++) {
4066 if (g_ascii_isprint(ssid[i]))
4072 connman_network_set_name(network, name);
4076 index = connman_device_get_index(device);
4077 connman_network_set_index(network, index);
4079 if (connman_device_add_network(device, network) < 0) {
4080 connman_network_unregister(network);
4081 connman_network_unref(network);
4085 connman_network_set_available(network, TRUE);
4090 int __connman_service_create_and_connect(DBusMessage *msg)
4092 struct connman_service *service;
4093 struct connman_network *network;
4094 struct connman_device *device;
4095 DBusMessageIter iter, array;
4096 const char *mode = "managed", *security = "none", *group_security;
4097 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4098 unsigned int ssid_len = 0;
4103 dbus_message_iter_init(msg, &iter);
4104 dbus_message_iter_recurse(&iter, &array);
4106 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4107 DBusMessageIter entry, value;
4110 dbus_message_iter_recurse(&array, &entry);
4111 dbus_message_iter_get_basic(&entry, &key);
4113 dbus_message_iter_next(&entry);
4114 dbus_message_iter_recurse(&entry, &value);
4116 switch (dbus_message_iter_get_arg_type(&value)) {
4117 case DBUS_TYPE_STRING:
4118 if (g_str_equal(key, "Type") == TRUE)
4119 dbus_message_iter_get_basic(&value, &type);
4120 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4121 g_str_equal(key, "Mode") == TRUE)
4122 dbus_message_iter_get_basic(&value, &mode);
4123 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4124 g_str_equal(key, "Security") == TRUE)
4125 dbus_message_iter_get_basic(&value, &security);
4126 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4127 g_str_equal(key, "Passphrase") == TRUE)
4128 dbus_message_iter_get_basic(&value, &passphrase);
4129 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4130 g_str_equal(key, "SSID") == TRUE)
4131 dbus_message_iter_get_basic(&value, &ssid);
4134 dbus_message_iter_next(&array);
4140 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4146 ssid_len = strlen(ssid);
4150 if (g_strcmp0(security, "none") != 0 &&
4151 g_strcmp0(security, "wep") != 0 &&
4152 g_strcmp0(security, "psk") != 0 &&
4153 g_strcmp0(security, "wpa") != 0 &&
4154 g_strcmp0(security, "rsn") != 0 &&
4155 g_strcmp0(security, "ieee8021x") != 0)
4158 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4162 ident = connman_device_get_ident(device);
4167 if (!g_strcmp0(security, "wpa") ||
4168 !g_strcmp0(security, "rsn"))
4169 group_security = "psk";
4171 group_security = security;
4173 group = connman_wifi_build_group_name((unsigned char *) ssid,
4174 ssid_len, mode, group_security);
4178 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4180 service = lookup_by_identifier(name);
4182 if (service != NULL)
4185 network = create_hidden_wifi(device, ssid, mode, security);
4186 if (network != NULL) {
4187 connman_network_set_group(network, group);
4188 service->network_created = TRUE;
4191 service = lookup_by_identifier(name);
4197 if (service == NULL) {
4202 if (is_connected(service) == TRUE) {
4207 if (is_connecting(service) == TRUE) {
4212 set_reconnect_state(service, FALSE);
4214 __connman_device_disconnect(device);
4216 if (passphrase != NULL) {
4217 g_free(service->passphrase);
4218 service->passphrase = g_strdup(passphrase);
4221 service->userconnect = TRUE;
4223 err = __connman_service_connect(service);
4224 if (err < 0 && err != -EINPROGRESS)
4227 service->pending = dbus_message_ref(msg);
4232 if (service != NULL && service->network_created == TRUE) {
4233 struct connman_network *network = service->network;
4235 if (network != NULL) {
4236 connman_network_set_available(network, FALSE);
4237 __connman_device_cleanup_networks(device);
4239 __connman_service_put(service);
4245 int __connman_service_provision(DBusMessage *msg)
4247 GKeyFile *keyfile = NULL;
4248 const char *config_str = NULL;
4249 char *group = NULL, *ident = NULL;
4251 struct connman_service *service;
4255 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4258 if (config_str == NULL || strlen(config_str) == 0)
4261 keyfile = g_key_file_new();
4263 /* populate GKeyFile with config_str */
4264 if (g_key_file_load_from_data(keyfile, config_str,
4265 strlen(config_str), 0, NULL) == FALSE) {
4271 * read only one group of settings (only one service supported, no
4274 group = g_key_file_get_start_group(keyfile);
4276 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4281 err = __connman_config_load_service(keyfile, group, TRUE);
4285 ident = group + strlen("service_");
4287 /* trigger service provisioning if service exists */
4288 service = lookup_by_identifier(ident);
4289 if (service != NULL)
4290 __connman_config_provision_service(service);
4292 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4298 if (keyfile != NULL)
4299 g_key_file_free(keyfile);
4305 * __connman_service_get:
4306 * @identifier: service identifier
4308 * Look up a service by identifier or create a new one if not found
4310 static struct connman_service *service_get(const char *identifier)
4312 struct connman_service *service;
4313 GSequenceIter *iter;
4315 iter = g_hash_table_lookup(service_hash, identifier);
4317 service = g_sequence_get(iter);
4318 if (service != NULL)
4319 connman_service_ref(service);
4323 service = connman_service_create();
4324 if (service == NULL)
4327 DBG("service %p", service);
4329 service->identifier = g_strdup(identifier);
4331 service->profile = g_strdup(__connman_profile_active_ident());
4333 iter = g_sequence_insert_sorted(service_list, service,
4334 service_compare, NULL);
4336 g_hash_table_insert(service_hash, service->identifier, iter);
4341 static int service_register(struct connman_service *service)
4343 const char *path = __connman_profile_active_path();
4344 GSequenceIter *iter;
4346 DBG("service %p", service);
4348 if (service->path != NULL)
4351 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4353 DBG("path %s", service->path);
4355 __connman_config_provision_service(service);
4357 __connman_storage_load_service(service);
4359 g_dbus_register_interface(connection, service->path,
4360 CONNMAN_SERVICE_INTERFACE,
4361 service_methods, service_signals,
4362 NULL, service, NULL);
4364 iter = g_hash_table_lookup(service_hash, service->identifier);
4366 g_sequence_sort_changed(iter, service_compare, NULL);
4368 __connman_profile_changed(TRUE);
4373 static void service_up(struct connman_ipconfig *ipconfig)
4375 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4377 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4379 link_changed(service);
4381 service->stats.valid = FALSE;
4382 service->stats_roaming.valid = FALSE;
4385 static void service_down(struct connman_ipconfig *ipconfig)
4387 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4390 static void service_lower_up(struct connman_ipconfig *ipconfig)
4392 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4394 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4396 stats_start(service);
4399 static void service_lower_down(struct connman_ipconfig *ipconfig)
4401 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4403 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4405 stats_stop(service);
4406 __connman_storage_save_service(service);
4409 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4411 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4412 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4413 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4415 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4417 type = __connman_ipconfig_get_config_type(ipconfig);
4418 method = __connman_ipconfig_get_method(ipconfig);
4420 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4423 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4424 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4425 __connman_service_set_ipconfig_ready(service,
4426 CONNMAN_IPCONFIG_TYPE_IPV6);
4428 settings_changed(service, ipconfig);
4431 static void service_ip_release(struct connman_ipconfig *ipconfig)
4433 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4434 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4435 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4437 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4439 type = __connman_ipconfig_get_config_type(ipconfig);
4440 method = __connman_ipconfig_get_method(ipconfig);
4442 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4445 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4446 method == CONNMAN_IPCONFIG_METHOD_OFF)
4447 __connman_service_indicate_state(service,
4448 CONNMAN_SERVICE_STATE_DISCONNECT,
4449 CONNMAN_IPCONFIG_TYPE_IPV6);
4451 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4452 method == CONNMAN_IPCONFIG_METHOD_OFF)
4453 __connman_service_indicate_state(service,
4454 CONNMAN_SERVICE_STATE_DISCONNECT,
4455 CONNMAN_IPCONFIG_TYPE_IPV4);
4457 settings_changed(service, ipconfig);
4460 static const struct connman_ipconfig_ops service_ops = {
4462 .down = service_down,
4463 .lower_up = service_lower_up,
4464 .lower_down = service_lower_down,
4465 .ip_bound = service_ip_bound,
4466 .ip_release = service_ip_release,
4469 static void setup_ip4config(struct connman_service *service, int index)
4474 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4475 CONNMAN_IPCONFIG_TYPE_IPV4);
4476 if (service->ipconfig_ipv4 == NULL)
4479 connman_ipconfig_set_method(service->ipconfig_ipv4,
4480 CONNMAN_IPCONFIG_METHOD_DHCP);
4482 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4484 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4487 static void setup_ip6config(struct connman_service *service, int index)
4492 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4493 CONNMAN_IPCONFIG_TYPE_IPV6);
4494 if (service->ipconfig_ipv6 == NULL)
4497 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4499 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4502 void __connman_service_create_ip4config(struct connman_service *service,
4505 const char *ident = service->profile;
4508 DBG("ipv4 %p", service->ipconfig_ipv4);
4510 if (service->ipconfig_ipv4 != NULL)
4513 setup_ip4config(service, index);
4518 keyfile = __connman_storage_open_profile(ident);
4519 if (keyfile == NULL)
4522 if (service->ipconfig_ipv4)
4523 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4524 service->identifier, "IPv4.");
4525 g_key_file_free(keyfile);
4528 void __connman_service_create_ip6config(struct connman_service *service,
4531 const char *ident = service->profile;
4534 DBG("ipv6 %p", service->ipconfig_ipv6);
4536 if (service->ipconfig_ipv6 != NULL)
4539 setup_ip6config(service, index);
4544 keyfile = __connman_storage_open_profile(ident);
4545 if (keyfile == NULL)
4548 if (service->ipconfig_ipv6 != NULL)
4549 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4550 service->identifier, "IPv6.");
4552 g_key_file_free(keyfile);
4556 * __connman_service_lookup_from_network:
4557 * @network: network structure
4559 * Look up a service by network (reference count will not be increased)
4561 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4563 struct connman_service *service;
4564 const char *ident, *group;
4567 DBG("network %p", network);
4569 ident = __connman_network_get_ident(network);
4573 group = connman_network_get_group(network);
4577 name = g_strdup_printf("%s_%s_%s",
4578 __connman_network_get_type(network), ident, group);
4579 service = lookup_by_identifier(name);
4585 struct connman_service *__connman_service_lookup_from_index(int index)
4587 struct connman_service *service;
4588 GSequenceIter *iter;
4590 iter = g_sequence_get_begin_iter(service_list);
4592 while (g_sequence_iter_is_end(iter) == FALSE) {
4593 service = g_sequence_get(iter);
4595 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4599 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4603 iter = g_sequence_iter_next(iter);
4609 const char *__connman_service_get_ident(struct connman_service *service)
4611 return service->identifier;
4614 const char *__connman_service_get_path(struct connman_service *service)
4616 return service->path;
4619 unsigned int __connman_service_get_order(struct connman_service *service)
4621 GSequenceIter *iter;
4623 if (service == NULL)
4626 if (service->favorite == FALSE) {
4631 iter = g_hash_table_lookup(service_hash, service->identifier);
4633 if (g_sequence_iter_get_position(iter) == 0)
4635 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4636 service->order = 10;
4642 return service->order;
4645 static enum connman_service_type convert_network_type(struct connman_network *network)
4647 enum connman_network_type type = connman_network_get_type(network);
4650 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4651 case CONNMAN_NETWORK_TYPE_VENDOR:
4653 case CONNMAN_NETWORK_TYPE_ETHERNET:
4654 return CONNMAN_SERVICE_TYPE_ETHERNET;
4655 case CONNMAN_NETWORK_TYPE_WIFI:
4656 return CONNMAN_SERVICE_TYPE_WIFI;
4657 case CONNMAN_NETWORK_TYPE_WIMAX:
4658 return CONNMAN_SERVICE_TYPE_WIMAX;
4659 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4660 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4661 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4662 case CONNMAN_NETWORK_TYPE_CELLULAR:
4663 return CONNMAN_SERVICE_TYPE_CELLULAR;
4666 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4669 static enum connman_service_security convert_wifi_security(const char *security)
4671 if (security == NULL)
4672 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4673 else if (g_str_equal(security, "none") == TRUE)
4674 return CONNMAN_SERVICE_SECURITY_NONE;
4675 else if (g_str_equal(security, "wep") == TRUE)
4676 return CONNMAN_SERVICE_SECURITY_WEP;
4677 else if (g_str_equal(security, "psk") == TRUE)
4678 return CONNMAN_SERVICE_SECURITY_PSK;
4679 else if (g_str_equal(security, "ieee8021x") == TRUE)
4680 return CONNMAN_SERVICE_SECURITY_8021X;
4681 else if (g_str_equal(security, "wpa") == TRUE)
4682 return CONNMAN_SERVICE_SECURITY_WPA;
4683 else if (g_str_equal(security, "rsn") == TRUE)
4684 return CONNMAN_SERVICE_SECURITY_RSN;
4686 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4689 static void update_from_network(struct connman_service *service,
4690 struct connman_network *network)
4692 connman_uint8_t strength = service->strength;
4693 GSequenceIter *iter;
4696 DBG("service %p network %p", service, network);
4698 if (is_connected(service) == TRUE)
4701 if (is_connecting(service) == TRUE)
4704 str = connman_network_get_string(network, "Name");
4706 g_free(service->name);
4707 service->name = g_strdup(str);
4708 service->hidden = FALSE;
4710 g_free(service->name);
4711 service->name = NULL;
4712 service->hidden = TRUE;
4715 service->strength = connman_network_get_strength(network);
4716 service->roaming = connman_network_get_bool(network, "Roaming");
4718 if (service->strength == 0) {
4720 * Filter out 0-values; it's unclear what they mean
4721 * and they cause anomalous sorting of the priority list.
4723 service->strength = strength;
4726 str = connman_network_get_string(network, "WiFi.Security");
4727 service->security = convert_wifi_security(str);
4729 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4730 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4732 if (service->strength > strength && service->network != NULL) {
4733 connman_network_unref(service->network);
4734 service->network = connman_network_ref(network);
4736 strength_changed(service);
4739 if (service->network == NULL)
4740 service->network = connman_network_ref(network);
4742 iter = g_hash_table_lookup(service_hash, service->identifier);
4744 g_sequence_sort_changed(iter, service_compare, NULL);
4748 * __connman_service_create_from_network:
4749 * @network: network structure
4751 * Look up service by network and if not found, create one
4753 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4755 struct connman_service *service;
4756 struct connman_device *device;
4757 const char *ident, *group;
4761 DBG("network %p", network);
4763 ident = __connman_network_get_ident(network);
4767 group = connman_network_get_group(network);
4771 name = g_strdup_printf("%s_%s_%s",
4772 __connman_network_get_type(network), ident, group);
4773 service = service_get(name);
4776 if (service == NULL)
4779 if (__connman_network_get_weakness(network) == TRUE)
4782 if (service->path != NULL) {
4783 update_from_network(service, network);
4784 __connman_profile_changed(TRUE);
4788 service->type = convert_network_type(network);
4790 switch (service->type) {
4791 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4792 case CONNMAN_SERVICE_TYPE_SYSTEM:
4793 case CONNMAN_SERVICE_TYPE_ETHERNET:
4794 case CONNMAN_SERVICE_TYPE_WIMAX:
4795 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4796 case CONNMAN_SERVICE_TYPE_GPS:
4797 case CONNMAN_SERVICE_TYPE_VPN:
4798 case CONNMAN_SERVICE_TYPE_GADGET:
4799 service->autoconnect = FALSE;
4801 case CONNMAN_SERVICE_TYPE_WIFI:
4802 case CONNMAN_SERVICE_TYPE_CELLULAR:
4803 service->autoconnect = TRUE;
4807 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4809 update_from_network(service, network);
4811 index = connman_network_get_index(network);
4813 if (service->ipconfig_ipv4 == NULL)
4814 setup_ip4config(service, index);
4816 if (service->ipconfig_ipv6 == NULL)
4817 setup_ip6config(service, index);
4819 service_register(service);
4821 if (service->favorite == TRUE) {
4822 device = connman_network_get_device(service->network);
4823 if (device && __connman_device_scanning(device) == FALSE)
4824 __connman_service_auto_connect();
4827 __connman_notifier_service_add(service);
4832 void __connman_service_update_from_network(struct connman_network *network)
4834 struct connman_service *service;
4835 connman_uint8_t strength;
4836 connman_bool_t roaming;
4837 GSequenceIter *iter;
4839 connman_bool_t stats_enable;
4841 DBG("network %p", network);
4843 service = __connman_service_lookup_from_network(network);
4844 if (service == NULL)
4847 if (service->network == NULL)
4850 name = connman_network_get_string(service->network, "Name");
4851 if (g_strcmp0(service->name, name) != 0) {
4852 g_free(service->name);
4853 service->name = g_strdup(name);
4854 connman_dbus_property_changed_basic(service->path,
4855 CONNMAN_SERVICE_INTERFACE, "Name",
4856 DBUS_TYPE_STRING, &service->name);
4859 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4860 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4862 strength = connman_network_get_strength(service->network);
4863 if (strength == service->strength)
4866 service->strength = strength;
4868 strength_changed(service);
4871 roaming = connman_network_get_bool(service->network, "Roaming");
4872 if (roaming == service->roaming)
4875 stats_enable = stats_enabled(service);
4876 if (stats_enable == TRUE)
4877 stats_stop(service);
4879 service->roaming = roaming;
4881 if (stats_enable == TRUE)
4882 stats_start(service);
4884 roaming_changed(service);
4886 iter = g_hash_table_lookup(service_hash, service->identifier);
4888 g_sequence_sort_changed(iter, service_compare, NULL);
4891 void __connman_service_remove_from_network(struct connman_network *network)
4893 struct connman_service *service;
4895 DBG("network %p", network);
4897 service = __connman_service_lookup_from_network(network);
4898 if (service == NULL)
4901 __connman_service_put(service);
4905 * __connman_service_create_from_provider:
4906 * @provider: provider structure
4908 * Look up service by provider and if not found, create one
4910 struct connman_service *
4911 __connman_service_create_from_provider(struct connman_provider *provider)
4913 struct connman_service *service;
4914 const char *ident, *str;
4916 int index = connman_provider_get_index(provider);
4918 DBG("provider %p", provider);
4920 ident = __connman_provider_get_ident(provider);
4924 name = g_strdup_printf("vpn_%s", ident);
4925 service = service_get(name);
4928 if (service == NULL)
4931 service->type = CONNMAN_SERVICE_TYPE_VPN;
4932 service->provider = connman_provider_ref(provider);
4933 service->autoconnect = FALSE;
4935 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4937 str = connman_provider_get_string(provider, "Name");
4939 g_free(service->name);
4940 service->name = g_strdup(str);
4941 service->hidden = FALSE;
4943 g_free(service->name);
4944 service->name = NULL;
4945 service->hidden = TRUE;
4948 service->strength = 0;
4950 if (service->ipconfig_ipv4 == NULL)
4951 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4952 CONNMAN_IPCONFIG_TYPE_IPV4);
4953 if (service->ipconfig_ipv4 == NULL)
4956 connman_ipconfig_set_method(service->ipconfig_ipv4,
4957 CONNMAN_IPCONFIG_METHOD_MANUAL);
4958 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4959 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4961 if (service->ipconfig_ipv6 == NULL)
4962 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4963 CONNMAN_IPCONFIG_TYPE_IPV6);
4964 if (service->ipconfig_ipv6 == NULL)
4967 connman_ipconfig_set_method(service->ipconfig_ipv6,
4968 CONNMAN_IPCONFIG_METHOD_OFF);
4969 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4970 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4972 service_register(service);
4974 __connman_notifier_service_add(service);
4979 static int service_load(struct connman_service *service)
4981 const char *ident = service->profile;
4983 GError *error = NULL;
4984 gchar *pathname, *data = NULL;
4987 connman_bool_t autoconnect;
4988 unsigned int ssid_len;
4991 DBG("service %p", service);
4996 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4997 if (pathname == NULL)
5000 keyfile = g_key_file_new();
5002 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
5009 if (g_key_file_load_from_data(keyfile, data, length,
5010 0, NULL) == FALSE) {
5017 switch (service->type) {
5018 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5019 case CONNMAN_SERVICE_TYPE_SYSTEM:
5020 case CONNMAN_SERVICE_TYPE_ETHERNET:
5021 case CONNMAN_SERVICE_TYPE_GPS:
5022 case CONNMAN_SERVICE_TYPE_VPN:
5023 case CONNMAN_SERVICE_TYPE_GADGET:
5025 case CONNMAN_SERVICE_TYPE_WIFI:
5026 if (service->name == NULL) {
5029 name = g_key_file_get_string(keyfile,
5030 service->identifier, "Name", NULL);
5032 g_free(service->name);
5033 service->name = name;
5036 if (service->network != NULL)
5037 connman_network_set_name(service->network,
5041 if (service->network &&
5042 connman_network_get_blob(service->network,
5043 "WiFi.SSID", &ssid_len) == NULL) {
5046 hex_ssid = g_key_file_get_string(keyfile,
5047 service->identifier,
5050 if (hex_ssid != NULL) {
5052 unsigned int i, j = 0, hex;
5053 size_t hex_ssid_len = strlen(hex_ssid);
5055 ssid = g_try_malloc0(hex_ssid_len / 2);
5062 for (i = 0; i < hex_ssid_len; i += 2) {
5063 sscanf(hex_ssid + i, "%02x", &hex);
5067 connman_network_set_blob(service->network,
5068 "WiFi.SSID", ssid, hex_ssid_len / 2);
5075 case CONNMAN_SERVICE_TYPE_WIMAX:
5076 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5077 case CONNMAN_SERVICE_TYPE_CELLULAR:
5078 service->favorite = g_key_file_get_boolean(keyfile,
5079 service->identifier, "Favorite", NULL);
5081 autoconnect = g_key_file_get_boolean(keyfile,
5082 service->identifier, "AutoConnect", &error);
5084 service->autoconnect = autoconnect;
5085 g_clear_error(&error);
5087 str = g_key_file_get_string(keyfile,
5088 service->identifier, "Failure", NULL);
5090 if (service->favorite == FALSE)
5091 service->state_ipv4 = service->state_ipv6 =
5092 CONNMAN_SERVICE_STATE_FAILURE;
5093 service->error = string2error(str);
5098 str = g_key_file_get_string(keyfile,
5099 service->identifier, "Modified", NULL);
5101 g_time_val_from_iso8601(str, &service->modified);
5105 str = g_key_file_get_string(keyfile,
5106 service->identifier, "Passphrase", NULL);
5108 g_free(service->passphrase);
5109 service->passphrase = str;
5112 if (service->ipconfig_ipv4 != NULL)
5113 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5114 service->identifier, "IPv4.");
5116 if (service->ipconfig_ipv6 != NULL)
5117 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5118 service->identifier, "IPv6.");
5120 service->nameservers_config = g_key_file_get_string_list(keyfile,
5121 service->identifier, "Nameservers", &length, NULL);
5122 if (service->nameservers_config != NULL && length == 0) {
5123 g_strfreev(service->nameservers_config);
5124 service->nameservers_config = NULL;
5127 service->domains = g_key_file_get_string_list(keyfile,
5128 service->identifier, "Domains", &length, NULL);
5129 if (service->domains != NULL && length == 0) {
5130 g_strfreev(service->domains);
5131 service->domains = NULL;
5134 str = g_key_file_get_string(keyfile,
5135 service->identifier, "Proxy.Method", NULL);
5137 service->proxy_config = string2proxymethod(str);
5141 service->proxies = g_key_file_get_string_list(keyfile,
5142 service->identifier, "Proxy.Servers", &length, NULL);
5143 if (service->proxies != NULL && length == 0) {
5144 g_strfreev(service->proxies);
5145 service->proxies = NULL;
5148 service->excludes = g_key_file_get_string_list(keyfile,
5149 service->identifier, "Proxy.Excludes", &length, NULL);
5150 if (service->excludes != NULL && length == 0) {
5151 g_strfreev(service->excludes);
5152 service->excludes = NULL;
5155 str = g_key_file_get_string(keyfile,
5156 service->identifier, "Proxy.URL", NULL);
5158 g_free(service->pac);
5163 g_key_file_free(keyfile);
5168 static int service_save(struct connman_service *service)
5170 const char *ident = service->profile;
5172 gchar *pathname, *data = NULL;
5175 const char *cst_str = NULL;
5178 DBG("service %p", service);
5183 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5184 if (pathname == NULL)
5187 keyfile = g_key_file_new();
5189 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5193 if (g_key_file_load_from_data(keyfile, data, length,
5201 if (service->name != NULL)
5202 g_key_file_set_string(keyfile, service->identifier,
5203 "Name", service->name);
5205 switch (service->type) {
5206 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5207 case CONNMAN_SERVICE_TYPE_SYSTEM:
5208 case CONNMAN_SERVICE_TYPE_ETHERNET:
5209 case CONNMAN_SERVICE_TYPE_GPS:
5210 case CONNMAN_SERVICE_TYPE_VPN:
5211 case CONNMAN_SERVICE_TYPE_GADGET:
5213 case CONNMAN_SERVICE_TYPE_WIFI:
5214 if (service->network) {
5215 const unsigned char *ssid;
5216 unsigned int ssid_len = 0;
5218 ssid = connman_network_get_blob(service->network,
5219 "WiFi.SSID", &ssid_len);
5221 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5222 char *identifier = service->identifier;
5226 str = g_string_sized_new(ssid_len * 2);
5232 for (i = 0; i < ssid_len; i++)
5233 g_string_append_printf(str,
5236 g_key_file_set_string(keyfile, identifier,
5239 g_string_free(str, TRUE);
5244 case CONNMAN_SERVICE_TYPE_WIMAX:
5245 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5246 case CONNMAN_SERVICE_TYPE_CELLULAR:
5247 g_key_file_set_boolean(keyfile, service->identifier,
5248 "Favorite", service->favorite);
5250 if (service->favorite == TRUE)
5251 g_key_file_set_boolean(keyfile, service->identifier,
5252 "AutoConnect", service->autoconnect);
5254 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5255 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5256 const char *failure = error2string(service->error);
5257 if (failure != NULL)
5258 g_key_file_set_string(keyfile,
5259 service->identifier,
5260 "Failure", failure);
5262 g_key_file_remove_key(keyfile, service->identifier,
5268 str = g_time_val_to_iso8601(&service->modified);
5270 g_key_file_set_string(keyfile, service->identifier,
5275 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5276 g_key_file_set_string(keyfile, service->identifier,
5277 "Passphrase", service->passphrase);
5279 g_key_file_remove_key(keyfile, service->identifier,
5280 "Passphrase", NULL);
5282 if (service->ipconfig_ipv4 != NULL)
5283 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5284 service->identifier, "IPv4.");
5286 if (service->ipconfig_ipv6 != NULL)
5287 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5288 service->identifier, "IPv6.");
5290 if (service->nameservers_config != NULL) {
5291 guint len = g_strv_length(service->nameservers_config);
5293 g_key_file_set_string_list(keyfile, service->identifier,
5295 (const gchar **) service->nameservers_config, len);
5297 g_key_file_remove_key(keyfile, service->identifier,
5298 "Nameservers", NULL);
5300 if (service->domains != NULL) {
5301 guint len = g_strv_length(service->domains);
5303 g_key_file_set_string_list(keyfile, service->identifier,
5305 (const gchar **) service->domains, len);
5307 g_key_file_remove_key(keyfile, service->identifier,
5310 cst_str = proxymethod2string(service->proxy_config);
5311 if (cst_str != NULL)
5312 g_key_file_set_string(keyfile, service->identifier,
5313 "Proxy.Method", cst_str);
5315 if (service->proxies != NULL) {
5316 guint len = g_strv_length(service->proxies);
5318 g_key_file_set_string_list(keyfile, service->identifier,
5320 (const gchar **) service->proxies, len);
5322 g_key_file_remove_key(keyfile, service->identifier,
5323 "Proxy.Servers", NULL);
5325 if (service->excludes != NULL) {
5326 guint len = g_strv_length(service->excludes);
5328 g_key_file_set_string_list(keyfile, service->identifier,
5330 (const gchar **) service->excludes, len);
5332 g_key_file_remove_key(keyfile, service->identifier,
5333 "Proxy.Excludes", NULL);
5335 if (service->pac != NULL && strlen(service->pac) > 0)
5336 g_key_file_set_string(keyfile, service->identifier,
5337 "Proxy.URL", service->pac);
5339 g_key_file_remove_key(keyfile, service->identifier,
5342 data = g_key_file_to_data(keyfile, &length, NULL);
5344 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5345 connman_error("Failed to store service information");
5350 g_key_file_free(keyfile);
5357 static struct connman_storage service_storage = {
5359 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5360 .service_load = service_load,
5361 .service_save = service_save,
5364 int __connman_service_init(void)
5368 connection = connman_dbus_get_connection();
5370 if (connman_storage_register(&service_storage) < 0)
5371 connman_error("Failed to register service storage");
5373 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5376 service_list = g_sequence_new(service_free);
5381 void __connman_service_cleanup(void)
5385 g_sequence_free(service_list);
5386 service_list = NULL;
5388 g_hash_table_destroy(service_hash);
5389 service_hash = NULL;
5391 g_slist_free(counter_list);
5392 counter_list = NULL;
5394 connman_storage_unregister(&service_storage);
5396 dbus_connection_unref(connection);