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 GSequence *__connman_service_get_list(struct connman_session *session,
1553 service_match_cb service_match)
1556 GSequenceIter *iter;
1558 list = g_sequence_new(NULL);
1562 iter = g_sequence_get_begin_iter(service_list);
1564 while (g_sequence_iter_is_end(iter) == FALSE) {
1565 service = g_sequence_get(iter);
1567 if (service_match(session, service) == TRUE)
1568 g_sequence_append(list, service);
1570 iter = g_sequence_iter_next(iter);
1576 connman_bool_t __connman_service_is_connecting(struct connman_service *service)
1578 return is_connecting(service);
1581 connman_bool_t __connman_service_is_connected(struct connman_service *service)
1583 return is_connected(service);
1586 const char *__connman_service_get_name(struct connman_service *service)
1588 return service->name;
1591 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1592 struct connman_service *service)
1594 dbus_bool_t required;
1597 str = __connman_service_type2string(service->type);
1599 connman_dbus_dict_append_basic(dict, "Type",
1600 DBUS_TYPE_STRING, &str);
1602 str = mode2string(service->mode);
1604 connman_dbus_dict_append_basic(dict, "Mode",
1605 DBUS_TYPE_STRING, &str);
1607 connman_dbus_dict_append_array(dict, "Security",
1608 DBUS_TYPE_STRING, append_security, service);
1610 str = state2string(combine_state(service->state_ipv4,
1611 service->state_ipv6));
1613 connman_dbus_dict_append_basic(dict, "State",
1614 DBUS_TYPE_STRING, &str);
1616 str = error2string(service->error);
1618 connman_dbus_dict_append_basic(dict, "Error",
1619 DBUS_TYPE_STRING, &str);
1621 if (service->strength > 0)
1622 connman_dbus_dict_append_basic(dict, "Strength",
1623 DBUS_TYPE_BYTE, &service->strength);
1625 connman_dbus_dict_append_basic(dict, "Favorite",
1626 DBUS_TYPE_BOOLEAN, &service->favorite);
1628 connman_dbus_dict_append_basic(dict, "Immutable",
1629 DBUS_TYPE_BOOLEAN, &service->immutable);
1631 if (service->favorite == TRUE)
1632 connman_dbus_dict_append_basic(dict, "AutoConnect",
1633 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1635 connman_dbus_dict_append_basic(dict, "AutoConnect",
1636 DBUS_TYPE_BOOLEAN, &service->favorite);
1638 if (service->name != NULL)
1639 connman_dbus_dict_append_basic(dict, "Name",
1640 DBUS_TYPE_STRING, &service->name);
1642 connman_dbus_dict_append_basic(dict, "LoginRequired",
1643 DBUS_TYPE_BOOLEAN, &service->login_required);
1645 switch (service->type) {
1646 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1647 case CONNMAN_SERVICE_TYPE_SYSTEM:
1648 case CONNMAN_SERVICE_TYPE_GPS:
1649 case CONNMAN_SERVICE_TYPE_VPN:
1650 case CONNMAN_SERVICE_TYPE_GADGET:
1652 case CONNMAN_SERVICE_TYPE_CELLULAR:
1653 connman_dbus_dict_append_basic(dict, "Roaming",
1654 DBUS_TYPE_BOOLEAN, &service->roaming);
1657 connman_dbus_dict_append_basic(dict, "SetupRequired",
1658 DBUS_TYPE_BOOLEAN, &required);
1659 connman_dbus_dict_append_dict(dict, "Ethernet",
1660 append_ethernet, service);
1662 case CONNMAN_SERVICE_TYPE_WIFI:
1663 if (service->passphrase != NULL && limited == FALSE)
1664 connman_dbus_dict_append_basic(dict, "Passphrase",
1665 DBUS_TYPE_STRING, &service->passphrase);
1669 switch (service->security) {
1670 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1671 case CONNMAN_SERVICE_SECURITY_NONE:
1673 case CONNMAN_SERVICE_SECURITY_WEP:
1674 case CONNMAN_SERVICE_SECURITY_PSK:
1675 case CONNMAN_SERVICE_SECURITY_WPA:
1676 case CONNMAN_SERVICE_SECURITY_RSN:
1677 if (service->passphrase == NULL)
1680 case CONNMAN_SERVICE_SECURITY_8021X:
1684 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1685 DBUS_TYPE_BOOLEAN, &required);
1688 case CONNMAN_SERVICE_TYPE_ETHERNET:
1689 case CONNMAN_SERVICE_TYPE_WIMAX:
1690 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1691 connman_dbus_dict_append_dict(dict, "Ethernet",
1692 append_ethernet, service);
1696 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1698 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1699 append_ipv4config, service);
1701 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1703 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1704 append_ipv6config, service);
1706 connman_dbus_dict_append_array(dict, "Nameservers",
1707 DBUS_TYPE_STRING, append_dns, service);
1709 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1710 DBUS_TYPE_STRING, append_dnsconfig, service);
1712 connman_dbus_dict_append_array(dict, "Domains",
1713 DBUS_TYPE_STRING, append_domain, service);
1715 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1716 DBUS_TYPE_STRING, append_domainconfig, service);
1718 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1720 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1721 append_proxyconfig, service);
1723 connman_dbus_dict_append_dict(dict, "Provider",
1724 append_provider, service);
1727 static void append_struct(gpointer value, gpointer user_data)
1729 struct connman_service *service = value;
1730 DBusMessageIter *iter = user_data;
1731 DBusMessageIter entry, dict;
1733 if (service->path == NULL || service->hidden == TRUE)
1736 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1738 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1741 connman_dbus_dict_open(&entry, &dict);
1742 append_properties(&dict, TRUE, service);
1743 connman_dbus_dict_close(&entry, &dict);
1745 dbus_message_iter_close_container(iter, &entry);
1748 void __connman_service_list_struct(DBusMessageIter *iter)
1750 g_sequence_foreach(service_list, append_struct, iter);
1753 int __connman_service_get_index(struct connman_service *service)
1755 if (service == NULL)
1758 if (service->network != NULL)
1759 return connman_network_get_index(service->network);
1760 else if (service->provider != NULL)
1761 return connman_provider_get_index(service->provider);
1766 void __connman_service_set_domainname(struct connman_service *service,
1767 const char *domainname)
1769 if (service == NULL)
1772 g_free(service->domainname);
1773 service->domainname = g_strdup(domainname);
1775 domain_changed(service);
1778 const char *connman_service_get_domainname(struct connman_service *service)
1780 if (service == NULL)
1783 if (service->domains != NULL)
1784 return service->domains[0];
1786 return service->domainname;
1789 char **connman_service_get_nameservers(struct connman_service *service)
1791 if (service == NULL)
1794 if (service->nameservers_config != NULL)
1795 return service->nameservers_config;
1796 else if (service->nameservers != NULL)
1797 return service->nameservers;
1802 void connman_service_set_proxy_method(struct connman_service *service,
1803 enum connman_service_proxy_method method)
1805 if (service == NULL)
1808 service->proxy = method;
1810 proxy_changed(service);
1812 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1813 __connman_notifier_proxy_changed(service);
1816 enum connman_service_proxy_method connman_service_get_proxy_method(
1817 struct connman_service *service)
1819 if (service == NULL)
1820 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1822 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1823 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1824 service->pac == NULL)
1825 return service->proxy;
1827 return service->proxy_config;
1830 return service->proxy;
1833 char **connman_service_get_proxy_servers(struct connman_service *service)
1835 return g_strdupv(service->proxies);
1838 char **connman_service_get_proxy_excludes(struct connman_service *service)
1840 return g_strdupv(service->excludes);
1843 const char *connman_service_get_proxy_url(struct connman_service *service)
1845 if (service == NULL)
1848 return service->pac;
1851 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1854 if (service == NULL)
1857 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1859 if (service->ipconfig_ipv4) {
1860 if (__connman_ipconfig_set_proxy_autoconfig(
1861 service->ipconfig_ipv4, url) < 0)
1863 } else if (service->ipconfig_ipv6) {
1864 if (__connman_ipconfig_set_proxy_autoconfig(
1865 service->ipconfig_ipv6, url) < 0)
1870 proxy_changed(service);
1872 __connman_notifier_proxy_changed(service);
1875 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1877 if (service == NULL)
1880 if (service->ipconfig_ipv4)
1881 return __connman_ipconfig_get_proxy_autoconfig(
1882 service->ipconfig_ipv4);
1883 else if (service->ipconfig_ipv6)
1884 return __connman_ipconfig_get_proxy_autoconfig(
1885 service->ipconfig_ipv6);
1889 static void update_timeservers(struct connman_service *service)
1893 if (service->timeservers == NULL)
1896 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
1897 case CONNMAN_SERVICE_STATE_UNKNOWN:
1898 case CONNMAN_SERVICE_STATE_IDLE:
1899 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1900 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1902 case CONNMAN_SERVICE_STATE_FAILURE:
1903 case CONNMAN_SERVICE_STATE_DISCONNECT:
1904 for (i = 0; service->timeservers[i] != NULL; i++)
1905 connman_timeserver_remove(service->timeservers[i]);
1907 case CONNMAN_SERVICE_STATE_READY:
1908 case CONNMAN_SERVICE_STATE_ONLINE:
1912 for (i = 0; service->timeservers[i] != NULL; i++)
1913 connman_timeserver_append(service->timeservers[i]);
1916 int __connman_service_timeserver_append(struct connman_service *service,
1917 const char *timeserver)
1921 DBG("service %p timeserver %s", service, timeserver);
1923 if (timeserver == NULL)
1926 if (service->timeservers != NULL) {
1929 for (i = 0; service->timeservers[i] != NULL; i++)
1930 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1933 len = g_strv_length(service->timeservers);
1934 service->timeservers = g_try_renew(char *, service->timeservers,
1938 service->timeservers = g_try_new0(char *, len + 2);
1941 if (service->timeservers == NULL)
1944 service->timeservers[len] = g_strdup(timeserver);
1945 service->timeservers[len + 1] = NULL;
1947 update_timeservers(service);
1952 int __connman_service_timeserver_remove(struct connman_service *service,
1953 const char *timeserver)
1958 DBG("service %p timeserver %s", service, timeserver);
1960 if (timeserver == NULL)
1963 if (service->timeservers == NULL)
1966 len = g_strv_length(service->timeservers);
1968 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
1971 g_strfreev(service->timeservers);
1972 service->timeservers = NULL;
1977 servers = g_try_new0(char *, len - 1);
1978 if (servers == NULL)
1981 for (i = 0, j = 0; i < len; i++) {
1982 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
1983 servers[j] = g_strdup(service->timeservers[i]);
1987 servers[len - 2] = NULL;
1989 g_strfreev(service->timeservers);
1990 service->timeservers = servers;
1992 update_timeservers(service);
1997 void __connman_service_set_pac(struct connman_service *service,
2003 g_free(service->pac);
2004 service->pac = g_strdup(pac);
2007 void __connman_service_set_passphrase(struct connman_service *service,
2008 const char* passphrase)
2010 if (service->immutable == TRUE)
2013 g_free(service->passphrase);
2014 service->passphrase = g_strdup(passphrase);
2016 passphrase_changed(service);
2018 if (service->network != NULL)
2019 connman_network_set_string(service->network,
2021 service->passphrase);
2023 __connman_storage_save_service(service);
2026 static DBusMessage *get_properties(DBusConnection *conn,
2027 DBusMessage *msg, void *user_data)
2029 struct connman_service *service = user_data;
2031 DBusMessageIter array, dict;
2033 DBG("service %p", service);
2035 reply = dbus_message_new_method_return(msg);
2039 dbus_message_iter_init_append(reply, &array);
2041 connman_dbus_dict_open(&array, &dict);
2042 append_properties(&dict, FALSE, service);
2043 connman_dbus_dict_close(&array, &dict);
2048 static int update_proxy_configuration(struct connman_service *service,
2049 DBusMessageIter *array)
2051 DBusMessageIter dict;
2052 enum connman_service_proxy_method method;
2053 GString *servers_str = NULL;
2054 GString *excludes_str = NULL;
2055 const char *url = NULL;
2057 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2059 dbus_message_iter_recurse(array, &dict);
2061 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2062 DBusMessageIter entry, variant;
2066 dbus_message_iter_recurse(&dict, &entry);
2068 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2071 dbus_message_iter_get_basic(&entry, &key);
2072 dbus_message_iter_next(&entry);
2074 if (dbus_message_iter_get_arg_type(&entry) !=
2078 dbus_message_iter_recurse(&entry, &variant);
2079 type = dbus_message_iter_get_arg_type(&variant);
2081 if (g_str_equal(key, "Method") == TRUE) {
2084 if (type != DBUS_TYPE_STRING)
2087 dbus_message_iter_get_basic(&variant, &val);
2088 method = string2proxymethod(val);
2089 } else if (g_str_equal(key, "URL") == TRUE) {
2090 if (type != DBUS_TYPE_STRING)
2093 dbus_message_iter_get_basic(&variant, &url);
2094 } else if (g_str_equal(key, "Servers") == TRUE) {
2095 DBusMessageIter str_array;
2097 if (type != DBUS_TYPE_ARRAY)
2100 servers_str = g_string_new(NULL);
2101 if (servers_str == NULL)
2104 dbus_message_iter_recurse(&variant, &str_array);
2106 while (dbus_message_iter_get_arg_type(&str_array) ==
2110 dbus_message_iter_get_basic(&str_array, &val);
2112 if (servers_str->len > 0)
2113 g_string_append_printf(servers_str,
2116 g_string_append(servers_str, val);
2118 dbus_message_iter_next(&str_array);
2120 } else if (g_str_equal(key, "Excludes") == TRUE) {
2121 DBusMessageIter str_array;
2123 if (type != DBUS_TYPE_ARRAY)
2126 excludes_str = g_string_new(NULL);
2127 if (excludes_str == NULL)
2130 dbus_message_iter_recurse(&variant, &str_array);
2132 while (dbus_message_iter_get_arg_type(&str_array) ==
2136 dbus_message_iter_get_basic(&str_array, &val);
2138 if (excludes_str->len > 0)
2139 g_string_append_printf(excludes_str,
2142 g_string_append(excludes_str, val);
2144 dbus_message_iter_next(&str_array);
2148 dbus_message_iter_next(&dict);
2152 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2154 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2155 if (servers_str == NULL && service->proxies == NULL)
2158 if (servers_str != NULL) {
2159 g_strfreev(service->proxies);
2161 if (servers_str->len > 0)
2162 service->proxies = g_strsplit_set(
2163 servers_str->str, " ", 0);
2165 service->proxies = NULL;
2168 if (excludes_str != NULL) {
2169 g_strfreev(service->excludes);
2171 if (excludes_str->len > 0)
2172 service->excludes = g_strsplit_set(
2173 excludes_str->str, " ", 0);
2175 service->excludes = NULL;
2178 if (service->proxies == NULL)
2179 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2182 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2183 g_free(service->pac);
2185 if (url != NULL && strlen(url) > 0)
2186 service->pac = g_strdup(url);
2188 service->pac = NULL;
2190 /* if we are connected:
2191 - if service->pac == NULL
2192 - if __connman_ipconfig_get_proxy_autoconfig(
2193 service->ipconfig) == NULL
2194 --> We should start WPAD */
2197 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2201 if (servers_str != NULL)
2202 g_string_free(servers_str, TRUE);
2204 if (excludes_str != NULL)
2205 g_string_free(excludes_str, TRUE);
2207 service->proxy_config = method;
2212 if (servers_str != NULL)
2213 g_string_free(servers_str, TRUE);
2215 if (excludes_str != NULL)
2216 g_string_free(excludes_str, TRUE);
2221 static int set_ipconfig(struct connman_service *service,
2222 struct connman_ipconfig *ipconfig,
2223 DBusMessageIter *array,
2224 enum connman_service_state state,
2225 enum connman_service_state *new_state)
2227 enum connman_ipconfig_method old_method;
2228 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2229 enum connman_ipconfig_type type;
2232 old_method = __connman_ipconfig_get_method(ipconfig);
2234 if (is_connecting_state(service, state) ||
2235 is_connected_state(service, state))
2236 __connman_network_clear_ipconfig(service->network, ipconfig);
2238 err = __connman_ipconfig_set_config(ipconfig, array);
2239 method = __connman_ipconfig_get_method(ipconfig);
2240 type = __connman_ipconfig_get_config_type(ipconfig);
2242 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2243 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2244 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2245 *new_state = service->state_ipv4 =
2246 CONNMAN_SERVICE_STATE_CONFIGURATION;
2247 __connman_ipconfig_enable(ipconfig);
2250 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2251 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2252 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2253 *new_state = service->state_ipv6;
2254 __connman_ipconfig_enable(ipconfig);
2258 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2259 type, method, state2string(*new_state));
2264 static DBusMessage *set_property(DBusConnection *conn,
2265 DBusMessage *msg, void *user_data)
2267 struct connman_service *service = user_data;
2268 DBusMessageIter iter, value;
2272 DBG("service %p", service);
2274 if (dbus_message_iter_init(msg, &iter) == FALSE)
2275 return __connman_error_invalid_arguments(msg);
2277 dbus_message_iter_get_basic(&iter, &name);
2278 dbus_message_iter_next(&iter);
2279 dbus_message_iter_recurse(&iter, &value);
2281 type = dbus_message_iter_get_arg_type(&value);
2283 if (g_str_equal(name, "AutoConnect") == TRUE) {
2284 connman_bool_t autoconnect;
2286 if (type != DBUS_TYPE_BOOLEAN)
2287 return __connman_error_invalid_arguments(msg);
2289 if (service->favorite == FALSE)
2290 return __connman_error_invalid_service(msg);
2292 dbus_message_iter_get_basic(&value, &autoconnect);
2294 if (service->autoconnect == autoconnect)
2295 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2297 service->autoconnect = autoconnect;
2299 autoconnect_changed(service);
2301 __connman_storage_save_service(service);
2302 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2303 const char *passphrase;
2305 if (type != DBUS_TYPE_STRING)
2306 return __connman_error_invalid_arguments(msg);
2308 if (service->immutable == TRUE)
2309 return __connman_error_not_supported(msg);
2311 dbus_message_iter_get_basic(&value, &passphrase);
2313 __connman_service_set_passphrase(service, passphrase);
2314 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2315 DBusMessageIter entry;
2320 if (type != DBUS_TYPE_ARRAY)
2321 return __connman_error_invalid_arguments(msg);
2323 str = g_string_new(NULL);
2325 return __connman_error_invalid_arguments(msg);
2327 index = connman_network_get_index(service->network);
2328 gw = __connman_ipconfig_get_gateway_from_index(index);
2330 if (gw && strlen(gw))
2331 __connman_service_nameserver_del_routes(service);
2333 dbus_message_iter_recurse(&value, &entry);
2335 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2337 dbus_message_iter_get_basic(&entry, &val);
2338 dbus_message_iter_next(&entry);
2340 g_string_append_printf(str, " %s", val);
2342 g_string_append(str, val);
2345 g_strfreev(service->nameservers_config);
2348 service->nameservers_config =
2349 g_strsplit_set(str->str, " ", 0);
2351 service->nameservers_config = NULL;
2354 g_string_free(str, TRUE);
2356 if (gw && strlen(gw))
2357 __connman_service_nameserver_add_routes(service, gw);
2359 update_nameservers(service);
2360 dns_configuration_changed(service);
2362 __connman_storage_save_service(service);
2363 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2364 DBusMessageIter entry;
2367 if (type != DBUS_TYPE_ARRAY)
2368 return __connman_error_invalid_arguments(msg);
2370 str = g_string_new(NULL);
2372 return __connman_error_invalid_arguments(msg);
2374 dbus_message_iter_recurse(&value, &entry);
2376 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2378 dbus_message_iter_get_basic(&entry, &val);
2379 dbus_message_iter_next(&entry);
2381 g_string_append_printf(str, " %s", val);
2383 g_string_append(str, val);
2386 g_strfreev(service->domains);
2389 service->domains = g_strsplit_set(str->str, " ", 0);
2391 service->domains = NULL;
2393 g_string_free(str, TRUE);
2395 update_nameservers(service);
2396 domain_configuration_changed(service);
2398 __connman_storage_save_service(service);
2399 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2402 if (type != DBUS_TYPE_ARRAY)
2403 return __connman_error_invalid_arguments(msg);
2405 err = update_proxy_configuration(service, &value);
2408 return __connman_error_failed(msg, -err);
2410 proxy_configuration_changed(service);
2412 __connman_storage_save_service(service);
2413 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2414 g_str_equal(name, "IPv6.Configuration")) {
2416 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2417 enum connman_service_state state =
2418 CONNMAN_SERVICE_STATE_UNKNOWN;
2423 if (service->ipconfig_ipv4 == NULL &&
2424 service->ipconfig_ipv6 == NULL)
2425 return __connman_error_invalid_property(msg);
2427 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2428 ipv4 = service->ipconfig_ipv4;
2429 err = set_ipconfig(service, ipv4, &value,
2430 service->state_ipv4, &state);
2432 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2433 ipv6 = service->ipconfig_ipv6;
2434 err = set_ipconfig(service, ipv6, &value,
2435 service->state_ipv6, &state);
2439 if (is_connected_state(service, state) ||
2440 is_connecting_state(service, state))
2441 __connman_network_set_ipconfig(service->network,
2443 return __connman_error_failed(msg, -err);
2447 ipv4_configuration_changed(service);
2449 ipv6_configuration_changed(service);
2451 if (is_connecting(service) || is_connected(service))
2452 __connman_network_set_ipconfig(service->network,
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 void set_idle(struct connman_service *service)
2464 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
2465 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2466 state_changed(service);
2469 static DBusMessage *clear_property(DBusConnection *conn,
2470 DBusMessage *msg, void *user_data)
2472 struct connman_service *service = user_data;
2475 DBG("service %p", service);
2477 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2480 if (g_str_equal(name, "Error") == TRUE) {
2483 g_get_current_time(&service->modified);
2484 __connman_storage_save_service(service);
2485 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2486 if (service->immutable == TRUE)
2487 return __connman_error_not_supported(msg);
2489 g_free(service->passphrase);
2490 service->passphrase = NULL;
2492 passphrase_changed(service);
2494 __connman_storage_save_service(service);
2496 return __connman_error_invalid_property(msg);
2498 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2501 static connman_bool_t is_ignore(struct connman_service *service)
2503 if (service->autoconnect == FALSE)
2506 if (service->roaming == TRUE)
2509 if (service->ignore == TRUE)
2512 if (combine_state(service->state_ipv4, service->state_ipv6) ==
2513 CONNMAN_SERVICE_STATE_FAILURE)
2519 void __connman_service_auto_connect(void)
2521 struct connman_service *service = NULL;
2522 GSequenceIter *iter;
2526 if (__connman_session_mode() == TRUE) {
2527 DBG("Session mode enabled: auto connect disabled");
2531 iter = g_sequence_get_begin_iter(service_list);
2533 while (g_sequence_iter_is_end(iter) == FALSE) {
2534 service = g_sequence_get(iter);
2536 if (service->pending != NULL)
2539 if (is_connecting(service) == TRUE)
2542 if (service->favorite == FALSE)
2545 if (is_connected(service) == TRUE)
2548 if (is_ignore(service) == FALSE &&
2549 combine_state(service->state_ipv4,
2550 service->state_ipv6) ==
2551 CONNMAN_SERVICE_STATE_IDLE)
2556 iter = g_sequence_iter_next(iter);
2559 if (service != NULL) {
2560 service->userconnect = FALSE;
2561 __connman_service_connect(service);
2565 static void remove_timeout(struct connman_service *service)
2567 if (service->timeout > 0) {
2568 g_source_remove(service->timeout);
2569 service->timeout = 0;
2573 static void reply_pending(struct connman_service *service, int error)
2575 remove_timeout(service);
2577 if (service->pending != NULL) {
2581 reply = __connman_error_failed(service->pending,
2584 g_dbus_send_message(connection, reply);
2588 sender = dbus_message_get_interface(service->pending);
2590 DBG("sender %s", sender);
2592 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2593 g_dbus_send_reply(connection, service->pending,
2594 DBUS_TYPE_OBJECT_PATH, &service->path,
2597 g_dbus_send_reply(connection, service->pending,
2601 dbus_message_unref(service->pending);
2602 service->pending = NULL;
2606 static gboolean connect_timeout(gpointer user_data)
2608 struct connman_service *service = user_data;
2609 connman_bool_t autoconnect = FALSE;
2611 DBG("service %p", service);
2613 service->timeout = 0;
2615 if (service->network != NULL)
2616 __connman_network_disconnect(service->network);
2618 __connman_ipconfig_disable(service->ipconfig_ipv4);
2619 __connman_ipconfig_disable(service->ipconfig_ipv6);
2621 __connman_stats_service_unregister(service);
2623 if (service->pending != NULL) {
2626 reply = __connman_error_operation_timeout(service->pending);
2628 g_dbus_send_message(connection, reply);
2630 dbus_message_unref(service->pending);
2631 service->pending = NULL;
2635 __connman_service_indicate_state(service,
2636 CONNMAN_SERVICE_STATE_FAILURE,
2637 CONNMAN_IPCONFIG_TYPE_IPV4);
2638 __connman_service_indicate_state(service,
2639 CONNMAN_SERVICE_STATE_FAILURE,
2640 CONNMAN_IPCONFIG_TYPE_IPV6);
2642 if (autoconnect == TRUE && service->userconnect == FALSE)
2643 __connman_service_auto_connect();
2648 static void set_reconnect_state(struct connman_service *service,
2649 connman_bool_t reconnect)
2651 struct connman_device *device;
2653 if (service->network == NULL)
2656 device = connman_network_get_device(service->network);
2660 __connman_device_set_reconnect(device, reconnect);
2663 static connman_bool_t get_reconnect_state(struct connman_service *service)
2665 struct connman_device *device;
2667 if (service->network == NULL)
2670 device = connman_network_get_device(service->network);
2674 return __connman_device_get_reconnect(device);
2677 static void request_input_cb (struct connman_service *service,
2678 const char *passphrase, void *user_data)
2680 DBG ("RequestInput return, %p", service);
2682 if (passphrase == NULL && service->wps == FALSE)
2685 if (passphrase != NULL)
2686 __connman_service_set_passphrase(service, passphrase);
2688 __connman_service_connect(service);
2691 static DBusMessage *connect_service(DBusConnection *conn,
2692 DBusMessage *msg, void *user_data)
2694 struct connman_service *service = user_data;
2695 GSequenceIter *iter;
2698 DBG("service %p", service);
2700 if (service->pending != NULL)
2701 return __connman_error_in_progress(msg);
2703 iter = g_sequence_get_begin_iter(service_list);
2705 while (g_sequence_iter_is_end(iter) == FALSE) {
2706 struct connman_service *temp = g_sequence_get(iter);
2708 if (service->type == temp->type && is_connecting(temp) == TRUE)
2709 return __connman_error_in_progress(msg);
2711 iter = g_sequence_iter_next(iter);
2714 service->ignore = FALSE;
2716 service->userconnect = TRUE;
2718 service->pending = dbus_message_ref(msg);
2720 set_reconnect_state(service, FALSE);
2722 err = __connman_service_connect(service);
2724 if (err == -ENOKEY) {
2725 if (__connman_agent_request_input(service,
2731 if (service->pending == NULL)
2734 if (err != -EINPROGRESS) {
2735 dbus_message_unref(service->pending);
2736 service->pending = NULL;
2738 return __connman_error_failed(msg, -err);
2744 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2747 static DBusMessage *disconnect_service(DBusConnection *conn,
2748 DBusMessage *msg, void *user_data)
2750 struct connman_service *service = user_data;
2753 DBG("service %p", service);
2755 reply_pending(service, ECONNABORTED);
2757 service->ignore = TRUE;
2759 set_reconnect_state(service, FALSE);
2761 err = __connman_service_disconnect(service);
2763 if (err != -EINPROGRESS)
2764 return __connman_error_failed(msg, -err);
2769 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2772 static DBusMessage *remove_service(DBusConnection *conn,
2773 DBusMessage *msg, void *user_data)
2775 struct connman_service *service = user_data;
2777 DBG("service %p", service);
2779 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2780 return __connman_error_not_supported(msg);
2782 if (service->immutable == TRUE)
2783 return __connman_error_not_supported(msg);
2785 if (service->favorite == FALSE &&
2786 combine_state(service->state_ipv4, service->state_ipv6) !=
2787 CONNMAN_SERVICE_STATE_FAILURE)
2788 return __connman_error_not_supported(msg);
2790 if (service->network != NULL) {
2791 set_reconnect_state(service, FALSE);
2793 __connman_network_disconnect(service->network);
2796 g_free(service->passphrase);
2797 service->passphrase = NULL;
2799 passphrase_changed(service);
2803 __connman_service_set_favorite(service, FALSE);
2804 __connman_storage_save_service(service);
2806 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2809 static DBusMessage *move_service(DBusConnection *conn,
2810 DBusMessage *msg, void *user_data,
2813 struct connman_service *service = user_data;
2814 struct connman_service *target;
2816 GSequenceIter *src, *dst;
2818 DBG("service %p", service);
2820 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2823 if (service->favorite == FALSE)
2824 return __connman_error_not_supported(msg);
2826 target = find_service(path);
2827 if (target == NULL || target->favorite == FALSE || target == service)
2828 return __connman_error_invalid_service(msg);
2830 DBG("target %s", target->identifier);
2832 if (target->state_ipv4 != service->state_ipv4 &&
2833 target->state_ipv6 != service->state_ipv6)
2834 return __connman_error_invalid_service(msg);
2836 g_get_current_time(&service->modified);
2837 __connman_storage_save_service(service);
2839 src = g_hash_table_lookup(service_hash, service->identifier);
2840 dst = g_hash_table_lookup(service_hash, target->identifier);
2842 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2844 __connman_profile_changed(FALSE);
2846 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2849 static DBusMessage *move_before(DBusConnection *conn,
2850 DBusMessage *msg, void *user_data)
2852 return move_service(conn, msg, user_data, TRUE);
2855 static DBusMessage *move_after(DBusConnection *conn,
2856 DBusMessage *msg, void *user_data)
2858 return move_service(conn, msg, user_data, FALSE);
2861 static DBusMessage *reset_counters(DBusConnection *conn,
2862 DBusMessage *msg, void *user_data)
2864 struct connman_service *service = user_data;
2866 reset_stats(service);
2868 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2871 static GDBusMethodTable service_methods[] = {
2872 { "GetProperties", "", "a{sv}", get_properties },
2873 { "SetProperty", "sv", "", set_property },
2874 { "ClearProperty", "s", "", clear_property },
2875 { "Connect", "", "", connect_service,
2876 G_DBUS_METHOD_FLAG_ASYNC },
2877 { "Disconnect", "", "", disconnect_service },
2878 { "Remove", "", "", remove_service },
2879 { "MoveBefore", "o", "", move_before },
2880 { "MoveAfter", "o", "", move_after },
2881 { "ResetCounters", "", "", reset_counters },
2885 static GDBusSignalTable service_signals[] = {
2886 { "PropertyChanged", "sv" },
2890 static void service_free(gpointer user_data)
2892 struct connman_service *service = user_data;
2893 char *path = service->path;
2895 DBG("service %p", service);
2897 reply_pending(service, ENOENT);
2899 g_hash_table_remove(service_hash, service->identifier);
2901 stats_stop(service);
2902 __connman_storage_save_service(service);
2904 service->path = NULL;
2907 __connman_profile_changed(FALSE);
2909 g_dbus_unregister_interface(connection, path,
2910 CONNMAN_SERVICE_INTERFACE);
2914 g_hash_table_destroy(service->counter_table);
2916 if (service->network != NULL)
2917 connman_network_unref(service->network);
2919 if (service->provider != NULL)
2920 connman_provider_unref(service->provider);
2922 if (service->ipconfig_ipv4 != NULL) {
2923 connman_ipconfig_unref(service->ipconfig_ipv4);
2924 service->ipconfig_ipv4 = NULL;
2927 if (service->ipconfig_ipv6 != NULL) {
2928 connman_ipconfig_unref(service->ipconfig_ipv6);
2929 service->ipconfig_ipv6 = NULL;
2932 if (service->location != NULL)
2933 connman_location_unref(service->location);
2935 g_strfreev(service->nameservers);
2936 g_strfreev(service->nameservers_config);
2937 g_strfreev(service->domains);
2938 g_strfreev(service->proxies);
2939 g_strfreev(service->excludes);
2941 g_free(service->domainname);
2942 g_free(service->pac);
2943 g_free(service->profile);
2944 g_free(service->name);
2945 g_free(service->passphrase);
2946 g_free(service->identifier);
2947 g_free(service->eap);
2948 g_free(service->identity);
2949 g_free(service->ca_cert_file);
2950 g_free(service->client_cert_file);
2951 g_free(service->private_key_file);
2952 g_free(service->private_key_passphrase);
2953 g_free(service->phase2);
2955 if (service->stats.timer != NULL)
2956 g_timer_destroy(service->stats.timer);
2957 if (service->stats_roaming.timer != NULL)
2958 g_timer_destroy(service->stats_roaming.timer);
2964 * __connman_service_put:
2965 * @service: service structure
2967 * Release service if no longer needed
2969 void __connman_service_put(struct connman_service *service)
2971 DBG("service %p", service);
2973 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2974 GSequenceIter *iter;
2976 iter = g_hash_table_lookup(service_hash, service->identifier);
2978 reply_pending(service, ECONNABORTED);
2980 __connman_service_disconnect(service);
2982 g_sequence_remove(iter);
2984 service_free(service);
2988 static void stats_init(struct connman_service *service)
2991 service->stats.valid = FALSE;
2992 service->stats.enabled = FALSE;
2993 service->stats.timer = g_timer_new();
2996 service->stats_roaming.valid = FALSE;
2997 service->stats_roaming.enabled = FALSE;
2998 service->stats_roaming.timer = g_timer_new();
3001 static void service_initialize(struct connman_service *service)
3003 DBG("service %p", service);
3005 service->refcount = 1;
3007 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3008 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
3009 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3011 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3012 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3014 service->favorite = FALSE;
3015 service->immutable = FALSE;
3016 service->hidden = FALSE;
3018 service->ignore = FALSE;
3020 service->userconnect = FALSE;
3024 stats_init(service);
3026 service->provider = NULL;
3028 service->wps = FALSE;
3032 * connman_service_create:
3034 * Allocate a new service.
3036 * Returns: a newly-allocated #connman_service structure
3038 struct connman_service *connman_service_create(void)
3041 struct connman_stats_counter *counters;
3042 const char *counter;
3044 struct connman_service *service;
3046 service = g_try_new0(struct connman_service, 1);
3047 if (service == NULL)
3050 DBG("service %p", service);
3052 service->counter_table = g_hash_table_new_full(g_str_hash,
3053 g_str_equal, NULL, g_free);
3055 for (list = counter_list; list; list = list->next) {
3056 counter = list->data;
3058 counters = g_try_new0(struct connman_stats_counter, 1);
3059 if (counters == NULL) {
3060 g_hash_table_destroy(service->counter_table);
3065 counters->append_all = TRUE;
3067 g_hash_table_replace(service->counter_table, (gpointer)counter,
3071 service_initialize(service);
3073 service->location = __connman_location_create(service);
3078 struct connman_location *__connman_service_get_location(struct connman_service *service)
3080 return service->location;
3084 * connman_service_ref:
3085 * @service: service structure
3087 * Increase reference counter of service
3089 struct connman_service *connman_service_ref(struct connman_service *service)
3093 g_atomic_int_inc(&service->refcount);
3099 * connman_service_unref:
3100 * @service: service structure
3102 * Decrease reference counter of service
3104 void connman_service_unref(struct connman_service *service)
3106 __connman_service_put(service);
3109 static gint service_compare(gconstpointer a, gconstpointer b,
3112 struct connman_service *service_a = (void *) a;
3113 struct connman_service *service_b = (void *) b;
3114 enum connman_service_state state_a, state_b;
3116 state_a = combine_state(service_a->state_ipv4, service_a->state_ipv6);
3117 state_b = combine_state(service_b->state_ipv4, service_b->state_ipv6);
3119 if (state_a != state_b) {
3120 if (is_connected(service_a) == TRUE)
3122 if (is_connected(service_b) == TRUE)
3125 if (is_connecting(service_a) == TRUE)
3127 if (is_connecting(service_b) == TRUE)
3131 if (service_a->order > service_b->order)
3134 if (service_a->order < service_b->order)
3137 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3140 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3143 if (service_a->type != service_b->type) {
3144 switch (service_a->type) {
3145 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3146 case CONNMAN_SERVICE_TYPE_SYSTEM:
3147 case CONNMAN_SERVICE_TYPE_ETHERNET:
3148 case CONNMAN_SERVICE_TYPE_GPS:
3149 case CONNMAN_SERVICE_TYPE_VPN:
3150 case CONNMAN_SERVICE_TYPE_GADGET:
3152 case CONNMAN_SERVICE_TYPE_WIFI:
3154 case CONNMAN_SERVICE_TYPE_WIMAX:
3155 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3156 case CONNMAN_SERVICE_TYPE_CELLULAR:
3161 return (gint) service_b->strength - (gint) service_a->strength;
3165 * connman_service_get_type:
3166 * @service: service structure
3168 * Get the type of service
3170 enum connman_service_type connman_service_get_type(struct connman_service *service)
3172 if (service == NULL)
3173 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3175 return service->type;
3179 * connman_service_get_interface:
3180 * @service: service structure
3182 * Get network interface of service
3184 char *connman_service_get_interface(struct connman_service *service)
3188 if (service == NULL)
3191 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3192 if (service->ipconfig_ipv4)
3193 index = connman_ipconfig_get_index(
3194 service->ipconfig_ipv4);
3195 else if (service->ipconfig_ipv6)
3196 index = connman_ipconfig_get_index(
3197 service->ipconfig_ipv6);
3201 return connman_inet_ifname(index);
3204 if (service->network == NULL)
3207 index = connman_network_get_index(service->network);
3209 return connman_inet_ifname(index);
3213 * connman_service_get_network:
3214 * @service: service structure
3216 * Get the service network
3218 struct connman_network *
3219 __connman_service_get_network(struct connman_service *service)
3221 if (service == NULL)
3224 return service->network;
3227 struct connman_ipconfig *
3228 __connman_service_get_ip4config(struct connman_service *service)
3230 if (service == NULL)
3233 return service->ipconfig_ipv4;
3236 struct connman_ipconfig *
3237 __connman_service_get_ip6config(struct connman_service *service)
3239 if (service == NULL)
3242 return service->ipconfig_ipv6;
3245 struct connman_ipconfig *
3246 __connman_service_get_ipconfig(struct connman_service *service, int family)
3248 if (family == AF_INET)
3249 return __connman_service_get_ip4config(service);
3250 else if (family == AF_INET6)
3251 return __connman_service_get_ip6config(service);
3257 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3259 if (service == NULL)
3260 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3262 return service->security;
3265 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3267 if (service == NULL)
3270 return service->wps;
3274 * __connman_service_set_favorite:
3275 * @service: service structure
3276 * @favorite: favorite value
3278 * Change the favorite setting of service
3280 int __connman_service_set_favorite(struct connman_service *service,
3281 connman_bool_t favorite)
3283 GSequenceIter *iter;
3285 iter = g_hash_table_lookup(service_hash, service->identifier);
3289 if (service->favorite == favorite)
3292 service->favorite = favorite;
3294 favorite_changed(service);
3296 g_sequence_sort_changed(iter, service_compare, NULL);
3298 __connman_profile_changed(FALSE);
3303 int __connman_service_set_immutable(struct connman_service *service,
3304 connman_bool_t immutable)
3306 service->immutable = immutable;
3308 immutable_changed(service);
3313 void __connman_service_set_string(struct connman_service *service,
3314 const char *key, const char *value)
3316 if (g_str_equal(key, "EAP") == TRUE) {
3317 g_free(service->eap);
3318 service->eap = g_strdup(value);
3319 } else if (g_str_equal(key, "Identity") == TRUE) {
3320 g_free(service->identity);
3321 service->identity = g_strdup(value);
3322 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3323 g_free(service->ca_cert_file);
3324 service->ca_cert_file = g_strdup(value);
3325 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3326 g_free(service->client_cert_file);
3327 service->client_cert_file = g_strdup(value);
3328 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3329 g_free(service->private_key_file);
3330 service->private_key_file = g_strdup(value);
3331 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3332 g_free(service->private_key_passphrase);
3333 service->private_key_passphrase = g_strdup(value);
3334 } else if (g_str_equal(key, "Phase2") == TRUE) {
3335 g_free(service->phase2);
3336 service->phase2 = g_strdup(value);
3337 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3338 g_free(service->passphrase);
3339 service->passphrase = g_strdup(value);
3343 static void service_complete(struct connman_service *service)
3345 reply_pending(service, EIO);
3347 if (service->userconnect == FALSE)
3348 __connman_service_auto_connect();
3350 g_get_current_time(&service->modified);
3351 __connman_storage_save_service(service);
3354 static void report_error_cb(struct connman_service *service,
3355 gboolean retry, void *user_data)
3358 __connman_service_connect(service);
3360 service_complete(service);
3361 __connman_profile_changed(FALSE);
3362 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3366 int __connman_service_indicate_state(struct connman_service *service,
3367 enum connman_service_state new_state,
3368 enum connman_ipconfig_type type)
3370 enum connman_service_state service_state,
3371 state = CONNMAN_SERVICE_STATE_IDLE;
3372 GSequenceIter *iter;
3374 if (service == NULL)
3377 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3378 if (service->state_ipv4 == new_state)
3380 state = combine_state(new_state, service->state_ipv6);
3381 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3382 if (service->state_ipv6 == new_state)
3384 state = combine_state(new_state, service->state_ipv4);
3388 service_state = combine_state(service->state_ipv4,
3389 service->state_ipv6);
3391 DBG("service %p state %s/%s => %s new %s/%d => %s",
3393 state2string(service->state_ipv4),
3394 state2string(service->state_ipv6),
3395 state2string(service_state),
3396 state2string(new_state),
3398 state2string(state));
3400 if (service_state == CONNMAN_SERVICE_STATE_FAILURE &&
3401 state == CONNMAN_SERVICE_STATE_IDLE)
3404 if (service_state == CONNMAN_SERVICE_STATE_IDLE &&
3405 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3408 if (state == CONNMAN_SERVICE_STATE_IDLE &&
3409 service_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3410 state_changed(service);
3412 reply_pending(service, ECONNABORTED);
3414 __connman_service_disconnect(service);
3417 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3418 if (__connman_stats_service_register(service) == 0) {
3419 __connman_stats_get(service, FALSE,
3420 &service->stats.data);
3421 __connman_stats_get(service, TRUE,
3422 &service->stats_roaming.data);
3425 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
3426 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3427 __connman_ipconfig_enable(service->ipconfig_ipv4);
3428 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
3429 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3430 __connman_ipconfig_enable(service->ipconfig_ipv6);
3433 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3434 service->state_ipv4 = new_state;
3435 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3436 service->state_ipv6 = new_state;
3438 if (state == service_state)
3441 state_changed(service);
3443 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3444 if (service->login_required == TRUE) {
3445 service->login_required = FALSE;
3446 login_changed(service);
3449 connman_timeserver_sync();
3452 if (state == CONNMAN_SERVICE_STATE_IDLE) {
3453 connman_bool_t reconnect;
3455 reconnect = get_reconnect_state(service);
3456 if (reconnect == TRUE)
3457 __connman_service_auto_connect();
3460 if (state == CONNMAN_SERVICE_STATE_READY) {
3461 enum connman_service_proxy_method proxy_config;
3462 enum connman_ipconfig_method method;
3464 set_reconnect_state(service, TRUE);
3466 __connman_service_set_favorite(service, TRUE);
3468 reply_pending(service, 0);
3470 service->userconnect = FALSE;
3472 g_get_current_time(&service->modified);
3473 __connman_storage_save_service(service);
3475 update_nameservers(service);
3476 dns_changed(service);
3477 domain_changed(service);
3479 proxy_config = service->proxy_config;
3482 * We start WPAD if we haven't got a PAC URL from DHCP and
3483 * if our proxy manual configuration is either empty or set
3484 * to AUTO with an empty URL.
3486 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3487 (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3489 CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3490 service->pac == NULL)))
3491 if (__connman_wpad_start(service) < 0) {
3493 CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3495 __connman_notifier_proxy_changed(service);
3498 __connman_notifier_connect(service->type);
3500 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3501 connman_network_get_bool(service->network,
3502 "WiFi.UseWPS") == TRUE) {
3505 pass = connman_network_get_string(service->network,
3508 __connman_service_set_passphrase(service, pass);
3510 connman_network_set_bool(service->network,
3511 "WiFi.UseWPS", FALSE);
3516 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3517 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3518 __connman_ipconfig_disable_ipv6(
3519 service->ipconfig_ipv6);
3521 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3522 __connman_location_finish(service);
3526 __connman_wpad_stop(service);
3528 update_nameservers(service);
3529 dns_changed(service);
3530 domain_changed(service);
3532 __connman_notifier_disconnect(service->type);
3535 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3536 if (service->userconnect == TRUE &&
3537 __connman_agent_report_error(service,
3538 error2string(service->error),
3539 report_error_cb, NULL) == -EIO)
3541 service_complete(service);
3543 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3545 iter = g_hash_table_lookup(service_hash, service->identifier);
3547 g_sequence_sort_changed(iter, service_compare, NULL);
3549 __connman_profile_changed(FALSE);
3551 service_state = combine_state(service->state_ipv4,
3552 service->state_ipv6);
3553 if (service_state == CONNMAN_SERVICE_STATE_ONLINE)
3556 if (service_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3557 struct connman_service *def_service = get_default();
3559 if (__connman_notifier_count_connected() == 0 &&
3560 def_service != NULL &&
3561 def_service->provider != NULL)
3562 __connman_provider_disconnect(def_service->provider);
3565 if (service_state == CONNMAN_SERVICE_STATE_IDLE ||
3566 service_state == CONNMAN_SERVICE_STATE_FAILURE)
3567 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3572 int __connman_service_indicate_error(struct connman_service *service,
3573 enum connman_service_error error)
3575 DBG("service %p error %d", service, error);
3577 if (service == NULL)
3580 service->error = error;
3582 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3583 __connman_service_set_passphrase(service, NULL);
3585 return __connman_service_indicate_state(service,
3586 CONNMAN_SERVICE_STATE_FAILURE,
3587 CONNMAN_IPCONFIG_TYPE_IPV4);
3590 int __connman_service_clear_error(struct connman_service *service)
3592 enum connman_service_state state;
3594 DBG("service %p", service);
3596 if (service == NULL)
3599 state = combine_state(service->state_ipv4, service->state_ipv6);
3601 if (state != CONNMAN_SERVICE_STATE_FAILURE)
3604 service->state_ipv4 = service->state_ipv6 =
3605 CONNMAN_SERVICE_STATE_UNKNOWN;
3606 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3608 if (service->favorite == TRUE)
3609 set_reconnect_state(service, TRUE);
3611 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
3612 CONNMAN_IPCONFIG_TYPE_IPV6);
3615 * Toggling the IPv6 state to IDLE could trigger the auto connect
3616 * machinery and consequently the IPv4 state.
3618 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3619 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3622 return __connman_service_indicate_state(service,
3623 CONNMAN_SERVICE_STATE_IDLE,
3624 CONNMAN_IPCONFIG_TYPE_IPV4);
3627 int __connman_service_indicate_default(struct connman_service *service)
3629 DBG("service %p", service);
3633 __connman_location_detect(service);
3638 int __connman_service_request_login(struct connman_service *service)
3640 DBG("service %p", service);
3642 if (service == NULL)
3645 service->login_required = TRUE;
3646 login_changed(service);
3651 static connman_bool_t prepare_network(struct connman_service *service)
3653 enum connman_network_type type;
3654 unsigned int ssid_len;
3656 type = connman_network_get_type(service->network);
3659 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3660 case CONNMAN_NETWORK_TYPE_VENDOR:
3662 case CONNMAN_NETWORK_TYPE_WIFI:
3663 if (connman_network_get_blob(service->network, "WiFi.SSID",
3667 if (service->passphrase != NULL)
3668 connman_network_set_string(service->network,
3669 "WiFi.Passphrase", service->passphrase);
3671 case CONNMAN_NETWORK_TYPE_ETHERNET:
3672 case CONNMAN_NETWORK_TYPE_WIMAX:
3673 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3674 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3675 case CONNMAN_NETWORK_TYPE_CELLULAR:
3682 static void prepare_8021x(struct connman_service *service)
3684 if (service->eap != NULL)
3685 connman_network_set_string(service->network, "WiFi.EAP",
3688 if (service->identity != NULL)
3689 connman_network_set_string(service->network, "WiFi.Identity",
3692 if (service->ca_cert_file != NULL)
3693 connman_network_set_string(service->network, "WiFi.CACertFile",
3694 service->ca_cert_file);
3696 if (service->client_cert_file != NULL)
3697 connman_network_set_string(service->network,
3698 "WiFi.ClientCertFile",
3699 service->client_cert_file);
3701 if (service->private_key_file != NULL)
3702 connman_network_set_string(service->network,
3703 "WiFi.PrivateKeyFile",
3704 service->private_key_file);
3706 if (service->private_key_passphrase != NULL)
3707 connman_network_set_string(service->network,
3708 "WiFi.PrivateKeyPassphrase",
3709 service->private_key_passphrase);
3711 if (service->phase2 != NULL)
3712 connman_network_set_string(service->network, "WiFi.Phase2",
3716 static int service_connect(struct connman_service *service)
3720 switch (service->type) {
3721 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3722 case CONNMAN_SERVICE_TYPE_SYSTEM:
3723 case CONNMAN_SERVICE_TYPE_GPS:
3724 case CONNMAN_SERVICE_TYPE_GADGET:
3726 case CONNMAN_SERVICE_TYPE_ETHERNET:
3727 case CONNMAN_SERVICE_TYPE_WIMAX:
3728 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3729 case CONNMAN_SERVICE_TYPE_CELLULAR:
3730 case CONNMAN_SERVICE_TYPE_VPN:
3732 case CONNMAN_SERVICE_TYPE_WIFI:
3733 switch (service->security) {
3734 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3735 case CONNMAN_SERVICE_SECURITY_NONE:
3737 case CONNMAN_SERVICE_SECURITY_WEP:
3738 case CONNMAN_SERVICE_SECURITY_PSK:
3739 case CONNMAN_SERVICE_SECURITY_WPA:
3740 case CONNMAN_SERVICE_SECURITY_RSN:
3741 if (service->passphrase == NULL) {
3742 if (service->network == NULL)
3745 if (service->wps == FALSE ||
3746 connman_network_get_bool(
3748 "WiFi.UseWPS") == FALSE)
3752 case CONNMAN_SERVICE_SECURITY_8021X:
3758 if (service->network != NULL) {
3759 if (prepare_network(service) == FALSE)
3762 switch (service->security) {
3763 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3764 case CONNMAN_SERVICE_SECURITY_NONE:
3765 case CONNMAN_SERVICE_SECURITY_WEP:
3766 case CONNMAN_SERVICE_SECURITY_PSK:
3767 case CONNMAN_SERVICE_SECURITY_WPA:
3768 case CONNMAN_SERVICE_SECURITY_RSN:
3770 case CONNMAN_SERVICE_SECURITY_8021X:
3771 prepare_8021x(service);
3775 if (__connman_stats_service_register(service) == 0) {
3776 __connman_stats_get(service, FALSE,
3777 &service->stats.data);
3778 __connman_stats_get(service, TRUE,
3779 &service->stats_roaming.data);
3782 if (service->ipconfig_ipv4)
3783 __connman_ipconfig_enable(service->ipconfig_ipv4);
3784 if (service->ipconfig_ipv6)
3785 __connman_ipconfig_enable(service->ipconfig_ipv6);
3787 err = __connman_network_connect(service->network);
3788 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3789 service->provider != NULL)
3790 err = __connman_provider_connect(service->provider);
3795 if (err != -EINPROGRESS) {
3796 __connman_ipconfig_disable(service->ipconfig_ipv4);
3797 __connman_ipconfig_disable(service->ipconfig_ipv6);
3798 __connman_stats_service_unregister(service);
3806 int __connman_service_connect(struct connman_service *service)
3808 enum connman_service_state state;
3811 state = combine_state(service->state_ipv4, service->state_ipv6);
3813 DBG("service %p state %s", service, state2string(state));
3815 if (is_connected(service) == TRUE)
3818 if (is_connecting(service) == TRUE)
3821 switch (service->type) {
3822 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3823 case CONNMAN_SERVICE_TYPE_SYSTEM:
3824 case CONNMAN_SERVICE_TYPE_GPS:
3825 case CONNMAN_SERVICE_TYPE_GADGET:
3828 err = service_connect(service);
3834 if (err == -EINPROGRESS) {
3835 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
3836 connect_timeout, service);
3838 return -EINPROGRESS;
3844 if (service->userconnect == TRUE)
3845 reply_pending(service, err);
3847 __connman_service_indicate_state(service,
3848 CONNMAN_SERVICE_STATE_FAILURE,
3849 CONNMAN_IPCONFIG_TYPE_IPV4);
3850 __connman_service_indicate_state(service,
3851 CONNMAN_SERVICE_STATE_FAILURE,
3852 CONNMAN_IPCONFIG_TYPE_IPV6);
3857 int __connman_service_disconnect(struct connman_service *service)
3861 DBG("service %p", service);
3863 if (service->network != NULL) {
3864 err = __connman_network_disconnect(service->network);
3865 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3866 service->provider != NULL)
3867 err = __connman_provider_disconnect(service->provider);
3871 if (err < 0 && err != -EINPROGRESS)
3874 __connman_6to4_remove(service->ipconfig_ipv4);
3876 if (service->ipconfig_ipv4)
3877 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3880 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3883 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3884 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3886 __connman_ipconfig_disable(service->ipconfig_ipv4);
3887 __connman_ipconfig_disable(service->ipconfig_ipv6);
3889 __connman_stats_service_unregister(service);
3894 int __connman_service_disconnect_all(void)
3896 GSequenceIter *iter;
3900 iter = g_sequence_get_begin_iter(service_list);
3902 while (g_sequence_iter_is_end(iter) == FALSE) {
3903 struct connman_service *service = g_sequence_get(iter);
3905 service->ignore = TRUE;
3907 set_reconnect_state(service, FALSE);
3909 __connman_service_disconnect(service);
3911 iter = g_sequence_iter_next(iter);
3919 * __connman_service_lookup:
3920 * @pattern: search pattern
3921 * @path: return object path
3923 * Look up a service path from a search pattern
3925 int __connman_service_lookup(const char *pattern, const char **path)
3927 GHashTableIter iter;
3928 gpointer key, value;
3930 g_hash_table_iter_init(&iter, service_hash);
3932 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
3933 GSequenceIter *iter = value;
3934 struct connman_service *service = g_sequence_get(iter);
3936 if (g_strcmp0(service->identifier, pattern) == 0 ||
3937 g_strcmp0(service->name, pattern) == 0) {
3938 *path = (const char *) service->path;
3947 * lookup_by_identifier:
3948 * @identifier: service identifier
3950 * Look up a service by identifier (reference count will not be increased)
3952 static struct connman_service *lookup_by_identifier(const char *identifier)
3954 GSequenceIter *iter;
3956 iter = g_hash_table_lookup(service_hash, identifier);
3958 return g_sequence_get(iter);
3963 static struct connman_network *create_hidden_wifi(struct connman_device *device,
3964 const char *ssid, const char *mode, const char *security)
3966 struct connman_network *network;
3969 unsigned int i, ssid_len;
3971 ssid_len = strlen(ssid);
3975 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
3976 if (network == NULL)
3979 connman_network_set_blob(network, "WiFi.SSID",
3980 (unsigned char *) ssid, ssid_len);
3982 connman_network_set_string(network, "WiFi.Mode", mode);
3983 connman_network_set_string(network, "WiFi.Security", security);
3985 name = g_try_malloc0(ssid_len + 1);
3987 connman_network_unref(network);
3991 for (i = 0; i < ssid_len; i++) {
3992 if (g_ascii_isprint(ssid[i]))
3998 connman_network_set_name(network, name);
4002 index = connman_device_get_index(device);
4003 connman_network_set_index(network, index);
4005 if (connman_device_add_network(device, network) < 0) {
4006 connman_network_unref(network);
4010 connman_network_set_available(network, TRUE);
4015 int __connman_service_create_and_connect(DBusMessage *msg)
4017 struct connman_service *service;
4018 struct connman_network *network;
4019 struct connman_device *device;
4020 DBusMessageIter iter, array;
4021 const char *mode = "managed", *security = "none", *group_security;
4022 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4023 unsigned int ssid_len = 0;
4026 gboolean created = FALSE;
4029 dbus_message_iter_init(msg, &iter);
4030 dbus_message_iter_recurse(&iter, &array);
4032 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4033 DBusMessageIter entry, value;
4036 dbus_message_iter_recurse(&array, &entry);
4037 dbus_message_iter_get_basic(&entry, &key);
4039 dbus_message_iter_next(&entry);
4040 dbus_message_iter_recurse(&entry, &value);
4042 switch (dbus_message_iter_get_arg_type(&value)) {
4043 case DBUS_TYPE_STRING:
4044 if (g_str_equal(key, "Type") == TRUE)
4045 dbus_message_iter_get_basic(&value, &type);
4046 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4047 g_str_equal(key, "Mode") == TRUE)
4048 dbus_message_iter_get_basic(&value, &mode);
4049 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4050 g_str_equal(key, "Security") == TRUE)
4051 dbus_message_iter_get_basic(&value, &security);
4052 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4053 g_str_equal(key, "Passphrase") == TRUE)
4054 dbus_message_iter_get_basic(&value, &passphrase);
4055 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4056 g_str_equal(key, "SSID") == TRUE)
4057 dbus_message_iter_get_basic(&value, &ssid);
4060 dbus_message_iter_next(&array);
4066 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4072 ssid_len = strlen(ssid);
4076 if (g_strcmp0(security, "none") != 0 &&
4077 g_strcmp0(security, "wep") != 0 &&
4078 g_strcmp0(security, "psk") != 0 &&
4079 g_strcmp0(security, "wpa") != 0 &&
4080 g_strcmp0(security, "rsn") != 0 &&
4081 g_strcmp0(security, "ieee8021x") != 0)
4084 device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4088 ident = connman_device_get_ident(device);
4093 if (!g_strcmp0(security, "wpa") ||
4094 !g_strcmp0(security, "rsn"))
4095 group_security = "psk";
4097 group_security = security;
4099 group = connman_wifi_build_group_name((unsigned char *) ssid,
4100 ssid_len, mode, group_security);
4104 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4106 service = lookup_by_identifier(name);
4108 if (service != NULL)
4111 network = create_hidden_wifi(device, ssid, mode, security);
4112 if (network != NULL) {
4113 connman_network_set_group(network, group);
4117 service = lookup_by_identifier(name);
4123 if (service == NULL) {
4128 if (is_connected(service) == TRUE) {
4133 if (is_connecting(service) == TRUE) {
4138 set_reconnect_state(service, FALSE);
4140 __connman_device_disconnect(device);
4142 if (passphrase != NULL) {
4143 g_free(service->passphrase);
4144 service->passphrase = g_strdup(passphrase);
4147 service->userconnect = TRUE;
4149 err = __connman_service_connect(service);
4150 if (err < 0 && err != -EINPROGRESS)
4153 service->pending = dbus_message_ref(msg);
4158 if (service != NULL && created == TRUE) {
4159 struct connman_network *network = service->network;
4161 if (network != NULL) {
4162 connman_network_set_available(network, FALSE);
4163 __connman_device_cleanup_networks(device);
4165 __connman_service_put(service);
4172 * __connman_service_get:
4173 * @identifier: service identifier
4175 * Look up a service by identifier or create a new one if not found
4177 static struct connman_service *service_get(const char *identifier)
4179 struct connman_service *service;
4180 GSequenceIter *iter;
4182 iter = g_hash_table_lookup(service_hash, identifier);
4184 service = g_sequence_get(iter);
4185 if (service != NULL)
4186 connman_service_ref(service);
4190 service = connman_service_create();
4191 if (service == NULL)
4194 DBG("service %p", service);
4196 service->identifier = g_strdup(identifier);
4198 service->profile = g_strdup(__connman_profile_active_ident());
4200 iter = g_sequence_insert_sorted(service_list, service,
4201 service_compare, NULL);
4203 g_hash_table_insert(service_hash, service->identifier, iter);
4208 static int service_register(struct connman_service *service)
4210 const char *path = __connman_profile_active_path();
4211 GSequenceIter *iter;
4213 DBG("service %p", service);
4215 if (service->path != NULL)
4218 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4220 DBG("path %s", service->path);
4222 __connman_config_provision_service(service);
4224 __connman_storage_load_service(service);
4226 g_dbus_register_interface(connection, service->path,
4227 CONNMAN_SERVICE_INTERFACE,
4228 service_methods, service_signals,
4229 NULL, service, NULL);
4231 iter = g_hash_table_lookup(service_hash, service->identifier);
4233 g_sequence_sort_changed(iter, service_compare, NULL);
4235 __connman_profile_changed(TRUE);
4240 static void service_up(struct connman_ipconfig *ipconfig)
4242 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4244 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
4246 link_changed(service);
4248 service->stats.valid = FALSE;
4249 service->stats_roaming.valid = FALSE;
4252 static void service_down(struct connman_ipconfig *ipconfig)
4254 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
4257 static void service_lower_up(struct connman_ipconfig *ipconfig)
4259 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4261 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4263 stats_start(service);
4266 static void service_lower_down(struct connman_ipconfig *ipconfig)
4268 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4270 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4272 stats_stop(service);
4273 __connman_storage_save_service(service);
4276 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4278 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4279 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4280 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4282 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4284 type = __connman_ipconfig_get_config_type(ipconfig);
4285 method = __connman_ipconfig_get_method(ipconfig);
4287 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4290 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4291 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4292 __connman_service_indicate_state(service,
4293 CONNMAN_SERVICE_STATE_READY,
4294 CONNMAN_IPCONFIG_TYPE_IPV6);
4296 settings_changed(service);
4299 static void service_ip_release(struct connman_ipconfig *ipconfig)
4301 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4302 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4303 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4305 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4307 type = __connman_ipconfig_get_config_type(ipconfig);
4308 method = __connman_ipconfig_get_method(ipconfig);
4310 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4313 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4314 method == CONNMAN_IPCONFIG_METHOD_OFF)
4315 __connman_service_indicate_state(service,
4316 CONNMAN_SERVICE_STATE_DISCONNECT,
4317 CONNMAN_IPCONFIG_TYPE_IPV6);
4319 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4320 method == CONNMAN_IPCONFIG_METHOD_OFF)
4321 __connman_service_indicate_state(service,
4322 CONNMAN_SERVICE_STATE_DISCONNECT,
4323 CONNMAN_IPCONFIG_TYPE_IPV4);
4325 settings_changed(service);
4328 static const struct connman_ipconfig_ops service_ops = {
4330 .down = service_down,
4331 .lower_up = service_lower_up,
4332 .lower_down = service_lower_down,
4333 .ip_bound = service_ip_bound,
4334 .ip_release = service_ip_release,
4337 static void setup_ip4config(struct connman_service *service, int index)
4342 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4343 CONNMAN_IPCONFIG_TYPE_IPV4);
4344 if (service->ipconfig_ipv4 == NULL)
4347 connman_ipconfig_set_method(service->ipconfig_ipv4,
4348 CONNMAN_IPCONFIG_METHOD_DHCP);
4350 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4352 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4355 static void setup_ip6config(struct connman_service *service, int index)
4360 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4361 CONNMAN_IPCONFIG_TYPE_IPV6);
4362 if (service->ipconfig_ipv6 == NULL)
4365 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4367 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4370 void __connman_service_create_ip4config(struct connman_service *service,
4373 const char *ident = service->profile;
4376 DBG("ipv4 %p", service->ipconfig_ipv4);
4378 if (service->ipconfig_ipv4 != NULL)
4381 setup_ip4config(service, index);
4386 keyfile = __connman_storage_open_profile(ident);
4387 if (keyfile == NULL)
4390 if (service->ipconfig_ipv4)
4391 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4392 service->identifier, "IPv4.");
4393 g_key_file_free(keyfile);
4396 void __connman_service_create_ip6config(struct connman_service *service,
4399 const char *ident = service->profile;
4402 DBG("ipv6 %p", service->ipconfig_ipv6);
4404 if (service->ipconfig_ipv6 != NULL)
4407 setup_ip6config(service, index);
4412 keyfile = __connman_storage_open_profile(ident);
4413 if (keyfile == NULL)
4416 if (service->ipconfig_ipv6 != NULL)
4417 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4418 service->identifier, "IPv6.");
4420 g_key_file_free(keyfile);
4424 * __connman_service_lookup_from_network:
4425 * @network: network structure
4427 * Look up a service by network (reference count will not be increased)
4429 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4431 struct connman_service *service;
4432 const char *ident, *group;
4435 DBG("network %p", network);
4437 ident = __connman_network_get_ident(network);
4441 group = connman_network_get_group(network);
4445 name = g_strdup_printf("%s_%s_%s",
4446 __connman_network_get_type(network), ident, group);
4447 service = lookup_by_identifier(name);
4453 struct connman_service *__connman_service_lookup_from_index(int index)
4455 struct connman_service *service;
4456 GSequenceIter *iter;
4458 iter = g_sequence_get_begin_iter(service_list);
4460 while (g_sequence_iter_is_end(iter) == FALSE) {
4461 service = g_sequence_get(iter);
4463 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4467 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4471 iter = g_sequence_iter_next(iter);
4477 const char *__connman_service_get_ident(struct connman_service *service)
4479 return service->identifier;
4482 const char *__connman_service_get_path(struct connman_service *service)
4484 return service->path;
4487 unsigned int __connman_service_get_order(struct connman_service *service)
4489 GSequenceIter *iter;
4491 if (service == NULL)
4494 if (service->favorite == FALSE) {
4499 iter = g_hash_table_lookup(service_hash, service->identifier);
4501 if (g_sequence_iter_get_position(iter) == 0)
4503 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4504 service->order = 10;
4510 return service->order;
4513 static enum connman_service_type convert_network_type(struct connman_network *network)
4515 enum connman_network_type type = connman_network_get_type(network);
4518 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4519 case CONNMAN_NETWORK_TYPE_VENDOR:
4521 case CONNMAN_NETWORK_TYPE_ETHERNET:
4522 return CONNMAN_SERVICE_TYPE_ETHERNET;
4523 case CONNMAN_NETWORK_TYPE_WIFI:
4524 return CONNMAN_SERVICE_TYPE_WIFI;
4525 case CONNMAN_NETWORK_TYPE_WIMAX:
4526 return CONNMAN_SERVICE_TYPE_WIMAX;
4527 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4528 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4529 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4530 case CONNMAN_NETWORK_TYPE_CELLULAR:
4531 return CONNMAN_SERVICE_TYPE_CELLULAR;
4534 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4537 static enum connman_service_mode convert_wifi_mode(const char *mode)
4540 return CONNMAN_SERVICE_MODE_UNKNOWN;
4541 else if (g_str_equal(mode, "managed") == TRUE)
4542 return CONNMAN_SERVICE_MODE_MANAGED;
4543 else if (g_str_equal(mode, "adhoc") == TRUE)
4544 return CONNMAN_SERVICE_MODE_ADHOC;
4546 return CONNMAN_SERVICE_MODE_UNKNOWN;
4549 static enum connman_service_security convert_wifi_security(const char *security)
4551 if (security == NULL)
4552 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4553 else if (g_str_equal(security, "none") == TRUE)
4554 return CONNMAN_SERVICE_SECURITY_NONE;
4555 else if (g_str_equal(security, "wep") == TRUE)
4556 return CONNMAN_SERVICE_SECURITY_WEP;
4557 else if (g_str_equal(security, "psk") == TRUE)
4558 return CONNMAN_SERVICE_SECURITY_PSK;
4559 else if (g_str_equal(security, "ieee8021x") == TRUE)
4560 return CONNMAN_SERVICE_SECURITY_8021X;
4561 else if (g_str_equal(security, "wpa") == TRUE)
4562 return CONNMAN_SERVICE_SECURITY_WPA;
4563 else if (g_str_equal(security, "rsn") == TRUE)
4564 return CONNMAN_SERVICE_SECURITY_RSN;
4566 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4569 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
4574 return CONNMAN_SERVICE_MODE_GPRS;
4576 return CONNMAN_SERVICE_MODE_EDGE;
4581 return CONNMAN_SERVICE_MODE_UMTS;
4584 return CONNMAN_SERVICE_MODE_UNKNOWN;
4587 static void update_from_network(struct connman_service *service,
4588 struct connman_network *network)
4590 connman_uint8_t strength = service->strength;
4591 GSequenceIter *iter;
4594 DBG("service %p network %p", service, network);
4596 if (is_connected(service) == TRUE)
4599 if (is_connecting(service) == TRUE)
4602 str = connman_network_get_string(network, "Name");
4604 g_free(service->name);
4605 service->name = g_strdup(str);
4606 service->hidden = FALSE;
4608 g_free(service->name);
4609 service->name = NULL;
4610 service->hidden = TRUE;
4613 service->strength = connman_network_get_uint8(network, "Strength");
4614 service->roaming = connman_network_get_bool(network, "Roaming");
4616 if (service->strength == 0) {
4618 * Filter out 0-values; it's unclear what they mean
4619 * and they cause anomalous sorting of the priority list.
4621 service->strength = strength;
4624 str = connman_network_get_string(network, "WiFi.Mode");
4625 service->mode = convert_wifi_mode(str);
4627 str = connman_network_get_string(network, "WiFi.Security");
4628 service->security = convert_wifi_security(str);
4630 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
4631 connman_uint8_t value = connman_network_get_uint8(network,
4634 service->mode = convert_cellular_mode(value);
4635 } else if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4636 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4638 if (service->strength > strength && service->network != NULL) {
4639 connman_network_unref(service->network);
4640 service->network = connman_network_ref(network);
4642 strength_changed(service);
4645 if (service->network == NULL)
4646 service->network = connman_network_ref(network);
4648 iter = g_hash_table_lookup(service_hash, service->identifier);
4650 g_sequence_sort_changed(iter, service_compare, NULL);
4654 * __connman_service_create_from_network:
4655 * @network: network structure
4657 * Look up service by network and if not found, create one
4659 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4661 struct connman_service *service;
4662 struct connman_device *device;
4663 const char *ident, *group;
4667 DBG("network %p", network);
4669 ident = __connman_network_get_ident(network);
4673 group = connman_network_get_group(network);
4677 name = g_strdup_printf("%s_%s_%s",
4678 __connman_network_get_type(network), ident, group);
4679 service = service_get(name);
4682 if (service == NULL)
4685 if (__connman_network_get_weakness(network) == TRUE)
4688 if (service->path != NULL) {
4689 update_from_network(service, network);
4690 __connman_profile_changed(TRUE);
4694 service->type = convert_network_type(network);
4696 switch (service->type) {
4697 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4698 case CONNMAN_SERVICE_TYPE_SYSTEM:
4699 case CONNMAN_SERVICE_TYPE_ETHERNET:
4700 case CONNMAN_SERVICE_TYPE_WIMAX:
4701 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4702 case CONNMAN_SERVICE_TYPE_GPS:
4703 case CONNMAN_SERVICE_TYPE_VPN:
4704 case CONNMAN_SERVICE_TYPE_GADGET:
4705 service->autoconnect = FALSE;
4707 case CONNMAN_SERVICE_TYPE_WIFI:
4708 case CONNMAN_SERVICE_TYPE_CELLULAR:
4709 service->autoconnect = TRUE;
4713 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4715 update_from_network(service, network);
4717 index = connman_network_get_index(network);
4719 if (service->ipconfig_ipv4 == NULL)
4720 setup_ip4config(service, index);
4722 if (service->ipconfig_ipv6 == NULL)
4723 setup_ip6config(service, index);
4725 service_register(service);
4727 if (service->favorite == TRUE) {
4728 device = connman_network_get_device(service->network);
4729 if (device && __connman_device_scanning(device) == FALSE)
4730 __connman_service_auto_connect();
4736 void __connman_service_update_from_network(struct connman_network *network)
4738 struct connman_service *service;
4739 enum connman_service_mode mode;
4740 connman_uint8_t strength, value;
4741 connman_bool_t roaming;
4742 GSequenceIter *iter;
4744 connman_bool_t stats_enable;
4746 DBG("network %p", network);
4748 service = __connman_service_lookup_from_network(network);
4749 if (service == NULL)
4752 if (service->network == NULL)
4755 name = connman_network_get_string(service->network, "Name");
4756 if (g_strcmp0(service->name, name) != 0) {
4757 g_free(service->name);
4758 service->name = g_strdup(name);
4759 connman_dbus_property_changed_basic(service->path,
4760 CONNMAN_SERVICE_INTERFACE, "Name",
4761 DBUS_TYPE_STRING, &service->name);
4764 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4765 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4767 strength = connman_network_get_uint8(service->network, "Strength");
4768 if (strength == service->strength)
4771 service->strength = strength;
4773 strength_changed(service);
4776 roaming = connman_network_get_bool(service->network, "Roaming");
4777 if (roaming == service->roaming)
4780 stats_enable = stats_enabled(service);
4781 if (stats_enable == TRUE)
4782 stats_stop(service);
4784 service->roaming = roaming;
4786 if (stats_enable == TRUE)
4787 stats_start(service);
4789 roaming_changed(service);
4791 iter = g_hash_table_lookup(service_hash, service->identifier);
4793 g_sequence_sort_changed(iter, service_compare, NULL);
4796 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
4799 value = connman_network_get_uint8(service->network, "Cellular.Mode");
4800 mode = convert_cellular_mode(value);
4802 if (mode == service->mode)
4805 service->mode = mode;
4807 mode_changed(service);
4810 void __connman_service_remove_from_network(struct connman_network *network)
4812 struct connman_service *service;
4814 DBG("network %p", network);
4816 service = __connman_service_lookup_from_network(network);
4817 if (service == NULL)
4820 __connman_service_put(service);
4824 * __connman_service_create_from_provider:
4825 * @provider: provider structure
4827 * Look up service by provider and if not found, create one
4829 struct connman_service *
4830 __connman_service_create_from_provider(struct connman_provider *provider)
4832 struct connman_service *service;
4833 const char *ident, *str;
4835 int index = connman_provider_get_index(provider);
4837 DBG("provider %p", provider);
4839 ident = __connman_provider_get_ident(provider);
4843 name = g_strdup_printf("vpn_%s", ident);
4844 service = service_get(name);
4847 if (service == NULL)
4850 service->type = CONNMAN_SERVICE_TYPE_VPN;
4851 service->provider = connman_provider_ref(provider);
4852 service->autoconnect = FALSE;
4854 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4856 str = connman_provider_get_string(provider, "Name");
4858 g_free(service->name);
4859 service->name = g_strdup(str);
4860 service->hidden = FALSE;
4862 g_free(service->name);
4863 service->name = NULL;
4864 service->hidden = TRUE;
4867 service->strength = 0;
4869 if (service->ipconfig_ipv4 == NULL)
4870 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4871 CONNMAN_IPCONFIG_TYPE_IPV4);
4872 if (service->ipconfig_ipv4 == NULL)
4875 connman_ipconfig_set_method(service->ipconfig_ipv4,
4876 CONNMAN_IPCONFIG_METHOD_MANUAL);
4877 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4878 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4880 if (service->ipconfig_ipv6 == NULL)
4881 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4882 CONNMAN_IPCONFIG_TYPE_IPV6);
4883 if (service->ipconfig_ipv6 == NULL)
4886 connman_ipconfig_set_method(service->ipconfig_ipv6,
4887 CONNMAN_IPCONFIG_METHOD_OFF);
4888 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4889 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4891 service_register(service);
4896 static int service_load(struct connman_service *service)
4898 const char *ident = service->profile;
4900 GError *error = NULL;
4901 gchar *pathname, *data = NULL;
4904 connman_bool_t autoconnect;
4905 unsigned int ssid_len;
4908 DBG("service %p", service);
4913 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4914 if (pathname == NULL)
4917 keyfile = g_key_file_new();
4919 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
4926 if (g_key_file_load_from_data(keyfile, data, length,
4927 0, NULL) == FALSE) {
4934 switch (service->type) {
4935 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4936 case CONNMAN_SERVICE_TYPE_SYSTEM:
4937 case CONNMAN_SERVICE_TYPE_ETHERNET:
4938 case CONNMAN_SERVICE_TYPE_GPS:
4939 case CONNMAN_SERVICE_TYPE_VPN:
4940 case CONNMAN_SERVICE_TYPE_GADGET:
4942 case CONNMAN_SERVICE_TYPE_WIFI:
4943 if (service->name == NULL) {
4946 name = g_key_file_get_string(keyfile,
4947 service->identifier, "Name", NULL);
4949 g_free(service->name);
4950 service->name = name;
4953 if (service->network != NULL)
4954 connman_network_set_name(service->network,
4958 if (service->network &&
4959 connman_network_get_blob(service->network,
4960 "WiFi.SSID", &ssid_len) == NULL) {
4963 hex_ssid = g_key_file_get_string(keyfile,
4964 service->identifier,
4967 if (hex_ssid != NULL) {
4969 unsigned int i, j = 0, hex;
4970 size_t hex_ssid_len = strlen(hex_ssid);
4972 ssid = g_try_malloc0(hex_ssid_len / 2);
4979 for (i = 0; i < hex_ssid_len; i += 2) {
4980 sscanf(hex_ssid + i, "%02x", &hex);
4984 connman_network_set_blob(service->network,
4985 "WiFi.SSID", ssid, hex_ssid_len / 2);
4992 case CONNMAN_SERVICE_TYPE_WIMAX:
4993 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4994 case CONNMAN_SERVICE_TYPE_CELLULAR:
4995 service->favorite = g_key_file_get_boolean(keyfile,
4996 service->identifier, "Favorite", NULL);
4998 autoconnect = g_key_file_get_boolean(keyfile,
4999 service->identifier, "AutoConnect", &error);
5001 service->autoconnect = autoconnect;
5002 g_clear_error(&error);
5004 str = g_key_file_get_string(keyfile,
5005 service->identifier, "Failure", NULL);
5007 if (service->favorite == FALSE)
5008 service->state_ipv4 = service->state_ipv6 =
5009 CONNMAN_SERVICE_STATE_FAILURE;
5010 service->error = string2error(str);
5015 str = g_key_file_get_string(keyfile,
5016 service->identifier, "Modified", NULL);
5018 g_time_val_from_iso8601(str, &service->modified);
5022 str = g_key_file_get_string(keyfile,
5023 service->identifier, "Passphrase", NULL);
5025 g_free(service->passphrase);
5026 service->passphrase = str;
5029 if (service->ipconfig_ipv4 != NULL)
5030 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5031 service->identifier, "IPv4.");
5033 if (service->ipconfig_ipv6 != NULL)
5034 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5035 service->identifier, "IPv6.");
5037 service->nameservers_config = g_key_file_get_string_list(keyfile,
5038 service->identifier, "Nameservers", &length, NULL);
5039 if (service->nameservers_config != NULL && length == 0) {
5040 g_strfreev(service->nameservers_config);
5041 service->nameservers_config = NULL;
5044 service->domains = g_key_file_get_string_list(keyfile,
5045 service->identifier, "Domains", &length, NULL);
5046 if (service->domains != NULL && length == 0) {
5047 g_strfreev(service->domains);
5048 service->domains = NULL;
5051 str = g_key_file_get_string(keyfile,
5052 service->identifier, "Proxy.Method", NULL);
5054 service->proxy_config = string2proxymethod(str);
5058 service->proxies = g_key_file_get_string_list(keyfile,
5059 service->identifier, "Proxy.Servers", &length, NULL);
5060 if (service->proxies != NULL && length == 0) {
5061 g_strfreev(service->proxies);
5062 service->proxies = NULL;
5065 service->excludes = g_key_file_get_string_list(keyfile,
5066 service->identifier, "Proxy.Excludes", &length, NULL);
5067 if (service->excludes != NULL && length == 0) {
5068 g_strfreev(service->excludes);
5069 service->excludes = NULL;
5072 str = g_key_file_get_string(keyfile,
5073 service->identifier, "Proxy.URL", NULL);
5075 g_free(service->pac);
5080 g_key_file_free(keyfile);
5085 static int service_save(struct connman_service *service)
5087 const char *ident = service->profile;
5089 gchar *pathname, *data = NULL;
5092 const char *cst_str = NULL;
5095 DBG("service %p", service);
5100 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5101 if (pathname == NULL)
5104 keyfile = g_key_file_new();
5106 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5110 if (g_key_file_load_from_data(keyfile, data, length,
5118 if (service->name != NULL)
5119 g_key_file_set_string(keyfile, service->identifier,
5120 "Name", service->name);
5122 switch (service->type) {
5123 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5124 case CONNMAN_SERVICE_TYPE_SYSTEM:
5125 case CONNMAN_SERVICE_TYPE_ETHERNET:
5126 case CONNMAN_SERVICE_TYPE_GPS:
5127 case CONNMAN_SERVICE_TYPE_VPN:
5128 case CONNMAN_SERVICE_TYPE_GADGET:
5130 case CONNMAN_SERVICE_TYPE_WIFI:
5131 if (service->network) {
5132 const unsigned char *ssid;
5133 unsigned int ssid_len = 0;
5135 ssid = connman_network_get_blob(service->network,
5136 "WiFi.SSID", &ssid_len);
5138 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5139 char *identifier = service->identifier;
5143 str = g_string_sized_new(ssid_len * 2);
5149 for (i = 0; i < ssid_len; i++)
5150 g_string_append_printf(str,
5153 g_key_file_set_string(keyfile, identifier,
5156 g_string_free(str, TRUE);
5161 case CONNMAN_SERVICE_TYPE_WIMAX:
5162 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5163 case CONNMAN_SERVICE_TYPE_CELLULAR:
5164 g_key_file_set_boolean(keyfile, service->identifier,
5165 "Favorite", service->favorite);
5167 if (service->favorite == TRUE)
5168 g_key_file_set_boolean(keyfile, service->identifier,
5169 "AutoConnect", service->autoconnect);
5171 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5172 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5173 const char *failure = error2string(service->error);
5174 if (failure != NULL)
5175 g_key_file_set_string(keyfile,
5176 service->identifier,
5177 "Failure", failure);
5179 g_key_file_remove_key(keyfile, service->identifier,
5185 str = g_time_val_to_iso8601(&service->modified);
5187 g_key_file_set_string(keyfile, service->identifier,
5192 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5193 g_key_file_set_string(keyfile, service->identifier,
5194 "Passphrase", service->passphrase);
5196 g_key_file_remove_key(keyfile, service->identifier,
5197 "Passphrase", NULL);
5199 if (service->ipconfig_ipv4 != NULL)
5200 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5201 service->identifier, "IPv4.");
5203 if (service->ipconfig_ipv6 != NULL)
5204 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5205 service->identifier, "IPv6.");
5207 if (service->nameservers_config != NULL) {
5208 guint len = g_strv_length(service->nameservers_config);
5210 g_key_file_set_string_list(keyfile, service->identifier,
5212 (const gchar **) service->nameservers_config, len);
5214 g_key_file_remove_key(keyfile, service->identifier,
5215 "Nameservers", NULL);
5217 if (service->domains != NULL) {
5218 guint len = g_strv_length(service->domains);
5220 g_key_file_set_string_list(keyfile, service->identifier,
5222 (const gchar **) service->domains, len);
5224 g_key_file_remove_key(keyfile, service->identifier,
5227 cst_str = proxymethod2string(service->proxy_config);
5228 if (cst_str != NULL)
5229 g_key_file_set_string(keyfile, service->identifier,
5230 "Proxy.Method", cst_str);
5232 if (service->proxies != NULL) {
5233 guint len = g_strv_length(service->proxies);
5235 g_key_file_set_string_list(keyfile, service->identifier,
5237 (const gchar **) service->proxies, len);
5239 g_key_file_remove_key(keyfile, service->identifier,
5240 "Proxy.Servers", NULL);
5242 if (service->excludes != NULL) {
5243 guint len = g_strv_length(service->excludes);
5245 g_key_file_set_string_list(keyfile, service->identifier,
5247 (const gchar **) service->excludes, len);
5249 g_key_file_remove_key(keyfile, service->identifier,
5250 "Proxy.Excludes", NULL);
5252 if (service->pac != NULL && strlen(service->pac) > 0)
5253 g_key_file_set_string(keyfile, service->identifier,
5254 "Proxy.URL", service->pac);
5256 g_key_file_remove_key(keyfile, service->identifier,
5259 data = g_key_file_to_data(keyfile, &length, NULL);
5261 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5262 connman_error("Failed to store service information");
5267 g_key_file_free(keyfile);
5274 static struct connman_storage service_storage = {
5276 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5277 .service_load = service_load,
5278 .service_save = service_save,
5281 int __connman_service_init(void)
5285 connection = connman_dbus_get_connection();
5287 if (connman_storage_register(&service_storage) < 0)
5288 connman_error("Failed to register service storage");
5290 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5293 service_list = g_sequence_new(service_free);
5298 void __connman_service_cleanup(void)
5302 g_sequence_free(service_list);
5303 service_list = NULL;
5305 g_hash_table_destroy(service_hash);
5306 service_hash = NULL;
5308 g_slist_free(counter_list);
5309 counter_list = NULL;
5311 connman_storage_unregister(&service_storage);
5313 dbus_connection_unref(connection);