5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #define CONNECT_TIMEOUT 120
34 static DBusConnection *connection = NULL;
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
38 static GSList *counter_list = NULL;
40 struct connman_stats {
42 connman_bool_t enabled;
43 struct connman_stats_data data_last;
44 struct connman_stats_data data;
48 struct connman_stats_counter {
49 connman_bool_t append_all;
50 struct connman_stats stats;
51 struct connman_stats stats_roaming;
54 struct connman_service {
56 gint session_usage_count;
59 enum connman_service_type type;
60 enum connman_service_security security;
61 enum connman_service_state state_ipv4;
62 enum connman_service_state state_ipv6;
63 enum connman_service_error error;
64 connman_uint8_t strength;
65 connman_bool_t favorite;
66 connman_bool_t immutable;
67 connman_bool_t hidden;
68 connman_bool_t ignore;
69 connman_bool_t autoconnect;
70 connman_bool_t userconnect;
76 connman_bool_t roaming;
77 connman_bool_t login_required;
78 struct connman_ipconfig *ipconfig_ipv4;
79 struct connman_ipconfig *ipconfig_ipv6;
80 struct connman_network *network;
81 struct connman_provider *provider;
83 char **nameservers_config;
87 /* 802.1x settings from the config files */
91 char *client_cert_file;
92 char *private_key_file;
93 char *private_key_passphrase;
97 struct connman_location *location;
98 struct connman_stats stats;
99 struct connman_stats stats_roaming;
100 GHashTable *counter_table;
101 enum connman_service_proxy_method proxy;
102 enum connman_service_proxy_method proxy_config;
109 static void append_path(gpointer value, gpointer user_data)
111 struct connman_service *service = value;
112 DBusMessageIter *iter = user_data;
114 if (service->path == NULL || service->hidden == TRUE)
117 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
121 void __connman_service_list(DBusMessageIter *iter, void *user_data)
123 g_sequence_foreach(service_list, append_path, iter);
128 struct connman_service *service;
131 static void compare_path(gpointer value, gpointer user_data)
133 struct connman_service *service = value;
134 struct find_data *data = user_data;
136 if (data->service != NULL)
139 if (g_strcmp0(service->path, data->path) == 0)
140 data->service = service;
143 static struct connman_service *find_service(const char *path)
145 struct find_data data = { .path = path, .service = NULL };
147 DBG("path %s", path);
149 g_sequence_foreach(service_list, compare_path, &data);
154 const char *__connman_service_type2string(enum connman_service_type type)
157 case CONNMAN_SERVICE_TYPE_UNKNOWN:
159 case CONNMAN_SERVICE_TYPE_SYSTEM:
161 case CONNMAN_SERVICE_TYPE_ETHERNET:
163 case CONNMAN_SERVICE_TYPE_WIFI:
165 case CONNMAN_SERVICE_TYPE_WIMAX:
167 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
169 case CONNMAN_SERVICE_TYPE_CELLULAR:
171 case CONNMAN_SERVICE_TYPE_GPS:
173 case CONNMAN_SERVICE_TYPE_VPN:
175 case CONNMAN_SERVICE_TYPE_GADGET:
182 static const char *security2string(enum connman_service_security security)
185 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
187 case CONNMAN_SERVICE_SECURITY_NONE:
189 case CONNMAN_SERVICE_SECURITY_WEP:
191 case CONNMAN_SERVICE_SECURITY_PSK:
193 case CONNMAN_SERVICE_SECURITY_8021X:
195 case CONNMAN_SERVICE_SECURITY_WPA:
197 case CONNMAN_SERVICE_SECURITY_RSN:
204 static const char *state2string(enum connman_service_state state)
207 case CONNMAN_SERVICE_STATE_UNKNOWN:
209 case CONNMAN_SERVICE_STATE_IDLE:
211 case CONNMAN_SERVICE_STATE_ASSOCIATION:
212 return "association";
213 case CONNMAN_SERVICE_STATE_CONFIGURATION:
214 return "configuration";
215 case CONNMAN_SERVICE_STATE_READY:
217 case CONNMAN_SERVICE_STATE_ONLINE:
219 case CONNMAN_SERVICE_STATE_DISCONNECT:
221 case CONNMAN_SERVICE_STATE_FAILURE:
228 static const char *error2string(enum connman_service_error error)
231 case CONNMAN_SERVICE_ERROR_UNKNOWN:
233 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
234 return "out-of-range";
235 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
236 return "pin-missing";
237 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
238 return "dhcp-failed";
239 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
240 return "connect-failed";
241 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
242 return "login-failed";
243 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
244 return "auth-failed";
245 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
246 return "invalid-key";
252 static enum connman_service_error string2error(const char *error)
254 if (g_strcmp0(error, "dhcp-failed") == 0)
255 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
256 else if (g_strcmp0(error, "pin-missing") == 0)
257 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
258 else if (g_strcmp0(error, "invalid-key") == 0)
259 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
261 return CONNMAN_SERVICE_ERROR_UNKNOWN;
264 static const char *proxymethod2string(enum connman_service_proxy_method method)
267 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
269 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
271 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
273 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
280 static enum connman_service_proxy_method string2proxymethod(const char *method)
282 if (g_strcmp0(method, "direct") == 0)
283 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
284 else if (g_strcmp0(method, "auto") == 0)
285 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
286 else if (g_strcmp0(method, "manual") == 0)
287 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
289 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
292 static enum connman_service_state combine_state(
293 enum connman_service_state state_a,
294 enum connman_service_state state_b)
296 enum connman_service_state result;
298 if (state_a == state_b) {
303 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
308 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
313 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
318 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
323 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
324 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
325 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
326 state_b == CONNMAN_SERVICE_STATE_READY)
333 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
334 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
335 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
336 state_a == CONNMAN_SERVICE_STATE_READY)
343 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
344 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
345 state_b == CONNMAN_SERVICE_STATE_READY)
352 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
353 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
354 state_a == CONNMAN_SERVICE_STATE_READY)
361 if (state_a == CONNMAN_SERVICE_STATE_READY) {
362 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
369 if (state_b == CONNMAN_SERVICE_STATE_READY) {
370 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
377 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
382 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
387 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
392 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
397 result = CONNMAN_SERVICE_STATE_FAILURE;
403 static connman_bool_t is_connecting_state(struct connman_service *service,
404 enum connman_service_state state)
407 case CONNMAN_SERVICE_STATE_UNKNOWN:
408 case CONNMAN_SERVICE_STATE_IDLE:
409 case CONNMAN_SERVICE_STATE_FAILURE:
410 if (service->network != NULL)
411 return connman_network_get_connecting(service->network);
412 case CONNMAN_SERVICE_STATE_DISCONNECT:
413 case CONNMAN_SERVICE_STATE_READY:
414 case CONNMAN_SERVICE_STATE_ONLINE:
416 case CONNMAN_SERVICE_STATE_ASSOCIATION:
417 case CONNMAN_SERVICE_STATE_CONFIGURATION:
424 static connman_bool_t is_connected_state(const struct connman_service *service,
425 enum connman_service_state state)
428 case CONNMAN_SERVICE_STATE_UNKNOWN:
429 case CONNMAN_SERVICE_STATE_IDLE:
430 case CONNMAN_SERVICE_STATE_ASSOCIATION:
431 case CONNMAN_SERVICE_STATE_CONFIGURATION:
432 case CONNMAN_SERVICE_STATE_DISCONNECT:
433 case CONNMAN_SERVICE_STATE_FAILURE:
435 case CONNMAN_SERVICE_STATE_READY:
436 case CONNMAN_SERVICE_STATE_ONLINE:
443 static connman_bool_t is_connecting(struct connman_service *service)
445 enum connman_service_state state;
447 state = combine_state(service->state_ipv4, service->state_ipv6);
449 return is_connecting_state(service, state);
452 static connman_bool_t is_connected(struct connman_service *service)
454 enum connman_service_state state;
456 state = combine_state(service->state_ipv4, service->state_ipv6);
458 return is_connected_state(service, state);
461 static void update_nameservers(struct connman_service *service)
465 if (service->ipconfig_ipv4)
466 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
467 else if (service->ipconfig_ipv6)
468 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
475 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
476 case CONNMAN_SERVICE_STATE_UNKNOWN:
477 case CONNMAN_SERVICE_STATE_IDLE:
478 case CONNMAN_SERVICE_STATE_ASSOCIATION:
479 case CONNMAN_SERVICE_STATE_CONFIGURATION:
481 case CONNMAN_SERVICE_STATE_FAILURE:
482 case CONNMAN_SERVICE_STATE_DISCONNECT:
483 connman_resolver_remove_all(ifname);
485 case CONNMAN_SERVICE_STATE_READY:
486 case CONNMAN_SERVICE_STATE_ONLINE:
490 connman_resolver_remove_all(ifname);
492 if (service->nameservers_config != NULL) {
495 for (i = 0; service->nameservers_config[i] != NULL; i++) {
496 connman_resolver_append(ifname, NULL,
497 service->nameservers_config[i]);
499 } else if (service->nameservers != NULL) {
502 for (i = 0; service->nameservers[i] != NULL; i++) {
503 connman_resolver_append(ifname, NULL,
504 service->nameservers[i]);
508 if (service->domains != NULL) {
511 for (i = 0; service->domains[i]; i++)
512 connman_resolver_append(ifname, service->domains[i],
514 } else if (service->domainname != NULL)
515 connman_resolver_append(ifname, service->domainname, NULL);
517 connman_resolver_flush();
520 int __connman_service_nameserver_append(struct connman_service *service,
521 const char *nameserver)
525 DBG("service %p nameserver %s", service, nameserver);
527 if (nameserver == NULL)
530 if (service->nameservers != NULL) {
533 for (i = 0; service->nameservers[i] != NULL; i++)
534 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
537 len = g_strv_length(service->nameservers);
538 service->nameservers = g_try_renew(char *, service->nameservers,
542 service->nameservers = g_try_new0(char *, len + 2);
545 if (service->nameservers == NULL)
548 service->nameservers[len] = g_strdup(nameserver);
549 service->nameservers[len + 1] = NULL;
551 update_nameservers(service);
556 int __connman_service_nameserver_remove(struct connman_service *service,
557 const char *nameserver)
562 DBG("service %p nameserver %s", service, nameserver);
564 if (nameserver == NULL)
567 if (service->nameservers == NULL)
570 len = g_strv_length(service->nameservers);
572 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
575 g_strfreev(service->nameservers);
576 service->nameservers = NULL;
581 servers = g_try_new0(char *, len - 1);
585 for (i = 0, j = 0; i < len; i++) {
586 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
587 servers[j] = g_strdup(service->nameservers[i]);
591 servers[len - 2] = NULL;
593 g_strfreev(service->nameservers);
594 service->nameservers = servers;
596 update_nameservers(service);
601 void __connman_service_nameserver_clear(struct connman_service *service)
603 g_strfreev(service->nameservers);
604 service->nameservers = NULL;
606 update_nameservers(service);
609 static void nameserver_add_routes(int index, char **nameservers,
614 for (i = 0; nameservers[i] != NULL; i++) {
615 if (connman_inet_compare_subnet(index, nameservers[i]))
618 connman_inet_add_host_route(index, nameservers[i], gw);
622 static void nameserver_del_routes(int index, char **nameservers)
626 for (i = 0; nameservers[i] != NULL; i++)
627 connman_inet_del_host_route(index, nameservers[i]);
630 void __connman_service_nameserver_add_routes(struct connman_service *service,
638 if (service->network != NULL)
639 index = connman_network_get_index(service->network);
640 else if (service->provider != NULL)
641 index = connman_provider_get_index(service->provider);
643 if (service->nameservers_config != NULL) {
645 * Configured nameserver takes preference over the
646 * discoverd nameserver gathered from DHCP, VPN, etc.
648 nameserver_add_routes(index, service->nameservers_config, gw);
649 } else if (service->nameservers != NULL) {
651 * We add nameservers host routes for nameservers that
652 * are not on our subnet. For those who are, the subnet
653 * route will be installed by the time the dns proxy code
654 * tries to reach them. The subnet route is installed
655 * when setting the interface IP address.
657 nameserver_add_routes(index, service->nameservers, gw);
661 void __connman_service_nameserver_del_routes(struct connman_service *service)
668 if (service->network != NULL)
669 index = connman_network_get_index(service->network);
670 else if (service->provider != NULL)
671 index = connman_provider_get_index(service->provider);
673 if (service->nameservers_config != NULL)
674 nameserver_del_routes(index, service->nameservers_config);
675 else if (service->nameservers != NULL)
676 nameserver_del_routes(index, service->nameservers);
679 static struct connman_stats *stats_get(struct connman_service *service)
681 if (service->roaming == TRUE)
682 return &service->stats_roaming;
684 return &service->stats;
687 static connman_bool_t stats_enabled(struct connman_service *service)
689 struct connman_stats *stats = stats_get(service);
691 return stats->enabled;
694 static void stats_start(struct connman_service *service)
696 struct connman_stats *stats = stats_get(service);
698 DBG("service %p", service);
700 if (stats->timer == NULL)
703 stats->enabled = TRUE;
704 stats->data_last.time = stats->data.time;
706 g_timer_start(stats->timer);
709 static void stats_stop(struct connman_service *service)
711 struct connman_stats *stats = stats_get(service);
712 unsigned int seconds;
714 DBG("service %p", service);
716 if (stats->timer == NULL)
719 if (stats->enabled == FALSE)
722 g_timer_stop(stats->timer);
724 seconds = g_timer_elapsed(stats->timer, NULL);
725 stats->data.time = stats->data_last.time + seconds;
727 stats->enabled = FALSE;
730 static void reset_stats(struct connman_service *service)
732 DBG("service %p", service);
735 service->stats.valid = FALSE;
737 service->stats.data.rx_packets = 0;
738 service->stats.data.tx_packets = 0;
739 service->stats.data.rx_bytes = 0;
740 service->stats.data.tx_bytes = 0;
741 service->stats.data.rx_errors = 0;
742 service->stats.data.tx_errors = 0;
743 service->stats.data.rx_dropped = 0;
744 service->stats.data.tx_dropped = 0;
745 service->stats.data.time = 0;
746 service->stats.data_last.time = 0;
748 g_timer_reset(service->stats.timer);
751 service->stats_roaming.valid = FALSE;
753 service->stats_roaming.data.rx_packets = 0;
754 service->stats_roaming.data.tx_packets = 0;
755 service->stats_roaming.data.rx_bytes = 0;
756 service->stats_roaming.data.tx_bytes = 0;
757 service->stats_roaming.data.rx_errors = 0;
758 service->stats_roaming.data.tx_errors = 0;
759 service->stats_roaming.data.rx_dropped = 0;
760 service->stats_roaming.data.tx_dropped = 0;
761 service->stats_roaming.data.time = 0;
762 service->stats_roaming.data_last.time = 0;
764 g_timer_reset(service->stats_roaming.timer);
767 static struct connman_service *get_default(void)
769 struct connman_service *service;
772 iter = g_sequence_get_begin_iter(service_list);
774 if (g_sequence_iter_is_end(iter) == TRUE)
777 service = g_sequence_get(iter);
779 if (is_connected(service) == FALSE)
785 static void default_changed(void)
787 struct connman_service *service = get_default();
789 __connman_notifier_default_changed(service);
792 const char *__connman_service_default(void)
794 struct connman_service *service;
796 service = get_default();
800 return __connman_service_type2string(service->type);
803 static void state_changed(struct connman_service *service)
805 enum connman_service_state state;
808 state = combine_state(service->state_ipv4, service->state_ipv6);
810 __connman_notifier_service_state_changed(service, state);
812 str = state2string(state);
816 connman_dbus_property_changed_basic(service->path,
817 CONNMAN_SERVICE_INTERFACE, "State",
818 DBUS_TYPE_STRING, &str);
821 static void strength_changed(struct connman_service *service)
823 if (service->strength == 0)
826 connman_dbus_property_changed_basic(service->path,
827 CONNMAN_SERVICE_INTERFACE, "Strength",
828 DBUS_TYPE_BYTE, &service->strength);
831 static void favorite_changed(struct connman_service *service)
833 if (service->path == NULL)
836 connman_dbus_property_changed_basic(service->path,
837 CONNMAN_SERVICE_INTERFACE, "Favorite",
838 DBUS_TYPE_BOOLEAN, &service->favorite);
841 static void immutable_changed(struct connman_service *service)
843 if (service->path == NULL)
846 connman_dbus_property_changed_basic(service->path,
847 CONNMAN_SERVICE_INTERFACE, "Immutable",
848 DBUS_TYPE_BOOLEAN, &service->immutable);
851 static void roaming_changed(struct connman_service *service)
853 if (service->path == NULL)
856 connman_dbus_property_changed_basic(service->path,
857 CONNMAN_SERVICE_INTERFACE, "Roaming",
858 DBUS_TYPE_BOOLEAN, &service->roaming);
861 static void autoconnect_changed(struct connman_service *service)
863 if (service->path == NULL)
866 connman_dbus_property_changed_basic(service->path,
867 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
868 DBUS_TYPE_BOOLEAN, &service->autoconnect);
871 static void passphrase_changed(struct connman_service *service)
873 dbus_bool_t required;
875 switch (service->type) {
876 case CONNMAN_SERVICE_TYPE_UNKNOWN:
877 case CONNMAN_SERVICE_TYPE_SYSTEM:
878 case CONNMAN_SERVICE_TYPE_ETHERNET:
879 case CONNMAN_SERVICE_TYPE_WIMAX:
880 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
881 case CONNMAN_SERVICE_TYPE_CELLULAR:
882 case CONNMAN_SERVICE_TYPE_GPS:
883 case CONNMAN_SERVICE_TYPE_VPN:
884 case CONNMAN_SERVICE_TYPE_GADGET:
886 case CONNMAN_SERVICE_TYPE_WIFI:
889 switch (service->security) {
890 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
891 case CONNMAN_SERVICE_SECURITY_NONE:
893 case CONNMAN_SERVICE_SECURITY_WEP:
894 case CONNMAN_SERVICE_SECURITY_PSK:
895 case CONNMAN_SERVICE_SECURITY_WPA:
896 case CONNMAN_SERVICE_SECURITY_RSN:
897 if (service->passphrase == NULL)
900 case CONNMAN_SERVICE_SECURITY_8021X:
906 connman_dbus_property_changed_basic(service->path,
907 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
908 DBUS_TYPE_BOOLEAN, &required);
911 static void login_changed(struct connman_service *service)
913 dbus_bool_t required = service->login_required;
915 if (service->path == NULL)
918 connman_dbus_property_changed_basic(service->path,
919 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
920 DBUS_TYPE_BOOLEAN, &required);
923 static void append_security(DBusMessageIter *iter, void *user_data)
925 struct connman_service *service = user_data;
928 str = security2string(service->security);
930 dbus_message_iter_append_basic(iter,
931 DBUS_TYPE_STRING, &str);
934 if (service->wps == TRUE)
935 dbus_message_iter_append_basic(iter,
936 DBUS_TYPE_STRING, &str);
939 static void append_ethernet(DBusMessageIter *iter, void *user_data)
941 struct connman_service *service = user_data;
943 if (service->ipconfig_ipv4 != NULL)
944 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
946 else if (service->ipconfig_ipv6 != NULL)
947 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
951 static void append_ipv4(DBusMessageIter *iter, void *user_data)
953 struct connman_service *service = user_data;
955 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
956 state2string(service->state_ipv4));
958 if (is_connected_state(service, service->state_ipv4) == FALSE)
961 if (service->ipconfig_ipv4 != NULL)
962 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
965 static void append_ipv6(DBusMessageIter *iter, void *user_data)
967 struct connman_service *service = user_data;
969 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
970 state2string(service->state_ipv6));
972 if (is_connected_state(service, service->state_ipv6) == FALSE)
975 if (service->ipconfig_ipv6 != NULL)
976 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
977 service->ipconfig_ipv4);
980 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
982 struct connman_service *service = user_data;
984 if (service->ipconfig_ipv4 != NULL)
985 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
989 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
991 struct connman_service *service = user_data;
993 if (service->ipconfig_ipv6 != NULL)
994 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
998 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1003 servers = *nameservers;
1005 for (i = 0; servers[i] != NULL; i++) {
1006 dbus_message_iter_append_basic(iter,
1007 DBUS_TYPE_STRING, &servers[i]);
1011 static void append_dns(DBusMessageIter *iter, void *user_data)
1013 struct connman_service *service = user_data;
1015 if (is_connected(service) == FALSE)
1018 if (service->nameservers_config != NULL) {
1019 append_nameserver(iter, &service->nameservers_config);
1021 } else if (service->nameservers != NULL) {
1022 append_nameserver(iter, &service->nameservers);
1027 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1029 struct connman_service *service = user_data;
1032 if (service->nameservers_config == NULL)
1035 for (i = 0; service->nameservers_config[i]; i++) {
1036 dbus_message_iter_append_basic(iter,
1038 &service->nameservers_config[i]);
1042 static void append_domain(DBusMessageIter *iter, void *user_data)
1044 struct connman_service *service = user_data;
1046 if (is_connected(service) == FALSE &&
1047 is_connecting(service) == FALSE)
1050 if (service->domainname == NULL)
1053 dbus_message_iter_append_basic(iter,
1054 DBUS_TYPE_STRING, &service->domainname);
1057 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1059 struct connman_service *service = user_data;
1062 if (service->domains == NULL)
1065 for (i = 0; service->domains[i]; i++)
1066 dbus_message_iter_append_basic(iter,
1067 DBUS_TYPE_STRING, &service->domains[i]);
1070 static void append_proxies(DBusMessageIter *iter, void *user_data)
1072 struct connman_service *service = user_data;
1075 if (service->proxies == NULL)
1078 for (i = 0; service->proxies[i]; i++)
1079 dbus_message_iter_append_basic(iter,
1080 DBUS_TYPE_STRING, &service->proxies[i]);
1083 static void append_excludes(DBusMessageIter *iter, void *user_data)
1085 struct connman_service *service = user_data;
1088 if (service->excludes == NULL)
1091 for (i = 0; service->excludes[i]; i++)
1092 dbus_message_iter_append_basic(iter,
1093 DBUS_TYPE_STRING, &service->excludes[i]);
1096 static void append_proxy(DBusMessageIter *iter, void *user_data)
1098 struct connman_service *service = user_data;
1099 enum connman_service_proxy_method proxy;
1100 const char *pac = NULL;
1101 const char *method = proxymethod2string(
1102 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1106 if (is_connected(service) == FALSE)
1109 proxy = connman_service_get_proxy_method(service);
1112 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1114 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1116 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1117 connman_dbus_dict_append_array(iter, "Servers",
1118 DBUS_TYPE_STRING, append_proxies,
1121 connman_dbus_dict_append_array(iter, "Excludes",
1122 DBUS_TYPE_STRING, append_excludes,
1125 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1126 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1127 if (service->ipconfig_ipv4 != NULL)
1128 pac = __connman_ipconfig_get_proxy_autoconfig(
1129 service->ipconfig_ipv4);
1130 else if (service->ipconfig_ipv6 != NULL)
1131 pac = __connman_ipconfig_get_proxy_autoconfig(
1132 service->ipconfig_ipv6);
1134 if (service->pac == NULL && pac == NULL)
1137 if (service->pac != NULL)
1140 connman_dbus_dict_append_basic(iter, "URL",
1141 DBUS_TYPE_STRING, &pac);
1145 method = proxymethod2string(proxy);
1148 connman_dbus_dict_append_basic(iter, "Method",
1149 DBUS_TYPE_STRING, &method);
1152 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1154 struct connman_service *service = user_data;
1157 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1160 switch (service->proxy_config) {
1161 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1163 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1165 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1166 if (service->proxies != NULL)
1167 connman_dbus_dict_append_array(iter, "Servers",
1169 append_proxies, service);
1171 if (service->excludes != NULL)
1172 connman_dbus_dict_append_array(iter, "Excludes",
1174 append_excludes, service);
1176 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1177 if (service->pac != NULL)
1178 connman_dbus_dict_append_basic(iter, "URL",
1179 DBUS_TYPE_STRING, &service->pac);
1183 method = proxymethod2string(service->proxy_config);
1185 connman_dbus_dict_append_basic(iter, "Method",
1186 DBUS_TYPE_STRING, &method);
1189 static void append_provider(DBusMessageIter *iter, void *user_data)
1191 struct connman_service *service = user_data;
1193 DBG("%p %p", service, service->provider);
1195 if (is_connected(service) == FALSE)
1198 if (service->provider != NULL)
1199 __connman_provider_append_properties(service->provider, iter);
1203 static void settings_changed(struct connman_service *service,
1204 struct connman_ipconfig *ipconfig)
1206 connman_dbus_property_changed_dict(service->path,
1207 CONNMAN_SERVICE_INTERFACE, "IPv4",
1208 append_ipv4, service);
1210 connman_dbus_property_changed_dict(service->path,
1211 CONNMAN_SERVICE_INTERFACE, "IPv6",
1212 append_ipv6, service);
1214 __connman_notifier_ipconfig_changed(service, ipconfig);
1217 static void ipv4_configuration_changed(struct connman_service *service)
1219 connman_dbus_property_changed_dict(service->path,
1220 CONNMAN_SERVICE_INTERFACE,
1221 "IPv4.Configuration",
1226 static void ipv6_configuration_changed(struct connman_service *service)
1228 connman_dbus_property_changed_dict(service->path,
1229 CONNMAN_SERVICE_INTERFACE,
1230 "IPv6.Configuration",
1235 static void dns_changed(struct connman_service *service)
1237 connman_dbus_property_changed_array(service->path,
1238 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1239 DBUS_TYPE_STRING, append_dns, service);
1242 static void dns_configuration_changed(struct connman_service *service)
1244 connman_dbus_property_changed_array(service->path,
1245 CONNMAN_SERVICE_INTERFACE,
1246 "Nameservers.Configuration",
1247 DBUS_TYPE_STRING, append_dnsconfig, service);
1249 dns_changed(service);
1252 static void domain_changed(struct connman_service *service)
1254 connman_dbus_property_changed_array(service->path,
1255 CONNMAN_SERVICE_INTERFACE, "Domains",
1256 DBUS_TYPE_STRING, append_domain, service);
1259 static void domain_configuration_changed(struct connman_service *service)
1261 connman_dbus_property_changed_array(service->path,
1262 CONNMAN_SERVICE_INTERFACE,
1263 "Domains.Configuration",
1264 DBUS_TYPE_STRING, append_domainconfig, service);
1267 static void proxy_changed(struct connman_service *service)
1269 connman_dbus_property_changed_dict(service->path,
1270 CONNMAN_SERVICE_INTERFACE, "Proxy",
1271 append_proxy, service);
1274 static void proxy_configuration_changed(struct connman_service *service)
1276 connman_dbus_property_changed_dict(service->path,
1277 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1278 append_proxyconfig, service);
1280 proxy_changed(service);
1283 static void link_changed(struct connman_service *service)
1285 connman_dbus_property_changed_dict(service->path,
1286 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1287 append_ethernet, service);
1290 static void stats_append_counters(DBusMessageIter *dict,
1291 struct connman_stats_data *stats,
1292 struct connman_stats_data *counters,
1293 connman_bool_t append_all)
1295 if (counters->rx_packets != stats->rx_packets || append_all) {
1296 counters->rx_packets = stats->rx_packets;
1297 connman_dbus_dict_append_basic(dict, "RX.Packets",
1298 DBUS_TYPE_UINT32, &stats->rx_packets);
1301 if (counters->tx_packets != stats->tx_packets || append_all) {
1302 counters->tx_packets = stats->tx_packets;
1303 connman_dbus_dict_append_basic(dict, "TX.Packets",
1304 DBUS_TYPE_UINT32, &stats->tx_packets);
1307 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1308 counters->rx_bytes = stats->rx_bytes;
1309 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1310 DBUS_TYPE_UINT32, &stats->rx_bytes);
1313 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1314 counters->tx_bytes = stats->tx_bytes;
1315 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1316 DBUS_TYPE_UINT32, &stats->tx_bytes);
1319 if (counters->rx_errors != stats->rx_errors || append_all) {
1320 counters->rx_errors = stats->rx_errors;
1321 connman_dbus_dict_append_basic(dict, "RX.Errors",
1322 DBUS_TYPE_UINT32, &stats->rx_errors);
1325 if (counters->tx_errors != stats->tx_errors || append_all) {
1326 counters->tx_errors = stats->tx_errors;
1327 connman_dbus_dict_append_basic(dict, "TX.Errors",
1328 DBUS_TYPE_UINT32, &stats->tx_errors);
1331 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1332 counters->rx_dropped = stats->rx_dropped;
1333 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1334 DBUS_TYPE_UINT32, &stats->rx_dropped);
1337 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1338 counters->tx_dropped = stats->tx_dropped;
1339 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1340 DBUS_TYPE_UINT32, &stats->tx_dropped);
1343 if (counters->time != stats->time || append_all) {
1344 counters->time = stats->time;
1345 connman_dbus_dict_append_basic(dict, "Time",
1346 DBUS_TYPE_UINT32, &stats->time);
1350 static void stats_append(struct connman_service *service,
1351 const char *counter,
1352 struct connman_stats_counter *counters,
1353 connman_bool_t append_all)
1355 DBusMessageIter array, dict;
1358 DBG("service %p counter %s", service, counter);
1360 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1364 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1365 &service->path, DBUS_TYPE_INVALID);
1367 dbus_message_iter_init_append(msg, &array);
1370 connman_dbus_dict_open(&array, &dict);
1372 stats_append_counters(&dict, &service->stats.data,
1373 &counters->stats.data, append_all);
1375 connman_dbus_dict_close(&array, &dict);
1377 /* roaming counter */
1378 connman_dbus_dict_open(&array, &dict);
1380 stats_append_counters(&dict, &service->stats_roaming.data,
1381 &counters->stats_roaming.data, append_all);
1383 connman_dbus_dict_close(&array, &dict);
1385 __connman_counter_send_usage(counter, msg);
1388 static void stats_update(struct connman_service *service,
1389 unsigned int rx_packets, unsigned int tx_packets,
1390 unsigned int rx_bytes, unsigned int tx_bytes,
1391 unsigned int rx_errors, unsigned int tx_errors,
1392 unsigned int rx_dropped, unsigned int tx_dropped)
1394 struct connman_stats *stats = stats_get(service);
1395 struct connman_stats_data *data_last = &stats->data_last;
1396 struct connman_stats_data *data = &stats->data;
1397 unsigned int seconds;
1399 DBG("service %p", service);
1401 if (stats->valid == TRUE) {
1403 rx_packets - data_last->rx_packets;
1405 tx_packets - data_last->tx_packets;
1407 rx_bytes - data_last->rx_bytes;
1409 tx_bytes - data_last->tx_bytes;
1411 rx_errors - data_last->rx_errors;
1413 tx_errors - data_last->tx_errors;
1415 rx_dropped - data_last->rx_dropped;
1417 tx_dropped - data_last->tx_dropped;
1419 stats->valid = TRUE;
1422 data_last->rx_packets = rx_packets;
1423 data_last->tx_packets = tx_packets;
1424 data_last->rx_bytes = rx_bytes;
1425 data_last->tx_bytes = tx_bytes;
1426 data_last->rx_errors = rx_errors;
1427 data_last->tx_errors = tx_errors;
1428 data_last->rx_dropped = rx_dropped;
1429 data_last->tx_dropped = tx_dropped;
1431 seconds = g_timer_elapsed(stats->timer, NULL);
1432 stats->data.time = stats->data_last.time + seconds;
1435 void __connman_service_notify(struct connman_service *service,
1436 unsigned int rx_packets, unsigned int tx_packets,
1437 unsigned int rx_bytes, unsigned int tx_bytes,
1438 unsigned int rx_errors, unsigned int tx_errors,
1439 unsigned int rx_dropped, unsigned int tx_dropped)
1441 GHashTableIter iter;
1442 gpointer key, value;
1443 const char *counter;
1444 struct connman_stats_counter *counters;
1445 struct connman_stats_data *data;
1448 if (service == NULL)
1451 if (is_connected(service) == FALSE)
1454 stats_update(service,
1455 rx_packets, tx_packets,
1457 rx_errors, tx_errors,
1458 rx_dropped, tx_dropped);
1460 data = &stats_get(service)->data;
1461 err = __connman_stats_update(service, service->roaming, data);
1463 connman_error("Failed to store statistics for %s",
1464 service->identifier);
1466 g_hash_table_iter_init(&iter, service->counter_table);
1467 while (g_hash_table_iter_next(&iter, &key, &value)) {
1471 stats_append(service, counter, counters, counters->append_all);
1472 counters->append_all = FALSE;
1476 int __connman_service_counter_register(const char *counter)
1478 struct connman_service *service;
1479 GSequenceIter *iter;
1480 struct connman_stats_counter *counters;
1482 DBG("counter %s", counter);
1484 counter_list = g_slist_append(counter_list, (gpointer)counter);
1486 iter = g_sequence_get_begin_iter(service_list);
1488 while (g_sequence_iter_is_end(iter) == FALSE) {
1489 service = g_sequence_get(iter);
1491 counters = g_try_new0(struct connman_stats_counter, 1);
1492 if (counters == NULL)
1495 counters->append_all = TRUE;
1497 g_hash_table_replace(service->counter_table, (gpointer)counter,
1500 iter = g_sequence_iter_next(iter);
1506 void __connman_service_counter_unregister(const char *counter)
1508 struct connman_service *service;
1509 GSequenceIter *iter;
1511 DBG("counter %s", counter);
1513 iter = g_sequence_get_begin_iter(service_list);
1515 while (g_sequence_iter_is_end(iter) == FALSE) {
1516 service = g_sequence_get(iter);
1518 g_hash_table_remove(service->counter_table, counter);
1520 iter = g_sequence_iter_next(iter);
1523 counter_list = g_slist_remove(counter_list, counter);
1526 GSequence *__connman_service_get_list(struct connman_session *session,
1527 service_match_cb service_match,
1528 create_service_entry_cb create_service_entry,
1529 GDestroyNotify destroy_service_entry)
1532 GSequenceIter *iter;
1533 struct connman_service *service;
1534 enum connman_service_state state;
1535 struct service_entry *entry;
1537 list = g_sequence_new(destroy_service_entry);
1541 iter = g_sequence_get_begin_iter(service_list);
1543 while (g_sequence_iter_is_end(iter) == FALSE) {
1544 service = g_sequence_get(iter);
1546 if (service_match(session, service) == TRUE) {
1547 state = combine_state(service->state_ipv4,
1548 service->state_ipv6);
1549 entry = create_service_entry(service, service->name,
1554 g_sequence_append(list, entry);
1557 iter = g_sequence_iter_next(iter);
1563 void __connman_service_session_inc(struct connman_service *service)
1565 DBG("service %p", service);
1567 g_atomic_int_inc(&service->session_usage_count);
1570 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1572 connman_bool_t in_use;
1574 if (g_atomic_int_dec_and_test(&service->session_usage_count) == TRUE)
1579 DBG("service %p last %d", service, in_use);
1584 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1585 struct connman_service *service)
1587 dbus_bool_t required;
1590 str = __connman_service_type2string(service->type);
1592 connman_dbus_dict_append_basic(dict, "Type",
1593 DBUS_TYPE_STRING, &str);
1595 connman_dbus_dict_append_array(dict, "Security",
1596 DBUS_TYPE_STRING, append_security, service);
1598 str = state2string(combine_state(service->state_ipv4,
1599 service->state_ipv6));
1601 connman_dbus_dict_append_basic(dict, "State",
1602 DBUS_TYPE_STRING, &str);
1604 str = error2string(service->error);
1606 connman_dbus_dict_append_basic(dict, "Error",
1607 DBUS_TYPE_STRING, &str);
1609 if (service->strength > 0)
1610 connman_dbus_dict_append_basic(dict, "Strength",
1611 DBUS_TYPE_BYTE, &service->strength);
1613 connman_dbus_dict_append_basic(dict, "Favorite",
1614 DBUS_TYPE_BOOLEAN, &service->favorite);
1616 connman_dbus_dict_append_basic(dict, "Immutable",
1617 DBUS_TYPE_BOOLEAN, &service->immutable);
1619 if (service->favorite == TRUE)
1620 connman_dbus_dict_append_basic(dict, "AutoConnect",
1621 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1623 connman_dbus_dict_append_basic(dict, "AutoConnect",
1624 DBUS_TYPE_BOOLEAN, &service->favorite);
1626 if (service->name != NULL)
1627 connman_dbus_dict_append_basic(dict, "Name",
1628 DBUS_TYPE_STRING, &service->name);
1630 connman_dbus_dict_append_basic(dict, "LoginRequired",
1631 DBUS_TYPE_BOOLEAN, &service->login_required);
1633 switch (service->type) {
1634 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1635 case CONNMAN_SERVICE_TYPE_SYSTEM:
1636 case CONNMAN_SERVICE_TYPE_GPS:
1637 case CONNMAN_SERVICE_TYPE_VPN:
1638 case CONNMAN_SERVICE_TYPE_GADGET:
1640 case CONNMAN_SERVICE_TYPE_CELLULAR:
1641 connman_dbus_dict_append_basic(dict, "Roaming",
1642 DBUS_TYPE_BOOLEAN, &service->roaming);
1645 connman_dbus_dict_append_basic(dict, "SetupRequired",
1646 DBUS_TYPE_BOOLEAN, &required);
1647 connman_dbus_dict_append_dict(dict, "Ethernet",
1648 append_ethernet, service);
1650 case CONNMAN_SERVICE_TYPE_WIFI:
1651 if (service->passphrase != NULL && limited == FALSE)
1652 connman_dbus_dict_append_basic(dict, "Passphrase",
1653 DBUS_TYPE_STRING, &service->passphrase);
1657 switch (service->security) {
1658 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1659 case CONNMAN_SERVICE_SECURITY_NONE:
1661 case CONNMAN_SERVICE_SECURITY_WEP:
1662 case CONNMAN_SERVICE_SECURITY_PSK:
1663 case CONNMAN_SERVICE_SECURITY_WPA:
1664 case CONNMAN_SERVICE_SECURITY_RSN:
1665 if (service->passphrase == NULL)
1668 case CONNMAN_SERVICE_SECURITY_8021X:
1672 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1673 DBUS_TYPE_BOOLEAN, &required);
1676 case CONNMAN_SERVICE_TYPE_ETHERNET:
1677 case CONNMAN_SERVICE_TYPE_WIMAX:
1678 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1679 connman_dbus_dict_append_dict(dict, "Ethernet",
1680 append_ethernet, service);
1684 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1686 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1687 append_ipv4config, service);
1689 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1691 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1692 append_ipv6config, service);
1694 connman_dbus_dict_append_array(dict, "Nameservers",
1695 DBUS_TYPE_STRING, append_dns, service);
1697 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1698 DBUS_TYPE_STRING, append_dnsconfig, service);
1700 connman_dbus_dict_append_array(dict, "Domains",
1701 DBUS_TYPE_STRING, append_domain, service);
1703 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1704 DBUS_TYPE_STRING, append_domainconfig, service);
1706 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1708 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1709 append_proxyconfig, service);
1711 connman_dbus_dict_append_dict(dict, "Provider",
1712 append_provider, service);
1715 static void append_struct(gpointer value, gpointer user_data)
1717 struct connman_service *service = value;
1718 DBusMessageIter *iter = user_data;
1719 DBusMessageIter entry, dict;
1721 if (service->path == NULL || service->hidden == TRUE)
1724 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1726 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1729 connman_dbus_dict_open(&entry, &dict);
1730 append_properties(&dict, TRUE, service);
1731 connman_dbus_dict_close(&entry, &dict);
1733 dbus_message_iter_close_container(iter, &entry);
1736 void __connman_service_list_struct(DBusMessageIter *iter)
1738 g_sequence_foreach(service_list, append_struct, iter);
1741 int __connman_service_get_index(struct connman_service *service)
1743 if (service == NULL)
1746 if (service->network != NULL)
1747 return connman_network_get_index(service->network);
1748 else if (service->provider != NULL)
1749 return connman_provider_get_index(service->provider);
1754 void __connman_service_set_domainname(struct connman_service *service,
1755 const char *domainname)
1757 if (service == NULL)
1760 g_free(service->domainname);
1761 service->domainname = g_strdup(domainname);
1763 domain_changed(service);
1766 const char *connman_service_get_domainname(struct connman_service *service)
1768 if (service == NULL)
1771 if (service->domains != NULL)
1772 return service->domains[0];
1774 return service->domainname;
1777 char **connman_service_get_nameservers(struct connman_service *service)
1779 if (service == NULL)
1782 if (service->nameservers_config != NULL)
1783 return service->nameservers_config;
1784 else if (service->nameservers != NULL)
1785 return service->nameservers;
1790 void connman_service_set_proxy_method(struct connman_service *service,
1791 enum connman_service_proxy_method method)
1793 if (service == NULL)
1796 service->proxy = method;
1798 proxy_changed(service);
1800 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1801 __connman_notifier_proxy_changed(service);
1804 enum connman_service_proxy_method connman_service_get_proxy_method(
1805 struct connman_service *service)
1807 if (service == NULL)
1808 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1810 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1811 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1812 service->pac == NULL)
1813 return service->proxy;
1815 return service->proxy_config;
1818 return service->proxy;
1821 char **connman_service_get_proxy_servers(struct connman_service *service)
1823 return g_strdupv(service->proxies);
1826 char **connman_service_get_proxy_excludes(struct connman_service *service)
1828 return g_strdupv(service->excludes);
1831 const char *connman_service_get_proxy_url(struct connman_service *service)
1833 if (service == NULL)
1836 return service->pac;
1839 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1842 if (service == NULL)
1845 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1847 if (service->ipconfig_ipv4) {
1848 if (__connman_ipconfig_set_proxy_autoconfig(
1849 service->ipconfig_ipv4, url) < 0)
1851 } else if (service->ipconfig_ipv6) {
1852 if (__connman_ipconfig_set_proxy_autoconfig(
1853 service->ipconfig_ipv6, url) < 0)
1858 proxy_changed(service);
1860 __connman_notifier_proxy_changed(service);
1863 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1865 if (service == NULL)
1868 if (service->ipconfig_ipv4)
1869 return __connman_ipconfig_get_proxy_autoconfig(
1870 service->ipconfig_ipv4);
1871 else if (service->ipconfig_ipv6)
1872 return __connman_ipconfig_get_proxy_autoconfig(
1873 service->ipconfig_ipv6);
1877 static void update_timeservers(struct connman_service *service)
1881 if (service->timeservers == NULL)
1884 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
1885 case CONNMAN_SERVICE_STATE_UNKNOWN:
1886 case CONNMAN_SERVICE_STATE_IDLE:
1887 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1888 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1890 case CONNMAN_SERVICE_STATE_FAILURE:
1891 case CONNMAN_SERVICE_STATE_DISCONNECT:
1892 for (i = 0; service->timeservers[i] != NULL; i++)
1893 connman_timeserver_remove(service->timeservers[i]);
1895 case CONNMAN_SERVICE_STATE_READY:
1896 case CONNMAN_SERVICE_STATE_ONLINE:
1900 for (i = 0; service->timeservers[i] != NULL; i++)
1901 connman_timeserver_append(service->timeservers[i]);
1904 int __connman_service_timeserver_append(struct connman_service *service,
1905 const char *timeserver)
1909 DBG("service %p timeserver %s", service, timeserver);
1911 if (timeserver == NULL)
1914 if (service->timeservers != NULL) {
1917 for (i = 0; service->timeservers[i] != NULL; i++)
1918 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1921 len = g_strv_length(service->timeservers);
1922 service->timeservers = g_try_renew(char *, service->timeservers,
1926 service->timeservers = g_try_new0(char *, len + 2);
1929 if (service->timeservers == NULL)
1932 service->timeservers[len] = g_strdup(timeserver);
1933 service->timeservers[len + 1] = NULL;
1935 update_timeservers(service);
1940 int __connman_service_timeserver_remove(struct connman_service *service,
1941 const char *timeserver)
1946 DBG("service %p timeserver %s", service, timeserver);
1948 if (timeserver == NULL)
1951 if (service->timeservers == NULL)
1954 len = g_strv_length(service->timeservers);
1956 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
1959 g_strfreev(service->timeservers);
1960 service->timeservers = NULL;
1965 servers = g_try_new0(char *, len - 1);
1966 if (servers == NULL)
1969 for (i = 0, j = 0; i < len; i++) {
1970 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
1971 servers[j] = g_strdup(service->timeservers[i]);
1975 servers[len - 2] = NULL;
1977 g_strfreev(service->timeservers);
1978 service->timeservers = servers;
1980 update_timeservers(service);
1985 void __connman_service_set_pac(struct connman_service *service,
1991 g_free(service->pac);
1992 service->pac = g_strdup(pac);
1995 void __connman_service_set_identity(struct connman_service *service,
1996 const char *identity)
1998 if (service->immutable)
2001 g_free(service->identity);
2002 service->identity = g_strdup(identity);
2004 if (service->network != NULL)
2005 connman_network_set_string(service->network,
2010 void __connman_service_set_passphrase(struct connman_service *service,
2011 const char* passphrase)
2013 if (service->immutable == TRUE)
2016 g_free(service->passphrase);
2017 service->passphrase = g_strdup(passphrase);
2019 passphrase_changed(service);
2021 if (service->network != NULL)
2022 connman_network_set_string(service->network,
2024 service->passphrase);
2026 __connman_storage_save_service(service);
2029 static DBusMessage *get_properties(DBusConnection *conn,
2030 DBusMessage *msg, void *user_data)
2032 struct connman_service *service = user_data;
2034 DBusMessageIter array, dict;
2036 DBG("service %p", service);
2038 reply = dbus_message_new_method_return(msg);
2042 dbus_message_iter_init_append(reply, &array);
2044 connman_dbus_dict_open(&array, &dict);
2045 append_properties(&dict, FALSE, service);
2046 connman_dbus_dict_close(&array, &dict);
2051 static int update_proxy_configuration(struct connman_service *service,
2052 DBusMessageIter *array)
2054 DBusMessageIter dict;
2055 enum connman_service_proxy_method method;
2056 GString *servers_str = NULL;
2057 GString *excludes_str = NULL;
2058 const char *url = NULL;
2060 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2062 dbus_message_iter_recurse(array, &dict);
2064 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2065 DBusMessageIter entry, variant;
2069 dbus_message_iter_recurse(&dict, &entry);
2071 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2074 dbus_message_iter_get_basic(&entry, &key);
2075 dbus_message_iter_next(&entry);
2077 if (dbus_message_iter_get_arg_type(&entry) !=
2081 dbus_message_iter_recurse(&entry, &variant);
2082 type = dbus_message_iter_get_arg_type(&variant);
2084 if (g_str_equal(key, "Method") == TRUE) {
2087 if (type != DBUS_TYPE_STRING)
2090 dbus_message_iter_get_basic(&variant, &val);
2091 method = string2proxymethod(val);
2092 } else if (g_str_equal(key, "URL") == TRUE) {
2093 if (type != DBUS_TYPE_STRING)
2096 dbus_message_iter_get_basic(&variant, &url);
2097 } else if (g_str_equal(key, "Servers") == TRUE) {
2098 DBusMessageIter str_array;
2100 if (type != DBUS_TYPE_ARRAY)
2103 servers_str = g_string_new(NULL);
2104 if (servers_str == NULL)
2107 dbus_message_iter_recurse(&variant, &str_array);
2109 while (dbus_message_iter_get_arg_type(&str_array) ==
2113 dbus_message_iter_get_basic(&str_array, &val);
2115 if (servers_str->len > 0)
2116 g_string_append_printf(servers_str,
2119 g_string_append(servers_str, val);
2121 dbus_message_iter_next(&str_array);
2123 } else if (g_str_equal(key, "Excludes") == TRUE) {
2124 DBusMessageIter str_array;
2126 if (type != DBUS_TYPE_ARRAY)
2129 excludes_str = g_string_new(NULL);
2130 if (excludes_str == NULL)
2133 dbus_message_iter_recurse(&variant, &str_array);
2135 while (dbus_message_iter_get_arg_type(&str_array) ==
2139 dbus_message_iter_get_basic(&str_array, &val);
2141 if (excludes_str->len > 0)
2142 g_string_append_printf(excludes_str,
2145 g_string_append(excludes_str, val);
2147 dbus_message_iter_next(&str_array);
2151 dbus_message_iter_next(&dict);
2155 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2157 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2158 if (servers_str == NULL && service->proxies == NULL)
2161 if (servers_str != NULL) {
2162 g_strfreev(service->proxies);
2164 if (servers_str->len > 0)
2165 service->proxies = g_strsplit_set(
2166 servers_str->str, " ", 0);
2168 service->proxies = NULL;
2171 if (excludes_str != NULL) {
2172 g_strfreev(service->excludes);
2174 if (excludes_str->len > 0)
2175 service->excludes = g_strsplit_set(
2176 excludes_str->str, " ", 0);
2178 service->excludes = NULL;
2181 if (service->proxies == NULL)
2182 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2185 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2186 g_free(service->pac);
2188 if (url != NULL && strlen(url) > 0)
2189 service->pac = g_strdup(url);
2191 service->pac = NULL;
2193 /* if we are connected:
2194 - if service->pac == NULL
2195 - if __connman_ipconfig_get_proxy_autoconfig(
2196 service->ipconfig) == NULL
2197 --> We should start WPAD */
2200 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2204 if (servers_str != NULL)
2205 g_string_free(servers_str, TRUE);
2207 if (excludes_str != NULL)
2208 g_string_free(excludes_str, TRUE);
2210 service->proxy_config = method;
2215 if (servers_str != NULL)
2216 g_string_free(servers_str, TRUE);
2218 if (excludes_str != NULL)
2219 g_string_free(excludes_str, TRUE);
2224 static int set_ipconfig(struct connman_service *service,
2225 struct connman_ipconfig *ipconfig,
2226 DBusMessageIter *array,
2227 enum connman_service_state state,
2228 enum connman_service_state *new_state)
2230 enum connman_ipconfig_method old_method;
2231 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2232 enum connman_ipconfig_type type;
2235 old_method = __connman_ipconfig_get_method(ipconfig);
2237 if (is_connecting_state(service, state) ||
2238 is_connected_state(service, state))
2239 __connman_network_clear_ipconfig(service->network, ipconfig);
2241 err = __connman_ipconfig_set_config(ipconfig, array);
2242 method = __connman_ipconfig_get_method(ipconfig);
2243 type = __connman_ipconfig_get_config_type(ipconfig);
2245 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2246 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2247 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2248 *new_state = service->state_ipv4 =
2249 CONNMAN_SERVICE_STATE_CONFIGURATION;
2250 __connman_ipconfig_enable(ipconfig);
2253 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2254 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2255 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2256 *new_state = service->state_ipv6;
2257 __connman_ipconfig_enable(ipconfig);
2261 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2262 type, method, state2string(*new_state));
2267 static DBusMessage *set_property(DBusConnection *conn,
2268 DBusMessage *msg, void *user_data)
2270 struct connman_service *service = user_data;
2271 DBusMessageIter iter, value;
2275 DBG("service %p", service);
2277 if (dbus_message_iter_init(msg, &iter) == FALSE)
2278 return __connman_error_invalid_arguments(msg);
2280 dbus_message_iter_get_basic(&iter, &name);
2281 dbus_message_iter_next(&iter);
2282 dbus_message_iter_recurse(&iter, &value);
2284 type = dbus_message_iter_get_arg_type(&value);
2286 if (g_str_equal(name, "AutoConnect") == TRUE) {
2287 connman_bool_t autoconnect;
2289 if (type != DBUS_TYPE_BOOLEAN)
2290 return __connman_error_invalid_arguments(msg);
2292 if (service->favorite == FALSE)
2293 return __connman_error_invalid_service(msg);
2295 dbus_message_iter_get_basic(&value, &autoconnect);
2297 if (service->autoconnect == autoconnect)
2298 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2300 service->autoconnect = autoconnect;
2302 autoconnect_changed(service);
2304 __connman_storage_save_service(service);
2305 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2306 const char *passphrase;
2308 if (type != DBUS_TYPE_STRING)
2309 return __connman_error_invalid_arguments(msg);
2311 if (service->immutable == TRUE)
2312 return __connman_error_not_supported(msg);
2314 dbus_message_iter_get_basic(&value, &passphrase);
2316 __connman_service_set_passphrase(service, passphrase);
2317 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2318 DBusMessageIter entry;
2323 if (type != DBUS_TYPE_ARRAY)
2324 return __connman_error_invalid_arguments(msg);
2326 str = g_string_new(NULL);
2328 return __connman_error_invalid_arguments(msg);
2330 index = connman_network_get_index(service->network);
2331 gw = __connman_ipconfig_get_gateway_from_index(index);
2333 if (gw && strlen(gw))
2334 __connman_service_nameserver_del_routes(service);
2336 dbus_message_iter_recurse(&value, &entry);
2338 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2340 dbus_message_iter_get_basic(&entry, &val);
2341 dbus_message_iter_next(&entry);
2343 g_string_append_printf(str, " %s", val);
2345 g_string_append(str, val);
2348 g_strfreev(service->nameservers_config);
2351 service->nameservers_config =
2352 g_strsplit_set(str->str, " ", 0);
2354 service->nameservers_config = NULL;
2357 g_string_free(str, TRUE);
2359 if (gw && strlen(gw))
2360 __connman_service_nameserver_add_routes(service, gw);
2362 update_nameservers(service);
2363 dns_configuration_changed(service);
2365 __connman_storage_save_service(service);
2366 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2367 DBusMessageIter entry;
2370 if (type != DBUS_TYPE_ARRAY)
2371 return __connman_error_invalid_arguments(msg);
2373 str = g_string_new(NULL);
2375 return __connman_error_invalid_arguments(msg);
2377 dbus_message_iter_recurse(&value, &entry);
2379 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2381 dbus_message_iter_get_basic(&entry, &val);
2382 dbus_message_iter_next(&entry);
2384 g_string_append_printf(str, " %s", val);
2386 g_string_append(str, val);
2389 g_strfreev(service->domains);
2392 service->domains = g_strsplit_set(str->str, " ", 0);
2394 service->domains = NULL;
2396 g_string_free(str, TRUE);
2398 update_nameservers(service);
2399 domain_configuration_changed(service);
2401 __connman_storage_save_service(service);
2402 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2405 if (type != DBUS_TYPE_ARRAY)
2406 return __connman_error_invalid_arguments(msg);
2408 err = update_proxy_configuration(service, &value);
2411 return __connman_error_failed(msg, -err);
2413 proxy_configuration_changed(service);
2415 __connman_storage_save_service(service);
2416 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2417 g_str_equal(name, "IPv6.Configuration")) {
2419 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2420 enum connman_service_state state =
2421 CONNMAN_SERVICE_STATE_UNKNOWN;
2426 if (service->ipconfig_ipv4 == NULL &&
2427 service->ipconfig_ipv6 == NULL)
2428 return __connman_error_invalid_property(msg);
2430 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2431 ipv4 = service->ipconfig_ipv4;
2432 err = set_ipconfig(service, ipv4, &value,
2433 service->state_ipv4, &state);
2435 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2436 ipv6 = service->ipconfig_ipv6;
2437 err = set_ipconfig(service, ipv6, &value,
2438 service->state_ipv6, &state);
2442 if (is_connected_state(service, state) ||
2443 is_connecting_state(service, state))
2444 __connman_network_set_ipconfig(service->network,
2446 return __connman_error_failed(msg, -err);
2450 ipv4_configuration_changed(service);
2452 ipv6_configuration_changed(service);
2454 if (is_connecting(service) || is_connected(service))
2455 __connman_network_set_ipconfig(service->network,
2458 __connman_storage_save_service(service);
2460 return __connman_error_invalid_property(msg);
2462 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2465 static void set_idle(struct connman_service *service)
2467 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
2468 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2469 state_changed(service);
2472 static DBusMessage *clear_property(DBusConnection *conn,
2473 DBusMessage *msg, void *user_data)
2475 struct connman_service *service = user_data;
2478 DBG("service %p", service);
2480 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2483 if (g_str_equal(name, "Error") == TRUE) {
2486 g_get_current_time(&service->modified);
2487 __connman_storage_save_service(service);
2488 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2489 if (service->immutable == TRUE)
2490 return __connman_error_not_supported(msg);
2492 g_free(service->passphrase);
2493 service->passphrase = NULL;
2495 passphrase_changed(service);
2497 __connman_storage_save_service(service);
2499 return __connman_error_invalid_property(msg);
2501 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2504 static connman_bool_t is_ignore(struct connman_service *service)
2506 if (service->autoconnect == FALSE)
2509 if (service->roaming == TRUE)
2512 if (service->ignore == TRUE)
2515 if (combine_state(service->state_ipv4, service->state_ipv6) ==
2516 CONNMAN_SERVICE_STATE_FAILURE)
2522 void __connman_service_auto_connect(void)
2524 struct connman_service *service = NULL;
2525 GSequenceIter *iter;
2529 if (__connman_session_mode() == TRUE) {
2530 DBG("Session mode enabled: auto connect disabled");
2534 iter = g_sequence_get_begin_iter(service_list);
2536 while (g_sequence_iter_is_end(iter) == FALSE) {
2537 service = g_sequence_get(iter);
2539 if (service->pending != NULL)
2542 if (is_connecting(service) == TRUE)
2545 if (service->favorite == FALSE)
2548 if (is_connected(service) == TRUE)
2551 if (is_ignore(service) == FALSE &&
2552 combine_state(service->state_ipv4,
2553 service->state_ipv6) ==
2554 CONNMAN_SERVICE_STATE_IDLE)
2559 iter = g_sequence_iter_next(iter);
2562 if (service != NULL) {
2563 service->userconnect = FALSE;
2564 __connman_service_connect(service);
2568 static void remove_timeout(struct connman_service *service)
2570 if (service->timeout > 0) {
2571 g_source_remove(service->timeout);
2572 service->timeout = 0;
2576 static void reply_pending(struct connman_service *service, int error)
2578 remove_timeout(service);
2580 if (service->pending != NULL) {
2584 reply = __connman_error_failed(service->pending,
2587 g_dbus_send_message(connection, reply);
2591 sender = dbus_message_get_interface(service->pending);
2593 DBG("sender %s", sender);
2595 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2596 g_dbus_send_reply(connection, service->pending,
2597 DBUS_TYPE_OBJECT_PATH, &service->path,
2600 g_dbus_send_reply(connection, service->pending,
2604 dbus_message_unref(service->pending);
2605 service->pending = NULL;
2609 static gboolean connect_timeout(gpointer user_data)
2611 struct connman_service *service = user_data;
2612 connman_bool_t autoconnect = FALSE;
2614 DBG("service %p", service);
2616 service->timeout = 0;
2618 if (service->network != NULL)
2619 __connman_network_disconnect(service->network);
2621 __connman_ipconfig_disable(service->ipconfig_ipv4);
2622 __connman_ipconfig_disable(service->ipconfig_ipv6);
2624 __connman_stats_service_unregister(service);
2626 if (service->pending != NULL) {
2629 reply = __connman_error_operation_timeout(service->pending);
2631 g_dbus_send_message(connection, reply);
2633 dbus_message_unref(service->pending);
2634 service->pending = NULL;
2638 __connman_service_indicate_state(service,
2639 CONNMAN_SERVICE_STATE_FAILURE,
2640 CONNMAN_IPCONFIG_TYPE_IPV4);
2641 __connman_service_indicate_state(service,
2642 CONNMAN_SERVICE_STATE_FAILURE,
2643 CONNMAN_IPCONFIG_TYPE_IPV6);
2645 if (autoconnect == TRUE && service->userconnect == FALSE)
2646 __connman_service_auto_connect();
2651 static void set_reconnect_state(struct connman_service *service,
2652 connman_bool_t reconnect)
2654 struct connman_device *device;
2656 if (service->network == NULL)
2659 device = connman_network_get_device(service->network);
2663 __connman_device_set_reconnect(device, reconnect);
2666 static connman_bool_t get_reconnect_state(struct connman_service *service)
2668 struct connman_device *device;
2670 if (service->network == NULL)
2673 device = connman_network_get_device(service->network);
2677 return __connman_device_get_reconnect(device);
2680 static void request_input_cb (struct connman_service *service,
2681 const char *identity, const char *passphrase,
2684 DBG ("RequestInput return, %p", service);
2686 if (identity == NULL && passphrase == NULL && service->wps == FALSE)
2689 if (identity != NULL)
2690 __connman_service_set_identity(service, identity);
2692 if (passphrase != NULL)
2693 __connman_service_set_passphrase(service, passphrase);
2695 __connman_service_connect(service);
2698 static DBusMessage *connect_service(DBusConnection *conn,
2699 DBusMessage *msg, void *user_data)
2701 struct connman_service *service = user_data;
2702 GSequenceIter *iter;
2705 DBG("service %p", service);
2707 if (service->pending != NULL)
2708 return __connman_error_in_progress(msg);
2710 iter = g_sequence_get_begin_iter(service_list);
2712 while (g_sequence_iter_is_end(iter) == FALSE) {
2713 struct connman_service *temp = g_sequence_get(iter);
2715 if (service->type == temp->type && is_connecting(temp) == TRUE)
2716 return __connman_error_in_progress(msg);
2718 iter = g_sequence_iter_next(iter);
2721 service->ignore = FALSE;
2723 service->userconnect = TRUE;
2725 service->pending = dbus_message_ref(msg);
2727 set_reconnect_state(service, FALSE);
2729 err = __connman_service_connect(service);
2731 if (err == -ENOKEY) {
2732 if (__connman_agent_request_input(service,
2738 if (service->pending == NULL)
2741 if (err != -EINPROGRESS) {
2742 dbus_message_unref(service->pending);
2743 service->pending = NULL;
2745 return __connman_error_failed(msg, -err);
2751 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2754 static DBusMessage *disconnect_service(DBusConnection *conn,
2755 DBusMessage *msg, void *user_data)
2757 struct connman_service *service = user_data;
2760 DBG("service %p", service);
2762 reply_pending(service, ECONNABORTED);
2764 service->ignore = TRUE;
2766 set_reconnect_state(service, FALSE);
2768 err = __connman_service_disconnect(service);
2770 if (err != -EINPROGRESS)
2771 return __connman_error_failed(msg, -err);
2776 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2779 static DBusMessage *remove_service(DBusConnection *conn,
2780 DBusMessage *msg, void *user_data)
2782 struct connman_service *service = user_data;
2784 DBG("service %p", service);
2786 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2787 return __connman_error_not_supported(msg);
2789 if (service->immutable == TRUE)
2790 return __connman_error_not_supported(msg);
2792 if (service->favorite == FALSE &&
2793 combine_state(service->state_ipv4, service->state_ipv6) !=
2794 CONNMAN_SERVICE_STATE_FAILURE)
2795 return __connman_error_not_supported(msg);
2797 if (service->network != NULL) {
2798 set_reconnect_state(service, FALSE);
2800 __connman_network_disconnect(service->network);
2803 g_free(service->passphrase);
2804 service->passphrase = NULL;
2806 passphrase_changed(service);
2810 __connman_service_set_favorite(service, FALSE);
2811 __connman_storage_save_service(service);
2813 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2816 static DBusMessage *move_service(DBusConnection *conn,
2817 DBusMessage *msg, void *user_data,
2820 struct connman_service *service = user_data;
2821 struct connman_service *target;
2823 GSequenceIter *src, *dst;
2825 DBG("service %p", service);
2827 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2830 if (service->favorite == FALSE)
2831 return __connman_error_not_supported(msg);
2833 target = find_service(path);
2834 if (target == NULL || target->favorite == FALSE || target == service)
2835 return __connman_error_invalid_service(msg);
2837 DBG("target %s", target->identifier);
2839 if (target->state_ipv4 != service->state_ipv4 &&
2840 target->state_ipv6 != service->state_ipv6)
2841 return __connman_error_invalid_service(msg);
2843 g_get_current_time(&service->modified);
2844 __connman_storage_save_service(service);
2846 src = g_hash_table_lookup(service_hash, service->identifier);
2847 dst = g_hash_table_lookup(service_hash, target->identifier);
2849 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2851 __connman_profile_changed(FALSE);
2853 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2856 static DBusMessage *move_before(DBusConnection *conn,
2857 DBusMessage *msg, void *user_data)
2859 return move_service(conn, msg, user_data, TRUE);
2862 static DBusMessage *move_after(DBusConnection *conn,
2863 DBusMessage *msg, void *user_data)
2865 return move_service(conn, msg, user_data, FALSE);
2868 static DBusMessage *reset_counters(DBusConnection *conn,
2869 DBusMessage *msg, void *user_data)
2871 struct connman_service *service = user_data;
2873 reset_stats(service);
2875 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2878 static GDBusMethodTable service_methods[] = {
2879 { "GetProperties", "", "a{sv}", get_properties },
2880 { "SetProperty", "sv", "", set_property },
2881 { "ClearProperty", "s", "", clear_property },
2882 { "Connect", "", "", connect_service,
2883 G_DBUS_METHOD_FLAG_ASYNC },
2884 { "Disconnect", "", "", disconnect_service },
2885 { "Remove", "", "", remove_service },
2886 { "MoveBefore", "o", "", move_before },
2887 { "MoveAfter", "o", "", move_after },
2888 { "ResetCounters", "", "", reset_counters },
2892 static GDBusSignalTable service_signals[] = {
2893 { "PropertyChanged", "sv" },
2897 static void service_free(gpointer user_data)
2899 struct connman_service *service = user_data;
2900 char *path = service->path;
2902 DBG("service %p", service);
2904 reply_pending(service, ENOENT);
2906 g_hash_table_remove(service_hash, service->identifier);
2908 __connman_notifier_service_remove(service);
2910 stats_stop(service);
2911 __connman_storage_save_service(service);
2913 service->path = NULL;
2916 __connman_profile_changed(FALSE);
2918 g_dbus_unregister_interface(connection, path,
2919 CONNMAN_SERVICE_INTERFACE);
2923 g_hash_table_destroy(service->counter_table);
2925 if (service->network != NULL)
2926 connman_network_unref(service->network);
2928 if (service->provider != NULL)
2929 connman_provider_unref(service->provider);
2931 if (service->ipconfig_ipv4 != NULL) {
2932 connman_ipconfig_unref(service->ipconfig_ipv4);
2933 service->ipconfig_ipv4 = NULL;
2936 if (service->ipconfig_ipv6 != NULL) {
2937 connman_ipconfig_unref(service->ipconfig_ipv6);
2938 service->ipconfig_ipv6 = NULL;
2941 if (service->location != NULL)
2942 connman_location_unref(service->location);
2944 g_strfreev(service->nameservers);
2945 g_strfreev(service->nameservers_config);
2946 g_strfreev(service->domains);
2947 g_strfreev(service->proxies);
2948 g_strfreev(service->excludes);
2950 g_free(service->domainname);
2951 g_free(service->pac);
2952 g_free(service->profile);
2953 g_free(service->name);
2954 g_free(service->passphrase);
2955 g_free(service->identifier);
2956 g_free(service->eap);
2957 g_free(service->identity);
2958 g_free(service->ca_cert_file);
2959 g_free(service->client_cert_file);
2960 g_free(service->private_key_file);
2961 g_free(service->private_key_passphrase);
2962 g_free(service->phase2);
2964 if (service->stats.timer != NULL)
2965 g_timer_destroy(service->stats.timer);
2966 if (service->stats_roaming.timer != NULL)
2967 g_timer_destroy(service->stats_roaming.timer);
2973 * __connman_service_put:
2974 * @service: service structure
2976 * Release service if no longer needed
2978 void __connman_service_put(struct connman_service *service)
2980 DBG("service %p", service);
2982 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2983 GSequenceIter *iter;
2985 iter = g_hash_table_lookup(service_hash, service->identifier);
2987 reply_pending(service, ECONNABORTED);
2989 __connman_service_disconnect(service);
2991 g_sequence_remove(iter);
2993 service_free(service);
2997 static void stats_init(struct connman_service *service)
3000 service->stats.valid = FALSE;
3001 service->stats.enabled = FALSE;
3002 service->stats.timer = g_timer_new();
3005 service->stats_roaming.valid = FALSE;
3006 service->stats_roaming.enabled = FALSE;
3007 service->stats_roaming.timer = g_timer_new();
3010 static void service_initialize(struct connman_service *service)
3012 DBG("service %p", service);
3014 service->refcount = 1;
3015 service->session_usage_count = 0;
3017 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3018 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3020 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3021 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3023 service->favorite = FALSE;
3024 service->immutable = FALSE;
3025 service->hidden = FALSE;
3027 service->ignore = FALSE;
3029 service->userconnect = FALSE;
3033 stats_init(service);
3035 service->provider = NULL;
3037 service->wps = FALSE;
3041 * connman_service_create:
3043 * Allocate a new service.
3045 * Returns: a newly-allocated #connman_service structure
3047 struct connman_service *connman_service_create(void)
3050 struct connman_stats_counter *counters;
3051 const char *counter;
3053 struct connman_service *service;
3055 service = g_try_new0(struct connman_service, 1);
3056 if (service == NULL)
3059 DBG("service %p", service);
3061 service->counter_table = g_hash_table_new_full(g_str_hash,
3062 g_str_equal, NULL, g_free);
3064 for (list = counter_list; list; list = list->next) {
3065 counter = list->data;
3067 counters = g_try_new0(struct connman_stats_counter, 1);
3068 if (counters == NULL) {
3069 g_hash_table_destroy(service->counter_table);
3074 counters->append_all = TRUE;
3076 g_hash_table_replace(service->counter_table, (gpointer)counter,
3080 service_initialize(service);
3082 service->location = __connman_location_create(service);
3087 struct connman_location *__connman_service_get_location(struct connman_service *service)
3089 return service->location;
3093 * connman_service_ref:
3094 * @service: service structure
3096 * Increase reference counter of service
3098 struct connman_service *connman_service_ref(struct connman_service *service)
3102 g_atomic_int_inc(&service->refcount);
3108 * connman_service_unref:
3109 * @service: service structure
3111 * Decrease reference counter of service
3113 void connman_service_unref(struct connman_service *service)
3115 __connman_service_put(service);
3118 static gint service_compare(gconstpointer a, gconstpointer b,
3121 struct connman_service *service_a = (void *) a;
3122 struct connman_service *service_b = (void *) b;
3123 enum connman_service_state state_a, state_b;
3125 state_a = combine_state(service_a->state_ipv4, service_a->state_ipv6);
3126 state_b = combine_state(service_b->state_ipv4, service_b->state_ipv6);
3128 if (state_a != state_b) {
3129 if (is_connected(service_a) == TRUE)
3131 if (is_connected(service_b) == TRUE)
3134 if (is_connecting(service_a) == TRUE)
3136 if (is_connecting(service_b) == TRUE)
3140 if (service_a->order > service_b->order)
3143 if (service_a->order < service_b->order)
3146 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3149 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3152 if (service_a->type != service_b->type) {
3153 switch (service_a->type) {
3154 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3155 case CONNMAN_SERVICE_TYPE_SYSTEM:
3156 case CONNMAN_SERVICE_TYPE_ETHERNET:
3157 case CONNMAN_SERVICE_TYPE_GPS:
3158 case CONNMAN_SERVICE_TYPE_VPN:
3159 case CONNMAN_SERVICE_TYPE_GADGET:
3161 case CONNMAN_SERVICE_TYPE_WIFI:
3163 case CONNMAN_SERVICE_TYPE_WIMAX:
3164 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3165 case CONNMAN_SERVICE_TYPE_CELLULAR:
3170 return (gint) service_b->strength - (gint) service_a->strength;
3174 * connman_service_get_type:
3175 * @service: service structure
3177 * Get the type of service
3179 enum connman_service_type connman_service_get_type(struct connman_service *service)
3181 if (service == NULL)
3182 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3184 return service->type;
3188 * connman_service_get_interface:
3189 * @service: service structure
3191 * Get network interface of service
3193 char *connman_service_get_interface(struct connman_service *service)
3197 if (service == NULL)
3200 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3201 if (service->ipconfig_ipv4)
3202 index = connman_ipconfig_get_index(
3203 service->ipconfig_ipv4);
3204 else if (service->ipconfig_ipv6)
3205 index = connman_ipconfig_get_index(
3206 service->ipconfig_ipv6);
3210 return connman_inet_ifname(index);
3213 if (service->network == NULL)
3216 index = connman_network_get_index(service->network);
3218 return connman_inet_ifname(index);
3222 * connman_service_get_network:
3223 * @service: service structure
3225 * Get the service network
3227 struct connman_network *
3228 __connman_service_get_network(struct connman_service *service)
3230 if (service == NULL)
3233 return service->network;
3236 struct connman_ipconfig *
3237 __connman_service_get_ip4config(struct connman_service *service)
3239 if (service == NULL)
3242 return service->ipconfig_ipv4;
3245 struct connman_ipconfig *
3246 __connman_service_get_ip6config(struct connman_service *service)
3248 if (service == NULL)
3251 return service->ipconfig_ipv6;
3254 struct connman_ipconfig *
3255 __connman_service_get_ipconfig(struct connman_service *service, int family)
3257 if (family == AF_INET)
3258 return __connman_service_get_ip4config(service);
3259 else if (family == AF_INET6)
3260 return __connman_service_get_ip6config(service);
3266 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3268 if (service == NULL)
3269 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3271 return service->security;
3274 const char *__connman_service_get_phase2(struct connman_service *service)
3276 if (service == NULL)
3279 return service->phase2;
3282 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3284 if (service == NULL)
3287 return service->wps;
3291 * __connman_service_set_favorite:
3292 * @service: service structure
3293 * @favorite: favorite value
3295 * Change the favorite setting of service
3297 int __connman_service_set_favorite(struct connman_service *service,
3298 connman_bool_t favorite)
3300 GSequenceIter *iter;
3302 iter = g_hash_table_lookup(service_hash, service->identifier);
3306 if (service->favorite == favorite)
3309 service->favorite = favorite;
3311 favorite_changed(service);
3313 g_sequence_sort_changed(iter, service_compare, NULL);
3315 __connman_profile_changed(FALSE);
3320 connman_bool_t __connman_service_get_immutable(struct connman_service *service)
3322 if (service == NULL)
3325 return service->immutable;
3328 int __connman_service_set_immutable(struct connman_service *service,
3329 connman_bool_t immutable)
3331 service->immutable = immutable;
3333 immutable_changed(service);
3338 void __connman_service_set_string(struct connman_service *service,
3339 const char *key, const char *value)
3341 if (g_str_equal(key, "EAP") == TRUE) {
3342 g_free(service->eap);
3343 service->eap = g_strdup(value);
3344 } else if (g_str_equal(key, "Identity") == TRUE) {
3345 g_free(service->identity);
3346 service->identity = g_strdup(value);
3347 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3348 g_free(service->ca_cert_file);
3349 service->ca_cert_file = g_strdup(value);
3350 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3351 g_free(service->client_cert_file);
3352 service->client_cert_file = g_strdup(value);
3353 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3354 g_free(service->private_key_file);
3355 service->private_key_file = g_strdup(value);
3356 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3357 g_free(service->private_key_passphrase);
3358 service->private_key_passphrase = g_strdup(value);
3359 } else if (g_str_equal(key, "Phase2") == TRUE) {
3360 g_free(service->phase2);
3361 service->phase2 = g_strdup(value);
3362 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3363 g_free(service->passphrase);
3364 service->passphrase = g_strdup(value);
3368 static void service_complete(struct connman_service *service)
3370 reply_pending(service, EIO);
3372 if (service->userconnect == FALSE)
3373 __connman_service_auto_connect();
3375 g_get_current_time(&service->modified);
3376 __connman_storage_save_service(service);
3379 static void report_error_cb(struct connman_service *service,
3380 gboolean retry, void *user_data)
3383 __connman_service_connect(service);
3385 service_complete(service);
3386 __connman_profile_changed(FALSE);
3387 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3391 int __connman_service_set_ipconfig_ready(struct connman_service *service,
3392 enum connman_ipconfig_type type)
3394 enum connman_service_state state;
3397 DBG("service %p (%s) type %d (%s)",
3398 service, service ? service->identifier : NULL,
3399 type, __connman_ipconfig_type2string(type));
3401 if (service == NULL)
3404 state = combine_state(service->state_ipv4, service->state_ipv6);
3406 if (state == CONNMAN_SERVICE_STATE_READY ||
3407 state == CONNMAN_SERVICE_STATE_ONLINE) {
3410 err = __connman_service_indicate_state(service,
3411 CONNMAN_SERVICE_STATE_READY, type);
3417 int __connman_service_indicate_state(struct connman_service *service,
3418 enum connman_service_state new_state,
3419 enum connman_ipconfig_type type)
3421 enum connman_service_state service_state,
3422 state = CONNMAN_SERVICE_STATE_IDLE;
3423 GSequenceIter *iter;
3425 if (service == NULL)
3428 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3429 if (service->state_ipv4 == new_state)
3431 state = combine_state(new_state, service->state_ipv6);
3432 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3433 if (service->state_ipv6 == new_state)
3435 state = combine_state(new_state, service->state_ipv4);
3439 service_state = combine_state(service->state_ipv4,
3440 service->state_ipv6);
3442 DBG("service %p state %s/%s => %s new %s/%d => %s",
3444 state2string(service->state_ipv4),
3445 state2string(service->state_ipv6),
3446 state2string(service_state),
3447 state2string(new_state),
3449 state2string(state));
3451 if (service_state == CONNMAN_SERVICE_STATE_FAILURE &&
3452 state == CONNMAN_SERVICE_STATE_IDLE)
3455 if (service_state == CONNMAN_SERVICE_STATE_IDLE &&
3456 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3459 if (state == CONNMAN_SERVICE_STATE_IDLE &&
3460 service_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3461 state_changed(service);
3463 reply_pending(service, ECONNABORTED);
3465 __connman_service_disconnect(service);
3468 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3469 if (__connman_stats_service_register(service) == 0) {
3470 __connman_stats_get(service, FALSE,
3471 &service->stats.data);
3472 __connman_stats_get(service, TRUE,
3473 &service->stats_roaming.data);
3476 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
3477 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3478 __connman_ipconfig_enable(service->ipconfig_ipv4);
3479 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
3480 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3481 __connman_ipconfig_enable(service->ipconfig_ipv6);
3484 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3485 service->state_ipv4 = new_state;
3486 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3487 service->state_ipv6 = new_state;
3489 if (state == service_state)
3492 state_changed(service);
3494 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3495 if (service->login_required == TRUE) {
3496 service->login_required = FALSE;
3497 login_changed(service);
3500 connman_timeserver_sync();
3503 if (state == CONNMAN_SERVICE_STATE_IDLE) {
3504 connman_bool_t reconnect;
3506 reconnect = get_reconnect_state(service);
3507 if (reconnect == TRUE)
3508 __connman_service_auto_connect();
3511 if (state == CONNMAN_SERVICE_STATE_READY) {
3512 enum connman_service_proxy_method proxy_config;
3513 enum connman_ipconfig_method method;
3515 set_reconnect_state(service, TRUE);
3517 __connman_service_set_favorite(service, TRUE);
3519 reply_pending(service, 0);
3521 service->userconnect = FALSE;
3523 g_get_current_time(&service->modified);
3524 __connman_storage_save_service(service);
3526 update_nameservers(service);
3527 dns_changed(service);
3528 domain_changed(service);
3530 proxy_config = service->proxy_config;
3533 * We start WPAD if we haven't got a PAC URL from DHCP and
3534 * if our proxy manual configuration is either empty or set
3535 * to AUTO with an empty URL.
3537 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3538 (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3540 CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3541 service->pac == NULL)))
3542 if (__connman_wpad_start(service) < 0) {
3544 CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3546 __connman_notifier_proxy_changed(service);
3549 __connman_notifier_connect(service->type);
3551 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3552 connman_network_get_bool(service->network,
3553 "WiFi.UseWPS") == TRUE) {
3556 pass = connman_network_get_string(service->network,
3559 __connman_service_set_passphrase(service, pass);
3561 connman_network_set_bool(service->network,
3562 "WiFi.UseWPS", FALSE);
3567 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3568 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3569 __connman_ipconfig_disable_ipv6(
3570 service->ipconfig_ipv6);
3572 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3573 __connman_location_finish(service);
3577 __connman_wpad_stop(service);
3579 update_nameservers(service);
3580 dns_changed(service);
3581 domain_changed(service);
3583 __connman_notifier_disconnect(service->type);
3586 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3587 if (service->userconnect == TRUE &&
3588 __connman_agent_report_error(service,
3589 error2string(service->error),
3590 report_error_cb, NULL) == -EIO)
3592 service_complete(service);
3594 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3596 iter = g_hash_table_lookup(service_hash, service->identifier);
3598 g_sequence_sort_changed(iter, service_compare, NULL);
3600 __connman_profile_changed(FALSE);
3602 service_state = combine_state(service->state_ipv4,
3603 service->state_ipv6);
3604 if (service_state == CONNMAN_SERVICE_STATE_ONLINE)
3607 if (service_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3608 struct connman_service *def_service = get_default();
3610 if (__connman_notifier_count_connected() == 0 &&
3611 def_service != NULL &&
3612 def_service->provider != NULL)
3613 __connman_provider_disconnect(def_service->provider);
3616 if (service_state == CONNMAN_SERVICE_STATE_IDLE ||
3617 service_state == CONNMAN_SERVICE_STATE_FAILURE)
3618 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3623 int __connman_service_indicate_error(struct connman_service *service,
3624 enum connman_service_error error)
3626 DBG("service %p error %d", service, error);
3628 if (service == NULL)
3631 service->error = error;
3633 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3634 __connman_service_set_passphrase(service, NULL);
3636 return __connman_service_indicate_state(service,
3637 CONNMAN_SERVICE_STATE_FAILURE,
3638 CONNMAN_IPCONFIG_TYPE_IPV4);
3641 int __connman_service_clear_error(struct connman_service *service)
3643 enum connman_service_state state;
3645 DBG("service %p", service);
3647 if (service == NULL)
3650 state = combine_state(service->state_ipv4, service->state_ipv6);
3652 if (state != CONNMAN_SERVICE_STATE_FAILURE)
3655 service->state_ipv4 = service->state_ipv6 =
3656 CONNMAN_SERVICE_STATE_UNKNOWN;
3657 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3659 if (service->favorite == TRUE)
3660 set_reconnect_state(service, TRUE);
3662 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
3663 CONNMAN_IPCONFIG_TYPE_IPV6);
3666 * Toggling the IPv6 state to IDLE could trigger the auto connect
3667 * machinery and consequently the IPv4 state.
3669 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3670 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3673 return __connman_service_indicate_state(service,
3674 CONNMAN_SERVICE_STATE_IDLE,
3675 CONNMAN_IPCONFIG_TYPE_IPV4);
3678 int __connman_service_indicate_default(struct connman_service *service)
3680 DBG("service %p", service);
3684 __connman_location_detect(service);
3689 int __connman_service_request_login(struct connman_service *service)
3691 DBG("service %p", service);
3693 if (service == NULL)
3696 service->login_required = TRUE;
3697 login_changed(service);
3702 static connman_bool_t prepare_network(struct connman_service *service)
3704 enum connman_network_type type;
3705 unsigned int ssid_len;
3707 type = connman_network_get_type(service->network);
3710 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3711 case CONNMAN_NETWORK_TYPE_VENDOR:
3713 case CONNMAN_NETWORK_TYPE_WIFI:
3714 if (connman_network_get_blob(service->network, "WiFi.SSID",
3718 if (service->passphrase != NULL)
3719 connman_network_set_string(service->network,
3720 "WiFi.Passphrase", service->passphrase);
3722 case CONNMAN_NETWORK_TYPE_ETHERNET:
3723 case CONNMAN_NETWORK_TYPE_WIMAX:
3724 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3725 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3726 case CONNMAN_NETWORK_TYPE_CELLULAR:
3733 static void prepare_8021x(struct connman_service *service)
3735 if (service->eap != NULL)
3736 connman_network_set_string(service->network, "WiFi.EAP",
3739 if (service->identity != NULL)
3740 connman_network_set_string(service->network, "WiFi.Identity",
3743 if (service->ca_cert_file != NULL)
3744 connman_network_set_string(service->network, "WiFi.CACertFile",
3745 service->ca_cert_file);
3747 if (service->client_cert_file != NULL)
3748 connman_network_set_string(service->network,
3749 "WiFi.ClientCertFile",
3750 service->client_cert_file);
3752 if (service->private_key_file != NULL)
3753 connman_network_set_string(service->network,
3754 "WiFi.PrivateKeyFile",
3755 service->private_key_file);
3757 if (service->private_key_passphrase != NULL)
3758 connman_network_set_string(service->network,
3759 "WiFi.PrivateKeyPassphrase",
3760 service->private_key_passphrase);
3762 if (service->phase2 != NULL)
3763 connman_network_set_string(service->network, "WiFi.Phase2",
3767 static int service_connect(struct connman_service *service)
3771 switch (service->type) {
3772 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3773 case CONNMAN_SERVICE_TYPE_SYSTEM:
3774 case CONNMAN_SERVICE_TYPE_GPS:
3775 case CONNMAN_SERVICE_TYPE_GADGET:
3777 case CONNMAN_SERVICE_TYPE_ETHERNET:
3778 case CONNMAN_SERVICE_TYPE_WIMAX:
3779 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3780 case CONNMAN_SERVICE_TYPE_CELLULAR:
3781 case CONNMAN_SERVICE_TYPE_VPN:
3783 case CONNMAN_SERVICE_TYPE_WIFI:
3784 switch (service->security) {
3785 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3786 case CONNMAN_SERVICE_SECURITY_NONE:
3788 case CONNMAN_SERVICE_SECURITY_WEP:
3789 case CONNMAN_SERVICE_SECURITY_PSK:
3790 case CONNMAN_SERVICE_SECURITY_WPA:
3791 case CONNMAN_SERVICE_SECURITY_RSN:
3792 if (service->passphrase == NULL) {
3793 if (service->network == NULL)
3796 if (service->wps == FALSE ||
3797 connman_network_get_bool(
3799 "WiFi.UseWPS") == FALSE)
3803 case CONNMAN_SERVICE_SECURITY_8021X:
3804 if (service->eap == NULL)
3808 * never request credentials if using EAP-TLS
3809 * (EAP-TLS networks need to be fully provisioned)
3811 if (g_str_equal(service->eap, "tls") == TRUE)
3814 if (service->immutable != TRUE)
3822 if (service->network != NULL) {
3823 if (prepare_network(service) == FALSE)
3826 switch (service->security) {
3827 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3828 case CONNMAN_SERVICE_SECURITY_NONE:
3829 case CONNMAN_SERVICE_SECURITY_WEP:
3830 case CONNMAN_SERVICE_SECURITY_PSK:
3831 case CONNMAN_SERVICE_SECURITY_WPA:
3832 case CONNMAN_SERVICE_SECURITY_RSN:
3834 case CONNMAN_SERVICE_SECURITY_8021X:
3835 prepare_8021x(service);
3839 if (__connman_stats_service_register(service) == 0) {
3840 __connman_stats_get(service, FALSE,
3841 &service->stats.data);
3842 __connman_stats_get(service, TRUE,
3843 &service->stats_roaming.data);
3846 if (service->ipconfig_ipv4)
3847 __connman_ipconfig_enable(service->ipconfig_ipv4);
3848 if (service->ipconfig_ipv6)
3849 __connman_ipconfig_enable(service->ipconfig_ipv6);
3851 err = __connman_network_connect(service->network);
3852 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3853 service->provider != NULL)
3854 err = __connman_provider_connect(service->provider);
3859 if (err != -EINPROGRESS) {
3860 __connman_ipconfig_disable(service->ipconfig_ipv4);
3861 __connman_ipconfig_disable(service->ipconfig_ipv6);
3862 __connman_stats_service_unregister(service);
3870 int __connman_service_connect(struct connman_service *service)
3872 enum connman_service_state state;
3875 state = combine_state(service->state_ipv4, service->state_ipv6);
3877 DBG("service %p state %s", service, state2string(state));
3879 if (is_connected(service) == TRUE)
3882 if (is_connecting(service) == TRUE)
3885 switch (service->type) {
3886 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3887 case CONNMAN_SERVICE_TYPE_SYSTEM:
3888 case CONNMAN_SERVICE_TYPE_GPS:
3889 case CONNMAN_SERVICE_TYPE_GADGET:
3892 err = service_connect(service);
3898 if (err == -EINPROGRESS) {
3899 if (service->timeout == 0)
3900 service->timeout = g_timeout_add_seconds(
3901 CONNECT_TIMEOUT, connect_timeout, service);
3903 return -EINPROGRESS;
3909 if (service->userconnect == TRUE)
3910 reply_pending(service, err);
3912 __connman_service_indicate_state(service,
3913 CONNMAN_SERVICE_STATE_FAILURE,
3914 CONNMAN_IPCONFIG_TYPE_IPV4);
3915 __connman_service_indicate_state(service,
3916 CONNMAN_SERVICE_STATE_FAILURE,
3917 CONNMAN_IPCONFIG_TYPE_IPV6);
3922 int __connman_service_disconnect(struct connman_service *service)
3926 DBG("service %p", service);
3928 if (service->network != NULL) {
3929 err = __connman_network_disconnect(service->network);
3930 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3931 service->provider != NULL)
3932 err = __connman_provider_disconnect(service->provider);
3936 if (err < 0 && err != -EINPROGRESS)
3939 __connman_6to4_remove(service->ipconfig_ipv4);
3941 if (service->ipconfig_ipv4)
3942 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3945 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3948 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3949 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3951 __connman_ipconfig_disable(service->ipconfig_ipv4);
3952 __connman_ipconfig_disable(service->ipconfig_ipv6);
3954 __connman_stats_service_unregister(service);
3959 int __connman_service_disconnect_all(void)
3961 GSequenceIter *iter;
3965 iter = g_sequence_get_begin_iter(service_list);
3967 while (g_sequence_iter_is_end(iter) == FALSE) {
3968 struct connman_service *service = g_sequence_get(iter);
3970 service->ignore = TRUE;
3972 set_reconnect_state(service, FALSE);
3974 __connman_service_disconnect(service);
3976 iter = g_sequence_iter_next(iter);
3984 * __connman_service_lookup:
3985 * @pattern: search pattern
3986 * @path: return object path
3988 * Look up a service path from a search pattern
3990 int __connman_service_lookup(const char *pattern, const char **path)
3992 GHashTableIter iter;
3993 gpointer key, value;
3995 g_hash_table_iter_init(&iter, service_hash);
3997 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
3998 GSequenceIter *iter = value;
3999 struct connman_service *service = g_sequence_get(iter);
4001 if (g_strcmp0(service->identifier, pattern) == 0 ||
4002 g_strcmp0(service->name, pattern) == 0) {
4003 *path = (const char *) service->path;
4012 * lookup_by_identifier:
4013 * @identifier: service identifier
4015 * Look up a service by identifier (reference count will not be increased)
4017 static struct connman_service *lookup_by_identifier(const char *identifier)
4019 GSequenceIter *iter;
4021 iter = g_hash_table_lookup(service_hash, identifier);
4023 return g_sequence_get(iter);
4028 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4029 const char *ssid, const char *mode, const char *security)
4031 struct connman_network *network;
4034 unsigned int i, ssid_len;
4036 ssid_len = strlen(ssid);
4040 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
4041 if (network == NULL)
4044 connman_network_set_blob(network, "WiFi.SSID",
4045 (unsigned char *) ssid, ssid_len);
4047 connman_network_set_string(network, "WiFi.Mode", mode);
4048 connman_network_set_string(network, "WiFi.Security", security);
4050 name = g_try_malloc0(ssid_len + 1);
4052 connman_network_unref(network);
4056 for (i = 0; i < ssid_len; i++) {
4057 if (g_ascii_isprint(ssid[i]))
4063 connman_network_set_name(network, name);
4067 index = connman_device_get_index(device);
4068 connman_network_set_index(network, index);
4070 if (connman_device_add_network(device, network) < 0) {
4071 connman_network_unref(network);
4075 connman_network_set_available(network, TRUE);
4080 int __connman_service_create_and_connect(DBusMessage *msg)
4082 struct connman_service *service;
4083 struct connman_network *network;
4084 struct connman_device *device;
4085 DBusMessageIter iter, array;
4086 const char *mode = "managed", *security = "none", *group_security;
4087 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4088 unsigned int ssid_len = 0;
4091 gboolean created = FALSE;
4094 dbus_message_iter_init(msg, &iter);
4095 dbus_message_iter_recurse(&iter, &array);
4097 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4098 DBusMessageIter entry, value;
4101 dbus_message_iter_recurse(&array, &entry);
4102 dbus_message_iter_get_basic(&entry, &key);
4104 dbus_message_iter_next(&entry);
4105 dbus_message_iter_recurse(&entry, &value);
4107 switch (dbus_message_iter_get_arg_type(&value)) {
4108 case DBUS_TYPE_STRING:
4109 if (g_str_equal(key, "Type") == TRUE)
4110 dbus_message_iter_get_basic(&value, &type);
4111 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4112 g_str_equal(key, "Mode") == TRUE)
4113 dbus_message_iter_get_basic(&value, &mode);
4114 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4115 g_str_equal(key, "Security") == TRUE)
4116 dbus_message_iter_get_basic(&value, &security);
4117 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4118 g_str_equal(key, "Passphrase") == TRUE)
4119 dbus_message_iter_get_basic(&value, &passphrase);
4120 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4121 g_str_equal(key, "SSID") == TRUE)
4122 dbus_message_iter_get_basic(&value, &ssid);
4125 dbus_message_iter_next(&array);
4131 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4137 ssid_len = strlen(ssid);
4141 if (g_strcmp0(security, "none") != 0 &&
4142 g_strcmp0(security, "wep") != 0 &&
4143 g_strcmp0(security, "psk") != 0 &&
4144 g_strcmp0(security, "wpa") != 0 &&
4145 g_strcmp0(security, "rsn") != 0 &&
4146 g_strcmp0(security, "ieee8021x") != 0)
4149 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4153 ident = connman_device_get_ident(device);
4158 if (!g_strcmp0(security, "wpa") ||
4159 !g_strcmp0(security, "rsn"))
4160 group_security = "psk";
4162 group_security = security;
4164 group = connman_wifi_build_group_name((unsigned char *) ssid,
4165 ssid_len, mode, group_security);
4169 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4171 service = lookup_by_identifier(name);
4173 if (service != NULL)
4176 network = create_hidden_wifi(device, ssid, mode, security);
4177 if (network != NULL) {
4178 connman_network_set_group(network, group);
4182 service = lookup_by_identifier(name);
4188 if (service == NULL) {
4193 if (is_connected(service) == TRUE) {
4198 if (is_connecting(service) == TRUE) {
4203 set_reconnect_state(service, FALSE);
4205 __connman_device_disconnect(device);
4207 if (passphrase != NULL) {
4208 g_free(service->passphrase);
4209 service->passphrase = g_strdup(passphrase);
4212 service->userconnect = TRUE;
4214 err = __connman_service_connect(service);
4215 if (err < 0 && err != -EINPROGRESS)
4218 service->pending = dbus_message_ref(msg);
4223 if (service != NULL && created == TRUE) {
4224 struct connman_network *network = service->network;
4226 if (network != NULL) {
4227 connman_network_set_available(network, FALSE);
4228 __connman_device_cleanup_networks(device);
4230 __connman_service_put(service);
4236 int __connman_service_provision(DBusMessage *msg)
4238 GKeyFile *keyfile = NULL;
4239 const char *config_str = NULL;
4240 char *group = NULL, *ident = NULL;
4242 struct connman_service *service;
4246 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4249 if (config_str == NULL || strlen(config_str) == 0)
4252 keyfile = g_key_file_new();
4254 /* populate GKeyFile with config_str */
4255 if (g_key_file_load_from_data(keyfile, config_str,
4256 strlen(config_str), 0, NULL) == FALSE) {
4262 * read only one group of settings (only one service supported, no
4265 group = g_key_file_get_start_group(keyfile);
4267 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4272 err = __connman_config_load_service(keyfile, group, TRUE);
4276 ident = group + strlen("service_");
4278 /* trigger service provisioning if service exists */
4279 service = lookup_by_identifier(ident);
4280 if (service != NULL)
4281 __connman_config_provision_service(service);
4283 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4289 if (keyfile != NULL)
4290 g_key_file_free(keyfile);
4296 * __connman_service_get:
4297 * @identifier: service identifier
4299 * Look up a service by identifier or create a new one if not found
4301 static struct connman_service *service_get(const char *identifier)
4303 struct connman_service *service;
4304 GSequenceIter *iter;
4306 iter = g_hash_table_lookup(service_hash, identifier);
4308 service = g_sequence_get(iter);
4309 if (service != NULL)
4310 connman_service_ref(service);
4314 service = connman_service_create();
4315 if (service == NULL)
4318 DBG("service %p", service);
4320 service->identifier = g_strdup(identifier);
4322 service->profile = g_strdup(__connman_profile_active_ident());
4324 iter = g_sequence_insert_sorted(service_list, service,
4325 service_compare, NULL);
4327 g_hash_table_insert(service_hash, service->identifier, iter);
4332 static int service_register(struct connman_service *service)
4334 const char *path = __connman_profile_active_path();
4335 GSequenceIter *iter;
4337 DBG("service %p", service);
4339 if (service->path != NULL)
4342 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4344 DBG("path %s", service->path);
4346 __connman_config_provision_service(service);
4348 __connman_storage_load_service(service);
4350 g_dbus_register_interface(connection, service->path,
4351 CONNMAN_SERVICE_INTERFACE,
4352 service_methods, service_signals,
4353 NULL, service, NULL);
4355 iter = g_hash_table_lookup(service_hash, service->identifier);
4357 g_sequence_sort_changed(iter, service_compare, NULL);
4359 __connman_profile_changed(TRUE);
4364 static void service_up(struct connman_ipconfig *ipconfig)
4366 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4368 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4370 link_changed(service);
4372 service->stats.valid = FALSE;
4373 service->stats_roaming.valid = FALSE;
4376 static void service_down(struct connman_ipconfig *ipconfig)
4378 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4381 static void service_lower_up(struct connman_ipconfig *ipconfig)
4383 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4385 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4387 stats_start(service);
4390 static void service_lower_down(struct connman_ipconfig *ipconfig)
4392 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4394 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4396 stats_stop(service);
4397 __connman_storage_save_service(service);
4400 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4402 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4403 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4404 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4406 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4408 type = __connman_ipconfig_get_config_type(ipconfig);
4409 method = __connman_ipconfig_get_method(ipconfig);
4411 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4414 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4415 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4416 __connman_service_set_ipconfig_ready(service,
4417 CONNMAN_IPCONFIG_TYPE_IPV6);
4419 settings_changed(service, ipconfig);
4422 static void service_ip_release(struct connman_ipconfig *ipconfig)
4424 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4425 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4426 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4428 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4430 type = __connman_ipconfig_get_config_type(ipconfig);
4431 method = __connman_ipconfig_get_method(ipconfig);
4433 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4436 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4437 method == CONNMAN_IPCONFIG_METHOD_OFF)
4438 __connman_service_indicate_state(service,
4439 CONNMAN_SERVICE_STATE_DISCONNECT,
4440 CONNMAN_IPCONFIG_TYPE_IPV6);
4442 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4443 method == CONNMAN_IPCONFIG_METHOD_OFF)
4444 __connman_service_indicate_state(service,
4445 CONNMAN_SERVICE_STATE_DISCONNECT,
4446 CONNMAN_IPCONFIG_TYPE_IPV4);
4448 settings_changed(service, ipconfig);
4451 static const struct connman_ipconfig_ops service_ops = {
4453 .down = service_down,
4454 .lower_up = service_lower_up,
4455 .lower_down = service_lower_down,
4456 .ip_bound = service_ip_bound,
4457 .ip_release = service_ip_release,
4460 static void setup_ip4config(struct connman_service *service, int index)
4465 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4466 CONNMAN_IPCONFIG_TYPE_IPV4);
4467 if (service->ipconfig_ipv4 == NULL)
4470 connman_ipconfig_set_method(service->ipconfig_ipv4,
4471 CONNMAN_IPCONFIG_METHOD_DHCP);
4473 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4475 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4478 static void setup_ip6config(struct connman_service *service, int index)
4483 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4484 CONNMAN_IPCONFIG_TYPE_IPV6);
4485 if (service->ipconfig_ipv6 == NULL)
4488 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4490 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4493 void __connman_service_create_ip4config(struct connman_service *service,
4496 const char *ident = service->profile;
4499 DBG("ipv4 %p", service->ipconfig_ipv4);
4501 if (service->ipconfig_ipv4 != NULL)
4504 setup_ip4config(service, index);
4509 keyfile = __connman_storage_open_profile(ident);
4510 if (keyfile == NULL)
4513 if (service->ipconfig_ipv4)
4514 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4515 service->identifier, "IPv4.");
4516 g_key_file_free(keyfile);
4519 void __connman_service_create_ip6config(struct connman_service *service,
4522 const char *ident = service->profile;
4525 DBG("ipv6 %p", service->ipconfig_ipv6);
4527 if (service->ipconfig_ipv6 != NULL)
4530 setup_ip6config(service, index);
4535 keyfile = __connman_storage_open_profile(ident);
4536 if (keyfile == NULL)
4539 if (service->ipconfig_ipv6 != NULL)
4540 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4541 service->identifier, "IPv6.");
4543 g_key_file_free(keyfile);
4547 * __connman_service_lookup_from_network:
4548 * @network: network structure
4550 * Look up a service by network (reference count will not be increased)
4552 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4554 struct connman_service *service;
4555 const char *ident, *group;
4558 DBG("network %p", network);
4560 ident = __connman_network_get_ident(network);
4564 group = connman_network_get_group(network);
4568 name = g_strdup_printf("%s_%s_%s",
4569 __connman_network_get_type(network), ident, group);
4570 service = lookup_by_identifier(name);
4576 struct connman_service *__connman_service_lookup_from_index(int index)
4578 struct connman_service *service;
4579 GSequenceIter *iter;
4581 iter = g_sequence_get_begin_iter(service_list);
4583 while (g_sequence_iter_is_end(iter) == FALSE) {
4584 service = g_sequence_get(iter);
4586 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4590 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4594 iter = g_sequence_iter_next(iter);
4600 const char *__connman_service_get_ident(struct connman_service *service)
4602 return service->identifier;
4605 const char *__connman_service_get_path(struct connman_service *service)
4607 return service->path;
4610 unsigned int __connman_service_get_order(struct connman_service *service)
4612 GSequenceIter *iter;
4614 if (service == NULL)
4617 if (service->favorite == FALSE) {
4622 iter = g_hash_table_lookup(service_hash, service->identifier);
4624 if (g_sequence_iter_get_position(iter) == 0)
4626 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4627 service->order = 10;
4633 return service->order;
4636 static enum connman_service_type convert_network_type(struct connman_network *network)
4638 enum connman_network_type type = connman_network_get_type(network);
4641 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4642 case CONNMAN_NETWORK_TYPE_VENDOR:
4644 case CONNMAN_NETWORK_TYPE_ETHERNET:
4645 return CONNMAN_SERVICE_TYPE_ETHERNET;
4646 case CONNMAN_NETWORK_TYPE_WIFI:
4647 return CONNMAN_SERVICE_TYPE_WIFI;
4648 case CONNMAN_NETWORK_TYPE_WIMAX:
4649 return CONNMAN_SERVICE_TYPE_WIMAX;
4650 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4651 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4652 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4653 case CONNMAN_NETWORK_TYPE_CELLULAR:
4654 return CONNMAN_SERVICE_TYPE_CELLULAR;
4657 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4660 static enum connman_service_security convert_wifi_security(const char *security)
4662 if (security == NULL)
4663 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4664 else if (g_str_equal(security, "none") == TRUE)
4665 return CONNMAN_SERVICE_SECURITY_NONE;
4666 else if (g_str_equal(security, "wep") == TRUE)
4667 return CONNMAN_SERVICE_SECURITY_WEP;
4668 else if (g_str_equal(security, "psk") == TRUE)
4669 return CONNMAN_SERVICE_SECURITY_PSK;
4670 else if (g_str_equal(security, "ieee8021x") == TRUE)
4671 return CONNMAN_SERVICE_SECURITY_8021X;
4672 else if (g_str_equal(security, "wpa") == TRUE)
4673 return CONNMAN_SERVICE_SECURITY_WPA;
4674 else if (g_str_equal(security, "rsn") == TRUE)
4675 return CONNMAN_SERVICE_SECURITY_RSN;
4677 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4680 static void update_from_network(struct connman_service *service,
4681 struct connman_network *network)
4683 connman_uint8_t strength = service->strength;
4684 GSequenceIter *iter;
4687 DBG("service %p network %p", service, network);
4689 if (is_connected(service) == TRUE)
4692 if (is_connecting(service) == TRUE)
4695 str = connman_network_get_string(network, "Name");
4697 g_free(service->name);
4698 service->name = g_strdup(str);
4699 service->hidden = FALSE;
4701 g_free(service->name);
4702 service->name = NULL;
4703 service->hidden = TRUE;
4706 service->strength = connman_network_get_uint8(network, "Strength");
4707 service->roaming = connman_network_get_bool(network, "Roaming");
4709 if (service->strength == 0) {
4711 * Filter out 0-values; it's unclear what they mean
4712 * and they cause anomalous sorting of the priority list.
4714 service->strength = strength;
4717 str = connman_network_get_string(network, "WiFi.Security");
4718 service->security = convert_wifi_security(str);
4720 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4721 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4723 if (service->strength > strength && service->network != NULL) {
4724 connman_network_unref(service->network);
4725 service->network = connman_network_ref(network);
4727 strength_changed(service);
4730 if (service->network == NULL)
4731 service->network = connman_network_ref(network);
4733 iter = g_hash_table_lookup(service_hash, service->identifier);
4735 g_sequence_sort_changed(iter, service_compare, NULL);
4739 * __connman_service_create_from_network:
4740 * @network: network structure
4742 * Look up service by network and if not found, create one
4744 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4746 struct connman_service *service;
4747 struct connman_device *device;
4748 const char *ident, *group;
4752 DBG("network %p", network);
4754 ident = __connman_network_get_ident(network);
4758 group = connman_network_get_group(network);
4762 name = g_strdup_printf("%s_%s_%s",
4763 __connman_network_get_type(network), ident, group);
4764 service = service_get(name);
4767 if (service == NULL)
4770 if (__connman_network_get_weakness(network) == TRUE)
4773 if (service->path != NULL) {
4774 update_from_network(service, network);
4775 __connman_profile_changed(TRUE);
4779 service->type = convert_network_type(network);
4781 switch (service->type) {
4782 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4783 case CONNMAN_SERVICE_TYPE_SYSTEM:
4784 case CONNMAN_SERVICE_TYPE_ETHERNET:
4785 case CONNMAN_SERVICE_TYPE_WIMAX:
4786 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4787 case CONNMAN_SERVICE_TYPE_GPS:
4788 case CONNMAN_SERVICE_TYPE_VPN:
4789 case CONNMAN_SERVICE_TYPE_GADGET:
4790 service->autoconnect = FALSE;
4792 case CONNMAN_SERVICE_TYPE_WIFI:
4793 case CONNMAN_SERVICE_TYPE_CELLULAR:
4794 service->autoconnect = TRUE;
4798 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4800 update_from_network(service, network);
4802 index = connman_network_get_index(network);
4804 if (service->ipconfig_ipv4 == NULL)
4805 setup_ip4config(service, index);
4807 if (service->ipconfig_ipv6 == NULL)
4808 setup_ip6config(service, index);
4810 service_register(service);
4812 if (service->favorite == TRUE) {
4813 device = connman_network_get_device(service->network);
4814 if (device && __connman_device_scanning(device) == FALSE)
4815 __connman_service_auto_connect();
4818 __connman_notifier_service_add(service);
4823 void __connman_service_update_from_network(struct connman_network *network)
4825 struct connman_service *service;
4826 connman_uint8_t strength;
4827 connman_bool_t roaming;
4828 GSequenceIter *iter;
4830 connman_bool_t stats_enable;
4832 DBG("network %p", network);
4834 service = __connman_service_lookup_from_network(network);
4835 if (service == NULL)
4838 if (service->network == NULL)
4841 name = connman_network_get_string(service->network, "Name");
4842 if (g_strcmp0(service->name, name) != 0) {
4843 g_free(service->name);
4844 service->name = g_strdup(name);
4845 connman_dbus_property_changed_basic(service->path,
4846 CONNMAN_SERVICE_INTERFACE, "Name",
4847 DBUS_TYPE_STRING, &service->name);
4850 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4851 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4853 strength = connman_network_get_uint8(service->network, "Strength");
4854 if (strength == service->strength)
4857 service->strength = strength;
4859 strength_changed(service);
4862 roaming = connman_network_get_bool(service->network, "Roaming");
4863 if (roaming == service->roaming)
4866 stats_enable = stats_enabled(service);
4867 if (stats_enable == TRUE)
4868 stats_stop(service);
4870 service->roaming = roaming;
4872 if (stats_enable == TRUE)
4873 stats_start(service);
4875 roaming_changed(service);
4877 iter = g_hash_table_lookup(service_hash, service->identifier);
4879 g_sequence_sort_changed(iter, service_compare, NULL);
4882 void __connman_service_remove_from_network(struct connman_network *network)
4884 struct connman_service *service;
4886 DBG("network %p", network);
4888 service = __connman_service_lookup_from_network(network);
4889 if (service == NULL)
4892 __connman_service_put(service);
4896 * __connman_service_create_from_provider:
4897 * @provider: provider structure
4899 * Look up service by provider and if not found, create one
4901 struct connman_service *
4902 __connman_service_create_from_provider(struct connman_provider *provider)
4904 struct connman_service *service;
4905 const char *ident, *str;
4907 int index = connman_provider_get_index(provider);
4909 DBG("provider %p", provider);
4911 ident = __connman_provider_get_ident(provider);
4915 name = g_strdup_printf("vpn_%s", ident);
4916 service = service_get(name);
4919 if (service == NULL)
4922 service->type = CONNMAN_SERVICE_TYPE_VPN;
4923 service->provider = connman_provider_ref(provider);
4924 service->autoconnect = FALSE;
4926 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4928 str = connman_provider_get_string(provider, "Name");
4930 g_free(service->name);
4931 service->name = g_strdup(str);
4932 service->hidden = FALSE;
4934 g_free(service->name);
4935 service->name = NULL;
4936 service->hidden = TRUE;
4939 service->strength = 0;
4941 if (service->ipconfig_ipv4 == NULL)
4942 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4943 CONNMAN_IPCONFIG_TYPE_IPV4);
4944 if (service->ipconfig_ipv4 == NULL)
4947 connman_ipconfig_set_method(service->ipconfig_ipv4,
4948 CONNMAN_IPCONFIG_METHOD_MANUAL);
4949 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4950 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4952 if (service->ipconfig_ipv6 == NULL)
4953 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4954 CONNMAN_IPCONFIG_TYPE_IPV6);
4955 if (service->ipconfig_ipv6 == NULL)
4958 connman_ipconfig_set_method(service->ipconfig_ipv6,
4959 CONNMAN_IPCONFIG_METHOD_OFF);
4960 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4961 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4963 service_register(service);
4965 __connman_notifier_service_add(service);
4970 static int service_load(struct connman_service *service)
4972 const char *ident = service->profile;
4974 GError *error = NULL;
4975 gchar *pathname, *data = NULL;
4978 connman_bool_t autoconnect;
4979 unsigned int ssid_len;
4982 DBG("service %p", service);
4987 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4988 if (pathname == NULL)
4991 keyfile = g_key_file_new();
4993 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
5000 if (g_key_file_load_from_data(keyfile, data, length,
5001 0, NULL) == FALSE) {
5008 switch (service->type) {
5009 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5010 case CONNMAN_SERVICE_TYPE_SYSTEM:
5011 case CONNMAN_SERVICE_TYPE_ETHERNET:
5012 case CONNMAN_SERVICE_TYPE_GPS:
5013 case CONNMAN_SERVICE_TYPE_VPN:
5014 case CONNMAN_SERVICE_TYPE_GADGET:
5016 case CONNMAN_SERVICE_TYPE_WIFI:
5017 if (service->name == NULL) {
5020 name = g_key_file_get_string(keyfile,
5021 service->identifier, "Name", NULL);
5023 g_free(service->name);
5024 service->name = name;
5027 if (service->network != NULL)
5028 connman_network_set_name(service->network,
5032 if (service->network &&
5033 connman_network_get_blob(service->network,
5034 "WiFi.SSID", &ssid_len) == NULL) {
5037 hex_ssid = g_key_file_get_string(keyfile,
5038 service->identifier,
5041 if (hex_ssid != NULL) {
5043 unsigned int i, j = 0, hex;
5044 size_t hex_ssid_len = strlen(hex_ssid);
5046 ssid = g_try_malloc0(hex_ssid_len / 2);
5053 for (i = 0; i < hex_ssid_len; i += 2) {
5054 sscanf(hex_ssid + i, "%02x", &hex);
5058 connman_network_set_blob(service->network,
5059 "WiFi.SSID", ssid, hex_ssid_len / 2);
5066 case CONNMAN_SERVICE_TYPE_WIMAX:
5067 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5068 case CONNMAN_SERVICE_TYPE_CELLULAR:
5069 service->favorite = g_key_file_get_boolean(keyfile,
5070 service->identifier, "Favorite", NULL);
5072 autoconnect = g_key_file_get_boolean(keyfile,
5073 service->identifier, "AutoConnect", &error);
5075 service->autoconnect = autoconnect;
5076 g_clear_error(&error);
5078 str = g_key_file_get_string(keyfile,
5079 service->identifier, "Failure", NULL);
5081 if (service->favorite == FALSE)
5082 service->state_ipv4 = service->state_ipv6 =
5083 CONNMAN_SERVICE_STATE_FAILURE;
5084 service->error = string2error(str);
5089 str = g_key_file_get_string(keyfile,
5090 service->identifier, "Modified", NULL);
5092 g_time_val_from_iso8601(str, &service->modified);
5096 str = g_key_file_get_string(keyfile,
5097 service->identifier, "Passphrase", NULL);
5099 g_free(service->passphrase);
5100 service->passphrase = str;
5103 if (service->ipconfig_ipv4 != NULL)
5104 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5105 service->identifier, "IPv4.");
5107 if (service->ipconfig_ipv6 != NULL)
5108 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5109 service->identifier, "IPv6.");
5111 service->nameservers_config = g_key_file_get_string_list(keyfile,
5112 service->identifier, "Nameservers", &length, NULL);
5113 if (service->nameservers_config != NULL && length == 0) {
5114 g_strfreev(service->nameservers_config);
5115 service->nameservers_config = NULL;
5118 service->domains = g_key_file_get_string_list(keyfile,
5119 service->identifier, "Domains", &length, NULL);
5120 if (service->domains != NULL && length == 0) {
5121 g_strfreev(service->domains);
5122 service->domains = NULL;
5125 str = g_key_file_get_string(keyfile,
5126 service->identifier, "Proxy.Method", NULL);
5128 service->proxy_config = string2proxymethod(str);
5132 service->proxies = g_key_file_get_string_list(keyfile,
5133 service->identifier, "Proxy.Servers", &length, NULL);
5134 if (service->proxies != NULL && length == 0) {
5135 g_strfreev(service->proxies);
5136 service->proxies = NULL;
5139 service->excludes = g_key_file_get_string_list(keyfile,
5140 service->identifier, "Proxy.Excludes", &length, NULL);
5141 if (service->excludes != NULL && length == 0) {
5142 g_strfreev(service->excludes);
5143 service->excludes = NULL;
5146 str = g_key_file_get_string(keyfile,
5147 service->identifier, "Proxy.URL", NULL);
5149 g_free(service->pac);
5154 g_key_file_free(keyfile);
5159 static int service_save(struct connman_service *service)
5161 const char *ident = service->profile;
5163 gchar *pathname, *data = NULL;
5166 const char *cst_str = NULL;
5169 DBG("service %p", service);
5174 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5175 if (pathname == NULL)
5178 keyfile = g_key_file_new();
5180 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5184 if (g_key_file_load_from_data(keyfile, data, length,
5192 if (service->name != NULL)
5193 g_key_file_set_string(keyfile, service->identifier,
5194 "Name", service->name);
5196 switch (service->type) {
5197 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5198 case CONNMAN_SERVICE_TYPE_SYSTEM:
5199 case CONNMAN_SERVICE_TYPE_ETHERNET:
5200 case CONNMAN_SERVICE_TYPE_GPS:
5201 case CONNMAN_SERVICE_TYPE_VPN:
5202 case CONNMAN_SERVICE_TYPE_GADGET:
5204 case CONNMAN_SERVICE_TYPE_WIFI:
5205 if (service->network) {
5206 const unsigned char *ssid;
5207 unsigned int ssid_len = 0;
5209 ssid = connman_network_get_blob(service->network,
5210 "WiFi.SSID", &ssid_len);
5212 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5213 char *identifier = service->identifier;
5217 str = g_string_sized_new(ssid_len * 2);
5223 for (i = 0; i < ssid_len; i++)
5224 g_string_append_printf(str,
5227 g_key_file_set_string(keyfile, identifier,
5230 g_string_free(str, TRUE);
5235 case CONNMAN_SERVICE_TYPE_WIMAX:
5236 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5237 case CONNMAN_SERVICE_TYPE_CELLULAR:
5238 g_key_file_set_boolean(keyfile, service->identifier,
5239 "Favorite", service->favorite);
5241 if (service->favorite == TRUE)
5242 g_key_file_set_boolean(keyfile, service->identifier,
5243 "AutoConnect", service->autoconnect);
5245 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5246 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5247 const char *failure = error2string(service->error);
5248 if (failure != NULL)
5249 g_key_file_set_string(keyfile,
5250 service->identifier,
5251 "Failure", failure);
5253 g_key_file_remove_key(keyfile, service->identifier,
5259 str = g_time_val_to_iso8601(&service->modified);
5261 g_key_file_set_string(keyfile, service->identifier,
5266 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5267 g_key_file_set_string(keyfile, service->identifier,
5268 "Passphrase", service->passphrase);
5270 g_key_file_remove_key(keyfile, service->identifier,
5271 "Passphrase", NULL);
5273 if (service->ipconfig_ipv4 != NULL)
5274 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5275 service->identifier, "IPv4.");
5277 if (service->ipconfig_ipv6 != NULL)
5278 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5279 service->identifier, "IPv6.");
5281 if (service->nameservers_config != NULL) {
5282 guint len = g_strv_length(service->nameservers_config);
5284 g_key_file_set_string_list(keyfile, service->identifier,
5286 (const gchar **) service->nameservers_config, len);
5288 g_key_file_remove_key(keyfile, service->identifier,
5289 "Nameservers", NULL);
5291 if (service->domains != NULL) {
5292 guint len = g_strv_length(service->domains);
5294 g_key_file_set_string_list(keyfile, service->identifier,
5296 (const gchar **) service->domains, len);
5298 g_key_file_remove_key(keyfile, service->identifier,
5301 cst_str = proxymethod2string(service->proxy_config);
5302 if (cst_str != NULL)
5303 g_key_file_set_string(keyfile, service->identifier,
5304 "Proxy.Method", cst_str);
5306 if (service->proxies != NULL) {
5307 guint len = g_strv_length(service->proxies);
5309 g_key_file_set_string_list(keyfile, service->identifier,
5311 (const gchar **) service->proxies, len);
5313 g_key_file_remove_key(keyfile, service->identifier,
5314 "Proxy.Servers", NULL);
5316 if (service->excludes != NULL) {
5317 guint len = g_strv_length(service->excludes);
5319 g_key_file_set_string_list(keyfile, service->identifier,
5321 (const gchar **) service->excludes, len);
5323 g_key_file_remove_key(keyfile, service->identifier,
5324 "Proxy.Excludes", NULL);
5326 if (service->pac != NULL && strlen(service->pac) > 0)
5327 g_key_file_set_string(keyfile, service->identifier,
5328 "Proxy.URL", service->pac);
5330 g_key_file_remove_key(keyfile, service->identifier,
5333 data = g_key_file_to_data(keyfile, &length, NULL);
5335 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5336 connman_error("Failed to store service information");
5341 g_key_file_free(keyfile);
5348 static struct connman_storage service_storage = {
5350 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5351 .service_load = service_load,
5352 .service_save = service_save,
5355 int __connman_service_init(void)
5359 connection = connman_dbus_get_connection();
5361 if (connman_storage_register(&service_storage) < 0)
5362 connman_error("Failed to register service storage");
5364 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5367 service_list = g_sequence_new(service_free);
5372 void __connman_service_cleanup(void)
5376 g_sequence_free(service_list);
5377 service_list = NULL;
5379 g_hash_table_destroy(service_hash);
5380 service_hash = NULL;
5382 g_slist_free(counter_list);
5383 counter_list = NULL;
5385 connman_storage_unregister(&service_storage);
5387 dbus_connection_unref(connection);