5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #define CONNECT_TIMEOUT 120
34 static DBusConnection *connection = NULL;
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
38 static GSList *counter_list = NULL;
40 struct connman_stats {
42 connman_bool_t enabled;
43 struct connman_stats_data data_last;
44 struct connman_stats_data data;
48 struct connman_stats_counter {
49 connman_bool_t append_all;
50 struct connman_stats stats;
51 struct connman_stats stats_roaming;
54 struct connman_service {
58 enum connman_service_type type;
59 enum connman_service_mode mode;
60 enum connman_service_security security;
61 enum connman_service_state state_ipv4;
62 enum connman_service_state state_ipv6;
63 enum connman_service_error error;
64 connman_uint8_t strength;
65 connman_bool_t favorite;
66 connman_bool_t immutable;
67 connman_bool_t hidden;
68 connman_bool_t ignore;
69 connman_bool_t autoconnect;
70 connman_bool_t userconnect;
76 connman_bool_t roaming;
77 connman_bool_t login_required;
78 struct connman_ipconfig *ipconfig_ipv4;
79 struct connman_ipconfig *ipconfig_ipv6;
80 struct connman_network *network;
81 struct connman_provider *provider;
83 char **nameservers_config;
87 /* 802.1x settings from the config files */
91 char *client_cert_file;
92 char *private_key_file;
93 char *private_key_passphrase;
97 struct connman_location *location;
98 struct connman_stats stats;
99 struct connman_stats stats_roaming;
100 GHashTable *counter_table;
101 enum connman_service_proxy_method proxy;
102 enum connman_service_proxy_method proxy_config;
109 static void append_path(gpointer value, gpointer user_data)
111 struct connman_service *service = value;
112 DBusMessageIter *iter = user_data;
114 if (service->path == NULL || service->hidden == TRUE)
117 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
121 void __connman_service_list(DBusMessageIter *iter, void *user_data)
123 g_sequence_foreach(service_list, append_path, iter);
128 struct connman_service *service;
131 static void compare_path(gpointer value, gpointer user_data)
133 struct connman_service *service = value;
134 struct find_data *data = user_data;
136 if (data->service != NULL)
139 if (g_strcmp0(service->path, data->path) == 0)
140 data->service = service;
143 static struct connman_service *find_service(const char *path)
145 struct find_data data = { .path = path, .service = NULL };
147 DBG("path %s", path);
149 g_sequence_foreach(service_list, compare_path, &data);
154 const char *__connman_service_type2string(enum connman_service_type type)
157 case CONNMAN_SERVICE_TYPE_UNKNOWN:
159 case CONNMAN_SERVICE_TYPE_SYSTEM:
161 case CONNMAN_SERVICE_TYPE_ETHERNET:
163 case CONNMAN_SERVICE_TYPE_WIFI:
165 case CONNMAN_SERVICE_TYPE_WIMAX:
167 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
169 case CONNMAN_SERVICE_TYPE_CELLULAR:
171 case CONNMAN_SERVICE_TYPE_GPS:
173 case CONNMAN_SERVICE_TYPE_VPN:
175 case CONNMAN_SERVICE_TYPE_GADGET:
182 static const char *mode2string(enum connman_service_mode mode)
185 case CONNMAN_SERVICE_MODE_UNKNOWN:
187 case CONNMAN_SERVICE_MODE_MANAGED:
189 case CONNMAN_SERVICE_MODE_ADHOC:
191 case CONNMAN_SERVICE_MODE_GPRS:
193 case CONNMAN_SERVICE_MODE_EDGE:
195 case CONNMAN_SERVICE_MODE_UMTS:
202 static const char *security2string(enum connman_service_security security)
205 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
207 case CONNMAN_SERVICE_SECURITY_NONE:
209 case CONNMAN_SERVICE_SECURITY_WEP:
211 case CONNMAN_SERVICE_SECURITY_PSK:
213 case CONNMAN_SERVICE_SECURITY_8021X:
215 case CONNMAN_SERVICE_SECURITY_WPA:
217 case CONNMAN_SERVICE_SECURITY_RSN:
224 static const char *state2string(enum connman_service_state state)
227 case CONNMAN_SERVICE_STATE_UNKNOWN:
229 case CONNMAN_SERVICE_STATE_IDLE:
231 case CONNMAN_SERVICE_STATE_ASSOCIATION:
232 return "association";
233 case CONNMAN_SERVICE_STATE_CONFIGURATION:
234 return "configuration";
235 case CONNMAN_SERVICE_STATE_READY:
237 case CONNMAN_SERVICE_STATE_ONLINE:
239 case CONNMAN_SERVICE_STATE_DISCONNECT:
241 case CONNMAN_SERVICE_STATE_FAILURE:
248 static const char *error2string(enum connman_service_error error)
251 case CONNMAN_SERVICE_ERROR_UNKNOWN:
253 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
254 return "out-of-range";
255 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
256 return "pin-missing";
257 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
258 return "dhcp-failed";
259 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
260 return "connect-failed";
261 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
262 return "login-failed";
263 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
264 return "auth-failed";
265 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
266 return "invalid-key";
272 static enum connman_service_error string2error(const char *error)
274 if (g_strcmp0(error, "dhcp-failed") == 0)
275 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
276 else if (g_strcmp0(error, "pin-missing") == 0)
277 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
278 else if (g_strcmp0(error, "invalid-key") == 0)
279 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
281 return CONNMAN_SERVICE_ERROR_UNKNOWN;
284 static const char *proxymethod2string(enum connman_service_proxy_method method)
287 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
289 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
291 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
293 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
300 static enum connman_service_proxy_method string2proxymethod(const char *method)
302 if (g_strcmp0(method, "direct") == 0)
303 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
304 else if (g_strcmp0(method, "auto") == 0)
305 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
306 else if (g_strcmp0(method, "manual") == 0)
307 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
309 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
312 static enum connman_service_state combine_state(
313 enum connman_service_state state_a,
314 enum connman_service_state state_b)
316 enum connman_service_state result;
318 if (state_a == state_b) {
323 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
328 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
333 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
338 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
343 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
344 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
345 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
346 state_b == CONNMAN_SERVICE_STATE_READY)
353 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
354 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
355 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
356 state_a == CONNMAN_SERVICE_STATE_READY)
363 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
364 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
365 state_b == CONNMAN_SERVICE_STATE_READY)
372 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
373 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
374 state_a == CONNMAN_SERVICE_STATE_READY)
381 if (state_a == CONNMAN_SERVICE_STATE_READY) {
382 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
389 if (state_b == CONNMAN_SERVICE_STATE_READY) {
390 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
397 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
402 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
407 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
412 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
417 result = CONNMAN_SERVICE_STATE_FAILURE;
423 static connman_bool_t is_connecting_state(struct connman_service *service,
424 enum connman_service_state state)
427 case CONNMAN_SERVICE_STATE_UNKNOWN:
428 case CONNMAN_SERVICE_STATE_IDLE:
429 case CONNMAN_SERVICE_STATE_FAILURE:
430 if (service->network != NULL)
431 return connman_network_get_connecting(service->network);
432 case CONNMAN_SERVICE_STATE_DISCONNECT:
433 case CONNMAN_SERVICE_STATE_READY:
434 case CONNMAN_SERVICE_STATE_ONLINE:
436 case CONNMAN_SERVICE_STATE_ASSOCIATION:
437 case CONNMAN_SERVICE_STATE_CONFIGURATION:
444 static connman_bool_t is_connected_state(const struct connman_service *service,
445 enum connman_service_state state)
448 case CONNMAN_SERVICE_STATE_UNKNOWN:
449 case CONNMAN_SERVICE_STATE_IDLE:
450 case CONNMAN_SERVICE_STATE_ASSOCIATION:
451 case CONNMAN_SERVICE_STATE_CONFIGURATION:
452 case CONNMAN_SERVICE_STATE_DISCONNECT:
453 case CONNMAN_SERVICE_STATE_FAILURE:
455 case CONNMAN_SERVICE_STATE_READY:
456 case CONNMAN_SERVICE_STATE_ONLINE:
463 static connman_bool_t is_connecting(struct connman_service *service)
465 enum connman_service_state state;
467 state = combine_state(service->state_ipv4, service->state_ipv6);
469 return is_connecting_state(service, state);
472 static connman_bool_t is_connected(struct connman_service *service)
474 enum connman_service_state state;
476 state = combine_state(service->state_ipv4, service->state_ipv6);
478 return is_connected_state(service, state);
481 static void update_nameservers(struct connman_service *service)
485 if (service->ipconfig_ipv4)
486 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
487 else if (service->ipconfig_ipv6)
488 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
495 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
496 case CONNMAN_SERVICE_STATE_UNKNOWN:
497 case CONNMAN_SERVICE_STATE_IDLE:
498 case CONNMAN_SERVICE_STATE_ASSOCIATION:
499 case CONNMAN_SERVICE_STATE_CONFIGURATION:
501 case CONNMAN_SERVICE_STATE_FAILURE:
502 case CONNMAN_SERVICE_STATE_DISCONNECT:
503 connman_resolver_remove_all(ifname);
505 case CONNMAN_SERVICE_STATE_READY:
506 case CONNMAN_SERVICE_STATE_ONLINE:
510 connman_resolver_remove_all(ifname);
512 if (service->nameservers_config != NULL) {
515 for (i = 0; service->nameservers_config[i] != NULL; i++) {
516 connman_resolver_append(ifname, NULL,
517 service->nameservers_config[i]);
519 } else if (service->nameservers != NULL) {
522 for (i = 0; service->nameservers[i] != NULL; i++) {
523 connman_resolver_append(ifname, NULL,
524 service->nameservers[i]);
528 if (service->domains != NULL) {
531 for (i = 0; service->domains[i]; i++)
532 connman_resolver_append(ifname, service->domains[i],
534 } else if (service->domainname != NULL)
535 connman_resolver_append(ifname, service->domainname, NULL);
537 connman_resolver_flush();
540 int __connman_service_nameserver_append(struct connman_service *service,
541 const char *nameserver)
545 DBG("service %p nameserver %s", service, nameserver);
547 if (nameserver == NULL)
550 if (service->nameservers != NULL) {
553 for (i = 0; service->nameservers[i] != NULL; i++)
554 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
557 len = g_strv_length(service->nameservers);
558 service->nameservers = g_try_renew(char *, service->nameservers,
562 service->nameservers = g_try_new0(char *, len + 2);
565 if (service->nameservers == NULL)
568 service->nameservers[len] = g_strdup(nameserver);
569 service->nameservers[len + 1] = NULL;
571 update_nameservers(service);
576 int __connman_service_nameserver_remove(struct connman_service *service,
577 const char *nameserver)
582 DBG("service %p nameserver %s", service, nameserver);
584 if (nameserver == NULL)
587 if (service->nameservers == NULL)
590 len = g_strv_length(service->nameservers);
592 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
595 g_strfreev(service->nameservers);
596 service->nameservers = NULL;
601 servers = g_try_new0(char *, len - 1);
605 for (i = 0, j = 0; i < len; i++) {
606 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
607 servers[j] = g_strdup(service->nameservers[i]);
611 servers[len - 2] = NULL;
613 g_strfreev(service->nameservers);
614 service->nameservers = servers;
616 update_nameservers(service);
621 void __connman_service_nameserver_clear(struct connman_service *service)
623 g_strfreev(service->nameservers);
624 service->nameservers = NULL;
626 update_nameservers(service);
629 static void nameserver_add_routes(int index, char **nameservers,
634 for (i = 0; nameservers[i] != NULL; i++) {
635 if (connman_inet_compare_subnet(index, nameservers[i]))
638 connman_inet_add_host_route(index, nameservers[i], gw);
642 static void nameserver_del_routes(int index, char **nameservers)
646 for (i = 0; nameservers[i] != NULL; i++)
647 connman_inet_del_host_route(index, nameservers[i]);
650 void __connman_service_nameserver_add_routes(struct connman_service *service,
658 if (service->network != NULL)
659 index = connman_network_get_index(service->network);
660 else if (service->provider != NULL)
661 index = connman_provider_get_index(service->provider);
663 if (service->nameservers_config != NULL) {
665 * Configured nameserver takes preference over the
666 * discoverd nameserver gathered from DHCP, VPN, etc.
668 nameserver_add_routes(index, service->nameservers_config, gw);
669 } else if (service->nameservers != NULL) {
671 * We add nameservers host routes for nameservers that
672 * are not on our subnet. For those who are, the subnet
673 * route will be installed by the time the dns proxy code
674 * tries to reach them. The subnet route is installed
675 * when setting the interface IP address.
677 nameserver_add_routes(index, service->nameservers, gw);
681 void __connman_service_nameserver_del_routes(struct connman_service *service)
688 if (service->network != NULL)
689 index = connman_network_get_index(service->network);
690 else if (service->provider != NULL)
691 index = connman_provider_get_index(service->provider);
693 if (service->nameservers_config != NULL)
694 nameserver_del_routes(index, service->nameservers_config);
695 else if (service->nameservers != NULL)
696 nameserver_del_routes(index, service->nameservers);
699 static struct connman_stats *stats_get(struct connman_service *service)
701 if (service->roaming == TRUE)
702 return &service->stats_roaming;
704 return &service->stats;
707 static connman_bool_t stats_enabled(struct connman_service *service)
709 struct connman_stats *stats = stats_get(service);
711 return stats->enabled;
714 static void stats_start(struct connman_service *service)
716 struct connman_stats *stats = stats_get(service);
718 DBG("service %p", service);
720 if (stats->timer == NULL)
723 stats->enabled = TRUE;
724 stats->data_last.time = stats->data.time;
726 g_timer_start(stats->timer);
729 static void stats_stop(struct connman_service *service)
731 struct connman_stats *stats = stats_get(service);
732 unsigned int seconds;
734 DBG("service %p", service);
736 if (stats->timer == NULL)
739 if (stats->enabled == FALSE)
742 g_timer_stop(stats->timer);
744 seconds = g_timer_elapsed(stats->timer, NULL);
745 stats->data.time = stats->data_last.time + seconds;
747 stats->enabled = FALSE;
750 static void reset_stats(struct connman_service *service)
752 DBG("service %p", service);
755 service->stats.valid = FALSE;
757 service->stats.data.rx_packets = 0;
758 service->stats.data.tx_packets = 0;
759 service->stats.data.rx_bytes = 0;
760 service->stats.data.tx_bytes = 0;
761 service->stats.data.rx_errors = 0;
762 service->stats.data.tx_errors = 0;
763 service->stats.data.rx_dropped = 0;
764 service->stats.data.tx_dropped = 0;
765 service->stats.data.time = 0;
766 service->stats.data_last.time = 0;
768 g_timer_reset(service->stats.timer);
771 service->stats_roaming.valid = FALSE;
773 service->stats_roaming.data.rx_packets = 0;
774 service->stats_roaming.data.tx_packets = 0;
775 service->stats_roaming.data.rx_bytes = 0;
776 service->stats_roaming.data.tx_bytes = 0;
777 service->stats_roaming.data.rx_errors = 0;
778 service->stats_roaming.data.tx_errors = 0;
779 service->stats_roaming.data.rx_dropped = 0;
780 service->stats_roaming.data.tx_dropped = 0;
781 service->stats_roaming.data.time = 0;
782 service->stats_roaming.data_last.time = 0;
784 g_timer_reset(service->stats_roaming.timer);
787 static struct connman_service *get_default(void)
789 struct connman_service *service;
792 iter = g_sequence_get_begin_iter(service_list);
794 if (g_sequence_iter_is_end(iter) == TRUE)
797 service = g_sequence_get(iter);
799 if (is_connected(service) == FALSE)
805 static void default_changed(void)
807 struct connman_service *service = get_default();
809 __connman_notifier_default_changed(service);
812 const char *__connman_service_default(void)
814 struct connman_service *service;
816 service = get_default();
820 return __connman_service_type2string(service->type);
823 static void mode_changed(struct connman_service *service)
827 str = mode2string(service->mode);
831 connman_dbus_property_changed_basic(service->path,
832 CONNMAN_SERVICE_INTERFACE, "Mode",
833 DBUS_TYPE_STRING, &str);
836 static void state_changed(struct connman_service *service)
840 str = state2string(combine_state(service->state_ipv4,
841 service->state_ipv6));
845 connman_dbus_property_changed_basic(service->path,
846 CONNMAN_SERVICE_INTERFACE, "State",
847 DBUS_TYPE_STRING, &str);
850 static void strength_changed(struct connman_service *service)
852 if (service->strength == 0)
855 connman_dbus_property_changed_basic(service->path,
856 CONNMAN_SERVICE_INTERFACE, "Strength",
857 DBUS_TYPE_BYTE, &service->strength);
860 static void favorite_changed(struct connman_service *service)
862 if (service->path == NULL)
865 connman_dbus_property_changed_basic(service->path,
866 CONNMAN_SERVICE_INTERFACE, "Favorite",
867 DBUS_TYPE_BOOLEAN, &service->favorite);
870 static void immutable_changed(struct connman_service *service)
872 if (service->path == NULL)
875 connman_dbus_property_changed_basic(service->path,
876 CONNMAN_SERVICE_INTERFACE, "Immutable",
877 DBUS_TYPE_BOOLEAN, &service->immutable);
880 static void roaming_changed(struct connman_service *service)
882 if (service->path == NULL)
885 connman_dbus_property_changed_basic(service->path,
886 CONNMAN_SERVICE_INTERFACE, "Roaming",
887 DBUS_TYPE_BOOLEAN, &service->roaming);
890 static void autoconnect_changed(struct connman_service *service)
892 if (service->path == NULL)
895 connman_dbus_property_changed_basic(service->path,
896 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
897 DBUS_TYPE_BOOLEAN, &service->autoconnect);
900 static void passphrase_changed(struct connman_service *service)
902 dbus_bool_t required;
904 switch (service->type) {
905 case CONNMAN_SERVICE_TYPE_UNKNOWN:
906 case CONNMAN_SERVICE_TYPE_SYSTEM:
907 case CONNMAN_SERVICE_TYPE_ETHERNET:
908 case CONNMAN_SERVICE_TYPE_WIMAX:
909 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
910 case CONNMAN_SERVICE_TYPE_CELLULAR:
911 case CONNMAN_SERVICE_TYPE_GPS:
912 case CONNMAN_SERVICE_TYPE_VPN:
913 case CONNMAN_SERVICE_TYPE_GADGET:
915 case CONNMAN_SERVICE_TYPE_WIFI:
918 switch (service->security) {
919 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
920 case CONNMAN_SERVICE_SECURITY_NONE:
922 case CONNMAN_SERVICE_SECURITY_WEP:
923 case CONNMAN_SERVICE_SECURITY_PSK:
924 case CONNMAN_SERVICE_SECURITY_WPA:
925 case CONNMAN_SERVICE_SECURITY_RSN:
926 if (service->passphrase == NULL)
929 case CONNMAN_SERVICE_SECURITY_8021X:
935 connman_dbus_property_changed_basic(service->path,
936 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
937 DBUS_TYPE_BOOLEAN, &required);
940 static void login_changed(struct connman_service *service)
942 dbus_bool_t required = service->login_required;
944 if (service->path == NULL)
947 connman_dbus_property_changed_basic(service->path,
948 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
949 DBUS_TYPE_BOOLEAN, &required);
952 static void append_security(DBusMessageIter *iter, void *user_data)
954 struct connman_service *service = user_data;
957 str = security2string(service->security);
959 dbus_message_iter_append_basic(iter,
960 DBUS_TYPE_STRING, &str);
963 if (service->wps == TRUE)
964 dbus_message_iter_append_basic(iter,
965 DBUS_TYPE_STRING, &str);
968 static void append_ethernet(DBusMessageIter *iter, void *user_data)
970 struct connman_service *service = user_data;
972 if (service->ipconfig_ipv4 != NULL)
973 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
975 else if (service->ipconfig_ipv6 != NULL)
976 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
980 static void append_ipv4(DBusMessageIter *iter, void *user_data)
982 struct connman_service *service = user_data;
984 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
985 state2string(service->state_ipv4));
987 if (is_connected_state(service, service->state_ipv4) == FALSE)
990 if (service->ipconfig_ipv4 != NULL)
991 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
994 static void append_ipv6(DBusMessageIter *iter, void *user_data)
996 struct connman_service *service = user_data;
998 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
999 state2string(service->state_ipv6));
1001 if (is_connected_state(service, service->state_ipv6) == FALSE)
1004 if (service->ipconfig_ipv6 != NULL)
1005 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1006 service->ipconfig_ipv4);
1009 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1011 struct connman_service *service = user_data;
1013 if (service->ipconfig_ipv4 != NULL)
1014 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1018 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1020 struct connman_service *service = user_data;
1022 if (service->ipconfig_ipv6 != NULL)
1023 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1027 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1032 servers = *nameservers;
1034 for (i = 0; servers[i] != NULL; i++) {
1035 dbus_message_iter_append_basic(iter,
1036 DBUS_TYPE_STRING, &servers[i]);
1040 static void append_dns(DBusMessageIter *iter, void *user_data)
1042 struct connman_service *service = user_data;
1044 if (is_connected(service) == FALSE)
1047 if (service->nameservers_config != NULL) {
1048 append_nameserver(iter, &service->nameservers_config);
1050 } else if (service->nameservers != NULL) {
1051 append_nameserver(iter, &service->nameservers);
1056 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1058 struct connman_service *service = user_data;
1061 if (service->nameservers_config == NULL)
1064 for (i = 0; service->nameservers_config[i]; i++) {
1065 dbus_message_iter_append_basic(iter,
1067 &service->nameservers_config[i]);
1071 static void append_domain(DBusMessageIter *iter, void *user_data)
1073 struct connman_service *service = user_data;
1075 if (is_connected(service) == FALSE &&
1076 is_connecting(service) == FALSE)
1079 if (service->domainname == NULL)
1082 dbus_message_iter_append_basic(iter,
1083 DBUS_TYPE_STRING, &service->domainname);
1086 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1088 struct connman_service *service = user_data;
1091 if (service->domains == NULL)
1094 for (i = 0; service->domains[i]; i++)
1095 dbus_message_iter_append_basic(iter,
1096 DBUS_TYPE_STRING, &service->domains[i]);
1099 static void append_proxies(DBusMessageIter *iter, void *user_data)
1101 struct connman_service *service = user_data;
1104 if (service->proxies == NULL)
1107 for (i = 0; service->proxies[i]; i++)
1108 dbus_message_iter_append_basic(iter,
1109 DBUS_TYPE_STRING, &service->proxies[i]);
1112 static void append_excludes(DBusMessageIter *iter, void *user_data)
1114 struct connman_service *service = user_data;
1117 if (service->excludes == NULL)
1120 for (i = 0; service->excludes[i]; i++)
1121 dbus_message_iter_append_basic(iter,
1122 DBUS_TYPE_STRING, &service->excludes[i]);
1125 static void append_proxy(DBusMessageIter *iter, void *user_data)
1127 struct connman_service *service = user_data;
1128 enum connman_service_proxy_method proxy;
1129 const char *pac = NULL;
1130 const char *method = proxymethod2string(
1131 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1135 if (is_connected(service) == FALSE)
1138 proxy = connman_service_get_proxy_method(service);
1141 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1143 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1145 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1146 connman_dbus_dict_append_array(iter, "Servers",
1147 DBUS_TYPE_STRING, append_proxies,
1150 connman_dbus_dict_append_array(iter, "Excludes",
1151 DBUS_TYPE_STRING, append_excludes,
1154 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1155 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1156 if (service->ipconfig_ipv4 != NULL)
1157 pac = __connman_ipconfig_get_proxy_autoconfig(
1158 service->ipconfig_ipv4);
1159 else if (service->ipconfig_ipv6 != NULL)
1160 pac = __connman_ipconfig_get_proxy_autoconfig(
1161 service->ipconfig_ipv6);
1163 if (service->pac == NULL && pac == NULL)
1166 if (service->pac != NULL)
1169 connman_dbus_dict_append_basic(iter, "URL",
1170 DBUS_TYPE_STRING, &pac);
1174 method = proxymethod2string(proxy);
1177 connman_dbus_dict_append_basic(iter, "Method",
1178 DBUS_TYPE_STRING, &method);
1181 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1183 struct connman_service *service = user_data;
1186 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1189 switch (service->proxy_config) {
1190 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1192 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1194 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1195 if (service->proxies != NULL)
1196 connman_dbus_dict_append_array(iter, "Servers",
1198 append_proxies, service);
1200 if (service->excludes != NULL)
1201 connman_dbus_dict_append_array(iter, "Excludes",
1203 append_excludes, service);
1205 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1206 if (service->pac != NULL)
1207 connman_dbus_dict_append_basic(iter, "URL",
1208 DBUS_TYPE_STRING, &service->pac);
1212 method = proxymethod2string(service->proxy_config);
1214 connman_dbus_dict_append_basic(iter, "Method",
1215 DBUS_TYPE_STRING, &method);
1218 static void append_provider(DBusMessageIter *iter, void *user_data)
1220 struct connman_service *service = user_data;
1222 DBG("%p %p", service, service->provider);
1224 if (is_connected(service) == FALSE)
1227 if (service->provider != NULL)
1228 __connman_provider_append_properties(service->provider, iter);
1232 static void settings_changed(struct connman_service *service)
1234 connman_dbus_property_changed_dict(service->path,
1235 CONNMAN_SERVICE_INTERFACE, "IPv4",
1236 append_ipv4, service);
1238 connman_dbus_property_changed_dict(service->path,
1239 CONNMAN_SERVICE_INTERFACE, "IPv6",
1240 append_ipv6, service);
1243 static void ipv4_configuration_changed(struct connman_service *service)
1245 connman_dbus_property_changed_dict(service->path,
1246 CONNMAN_SERVICE_INTERFACE,
1247 "IPv4.Configuration",
1252 static void ipv6_configuration_changed(struct connman_service *service)
1254 connman_dbus_property_changed_dict(service->path,
1255 CONNMAN_SERVICE_INTERFACE,
1256 "IPv6.Configuration",
1261 static void dns_changed(struct connman_service *service)
1263 connman_dbus_property_changed_array(service->path,
1264 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1265 DBUS_TYPE_STRING, append_dns, service);
1268 static void dns_configuration_changed(struct connman_service *service)
1270 connman_dbus_property_changed_array(service->path,
1271 CONNMAN_SERVICE_INTERFACE,
1272 "Nameservers.Configuration",
1273 DBUS_TYPE_STRING, append_dnsconfig, service);
1275 dns_changed(service);
1278 static void domain_changed(struct connman_service *service)
1280 connman_dbus_property_changed_array(service->path,
1281 CONNMAN_SERVICE_INTERFACE, "Domains",
1282 DBUS_TYPE_STRING, append_domain, service);
1285 static void domain_configuration_changed(struct connman_service *service)
1287 connman_dbus_property_changed_array(service->path,
1288 CONNMAN_SERVICE_INTERFACE,
1289 "Domains.Configuration",
1290 DBUS_TYPE_STRING, append_domainconfig, service);
1293 static void proxy_changed(struct connman_service *service)
1295 connman_dbus_property_changed_dict(service->path,
1296 CONNMAN_SERVICE_INTERFACE, "Proxy",
1297 append_proxy, service);
1300 static void proxy_configuration_changed(struct connman_service *service)
1302 connman_dbus_property_changed_dict(service->path,
1303 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1304 append_proxyconfig, service);
1306 proxy_changed(service);
1309 static void link_changed(struct connman_service *service)
1311 connman_dbus_property_changed_dict(service->path,
1312 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1313 append_ethernet, service);
1316 static void stats_append_counters(DBusMessageIter *dict,
1317 struct connman_stats_data *stats,
1318 struct connman_stats_data *counters,
1319 connman_bool_t append_all)
1321 if (counters->rx_packets != stats->rx_packets || append_all) {
1322 counters->rx_packets = stats->rx_packets;
1323 connman_dbus_dict_append_basic(dict, "RX.Packets",
1324 DBUS_TYPE_UINT32, &stats->rx_packets);
1327 if (counters->tx_packets != stats->tx_packets || append_all) {
1328 counters->tx_packets = stats->tx_packets;
1329 connman_dbus_dict_append_basic(dict, "TX.Packets",
1330 DBUS_TYPE_UINT32, &stats->tx_packets);
1333 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1334 counters->rx_bytes = stats->rx_bytes;
1335 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1336 DBUS_TYPE_UINT32, &stats->rx_bytes);
1339 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1340 counters->tx_bytes = stats->tx_bytes;
1341 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1342 DBUS_TYPE_UINT32, &stats->tx_bytes);
1345 if (counters->rx_errors != stats->rx_errors || append_all) {
1346 counters->rx_errors = stats->rx_errors;
1347 connman_dbus_dict_append_basic(dict, "RX.Errors",
1348 DBUS_TYPE_UINT32, &stats->rx_errors);
1351 if (counters->tx_errors != stats->tx_errors || append_all) {
1352 counters->tx_errors = stats->tx_errors;
1353 connman_dbus_dict_append_basic(dict, "TX.Errors",
1354 DBUS_TYPE_UINT32, &stats->tx_errors);
1357 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1358 counters->rx_dropped = stats->rx_dropped;
1359 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1360 DBUS_TYPE_UINT32, &stats->rx_dropped);
1363 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1364 counters->tx_dropped = stats->tx_dropped;
1365 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1366 DBUS_TYPE_UINT32, &stats->tx_dropped);
1369 if (counters->time != stats->time || append_all) {
1370 counters->time = stats->time;
1371 connman_dbus_dict_append_basic(dict, "Time",
1372 DBUS_TYPE_UINT32, &stats->time);
1376 static void stats_append(struct connman_service *service,
1377 const char *counter,
1378 struct connman_stats_counter *counters,
1379 connman_bool_t append_all)
1381 DBusMessageIter array, dict;
1384 DBG("service %p counter %s", service, counter);
1386 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1390 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1391 &service->path, DBUS_TYPE_INVALID);
1393 dbus_message_iter_init_append(msg, &array);
1396 connman_dbus_dict_open(&array, &dict);
1398 stats_append_counters(&dict, &service->stats.data,
1399 &counters->stats.data, append_all);
1401 connman_dbus_dict_close(&array, &dict);
1403 /* roaming counter */
1404 connman_dbus_dict_open(&array, &dict);
1406 stats_append_counters(&dict, &service->stats_roaming.data,
1407 &counters->stats_roaming.data, append_all);
1409 connman_dbus_dict_close(&array, &dict);
1411 __connman_counter_send_usage(counter, msg);
1414 static void stats_update(struct connman_service *service,
1415 unsigned int rx_packets, unsigned int tx_packets,
1416 unsigned int rx_bytes, unsigned int tx_bytes,
1417 unsigned int rx_errors, unsigned int tx_errors,
1418 unsigned int rx_dropped, unsigned int tx_dropped)
1420 struct connman_stats *stats = stats_get(service);
1421 struct connman_stats_data *data_last = &stats->data_last;
1422 struct connman_stats_data *data = &stats->data;
1423 unsigned int seconds;
1425 DBG("service %p", service);
1427 if (stats->valid == TRUE) {
1429 rx_packets - data_last->rx_packets;
1431 tx_packets - data_last->tx_packets;
1433 rx_bytes - data_last->rx_bytes;
1435 tx_bytes - data_last->tx_bytes;
1437 rx_errors - data_last->rx_errors;
1439 tx_errors - data_last->tx_errors;
1441 rx_dropped - data_last->rx_dropped;
1443 tx_dropped - data_last->tx_dropped;
1445 stats->valid = TRUE;
1448 data_last->rx_packets = rx_packets;
1449 data_last->tx_packets = tx_packets;
1450 data_last->rx_bytes = rx_bytes;
1451 data_last->tx_bytes = tx_bytes;
1452 data_last->rx_errors = rx_errors;
1453 data_last->tx_errors = tx_errors;
1454 data_last->rx_dropped = rx_dropped;
1455 data_last->tx_dropped = tx_dropped;
1457 seconds = g_timer_elapsed(stats->timer, NULL);
1458 stats->data.time = stats->data_last.time + seconds;
1461 void __connman_service_notify(struct connman_service *service,
1462 unsigned int rx_packets, unsigned int tx_packets,
1463 unsigned int rx_bytes, unsigned int tx_bytes,
1464 unsigned int rx_errors, unsigned int tx_errors,
1465 unsigned int rx_dropped, unsigned int tx_dropped)
1467 GHashTableIter iter;
1468 gpointer key, value;
1469 const char *counter;
1470 struct connman_stats_counter *counters;
1471 struct connman_stats_data *data;
1474 if (service == NULL)
1477 if (is_connected(service) == FALSE)
1480 stats_update(service,
1481 rx_packets, tx_packets,
1483 rx_errors, tx_errors,
1484 rx_dropped, tx_dropped);
1486 data = &stats_get(service)->data;
1487 err = __connman_stats_update(service, service->roaming, data);
1489 connman_error("Failed to store statistics for %s",
1490 service->identifier);
1492 g_hash_table_iter_init(&iter, service->counter_table);
1493 while (g_hash_table_iter_next(&iter, &key, &value)) {
1497 stats_append(service, counter, counters, counters->append_all);
1498 counters->append_all = FALSE;
1502 int __connman_service_counter_register(const char *counter)
1504 struct connman_service *service;
1505 GSequenceIter *iter;
1506 struct connman_stats_counter *counters;
1508 DBG("counter %s", counter);
1510 counter_list = g_slist_append(counter_list, (gpointer)counter);
1512 iter = g_sequence_get_begin_iter(service_list);
1514 while (g_sequence_iter_is_end(iter) == FALSE) {
1515 service = g_sequence_get(iter);
1517 counters = g_try_new0(struct connman_stats_counter, 1);
1518 if (counters == NULL)
1521 counters->append_all = TRUE;
1523 g_hash_table_replace(service->counter_table, (gpointer)counter,
1526 iter = g_sequence_iter_next(iter);
1532 void __connman_service_counter_unregister(const char *counter)
1534 struct connman_service *service;
1535 GSequenceIter *iter;
1537 DBG("counter %s", counter);
1539 iter = g_sequence_get_begin_iter(service_list);
1541 while (g_sequence_iter_is_end(iter) == FALSE) {
1542 service = g_sequence_get(iter);
1544 g_hash_table_remove(service->counter_table, counter);
1546 iter = g_sequence_iter_next(iter);
1549 counter_list = g_slist_remove(counter_list, counter);
1552 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1553 struct connman_service *service)
1555 dbus_bool_t required;
1558 str = __connman_service_type2string(service->type);
1560 connman_dbus_dict_append_basic(dict, "Type",
1561 DBUS_TYPE_STRING, &str);
1563 str = mode2string(service->mode);
1565 connman_dbus_dict_append_basic(dict, "Mode",
1566 DBUS_TYPE_STRING, &str);
1568 connman_dbus_dict_append_array(dict, "Security",
1569 DBUS_TYPE_STRING, append_security, service);
1571 str = state2string(combine_state(service->state_ipv4,
1572 service->state_ipv6));
1574 connman_dbus_dict_append_basic(dict, "State",
1575 DBUS_TYPE_STRING, &str);
1577 str = error2string(service->error);
1579 connman_dbus_dict_append_basic(dict, "Error",
1580 DBUS_TYPE_STRING, &str);
1582 if (service->strength > 0)
1583 connman_dbus_dict_append_basic(dict, "Strength",
1584 DBUS_TYPE_BYTE, &service->strength);
1586 connman_dbus_dict_append_basic(dict, "Favorite",
1587 DBUS_TYPE_BOOLEAN, &service->favorite);
1589 connman_dbus_dict_append_basic(dict, "Immutable",
1590 DBUS_TYPE_BOOLEAN, &service->immutable);
1592 if (service->favorite == TRUE)
1593 connman_dbus_dict_append_basic(dict, "AutoConnect",
1594 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1596 connman_dbus_dict_append_basic(dict, "AutoConnect",
1597 DBUS_TYPE_BOOLEAN, &service->favorite);
1599 if (service->name != NULL)
1600 connman_dbus_dict_append_basic(dict, "Name",
1601 DBUS_TYPE_STRING, &service->name);
1603 connman_dbus_dict_append_basic(dict, "LoginRequired",
1604 DBUS_TYPE_BOOLEAN, &service->login_required);
1606 switch (service->type) {
1607 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1608 case CONNMAN_SERVICE_TYPE_SYSTEM:
1609 case CONNMAN_SERVICE_TYPE_GPS:
1610 case CONNMAN_SERVICE_TYPE_VPN:
1611 case CONNMAN_SERVICE_TYPE_GADGET:
1613 case CONNMAN_SERVICE_TYPE_CELLULAR:
1614 connman_dbus_dict_append_basic(dict, "Roaming",
1615 DBUS_TYPE_BOOLEAN, &service->roaming);
1618 connman_dbus_dict_append_basic(dict, "SetupRequired",
1619 DBUS_TYPE_BOOLEAN, &required);
1620 connman_dbus_dict_append_dict(dict, "Ethernet",
1621 append_ethernet, service);
1623 case CONNMAN_SERVICE_TYPE_WIFI:
1624 if (service->passphrase != NULL && limited == FALSE)
1625 connman_dbus_dict_append_basic(dict, "Passphrase",
1626 DBUS_TYPE_STRING, &service->passphrase);
1630 switch (service->security) {
1631 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1632 case CONNMAN_SERVICE_SECURITY_NONE:
1634 case CONNMAN_SERVICE_SECURITY_WEP:
1635 case CONNMAN_SERVICE_SECURITY_PSK:
1636 case CONNMAN_SERVICE_SECURITY_WPA:
1637 case CONNMAN_SERVICE_SECURITY_RSN:
1638 if (service->passphrase == NULL)
1641 case CONNMAN_SERVICE_SECURITY_8021X:
1645 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1646 DBUS_TYPE_BOOLEAN, &required);
1649 case CONNMAN_SERVICE_TYPE_ETHERNET:
1650 case CONNMAN_SERVICE_TYPE_WIMAX:
1651 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1652 connman_dbus_dict_append_dict(dict, "Ethernet",
1653 append_ethernet, service);
1657 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1659 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1660 append_ipv4config, service);
1662 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1664 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1665 append_ipv6config, service);
1667 connman_dbus_dict_append_array(dict, "Nameservers",
1668 DBUS_TYPE_STRING, append_dns, service);
1670 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1671 DBUS_TYPE_STRING, append_dnsconfig, service);
1673 connman_dbus_dict_append_array(dict, "Domains",
1674 DBUS_TYPE_STRING, append_domain, service);
1676 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1677 DBUS_TYPE_STRING, append_domainconfig, service);
1679 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1681 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1682 append_proxyconfig, service);
1684 connman_dbus_dict_append_dict(dict, "Provider",
1685 append_provider, service);
1688 static void append_struct(gpointer value, gpointer user_data)
1690 struct connman_service *service = value;
1691 DBusMessageIter *iter = user_data;
1692 DBusMessageIter entry, dict;
1694 if (service->path == NULL || service->hidden == TRUE)
1697 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1699 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1702 connman_dbus_dict_open(&entry, &dict);
1703 append_properties(&dict, TRUE, service);
1704 connman_dbus_dict_close(&entry, &dict);
1706 dbus_message_iter_close_container(iter, &entry);
1709 void __connman_service_list_struct(DBusMessageIter *iter)
1711 g_sequence_foreach(service_list, append_struct, iter);
1714 int __connman_service_get_index(struct connman_service *service)
1716 if (service == NULL)
1719 if (service->network != NULL)
1720 return connman_network_get_index(service->network);
1721 else if (service->provider != NULL)
1722 return connman_provider_get_index(service->provider);
1727 void __connman_service_set_domainname(struct connman_service *service,
1728 const char *domainname)
1730 if (service == NULL)
1733 g_free(service->domainname);
1734 service->domainname = g_strdup(domainname);
1736 domain_changed(service);
1739 const char *connman_service_get_domainname(struct connman_service *service)
1741 if (service == NULL)
1744 if (service->domains != NULL)
1745 return service->domains[0];
1747 return service->domainname;
1750 char **connman_service_get_nameservers(struct connman_service *service)
1752 if (service == NULL)
1755 if (service->nameservers_config != NULL)
1756 return service->nameservers_config;
1757 else if (service->nameservers != NULL)
1758 return service->nameservers;
1763 void connman_service_set_proxy_method(struct connman_service *service,
1764 enum connman_service_proxy_method method)
1766 if (service == NULL)
1769 service->proxy = method;
1771 proxy_changed(service);
1773 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1774 __connman_notifier_proxy_changed(service);
1777 enum connman_service_proxy_method connman_service_get_proxy_method(
1778 struct connman_service *service)
1780 if (service == NULL)
1781 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1783 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1784 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1785 service->pac == NULL)
1786 return service->proxy;
1788 return service->proxy_config;
1791 return service->proxy;
1794 char **connman_service_get_proxy_servers(struct connman_service *service)
1796 return g_strdupv(service->proxies);
1799 char **connman_service_get_proxy_excludes(struct connman_service *service)
1801 return g_strdupv(service->excludes);
1804 const char *connman_service_get_proxy_url(struct connman_service *service)
1806 if (service == NULL)
1809 return service->pac;
1812 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1815 if (service == NULL)
1818 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1820 if (service->ipconfig_ipv4) {
1821 if (__connman_ipconfig_set_proxy_autoconfig(
1822 service->ipconfig_ipv4, url) < 0)
1824 } else if (service->ipconfig_ipv6) {
1825 if (__connman_ipconfig_set_proxy_autoconfig(
1826 service->ipconfig_ipv6, url) < 0)
1831 proxy_changed(service);
1833 __connman_notifier_proxy_changed(service);
1836 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1838 if (service == NULL)
1841 if (service->ipconfig_ipv4)
1842 return __connman_ipconfig_get_proxy_autoconfig(
1843 service->ipconfig_ipv4);
1844 else if (service->ipconfig_ipv6)
1845 return __connman_ipconfig_get_proxy_autoconfig(
1846 service->ipconfig_ipv6);
1850 static void update_timeservers(struct connman_service *service)
1854 if (service->timeservers == NULL)
1857 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
1858 case CONNMAN_SERVICE_STATE_UNKNOWN:
1859 case CONNMAN_SERVICE_STATE_IDLE:
1860 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1861 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1863 case CONNMAN_SERVICE_STATE_FAILURE:
1864 case CONNMAN_SERVICE_STATE_DISCONNECT:
1865 for (i = 0; service->timeservers[i] != NULL; i++)
1866 connman_timeserver_remove(service->timeservers[i]);
1868 case CONNMAN_SERVICE_STATE_READY:
1869 case CONNMAN_SERVICE_STATE_ONLINE:
1873 for (i = 0; service->timeservers[i] != NULL; i++)
1874 connman_timeserver_append(service->timeservers[i]);
1877 int __connman_service_timeserver_append(struct connman_service *service,
1878 const char *timeserver)
1882 DBG("service %p timeserver %s", service, timeserver);
1884 if (timeserver == NULL)
1887 if (service->timeservers != NULL) {
1890 for (i = 0; service->timeservers[i] != NULL; i++)
1891 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1894 len = g_strv_length(service->timeservers);
1895 service->timeservers = g_try_renew(char *, service->timeservers,
1899 service->timeservers = g_try_new0(char *, len + 2);
1902 if (service->timeservers == NULL)
1905 service->timeservers[len] = g_strdup(timeserver);
1906 service->timeservers[len + 1] = NULL;
1908 update_timeservers(service);
1913 int __connman_service_timeserver_remove(struct connman_service *service,
1914 const char *timeserver)
1919 DBG("service %p timeserver %s", service, timeserver);
1921 if (timeserver == NULL)
1924 if (service->timeservers == NULL)
1927 len = g_strv_length(service->timeservers);
1929 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
1932 g_strfreev(service->timeservers);
1933 service->timeservers = NULL;
1938 servers = g_try_new0(char *, len - 1);
1939 if (servers == NULL)
1942 for (i = 0, j = 0; i < len; i++) {
1943 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
1944 servers[j] = g_strdup(service->timeservers[i]);
1948 servers[len - 2] = NULL;
1950 g_strfreev(service->timeservers);
1951 service->timeservers = servers;
1953 update_timeservers(service);
1958 void __connman_service_set_pac(struct connman_service *service,
1964 g_free(service->pac);
1965 service->pac = g_strdup(pac);
1968 void __connman_service_set_passphrase(struct connman_service *service,
1969 const char* passphrase)
1971 if (service->immutable == TRUE)
1974 g_free(service->passphrase);
1975 service->passphrase = g_strdup(passphrase);
1977 passphrase_changed(service);
1979 if (service->network != NULL)
1980 connman_network_set_string(service->network,
1982 service->passphrase);
1984 __connman_storage_save_service(service);
1987 static DBusMessage *get_properties(DBusConnection *conn,
1988 DBusMessage *msg, void *user_data)
1990 struct connman_service *service = user_data;
1992 DBusMessageIter array, dict;
1994 DBG("service %p", service);
1996 reply = dbus_message_new_method_return(msg);
2000 dbus_message_iter_init_append(reply, &array);
2002 connman_dbus_dict_open(&array, &dict);
2003 append_properties(&dict, FALSE, service);
2004 connman_dbus_dict_close(&array, &dict);
2009 static int update_proxy_configuration(struct connman_service *service,
2010 DBusMessageIter *array)
2012 DBusMessageIter dict;
2013 enum connman_service_proxy_method method;
2014 GString *servers_str = NULL;
2015 GString *excludes_str = NULL;
2016 const char *url = NULL;
2018 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2020 dbus_message_iter_recurse(array, &dict);
2022 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2023 DBusMessageIter entry, variant;
2027 dbus_message_iter_recurse(&dict, &entry);
2029 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2032 dbus_message_iter_get_basic(&entry, &key);
2033 dbus_message_iter_next(&entry);
2035 if (dbus_message_iter_get_arg_type(&entry) !=
2039 dbus_message_iter_recurse(&entry, &variant);
2040 type = dbus_message_iter_get_arg_type(&variant);
2042 if (g_str_equal(key, "Method") == TRUE) {
2045 if (type != DBUS_TYPE_STRING)
2048 dbus_message_iter_get_basic(&variant, &val);
2049 method = string2proxymethod(val);
2050 } else if (g_str_equal(key, "URL") == TRUE) {
2051 if (type != DBUS_TYPE_STRING)
2054 dbus_message_iter_get_basic(&variant, &url);
2055 } else if (g_str_equal(key, "Servers") == TRUE) {
2056 DBusMessageIter str_array;
2058 if (type != DBUS_TYPE_ARRAY)
2061 servers_str = g_string_new(NULL);
2062 if (servers_str == NULL)
2065 dbus_message_iter_recurse(&variant, &str_array);
2067 while (dbus_message_iter_get_arg_type(&str_array) ==
2071 dbus_message_iter_get_basic(&str_array, &val);
2073 if (servers_str->len > 0)
2074 g_string_append_printf(servers_str,
2077 g_string_append(servers_str, val);
2079 dbus_message_iter_next(&str_array);
2081 } else if (g_str_equal(key, "Excludes") == TRUE) {
2082 DBusMessageIter str_array;
2084 if (type != DBUS_TYPE_ARRAY)
2087 excludes_str = g_string_new(NULL);
2088 if (excludes_str == NULL)
2091 dbus_message_iter_recurse(&variant, &str_array);
2093 while (dbus_message_iter_get_arg_type(&str_array) ==
2097 dbus_message_iter_get_basic(&str_array, &val);
2099 if (excludes_str->len > 0)
2100 g_string_append_printf(excludes_str,
2103 g_string_append(excludes_str, val);
2105 dbus_message_iter_next(&str_array);
2109 dbus_message_iter_next(&dict);
2113 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2115 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2116 if (servers_str == NULL && service->proxies == NULL)
2119 if (servers_str != NULL) {
2120 g_strfreev(service->proxies);
2122 if (servers_str->len > 0)
2123 service->proxies = g_strsplit_set(
2124 servers_str->str, " ", 0);
2126 service->proxies = NULL;
2129 if (excludes_str != NULL) {
2130 g_strfreev(service->excludes);
2132 if (excludes_str->len > 0)
2133 service->excludes = g_strsplit_set(
2134 excludes_str->str, " ", 0);
2136 service->excludes = NULL;
2139 if (service->proxies == NULL)
2140 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2143 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2144 g_free(service->pac);
2146 if (url != NULL && strlen(url) > 0)
2147 service->pac = g_strdup(url);
2149 service->pac = NULL;
2151 /* if we are connected:
2152 - if service->pac == NULL
2153 - if __connman_ipconfig_get_proxy_autoconfig(
2154 service->ipconfig) == NULL
2155 --> We should start WPAD */
2158 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2162 if (servers_str != NULL)
2163 g_string_free(servers_str, TRUE);
2165 if (excludes_str != NULL)
2166 g_string_free(excludes_str, TRUE);
2168 service->proxy_config = method;
2173 if (servers_str != NULL)
2174 g_string_free(servers_str, TRUE);
2176 if (excludes_str != NULL)
2177 g_string_free(excludes_str, TRUE);
2182 static int set_ipconfig(struct connman_service *service,
2183 struct connman_ipconfig *ipconfig,
2184 DBusMessageIter *array,
2185 enum connman_service_state state,
2186 enum connman_service_state *new_state)
2188 enum connman_ipconfig_method old_method;
2189 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2190 enum connman_ipconfig_type type;
2193 old_method = __connman_ipconfig_get_method(ipconfig);
2195 if (is_connecting_state(service, state) ||
2196 is_connected_state(service, state))
2197 __connman_network_clear_ipconfig(service->network, ipconfig);
2199 err = __connman_ipconfig_set_config(ipconfig, array);
2200 method = __connman_ipconfig_get_method(ipconfig);
2201 type = __connman_ipconfig_get_config_type(ipconfig);
2203 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2204 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2205 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2206 *new_state = service->state_ipv4 =
2207 CONNMAN_SERVICE_STATE_CONFIGURATION;
2208 __connman_ipconfig_enable(ipconfig);
2211 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2212 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2213 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2214 *new_state = service->state_ipv6;
2215 __connman_ipconfig_enable(ipconfig);
2219 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2220 type, method, state2string(*new_state));
2225 static DBusMessage *set_property(DBusConnection *conn,
2226 DBusMessage *msg, void *user_data)
2228 struct connman_service *service = user_data;
2229 DBusMessageIter iter, value;
2233 DBG("service %p", service);
2235 if (dbus_message_iter_init(msg, &iter) == FALSE)
2236 return __connman_error_invalid_arguments(msg);
2238 dbus_message_iter_get_basic(&iter, &name);
2239 dbus_message_iter_next(&iter);
2240 dbus_message_iter_recurse(&iter, &value);
2242 type = dbus_message_iter_get_arg_type(&value);
2244 if (g_str_equal(name, "AutoConnect") == TRUE) {
2245 connman_bool_t autoconnect;
2247 if (type != DBUS_TYPE_BOOLEAN)
2248 return __connman_error_invalid_arguments(msg);
2250 if (service->favorite == FALSE)
2251 return __connman_error_invalid_service(msg);
2253 dbus_message_iter_get_basic(&value, &autoconnect);
2255 if (service->autoconnect == autoconnect)
2256 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2258 service->autoconnect = autoconnect;
2260 autoconnect_changed(service);
2262 __connman_storage_save_service(service);
2263 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2264 const char *passphrase;
2266 if (type != DBUS_TYPE_STRING)
2267 return __connman_error_invalid_arguments(msg);
2269 if (service->immutable == TRUE)
2270 return __connman_error_not_supported(msg);
2272 dbus_message_iter_get_basic(&value, &passphrase);
2274 __connman_service_set_passphrase(service, passphrase);
2275 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2276 DBusMessageIter entry;
2281 if (type != DBUS_TYPE_ARRAY)
2282 return __connman_error_invalid_arguments(msg);
2284 str = g_string_new(NULL);
2286 return __connman_error_invalid_arguments(msg);
2288 index = connman_network_get_index(service->network);
2289 gw = __connman_ipconfig_get_gateway_from_index(index);
2291 if (gw && strlen(gw))
2292 __connman_service_nameserver_del_routes(service);
2294 dbus_message_iter_recurse(&value, &entry);
2296 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2298 dbus_message_iter_get_basic(&entry, &val);
2299 dbus_message_iter_next(&entry);
2301 g_string_append_printf(str, " %s", val);
2303 g_string_append(str, val);
2306 g_strfreev(service->nameservers_config);
2309 service->nameservers_config =
2310 g_strsplit_set(str->str, " ", 0);
2312 service->nameservers_config = NULL;
2315 g_string_free(str, TRUE);
2317 if (gw && strlen(gw))
2318 __connman_service_nameserver_add_routes(service, gw);
2320 update_nameservers(service);
2321 dns_configuration_changed(service);
2323 __connman_storage_save_service(service);
2324 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2325 DBusMessageIter entry;
2328 if (type != DBUS_TYPE_ARRAY)
2329 return __connman_error_invalid_arguments(msg);
2331 str = g_string_new(NULL);
2333 return __connman_error_invalid_arguments(msg);
2335 dbus_message_iter_recurse(&value, &entry);
2337 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2339 dbus_message_iter_get_basic(&entry, &val);
2340 dbus_message_iter_next(&entry);
2342 g_string_append_printf(str, " %s", val);
2344 g_string_append(str, val);
2347 g_strfreev(service->domains);
2350 service->domains = g_strsplit_set(str->str, " ", 0);
2352 service->domains = NULL;
2354 g_string_free(str, TRUE);
2356 update_nameservers(service);
2357 domain_configuration_changed(service);
2359 __connman_storage_save_service(service);
2360 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2363 if (type != DBUS_TYPE_ARRAY)
2364 return __connman_error_invalid_arguments(msg);
2366 err = update_proxy_configuration(service, &value);
2369 return __connman_error_failed(msg, -err);
2371 proxy_configuration_changed(service);
2373 __connman_storage_save_service(service);
2374 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2375 g_str_equal(name, "IPv6.Configuration")) {
2377 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2378 enum connman_service_state state =
2379 CONNMAN_SERVICE_STATE_UNKNOWN;
2384 if (service->ipconfig_ipv4 == NULL &&
2385 service->ipconfig_ipv6 == NULL)
2386 return __connman_error_invalid_property(msg);
2388 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2389 ipv4 = service->ipconfig_ipv4;
2390 err = set_ipconfig(service, ipv4, &value,
2391 service->state_ipv4, &state);
2393 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2394 ipv6 = service->ipconfig_ipv6;
2395 err = set_ipconfig(service, ipv6, &value,
2396 service->state_ipv6, &state);
2400 if (is_connected_state(service, state) ||
2401 is_connecting_state(service, state))
2402 __connman_network_set_ipconfig(service->network,
2404 return __connman_error_failed(msg, -err);
2408 ipv4_configuration_changed(service);
2410 ipv6_configuration_changed(service);
2412 if (is_connecting(service) || is_connected(service))
2413 __connman_network_set_ipconfig(service->network,
2416 __connman_storage_save_service(service);
2418 return __connman_error_invalid_property(msg);
2420 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2423 static void set_idle(struct connman_service *service)
2425 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
2426 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2427 state_changed(service);
2430 static DBusMessage *clear_property(DBusConnection *conn,
2431 DBusMessage *msg, void *user_data)
2433 struct connman_service *service = user_data;
2436 DBG("service %p", service);
2438 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2441 if (g_str_equal(name, "Error") == TRUE) {
2444 g_get_current_time(&service->modified);
2445 __connman_storage_save_service(service);
2446 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2447 if (service->immutable == TRUE)
2448 return __connman_error_not_supported(msg);
2450 g_free(service->passphrase);
2451 service->passphrase = NULL;
2453 passphrase_changed(service);
2455 __connman_storage_save_service(service);
2457 return __connman_error_invalid_property(msg);
2459 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2462 static connman_bool_t is_ignore(struct connman_service *service)
2464 if (service->autoconnect == FALSE)
2467 if (service->roaming == TRUE)
2470 if (service->ignore == TRUE)
2473 if (combine_state(service->state_ipv4, service->state_ipv6) ==
2474 CONNMAN_SERVICE_STATE_FAILURE)
2480 void __connman_service_auto_connect(void)
2482 struct connman_service *service = NULL;
2483 GSequenceIter *iter;
2487 if (__connman_session_mode() == TRUE) {
2488 DBG("Session mode enabled: auto connect disabled");
2492 iter = g_sequence_get_begin_iter(service_list);
2494 while (g_sequence_iter_is_end(iter) == FALSE) {
2495 service = g_sequence_get(iter);
2497 if (service->pending != NULL)
2500 if (is_connecting(service) == TRUE)
2503 if (service->favorite == FALSE)
2506 if (is_connected(service) == TRUE)
2509 if (is_ignore(service) == FALSE &&
2510 combine_state(service->state_ipv4,
2511 service->state_ipv6) ==
2512 CONNMAN_SERVICE_STATE_IDLE)
2517 iter = g_sequence_iter_next(iter);
2520 if (service != NULL) {
2521 service->userconnect = FALSE;
2522 __connman_service_connect(service);
2526 static void remove_timeout(struct connman_service *service)
2528 if (service->timeout > 0) {
2529 g_source_remove(service->timeout);
2530 service->timeout = 0;
2534 static void reply_pending(struct connman_service *service, int error)
2536 remove_timeout(service);
2538 if (service->pending != NULL) {
2542 reply = __connman_error_failed(service->pending,
2545 g_dbus_send_message(connection, reply);
2549 sender = dbus_message_get_interface(service->pending);
2551 DBG("sender %s", sender);
2553 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2554 g_dbus_send_reply(connection, service->pending,
2555 DBUS_TYPE_OBJECT_PATH, &service->path,
2558 g_dbus_send_reply(connection, service->pending,
2562 dbus_message_unref(service->pending);
2563 service->pending = NULL;
2567 static gboolean connect_timeout(gpointer user_data)
2569 struct connman_service *service = user_data;
2570 connman_bool_t autoconnect = FALSE;
2572 DBG("service %p", service);
2574 service->timeout = 0;
2576 if (service->network != NULL)
2577 __connman_network_disconnect(service->network);
2579 __connman_ipconfig_disable(service->ipconfig_ipv4);
2580 __connman_ipconfig_disable(service->ipconfig_ipv6);
2582 __connman_stats_service_unregister(service);
2584 if (service->pending != NULL) {
2587 reply = __connman_error_operation_timeout(service->pending);
2589 g_dbus_send_message(connection, reply);
2591 dbus_message_unref(service->pending);
2592 service->pending = NULL;
2596 __connman_service_indicate_state(service,
2597 CONNMAN_SERVICE_STATE_FAILURE,
2598 CONNMAN_IPCONFIG_TYPE_IPV4);
2599 __connman_service_indicate_state(service,
2600 CONNMAN_SERVICE_STATE_FAILURE,
2601 CONNMAN_IPCONFIG_TYPE_IPV6);
2603 if (autoconnect == TRUE && service->userconnect == FALSE)
2604 __connman_service_auto_connect();
2609 static void set_reconnect_state(struct connman_service *service,
2610 connman_bool_t reconnect)
2612 struct connman_device *device;
2614 if (service->network == NULL)
2617 device = connman_network_get_device(service->network);
2621 __connman_device_set_reconnect(device, reconnect);
2624 static connman_bool_t get_reconnect_state(struct connman_service *service)
2626 struct connman_device *device;
2628 if (service->network == NULL)
2631 device = connman_network_get_device(service->network);
2635 return __connman_device_get_reconnect(device);
2638 static void request_input_cb (struct connman_service *service,
2639 const char *passphrase, void *user_data)
2641 DBG ("RequestInput return, %p", service);
2643 if (passphrase == NULL && service->wps == FALSE)
2646 if (passphrase != NULL)
2647 __connman_service_set_passphrase(service, passphrase);
2649 __connman_service_connect(service);
2652 static DBusMessage *connect_service(DBusConnection *conn,
2653 DBusMessage *msg, void *user_data)
2655 struct connman_service *service = user_data;
2656 GSequenceIter *iter;
2659 DBG("service %p", service);
2661 if (service->pending != NULL)
2662 return __connman_error_in_progress(msg);
2664 iter = g_sequence_get_begin_iter(service_list);
2666 while (g_sequence_iter_is_end(iter) == FALSE) {
2667 struct connman_service *temp = g_sequence_get(iter);
2669 if (service->type == temp->type && is_connecting(temp) == TRUE)
2670 return __connman_error_in_progress(msg);
2672 iter = g_sequence_iter_next(iter);
2675 service->ignore = FALSE;
2677 service->userconnect = TRUE;
2679 service->pending = dbus_message_ref(msg);
2681 set_reconnect_state(service, FALSE);
2683 err = __connman_service_connect(service);
2685 if (err == -ENOKEY) {
2686 if (__connman_agent_request_input(service,
2692 if (service->pending == NULL)
2695 if (err != -EINPROGRESS) {
2696 dbus_message_unref(service->pending);
2697 service->pending = NULL;
2699 return __connman_error_failed(msg, -err);
2705 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2708 static DBusMessage *disconnect_service(DBusConnection *conn,
2709 DBusMessage *msg, void *user_data)
2711 struct connman_service *service = user_data;
2714 DBG("service %p", service);
2716 reply_pending(service, ECONNABORTED);
2718 service->ignore = TRUE;
2720 set_reconnect_state(service, FALSE);
2722 err = __connman_service_disconnect(service);
2724 if (err != -EINPROGRESS)
2725 return __connman_error_failed(msg, -err);
2730 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2733 static DBusMessage *remove_service(DBusConnection *conn,
2734 DBusMessage *msg, void *user_data)
2736 struct connman_service *service = user_data;
2738 DBG("service %p", service);
2740 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2741 return __connman_error_not_supported(msg);
2743 if (service->immutable == TRUE)
2744 return __connman_error_not_supported(msg);
2746 if (service->favorite == FALSE &&
2747 combine_state(service->state_ipv4, service->state_ipv6) !=
2748 CONNMAN_SERVICE_STATE_FAILURE)
2749 return __connman_error_not_supported(msg);
2751 if (service->network != NULL) {
2752 set_reconnect_state(service, FALSE);
2754 __connman_network_disconnect(service->network);
2757 g_free(service->passphrase);
2758 service->passphrase = NULL;
2760 passphrase_changed(service);
2764 __connman_service_set_favorite(service, FALSE);
2765 __connman_storage_save_service(service);
2767 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2770 static DBusMessage *move_service(DBusConnection *conn,
2771 DBusMessage *msg, void *user_data,
2774 struct connman_service *service = user_data;
2775 struct connman_service *target;
2777 GSequenceIter *src, *dst;
2779 DBG("service %p", service);
2781 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2784 if (service->favorite == FALSE)
2785 return __connman_error_not_supported(msg);
2787 target = find_service(path);
2788 if (target == NULL || target->favorite == FALSE || target == service)
2789 return __connman_error_invalid_service(msg);
2791 DBG("target %s", target->identifier);
2793 if (target->state_ipv4 != service->state_ipv4 &&
2794 target->state_ipv6 != service->state_ipv6)
2795 return __connman_error_invalid_service(msg);
2797 g_get_current_time(&service->modified);
2798 __connman_storage_save_service(service);
2800 src = g_hash_table_lookup(service_hash, service->identifier);
2801 dst = g_hash_table_lookup(service_hash, target->identifier);
2803 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2805 __connman_profile_changed(FALSE);
2807 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2810 static DBusMessage *move_before(DBusConnection *conn,
2811 DBusMessage *msg, void *user_data)
2813 return move_service(conn, msg, user_data, TRUE);
2816 static DBusMessage *move_after(DBusConnection *conn,
2817 DBusMessage *msg, void *user_data)
2819 return move_service(conn, msg, user_data, FALSE);
2822 static DBusMessage *reset_counters(DBusConnection *conn,
2823 DBusMessage *msg, void *user_data)
2825 struct connman_service *service = user_data;
2827 reset_stats(service);
2829 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2832 static GDBusMethodTable service_methods[] = {
2833 { "GetProperties", "", "a{sv}", get_properties },
2834 { "SetProperty", "sv", "", set_property },
2835 { "ClearProperty", "s", "", clear_property },
2836 { "Connect", "", "", connect_service,
2837 G_DBUS_METHOD_FLAG_ASYNC },
2838 { "Disconnect", "", "", disconnect_service },
2839 { "Remove", "", "", remove_service },
2840 { "MoveBefore", "o", "", move_before },
2841 { "MoveAfter", "o", "", move_after },
2842 { "ResetCounters", "", "", reset_counters },
2846 static GDBusSignalTable service_signals[] = {
2847 { "PropertyChanged", "sv" },
2851 static void service_free(gpointer user_data)
2853 struct connman_service *service = user_data;
2854 char *path = service->path;
2856 DBG("service %p", service);
2858 reply_pending(service, ENOENT);
2860 g_hash_table_remove(service_hash, service->identifier);
2862 stats_stop(service);
2863 __connman_storage_save_service(service);
2865 service->path = NULL;
2868 __connman_profile_changed(FALSE);
2870 g_dbus_unregister_interface(connection, path,
2871 CONNMAN_SERVICE_INTERFACE);
2875 g_hash_table_destroy(service->counter_table);
2877 if (service->network != NULL)
2878 connman_network_unref(service->network);
2880 if (service->provider != NULL)
2881 connman_provider_unref(service->provider);
2883 if (service->ipconfig_ipv4 != NULL) {
2884 connman_ipconfig_unref(service->ipconfig_ipv4);
2885 service->ipconfig_ipv4 = NULL;
2888 if (service->ipconfig_ipv6 != NULL) {
2889 connman_ipconfig_unref(service->ipconfig_ipv6);
2890 service->ipconfig_ipv6 = NULL;
2893 if (service->location != NULL)
2894 connman_location_unref(service->location);
2896 g_strfreev(service->nameservers);
2897 g_strfreev(service->nameservers_config);
2898 g_strfreev(service->domains);
2899 g_strfreev(service->proxies);
2900 g_strfreev(service->excludes);
2902 g_free(service->domainname);
2903 g_free(service->pac);
2904 g_free(service->profile);
2905 g_free(service->name);
2906 g_free(service->passphrase);
2907 g_free(service->identifier);
2908 g_free(service->eap);
2909 g_free(service->identity);
2910 g_free(service->ca_cert_file);
2911 g_free(service->client_cert_file);
2912 g_free(service->private_key_file);
2913 g_free(service->private_key_passphrase);
2914 g_free(service->phase2);
2916 if (service->stats.timer != NULL)
2917 g_timer_destroy(service->stats.timer);
2918 if (service->stats_roaming.timer != NULL)
2919 g_timer_destroy(service->stats_roaming.timer);
2925 * __connman_service_put:
2926 * @service: service structure
2928 * Release service if no longer needed
2930 void __connman_service_put(struct connman_service *service)
2932 DBG("service %p", service);
2934 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2935 GSequenceIter *iter;
2937 iter = g_hash_table_lookup(service_hash, service->identifier);
2939 reply_pending(service, ECONNABORTED);
2941 __connman_service_disconnect(service);
2943 g_sequence_remove(iter);
2945 service_free(service);
2949 static void stats_init(struct connman_service *service)
2952 service->stats.valid = FALSE;
2953 service->stats.enabled = FALSE;
2954 service->stats.timer = g_timer_new();
2957 service->stats_roaming.valid = FALSE;
2958 service->stats_roaming.enabled = FALSE;
2959 service->stats_roaming.timer = g_timer_new();
2962 static void service_initialize(struct connman_service *service)
2964 DBG("service %p", service);
2966 service->refcount = 1;
2968 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
2969 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
2970 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
2972 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
2973 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
2975 service->favorite = FALSE;
2976 service->immutable = FALSE;
2977 service->hidden = FALSE;
2979 service->ignore = FALSE;
2981 service->userconnect = FALSE;
2985 stats_init(service);
2987 service->provider = NULL;
2989 service->wps = FALSE;
2993 * connman_service_create:
2995 * Allocate a new service.
2997 * Returns: a newly-allocated #connman_service structure
2999 struct connman_service *connman_service_create(void)
3002 struct connman_stats_counter *counters;
3003 const char *counter;
3005 struct connman_service *service;
3007 service = g_try_new0(struct connman_service, 1);
3008 if (service == NULL)
3011 DBG("service %p", service);
3013 service->counter_table = g_hash_table_new_full(g_str_hash,
3014 g_str_equal, NULL, g_free);
3016 for (list = counter_list; list; list = list->next) {
3017 counter = list->data;
3019 counters = g_try_new0(struct connman_stats_counter, 1);
3020 if (counters == NULL) {
3021 g_hash_table_destroy(service->counter_table);
3026 counters->append_all = TRUE;
3028 g_hash_table_replace(service->counter_table, (gpointer)counter,
3032 service_initialize(service);
3034 service->location = __connman_location_create(service);
3039 struct connman_location *__connman_service_get_location(struct connman_service *service)
3041 return service->location;
3045 * connman_service_ref:
3046 * @service: service structure
3048 * Increase reference counter of service
3050 struct connman_service *connman_service_ref(struct connman_service *service)
3054 g_atomic_int_inc(&service->refcount);
3060 * connman_service_unref:
3061 * @service: service structure
3063 * Decrease reference counter of service
3065 void connman_service_unref(struct connman_service *service)
3067 __connman_service_put(service);
3070 static gint service_compare(gconstpointer a, gconstpointer b,
3073 struct connman_service *service_a = (void *) a;
3074 struct connman_service *service_b = (void *) b;
3075 enum connman_service_state state_a, state_b;
3077 state_a = combine_state(service_a->state_ipv4, service_a->state_ipv6);
3078 state_b = combine_state(service_b->state_ipv4, service_b->state_ipv6);
3080 if (state_a != state_b) {
3081 if (is_connected(service_a) == TRUE)
3083 if (is_connected(service_b) == TRUE)
3086 if (is_connecting(service_a) == TRUE)
3088 if (is_connecting(service_b) == TRUE)
3092 if (service_a->order > service_b->order)
3095 if (service_a->order < service_b->order)
3098 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3101 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3104 if (service_a->type != service_b->type) {
3105 switch (service_a->type) {
3106 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3107 case CONNMAN_SERVICE_TYPE_SYSTEM:
3108 case CONNMAN_SERVICE_TYPE_ETHERNET:
3109 case CONNMAN_SERVICE_TYPE_GPS:
3110 case CONNMAN_SERVICE_TYPE_VPN:
3111 case CONNMAN_SERVICE_TYPE_GADGET:
3113 case CONNMAN_SERVICE_TYPE_WIFI:
3115 case CONNMAN_SERVICE_TYPE_WIMAX:
3116 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3117 case CONNMAN_SERVICE_TYPE_CELLULAR:
3122 return (gint) service_b->strength - (gint) service_a->strength;
3126 * connman_service_get_type:
3127 * @service: service structure
3129 * Get the type of service
3131 enum connman_service_type connman_service_get_type(struct connman_service *service)
3133 if (service == NULL)
3134 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3136 return service->type;
3140 * connman_service_get_interface:
3141 * @service: service structure
3143 * Get network interface of service
3145 char *connman_service_get_interface(struct connman_service *service)
3149 if (service == NULL)
3152 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3153 if (service->ipconfig_ipv4)
3154 index = connman_ipconfig_get_index(
3155 service->ipconfig_ipv4);
3156 else if (service->ipconfig_ipv6)
3157 index = connman_ipconfig_get_index(
3158 service->ipconfig_ipv6);
3162 return connman_inet_ifname(index);
3165 if (service->network == NULL)
3168 index = connman_network_get_index(service->network);
3170 return connman_inet_ifname(index);
3174 * connman_service_get_network:
3175 * @service: service structure
3177 * Get the service network
3179 struct connman_network *
3180 __connman_service_get_network(struct connman_service *service)
3182 if (service == NULL)
3185 return service->network;
3188 struct connman_ipconfig *
3189 __connman_service_get_ip4config(struct connman_service *service)
3191 if (service == NULL)
3194 return service->ipconfig_ipv4;
3197 struct connman_ipconfig *
3198 __connman_service_get_ip6config(struct connman_service *service)
3200 if (service == NULL)
3203 return service->ipconfig_ipv6;
3206 struct connman_ipconfig *
3207 __connman_service_get_ipconfig(struct connman_service *service, int family)
3209 if (family == AF_INET)
3210 return __connman_service_get_ip4config(service);
3211 else if (family == AF_INET6)
3212 return __connman_service_get_ip6config(service);
3218 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3220 if (service == NULL)
3221 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3223 return service->security;
3226 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3228 if (service == NULL)
3231 return service->wps;
3235 * __connman_service_set_favorite:
3236 * @service: service structure
3237 * @favorite: favorite value
3239 * Change the favorite setting of service
3241 int __connman_service_set_favorite(struct connman_service *service,
3242 connman_bool_t favorite)
3244 GSequenceIter *iter;
3246 iter = g_hash_table_lookup(service_hash, service->identifier);
3250 if (service->favorite == favorite)
3253 service->favorite = favorite;
3255 favorite_changed(service);
3257 g_sequence_sort_changed(iter, service_compare, NULL);
3259 __connman_profile_changed(FALSE);
3264 int __connman_service_set_immutable(struct connman_service *service,
3265 connman_bool_t immutable)
3267 service->immutable = immutable;
3269 immutable_changed(service);
3274 void __connman_service_set_string(struct connman_service *service,
3275 const char *key, const char *value)
3277 if (g_str_equal(key, "EAP") == TRUE) {
3278 g_free(service->eap);
3279 service->eap = g_strdup(value);
3280 } else if (g_str_equal(key, "Identity") == TRUE) {
3281 g_free(service->identity);
3282 service->identity = g_strdup(value);
3283 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3284 g_free(service->ca_cert_file);
3285 service->ca_cert_file = g_strdup(value);
3286 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3287 g_free(service->client_cert_file);
3288 service->client_cert_file = g_strdup(value);
3289 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3290 g_free(service->private_key_file);
3291 service->private_key_file = g_strdup(value);
3292 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3293 g_free(service->private_key_passphrase);
3294 service->private_key_passphrase = g_strdup(value);
3295 } else if (g_str_equal(key, "Phase2") == TRUE) {
3296 g_free(service->phase2);
3297 service->phase2 = g_strdup(value);
3298 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3299 g_free(service->passphrase);
3300 service->passphrase = g_strdup(value);
3304 static void service_complete(struct connman_service *service)
3306 reply_pending(service, EIO);
3308 if (service->userconnect == FALSE)
3309 __connman_service_auto_connect();
3311 g_get_current_time(&service->modified);
3312 __connman_storage_save_service(service);
3315 static void report_error_cb(struct connman_service *service,
3316 gboolean retry, void *user_data)
3319 __connman_service_connect(service);
3321 service_complete(service);
3322 __connman_profile_changed(FALSE);
3323 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3327 int __connman_service_indicate_state(struct connman_service *service,
3328 enum connman_service_state new_state,
3329 enum connman_ipconfig_type type)
3331 enum connman_service_state service_state,
3332 state = CONNMAN_SERVICE_STATE_IDLE;
3333 GSequenceIter *iter;
3335 if (service == NULL)
3338 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3339 if (service->state_ipv4 == new_state)
3341 state = combine_state(new_state, service->state_ipv6);
3342 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3343 if (service->state_ipv6 == new_state)
3345 state = combine_state(new_state, service->state_ipv4);
3349 service_state = combine_state(service->state_ipv4,
3350 service->state_ipv6);
3352 DBG("service %p state %s/%s => %s new %s/%d => %s",
3354 state2string(service->state_ipv4),
3355 state2string(service->state_ipv6),
3356 state2string(service_state),
3357 state2string(new_state),
3359 state2string(state));
3361 if (service_state == CONNMAN_SERVICE_STATE_FAILURE &&
3362 state == CONNMAN_SERVICE_STATE_IDLE)
3365 if (service_state == CONNMAN_SERVICE_STATE_IDLE &&
3366 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3369 if (state == CONNMAN_SERVICE_STATE_IDLE &&
3370 service_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3371 state_changed(service);
3373 reply_pending(service, ECONNABORTED);
3375 __connman_service_disconnect(service);
3378 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3379 if (__connman_stats_service_register(service) == 0) {
3380 __connman_stats_get(service, FALSE,
3381 &service->stats.data);
3382 __connman_stats_get(service, TRUE,
3383 &service->stats_roaming.data);
3386 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
3387 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3388 __connman_ipconfig_enable(service->ipconfig_ipv4);
3389 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
3390 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3391 __connman_ipconfig_enable(service->ipconfig_ipv6);
3394 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3395 service->state_ipv4 = new_state;
3396 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3397 service->state_ipv6 = new_state;
3399 if (state == service_state)
3402 state_changed(service);
3404 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3405 if (service->login_required == TRUE) {
3406 service->login_required = FALSE;
3407 login_changed(service);
3410 connman_timeserver_sync();
3413 if (state == CONNMAN_SERVICE_STATE_IDLE) {
3414 connman_bool_t reconnect;
3416 reconnect = get_reconnect_state(service);
3417 if (reconnect == TRUE)
3418 __connman_service_auto_connect();
3421 if (state == CONNMAN_SERVICE_STATE_READY) {
3422 enum connman_service_proxy_method proxy_config;
3423 enum connman_ipconfig_method method;
3425 set_reconnect_state(service, TRUE);
3427 __connman_service_set_favorite(service, TRUE);
3429 reply_pending(service, 0);
3431 service->userconnect = FALSE;
3433 g_get_current_time(&service->modified);
3434 __connman_storage_save_service(service);
3436 update_nameservers(service);
3437 dns_changed(service);
3438 domain_changed(service);
3440 proxy_config = service->proxy_config;
3443 * We start WPAD if we haven't got a PAC URL from DHCP and
3444 * if our proxy manual configuration is either empty or set
3445 * to AUTO with an empty URL.
3447 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3448 (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3450 CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3451 service->pac == NULL)))
3452 if (__connman_wpad_start(service) < 0) {
3454 CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3456 __connman_notifier_proxy_changed(service);
3459 __connman_notifier_connect(service->type);
3461 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3462 connman_network_get_bool(service->network,
3463 "WiFi.UseWPS") == TRUE) {
3466 pass = connman_network_get_string(service->network,
3469 __connman_service_set_passphrase(service, pass);
3471 connman_network_set_bool(service->network,
3472 "WiFi.UseWPS", FALSE);
3477 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3478 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3479 __connman_ipconfig_disable_ipv6(
3480 service->ipconfig_ipv6);
3482 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3483 __connman_location_finish(service);
3487 __connman_wpad_stop(service);
3489 update_nameservers(service);
3490 dns_changed(service);
3491 domain_changed(service);
3493 __connman_notifier_disconnect(service->type);
3496 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3497 if (service->userconnect == TRUE &&
3498 __connman_agent_report_error(service,
3499 error2string(service->error),
3500 report_error_cb, NULL) == -EIO)
3502 service_complete(service);
3504 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3506 iter = g_hash_table_lookup(service_hash, service->identifier);
3508 g_sequence_sort_changed(iter, service_compare, NULL);
3510 __connman_profile_changed(FALSE);
3512 service_state = combine_state(service->state_ipv4,
3513 service->state_ipv6);
3514 if (service_state == CONNMAN_SERVICE_STATE_ONLINE)
3517 if (service_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3518 struct connman_service *def_service = get_default();
3520 if (__connman_notifier_count_connected() == 0 &&
3521 def_service != NULL &&
3522 def_service->provider != NULL)
3523 __connman_provider_disconnect(def_service->provider);
3526 if (service_state == CONNMAN_SERVICE_STATE_IDLE ||
3527 service_state == CONNMAN_SERVICE_STATE_FAILURE)
3528 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3533 int __connman_service_indicate_error(struct connman_service *service,
3534 enum connman_service_error error)
3536 DBG("service %p error %d", service, error);
3538 if (service == NULL)
3541 service->error = error;
3543 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3544 __connman_service_set_passphrase(service, NULL);
3546 return __connman_service_indicate_state(service,
3547 CONNMAN_SERVICE_STATE_FAILURE,
3548 CONNMAN_IPCONFIG_TYPE_IPV4);
3551 int __connman_service_clear_error(struct connman_service *service)
3553 enum connman_service_state state;
3555 DBG("service %p", service);
3557 if (service == NULL)
3560 state = combine_state(service->state_ipv4, service->state_ipv6);
3562 if (state != CONNMAN_SERVICE_STATE_FAILURE)
3565 service->state_ipv4 = service->state_ipv6 =
3566 CONNMAN_SERVICE_STATE_UNKNOWN;
3567 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3569 if (service->favorite == TRUE)
3570 set_reconnect_state(service, TRUE);
3572 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
3573 CONNMAN_IPCONFIG_TYPE_IPV6);
3576 * Toggling the IPv6 state to IDLE could trigger the auto connect
3577 * machinery and consequently the IPv4 state.
3579 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3580 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3583 return __connman_service_indicate_state(service,
3584 CONNMAN_SERVICE_STATE_IDLE,
3585 CONNMAN_IPCONFIG_TYPE_IPV4);
3588 int __connman_service_indicate_default(struct connman_service *service)
3590 DBG("service %p", service);
3594 __connman_location_detect(service);
3599 int __connman_service_request_login(struct connman_service *service)
3601 DBG("service %p", service);
3603 if (service == NULL)
3606 service->login_required = TRUE;
3607 login_changed(service);
3612 static connman_bool_t prepare_network(struct connman_service *service)
3614 enum connman_network_type type;
3615 unsigned int ssid_len;
3617 type = connman_network_get_type(service->network);
3620 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3621 case CONNMAN_NETWORK_TYPE_VENDOR:
3623 case CONNMAN_NETWORK_TYPE_WIFI:
3624 if (connman_network_get_blob(service->network, "WiFi.SSID",
3628 if (service->passphrase != NULL)
3629 connman_network_set_string(service->network,
3630 "WiFi.Passphrase", service->passphrase);
3632 case CONNMAN_NETWORK_TYPE_ETHERNET:
3633 case CONNMAN_NETWORK_TYPE_WIMAX:
3634 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3635 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3636 case CONNMAN_NETWORK_TYPE_CELLULAR:
3643 static void prepare_8021x(struct connman_service *service)
3645 if (service->eap != NULL)
3646 connman_network_set_string(service->network, "WiFi.EAP",
3649 if (service->identity != NULL)
3650 connman_network_set_string(service->network, "WiFi.Identity",
3653 if (service->ca_cert_file != NULL)
3654 connman_network_set_string(service->network, "WiFi.CACertFile",
3655 service->ca_cert_file);
3657 if (service->client_cert_file != NULL)
3658 connman_network_set_string(service->network,
3659 "WiFi.ClientCertFile",
3660 service->client_cert_file);
3662 if (service->private_key_file != NULL)
3663 connman_network_set_string(service->network,
3664 "WiFi.PrivateKeyFile",
3665 service->private_key_file);
3667 if (service->private_key_passphrase != NULL)
3668 connman_network_set_string(service->network,
3669 "WiFi.PrivateKeyPassphrase",
3670 service->private_key_passphrase);
3672 if (service->phase2 != NULL)
3673 connman_network_set_string(service->network, "WiFi.Phase2",
3677 static int service_connect(struct connman_service *service)
3681 switch (service->type) {
3682 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3683 case CONNMAN_SERVICE_TYPE_SYSTEM:
3684 case CONNMAN_SERVICE_TYPE_GPS:
3685 case CONNMAN_SERVICE_TYPE_GADGET:
3687 case CONNMAN_SERVICE_TYPE_ETHERNET:
3688 case CONNMAN_SERVICE_TYPE_WIMAX:
3689 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3690 case CONNMAN_SERVICE_TYPE_CELLULAR:
3691 case CONNMAN_SERVICE_TYPE_VPN:
3693 case CONNMAN_SERVICE_TYPE_WIFI:
3694 switch (service->security) {
3695 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3696 case CONNMAN_SERVICE_SECURITY_NONE:
3698 case CONNMAN_SERVICE_SECURITY_WEP:
3699 case CONNMAN_SERVICE_SECURITY_PSK:
3700 case CONNMAN_SERVICE_SECURITY_WPA:
3701 case CONNMAN_SERVICE_SECURITY_RSN:
3702 if (service->passphrase == NULL) {
3703 if (service->network == NULL)
3706 if (service->wps == FALSE ||
3707 connman_network_get_bool(
3709 "WiFi.UseWPS") == FALSE)
3713 case CONNMAN_SERVICE_SECURITY_8021X:
3719 if (service->network != NULL) {
3720 if (prepare_network(service) == FALSE)
3723 switch (service->security) {
3724 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3725 case CONNMAN_SERVICE_SECURITY_NONE:
3726 case CONNMAN_SERVICE_SECURITY_WEP:
3727 case CONNMAN_SERVICE_SECURITY_PSK:
3728 case CONNMAN_SERVICE_SECURITY_WPA:
3729 case CONNMAN_SERVICE_SECURITY_RSN:
3731 case CONNMAN_SERVICE_SECURITY_8021X:
3732 prepare_8021x(service);
3736 if (__connman_stats_service_register(service) == 0) {
3737 __connman_stats_get(service, FALSE,
3738 &service->stats.data);
3739 __connman_stats_get(service, TRUE,
3740 &service->stats_roaming.data);
3743 if (service->ipconfig_ipv4)
3744 __connman_ipconfig_enable(service->ipconfig_ipv4);
3745 if (service->ipconfig_ipv6)
3746 __connman_ipconfig_enable(service->ipconfig_ipv6);
3748 err = __connman_network_connect(service->network);
3749 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3750 service->provider != NULL)
3751 err = __connman_provider_connect(service->provider);
3756 if (err != -EINPROGRESS) {
3757 __connman_ipconfig_disable(service->ipconfig_ipv4);
3758 __connman_ipconfig_disable(service->ipconfig_ipv6);
3759 __connman_stats_service_unregister(service);
3767 int __connman_service_connect(struct connman_service *service)
3769 enum connman_service_state state;
3772 state = combine_state(service->state_ipv4, service->state_ipv6);
3774 DBG("service %p state %s", service, state2string(state));
3776 if (is_connected(service) == TRUE)
3779 if (is_connecting(service) == TRUE)
3782 switch (service->type) {
3783 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3784 case CONNMAN_SERVICE_TYPE_SYSTEM:
3785 case CONNMAN_SERVICE_TYPE_GPS:
3786 case CONNMAN_SERVICE_TYPE_GADGET:
3789 err = service_connect(service);
3795 if (err == -EINPROGRESS) {
3796 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
3797 connect_timeout, service);
3799 return -EINPROGRESS;
3805 if (service->userconnect == TRUE)
3806 reply_pending(service, err);
3808 __connman_service_indicate_state(service,
3809 CONNMAN_SERVICE_STATE_FAILURE,
3810 CONNMAN_IPCONFIG_TYPE_IPV4);
3811 __connman_service_indicate_state(service,
3812 CONNMAN_SERVICE_STATE_FAILURE,
3813 CONNMAN_IPCONFIG_TYPE_IPV6);
3818 int __connman_service_disconnect(struct connman_service *service)
3822 DBG("service %p", service);
3824 if (service->network != NULL) {
3825 err = __connman_network_disconnect(service->network);
3826 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3827 service->provider != NULL)
3828 err = __connman_provider_disconnect(service->provider);
3832 if (err < 0 && err != -EINPROGRESS)
3835 __connman_6to4_remove(service->ipconfig_ipv4);
3837 if (service->ipconfig_ipv4)
3838 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3841 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3844 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3845 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3847 __connman_ipconfig_disable(service->ipconfig_ipv4);
3848 __connman_ipconfig_disable(service->ipconfig_ipv6);
3850 __connman_stats_service_unregister(service);
3855 int __connman_service_disconnect_all(void)
3857 GSequenceIter *iter;
3861 iter = g_sequence_get_begin_iter(service_list);
3863 while (g_sequence_iter_is_end(iter) == FALSE) {
3864 struct connman_service *service = g_sequence_get(iter);
3866 service->ignore = TRUE;
3868 set_reconnect_state(service, FALSE);
3870 __connman_service_disconnect(service);
3872 iter = g_sequence_iter_next(iter);
3880 * __connman_service_lookup:
3881 * @pattern: search pattern
3882 * @path: return object path
3884 * Look up a service path from a search pattern
3886 int __connman_service_lookup(const char *pattern, const char **path)
3888 GHashTableIter iter;
3889 gpointer key, value;
3891 g_hash_table_iter_init(&iter, service_hash);
3893 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
3894 GSequenceIter *iter = value;
3895 struct connman_service *service = g_sequence_get(iter);
3897 if (g_strcmp0(service->identifier, pattern) == 0 ||
3898 g_strcmp0(service->name, pattern) == 0) {
3899 *path = (const char *) service->path;
3908 * lookup_by_identifier:
3909 * @identifier: service identifier
3911 * Look up a service by identifier (reference count will not be increased)
3913 static struct connman_service *lookup_by_identifier(const char *identifier)
3915 GSequenceIter *iter;
3917 iter = g_hash_table_lookup(service_hash, identifier);
3919 return g_sequence_get(iter);
3924 static struct connman_network *create_hidden_wifi(struct connman_device *device,
3925 const char *ssid, const char *mode, const char *security)
3927 struct connman_network *network;
3930 unsigned int i, ssid_len;
3932 ssid_len = strlen(ssid);
3936 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
3937 if (network == NULL)
3940 connman_network_set_blob(network, "WiFi.SSID",
3941 (unsigned char *) ssid, ssid_len);
3943 connman_network_set_string(network, "WiFi.Mode", mode);
3944 connman_network_set_string(network, "WiFi.Security", security);
3946 name = g_try_malloc0(ssid_len + 1);
3948 connman_network_unref(network);
3952 for (i = 0; i < ssid_len; i++) {
3953 if (g_ascii_isprint(ssid[i]))
3959 connman_network_set_name(network, name);
3963 index = connman_device_get_index(device);
3964 connman_network_set_index(network, index);
3966 if (connman_device_add_network(device, network) < 0) {
3967 connman_network_unref(network);
3971 connman_network_set_available(network, TRUE);
3976 int __connman_service_create_and_connect(DBusMessage *msg)
3978 struct connman_service *service;
3979 struct connman_network *network;
3980 struct connman_device *device;
3981 DBusMessageIter iter, array;
3982 const char *mode = "managed", *security = "none", *group_security;
3983 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
3984 unsigned int ssid_len = 0;
3987 gboolean created = FALSE;
3990 dbus_message_iter_init(msg, &iter);
3991 dbus_message_iter_recurse(&iter, &array);
3993 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
3994 DBusMessageIter entry, value;
3997 dbus_message_iter_recurse(&array, &entry);
3998 dbus_message_iter_get_basic(&entry, &key);
4000 dbus_message_iter_next(&entry);
4001 dbus_message_iter_recurse(&entry, &value);
4003 switch (dbus_message_iter_get_arg_type(&value)) {
4004 case DBUS_TYPE_STRING:
4005 if (g_str_equal(key, "Type") == TRUE)
4006 dbus_message_iter_get_basic(&value, &type);
4007 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4008 g_str_equal(key, "Mode") == TRUE)
4009 dbus_message_iter_get_basic(&value, &mode);
4010 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4011 g_str_equal(key, "Security") == TRUE)
4012 dbus_message_iter_get_basic(&value, &security);
4013 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4014 g_str_equal(key, "Passphrase") == TRUE)
4015 dbus_message_iter_get_basic(&value, &passphrase);
4016 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4017 g_str_equal(key, "SSID") == TRUE)
4018 dbus_message_iter_get_basic(&value, &ssid);
4021 dbus_message_iter_next(&array);
4027 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4033 ssid_len = strlen(ssid);
4037 if (g_strcmp0(security, "none") != 0 &&
4038 g_strcmp0(security, "wep") != 0 &&
4039 g_strcmp0(security, "psk") != 0 &&
4040 g_strcmp0(security, "wpa") != 0 &&
4041 g_strcmp0(security, "rsn") != 0 &&
4042 g_strcmp0(security, "ieee8021x") != 0)
4045 device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4049 ident = connman_device_get_ident(device);
4054 if (!g_strcmp0(security, "wpa") ||
4055 !g_strcmp0(security, "rsn"))
4056 group_security = "psk";
4058 group_security = security;
4060 group = connman_wifi_build_group_name((unsigned char *) ssid,
4061 ssid_len, mode, group_security);
4065 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4067 service = lookup_by_identifier(name);
4069 if (service != NULL)
4072 network = create_hidden_wifi(device, ssid, mode, security);
4073 if (network != NULL) {
4074 connman_network_set_group(network, group);
4078 service = lookup_by_identifier(name);
4084 if (service == NULL) {
4089 if (is_connected(service) == TRUE) {
4094 if (is_connecting(service) == TRUE) {
4099 set_reconnect_state(service, FALSE);
4101 __connman_device_disconnect(device);
4103 if (passphrase != NULL) {
4104 g_free(service->passphrase);
4105 service->passphrase = g_strdup(passphrase);
4108 service->userconnect = TRUE;
4110 err = __connman_service_connect(service);
4111 if (err < 0 && err != -EINPROGRESS)
4114 service->pending = dbus_message_ref(msg);
4119 if (service != NULL && created == TRUE) {
4120 struct connman_network *network = service->network;
4122 if (network != NULL) {
4123 connman_network_set_available(network, FALSE);
4124 __connman_device_cleanup_networks(device);
4126 __connman_service_put(service);
4133 * __connman_service_get:
4134 * @identifier: service identifier
4136 * Look up a service by identifier or create a new one if not found
4138 static struct connman_service *service_get(const char *identifier)
4140 struct connman_service *service;
4141 GSequenceIter *iter;
4143 iter = g_hash_table_lookup(service_hash, identifier);
4145 service = g_sequence_get(iter);
4146 if (service != NULL)
4147 connman_service_ref(service);
4151 service = connman_service_create();
4152 if (service == NULL)
4155 DBG("service %p", service);
4157 service->identifier = g_strdup(identifier);
4159 service->profile = g_strdup(__connman_profile_active_ident());
4161 iter = g_sequence_insert_sorted(service_list, service,
4162 service_compare, NULL);
4164 g_hash_table_insert(service_hash, service->identifier, iter);
4169 static int service_register(struct connman_service *service)
4171 const char *path = __connman_profile_active_path();
4172 GSequenceIter *iter;
4174 DBG("service %p", service);
4176 if (service->path != NULL)
4179 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4181 DBG("path %s", service->path);
4183 __connman_config_provision_service(service);
4185 __connman_storage_load_service(service);
4187 g_dbus_register_interface(connection, service->path,
4188 CONNMAN_SERVICE_INTERFACE,
4189 service_methods, service_signals,
4190 NULL, service, NULL);
4192 iter = g_hash_table_lookup(service_hash, service->identifier);
4194 g_sequence_sort_changed(iter, service_compare, NULL);
4196 __connman_profile_changed(TRUE);
4201 static void service_up(struct connman_ipconfig *ipconfig)
4203 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4205 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
4207 link_changed(service);
4209 service->stats.valid = FALSE;
4210 service->stats_roaming.valid = FALSE;
4213 static void service_down(struct connman_ipconfig *ipconfig)
4215 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
4218 static void service_lower_up(struct connman_ipconfig *ipconfig)
4220 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4222 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4224 stats_start(service);
4227 static void service_lower_down(struct connman_ipconfig *ipconfig)
4229 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4231 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4233 stats_stop(service);
4234 __connman_storage_save_service(service);
4237 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4239 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4240 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4241 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4243 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4245 type = __connman_ipconfig_get_config_type(ipconfig);
4246 method = __connman_ipconfig_get_method(ipconfig);
4248 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4251 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4252 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4253 __connman_service_indicate_state(service,
4254 CONNMAN_SERVICE_STATE_READY,
4255 CONNMAN_IPCONFIG_TYPE_IPV6);
4257 settings_changed(service);
4260 static void service_ip_release(struct connman_ipconfig *ipconfig)
4262 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4263 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4264 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4266 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4268 type = __connman_ipconfig_get_config_type(ipconfig);
4269 method = __connman_ipconfig_get_method(ipconfig);
4271 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4274 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4275 method == CONNMAN_IPCONFIG_METHOD_OFF)
4276 __connman_service_indicate_state(service,
4277 CONNMAN_SERVICE_STATE_DISCONNECT,
4278 CONNMAN_IPCONFIG_TYPE_IPV6);
4280 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4281 method == CONNMAN_IPCONFIG_METHOD_OFF)
4282 __connman_service_indicate_state(service,
4283 CONNMAN_SERVICE_STATE_DISCONNECT,
4284 CONNMAN_IPCONFIG_TYPE_IPV4);
4286 settings_changed(service);
4289 static const struct connman_ipconfig_ops service_ops = {
4291 .down = service_down,
4292 .lower_up = service_lower_up,
4293 .lower_down = service_lower_down,
4294 .ip_bound = service_ip_bound,
4295 .ip_release = service_ip_release,
4298 static void setup_ip4config(struct connman_service *service, int index)
4303 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4304 CONNMAN_IPCONFIG_TYPE_IPV4);
4305 if (service->ipconfig_ipv4 == NULL)
4308 connman_ipconfig_set_method(service->ipconfig_ipv4,
4309 CONNMAN_IPCONFIG_METHOD_DHCP);
4311 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4313 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4316 static void setup_ip6config(struct connman_service *service, int index)
4321 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4322 CONNMAN_IPCONFIG_TYPE_IPV6);
4323 if (service->ipconfig_ipv6 == NULL)
4326 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4328 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4331 void __connman_service_create_ip4config(struct connman_service *service,
4334 const char *ident = service->profile;
4337 DBG("ipv4 %p", service->ipconfig_ipv4);
4339 if (service->ipconfig_ipv4 != NULL)
4342 setup_ip4config(service, index);
4347 keyfile = __connman_storage_open_profile(ident);
4348 if (keyfile == NULL)
4351 if (service->ipconfig_ipv4)
4352 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4353 service->identifier, "IPv4.");
4354 g_key_file_free(keyfile);
4357 void __connman_service_create_ip6config(struct connman_service *service,
4360 const char *ident = service->profile;
4363 DBG("ipv6 %p", service->ipconfig_ipv6);
4365 if (service->ipconfig_ipv6 != NULL)
4368 setup_ip6config(service, index);
4373 keyfile = __connman_storage_open_profile(ident);
4374 if (keyfile == NULL)
4377 if (service->ipconfig_ipv6 != NULL)
4378 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4379 service->identifier, "IPv6.");
4381 g_key_file_free(keyfile);
4385 * __connman_service_lookup_from_network:
4386 * @network: network structure
4388 * Look up a service by network (reference count will not be increased)
4390 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4392 struct connman_service *service;
4393 const char *ident, *group;
4396 DBG("network %p", network);
4398 ident = __connman_network_get_ident(network);
4402 group = connman_network_get_group(network);
4406 name = g_strdup_printf("%s_%s_%s",
4407 __connman_network_get_type(network), ident, group);
4408 service = lookup_by_identifier(name);
4414 struct connman_service *__connman_service_lookup_from_index(int index)
4416 struct connman_service *service;
4417 GSequenceIter *iter;
4419 iter = g_sequence_get_begin_iter(service_list);
4421 while (g_sequence_iter_is_end(iter) == FALSE) {
4422 service = g_sequence_get(iter);
4424 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4428 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4432 iter = g_sequence_iter_next(iter);
4438 const char *__connman_service_get_ident(struct connman_service *service)
4440 return service->identifier;
4443 const char *__connman_service_get_path(struct connman_service *service)
4445 return service->path;
4448 unsigned int __connman_service_get_order(struct connman_service *service)
4450 GSequenceIter *iter;
4452 if (service == NULL)
4455 if (service->favorite == FALSE) {
4460 iter = g_hash_table_lookup(service_hash, service->identifier);
4462 if (g_sequence_iter_get_position(iter) == 0)
4464 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4465 service->order = 10;
4471 return service->order;
4474 static enum connman_service_type convert_network_type(struct connman_network *network)
4476 enum connman_network_type type = connman_network_get_type(network);
4479 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4480 case CONNMAN_NETWORK_TYPE_VENDOR:
4482 case CONNMAN_NETWORK_TYPE_ETHERNET:
4483 return CONNMAN_SERVICE_TYPE_ETHERNET;
4484 case CONNMAN_NETWORK_TYPE_WIFI:
4485 return CONNMAN_SERVICE_TYPE_WIFI;
4486 case CONNMAN_NETWORK_TYPE_WIMAX:
4487 return CONNMAN_SERVICE_TYPE_WIMAX;
4488 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4489 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4490 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4491 case CONNMAN_NETWORK_TYPE_CELLULAR:
4492 return CONNMAN_SERVICE_TYPE_CELLULAR;
4495 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4498 static enum connman_service_mode convert_wifi_mode(const char *mode)
4501 return CONNMAN_SERVICE_MODE_UNKNOWN;
4502 else if (g_str_equal(mode, "managed") == TRUE)
4503 return CONNMAN_SERVICE_MODE_MANAGED;
4504 else if (g_str_equal(mode, "adhoc") == TRUE)
4505 return CONNMAN_SERVICE_MODE_ADHOC;
4507 return CONNMAN_SERVICE_MODE_UNKNOWN;
4510 static enum connman_service_security convert_wifi_security(const char *security)
4512 if (security == NULL)
4513 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4514 else if (g_str_equal(security, "none") == TRUE)
4515 return CONNMAN_SERVICE_SECURITY_NONE;
4516 else if (g_str_equal(security, "wep") == TRUE)
4517 return CONNMAN_SERVICE_SECURITY_WEP;
4518 else if (g_str_equal(security, "psk") == TRUE)
4519 return CONNMAN_SERVICE_SECURITY_PSK;
4520 else if (g_str_equal(security, "ieee8021x") == TRUE)
4521 return CONNMAN_SERVICE_SECURITY_8021X;
4522 else if (g_str_equal(security, "wpa") == TRUE)
4523 return CONNMAN_SERVICE_SECURITY_WPA;
4524 else if (g_str_equal(security, "rsn") == TRUE)
4525 return CONNMAN_SERVICE_SECURITY_RSN;
4527 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4530 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
4535 return CONNMAN_SERVICE_MODE_GPRS;
4537 return CONNMAN_SERVICE_MODE_EDGE;
4542 return CONNMAN_SERVICE_MODE_UMTS;
4545 return CONNMAN_SERVICE_MODE_UNKNOWN;
4548 static void update_from_network(struct connman_service *service,
4549 struct connman_network *network)
4551 connman_uint8_t strength = service->strength;
4552 GSequenceIter *iter;
4555 DBG("service %p network %p", service, network);
4557 if (is_connected(service) == TRUE)
4560 if (is_connecting(service) == TRUE)
4563 str = connman_network_get_string(network, "Name");
4565 g_free(service->name);
4566 service->name = g_strdup(str);
4567 service->hidden = FALSE;
4569 g_free(service->name);
4570 service->name = NULL;
4571 service->hidden = TRUE;
4574 service->strength = connman_network_get_uint8(network, "Strength");
4575 service->roaming = connman_network_get_bool(network, "Roaming");
4577 if (service->strength == 0) {
4579 * Filter out 0-values; it's unclear what they mean
4580 * and they cause anomalous sorting of the priority list.
4582 service->strength = strength;
4585 str = connman_network_get_string(network, "WiFi.Mode");
4586 service->mode = convert_wifi_mode(str);
4588 str = connman_network_get_string(network, "WiFi.Security");
4589 service->security = convert_wifi_security(str);
4591 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
4592 connman_uint8_t value = connman_network_get_uint8(network,
4595 service->mode = convert_cellular_mode(value);
4596 } else if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4597 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4599 if (service->strength > strength && service->network != NULL) {
4600 connman_network_unref(service->network);
4601 service->network = connman_network_ref(network);
4603 strength_changed(service);
4606 if (service->network == NULL)
4607 service->network = connman_network_ref(network);
4609 iter = g_hash_table_lookup(service_hash, service->identifier);
4611 g_sequence_sort_changed(iter, service_compare, NULL);
4615 * __connman_service_create_from_network:
4616 * @network: network structure
4618 * Look up service by network and if not found, create one
4620 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4622 struct connman_service *service;
4623 struct connman_device *device;
4624 const char *ident, *group;
4628 DBG("network %p", network);
4630 ident = __connman_network_get_ident(network);
4634 group = connman_network_get_group(network);
4638 name = g_strdup_printf("%s_%s_%s",
4639 __connman_network_get_type(network), ident, group);
4640 service = service_get(name);
4643 if (service == NULL)
4646 if (__connman_network_get_weakness(network) == TRUE)
4649 if (service->path != NULL) {
4650 update_from_network(service, network);
4651 __connman_profile_changed(TRUE);
4655 service->type = convert_network_type(network);
4657 switch (service->type) {
4658 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4659 case CONNMAN_SERVICE_TYPE_SYSTEM:
4660 case CONNMAN_SERVICE_TYPE_ETHERNET:
4661 case CONNMAN_SERVICE_TYPE_WIMAX:
4662 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4663 case CONNMAN_SERVICE_TYPE_GPS:
4664 case CONNMAN_SERVICE_TYPE_VPN:
4665 case CONNMAN_SERVICE_TYPE_GADGET:
4666 service->autoconnect = FALSE;
4668 case CONNMAN_SERVICE_TYPE_WIFI:
4669 case CONNMAN_SERVICE_TYPE_CELLULAR:
4670 service->autoconnect = TRUE;
4674 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4676 update_from_network(service, network);
4678 index = connman_network_get_index(network);
4680 if (service->ipconfig_ipv4 == NULL)
4681 setup_ip4config(service, index);
4683 if (service->ipconfig_ipv6 == NULL)
4684 setup_ip6config(service, index);
4686 service_register(service);
4688 if (service->favorite == TRUE) {
4689 device = connman_network_get_device(service->network);
4690 if (device && __connman_device_scanning(device) == FALSE)
4691 __connman_service_auto_connect();
4697 void __connman_service_update_from_network(struct connman_network *network)
4699 struct connman_service *service;
4700 enum connman_service_mode mode;
4701 connman_uint8_t strength, value;
4702 connman_bool_t roaming;
4703 GSequenceIter *iter;
4705 connman_bool_t stats_enable;
4707 DBG("network %p", network);
4709 service = __connman_service_lookup_from_network(network);
4710 if (service == NULL)
4713 if (service->network == NULL)
4716 name = connman_network_get_string(service->network, "Name");
4717 if (g_strcmp0(service->name, name) != 0) {
4718 g_free(service->name);
4719 service->name = g_strdup(name);
4720 connman_dbus_property_changed_basic(service->path,
4721 CONNMAN_SERVICE_INTERFACE, "Name",
4722 DBUS_TYPE_STRING, &service->name);
4725 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4726 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4728 strength = connman_network_get_uint8(service->network, "Strength");
4729 if (strength == service->strength)
4732 service->strength = strength;
4734 strength_changed(service);
4737 roaming = connman_network_get_bool(service->network, "Roaming");
4738 if (roaming == service->roaming)
4741 stats_enable = stats_enabled(service);
4742 if (stats_enable == TRUE)
4743 stats_stop(service);
4745 service->roaming = roaming;
4747 if (stats_enable == TRUE)
4748 stats_start(service);
4750 roaming_changed(service);
4752 iter = g_hash_table_lookup(service_hash, service->identifier);
4754 g_sequence_sort_changed(iter, service_compare, NULL);
4757 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
4760 value = connman_network_get_uint8(service->network, "Cellular.Mode");
4761 mode = convert_cellular_mode(value);
4763 if (mode == service->mode)
4766 service->mode = mode;
4768 mode_changed(service);
4771 void __connman_service_remove_from_network(struct connman_network *network)
4773 struct connman_service *service;
4775 DBG("network %p", network);
4777 service = __connman_service_lookup_from_network(network);
4778 if (service == NULL)
4781 __connman_service_put(service);
4785 * __connman_service_create_from_provider:
4786 * @provider: provider structure
4788 * Look up service by provider and if not found, create one
4790 struct connman_service *
4791 __connman_service_create_from_provider(struct connman_provider *provider)
4793 struct connman_service *service;
4794 const char *ident, *str;
4796 int index = connman_provider_get_index(provider);
4798 DBG("provider %p", provider);
4800 ident = __connman_provider_get_ident(provider);
4804 name = g_strdup_printf("vpn_%s", ident);
4805 service = service_get(name);
4808 if (service == NULL)
4811 service->type = CONNMAN_SERVICE_TYPE_VPN;
4812 service->provider = connman_provider_ref(provider);
4813 service->autoconnect = FALSE;
4815 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4817 str = connman_provider_get_string(provider, "Name");
4819 g_free(service->name);
4820 service->name = g_strdup(str);
4821 service->hidden = FALSE;
4823 g_free(service->name);
4824 service->name = NULL;
4825 service->hidden = TRUE;
4828 service->strength = 0;
4830 if (service->ipconfig_ipv4 == NULL)
4831 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4832 CONNMAN_IPCONFIG_TYPE_IPV4);
4833 if (service->ipconfig_ipv4 == NULL)
4836 connman_ipconfig_set_method(service->ipconfig_ipv4,
4837 CONNMAN_IPCONFIG_METHOD_MANUAL);
4838 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4839 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4841 if (service->ipconfig_ipv6 == NULL)
4842 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4843 CONNMAN_IPCONFIG_TYPE_IPV6);
4844 if (service->ipconfig_ipv6 == NULL)
4847 connman_ipconfig_set_method(service->ipconfig_ipv6,
4848 CONNMAN_IPCONFIG_METHOD_OFF);
4849 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4850 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4852 service_register(service);
4857 static int service_load(struct connman_service *service)
4859 const char *ident = service->profile;
4861 GError *error = NULL;
4862 gchar *pathname, *data = NULL;
4865 connman_bool_t autoconnect;
4866 unsigned int ssid_len;
4869 DBG("service %p", service);
4874 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4875 if (pathname == NULL)
4878 keyfile = g_key_file_new();
4880 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
4887 if (g_key_file_load_from_data(keyfile, data, length,
4888 0, NULL) == FALSE) {
4895 switch (service->type) {
4896 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4897 case CONNMAN_SERVICE_TYPE_SYSTEM:
4898 case CONNMAN_SERVICE_TYPE_ETHERNET:
4899 case CONNMAN_SERVICE_TYPE_GPS:
4900 case CONNMAN_SERVICE_TYPE_VPN:
4901 case CONNMAN_SERVICE_TYPE_GADGET:
4903 case CONNMAN_SERVICE_TYPE_WIFI:
4904 if (service->name == NULL) {
4907 name = g_key_file_get_string(keyfile,
4908 service->identifier, "Name", NULL);
4910 g_free(service->name);
4911 service->name = name;
4914 if (service->network != NULL)
4915 connman_network_set_name(service->network,
4919 if (service->network &&
4920 connman_network_get_blob(service->network,
4921 "WiFi.SSID", &ssid_len) == NULL) {
4924 hex_ssid = g_key_file_get_string(keyfile,
4925 service->identifier,
4928 if (hex_ssid != NULL) {
4930 unsigned int i, j = 0, hex;
4931 size_t hex_ssid_len = strlen(hex_ssid);
4933 ssid = g_try_malloc0(hex_ssid_len / 2);
4940 for (i = 0; i < hex_ssid_len; i += 2) {
4941 sscanf(hex_ssid + i, "%02x", &hex);
4945 connman_network_set_blob(service->network,
4946 "WiFi.SSID", ssid, hex_ssid_len / 2);
4953 case CONNMAN_SERVICE_TYPE_WIMAX:
4954 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4955 case CONNMAN_SERVICE_TYPE_CELLULAR:
4956 service->favorite = g_key_file_get_boolean(keyfile,
4957 service->identifier, "Favorite", NULL);
4959 autoconnect = g_key_file_get_boolean(keyfile,
4960 service->identifier, "AutoConnect", &error);
4962 service->autoconnect = autoconnect;
4963 g_clear_error(&error);
4965 str = g_key_file_get_string(keyfile,
4966 service->identifier, "Failure", NULL);
4968 if (service->favorite == FALSE)
4969 service->state_ipv4 = service->state_ipv6 =
4970 CONNMAN_SERVICE_STATE_FAILURE;
4971 service->error = string2error(str);
4976 str = g_key_file_get_string(keyfile,
4977 service->identifier, "Modified", NULL);
4979 g_time_val_from_iso8601(str, &service->modified);
4983 str = g_key_file_get_string(keyfile,
4984 service->identifier, "Passphrase", NULL);
4986 g_free(service->passphrase);
4987 service->passphrase = str;
4990 if (service->ipconfig_ipv4 != NULL)
4991 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4992 service->identifier, "IPv4.");
4994 if (service->ipconfig_ipv6 != NULL)
4995 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4996 service->identifier, "IPv6.");
4998 service->nameservers_config = g_key_file_get_string_list(keyfile,
4999 service->identifier, "Nameservers", &length, NULL);
5000 if (service->nameservers_config != NULL && length == 0) {
5001 g_strfreev(service->nameservers_config);
5002 service->nameservers_config = NULL;
5005 service->domains = g_key_file_get_string_list(keyfile,
5006 service->identifier, "Domains", &length, NULL);
5007 if (service->domains != NULL && length == 0) {
5008 g_strfreev(service->domains);
5009 service->domains = NULL;
5012 str = g_key_file_get_string(keyfile,
5013 service->identifier, "Proxy.Method", NULL);
5015 service->proxy_config = string2proxymethod(str);
5019 service->proxies = g_key_file_get_string_list(keyfile,
5020 service->identifier, "Proxy.Servers", &length, NULL);
5021 if (service->proxies != NULL && length == 0) {
5022 g_strfreev(service->proxies);
5023 service->proxies = NULL;
5026 service->excludes = g_key_file_get_string_list(keyfile,
5027 service->identifier, "Proxy.Excludes", &length, NULL);
5028 if (service->excludes != NULL && length == 0) {
5029 g_strfreev(service->excludes);
5030 service->excludes = NULL;
5033 str = g_key_file_get_string(keyfile,
5034 service->identifier, "Proxy.URL", NULL);
5036 g_free(service->pac);
5041 g_key_file_free(keyfile);
5046 static int service_save(struct connman_service *service)
5048 const char *ident = service->profile;
5050 gchar *pathname, *data = NULL;
5053 const char *cst_str = NULL;
5056 DBG("service %p", service);
5061 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5062 if (pathname == NULL)
5065 keyfile = g_key_file_new();
5067 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5071 if (g_key_file_load_from_data(keyfile, data, length,
5079 if (service->name != NULL)
5080 g_key_file_set_string(keyfile, service->identifier,
5081 "Name", service->name);
5083 switch (service->type) {
5084 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5085 case CONNMAN_SERVICE_TYPE_SYSTEM:
5086 case CONNMAN_SERVICE_TYPE_ETHERNET:
5087 case CONNMAN_SERVICE_TYPE_GPS:
5088 case CONNMAN_SERVICE_TYPE_VPN:
5089 case CONNMAN_SERVICE_TYPE_GADGET:
5091 case CONNMAN_SERVICE_TYPE_WIFI:
5092 if (service->network) {
5093 const unsigned char *ssid;
5094 unsigned int ssid_len = 0;
5096 ssid = connman_network_get_blob(service->network,
5097 "WiFi.SSID", &ssid_len);
5099 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5100 char *identifier = service->identifier;
5104 str = g_string_sized_new(ssid_len * 2);
5110 for (i = 0; i < ssid_len; i++)
5111 g_string_append_printf(str,
5114 g_key_file_set_string(keyfile, identifier,
5117 g_string_free(str, TRUE);
5122 case CONNMAN_SERVICE_TYPE_WIMAX:
5123 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5124 case CONNMAN_SERVICE_TYPE_CELLULAR:
5125 g_key_file_set_boolean(keyfile, service->identifier,
5126 "Favorite", service->favorite);
5128 if (service->favorite == TRUE)
5129 g_key_file_set_boolean(keyfile, service->identifier,
5130 "AutoConnect", service->autoconnect);
5132 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5133 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5134 const char *failure = error2string(service->error);
5135 if (failure != NULL)
5136 g_key_file_set_string(keyfile,
5137 service->identifier,
5138 "Failure", failure);
5140 g_key_file_remove_key(keyfile, service->identifier,
5146 str = g_time_val_to_iso8601(&service->modified);
5148 g_key_file_set_string(keyfile, service->identifier,
5153 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5154 g_key_file_set_string(keyfile, service->identifier,
5155 "Passphrase", service->passphrase);
5157 g_key_file_remove_key(keyfile, service->identifier,
5158 "Passphrase", NULL);
5160 if (service->ipconfig_ipv4 != NULL)
5161 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5162 service->identifier, "IPv4.");
5164 if (service->ipconfig_ipv6 != NULL)
5165 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5166 service->identifier, "IPv6.");
5168 if (service->nameservers_config != NULL) {
5169 guint len = g_strv_length(service->nameservers_config);
5171 g_key_file_set_string_list(keyfile, service->identifier,
5173 (const gchar **) service->nameservers_config, len);
5175 g_key_file_remove_key(keyfile, service->identifier,
5176 "Nameservers", NULL);
5178 if (service->domains != NULL) {
5179 guint len = g_strv_length(service->domains);
5181 g_key_file_set_string_list(keyfile, service->identifier,
5183 (const gchar **) service->domains, len);
5185 g_key_file_remove_key(keyfile, service->identifier,
5188 cst_str = proxymethod2string(service->proxy_config);
5189 if (cst_str != NULL)
5190 g_key_file_set_string(keyfile, service->identifier,
5191 "Proxy.Method", cst_str);
5193 if (service->proxies != NULL) {
5194 guint len = g_strv_length(service->proxies);
5196 g_key_file_set_string_list(keyfile, service->identifier,
5198 (const gchar **) service->proxies, len);
5200 g_key_file_remove_key(keyfile, service->identifier,
5201 "Proxy.Servers", NULL);
5203 if (service->excludes != NULL) {
5204 guint len = g_strv_length(service->excludes);
5206 g_key_file_set_string_list(keyfile, service->identifier,
5208 (const gchar **) service->excludes, len);
5210 g_key_file_remove_key(keyfile, service->identifier,
5211 "Proxy.Excludes", NULL);
5213 if (service->pac != NULL && strlen(service->pac) > 0)
5214 g_key_file_set_string(keyfile, service->identifier,
5215 "Proxy.URL", service->pac);
5217 g_key_file_remove_key(keyfile, service->identifier,
5220 data = g_key_file_to_data(keyfile, &length, NULL);
5222 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5223 connman_error("Failed to store service information");
5228 g_key_file_free(keyfile);
5235 static struct connman_storage service_storage = {
5237 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5238 .service_load = service_load,
5239 .service_save = service_save,
5242 int __connman_service_init(void)
5246 connection = connman_dbus_get_connection();
5248 if (connman_storage_register(&service_storage) < 0)
5249 connman_error("Failed to register service storage");
5251 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5254 service_list = g_sequence_new(service_free);
5259 void __connman_service_cleanup(void)
5263 g_sequence_free(service_list);
5264 service_list = NULL;
5266 g_hash_table_destroy(service_hash);
5267 service_hash = NULL;
5269 g_slist_free(counter_list);
5270 counter_list = NULL;
5272 connman_storage_unregister(&service_storage);
5274 dbus_connection_unref(connection);