5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #define CONNECT_TIMEOUT 120
34 static DBusConnection *connection = NULL;
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
38 static GSList *counter_list = NULL;
40 struct connman_stats {
42 connman_bool_t enabled;
43 struct connman_stats_data data_last;
44 struct connman_stats_data data;
48 struct connman_stats_counter {
49 connman_bool_t append_all;
50 struct connman_stats stats;
51 struct connman_stats stats_roaming;
54 struct connman_service {
58 enum connman_service_type type;
59 enum connman_service_mode mode;
60 enum connman_service_security security;
61 enum connman_service_state state_ipv4;
62 enum connman_service_state state_ipv6;
63 enum connman_service_error error;
64 connman_uint8_t strength;
65 connman_bool_t favorite;
66 connman_bool_t immutable;
67 connman_bool_t hidden;
68 connman_bool_t ignore;
69 connman_bool_t autoconnect;
70 connman_bool_t userconnect;
76 connman_bool_t roaming;
77 connman_bool_t login_required;
78 struct connman_ipconfig *ipconfig_ipv4;
79 struct connman_ipconfig *ipconfig_ipv6;
80 struct connman_network *network;
81 struct connman_provider *provider;
83 char **nameservers_config;
87 /* 802.1x settings from the config files */
91 char *client_cert_file;
92 char *private_key_file;
93 char *private_key_passphrase;
97 struct connman_location *location;
98 struct connman_stats stats;
99 struct connman_stats stats_roaming;
100 GHashTable *counter_table;
101 enum connman_service_proxy_method proxy;
102 enum connman_service_proxy_method proxy_config;
109 static void append_path(gpointer value, gpointer user_data)
111 struct connman_service *service = value;
112 DBusMessageIter *iter = user_data;
114 if (service->path == NULL || service->hidden == TRUE)
117 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
121 void __connman_service_list(DBusMessageIter *iter, void *user_data)
123 g_sequence_foreach(service_list, append_path, iter);
128 struct connman_service *service;
131 static void compare_path(gpointer value, gpointer user_data)
133 struct connman_service *service = value;
134 struct find_data *data = user_data;
136 if (data->service != NULL)
139 if (g_strcmp0(service->path, data->path) == 0)
140 data->service = service;
143 static struct connman_service *find_service(const char *path)
145 struct find_data data = { .path = path, .service = NULL };
147 DBG("path %s", path);
149 g_sequence_foreach(service_list, compare_path, &data);
154 const char *__connman_service_type2string(enum connman_service_type type)
157 case CONNMAN_SERVICE_TYPE_UNKNOWN:
159 case CONNMAN_SERVICE_TYPE_SYSTEM:
161 case CONNMAN_SERVICE_TYPE_ETHERNET:
163 case CONNMAN_SERVICE_TYPE_WIFI:
165 case CONNMAN_SERVICE_TYPE_WIMAX:
167 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
169 case CONNMAN_SERVICE_TYPE_CELLULAR:
171 case CONNMAN_SERVICE_TYPE_GPS:
173 case CONNMAN_SERVICE_TYPE_VPN:
175 case CONNMAN_SERVICE_TYPE_GADGET:
182 static const char *mode2string(enum connman_service_mode mode)
185 case CONNMAN_SERVICE_MODE_UNKNOWN:
187 case CONNMAN_SERVICE_MODE_MANAGED:
189 case CONNMAN_SERVICE_MODE_ADHOC:
191 case CONNMAN_SERVICE_MODE_GPRS:
193 case CONNMAN_SERVICE_MODE_EDGE:
195 case CONNMAN_SERVICE_MODE_UMTS:
202 static const char *security2string(enum connman_service_security security)
205 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
207 case CONNMAN_SERVICE_SECURITY_NONE:
209 case CONNMAN_SERVICE_SECURITY_WEP:
211 case CONNMAN_SERVICE_SECURITY_PSK:
213 case CONNMAN_SERVICE_SECURITY_8021X:
215 case CONNMAN_SERVICE_SECURITY_WPA:
217 case CONNMAN_SERVICE_SECURITY_RSN:
224 static const char *state2string(enum connman_service_state state)
227 case CONNMAN_SERVICE_STATE_UNKNOWN:
229 case CONNMAN_SERVICE_STATE_IDLE:
231 case CONNMAN_SERVICE_STATE_ASSOCIATION:
232 return "association";
233 case CONNMAN_SERVICE_STATE_CONFIGURATION:
234 return "configuration";
235 case CONNMAN_SERVICE_STATE_READY:
237 case CONNMAN_SERVICE_STATE_ONLINE:
239 case CONNMAN_SERVICE_STATE_DISCONNECT:
241 case CONNMAN_SERVICE_STATE_FAILURE:
248 static const char *error2string(enum connman_service_error error)
251 case CONNMAN_SERVICE_ERROR_UNKNOWN:
253 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
254 return "out-of-range";
255 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
256 return "pin-missing";
257 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
258 return "dhcp-failed";
259 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
260 return "connect-failed";
261 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
262 return "login-failed";
263 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
264 return "auth-failed";
265 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
266 return "invalid-key";
272 static enum connman_service_error string2error(const char *error)
274 if (g_strcmp0(error, "dhcp-failed") == 0)
275 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
276 else if (g_strcmp0(error, "pin-missing") == 0)
277 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
278 else if (g_strcmp0(error, "invalid-key") == 0)
279 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
281 return CONNMAN_SERVICE_ERROR_UNKNOWN;
284 static const char *proxymethod2string(enum connman_service_proxy_method method)
287 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
289 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
291 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
293 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
300 static enum connman_service_proxy_method string2proxymethod(const char *method)
302 if (g_strcmp0(method, "direct") == 0)
303 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
304 else if (g_strcmp0(method, "auto") == 0)
305 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
306 else if (g_strcmp0(method, "manual") == 0)
307 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
309 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
312 static enum connman_service_state combine_state(
313 enum connman_service_state state_a,
314 enum connman_service_state state_b)
316 enum connman_service_state result;
318 if (state_a == state_b) {
323 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
328 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
333 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
338 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
343 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
344 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
345 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
346 state_b == CONNMAN_SERVICE_STATE_READY)
353 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
354 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
355 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
356 state_a == CONNMAN_SERVICE_STATE_READY)
363 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
364 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
365 state_b == CONNMAN_SERVICE_STATE_READY)
372 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
373 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
374 state_a == CONNMAN_SERVICE_STATE_READY)
381 if (state_a == CONNMAN_SERVICE_STATE_READY) {
382 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
389 if (state_b == CONNMAN_SERVICE_STATE_READY) {
390 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
397 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
402 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
407 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
412 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
417 result = CONNMAN_SERVICE_STATE_FAILURE;
423 static connman_bool_t is_connecting_state(struct connman_service *service,
424 enum connman_service_state state)
427 case CONNMAN_SERVICE_STATE_UNKNOWN:
428 case CONNMAN_SERVICE_STATE_IDLE:
429 case CONNMAN_SERVICE_STATE_FAILURE:
430 if (service->network != NULL)
431 return connman_network_get_connecting(service->network);
432 case CONNMAN_SERVICE_STATE_DISCONNECT:
433 case CONNMAN_SERVICE_STATE_READY:
434 case CONNMAN_SERVICE_STATE_ONLINE:
436 case CONNMAN_SERVICE_STATE_ASSOCIATION:
437 case CONNMAN_SERVICE_STATE_CONFIGURATION:
444 static connman_bool_t is_connected_state(const struct connman_service *service,
445 enum connman_service_state state)
448 case CONNMAN_SERVICE_STATE_UNKNOWN:
449 case CONNMAN_SERVICE_STATE_IDLE:
450 case CONNMAN_SERVICE_STATE_ASSOCIATION:
451 case CONNMAN_SERVICE_STATE_CONFIGURATION:
452 case CONNMAN_SERVICE_STATE_DISCONNECT:
453 case CONNMAN_SERVICE_STATE_FAILURE:
455 case CONNMAN_SERVICE_STATE_READY:
456 case CONNMAN_SERVICE_STATE_ONLINE:
463 static connman_bool_t is_connecting(struct connman_service *service)
465 enum connman_service_state state;
467 state = combine_state(service->state_ipv4, service->state_ipv6);
469 return is_connecting_state(service, state);
472 static connman_bool_t is_connected(struct connman_service *service)
474 enum connman_service_state state;
476 state = combine_state(service->state_ipv4, service->state_ipv6);
478 return is_connected_state(service, state);
481 static connman_bool_t is_idle(struct connman_service *service)
483 enum connman_service_state state;
485 state = combine_state(service->state_ipv4, service->state_ipv6);
487 return state == CONNMAN_SERVICE_STATE_IDLE;
490 static void update_nameservers(struct connman_service *service)
494 if (service->ipconfig_ipv4)
495 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
496 else if (service->ipconfig_ipv6)
497 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
504 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
505 case CONNMAN_SERVICE_STATE_UNKNOWN:
506 case CONNMAN_SERVICE_STATE_IDLE:
507 case CONNMAN_SERVICE_STATE_ASSOCIATION:
508 case CONNMAN_SERVICE_STATE_CONFIGURATION:
510 case CONNMAN_SERVICE_STATE_FAILURE:
511 case CONNMAN_SERVICE_STATE_DISCONNECT:
512 connman_resolver_remove_all(ifname);
514 case CONNMAN_SERVICE_STATE_READY:
515 case CONNMAN_SERVICE_STATE_ONLINE:
519 connman_resolver_remove_all(ifname);
521 if (service->nameservers_config != NULL) {
524 for (i = 0; service->nameservers_config[i] != NULL; i++) {
525 connman_resolver_append(ifname, NULL,
526 service->nameservers_config[i]);
528 } else if (service->nameservers != NULL) {
531 for (i = 0; service->nameservers[i] != NULL; i++) {
532 connman_resolver_append(ifname, NULL,
533 service->nameservers[i]);
537 if (service->domains != NULL) {
540 for (i = 0; service->domains[i]; i++)
541 connman_resolver_append(ifname, service->domains[i],
543 } else if (service->domainname != NULL)
544 connman_resolver_append(ifname, service->domainname, NULL);
546 connman_resolver_flush();
549 int __connman_service_nameserver_append(struct connman_service *service,
550 const char *nameserver)
554 DBG("service %p nameserver %s", service, nameserver);
556 if (nameserver == NULL)
559 if (service->nameservers != NULL) {
562 for (i = 0; service->nameservers[i] != NULL; i++)
563 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
566 len = g_strv_length(service->nameservers);
567 service->nameservers = g_try_renew(char *, service->nameservers,
571 service->nameservers = g_try_new0(char *, len + 2);
574 if (service->nameservers == NULL)
577 service->nameservers[len] = g_strdup(nameserver);
578 service->nameservers[len + 1] = NULL;
580 update_nameservers(service);
585 int __connman_service_nameserver_remove(struct connman_service *service,
586 const char *nameserver)
591 DBG("service %p nameserver %s", service, nameserver);
593 if (nameserver == NULL)
596 if (service->nameservers == NULL)
599 len = g_strv_length(service->nameservers);
601 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
604 g_strfreev(service->nameservers);
605 service->nameservers = NULL;
610 servers = g_try_new0(char *, len - 1);
614 for (i = 0, j = 0; i < len; i++) {
615 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
616 servers[j] = g_strdup(service->nameservers[i]);
620 servers[len - 2] = NULL;
622 g_strfreev(service->nameservers);
623 service->nameservers = servers;
625 update_nameservers(service);
630 void __connman_service_nameserver_clear(struct connman_service *service)
632 g_strfreev(service->nameservers);
633 service->nameservers = NULL;
635 update_nameservers(service);
638 static void nameserver_add_routes(int index, char **nameservers,
643 for (i = 0; nameservers[i] != NULL; i++) {
644 if (connman_inet_compare_subnet(index, nameservers[i]))
647 connman_inet_add_host_route(index, nameservers[i], gw);
651 static void nameserver_del_routes(int index, char **nameservers)
655 for (i = 0; nameservers[i] != NULL; i++)
656 connman_inet_del_host_route(index, nameservers[i]);
659 void __connman_service_nameserver_add_routes(struct connman_service *service,
667 if (service->network != NULL)
668 index = connman_network_get_index(service->network);
669 else if (service->provider != NULL)
670 index = connman_provider_get_index(service->provider);
672 if (service->nameservers_config != NULL) {
674 * Configured nameserver takes preference over the
675 * discoverd nameserver gathered from DHCP, VPN, etc.
677 nameserver_add_routes(index, service->nameservers_config, gw);
678 } else if (service->nameservers != NULL) {
680 * We add nameservers host routes for nameservers that
681 * are not on our subnet. For those who are, the subnet
682 * route will be installed by the time the dns proxy code
683 * tries to reach them. The subnet route is installed
684 * when setting the interface IP address.
686 nameserver_add_routes(index, service->nameservers, gw);
690 void __connman_service_nameserver_del_routes(struct connman_service *service)
697 if (service->network != NULL)
698 index = connman_network_get_index(service->network);
699 else if (service->provider != NULL)
700 index = connman_provider_get_index(service->provider);
702 if (service->nameservers_config != NULL)
703 nameserver_del_routes(index, service->nameservers_config);
704 else if (service->nameservers != NULL)
705 nameserver_del_routes(index, service->nameservers);
708 static struct connman_stats *stats_get(struct connman_service *service)
710 if (service->roaming == TRUE)
711 return &service->stats_roaming;
713 return &service->stats;
716 static connman_bool_t stats_enabled(struct connman_service *service)
718 struct connman_stats *stats = stats_get(service);
720 return stats->enabled;
723 static void stats_start(struct connman_service *service)
725 struct connman_stats *stats = stats_get(service);
727 DBG("service %p", service);
729 if (stats->timer == NULL)
732 stats->enabled = TRUE;
733 stats->data_last.time = stats->data.time;
735 g_timer_start(stats->timer);
738 static void stats_stop(struct connman_service *service)
740 struct connman_stats *stats = stats_get(service);
741 unsigned int seconds;
743 DBG("service %p", service);
745 if (stats->timer == NULL)
748 if (stats->enabled == FALSE)
751 g_timer_stop(stats->timer);
753 seconds = g_timer_elapsed(stats->timer, NULL);
754 stats->data.time = stats->data_last.time + seconds;
756 stats->enabled = FALSE;
759 static void reset_stats(struct connman_service *service)
761 DBG("service %p", service);
764 service->stats.valid = FALSE;
766 service->stats.data.rx_packets = 0;
767 service->stats.data.tx_packets = 0;
768 service->stats.data.rx_bytes = 0;
769 service->stats.data.tx_bytes = 0;
770 service->stats.data.rx_errors = 0;
771 service->stats.data.tx_errors = 0;
772 service->stats.data.rx_dropped = 0;
773 service->stats.data.tx_dropped = 0;
774 service->stats.data.time = 0;
775 service->stats.data_last.time = 0;
777 g_timer_reset(service->stats.timer);
780 service->stats_roaming.valid = FALSE;
782 service->stats_roaming.data.rx_packets = 0;
783 service->stats_roaming.data.tx_packets = 0;
784 service->stats_roaming.data.rx_bytes = 0;
785 service->stats_roaming.data.tx_bytes = 0;
786 service->stats_roaming.data.rx_errors = 0;
787 service->stats_roaming.data.tx_errors = 0;
788 service->stats_roaming.data.rx_dropped = 0;
789 service->stats_roaming.data.tx_dropped = 0;
790 service->stats_roaming.data.time = 0;
791 service->stats_roaming.data_last.time = 0;
793 g_timer_reset(service->stats_roaming.timer);
796 static struct connman_service *get_default(void)
798 struct connman_service *service;
801 iter = g_sequence_get_begin_iter(service_list);
803 if (g_sequence_iter_is_end(iter) == TRUE)
806 service = g_sequence_get(iter);
808 if (is_connected(service) == FALSE)
814 static void default_changed(void)
816 struct connman_service *service = get_default();
818 __connman_notifier_default_changed(service);
821 const char *__connman_service_default(void)
823 struct connman_service *service;
825 service = get_default();
829 return __connman_service_type2string(service->type);
832 static void mode_changed(struct connman_service *service)
836 str = mode2string(service->mode);
840 connman_dbus_property_changed_basic(service->path,
841 CONNMAN_SERVICE_INTERFACE, "Mode",
842 DBUS_TYPE_STRING, &str);
845 static void state_changed(struct connman_service *service)
847 enum connman_service_state state;
850 state = combine_state(service->state_ipv4, service->state_ipv6);
852 __connman_notifier_service_state_changed(service, state);
854 str = state2string(state);
858 connman_dbus_property_changed_basic(service->path,
859 CONNMAN_SERVICE_INTERFACE, "State",
860 DBUS_TYPE_STRING, &str);
863 static void strength_changed(struct connman_service *service)
865 if (service->strength == 0)
868 connman_dbus_property_changed_basic(service->path,
869 CONNMAN_SERVICE_INTERFACE, "Strength",
870 DBUS_TYPE_BYTE, &service->strength);
873 static void favorite_changed(struct connman_service *service)
875 if (service->path == NULL)
878 connman_dbus_property_changed_basic(service->path,
879 CONNMAN_SERVICE_INTERFACE, "Favorite",
880 DBUS_TYPE_BOOLEAN, &service->favorite);
883 static void immutable_changed(struct connman_service *service)
885 if (service->path == NULL)
888 connman_dbus_property_changed_basic(service->path,
889 CONNMAN_SERVICE_INTERFACE, "Immutable",
890 DBUS_TYPE_BOOLEAN, &service->immutable);
893 static void roaming_changed(struct connman_service *service)
895 if (service->path == NULL)
898 connman_dbus_property_changed_basic(service->path,
899 CONNMAN_SERVICE_INTERFACE, "Roaming",
900 DBUS_TYPE_BOOLEAN, &service->roaming);
903 static void autoconnect_changed(struct connman_service *service)
905 if (service->path == NULL)
908 connman_dbus_property_changed_basic(service->path,
909 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
910 DBUS_TYPE_BOOLEAN, &service->autoconnect);
913 static void passphrase_changed(struct connman_service *service)
915 dbus_bool_t required;
917 switch (service->type) {
918 case CONNMAN_SERVICE_TYPE_UNKNOWN:
919 case CONNMAN_SERVICE_TYPE_SYSTEM:
920 case CONNMAN_SERVICE_TYPE_ETHERNET:
921 case CONNMAN_SERVICE_TYPE_WIMAX:
922 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
923 case CONNMAN_SERVICE_TYPE_CELLULAR:
924 case CONNMAN_SERVICE_TYPE_GPS:
925 case CONNMAN_SERVICE_TYPE_VPN:
926 case CONNMAN_SERVICE_TYPE_GADGET:
928 case CONNMAN_SERVICE_TYPE_WIFI:
931 switch (service->security) {
932 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
933 case CONNMAN_SERVICE_SECURITY_NONE:
935 case CONNMAN_SERVICE_SECURITY_WEP:
936 case CONNMAN_SERVICE_SECURITY_PSK:
937 case CONNMAN_SERVICE_SECURITY_WPA:
938 case CONNMAN_SERVICE_SECURITY_RSN:
939 if (service->passphrase == NULL)
942 case CONNMAN_SERVICE_SECURITY_8021X:
948 connman_dbus_property_changed_basic(service->path,
949 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
950 DBUS_TYPE_BOOLEAN, &required);
953 static void login_changed(struct connman_service *service)
955 dbus_bool_t required = service->login_required;
957 if (service->path == NULL)
960 connman_dbus_property_changed_basic(service->path,
961 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
962 DBUS_TYPE_BOOLEAN, &required);
965 static void append_security(DBusMessageIter *iter, void *user_data)
967 struct connman_service *service = user_data;
970 str = security2string(service->security);
972 dbus_message_iter_append_basic(iter,
973 DBUS_TYPE_STRING, &str);
976 if (service->wps == TRUE)
977 dbus_message_iter_append_basic(iter,
978 DBUS_TYPE_STRING, &str);
981 static void append_ethernet(DBusMessageIter *iter, void *user_data)
983 struct connman_service *service = user_data;
985 if (service->ipconfig_ipv4 != NULL)
986 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
988 else if (service->ipconfig_ipv6 != NULL)
989 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
993 static void append_ipv4(DBusMessageIter *iter, void *user_data)
995 struct connman_service *service = user_data;
997 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
998 state2string(service->state_ipv4));
1000 if (is_connected_state(service, service->state_ipv4) == FALSE)
1003 if (service->ipconfig_ipv4 != NULL)
1004 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1007 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1009 struct connman_service *service = user_data;
1011 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1012 state2string(service->state_ipv6));
1014 if (is_connected_state(service, service->state_ipv6) == FALSE)
1017 if (service->ipconfig_ipv6 != NULL)
1018 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1019 service->ipconfig_ipv4);
1022 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1024 struct connman_service *service = user_data;
1026 if (service->ipconfig_ipv4 != NULL)
1027 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1031 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1033 struct connman_service *service = user_data;
1035 if (service->ipconfig_ipv6 != NULL)
1036 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1040 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1045 servers = *nameservers;
1047 for (i = 0; servers[i] != NULL; i++) {
1048 dbus_message_iter_append_basic(iter,
1049 DBUS_TYPE_STRING, &servers[i]);
1053 static void append_dns(DBusMessageIter *iter, void *user_data)
1055 struct connman_service *service = user_data;
1057 if (is_connected(service) == FALSE)
1060 if (service->nameservers_config != NULL) {
1061 append_nameserver(iter, &service->nameservers_config);
1063 } else if (service->nameservers != NULL) {
1064 append_nameserver(iter, &service->nameservers);
1069 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1071 struct connman_service *service = user_data;
1074 if (service->nameservers_config == NULL)
1077 for (i = 0; service->nameservers_config[i]; i++) {
1078 dbus_message_iter_append_basic(iter,
1080 &service->nameservers_config[i]);
1084 static void append_domain(DBusMessageIter *iter, void *user_data)
1086 struct connman_service *service = user_data;
1088 if (is_connected(service) == FALSE &&
1089 is_connecting(service) == FALSE)
1092 if (service->domainname == NULL)
1095 dbus_message_iter_append_basic(iter,
1096 DBUS_TYPE_STRING, &service->domainname);
1099 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1101 struct connman_service *service = user_data;
1104 if (service->domains == NULL)
1107 for (i = 0; service->domains[i]; i++)
1108 dbus_message_iter_append_basic(iter,
1109 DBUS_TYPE_STRING, &service->domains[i]);
1112 static void append_proxies(DBusMessageIter *iter, void *user_data)
1114 struct connman_service *service = user_data;
1117 if (service->proxies == NULL)
1120 for (i = 0; service->proxies[i]; i++)
1121 dbus_message_iter_append_basic(iter,
1122 DBUS_TYPE_STRING, &service->proxies[i]);
1125 static void append_excludes(DBusMessageIter *iter, void *user_data)
1127 struct connman_service *service = user_data;
1130 if (service->excludes == NULL)
1133 for (i = 0; service->excludes[i]; i++)
1134 dbus_message_iter_append_basic(iter,
1135 DBUS_TYPE_STRING, &service->excludes[i]);
1138 static void append_proxy(DBusMessageIter *iter, void *user_data)
1140 struct connman_service *service = user_data;
1141 enum connman_service_proxy_method proxy;
1142 const char *pac = NULL;
1143 const char *method = proxymethod2string(
1144 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1148 if (is_connected(service) == FALSE)
1151 proxy = connman_service_get_proxy_method(service);
1154 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1156 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1158 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1159 connman_dbus_dict_append_array(iter, "Servers",
1160 DBUS_TYPE_STRING, append_proxies,
1163 connman_dbus_dict_append_array(iter, "Excludes",
1164 DBUS_TYPE_STRING, append_excludes,
1167 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1168 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1169 if (service->ipconfig_ipv4 != NULL)
1170 pac = __connman_ipconfig_get_proxy_autoconfig(
1171 service->ipconfig_ipv4);
1172 else if (service->ipconfig_ipv6 != NULL)
1173 pac = __connman_ipconfig_get_proxy_autoconfig(
1174 service->ipconfig_ipv6);
1176 if (service->pac == NULL && pac == NULL)
1179 if (service->pac != NULL)
1182 connman_dbus_dict_append_basic(iter, "URL",
1183 DBUS_TYPE_STRING, &pac);
1187 method = proxymethod2string(proxy);
1190 connman_dbus_dict_append_basic(iter, "Method",
1191 DBUS_TYPE_STRING, &method);
1194 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1196 struct connman_service *service = user_data;
1199 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1202 switch (service->proxy_config) {
1203 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1205 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1207 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1208 if (service->proxies != NULL)
1209 connman_dbus_dict_append_array(iter, "Servers",
1211 append_proxies, service);
1213 if (service->excludes != NULL)
1214 connman_dbus_dict_append_array(iter, "Excludes",
1216 append_excludes, service);
1218 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1219 if (service->pac != NULL)
1220 connman_dbus_dict_append_basic(iter, "URL",
1221 DBUS_TYPE_STRING, &service->pac);
1225 method = proxymethod2string(service->proxy_config);
1227 connman_dbus_dict_append_basic(iter, "Method",
1228 DBUS_TYPE_STRING, &method);
1231 static void append_provider(DBusMessageIter *iter, void *user_data)
1233 struct connman_service *service = user_data;
1235 DBG("%p %p", service, service->provider);
1237 if (is_connected(service) == FALSE)
1240 if (service->provider != NULL)
1241 __connman_provider_append_properties(service->provider, iter);
1245 static void settings_changed(struct connman_service *service,
1246 struct connman_ipconfig *ipconfig)
1248 connman_dbus_property_changed_dict(service->path,
1249 CONNMAN_SERVICE_INTERFACE, "IPv4",
1250 append_ipv4, service);
1252 connman_dbus_property_changed_dict(service->path,
1253 CONNMAN_SERVICE_INTERFACE, "IPv6",
1254 append_ipv6, service);
1256 __connman_notifier_ipconfig_changed(service, ipconfig);
1259 static void ipv4_configuration_changed(struct connman_service *service)
1261 connman_dbus_property_changed_dict(service->path,
1262 CONNMAN_SERVICE_INTERFACE,
1263 "IPv4.Configuration",
1268 static void ipv6_configuration_changed(struct connman_service *service)
1270 connman_dbus_property_changed_dict(service->path,
1271 CONNMAN_SERVICE_INTERFACE,
1272 "IPv6.Configuration",
1277 static void dns_changed(struct connman_service *service)
1279 connman_dbus_property_changed_array(service->path,
1280 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1281 DBUS_TYPE_STRING, append_dns, service);
1284 static void dns_configuration_changed(struct connman_service *service)
1286 connman_dbus_property_changed_array(service->path,
1287 CONNMAN_SERVICE_INTERFACE,
1288 "Nameservers.Configuration",
1289 DBUS_TYPE_STRING, append_dnsconfig, service);
1291 dns_changed(service);
1294 static void domain_changed(struct connman_service *service)
1296 connman_dbus_property_changed_array(service->path,
1297 CONNMAN_SERVICE_INTERFACE, "Domains",
1298 DBUS_TYPE_STRING, append_domain, service);
1301 static void domain_configuration_changed(struct connman_service *service)
1303 connman_dbus_property_changed_array(service->path,
1304 CONNMAN_SERVICE_INTERFACE,
1305 "Domains.Configuration",
1306 DBUS_TYPE_STRING, append_domainconfig, service);
1309 static void proxy_changed(struct connman_service *service)
1311 connman_dbus_property_changed_dict(service->path,
1312 CONNMAN_SERVICE_INTERFACE, "Proxy",
1313 append_proxy, service);
1316 static void proxy_configuration_changed(struct connman_service *service)
1318 connman_dbus_property_changed_dict(service->path,
1319 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1320 append_proxyconfig, service);
1322 proxy_changed(service);
1325 static void link_changed(struct connman_service *service)
1327 connman_dbus_property_changed_dict(service->path,
1328 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1329 append_ethernet, service);
1332 static void stats_append_counters(DBusMessageIter *dict,
1333 struct connman_stats_data *stats,
1334 struct connman_stats_data *counters,
1335 connman_bool_t append_all)
1337 if (counters->rx_packets != stats->rx_packets || append_all) {
1338 counters->rx_packets = stats->rx_packets;
1339 connman_dbus_dict_append_basic(dict, "RX.Packets",
1340 DBUS_TYPE_UINT32, &stats->rx_packets);
1343 if (counters->tx_packets != stats->tx_packets || append_all) {
1344 counters->tx_packets = stats->tx_packets;
1345 connman_dbus_dict_append_basic(dict, "TX.Packets",
1346 DBUS_TYPE_UINT32, &stats->tx_packets);
1349 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1350 counters->rx_bytes = stats->rx_bytes;
1351 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1352 DBUS_TYPE_UINT32, &stats->rx_bytes);
1355 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1356 counters->tx_bytes = stats->tx_bytes;
1357 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1358 DBUS_TYPE_UINT32, &stats->tx_bytes);
1361 if (counters->rx_errors != stats->rx_errors || append_all) {
1362 counters->rx_errors = stats->rx_errors;
1363 connman_dbus_dict_append_basic(dict, "RX.Errors",
1364 DBUS_TYPE_UINT32, &stats->rx_errors);
1367 if (counters->tx_errors != stats->tx_errors || append_all) {
1368 counters->tx_errors = stats->tx_errors;
1369 connman_dbus_dict_append_basic(dict, "TX.Errors",
1370 DBUS_TYPE_UINT32, &stats->tx_errors);
1373 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1374 counters->rx_dropped = stats->rx_dropped;
1375 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1376 DBUS_TYPE_UINT32, &stats->rx_dropped);
1379 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1380 counters->tx_dropped = stats->tx_dropped;
1381 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1382 DBUS_TYPE_UINT32, &stats->tx_dropped);
1385 if (counters->time != stats->time || append_all) {
1386 counters->time = stats->time;
1387 connman_dbus_dict_append_basic(dict, "Time",
1388 DBUS_TYPE_UINT32, &stats->time);
1392 static void stats_append(struct connman_service *service,
1393 const char *counter,
1394 struct connman_stats_counter *counters,
1395 connman_bool_t append_all)
1397 DBusMessageIter array, dict;
1400 DBG("service %p counter %s", service, counter);
1402 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1406 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1407 &service->path, DBUS_TYPE_INVALID);
1409 dbus_message_iter_init_append(msg, &array);
1412 connman_dbus_dict_open(&array, &dict);
1414 stats_append_counters(&dict, &service->stats.data,
1415 &counters->stats.data, append_all);
1417 connman_dbus_dict_close(&array, &dict);
1419 /* roaming counter */
1420 connman_dbus_dict_open(&array, &dict);
1422 stats_append_counters(&dict, &service->stats_roaming.data,
1423 &counters->stats_roaming.data, append_all);
1425 connman_dbus_dict_close(&array, &dict);
1427 __connman_counter_send_usage(counter, msg);
1430 static void stats_update(struct connman_service *service,
1431 unsigned int rx_packets, unsigned int tx_packets,
1432 unsigned int rx_bytes, unsigned int tx_bytes,
1433 unsigned int rx_errors, unsigned int tx_errors,
1434 unsigned int rx_dropped, unsigned int tx_dropped)
1436 struct connman_stats *stats = stats_get(service);
1437 struct connman_stats_data *data_last = &stats->data_last;
1438 struct connman_stats_data *data = &stats->data;
1439 unsigned int seconds;
1441 DBG("service %p", service);
1443 if (stats->valid == TRUE) {
1445 rx_packets - data_last->rx_packets;
1447 tx_packets - data_last->tx_packets;
1449 rx_bytes - data_last->rx_bytes;
1451 tx_bytes - data_last->tx_bytes;
1453 rx_errors - data_last->rx_errors;
1455 tx_errors - data_last->tx_errors;
1457 rx_dropped - data_last->rx_dropped;
1459 tx_dropped - data_last->tx_dropped;
1461 stats->valid = TRUE;
1464 data_last->rx_packets = rx_packets;
1465 data_last->tx_packets = tx_packets;
1466 data_last->rx_bytes = rx_bytes;
1467 data_last->tx_bytes = tx_bytes;
1468 data_last->rx_errors = rx_errors;
1469 data_last->tx_errors = tx_errors;
1470 data_last->rx_dropped = rx_dropped;
1471 data_last->tx_dropped = tx_dropped;
1473 seconds = g_timer_elapsed(stats->timer, NULL);
1474 stats->data.time = stats->data_last.time + seconds;
1477 void __connman_service_notify(struct connman_service *service,
1478 unsigned int rx_packets, unsigned int tx_packets,
1479 unsigned int rx_bytes, unsigned int tx_bytes,
1480 unsigned int rx_errors, unsigned int tx_errors,
1481 unsigned int rx_dropped, unsigned int tx_dropped)
1483 GHashTableIter iter;
1484 gpointer key, value;
1485 const char *counter;
1486 struct connman_stats_counter *counters;
1487 struct connman_stats_data *data;
1490 if (service == NULL)
1493 if (is_connected(service) == FALSE)
1496 stats_update(service,
1497 rx_packets, tx_packets,
1499 rx_errors, tx_errors,
1500 rx_dropped, tx_dropped);
1502 data = &stats_get(service)->data;
1503 err = __connman_stats_update(service, service->roaming, data);
1505 connman_error("Failed to store statistics for %s",
1506 service->identifier);
1508 g_hash_table_iter_init(&iter, service->counter_table);
1509 while (g_hash_table_iter_next(&iter, &key, &value)) {
1513 stats_append(service, counter, counters, counters->append_all);
1514 counters->append_all = FALSE;
1518 int __connman_service_counter_register(const char *counter)
1520 struct connman_service *service;
1521 GSequenceIter *iter;
1522 struct connman_stats_counter *counters;
1524 DBG("counter %s", counter);
1526 counter_list = g_slist_append(counter_list, (gpointer)counter);
1528 iter = g_sequence_get_begin_iter(service_list);
1530 while (g_sequence_iter_is_end(iter) == FALSE) {
1531 service = g_sequence_get(iter);
1533 counters = g_try_new0(struct connman_stats_counter, 1);
1534 if (counters == NULL)
1537 counters->append_all = TRUE;
1539 g_hash_table_replace(service->counter_table, (gpointer)counter,
1542 iter = g_sequence_iter_next(iter);
1548 void __connman_service_counter_unregister(const char *counter)
1550 struct connman_service *service;
1551 GSequenceIter *iter;
1553 DBG("counter %s", counter);
1555 iter = g_sequence_get_begin_iter(service_list);
1557 while (g_sequence_iter_is_end(iter) == FALSE) {
1558 service = g_sequence_get(iter);
1560 g_hash_table_remove(service->counter_table, counter);
1562 iter = g_sequence_iter_next(iter);
1565 counter_list = g_slist_remove(counter_list, counter);
1568 GSequence *__connman_service_get_list(struct connman_session *session,
1569 service_match_cb service_match)
1572 GSequenceIter *iter;
1574 list = g_sequence_new(NULL);
1578 iter = g_sequence_get_begin_iter(service_list);
1580 while (g_sequence_iter_is_end(iter) == FALSE) {
1581 service = g_sequence_get(iter);
1583 if (service_match(session, service) == TRUE)
1584 g_sequence_append(list, service);
1586 iter = g_sequence_iter_next(iter);
1592 connman_bool_t __connman_service_is_connecting(struct connman_service *service)
1594 return is_connecting(service);
1597 connman_bool_t __connman_service_is_connected(struct connman_service *service)
1599 return is_connected(service);
1602 connman_bool_t __connman_service_is_idle(struct connman_service *service)
1604 return is_idle(service);
1607 const char *__connman_service_get_name(struct connman_service *service)
1609 return service->name;
1612 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1613 struct connman_service *service)
1615 dbus_bool_t required;
1618 str = __connman_service_type2string(service->type);
1620 connman_dbus_dict_append_basic(dict, "Type",
1621 DBUS_TYPE_STRING, &str);
1623 str = mode2string(service->mode);
1625 connman_dbus_dict_append_basic(dict, "Mode",
1626 DBUS_TYPE_STRING, &str);
1628 connman_dbus_dict_append_array(dict, "Security",
1629 DBUS_TYPE_STRING, append_security, service);
1631 str = state2string(combine_state(service->state_ipv4,
1632 service->state_ipv6));
1634 connman_dbus_dict_append_basic(dict, "State",
1635 DBUS_TYPE_STRING, &str);
1637 str = error2string(service->error);
1639 connman_dbus_dict_append_basic(dict, "Error",
1640 DBUS_TYPE_STRING, &str);
1642 if (service->strength > 0)
1643 connman_dbus_dict_append_basic(dict, "Strength",
1644 DBUS_TYPE_BYTE, &service->strength);
1646 connman_dbus_dict_append_basic(dict, "Favorite",
1647 DBUS_TYPE_BOOLEAN, &service->favorite);
1649 connman_dbus_dict_append_basic(dict, "Immutable",
1650 DBUS_TYPE_BOOLEAN, &service->immutable);
1652 if (service->favorite == TRUE)
1653 connman_dbus_dict_append_basic(dict, "AutoConnect",
1654 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1656 connman_dbus_dict_append_basic(dict, "AutoConnect",
1657 DBUS_TYPE_BOOLEAN, &service->favorite);
1659 if (service->name != NULL)
1660 connman_dbus_dict_append_basic(dict, "Name",
1661 DBUS_TYPE_STRING, &service->name);
1663 connman_dbus_dict_append_basic(dict, "LoginRequired",
1664 DBUS_TYPE_BOOLEAN, &service->login_required);
1666 switch (service->type) {
1667 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1668 case CONNMAN_SERVICE_TYPE_SYSTEM:
1669 case CONNMAN_SERVICE_TYPE_GPS:
1670 case CONNMAN_SERVICE_TYPE_VPN:
1671 case CONNMAN_SERVICE_TYPE_GADGET:
1673 case CONNMAN_SERVICE_TYPE_CELLULAR:
1674 connman_dbus_dict_append_basic(dict, "Roaming",
1675 DBUS_TYPE_BOOLEAN, &service->roaming);
1678 connman_dbus_dict_append_basic(dict, "SetupRequired",
1679 DBUS_TYPE_BOOLEAN, &required);
1680 connman_dbus_dict_append_dict(dict, "Ethernet",
1681 append_ethernet, service);
1683 case CONNMAN_SERVICE_TYPE_WIFI:
1684 if (service->passphrase != NULL && limited == FALSE)
1685 connman_dbus_dict_append_basic(dict, "Passphrase",
1686 DBUS_TYPE_STRING, &service->passphrase);
1690 switch (service->security) {
1691 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1692 case CONNMAN_SERVICE_SECURITY_NONE:
1694 case CONNMAN_SERVICE_SECURITY_WEP:
1695 case CONNMAN_SERVICE_SECURITY_PSK:
1696 case CONNMAN_SERVICE_SECURITY_WPA:
1697 case CONNMAN_SERVICE_SECURITY_RSN:
1698 if (service->passphrase == NULL)
1701 case CONNMAN_SERVICE_SECURITY_8021X:
1705 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1706 DBUS_TYPE_BOOLEAN, &required);
1709 case CONNMAN_SERVICE_TYPE_ETHERNET:
1710 case CONNMAN_SERVICE_TYPE_WIMAX:
1711 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1712 connman_dbus_dict_append_dict(dict, "Ethernet",
1713 append_ethernet, service);
1717 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1719 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1720 append_ipv4config, service);
1722 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1724 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1725 append_ipv6config, service);
1727 connman_dbus_dict_append_array(dict, "Nameservers",
1728 DBUS_TYPE_STRING, append_dns, service);
1730 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1731 DBUS_TYPE_STRING, append_dnsconfig, service);
1733 connman_dbus_dict_append_array(dict, "Domains",
1734 DBUS_TYPE_STRING, append_domain, service);
1736 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1737 DBUS_TYPE_STRING, append_domainconfig, service);
1739 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1741 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1742 append_proxyconfig, service);
1744 connman_dbus_dict_append_dict(dict, "Provider",
1745 append_provider, service);
1748 static void append_struct(gpointer value, gpointer user_data)
1750 struct connman_service *service = value;
1751 DBusMessageIter *iter = user_data;
1752 DBusMessageIter entry, dict;
1754 if (service->path == NULL || service->hidden == TRUE)
1757 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1759 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1762 connman_dbus_dict_open(&entry, &dict);
1763 append_properties(&dict, TRUE, service);
1764 connman_dbus_dict_close(&entry, &dict);
1766 dbus_message_iter_close_container(iter, &entry);
1769 void __connman_service_list_struct(DBusMessageIter *iter)
1771 g_sequence_foreach(service_list, append_struct, iter);
1774 int __connman_service_get_index(struct connman_service *service)
1776 if (service == NULL)
1779 if (service->network != NULL)
1780 return connman_network_get_index(service->network);
1781 else if (service->provider != NULL)
1782 return connman_provider_get_index(service->provider);
1787 void __connman_service_set_domainname(struct connman_service *service,
1788 const char *domainname)
1790 if (service == NULL)
1793 g_free(service->domainname);
1794 service->domainname = g_strdup(domainname);
1796 domain_changed(service);
1799 const char *connman_service_get_domainname(struct connman_service *service)
1801 if (service == NULL)
1804 if (service->domains != NULL)
1805 return service->domains[0];
1807 return service->domainname;
1810 char **connman_service_get_nameservers(struct connman_service *service)
1812 if (service == NULL)
1815 if (service->nameservers_config != NULL)
1816 return service->nameservers_config;
1817 else if (service->nameservers != NULL)
1818 return service->nameservers;
1823 void connman_service_set_proxy_method(struct connman_service *service,
1824 enum connman_service_proxy_method method)
1826 if (service == NULL)
1829 service->proxy = method;
1831 proxy_changed(service);
1833 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1834 __connman_notifier_proxy_changed(service);
1837 enum connman_service_proxy_method connman_service_get_proxy_method(
1838 struct connman_service *service)
1840 if (service == NULL)
1841 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1843 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1844 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1845 service->pac == NULL)
1846 return service->proxy;
1848 return service->proxy_config;
1851 return service->proxy;
1854 char **connman_service_get_proxy_servers(struct connman_service *service)
1856 return g_strdupv(service->proxies);
1859 char **connman_service_get_proxy_excludes(struct connman_service *service)
1861 return g_strdupv(service->excludes);
1864 const char *connman_service_get_proxy_url(struct connman_service *service)
1866 if (service == NULL)
1869 return service->pac;
1872 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1875 if (service == NULL)
1878 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1880 if (service->ipconfig_ipv4) {
1881 if (__connman_ipconfig_set_proxy_autoconfig(
1882 service->ipconfig_ipv4, url) < 0)
1884 } else if (service->ipconfig_ipv6) {
1885 if (__connman_ipconfig_set_proxy_autoconfig(
1886 service->ipconfig_ipv6, url) < 0)
1891 proxy_changed(service);
1893 __connman_notifier_proxy_changed(service);
1896 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1898 if (service == NULL)
1901 if (service->ipconfig_ipv4)
1902 return __connman_ipconfig_get_proxy_autoconfig(
1903 service->ipconfig_ipv4);
1904 else if (service->ipconfig_ipv6)
1905 return __connman_ipconfig_get_proxy_autoconfig(
1906 service->ipconfig_ipv6);
1910 static void update_timeservers(struct connman_service *service)
1914 if (service->timeservers == NULL)
1917 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
1918 case CONNMAN_SERVICE_STATE_UNKNOWN:
1919 case CONNMAN_SERVICE_STATE_IDLE:
1920 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1921 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1923 case CONNMAN_SERVICE_STATE_FAILURE:
1924 case CONNMAN_SERVICE_STATE_DISCONNECT:
1925 for (i = 0; service->timeservers[i] != NULL; i++)
1926 connman_timeserver_remove(service->timeservers[i]);
1928 case CONNMAN_SERVICE_STATE_READY:
1929 case CONNMAN_SERVICE_STATE_ONLINE:
1933 for (i = 0; service->timeservers[i] != NULL; i++)
1934 connman_timeserver_append(service->timeservers[i]);
1937 int __connman_service_timeserver_append(struct connman_service *service,
1938 const char *timeserver)
1942 DBG("service %p timeserver %s", service, timeserver);
1944 if (timeserver == NULL)
1947 if (service->timeservers != NULL) {
1950 for (i = 0; service->timeservers[i] != NULL; i++)
1951 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1954 len = g_strv_length(service->timeservers);
1955 service->timeservers = g_try_renew(char *, service->timeservers,
1959 service->timeservers = g_try_new0(char *, len + 2);
1962 if (service->timeservers == NULL)
1965 service->timeservers[len] = g_strdup(timeserver);
1966 service->timeservers[len + 1] = NULL;
1968 update_timeservers(service);
1973 int __connman_service_timeserver_remove(struct connman_service *service,
1974 const char *timeserver)
1979 DBG("service %p timeserver %s", service, timeserver);
1981 if (timeserver == NULL)
1984 if (service->timeservers == NULL)
1987 len = g_strv_length(service->timeservers);
1989 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
1992 g_strfreev(service->timeservers);
1993 service->timeservers = NULL;
1998 servers = g_try_new0(char *, len - 1);
1999 if (servers == NULL)
2002 for (i = 0, j = 0; i < len; i++) {
2003 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2004 servers[j] = g_strdup(service->timeservers[i]);
2008 servers[len - 2] = NULL;
2010 g_strfreev(service->timeservers);
2011 service->timeservers = servers;
2013 update_timeservers(service);
2018 void __connman_service_set_pac(struct connman_service *service,
2024 g_free(service->pac);
2025 service->pac = g_strdup(pac);
2028 void __connman_service_set_identity(struct connman_service *service,
2029 const char *identity)
2031 if (service->immutable)
2034 g_free(service->identity);
2035 service->identity = g_strdup(identity);
2037 if (service->network != NULL)
2038 connman_network_set_string(service->network,
2043 void __connman_service_set_passphrase(struct connman_service *service,
2044 const char* passphrase)
2046 if (service->immutable == TRUE)
2049 g_free(service->passphrase);
2050 service->passphrase = g_strdup(passphrase);
2052 passphrase_changed(service);
2054 if (service->network != NULL)
2055 connman_network_set_string(service->network,
2057 service->passphrase);
2059 __connman_storage_save_service(service);
2062 static DBusMessage *get_properties(DBusConnection *conn,
2063 DBusMessage *msg, void *user_data)
2065 struct connman_service *service = user_data;
2067 DBusMessageIter array, dict;
2069 DBG("service %p", service);
2071 reply = dbus_message_new_method_return(msg);
2075 dbus_message_iter_init_append(reply, &array);
2077 connman_dbus_dict_open(&array, &dict);
2078 append_properties(&dict, FALSE, service);
2079 connman_dbus_dict_close(&array, &dict);
2084 static int update_proxy_configuration(struct connman_service *service,
2085 DBusMessageIter *array)
2087 DBusMessageIter dict;
2088 enum connman_service_proxy_method method;
2089 GString *servers_str = NULL;
2090 GString *excludes_str = NULL;
2091 const char *url = NULL;
2093 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2095 dbus_message_iter_recurse(array, &dict);
2097 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2098 DBusMessageIter entry, variant;
2102 dbus_message_iter_recurse(&dict, &entry);
2104 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2107 dbus_message_iter_get_basic(&entry, &key);
2108 dbus_message_iter_next(&entry);
2110 if (dbus_message_iter_get_arg_type(&entry) !=
2114 dbus_message_iter_recurse(&entry, &variant);
2115 type = dbus_message_iter_get_arg_type(&variant);
2117 if (g_str_equal(key, "Method") == TRUE) {
2120 if (type != DBUS_TYPE_STRING)
2123 dbus_message_iter_get_basic(&variant, &val);
2124 method = string2proxymethod(val);
2125 } else if (g_str_equal(key, "URL") == TRUE) {
2126 if (type != DBUS_TYPE_STRING)
2129 dbus_message_iter_get_basic(&variant, &url);
2130 } else if (g_str_equal(key, "Servers") == TRUE) {
2131 DBusMessageIter str_array;
2133 if (type != DBUS_TYPE_ARRAY)
2136 servers_str = g_string_new(NULL);
2137 if (servers_str == NULL)
2140 dbus_message_iter_recurse(&variant, &str_array);
2142 while (dbus_message_iter_get_arg_type(&str_array) ==
2146 dbus_message_iter_get_basic(&str_array, &val);
2148 if (servers_str->len > 0)
2149 g_string_append_printf(servers_str,
2152 g_string_append(servers_str, val);
2154 dbus_message_iter_next(&str_array);
2156 } else if (g_str_equal(key, "Excludes") == TRUE) {
2157 DBusMessageIter str_array;
2159 if (type != DBUS_TYPE_ARRAY)
2162 excludes_str = g_string_new(NULL);
2163 if (excludes_str == NULL)
2166 dbus_message_iter_recurse(&variant, &str_array);
2168 while (dbus_message_iter_get_arg_type(&str_array) ==
2172 dbus_message_iter_get_basic(&str_array, &val);
2174 if (excludes_str->len > 0)
2175 g_string_append_printf(excludes_str,
2178 g_string_append(excludes_str, val);
2180 dbus_message_iter_next(&str_array);
2184 dbus_message_iter_next(&dict);
2188 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2190 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2191 if (servers_str == NULL && service->proxies == NULL)
2194 if (servers_str != NULL) {
2195 g_strfreev(service->proxies);
2197 if (servers_str->len > 0)
2198 service->proxies = g_strsplit_set(
2199 servers_str->str, " ", 0);
2201 service->proxies = NULL;
2204 if (excludes_str != NULL) {
2205 g_strfreev(service->excludes);
2207 if (excludes_str->len > 0)
2208 service->excludes = g_strsplit_set(
2209 excludes_str->str, " ", 0);
2211 service->excludes = NULL;
2214 if (service->proxies == NULL)
2215 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2218 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2219 g_free(service->pac);
2221 if (url != NULL && strlen(url) > 0)
2222 service->pac = g_strdup(url);
2224 service->pac = NULL;
2226 /* if we are connected:
2227 - if service->pac == NULL
2228 - if __connman_ipconfig_get_proxy_autoconfig(
2229 service->ipconfig) == NULL
2230 --> We should start WPAD */
2233 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2237 if (servers_str != NULL)
2238 g_string_free(servers_str, TRUE);
2240 if (excludes_str != NULL)
2241 g_string_free(excludes_str, TRUE);
2243 service->proxy_config = method;
2248 if (servers_str != NULL)
2249 g_string_free(servers_str, TRUE);
2251 if (excludes_str != NULL)
2252 g_string_free(excludes_str, TRUE);
2257 static int set_ipconfig(struct connman_service *service,
2258 struct connman_ipconfig *ipconfig,
2259 DBusMessageIter *array,
2260 enum connman_service_state state,
2261 enum connman_service_state *new_state)
2263 enum connman_ipconfig_method old_method;
2264 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2265 enum connman_ipconfig_type type;
2268 old_method = __connman_ipconfig_get_method(ipconfig);
2270 if (is_connecting_state(service, state) ||
2271 is_connected_state(service, state))
2272 __connman_network_clear_ipconfig(service->network, ipconfig);
2274 err = __connman_ipconfig_set_config(ipconfig, array);
2275 method = __connman_ipconfig_get_method(ipconfig);
2276 type = __connman_ipconfig_get_config_type(ipconfig);
2278 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2279 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2280 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2281 *new_state = service->state_ipv4 =
2282 CONNMAN_SERVICE_STATE_CONFIGURATION;
2283 __connman_ipconfig_enable(ipconfig);
2286 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2287 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2288 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2289 *new_state = service->state_ipv6;
2290 __connman_ipconfig_enable(ipconfig);
2294 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2295 type, method, state2string(*new_state));
2300 static DBusMessage *set_property(DBusConnection *conn,
2301 DBusMessage *msg, void *user_data)
2303 struct connman_service *service = user_data;
2304 DBusMessageIter iter, value;
2308 DBG("service %p", service);
2310 if (dbus_message_iter_init(msg, &iter) == FALSE)
2311 return __connman_error_invalid_arguments(msg);
2313 dbus_message_iter_get_basic(&iter, &name);
2314 dbus_message_iter_next(&iter);
2315 dbus_message_iter_recurse(&iter, &value);
2317 type = dbus_message_iter_get_arg_type(&value);
2319 if (g_str_equal(name, "AutoConnect") == TRUE) {
2320 connman_bool_t autoconnect;
2322 if (type != DBUS_TYPE_BOOLEAN)
2323 return __connman_error_invalid_arguments(msg);
2325 if (service->favorite == FALSE)
2326 return __connman_error_invalid_service(msg);
2328 dbus_message_iter_get_basic(&value, &autoconnect);
2330 if (service->autoconnect == autoconnect)
2331 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2333 service->autoconnect = autoconnect;
2335 autoconnect_changed(service);
2337 __connman_storage_save_service(service);
2338 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2339 const char *passphrase;
2341 if (type != DBUS_TYPE_STRING)
2342 return __connman_error_invalid_arguments(msg);
2344 if (service->immutable == TRUE)
2345 return __connman_error_not_supported(msg);
2347 dbus_message_iter_get_basic(&value, &passphrase);
2349 __connman_service_set_passphrase(service, passphrase);
2350 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2351 DBusMessageIter entry;
2356 if (type != DBUS_TYPE_ARRAY)
2357 return __connman_error_invalid_arguments(msg);
2359 str = g_string_new(NULL);
2361 return __connman_error_invalid_arguments(msg);
2363 index = connman_network_get_index(service->network);
2364 gw = __connman_ipconfig_get_gateway_from_index(index);
2366 if (gw && strlen(gw))
2367 __connman_service_nameserver_del_routes(service);
2369 dbus_message_iter_recurse(&value, &entry);
2371 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2373 dbus_message_iter_get_basic(&entry, &val);
2374 dbus_message_iter_next(&entry);
2376 g_string_append_printf(str, " %s", val);
2378 g_string_append(str, val);
2381 g_strfreev(service->nameservers_config);
2384 service->nameservers_config =
2385 g_strsplit_set(str->str, " ", 0);
2387 service->nameservers_config = NULL;
2390 g_string_free(str, TRUE);
2392 if (gw && strlen(gw))
2393 __connman_service_nameserver_add_routes(service, gw);
2395 update_nameservers(service);
2396 dns_configuration_changed(service);
2398 __connman_storage_save_service(service);
2399 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2400 DBusMessageIter entry;
2403 if (type != DBUS_TYPE_ARRAY)
2404 return __connman_error_invalid_arguments(msg);
2406 str = g_string_new(NULL);
2408 return __connman_error_invalid_arguments(msg);
2410 dbus_message_iter_recurse(&value, &entry);
2412 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2414 dbus_message_iter_get_basic(&entry, &val);
2415 dbus_message_iter_next(&entry);
2417 g_string_append_printf(str, " %s", val);
2419 g_string_append(str, val);
2422 g_strfreev(service->domains);
2425 service->domains = g_strsplit_set(str->str, " ", 0);
2427 service->domains = NULL;
2429 g_string_free(str, TRUE);
2431 update_nameservers(service);
2432 domain_configuration_changed(service);
2434 __connman_storage_save_service(service);
2435 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2438 if (type != DBUS_TYPE_ARRAY)
2439 return __connman_error_invalid_arguments(msg);
2441 err = update_proxy_configuration(service, &value);
2444 return __connman_error_failed(msg, -err);
2446 proxy_configuration_changed(service);
2448 __connman_storage_save_service(service);
2449 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2450 g_str_equal(name, "IPv6.Configuration")) {
2452 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2453 enum connman_service_state state =
2454 CONNMAN_SERVICE_STATE_UNKNOWN;
2459 if (service->ipconfig_ipv4 == NULL &&
2460 service->ipconfig_ipv6 == NULL)
2461 return __connman_error_invalid_property(msg);
2463 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2464 ipv4 = service->ipconfig_ipv4;
2465 err = set_ipconfig(service, ipv4, &value,
2466 service->state_ipv4, &state);
2468 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2469 ipv6 = service->ipconfig_ipv6;
2470 err = set_ipconfig(service, ipv6, &value,
2471 service->state_ipv6, &state);
2475 if (is_connected_state(service, state) ||
2476 is_connecting_state(service, state))
2477 __connman_network_set_ipconfig(service->network,
2479 return __connman_error_failed(msg, -err);
2483 ipv4_configuration_changed(service);
2485 ipv6_configuration_changed(service);
2487 if (is_connecting(service) || is_connected(service))
2488 __connman_network_set_ipconfig(service->network,
2491 __connman_storage_save_service(service);
2493 return __connman_error_invalid_property(msg);
2495 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2498 static void set_idle(struct connman_service *service)
2500 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
2501 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2502 state_changed(service);
2505 static DBusMessage *clear_property(DBusConnection *conn,
2506 DBusMessage *msg, void *user_data)
2508 struct connman_service *service = user_data;
2511 DBG("service %p", service);
2513 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2516 if (g_str_equal(name, "Error") == TRUE) {
2519 g_get_current_time(&service->modified);
2520 __connman_storage_save_service(service);
2521 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2522 if (service->immutable == TRUE)
2523 return __connman_error_not_supported(msg);
2525 g_free(service->passphrase);
2526 service->passphrase = NULL;
2528 passphrase_changed(service);
2530 __connman_storage_save_service(service);
2532 return __connman_error_invalid_property(msg);
2534 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2537 static connman_bool_t is_ignore(struct connman_service *service)
2539 if (service->autoconnect == FALSE)
2542 if (service->roaming == TRUE)
2545 if (service->ignore == TRUE)
2548 if (combine_state(service->state_ipv4, service->state_ipv6) ==
2549 CONNMAN_SERVICE_STATE_FAILURE)
2555 void __connman_service_auto_connect(void)
2557 struct connman_service *service = NULL;
2558 GSequenceIter *iter;
2562 if (__connman_session_mode() == TRUE) {
2563 DBG("Session mode enabled: auto connect disabled");
2567 iter = g_sequence_get_begin_iter(service_list);
2569 while (g_sequence_iter_is_end(iter) == FALSE) {
2570 service = g_sequence_get(iter);
2572 if (service->pending != NULL)
2575 if (is_connecting(service) == TRUE)
2578 if (service->favorite == FALSE)
2581 if (is_connected(service) == TRUE)
2584 if (is_ignore(service) == FALSE &&
2585 combine_state(service->state_ipv4,
2586 service->state_ipv6) ==
2587 CONNMAN_SERVICE_STATE_IDLE)
2592 iter = g_sequence_iter_next(iter);
2595 if (service != NULL) {
2596 service->userconnect = FALSE;
2597 __connman_service_connect(service);
2601 static void remove_timeout(struct connman_service *service)
2603 if (service->timeout > 0) {
2604 g_source_remove(service->timeout);
2605 service->timeout = 0;
2609 static void reply_pending(struct connman_service *service, int error)
2611 remove_timeout(service);
2613 if (service->pending != NULL) {
2617 reply = __connman_error_failed(service->pending,
2620 g_dbus_send_message(connection, reply);
2624 sender = dbus_message_get_interface(service->pending);
2626 DBG("sender %s", sender);
2628 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2629 g_dbus_send_reply(connection, service->pending,
2630 DBUS_TYPE_OBJECT_PATH, &service->path,
2633 g_dbus_send_reply(connection, service->pending,
2637 dbus_message_unref(service->pending);
2638 service->pending = NULL;
2642 static gboolean connect_timeout(gpointer user_data)
2644 struct connman_service *service = user_data;
2645 connman_bool_t autoconnect = FALSE;
2647 DBG("service %p", service);
2649 service->timeout = 0;
2651 if (service->network != NULL)
2652 __connman_network_disconnect(service->network);
2654 __connman_ipconfig_disable(service->ipconfig_ipv4);
2655 __connman_ipconfig_disable(service->ipconfig_ipv6);
2657 __connman_stats_service_unregister(service);
2659 if (service->pending != NULL) {
2662 reply = __connman_error_operation_timeout(service->pending);
2664 g_dbus_send_message(connection, reply);
2666 dbus_message_unref(service->pending);
2667 service->pending = NULL;
2671 __connman_service_indicate_state(service,
2672 CONNMAN_SERVICE_STATE_FAILURE,
2673 CONNMAN_IPCONFIG_TYPE_IPV4);
2674 __connman_service_indicate_state(service,
2675 CONNMAN_SERVICE_STATE_FAILURE,
2676 CONNMAN_IPCONFIG_TYPE_IPV6);
2678 if (autoconnect == TRUE && service->userconnect == FALSE)
2679 __connman_service_auto_connect();
2684 static void set_reconnect_state(struct connman_service *service,
2685 connman_bool_t reconnect)
2687 struct connman_device *device;
2689 if (service->network == NULL)
2692 device = connman_network_get_device(service->network);
2696 __connman_device_set_reconnect(device, reconnect);
2699 static connman_bool_t get_reconnect_state(struct connman_service *service)
2701 struct connman_device *device;
2703 if (service->network == NULL)
2706 device = connman_network_get_device(service->network);
2710 return __connman_device_get_reconnect(device);
2713 static void request_input_cb (struct connman_service *service,
2714 const char *identity, const char *passphrase,
2717 DBG ("RequestInput return, %p", service);
2719 if (identity == NULL && passphrase == NULL && service->wps == FALSE)
2722 if (identity != NULL)
2723 __connman_service_set_identity(service, identity);
2725 if (passphrase != NULL)
2726 __connman_service_set_passphrase(service, passphrase);
2728 __connman_service_connect(service);
2731 static DBusMessage *connect_service(DBusConnection *conn,
2732 DBusMessage *msg, void *user_data)
2734 struct connman_service *service = user_data;
2735 GSequenceIter *iter;
2738 DBG("service %p", service);
2740 if (service->pending != NULL)
2741 return __connman_error_in_progress(msg);
2743 iter = g_sequence_get_begin_iter(service_list);
2745 while (g_sequence_iter_is_end(iter) == FALSE) {
2746 struct connman_service *temp = g_sequence_get(iter);
2748 if (service->type == temp->type && is_connecting(temp) == TRUE)
2749 return __connman_error_in_progress(msg);
2751 iter = g_sequence_iter_next(iter);
2754 service->ignore = FALSE;
2756 service->userconnect = TRUE;
2758 service->pending = dbus_message_ref(msg);
2760 set_reconnect_state(service, FALSE);
2762 err = __connman_service_connect(service);
2764 if (err == -ENOKEY) {
2765 if (__connman_agent_request_input(service,
2771 if (service->pending == NULL)
2774 if (err != -EINPROGRESS) {
2775 dbus_message_unref(service->pending);
2776 service->pending = NULL;
2778 return __connman_error_failed(msg, -err);
2784 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2787 static DBusMessage *disconnect_service(DBusConnection *conn,
2788 DBusMessage *msg, void *user_data)
2790 struct connman_service *service = user_data;
2793 DBG("service %p", service);
2795 reply_pending(service, ECONNABORTED);
2797 service->ignore = TRUE;
2799 set_reconnect_state(service, FALSE);
2801 err = __connman_service_disconnect(service);
2803 if (err != -EINPROGRESS)
2804 return __connman_error_failed(msg, -err);
2809 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2812 static DBusMessage *remove_service(DBusConnection *conn,
2813 DBusMessage *msg, void *user_data)
2815 struct connman_service *service = user_data;
2817 DBG("service %p", service);
2819 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2820 return __connman_error_not_supported(msg);
2822 if (service->immutable == TRUE)
2823 return __connman_error_not_supported(msg);
2825 if (service->favorite == FALSE &&
2826 combine_state(service->state_ipv4, service->state_ipv6) !=
2827 CONNMAN_SERVICE_STATE_FAILURE)
2828 return __connman_error_not_supported(msg);
2830 if (service->network != NULL) {
2831 set_reconnect_state(service, FALSE);
2833 __connman_network_disconnect(service->network);
2836 g_free(service->passphrase);
2837 service->passphrase = NULL;
2839 passphrase_changed(service);
2843 __connman_service_set_favorite(service, FALSE);
2844 __connman_storage_save_service(service);
2846 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2849 static DBusMessage *move_service(DBusConnection *conn,
2850 DBusMessage *msg, void *user_data,
2853 struct connman_service *service = user_data;
2854 struct connman_service *target;
2856 GSequenceIter *src, *dst;
2858 DBG("service %p", service);
2860 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2863 if (service->favorite == FALSE)
2864 return __connman_error_not_supported(msg);
2866 target = find_service(path);
2867 if (target == NULL || target->favorite == FALSE || target == service)
2868 return __connman_error_invalid_service(msg);
2870 DBG("target %s", target->identifier);
2872 if (target->state_ipv4 != service->state_ipv4 &&
2873 target->state_ipv6 != service->state_ipv6)
2874 return __connman_error_invalid_service(msg);
2876 g_get_current_time(&service->modified);
2877 __connman_storage_save_service(service);
2879 src = g_hash_table_lookup(service_hash, service->identifier);
2880 dst = g_hash_table_lookup(service_hash, target->identifier);
2882 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2884 __connman_profile_changed(FALSE);
2886 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2889 static DBusMessage *move_before(DBusConnection *conn,
2890 DBusMessage *msg, void *user_data)
2892 return move_service(conn, msg, user_data, TRUE);
2895 static DBusMessage *move_after(DBusConnection *conn,
2896 DBusMessage *msg, void *user_data)
2898 return move_service(conn, msg, user_data, FALSE);
2901 static DBusMessage *reset_counters(DBusConnection *conn,
2902 DBusMessage *msg, void *user_data)
2904 struct connman_service *service = user_data;
2906 reset_stats(service);
2908 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2911 static GDBusMethodTable service_methods[] = {
2912 { "GetProperties", "", "a{sv}", get_properties },
2913 { "SetProperty", "sv", "", set_property },
2914 { "ClearProperty", "s", "", clear_property },
2915 { "Connect", "", "", connect_service,
2916 G_DBUS_METHOD_FLAG_ASYNC },
2917 { "Disconnect", "", "", disconnect_service },
2918 { "Remove", "", "", remove_service },
2919 { "MoveBefore", "o", "", move_before },
2920 { "MoveAfter", "o", "", move_after },
2921 { "ResetCounters", "", "", reset_counters },
2925 static GDBusSignalTable service_signals[] = {
2926 { "PropertyChanged", "sv" },
2930 static void service_free(gpointer user_data)
2932 struct connman_service *service = user_data;
2933 char *path = service->path;
2935 DBG("service %p", service);
2937 reply_pending(service, ENOENT);
2939 g_hash_table_remove(service_hash, service->identifier);
2941 __connman_notifier_service_remove(service);
2943 stats_stop(service);
2944 __connman_storage_save_service(service);
2946 service->path = NULL;
2949 __connman_profile_changed(FALSE);
2951 g_dbus_unregister_interface(connection, path,
2952 CONNMAN_SERVICE_INTERFACE);
2956 g_hash_table_destroy(service->counter_table);
2958 if (service->network != NULL)
2959 connman_network_unref(service->network);
2961 if (service->provider != NULL)
2962 connman_provider_unref(service->provider);
2964 if (service->ipconfig_ipv4 != NULL) {
2965 connman_ipconfig_unref(service->ipconfig_ipv4);
2966 service->ipconfig_ipv4 = NULL;
2969 if (service->ipconfig_ipv6 != NULL) {
2970 connman_ipconfig_unref(service->ipconfig_ipv6);
2971 service->ipconfig_ipv6 = NULL;
2974 if (service->location != NULL)
2975 connman_location_unref(service->location);
2977 g_strfreev(service->nameservers);
2978 g_strfreev(service->nameservers_config);
2979 g_strfreev(service->domains);
2980 g_strfreev(service->proxies);
2981 g_strfreev(service->excludes);
2983 g_free(service->domainname);
2984 g_free(service->pac);
2985 g_free(service->profile);
2986 g_free(service->name);
2987 g_free(service->passphrase);
2988 g_free(service->identifier);
2989 g_free(service->eap);
2990 g_free(service->identity);
2991 g_free(service->ca_cert_file);
2992 g_free(service->client_cert_file);
2993 g_free(service->private_key_file);
2994 g_free(service->private_key_passphrase);
2995 g_free(service->phase2);
2997 if (service->stats.timer != NULL)
2998 g_timer_destroy(service->stats.timer);
2999 if (service->stats_roaming.timer != NULL)
3000 g_timer_destroy(service->stats_roaming.timer);
3006 * __connman_service_put:
3007 * @service: service structure
3009 * Release service if no longer needed
3011 void __connman_service_put(struct connman_service *service)
3013 DBG("service %p", service);
3015 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
3016 GSequenceIter *iter;
3018 iter = g_hash_table_lookup(service_hash, service->identifier);
3020 reply_pending(service, ECONNABORTED);
3022 __connman_service_disconnect(service);
3024 g_sequence_remove(iter);
3026 service_free(service);
3030 static void stats_init(struct connman_service *service)
3033 service->stats.valid = FALSE;
3034 service->stats.enabled = FALSE;
3035 service->stats.timer = g_timer_new();
3038 service->stats_roaming.valid = FALSE;
3039 service->stats_roaming.enabled = FALSE;
3040 service->stats_roaming.timer = g_timer_new();
3043 static void service_initialize(struct connman_service *service)
3045 DBG("service %p", service);
3047 service->refcount = 1;
3049 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3050 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
3051 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3053 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3054 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3056 service->favorite = FALSE;
3057 service->immutable = FALSE;
3058 service->hidden = FALSE;
3060 service->ignore = FALSE;
3062 service->userconnect = FALSE;
3066 stats_init(service);
3068 service->provider = NULL;
3070 service->wps = FALSE;
3074 * connman_service_create:
3076 * Allocate a new service.
3078 * Returns: a newly-allocated #connman_service structure
3080 struct connman_service *connman_service_create(void)
3083 struct connman_stats_counter *counters;
3084 const char *counter;
3086 struct connman_service *service;
3088 service = g_try_new0(struct connman_service, 1);
3089 if (service == NULL)
3092 DBG("service %p", service);
3094 service->counter_table = g_hash_table_new_full(g_str_hash,
3095 g_str_equal, NULL, g_free);
3097 for (list = counter_list; list; list = list->next) {
3098 counter = list->data;
3100 counters = g_try_new0(struct connman_stats_counter, 1);
3101 if (counters == NULL) {
3102 g_hash_table_destroy(service->counter_table);
3107 counters->append_all = TRUE;
3109 g_hash_table_replace(service->counter_table, (gpointer)counter,
3113 service_initialize(service);
3115 service->location = __connman_location_create(service);
3120 struct connman_location *__connman_service_get_location(struct connman_service *service)
3122 return service->location;
3126 * connman_service_ref:
3127 * @service: service structure
3129 * Increase reference counter of service
3131 struct connman_service *connman_service_ref(struct connman_service *service)
3135 g_atomic_int_inc(&service->refcount);
3141 * connman_service_unref:
3142 * @service: service structure
3144 * Decrease reference counter of service
3146 void connman_service_unref(struct connman_service *service)
3148 __connman_service_put(service);
3151 static gint service_compare(gconstpointer a, gconstpointer b,
3154 struct connman_service *service_a = (void *) a;
3155 struct connman_service *service_b = (void *) b;
3156 enum connman_service_state state_a, state_b;
3158 state_a = combine_state(service_a->state_ipv4, service_a->state_ipv6);
3159 state_b = combine_state(service_b->state_ipv4, service_b->state_ipv6);
3161 if (state_a != state_b) {
3162 if (is_connected(service_a) == TRUE)
3164 if (is_connected(service_b) == TRUE)
3167 if (is_connecting(service_a) == TRUE)
3169 if (is_connecting(service_b) == TRUE)
3173 if (service_a->order > service_b->order)
3176 if (service_a->order < service_b->order)
3179 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3182 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3185 if (service_a->type != service_b->type) {
3186 switch (service_a->type) {
3187 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3188 case CONNMAN_SERVICE_TYPE_SYSTEM:
3189 case CONNMAN_SERVICE_TYPE_ETHERNET:
3190 case CONNMAN_SERVICE_TYPE_GPS:
3191 case CONNMAN_SERVICE_TYPE_VPN:
3192 case CONNMAN_SERVICE_TYPE_GADGET:
3194 case CONNMAN_SERVICE_TYPE_WIFI:
3196 case CONNMAN_SERVICE_TYPE_WIMAX:
3197 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3198 case CONNMAN_SERVICE_TYPE_CELLULAR:
3203 return (gint) service_b->strength - (gint) service_a->strength;
3207 * connman_service_get_type:
3208 * @service: service structure
3210 * Get the type of service
3212 enum connman_service_type connman_service_get_type(struct connman_service *service)
3214 if (service == NULL)
3215 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3217 return service->type;
3221 * connman_service_get_interface:
3222 * @service: service structure
3224 * Get network interface of service
3226 char *connman_service_get_interface(struct connman_service *service)
3230 if (service == NULL)
3233 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3234 if (service->ipconfig_ipv4)
3235 index = connman_ipconfig_get_index(
3236 service->ipconfig_ipv4);
3237 else if (service->ipconfig_ipv6)
3238 index = connman_ipconfig_get_index(
3239 service->ipconfig_ipv6);
3243 return connman_inet_ifname(index);
3246 if (service->network == NULL)
3249 index = connman_network_get_index(service->network);
3251 return connman_inet_ifname(index);
3255 * connman_service_get_network:
3256 * @service: service structure
3258 * Get the service network
3260 struct connman_network *
3261 __connman_service_get_network(struct connman_service *service)
3263 if (service == NULL)
3266 return service->network;
3269 struct connman_ipconfig *
3270 __connman_service_get_ip4config(struct connman_service *service)
3272 if (service == NULL)
3275 return service->ipconfig_ipv4;
3278 struct connman_ipconfig *
3279 __connman_service_get_ip6config(struct connman_service *service)
3281 if (service == NULL)
3284 return service->ipconfig_ipv6;
3287 struct connman_ipconfig *
3288 __connman_service_get_ipconfig(struct connman_service *service, int family)
3290 if (family == AF_INET)
3291 return __connman_service_get_ip4config(service);
3292 else if (family == AF_INET6)
3293 return __connman_service_get_ip6config(service);
3299 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3301 if (service == NULL)
3302 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3304 return service->security;
3307 const char *__connman_service_get_phase2(struct connman_service *service)
3309 if (service == NULL)
3312 return service->phase2;
3315 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3317 if (service == NULL)
3320 return service->wps;
3324 * __connman_service_set_favorite:
3325 * @service: service structure
3326 * @favorite: favorite value
3328 * Change the favorite setting of service
3330 int __connman_service_set_favorite(struct connman_service *service,
3331 connman_bool_t favorite)
3333 GSequenceIter *iter;
3335 iter = g_hash_table_lookup(service_hash, service->identifier);
3339 if (service->favorite == favorite)
3342 service->favorite = favorite;
3344 favorite_changed(service);
3346 g_sequence_sort_changed(iter, service_compare, NULL);
3348 __connman_profile_changed(FALSE);
3353 connman_bool_t __connman_service_get_immutable(struct connman_service *service)
3355 if (service == NULL)
3358 return service->immutable;
3361 int __connman_service_set_immutable(struct connman_service *service,
3362 connman_bool_t immutable)
3364 service->immutable = immutable;
3366 immutable_changed(service);
3371 void __connman_service_set_string(struct connman_service *service,
3372 const char *key, const char *value)
3374 if (g_str_equal(key, "EAP") == TRUE) {
3375 g_free(service->eap);
3376 service->eap = g_strdup(value);
3377 } else if (g_str_equal(key, "Identity") == TRUE) {
3378 g_free(service->identity);
3379 service->identity = g_strdup(value);
3380 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3381 g_free(service->ca_cert_file);
3382 service->ca_cert_file = g_strdup(value);
3383 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3384 g_free(service->client_cert_file);
3385 service->client_cert_file = g_strdup(value);
3386 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3387 g_free(service->private_key_file);
3388 service->private_key_file = g_strdup(value);
3389 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3390 g_free(service->private_key_passphrase);
3391 service->private_key_passphrase = g_strdup(value);
3392 } else if (g_str_equal(key, "Phase2") == TRUE) {
3393 g_free(service->phase2);
3394 service->phase2 = g_strdup(value);
3395 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3396 g_free(service->passphrase);
3397 service->passphrase = g_strdup(value);
3401 static void service_complete(struct connman_service *service)
3403 reply_pending(service, EIO);
3405 if (service->userconnect == FALSE)
3406 __connman_service_auto_connect();
3408 g_get_current_time(&service->modified);
3409 __connman_storage_save_service(service);
3412 static void report_error_cb(struct connman_service *service,
3413 gboolean retry, void *user_data)
3416 __connman_service_connect(service);
3418 service_complete(service);
3419 __connman_profile_changed(FALSE);
3420 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3424 int __connman_service_indicate_state(struct connman_service *service,
3425 enum connman_service_state new_state,
3426 enum connman_ipconfig_type type)
3428 enum connman_service_state service_state,
3429 state = CONNMAN_SERVICE_STATE_IDLE;
3430 GSequenceIter *iter;
3432 if (service == NULL)
3435 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3436 if (service->state_ipv4 == new_state)
3438 state = combine_state(new_state, service->state_ipv6);
3439 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3440 if (service->state_ipv6 == new_state)
3442 state = combine_state(new_state, service->state_ipv4);
3446 service_state = combine_state(service->state_ipv4,
3447 service->state_ipv6);
3449 DBG("service %p state %s/%s => %s new %s/%d => %s",
3451 state2string(service->state_ipv4),
3452 state2string(service->state_ipv6),
3453 state2string(service_state),
3454 state2string(new_state),
3456 state2string(state));
3458 if (service_state == CONNMAN_SERVICE_STATE_FAILURE &&
3459 state == CONNMAN_SERVICE_STATE_IDLE)
3462 if (service_state == CONNMAN_SERVICE_STATE_IDLE &&
3463 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3466 if (state == CONNMAN_SERVICE_STATE_IDLE &&
3467 service_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3468 state_changed(service);
3470 reply_pending(service, ECONNABORTED);
3472 __connman_service_disconnect(service);
3475 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3476 if (__connman_stats_service_register(service) == 0) {
3477 __connman_stats_get(service, FALSE,
3478 &service->stats.data);
3479 __connman_stats_get(service, TRUE,
3480 &service->stats_roaming.data);
3483 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
3484 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3485 __connman_ipconfig_enable(service->ipconfig_ipv4);
3486 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
3487 new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3488 __connman_ipconfig_enable(service->ipconfig_ipv6);
3491 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3492 service->state_ipv4 = new_state;
3493 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3494 service->state_ipv6 = new_state;
3496 if (state == service_state)
3499 state_changed(service);
3501 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3502 if (service->login_required == TRUE) {
3503 service->login_required = FALSE;
3504 login_changed(service);
3507 connman_timeserver_sync();
3510 if (state == CONNMAN_SERVICE_STATE_IDLE) {
3511 connman_bool_t reconnect;
3513 reconnect = get_reconnect_state(service);
3514 if (reconnect == TRUE)
3515 __connman_service_auto_connect();
3518 if (state == CONNMAN_SERVICE_STATE_READY) {
3519 enum connman_service_proxy_method proxy_config;
3520 enum connman_ipconfig_method method;
3522 set_reconnect_state(service, TRUE);
3524 __connman_service_set_favorite(service, TRUE);
3526 reply_pending(service, 0);
3528 service->userconnect = FALSE;
3530 g_get_current_time(&service->modified);
3531 __connman_storage_save_service(service);
3533 update_nameservers(service);
3534 dns_changed(service);
3535 domain_changed(service);
3537 proxy_config = service->proxy_config;
3540 * We start WPAD if we haven't got a PAC URL from DHCP and
3541 * if our proxy manual configuration is either empty or set
3542 * to AUTO with an empty URL.
3544 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3545 (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3547 CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3548 service->pac == NULL)))
3549 if (__connman_wpad_start(service) < 0) {
3551 CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3553 __connman_notifier_proxy_changed(service);
3556 __connman_notifier_connect(service->type);
3558 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3559 connman_network_get_bool(service->network,
3560 "WiFi.UseWPS") == TRUE) {
3563 pass = connman_network_get_string(service->network,
3566 __connman_service_set_passphrase(service, pass);
3568 connman_network_set_bool(service->network,
3569 "WiFi.UseWPS", FALSE);
3574 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3575 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3576 __connman_ipconfig_disable_ipv6(
3577 service->ipconfig_ipv6);
3579 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3580 __connman_location_finish(service);
3584 __connman_wpad_stop(service);
3586 update_nameservers(service);
3587 dns_changed(service);
3588 domain_changed(service);
3590 __connman_notifier_disconnect(service->type);
3593 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3594 if (service->userconnect == TRUE &&
3595 __connman_agent_report_error(service,
3596 error2string(service->error),
3597 report_error_cb, NULL) == -EIO)
3599 service_complete(service);
3601 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3603 iter = g_hash_table_lookup(service_hash, service->identifier);
3605 g_sequence_sort_changed(iter, service_compare, NULL);
3607 __connman_profile_changed(FALSE);
3609 service_state = combine_state(service->state_ipv4,
3610 service->state_ipv6);
3611 if (service_state == CONNMAN_SERVICE_STATE_ONLINE)
3614 if (service_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3615 struct connman_service *def_service = get_default();
3617 if (__connman_notifier_count_connected() == 0 &&
3618 def_service != NULL &&
3619 def_service->provider != NULL)
3620 __connman_provider_disconnect(def_service->provider);
3623 if (service_state == CONNMAN_SERVICE_STATE_IDLE ||
3624 service_state == CONNMAN_SERVICE_STATE_FAILURE)
3625 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3630 int __connman_service_indicate_error(struct connman_service *service,
3631 enum connman_service_error error)
3633 DBG("service %p error %d", service, error);
3635 if (service == NULL)
3638 service->error = error;
3640 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3641 __connman_service_set_passphrase(service, NULL);
3643 return __connman_service_indicate_state(service,
3644 CONNMAN_SERVICE_STATE_FAILURE,
3645 CONNMAN_IPCONFIG_TYPE_IPV4);
3648 int __connman_service_clear_error(struct connman_service *service)
3650 enum connman_service_state state;
3652 DBG("service %p", service);
3654 if (service == NULL)
3657 state = combine_state(service->state_ipv4, service->state_ipv6);
3659 if (state != CONNMAN_SERVICE_STATE_FAILURE)
3662 service->state_ipv4 = service->state_ipv6 =
3663 CONNMAN_SERVICE_STATE_UNKNOWN;
3664 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3666 if (service->favorite == TRUE)
3667 set_reconnect_state(service, TRUE);
3669 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
3670 CONNMAN_IPCONFIG_TYPE_IPV6);
3673 * Toggling the IPv6 state to IDLE could trigger the auto connect
3674 * machinery and consequently the IPv4 state.
3676 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3677 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3680 return __connman_service_indicate_state(service,
3681 CONNMAN_SERVICE_STATE_IDLE,
3682 CONNMAN_IPCONFIG_TYPE_IPV4);
3685 int __connman_service_indicate_default(struct connman_service *service)
3687 DBG("service %p", service);
3691 __connman_location_detect(service);
3696 int __connman_service_request_login(struct connman_service *service)
3698 DBG("service %p", service);
3700 if (service == NULL)
3703 service->login_required = TRUE;
3704 login_changed(service);
3709 static connman_bool_t prepare_network(struct connman_service *service)
3711 enum connman_network_type type;
3712 unsigned int ssid_len;
3714 type = connman_network_get_type(service->network);
3717 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3718 case CONNMAN_NETWORK_TYPE_VENDOR:
3720 case CONNMAN_NETWORK_TYPE_WIFI:
3721 if (connman_network_get_blob(service->network, "WiFi.SSID",
3725 if (service->passphrase != NULL)
3726 connman_network_set_string(service->network,
3727 "WiFi.Passphrase", service->passphrase);
3729 case CONNMAN_NETWORK_TYPE_ETHERNET:
3730 case CONNMAN_NETWORK_TYPE_WIMAX:
3731 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3732 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3733 case CONNMAN_NETWORK_TYPE_CELLULAR:
3740 static void prepare_8021x(struct connman_service *service)
3742 if (service->eap != NULL)
3743 connman_network_set_string(service->network, "WiFi.EAP",
3746 if (service->identity != NULL)
3747 connman_network_set_string(service->network, "WiFi.Identity",
3750 if (service->ca_cert_file != NULL)
3751 connman_network_set_string(service->network, "WiFi.CACertFile",
3752 service->ca_cert_file);
3754 if (service->client_cert_file != NULL)
3755 connman_network_set_string(service->network,
3756 "WiFi.ClientCertFile",
3757 service->client_cert_file);
3759 if (service->private_key_file != NULL)
3760 connman_network_set_string(service->network,
3761 "WiFi.PrivateKeyFile",
3762 service->private_key_file);
3764 if (service->private_key_passphrase != NULL)
3765 connman_network_set_string(service->network,
3766 "WiFi.PrivateKeyPassphrase",
3767 service->private_key_passphrase);
3769 if (service->phase2 != NULL)
3770 connman_network_set_string(service->network, "WiFi.Phase2",
3774 static int service_connect(struct connman_service *service)
3778 switch (service->type) {
3779 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3780 case CONNMAN_SERVICE_TYPE_SYSTEM:
3781 case CONNMAN_SERVICE_TYPE_GPS:
3782 case CONNMAN_SERVICE_TYPE_GADGET:
3784 case CONNMAN_SERVICE_TYPE_ETHERNET:
3785 case CONNMAN_SERVICE_TYPE_WIMAX:
3786 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3787 case CONNMAN_SERVICE_TYPE_CELLULAR:
3788 case CONNMAN_SERVICE_TYPE_VPN:
3790 case CONNMAN_SERVICE_TYPE_WIFI:
3791 switch (service->security) {
3792 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3793 case CONNMAN_SERVICE_SECURITY_NONE:
3795 case CONNMAN_SERVICE_SECURITY_WEP:
3796 case CONNMAN_SERVICE_SECURITY_PSK:
3797 case CONNMAN_SERVICE_SECURITY_WPA:
3798 case CONNMAN_SERVICE_SECURITY_RSN:
3799 if (service->passphrase == NULL) {
3800 if (service->network == NULL)
3803 if (service->wps == FALSE ||
3804 connman_network_get_bool(
3806 "WiFi.UseWPS") == FALSE)
3810 case CONNMAN_SERVICE_SECURITY_8021X:
3811 if (service->eap == NULL)
3815 * never request credentials if using EAP-TLS
3816 * (EAP-TLS networks need to be fully provisioned)
3818 if (g_str_equal(service->eap, "tls") == TRUE)
3821 if (service->immutable != TRUE)
3829 if (service->network != NULL) {
3830 if (prepare_network(service) == FALSE)
3833 switch (service->security) {
3834 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3835 case CONNMAN_SERVICE_SECURITY_NONE:
3836 case CONNMAN_SERVICE_SECURITY_WEP:
3837 case CONNMAN_SERVICE_SECURITY_PSK:
3838 case CONNMAN_SERVICE_SECURITY_WPA:
3839 case CONNMAN_SERVICE_SECURITY_RSN:
3841 case CONNMAN_SERVICE_SECURITY_8021X:
3842 prepare_8021x(service);
3846 if (__connman_stats_service_register(service) == 0) {
3847 __connman_stats_get(service, FALSE,
3848 &service->stats.data);
3849 __connman_stats_get(service, TRUE,
3850 &service->stats_roaming.data);
3853 if (service->ipconfig_ipv4)
3854 __connman_ipconfig_enable(service->ipconfig_ipv4);
3855 if (service->ipconfig_ipv6)
3856 __connman_ipconfig_enable(service->ipconfig_ipv6);
3858 err = __connman_network_connect(service->network);
3859 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3860 service->provider != NULL)
3861 err = __connman_provider_connect(service->provider);
3866 if (err != -EINPROGRESS) {
3867 __connman_ipconfig_disable(service->ipconfig_ipv4);
3868 __connman_ipconfig_disable(service->ipconfig_ipv6);
3869 __connman_stats_service_unregister(service);
3877 int __connman_service_connect(struct connman_service *service)
3879 enum connman_service_state state;
3882 state = combine_state(service->state_ipv4, service->state_ipv6);
3884 DBG("service %p state %s", service, state2string(state));
3886 if (is_connected(service) == TRUE)
3889 if (is_connecting(service) == TRUE)
3892 switch (service->type) {
3893 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3894 case CONNMAN_SERVICE_TYPE_SYSTEM:
3895 case CONNMAN_SERVICE_TYPE_GPS:
3896 case CONNMAN_SERVICE_TYPE_GADGET:
3899 err = service_connect(service);
3905 if (err == -EINPROGRESS) {
3906 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
3907 connect_timeout, service);
3909 return -EINPROGRESS;
3915 if (service->userconnect == TRUE)
3916 reply_pending(service, err);
3918 __connman_service_indicate_state(service,
3919 CONNMAN_SERVICE_STATE_FAILURE,
3920 CONNMAN_IPCONFIG_TYPE_IPV4);
3921 __connman_service_indicate_state(service,
3922 CONNMAN_SERVICE_STATE_FAILURE,
3923 CONNMAN_IPCONFIG_TYPE_IPV6);
3928 int __connman_service_disconnect(struct connman_service *service)
3932 DBG("service %p", service);
3934 if (service->network != NULL) {
3935 err = __connman_network_disconnect(service->network);
3936 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3937 service->provider != NULL)
3938 err = __connman_provider_disconnect(service->provider);
3942 if (err < 0 && err != -EINPROGRESS)
3945 __connman_6to4_remove(service->ipconfig_ipv4);
3947 if (service->ipconfig_ipv4)
3948 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3951 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3954 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3955 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3957 __connman_ipconfig_disable(service->ipconfig_ipv4);
3958 __connman_ipconfig_disable(service->ipconfig_ipv6);
3960 __connman_stats_service_unregister(service);
3965 int __connman_service_disconnect_all(void)
3967 GSequenceIter *iter;
3971 iter = g_sequence_get_begin_iter(service_list);
3973 while (g_sequence_iter_is_end(iter) == FALSE) {
3974 struct connman_service *service = g_sequence_get(iter);
3976 service->ignore = TRUE;
3978 set_reconnect_state(service, FALSE);
3980 __connman_service_disconnect(service);
3982 iter = g_sequence_iter_next(iter);
3990 * __connman_service_lookup:
3991 * @pattern: search pattern
3992 * @path: return object path
3994 * Look up a service path from a search pattern
3996 int __connman_service_lookup(const char *pattern, const char **path)
3998 GHashTableIter iter;
3999 gpointer key, value;
4001 g_hash_table_iter_init(&iter, service_hash);
4003 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4004 GSequenceIter *iter = value;
4005 struct connman_service *service = g_sequence_get(iter);
4007 if (g_strcmp0(service->identifier, pattern) == 0 ||
4008 g_strcmp0(service->name, pattern) == 0) {
4009 *path = (const char *) service->path;
4018 * lookup_by_identifier:
4019 * @identifier: service identifier
4021 * Look up a service by identifier (reference count will not be increased)
4023 static struct connman_service *lookup_by_identifier(const char *identifier)
4025 GSequenceIter *iter;
4027 iter = g_hash_table_lookup(service_hash, identifier);
4029 return g_sequence_get(iter);
4034 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4035 const char *ssid, const char *mode, const char *security)
4037 struct connman_network *network;
4040 unsigned int i, ssid_len;
4042 ssid_len = strlen(ssid);
4046 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
4047 if (network == NULL)
4050 connman_network_set_blob(network, "WiFi.SSID",
4051 (unsigned char *) ssid, ssid_len);
4053 connman_network_set_string(network, "WiFi.Mode", mode);
4054 connman_network_set_string(network, "WiFi.Security", security);
4056 name = g_try_malloc0(ssid_len + 1);
4058 connman_network_unref(network);
4062 for (i = 0; i < ssid_len; i++) {
4063 if (g_ascii_isprint(ssid[i]))
4069 connman_network_set_name(network, name);
4073 index = connman_device_get_index(device);
4074 connman_network_set_index(network, index);
4076 if (connman_device_add_network(device, network) < 0) {
4077 connman_network_unref(network);
4081 connman_network_set_available(network, TRUE);
4086 int __connman_service_create_and_connect(DBusMessage *msg)
4088 struct connman_service *service;
4089 struct connman_network *network;
4090 struct connman_device *device;
4091 DBusMessageIter iter, array;
4092 const char *mode = "managed", *security = "none", *group_security;
4093 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4094 unsigned int ssid_len = 0;
4097 gboolean created = FALSE;
4100 dbus_message_iter_init(msg, &iter);
4101 dbus_message_iter_recurse(&iter, &array);
4103 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4104 DBusMessageIter entry, value;
4107 dbus_message_iter_recurse(&array, &entry);
4108 dbus_message_iter_get_basic(&entry, &key);
4110 dbus_message_iter_next(&entry);
4111 dbus_message_iter_recurse(&entry, &value);
4113 switch (dbus_message_iter_get_arg_type(&value)) {
4114 case DBUS_TYPE_STRING:
4115 if (g_str_equal(key, "Type") == TRUE)
4116 dbus_message_iter_get_basic(&value, &type);
4117 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4118 g_str_equal(key, "Mode") == TRUE)
4119 dbus_message_iter_get_basic(&value, &mode);
4120 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4121 g_str_equal(key, "Security") == TRUE)
4122 dbus_message_iter_get_basic(&value, &security);
4123 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4124 g_str_equal(key, "Passphrase") == TRUE)
4125 dbus_message_iter_get_basic(&value, &passphrase);
4126 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4127 g_str_equal(key, "SSID") == TRUE)
4128 dbus_message_iter_get_basic(&value, &ssid);
4131 dbus_message_iter_next(&array);
4137 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4143 ssid_len = strlen(ssid);
4147 if (g_strcmp0(security, "none") != 0 &&
4148 g_strcmp0(security, "wep") != 0 &&
4149 g_strcmp0(security, "psk") != 0 &&
4150 g_strcmp0(security, "wpa") != 0 &&
4151 g_strcmp0(security, "rsn") != 0 &&
4152 g_strcmp0(security, "ieee8021x") != 0)
4155 device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4159 ident = connman_device_get_ident(device);
4164 if (!g_strcmp0(security, "wpa") ||
4165 !g_strcmp0(security, "rsn"))
4166 group_security = "psk";
4168 group_security = security;
4170 group = connman_wifi_build_group_name((unsigned char *) ssid,
4171 ssid_len, mode, group_security);
4175 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4177 service = lookup_by_identifier(name);
4179 if (service != NULL)
4182 network = create_hidden_wifi(device, ssid, mode, security);
4183 if (network != NULL) {
4184 connman_network_set_group(network, group);
4188 service = lookup_by_identifier(name);
4194 if (service == NULL) {
4199 if (is_connected(service) == TRUE) {
4204 if (is_connecting(service) == TRUE) {
4209 set_reconnect_state(service, FALSE);
4211 __connman_device_disconnect(device);
4213 if (passphrase != NULL) {
4214 g_free(service->passphrase);
4215 service->passphrase = g_strdup(passphrase);
4218 service->userconnect = TRUE;
4220 err = __connman_service_connect(service);
4221 if (err < 0 && err != -EINPROGRESS)
4224 service->pending = dbus_message_ref(msg);
4229 if (service != NULL && created == TRUE) {
4230 struct connman_network *network = service->network;
4232 if (network != NULL) {
4233 connman_network_set_available(network, FALSE);
4234 __connman_device_cleanup_networks(device);
4236 __connman_service_put(service);
4242 int __connman_service_provision(DBusMessage *msg)
4244 GKeyFile *keyfile = NULL;
4245 const char *config_str = NULL;
4246 char *group = NULL, *ident = NULL;
4251 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4254 if (config_str == NULL || strlen(config_str) == 0)
4257 keyfile = g_key_file_new();
4259 /* populate GKeyFile with config_str */
4260 if (g_key_file_load_from_data(keyfile, config_str,
4261 strlen(config_str), 0, NULL) == FALSE) {
4267 * read only one group of settings (only one service supported, no
4270 group = g_key_file_get_start_group(keyfile);
4272 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4277 err = __connman_config_load_service(keyfile, group, TRUE);
4281 ident = group + strlen("service_");
4283 /* trigger service provisioning if service exists */
4284 service = lookup_by_identifier(ident);
4285 if (service != NULL)
4286 __connman_config_provision_service(service);
4288 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4294 if (keyfile != NULL)
4295 g_key_file_free(keyfile);
4301 * __connman_service_get:
4302 * @identifier: service identifier
4304 * Look up a service by identifier or create a new one if not found
4306 static struct connman_service *service_get(const char *identifier)
4308 struct connman_service *service;
4309 GSequenceIter *iter;
4311 iter = g_hash_table_lookup(service_hash, identifier);
4313 service = g_sequence_get(iter);
4314 if (service != NULL)
4315 connman_service_ref(service);
4319 service = connman_service_create();
4320 if (service == NULL)
4323 DBG("service %p", service);
4325 service->identifier = g_strdup(identifier);
4327 service->profile = g_strdup(__connman_profile_active_ident());
4329 iter = g_sequence_insert_sorted(service_list, service,
4330 service_compare, NULL);
4332 g_hash_table_insert(service_hash, service->identifier, iter);
4337 static int service_register(struct connman_service *service)
4339 const char *path = __connman_profile_active_path();
4340 GSequenceIter *iter;
4342 DBG("service %p", service);
4344 if (service->path != NULL)
4347 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4349 DBG("path %s", service->path);
4351 __connman_config_provision_service(service);
4353 __connman_storage_load_service(service);
4355 g_dbus_register_interface(connection, service->path,
4356 CONNMAN_SERVICE_INTERFACE,
4357 service_methods, service_signals,
4358 NULL, service, NULL);
4360 iter = g_hash_table_lookup(service_hash, service->identifier);
4362 g_sequence_sort_changed(iter, service_compare, NULL);
4364 __connman_profile_changed(TRUE);
4369 static void service_up(struct connman_ipconfig *ipconfig)
4371 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4373 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
4375 link_changed(service);
4377 service->stats.valid = FALSE;
4378 service->stats_roaming.valid = FALSE;
4381 static void service_down(struct connman_ipconfig *ipconfig)
4383 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
4386 static void service_lower_up(struct connman_ipconfig *ipconfig)
4388 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4390 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4392 stats_start(service);
4395 static void service_lower_down(struct connman_ipconfig *ipconfig)
4397 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4399 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4401 stats_stop(service);
4402 __connman_storage_save_service(service);
4405 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4407 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4408 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4409 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4411 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4413 type = __connman_ipconfig_get_config_type(ipconfig);
4414 method = __connman_ipconfig_get_method(ipconfig);
4416 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4419 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4420 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4421 __connman_service_indicate_state(service,
4422 CONNMAN_SERVICE_STATE_READY,
4423 CONNMAN_IPCONFIG_TYPE_IPV6);
4425 settings_changed(service, ipconfig);
4428 static void service_ip_release(struct connman_ipconfig *ipconfig)
4430 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4431 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4432 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4434 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4436 type = __connman_ipconfig_get_config_type(ipconfig);
4437 method = __connman_ipconfig_get_method(ipconfig);
4439 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4442 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4443 method == CONNMAN_IPCONFIG_METHOD_OFF)
4444 __connman_service_indicate_state(service,
4445 CONNMAN_SERVICE_STATE_DISCONNECT,
4446 CONNMAN_IPCONFIG_TYPE_IPV6);
4448 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4449 method == CONNMAN_IPCONFIG_METHOD_OFF)
4450 __connman_service_indicate_state(service,
4451 CONNMAN_SERVICE_STATE_DISCONNECT,
4452 CONNMAN_IPCONFIG_TYPE_IPV4);
4454 settings_changed(service, ipconfig);
4457 static const struct connman_ipconfig_ops service_ops = {
4459 .down = service_down,
4460 .lower_up = service_lower_up,
4461 .lower_down = service_lower_down,
4462 .ip_bound = service_ip_bound,
4463 .ip_release = service_ip_release,
4466 static void setup_ip4config(struct connman_service *service, int index)
4471 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4472 CONNMAN_IPCONFIG_TYPE_IPV4);
4473 if (service->ipconfig_ipv4 == NULL)
4476 connman_ipconfig_set_method(service->ipconfig_ipv4,
4477 CONNMAN_IPCONFIG_METHOD_DHCP);
4479 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4481 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4484 static void setup_ip6config(struct connman_service *service, int index)
4489 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4490 CONNMAN_IPCONFIG_TYPE_IPV6);
4491 if (service->ipconfig_ipv6 == NULL)
4494 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4496 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4499 void __connman_service_create_ip4config(struct connman_service *service,
4502 const char *ident = service->profile;
4505 DBG("ipv4 %p", service->ipconfig_ipv4);
4507 if (service->ipconfig_ipv4 != NULL)
4510 setup_ip4config(service, index);
4515 keyfile = __connman_storage_open_profile(ident);
4516 if (keyfile == NULL)
4519 if (service->ipconfig_ipv4)
4520 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4521 service->identifier, "IPv4.");
4522 g_key_file_free(keyfile);
4525 void __connman_service_create_ip6config(struct connman_service *service,
4528 const char *ident = service->profile;
4531 DBG("ipv6 %p", service->ipconfig_ipv6);
4533 if (service->ipconfig_ipv6 != NULL)
4536 setup_ip6config(service, index);
4541 keyfile = __connman_storage_open_profile(ident);
4542 if (keyfile == NULL)
4545 if (service->ipconfig_ipv6 != NULL)
4546 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4547 service->identifier, "IPv6.");
4549 g_key_file_free(keyfile);
4553 * __connman_service_lookup_from_network:
4554 * @network: network structure
4556 * Look up a service by network (reference count will not be increased)
4558 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4560 struct connman_service *service;
4561 const char *ident, *group;
4564 DBG("network %p", network);
4566 ident = __connman_network_get_ident(network);
4570 group = connman_network_get_group(network);
4574 name = g_strdup_printf("%s_%s_%s",
4575 __connman_network_get_type(network), ident, group);
4576 service = lookup_by_identifier(name);
4582 struct connman_service *__connman_service_lookup_from_index(int index)
4584 struct connman_service *service;
4585 GSequenceIter *iter;
4587 iter = g_sequence_get_begin_iter(service_list);
4589 while (g_sequence_iter_is_end(iter) == FALSE) {
4590 service = g_sequence_get(iter);
4592 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4596 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4600 iter = g_sequence_iter_next(iter);
4606 const char *__connman_service_get_ident(struct connman_service *service)
4608 return service->identifier;
4611 const char *__connman_service_get_path(struct connman_service *service)
4613 return service->path;
4616 unsigned int __connman_service_get_order(struct connman_service *service)
4618 GSequenceIter *iter;
4620 if (service == NULL)
4623 if (service->favorite == FALSE) {
4628 iter = g_hash_table_lookup(service_hash, service->identifier);
4630 if (g_sequence_iter_get_position(iter) == 0)
4632 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4633 service->order = 10;
4639 return service->order;
4642 static enum connman_service_type convert_network_type(struct connman_network *network)
4644 enum connman_network_type type = connman_network_get_type(network);
4647 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4648 case CONNMAN_NETWORK_TYPE_VENDOR:
4650 case CONNMAN_NETWORK_TYPE_ETHERNET:
4651 return CONNMAN_SERVICE_TYPE_ETHERNET;
4652 case CONNMAN_NETWORK_TYPE_WIFI:
4653 return CONNMAN_SERVICE_TYPE_WIFI;
4654 case CONNMAN_NETWORK_TYPE_WIMAX:
4655 return CONNMAN_SERVICE_TYPE_WIMAX;
4656 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4657 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4658 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4659 case CONNMAN_NETWORK_TYPE_CELLULAR:
4660 return CONNMAN_SERVICE_TYPE_CELLULAR;
4663 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4666 static enum connman_service_mode convert_wifi_mode(const char *mode)
4669 return CONNMAN_SERVICE_MODE_UNKNOWN;
4670 else if (g_str_equal(mode, "managed") == TRUE)
4671 return CONNMAN_SERVICE_MODE_MANAGED;
4672 else if (g_str_equal(mode, "adhoc") == TRUE)
4673 return CONNMAN_SERVICE_MODE_ADHOC;
4675 return CONNMAN_SERVICE_MODE_UNKNOWN;
4678 static enum connman_service_security convert_wifi_security(const char *security)
4680 if (security == NULL)
4681 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4682 else if (g_str_equal(security, "none") == TRUE)
4683 return CONNMAN_SERVICE_SECURITY_NONE;
4684 else if (g_str_equal(security, "wep") == TRUE)
4685 return CONNMAN_SERVICE_SECURITY_WEP;
4686 else if (g_str_equal(security, "psk") == TRUE)
4687 return CONNMAN_SERVICE_SECURITY_PSK;
4688 else if (g_str_equal(security, "ieee8021x") == TRUE)
4689 return CONNMAN_SERVICE_SECURITY_8021X;
4690 else if (g_str_equal(security, "wpa") == TRUE)
4691 return CONNMAN_SERVICE_SECURITY_WPA;
4692 else if (g_str_equal(security, "rsn") == TRUE)
4693 return CONNMAN_SERVICE_SECURITY_RSN;
4695 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4698 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
4703 return CONNMAN_SERVICE_MODE_GPRS;
4705 return CONNMAN_SERVICE_MODE_EDGE;
4710 return CONNMAN_SERVICE_MODE_UMTS;
4713 return CONNMAN_SERVICE_MODE_UNKNOWN;
4716 static void update_from_network(struct connman_service *service,
4717 struct connman_network *network)
4719 connman_uint8_t strength = service->strength;
4720 GSequenceIter *iter;
4723 DBG("service %p network %p", service, network);
4725 if (is_connected(service) == TRUE)
4728 if (is_connecting(service) == TRUE)
4731 str = connman_network_get_string(network, "Name");
4733 g_free(service->name);
4734 service->name = g_strdup(str);
4735 service->hidden = FALSE;
4737 g_free(service->name);
4738 service->name = NULL;
4739 service->hidden = TRUE;
4742 service->strength = connman_network_get_uint8(network, "Strength");
4743 service->roaming = connman_network_get_bool(network, "Roaming");
4745 if (service->strength == 0) {
4747 * Filter out 0-values; it's unclear what they mean
4748 * and they cause anomalous sorting of the priority list.
4750 service->strength = strength;
4753 str = connman_network_get_string(network, "WiFi.Mode");
4754 service->mode = convert_wifi_mode(str);
4756 str = connman_network_get_string(network, "WiFi.Security");
4757 service->security = convert_wifi_security(str);
4759 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
4760 connman_uint8_t value = connman_network_get_uint8(network,
4763 service->mode = convert_cellular_mode(value);
4764 } else if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4765 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4767 if (service->strength > strength && service->network != NULL) {
4768 connman_network_unref(service->network);
4769 service->network = connman_network_ref(network);
4771 strength_changed(service);
4774 if (service->network == NULL)
4775 service->network = connman_network_ref(network);
4777 iter = g_hash_table_lookup(service_hash, service->identifier);
4779 g_sequence_sort_changed(iter, service_compare, NULL);
4783 * __connman_service_create_from_network:
4784 * @network: network structure
4786 * Look up service by network and if not found, create one
4788 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4790 struct connman_service *service;
4791 struct connman_device *device;
4792 const char *ident, *group;
4796 DBG("network %p", network);
4798 ident = __connman_network_get_ident(network);
4802 group = connman_network_get_group(network);
4806 name = g_strdup_printf("%s_%s_%s",
4807 __connman_network_get_type(network), ident, group);
4808 service = service_get(name);
4811 if (service == NULL)
4814 if (__connman_network_get_weakness(network) == TRUE)
4817 if (service->path != NULL) {
4818 update_from_network(service, network);
4819 __connman_profile_changed(TRUE);
4823 service->type = convert_network_type(network);
4825 switch (service->type) {
4826 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4827 case CONNMAN_SERVICE_TYPE_SYSTEM:
4828 case CONNMAN_SERVICE_TYPE_ETHERNET:
4829 case CONNMAN_SERVICE_TYPE_WIMAX:
4830 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4831 case CONNMAN_SERVICE_TYPE_GPS:
4832 case CONNMAN_SERVICE_TYPE_VPN:
4833 case CONNMAN_SERVICE_TYPE_GADGET:
4834 service->autoconnect = FALSE;
4836 case CONNMAN_SERVICE_TYPE_WIFI:
4837 case CONNMAN_SERVICE_TYPE_CELLULAR:
4838 service->autoconnect = TRUE;
4842 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4844 update_from_network(service, network);
4846 index = connman_network_get_index(network);
4848 if (service->ipconfig_ipv4 == NULL)
4849 setup_ip4config(service, index);
4851 if (service->ipconfig_ipv6 == NULL)
4852 setup_ip6config(service, index);
4854 service_register(service);
4856 if (service->favorite == TRUE) {
4857 device = connman_network_get_device(service->network);
4858 if (device && __connman_device_scanning(device) == FALSE)
4859 __connman_service_auto_connect();
4862 __connman_notifier_service_add(service);
4867 void __connman_service_update_from_network(struct connman_network *network)
4869 struct connman_service *service;
4870 enum connman_service_mode mode;
4871 connman_uint8_t strength, value;
4872 connman_bool_t roaming;
4873 GSequenceIter *iter;
4875 connman_bool_t stats_enable;
4877 DBG("network %p", network);
4879 service = __connman_service_lookup_from_network(network);
4880 if (service == NULL)
4883 if (service->network == NULL)
4886 name = connman_network_get_string(service->network, "Name");
4887 if (g_strcmp0(service->name, name) != 0) {
4888 g_free(service->name);
4889 service->name = g_strdup(name);
4890 connman_dbus_property_changed_basic(service->path,
4891 CONNMAN_SERVICE_INTERFACE, "Name",
4892 DBUS_TYPE_STRING, &service->name);
4895 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4896 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4898 strength = connman_network_get_uint8(service->network, "Strength");
4899 if (strength == service->strength)
4902 service->strength = strength;
4904 strength_changed(service);
4907 roaming = connman_network_get_bool(service->network, "Roaming");
4908 if (roaming == service->roaming)
4911 stats_enable = stats_enabled(service);
4912 if (stats_enable == TRUE)
4913 stats_stop(service);
4915 service->roaming = roaming;
4917 if (stats_enable == TRUE)
4918 stats_start(service);
4920 roaming_changed(service);
4922 iter = g_hash_table_lookup(service_hash, service->identifier);
4924 g_sequence_sort_changed(iter, service_compare, NULL);
4927 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
4930 value = connman_network_get_uint8(service->network, "Cellular.Mode");
4931 mode = convert_cellular_mode(value);
4933 if (mode == service->mode)
4936 service->mode = mode;
4938 mode_changed(service);
4941 void __connman_service_remove_from_network(struct connman_network *network)
4943 struct connman_service *service;
4945 DBG("network %p", network);
4947 service = __connman_service_lookup_from_network(network);
4948 if (service == NULL)
4951 __connman_service_put(service);
4955 * __connman_service_create_from_provider:
4956 * @provider: provider structure
4958 * Look up service by provider and if not found, create one
4960 struct connman_service *
4961 __connman_service_create_from_provider(struct connman_provider *provider)
4963 struct connman_service *service;
4964 const char *ident, *str;
4966 int index = connman_provider_get_index(provider);
4968 DBG("provider %p", provider);
4970 ident = __connman_provider_get_ident(provider);
4974 name = g_strdup_printf("vpn_%s", ident);
4975 service = service_get(name);
4978 if (service == NULL)
4981 service->type = CONNMAN_SERVICE_TYPE_VPN;
4982 service->provider = connman_provider_ref(provider);
4983 service->autoconnect = FALSE;
4985 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4987 str = connman_provider_get_string(provider, "Name");
4989 g_free(service->name);
4990 service->name = g_strdup(str);
4991 service->hidden = FALSE;
4993 g_free(service->name);
4994 service->name = NULL;
4995 service->hidden = TRUE;
4998 service->strength = 0;
5000 if (service->ipconfig_ipv4 == NULL)
5001 service->ipconfig_ipv4 = connman_ipconfig_create(index,
5002 CONNMAN_IPCONFIG_TYPE_IPV4);
5003 if (service->ipconfig_ipv4 == NULL)
5006 connman_ipconfig_set_method(service->ipconfig_ipv4,
5007 CONNMAN_IPCONFIG_METHOD_MANUAL);
5008 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5009 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5011 if (service->ipconfig_ipv6 == NULL)
5012 service->ipconfig_ipv6 = connman_ipconfig_create(index,
5013 CONNMAN_IPCONFIG_TYPE_IPV6);
5014 if (service->ipconfig_ipv6 == NULL)
5017 connman_ipconfig_set_method(service->ipconfig_ipv6,
5018 CONNMAN_IPCONFIG_METHOD_OFF);
5019 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5020 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5022 service_register(service);
5024 __connman_notifier_service_add(service);
5029 static int service_load(struct connman_service *service)
5031 const char *ident = service->profile;
5033 GError *error = NULL;
5034 gchar *pathname, *data = NULL;
5037 connman_bool_t autoconnect;
5038 unsigned int ssid_len;
5041 DBG("service %p", service);
5046 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5047 if (pathname == NULL)
5050 keyfile = g_key_file_new();
5052 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
5059 if (g_key_file_load_from_data(keyfile, data, length,
5060 0, NULL) == FALSE) {
5067 switch (service->type) {
5068 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5069 case CONNMAN_SERVICE_TYPE_SYSTEM:
5070 case CONNMAN_SERVICE_TYPE_ETHERNET:
5071 case CONNMAN_SERVICE_TYPE_GPS:
5072 case CONNMAN_SERVICE_TYPE_VPN:
5073 case CONNMAN_SERVICE_TYPE_GADGET:
5075 case CONNMAN_SERVICE_TYPE_WIFI:
5076 if (service->name == NULL) {
5079 name = g_key_file_get_string(keyfile,
5080 service->identifier, "Name", NULL);
5082 g_free(service->name);
5083 service->name = name;
5086 if (service->network != NULL)
5087 connman_network_set_name(service->network,
5091 if (service->network &&
5092 connman_network_get_blob(service->network,
5093 "WiFi.SSID", &ssid_len) == NULL) {
5096 hex_ssid = g_key_file_get_string(keyfile,
5097 service->identifier,
5100 if (hex_ssid != NULL) {
5102 unsigned int i, j = 0, hex;
5103 size_t hex_ssid_len = strlen(hex_ssid);
5105 ssid = g_try_malloc0(hex_ssid_len / 2);
5112 for (i = 0; i < hex_ssid_len; i += 2) {
5113 sscanf(hex_ssid + i, "%02x", &hex);
5117 connman_network_set_blob(service->network,
5118 "WiFi.SSID", ssid, hex_ssid_len / 2);
5125 case CONNMAN_SERVICE_TYPE_WIMAX:
5126 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5127 case CONNMAN_SERVICE_TYPE_CELLULAR:
5128 service->favorite = g_key_file_get_boolean(keyfile,
5129 service->identifier, "Favorite", NULL);
5131 autoconnect = g_key_file_get_boolean(keyfile,
5132 service->identifier, "AutoConnect", &error);
5134 service->autoconnect = autoconnect;
5135 g_clear_error(&error);
5137 str = g_key_file_get_string(keyfile,
5138 service->identifier, "Failure", NULL);
5140 if (service->favorite == FALSE)
5141 service->state_ipv4 = service->state_ipv6 =
5142 CONNMAN_SERVICE_STATE_FAILURE;
5143 service->error = string2error(str);
5148 str = g_key_file_get_string(keyfile,
5149 service->identifier, "Modified", NULL);
5151 g_time_val_from_iso8601(str, &service->modified);
5155 str = g_key_file_get_string(keyfile,
5156 service->identifier, "Passphrase", NULL);
5158 g_free(service->passphrase);
5159 service->passphrase = str;
5162 if (service->ipconfig_ipv4 != NULL)
5163 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5164 service->identifier, "IPv4.");
5166 if (service->ipconfig_ipv6 != NULL)
5167 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5168 service->identifier, "IPv6.");
5170 service->nameservers_config = g_key_file_get_string_list(keyfile,
5171 service->identifier, "Nameservers", &length, NULL);
5172 if (service->nameservers_config != NULL && length == 0) {
5173 g_strfreev(service->nameservers_config);
5174 service->nameservers_config = NULL;
5177 service->domains = g_key_file_get_string_list(keyfile,
5178 service->identifier, "Domains", &length, NULL);
5179 if (service->domains != NULL && length == 0) {
5180 g_strfreev(service->domains);
5181 service->domains = NULL;
5184 str = g_key_file_get_string(keyfile,
5185 service->identifier, "Proxy.Method", NULL);
5187 service->proxy_config = string2proxymethod(str);
5191 service->proxies = g_key_file_get_string_list(keyfile,
5192 service->identifier, "Proxy.Servers", &length, NULL);
5193 if (service->proxies != NULL && length == 0) {
5194 g_strfreev(service->proxies);
5195 service->proxies = NULL;
5198 service->excludes = g_key_file_get_string_list(keyfile,
5199 service->identifier, "Proxy.Excludes", &length, NULL);
5200 if (service->excludes != NULL && length == 0) {
5201 g_strfreev(service->excludes);
5202 service->excludes = NULL;
5205 str = g_key_file_get_string(keyfile,
5206 service->identifier, "Proxy.URL", NULL);
5208 g_free(service->pac);
5213 g_key_file_free(keyfile);
5218 static int service_save(struct connman_service *service)
5220 const char *ident = service->profile;
5222 gchar *pathname, *data = NULL;
5225 const char *cst_str = NULL;
5228 DBG("service %p", service);
5233 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5234 if (pathname == NULL)
5237 keyfile = g_key_file_new();
5239 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5243 if (g_key_file_load_from_data(keyfile, data, length,
5251 if (service->name != NULL)
5252 g_key_file_set_string(keyfile, service->identifier,
5253 "Name", service->name);
5255 switch (service->type) {
5256 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5257 case CONNMAN_SERVICE_TYPE_SYSTEM:
5258 case CONNMAN_SERVICE_TYPE_ETHERNET:
5259 case CONNMAN_SERVICE_TYPE_GPS:
5260 case CONNMAN_SERVICE_TYPE_VPN:
5261 case CONNMAN_SERVICE_TYPE_GADGET:
5263 case CONNMAN_SERVICE_TYPE_WIFI:
5264 if (service->network) {
5265 const unsigned char *ssid;
5266 unsigned int ssid_len = 0;
5268 ssid = connman_network_get_blob(service->network,
5269 "WiFi.SSID", &ssid_len);
5271 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5272 char *identifier = service->identifier;
5276 str = g_string_sized_new(ssid_len * 2);
5282 for (i = 0; i < ssid_len; i++)
5283 g_string_append_printf(str,
5286 g_key_file_set_string(keyfile, identifier,
5289 g_string_free(str, TRUE);
5294 case CONNMAN_SERVICE_TYPE_WIMAX:
5295 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5296 case CONNMAN_SERVICE_TYPE_CELLULAR:
5297 g_key_file_set_boolean(keyfile, service->identifier,
5298 "Favorite", service->favorite);
5300 if (service->favorite == TRUE)
5301 g_key_file_set_boolean(keyfile, service->identifier,
5302 "AutoConnect", service->autoconnect);
5304 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5305 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5306 const char *failure = error2string(service->error);
5307 if (failure != NULL)
5308 g_key_file_set_string(keyfile,
5309 service->identifier,
5310 "Failure", failure);
5312 g_key_file_remove_key(keyfile, service->identifier,
5318 str = g_time_val_to_iso8601(&service->modified);
5320 g_key_file_set_string(keyfile, service->identifier,
5325 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5326 g_key_file_set_string(keyfile, service->identifier,
5327 "Passphrase", service->passphrase);
5329 g_key_file_remove_key(keyfile, service->identifier,
5330 "Passphrase", NULL);
5332 if (service->ipconfig_ipv4 != NULL)
5333 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5334 service->identifier, "IPv4.");
5336 if (service->ipconfig_ipv6 != NULL)
5337 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5338 service->identifier, "IPv6.");
5340 if (service->nameservers_config != NULL) {
5341 guint len = g_strv_length(service->nameservers_config);
5343 g_key_file_set_string_list(keyfile, service->identifier,
5345 (const gchar **) service->nameservers_config, len);
5347 g_key_file_remove_key(keyfile, service->identifier,
5348 "Nameservers", NULL);
5350 if (service->domains != NULL) {
5351 guint len = g_strv_length(service->domains);
5353 g_key_file_set_string_list(keyfile, service->identifier,
5355 (const gchar **) service->domains, len);
5357 g_key_file_remove_key(keyfile, service->identifier,
5360 cst_str = proxymethod2string(service->proxy_config);
5361 if (cst_str != NULL)
5362 g_key_file_set_string(keyfile, service->identifier,
5363 "Proxy.Method", cst_str);
5365 if (service->proxies != NULL) {
5366 guint len = g_strv_length(service->proxies);
5368 g_key_file_set_string_list(keyfile, service->identifier,
5370 (const gchar **) service->proxies, len);
5372 g_key_file_remove_key(keyfile, service->identifier,
5373 "Proxy.Servers", NULL);
5375 if (service->excludes != NULL) {
5376 guint len = g_strv_length(service->excludes);
5378 g_key_file_set_string_list(keyfile, service->identifier,
5380 (const gchar **) service->excludes, len);
5382 g_key_file_remove_key(keyfile, service->identifier,
5383 "Proxy.Excludes", NULL);
5385 if (service->pac != NULL && strlen(service->pac) > 0)
5386 g_key_file_set_string(keyfile, service->identifier,
5387 "Proxy.URL", service->pac);
5389 g_key_file_remove_key(keyfile, service->identifier,
5392 data = g_key_file_to_data(keyfile, &length, NULL);
5394 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5395 connman_error("Failed to store service information");
5400 g_key_file_free(keyfile);
5407 static struct connman_storage service_storage = {
5409 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5410 .service_load = service_load,
5411 .service_save = service_save,
5414 int __connman_service_init(void)
5418 connection = connman_dbus_get_connection();
5420 if (connman_storage_register(&service_storage) < 0)
5421 connman_error("Failed to register service storage");
5423 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5426 service_list = g_sequence_new(service_free);
5431 void __connman_service_cleanup(void)
5435 g_sequence_free(service_list);
5436 service_list = NULL;
5438 g_hash_table_destroy(service_hash);
5439 service_hash = NULL;
5441 g_slist_free(counter_list);
5442 counter_list = NULL;
5444 connman_storage_unregister(&service_storage);
5446 dbus_connection_unref(connection);