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
33 #define CONNECT_TIMEOUT 120
35 static DBusConnection *connection = NULL;
37 static GSequence *service_list = NULL;
38 static GHashTable *service_hash = NULL;
39 static GSList *counter_list = NULL;
41 struct connman_stats {
43 connman_bool_t enabled;
44 struct connman_stats_data data_last;
45 struct connman_stats_data data;
49 struct connman_stats_counter {
50 connman_bool_t append_all;
51 struct connman_stats stats;
52 struct connman_stats stats_roaming;
55 struct connman_service {
57 gint session_usage_count;
60 enum connman_service_type type;
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 connman_bool_t network_created;
80 struct connman_ipconfig *ipconfig_ipv4;
81 struct connman_ipconfig *ipconfig_ipv6;
82 struct connman_network *network;
83 struct connman_provider *provider;
85 char **nameservers_config;
89 /* 802.1x settings from the config files */
93 char *client_cert_file;
94 char *private_key_file;
95 char *private_key_passphrase;
99 struct connman_location *location;
100 struct connman_stats stats;
101 struct connman_stats stats_roaming;
102 GHashTable *counter_table;
103 enum connman_service_proxy_method proxy;
104 enum connman_service_proxy_method proxy_config;
111 static void append_path(gpointer value, gpointer user_data)
113 struct connman_service *service = value;
114 DBusMessageIter *iter = user_data;
116 if (service->path == NULL || service->hidden == TRUE)
119 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
123 void __connman_service_list(DBusMessageIter *iter, void *user_data)
125 g_sequence_foreach(service_list, append_path, iter);
130 struct connman_service *service;
133 static void compare_path(gpointer value, gpointer user_data)
135 struct connman_service *service = value;
136 struct find_data *data = user_data;
138 if (data->service != NULL)
141 if (g_strcmp0(service->path, data->path) == 0)
142 data->service = service;
145 static struct connman_service *find_service(const char *path)
147 struct find_data data = { .path = path, .service = NULL };
149 DBG("path %s", path);
151 g_sequence_foreach(service_list, compare_path, &data);
156 const char *__connman_service_type2string(enum connman_service_type type)
159 case CONNMAN_SERVICE_TYPE_UNKNOWN:
161 case CONNMAN_SERVICE_TYPE_SYSTEM:
163 case CONNMAN_SERVICE_TYPE_ETHERNET:
165 case CONNMAN_SERVICE_TYPE_WIFI:
167 case CONNMAN_SERVICE_TYPE_WIMAX:
169 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
171 case CONNMAN_SERVICE_TYPE_CELLULAR:
173 case CONNMAN_SERVICE_TYPE_GPS:
175 case CONNMAN_SERVICE_TYPE_VPN:
177 case CONNMAN_SERVICE_TYPE_GADGET:
184 static const char *security2string(enum connman_service_security security)
187 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
189 case CONNMAN_SERVICE_SECURITY_NONE:
191 case CONNMAN_SERVICE_SECURITY_WEP:
193 case CONNMAN_SERVICE_SECURITY_PSK:
195 case CONNMAN_SERVICE_SECURITY_8021X:
197 case CONNMAN_SERVICE_SECURITY_WPA:
199 case CONNMAN_SERVICE_SECURITY_RSN:
206 static const char *state2string(enum connman_service_state state)
209 case CONNMAN_SERVICE_STATE_UNKNOWN:
211 case CONNMAN_SERVICE_STATE_IDLE:
213 case CONNMAN_SERVICE_STATE_ASSOCIATION:
214 return "association";
215 case CONNMAN_SERVICE_STATE_CONFIGURATION:
216 return "configuration";
217 case CONNMAN_SERVICE_STATE_READY:
219 case CONNMAN_SERVICE_STATE_ONLINE:
221 case CONNMAN_SERVICE_STATE_DISCONNECT:
223 case CONNMAN_SERVICE_STATE_FAILURE:
230 static const char *error2string(enum connman_service_error error)
233 case CONNMAN_SERVICE_ERROR_UNKNOWN:
235 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
236 return "out-of-range";
237 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
238 return "pin-missing";
239 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
240 return "dhcp-failed";
241 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
242 return "connect-failed";
243 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
244 return "login-failed";
245 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
246 return "auth-failed";
247 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
248 return "invalid-key";
254 static enum connman_service_error string2error(const char *error)
256 if (g_strcmp0(error, "dhcp-failed") == 0)
257 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
258 else if (g_strcmp0(error, "pin-missing") == 0)
259 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
260 else if (g_strcmp0(error, "invalid-key") == 0)
261 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
263 return CONNMAN_SERVICE_ERROR_UNKNOWN;
266 static const char *proxymethod2string(enum connman_service_proxy_method method)
269 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
271 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
273 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
275 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
282 static enum connman_service_proxy_method string2proxymethod(const char *method)
284 if (g_strcmp0(method, "direct") == 0)
285 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
286 else if (g_strcmp0(method, "auto") == 0)
287 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
288 else if (g_strcmp0(method, "manual") == 0)
289 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
291 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
294 static enum connman_service_state combine_state(
295 enum connman_service_state state_a,
296 enum connman_service_state state_b)
298 enum connman_service_state result;
300 if (state_a == state_b) {
305 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
310 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
315 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
320 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
325 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
326 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
327 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
328 state_b == CONNMAN_SERVICE_STATE_READY)
335 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
336 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
337 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
338 state_a == CONNMAN_SERVICE_STATE_READY)
345 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
346 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
347 state_b == CONNMAN_SERVICE_STATE_READY)
354 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
355 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
356 state_a == CONNMAN_SERVICE_STATE_READY)
363 if (state_a == CONNMAN_SERVICE_STATE_READY) {
364 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
371 if (state_b == CONNMAN_SERVICE_STATE_READY) {
372 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
379 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
384 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
389 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
394 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
399 result = CONNMAN_SERVICE_STATE_FAILURE;
405 static connman_bool_t is_connecting_state(struct connman_service *service,
406 enum connman_service_state state)
409 case CONNMAN_SERVICE_STATE_UNKNOWN:
410 case CONNMAN_SERVICE_STATE_IDLE:
411 case CONNMAN_SERVICE_STATE_FAILURE:
412 if (service->network != NULL)
413 return connman_network_get_connecting(service->network);
414 case CONNMAN_SERVICE_STATE_DISCONNECT:
415 case CONNMAN_SERVICE_STATE_READY:
416 case CONNMAN_SERVICE_STATE_ONLINE:
418 case CONNMAN_SERVICE_STATE_ASSOCIATION:
419 case CONNMAN_SERVICE_STATE_CONFIGURATION:
426 static connman_bool_t is_connected_state(const struct connman_service *service,
427 enum connman_service_state state)
430 case CONNMAN_SERVICE_STATE_UNKNOWN:
431 case CONNMAN_SERVICE_STATE_IDLE:
432 case CONNMAN_SERVICE_STATE_ASSOCIATION:
433 case CONNMAN_SERVICE_STATE_CONFIGURATION:
434 case CONNMAN_SERVICE_STATE_DISCONNECT:
435 case CONNMAN_SERVICE_STATE_FAILURE:
437 case CONNMAN_SERVICE_STATE_READY:
438 case CONNMAN_SERVICE_STATE_ONLINE:
445 static connman_bool_t is_connecting(struct connman_service *service)
447 enum connman_service_state state;
449 state = combine_state(service->state_ipv4, service->state_ipv6);
451 return is_connecting_state(service, state);
454 static connman_bool_t is_connected(struct connman_service *service)
456 enum connman_service_state state;
458 state = combine_state(service->state_ipv4, service->state_ipv6);
460 return is_connected_state(service, state);
463 static void update_nameservers(struct connman_service *service)
467 if (service->ipconfig_ipv4)
468 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
469 else if (service->ipconfig_ipv6)
470 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
477 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
478 case CONNMAN_SERVICE_STATE_UNKNOWN:
479 case CONNMAN_SERVICE_STATE_IDLE:
480 case CONNMAN_SERVICE_STATE_ASSOCIATION:
481 case CONNMAN_SERVICE_STATE_CONFIGURATION:
483 case CONNMAN_SERVICE_STATE_FAILURE:
484 case CONNMAN_SERVICE_STATE_DISCONNECT:
485 connman_resolver_remove_all(ifname);
487 case CONNMAN_SERVICE_STATE_READY:
488 case CONNMAN_SERVICE_STATE_ONLINE:
492 connman_resolver_remove_all(ifname);
494 if (service->nameservers_config != NULL) {
497 for (i = 0; service->nameservers_config[i] != NULL; i++) {
498 connman_resolver_append(ifname, NULL,
499 service->nameservers_config[i]);
501 } else if (service->nameservers != NULL) {
504 for (i = 0; service->nameservers[i] != NULL; i++) {
505 connman_resolver_append(ifname, NULL,
506 service->nameservers[i]);
510 if (service->domains != NULL) {
513 for (i = 0; service->domains[i]; i++)
514 connman_resolver_append(ifname, service->domains[i],
516 } else if (service->domainname != NULL)
517 connman_resolver_append(ifname, service->domainname, NULL);
519 connman_resolver_flush();
522 int __connman_service_nameserver_append(struct connman_service *service,
523 const char *nameserver)
527 DBG("service %p nameserver %s", service, nameserver);
529 if (nameserver == NULL)
532 if (service->nameservers != NULL) {
535 for (i = 0; service->nameservers[i] != NULL; i++)
536 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
539 len = g_strv_length(service->nameservers);
540 service->nameservers = g_try_renew(char *, service->nameservers,
544 service->nameservers = g_try_new0(char *, len + 2);
547 if (service->nameservers == NULL)
550 service->nameservers[len] = g_strdup(nameserver);
551 service->nameservers[len + 1] = NULL;
553 update_nameservers(service);
558 int __connman_service_nameserver_remove(struct connman_service *service,
559 const char *nameserver)
564 DBG("service %p nameserver %s", service, nameserver);
566 if (nameserver == NULL)
569 if (service->nameservers == NULL)
572 len = g_strv_length(service->nameservers);
574 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
577 g_strfreev(service->nameservers);
578 service->nameservers = NULL;
583 servers = g_try_new0(char *, len - 1);
587 for (i = 0, j = 0; i < len; i++) {
588 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
589 servers[j] = g_strdup(service->nameservers[i]);
593 servers[len - 2] = NULL;
595 g_strfreev(service->nameservers);
596 service->nameservers = servers;
598 update_nameservers(service);
603 void __connman_service_nameserver_clear(struct connman_service *service)
605 g_strfreev(service->nameservers);
606 service->nameservers = NULL;
608 update_nameservers(service);
611 static void nameserver_add_routes(int index, char **nameservers,
616 for (i = 0; nameservers[i] != NULL; i++) {
617 if (connman_inet_compare_subnet(index, nameservers[i]))
620 connman_inet_add_host_route(index, nameservers[i], gw);
624 static void nameserver_del_routes(int index, char **nameservers)
628 for (i = 0; nameservers[i] != NULL; i++)
629 connman_inet_del_host_route(index, nameservers[i]);
632 void __connman_service_nameserver_add_routes(struct connman_service *service,
640 if (service->network != NULL)
641 index = connman_network_get_index(service->network);
642 else if (service->provider != NULL)
643 index = connman_provider_get_index(service->provider);
645 if (service->nameservers_config != NULL) {
647 * Configured nameserver takes preference over the
648 * discoverd nameserver gathered from DHCP, VPN, etc.
650 nameserver_add_routes(index, service->nameservers_config, gw);
651 } else if (service->nameservers != NULL) {
653 * We add nameservers host routes for nameservers that
654 * are not on our subnet. For those who are, the subnet
655 * route will be installed by the time the dns proxy code
656 * tries to reach them. The subnet route is installed
657 * when setting the interface IP address.
659 nameserver_add_routes(index, service->nameservers, gw);
663 void __connman_service_nameserver_del_routes(struct connman_service *service)
670 if (service->network != NULL)
671 index = connman_network_get_index(service->network);
672 else if (service->provider != NULL)
673 index = connman_provider_get_index(service->provider);
675 if (service->nameservers_config != NULL)
676 nameserver_del_routes(index, service->nameservers_config);
677 else if (service->nameservers != NULL)
678 nameserver_del_routes(index, service->nameservers);
681 static struct connman_stats *stats_get(struct connman_service *service)
683 if (service->roaming == TRUE)
684 return &service->stats_roaming;
686 return &service->stats;
689 static connman_bool_t stats_enabled(struct connman_service *service)
691 struct connman_stats *stats = stats_get(service);
693 return stats->enabled;
696 static void stats_start(struct connman_service *service)
698 struct connman_stats *stats = stats_get(service);
700 DBG("service %p", service);
702 if (stats->timer == NULL)
705 stats->enabled = TRUE;
706 stats->data_last.time = stats->data.time;
708 g_timer_start(stats->timer);
711 static void stats_stop(struct connman_service *service)
713 struct connman_stats *stats = stats_get(service);
714 unsigned int seconds;
716 DBG("service %p", service);
718 if (stats->timer == NULL)
721 if (stats->enabled == FALSE)
724 g_timer_stop(stats->timer);
726 seconds = g_timer_elapsed(stats->timer, NULL);
727 stats->data.time = stats->data_last.time + seconds;
729 stats->enabled = FALSE;
732 static void reset_stats(struct connman_service *service)
734 DBG("service %p", service);
737 service->stats.valid = FALSE;
739 service->stats.data.rx_packets = 0;
740 service->stats.data.tx_packets = 0;
741 service->stats.data.rx_bytes = 0;
742 service->stats.data.tx_bytes = 0;
743 service->stats.data.rx_errors = 0;
744 service->stats.data.tx_errors = 0;
745 service->stats.data.rx_dropped = 0;
746 service->stats.data.tx_dropped = 0;
747 service->stats.data.time = 0;
748 service->stats.data_last.time = 0;
750 g_timer_reset(service->stats.timer);
753 service->stats_roaming.valid = FALSE;
755 service->stats_roaming.data.rx_packets = 0;
756 service->stats_roaming.data.tx_packets = 0;
757 service->stats_roaming.data.rx_bytes = 0;
758 service->stats_roaming.data.tx_bytes = 0;
759 service->stats_roaming.data.rx_errors = 0;
760 service->stats_roaming.data.tx_errors = 0;
761 service->stats_roaming.data.rx_dropped = 0;
762 service->stats_roaming.data.tx_dropped = 0;
763 service->stats_roaming.data.time = 0;
764 service->stats_roaming.data_last.time = 0;
766 g_timer_reset(service->stats_roaming.timer);
769 static struct connman_service *get_default(void)
771 struct connman_service *service;
774 iter = g_sequence_get_begin_iter(service_list);
776 if (g_sequence_iter_is_end(iter) == TRUE)
779 service = g_sequence_get(iter);
781 if (is_connected(service) == FALSE)
787 static void default_changed(void)
789 struct connman_service *service = get_default();
791 __connman_notifier_default_changed(service);
794 const char *__connman_service_default(void)
796 struct connman_service *service;
798 service = get_default();
802 return __connman_service_type2string(service->type);
805 static void state_changed(struct connman_service *service)
807 enum connman_service_state state;
810 state = combine_state(service->state_ipv4, service->state_ipv6);
812 __connman_notifier_service_state_changed(service, state);
814 str = state2string(state);
818 connman_dbus_property_changed_basic(service->path,
819 CONNMAN_SERVICE_INTERFACE, "State",
820 DBUS_TYPE_STRING, &str);
823 static void strength_changed(struct connman_service *service)
825 if (service->strength == 0)
828 connman_dbus_property_changed_basic(service->path,
829 CONNMAN_SERVICE_INTERFACE, "Strength",
830 DBUS_TYPE_BYTE, &service->strength);
833 static void favorite_changed(struct connman_service *service)
835 if (service->path == NULL)
838 connman_dbus_property_changed_basic(service->path,
839 CONNMAN_SERVICE_INTERFACE, "Favorite",
840 DBUS_TYPE_BOOLEAN, &service->favorite);
843 static void immutable_changed(struct connman_service *service)
845 if (service->path == NULL)
848 connman_dbus_property_changed_basic(service->path,
849 CONNMAN_SERVICE_INTERFACE, "Immutable",
850 DBUS_TYPE_BOOLEAN, &service->immutable);
853 static void roaming_changed(struct connman_service *service)
855 if (service->path == NULL)
858 connman_dbus_property_changed_basic(service->path,
859 CONNMAN_SERVICE_INTERFACE, "Roaming",
860 DBUS_TYPE_BOOLEAN, &service->roaming);
863 static void autoconnect_changed(struct connman_service *service)
865 if (service->path == NULL)
868 connman_dbus_property_changed_basic(service->path,
869 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
870 DBUS_TYPE_BOOLEAN, &service->autoconnect);
873 static void passphrase_changed(struct connman_service *service)
875 dbus_bool_t required;
877 switch (service->type) {
878 case CONNMAN_SERVICE_TYPE_UNKNOWN:
879 case CONNMAN_SERVICE_TYPE_SYSTEM:
880 case CONNMAN_SERVICE_TYPE_ETHERNET:
881 case CONNMAN_SERVICE_TYPE_WIMAX:
882 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
883 case CONNMAN_SERVICE_TYPE_CELLULAR:
884 case CONNMAN_SERVICE_TYPE_GPS:
885 case CONNMAN_SERVICE_TYPE_VPN:
886 case CONNMAN_SERVICE_TYPE_GADGET:
888 case CONNMAN_SERVICE_TYPE_WIFI:
891 switch (service->security) {
892 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
893 case CONNMAN_SERVICE_SECURITY_NONE:
895 case CONNMAN_SERVICE_SECURITY_WEP:
896 case CONNMAN_SERVICE_SECURITY_PSK:
897 case CONNMAN_SERVICE_SECURITY_WPA:
898 case CONNMAN_SERVICE_SECURITY_RSN:
899 if (service->passphrase == NULL)
902 case CONNMAN_SERVICE_SECURITY_8021X:
908 connman_dbus_property_changed_basic(service->path,
909 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
910 DBUS_TYPE_BOOLEAN, &required);
913 static void login_changed(struct connman_service *service)
915 dbus_bool_t required = service->login_required;
917 if (service->path == NULL)
920 connman_dbus_property_changed_basic(service->path,
921 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
922 DBUS_TYPE_BOOLEAN, &required);
925 static void append_security(DBusMessageIter *iter, void *user_data)
927 struct connman_service *service = user_data;
930 str = security2string(service->security);
932 dbus_message_iter_append_basic(iter,
933 DBUS_TYPE_STRING, &str);
936 if (service->wps == TRUE)
937 dbus_message_iter_append_basic(iter,
938 DBUS_TYPE_STRING, &str);
941 static void append_ethernet(DBusMessageIter *iter, void *user_data)
943 struct connman_service *service = user_data;
945 if (service->ipconfig_ipv4 != NULL)
946 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
948 else if (service->ipconfig_ipv6 != NULL)
949 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
953 static void append_ipv4(DBusMessageIter *iter, void *user_data)
955 struct connman_service *service = user_data;
957 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
958 state2string(service->state_ipv4));
960 if (is_connected_state(service, service->state_ipv4) == FALSE)
963 if (service->ipconfig_ipv4 != NULL)
964 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
967 static void append_ipv6(DBusMessageIter *iter, void *user_data)
969 struct connman_service *service = user_data;
971 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
972 state2string(service->state_ipv6));
974 if (is_connected_state(service, service->state_ipv6) == FALSE)
977 if (service->ipconfig_ipv6 != NULL)
978 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
979 service->ipconfig_ipv4);
982 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
984 struct connman_service *service = user_data;
986 if (service->ipconfig_ipv4 != NULL)
987 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
991 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
993 struct connman_service *service = user_data;
995 if (service->ipconfig_ipv6 != NULL)
996 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1000 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1005 servers = *nameservers;
1007 for (i = 0; servers[i] != NULL; i++) {
1008 dbus_message_iter_append_basic(iter,
1009 DBUS_TYPE_STRING, &servers[i]);
1013 static void append_dns(DBusMessageIter *iter, void *user_data)
1015 struct connman_service *service = user_data;
1017 if (is_connected(service) == FALSE)
1020 if (service->nameservers_config != NULL) {
1021 append_nameserver(iter, &service->nameservers_config);
1023 } else if (service->nameservers != NULL) {
1024 append_nameserver(iter, &service->nameservers);
1029 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1031 struct connman_service *service = user_data;
1034 if (service->nameservers_config == NULL)
1037 for (i = 0; service->nameservers_config[i]; i++) {
1038 dbus_message_iter_append_basic(iter,
1040 &service->nameservers_config[i]);
1044 static void append_domain(DBusMessageIter *iter, void *user_data)
1046 struct connman_service *service = user_data;
1048 if (is_connected(service) == FALSE &&
1049 is_connecting(service) == FALSE)
1052 if (service->domainname == NULL)
1055 dbus_message_iter_append_basic(iter,
1056 DBUS_TYPE_STRING, &service->domainname);
1059 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1061 struct connman_service *service = user_data;
1064 if (service->domains == NULL)
1067 for (i = 0; service->domains[i]; i++)
1068 dbus_message_iter_append_basic(iter,
1069 DBUS_TYPE_STRING, &service->domains[i]);
1072 static void append_proxies(DBusMessageIter *iter, void *user_data)
1074 struct connman_service *service = user_data;
1077 if (service->proxies == NULL)
1080 for (i = 0; service->proxies[i]; i++)
1081 dbus_message_iter_append_basic(iter,
1082 DBUS_TYPE_STRING, &service->proxies[i]);
1085 static void append_excludes(DBusMessageIter *iter, void *user_data)
1087 struct connman_service *service = user_data;
1090 if (service->excludes == NULL)
1093 for (i = 0; service->excludes[i]; i++)
1094 dbus_message_iter_append_basic(iter,
1095 DBUS_TYPE_STRING, &service->excludes[i]);
1098 static void append_proxy(DBusMessageIter *iter, void *user_data)
1100 struct connman_service *service = user_data;
1101 enum connman_service_proxy_method proxy;
1102 const char *pac = NULL;
1103 const char *method = proxymethod2string(
1104 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1108 if (is_connected(service) == FALSE)
1111 proxy = connman_service_get_proxy_method(service);
1114 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1116 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1118 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1119 connman_dbus_dict_append_array(iter, "Servers",
1120 DBUS_TYPE_STRING, append_proxies,
1123 connman_dbus_dict_append_array(iter, "Excludes",
1124 DBUS_TYPE_STRING, append_excludes,
1127 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1128 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1129 if (service->ipconfig_ipv4 != NULL)
1130 pac = __connman_ipconfig_get_proxy_autoconfig(
1131 service->ipconfig_ipv4);
1132 else if (service->ipconfig_ipv6 != NULL)
1133 pac = __connman_ipconfig_get_proxy_autoconfig(
1134 service->ipconfig_ipv6);
1136 if (service->pac == NULL && pac == NULL)
1139 if (service->pac != NULL)
1142 connman_dbus_dict_append_basic(iter, "URL",
1143 DBUS_TYPE_STRING, &pac);
1147 method = proxymethod2string(proxy);
1150 connman_dbus_dict_append_basic(iter, "Method",
1151 DBUS_TYPE_STRING, &method);
1154 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1156 struct connman_service *service = user_data;
1159 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1162 switch (service->proxy_config) {
1163 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1165 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1167 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1168 if (service->proxies != NULL)
1169 connman_dbus_dict_append_array(iter, "Servers",
1171 append_proxies, service);
1173 if (service->excludes != NULL)
1174 connman_dbus_dict_append_array(iter, "Excludes",
1176 append_excludes, service);
1178 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1179 if (service->pac != NULL)
1180 connman_dbus_dict_append_basic(iter, "URL",
1181 DBUS_TYPE_STRING, &service->pac);
1185 method = proxymethod2string(service->proxy_config);
1187 connman_dbus_dict_append_basic(iter, "Method",
1188 DBUS_TYPE_STRING, &method);
1191 static void append_provider(DBusMessageIter *iter, void *user_data)
1193 struct connman_service *service = user_data;
1195 DBG("%p %p", service, service->provider);
1197 if (is_connected(service) == FALSE)
1200 if (service->provider != NULL)
1201 __connman_provider_append_properties(service->provider, iter);
1205 static void settings_changed(struct connman_service *service,
1206 struct connman_ipconfig *ipconfig)
1208 connman_dbus_property_changed_dict(service->path,
1209 CONNMAN_SERVICE_INTERFACE, "IPv4",
1210 append_ipv4, service);
1212 connman_dbus_property_changed_dict(service->path,
1213 CONNMAN_SERVICE_INTERFACE, "IPv6",
1214 append_ipv6, service);
1216 __connman_notifier_ipconfig_changed(service, ipconfig);
1219 static void ipv4_configuration_changed(struct connman_service *service)
1221 connman_dbus_property_changed_dict(service->path,
1222 CONNMAN_SERVICE_INTERFACE,
1223 "IPv4.Configuration",
1228 static void ipv6_configuration_changed(struct connman_service *service)
1230 connman_dbus_property_changed_dict(service->path,
1231 CONNMAN_SERVICE_INTERFACE,
1232 "IPv6.Configuration",
1237 static void dns_changed(struct connman_service *service)
1239 connman_dbus_property_changed_array(service->path,
1240 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1241 DBUS_TYPE_STRING, append_dns, service);
1244 static void dns_configuration_changed(struct connman_service *service)
1246 connman_dbus_property_changed_array(service->path,
1247 CONNMAN_SERVICE_INTERFACE,
1248 "Nameservers.Configuration",
1249 DBUS_TYPE_STRING, append_dnsconfig, service);
1251 dns_changed(service);
1254 static void domain_changed(struct connman_service *service)
1256 connman_dbus_property_changed_array(service->path,
1257 CONNMAN_SERVICE_INTERFACE, "Domains",
1258 DBUS_TYPE_STRING, append_domain, service);
1261 static void domain_configuration_changed(struct connman_service *service)
1263 connman_dbus_property_changed_array(service->path,
1264 CONNMAN_SERVICE_INTERFACE,
1265 "Domains.Configuration",
1266 DBUS_TYPE_STRING, append_domainconfig, service);
1269 static void proxy_changed(struct connman_service *service)
1271 connman_dbus_property_changed_dict(service->path,
1272 CONNMAN_SERVICE_INTERFACE, "Proxy",
1273 append_proxy, service);
1276 static void proxy_configuration_changed(struct connman_service *service)
1278 connman_dbus_property_changed_dict(service->path,
1279 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1280 append_proxyconfig, service);
1282 proxy_changed(service);
1285 static void link_changed(struct connman_service *service)
1287 connman_dbus_property_changed_dict(service->path,
1288 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1289 append_ethernet, service);
1292 static void stats_append_counters(DBusMessageIter *dict,
1293 struct connman_stats_data *stats,
1294 struct connman_stats_data *counters,
1295 connman_bool_t append_all)
1297 if (counters->rx_packets != stats->rx_packets || append_all) {
1298 counters->rx_packets = stats->rx_packets;
1299 connman_dbus_dict_append_basic(dict, "RX.Packets",
1300 DBUS_TYPE_UINT32, &stats->rx_packets);
1303 if (counters->tx_packets != stats->tx_packets || append_all) {
1304 counters->tx_packets = stats->tx_packets;
1305 connman_dbus_dict_append_basic(dict, "TX.Packets",
1306 DBUS_TYPE_UINT32, &stats->tx_packets);
1309 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1310 counters->rx_bytes = stats->rx_bytes;
1311 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1312 DBUS_TYPE_UINT32, &stats->rx_bytes);
1315 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1316 counters->tx_bytes = stats->tx_bytes;
1317 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1318 DBUS_TYPE_UINT32, &stats->tx_bytes);
1321 if (counters->rx_errors != stats->rx_errors || append_all) {
1322 counters->rx_errors = stats->rx_errors;
1323 connman_dbus_dict_append_basic(dict, "RX.Errors",
1324 DBUS_TYPE_UINT32, &stats->rx_errors);
1327 if (counters->tx_errors != stats->tx_errors || append_all) {
1328 counters->tx_errors = stats->tx_errors;
1329 connman_dbus_dict_append_basic(dict, "TX.Errors",
1330 DBUS_TYPE_UINT32, &stats->tx_errors);
1333 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1334 counters->rx_dropped = stats->rx_dropped;
1335 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1336 DBUS_TYPE_UINT32, &stats->rx_dropped);
1339 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1340 counters->tx_dropped = stats->tx_dropped;
1341 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1342 DBUS_TYPE_UINT32, &stats->tx_dropped);
1345 if (counters->time != stats->time || append_all) {
1346 counters->time = stats->time;
1347 connman_dbus_dict_append_basic(dict, "Time",
1348 DBUS_TYPE_UINT32, &stats->time);
1352 static void stats_append(struct connman_service *service,
1353 const char *counter,
1354 struct connman_stats_counter *counters,
1355 connman_bool_t append_all)
1357 DBusMessageIter array, dict;
1360 DBG("service %p counter %s", service, counter);
1362 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1366 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1367 &service->path, DBUS_TYPE_INVALID);
1369 dbus_message_iter_init_append(msg, &array);
1372 connman_dbus_dict_open(&array, &dict);
1374 stats_append_counters(&dict, &service->stats.data,
1375 &counters->stats.data, append_all);
1377 connman_dbus_dict_close(&array, &dict);
1379 /* roaming counter */
1380 connman_dbus_dict_open(&array, &dict);
1382 stats_append_counters(&dict, &service->stats_roaming.data,
1383 &counters->stats_roaming.data, append_all);
1385 connman_dbus_dict_close(&array, &dict);
1387 __connman_counter_send_usage(counter, msg);
1390 static void stats_update(struct connman_service *service,
1391 unsigned int rx_packets, unsigned int tx_packets,
1392 unsigned int rx_bytes, unsigned int tx_bytes,
1393 unsigned int rx_errors, unsigned int tx_errors,
1394 unsigned int rx_dropped, unsigned int tx_dropped)
1396 struct connman_stats *stats = stats_get(service);
1397 struct connman_stats_data *data_last = &stats->data_last;
1398 struct connman_stats_data *data = &stats->data;
1399 unsigned int seconds;
1401 DBG("service %p", service);
1403 if (stats->valid == TRUE) {
1405 rx_packets - data_last->rx_packets;
1407 tx_packets - data_last->tx_packets;
1409 rx_bytes - data_last->rx_bytes;
1411 tx_bytes - data_last->tx_bytes;
1413 rx_errors - data_last->rx_errors;
1415 tx_errors - data_last->tx_errors;
1417 rx_dropped - data_last->rx_dropped;
1419 tx_dropped - data_last->tx_dropped;
1421 stats->valid = TRUE;
1424 data_last->rx_packets = rx_packets;
1425 data_last->tx_packets = tx_packets;
1426 data_last->rx_bytes = rx_bytes;
1427 data_last->tx_bytes = tx_bytes;
1428 data_last->rx_errors = rx_errors;
1429 data_last->tx_errors = tx_errors;
1430 data_last->rx_dropped = rx_dropped;
1431 data_last->tx_dropped = tx_dropped;
1433 seconds = g_timer_elapsed(stats->timer, NULL);
1434 stats->data.time = stats->data_last.time + seconds;
1437 void __connman_service_notify(struct connman_service *service,
1438 unsigned int rx_packets, unsigned int tx_packets,
1439 unsigned int rx_bytes, unsigned int tx_bytes,
1440 unsigned int rx_errors, unsigned int tx_errors,
1441 unsigned int rx_dropped, unsigned int tx_dropped)
1443 GHashTableIter iter;
1444 gpointer key, value;
1445 const char *counter;
1446 struct connman_stats_counter *counters;
1447 struct connman_stats_data *data;
1450 if (service == NULL)
1453 if (is_connected(service) == FALSE)
1456 stats_update(service,
1457 rx_packets, tx_packets,
1459 rx_errors, tx_errors,
1460 rx_dropped, tx_dropped);
1462 data = &stats_get(service)->data;
1463 err = __connman_stats_update(service, service->roaming, data);
1465 connman_error("Failed to store statistics for %s",
1466 service->identifier);
1468 g_hash_table_iter_init(&iter, service->counter_table);
1469 while (g_hash_table_iter_next(&iter, &key, &value)) {
1473 stats_append(service, counter, counters, counters->append_all);
1474 counters->append_all = FALSE;
1478 int __connman_service_counter_register(const char *counter)
1480 struct connman_service *service;
1481 GSequenceIter *iter;
1482 struct connman_stats_counter *counters;
1484 DBG("counter %s", counter);
1486 counter_list = g_slist_append(counter_list, (gpointer)counter);
1488 iter = g_sequence_get_begin_iter(service_list);
1490 while (g_sequence_iter_is_end(iter) == FALSE) {
1491 service = g_sequence_get(iter);
1493 counters = g_try_new0(struct connman_stats_counter, 1);
1494 if (counters == NULL)
1497 counters->append_all = TRUE;
1499 g_hash_table_replace(service->counter_table, (gpointer)counter,
1502 iter = g_sequence_iter_next(iter);
1508 void __connman_service_counter_unregister(const char *counter)
1510 struct connman_service *service;
1511 GSequenceIter *iter;
1513 DBG("counter %s", counter);
1515 iter = g_sequence_get_begin_iter(service_list);
1517 while (g_sequence_iter_is_end(iter) == FALSE) {
1518 service = g_sequence_get(iter);
1520 g_hash_table_remove(service->counter_table, counter);
1522 iter = g_sequence_iter_next(iter);
1525 counter_list = g_slist_remove(counter_list, counter);
1528 GSequence *__connman_service_get_list(struct connman_session *session,
1529 service_match_cb service_match,
1530 create_service_entry_cb create_service_entry,
1531 GDestroyNotify destroy_service_entry)
1534 GSequenceIter *iter;
1535 struct connman_service *service;
1536 enum connman_service_state state;
1537 struct service_entry *entry;
1539 list = g_sequence_new(destroy_service_entry);
1543 iter = g_sequence_get_begin_iter(service_list);
1545 while (g_sequence_iter_is_end(iter) == FALSE) {
1546 service = g_sequence_get(iter);
1548 if (service_match(session, service) == TRUE) {
1549 state = combine_state(service->state_ipv4,
1550 service->state_ipv6);
1551 entry = create_service_entry(service, service->name,
1556 g_sequence_append(list, entry);
1559 iter = g_sequence_iter_next(iter);
1565 void __connman_service_session_inc(struct connman_service *service)
1567 DBG("service %p", service);
1569 g_atomic_int_inc(&service->session_usage_count);
1572 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1574 connman_bool_t in_use;
1576 if (g_atomic_int_dec_and_test(&service->session_usage_count) == TRUE)
1581 DBG("service %p last %d", service, in_use);
1586 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1587 struct connman_service *service)
1589 dbus_bool_t required;
1592 str = __connman_service_type2string(service->type);
1594 connman_dbus_dict_append_basic(dict, "Type",
1595 DBUS_TYPE_STRING, &str);
1597 connman_dbus_dict_append_array(dict, "Security",
1598 DBUS_TYPE_STRING, append_security, service);
1600 str = state2string(combine_state(service->state_ipv4,
1601 service->state_ipv6));
1603 connman_dbus_dict_append_basic(dict, "State",
1604 DBUS_TYPE_STRING, &str);
1606 str = error2string(service->error);
1608 connman_dbus_dict_append_basic(dict, "Error",
1609 DBUS_TYPE_STRING, &str);
1611 if (service->strength > 0)
1612 connman_dbus_dict_append_basic(dict, "Strength",
1613 DBUS_TYPE_BYTE, &service->strength);
1615 connman_dbus_dict_append_basic(dict, "Favorite",
1616 DBUS_TYPE_BOOLEAN, &service->favorite);
1618 connman_dbus_dict_append_basic(dict, "Immutable",
1619 DBUS_TYPE_BOOLEAN, &service->immutable);
1621 if (service->favorite == TRUE)
1622 connman_dbus_dict_append_basic(dict, "AutoConnect",
1623 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1625 connman_dbus_dict_append_basic(dict, "AutoConnect",
1626 DBUS_TYPE_BOOLEAN, &service->favorite);
1628 if (service->name != NULL)
1629 connman_dbus_dict_append_basic(dict, "Name",
1630 DBUS_TYPE_STRING, &service->name);
1632 connman_dbus_dict_append_basic(dict, "LoginRequired",
1633 DBUS_TYPE_BOOLEAN, &service->login_required);
1635 switch (service->type) {
1636 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1637 case CONNMAN_SERVICE_TYPE_SYSTEM:
1638 case CONNMAN_SERVICE_TYPE_GPS:
1639 case CONNMAN_SERVICE_TYPE_VPN:
1640 case CONNMAN_SERVICE_TYPE_GADGET:
1642 case CONNMAN_SERVICE_TYPE_CELLULAR:
1643 connman_dbus_dict_append_basic(dict, "Roaming",
1644 DBUS_TYPE_BOOLEAN, &service->roaming);
1647 connman_dbus_dict_append_basic(dict, "SetupRequired",
1648 DBUS_TYPE_BOOLEAN, &required);
1649 connman_dbus_dict_append_dict(dict, "Ethernet",
1650 append_ethernet, service);
1652 case CONNMAN_SERVICE_TYPE_WIFI:
1653 if (service->passphrase != NULL && limited == FALSE)
1654 connman_dbus_dict_append_basic(dict, "Passphrase",
1655 DBUS_TYPE_STRING, &service->passphrase);
1659 switch (service->security) {
1660 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1661 case CONNMAN_SERVICE_SECURITY_NONE:
1663 case CONNMAN_SERVICE_SECURITY_WEP:
1664 case CONNMAN_SERVICE_SECURITY_PSK:
1665 case CONNMAN_SERVICE_SECURITY_WPA:
1666 case CONNMAN_SERVICE_SECURITY_RSN:
1667 if (service->passphrase == NULL)
1670 case CONNMAN_SERVICE_SECURITY_8021X:
1674 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1675 DBUS_TYPE_BOOLEAN, &required);
1678 case CONNMAN_SERVICE_TYPE_ETHERNET:
1679 case CONNMAN_SERVICE_TYPE_WIMAX:
1680 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1681 connman_dbus_dict_append_dict(dict, "Ethernet",
1682 append_ethernet, service);
1686 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1688 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1689 append_ipv4config, service);
1691 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1693 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1694 append_ipv6config, service);
1696 connman_dbus_dict_append_array(dict, "Nameservers",
1697 DBUS_TYPE_STRING, append_dns, service);
1699 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1700 DBUS_TYPE_STRING, append_dnsconfig, service);
1702 connman_dbus_dict_append_array(dict, "Domains",
1703 DBUS_TYPE_STRING, append_domain, service);
1705 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1706 DBUS_TYPE_STRING, append_domainconfig, service);
1708 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1710 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1711 append_proxyconfig, service);
1713 connman_dbus_dict_append_dict(dict, "Provider",
1714 append_provider, service);
1717 static void append_struct(gpointer value, gpointer user_data)
1719 struct connman_service *service = value;
1720 DBusMessageIter *iter = user_data;
1721 DBusMessageIter entry, dict;
1723 if (service->path == NULL || service->hidden == TRUE)
1726 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1728 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1731 connman_dbus_dict_open(&entry, &dict);
1732 append_properties(&dict, TRUE, service);
1733 connman_dbus_dict_close(&entry, &dict);
1735 dbus_message_iter_close_container(iter, &entry);
1738 void __connman_service_list_struct(DBusMessageIter *iter)
1740 g_sequence_foreach(service_list, append_struct, iter);
1743 int __connman_service_get_index(struct connman_service *service)
1745 if (service == NULL)
1748 if (service->network != NULL)
1749 return connman_network_get_index(service->network);
1750 else if (service->provider != NULL)
1751 return connman_provider_get_index(service->provider);
1756 void __connman_service_set_domainname(struct connman_service *service,
1757 const char *domainname)
1759 if (service == NULL)
1762 g_free(service->domainname);
1763 service->domainname = g_strdup(domainname);
1765 domain_changed(service);
1768 const char *connman_service_get_domainname(struct connman_service *service)
1770 if (service == NULL)
1773 if (service->domains != NULL)
1774 return service->domains[0];
1776 return service->domainname;
1779 char **connman_service_get_nameservers(struct connman_service *service)
1781 if (service == NULL)
1784 if (service->nameservers_config != NULL)
1785 return service->nameservers_config;
1786 else if (service->nameservers != NULL)
1787 return service->nameservers;
1792 void connman_service_set_proxy_method(struct connman_service *service,
1793 enum connman_service_proxy_method method)
1795 if (service == NULL)
1798 service->proxy = method;
1800 proxy_changed(service);
1802 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1803 __connman_notifier_proxy_changed(service);
1806 enum connman_service_proxy_method connman_service_get_proxy_method(
1807 struct connman_service *service)
1809 if (service == NULL)
1810 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1812 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1813 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1814 service->pac == NULL)
1815 return service->proxy;
1817 return service->proxy_config;
1820 return service->proxy;
1823 char **connman_service_get_proxy_servers(struct connman_service *service)
1825 return g_strdupv(service->proxies);
1828 char **connman_service_get_proxy_excludes(struct connman_service *service)
1830 return g_strdupv(service->excludes);
1833 const char *connman_service_get_proxy_url(struct connman_service *service)
1835 if (service == NULL)
1838 return service->pac;
1841 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1844 if (service == NULL)
1847 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1849 if (service->ipconfig_ipv4) {
1850 if (__connman_ipconfig_set_proxy_autoconfig(
1851 service->ipconfig_ipv4, url) < 0)
1853 } else if (service->ipconfig_ipv6) {
1854 if (__connman_ipconfig_set_proxy_autoconfig(
1855 service->ipconfig_ipv6, url) < 0)
1860 proxy_changed(service);
1862 __connman_notifier_proxy_changed(service);
1865 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1867 if (service == NULL)
1870 if (service->ipconfig_ipv4)
1871 return __connman_ipconfig_get_proxy_autoconfig(
1872 service->ipconfig_ipv4);
1873 else if (service->ipconfig_ipv6)
1874 return __connman_ipconfig_get_proxy_autoconfig(
1875 service->ipconfig_ipv6);
1879 static void update_timeservers(struct connman_service *service)
1883 if (service->timeservers == NULL)
1886 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
1887 case CONNMAN_SERVICE_STATE_UNKNOWN:
1888 case CONNMAN_SERVICE_STATE_IDLE:
1889 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1890 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1892 case CONNMAN_SERVICE_STATE_FAILURE:
1893 case CONNMAN_SERVICE_STATE_DISCONNECT:
1894 for (i = 0; service->timeservers[i] != NULL; i++)
1895 connman_timeserver_remove(service->timeservers[i]);
1897 case CONNMAN_SERVICE_STATE_READY:
1898 case CONNMAN_SERVICE_STATE_ONLINE:
1902 for (i = 0; service->timeservers[i] != NULL; i++)
1903 connman_timeserver_append(service->timeservers[i]);
1906 int __connman_service_timeserver_append(struct connman_service *service,
1907 const char *timeserver)
1911 DBG("service %p timeserver %s", service, timeserver);
1913 if (timeserver == NULL)
1916 if (service->timeservers != NULL) {
1919 for (i = 0; service->timeservers[i] != NULL; i++)
1920 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1923 len = g_strv_length(service->timeservers);
1924 service->timeservers = g_try_renew(char *, service->timeservers,
1928 service->timeservers = g_try_new0(char *, len + 2);
1931 if (service->timeservers == NULL)
1934 service->timeservers[len] = g_strdup(timeserver);
1935 service->timeservers[len + 1] = NULL;
1937 update_timeservers(service);
1942 int __connman_service_timeserver_remove(struct connman_service *service,
1943 const char *timeserver)
1948 DBG("service %p timeserver %s", service, timeserver);
1950 if (timeserver == NULL)
1953 if (service->timeservers == NULL)
1956 len = g_strv_length(service->timeservers);
1958 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
1961 g_strfreev(service->timeservers);
1962 service->timeservers = NULL;
1967 servers = g_try_new0(char *, len - 1);
1968 if (servers == NULL)
1971 for (i = 0, j = 0; i < len; i++) {
1972 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
1973 servers[j] = g_strdup(service->timeservers[i]);
1977 servers[len - 2] = NULL;
1979 g_strfreev(service->timeservers);
1980 service->timeservers = servers;
1982 update_timeservers(service);
1987 void __connman_service_set_pac(struct connman_service *service,
1993 g_free(service->pac);
1994 service->pac = g_strdup(pac);
1997 void __connman_service_set_identity(struct connman_service *service,
1998 const char *identity)
2000 if (service->immutable)
2003 g_free(service->identity);
2004 service->identity = g_strdup(identity);
2006 if (service->network != NULL)
2007 connman_network_set_string(service->network,
2012 void __connman_service_set_passphrase(struct connman_service *service,
2013 const char* passphrase)
2015 if (service->immutable == TRUE)
2018 g_free(service->passphrase);
2019 service->passphrase = g_strdup(passphrase);
2021 passphrase_changed(service);
2023 if (service->network != NULL)
2024 connman_network_set_string(service->network,
2026 service->passphrase);
2028 __connman_storage_save_service(service);
2031 static DBusMessage *get_properties(DBusConnection *conn,
2032 DBusMessage *msg, void *user_data)
2034 struct connman_service *service = user_data;
2036 DBusMessageIter array, dict;
2038 DBG("service %p", service);
2040 reply = dbus_message_new_method_return(msg);
2044 dbus_message_iter_init_append(reply, &array);
2046 connman_dbus_dict_open(&array, &dict);
2047 append_properties(&dict, FALSE, service);
2048 connman_dbus_dict_close(&array, &dict);
2053 static int update_proxy_configuration(struct connman_service *service,
2054 DBusMessageIter *array)
2056 DBusMessageIter dict;
2057 enum connman_service_proxy_method method;
2058 GString *servers_str = NULL;
2059 GString *excludes_str = NULL;
2060 const char *url = NULL;
2062 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2064 dbus_message_iter_recurse(array, &dict);
2066 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2067 DBusMessageIter entry, variant;
2071 dbus_message_iter_recurse(&dict, &entry);
2073 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2076 dbus_message_iter_get_basic(&entry, &key);
2077 dbus_message_iter_next(&entry);
2079 if (dbus_message_iter_get_arg_type(&entry) !=
2083 dbus_message_iter_recurse(&entry, &variant);
2084 type = dbus_message_iter_get_arg_type(&variant);
2086 if (g_str_equal(key, "Method") == TRUE) {
2089 if (type != DBUS_TYPE_STRING)
2092 dbus_message_iter_get_basic(&variant, &val);
2093 method = string2proxymethod(val);
2094 } else if (g_str_equal(key, "URL") == TRUE) {
2095 if (type != DBUS_TYPE_STRING)
2098 dbus_message_iter_get_basic(&variant, &url);
2099 } else if (g_str_equal(key, "Servers") == TRUE) {
2100 DBusMessageIter str_array;
2102 if (type != DBUS_TYPE_ARRAY)
2105 servers_str = g_string_new(NULL);
2106 if (servers_str == NULL)
2109 dbus_message_iter_recurse(&variant, &str_array);
2111 while (dbus_message_iter_get_arg_type(&str_array) ==
2115 dbus_message_iter_get_basic(&str_array, &val);
2117 if (servers_str->len > 0)
2118 g_string_append_printf(servers_str,
2121 g_string_append(servers_str, val);
2123 dbus_message_iter_next(&str_array);
2125 } else if (g_str_equal(key, "Excludes") == TRUE) {
2126 DBusMessageIter str_array;
2128 if (type != DBUS_TYPE_ARRAY)
2131 excludes_str = g_string_new(NULL);
2132 if (excludes_str == NULL)
2135 dbus_message_iter_recurse(&variant, &str_array);
2137 while (dbus_message_iter_get_arg_type(&str_array) ==
2141 dbus_message_iter_get_basic(&str_array, &val);
2143 if (excludes_str->len > 0)
2144 g_string_append_printf(excludes_str,
2147 g_string_append(excludes_str, val);
2149 dbus_message_iter_next(&str_array);
2153 dbus_message_iter_next(&dict);
2157 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2159 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2160 if (servers_str == NULL && service->proxies == NULL)
2163 if (servers_str != NULL) {
2164 g_strfreev(service->proxies);
2166 if (servers_str->len > 0)
2167 service->proxies = g_strsplit_set(
2168 servers_str->str, " ", 0);
2170 service->proxies = NULL;
2173 if (excludes_str != NULL) {
2174 g_strfreev(service->excludes);
2176 if (excludes_str->len > 0)
2177 service->excludes = g_strsplit_set(
2178 excludes_str->str, " ", 0);
2180 service->excludes = NULL;
2183 if (service->proxies == NULL)
2184 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2187 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2188 g_free(service->pac);
2190 if (url != NULL && strlen(url) > 0)
2191 service->pac = g_strdup(url);
2193 service->pac = NULL;
2195 /* if we are connected:
2196 - if service->pac == NULL
2197 - if __connman_ipconfig_get_proxy_autoconfig(
2198 service->ipconfig) == NULL
2199 --> We should start WPAD */
2202 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2206 if (servers_str != NULL)
2207 g_string_free(servers_str, TRUE);
2209 if (excludes_str != NULL)
2210 g_string_free(excludes_str, TRUE);
2212 service->proxy_config = method;
2217 if (servers_str != NULL)
2218 g_string_free(servers_str, TRUE);
2220 if (excludes_str != NULL)
2221 g_string_free(excludes_str, TRUE);
2226 static int set_ipconfig(struct connman_service *service,
2227 struct connman_ipconfig *ipconfig,
2228 DBusMessageIter *array,
2229 enum connman_service_state state,
2230 enum connman_service_state *new_state)
2232 enum connman_ipconfig_method old_method;
2233 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2234 enum connman_ipconfig_type type;
2237 old_method = __connman_ipconfig_get_method(ipconfig);
2239 if (is_connecting_state(service, state) ||
2240 is_connected_state(service, state))
2241 __connman_network_clear_ipconfig(service->network, ipconfig);
2243 err = __connman_ipconfig_set_config(ipconfig, array);
2244 method = __connman_ipconfig_get_method(ipconfig);
2245 type = __connman_ipconfig_get_config_type(ipconfig);
2247 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2248 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2249 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2250 *new_state = service->state_ipv4 =
2251 CONNMAN_SERVICE_STATE_CONFIGURATION;
2252 __connman_ipconfig_enable(ipconfig);
2255 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2256 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2257 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2258 *new_state = service->state_ipv6;
2259 __connman_ipconfig_enable(ipconfig);
2263 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2264 type, method, state2string(*new_state));
2269 static DBusMessage *set_property(DBusConnection *conn,
2270 DBusMessage *msg, void *user_data)
2272 struct connman_service *service = user_data;
2273 DBusMessageIter iter, value;
2277 DBG("service %p", service);
2279 if (dbus_message_iter_init(msg, &iter) == FALSE)
2280 return __connman_error_invalid_arguments(msg);
2282 dbus_message_iter_get_basic(&iter, &name);
2283 dbus_message_iter_next(&iter);
2284 dbus_message_iter_recurse(&iter, &value);
2286 type = dbus_message_iter_get_arg_type(&value);
2288 if (g_str_equal(name, "AutoConnect") == TRUE) {
2289 connman_bool_t autoconnect;
2291 if (type != DBUS_TYPE_BOOLEAN)
2292 return __connman_error_invalid_arguments(msg);
2294 if (service->favorite == FALSE)
2295 return __connman_error_invalid_service(msg);
2297 dbus_message_iter_get_basic(&value, &autoconnect);
2299 if (service->autoconnect == autoconnect)
2300 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2302 service->autoconnect = autoconnect;
2304 autoconnect_changed(service);
2306 __connman_storage_save_service(service);
2307 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2308 const char *passphrase;
2310 if (type != DBUS_TYPE_STRING)
2311 return __connman_error_invalid_arguments(msg);
2313 if (service->immutable == TRUE)
2314 return __connman_error_not_supported(msg);
2316 dbus_message_iter_get_basic(&value, &passphrase);
2318 __connman_service_set_passphrase(service, passphrase);
2319 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2320 DBusMessageIter entry;
2325 if (type != DBUS_TYPE_ARRAY)
2326 return __connman_error_invalid_arguments(msg);
2328 str = g_string_new(NULL);
2330 return __connman_error_invalid_arguments(msg);
2332 index = connman_network_get_index(service->network);
2333 gw = __connman_ipconfig_get_gateway_from_index(index);
2335 if (gw && strlen(gw))
2336 __connman_service_nameserver_del_routes(service);
2338 dbus_message_iter_recurse(&value, &entry);
2340 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2342 dbus_message_iter_get_basic(&entry, &val);
2343 dbus_message_iter_next(&entry);
2345 g_string_append_printf(str, " %s", val);
2347 g_string_append(str, val);
2350 g_strfreev(service->nameservers_config);
2353 service->nameservers_config =
2354 g_strsplit_set(str->str, " ", 0);
2356 service->nameservers_config = NULL;
2359 g_string_free(str, TRUE);
2361 if (gw && strlen(gw))
2362 __connman_service_nameserver_add_routes(service, gw);
2364 update_nameservers(service);
2365 dns_configuration_changed(service);
2367 __connman_storage_save_service(service);
2368 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2369 DBusMessageIter entry;
2372 if (type != DBUS_TYPE_ARRAY)
2373 return __connman_error_invalid_arguments(msg);
2375 str = g_string_new(NULL);
2377 return __connman_error_invalid_arguments(msg);
2379 dbus_message_iter_recurse(&value, &entry);
2381 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2383 dbus_message_iter_get_basic(&entry, &val);
2384 dbus_message_iter_next(&entry);
2386 g_string_append_printf(str, " %s", val);
2388 g_string_append(str, val);
2391 g_strfreev(service->domains);
2394 service->domains = g_strsplit_set(str->str, " ", 0);
2396 service->domains = NULL;
2398 g_string_free(str, TRUE);
2400 update_nameservers(service);
2401 domain_configuration_changed(service);
2403 __connman_storage_save_service(service);
2404 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2407 if (type != DBUS_TYPE_ARRAY)
2408 return __connman_error_invalid_arguments(msg);
2410 err = update_proxy_configuration(service, &value);
2413 return __connman_error_failed(msg, -err);
2415 proxy_configuration_changed(service);
2417 __connman_storage_save_service(service);
2418 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2419 g_str_equal(name, "IPv6.Configuration")) {
2421 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2422 enum connman_service_state state =
2423 CONNMAN_SERVICE_STATE_UNKNOWN;
2428 if (service->ipconfig_ipv4 == NULL &&
2429 service->ipconfig_ipv6 == NULL)
2430 return __connman_error_invalid_property(msg);
2432 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2433 ipv4 = service->ipconfig_ipv4;
2434 err = set_ipconfig(service, ipv4, &value,
2435 service->state_ipv4, &state);
2437 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2438 ipv6 = service->ipconfig_ipv6;
2439 err = set_ipconfig(service, ipv6, &value,
2440 service->state_ipv6, &state);
2444 if (is_connected_state(service, state) ||
2445 is_connecting_state(service, state))
2446 __connman_network_set_ipconfig(service->network,
2448 return __connman_error_failed(msg, -err);
2452 ipv4_configuration_changed(service);
2454 ipv6_configuration_changed(service);
2456 if (is_connecting(service) || is_connected(service))
2457 __connman_network_set_ipconfig(service->network,
2460 __connman_storage_save_service(service);
2462 return __connman_error_invalid_property(msg);
2464 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2467 static void set_idle(struct connman_service *service)
2469 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
2470 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2471 state_changed(service);
2474 static DBusMessage *clear_property(DBusConnection *conn,
2475 DBusMessage *msg, void *user_data)
2477 struct connman_service *service = user_data;
2480 DBG("service %p", service);
2482 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2485 if (g_str_equal(name, "Error") == TRUE) {
2488 g_get_current_time(&service->modified);
2489 __connman_storage_save_service(service);
2490 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2491 if (service->immutable == TRUE)
2492 return __connman_error_not_supported(msg);
2494 g_free(service->passphrase);
2495 service->passphrase = NULL;
2497 passphrase_changed(service);
2499 __connman_storage_save_service(service);
2501 return __connman_error_invalid_property(msg);
2503 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2506 static connman_bool_t is_ignore(struct connman_service *service)
2508 if (service->autoconnect == FALSE)
2511 if (service->roaming == TRUE)
2514 if (service->ignore == TRUE)
2517 if (combine_state(service->state_ipv4, service->state_ipv6) ==
2518 CONNMAN_SERVICE_STATE_FAILURE)
2524 void __connman_service_auto_connect(void)
2526 struct connman_service *service = NULL;
2527 GSequenceIter *iter;
2531 if (__connman_session_mode() == TRUE) {
2532 DBG("Session mode enabled: auto connect disabled");
2536 iter = g_sequence_get_begin_iter(service_list);
2538 while (g_sequence_iter_is_end(iter) == FALSE) {
2539 service = g_sequence_get(iter);
2541 if (service->pending != NULL)
2544 if (is_connecting(service) == TRUE)
2547 if (service->favorite == FALSE)
2550 if (is_connected(service) == TRUE)
2553 if (is_ignore(service) == FALSE &&
2554 combine_state(service->state_ipv4,
2555 service->state_ipv6) ==
2556 CONNMAN_SERVICE_STATE_IDLE)
2561 iter = g_sequence_iter_next(iter);
2564 if (service != NULL) {
2565 service->userconnect = FALSE;
2566 __connman_service_connect(service);
2570 static void remove_timeout(struct connman_service *service)
2572 if (service->timeout > 0) {
2573 g_source_remove(service->timeout);
2574 service->timeout = 0;
2578 static void reply_pending(struct connman_service *service, int error)
2580 remove_timeout(service);
2582 if (service->pending != NULL) {
2586 reply = __connman_error_failed(service->pending,
2589 g_dbus_send_message(connection, reply);
2593 sender = dbus_message_get_interface(service->pending);
2595 DBG("sender %s", sender);
2597 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2598 g_dbus_send_reply(connection, service->pending,
2599 DBUS_TYPE_OBJECT_PATH, &service->path,
2602 g_dbus_send_reply(connection, service->pending,
2606 dbus_message_unref(service->pending);
2607 service->pending = NULL;
2611 static gboolean connect_timeout(gpointer user_data)
2613 struct connman_service *service = user_data;
2614 connman_bool_t autoconnect = FALSE;
2616 DBG("service %p", service);
2618 service->timeout = 0;
2620 if (service->network != NULL)
2621 __connman_network_disconnect(service->network);
2623 __connman_ipconfig_disable(service->ipconfig_ipv4);
2624 __connman_ipconfig_disable(service->ipconfig_ipv6);
2626 __connman_stats_service_unregister(service);
2628 if (service->pending != NULL) {
2631 reply = __connman_error_operation_timeout(service->pending);
2633 g_dbus_send_message(connection, reply);
2635 dbus_message_unref(service->pending);
2636 service->pending = NULL;
2640 __connman_service_indicate_state(service,
2641 CONNMAN_SERVICE_STATE_FAILURE,
2642 CONNMAN_IPCONFIG_TYPE_IPV4);
2643 __connman_service_indicate_state(service,
2644 CONNMAN_SERVICE_STATE_FAILURE,
2645 CONNMAN_IPCONFIG_TYPE_IPV6);
2647 if (autoconnect == TRUE && service->userconnect == FALSE)
2648 __connman_service_auto_connect();
2653 static void set_reconnect_state(struct connman_service *service,
2654 connman_bool_t reconnect)
2656 struct connman_device *device;
2658 if (service->network == NULL)
2661 device = connman_network_get_device(service->network);
2665 __connman_device_set_reconnect(device, reconnect);
2668 static connman_bool_t get_reconnect_state(struct connman_service *service)
2670 struct connman_device *device;
2672 if (service->network == NULL)
2675 device = connman_network_get_device(service->network);
2679 return __connman_device_get_reconnect(device);
2682 static void request_input_cb (struct connman_service *service,
2683 const char *identity, const char *passphrase,
2686 DBG ("RequestInput return, %p", service);
2688 if (identity == NULL && passphrase == NULL && service->wps == FALSE)
2691 if (identity != NULL)
2692 __connman_service_set_identity(service, identity);
2694 if (passphrase != NULL)
2695 __connman_service_set_passphrase(service, passphrase);
2697 __connman_service_connect(service);
2700 static DBusMessage *connect_service(DBusConnection *conn,
2701 DBusMessage *msg, void *user_data)
2703 struct connman_service *service = user_data;
2704 GSequenceIter *iter;
2707 DBG("service %p", service);
2709 if (service->pending != NULL)
2710 return __connman_error_in_progress(msg);
2712 iter = g_sequence_get_begin_iter(service_list);
2714 while (g_sequence_iter_is_end(iter) == FALSE) {
2715 struct connman_service *temp = g_sequence_get(iter);
2717 if (service->type == temp->type && is_connecting(temp) == TRUE)
2718 return __connman_error_in_progress(msg);
2720 iter = g_sequence_iter_next(iter);
2723 service->ignore = FALSE;
2725 service->userconnect = TRUE;
2727 service->pending = dbus_message_ref(msg);
2729 set_reconnect_state(service, FALSE);
2731 err = __connman_service_connect(service);
2733 if (err == -ENOKEY) {
2734 if (__connman_agent_request_input(service,
2740 if (service->pending == NULL)
2743 if (err != -EINPROGRESS) {
2744 dbus_message_unref(service->pending);
2745 service->pending = NULL;
2747 return __connman_error_failed(msg, -err);
2753 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2756 static DBusMessage *disconnect_service(DBusConnection *conn,
2757 DBusMessage *msg, void *user_data)
2759 struct connman_service *service = user_data;
2762 DBG("service %p", service);
2764 reply_pending(service, ECONNABORTED);
2766 service->ignore = TRUE;
2768 set_reconnect_state(service, FALSE);
2770 err = __connman_service_disconnect(service);
2772 if (err != -EINPROGRESS)
2773 return __connman_error_failed(msg, -err);
2778 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2781 static DBusMessage *remove_service(DBusConnection *conn,
2782 DBusMessage *msg, void *user_data)
2784 struct connman_service *service = user_data;
2786 DBG("service %p", service);
2788 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2789 return __connman_error_not_supported(msg);
2791 if (service->immutable == TRUE)
2792 return __connman_error_not_supported(msg);
2794 if (service->favorite == FALSE &&
2795 combine_state(service->state_ipv4, service->state_ipv6) !=
2796 CONNMAN_SERVICE_STATE_FAILURE)
2797 return __connman_error_not_supported(msg);
2799 if (service->network != NULL) {
2800 set_reconnect_state(service, FALSE);
2802 __connman_network_disconnect(service->network);
2805 g_free(service->passphrase);
2806 service->passphrase = NULL;
2808 passphrase_changed(service);
2812 __connman_service_set_favorite(service, FALSE);
2813 __connman_storage_save_service(service);
2815 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2818 static DBusMessage *move_service(DBusConnection *conn,
2819 DBusMessage *msg, void *user_data,
2822 struct connman_service *service = user_data;
2823 struct connman_service *target;
2825 GSequenceIter *src, *dst;
2827 DBG("service %p", service);
2829 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2832 if (service->favorite == FALSE)
2833 return __connman_error_not_supported(msg);
2835 target = find_service(path);
2836 if (target == NULL || target->favorite == FALSE || target == service)
2837 return __connman_error_invalid_service(msg);
2839 DBG("target %s", target->identifier);
2841 if (target->state_ipv4 != service->state_ipv4 &&
2842 target->state_ipv6 != service->state_ipv6)
2843 return __connman_error_invalid_service(msg);
2845 g_get_current_time(&service->modified);
2846 __connman_storage_save_service(service);
2848 src = g_hash_table_lookup(service_hash, service->identifier);
2849 dst = g_hash_table_lookup(service_hash, target->identifier);
2851 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2853 __connman_profile_changed(FALSE);
2855 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2858 static DBusMessage *move_before(DBusConnection *conn,
2859 DBusMessage *msg, void *user_data)
2861 return move_service(conn, msg, user_data, TRUE);
2864 static DBusMessage *move_after(DBusConnection *conn,
2865 DBusMessage *msg, void *user_data)
2867 return move_service(conn, msg, user_data, FALSE);
2870 static DBusMessage *reset_counters(DBusConnection *conn,
2871 DBusMessage *msg, void *user_data)
2873 struct connman_service *service = user_data;
2875 reset_stats(service);
2877 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2880 static GDBusMethodTable service_methods[] = {
2881 { "GetProperties", "", "a{sv}", get_properties },
2882 { "SetProperty", "sv", "", set_property },
2883 { "ClearProperty", "s", "", clear_property },
2884 { "Connect", "", "", connect_service,
2885 G_DBUS_METHOD_FLAG_ASYNC },
2886 { "Disconnect", "", "", disconnect_service },
2887 { "Remove", "", "", remove_service },
2888 { "MoveBefore", "o", "", move_before },
2889 { "MoveAfter", "o", "", move_after },
2890 { "ResetCounters", "", "", reset_counters },
2894 static GDBusSignalTable service_signals[] = {
2895 { "PropertyChanged", "sv" },
2899 static void service_free(gpointer user_data)
2901 struct connman_service *service = user_data;
2902 char *path = service->path;
2904 DBG("service %p", service);
2906 reply_pending(service, ENOENT);
2908 g_hash_table_remove(service_hash, service->identifier);
2910 __connman_notifier_service_remove(service);
2912 stats_stop(service);
2913 __connman_storage_save_service(service);
2915 service->path = NULL;
2918 __connman_profile_changed(FALSE);
2920 g_dbus_unregister_interface(connection, path,
2921 CONNMAN_SERVICE_INTERFACE);
2925 g_hash_table_destroy(service->counter_table);
2927 if (service->network != NULL) {
2928 if (service->network_created == TRUE)
2929 connman_network_unregister(service->network);
2930 connman_network_unref(service->network);
2933 if (service->provider != NULL)
2934 connman_provider_unref(service->provider);
2936 if (service->ipconfig_ipv4 != NULL) {
2937 connman_ipconfig_unref(service->ipconfig_ipv4);
2938 service->ipconfig_ipv4 = NULL;
2941 if (service->ipconfig_ipv6 != NULL) {
2942 connman_ipconfig_unref(service->ipconfig_ipv6);
2943 service->ipconfig_ipv6 = NULL;
2946 if (service->location != NULL)
2947 connman_location_unref(service->location);
2949 g_strfreev(service->nameservers);
2950 g_strfreev(service->nameservers_config);
2951 g_strfreev(service->domains);
2952 g_strfreev(service->proxies);
2953 g_strfreev(service->excludes);
2955 g_free(service->domainname);
2956 g_free(service->pac);
2957 g_free(service->profile);
2958 g_free(service->name);
2959 g_free(service->passphrase);
2960 g_free(service->identifier);
2961 g_free(service->eap);
2962 g_free(service->identity);
2963 g_free(service->ca_cert_file);
2964 g_free(service->client_cert_file);
2965 g_free(service->private_key_file);
2966 g_free(service->private_key_passphrase);
2967 g_free(service->phase2);
2969 if (service->stats.timer != NULL)
2970 g_timer_destroy(service->stats.timer);
2971 if (service->stats_roaming.timer != NULL)
2972 g_timer_destroy(service->stats_roaming.timer);
2978 * __connman_service_put:
2979 * @service: service structure
2981 * Release service if no longer needed
2983 void __connman_service_put(struct connman_service *service)
2985 DBG("service %p", service);
2987 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2988 GSequenceIter *iter;
2990 iter = g_hash_table_lookup(service_hash, service->identifier);
2992 reply_pending(service, ECONNABORTED);
2994 __connman_service_disconnect(service);
2996 g_sequence_remove(iter);
2998 service_free(service);
3002 static void stats_init(struct connman_service *service)
3005 service->stats.valid = FALSE;
3006 service->stats.enabled = FALSE;
3007 service->stats.timer = g_timer_new();
3010 service->stats_roaming.valid = FALSE;
3011 service->stats_roaming.enabled = FALSE;
3012 service->stats_roaming.timer = g_timer_new();
3015 static void service_initialize(struct connman_service *service)
3017 DBG("service %p", service);
3019 service->refcount = 1;
3020 service->session_usage_count = 0;
3022 service->network_created = FALSE;
3024 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3025 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3027 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3028 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3030 service->favorite = FALSE;
3031 service->immutable = FALSE;
3032 service->hidden = FALSE;
3034 service->ignore = FALSE;
3036 service->userconnect = FALSE;
3040 stats_init(service);
3042 service->provider = NULL;
3044 service->wps = FALSE;
3048 * connman_service_create:
3050 * Allocate a new service.
3052 * Returns: a newly-allocated #connman_service structure
3054 struct connman_service *connman_service_create(void)
3057 struct connman_stats_counter *counters;
3058 const char *counter;
3060 struct connman_service *service;
3062 service = g_try_new0(struct connman_service, 1);
3063 if (service == NULL)
3066 DBG("service %p", service);
3068 service->counter_table = g_hash_table_new_full(g_str_hash,
3069 g_str_equal, NULL, g_free);
3071 for (list = counter_list; list; list = list->next) {
3072 counter = list->data;
3074 counters = g_try_new0(struct connman_stats_counter, 1);
3075 if (counters == NULL) {
3076 g_hash_table_destroy(service->counter_table);
3081 counters->append_all = TRUE;
3083 g_hash_table_replace(service->counter_table, (gpointer)counter,
3087 service_initialize(service);
3089 service->location = __connman_location_create(service);
3094 struct connman_location *__connman_service_get_location(struct connman_service *service)
3096 return service->location;
3100 * connman_service_ref:
3101 * @service: service structure
3103 * Increase reference counter of service
3105 struct connman_service *connman_service_ref(struct connman_service *service)
3109 g_atomic_int_inc(&service->refcount);
3115 * connman_service_unref:
3116 * @service: service structure
3118 * Decrease reference counter of service
3120 void connman_service_unref(struct connman_service *service)
3122 __connman_service_put(service);
3125 static gint service_compare(gconstpointer a, gconstpointer b,
3128 struct connman_service *service_a = (void *) a;
3129 struct connman_service *service_b = (void *) b;
3130 enum connman_service_state state_a, state_b;
3132 state_a = combine_state(service_a->state_ipv4, service_a->state_ipv6);
3133 state_b = combine_state(service_b->state_ipv4, service_b->state_ipv6);
3135 if (state_a != state_b) {
3136 if (is_connected(service_a) == TRUE)
3138 if (is_connected(service_b) == TRUE)
3141 if (is_connecting(service_a) == TRUE)
3143 if (is_connecting(service_b) == TRUE)
3147 if (service_a->order > service_b->order)
3150 if (service_a->order < service_b->order)
3153 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3156 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3159 if (service_a->type != service_b->type) {
3160 switch (service_a->type) {
3161 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3162 case CONNMAN_SERVICE_TYPE_SYSTEM:
3163 case CONNMAN_SERVICE_TYPE_ETHERNET:
3164 case CONNMAN_SERVICE_TYPE_GPS:
3165 case CONNMAN_SERVICE_TYPE_VPN:
3166 case CONNMAN_SERVICE_TYPE_GADGET:
3168 case CONNMAN_SERVICE_TYPE_WIFI:
3170 case CONNMAN_SERVICE_TYPE_WIMAX:
3171 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3172 case CONNMAN_SERVICE_TYPE_CELLULAR:
3177 return (gint) service_b->strength - (gint) service_a->strength;
3181 * connman_service_get_type:
3182 * @service: service structure
3184 * Get the type of service
3186 enum connman_service_type connman_service_get_type(struct connman_service *service)
3188 if (service == NULL)
3189 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3191 return service->type;
3195 * connman_service_get_interface:
3196 * @service: service structure
3198 * Get network interface of service
3200 char *connman_service_get_interface(struct connman_service *service)
3204 if (service == NULL)
3207 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3208 if (service->ipconfig_ipv4)
3209 index = connman_ipconfig_get_index(
3210 service->ipconfig_ipv4);
3211 else if (service->ipconfig_ipv6)
3212 index = connman_ipconfig_get_index(
3213 service->ipconfig_ipv6);
3217 return connman_inet_ifname(index);
3220 if (service->network == NULL)
3223 index = connman_network_get_index(service->network);
3225 return connman_inet_ifname(index);
3229 * connman_service_get_network:
3230 * @service: service structure
3232 * Get the service network
3234 struct connman_network *
3235 __connman_service_get_network(struct connman_service *service)
3237 if (service == NULL)
3240 return service->network;
3243 struct connman_ipconfig *
3244 __connman_service_get_ip4config(struct connman_service *service)
3246 if (service == NULL)
3249 return service->ipconfig_ipv4;
3252 struct connman_ipconfig *
3253 __connman_service_get_ip6config(struct connman_service *service)
3255 if (service == NULL)
3258 return service->ipconfig_ipv6;
3261 struct connman_ipconfig *
3262 __connman_service_get_ipconfig(struct connman_service *service, int family)
3264 if (family == AF_INET)
3265 return __connman_service_get_ip4config(service);
3266 else if (family == AF_INET6)
3267 return __connman_service_get_ip6config(service);
3273 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3275 if (service == NULL)
3276 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3278 return service->security;
3281 const char *__connman_service_get_phase2(struct connman_service *service)
3283 if (service == NULL)
3286 return service->phase2;
3289 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3291 if (service == NULL)
3294 return service->wps;
3298 * __connman_service_set_favorite:
3299 * @service: service structure
3300 * @favorite: favorite value
3302 * Change the favorite setting of service
3304 int __connman_service_set_favorite(struct connman_service *service,
3305 connman_bool_t favorite)
3307 GSequenceIter *iter;
3309 iter = g_hash_table_lookup(service_hash, service->identifier);
3313 if (service->favorite == favorite)
3316 service->favorite = favorite;
3318 favorite_changed(service);
3320 g_sequence_sort_changed(iter, service_compare, NULL);
3322 __connman_profile_changed(FALSE);
3327 int __connman_service_set_immutable(struct connman_service *service,
3328 connman_bool_t immutable)
3330 service->immutable = immutable;
3332 immutable_changed(service);
3337 void __connman_service_set_string(struct connman_service *service,
3338 const char *key, const char *value)
3340 if (g_str_equal(key, "EAP") == TRUE) {
3341 g_free(service->eap);
3342 service->eap = g_strdup(value);
3343 } else if (g_str_equal(key, "Identity") == TRUE) {
3344 g_free(service->identity);
3345 service->identity = g_strdup(value);
3346 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3347 g_free(service->ca_cert_file);
3348 service->ca_cert_file = g_strdup(value);
3349 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3350 g_free(service->client_cert_file);
3351 service->client_cert_file = g_strdup(value);
3352 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3353 g_free(service->private_key_file);
3354 service->private_key_file = g_strdup(value);
3355 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3356 g_free(service->private_key_passphrase);
3357 service->private_key_passphrase = g_strdup(value);
3358 } else if (g_str_equal(key, "Phase2") == TRUE) {
3359 g_free(service->phase2);
3360 service->phase2 = g_strdup(value);
3361 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3362 g_free(service->passphrase);
3363 service->passphrase = g_strdup(value);
3367 static void service_complete(struct connman_service *service)
3369 reply_pending(service, EIO);
3371 if (service->userconnect == FALSE)
3372 __connman_service_auto_connect();
3374 g_get_current_time(&service->modified);
3375 __connman_storage_save_service(service);
3378 static void report_error_cb(struct connman_service *service,
3379 gboolean retry, void *user_data)
3382 __connman_service_connect(service);
3384 service_complete(service);
3385 __connman_profile_changed(FALSE);
3386 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3390 int __connman_service_set_ipconfig_ready(struct connman_service *service,
3391 enum connman_ipconfig_type type)
3393 enum connman_service_state state;
3396 DBG("service %p (%s) type %d (%s)",
3397 service, service ? service->identifier : NULL,
3398 type, __connman_ipconfig_type2string(type));
3400 if (service == NULL)
3403 state = combine_state(service->state_ipv4, service->state_ipv6);
3405 if (state == CONNMAN_SERVICE_STATE_READY ||
3406 state == CONNMAN_SERVICE_STATE_ONLINE) {
3409 err = __connman_service_indicate_state(service,
3410 CONNMAN_SERVICE_STATE_READY, type);
3416 int __connman_service_indicate_state(struct connman_service *service,
3417 enum connman_service_state new_state,
3418 enum connman_ipconfig_type type)
3420 enum connman_service_state service_state,
3421 state = CONNMAN_SERVICE_STATE_IDLE;
3422 GSequenceIter *iter;
3424 if (service == NULL)
3427 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3428 if (service->state_ipv4 == new_state)
3430 state = combine_state(new_state, service->state_ipv6);
3431 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3432 if (service->state_ipv6 == new_state)
3434 state = combine_state(new_state, service->state_ipv4);
3438 service_state = combine_state(service->state_ipv4,
3439 service->state_ipv6);
3441 DBG("service %p state %s/%s => %s new %s/%d => %s",
3443 state2string(service->state_ipv4),
3444 state2string(service->state_ipv6),
3445 state2string(service_state),
3446 state2string(new_state),
3448 state2string(state));
3450 if (service_state == CONNMAN_SERVICE_STATE_FAILURE &&
3451 state == CONNMAN_SERVICE_STATE_IDLE)
3454 if (service_state == CONNMAN_SERVICE_STATE_IDLE &&
3455 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3458 if (state == CONNMAN_SERVICE_STATE_IDLE &&
3459 service_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3460 state_changed(service);
3462 reply_pending(service, ECONNABORTED);
3464 __connman_service_disconnect(service);
3467 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3468 if (__connman_stats_service_register(service) == 0) {
3469 __connman_stats_get(service, FALSE,
3470 &service->stats.data);
3471 __connman_stats_get(service, TRUE,
3472 &service->stats_roaming.data);
3475 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
3476 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3477 __connman_ipconfig_enable(service->ipconfig_ipv4);
3478 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
3479 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3480 __connman_ipconfig_enable(service->ipconfig_ipv6);
3483 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3484 service->state_ipv4 = new_state;
3485 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3486 service->state_ipv6 = new_state;
3488 if (state == service_state)
3491 state_changed(service);
3493 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3494 if (service->login_required == TRUE) {
3495 service->login_required = FALSE;
3496 login_changed(service);
3499 connman_timeserver_sync();
3502 if (state == CONNMAN_SERVICE_STATE_IDLE) {
3503 connman_bool_t reconnect;
3505 reconnect = get_reconnect_state(service);
3506 if (reconnect == TRUE)
3507 __connman_service_auto_connect();
3510 if (state == CONNMAN_SERVICE_STATE_READY) {
3511 enum connman_service_proxy_method proxy_config;
3512 enum connman_ipconfig_method method;
3514 set_reconnect_state(service, TRUE);
3516 __connman_service_set_favorite(service, TRUE);
3518 reply_pending(service, 0);
3520 service->userconnect = FALSE;
3522 g_get_current_time(&service->modified);
3523 __connman_storage_save_service(service);
3525 update_nameservers(service);
3526 dns_changed(service);
3527 domain_changed(service);
3529 proxy_config = service->proxy_config;
3532 * We start WPAD if we haven't got a PAC URL from DHCP and
3533 * if our proxy manual configuration is either empty or set
3534 * to AUTO with an empty URL.
3536 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3537 (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3539 CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3540 service->pac == NULL)))
3541 if (__connman_wpad_start(service) < 0) {
3543 CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3545 __connman_notifier_proxy_changed(service);
3548 __connman_notifier_connect(service->type);
3550 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3551 connman_network_get_bool(service->network,
3552 "WiFi.UseWPS") == TRUE) {
3555 pass = connman_network_get_string(service->network,
3558 __connman_service_set_passphrase(service, pass);
3560 connman_network_set_bool(service->network,
3561 "WiFi.UseWPS", FALSE);
3566 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3567 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3568 __connman_ipconfig_disable_ipv6(
3569 service->ipconfig_ipv6);
3571 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3572 __connman_location_finish(service);
3576 __connman_wpad_stop(service);
3578 update_nameservers(service);
3579 dns_changed(service);
3580 domain_changed(service);
3582 __connman_notifier_disconnect(service->type);
3585 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3586 if (service->userconnect == TRUE &&
3587 __connman_agent_report_error(service,
3588 error2string(service->error),
3589 report_error_cb, NULL) == -EIO)
3591 service_complete(service);
3593 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3595 iter = g_hash_table_lookup(service_hash, service->identifier);
3597 g_sequence_sort_changed(iter, service_compare, NULL);
3599 __connman_profile_changed(FALSE);
3601 service_state = combine_state(service->state_ipv4,
3602 service->state_ipv6);
3603 if (service_state == CONNMAN_SERVICE_STATE_ONLINE)
3606 if (service_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3607 struct connman_service *def_service = get_default();
3609 if (__connman_notifier_count_connected() == 0 &&
3610 def_service != NULL &&
3611 def_service->provider != NULL)
3612 __connman_provider_disconnect(def_service->provider);
3615 if (service_state == CONNMAN_SERVICE_STATE_IDLE ||
3616 service_state == CONNMAN_SERVICE_STATE_FAILURE)
3617 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3622 int __connman_service_indicate_error(struct connman_service *service,
3623 enum connman_service_error error)
3625 DBG("service %p error %d", service, error);
3627 if (service == NULL)
3630 service->error = error;
3632 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3633 __connman_service_set_passphrase(service, NULL);
3635 return __connman_service_indicate_state(service,
3636 CONNMAN_SERVICE_STATE_FAILURE,
3637 CONNMAN_IPCONFIG_TYPE_IPV4);
3640 int __connman_service_clear_error(struct connman_service *service)
3642 enum connman_service_state state;
3644 DBG("service %p", service);
3646 if (service == NULL)
3649 state = combine_state(service->state_ipv4, service->state_ipv6);
3651 if (state != CONNMAN_SERVICE_STATE_FAILURE)
3654 service->state_ipv4 = service->state_ipv6 =
3655 CONNMAN_SERVICE_STATE_UNKNOWN;
3656 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3658 if (service->favorite == TRUE)
3659 set_reconnect_state(service, TRUE);
3661 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
3662 CONNMAN_IPCONFIG_TYPE_IPV6);
3665 * Toggling the IPv6 state to IDLE could trigger the auto connect
3666 * machinery and consequently the IPv4 state.
3668 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3669 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3672 return __connman_service_indicate_state(service,
3673 CONNMAN_SERVICE_STATE_IDLE,
3674 CONNMAN_IPCONFIG_TYPE_IPV4);
3677 int __connman_service_indicate_default(struct connman_service *service)
3679 DBG("service %p", service);
3683 __connman_location_detect(service);
3688 int __connman_service_request_login(struct connman_service *service)
3690 DBG("service %p", service);
3692 if (service == NULL)
3695 service->login_required = TRUE;
3696 login_changed(service);
3701 static connman_bool_t prepare_network(struct connman_service *service)
3703 enum connman_network_type type;
3704 unsigned int ssid_len;
3706 type = connman_network_get_type(service->network);
3709 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3710 case CONNMAN_NETWORK_TYPE_VENDOR:
3712 case CONNMAN_NETWORK_TYPE_WIFI:
3713 if (connman_network_get_blob(service->network, "WiFi.SSID",
3717 if (service->passphrase != NULL)
3718 connman_network_set_string(service->network,
3719 "WiFi.Passphrase", service->passphrase);
3721 case CONNMAN_NETWORK_TYPE_ETHERNET:
3722 case CONNMAN_NETWORK_TYPE_WIMAX:
3723 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3724 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3725 case CONNMAN_NETWORK_TYPE_CELLULAR:
3732 static void prepare_8021x(struct connman_service *service)
3734 if (service->eap != NULL)
3735 connman_network_set_string(service->network, "WiFi.EAP",
3738 if (service->identity != NULL)
3739 connman_network_set_string(service->network, "WiFi.Identity",
3742 if (service->ca_cert_file != NULL)
3743 connman_network_set_string(service->network, "WiFi.CACertFile",
3744 service->ca_cert_file);
3746 if (service->client_cert_file != NULL)
3747 connman_network_set_string(service->network,
3748 "WiFi.ClientCertFile",
3749 service->client_cert_file);
3751 if (service->private_key_file != NULL)
3752 connman_network_set_string(service->network,
3753 "WiFi.PrivateKeyFile",
3754 service->private_key_file);
3756 if (service->private_key_passphrase != NULL)
3757 connman_network_set_string(service->network,
3758 "WiFi.PrivateKeyPassphrase",
3759 service->private_key_passphrase);
3761 if (service->phase2 != NULL)
3762 connman_network_set_string(service->network, "WiFi.Phase2",
3766 static int service_connect(struct connman_service *service)
3770 switch (service->type) {
3771 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3772 case CONNMAN_SERVICE_TYPE_SYSTEM:
3773 case CONNMAN_SERVICE_TYPE_GPS:
3774 case CONNMAN_SERVICE_TYPE_GADGET:
3776 case CONNMAN_SERVICE_TYPE_ETHERNET:
3777 case CONNMAN_SERVICE_TYPE_WIMAX:
3778 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3779 case CONNMAN_SERVICE_TYPE_CELLULAR:
3780 case CONNMAN_SERVICE_TYPE_VPN:
3782 case CONNMAN_SERVICE_TYPE_WIFI:
3783 switch (service->security) {
3784 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3785 case CONNMAN_SERVICE_SECURITY_NONE:
3787 case CONNMAN_SERVICE_SECURITY_WEP:
3788 case CONNMAN_SERVICE_SECURITY_PSK:
3789 case CONNMAN_SERVICE_SECURITY_WPA:
3790 case CONNMAN_SERVICE_SECURITY_RSN:
3791 if (service->passphrase == NULL) {
3792 if (service->network == NULL)
3795 if (service->wps == FALSE ||
3796 connman_network_get_bool(
3798 "WiFi.UseWPS") == FALSE)
3802 case CONNMAN_SERVICE_SECURITY_8021X:
3803 if (service->eap == NULL)
3807 * never request credentials if using EAP-TLS
3808 * (EAP-TLS networks need to be fully provisioned)
3810 if (g_str_equal(service->eap, "tls") == TRUE)
3813 if (service->immutable != TRUE)
3821 if (service->network != NULL) {
3822 if (prepare_network(service) == FALSE)
3825 switch (service->security) {
3826 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3827 case CONNMAN_SERVICE_SECURITY_NONE:
3828 case CONNMAN_SERVICE_SECURITY_WEP:
3829 case CONNMAN_SERVICE_SECURITY_PSK:
3830 case CONNMAN_SERVICE_SECURITY_WPA:
3831 case CONNMAN_SERVICE_SECURITY_RSN:
3833 case CONNMAN_SERVICE_SECURITY_8021X:
3834 prepare_8021x(service);
3838 if (__connman_stats_service_register(service) == 0) {
3839 __connman_stats_get(service, FALSE,
3840 &service->stats.data);
3841 __connman_stats_get(service, TRUE,
3842 &service->stats_roaming.data);
3845 if (service->ipconfig_ipv4)
3846 __connman_ipconfig_enable(service->ipconfig_ipv4);
3847 if (service->ipconfig_ipv6)
3848 __connman_ipconfig_enable(service->ipconfig_ipv6);
3850 err = __connman_network_connect(service->network);
3851 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3852 service->provider != NULL)
3853 err = __connman_provider_connect(service->provider);
3858 if (err != -EINPROGRESS) {
3859 __connman_ipconfig_disable(service->ipconfig_ipv4);
3860 __connman_ipconfig_disable(service->ipconfig_ipv6);
3861 __connman_stats_service_unregister(service);
3869 int __connman_service_connect(struct connman_service *service)
3871 enum connman_service_state state;
3874 state = combine_state(service->state_ipv4, service->state_ipv6);
3876 DBG("service %p state %s", service, state2string(state));
3878 if (is_connected(service) == TRUE)
3881 if (is_connecting(service) == TRUE)
3884 switch (service->type) {
3885 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3886 case CONNMAN_SERVICE_TYPE_SYSTEM:
3887 case CONNMAN_SERVICE_TYPE_GPS:
3888 case CONNMAN_SERVICE_TYPE_GADGET:
3891 err = service_connect(service);
3897 if (err == -EINPROGRESS) {
3898 if (service->timeout == 0)
3899 service->timeout = g_timeout_add_seconds(
3900 CONNECT_TIMEOUT, connect_timeout, service);
3902 return -EINPROGRESS;
3908 if (service->userconnect == TRUE)
3909 reply_pending(service, err);
3911 __connman_service_indicate_state(service,
3912 CONNMAN_SERVICE_STATE_FAILURE,
3913 CONNMAN_IPCONFIG_TYPE_IPV4);
3914 __connman_service_indicate_state(service,
3915 CONNMAN_SERVICE_STATE_FAILURE,
3916 CONNMAN_IPCONFIG_TYPE_IPV6);
3921 int __connman_service_disconnect(struct connman_service *service)
3925 DBG("service %p", service);
3927 if (service->network != NULL) {
3928 err = __connman_network_disconnect(service->network);
3929 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3930 service->provider != NULL)
3931 err = __connman_provider_disconnect(service->provider);
3935 if (err < 0 && err != -EINPROGRESS)
3938 __connman_6to4_remove(service->ipconfig_ipv4);
3940 if (service->ipconfig_ipv4)
3941 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3944 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3947 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3948 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3950 __connman_ipconfig_disable(service->ipconfig_ipv4);
3951 __connman_ipconfig_disable(service->ipconfig_ipv6);
3953 __connman_stats_service_unregister(service);
3958 int __connman_service_disconnect_all(void)
3960 GSequenceIter *iter;
3964 iter = g_sequence_get_begin_iter(service_list);
3966 while (g_sequence_iter_is_end(iter) == FALSE) {
3967 struct connman_service *service = g_sequence_get(iter);
3969 service->ignore = TRUE;
3971 set_reconnect_state(service, FALSE);
3973 __connman_service_disconnect(service);
3975 iter = g_sequence_iter_next(iter);
3983 * __connman_service_lookup:
3984 * @pattern: search pattern
3985 * @path: return object path
3987 * Look up a service path from a search pattern
3989 int __connman_service_lookup(const char *pattern, const char **path)
3991 GHashTableIter iter;
3992 gpointer key, value;
3994 g_hash_table_iter_init(&iter, service_hash);
3996 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
3997 GSequenceIter *iter = value;
3998 struct connman_service *service = g_sequence_get(iter);
4000 if (g_strcmp0(service->identifier, pattern) == 0 ||
4001 g_strcmp0(service->name, pattern) == 0) {
4002 *path = (const char *) service->path;
4011 * lookup_by_identifier:
4012 * @identifier: service identifier
4014 * Look up a service by identifier (reference count will not be increased)
4016 static struct connman_service *lookup_by_identifier(const char *identifier)
4018 GSequenceIter *iter;
4020 iter = g_hash_table_lookup(service_hash, identifier);
4022 return g_sequence_get(iter);
4027 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4028 const char *ssid, const char *mode, const char *security)
4030 struct connman_network *network;
4033 unsigned int i, ssid_len;
4035 ssid_len = strlen(ssid);
4039 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
4040 if (network == NULL)
4043 connman_network_register(network);
4045 connman_network_set_blob(network, "WiFi.SSID",
4046 (unsigned char *) ssid, ssid_len);
4048 connman_network_set_string(network, "WiFi.Mode", mode);
4049 connman_network_set_string(network, "WiFi.Security", security);
4051 name = g_try_malloc0(ssid_len + 1);
4053 connman_network_unregister(network);
4054 connman_network_unref(network);
4058 for (i = 0; i < ssid_len; i++) {
4059 if (g_ascii_isprint(ssid[i]))
4065 connman_network_set_name(network, name);
4069 index = connman_device_get_index(device);
4070 connman_network_set_index(network, index);
4072 if (connman_device_add_network(device, network) < 0) {
4073 connman_network_unregister(network);
4074 connman_network_unref(network);
4078 connman_network_set_available(network, TRUE);
4083 int __connman_service_create_and_connect(DBusMessage *msg)
4085 struct connman_service *service;
4086 struct connman_network *network;
4087 struct connman_device *device;
4088 DBusMessageIter iter, array;
4089 const char *mode = "managed", *security = "none", *group_security;
4090 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4091 unsigned int ssid_len = 0;
4096 dbus_message_iter_init(msg, &iter);
4097 dbus_message_iter_recurse(&iter, &array);
4099 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4100 DBusMessageIter entry, value;
4103 dbus_message_iter_recurse(&array, &entry);
4104 dbus_message_iter_get_basic(&entry, &key);
4106 dbus_message_iter_next(&entry);
4107 dbus_message_iter_recurse(&entry, &value);
4109 switch (dbus_message_iter_get_arg_type(&value)) {
4110 case DBUS_TYPE_STRING:
4111 if (g_str_equal(key, "Type") == TRUE)
4112 dbus_message_iter_get_basic(&value, &type);
4113 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4114 g_str_equal(key, "Mode") == TRUE)
4115 dbus_message_iter_get_basic(&value, &mode);
4116 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4117 g_str_equal(key, "Security") == TRUE)
4118 dbus_message_iter_get_basic(&value, &security);
4119 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4120 g_str_equal(key, "Passphrase") == TRUE)
4121 dbus_message_iter_get_basic(&value, &passphrase);
4122 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4123 g_str_equal(key, "SSID") == TRUE)
4124 dbus_message_iter_get_basic(&value, &ssid);
4127 dbus_message_iter_next(&array);
4133 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4139 ssid_len = strlen(ssid);
4143 if (g_strcmp0(security, "none") != 0 &&
4144 g_strcmp0(security, "wep") != 0 &&
4145 g_strcmp0(security, "psk") != 0 &&
4146 g_strcmp0(security, "wpa") != 0 &&
4147 g_strcmp0(security, "rsn") != 0 &&
4148 g_strcmp0(security, "ieee8021x") != 0)
4151 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4155 ident = connman_device_get_ident(device);
4160 if (!g_strcmp0(security, "wpa") ||
4161 !g_strcmp0(security, "rsn"))
4162 group_security = "psk";
4164 group_security = security;
4166 group = connman_wifi_build_group_name((unsigned char *) ssid,
4167 ssid_len, mode, group_security);
4171 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4173 service = lookup_by_identifier(name);
4175 if (service != NULL)
4178 network = create_hidden_wifi(device, ssid, mode, security);
4179 if (network != NULL) {
4180 connman_network_set_group(network, group);
4181 service->network_created = TRUE;
4184 service = lookup_by_identifier(name);
4190 if (service == NULL) {
4195 if (is_connected(service) == TRUE) {
4200 if (is_connecting(service) == TRUE) {
4205 set_reconnect_state(service, FALSE);
4207 __connman_device_disconnect(device);
4209 if (passphrase != NULL) {
4210 g_free(service->passphrase);
4211 service->passphrase = g_strdup(passphrase);
4214 service->userconnect = TRUE;
4216 err = __connman_service_connect(service);
4217 if (err < 0 && err != -EINPROGRESS)
4220 service->pending = dbus_message_ref(msg);
4225 if (service != NULL && service->network_created == TRUE) {
4226 struct connman_network *network = service->network;
4228 if (network != NULL) {
4229 connman_network_set_available(network, FALSE);
4230 __connman_device_cleanup_networks(device);
4232 __connman_service_put(service);
4238 static void provision_changed(gpointer value, gpointer user_data)
4240 struct connman_service *service = value;
4241 char *path = user_data;
4243 __connman_config_provision_service_ident(service, path);
4246 void __connman_service_provision_changed(const char *ident)
4248 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4251 int __connman_service_provision(DBusMessage *msg)
4253 GKeyFile *keyfile = NULL;
4254 const char *config_str = NULL;
4255 char *group = NULL, *ident = NULL;
4257 struct connman_service *service;
4261 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4264 if (config_str == NULL || strlen(config_str) == 0)
4267 keyfile = g_key_file_new();
4269 /* populate GKeyFile with config_str */
4270 if (g_key_file_load_from_data(keyfile, config_str,
4271 strlen(config_str), 0, NULL) == FALSE) {
4277 * read only one group of settings (only one service supported, no
4280 group = g_key_file_get_start_group(keyfile);
4282 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4287 err = __connman_config_load_service(keyfile, group, TRUE);
4291 ident = group + strlen("service_");
4293 /* trigger service provisioning if service exists */
4294 service = lookup_by_identifier(ident);
4295 if (service != NULL)
4296 __connman_config_provision_service(service);
4298 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4304 if (keyfile != NULL)
4305 g_key_file_free(keyfile);
4311 * __connman_service_get:
4312 * @identifier: service identifier
4314 * Look up a service by identifier or create a new one if not found
4316 static struct connman_service *service_get(const char *identifier)
4318 struct connman_service *service;
4319 GSequenceIter *iter;
4321 iter = g_hash_table_lookup(service_hash, identifier);
4323 service = g_sequence_get(iter);
4324 if (service != NULL)
4325 connman_service_ref(service);
4329 service = connman_service_create();
4330 if (service == NULL)
4333 DBG("service %p", service);
4335 service->identifier = g_strdup(identifier);
4337 service->profile = g_strdup(__connman_profile_active_ident());
4339 iter = g_sequence_insert_sorted(service_list, service,
4340 service_compare, NULL);
4342 g_hash_table_insert(service_hash, service->identifier, iter);
4347 static int service_register(struct connman_service *service)
4349 const char *path = __connman_profile_active_path();
4350 GSequenceIter *iter;
4352 DBG("service %p", service);
4354 if (service->path != NULL)
4357 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4359 DBG("path %s", service->path);
4361 __connman_config_provision_service(service);
4363 __connman_storage_load_service(service);
4365 g_dbus_register_interface(connection, service->path,
4366 CONNMAN_SERVICE_INTERFACE,
4367 service_methods, service_signals,
4368 NULL, service, NULL);
4370 iter = g_hash_table_lookup(service_hash, service->identifier);
4372 g_sequence_sort_changed(iter, service_compare, NULL);
4374 __connman_profile_changed(TRUE);
4379 static void service_up(struct connman_ipconfig *ipconfig)
4381 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4383 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4385 link_changed(service);
4387 service->stats.valid = FALSE;
4388 service->stats_roaming.valid = FALSE;
4391 static void service_down(struct connman_ipconfig *ipconfig)
4393 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4396 static void service_lower_up(struct connman_ipconfig *ipconfig)
4398 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4400 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4402 stats_start(service);
4405 static void service_lower_down(struct connman_ipconfig *ipconfig)
4407 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4409 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4411 stats_stop(service);
4412 __connman_storage_save_service(service);
4415 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4417 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4418 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4419 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4421 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4423 type = __connman_ipconfig_get_config_type(ipconfig);
4424 method = __connman_ipconfig_get_method(ipconfig);
4426 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4429 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4430 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4431 __connman_service_set_ipconfig_ready(service,
4432 CONNMAN_IPCONFIG_TYPE_IPV6);
4434 settings_changed(service, ipconfig);
4437 static void service_ip_release(struct connman_ipconfig *ipconfig)
4439 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4440 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4441 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4443 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4445 type = __connman_ipconfig_get_config_type(ipconfig);
4446 method = __connman_ipconfig_get_method(ipconfig);
4448 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4451 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4452 method == CONNMAN_IPCONFIG_METHOD_OFF)
4453 __connman_service_indicate_state(service,
4454 CONNMAN_SERVICE_STATE_DISCONNECT,
4455 CONNMAN_IPCONFIG_TYPE_IPV6);
4457 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4458 method == CONNMAN_IPCONFIG_METHOD_OFF)
4459 __connman_service_indicate_state(service,
4460 CONNMAN_SERVICE_STATE_DISCONNECT,
4461 CONNMAN_IPCONFIG_TYPE_IPV4);
4463 settings_changed(service, ipconfig);
4466 static const struct connman_ipconfig_ops service_ops = {
4468 .down = service_down,
4469 .lower_up = service_lower_up,
4470 .lower_down = service_lower_down,
4471 .ip_bound = service_ip_bound,
4472 .ip_release = service_ip_release,
4475 static void setup_ip4config(struct connman_service *service, int index,
4476 enum connman_ipconfig_method method)
4481 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4482 CONNMAN_IPCONFIG_TYPE_IPV4);
4483 if (service->ipconfig_ipv4 == NULL)
4486 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4488 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4490 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4493 static void setup_ip6config(struct connman_service *service, int index)
4498 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4499 CONNMAN_IPCONFIG_TYPE_IPV6);
4500 if (service->ipconfig_ipv6 == NULL)
4503 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4505 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4508 void __connman_service_create_ip4config(struct connman_service *service,
4511 const char *ident = service->profile;
4514 DBG("ipv4 %p", service->ipconfig_ipv4);
4516 if (service->ipconfig_ipv4 != NULL)
4519 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4524 keyfile = __connman_storage_open_profile(ident);
4525 if (keyfile == NULL)
4528 if (service->ipconfig_ipv4)
4529 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4530 service->identifier, "IPv4.");
4531 g_key_file_free(keyfile);
4534 void __connman_service_create_ip6config(struct connman_service *service,
4537 const char *ident = service->profile;
4540 DBG("ipv6 %p", service->ipconfig_ipv6);
4542 if (service->ipconfig_ipv6 != NULL)
4545 setup_ip6config(service, index);
4550 keyfile = __connman_storage_open_profile(ident);
4551 if (keyfile == NULL)
4554 if (service->ipconfig_ipv6 != NULL)
4555 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4556 service->identifier, "IPv6.");
4558 g_key_file_free(keyfile);
4562 * __connman_service_lookup_from_network:
4563 * @network: network structure
4565 * Look up a service by network (reference count will not be increased)
4567 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4569 struct connman_service *service;
4570 const char *ident, *group;
4573 DBG("network %p", network);
4575 ident = __connman_network_get_ident(network);
4579 group = connman_network_get_group(network);
4583 name = g_strdup_printf("%s_%s_%s",
4584 __connman_network_get_type(network), ident, group);
4585 service = lookup_by_identifier(name);
4591 struct connman_service *__connman_service_lookup_from_index(int index)
4593 struct connman_service *service;
4594 GSequenceIter *iter;
4596 iter = g_sequence_get_begin_iter(service_list);
4598 while (g_sequence_iter_is_end(iter) == FALSE) {
4599 service = g_sequence_get(iter);
4601 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4605 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4609 iter = g_sequence_iter_next(iter);
4615 const char *__connman_service_get_ident(struct connman_service *service)
4617 return service->identifier;
4620 const char *__connman_service_get_path(struct connman_service *service)
4622 return service->path;
4625 unsigned int __connman_service_get_order(struct connman_service *service)
4627 GSequenceIter *iter;
4629 if (service == NULL)
4632 if (service->favorite == FALSE) {
4637 iter = g_hash_table_lookup(service_hash, service->identifier);
4639 if (g_sequence_iter_get_position(iter) == 0)
4641 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4642 service->order = 10;
4648 return service->order;
4651 static enum connman_service_type convert_network_type(struct connman_network *network)
4653 enum connman_network_type type = connman_network_get_type(network);
4656 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4657 case CONNMAN_NETWORK_TYPE_VENDOR:
4659 case CONNMAN_NETWORK_TYPE_ETHERNET:
4660 return CONNMAN_SERVICE_TYPE_ETHERNET;
4661 case CONNMAN_NETWORK_TYPE_WIFI:
4662 return CONNMAN_SERVICE_TYPE_WIFI;
4663 case CONNMAN_NETWORK_TYPE_WIMAX:
4664 return CONNMAN_SERVICE_TYPE_WIMAX;
4665 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4666 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4667 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4668 case CONNMAN_NETWORK_TYPE_CELLULAR:
4669 return CONNMAN_SERVICE_TYPE_CELLULAR;
4672 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4675 static enum connman_service_security convert_wifi_security(const char *security)
4677 if (security == NULL)
4678 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4679 else if (g_str_equal(security, "none") == TRUE)
4680 return CONNMAN_SERVICE_SECURITY_NONE;
4681 else if (g_str_equal(security, "wep") == TRUE)
4682 return CONNMAN_SERVICE_SECURITY_WEP;
4683 else if (g_str_equal(security, "psk") == TRUE)
4684 return CONNMAN_SERVICE_SECURITY_PSK;
4685 else if (g_str_equal(security, "ieee8021x") == TRUE)
4686 return CONNMAN_SERVICE_SECURITY_8021X;
4687 else if (g_str_equal(security, "wpa") == TRUE)
4688 return CONNMAN_SERVICE_SECURITY_WPA;
4689 else if (g_str_equal(security, "rsn") == TRUE)
4690 return CONNMAN_SERVICE_SECURITY_RSN;
4692 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4695 static void update_from_network(struct connman_service *service,
4696 struct connman_network *network)
4698 connman_uint8_t strength = service->strength;
4699 GSequenceIter *iter;
4702 DBG("service %p network %p", service, network);
4704 if (is_connected(service) == TRUE)
4707 if (is_connecting(service) == TRUE)
4710 str = connman_network_get_string(network, "Name");
4712 g_free(service->name);
4713 service->name = g_strdup(str);
4714 service->hidden = FALSE;
4716 g_free(service->name);
4717 service->name = NULL;
4718 service->hidden = TRUE;
4721 service->strength = connman_network_get_strength(network);
4722 service->roaming = connman_network_get_bool(network, "Roaming");
4724 if (service->strength == 0) {
4726 * Filter out 0-values; it's unclear what they mean
4727 * and they cause anomalous sorting of the priority list.
4729 service->strength = strength;
4732 str = connman_network_get_string(network, "WiFi.Security");
4733 service->security = convert_wifi_security(str);
4735 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4736 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4738 if (service->strength > strength && service->network != NULL) {
4739 connman_network_unref(service->network);
4740 service->network = connman_network_ref(network);
4742 strength_changed(service);
4745 if (service->network == NULL)
4746 service->network = connman_network_ref(network);
4748 iter = g_hash_table_lookup(service_hash, service->identifier);
4750 g_sequence_sort_changed(iter, service_compare, NULL);
4754 * __connman_service_create_from_network:
4755 * @network: network structure
4757 * Look up service by network and if not found, create one
4759 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4761 struct connman_service *service;
4762 struct connman_device *device;
4763 const char *ident, *group;
4767 DBG("network %p", network);
4769 ident = __connman_network_get_ident(network);
4773 group = connman_network_get_group(network);
4777 name = g_strdup_printf("%s_%s_%s",
4778 __connman_network_get_type(network), ident, group);
4779 service = service_get(name);
4782 if (service == NULL)
4785 if (__connman_network_get_weakness(network) == TRUE)
4788 if (service->path != NULL) {
4789 update_from_network(service, network);
4790 __connman_profile_changed(TRUE);
4794 service->type = convert_network_type(network);
4796 switch (service->type) {
4797 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4798 case CONNMAN_SERVICE_TYPE_SYSTEM:
4799 case CONNMAN_SERVICE_TYPE_ETHERNET:
4800 case CONNMAN_SERVICE_TYPE_WIMAX:
4801 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4802 case CONNMAN_SERVICE_TYPE_GPS:
4803 case CONNMAN_SERVICE_TYPE_VPN:
4804 case CONNMAN_SERVICE_TYPE_GADGET:
4805 service->autoconnect = FALSE;
4807 case CONNMAN_SERVICE_TYPE_WIFI:
4808 case CONNMAN_SERVICE_TYPE_CELLULAR:
4809 service->autoconnect = TRUE;
4813 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4815 update_from_network(service, network);
4817 index = connman_network_get_index(network);
4819 if (service->ipconfig_ipv4 == NULL)
4820 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4822 if (service->ipconfig_ipv6 == NULL)
4823 setup_ip6config(service, index);
4825 service_register(service);
4827 if (service->favorite == TRUE) {
4828 device = connman_network_get_device(service->network);
4829 if (device && __connman_device_scanning(device) == FALSE)
4830 __connman_service_auto_connect();
4833 __connman_notifier_service_add(service);
4838 void __connman_service_update_from_network(struct connman_network *network)
4840 struct connman_service *service;
4841 connman_uint8_t strength;
4842 connman_bool_t roaming;
4843 GSequenceIter *iter;
4845 connman_bool_t stats_enable;
4847 DBG("network %p", network);
4849 service = __connman_service_lookup_from_network(network);
4850 if (service == NULL)
4853 if (service->network == NULL)
4856 name = connman_network_get_string(service->network, "Name");
4857 if (g_strcmp0(service->name, name) != 0) {
4858 g_free(service->name);
4859 service->name = g_strdup(name);
4860 connman_dbus_property_changed_basic(service->path,
4861 CONNMAN_SERVICE_INTERFACE, "Name",
4862 DBUS_TYPE_STRING, &service->name);
4865 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4866 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4868 strength = connman_network_get_strength(service->network);
4869 if (strength == service->strength)
4872 service->strength = strength;
4874 strength_changed(service);
4877 roaming = connman_network_get_bool(service->network, "Roaming");
4878 if (roaming == service->roaming)
4881 stats_enable = stats_enabled(service);
4882 if (stats_enable == TRUE)
4883 stats_stop(service);
4885 service->roaming = roaming;
4887 if (stats_enable == TRUE)
4888 stats_start(service);
4890 roaming_changed(service);
4892 iter = g_hash_table_lookup(service_hash, service->identifier);
4894 g_sequence_sort_changed(iter, service_compare, NULL);
4897 void __connman_service_remove_from_network(struct connman_network *network)
4899 struct connman_service *service;
4901 DBG("network %p", network);
4903 service = __connman_service_lookup_from_network(network);
4904 if (service == NULL)
4907 __connman_service_put(service);
4911 * __connman_service_create_from_provider:
4912 * @provider: provider structure
4914 * Look up service by provider and if not found, create one
4916 struct connman_service *
4917 __connman_service_create_from_provider(struct connman_provider *provider)
4919 struct connman_service *service;
4920 const char *ident, *str;
4922 int index = connman_provider_get_index(provider);
4924 DBG("provider %p", provider);
4926 ident = __connman_provider_get_ident(provider);
4930 name = g_strdup_printf("vpn_%s", ident);
4931 service = service_get(name);
4934 if (service == NULL)
4937 service->type = CONNMAN_SERVICE_TYPE_VPN;
4938 service->provider = connman_provider_ref(provider);
4939 service->autoconnect = FALSE;
4941 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4943 str = connman_provider_get_string(provider, "Name");
4945 g_free(service->name);
4946 service->name = g_strdup(str);
4947 service->hidden = FALSE;
4949 g_free(service->name);
4950 service->name = NULL;
4951 service->hidden = TRUE;
4954 service->strength = 0;
4956 if (service->ipconfig_ipv4 == NULL)
4957 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
4959 if (service->ipconfig_ipv6 == NULL)
4960 setup_ip6config(service, index);
4962 service_register(service);
4964 __connman_notifier_service_add(service);
4969 static int service_load(struct connman_service *service)
4971 const char *ident = service->profile;
4973 GError *error = NULL;
4974 gchar *pathname, *data = NULL;
4977 connman_bool_t autoconnect;
4978 unsigned int ssid_len;
4981 DBG("service %p", service);
4986 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4987 if (pathname == NULL)
4990 keyfile = g_key_file_new();
4992 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
4999 if (g_key_file_load_from_data(keyfile, data, length,
5000 0, NULL) == FALSE) {
5007 switch (service->type) {
5008 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5009 case CONNMAN_SERVICE_TYPE_SYSTEM:
5010 case CONNMAN_SERVICE_TYPE_ETHERNET:
5011 case CONNMAN_SERVICE_TYPE_GPS:
5012 case CONNMAN_SERVICE_TYPE_VPN:
5013 case CONNMAN_SERVICE_TYPE_GADGET:
5015 case CONNMAN_SERVICE_TYPE_WIFI:
5016 if (service->name == NULL) {
5019 name = g_key_file_get_string(keyfile,
5020 service->identifier, "Name", NULL);
5022 g_free(service->name);
5023 service->name = name;
5026 if (service->network != NULL)
5027 connman_network_set_name(service->network,
5031 if (service->network &&
5032 connman_network_get_blob(service->network,
5033 "WiFi.SSID", &ssid_len) == NULL) {
5036 hex_ssid = g_key_file_get_string(keyfile,
5037 service->identifier,
5040 if (hex_ssid != NULL) {
5042 unsigned int i, j = 0, hex;
5043 size_t hex_ssid_len = strlen(hex_ssid);
5045 ssid = g_try_malloc0(hex_ssid_len / 2);
5052 for (i = 0; i < hex_ssid_len; i += 2) {
5053 sscanf(hex_ssid + i, "%02x", &hex);
5057 connman_network_set_blob(service->network,
5058 "WiFi.SSID", ssid, hex_ssid_len / 2);
5065 case CONNMAN_SERVICE_TYPE_WIMAX:
5066 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5067 case CONNMAN_SERVICE_TYPE_CELLULAR:
5068 service->favorite = g_key_file_get_boolean(keyfile,
5069 service->identifier, "Favorite", NULL);
5071 autoconnect = g_key_file_get_boolean(keyfile,
5072 service->identifier, "AutoConnect", &error);
5074 service->autoconnect = autoconnect;
5075 g_clear_error(&error);
5077 str = g_key_file_get_string(keyfile,
5078 service->identifier, "Failure", NULL);
5080 if (service->favorite == FALSE)
5081 service->state_ipv4 = service->state_ipv6 =
5082 CONNMAN_SERVICE_STATE_FAILURE;
5083 service->error = string2error(str);
5088 str = g_key_file_get_string(keyfile,
5089 service->identifier, "Modified", NULL);
5091 g_time_val_from_iso8601(str, &service->modified);
5095 str = g_key_file_get_string(keyfile,
5096 service->identifier, "Passphrase", NULL);
5098 g_free(service->passphrase);
5099 service->passphrase = str;
5102 if (service->ipconfig_ipv4 != NULL)
5103 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5104 service->identifier, "IPv4.");
5106 if (service->ipconfig_ipv6 != NULL)
5107 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5108 service->identifier, "IPv6.");
5110 service->nameservers_config = g_key_file_get_string_list(keyfile,
5111 service->identifier, "Nameservers", &length, NULL);
5112 if (service->nameservers_config != NULL && length == 0) {
5113 g_strfreev(service->nameservers_config);
5114 service->nameservers_config = NULL;
5117 service->domains = g_key_file_get_string_list(keyfile,
5118 service->identifier, "Domains", &length, NULL);
5119 if (service->domains != NULL && length == 0) {
5120 g_strfreev(service->domains);
5121 service->domains = NULL;
5124 str = g_key_file_get_string(keyfile,
5125 service->identifier, "Proxy.Method", NULL);
5127 service->proxy_config = string2proxymethod(str);
5131 service->proxies = g_key_file_get_string_list(keyfile,
5132 service->identifier, "Proxy.Servers", &length, NULL);
5133 if (service->proxies != NULL && length == 0) {
5134 g_strfreev(service->proxies);
5135 service->proxies = NULL;
5138 service->excludes = g_key_file_get_string_list(keyfile,
5139 service->identifier, "Proxy.Excludes", &length, NULL);
5140 if (service->excludes != NULL && length == 0) {
5141 g_strfreev(service->excludes);
5142 service->excludes = NULL;
5145 str = g_key_file_get_string(keyfile,
5146 service->identifier, "Proxy.URL", NULL);
5148 g_free(service->pac);
5153 g_key_file_free(keyfile);
5158 static int service_save(struct connman_service *service)
5160 const char *ident = service->profile;
5162 gchar *pathname, *data = NULL;
5165 const char *cst_str = NULL;
5168 DBG("service %p", service);
5173 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5174 if (pathname == NULL)
5177 keyfile = g_key_file_new();
5179 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5183 if (g_key_file_load_from_data(keyfile, data, length,
5191 if (service->name != NULL)
5192 g_key_file_set_string(keyfile, service->identifier,
5193 "Name", service->name);
5195 switch (service->type) {
5196 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5197 case CONNMAN_SERVICE_TYPE_SYSTEM:
5198 case CONNMAN_SERVICE_TYPE_ETHERNET:
5199 case CONNMAN_SERVICE_TYPE_GPS:
5200 case CONNMAN_SERVICE_TYPE_VPN:
5201 case CONNMAN_SERVICE_TYPE_GADGET:
5203 case CONNMAN_SERVICE_TYPE_WIFI:
5204 if (service->network) {
5205 const unsigned char *ssid;
5206 unsigned int ssid_len = 0;
5208 ssid = connman_network_get_blob(service->network,
5209 "WiFi.SSID", &ssid_len);
5211 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5212 char *identifier = service->identifier;
5216 str = g_string_sized_new(ssid_len * 2);
5222 for (i = 0; i < ssid_len; i++)
5223 g_string_append_printf(str,
5226 g_key_file_set_string(keyfile, identifier,
5229 g_string_free(str, TRUE);
5234 case CONNMAN_SERVICE_TYPE_WIMAX:
5235 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5236 case CONNMAN_SERVICE_TYPE_CELLULAR:
5237 g_key_file_set_boolean(keyfile, service->identifier,
5238 "Favorite", service->favorite);
5240 if (service->favorite == TRUE)
5241 g_key_file_set_boolean(keyfile, service->identifier,
5242 "AutoConnect", service->autoconnect);
5244 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5245 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5246 const char *failure = error2string(service->error);
5247 if (failure != NULL)
5248 g_key_file_set_string(keyfile,
5249 service->identifier,
5250 "Failure", failure);
5252 g_key_file_remove_key(keyfile, service->identifier,
5258 str = g_time_val_to_iso8601(&service->modified);
5260 g_key_file_set_string(keyfile, service->identifier,
5265 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5266 g_key_file_set_string(keyfile, service->identifier,
5267 "Passphrase", service->passphrase);
5269 g_key_file_remove_key(keyfile, service->identifier,
5270 "Passphrase", NULL);
5272 if (service->ipconfig_ipv4 != NULL)
5273 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5274 service->identifier, "IPv4.");
5276 if (service->ipconfig_ipv6 != NULL)
5277 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5278 service->identifier, "IPv6.");
5280 if (service->nameservers_config != NULL) {
5281 guint len = g_strv_length(service->nameservers_config);
5283 g_key_file_set_string_list(keyfile, service->identifier,
5285 (const gchar **) service->nameservers_config, len);
5287 g_key_file_remove_key(keyfile, service->identifier,
5288 "Nameservers", NULL);
5290 if (service->domains != NULL) {
5291 guint len = g_strv_length(service->domains);
5293 g_key_file_set_string_list(keyfile, service->identifier,
5295 (const gchar **) service->domains, len);
5297 g_key_file_remove_key(keyfile, service->identifier,
5300 cst_str = proxymethod2string(service->proxy_config);
5301 if (cst_str != NULL)
5302 g_key_file_set_string(keyfile, service->identifier,
5303 "Proxy.Method", cst_str);
5305 if (service->proxies != NULL) {
5306 guint len = g_strv_length(service->proxies);
5308 g_key_file_set_string_list(keyfile, service->identifier,
5310 (const gchar **) service->proxies, len);
5312 g_key_file_remove_key(keyfile, service->identifier,
5313 "Proxy.Servers", NULL);
5315 if (service->excludes != NULL) {
5316 guint len = g_strv_length(service->excludes);
5318 g_key_file_set_string_list(keyfile, service->identifier,
5320 (const gchar **) service->excludes, len);
5322 g_key_file_remove_key(keyfile, service->identifier,
5323 "Proxy.Excludes", NULL);
5325 if (service->pac != NULL && strlen(service->pac) > 0)
5326 g_key_file_set_string(keyfile, service->identifier,
5327 "Proxy.URL", service->pac);
5329 g_key_file_remove_key(keyfile, service->identifier,
5332 data = g_key_file_to_data(keyfile, &length, NULL);
5334 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5335 connman_error("Failed to store service information");
5340 g_key_file_free(keyfile);
5347 static struct connman_storage service_storage = {
5349 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5350 .service_load = service_load,
5351 .service_save = service_save,
5354 int __connman_service_init(void)
5358 connection = connman_dbus_get_connection();
5360 if (connman_storage_register(&service_storage) < 0)
5361 connman_error("Failed to register service storage");
5363 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5366 service_list = g_sequence_new(service_free);
5371 void __connman_service_cleanup(void)
5375 g_sequence_free(service_list);
5376 service_list = NULL;
5378 g_hash_table_destroy(service_hash);
5379 service_hash = NULL;
5381 g_slist_free(counter_list);
5382 counter_list = NULL;
5384 connman_storage_unregister(&service_storage);
5386 dbus_connection_unref(connection);