5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #define CONNECT_TIMEOUT 120
34 static DBusConnection *connection = NULL;
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
38 static GSList *counter_list = NULL;
40 struct connman_stats {
42 connman_bool_t enabled;
43 struct connman_stats_data data_last;
44 struct connman_stats_data data;
48 struct connman_stats_counter {
49 connman_bool_t append_all;
50 struct connman_stats stats;
51 struct connman_stats stats_roaming;
54 struct connman_service {
56 gint session_usage_count;
59 enum connman_service_type type;
60 enum connman_service_mode mode;
61 enum connman_service_security security;
62 enum connman_service_state state_ipv4;
63 enum connman_service_state state_ipv6;
64 enum connman_service_error error;
65 connman_uint8_t strength;
66 connman_bool_t favorite;
67 connman_bool_t immutable;
68 connman_bool_t hidden;
69 connman_bool_t ignore;
70 connman_bool_t autoconnect;
71 connman_bool_t userconnect;
77 connman_bool_t roaming;
78 connman_bool_t login_required;
79 struct connman_ipconfig *ipconfig_ipv4;
80 struct connman_ipconfig *ipconfig_ipv6;
81 struct connman_network *network;
82 struct connman_provider *provider;
84 char **nameservers_config;
88 /* 802.1x settings from the config files */
92 char *client_cert_file;
93 char *private_key_file;
94 char *private_key_passphrase;
98 struct connman_location *location;
99 struct connman_stats stats;
100 struct connman_stats stats_roaming;
101 GHashTable *counter_table;
102 enum connman_service_proxy_method proxy;
103 enum connman_service_proxy_method proxy_config;
110 static void append_path(gpointer value, gpointer user_data)
112 struct connman_service *service = value;
113 DBusMessageIter *iter = user_data;
115 if (service->path == NULL || service->hidden == TRUE)
118 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
122 void __connman_service_list(DBusMessageIter *iter, void *user_data)
124 g_sequence_foreach(service_list, append_path, iter);
129 struct connman_service *service;
132 static void compare_path(gpointer value, gpointer user_data)
134 struct connman_service *service = value;
135 struct find_data *data = user_data;
137 if (data->service != NULL)
140 if (g_strcmp0(service->path, data->path) == 0)
141 data->service = service;
144 static struct connman_service *find_service(const char *path)
146 struct find_data data = { .path = path, .service = NULL };
148 DBG("path %s", path);
150 g_sequence_foreach(service_list, compare_path, &data);
155 const char *__connman_service_type2string(enum connman_service_type type)
158 case CONNMAN_SERVICE_TYPE_UNKNOWN:
160 case CONNMAN_SERVICE_TYPE_SYSTEM:
162 case CONNMAN_SERVICE_TYPE_ETHERNET:
164 case CONNMAN_SERVICE_TYPE_WIFI:
166 case CONNMAN_SERVICE_TYPE_WIMAX:
168 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
170 case CONNMAN_SERVICE_TYPE_CELLULAR:
172 case CONNMAN_SERVICE_TYPE_GPS:
174 case CONNMAN_SERVICE_TYPE_VPN:
176 case CONNMAN_SERVICE_TYPE_GADGET:
183 static const char *mode2string(enum connman_service_mode mode)
186 case CONNMAN_SERVICE_MODE_UNKNOWN:
188 case CONNMAN_SERVICE_MODE_MANAGED:
190 case CONNMAN_SERVICE_MODE_ADHOC:
192 case CONNMAN_SERVICE_MODE_GPRS:
194 case CONNMAN_SERVICE_MODE_EDGE:
196 case CONNMAN_SERVICE_MODE_UMTS:
203 static const char *security2string(enum connman_service_security security)
206 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
208 case CONNMAN_SERVICE_SECURITY_NONE:
210 case CONNMAN_SERVICE_SECURITY_WEP:
212 case CONNMAN_SERVICE_SECURITY_PSK:
214 case CONNMAN_SERVICE_SECURITY_8021X:
216 case CONNMAN_SERVICE_SECURITY_WPA:
218 case CONNMAN_SERVICE_SECURITY_RSN:
225 static const char *state2string(enum connman_service_state state)
228 case CONNMAN_SERVICE_STATE_UNKNOWN:
230 case CONNMAN_SERVICE_STATE_IDLE:
232 case CONNMAN_SERVICE_STATE_ASSOCIATION:
233 return "association";
234 case CONNMAN_SERVICE_STATE_CONFIGURATION:
235 return "configuration";
236 case CONNMAN_SERVICE_STATE_READY:
238 case CONNMAN_SERVICE_STATE_ONLINE:
240 case CONNMAN_SERVICE_STATE_DISCONNECT:
242 case CONNMAN_SERVICE_STATE_FAILURE:
249 static const char *error2string(enum connman_service_error error)
252 case CONNMAN_SERVICE_ERROR_UNKNOWN:
254 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
255 return "out-of-range";
256 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
257 return "pin-missing";
258 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
259 return "dhcp-failed";
260 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
261 return "connect-failed";
262 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
263 return "login-failed";
264 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
265 return "auth-failed";
266 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
267 return "invalid-key";
273 static enum connman_service_error string2error(const char *error)
275 if (g_strcmp0(error, "dhcp-failed") == 0)
276 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
277 else if (g_strcmp0(error, "pin-missing") == 0)
278 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
279 else if (g_strcmp0(error, "invalid-key") == 0)
280 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
282 return CONNMAN_SERVICE_ERROR_UNKNOWN;
285 static const char *proxymethod2string(enum connman_service_proxy_method method)
288 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
290 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
292 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
294 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
301 static enum connman_service_proxy_method string2proxymethod(const char *method)
303 if (g_strcmp0(method, "direct") == 0)
304 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
305 else if (g_strcmp0(method, "auto") == 0)
306 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
307 else if (g_strcmp0(method, "manual") == 0)
308 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
310 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
313 static enum connman_service_state combine_state(
314 enum connman_service_state state_a,
315 enum connman_service_state state_b)
317 enum connman_service_state result;
319 if (state_a == state_b) {
324 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
329 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
334 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
339 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
344 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
345 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
346 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
347 state_b == CONNMAN_SERVICE_STATE_READY)
354 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
355 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
356 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
357 state_a == CONNMAN_SERVICE_STATE_READY)
364 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
365 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
366 state_b == CONNMAN_SERVICE_STATE_READY)
373 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
374 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
375 state_a == CONNMAN_SERVICE_STATE_READY)
382 if (state_a == CONNMAN_SERVICE_STATE_READY) {
383 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
390 if (state_b == CONNMAN_SERVICE_STATE_READY) {
391 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
398 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
403 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
408 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
413 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
418 result = CONNMAN_SERVICE_STATE_FAILURE;
424 static connman_bool_t is_connecting_state(struct connman_service *service,
425 enum connman_service_state state)
428 case CONNMAN_SERVICE_STATE_UNKNOWN:
429 case CONNMAN_SERVICE_STATE_IDLE:
430 case CONNMAN_SERVICE_STATE_FAILURE:
431 if (service->network != NULL)
432 return connman_network_get_connecting(service->network);
433 case CONNMAN_SERVICE_STATE_DISCONNECT:
434 case CONNMAN_SERVICE_STATE_READY:
435 case CONNMAN_SERVICE_STATE_ONLINE:
437 case CONNMAN_SERVICE_STATE_ASSOCIATION:
438 case CONNMAN_SERVICE_STATE_CONFIGURATION:
445 static connman_bool_t is_connected_state(const struct connman_service *service,
446 enum connman_service_state state)
449 case CONNMAN_SERVICE_STATE_UNKNOWN:
450 case CONNMAN_SERVICE_STATE_IDLE:
451 case CONNMAN_SERVICE_STATE_ASSOCIATION:
452 case CONNMAN_SERVICE_STATE_CONFIGURATION:
453 case CONNMAN_SERVICE_STATE_DISCONNECT:
454 case CONNMAN_SERVICE_STATE_FAILURE:
456 case CONNMAN_SERVICE_STATE_READY:
457 case CONNMAN_SERVICE_STATE_ONLINE:
464 static connman_bool_t is_connecting(struct connman_service *service)
466 enum connman_service_state state;
468 state = combine_state(service->state_ipv4, service->state_ipv6);
470 return is_connecting_state(service, state);
473 static connman_bool_t is_connected(struct connman_service *service)
475 enum connman_service_state state;
477 state = combine_state(service->state_ipv4, service->state_ipv6);
479 return is_connected_state(service, state);
482 static connman_bool_t is_idle(struct connman_service *service)
484 enum connman_service_state state;
486 state = combine_state(service->state_ipv4, service->state_ipv6);
488 return state == CONNMAN_SERVICE_STATE_IDLE;
491 static void update_nameservers(struct connman_service *service)
495 if (service->ipconfig_ipv4)
496 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
497 else if (service->ipconfig_ipv6)
498 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
505 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
506 case CONNMAN_SERVICE_STATE_UNKNOWN:
507 case CONNMAN_SERVICE_STATE_IDLE:
508 case CONNMAN_SERVICE_STATE_ASSOCIATION:
509 case CONNMAN_SERVICE_STATE_CONFIGURATION:
511 case CONNMAN_SERVICE_STATE_FAILURE:
512 case CONNMAN_SERVICE_STATE_DISCONNECT:
513 connman_resolver_remove_all(ifname);
515 case CONNMAN_SERVICE_STATE_READY:
516 case CONNMAN_SERVICE_STATE_ONLINE:
520 connman_resolver_remove_all(ifname);
522 if (service->nameservers_config != NULL) {
525 for (i = 0; service->nameservers_config[i] != NULL; i++) {
526 connman_resolver_append(ifname, NULL,
527 service->nameservers_config[i]);
529 } else if (service->nameservers != NULL) {
532 for (i = 0; service->nameservers[i] != NULL; i++) {
533 connman_resolver_append(ifname, NULL,
534 service->nameservers[i]);
538 if (service->domains != NULL) {
541 for (i = 0; service->domains[i]; i++)
542 connman_resolver_append(ifname, service->domains[i],
544 } else if (service->domainname != NULL)
545 connman_resolver_append(ifname, service->domainname, NULL);
547 connman_resolver_flush();
550 int __connman_service_nameserver_append(struct connman_service *service,
551 const char *nameserver)
555 DBG("service %p nameserver %s", service, nameserver);
557 if (nameserver == NULL)
560 if (service->nameservers != NULL) {
563 for (i = 0; service->nameservers[i] != NULL; i++)
564 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
567 len = g_strv_length(service->nameservers);
568 service->nameservers = g_try_renew(char *, service->nameservers,
572 service->nameservers = g_try_new0(char *, len + 2);
575 if (service->nameservers == NULL)
578 service->nameservers[len] = g_strdup(nameserver);
579 service->nameservers[len + 1] = NULL;
581 update_nameservers(service);
586 int __connman_service_nameserver_remove(struct connman_service *service,
587 const char *nameserver)
592 DBG("service %p nameserver %s", service, nameserver);
594 if (nameserver == NULL)
597 if (service->nameservers == NULL)
600 len = g_strv_length(service->nameservers);
602 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
605 g_strfreev(service->nameservers);
606 service->nameservers = NULL;
611 servers = g_try_new0(char *, len - 1);
615 for (i = 0, j = 0; i < len; i++) {
616 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
617 servers[j] = g_strdup(service->nameservers[i]);
621 servers[len - 2] = NULL;
623 g_strfreev(service->nameservers);
624 service->nameservers = servers;
626 update_nameservers(service);
631 void __connman_service_nameserver_clear(struct connman_service *service)
633 g_strfreev(service->nameservers);
634 service->nameservers = NULL;
636 update_nameservers(service);
639 static void nameserver_add_routes(int index, char **nameservers,
644 for (i = 0; nameservers[i] != NULL; i++) {
645 if (connman_inet_compare_subnet(index, nameservers[i]))
648 connman_inet_add_host_route(index, nameservers[i], gw);
652 static void nameserver_del_routes(int index, char **nameservers)
656 for (i = 0; nameservers[i] != NULL; i++)
657 connman_inet_del_host_route(index, nameservers[i]);
660 void __connman_service_nameserver_add_routes(struct connman_service *service,
668 if (service->network != NULL)
669 index = connman_network_get_index(service->network);
670 else if (service->provider != NULL)
671 index = connman_provider_get_index(service->provider);
673 if (service->nameservers_config != NULL) {
675 * Configured nameserver takes preference over the
676 * discoverd nameserver gathered from DHCP, VPN, etc.
678 nameserver_add_routes(index, service->nameservers_config, gw);
679 } else if (service->nameservers != NULL) {
681 * We add nameservers host routes for nameservers that
682 * are not on our subnet. For those who are, the subnet
683 * route will be installed by the time the dns proxy code
684 * tries to reach them. The subnet route is installed
685 * when setting the interface IP address.
687 nameserver_add_routes(index, service->nameservers, gw);
691 void __connman_service_nameserver_del_routes(struct connman_service *service)
698 if (service->network != NULL)
699 index = connman_network_get_index(service->network);
700 else if (service->provider != NULL)
701 index = connman_provider_get_index(service->provider);
703 if (service->nameservers_config != NULL)
704 nameserver_del_routes(index, service->nameservers_config);
705 else if (service->nameservers != NULL)
706 nameserver_del_routes(index, service->nameservers);
709 static struct connman_stats *stats_get(struct connman_service *service)
711 if (service->roaming == TRUE)
712 return &service->stats_roaming;
714 return &service->stats;
717 static connman_bool_t stats_enabled(struct connman_service *service)
719 struct connman_stats *stats = stats_get(service);
721 return stats->enabled;
724 static void stats_start(struct connman_service *service)
726 struct connman_stats *stats = stats_get(service);
728 DBG("service %p", service);
730 if (stats->timer == NULL)
733 stats->enabled = TRUE;
734 stats->data_last.time = stats->data.time;
736 g_timer_start(stats->timer);
739 static void stats_stop(struct connman_service *service)
741 struct connman_stats *stats = stats_get(service);
742 unsigned int seconds;
744 DBG("service %p", service);
746 if (stats->timer == NULL)
749 if (stats->enabled == FALSE)
752 g_timer_stop(stats->timer);
754 seconds = g_timer_elapsed(stats->timer, NULL);
755 stats->data.time = stats->data_last.time + seconds;
757 stats->enabled = FALSE;
760 static void reset_stats(struct connman_service *service)
762 DBG("service %p", service);
765 service->stats.valid = FALSE;
767 service->stats.data.rx_packets = 0;
768 service->stats.data.tx_packets = 0;
769 service->stats.data.rx_bytes = 0;
770 service->stats.data.tx_bytes = 0;
771 service->stats.data.rx_errors = 0;
772 service->stats.data.tx_errors = 0;
773 service->stats.data.rx_dropped = 0;
774 service->stats.data.tx_dropped = 0;
775 service->stats.data.time = 0;
776 service->stats.data_last.time = 0;
778 g_timer_reset(service->stats.timer);
781 service->stats_roaming.valid = FALSE;
783 service->stats_roaming.data.rx_packets = 0;
784 service->stats_roaming.data.tx_packets = 0;
785 service->stats_roaming.data.rx_bytes = 0;
786 service->stats_roaming.data.tx_bytes = 0;
787 service->stats_roaming.data.rx_errors = 0;
788 service->stats_roaming.data.tx_errors = 0;
789 service->stats_roaming.data.rx_dropped = 0;
790 service->stats_roaming.data.tx_dropped = 0;
791 service->stats_roaming.data.time = 0;
792 service->stats_roaming.data_last.time = 0;
794 g_timer_reset(service->stats_roaming.timer);
797 static struct connman_service *get_default(void)
799 struct connman_service *service;
802 iter = g_sequence_get_begin_iter(service_list);
804 if (g_sequence_iter_is_end(iter) == TRUE)
807 service = g_sequence_get(iter);
809 if (is_connected(service) == FALSE)
815 static void default_changed(void)
817 struct connman_service *service = get_default();
819 __connman_notifier_default_changed(service);
822 const char *__connman_service_default(void)
824 struct connman_service *service;
826 service = get_default();
830 return __connman_service_type2string(service->type);
833 static void mode_changed(struct connman_service *service)
837 str = mode2string(service->mode);
841 connman_dbus_property_changed_basic(service->path,
842 CONNMAN_SERVICE_INTERFACE, "Mode",
843 DBUS_TYPE_STRING, &str);
846 static void state_changed(struct connman_service *service)
848 enum connman_service_state state;
851 state = combine_state(service->state_ipv4, service->state_ipv6);
853 __connman_notifier_service_state_changed(service, state);
855 str = state2string(state);
859 connman_dbus_property_changed_basic(service->path,
860 CONNMAN_SERVICE_INTERFACE, "State",
861 DBUS_TYPE_STRING, &str);
864 static void strength_changed(struct connman_service *service)
866 if (service->strength == 0)
869 connman_dbus_property_changed_basic(service->path,
870 CONNMAN_SERVICE_INTERFACE, "Strength",
871 DBUS_TYPE_BYTE, &service->strength);
874 static void favorite_changed(struct connman_service *service)
876 if (service->path == NULL)
879 connman_dbus_property_changed_basic(service->path,
880 CONNMAN_SERVICE_INTERFACE, "Favorite",
881 DBUS_TYPE_BOOLEAN, &service->favorite);
884 static void immutable_changed(struct connman_service *service)
886 if (service->path == NULL)
889 connman_dbus_property_changed_basic(service->path,
890 CONNMAN_SERVICE_INTERFACE, "Immutable",
891 DBUS_TYPE_BOOLEAN, &service->immutable);
894 static void roaming_changed(struct connman_service *service)
896 if (service->path == NULL)
899 connman_dbus_property_changed_basic(service->path,
900 CONNMAN_SERVICE_INTERFACE, "Roaming",
901 DBUS_TYPE_BOOLEAN, &service->roaming);
904 static void autoconnect_changed(struct connman_service *service)
906 if (service->path == NULL)
909 connman_dbus_property_changed_basic(service->path,
910 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
911 DBUS_TYPE_BOOLEAN, &service->autoconnect);
914 static void passphrase_changed(struct connman_service *service)
916 dbus_bool_t required;
918 switch (service->type) {
919 case CONNMAN_SERVICE_TYPE_UNKNOWN:
920 case CONNMAN_SERVICE_TYPE_SYSTEM:
921 case CONNMAN_SERVICE_TYPE_ETHERNET:
922 case CONNMAN_SERVICE_TYPE_WIMAX:
923 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
924 case CONNMAN_SERVICE_TYPE_CELLULAR:
925 case CONNMAN_SERVICE_TYPE_GPS:
926 case CONNMAN_SERVICE_TYPE_VPN:
927 case CONNMAN_SERVICE_TYPE_GADGET:
929 case CONNMAN_SERVICE_TYPE_WIFI:
932 switch (service->security) {
933 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
934 case CONNMAN_SERVICE_SECURITY_NONE:
936 case CONNMAN_SERVICE_SECURITY_WEP:
937 case CONNMAN_SERVICE_SECURITY_PSK:
938 case CONNMAN_SERVICE_SECURITY_WPA:
939 case CONNMAN_SERVICE_SECURITY_RSN:
940 if (service->passphrase == NULL)
943 case CONNMAN_SERVICE_SECURITY_8021X:
949 connman_dbus_property_changed_basic(service->path,
950 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
951 DBUS_TYPE_BOOLEAN, &required);
954 static void login_changed(struct connman_service *service)
956 dbus_bool_t required = service->login_required;
958 if (service->path == NULL)
961 connman_dbus_property_changed_basic(service->path,
962 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
963 DBUS_TYPE_BOOLEAN, &required);
966 static void append_security(DBusMessageIter *iter, void *user_data)
968 struct connman_service *service = user_data;
971 str = security2string(service->security);
973 dbus_message_iter_append_basic(iter,
974 DBUS_TYPE_STRING, &str);
977 if (service->wps == TRUE)
978 dbus_message_iter_append_basic(iter,
979 DBUS_TYPE_STRING, &str);
982 static void append_ethernet(DBusMessageIter *iter, void *user_data)
984 struct connman_service *service = user_data;
986 if (service->ipconfig_ipv4 != NULL)
987 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
989 else if (service->ipconfig_ipv6 != NULL)
990 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
994 static void append_ipv4(DBusMessageIter *iter, void *user_data)
996 struct connman_service *service = user_data;
998 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
999 state2string(service->state_ipv4));
1001 if (is_connected_state(service, service->state_ipv4) == FALSE)
1004 if (service->ipconfig_ipv4 != NULL)
1005 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1008 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1010 struct connman_service *service = user_data;
1012 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1013 state2string(service->state_ipv6));
1015 if (is_connected_state(service, service->state_ipv6) == FALSE)
1018 if (service->ipconfig_ipv6 != NULL)
1019 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1020 service->ipconfig_ipv4);
1023 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1025 struct connman_service *service = user_data;
1027 if (service->ipconfig_ipv4 != NULL)
1028 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1032 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1034 struct connman_service *service = user_data;
1036 if (service->ipconfig_ipv6 != NULL)
1037 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1041 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1046 servers = *nameservers;
1048 for (i = 0; servers[i] != NULL; i++) {
1049 dbus_message_iter_append_basic(iter,
1050 DBUS_TYPE_STRING, &servers[i]);
1054 static void append_dns(DBusMessageIter *iter, void *user_data)
1056 struct connman_service *service = user_data;
1058 if (is_connected(service) == FALSE)
1061 if (service->nameservers_config != NULL) {
1062 append_nameserver(iter, &service->nameservers_config);
1064 } else if (service->nameservers != NULL) {
1065 append_nameserver(iter, &service->nameservers);
1070 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1072 struct connman_service *service = user_data;
1075 if (service->nameservers_config == NULL)
1078 for (i = 0; service->nameservers_config[i]; i++) {
1079 dbus_message_iter_append_basic(iter,
1081 &service->nameservers_config[i]);
1085 static void append_domain(DBusMessageIter *iter, void *user_data)
1087 struct connman_service *service = user_data;
1089 if (is_connected(service) == FALSE &&
1090 is_connecting(service) == FALSE)
1093 if (service->domainname == NULL)
1096 dbus_message_iter_append_basic(iter,
1097 DBUS_TYPE_STRING, &service->domainname);
1100 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1102 struct connman_service *service = user_data;
1105 if (service->domains == NULL)
1108 for (i = 0; service->domains[i]; i++)
1109 dbus_message_iter_append_basic(iter,
1110 DBUS_TYPE_STRING, &service->domains[i]);
1113 static void append_proxies(DBusMessageIter *iter, void *user_data)
1115 struct connman_service *service = user_data;
1118 if (service->proxies == NULL)
1121 for (i = 0; service->proxies[i]; i++)
1122 dbus_message_iter_append_basic(iter,
1123 DBUS_TYPE_STRING, &service->proxies[i]);
1126 static void append_excludes(DBusMessageIter *iter, void *user_data)
1128 struct connman_service *service = user_data;
1131 if (service->excludes == NULL)
1134 for (i = 0; service->excludes[i]; i++)
1135 dbus_message_iter_append_basic(iter,
1136 DBUS_TYPE_STRING, &service->excludes[i]);
1139 static void append_proxy(DBusMessageIter *iter, void *user_data)
1141 struct connman_service *service = user_data;
1142 enum connman_service_proxy_method proxy;
1143 const char *pac = NULL;
1144 const char *method = proxymethod2string(
1145 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1149 if (is_connected(service) == FALSE)
1152 proxy = connman_service_get_proxy_method(service);
1155 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1157 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1159 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1160 connman_dbus_dict_append_array(iter, "Servers",
1161 DBUS_TYPE_STRING, append_proxies,
1164 connman_dbus_dict_append_array(iter, "Excludes",
1165 DBUS_TYPE_STRING, append_excludes,
1168 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1169 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1170 if (service->ipconfig_ipv4 != NULL)
1171 pac = __connman_ipconfig_get_proxy_autoconfig(
1172 service->ipconfig_ipv4);
1173 else if (service->ipconfig_ipv6 != NULL)
1174 pac = __connman_ipconfig_get_proxy_autoconfig(
1175 service->ipconfig_ipv6);
1177 if (service->pac == NULL && pac == NULL)
1180 if (service->pac != NULL)
1183 connman_dbus_dict_append_basic(iter, "URL",
1184 DBUS_TYPE_STRING, &pac);
1188 method = proxymethod2string(proxy);
1191 connman_dbus_dict_append_basic(iter, "Method",
1192 DBUS_TYPE_STRING, &method);
1195 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1197 struct connman_service *service = user_data;
1200 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1203 switch (service->proxy_config) {
1204 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1206 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1208 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1209 if (service->proxies != NULL)
1210 connman_dbus_dict_append_array(iter, "Servers",
1212 append_proxies, service);
1214 if (service->excludes != NULL)
1215 connman_dbus_dict_append_array(iter, "Excludes",
1217 append_excludes, service);
1219 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1220 if (service->pac != NULL)
1221 connman_dbus_dict_append_basic(iter, "URL",
1222 DBUS_TYPE_STRING, &service->pac);
1226 method = proxymethod2string(service->proxy_config);
1228 connman_dbus_dict_append_basic(iter, "Method",
1229 DBUS_TYPE_STRING, &method);
1232 static void append_provider(DBusMessageIter *iter, void *user_data)
1234 struct connman_service *service = user_data;
1236 DBG("%p %p", service, service->provider);
1238 if (is_connected(service) == FALSE)
1241 if (service->provider != NULL)
1242 __connman_provider_append_properties(service->provider, iter);
1246 static void settings_changed(struct connman_service *service,
1247 struct connman_ipconfig *ipconfig)
1249 connman_dbus_property_changed_dict(service->path,
1250 CONNMAN_SERVICE_INTERFACE, "IPv4",
1251 append_ipv4, service);
1253 connman_dbus_property_changed_dict(service->path,
1254 CONNMAN_SERVICE_INTERFACE, "IPv6",
1255 append_ipv6, service);
1257 __connman_notifier_ipconfig_changed(service, ipconfig);
1260 static void ipv4_configuration_changed(struct connman_service *service)
1262 connman_dbus_property_changed_dict(service->path,
1263 CONNMAN_SERVICE_INTERFACE,
1264 "IPv4.Configuration",
1269 static void ipv6_configuration_changed(struct connman_service *service)
1271 connman_dbus_property_changed_dict(service->path,
1272 CONNMAN_SERVICE_INTERFACE,
1273 "IPv6.Configuration",
1278 static void dns_changed(struct connman_service *service)
1280 connman_dbus_property_changed_array(service->path,
1281 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1282 DBUS_TYPE_STRING, append_dns, service);
1285 static void dns_configuration_changed(struct connman_service *service)
1287 connman_dbus_property_changed_array(service->path,
1288 CONNMAN_SERVICE_INTERFACE,
1289 "Nameservers.Configuration",
1290 DBUS_TYPE_STRING, append_dnsconfig, service);
1292 dns_changed(service);
1295 static void domain_changed(struct connman_service *service)
1297 connman_dbus_property_changed_array(service->path,
1298 CONNMAN_SERVICE_INTERFACE, "Domains",
1299 DBUS_TYPE_STRING, append_domain, service);
1302 static void domain_configuration_changed(struct connman_service *service)
1304 connman_dbus_property_changed_array(service->path,
1305 CONNMAN_SERVICE_INTERFACE,
1306 "Domains.Configuration",
1307 DBUS_TYPE_STRING, append_domainconfig, service);
1310 static void proxy_changed(struct connman_service *service)
1312 connman_dbus_property_changed_dict(service->path,
1313 CONNMAN_SERVICE_INTERFACE, "Proxy",
1314 append_proxy, service);
1317 static void proxy_configuration_changed(struct connman_service *service)
1319 connman_dbus_property_changed_dict(service->path,
1320 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1321 append_proxyconfig, service);
1323 proxy_changed(service);
1326 static void link_changed(struct connman_service *service)
1328 connman_dbus_property_changed_dict(service->path,
1329 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1330 append_ethernet, service);
1333 static void stats_append_counters(DBusMessageIter *dict,
1334 struct connman_stats_data *stats,
1335 struct connman_stats_data *counters,
1336 connman_bool_t append_all)
1338 if (counters->rx_packets != stats->rx_packets || append_all) {
1339 counters->rx_packets = stats->rx_packets;
1340 connman_dbus_dict_append_basic(dict, "RX.Packets",
1341 DBUS_TYPE_UINT32, &stats->rx_packets);
1344 if (counters->tx_packets != stats->tx_packets || append_all) {
1345 counters->tx_packets = stats->tx_packets;
1346 connman_dbus_dict_append_basic(dict, "TX.Packets",
1347 DBUS_TYPE_UINT32, &stats->tx_packets);
1350 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1351 counters->rx_bytes = stats->rx_bytes;
1352 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1353 DBUS_TYPE_UINT32, &stats->rx_bytes);
1356 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1357 counters->tx_bytes = stats->tx_bytes;
1358 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1359 DBUS_TYPE_UINT32, &stats->tx_bytes);
1362 if (counters->rx_errors != stats->rx_errors || append_all) {
1363 counters->rx_errors = stats->rx_errors;
1364 connman_dbus_dict_append_basic(dict, "RX.Errors",
1365 DBUS_TYPE_UINT32, &stats->rx_errors);
1368 if (counters->tx_errors != stats->tx_errors || append_all) {
1369 counters->tx_errors = stats->tx_errors;
1370 connman_dbus_dict_append_basic(dict, "TX.Errors",
1371 DBUS_TYPE_UINT32, &stats->tx_errors);
1374 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1375 counters->rx_dropped = stats->rx_dropped;
1376 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1377 DBUS_TYPE_UINT32, &stats->rx_dropped);
1380 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1381 counters->tx_dropped = stats->tx_dropped;
1382 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1383 DBUS_TYPE_UINT32, &stats->tx_dropped);
1386 if (counters->time != stats->time || append_all) {
1387 counters->time = stats->time;
1388 connman_dbus_dict_append_basic(dict, "Time",
1389 DBUS_TYPE_UINT32, &stats->time);
1393 static void stats_append(struct connman_service *service,
1394 const char *counter,
1395 struct connman_stats_counter *counters,
1396 connman_bool_t append_all)
1398 DBusMessageIter array, dict;
1401 DBG("service %p counter %s", service, counter);
1403 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1407 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1408 &service->path, DBUS_TYPE_INVALID);
1410 dbus_message_iter_init_append(msg, &array);
1413 connman_dbus_dict_open(&array, &dict);
1415 stats_append_counters(&dict, &service->stats.data,
1416 &counters->stats.data, append_all);
1418 connman_dbus_dict_close(&array, &dict);
1420 /* roaming counter */
1421 connman_dbus_dict_open(&array, &dict);
1423 stats_append_counters(&dict, &service->stats_roaming.data,
1424 &counters->stats_roaming.data, append_all);
1426 connman_dbus_dict_close(&array, &dict);
1428 __connman_counter_send_usage(counter, msg);
1431 static void stats_update(struct connman_service *service,
1432 unsigned int rx_packets, unsigned int tx_packets,
1433 unsigned int rx_bytes, unsigned int tx_bytes,
1434 unsigned int rx_errors, unsigned int tx_errors,
1435 unsigned int rx_dropped, unsigned int tx_dropped)
1437 struct connman_stats *stats = stats_get(service);
1438 struct connman_stats_data *data_last = &stats->data_last;
1439 struct connman_stats_data *data = &stats->data;
1440 unsigned int seconds;
1442 DBG("service %p", service);
1444 if (stats->valid == TRUE) {
1446 rx_packets - data_last->rx_packets;
1448 tx_packets - data_last->tx_packets;
1450 rx_bytes - data_last->rx_bytes;
1452 tx_bytes - data_last->tx_bytes;
1454 rx_errors - data_last->rx_errors;
1456 tx_errors - data_last->tx_errors;
1458 rx_dropped - data_last->rx_dropped;
1460 tx_dropped - data_last->tx_dropped;
1462 stats->valid = TRUE;
1465 data_last->rx_packets = rx_packets;
1466 data_last->tx_packets = tx_packets;
1467 data_last->rx_bytes = rx_bytes;
1468 data_last->tx_bytes = tx_bytes;
1469 data_last->rx_errors = rx_errors;
1470 data_last->tx_errors = tx_errors;
1471 data_last->rx_dropped = rx_dropped;
1472 data_last->tx_dropped = tx_dropped;
1474 seconds = g_timer_elapsed(stats->timer, NULL);
1475 stats->data.time = stats->data_last.time + seconds;
1478 void __connman_service_notify(struct connman_service *service,
1479 unsigned int rx_packets, unsigned int tx_packets,
1480 unsigned int rx_bytes, unsigned int tx_bytes,
1481 unsigned int rx_errors, unsigned int tx_errors,
1482 unsigned int rx_dropped, unsigned int tx_dropped)
1484 GHashTableIter iter;
1485 gpointer key, value;
1486 const char *counter;
1487 struct connman_stats_counter *counters;
1488 struct connman_stats_data *data;
1491 if (service == NULL)
1494 if (is_connected(service) == FALSE)
1497 stats_update(service,
1498 rx_packets, tx_packets,
1500 rx_errors, tx_errors,
1501 rx_dropped, tx_dropped);
1503 data = &stats_get(service)->data;
1504 err = __connman_stats_update(service, service->roaming, data);
1506 connman_error("Failed to store statistics for %s",
1507 service->identifier);
1509 g_hash_table_iter_init(&iter, service->counter_table);
1510 while (g_hash_table_iter_next(&iter, &key, &value)) {
1514 stats_append(service, counter, counters, counters->append_all);
1515 counters->append_all = FALSE;
1519 int __connman_service_counter_register(const char *counter)
1521 struct connman_service *service;
1522 GSequenceIter *iter;
1523 struct connman_stats_counter *counters;
1525 DBG("counter %s", counter);
1527 counter_list = g_slist_append(counter_list, (gpointer)counter);
1529 iter = g_sequence_get_begin_iter(service_list);
1531 while (g_sequence_iter_is_end(iter) == FALSE) {
1532 service = g_sequence_get(iter);
1534 counters = g_try_new0(struct connman_stats_counter, 1);
1535 if (counters == NULL)
1538 counters->append_all = TRUE;
1540 g_hash_table_replace(service->counter_table, (gpointer)counter,
1543 iter = g_sequence_iter_next(iter);
1549 void __connman_service_counter_unregister(const char *counter)
1551 struct connman_service *service;
1552 GSequenceIter *iter;
1554 DBG("counter %s", counter);
1556 iter = g_sequence_get_begin_iter(service_list);
1558 while (g_sequence_iter_is_end(iter) == FALSE) {
1559 service = g_sequence_get(iter);
1561 g_hash_table_remove(service->counter_table, counter);
1563 iter = g_sequence_iter_next(iter);
1566 counter_list = g_slist_remove(counter_list, counter);
1569 GSequence *__connman_service_get_list(struct connman_session *session,
1570 service_match_cb service_match)
1573 GSequenceIter *iter;
1574 struct connman_service *service;
1576 list = g_sequence_new(NULL);
1580 iter = g_sequence_get_begin_iter(service_list);
1582 while (g_sequence_iter_is_end(iter) == FALSE) {
1583 service = g_sequence_get(iter);
1585 if (service_match(session, service) == TRUE)
1586 g_sequence_append(list, service);
1588 iter = g_sequence_iter_next(iter);
1594 connman_bool_t __connman_service_is_connecting(struct connman_service *service)
1596 return is_connecting(service);
1599 connman_bool_t __connman_service_is_connected(struct connman_service *service)
1601 return is_connected(service);
1604 connman_bool_t __connman_service_is_idle(struct connman_service *service)
1606 return is_idle(service);
1609 const char *__connman_service_get_name(struct connman_service *service)
1611 return service->name;
1614 void __connman_service_session_inc(struct connman_service *service)
1616 DBG("service %p", service);
1618 g_atomic_int_inc(&service->session_usage_count);
1621 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1623 connman_bool_t in_use;
1625 if (g_atomic_int_dec_and_test(&service->session_usage_count) == TRUE)
1630 DBG("service %p last %d", service, in_use);
1635 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1636 struct connman_service *service)
1638 dbus_bool_t required;
1641 str = __connman_service_type2string(service->type);
1643 connman_dbus_dict_append_basic(dict, "Type",
1644 DBUS_TYPE_STRING, &str);
1646 str = mode2string(service->mode);
1648 connman_dbus_dict_append_basic(dict, "Mode",
1649 DBUS_TYPE_STRING, &str);
1651 connman_dbus_dict_append_array(dict, "Security",
1652 DBUS_TYPE_STRING, append_security, service);
1654 str = state2string(combine_state(service->state_ipv4,
1655 service->state_ipv6));
1657 connman_dbus_dict_append_basic(dict, "State",
1658 DBUS_TYPE_STRING, &str);
1660 str = error2string(service->error);
1662 connman_dbus_dict_append_basic(dict, "Error",
1663 DBUS_TYPE_STRING, &str);
1665 if (service->strength > 0)
1666 connman_dbus_dict_append_basic(dict, "Strength",
1667 DBUS_TYPE_BYTE, &service->strength);
1669 connman_dbus_dict_append_basic(dict, "Favorite",
1670 DBUS_TYPE_BOOLEAN, &service->favorite);
1672 connman_dbus_dict_append_basic(dict, "Immutable",
1673 DBUS_TYPE_BOOLEAN, &service->immutable);
1675 if (service->favorite == TRUE)
1676 connman_dbus_dict_append_basic(dict, "AutoConnect",
1677 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1679 connman_dbus_dict_append_basic(dict, "AutoConnect",
1680 DBUS_TYPE_BOOLEAN, &service->favorite);
1682 if (service->name != NULL)
1683 connman_dbus_dict_append_basic(dict, "Name",
1684 DBUS_TYPE_STRING, &service->name);
1686 connman_dbus_dict_append_basic(dict, "LoginRequired",
1687 DBUS_TYPE_BOOLEAN, &service->login_required);
1689 switch (service->type) {
1690 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1691 case CONNMAN_SERVICE_TYPE_SYSTEM:
1692 case CONNMAN_SERVICE_TYPE_GPS:
1693 case CONNMAN_SERVICE_TYPE_VPN:
1694 case CONNMAN_SERVICE_TYPE_GADGET:
1696 case CONNMAN_SERVICE_TYPE_CELLULAR:
1697 connman_dbus_dict_append_basic(dict, "Roaming",
1698 DBUS_TYPE_BOOLEAN, &service->roaming);
1701 connman_dbus_dict_append_basic(dict, "SetupRequired",
1702 DBUS_TYPE_BOOLEAN, &required);
1703 connman_dbus_dict_append_dict(dict, "Ethernet",
1704 append_ethernet, service);
1706 case CONNMAN_SERVICE_TYPE_WIFI:
1707 if (service->passphrase != NULL && limited == FALSE)
1708 connman_dbus_dict_append_basic(dict, "Passphrase",
1709 DBUS_TYPE_STRING, &service->passphrase);
1713 switch (service->security) {
1714 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1715 case CONNMAN_SERVICE_SECURITY_NONE:
1717 case CONNMAN_SERVICE_SECURITY_WEP:
1718 case CONNMAN_SERVICE_SECURITY_PSK:
1719 case CONNMAN_SERVICE_SECURITY_WPA:
1720 case CONNMAN_SERVICE_SECURITY_RSN:
1721 if (service->passphrase == NULL)
1724 case CONNMAN_SERVICE_SECURITY_8021X:
1728 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1729 DBUS_TYPE_BOOLEAN, &required);
1732 case CONNMAN_SERVICE_TYPE_ETHERNET:
1733 case CONNMAN_SERVICE_TYPE_WIMAX:
1734 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1735 connman_dbus_dict_append_dict(dict, "Ethernet",
1736 append_ethernet, service);
1740 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1742 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1743 append_ipv4config, service);
1745 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1747 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1748 append_ipv6config, service);
1750 connman_dbus_dict_append_array(dict, "Nameservers",
1751 DBUS_TYPE_STRING, append_dns, service);
1753 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1754 DBUS_TYPE_STRING, append_dnsconfig, service);
1756 connman_dbus_dict_append_array(dict, "Domains",
1757 DBUS_TYPE_STRING, append_domain, service);
1759 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1760 DBUS_TYPE_STRING, append_domainconfig, service);
1762 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1764 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1765 append_proxyconfig, service);
1767 connman_dbus_dict_append_dict(dict, "Provider",
1768 append_provider, service);
1771 static void append_struct(gpointer value, gpointer user_data)
1773 struct connman_service *service = value;
1774 DBusMessageIter *iter = user_data;
1775 DBusMessageIter entry, dict;
1777 if (service->path == NULL || service->hidden == TRUE)
1780 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1782 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1785 connman_dbus_dict_open(&entry, &dict);
1786 append_properties(&dict, TRUE, service);
1787 connman_dbus_dict_close(&entry, &dict);
1789 dbus_message_iter_close_container(iter, &entry);
1792 void __connman_service_list_struct(DBusMessageIter *iter)
1794 g_sequence_foreach(service_list, append_struct, iter);
1797 int __connman_service_get_index(struct connman_service *service)
1799 if (service == NULL)
1802 if (service->network != NULL)
1803 return connman_network_get_index(service->network);
1804 else if (service->provider != NULL)
1805 return connman_provider_get_index(service->provider);
1810 void __connman_service_set_domainname(struct connman_service *service,
1811 const char *domainname)
1813 if (service == NULL)
1816 g_free(service->domainname);
1817 service->domainname = g_strdup(domainname);
1819 domain_changed(service);
1822 const char *connman_service_get_domainname(struct connman_service *service)
1824 if (service == NULL)
1827 if (service->domains != NULL)
1828 return service->domains[0];
1830 return service->domainname;
1833 char **connman_service_get_nameservers(struct connman_service *service)
1835 if (service == NULL)
1838 if (service->nameservers_config != NULL)
1839 return service->nameservers_config;
1840 else if (service->nameservers != NULL)
1841 return service->nameservers;
1846 void connman_service_set_proxy_method(struct connman_service *service,
1847 enum connman_service_proxy_method method)
1849 if (service == NULL)
1852 service->proxy = method;
1854 proxy_changed(service);
1856 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1857 __connman_notifier_proxy_changed(service);
1860 enum connman_service_proxy_method connman_service_get_proxy_method(
1861 struct connman_service *service)
1863 if (service == NULL)
1864 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1866 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1867 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1868 service->pac == NULL)
1869 return service->proxy;
1871 return service->proxy_config;
1874 return service->proxy;
1877 char **connman_service_get_proxy_servers(struct connman_service *service)
1879 return g_strdupv(service->proxies);
1882 char **connman_service_get_proxy_excludes(struct connman_service *service)
1884 return g_strdupv(service->excludes);
1887 const char *connman_service_get_proxy_url(struct connman_service *service)
1889 if (service == NULL)
1892 return service->pac;
1895 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1898 if (service == NULL)
1901 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1903 if (service->ipconfig_ipv4) {
1904 if (__connman_ipconfig_set_proxy_autoconfig(
1905 service->ipconfig_ipv4, url) < 0)
1907 } else if (service->ipconfig_ipv6) {
1908 if (__connman_ipconfig_set_proxy_autoconfig(
1909 service->ipconfig_ipv6, url) < 0)
1914 proxy_changed(service);
1916 __connman_notifier_proxy_changed(service);
1919 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1921 if (service == NULL)
1924 if (service->ipconfig_ipv4)
1925 return __connman_ipconfig_get_proxy_autoconfig(
1926 service->ipconfig_ipv4);
1927 else if (service->ipconfig_ipv6)
1928 return __connman_ipconfig_get_proxy_autoconfig(
1929 service->ipconfig_ipv6);
1933 static void update_timeservers(struct connman_service *service)
1937 if (service->timeservers == NULL)
1940 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
1941 case CONNMAN_SERVICE_STATE_UNKNOWN:
1942 case CONNMAN_SERVICE_STATE_IDLE:
1943 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1944 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1946 case CONNMAN_SERVICE_STATE_FAILURE:
1947 case CONNMAN_SERVICE_STATE_DISCONNECT:
1948 for (i = 0; service->timeservers[i] != NULL; i++)
1949 connman_timeserver_remove(service->timeservers[i]);
1951 case CONNMAN_SERVICE_STATE_READY:
1952 case CONNMAN_SERVICE_STATE_ONLINE:
1956 for (i = 0; service->timeservers[i] != NULL; i++)
1957 connman_timeserver_append(service->timeservers[i]);
1960 int __connman_service_timeserver_append(struct connman_service *service,
1961 const char *timeserver)
1965 DBG("service %p timeserver %s", service, timeserver);
1967 if (timeserver == NULL)
1970 if (service->timeservers != NULL) {
1973 for (i = 0; service->timeservers[i] != NULL; i++)
1974 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1977 len = g_strv_length(service->timeservers);
1978 service->timeservers = g_try_renew(char *, service->timeservers,
1982 service->timeservers = g_try_new0(char *, len + 2);
1985 if (service->timeservers == NULL)
1988 service->timeservers[len] = g_strdup(timeserver);
1989 service->timeservers[len + 1] = NULL;
1991 update_timeservers(service);
1996 int __connman_service_timeserver_remove(struct connman_service *service,
1997 const char *timeserver)
2002 DBG("service %p timeserver %s", service, timeserver);
2004 if (timeserver == NULL)
2007 if (service->timeservers == NULL)
2010 len = g_strv_length(service->timeservers);
2012 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
2015 g_strfreev(service->timeservers);
2016 service->timeservers = NULL;
2021 servers = g_try_new0(char *, len - 1);
2022 if (servers == NULL)
2025 for (i = 0, j = 0; i < len; i++) {
2026 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2027 servers[j] = g_strdup(service->timeservers[i]);
2031 servers[len - 2] = NULL;
2033 g_strfreev(service->timeservers);
2034 service->timeservers = servers;
2036 update_timeservers(service);
2041 void __connman_service_set_pac(struct connman_service *service,
2047 g_free(service->pac);
2048 service->pac = g_strdup(pac);
2051 void __connman_service_set_identity(struct connman_service *service,
2052 const char *identity)
2054 if (service->immutable)
2057 g_free(service->identity);
2058 service->identity = g_strdup(identity);
2060 if (service->network != NULL)
2061 connman_network_set_string(service->network,
2066 void __connman_service_set_passphrase(struct connman_service *service,
2067 const char* passphrase)
2069 if (service->immutable == TRUE)
2072 g_free(service->passphrase);
2073 service->passphrase = g_strdup(passphrase);
2075 passphrase_changed(service);
2077 if (service->network != NULL)
2078 connman_network_set_string(service->network,
2080 service->passphrase);
2082 __connman_storage_save_service(service);
2085 static DBusMessage *get_properties(DBusConnection *conn,
2086 DBusMessage *msg, void *user_data)
2088 struct connman_service *service = user_data;
2090 DBusMessageIter array, dict;
2092 DBG("service %p", service);
2094 reply = dbus_message_new_method_return(msg);
2098 dbus_message_iter_init_append(reply, &array);
2100 connman_dbus_dict_open(&array, &dict);
2101 append_properties(&dict, FALSE, service);
2102 connman_dbus_dict_close(&array, &dict);
2107 static int update_proxy_configuration(struct connman_service *service,
2108 DBusMessageIter *array)
2110 DBusMessageIter dict;
2111 enum connman_service_proxy_method method;
2112 GString *servers_str = NULL;
2113 GString *excludes_str = NULL;
2114 const char *url = NULL;
2116 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2118 dbus_message_iter_recurse(array, &dict);
2120 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2121 DBusMessageIter entry, variant;
2125 dbus_message_iter_recurse(&dict, &entry);
2127 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2130 dbus_message_iter_get_basic(&entry, &key);
2131 dbus_message_iter_next(&entry);
2133 if (dbus_message_iter_get_arg_type(&entry) !=
2137 dbus_message_iter_recurse(&entry, &variant);
2138 type = dbus_message_iter_get_arg_type(&variant);
2140 if (g_str_equal(key, "Method") == TRUE) {
2143 if (type != DBUS_TYPE_STRING)
2146 dbus_message_iter_get_basic(&variant, &val);
2147 method = string2proxymethod(val);
2148 } else if (g_str_equal(key, "URL") == TRUE) {
2149 if (type != DBUS_TYPE_STRING)
2152 dbus_message_iter_get_basic(&variant, &url);
2153 } else if (g_str_equal(key, "Servers") == TRUE) {
2154 DBusMessageIter str_array;
2156 if (type != DBUS_TYPE_ARRAY)
2159 servers_str = g_string_new(NULL);
2160 if (servers_str == NULL)
2163 dbus_message_iter_recurse(&variant, &str_array);
2165 while (dbus_message_iter_get_arg_type(&str_array) ==
2169 dbus_message_iter_get_basic(&str_array, &val);
2171 if (servers_str->len > 0)
2172 g_string_append_printf(servers_str,
2175 g_string_append(servers_str, val);
2177 dbus_message_iter_next(&str_array);
2179 } else if (g_str_equal(key, "Excludes") == TRUE) {
2180 DBusMessageIter str_array;
2182 if (type != DBUS_TYPE_ARRAY)
2185 excludes_str = g_string_new(NULL);
2186 if (excludes_str == NULL)
2189 dbus_message_iter_recurse(&variant, &str_array);
2191 while (dbus_message_iter_get_arg_type(&str_array) ==
2195 dbus_message_iter_get_basic(&str_array, &val);
2197 if (excludes_str->len > 0)
2198 g_string_append_printf(excludes_str,
2201 g_string_append(excludes_str, val);
2203 dbus_message_iter_next(&str_array);
2207 dbus_message_iter_next(&dict);
2211 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2213 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2214 if (servers_str == NULL && service->proxies == NULL)
2217 if (servers_str != NULL) {
2218 g_strfreev(service->proxies);
2220 if (servers_str->len > 0)
2221 service->proxies = g_strsplit_set(
2222 servers_str->str, " ", 0);
2224 service->proxies = NULL;
2227 if (excludes_str != NULL) {
2228 g_strfreev(service->excludes);
2230 if (excludes_str->len > 0)
2231 service->excludes = g_strsplit_set(
2232 excludes_str->str, " ", 0);
2234 service->excludes = NULL;
2237 if (service->proxies == NULL)
2238 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2241 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2242 g_free(service->pac);
2244 if (url != NULL && strlen(url) > 0)
2245 service->pac = g_strdup(url);
2247 service->pac = NULL;
2249 /* if we are connected:
2250 - if service->pac == NULL
2251 - if __connman_ipconfig_get_proxy_autoconfig(
2252 service->ipconfig) == NULL
2253 --> We should start WPAD */
2256 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2260 if (servers_str != NULL)
2261 g_string_free(servers_str, TRUE);
2263 if (excludes_str != NULL)
2264 g_string_free(excludes_str, TRUE);
2266 service->proxy_config = method;
2271 if (servers_str != NULL)
2272 g_string_free(servers_str, TRUE);
2274 if (excludes_str != NULL)
2275 g_string_free(excludes_str, TRUE);
2280 static int set_ipconfig(struct connman_service *service,
2281 struct connman_ipconfig *ipconfig,
2282 DBusMessageIter *array,
2283 enum connman_service_state state,
2284 enum connman_service_state *new_state)
2286 enum connman_ipconfig_method old_method;
2287 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2288 enum connman_ipconfig_type type;
2291 old_method = __connman_ipconfig_get_method(ipconfig);
2293 if (is_connecting_state(service, state) ||
2294 is_connected_state(service, state))
2295 __connman_network_clear_ipconfig(service->network, ipconfig);
2297 err = __connman_ipconfig_set_config(ipconfig, array);
2298 method = __connman_ipconfig_get_method(ipconfig);
2299 type = __connman_ipconfig_get_config_type(ipconfig);
2301 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2302 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2303 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2304 *new_state = service->state_ipv4 =
2305 CONNMAN_SERVICE_STATE_CONFIGURATION;
2306 __connman_ipconfig_enable(ipconfig);
2309 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2310 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2311 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2312 *new_state = service->state_ipv6;
2313 __connman_ipconfig_enable(ipconfig);
2317 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2318 type, method, state2string(*new_state));
2323 static DBusMessage *set_property(DBusConnection *conn,
2324 DBusMessage *msg, void *user_data)
2326 struct connman_service *service = user_data;
2327 DBusMessageIter iter, value;
2331 DBG("service %p", service);
2333 if (dbus_message_iter_init(msg, &iter) == FALSE)
2334 return __connman_error_invalid_arguments(msg);
2336 dbus_message_iter_get_basic(&iter, &name);
2337 dbus_message_iter_next(&iter);
2338 dbus_message_iter_recurse(&iter, &value);
2340 type = dbus_message_iter_get_arg_type(&value);
2342 if (g_str_equal(name, "AutoConnect") == TRUE) {
2343 connman_bool_t autoconnect;
2345 if (type != DBUS_TYPE_BOOLEAN)
2346 return __connman_error_invalid_arguments(msg);
2348 if (service->favorite == FALSE)
2349 return __connman_error_invalid_service(msg);
2351 dbus_message_iter_get_basic(&value, &autoconnect);
2353 if (service->autoconnect == autoconnect)
2354 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2356 service->autoconnect = autoconnect;
2358 autoconnect_changed(service);
2360 __connman_storage_save_service(service);
2361 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2362 const char *passphrase;
2364 if (type != DBUS_TYPE_STRING)
2365 return __connman_error_invalid_arguments(msg);
2367 if (service->immutable == TRUE)
2368 return __connman_error_not_supported(msg);
2370 dbus_message_iter_get_basic(&value, &passphrase);
2372 __connman_service_set_passphrase(service, passphrase);
2373 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2374 DBusMessageIter entry;
2379 if (type != DBUS_TYPE_ARRAY)
2380 return __connman_error_invalid_arguments(msg);
2382 str = g_string_new(NULL);
2384 return __connman_error_invalid_arguments(msg);
2386 index = connman_network_get_index(service->network);
2387 gw = __connman_ipconfig_get_gateway_from_index(index);
2389 if (gw && strlen(gw))
2390 __connman_service_nameserver_del_routes(service);
2392 dbus_message_iter_recurse(&value, &entry);
2394 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2396 dbus_message_iter_get_basic(&entry, &val);
2397 dbus_message_iter_next(&entry);
2399 g_string_append_printf(str, " %s", val);
2401 g_string_append(str, val);
2404 g_strfreev(service->nameservers_config);
2407 service->nameservers_config =
2408 g_strsplit_set(str->str, " ", 0);
2410 service->nameservers_config = NULL;
2413 g_string_free(str, TRUE);
2415 if (gw && strlen(gw))
2416 __connman_service_nameserver_add_routes(service, gw);
2418 update_nameservers(service);
2419 dns_configuration_changed(service);
2421 __connman_storage_save_service(service);
2422 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2423 DBusMessageIter entry;
2426 if (type != DBUS_TYPE_ARRAY)
2427 return __connman_error_invalid_arguments(msg);
2429 str = g_string_new(NULL);
2431 return __connman_error_invalid_arguments(msg);
2433 dbus_message_iter_recurse(&value, &entry);
2435 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2437 dbus_message_iter_get_basic(&entry, &val);
2438 dbus_message_iter_next(&entry);
2440 g_string_append_printf(str, " %s", val);
2442 g_string_append(str, val);
2445 g_strfreev(service->domains);
2448 service->domains = g_strsplit_set(str->str, " ", 0);
2450 service->domains = NULL;
2452 g_string_free(str, TRUE);
2454 update_nameservers(service);
2455 domain_configuration_changed(service);
2457 __connman_storage_save_service(service);
2458 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2461 if (type != DBUS_TYPE_ARRAY)
2462 return __connman_error_invalid_arguments(msg);
2464 err = update_proxy_configuration(service, &value);
2467 return __connman_error_failed(msg, -err);
2469 proxy_configuration_changed(service);
2471 __connman_storage_save_service(service);
2472 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2473 g_str_equal(name, "IPv6.Configuration")) {
2475 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2476 enum connman_service_state state =
2477 CONNMAN_SERVICE_STATE_UNKNOWN;
2482 if (service->ipconfig_ipv4 == NULL &&
2483 service->ipconfig_ipv6 == NULL)
2484 return __connman_error_invalid_property(msg);
2486 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2487 ipv4 = service->ipconfig_ipv4;
2488 err = set_ipconfig(service, ipv4, &value,
2489 service->state_ipv4, &state);
2491 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2492 ipv6 = service->ipconfig_ipv6;
2493 err = set_ipconfig(service, ipv6, &value,
2494 service->state_ipv6, &state);
2498 if (is_connected_state(service, state) ||
2499 is_connecting_state(service, state))
2500 __connman_network_set_ipconfig(service->network,
2502 return __connman_error_failed(msg, -err);
2506 ipv4_configuration_changed(service);
2508 ipv6_configuration_changed(service);
2510 if (is_connecting(service) || is_connected(service))
2511 __connman_network_set_ipconfig(service->network,
2514 __connman_storage_save_service(service);
2516 return __connman_error_invalid_property(msg);
2518 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2521 static void set_idle(struct connman_service *service)
2523 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
2524 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2525 state_changed(service);
2528 static DBusMessage *clear_property(DBusConnection *conn,
2529 DBusMessage *msg, void *user_data)
2531 struct connman_service *service = user_data;
2534 DBG("service %p", service);
2536 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2539 if (g_str_equal(name, "Error") == TRUE) {
2542 g_get_current_time(&service->modified);
2543 __connman_storage_save_service(service);
2544 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2545 if (service->immutable == TRUE)
2546 return __connman_error_not_supported(msg);
2548 g_free(service->passphrase);
2549 service->passphrase = NULL;
2551 passphrase_changed(service);
2553 __connman_storage_save_service(service);
2555 return __connman_error_invalid_property(msg);
2557 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2560 static connman_bool_t is_ignore(struct connman_service *service)
2562 if (service->autoconnect == FALSE)
2565 if (service->roaming == TRUE)
2568 if (service->ignore == TRUE)
2571 if (combine_state(service->state_ipv4, service->state_ipv6) ==
2572 CONNMAN_SERVICE_STATE_FAILURE)
2578 void __connman_service_auto_connect(void)
2580 struct connman_service *service = NULL;
2581 GSequenceIter *iter;
2585 if (__connman_session_mode() == TRUE) {
2586 DBG("Session mode enabled: auto connect disabled");
2590 iter = g_sequence_get_begin_iter(service_list);
2592 while (g_sequence_iter_is_end(iter) == FALSE) {
2593 service = g_sequence_get(iter);
2595 if (service->pending != NULL)
2598 if (is_connecting(service) == TRUE)
2601 if (service->favorite == FALSE)
2604 if (is_connected(service) == TRUE)
2607 if (is_ignore(service) == FALSE &&
2608 combine_state(service->state_ipv4,
2609 service->state_ipv6) ==
2610 CONNMAN_SERVICE_STATE_IDLE)
2615 iter = g_sequence_iter_next(iter);
2618 if (service != NULL) {
2619 service->userconnect = FALSE;
2620 __connman_service_connect(service);
2624 static void remove_timeout(struct connman_service *service)
2626 if (service->timeout > 0) {
2627 g_source_remove(service->timeout);
2628 service->timeout = 0;
2632 static void reply_pending(struct connman_service *service, int error)
2634 remove_timeout(service);
2636 if (service->pending != NULL) {
2640 reply = __connman_error_failed(service->pending,
2643 g_dbus_send_message(connection, reply);
2647 sender = dbus_message_get_interface(service->pending);
2649 DBG("sender %s", sender);
2651 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2652 g_dbus_send_reply(connection, service->pending,
2653 DBUS_TYPE_OBJECT_PATH, &service->path,
2656 g_dbus_send_reply(connection, service->pending,
2660 dbus_message_unref(service->pending);
2661 service->pending = NULL;
2665 static gboolean connect_timeout(gpointer user_data)
2667 struct connman_service *service = user_data;
2668 connman_bool_t autoconnect = FALSE;
2670 DBG("service %p", service);
2672 service->timeout = 0;
2674 if (service->network != NULL)
2675 __connman_network_disconnect(service->network);
2677 __connman_ipconfig_disable(service->ipconfig_ipv4);
2678 __connman_ipconfig_disable(service->ipconfig_ipv6);
2680 __connman_stats_service_unregister(service);
2682 if (service->pending != NULL) {
2685 reply = __connman_error_operation_timeout(service->pending);
2687 g_dbus_send_message(connection, reply);
2689 dbus_message_unref(service->pending);
2690 service->pending = NULL;
2694 __connman_service_indicate_state(service,
2695 CONNMAN_SERVICE_STATE_FAILURE,
2696 CONNMAN_IPCONFIG_TYPE_IPV4);
2697 __connman_service_indicate_state(service,
2698 CONNMAN_SERVICE_STATE_FAILURE,
2699 CONNMAN_IPCONFIG_TYPE_IPV6);
2701 if (autoconnect == TRUE && service->userconnect == FALSE)
2702 __connman_service_auto_connect();
2707 static void set_reconnect_state(struct connman_service *service,
2708 connman_bool_t reconnect)
2710 struct connman_device *device;
2712 if (service->network == NULL)
2715 device = connman_network_get_device(service->network);
2719 __connman_device_set_reconnect(device, reconnect);
2722 static connman_bool_t get_reconnect_state(struct connman_service *service)
2724 struct connman_device *device;
2726 if (service->network == NULL)
2729 device = connman_network_get_device(service->network);
2733 return __connman_device_get_reconnect(device);
2736 static void request_input_cb (struct connman_service *service,
2737 const char *identity, const char *passphrase,
2740 DBG ("RequestInput return, %p", service);
2742 if (identity == NULL && passphrase == NULL && service->wps == FALSE)
2745 if (identity != NULL)
2746 __connman_service_set_identity(service, identity);
2748 if (passphrase != NULL)
2749 __connman_service_set_passphrase(service, passphrase);
2751 __connman_service_connect(service);
2754 static DBusMessage *connect_service(DBusConnection *conn,
2755 DBusMessage *msg, void *user_data)
2757 struct connman_service *service = user_data;
2758 GSequenceIter *iter;
2761 DBG("service %p", service);
2763 if (service->pending != NULL)
2764 return __connman_error_in_progress(msg);
2766 iter = g_sequence_get_begin_iter(service_list);
2768 while (g_sequence_iter_is_end(iter) == FALSE) {
2769 struct connman_service *temp = g_sequence_get(iter);
2771 if (service->type == temp->type && is_connecting(temp) == TRUE)
2772 return __connman_error_in_progress(msg);
2774 iter = g_sequence_iter_next(iter);
2777 service->ignore = FALSE;
2779 service->userconnect = TRUE;
2781 service->pending = dbus_message_ref(msg);
2783 set_reconnect_state(service, FALSE);
2785 err = __connman_service_connect(service);
2787 if (err == -ENOKEY) {
2788 if (__connman_agent_request_input(service,
2794 if (service->pending == NULL)
2797 if (err != -EINPROGRESS) {
2798 dbus_message_unref(service->pending);
2799 service->pending = NULL;
2801 return __connman_error_failed(msg, -err);
2807 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2810 static DBusMessage *disconnect_service(DBusConnection *conn,
2811 DBusMessage *msg, void *user_data)
2813 struct connman_service *service = user_data;
2816 DBG("service %p", service);
2818 reply_pending(service, ECONNABORTED);
2820 service->ignore = TRUE;
2822 set_reconnect_state(service, FALSE);
2824 err = __connman_service_disconnect(service);
2826 if (err != -EINPROGRESS)
2827 return __connman_error_failed(msg, -err);
2832 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2835 static DBusMessage *remove_service(DBusConnection *conn,
2836 DBusMessage *msg, void *user_data)
2838 struct connman_service *service = user_data;
2840 DBG("service %p", service);
2842 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2843 return __connman_error_not_supported(msg);
2845 if (service->immutable == TRUE)
2846 return __connman_error_not_supported(msg);
2848 if (service->favorite == FALSE &&
2849 combine_state(service->state_ipv4, service->state_ipv6) !=
2850 CONNMAN_SERVICE_STATE_FAILURE)
2851 return __connman_error_not_supported(msg);
2853 if (service->network != NULL) {
2854 set_reconnect_state(service, FALSE);
2856 __connman_network_disconnect(service->network);
2859 g_free(service->passphrase);
2860 service->passphrase = NULL;
2862 passphrase_changed(service);
2866 __connman_service_set_favorite(service, FALSE);
2867 __connman_storage_save_service(service);
2869 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2872 static DBusMessage *move_service(DBusConnection *conn,
2873 DBusMessage *msg, void *user_data,
2876 struct connman_service *service = user_data;
2877 struct connman_service *target;
2879 GSequenceIter *src, *dst;
2881 DBG("service %p", service);
2883 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2886 if (service->favorite == FALSE)
2887 return __connman_error_not_supported(msg);
2889 target = find_service(path);
2890 if (target == NULL || target->favorite == FALSE || target == service)
2891 return __connman_error_invalid_service(msg);
2893 DBG("target %s", target->identifier);
2895 if (target->state_ipv4 != service->state_ipv4 &&
2896 target->state_ipv6 != service->state_ipv6)
2897 return __connman_error_invalid_service(msg);
2899 g_get_current_time(&service->modified);
2900 __connman_storage_save_service(service);
2902 src = g_hash_table_lookup(service_hash, service->identifier);
2903 dst = g_hash_table_lookup(service_hash, target->identifier);
2905 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2907 __connman_profile_changed(FALSE);
2909 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2912 static DBusMessage *move_before(DBusConnection *conn,
2913 DBusMessage *msg, void *user_data)
2915 return move_service(conn, msg, user_data, TRUE);
2918 static DBusMessage *move_after(DBusConnection *conn,
2919 DBusMessage *msg, void *user_data)
2921 return move_service(conn, msg, user_data, FALSE);
2924 static DBusMessage *reset_counters(DBusConnection *conn,
2925 DBusMessage *msg, void *user_data)
2927 struct connman_service *service = user_data;
2929 reset_stats(service);
2931 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2934 static GDBusMethodTable service_methods[] = {
2935 { "GetProperties", "", "a{sv}", get_properties },
2936 { "SetProperty", "sv", "", set_property },
2937 { "ClearProperty", "s", "", clear_property },
2938 { "Connect", "", "", connect_service,
2939 G_DBUS_METHOD_FLAG_ASYNC },
2940 { "Disconnect", "", "", disconnect_service },
2941 { "Remove", "", "", remove_service },
2942 { "MoveBefore", "o", "", move_before },
2943 { "MoveAfter", "o", "", move_after },
2944 { "ResetCounters", "", "", reset_counters },
2948 static GDBusSignalTable service_signals[] = {
2949 { "PropertyChanged", "sv" },
2953 static void service_free(gpointer user_data)
2955 struct connman_service *service = user_data;
2956 char *path = service->path;
2958 DBG("service %p", service);
2960 reply_pending(service, ENOENT);
2962 g_hash_table_remove(service_hash, service->identifier);
2964 __connman_notifier_service_remove(service);
2966 stats_stop(service);
2967 __connman_storage_save_service(service);
2969 service->path = NULL;
2972 __connman_profile_changed(FALSE);
2974 g_dbus_unregister_interface(connection, path,
2975 CONNMAN_SERVICE_INTERFACE);
2979 g_hash_table_destroy(service->counter_table);
2981 if (service->network != NULL)
2982 connman_network_unref(service->network);
2984 if (service->provider != NULL)
2985 connman_provider_unref(service->provider);
2987 if (service->ipconfig_ipv4 != NULL) {
2988 connman_ipconfig_unref(service->ipconfig_ipv4);
2989 service->ipconfig_ipv4 = NULL;
2992 if (service->ipconfig_ipv6 != NULL) {
2993 connman_ipconfig_unref(service->ipconfig_ipv6);
2994 service->ipconfig_ipv6 = NULL;
2997 if (service->location != NULL)
2998 connman_location_unref(service->location);
3000 g_strfreev(service->nameservers);
3001 g_strfreev(service->nameservers_config);
3002 g_strfreev(service->domains);
3003 g_strfreev(service->proxies);
3004 g_strfreev(service->excludes);
3006 g_free(service->domainname);
3007 g_free(service->pac);
3008 g_free(service->profile);
3009 g_free(service->name);
3010 g_free(service->passphrase);
3011 g_free(service->identifier);
3012 g_free(service->eap);
3013 g_free(service->identity);
3014 g_free(service->ca_cert_file);
3015 g_free(service->client_cert_file);
3016 g_free(service->private_key_file);
3017 g_free(service->private_key_passphrase);
3018 g_free(service->phase2);
3020 if (service->stats.timer != NULL)
3021 g_timer_destroy(service->stats.timer);
3022 if (service->stats_roaming.timer != NULL)
3023 g_timer_destroy(service->stats_roaming.timer);
3029 * __connman_service_put:
3030 * @service: service structure
3032 * Release service if no longer needed
3034 void __connman_service_put(struct connman_service *service)
3036 DBG("service %p", service);
3038 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
3039 GSequenceIter *iter;
3041 iter = g_hash_table_lookup(service_hash, service->identifier);
3043 reply_pending(service, ECONNABORTED);
3045 __connman_service_disconnect(service);
3047 g_sequence_remove(iter);
3049 service_free(service);
3053 static void stats_init(struct connman_service *service)
3056 service->stats.valid = FALSE;
3057 service->stats.enabled = FALSE;
3058 service->stats.timer = g_timer_new();
3061 service->stats_roaming.valid = FALSE;
3062 service->stats_roaming.enabled = FALSE;
3063 service->stats_roaming.timer = g_timer_new();
3066 static void service_initialize(struct connman_service *service)
3068 DBG("service %p", service);
3070 service->refcount = 1;
3071 service->session_usage_count = 0;
3073 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3074 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
3075 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3077 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3078 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3080 service->favorite = FALSE;
3081 service->immutable = FALSE;
3082 service->hidden = FALSE;
3084 service->ignore = FALSE;
3086 service->userconnect = FALSE;
3090 stats_init(service);
3092 service->provider = NULL;
3094 service->wps = FALSE;
3098 * connman_service_create:
3100 * Allocate a new service.
3102 * Returns: a newly-allocated #connman_service structure
3104 struct connman_service *connman_service_create(void)
3107 struct connman_stats_counter *counters;
3108 const char *counter;
3110 struct connman_service *service;
3112 service = g_try_new0(struct connman_service, 1);
3113 if (service == NULL)
3116 DBG("service %p", service);
3118 service->counter_table = g_hash_table_new_full(g_str_hash,
3119 g_str_equal, NULL, g_free);
3121 for (list = counter_list; list; list = list->next) {
3122 counter = list->data;
3124 counters = g_try_new0(struct connman_stats_counter, 1);
3125 if (counters == NULL) {
3126 g_hash_table_destroy(service->counter_table);
3131 counters->append_all = TRUE;
3133 g_hash_table_replace(service->counter_table, (gpointer)counter,
3137 service_initialize(service);
3139 service->location = __connman_location_create(service);
3144 struct connman_location *__connman_service_get_location(struct connman_service *service)
3146 return service->location;
3150 * connman_service_ref:
3151 * @service: service structure
3153 * Increase reference counter of service
3155 struct connman_service *connman_service_ref(struct connman_service *service)
3159 g_atomic_int_inc(&service->refcount);
3165 * connman_service_unref:
3166 * @service: service structure
3168 * Decrease reference counter of service
3170 void connman_service_unref(struct connman_service *service)
3172 __connman_service_put(service);
3175 static gint service_compare(gconstpointer a, gconstpointer b,
3178 struct connman_service *service_a = (void *) a;
3179 struct connman_service *service_b = (void *) b;
3180 enum connman_service_state state_a, state_b;
3182 state_a = combine_state(service_a->state_ipv4, service_a->state_ipv6);
3183 state_b = combine_state(service_b->state_ipv4, service_b->state_ipv6);
3185 if (state_a != state_b) {
3186 if (is_connected(service_a) == TRUE)
3188 if (is_connected(service_b) == TRUE)
3191 if (is_connecting(service_a) == TRUE)
3193 if (is_connecting(service_b) == TRUE)
3197 if (service_a->order > service_b->order)
3200 if (service_a->order < service_b->order)
3203 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3206 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3209 if (service_a->type != service_b->type) {
3210 switch (service_a->type) {
3211 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3212 case CONNMAN_SERVICE_TYPE_SYSTEM:
3213 case CONNMAN_SERVICE_TYPE_ETHERNET:
3214 case CONNMAN_SERVICE_TYPE_GPS:
3215 case CONNMAN_SERVICE_TYPE_VPN:
3216 case CONNMAN_SERVICE_TYPE_GADGET:
3218 case CONNMAN_SERVICE_TYPE_WIFI:
3220 case CONNMAN_SERVICE_TYPE_WIMAX:
3221 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3222 case CONNMAN_SERVICE_TYPE_CELLULAR:
3227 return (gint) service_b->strength - (gint) service_a->strength;
3231 * connman_service_get_type:
3232 * @service: service structure
3234 * Get the type of service
3236 enum connman_service_type connman_service_get_type(struct connman_service *service)
3238 if (service == NULL)
3239 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3241 return service->type;
3245 * connman_service_get_interface:
3246 * @service: service structure
3248 * Get network interface of service
3250 char *connman_service_get_interface(struct connman_service *service)
3254 if (service == NULL)
3257 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3258 if (service->ipconfig_ipv4)
3259 index = connman_ipconfig_get_index(
3260 service->ipconfig_ipv4);
3261 else if (service->ipconfig_ipv6)
3262 index = connman_ipconfig_get_index(
3263 service->ipconfig_ipv6);
3267 return connman_inet_ifname(index);
3270 if (service->network == NULL)
3273 index = connman_network_get_index(service->network);
3275 return connman_inet_ifname(index);
3279 * connman_service_get_network:
3280 * @service: service structure
3282 * Get the service network
3284 struct connman_network *
3285 __connman_service_get_network(struct connman_service *service)
3287 if (service == NULL)
3290 return service->network;
3293 struct connman_ipconfig *
3294 __connman_service_get_ip4config(struct connman_service *service)
3296 if (service == NULL)
3299 return service->ipconfig_ipv4;
3302 struct connman_ipconfig *
3303 __connman_service_get_ip6config(struct connman_service *service)
3305 if (service == NULL)
3308 return service->ipconfig_ipv6;
3311 struct connman_ipconfig *
3312 __connman_service_get_ipconfig(struct connman_service *service, int family)
3314 if (family == AF_INET)
3315 return __connman_service_get_ip4config(service);
3316 else if (family == AF_INET6)
3317 return __connman_service_get_ip6config(service);
3323 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3325 if (service == NULL)
3326 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3328 return service->security;
3331 const char *__connman_service_get_phase2(struct connman_service *service)
3333 if (service == NULL)
3336 return service->phase2;
3339 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3341 if (service == NULL)
3344 return service->wps;
3348 * __connman_service_set_favorite:
3349 * @service: service structure
3350 * @favorite: favorite value
3352 * Change the favorite setting of service
3354 int __connman_service_set_favorite(struct connman_service *service,
3355 connman_bool_t favorite)
3357 GSequenceIter *iter;
3359 iter = g_hash_table_lookup(service_hash, service->identifier);
3363 if (service->favorite == favorite)
3366 service->favorite = favorite;
3368 favorite_changed(service);
3370 g_sequence_sort_changed(iter, service_compare, NULL);
3372 __connman_profile_changed(FALSE);
3377 connman_bool_t __connman_service_get_immutable(struct connman_service *service)
3379 if (service == NULL)
3382 return service->immutable;
3385 int __connman_service_set_immutable(struct connman_service *service,
3386 connman_bool_t immutable)
3388 service->immutable = immutable;
3390 immutable_changed(service);
3395 void __connman_service_set_string(struct connman_service *service,
3396 const char *key, const char *value)
3398 if (g_str_equal(key, "EAP") == TRUE) {
3399 g_free(service->eap);
3400 service->eap = g_strdup(value);
3401 } else if (g_str_equal(key, "Identity") == TRUE) {
3402 g_free(service->identity);
3403 service->identity = g_strdup(value);
3404 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3405 g_free(service->ca_cert_file);
3406 service->ca_cert_file = g_strdup(value);
3407 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3408 g_free(service->client_cert_file);
3409 service->client_cert_file = g_strdup(value);
3410 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3411 g_free(service->private_key_file);
3412 service->private_key_file = g_strdup(value);
3413 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3414 g_free(service->private_key_passphrase);
3415 service->private_key_passphrase = g_strdup(value);
3416 } else if (g_str_equal(key, "Phase2") == TRUE) {
3417 g_free(service->phase2);
3418 service->phase2 = g_strdup(value);
3419 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3420 g_free(service->passphrase);
3421 service->passphrase = g_strdup(value);
3425 static void service_complete(struct connman_service *service)
3427 reply_pending(service, EIO);
3429 if (service->userconnect == FALSE)
3430 __connman_service_auto_connect();
3432 g_get_current_time(&service->modified);
3433 __connman_storage_save_service(service);
3436 static void report_error_cb(struct connman_service *service,
3437 gboolean retry, void *user_data)
3440 __connman_service_connect(service);
3442 service_complete(service);
3443 __connman_profile_changed(FALSE);
3444 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3448 int __connman_service_indicate_state(struct connman_service *service,
3449 enum connman_service_state new_state,
3450 enum connman_ipconfig_type type)
3452 enum connman_service_state service_state,
3453 state = CONNMAN_SERVICE_STATE_IDLE;
3454 GSequenceIter *iter;
3456 if (service == NULL)
3459 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3460 if (service->state_ipv4 == new_state)
3462 state = combine_state(new_state, service->state_ipv6);
3463 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3464 if (service->state_ipv6 == new_state)
3466 state = combine_state(new_state, service->state_ipv4);
3470 service_state = combine_state(service->state_ipv4,
3471 service->state_ipv6);
3473 DBG("service %p state %s/%s => %s new %s/%d => %s",
3475 state2string(service->state_ipv4),
3476 state2string(service->state_ipv6),
3477 state2string(service_state),
3478 state2string(new_state),
3480 state2string(state));
3482 if (service_state == CONNMAN_SERVICE_STATE_FAILURE &&
3483 state == CONNMAN_SERVICE_STATE_IDLE)
3486 if (service_state == CONNMAN_SERVICE_STATE_IDLE &&
3487 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3490 if (state == CONNMAN_SERVICE_STATE_IDLE &&
3491 service_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3492 state_changed(service);
3494 reply_pending(service, ECONNABORTED);
3496 __connman_service_disconnect(service);
3499 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3500 if (__connman_stats_service_register(service) == 0) {
3501 __connman_stats_get(service, FALSE,
3502 &service->stats.data);
3503 __connman_stats_get(service, TRUE,
3504 &service->stats_roaming.data);
3507 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
3508 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3509 __connman_ipconfig_enable(service->ipconfig_ipv4);
3510 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
3511 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3512 __connman_ipconfig_enable(service->ipconfig_ipv6);
3515 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3516 service->state_ipv4 = new_state;
3517 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3518 service->state_ipv6 = new_state;
3520 if (state == service_state)
3523 state_changed(service);
3525 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3526 if (service->login_required == TRUE) {
3527 service->login_required = FALSE;
3528 login_changed(service);
3531 connman_timeserver_sync();
3534 if (state == CONNMAN_SERVICE_STATE_IDLE) {
3535 connman_bool_t reconnect;
3537 reconnect = get_reconnect_state(service);
3538 if (reconnect == TRUE)
3539 __connman_service_auto_connect();
3542 if (state == CONNMAN_SERVICE_STATE_READY) {
3543 enum connman_service_proxy_method proxy_config;
3544 enum connman_ipconfig_method method;
3546 set_reconnect_state(service, TRUE);
3548 __connman_service_set_favorite(service, TRUE);
3550 reply_pending(service, 0);
3552 service->userconnect = FALSE;
3554 g_get_current_time(&service->modified);
3555 __connman_storage_save_service(service);
3557 update_nameservers(service);
3558 dns_changed(service);
3559 domain_changed(service);
3561 proxy_config = service->proxy_config;
3564 * We start WPAD if we haven't got a PAC URL from DHCP and
3565 * if our proxy manual configuration is either empty or set
3566 * to AUTO with an empty URL.
3568 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3569 (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3571 CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3572 service->pac == NULL)))
3573 if (__connman_wpad_start(service) < 0) {
3575 CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3577 __connman_notifier_proxy_changed(service);
3580 __connman_notifier_connect(service->type);
3582 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3583 connman_network_get_bool(service->network,
3584 "WiFi.UseWPS") == TRUE) {
3587 pass = connman_network_get_string(service->network,
3590 __connman_service_set_passphrase(service, pass);
3592 connman_network_set_bool(service->network,
3593 "WiFi.UseWPS", FALSE);
3598 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3599 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3600 __connman_ipconfig_disable_ipv6(
3601 service->ipconfig_ipv6);
3603 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3604 __connman_location_finish(service);
3608 __connman_wpad_stop(service);
3610 update_nameservers(service);
3611 dns_changed(service);
3612 domain_changed(service);
3614 __connman_notifier_disconnect(service->type);
3617 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3618 if (service->userconnect == TRUE &&
3619 __connman_agent_report_error(service,
3620 error2string(service->error),
3621 report_error_cb, NULL) == -EIO)
3623 service_complete(service);
3625 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3627 iter = g_hash_table_lookup(service_hash, service->identifier);
3629 g_sequence_sort_changed(iter, service_compare, NULL);
3631 __connman_profile_changed(FALSE);
3633 service_state = combine_state(service->state_ipv4,
3634 service->state_ipv6);
3635 if (service_state == CONNMAN_SERVICE_STATE_ONLINE)
3638 if (service_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3639 struct connman_service *def_service = get_default();
3641 if (__connman_notifier_count_connected() == 0 &&
3642 def_service != NULL &&
3643 def_service->provider != NULL)
3644 __connman_provider_disconnect(def_service->provider);
3647 if (service_state == CONNMAN_SERVICE_STATE_IDLE ||
3648 service_state == CONNMAN_SERVICE_STATE_FAILURE)
3649 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3654 int __connman_service_indicate_error(struct connman_service *service,
3655 enum connman_service_error error)
3657 DBG("service %p error %d", service, error);
3659 if (service == NULL)
3662 service->error = error;
3664 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3665 __connman_service_set_passphrase(service, NULL);
3667 return __connman_service_indicate_state(service,
3668 CONNMAN_SERVICE_STATE_FAILURE,
3669 CONNMAN_IPCONFIG_TYPE_IPV4);
3672 int __connman_service_clear_error(struct connman_service *service)
3674 enum connman_service_state state;
3676 DBG("service %p", service);
3678 if (service == NULL)
3681 state = combine_state(service->state_ipv4, service->state_ipv6);
3683 if (state != CONNMAN_SERVICE_STATE_FAILURE)
3686 service->state_ipv4 = service->state_ipv6 =
3687 CONNMAN_SERVICE_STATE_UNKNOWN;
3688 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3690 if (service->favorite == TRUE)
3691 set_reconnect_state(service, TRUE);
3693 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
3694 CONNMAN_IPCONFIG_TYPE_IPV6);
3697 * Toggling the IPv6 state to IDLE could trigger the auto connect
3698 * machinery and consequently the IPv4 state.
3700 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3701 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3704 return __connman_service_indicate_state(service,
3705 CONNMAN_SERVICE_STATE_IDLE,
3706 CONNMAN_IPCONFIG_TYPE_IPV4);
3709 int __connman_service_indicate_default(struct connman_service *service)
3711 DBG("service %p", service);
3715 __connman_location_detect(service);
3720 int __connman_service_request_login(struct connman_service *service)
3722 DBG("service %p", service);
3724 if (service == NULL)
3727 service->login_required = TRUE;
3728 login_changed(service);
3733 static connman_bool_t prepare_network(struct connman_service *service)
3735 enum connman_network_type type;
3736 unsigned int ssid_len;
3738 type = connman_network_get_type(service->network);
3741 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3742 case CONNMAN_NETWORK_TYPE_VENDOR:
3744 case CONNMAN_NETWORK_TYPE_WIFI:
3745 if (connman_network_get_blob(service->network, "WiFi.SSID",
3749 if (service->passphrase != NULL)
3750 connman_network_set_string(service->network,
3751 "WiFi.Passphrase", service->passphrase);
3753 case CONNMAN_NETWORK_TYPE_ETHERNET:
3754 case CONNMAN_NETWORK_TYPE_WIMAX:
3755 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3756 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3757 case CONNMAN_NETWORK_TYPE_CELLULAR:
3764 static void prepare_8021x(struct connman_service *service)
3766 if (service->eap != NULL)
3767 connman_network_set_string(service->network, "WiFi.EAP",
3770 if (service->identity != NULL)
3771 connman_network_set_string(service->network, "WiFi.Identity",
3774 if (service->ca_cert_file != NULL)
3775 connman_network_set_string(service->network, "WiFi.CACertFile",
3776 service->ca_cert_file);
3778 if (service->client_cert_file != NULL)
3779 connman_network_set_string(service->network,
3780 "WiFi.ClientCertFile",
3781 service->client_cert_file);
3783 if (service->private_key_file != NULL)
3784 connman_network_set_string(service->network,
3785 "WiFi.PrivateKeyFile",
3786 service->private_key_file);
3788 if (service->private_key_passphrase != NULL)
3789 connman_network_set_string(service->network,
3790 "WiFi.PrivateKeyPassphrase",
3791 service->private_key_passphrase);
3793 if (service->phase2 != NULL)
3794 connman_network_set_string(service->network, "WiFi.Phase2",
3798 static int service_connect(struct connman_service *service)
3802 switch (service->type) {
3803 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3804 case CONNMAN_SERVICE_TYPE_SYSTEM:
3805 case CONNMAN_SERVICE_TYPE_GPS:
3806 case CONNMAN_SERVICE_TYPE_GADGET:
3808 case CONNMAN_SERVICE_TYPE_ETHERNET:
3809 case CONNMAN_SERVICE_TYPE_WIMAX:
3810 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3811 case CONNMAN_SERVICE_TYPE_CELLULAR:
3812 case CONNMAN_SERVICE_TYPE_VPN:
3814 case CONNMAN_SERVICE_TYPE_WIFI:
3815 switch (service->security) {
3816 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3817 case CONNMAN_SERVICE_SECURITY_NONE:
3819 case CONNMAN_SERVICE_SECURITY_WEP:
3820 case CONNMAN_SERVICE_SECURITY_PSK:
3821 case CONNMAN_SERVICE_SECURITY_WPA:
3822 case CONNMAN_SERVICE_SECURITY_RSN:
3823 if (service->passphrase == NULL) {
3824 if (service->network == NULL)
3827 if (service->wps == FALSE ||
3828 connman_network_get_bool(
3830 "WiFi.UseWPS") == FALSE)
3834 case CONNMAN_SERVICE_SECURITY_8021X:
3835 if (service->eap == NULL)
3839 * never request credentials if using EAP-TLS
3840 * (EAP-TLS networks need to be fully provisioned)
3842 if (g_str_equal(service->eap, "tls") == TRUE)
3845 if (service->immutable != TRUE)
3853 if (service->network != NULL) {
3854 if (prepare_network(service) == FALSE)
3857 switch (service->security) {
3858 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3859 case CONNMAN_SERVICE_SECURITY_NONE:
3860 case CONNMAN_SERVICE_SECURITY_WEP:
3861 case CONNMAN_SERVICE_SECURITY_PSK:
3862 case CONNMAN_SERVICE_SECURITY_WPA:
3863 case CONNMAN_SERVICE_SECURITY_RSN:
3865 case CONNMAN_SERVICE_SECURITY_8021X:
3866 prepare_8021x(service);
3870 if (__connman_stats_service_register(service) == 0) {
3871 __connman_stats_get(service, FALSE,
3872 &service->stats.data);
3873 __connman_stats_get(service, TRUE,
3874 &service->stats_roaming.data);
3877 if (service->ipconfig_ipv4)
3878 __connman_ipconfig_enable(service->ipconfig_ipv4);
3879 if (service->ipconfig_ipv6)
3880 __connman_ipconfig_enable(service->ipconfig_ipv6);
3882 err = __connman_network_connect(service->network);
3883 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3884 service->provider != NULL)
3885 err = __connman_provider_connect(service->provider);
3890 if (err != -EINPROGRESS) {
3891 __connman_ipconfig_disable(service->ipconfig_ipv4);
3892 __connman_ipconfig_disable(service->ipconfig_ipv6);
3893 __connman_stats_service_unregister(service);
3901 int __connman_service_connect(struct connman_service *service)
3903 enum connman_service_state state;
3906 state = combine_state(service->state_ipv4, service->state_ipv6);
3908 DBG("service %p state %s", service, state2string(state));
3910 if (is_connected(service) == TRUE)
3913 if (is_connecting(service) == TRUE)
3916 switch (service->type) {
3917 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3918 case CONNMAN_SERVICE_TYPE_SYSTEM:
3919 case CONNMAN_SERVICE_TYPE_GPS:
3920 case CONNMAN_SERVICE_TYPE_GADGET:
3923 err = service_connect(service);
3929 if (err == -EINPROGRESS) {
3930 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
3931 connect_timeout, service);
3933 return -EINPROGRESS;
3939 if (service->userconnect == TRUE)
3940 reply_pending(service, err);
3942 __connman_service_indicate_state(service,
3943 CONNMAN_SERVICE_STATE_FAILURE,
3944 CONNMAN_IPCONFIG_TYPE_IPV4);
3945 __connman_service_indicate_state(service,
3946 CONNMAN_SERVICE_STATE_FAILURE,
3947 CONNMAN_IPCONFIG_TYPE_IPV6);
3952 int __connman_service_disconnect(struct connman_service *service)
3956 DBG("service %p", service);
3958 if (service->network != NULL) {
3959 err = __connman_network_disconnect(service->network);
3960 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3961 service->provider != NULL)
3962 err = __connman_provider_disconnect(service->provider);
3966 if (err < 0 && err != -EINPROGRESS)
3969 __connman_6to4_remove(service->ipconfig_ipv4);
3971 if (service->ipconfig_ipv4)
3972 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3975 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3978 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3979 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3981 __connman_ipconfig_disable(service->ipconfig_ipv4);
3982 __connman_ipconfig_disable(service->ipconfig_ipv6);
3984 __connman_stats_service_unregister(service);
3989 int __connman_service_disconnect_all(void)
3991 GSequenceIter *iter;
3995 iter = g_sequence_get_begin_iter(service_list);
3997 while (g_sequence_iter_is_end(iter) == FALSE) {
3998 struct connman_service *service = g_sequence_get(iter);
4000 service->ignore = TRUE;
4002 set_reconnect_state(service, FALSE);
4004 __connman_service_disconnect(service);
4006 iter = g_sequence_iter_next(iter);
4014 * __connman_service_lookup:
4015 * @pattern: search pattern
4016 * @path: return object path
4018 * Look up a service path from a search pattern
4020 int __connman_service_lookup(const char *pattern, const char **path)
4022 GHashTableIter iter;
4023 gpointer key, value;
4025 g_hash_table_iter_init(&iter, service_hash);
4027 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4028 GSequenceIter *iter = value;
4029 struct connman_service *service = g_sequence_get(iter);
4031 if (g_strcmp0(service->identifier, pattern) == 0 ||
4032 g_strcmp0(service->name, pattern) == 0) {
4033 *path = (const char *) service->path;
4042 * lookup_by_identifier:
4043 * @identifier: service identifier
4045 * Look up a service by identifier (reference count will not be increased)
4047 static struct connman_service *lookup_by_identifier(const char *identifier)
4049 GSequenceIter *iter;
4051 iter = g_hash_table_lookup(service_hash, identifier);
4053 return g_sequence_get(iter);
4058 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4059 const char *ssid, const char *mode, const char *security)
4061 struct connman_network *network;
4064 unsigned int i, ssid_len;
4066 ssid_len = strlen(ssid);
4070 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
4071 if (network == NULL)
4074 connman_network_set_blob(network, "WiFi.SSID",
4075 (unsigned char *) ssid, ssid_len);
4077 connman_network_set_string(network, "WiFi.Mode", mode);
4078 connman_network_set_string(network, "WiFi.Security", security);
4080 name = g_try_malloc0(ssid_len + 1);
4082 connman_network_unref(network);
4086 for (i = 0; i < ssid_len; i++) {
4087 if (g_ascii_isprint(ssid[i]))
4093 connman_network_set_name(network, name);
4097 index = connman_device_get_index(device);
4098 connman_network_set_index(network, index);
4100 if (connman_device_add_network(device, network) < 0) {
4101 connman_network_unref(network);
4105 connman_network_set_available(network, TRUE);
4110 int __connman_service_create_and_connect(DBusMessage *msg)
4112 struct connman_service *service;
4113 struct connman_network *network;
4114 struct connman_device *device;
4115 DBusMessageIter iter, array;
4116 const char *mode = "managed", *security = "none", *group_security;
4117 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4118 unsigned int ssid_len = 0;
4121 gboolean created = FALSE;
4124 dbus_message_iter_init(msg, &iter);
4125 dbus_message_iter_recurse(&iter, &array);
4127 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4128 DBusMessageIter entry, value;
4131 dbus_message_iter_recurse(&array, &entry);
4132 dbus_message_iter_get_basic(&entry, &key);
4134 dbus_message_iter_next(&entry);
4135 dbus_message_iter_recurse(&entry, &value);
4137 switch (dbus_message_iter_get_arg_type(&value)) {
4138 case DBUS_TYPE_STRING:
4139 if (g_str_equal(key, "Type") == TRUE)
4140 dbus_message_iter_get_basic(&value, &type);
4141 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4142 g_str_equal(key, "Mode") == TRUE)
4143 dbus_message_iter_get_basic(&value, &mode);
4144 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4145 g_str_equal(key, "Security") == TRUE)
4146 dbus_message_iter_get_basic(&value, &security);
4147 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4148 g_str_equal(key, "Passphrase") == TRUE)
4149 dbus_message_iter_get_basic(&value, &passphrase);
4150 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4151 g_str_equal(key, "SSID") == TRUE)
4152 dbus_message_iter_get_basic(&value, &ssid);
4155 dbus_message_iter_next(&array);
4161 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4167 ssid_len = strlen(ssid);
4171 if (g_strcmp0(security, "none") != 0 &&
4172 g_strcmp0(security, "wep") != 0 &&
4173 g_strcmp0(security, "psk") != 0 &&
4174 g_strcmp0(security, "wpa") != 0 &&
4175 g_strcmp0(security, "rsn") != 0 &&
4176 g_strcmp0(security, "ieee8021x") != 0)
4179 device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4183 ident = connman_device_get_ident(device);
4188 if (!g_strcmp0(security, "wpa") ||
4189 !g_strcmp0(security, "rsn"))
4190 group_security = "psk";
4192 group_security = security;
4194 group = connman_wifi_build_group_name((unsigned char *) ssid,
4195 ssid_len, mode, group_security);
4199 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4201 service = lookup_by_identifier(name);
4203 if (service != NULL)
4206 network = create_hidden_wifi(device, ssid, mode, security);
4207 if (network != NULL) {
4208 connman_network_set_group(network, group);
4212 service = lookup_by_identifier(name);
4218 if (service == NULL) {
4223 if (is_connected(service) == TRUE) {
4228 if (is_connecting(service) == TRUE) {
4233 set_reconnect_state(service, FALSE);
4235 __connman_device_disconnect(device);
4237 if (passphrase != NULL) {
4238 g_free(service->passphrase);
4239 service->passphrase = g_strdup(passphrase);
4242 service->userconnect = TRUE;
4244 err = __connman_service_connect(service);
4245 if (err < 0 && err != -EINPROGRESS)
4248 service->pending = dbus_message_ref(msg);
4253 if (service != NULL && created == TRUE) {
4254 struct connman_network *network = service->network;
4256 if (network != NULL) {
4257 connman_network_set_available(network, FALSE);
4258 __connman_device_cleanup_networks(device);
4260 __connman_service_put(service);
4266 int __connman_service_provision(DBusMessage *msg)
4268 GKeyFile *keyfile = NULL;
4269 const char *config_str = NULL;
4270 char *group = NULL, *ident = NULL;
4272 struct connman_service *service;
4276 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4279 if (config_str == NULL || strlen(config_str) == 0)
4282 keyfile = g_key_file_new();
4284 /* populate GKeyFile with config_str */
4285 if (g_key_file_load_from_data(keyfile, config_str,
4286 strlen(config_str), 0, NULL) == FALSE) {
4292 * read only one group of settings (only one service supported, no
4295 group = g_key_file_get_start_group(keyfile);
4297 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4302 err = __connman_config_load_service(keyfile, group, TRUE);
4306 ident = group + strlen("service_");
4308 /* trigger service provisioning if service exists */
4309 service = lookup_by_identifier(ident);
4310 if (service != NULL)
4311 __connman_config_provision_service(service);
4313 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4319 if (keyfile != NULL)
4320 g_key_file_free(keyfile);
4326 * __connman_service_get:
4327 * @identifier: service identifier
4329 * Look up a service by identifier or create a new one if not found
4331 static struct connman_service *service_get(const char *identifier)
4333 struct connman_service *service;
4334 GSequenceIter *iter;
4336 iter = g_hash_table_lookup(service_hash, identifier);
4338 service = g_sequence_get(iter);
4339 if (service != NULL)
4340 connman_service_ref(service);
4344 service = connman_service_create();
4345 if (service == NULL)
4348 DBG("service %p", service);
4350 service->identifier = g_strdup(identifier);
4352 service->profile = g_strdup(__connman_profile_active_ident());
4354 iter = g_sequence_insert_sorted(service_list, service,
4355 service_compare, NULL);
4357 g_hash_table_insert(service_hash, service->identifier, iter);
4362 static int service_register(struct connman_service *service)
4364 const char *path = __connman_profile_active_path();
4365 GSequenceIter *iter;
4367 DBG("service %p", service);
4369 if (service->path != NULL)
4372 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4374 DBG("path %s", service->path);
4376 __connman_config_provision_service(service);
4378 __connman_storage_load_service(service);
4380 g_dbus_register_interface(connection, service->path,
4381 CONNMAN_SERVICE_INTERFACE,
4382 service_methods, service_signals,
4383 NULL, service, NULL);
4385 iter = g_hash_table_lookup(service_hash, service->identifier);
4387 g_sequence_sort_changed(iter, service_compare, NULL);
4389 __connman_profile_changed(TRUE);
4394 static void service_up(struct connman_ipconfig *ipconfig)
4396 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4398 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4400 link_changed(service);
4402 service->stats.valid = FALSE;
4403 service->stats_roaming.valid = FALSE;
4406 static void service_down(struct connman_ipconfig *ipconfig)
4408 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4411 static void service_lower_up(struct connman_ipconfig *ipconfig)
4413 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4415 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4417 stats_start(service);
4420 static void service_lower_down(struct connman_ipconfig *ipconfig)
4422 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4424 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4426 stats_stop(service);
4427 __connman_storage_save_service(service);
4430 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4432 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4433 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4434 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4436 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4438 type = __connman_ipconfig_get_config_type(ipconfig);
4439 method = __connman_ipconfig_get_method(ipconfig);
4441 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4444 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4445 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4446 __connman_service_indicate_state(service,
4447 CONNMAN_SERVICE_STATE_READY,
4448 CONNMAN_IPCONFIG_TYPE_IPV6);
4450 settings_changed(service, ipconfig);
4453 static void service_ip_release(struct connman_ipconfig *ipconfig)
4455 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4456 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4457 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4459 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4461 type = __connman_ipconfig_get_config_type(ipconfig);
4462 method = __connman_ipconfig_get_method(ipconfig);
4464 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4467 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4468 method == CONNMAN_IPCONFIG_METHOD_OFF)
4469 __connman_service_indicate_state(service,
4470 CONNMAN_SERVICE_STATE_DISCONNECT,
4471 CONNMAN_IPCONFIG_TYPE_IPV6);
4473 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4474 method == CONNMAN_IPCONFIG_METHOD_OFF)
4475 __connman_service_indicate_state(service,
4476 CONNMAN_SERVICE_STATE_DISCONNECT,
4477 CONNMAN_IPCONFIG_TYPE_IPV4);
4479 settings_changed(service, ipconfig);
4482 static const struct connman_ipconfig_ops service_ops = {
4484 .down = service_down,
4485 .lower_up = service_lower_up,
4486 .lower_down = service_lower_down,
4487 .ip_bound = service_ip_bound,
4488 .ip_release = service_ip_release,
4491 static void setup_ip4config(struct connman_service *service, int index)
4496 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4497 CONNMAN_IPCONFIG_TYPE_IPV4);
4498 if (service->ipconfig_ipv4 == NULL)
4501 connman_ipconfig_set_method(service->ipconfig_ipv4,
4502 CONNMAN_IPCONFIG_METHOD_DHCP);
4504 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4506 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4509 static void setup_ip6config(struct connman_service *service, int index)
4514 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4515 CONNMAN_IPCONFIG_TYPE_IPV6);
4516 if (service->ipconfig_ipv6 == NULL)
4519 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4521 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4524 void __connman_service_create_ip4config(struct connman_service *service,
4527 const char *ident = service->profile;
4530 DBG("ipv4 %p", service->ipconfig_ipv4);
4532 if (service->ipconfig_ipv4 != NULL)
4535 setup_ip4config(service, index);
4540 keyfile = __connman_storage_open_profile(ident);
4541 if (keyfile == NULL)
4544 if (service->ipconfig_ipv4)
4545 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4546 service->identifier, "IPv4.");
4547 g_key_file_free(keyfile);
4550 void __connman_service_create_ip6config(struct connman_service *service,
4553 const char *ident = service->profile;
4556 DBG("ipv6 %p", service->ipconfig_ipv6);
4558 if (service->ipconfig_ipv6 != NULL)
4561 setup_ip6config(service, index);
4566 keyfile = __connman_storage_open_profile(ident);
4567 if (keyfile == NULL)
4570 if (service->ipconfig_ipv6 != NULL)
4571 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4572 service->identifier, "IPv6.");
4574 g_key_file_free(keyfile);
4578 * __connman_service_lookup_from_network:
4579 * @network: network structure
4581 * Look up a service by network (reference count will not be increased)
4583 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4585 struct connman_service *service;
4586 const char *ident, *group;
4589 DBG("network %p", network);
4591 ident = __connman_network_get_ident(network);
4595 group = connman_network_get_group(network);
4599 name = g_strdup_printf("%s_%s_%s",
4600 __connman_network_get_type(network), ident, group);
4601 service = lookup_by_identifier(name);
4607 struct connman_service *__connman_service_lookup_from_index(int index)
4609 struct connman_service *service;
4610 GSequenceIter *iter;
4612 iter = g_sequence_get_begin_iter(service_list);
4614 while (g_sequence_iter_is_end(iter) == FALSE) {
4615 service = g_sequence_get(iter);
4617 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4621 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4625 iter = g_sequence_iter_next(iter);
4631 const char *__connman_service_get_ident(struct connman_service *service)
4633 return service->identifier;
4636 const char *__connman_service_get_path(struct connman_service *service)
4638 return service->path;
4641 unsigned int __connman_service_get_order(struct connman_service *service)
4643 GSequenceIter *iter;
4645 if (service == NULL)
4648 if (service->favorite == FALSE) {
4653 iter = g_hash_table_lookup(service_hash, service->identifier);
4655 if (g_sequence_iter_get_position(iter) == 0)
4657 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4658 service->order = 10;
4664 return service->order;
4667 static enum connman_service_type convert_network_type(struct connman_network *network)
4669 enum connman_network_type type = connman_network_get_type(network);
4672 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4673 case CONNMAN_NETWORK_TYPE_VENDOR:
4675 case CONNMAN_NETWORK_TYPE_ETHERNET:
4676 return CONNMAN_SERVICE_TYPE_ETHERNET;
4677 case CONNMAN_NETWORK_TYPE_WIFI:
4678 return CONNMAN_SERVICE_TYPE_WIFI;
4679 case CONNMAN_NETWORK_TYPE_WIMAX:
4680 return CONNMAN_SERVICE_TYPE_WIMAX;
4681 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4682 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4683 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4684 case CONNMAN_NETWORK_TYPE_CELLULAR:
4685 return CONNMAN_SERVICE_TYPE_CELLULAR;
4688 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4691 static enum connman_service_mode convert_wifi_mode(const char *mode)
4694 return CONNMAN_SERVICE_MODE_UNKNOWN;
4695 else if (g_str_equal(mode, "managed") == TRUE)
4696 return CONNMAN_SERVICE_MODE_MANAGED;
4697 else if (g_str_equal(mode, "adhoc") == TRUE)
4698 return CONNMAN_SERVICE_MODE_ADHOC;
4700 return CONNMAN_SERVICE_MODE_UNKNOWN;
4703 static enum connman_service_security convert_wifi_security(const char *security)
4705 if (security == NULL)
4706 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4707 else if (g_str_equal(security, "none") == TRUE)
4708 return CONNMAN_SERVICE_SECURITY_NONE;
4709 else if (g_str_equal(security, "wep") == TRUE)
4710 return CONNMAN_SERVICE_SECURITY_WEP;
4711 else if (g_str_equal(security, "psk") == TRUE)
4712 return CONNMAN_SERVICE_SECURITY_PSK;
4713 else if (g_str_equal(security, "ieee8021x") == TRUE)
4714 return CONNMAN_SERVICE_SECURITY_8021X;
4715 else if (g_str_equal(security, "wpa") == TRUE)
4716 return CONNMAN_SERVICE_SECURITY_WPA;
4717 else if (g_str_equal(security, "rsn") == TRUE)
4718 return CONNMAN_SERVICE_SECURITY_RSN;
4720 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4723 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
4728 return CONNMAN_SERVICE_MODE_GPRS;
4730 return CONNMAN_SERVICE_MODE_EDGE;
4735 return CONNMAN_SERVICE_MODE_UMTS;
4738 return CONNMAN_SERVICE_MODE_UNKNOWN;
4741 static void update_from_network(struct connman_service *service,
4742 struct connman_network *network)
4744 connman_uint8_t strength = service->strength;
4745 GSequenceIter *iter;
4748 DBG("service %p network %p", service, network);
4750 if (is_connected(service) == TRUE)
4753 if (is_connecting(service) == TRUE)
4756 str = connman_network_get_string(network, "Name");
4758 g_free(service->name);
4759 service->name = g_strdup(str);
4760 service->hidden = FALSE;
4762 g_free(service->name);
4763 service->name = NULL;
4764 service->hidden = TRUE;
4767 service->strength = connman_network_get_uint8(network, "Strength");
4768 service->roaming = connman_network_get_bool(network, "Roaming");
4770 if (service->strength == 0) {
4772 * Filter out 0-values; it's unclear what they mean
4773 * and they cause anomalous sorting of the priority list.
4775 service->strength = strength;
4778 str = connman_network_get_string(network, "WiFi.Mode");
4779 service->mode = convert_wifi_mode(str);
4781 str = connman_network_get_string(network, "WiFi.Security");
4782 service->security = convert_wifi_security(str);
4784 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
4785 connman_uint8_t value = connman_network_get_uint8(network,
4788 service->mode = convert_cellular_mode(value);
4789 } else if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4790 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4792 if (service->strength > strength && service->network != NULL) {
4793 connman_network_unref(service->network);
4794 service->network = connman_network_ref(network);
4796 strength_changed(service);
4799 if (service->network == NULL)
4800 service->network = connman_network_ref(network);
4802 iter = g_hash_table_lookup(service_hash, service->identifier);
4804 g_sequence_sort_changed(iter, service_compare, NULL);
4808 * __connman_service_create_from_network:
4809 * @network: network structure
4811 * Look up service by network and if not found, create one
4813 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4815 struct connman_service *service;
4816 struct connman_device *device;
4817 const char *ident, *group;
4821 DBG("network %p", network);
4823 ident = __connman_network_get_ident(network);
4827 group = connman_network_get_group(network);
4831 name = g_strdup_printf("%s_%s_%s",
4832 __connman_network_get_type(network), ident, group);
4833 service = service_get(name);
4836 if (service == NULL)
4839 if (__connman_network_get_weakness(network) == TRUE)
4842 if (service->path != NULL) {
4843 update_from_network(service, network);
4844 __connman_profile_changed(TRUE);
4848 service->type = convert_network_type(network);
4850 switch (service->type) {
4851 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4852 case CONNMAN_SERVICE_TYPE_SYSTEM:
4853 case CONNMAN_SERVICE_TYPE_ETHERNET:
4854 case CONNMAN_SERVICE_TYPE_WIMAX:
4855 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4856 case CONNMAN_SERVICE_TYPE_GPS:
4857 case CONNMAN_SERVICE_TYPE_VPN:
4858 case CONNMAN_SERVICE_TYPE_GADGET:
4859 service->autoconnect = FALSE;
4861 case CONNMAN_SERVICE_TYPE_WIFI:
4862 case CONNMAN_SERVICE_TYPE_CELLULAR:
4863 service->autoconnect = TRUE;
4867 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4869 update_from_network(service, network);
4871 index = connman_network_get_index(network);
4873 if (service->ipconfig_ipv4 == NULL)
4874 setup_ip4config(service, index);
4876 if (service->ipconfig_ipv6 == NULL)
4877 setup_ip6config(service, index);
4879 service_register(service);
4881 if (service->favorite == TRUE) {
4882 device = connman_network_get_device(service->network);
4883 if (device && __connman_device_scanning(device) == FALSE)
4884 __connman_service_auto_connect();
4887 __connman_notifier_service_add(service);
4892 void __connman_service_update_from_network(struct connman_network *network)
4894 struct connman_service *service;
4895 enum connman_service_mode mode;
4896 connman_uint8_t strength, value;
4897 connman_bool_t roaming;
4898 GSequenceIter *iter;
4900 connman_bool_t stats_enable;
4902 DBG("network %p", network);
4904 service = __connman_service_lookup_from_network(network);
4905 if (service == NULL)
4908 if (service->network == NULL)
4911 name = connman_network_get_string(service->network, "Name");
4912 if (g_strcmp0(service->name, name) != 0) {
4913 g_free(service->name);
4914 service->name = g_strdup(name);
4915 connman_dbus_property_changed_basic(service->path,
4916 CONNMAN_SERVICE_INTERFACE, "Name",
4917 DBUS_TYPE_STRING, &service->name);
4920 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4921 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4923 strength = connman_network_get_uint8(service->network, "Strength");
4924 if (strength == service->strength)
4927 service->strength = strength;
4929 strength_changed(service);
4932 roaming = connman_network_get_bool(service->network, "Roaming");
4933 if (roaming == service->roaming)
4936 stats_enable = stats_enabled(service);
4937 if (stats_enable == TRUE)
4938 stats_stop(service);
4940 service->roaming = roaming;
4942 if (stats_enable == TRUE)
4943 stats_start(service);
4945 roaming_changed(service);
4947 iter = g_hash_table_lookup(service_hash, service->identifier);
4949 g_sequence_sort_changed(iter, service_compare, NULL);
4952 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
4955 value = connman_network_get_uint8(service->network, "Cellular.Mode");
4956 mode = convert_cellular_mode(value);
4958 if (mode == service->mode)
4961 service->mode = mode;
4963 mode_changed(service);
4966 void __connman_service_remove_from_network(struct connman_network *network)
4968 struct connman_service *service;
4970 DBG("network %p", network);
4972 service = __connman_service_lookup_from_network(network);
4973 if (service == NULL)
4976 __connman_service_put(service);
4980 * __connman_service_create_from_provider:
4981 * @provider: provider structure
4983 * Look up service by provider and if not found, create one
4985 struct connman_service *
4986 __connman_service_create_from_provider(struct connman_provider *provider)
4988 struct connman_service *service;
4989 const char *ident, *str;
4991 int index = connman_provider_get_index(provider);
4993 DBG("provider %p", provider);
4995 ident = __connman_provider_get_ident(provider);
4999 name = g_strdup_printf("vpn_%s", ident);
5000 service = service_get(name);
5003 if (service == NULL)
5006 service->type = CONNMAN_SERVICE_TYPE_VPN;
5007 service->provider = connman_provider_ref(provider);
5008 service->autoconnect = FALSE;
5010 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5012 str = connman_provider_get_string(provider, "Name");
5014 g_free(service->name);
5015 service->name = g_strdup(str);
5016 service->hidden = FALSE;
5018 g_free(service->name);
5019 service->name = NULL;
5020 service->hidden = TRUE;
5023 service->strength = 0;
5025 if (service->ipconfig_ipv4 == NULL)
5026 service->ipconfig_ipv4 = connman_ipconfig_create(index,
5027 CONNMAN_IPCONFIG_TYPE_IPV4);
5028 if (service->ipconfig_ipv4 == NULL)
5031 connman_ipconfig_set_method(service->ipconfig_ipv4,
5032 CONNMAN_IPCONFIG_METHOD_MANUAL);
5033 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5034 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5036 if (service->ipconfig_ipv6 == NULL)
5037 service->ipconfig_ipv6 = connman_ipconfig_create(index,
5038 CONNMAN_IPCONFIG_TYPE_IPV6);
5039 if (service->ipconfig_ipv6 == NULL)
5042 connman_ipconfig_set_method(service->ipconfig_ipv6,
5043 CONNMAN_IPCONFIG_METHOD_OFF);
5044 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5045 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5047 service_register(service);
5049 __connman_notifier_service_add(service);
5054 static int service_load(struct connman_service *service)
5056 const char *ident = service->profile;
5058 GError *error = NULL;
5059 gchar *pathname, *data = NULL;
5062 connman_bool_t autoconnect;
5063 unsigned int ssid_len;
5066 DBG("service %p", service);
5071 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5072 if (pathname == NULL)
5075 keyfile = g_key_file_new();
5077 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
5084 if (g_key_file_load_from_data(keyfile, data, length,
5085 0, NULL) == FALSE) {
5092 switch (service->type) {
5093 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5094 case CONNMAN_SERVICE_TYPE_SYSTEM:
5095 case CONNMAN_SERVICE_TYPE_ETHERNET:
5096 case CONNMAN_SERVICE_TYPE_GPS:
5097 case CONNMAN_SERVICE_TYPE_VPN:
5098 case CONNMAN_SERVICE_TYPE_GADGET:
5100 case CONNMAN_SERVICE_TYPE_WIFI:
5101 if (service->name == NULL) {
5104 name = g_key_file_get_string(keyfile,
5105 service->identifier, "Name", NULL);
5107 g_free(service->name);
5108 service->name = name;
5111 if (service->network != NULL)
5112 connman_network_set_name(service->network,
5116 if (service->network &&
5117 connman_network_get_blob(service->network,
5118 "WiFi.SSID", &ssid_len) == NULL) {
5121 hex_ssid = g_key_file_get_string(keyfile,
5122 service->identifier,
5125 if (hex_ssid != NULL) {
5127 unsigned int i, j = 0, hex;
5128 size_t hex_ssid_len = strlen(hex_ssid);
5130 ssid = g_try_malloc0(hex_ssid_len / 2);
5137 for (i = 0; i < hex_ssid_len; i += 2) {
5138 sscanf(hex_ssid + i, "%02x", &hex);
5142 connman_network_set_blob(service->network,
5143 "WiFi.SSID", ssid, hex_ssid_len / 2);
5150 case CONNMAN_SERVICE_TYPE_WIMAX:
5151 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5152 case CONNMAN_SERVICE_TYPE_CELLULAR:
5153 service->favorite = g_key_file_get_boolean(keyfile,
5154 service->identifier, "Favorite", NULL);
5156 autoconnect = g_key_file_get_boolean(keyfile,
5157 service->identifier, "AutoConnect", &error);
5159 service->autoconnect = autoconnect;
5160 g_clear_error(&error);
5162 str = g_key_file_get_string(keyfile,
5163 service->identifier, "Failure", NULL);
5165 if (service->favorite == FALSE)
5166 service->state_ipv4 = service->state_ipv6 =
5167 CONNMAN_SERVICE_STATE_FAILURE;
5168 service->error = string2error(str);
5173 str = g_key_file_get_string(keyfile,
5174 service->identifier, "Modified", NULL);
5176 g_time_val_from_iso8601(str, &service->modified);
5180 str = g_key_file_get_string(keyfile,
5181 service->identifier, "Passphrase", NULL);
5183 g_free(service->passphrase);
5184 service->passphrase = str;
5187 if (service->ipconfig_ipv4 != NULL)
5188 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5189 service->identifier, "IPv4.");
5191 if (service->ipconfig_ipv6 != NULL)
5192 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5193 service->identifier, "IPv6.");
5195 service->nameservers_config = g_key_file_get_string_list(keyfile,
5196 service->identifier, "Nameservers", &length, NULL);
5197 if (service->nameservers_config != NULL && length == 0) {
5198 g_strfreev(service->nameservers_config);
5199 service->nameservers_config = NULL;
5202 service->domains = g_key_file_get_string_list(keyfile,
5203 service->identifier, "Domains", &length, NULL);
5204 if (service->domains != NULL && length == 0) {
5205 g_strfreev(service->domains);
5206 service->domains = NULL;
5209 str = g_key_file_get_string(keyfile,
5210 service->identifier, "Proxy.Method", NULL);
5212 service->proxy_config = string2proxymethod(str);
5216 service->proxies = g_key_file_get_string_list(keyfile,
5217 service->identifier, "Proxy.Servers", &length, NULL);
5218 if (service->proxies != NULL && length == 0) {
5219 g_strfreev(service->proxies);
5220 service->proxies = NULL;
5223 service->excludes = g_key_file_get_string_list(keyfile,
5224 service->identifier, "Proxy.Excludes", &length, NULL);
5225 if (service->excludes != NULL && length == 0) {
5226 g_strfreev(service->excludes);
5227 service->excludes = NULL;
5230 str = g_key_file_get_string(keyfile,
5231 service->identifier, "Proxy.URL", NULL);
5233 g_free(service->pac);
5238 g_key_file_free(keyfile);
5243 static int service_save(struct connman_service *service)
5245 const char *ident = service->profile;
5247 gchar *pathname, *data = NULL;
5250 const char *cst_str = NULL;
5253 DBG("service %p", service);
5258 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5259 if (pathname == NULL)
5262 keyfile = g_key_file_new();
5264 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5268 if (g_key_file_load_from_data(keyfile, data, length,
5276 if (service->name != NULL)
5277 g_key_file_set_string(keyfile, service->identifier,
5278 "Name", service->name);
5280 switch (service->type) {
5281 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5282 case CONNMAN_SERVICE_TYPE_SYSTEM:
5283 case CONNMAN_SERVICE_TYPE_ETHERNET:
5284 case CONNMAN_SERVICE_TYPE_GPS:
5285 case CONNMAN_SERVICE_TYPE_VPN:
5286 case CONNMAN_SERVICE_TYPE_GADGET:
5288 case CONNMAN_SERVICE_TYPE_WIFI:
5289 if (service->network) {
5290 const unsigned char *ssid;
5291 unsigned int ssid_len = 0;
5293 ssid = connman_network_get_blob(service->network,
5294 "WiFi.SSID", &ssid_len);
5296 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5297 char *identifier = service->identifier;
5301 str = g_string_sized_new(ssid_len * 2);
5307 for (i = 0; i < ssid_len; i++)
5308 g_string_append_printf(str,
5311 g_key_file_set_string(keyfile, identifier,
5314 g_string_free(str, TRUE);
5319 case CONNMAN_SERVICE_TYPE_WIMAX:
5320 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5321 case CONNMAN_SERVICE_TYPE_CELLULAR:
5322 g_key_file_set_boolean(keyfile, service->identifier,
5323 "Favorite", service->favorite);
5325 if (service->favorite == TRUE)
5326 g_key_file_set_boolean(keyfile, service->identifier,
5327 "AutoConnect", service->autoconnect);
5329 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5330 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5331 const char *failure = error2string(service->error);
5332 if (failure != NULL)
5333 g_key_file_set_string(keyfile,
5334 service->identifier,
5335 "Failure", failure);
5337 g_key_file_remove_key(keyfile, service->identifier,
5343 str = g_time_val_to_iso8601(&service->modified);
5345 g_key_file_set_string(keyfile, service->identifier,
5350 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5351 g_key_file_set_string(keyfile, service->identifier,
5352 "Passphrase", service->passphrase);
5354 g_key_file_remove_key(keyfile, service->identifier,
5355 "Passphrase", NULL);
5357 if (service->ipconfig_ipv4 != NULL)
5358 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5359 service->identifier, "IPv4.");
5361 if (service->ipconfig_ipv6 != NULL)
5362 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5363 service->identifier, "IPv6.");
5365 if (service->nameservers_config != NULL) {
5366 guint len = g_strv_length(service->nameservers_config);
5368 g_key_file_set_string_list(keyfile, service->identifier,
5370 (const gchar **) service->nameservers_config, len);
5372 g_key_file_remove_key(keyfile, service->identifier,
5373 "Nameservers", NULL);
5375 if (service->domains != NULL) {
5376 guint len = g_strv_length(service->domains);
5378 g_key_file_set_string_list(keyfile, service->identifier,
5380 (const gchar **) service->domains, len);
5382 g_key_file_remove_key(keyfile, service->identifier,
5385 cst_str = proxymethod2string(service->proxy_config);
5386 if (cst_str != NULL)
5387 g_key_file_set_string(keyfile, service->identifier,
5388 "Proxy.Method", cst_str);
5390 if (service->proxies != NULL) {
5391 guint len = g_strv_length(service->proxies);
5393 g_key_file_set_string_list(keyfile, service->identifier,
5395 (const gchar **) service->proxies, len);
5397 g_key_file_remove_key(keyfile, service->identifier,
5398 "Proxy.Servers", NULL);
5400 if (service->excludes != NULL) {
5401 guint len = g_strv_length(service->excludes);
5403 g_key_file_set_string_list(keyfile, service->identifier,
5405 (const gchar **) service->excludes, len);
5407 g_key_file_remove_key(keyfile, service->identifier,
5408 "Proxy.Excludes", NULL);
5410 if (service->pac != NULL && strlen(service->pac) > 0)
5411 g_key_file_set_string(keyfile, service->identifier,
5412 "Proxy.URL", service->pac);
5414 g_key_file_remove_key(keyfile, service->identifier,
5417 data = g_key_file_to_data(keyfile, &length, NULL);
5419 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5420 connman_error("Failed to store service information");
5425 g_key_file_free(keyfile);
5432 static struct connman_storage service_storage = {
5434 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5435 .service_load = service_load,
5436 .service_save = service_save,
5439 int __connman_service_init(void)
5443 connection = connman_dbus_get_connection();
5445 if (connman_storage_register(&service_storage) < 0)
5446 connman_error("Failed to register service storage");
5448 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5451 service_list = g_sequence_new(service_free);
5456 void __connman_service_cleanup(void)
5460 g_sequence_free(service_list);
5461 service_list = NULL;
5463 g_hash_table_destroy(service_hash);
5464 service_hash = NULL;
5466 g_slist_free(counter_list);
5467 counter_list = NULL;
5469 connman_storage_unregister(&service_storage);
5471 dbus_connection_unref(connection);