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
34 #define CONNECT_TIMEOUT 120
36 static DBusConnection *connection = NULL;
38 static GSequence *service_list = NULL;
39 static GHashTable *service_hash = NULL;
40 static GSList *counter_list = NULL;
42 struct connman_stats {
44 connman_bool_t enabled;
45 struct connman_stats_data data_last;
46 struct connman_stats_data data;
50 struct connman_stats_counter {
51 connman_bool_t append_all;
52 struct connman_stats stats;
53 struct connman_stats stats_roaming;
56 struct connman_service {
58 gint session_usage_count;
61 enum connman_service_type type;
62 enum connman_service_security security;
63 enum connman_service_state state;
64 enum connman_service_state state_ipv4;
65 enum connman_service_state state_ipv6;
66 enum connman_service_error error;
67 connman_uint8_t strength;
68 connman_bool_t favorite;
69 connman_bool_t immutable;
70 connman_bool_t hidden;
71 connman_bool_t ignore;
72 connman_bool_t autoconnect;
73 connman_bool_t userconnect;
79 connman_bool_t roaming;
80 connman_bool_t login_required;
81 connman_bool_t network_created;
82 struct connman_ipconfig *ipconfig_ipv4;
83 struct connman_ipconfig *ipconfig_ipv6;
84 struct connman_network *network;
85 struct connman_provider *provider;
87 char **nameservers_config;
91 /* 802.1x settings from the config files */
95 char *client_cert_file;
96 char *private_key_file;
97 char *private_key_passphrase;
101 struct connman_location *location;
102 struct connman_stats stats;
103 struct connman_stats stats_roaming;
104 GHashTable *counter_table;
105 enum connman_service_proxy_method proxy;
106 enum connman_service_proxy_method proxy_config;
113 static void append_path(gpointer value, gpointer user_data)
115 struct connman_service *service = value;
116 DBusMessageIter *iter = user_data;
118 if (service->path == NULL || service->hidden == TRUE)
121 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
125 void __connman_service_list(DBusMessageIter *iter, void *user_data)
127 if (service_list == NULL)
130 g_sequence_foreach(service_list, append_path, iter);
135 struct connman_service *service;
138 static void compare_path(gpointer value, gpointer user_data)
140 struct connman_service *service = value;
141 struct find_data *data = user_data;
143 if (data->service != NULL)
146 if (g_strcmp0(service->path, data->path) == 0)
147 data->service = service;
150 static struct connman_service *find_service(const char *path)
152 struct find_data data = { .path = path, .service = NULL };
154 DBG("path %s", path);
156 g_sequence_foreach(service_list, compare_path, &data);
161 const char *__connman_service_type2string(enum connman_service_type type)
164 case CONNMAN_SERVICE_TYPE_UNKNOWN:
166 case CONNMAN_SERVICE_TYPE_SYSTEM:
168 case CONNMAN_SERVICE_TYPE_ETHERNET:
170 case CONNMAN_SERVICE_TYPE_WIFI:
172 case CONNMAN_SERVICE_TYPE_WIMAX:
174 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
176 case CONNMAN_SERVICE_TYPE_CELLULAR:
178 case CONNMAN_SERVICE_TYPE_GPS:
180 case CONNMAN_SERVICE_TYPE_VPN:
182 case CONNMAN_SERVICE_TYPE_GADGET:
189 static const char *security2string(enum connman_service_security security)
192 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
194 case CONNMAN_SERVICE_SECURITY_NONE:
196 case CONNMAN_SERVICE_SECURITY_WEP:
198 case CONNMAN_SERVICE_SECURITY_PSK:
200 case CONNMAN_SERVICE_SECURITY_8021X:
202 case CONNMAN_SERVICE_SECURITY_WPA:
204 case CONNMAN_SERVICE_SECURITY_RSN:
211 static const char *state2string(enum connman_service_state state)
214 case CONNMAN_SERVICE_STATE_UNKNOWN:
216 case CONNMAN_SERVICE_STATE_IDLE:
218 case CONNMAN_SERVICE_STATE_ASSOCIATION:
219 return "association";
220 case CONNMAN_SERVICE_STATE_CONFIGURATION:
221 return "configuration";
222 case CONNMAN_SERVICE_STATE_READY:
224 case CONNMAN_SERVICE_STATE_ONLINE:
226 case CONNMAN_SERVICE_STATE_DISCONNECT:
228 case CONNMAN_SERVICE_STATE_FAILURE:
235 static const char *error2string(enum connman_service_error error)
238 case CONNMAN_SERVICE_ERROR_UNKNOWN:
240 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
241 return "out-of-range";
242 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
243 return "pin-missing";
244 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
245 return "dhcp-failed";
246 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
247 return "connect-failed";
248 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
249 return "login-failed";
250 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
251 return "auth-failed";
252 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
253 return "invalid-key";
259 static enum connman_service_error string2error(const char *error)
261 if (g_strcmp0(error, "dhcp-failed") == 0)
262 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
263 else if (g_strcmp0(error, "pin-missing") == 0)
264 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
265 else if (g_strcmp0(error, "invalid-key") == 0)
266 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
268 return CONNMAN_SERVICE_ERROR_UNKNOWN;
271 static const char *proxymethod2string(enum connman_service_proxy_method method)
274 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
276 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
278 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
280 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
287 static enum connman_service_proxy_method string2proxymethod(const char *method)
289 if (g_strcmp0(method, "direct") == 0)
290 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
291 else if (g_strcmp0(method, "auto") == 0)
292 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
293 else if (g_strcmp0(method, "manual") == 0)
294 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
296 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
299 static enum connman_service_state combine_state(
300 enum connman_service_state state_a,
301 enum connman_service_state state_b)
303 enum connman_service_state result;
305 if (state_a == state_b) {
310 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
315 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
320 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
325 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
330 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
331 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
332 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
333 state_b == CONNMAN_SERVICE_STATE_READY)
340 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
341 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
342 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
343 state_a == CONNMAN_SERVICE_STATE_READY)
350 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
351 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
352 state_b == CONNMAN_SERVICE_STATE_READY)
359 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
360 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
361 state_a == CONNMAN_SERVICE_STATE_READY)
368 if (state_a == CONNMAN_SERVICE_STATE_READY) {
369 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
370 state_b == CONNMAN_SERVICE_STATE_DISCONNECT)
377 if (state_b == CONNMAN_SERVICE_STATE_READY) {
378 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
379 state_a == CONNMAN_SERVICE_STATE_DISCONNECT)
386 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
387 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT)
394 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
395 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT)
402 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
407 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
412 result = CONNMAN_SERVICE_STATE_FAILURE;
418 static connman_bool_t is_connecting_state(struct connman_service *service,
419 enum connman_service_state state)
422 case CONNMAN_SERVICE_STATE_UNKNOWN:
423 case CONNMAN_SERVICE_STATE_IDLE:
424 case CONNMAN_SERVICE_STATE_FAILURE:
425 if (service->network != NULL)
426 return connman_network_get_connecting(service->network);
427 case CONNMAN_SERVICE_STATE_DISCONNECT:
428 case CONNMAN_SERVICE_STATE_READY:
429 case CONNMAN_SERVICE_STATE_ONLINE:
431 case CONNMAN_SERVICE_STATE_ASSOCIATION:
432 case CONNMAN_SERVICE_STATE_CONFIGURATION:
439 static connman_bool_t is_connected_state(const struct connman_service *service,
440 enum connman_service_state state)
443 case CONNMAN_SERVICE_STATE_UNKNOWN:
444 case CONNMAN_SERVICE_STATE_IDLE:
445 case CONNMAN_SERVICE_STATE_ASSOCIATION:
446 case CONNMAN_SERVICE_STATE_CONFIGURATION:
447 case CONNMAN_SERVICE_STATE_DISCONNECT:
448 case CONNMAN_SERVICE_STATE_FAILURE:
450 case CONNMAN_SERVICE_STATE_READY:
451 case CONNMAN_SERVICE_STATE_ONLINE:
458 static connman_bool_t is_connecting(struct connman_service *service)
460 return is_connecting_state(service, service->state);
463 static connman_bool_t is_connected(struct connman_service *service)
465 return is_connected_state(service, service->state);
468 static void update_nameservers(struct connman_service *service)
472 if (service->ipconfig_ipv4)
473 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
474 else if (service->ipconfig_ipv6)
475 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
482 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
483 case CONNMAN_SERVICE_STATE_UNKNOWN:
484 case CONNMAN_SERVICE_STATE_IDLE:
485 case CONNMAN_SERVICE_STATE_ASSOCIATION:
486 case CONNMAN_SERVICE_STATE_CONFIGURATION:
488 case CONNMAN_SERVICE_STATE_FAILURE:
489 case CONNMAN_SERVICE_STATE_DISCONNECT:
490 connman_resolver_remove_all(ifname);
492 case CONNMAN_SERVICE_STATE_READY:
493 case CONNMAN_SERVICE_STATE_ONLINE:
497 connman_resolver_remove_all(ifname);
499 if (service->nameservers_config != NULL) {
502 for (i = 0; service->nameservers_config[i] != NULL; i++) {
503 connman_resolver_append(ifname, NULL,
504 service->nameservers_config[i]);
506 } else if (service->nameservers != NULL) {
509 for (i = 0; service->nameservers[i] != NULL; i++) {
510 connman_resolver_append(ifname, NULL,
511 service->nameservers[i]);
515 if (service->domains != NULL) {
518 for (i = 0; service->domains[i]; i++)
519 connman_resolver_append(ifname, service->domains[i],
521 } else if (service->domainname != NULL)
522 connman_resolver_append(ifname, service->domainname, NULL);
524 connman_resolver_flush();
527 int __connman_service_nameserver_append(struct connman_service *service,
528 const char *nameserver)
532 DBG("service %p nameserver %s", service, nameserver);
534 if (nameserver == NULL)
537 if (service->nameservers != NULL) {
540 for (i = 0; service->nameservers[i] != NULL; i++)
541 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
544 len = g_strv_length(service->nameservers);
545 service->nameservers = g_try_renew(char *, service->nameservers,
549 service->nameservers = g_try_new0(char *, len + 2);
552 if (service->nameservers == NULL)
555 service->nameservers[len] = g_strdup(nameserver);
556 service->nameservers[len + 1] = NULL;
558 update_nameservers(service);
563 int __connman_service_nameserver_remove(struct connman_service *service,
564 const char *nameserver)
569 DBG("service %p nameserver %s", service, nameserver);
571 if (nameserver == NULL)
574 if (service->nameservers == NULL)
577 len = g_strv_length(service->nameservers);
579 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
582 g_strfreev(service->nameservers);
583 service->nameservers = NULL;
588 servers = g_try_new0(char *, len);
592 for (i = 0, j = 0; i < len; i++) {
593 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
594 servers[j] = g_strdup(service->nameservers[i]);
598 servers[len - 1] = NULL;
600 g_strfreev(service->nameservers);
601 service->nameservers = servers;
603 update_nameservers(service);
608 void __connman_service_nameserver_clear(struct connman_service *service)
610 g_strfreev(service->nameservers);
611 service->nameservers = NULL;
613 update_nameservers(service);
616 static void nameserver_add_routes(int index, char **nameservers,
620 struct addrinfo hints;
621 struct addrinfo *addr;
623 for (i = 0; nameservers[i] != NULL; i++) {
624 memset(&hints, 0, sizeof(struct addrinfo));
625 hints.ai_flags = AI_NUMERICHOST;
628 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
629 if (ret == EAI_NONAME)
630 family = AF_INET; /* use the IPv4 as a default */
634 family = addr->ai_family;
636 if (family == AF_INET) {
637 if (connman_inet_compare_subnet(index,
638 nameservers[i]) != TRUE)
639 connman_inet_add_host_route(index,
641 } else if (family == AF_INET6)
642 connman_inet_add_ipv6_host_route(index,
649 static void nameserver_del_routes(int index, char **nameservers)
652 struct addrinfo hints;
653 struct addrinfo *addr;
655 for (i = 0; nameservers[i] != NULL; i++) {
656 memset(&hints, 0, sizeof(struct addrinfo));
657 hints.ai_flags = AI_NUMERICHOST;
660 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
661 if (ret == EAI_NONAME)
662 family = AF_INET; /* use the IPv4 as a default */
666 family = addr->ai_family;
668 if (family == AF_INET)
669 connman_inet_del_host_route(index, nameservers[i]);
670 else if (family == AF_INET6)
671 connman_inet_del_ipv6_host_route(index,
678 void __connman_service_nameserver_add_routes(struct connman_service *service,
686 if (service->network != NULL)
687 index = connman_network_get_index(service->network);
688 else if (service->provider != NULL)
689 index = connman_provider_get_index(service->provider);
691 if (service->nameservers_config != NULL) {
693 * Configured nameserver takes preference over the
694 * discoverd nameserver gathered from DHCP, VPN, etc.
696 nameserver_add_routes(index, service->nameservers_config, gw);
697 } else if (service->nameservers != NULL) {
699 * We add nameservers host routes for nameservers that
700 * are not on our subnet. For those who are, the subnet
701 * route will be installed by the time the dns proxy code
702 * tries to reach them. The subnet route is installed
703 * when setting the interface IP address.
705 nameserver_add_routes(index, service->nameservers, gw);
709 void __connman_service_nameserver_del_routes(struct connman_service *service)
716 if (service->network != NULL)
717 index = connman_network_get_index(service->network);
718 else if (service->provider != NULL)
719 index = connman_provider_get_index(service->provider);
721 if (service->nameservers_config != NULL)
722 nameserver_del_routes(index, service->nameservers_config);
723 else if (service->nameservers != NULL)
724 nameserver_del_routes(index, service->nameservers);
727 static struct connman_stats *stats_get(struct connman_service *service)
729 if (service->roaming == TRUE)
730 return &service->stats_roaming;
732 return &service->stats;
735 static connman_bool_t stats_enabled(struct connman_service *service)
737 struct connman_stats *stats = stats_get(service);
739 return stats->enabled;
742 static void stats_start(struct connman_service *service)
744 struct connman_stats *stats = stats_get(service);
746 DBG("service %p", service);
748 if (stats->timer == NULL)
751 stats->enabled = TRUE;
752 stats->data_last.time = stats->data.time;
754 g_timer_start(stats->timer);
757 static void stats_stop(struct connman_service *service)
759 struct connman_stats *stats = stats_get(service);
760 unsigned int seconds;
762 DBG("service %p", service);
764 if (stats->timer == NULL)
767 if (stats->enabled == FALSE)
770 g_timer_stop(stats->timer);
772 seconds = g_timer_elapsed(stats->timer, NULL);
773 stats->data.time = stats->data_last.time + seconds;
775 stats->enabled = FALSE;
778 static void reset_stats(struct connman_service *service)
780 DBG("service %p", service);
783 service->stats.valid = FALSE;
785 service->stats.data.rx_packets = 0;
786 service->stats.data.tx_packets = 0;
787 service->stats.data.rx_bytes = 0;
788 service->stats.data.tx_bytes = 0;
789 service->stats.data.rx_errors = 0;
790 service->stats.data.tx_errors = 0;
791 service->stats.data.rx_dropped = 0;
792 service->stats.data.tx_dropped = 0;
793 service->stats.data.time = 0;
794 service->stats.data_last.time = 0;
796 g_timer_reset(service->stats.timer);
799 service->stats_roaming.valid = FALSE;
801 service->stats_roaming.data.rx_packets = 0;
802 service->stats_roaming.data.tx_packets = 0;
803 service->stats_roaming.data.rx_bytes = 0;
804 service->stats_roaming.data.tx_bytes = 0;
805 service->stats_roaming.data.rx_errors = 0;
806 service->stats_roaming.data.tx_errors = 0;
807 service->stats_roaming.data.rx_dropped = 0;
808 service->stats_roaming.data.tx_dropped = 0;
809 service->stats_roaming.data.time = 0;
810 service->stats_roaming.data_last.time = 0;
812 g_timer_reset(service->stats_roaming.timer);
815 static struct connman_service *get_default(void)
817 struct connman_service *service;
820 iter = g_sequence_get_begin_iter(service_list);
822 if (g_sequence_iter_is_end(iter) == TRUE)
825 service = g_sequence_get(iter);
827 if (is_connected(service) == FALSE)
833 static void default_changed(void)
835 struct connman_service *service = get_default();
837 __connman_notifier_default_changed(service);
840 const char *__connman_service_default(void)
842 struct connman_service *service;
844 service = get_default();
848 return __connman_service_type2string(service->type);
851 static void state_changed(struct connman_service *service)
855 __connman_notifier_service_state_changed(service, service->state);
857 str = state2string(service->state);
861 connman_dbus_property_changed_basic(service->path,
862 CONNMAN_SERVICE_INTERFACE, "State",
863 DBUS_TYPE_STRING, &str);
866 static void strength_changed(struct connman_service *service)
868 if (service->strength == 0)
871 connman_dbus_property_changed_basic(service->path,
872 CONNMAN_SERVICE_INTERFACE, "Strength",
873 DBUS_TYPE_BYTE, &service->strength);
876 static void favorite_changed(struct connman_service *service)
878 if (service->path == NULL)
881 connman_dbus_property_changed_basic(service->path,
882 CONNMAN_SERVICE_INTERFACE, "Favorite",
883 DBUS_TYPE_BOOLEAN, &service->favorite);
886 static void immutable_changed(struct connman_service *service)
888 if (service->path == NULL)
891 connman_dbus_property_changed_basic(service->path,
892 CONNMAN_SERVICE_INTERFACE, "Immutable",
893 DBUS_TYPE_BOOLEAN, &service->immutable);
896 static void roaming_changed(struct connman_service *service)
898 if (service->path == NULL)
901 connman_dbus_property_changed_basic(service->path,
902 CONNMAN_SERVICE_INTERFACE, "Roaming",
903 DBUS_TYPE_BOOLEAN, &service->roaming);
906 static void autoconnect_changed(struct connman_service *service)
908 if (service->path == NULL)
911 connman_dbus_property_changed_basic(service->path,
912 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
913 DBUS_TYPE_BOOLEAN, &service->autoconnect);
916 static void passphrase_changed(struct connman_service *service)
918 dbus_bool_t required;
920 switch (service->type) {
921 case CONNMAN_SERVICE_TYPE_UNKNOWN:
922 case CONNMAN_SERVICE_TYPE_SYSTEM:
923 case CONNMAN_SERVICE_TYPE_ETHERNET:
924 case CONNMAN_SERVICE_TYPE_WIMAX:
925 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
926 case CONNMAN_SERVICE_TYPE_CELLULAR:
927 case CONNMAN_SERVICE_TYPE_GPS:
928 case CONNMAN_SERVICE_TYPE_VPN:
929 case CONNMAN_SERVICE_TYPE_GADGET:
931 case CONNMAN_SERVICE_TYPE_WIFI:
934 switch (service->security) {
935 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
936 case CONNMAN_SERVICE_SECURITY_NONE:
938 case CONNMAN_SERVICE_SECURITY_WEP:
939 case CONNMAN_SERVICE_SECURITY_PSK:
940 case CONNMAN_SERVICE_SECURITY_WPA:
941 case CONNMAN_SERVICE_SECURITY_RSN:
942 if (service->passphrase == NULL)
945 case CONNMAN_SERVICE_SECURITY_8021X:
951 connman_dbus_property_changed_basic(service->path,
952 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
953 DBUS_TYPE_BOOLEAN, &required);
956 static void login_changed(struct connman_service *service)
958 dbus_bool_t required = service->login_required;
960 if (service->path == NULL)
963 connman_dbus_property_changed_basic(service->path,
964 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
965 DBUS_TYPE_BOOLEAN, &required);
968 static void append_security(DBusMessageIter *iter, void *user_data)
970 struct connman_service *service = user_data;
973 str = security2string(service->security);
975 dbus_message_iter_append_basic(iter,
976 DBUS_TYPE_STRING, &str);
979 if (service->wps == TRUE)
980 dbus_message_iter_append_basic(iter,
981 DBUS_TYPE_STRING, &str);
984 static void append_ethernet(DBusMessageIter *iter, void *user_data)
986 struct connman_service *service = user_data;
988 if (service->ipconfig_ipv4 != NULL)
989 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
991 else if (service->ipconfig_ipv6 != NULL)
992 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
996 static void append_ipv4(DBusMessageIter *iter, void *user_data)
998 struct connman_service *service = user_data;
1000 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1001 state2string(service->state_ipv4));
1003 if (is_connected_state(service, service->state_ipv4) == FALSE)
1006 if (service->ipconfig_ipv4 != NULL)
1007 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1010 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1012 struct connman_service *service = user_data;
1014 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1015 state2string(service->state_ipv6));
1017 if (is_connected_state(service, service->state_ipv6) == FALSE)
1020 if (service->ipconfig_ipv6 != NULL)
1021 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1022 service->ipconfig_ipv4);
1025 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1027 struct connman_service *service = user_data;
1029 if (service->ipconfig_ipv4 != NULL)
1030 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1034 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1036 struct connman_service *service = user_data;
1038 if (service->ipconfig_ipv6 != NULL)
1039 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1043 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1048 servers = *nameservers;
1050 for (i = 0; servers[i] != NULL; i++) {
1051 dbus_message_iter_append_basic(iter,
1052 DBUS_TYPE_STRING, &servers[i]);
1056 static void append_dns(DBusMessageIter *iter, void *user_data)
1058 struct connman_service *service = user_data;
1060 if (is_connected(service) == FALSE)
1063 if (service->nameservers_config != NULL) {
1064 append_nameserver(iter, &service->nameservers_config);
1066 } else if (service->nameservers != NULL) {
1067 append_nameserver(iter, &service->nameservers);
1072 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1074 struct connman_service *service = user_data;
1077 if (service->nameservers_config == NULL)
1080 for (i = 0; service->nameservers_config[i]; i++) {
1081 dbus_message_iter_append_basic(iter,
1083 &service->nameservers_config[i]);
1087 static void append_domain(DBusMessageIter *iter, void *user_data)
1089 struct connman_service *service = user_data;
1091 if (is_connected(service) == FALSE &&
1092 is_connecting(service) == FALSE)
1095 if (service->domainname == NULL)
1098 dbus_message_iter_append_basic(iter,
1099 DBUS_TYPE_STRING, &service->domainname);
1102 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1104 struct connman_service *service = user_data;
1107 if (service->domains == NULL)
1110 for (i = 0; service->domains[i]; i++)
1111 dbus_message_iter_append_basic(iter,
1112 DBUS_TYPE_STRING, &service->domains[i]);
1115 static void append_proxies(DBusMessageIter *iter, void *user_data)
1117 struct connman_service *service = user_data;
1120 if (service->proxies == NULL)
1123 for (i = 0; service->proxies[i]; i++)
1124 dbus_message_iter_append_basic(iter,
1125 DBUS_TYPE_STRING, &service->proxies[i]);
1128 static void append_excludes(DBusMessageIter *iter, void *user_data)
1130 struct connman_service *service = user_data;
1133 if (service->excludes == NULL)
1136 for (i = 0; service->excludes[i]; i++)
1137 dbus_message_iter_append_basic(iter,
1138 DBUS_TYPE_STRING, &service->excludes[i]);
1141 static void append_proxy(DBusMessageIter *iter, void *user_data)
1143 struct connman_service *service = user_data;
1144 enum connman_service_proxy_method proxy;
1145 const char *pac = NULL;
1146 const char *method = proxymethod2string(
1147 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1151 if (is_connected(service) == FALSE)
1154 proxy = connman_service_get_proxy_method(service);
1157 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1159 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1161 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1162 connman_dbus_dict_append_array(iter, "Servers",
1163 DBUS_TYPE_STRING, append_proxies,
1166 connman_dbus_dict_append_array(iter, "Excludes",
1167 DBUS_TYPE_STRING, append_excludes,
1170 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1171 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1172 if (service->ipconfig_ipv4 != NULL)
1173 pac = __connman_ipconfig_get_proxy_autoconfig(
1174 service->ipconfig_ipv4);
1175 else if (service->ipconfig_ipv6 != NULL)
1176 pac = __connman_ipconfig_get_proxy_autoconfig(
1177 service->ipconfig_ipv6);
1179 if (service->pac == NULL && pac == NULL)
1182 if (service->pac != NULL)
1185 connman_dbus_dict_append_basic(iter, "URL",
1186 DBUS_TYPE_STRING, &pac);
1190 method = proxymethod2string(proxy);
1193 connman_dbus_dict_append_basic(iter, "Method",
1194 DBUS_TYPE_STRING, &method);
1197 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1199 struct connman_service *service = user_data;
1202 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1205 switch (service->proxy_config) {
1206 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1208 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1210 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1211 if (service->proxies != NULL)
1212 connman_dbus_dict_append_array(iter, "Servers",
1214 append_proxies, service);
1216 if (service->excludes != NULL)
1217 connman_dbus_dict_append_array(iter, "Excludes",
1219 append_excludes, service);
1221 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1222 if (service->pac != NULL)
1223 connman_dbus_dict_append_basic(iter, "URL",
1224 DBUS_TYPE_STRING, &service->pac);
1228 method = proxymethod2string(service->proxy_config);
1230 connman_dbus_dict_append_basic(iter, "Method",
1231 DBUS_TYPE_STRING, &method);
1234 static void append_provider(DBusMessageIter *iter, void *user_data)
1236 struct connman_service *service = user_data;
1238 DBG("%p %p", service, service->provider);
1240 if (is_connected(service) == FALSE)
1243 if (service->provider != NULL)
1244 __connman_provider_append_properties(service->provider, iter);
1248 static void settings_changed(struct connman_service *service,
1249 struct connman_ipconfig *ipconfig)
1251 connman_dbus_property_changed_dict(service->path,
1252 CONNMAN_SERVICE_INTERFACE, "IPv4",
1253 append_ipv4, service);
1255 connman_dbus_property_changed_dict(service->path,
1256 CONNMAN_SERVICE_INTERFACE, "IPv6",
1257 append_ipv6, service);
1259 __connman_notifier_ipconfig_changed(service, ipconfig);
1262 static void ipv4_configuration_changed(struct connman_service *service)
1264 connman_dbus_property_changed_dict(service->path,
1265 CONNMAN_SERVICE_INTERFACE,
1266 "IPv4.Configuration",
1271 static void ipv6_configuration_changed(struct connman_service *service)
1273 connman_dbus_property_changed_dict(service->path,
1274 CONNMAN_SERVICE_INTERFACE,
1275 "IPv6.Configuration",
1280 static void dns_changed(struct connman_service *service)
1282 connman_dbus_property_changed_array(service->path,
1283 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1284 DBUS_TYPE_STRING, append_dns, service);
1287 static void dns_configuration_changed(struct connman_service *service)
1289 connman_dbus_property_changed_array(service->path,
1290 CONNMAN_SERVICE_INTERFACE,
1291 "Nameservers.Configuration",
1292 DBUS_TYPE_STRING, append_dnsconfig, service);
1294 dns_changed(service);
1297 static void domain_changed(struct connman_service *service)
1299 connman_dbus_property_changed_array(service->path,
1300 CONNMAN_SERVICE_INTERFACE, "Domains",
1301 DBUS_TYPE_STRING, append_domain, service);
1304 static void domain_configuration_changed(struct connman_service *service)
1306 connman_dbus_property_changed_array(service->path,
1307 CONNMAN_SERVICE_INTERFACE,
1308 "Domains.Configuration",
1309 DBUS_TYPE_STRING, append_domainconfig, service);
1312 static void proxy_changed(struct connman_service *service)
1314 connman_dbus_property_changed_dict(service->path,
1315 CONNMAN_SERVICE_INTERFACE, "Proxy",
1316 append_proxy, service);
1319 static void proxy_configuration_changed(struct connman_service *service)
1321 connman_dbus_property_changed_dict(service->path,
1322 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1323 append_proxyconfig, service);
1325 proxy_changed(service);
1328 static void link_changed(struct connman_service *service)
1330 connman_dbus_property_changed_dict(service->path,
1331 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1332 append_ethernet, service);
1335 static void stats_append_counters(DBusMessageIter *dict,
1336 struct connman_stats_data *stats,
1337 struct connman_stats_data *counters,
1338 connman_bool_t append_all)
1340 if (counters->rx_packets != stats->rx_packets || append_all) {
1341 counters->rx_packets = stats->rx_packets;
1342 connman_dbus_dict_append_basic(dict, "RX.Packets",
1343 DBUS_TYPE_UINT32, &stats->rx_packets);
1346 if (counters->tx_packets != stats->tx_packets || append_all) {
1347 counters->tx_packets = stats->tx_packets;
1348 connman_dbus_dict_append_basic(dict, "TX.Packets",
1349 DBUS_TYPE_UINT32, &stats->tx_packets);
1352 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1353 counters->rx_bytes = stats->rx_bytes;
1354 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1355 DBUS_TYPE_UINT32, &stats->rx_bytes);
1358 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1359 counters->tx_bytes = stats->tx_bytes;
1360 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1361 DBUS_TYPE_UINT32, &stats->tx_bytes);
1364 if (counters->rx_errors != stats->rx_errors || append_all) {
1365 counters->rx_errors = stats->rx_errors;
1366 connman_dbus_dict_append_basic(dict, "RX.Errors",
1367 DBUS_TYPE_UINT32, &stats->rx_errors);
1370 if (counters->tx_errors != stats->tx_errors || append_all) {
1371 counters->tx_errors = stats->tx_errors;
1372 connman_dbus_dict_append_basic(dict, "TX.Errors",
1373 DBUS_TYPE_UINT32, &stats->tx_errors);
1376 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1377 counters->rx_dropped = stats->rx_dropped;
1378 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1379 DBUS_TYPE_UINT32, &stats->rx_dropped);
1382 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1383 counters->tx_dropped = stats->tx_dropped;
1384 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1385 DBUS_TYPE_UINT32, &stats->tx_dropped);
1388 if (counters->time != stats->time || append_all) {
1389 counters->time = stats->time;
1390 connman_dbus_dict_append_basic(dict, "Time",
1391 DBUS_TYPE_UINT32, &stats->time);
1395 static void stats_append(struct connman_service *service,
1396 const char *counter,
1397 struct connman_stats_counter *counters,
1398 connman_bool_t append_all)
1400 DBusMessageIter array, dict;
1403 DBG("service %p counter %s", service, counter);
1405 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1409 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1410 &service->path, DBUS_TYPE_INVALID);
1412 dbus_message_iter_init_append(msg, &array);
1415 connman_dbus_dict_open(&array, &dict);
1417 stats_append_counters(&dict, &service->stats.data,
1418 &counters->stats.data, append_all);
1420 connman_dbus_dict_close(&array, &dict);
1422 /* roaming counter */
1423 connman_dbus_dict_open(&array, &dict);
1425 stats_append_counters(&dict, &service->stats_roaming.data,
1426 &counters->stats_roaming.data, append_all);
1428 connman_dbus_dict_close(&array, &dict);
1430 __connman_counter_send_usage(counter, msg);
1433 static void stats_update(struct connman_service *service,
1434 unsigned int rx_packets, unsigned int tx_packets,
1435 unsigned int rx_bytes, unsigned int tx_bytes,
1436 unsigned int rx_errors, unsigned int tx_errors,
1437 unsigned int rx_dropped, unsigned int tx_dropped)
1439 struct connman_stats *stats = stats_get(service);
1440 struct connman_stats_data *data_last = &stats->data_last;
1441 struct connman_stats_data *data = &stats->data;
1442 unsigned int seconds;
1444 DBG("service %p", service);
1446 if (stats->valid == TRUE) {
1448 rx_packets - data_last->rx_packets;
1450 tx_packets - data_last->tx_packets;
1452 rx_bytes - data_last->rx_bytes;
1454 tx_bytes - data_last->tx_bytes;
1456 rx_errors - data_last->rx_errors;
1458 tx_errors - data_last->tx_errors;
1460 rx_dropped - data_last->rx_dropped;
1462 tx_dropped - data_last->tx_dropped;
1464 stats->valid = TRUE;
1467 data_last->rx_packets = rx_packets;
1468 data_last->tx_packets = tx_packets;
1469 data_last->rx_bytes = rx_bytes;
1470 data_last->tx_bytes = tx_bytes;
1471 data_last->rx_errors = rx_errors;
1472 data_last->tx_errors = tx_errors;
1473 data_last->rx_dropped = rx_dropped;
1474 data_last->tx_dropped = tx_dropped;
1476 seconds = g_timer_elapsed(stats->timer, NULL);
1477 stats->data.time = stats->data_last.time + seconds;
1480 void __connman_service_notify(struct connman_service *service,
1481 unsigned int rx_packets, unsigned int tx_packets,
1482 unsigned int rx_bytes, unsigned int tx_bytes,
1483 unsigned int rx_errors, unsigned int tx_errors,
1484 unsigned int rx_dropped, unsigned int tx_dropped)
1486 GHashTableIter iter;
1487 gpointer key, value;
1488 const char *counter;
1489 struct connman_stats_counter *counters;
1490 struct connman_stats_data *data;
1493 if (service == NULL)
1496 if (is_connected(service) == FALSE)
1499 stats_update(service,
1500 rx_packets, tx_packets,
1502 rx_errors, tx_errors,
1503 rx_dropped, tx_dropped);
1505 data = &stats_get(service)->data;
1506 err = __connman_stats_update(service, service->roaming, data);
1508 connman_error("Failed to store statistics for %s",
1509 service->identifier);
1511 g_hash_table_iter_init(&iter, service->counter_table);
1512 while (g_hash_table_iter_next(&iter, &key, &value)) {
1516 stats_append(service, counter, counters, counters->append_all);
1517 counters->append_all = FALSE;
1521 int __connman_service_counter_register(const char *counter)
1523 struct connman_service *service;
1524 GSequenceIter *iter;
1525 struct connman_stats_counter *counters;
1527 DBG("counter %s", counter);
1529 counter_list = g_slist_append(counter_list, (gpointer)counter);
1531 iter = g_sequence_get_begin_iter(service_list);
1533 while (g_sequence_iter_is_end(iter) == FALSE) {
1534 service = g_sequence_get(iter);
1536 counters = g_try_new0(struct connman_stats_counter, 1);
1537 if (counters == NULL)
1540 counters->append_all = TRUE;
1542 g_hash_table_replace(service->counter_table, (gpointer)counter,
1545 iter = g_sequence_iter_next(iter);
1551 void __connman_service_counter_unregister(const char *counter)
1553 struct connman_service *service;
1554 GSequenceIter *iter;
1556 DBG("counter %s", counter);
1558 iter = g_sequence_get_begin_iter(service_list);
1560 while (g_sequence_iter_is_end(iter) == FALSE) {
1561 service = g_sequence_get(iter);
1563 g_hash_table_remove(service->counter_table, counter);
1565 iter = g_sequence_iter_next(iter);
1568 counter_list = g_slist_remove(counter_list, counter);
1571 GSequence *__connman_service_get_list(struct connman_session *session,
1572 service_match_cb service_match,
1573 create_service_entry_cb create_service_entry,
1574 GDestroyNotify destroy_service_entry)
1577 GSequenceIter *iter;
1578 struct connman_service *service;
1579 struct service_entry *entry;
1581 list = g_sequence_new(destroy_service_entry);
1585 iter = g_sequence_get_begin_iter(service_list);
1587 while (g_sequence_iter_is_end(iter) == FALSE) {
1588 service = g_sequence_get(iter);
1590 if (service_match(session, service) == TRUE) {
1591 entry = create_service_entry(service, service->name,
1596 g_sequence_append(list, entry);
1599 iter = g_sequence_iter_next(iter);
1605 void __connman_service_session_inc(struct connman_service *service)
1607 DBG("service %p", service);
1609 g_atomic_int_inc(&service->session_usage_count);
1612 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1614 connman_bool_t in_use;
1616 if (g_atomic_int_dec_and_test(&service->session_usage_count) == TRUE)
1621 DBG("service %p last %d", service, in_use);
1626 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1627 struct connman_service *service)
1629 dbus_bool_t required;
1632 str = __connman_service_type2string(service->type);
1634 connman_dbus_dict_append_basic(dict, "Type",
1635 DBUS_TYPE_STRING, &str);
1637 connman_dbus_dict_append_array(dict, "Security",
1638 DBUS_TYPE_STRING, append_security, service);
1640 str = state2string(service->state);
1642 connman_dbus_dict_append_basic(dict, "State",
1643 DBUS_TYPE_STRING, &str);
1645 str = error2string(service->error);
1647 connman_dbus_dict_append_basic(dict, "Error",
1648 DBUS_TYPE_STRING, &str);
1650 if (service->strength > 0)
1651 connman_dbus_dict_append_basic(dict, "Strength",
1652 DBUS_TYPE_BYTE, &service->strength);
1654 connman_dbus_dict_append_basic(dict, "Favorite",
1655 DBUS_TYPE_BOOLEAN, &service->favorite);
1657 connman_dbus_dict_append_basic(dict, "Immutable",
1658 DBUS_TYPE_BOOLEAN, &service->immutable);
1660 if (service->favorite == TRUE)
1661 connman_dbus_dict_append_basic(dict, "AutoConnect",
1662 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1664 connman_dbus_dict_append_basic(dict, "AutoConnect",
1665 DBUS_TYPE_BOOLEAN, &service->favorite);
1667 if (service->name != NULL)
1668 connman_dbus_dict_append_basic(dict, "Name",
1669 DBUS_TYPE_STRING, &service->name);
1671 connman_dbus_dict_append_basic(dict, "LoginRequired",
1672 DBUS_TYPE_BOOLEAN, &service->login_required);
1674 switch (service->type) {
1675 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1676 case CONNMAN_SERVICE_TYPE_SYSTEM:
1677 case CONNMAN_SERVICE_TYPE_GPS:
1678 case CONNMAN_SERVICE_TYPE_VPN:
1679 case CONNMAN_SERVICE_TYPE_GADGET:
1681 case CONNMAN_SERVICE_TYPE_CELLULAR:
1682 connman_dbus_dict_append_basic(dict, "Roaming",
1683 DBUS_TYPE_BOOLEAN, &service->roaming);
1686 connman_dbus_dict_append_basic(dict, "SetupRequired",
1687 DBUS_TYPE_BOOLEAN, &required);
1688 connman_dbus_dict_append_dict(dict, "Ethernet",
1689 append_ethernet, service);
1691 case CONNMAN_SERVICE_TYPE_WIFI:
1692 if (service->passphrase != NULL && limited == FALSE)
1693 connman_dbus_dict_append_basic(dict, "Passphrase",
1694 DBUS_TYPE_STRING, &service->passphrase);
1698 switch (service->security) {
1699 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1700 case CONNMAN_SERVICE_SECURITY_NONE:
1702 case CONNMAN_SERVICE_SECURITY_WEP:
1703 case CONNMAN_SERVICE_SECURITY_PSK:
1704 case CONNMAN_SERVICE_SECURITY_WPA:
1705 case CONNMAN_SERVICE_SECURITY_RSN:
1706 if (service->passphrase == NULL)
1709 case CONNMAN_SERVICE_SECURITY_8021X:
1713 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1714 DBUS_TYPE_BOOLEAN, &required);
1717 case CONNMAN_SERVICE_TYPE_ETHERNET:
1718 case CONNMAN_SERVICE_TYPE_WIMAX:
1719 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1720 connman_dbus_dict_append_dict(dict, "Ethernet",
1721 append_ethernet, service);
1725 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1727 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1728 append_ipv4config, service);
1730 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1732 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1733 append_ipv6config, service);
1735 connman_dbus_dict_append_array(dict, "Nameservers",
1736 DBUS_TYPE_STRING, append_dns, service);
1738 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1739 DBUS_TYPE_STRING, append_dnsconfig, service);
1741 connman_dbus_dict_append_array(dict, "Domains",
1742 DBUS_TYPE_STRING, append_domain, service);
1744 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1745 DBUS_TYPE_STRING, append_domainconfig, service);
1747 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1749 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1750 append_proxyconfig, service);
1752 connman_dbus_dict_append_dict(dict, "Provider",
1753 append_provider, service);
1756 static void append_struct(gpointer value, gpointer user_data)
1758 struct connman_service *service = value;
1759 DBusMessageIter *iter = user_data;
1760 DBusMessageIter entry, dict;
1762 if (service->path == NULL || service->hidden == TRUE)
1765 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1767 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1770 connman_dbus_dict_open(&entry, &dict);
1771 append_properties(&dict, TRUE, service);
1772 connman_dbus_dict_close(&entry, &dict);
1774 dbus_message_iter_close_container(iter, &entry);
1777 void __connman_service_list_struct(DBusMessageIter *iter)
1779 g_sequence_foreach(service_list, append_struct, iter);
1782 int __connman_service_get_index(struct connman_service *service)
1784 if (service == NULL)
1787 if (service->network != NULL)
1788 return connman_network_get_index(service->network);
1789 else if (service->provider != NULL)
1790 return connman_provider_get_index(service->provider);
1795 void __connman_service_set_domainname(struct connman_service *service,
1796 const char *domainname)
1798 if (service == NULL)
1801 g_free(service->domainname);
1802 service->domainname = g_strdup(domainname);
1804 domain_changed(service);
1807 const char *connman_service_get_domainname(struct connman_service *service)
1809 if (service == NULL)
1812 if (service->domains != NULL)
1813 return service->domains[0];
1815 return service->domainname;
1818 char **connman_service_get_nameservers(struct connman_service *service)
1820 if (service == NULL)
1823 if (service->nameservers_config != NULL)
1824 return service->nameservers_config;
1825 else if (service->nameservers != NULL)
1826 return service->nameservers;
1831 void connman_service_set_proxy_method(struct connman_service *service,
1832 enum connman_service_proxy_method method)
1834 if (service == NULL)
1837 service->proxy = method;
1839 proxy_changed(service);
1841 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1842 __connman_notifier_proxy_changed(service);
1845 enum connman_service_proxy_method connman_service_get_proxy_method(
1846 struct connman_service *service)
1848 if (service == NULL)
1849 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1851 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1852 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1853 service->pac == NULL)
1854 return service->proxy;
1856 return service->proxy_config;
1859 return service->proxy;
1862 char **connman_service_get_proxy_servers(struct connman_service *service)
1864 return g_strdupv(service->proxies);
1867 char **connman_service_get_proxy_excludes(struct connman_service *service)
1869 return g_strdupv(service->excludes);
1872 const char *connman_service_get_proxy_url(struct connman_service *service)
1874 if (service == NULL)
1877 return service->pac;
1880 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1883 if (service == NULL)
1886 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1888 if (service->ipconfig_ipv4) {
1889 if (__connman_ipconfig_set_proxy_autoconfig(
1890 service->ipconfig_ipv4, url) < 0)
1892 } else if (service->ipconfig_ipv6) {
1893 if (__connman_ipconfig_set_proxy_autoconfig(
1894 service->ipconfig_ipv6, url) < 0)
1899 proxy_changed(service);
1901 __connman_notifier_proxy_changed(service);
1904 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1906 if (service == NULL)
1909 if (service->ipconfig_ipv4)
1910 return __connman_ipconfig_get_proxy_autoconfig(
1911 service->ipconfig_ipv4);
1912 else if (service->ipconfig_ipv6)
1913 return __connman_ipconfig_get_proxy_autoconfig(
1914 service->ipconfig_ipv6);
1918 static void update_timeservers(struct connman_service *service)
1922 if (service->timeservers == NULL)
1925 switch (service->state) {
1926 case CONNMAN_SERVICE_STATE_UNKNOWN:
1927 case CONNMAN_SERVICE_STATE_IDLE:
1928 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1929 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1931 case CONNMAN_SERVICE_STATE_FAILURE:
1932 case CONNMAN_SERVICE_STATE_DISCONNECT:
1933 for (i = 0; service->timeservers[i] != NULL; i++)
1934 connman_timeserver_remove(service->timeservers[i]);
1936 case CONNMAN_SERVICE_STATE_READY:
1937 case CONNMAN_SERVICE_STATE_ONLINE:
1941 for (i = 0; service->timeservers[i] != NULL; i++)
1942 connman_timeserver_append(service->timeservers[i]);
1945 int __connman_service_timeserver_append(struct connman_service *service,
1946 const char *timeserver)
1950 DBG("service %p timeserver %s", service, timeserver);
1952 if (timeserver == NULL)
1955 if (service->timeservers != NULL) {
1958 for (i = 0; service->timeservers[i] != NULL; i++)
1959 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1962 len = g_strv_length(service->timeservers);
1963 service->timeservers = g_try_renew(char *, service->timeservers,
1967 service->timeservers = g_try_new0(char *, len + 2);
1970 if (service->timeservers == NULL)
1973 service->timeservers[len] = g_strdup(timeserver);
1974 service->timeservers[len + 1] = NULL;
1976 update_timeservers(service);
1981 int __connman_service_timeserver_remove(struct connman_service *service,
1982 const char *timeserver)
1987 DBG("service %p timeserver %s", service, timeserver);
1989 if (timeserver == NULL)
1992 if (service->timeservers == NULL)
1995 len = g_strv_length(service->timeservers);
1997 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
2000 g_strfreev(service->timeservers);
2001 service->timeservers = NULL;
2006 servers = g_try_new0(char *, len - 1);
2007 if (servers == NULL)
2010 for (i = 0, j = 0; i < len; i++) {
2011 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2012 servers[j] = g_strdup(service->timeservers[i]);
2016 servers[len - 2] = NULL;
2018 g_strfreev(service->timeservers);
2019 service->timeservers = servers;
2021 update_timeservers(service);
2026 void __connman_service_set_pac(struct connman_service *service,
2032 g_free(service->pac);
2033 service->pac = g_strdup(pac);
2036 void __connman_service_set_identity(struct connman_service *service,
2037 const char *identity)
2039 if (service->immutable)
2042 g_free(service->identity);
2043 service->identity = g_strdup(identity);
2045 if (service->network != NULL)
2046 connman_network_set_string(service->network,
2051 void __connman_service_set_passphrase(struct connman_service *service,
2052 const char* passphrase)
2054 if (service->immutable == TRUE)
2057 g_free(service->passphrase);
2058 service->passphrase = g_strdup(passphrase);
2060 passphrase_changed(service);
2062 if (service->network != NULL)
2063 connman_network_set_string(service->network,
2065 service->passphrase);
2067 __connman_storage_save_service(service);
2070 static DBusMessage *get_properties(DBusConnection *conn,
2071 DBusMessage *msg, void *user_data)
2073 struct connman_service *service = user_data;
2075 DBusMessageIter array, dict;
2077 DBG("service %p", service);
2079 reply = dbus_message_new_method_return(msg);
2083 dbus_message_iter_init_append(reply, &array);
2085 connman_dbus_dict_open(&array, &dict);
2086 append_properties(&dict, FALSE, service);
2087 connman_dbus_dict_close(&array, &dict);
2092 static int update_proxy_configuration(struct connman_service *service,
2093 DBusMessageIter *array)
2095 DBusMessageIter dict;
2096 enum connman_service_proxy_method method;
2097 GString *servers_str = NULL;
2098 GString *excludes_str = NULL;
2099 const char *url = NULL;
2101 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2103 dbus_message_iter_recurse(array, &dict);
2105 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2106 DBusMessageIter entry, variant;
2110 dbus_message_iter_recurse(&dict, &entry);
2112 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2115 dbus_message_iter_get_basic(&entry, &key);
2116 dbus_message_iter_next(&entry);
2118 if (dbus_message_iter_get_arg_type(&entry) !=
2122 dbus_message_iter_recurse(&entry, &variant);
2123 type = dbus_message_iter_get_arg_type(&variant);
2125 if (g_str_equal(key, "Method") == TRUE) {
2128 if (type != DBUS_TYPE_STRING)
2131 dbus_message_iter_get_basic(&variant, &val);
2132 method = string2proxymethod(val);
2133 } else if (g_str_equal(key, "URL") == TRUE) {
2134 if (type != DBUS_TYPE_STRING)
2137 dbus_message_iter_get_basic(&variant, &url);
2138 } else if (g_str_equal(key, "Servers") == TRUE) {
2139 DBusMessageIter str_array;
2141 if (type != DBUS_TYPE_ARRAY)
2144 servers_str = g_string_new(NULL);
2145 if (servers_str == NULL)
2148 dbus_message_iter_recurse(&variant, &str_array);
2150 while (dbus_message_iter_get_arg_type(&str_array) ==
2154 dbus_message_iter_get_basic(&str_array, &val);
2156 if (servers_str->len > 0)
2157 g_string_append_printf(servers_str,
2160 g_string_append(servers_str, val);
2162 dbus_message_iter_next(&str_array);
2164 } else if (g_str_equal(key, "Excludes") == TRUE) {
2165 DBusMessageIter str_array;
2167 if (type != DBUS_TYPE_ARRAY)
2170 excludes_str = g_string_new(NULL);
2171 if (excludes_str == NULL)
2174 dbus_message_iter_recurse(&variant, &str_array);
2176 while (dbus_message_iter_get_arg_type(&str_array) ==
2180 dbus_message_iter_get_basic(&str_array, &val);
2182 if (excludes_str->len > 0)
2183 g_string_append_printf(excludes_str,
2186 g_string_append(excludes_str, val);
2188 dbus_message_iter_next(&str_array);
2192 dbus_message_iter_next(&dict);
2196 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2198 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2199 if (servers_str == NULL && service->proxies == NULL)
2202 if (servers_str != NULL) {
2203 g_strfreev(service->proxies);
2205 if (servers_str->len > 0)
2206 service->proxies = g_strsplit_set(
2207 servers_str->str, " ", 0);
2209 service->proxies = NULL;
2212 if (excludes_str != NULL) {
2213 g_strfreev(service->excludes);
2215 if (excludes_str->len > 0)
2216 service->excludes = g_strsplit_set(
2217 excludes_str->str, " ", 0);
2219 service->excludes = NULL;
2222 if (service->proxies == NULL)
2223 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2226 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2227 g_free(service->pac);
2229 if (url != NULL && strlen(url) > 0)
2230 service->pac = g_strdup(url);
2232 service->pac = NULL;
2234 /* if we are connected:
2235 - if service->pac == NULL
2236 - if __connman_ipconfig_get_proxy_autoconfig(
2237 service->ipconfig) == NULL
2238 --> We should start WPAD */
2241 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2245 if (servers_str != NULL)
2246 g_string_free(servers_str, TRUE);
2248 if (excludes_str != NULL)
2249 g_string_free(excludes_str, TRUE);
2251 service->proxy_config = method;
2256 if (servers_str != NULL)
2257 g_string_free(servers_str, TRUE);
2259 if (excludes_str != NULL)
2260 g_string_free(excludes_str, TRUE);
2265 static int set_ipconfig(struct connman_service *service,
2266 struct connman_ipconfig *ipconfig,
2267 DBusMessageIter *array,
2268 enum connman_service_state state,
2269 enum connman_service_state *new_state)
2271 enum connman_ipconfig_method old_method;
2272 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2273 enum connman_ipconfig_type type;
2276 old_method = __connman_ipconfig_get_method(ipconfig);
2278 if (is_connecting_state(service, state) ||
2279 is_connected_state(service, state))
2280 __connman_network_clear_ipconfig(service->network, ipconfig);
2282 err = __connman_ipconfig_set_config(ipconfig, array);
2283 method = __connman_ipconfig_get_method(ipconfig);
2284 type = __connman_ipconfig_get_config_type(ipconfig);
2286 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2287 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2288 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2289 *new_state = service->state_ipv4 =
2290 CONNMAN_SERVICE_STATE_CONFIGURATION;
2291 __connman_ipconfig_enable(ipconfig);
2294 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2295 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2296 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2297 *new_state = service->state_ipv6;
2298 __connman_ipconfig_enable(ipconfig);
2302 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2303 type, method, state2string(*new_state));
2308 static DBusMessage *set_property(DBusConnection *conn,
2309 DBusMessage *msg, void *user_data)
2311 struct connman_service *service = user_data;
2312 DBusMessageIter iter, value;
2316 DBG("service %p", service);
2318 if (dbus_message_iter_init(msg, &iter) == FALSE)
2319 return __connman_error_invalid_arguments(msg);
2321 dbus_message_iter_get_basic(&iter, &name);
2322 dbus_message_iter_next(&iter);
2323 dbus_message_iter_recurse(&iter, &value);
2325 type = dbus_message_iter_get_arg_type(&value);
2327 if (g_str_equal(name, "AutoConnect") == TRUE) {
2328 connman_bool_t autoconnect;
2330 if (type != DBUS_TYPE_BOOLEAN)
2331 return __connman_error_invalid_arguments(msg);
2333 if (service->favorite == FALSE)
2334 return __connman_error_invalid_service(msg);
2336 dbus_message_iter_get_basic(&value, &autoconnect);
2338 if (service->autoconnect == autoconnect)
2339 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2341 service->autoconnect = autoconnect;
2343 autoconnect_changed(service);
2345 __connman_storage_save_service(service);
2346 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2347 const char *passphrase;
2349 if (type != DBUS_TYPE_STRING)
2350 return __connman_error_invalid_arguments(msg);
2352 if (service->immutable == TRUE)
2353 return __connman_error_not_supported(msg);
2355 dbus_message_iter_get_basic(&value, &passphrase);
2357 __connman_service_set_passphrase(service, passphrase);
2358 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2359 DBusMessageIter entry;
2364 if (type != DBUS_TYPE_ARRAY)
2365 return __connman_error_invalid_arguments(msg);
2367 str = g_string_new(NULL);
2369 return __connman_error_invalid_arguments(msg);
2371 index = connman_network_get_index(service->network);
2372 gw = __connman_ipconfig_get_gateway_from_index(index);
2374 if (gw && strlen(gw))
2375 __connman_service_nameserver_del_routes(service);
2377 dbus_message_iter_recurse(&value, &entry);
2379 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2381 dbus_message_iter_get_basic(&entry, &val);
2382 dbus_message_iter_next(&entry);
2384 g_string_append_printf(str, " %s", val);
2386 g_string_append(str, val);
2389 g_strfreev(service->nameservers_config);
2392 service->nameservers_config =
2393 g_strsplit_set(str->str, " ", 0);
2395 service->nameservers_config = NULL;
2398 g_string_free(str, TRUE);
2400 if (gw && strlen(gw))
2401 __connman_service_nameserver_add_routes(service, gw);
2403 update_nameservers(service);
2404 dns_configuration_changed(service);
2406 __connman_storage_save_service(service);
2407 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2408 DBusMessageIter entry;
2411 if (type != DBUS_TYPE_ARRAY)
2412 return __connman_error_invalid_arguments(msg);
2414 str = g_string_new(NULL);
2416 return __connman_error_invalid_arguments(msg);
2418 dbus_message_iter_recurse(&value, &entry);
2420 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2422 dbus_message_iter_get_basic(&entry, &val);
2423 dbus_message_iter_next(&entry);
2425 g_string_append_printf(str, " %s", val);
2427 g_string_append(str, val);
2430 g_strfreev(service->domains);
2433 service->domains = g_strsplit_set(str->str, " ", 0);
2435 service->domains = NULL;
2437 g_string_free(str, TRUE);
2439 update_nameservers(service);
2440 domain_configuration_changed(service);
2442 __connman_storage_save_service(service);
2443 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2446 if (type != DBUS_TYPE_ARRAY)
2447 return __connman_error_invalid_arguments(msg);
2449 err = update_proxy_configuration(service, &value);
2452 return __connman_error_failed(msg, -err);
2454 proxy_configuration_changed(service);
2456 __connman_storage_save_service(service);
2457 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2458 g_str_equal(name, "IPv6.Configuration")) {
2460 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2461 enum connman_service_state state =
2462 CONNMAN_SERVICE_STATE_UNKNOWN;
2467 if (service->ipconfig_ipv4 == NULL &&
2468 service->ipconfig_ipv6 == NULL)
2469 return __connman_error_invalid_property(msg);
2471 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2472 ipv4 = service->ipconfig_ipv4;
2473 err = set_ipconfig(service, ipv4, &value,
2474 service->state_ipv4, &state);
2476 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2477 ipv6 = service->ipconfig_ipv6;
2478 err = set_ipconfig(service, ipv6, &value,
2479 service->state_ipv6, &state);
2483 if (is_connected_state(service, state) ||
2484 is_connecting_state(service, state))
2485 __connman_network_set_ipconfig(service->network,
2487 return __connman_error_failed(msg, -err);
2491 ipv4_configuration_changed(service);
2493 ipv6_configuration_changed(service);
2495 if (is_connecting(service) || is_connected(service))
2496 __connman_network_set_ipconfig(service->network,
2499 __connman_storage_save_service(service);
2501 return __connman_error_invalid_property(msg);
2503 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2506 static void set_idle(struct connman_service *service)
2508 service->state = service->state_ipv4 = service->state_ipv6 =
2509 CONNMAN_SERVICE_STATE_IDLE;
2510 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2511 state_changed(service);
2514 static DBusMessage *clear_property(DBusConnection *conn,
2515 DBusMessage *msg, void *user_data)
2517 struct connman_service *service = user_data;
2520 DBG("service %p", service);
2522 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2525 if (g_str_equal(name, "Error") == TRUE) {
2528 g_get_current_time(&service->modified);
2529 __connman_storage_save_service(service);
2530 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2531 if (service->immutable == TRUE)
2532 return __connman_error_not_supported(msg);
2534 g_free(service->passphrase);
2535 service->passphrase = NULL;
2537 passphrase_changed(service);
2539 __connman_storage_save_service(service);
2541 return __connman_error_invalid_property(msg);
2543 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2546 static connman_bool_t is_ignore(struct connman_service *service)
2548 if (service->autoconnect == FALSE)
2551 if (service->roaming == TRUE)
2554 if (service->ignore == TRUE)
2557 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2563 void __connman_service_auto_connect(void)
2565 struct connman_service *service = NULL;
2566 GSequenceIter *iter;
2570 if (__connman_session_mode() == TRUE) {
2571 DBG("Session mode enabled: auto connect disabled");
2575 iter = g_sequence_get_begin_iter(service_list);
2577 while (g_sequence_iter_is_end(iter) == FALSE) {
2578 service = g_sequence_get(iter);
2580 if (service->pending != NULL)
2583 if (is_connecting(service) == TRUE)
2586 if (service->favorite == FALSE)
2589 if (is_connected(service) == TRUE)
2592 if (is_ignore(service) == FALSE && service->state ==
2593 CONNMAN_SERVICE_STATE_IDLE)
2598 iter = g_sequence_iter_next(iter);
2601 if (service != NULL) {
2602 service->userconnect = FALSE;
2603 __connman_service_connect(service);
2607 static void remove_timeout(struct connman_service *service)
2609 if (service->timeout > 0) {
2610 g_source_remove(service->timeout);
2611 service->timeout = 0;
2615 static void reply_pending(struct connman_service *service, int error)
2617 remove_timeout(service);
2619 if (service->pending != NULL) {
2623 reply = __connman_error_failed(service->pending,
2626 g_dbus_send_message(connection, reply);
2630 sender = dbus_message_get_interface(service->pending);
2632 DBG("sender %s", sender);
2634 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2635 g_dbus_send_reply(connection, service->pending,
2636 DBUS_TYPE_OBJECT_PATH, &service->path,
2639 g_dbus_send_reply(connection, service->pending,
2643 dbus_message_unref(service->pending);
2644 service->pending = NULL;
2648 static gboolean connect_timeout(gpointer user_data)
2650 struct connman_service *service = user_data;
2651 connman_bool_t autoconnect = FALSE;
2653 DBG("service %p", service);
2655 service->timeout = 0;
2657 if (service->network != NULL)
2658 __connman_network_disconnect(service->network);
2660 __connman_ipconfig_disable(service->ipconfig_ipv4);
2661 __connman_ipconfig_disable(service->ipconfig_ipv6);
2663 __connman_stats_service_unregister(service);
2665 if (service->pending != NULL) {
2668 reply = __connman_error_operation_timeout(service->pending);
2670 g_dbus_send_message(connection, reply);
2672 dbus_message_unref(service->pending);
2673 service->pending = NULL;
2677 __connman_service_ipconfig_indicate_state(service,
2678 CONNMAN_SERVICE_STATE_FAILURE,
2679 CONNMAN_IPCONFIG_TYPE_IPV4);
2680 __connman_service_ipconfig_indicate_state(service,
2681 CONNMAN_SERVICE_STATE_FAILURE,
2682 CONNMAN_IPCONFIG_TYPE_IPV6);
2684 if (autoconnect == TRUE && service->userconnect == FALSE)
2685 __connman_service_auto_connect();
2690 static void set_reconnect_state(struct connman_service *service,
2691 connman_bool_t reconnect)
2693 struct connman_device *device;
2695 if (service->network == NULL)
2698 device = connman_network_get_device(service->network);
2702 __connman_device_set_reconnect(device, reconnect);
2705 static connman_bool_t get_reconnect_state(struct connman_service *service)
2707 struct connman_device *device;
2709 if (service->network == NULL)
2712 device = connman_network_get_device(service->network);
2716 return __connman_device_get_reconnect(device);
2719 static void request_input_cb (struct connman_service *service,
2720 const char *identity, const char *passphrase,
2723 DBG ("RequestInput return, %p", service);
2725 if (identity == NULL && passphrase == NULL && service->wps == FALSE)
2728 if (identity != NULL)
2729 __connman_service_set_identity(service, identity);
2731 if (passphrase != NULL)
2732 __connman_service_set_passphrase(service, passphrase);
2734 __connman_service_connect(service);
2737 static DBusMessage *connect_service(DBusConnection *conn,
2738 DBusMessage *msg, void *user_data)
2740 struct connman_service *service = user_data;
2741 GSequenceIter *iter;
2744 DBG("service %p", service);
2746 if (service->pending != NULL)
2747 return __connman_error_in_progress(msg);
2749 iter = g_sequence_get_begin_iter(service_list);
2751 while (g_sequence_iter_is_end(iter) == FALSE) {
2752 struct connman_service *temp = g_sequence_get(iter);
2754 if (service->type == temp->type && is_connecting(temp) == TRUE)
2755 return __connman_error_in_progress(msg);
2757 iter = g_sequence_iter_next(iter);
2760 service->ignore = FALSE;
2762 service->userconnect = TRUE;
2764 service->pending = dbus_message_ref(msg);
2766 set_reconnect_state(service, FALSE);
2768 err = __connman_service_connect(service);
2770 if (err == -ENOKEY) {
2771 if (__connman_agent_request_input(service,
2777 if (service->pending == NULL)
2780 if (err != -EINPROGRESS) {
2781 dbus_message_unref(service->pending);
2782 service->pending = NULL;
2784 return __connman_error_failed(msg, -err);
2790 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2793 static DBusMessage *disconnect_service(DBusConnection *conn,
2794 DBusMessage *msg, void *user_data)
2796 struct connman_service *service = user_data;
2799 DBG("service %p", service);
2801 reply_pending(service, ECONNABORTED);
2803 service->ignore = TRUE;
2805 set_reconnect_state(service, FALSE);
2807 err = __connman_service_disconnect(service);
2809 if (err != -EINPROGRESS)
2810 return __connman_error_failed(msg, -err);
2815 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2818 static DBusMessage *remove_service(DBusConnection *conn,
2819 DBusMessage *msg, void *user_data)
2821 struct connman_service *service = user_data;
2823 DBG("service %p", service);
2825 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2826 return __connman_error_not_supported(msg);
2828 if (service->immutable == TRUE)
2829 return __connman_error_not_supported(msg);
2831 if (service->favorite == FALSE && service->state !=
2832 CONNMAN_SERVICE_STATE_FAILURE)
2833 return __connman_error_not_supported(msg);
2835 if (service->network != NULL) {
2836 set_reconnect_state(service, FALSE);
2838 __connman_network_disconnect(service->network);
2841 g_free(service->passphrase);
2842 service->passphrase = NULL;
2844 passphrase_changed(service);
2848 __connman_service_set_favorite(service, FALSE);
2849 __connman_storage_save_service(service);
2851 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2854 static DBusMessage *move_service(DBusConnection *conn,
2855 DBusMessage *msg, void *user_data,
2858 struct connman_service *service = user_data;
2859 struct connman_service *target;
2861 GSequenceIter *src, *dst;
2863 DBG("service %p", service);
2865 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2868 if (service->favorite == FALSE)
2869 return __connman_error_not_supported(msg);
2871 target = find_service(path);
2872 if (target == NULL || target->favorite == FALSE || target == service)
2873 return __connman_error_invalid_service(msg);
2875 DBG("target %s", target->identifier);
2877 if (target->state_ipv4 != service->state_ipv4 &&
2878 target->state_ipv6 != service->state_ipv6)
2879 return __connman_error_invalid_service(msg);
2881 g_get_current_time(&service->modified);
2882 __connman_storage_save_service(service);
2884 src = g_hash_table_lookup(service_hash, service->identifier);
2885 dst = g_hash_table_lookup(service_hash, target->identifier);
2887 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2889 __connman_profile_changed(FALSE);
2891 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2894 static DBusMessage *move_before(DBusConnection *conn,
2895 DBusMessage *msg, void *user_data)
2897 return move_service(conn, msg, user_data, TRUE);
2900 static DBusMessage *move_after(DBusConnection *conn,
2901 DBusMessage *msg, void *user_data)
2903 return move_service(conn, msg, user_data, FALSE);
2906 static DBusMessage *reset_counters(DBusConnection *conn,
2907 DBusMessage *msg, void *user_data)
2909 struct connman_service *service = user_data;
2911 reset_stats(service);
2913 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2916 static GDBusMethodTable service_methods[] = {
2917 { "GetProperties", "", "a{sv}", get_properties },
2918 { "SetProperty", "sv", "", set_property },
2919 { "ClearProperty", "s", "", clear_property },
2920 { "Connect", "", "", connect_service,
2921 G_DBUS_METHOD_FLAG_ASYNC },
2922 { "Disconnect", "", "", disconnect_service },
2923 { "Remove", "", "", remove_service },
2924 { "MoveBefore", "o", "", move_before },
2925 { "MoveAfter", "o", "", move_after },
2926 { "ResetCounters", "", "", reset_counters },
2930 static GDBusSignalTable service_signals[] = {
2931 { "PropertyChanged", "sv" },
2935 static void service_free(gpointer user_data)
2937 struct connman_service *service = user_data;
2938 char *path = service->path;
2940 DBG("service %p", service);
2942 reply_pending(service, ENOENT);
2944 g_hash_table_remove(service_hash, service->identifier);
2946 __connman_notifier_service_remove(service);
2948 stats_stop(service);
2949 __connman_storage_save_service(service);
2951 service->path = NULL;
2954 __connman_profile_changed(FALSE);
2956 g_dbus_unregister_interface(connection, path,
2957 CONNMAN_SERVICE_INTERFACE);
2961 g_hash_table_destroy(service->counter_table);
2963 if (service->network != NULL) {
2964 if (service->network_created == TRUE)
2965 connman_network_unref(service->network);
2968 if (service->provider != NULL)
2969 connman_provider_unref(service->provider);
2971 if (service->ipconfig_ipv4 != NULL) {
2972 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
2973 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
2974 connman_ipconfig_unref(service->ipconfig_ipv4);
2975 service->ipconfig_ipv4 = NULL;
2978 if (service->ipconfig_ipv6 != NULL) {
2979 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
2980 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
2981 connman_ipconfig_unref(service->ipconfig_ipv6);
2982 service->ipconfig_ipv6 = NULL;
2985 if (service->location != NULL)
2986 connman_location_unref(service->location);
2988 g_strfreev(service->nameservers);
2989 g_strfreev(service->nameservers_config);
2990 g_strfreev(service->domains);
2991 g_strfreev(service->proxies);
2992 g_strfreev(service->excludes);
2994 g_free(service->domainname);
2995 g_free(service->pac);
2996 g_free(service->profile);
2997 g_free(service->name);
2998 g_free(service->passphrase);
2999 g_free(service->identifier);
3000 g_free(service->eap);
3001 g_free(service->identity);
3002 g_free(service->ca_cert_file);
3003 g_free(service->client_cert_file);
3004 g_free(service->private_key_file);
3005 g_free(service->private_key_passphrase);
3006 g_free(service->phase2);
3008 if (service->stats.timer != NULL)
3009 g_timer_destroy(service->stats.timer);
3010 if (service->stats_roaming.timer != NULL)
3011 g_timer_destroy(service->stats_roaming.timer);
3017 * __connman_service_put:
3018 * @service: service structure
3020 * Release service if no longer needed
3022 void __connman_service_put(struct connman_service *service)
3024 DBG("service %p", service);
3026 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
3027 GSequenceIter *iter;
3029 iter = g_hash_table_lookup(service_hash, service->identifier);
3031 reply_pending(service, ECONNABORTED);
3033 __connman_service_disconnect(service);
3035 g_sequence_remove(iter);
3037 service_free(service);
3041 static void stats_init(struct connman_service *service)
3044 service->stats.valid = FALSE;
3045 service->stats.enabled = FALSE;
3046 service->stats.timer = g_timer_new();
3049 service->stats_roaming.valid = FALSE;
3050 service->stats_roaming.enabled = FALSE;
3051 service->stats_roaming.timer = g_timer_new();
3054 static void service_initialize(struct connman_service *service)
3056 DBG("service %p", service);
3058 service->refcount = 1;
3059 service->session_usage_count = 0;
3061 service->network_created = FALSE;
3063 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3064 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3066 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3067 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3068 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3070 service->favorite = FALSE;
3071 service->immutable = FALSE;
3072 service->hidden = FALSE;
3074 service->ignore = FALSE;
3076 service->userconnect = FALSE;
3080 stats_init(service);
3082 service->provider = NULL;
3084 service->wps = FALSE;
3088 * connman_service_create:
3090 * Allocate a new service.
3092 * Returns: a newly-allocated #connman_service structure
3094 struct connman_service *connman_service_create(void)
3097 struct connman_stats_counter *counters;
3098 const char *counter;
3100 struct connman_service *service;
3102 service = g_try_new0(struct connman_service, 1);
3103 if (service == NULL)
3106 DBG("service %p", service);
3108 service->counter_table = g_hash_table_new_full(g_str_hash,
3109 g_str_equal, NULL, g_free);
3111 for (list = counter_list; list; list = list->next) {
3112 counter = list->data;
3114 counters = g_try_new0(struct connman_stats_counter, 1);
3115 if (counters == NULL) {
3116 g_hash_table_destroy(service->counter_table);
3121 counters->append_all = TRUE;
3123 g_hash_table_replace(service->counter_table, (gpointer)counter,
3127 service_initialize(service);
3129 service->location = __connman_location_create(service);
3134 struct connman_location *__connman_service_get_location(struct connman_service *service)
3136 return service->location;
3140 * connman_service_ref:
3141 * @service: service structure
3143 * Increase reference counter of service
3145 struct connman_service *connman_service_ref(struct connman_service *service)
3149 g_atomic_int_inc(&service->refcount);
3155 * connman_service_unref:
3156 * @service: service structure
3158 * Decrease reference counter of service
3160 void connman_service_unref(struct connman_service *service)
3162 __connman_service_put(service);
3165 static gint service_compare(gconstpointer a, gconstpointer b,
3168 struct connman_service *service_a = (void *) a;
3169 struct connman_service *service_b = (void *) b;
3170 enum connman_service_state state_a, state_b;
3172 state_a = service_a->state;
3173 state_b = service_b->state;
3175 if (state_a != state_b) {
3176 if (is_connected(service_a) == TRUE)
3178 if (is_connected(service_b) == TRUE)
3181 if (is_connecting(service_a) == TRUE)
3183 if (is_connecting(service_b) == TRUE)
3187 if (service_a->order > service_b->order)
3190 if (service_a->order < service_b->order)
3193 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3196 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3199 if (service_a->type != service_b->type) {
3200 switch (service_a->type) {
3201 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3202 case CONNMAN_SERVICE_TYPE_SYSTEM:
3203 case CONNMAN_SERVICE_TYPE_ETHERNET:
3204 case CONNMAN_SERVICE_TYPE_GPS:
3205 case CONNMAN_SERVICE_TYPE_VPN:
3206 case CONNMAN_SERVICE_TYPE_GADGET:
3208 case CONNMAN_SERVICE_TYPE_WIFI:
3210 case CONNMAN_SERVICE_TYPE_WIMAX:
3211 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3212 case CONNMAN_SERVICE_TYPE_CELLULAR:
3217 return (gint) service_b->strength - (gint) service_a->strength;
3221 * connman_service_get_type:
3222 * @service: service structure
3224 * Get the type of service
3226 enum connman_service_type connman_service_get_type(struct connman_service *service)
3228 if (service == NULL)
3229 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3231 return service->type;
3235 * connman_service_get_interface:
3236 * @service: service structure
3238 * Get network interface of service
3240 char *connman_service_get_interface(struct connman_service *service)
3244 if (service == NULL)
3247 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3248 if (service->ipconfig_ipv4)
3249 index = connman_ipconfig_get_index(
3250 service->ipconfig_ipv4);
3251 else if (service->ipconfig_ipv6)
3252 index = connman_ipconfig_get_index(
3253 service->ipconfig_ipv6);
3257 return connman_inet_ifname(index);
3260 if (service->network == NULL)
3263 index = connman_network_get_index(service->network);
3265 return connman_inet_ifname(index);
3269 * connman_service_get_network:
3270 * @service: service structure
3272 * Get the service network
3274 struct connman_network *
3275 __connman_service_get_network(struct connman_service *service)
3277 if (service == NULL)
3280 return service->network;
3283 struct connman_ipconfig *
3284 __connman_service_get_ip4config(struct connman_service *service)
3286 if (service == NULL)
3289 return service->ipconfig_ipv4;
3292 struct connman_ipconfig *
3293 __connman_service_get_ip6config(struct connman_service *service)
3295 if (service == NULL)
3298 return service->ipconfig_ipv6;
3301 struct connman_ipconfig *
3302 __connman_service_get_ipconfig(struct connman_service *service, int family)
3304 if (family == AF_INET)
3305 return __connman_service_get_ip4config(service);
3306 else if (family == AF_INET6)
3307 return __connman_service_get_ip6config(service);
3313 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3315 if (service == NULL)
3316 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3318 return service->security;
3321 const char *__connman_service_get_phase2(struct connman_service *service)
3323 if (service == NULL)
3326 return service->phase2;
3329 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3331 if (service == NULL)
3334 return service->wps;
3338 * __connman_service_set_favorite:
3339 * @service: service structure
3340 * @favorite: favorite value
3342 * Change the favorite setting of service
3344 int __connman_service_set_favorite(struct connman_service *service,
3345 connman_bool_t favorite)
3347 GSequenceIter *iter;
3349 iter = g_hash_table_lookup(service_hash, service->identifier);
3353 if (service->favorite == favorite)
3356 service->favorite = favorite;
3358 favorite_changed(service);
3360 g_sequence_sort_changed(iter, service_compare, NULL);
3362 __connman_profile_changed(FALSE);
3367 int __connman_service_set_immutable(struct connman_service *service,
3368 connman_bool_t immutable)
3370 service->immutable = immutable;
3372 immutable_changed(service);
3377 void __connman_service_set_string(struct connman_service *service,
3378 const char *key, const char *value)
3380 if (g_str_equal(key, "EAP") == TRUE) {
3381 g_free(service->eap);
3382 service->eap = g_strdup(value);
3383 } else if (g_str_equal(key, "Identity") == TRUE) {
3384 g_free(service->identity);
3385 service->identity = g_strdup(value);
3386 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3387 g_free(service->ca_cert_file);
3388 service->ca_cert_file = g_strdup(value);
3389 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3390 g_free(service->client_cert_file);
3391 service->client_cert_file = g_strdup(value);
3392 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3393 g_free(service->private_key_file);
3394 service->private_key_file = g_strdup(value);
3395 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3396 g_free(service->private_key_passphrase);
3397 service->private_key_passphrase = g_strdup(value);
3398 } else if (g_str_equal(key, "Phase2") == TRUE) {
3399 g_free(service->phase2);
3400 service->phase2 = g_strdup(value);
3401 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3402 g_free(service->passphrase);
3403 service->passphrase = g_strdup(value);
3407 static void service_complete(struct connman_service *service)
3409 reply_pending(service, EIO);
3411 if (service->userconnect == FALSE)
3412 __connman_service_auto_connect();
3414 g_get_current_time(&service->modified);
3415 __connman_storage_save_service(service);
3418 static void report_error_cb(struct connman_service *service,
3419 gboolean retry, void *user_data)
3422 __connman_service_connect(service);
3424 service_complete(service);
3425 __connman_profile_changed(FALSE);
3426 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3430 static int __connman_service_indicate_state(struct connman_service *service)
3432 enum connman_service_state old_state, new_state;
3433 GSequenceIter *iter;
3435 if (service == NULL)
3438 old_state = service->state;
3439 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3441 if (old_state == new_state)
3444 DBG("service %p old %s - new %s/%s => %s",
3446 state2string(old_state),
3447 state2string(service->state_ipv4),
3448 state2string(service->state_ipv6),
3449 state2string(new_state));
3451 service->state = new_state;
3452 state_changed(service);
3454 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3455 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3456 reply_pending(service, ECONNABORTED);
3458 __connman_service_disconnect(service);
3461 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3462 if (__connman_stats_service_register(service) == 0) {
3463 __connman_stats_get(service, FALSE,
3464 &service->stats.data);
3465 __connman_stats_get(service, TRUE,
3466 &service->stats_roaming.data);
3470 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3471 if (service->login_required == TRUE) {
3472 service->login_required = FALSE;
3473 login_changed(service);
3476 connman_timeserver_sync();
3481 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
3482 connman_bool_t reconnect;
3484 reconnect = get_reconnect_state(service);
3485 if (reconnect == TRUE)
3486 __connman_service_auto_connect();
3488 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3491 if (new_state == CONNMAN_SERVICE_STATE_READY) {
3492 enum connman_ipconfig_method method;
3494 set_reconnect_state(service, TRUE);
3496 __connman_service_set_favorite(service, TRUE);
3498 reply_pending(service, 0);
3500 service->userconnect = FALSE;
3502 g_get_current_time(&service->modified);
3503 __connman_storage_save_service(service);
3505 update_nameservers(service);
3506 dns_changed(service);
3507 domain_changed(service);
3509 __connman_notifier_connect(service->type);
3511 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3512 connman_network_get_bool(service->network,
3513 "WiFi.UseWPS") == TRUE) {
3516 pass = connman_network_get_string(service->network,
3519 __connman_service_set_passphrase(service, pass);
3521 connman_network_set_bool(service->network,
3522 "WiFi.UseWPS", FALSE);
3527 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3528 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3529 __connman_ipconfig_disable_ipv6(
3530 service->ipconfig_ipv6);
3532 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3533 struct connman_service *def_service = get_default();
3535 if (__connman_notifier_count_connected() == 0 &&
3536 def_service != NULL &&
3537 def_service->provider != NULL)
3538 __connman_provider_disconnect(def_service->provider);
3540 __connman_location_finish(service);
3544 __connman_wpad_stop(service);
3546 update_nameservers(service);
3547 dns_changed(service);
3548 domain_changed(service);
3550 __connman_notifier_disconnect(service->type);
3553 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
3554 if (service->userconnect == TRUE &&
3555 __connman_agent_report_error(service,
3556 error2string(service->error),
3557 report_error_cb, NULL) == -EIO)
3559 service_complete(service);
3561 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3563 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3565 iter = g_hash_table_lookup(service_hash, service->identifier);
3567 g_sequence_sort_changed(iter, service_compare, NULL);
3569 __connman_profile_changed(FALSE);
3574 int __connman_service_indicate_error(struct connman_service *service,
3575 enum connman_service_error error)
3577 DBG("service %p error %d", service, error);
3579 if (service == NULL)
3582 service->error = error;
3584 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3585 __connman_service_set_passphrase(service, NULL);
3587 return __connman_service_ipconfig_indicate_state(service,
3588 CONNMAN_SERVICE_STATE_FAILURE,
3589 CONNMAN_IPCONFIG_TYPE_IPV4);
3592 int __connman_service_clear_error(struct connman_service *service)
3594 DBG("service %p", service);
3596 if (service == NULL)
3599 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
3602 service->state_ipv4 = service->state_ipv6 =
3603 CONNMAN_SERVICE_STATE_UNKNOWN;
3604 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3606 if (service->favorite == TRUE)
3607 set_reconnect_state(service, TRUE);
3609 __connman_service_ipconfig_indicate_state(service,
3610 CONNMAN_SERVICE_STATE_IDLE,
3611 CONNMAN_IPCONFIG_TYPE_IPV6);
3614 * Toggling the IPv6 state to IDLE could trigger the auto connect
3615 * machinery and consequently the IPv4 state.
3617 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3618 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3621 return __connman_service_ipconfig_indicate_state(service,
3622 CONNMAN_SERVICE_STATE_IDLE,
3623 CONNMAN_IPCONFIG_TYPE_IPV4);
3626 int __connman_service_indicate_default(struct connman_service *service)
3628 DBG("service %p", service);
3632 __connman_location_detect(service);
3637 static void check_proxy_setup(struct connman_service *service)
3640 * We start WPAD if we haven't got a PAC URL from DHCP and
3641 * if our proxy manual configuration is either empty or set
3642 * to AUTO with an empty URL.
3645 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
3648 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3649 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
3650 service->pac != NULL))
3653 if (__connman_wpad_start(service) < 0) {
3654 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3655 __connman_notifier_proxy_changed(service);
3659 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
3660 enum connman_service_state new_state,
3661 enum connman_ipconfig_type type)
3663 struct connman_ipconfig *ipconfig = NULL;
3664 enum connman_service_state old_state;
3666 if (service == NULL)
3669 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3670 old_state = service->state_ipv4;
3671 ipconfig = service->ipconfig_ipv4;
3672 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3673 old_state = service->state_ipv6;
3674 ipconfig = service->ipconfig_ipv6;
3677 if (ipconfig == NULL)
3681 if (old_state == new_state)
3684 DBG("service %p (%s) state %d (%s) type %d (%s)",
3685 service, service ? service->identifier : NULL,
3686 new_state, state2string(new_state),
3687 type, __connman_ipconfig_type2string(type));
3689 switch (new_state) {
3690 case CONNMAN_SERVICE_STATE_UNKNOWN:
3691 case CONNMAN_SERVICE_STATE_IDLE:
3692 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3695 case CONNMAN_SERVICE_STATE_ASSOCIATION:
3697 case CONNMAN_SERVICE_STATE_CONFIGURATION:
3698 __connman_ipconfig_enable(ipconfig);
3700 case CONNMAN_SERVICE_STATE_READY:
3701 update_nameservers(service);
3703 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3704 check_proxy_setup(service);
3706 case CONNMAN_SERVICE_STATE_ONLINE:
3708 case CONNMAN_SERVICE_STATE_DISCONNECT:
3709 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
3712 case CONNMAN_SERVICE_STATE_FAILURE:
3716 /* We keep that state */
3717 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3718 service->state_ipv4 = new_state;
3719 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3720 service->state_ipv6 = new_state;
3722 return __connman_service_indicate_state(service);
3725 int __connman_service_request_login(struct connman_service *service)
3727 DBG("service %p", service);
3729 if (service == NULL)
3732 service->login_required = TRUE;
3733 login_changed(service);
3738 static connman_bool_t prepare_network(struct connman_service *service)
3740 enum connman_network_type type;
3741 unsigned int ssid_len;
3743 type = connman_network_get_type(service->network);
3746 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3747 case CONNMAN_NETWORK_TYPE_VENDOR:
3749 case CONNMAN_NETWORK_TYPE_WIFI:
3750 if (connman_network_get_blob(service->network, "WiFi.SSID",
3754 if (service->passphrase != NULL)
3755 connman_network_set_string(service->network,
3756 "WiFi.Passphrase", service->passphrase);
3758 case CONNMAN_NETWORK_TYPE_ETHERNET:
3759 case CONNMAN_NETWORK_TYPE_WIMAX:
3760 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3761 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3762 case CONNMAN_NETWORK_TYPE_CELLULAR:
3769 static void prepare_8021x(struct connman_service *service)
3771 if (service->eap != NULL)
3772 connman_network_set_string(service->network, "WiFi.EAP",
3775 if (service->identity != NULL)
3776 connman_network_set_string(service->network, "WiFi.Identity",
3779 if (service->ca_cert_file != NULL)
3780 connman_network_set_string(service->network, "WiFi.CACertFile",
3781 service->ca_cert_file);
3783 if (service->client_cert_file != NULL)
3784 connman_network_set_string(service->network,
3785 "WiFi.ClientCertFile",
3786 service->client_cert_file);
3788 if (service->private_key_file != NULL)
3789 connman_network_set_string(service->network,
3790 "WiFi.PrivateKeyFile",
3791 service->private_key_file);
3793 if (service->private_key_passphrase != NULL)
3794 connman_network_set_string(service->network,
3795 "WiFi.PrivateKeyPassphrase",
3796 service->private_key_passphrase);
3798 if (service->phase2 != NULL)
3799 connman_network_set_string(service->network, "WiFi.Phase2",
3803 static int service_connect(struct connman_service *service)
3807 switch (service->type) {
3808 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3809 case CONNMAN_SERVICE_TYPE_SYSTEM:
3810 case CONNMAN_SERVICE_TYPE_GPS:
3811 case CONNMAN_SERVICE_TYPE_GADGET:
3813 case CONNMAN_SERVICE_TYPE_ETHERNET:
3814 case CONNMAN_SERVICE_TYPE_WIMAX:
3815 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3816 case CONNMAN_SERVICE_TYPE_CELLULAR:
3817 case CONNMAN_SERVICE_TYPE_VPN:
3819 case CONNMAN_SERVICE_TYPE_WIFI:
3820 switch (service->security) {
3821 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3822 case CONNMAN_SERVICE_SECURITY_NONE:
3824 case CONNMAN_SERVICE_SECURITY_WEP:
3825 case CONNMAN_SERVICE_SECURITY_PSK:
3826 case CONNMAN_SERVICE_SECURITY_WPA:
3827 case CONNMAN_SERVICE_SECURITY_RSN:
3828 if (service->passphrase == NULL) {
3829 if (service->network == NULL)
3832 if (service->wps == FALSE ||
3833 connman_network_get_bool(
3835 "WiFi.UseWPS") == FALSE)
3839 case CONNMAN_SERVICE_SECURITY_8021X:
3840 if (service->eap == NULL)
3844 * never request credentials if using EAP-TLS
3845 * (EAP-TLS networks need to be fully provisioned)
3847 if (g_str_equal(service->eap, "tls") == TRUE)
3850 if (service->immutable != TRUE)
3858 if (service->network != NULL) {
3859 if (prepare_network(service) == FALSE)
3862 switch (service->security) {
3863 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3864 case CONNMAN_SERVICE_SECURITY_NONE:
3865 case CONNMAN_SERVICE_SECURITY_WEP:
3866 case CONNMAN_SERVICE_SECURITY_PSK:
3867 case CONNMAN_SERVICE_SECURITY_WPA:
3868 case CONNMAN_SERVICE_SECURITY_RSN:
3870 case CONNMAN_SERVICE_SECURITY_8021X:
3871 prepare_8021x(service);
3875 if (__connman_stats_service_register(service) == 0) {
3876 __connman_stats_get(service, FALSE,
3877 &service->stats.data);
3878 __connman_stats_get(service, TRUE,
3879 &service->stats_roaming.data);
3882 if (service->ipconfig_ipv4)
3883 __connman_ipconfig_enable(service->ipconfig_ipv4);
3884 if (service->ipconfig_ipv6)
3885 __connman_ipconfig_enable(service->ipconfig_ipv6);
3887 err = __connman_network_connect(service->network);
3888 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3889 service->provider != NULL)
3890 err = __connman_provider_connect(service->provider);
3895 if (err != -EINPROGRESS) {
3896 __connman_ipconfig_disable(service->ipconfig_ipv4);
3897 __connman_ipconfig_disable(service->ipconfig_ipv6);
3898 __connman_stats_service_unregister(service);
3906 int __connman_service_connect(struct connman_service *service)
3910 DBG("service %p state %s", service, state2string(service->state));
3912 if (is_connected(service) == TRUE)
3915 if (is_connecting(service) == TRUE)
3918 switch (service->type) {
3919 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3920 case CONNMAN_SERVICE_TYPE_SYSTEM:
3921 case CONNMAN_SERVICE_TYPE_GPS:
3922 case CONNMAN_SERVICE_TYPE_GADGET:
3925 err = service_connect(service);
3931 if (err == -EINPROGRESS) {
3932 if (service->timeout == 0)
3933 service->timeout = g_timeout_add_seconds(
3934 CONNECT_TIMEOUT, connect_timeout, service);
3936 return -EINPROGRESS;
3942 if (service->userconnect == TRUE)
3943 reply_pending(service, err);
3945 __connman_service_ipconfig_indicate_state(service,
3946 CONNMAN_SERVICE_STATE_FAILURE,
3947 CONNMAN_IPCONFIG_TYPE_IPV4);
3948 __connman_service_ipconfig_indicate_state(service,
3949 CONNMAN_SERVICE_STATE_FAILURE,
3950 CONNMAN_IPCONFIG_TYPE_IPV6);
3955 int __connman_service_disconnect(struct connman_service *service)
3959 DBG("service %p", service);
3961 if (service->network != NULL) {
3962 err = __connman_network_disconnect(service->network);
3963 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3964 service->provider != NULL)
3965 err = __connman_provider_disconnect(service->provider);
3969 if (err < 0 && err != -EINPROGRESS)
3972 __connman_6to4_remove(service->ipconfig_ipv4);
3974 if (service->ipconfig_ipv4)
3975 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3978 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3981 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3982 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3984 __connman_ipconfig_disable(service->ipconfig_ipv4);
3985 __connman_ipconfig_disable(service->ipconfig_ipv6);
3987 __connman_stats_service_unregister(service);
3992 int __connman_service_disconnect_all(void)
3994 GSequenceIter *iter;
3998 iter = g_sequence_get_begin_iter(service_list);
4000 while (g_sequence_iter_is_end(iter) == FALSE) {
4001 struct connman_service *service = g_sequence_get(iter);
4003 service->ignore = TRUE;
4005 set_reconnect_state(service, FALSE);
4007 __connman_service_disconnect(service);
4009 iter = g_sequence_iter_next(iter);
4017 * __connman_service_lookup:
4018 * @pattern: search pattern
4019 * @path: return object path
4021 * Look up a service path from a search pattern
4023 int __connman_service_lookup(const char *pattern, const char **path)
4025 GHashTableIter iter;
4026 gpointer key, value;
4028 g_hash_table_iter_init(&iter, service_hash);
4030 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4031 GSequenceIter *iter = value;
4032 struct connman_service *service = g_sequence_get(iter);
4034 if (g_strcmp0(service->identifier, pattern) == 0 ||
4035 g_strcmp0(service->name, pattern) == 0) {
4036 *path = (const char *) service->path;
4045 * lookup_by_identifier:
4046 * @identifier: service identifier
4048 * Look up a service by identifier (reference count will not be increased)
4050 static struct connman_service *lookup_by_identifier(const char *identifier)
4052 GSequenceIter *iter;
4054 iter = g_hash_table_lookup(service_hash, identifier);
4056 return g_sequence_get(iter);
4061 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4062 const char *ssid, const char *mode, const char *security)
4064 struct connman_network *network;
4067 unsigned int i, ssid_len;
4069 ssid_len = strlen(ssid);
4073 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
4074 if (network == NULL)
4077 connman_network_set_blob(network, "WiFi.SSID",
4078 (unsigned char *) ssid, ssid_len);
4080 connman_network_set_string(network, "WiFi.Mode", mode);
4081 connman_network_set_string(network, "WiFi.Security", security);
4083 name = g_try_malloc0(ssid_len + 1);
4085 connman_network_unref(network);
4089 for (i = 0; i < ssid_len; i++) {
4090 if (g_ascii_isprint(ssid[i]))
4096 connman_network_set_name(network, name);
4100 index = connman_device_get_index(device);
4101 connman_network_set_index(network, index);
4103 if (connman_device_add_network(device, network) < 0) {
4104 connman_network_unref(network);
4108 connman_network_set_available(network, TRUE);
4113 int __connman_service_create_and_connect(DBusMessage *msg)
4115 struct connman_service *service;
4116 struct connman_network *network;
4117 struct connman_device *device;
4118 DBusMessageIter iter, array;
4119 const char *mode = "managed", *security = "none", *group_security;
4120 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4121 unsigned int ssid_len = 0;
4126 dbus_message_iter_init(msg, &iter);
4127 dbus_message_iter_recurse(&iter, &array);
4129 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4130 DBusMessageIter entry, value;
4133 dbus_message_iter_recurse(&array, &entry);
4134 dbus_message_iter_get_basic(&entry, &key);
4136 dbus_message_iter_next(&entry);
4137 dbus_message_iter_recurse(&entry, &value);
4139 switch (dbus_message_iter_get_arg_type(&value)) {
4140 case DBUS_TYPE_STRING:
4141 if (g_str_equal(key, "Type") == TRUE)
4142 dbus_message_iter_get_basic(&value, &type);
4143 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4144 g_str_equal(key, "Mode") == TRUE)
4145 dbus_message_iter_get_basic(&value, &mode);
4146 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4147 g_str_equal(key, "Security") == TRUE)
4148 dbus_message_iter_get_basic(&value, &security);
4149 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4150 g_str_equal(key, "Passphrase") == TRUE)
4151 dbus_message_iter_get_basic(&value, &passphrase);
4152 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4153 g_str_equal(key, "SSID") == TRUE)
4154 dbus_message_iter_get_basic(&value, &ssid);
4157 dbus_message_iter_next(&array);
4163 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4169 ssid_len = strlen(ssid);
4173 if (g_strcmp0(security, "none") != 0 &&
4174 g_strcmp0(security, "wep") != 0 &&
4175 g_strcmp0(security, "psk") != 0 &&
4176 g_strcmp0(security, "wpa") != 0 &&
4177 g_strcmp0(security, "rsn") != 0 &&
4178 g_strcmp0(security, "ieee8021x") != 0)
4181 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4185 ident = connman_device_get_ident(device);
4190 if (!g_strcmp0(security, "wpa") ||
4191 !g_strcmp0(security, "rsn"))
4192 group_security = "psk";
4194 group_security = security;
4196 group = connman_wifi_build_group_name((unsigned char *) ssid,
4197 ssid_len, mode, group_security);
4201 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4203 service = lookup_by_identifier(name);
4205 if (service != NULL)
4208 network = create_hidden_wifi(device, ssid, mode, security);
4209 if (network != NULL)
4210 connman_network_set_group(network, group);
4212 service = lookup_by_identifier(name);
4218 if (service == NULL) {
4223 service->network_created = TRUE;
4225 if (is_connected(service) == TRUE) {
4230 if (is_connecting(service) == TRUE) {
4235 set_reconnect_state(service, FALSE);
4237 __connman_device_disconnect(device);
4239 if (passphrase != NULL) {
4240 g_free(service->passphrase);
4241 service->passphrase = g_strdup(passphrase);
4244 service->userconnect = TRUE;
4246 err = __connman_service_connect(service);
4247 if (err < 0 && err != -EINPROGRESS)
4250 service->pending = dbus_message_ref(msg);
4255 if (service != NULL && service->network_created == TRUE) {
4256 struct connman_network *network = service->network;
4258 if (network != NULL) {
4259 connman_network_set_available(network, FALSE);
4260 __connman_device_cleanup_networks(device);
4262 __connman_service_put(service);
4268 static void provision_changed(gpointer value, gpointer user_data)
4270 struct connman_service *service = value;
4271 char *path = user_data;
4273 __connman_config_provision_service_ident(service, path);
4276 void __connman_service_provision_changed(const char *ident)
4278 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4281 int __connman_service_provision(DBusMessage *msg)
4283 GKeyFile *keyfile = NULL;
4284 const char *config_str = NULL;
4285 char *group = NULL, *ident = NULL;
4287 struct connman_service *service;
4291 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4294 if (config_str == NULL || strlen(config_str) == 0)
4297 keyfile = g_key_file_new();
4299 /* populate GKeyFile with config_str */
4300 if (g_key_file_load_from_data(keyfile, config_str,
4301 strlen(config_str), 0, NULL) == FALSE) {
4307 * read only one group of settings (only one service supported, no
4310 group = g_key_file_get_start_group(keyfile);
4312 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4317 err = __connman_config_load_service(keyfile, group, TRUE);
4321 ident = group + strlen("service_");
4323 /* trigger service provisioning if service exists */
4324 service = lookup_by_identifier(ident);
4325 if (service != NULL)
4326 __connman_config_provision_service(service);
4328 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4334 if (keyfile != NULL)
4335 g_key_file_free(keyfile);
4341 * __connman_service_get:
4342 * @identifier: service identifier
4344 * Look up a service by identifier or create a new one if not found
4346 static struct connman_service *service_get(const char *identifier)
4348 struct connman_service *service;
4349 GSequenceIter *iter;
4351 iter = g_hash_table_lookup(service_hash, identifier);
4353 service = g_sequence_get(iter);
4354 if (service != NULL)
4355 connman_service_ref(service);
4359 service = connman_service_create();
4360 if (service == NULL)
4363 DBG("service %p", service);
4365 service->identifier = g_strdup(identifier);
4367 service->profile = g_strdup(__connman_profile_active_ident());
4369 iter = g_sequence_insert_sorted(service_list, service,
4370 service_compare, NULL);
4372 g_hash_table_insert(service_hash, service->identifier, iter);
4377 static int service_register(struct connman_service *service)
4379 const char *path = __connman_profile_active_path();
4380 GSequenceIter *iter;
4382 DBG("service %p", service);
4384 if (service->path != NULL)
4387 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4389 DBG("path %s", service->path);
4391 __connman_config_provision_service(service);
4393 __connman_storage_load_service(service);
4395 g_dbus_register_interface(connection, service->path,
4396 CONNMAN_SERVICE_INTERFACE,
4397 service_methods, service_signals,
4398 NULL, service, NULL);
4400 iter = g_hash_table_lookup(service_hash, service->identifier);
4402 g_sequence_sort_changed(iter, service_compare, NULL);
4404 __connman_profile_changed(TRUE);
4409 static void service_up(struct connman_ipconfig *ipconfig)
4411 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4413 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4415 link_changed(service);
4417 service->stats.valid = FALSE;
4418 service->stats_roaming.valid = FALSE;
4421 static void service_down(struct connman_ipconfig *ipconfig)
4423 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4426 static void service_lower_up(struct connman_ipconfig *ipconfig)
4428 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4430 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4432 stats_start(service);
4435 static void service_lower_down(struct connman_ipconfig *ipconfig)
4437 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4439 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4441 stats_stop(service);
4442 __connman_storage_save_service(service);
4445 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4447 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4448 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4449 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4451 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4453 type = __connman_ipconfig_get_config_type(ipconfig);
4454 method = __connman_ipconfig_get_method(ipconfig);
4456 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4459 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4460 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4461 __connman_service_ipconfig_indicate_state(service,
4462 CONNMAN_SERVICE_STATE_READY,
4463 CONNMAN_IPCONFIG_TYPE_IPV6);
4465 settings_changed(service, ipconfig);
4468 static void service_ip_release(struct connman_ipconfig *ipconfig)
4470 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4471 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4472 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4474 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4476 type = __connman_ipconfig_get_config_type(ipconfig);
4477 method = __connman_ipconfig_get_method(ipconfig);
4479 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4482 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4483 method == CONNMAN_IPCONFIG_METHOD_OFF)
4484 __connman_service_ipconfig_indicate_state(service,
4485 CONNMAN_SERVICE_STATE_DISCONNECT,
4486 CONNMAN_IPCONFIG_TYPE_IPV6);
4488 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4489 method == CONNMAN_IPCONFIG_METHOD_OFF)
4490 __connman_service_ipconfig_indicate_state(service,
4491 CONNMAN_SERVICE_STATE_DISCONNECT,
4492 CONNMAN_IPCONFIG_TYPE_IPV4);
4494 settings_changed(service, ipconfig);
4497 static const struct connman_ipconfig_ops service_ops = {
4499 .down = service_down,
4500 .lower_up = service_lower_up,
4501 .lower_down = service_lower_down,
4502 .ip_bound = service_ip_bound,
4503 .ip_release = service_ip_release,
4506 static void setup_ip4config(struct connman_service *service, int index,
4507 enum connman_ipconfig_method method)
4512 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4513 CONNMAN_IPCONFIG_TYPE_IPV4);
4514 if (service->ipconfig_ipv4 == NULL)
4517 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4519 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4521 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4524 static void setup_ip6config(struct connman_service *service, int index)
4529 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4530 CONNMAN_IPCONFIG_TYPE_IPV6);
4531 if (service->ipconfig_ipv6 == NULL)
4534 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4536 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4539 void __connman_service_create_ip4config(struct connman_service *service,
4542 const char *ident = service->profile;
4545 DBG("ipv4 %p", service->ipconfig_ipv4);
4547 if (service->ipconfig_ipv4 != NULL)
4550 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4555 keyfile = __connman_storage_open_profile(ident);
4556 if (keyfile == NULL)
4559 if (service->ipconfig_ipv4)
4560 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4561 service->identifier, "IPv4.");
4562 g_key_file_free(keyfile);
4565 void __connman_service_create_ip6config(struct connman_service *service,
4568 const char *ident = service->profile;
4571 DBG("ipv6 %p", service->ipconfig_ipv6);
4573 if (service->ipconfig_ipv6 != NULL)
4576 setup_ip6config(service, index);
4581 keyfile = __connman_storage_open_profile(ident);
4582 if (keyfile == NULL)
4585 if (service->ipconfig_ipv6 != NULL)
4586 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4587 service->identifier, "IPv6.");
4589 g_key_file_free(keyfile);
4593 * __connman_service_lookup_from_network:
4594 * @network: network structure
4596 * Look up a service by network (reference count will not be increased)
4598 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4600 struct connman_service *service;
4601 const char *ident, *group;
4604 DBG("network %p", network);
4606 ident = __connman_network_get_ident(network);
4610 group = connman_network_get_group(network);
4614 name = g_strdup_printf("%s_%s_%s",
4615 __connman_network_get_type(network), ident, group);
4616 service = lookup_by_identifier(name);
4622 struct connman_service *__connman_service_lookup_from_index(int index)
4624 struct connman_service *service;
4625 GSequenceIter *iter;
4627 iter = g_sequence_get_begin_iter(service_list);
4629 while (g_sequence_iter_is_end(iter) == FALSE) {
4630 service = g_sequence_get(iter);
4632 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4636 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4640 iter = g_sequence_iter_next(iter);
4646 const char *__connman_service_get_ident(struct connman_service *service)
4648 return service->identifier;
4651 const char *__connman_service_get_path(struct connman_service *service)
4653 return service->path;
4656 unsigned int __connman_service_get_order(struct connman_service *service)
4658 GSequenceIter *iter;
4660 if (service == NULL)
4663 if (service->favorite == FALSE) {
4668 iter = g_hash_table_lookup(service_hash, service->identifier);
4670 if (g_sequence_iter_get_position(iter) == 0)
4672 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4673 service->order = 10;
4679 return service->order;
4682 static enum connman_service_type convert_network_type(struct connman_network *network)
4684 enum connman_network_type type = connman_network_get_type(network);
4687 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4688 case CONNMAN_NETWORK_TYPE_VENDOR:
4690 case CONNMAN_NETWORK_TYPE_ETHERNET:
4691 return CONNMAN_SERVICE_TYPE_ETHERNET;
4692 case CONNMAN_NETWORK_TYPE_WIFI:
4693 return CONNMAN_SERVICE_TYPE_WIFI;
4694 case CONNMAN_NETWORK_TYPE_WIMAX:
4695 return CONNMAN_SERVICE_TYPE_WIMAX;
4696 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4697 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4698 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4699 case CONNMAN_NETWORK_TYPE_CELLULAR:
4700 return CONNMAN_SERVICE_TYPE_CELLULAR;
4703 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4706 static enum connman_service_security convert_wifi_security(const char *security)
4708 if (security == NULL)
4709 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4710 else if (g_str_equal(security, "none") == TRUE)
4711 return CONNMAN_SERVICE_SECURITY_NONE;
4712 else if (g_str_equal(security, "wep") == TRUE)
4713 return CONNMAN_SERVICE_SECURITY_WEP;
4714 else if (g_str_equal(security, "psk") == TRUE)
4715 return CONNMAN_SERVICE_SECURITY_PSK;
4716 else if (g_str_equal(security, "ieee8021x") == TRUE)
4717 return CONNMAN_SERVICE_SECURITY_8021X;
4718 else if (g_str_equal(security, "wpa") == TRUE)
4719 return CONNMAN_SERVICE_SECURITY_WPA;
4720 else if (g_str_equal(security, "rsn") == TRUE)
4721 return CONNMAN_SERVICE_SECURITY_RSN;
4723 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4726 static void update_from_network(struct connman_service *service,
4727 struct connman_network *network)
4729 connman_uint8_t strength = service->strength;
4730 GSequenceIter *iter;
4733 DBG("service %p network %p", service, network);
4735 if (is_connected(service) == TRUE)
4738 if (is_connecting(service) == TRUE)
4741 str = connman_network_get_string(network, "Name");
4743 g_free(service->name);
4744 service->name = g_strdup(str);
4745 service->hidden = FALSE;
4747 g_free(service->name);
4748 service->name = NULL;
4749 service->hidden = TRUE;
4752 service->strength = connman_network_get_strength(network);
4753 service->roaming = connman_network_get_bool(network, "Roaming");
4755 if (service->strength == 0) {
4757 * Filter out 0-values; it's unclear what they mean
4758 * and they cause anomalous sorting of the priority list.
4760 service->strength = strength;
4763 str = connman_network_get_string(network, "WiFi.Security");
4764 service->security = convert_wifi_security(str);
4766 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4767 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4769 if (service->strength > strength && service->network != NULL) {
4770 service->network = network;
4772 strength_changed(service);
4775 if (service->network == NULL)
4776 service->network = network;
4778 iter = g_hash_table_lookup(service_hash, service->identifier);
4780 g_sequence_sort_changed(iter, service_compare, NULL);
4784 * __connman_service_create_from_network:
4785 * @network: network structure
4787 * Look up service by network and if not found, create one
4789 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4791 struct connman_service *service;
4792 struct connman_device *device;
4793 const char *ident, *group;
4797 DBG("network %p", network);
4799 ident = __connman_network_get_ident(network);
4803 group = connman_network_get_group(network);
4807 name = g_strdup_printf("%s_%s_%s",
4808 __connman_network_get_type(network), ident, group);
4809 service = service_get(name);
4812 if (service == NULL)
4815 if (__connman_network_get_weakness(network) == TRUE)
4818 if (service->path != NULL) {
4819 update_from_network(service, network);
4820 __connman_profile_changed(TRUE);
4824 service->type = convert_network_type(network);
4826 switch (service->type) {
4827 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4828 case CONNMAN_SERVICE_TYPE_SYSTEM:
4829 case CONNMAN_SERVICE_TYPE_ETHERNET:
4830 case CONNMAN_SERVICE_TYPE_WIMAX:
4831 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4832 case CONNMAN_SERVICE_TYPE_GPS:
4833 case CONNMAN_SERVICE_TYPE_VPN:
4834 case CONNMAN_SERVICE_TYPE_GADGET:
4835 service->autoconnect = FALSE;
4837 case CONNMAN_SERVICE_TYPE_WIFI:
4838 case CONNMAN_SERVICE_TYPE_CELLULAR:
4839 service->autoconnect = TRUE;
4843 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4844 service->state = combine_state(service->state_ipv4, service->state_ipv6);
4846 update_from_network(service, network);
4848 index = connman_network_get_index(network);
4850 if (service->ipconfig_ipv4 == NULL)
4851 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4853 if (service->ipconfig_ipv6 == NULL)
4854 setup_ip6config(service, index);
4856 service_register(service);
4858 if (service->favorite == TRUE) {
4859 device = connman_network_get_device(service->network);
4860 if (device && __connman_device_scanning(device) == FALSE)
4861 __connman_service_auto_connect();
4864 __connman_notifier_service_add(service);
4869 void __connman_service_update_from_network(struct connman_network *network)
4871 struct connman_service *service;
4872 connman_uint8_t strength;
4873 connman_bool_t roaming;
4874 GSequenceIter *iter;
4876 connman_bool_t stats_enable;
4878 DBG("network %p", network);
4880 service = __connman_service_lookup_from_network(network);
4881 if (service == NULL)
4884 if (service->network == NULL)
4887 name = connman_network_get_string(service->network, "Name");
4888 if (g_strcmp0(service->name, name) != 0) {
4889 g_free(service->name);
4890 service->name = g_strdup(name);
4891 connman_dbus_property_changed_basic(service->path,
4892 CONNMAN_SERVICE_INTERFACE, "Name",
4893 DBUS_TYPE_STRING, &service->name);
4896 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4897 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4899 strength = connman_network_get_strength(service->network);
4900 if (strength == service->strength)
4903 service->strength = strength;
4905 strength_changed(service);
4908 roaming = connman_network_get_bool(service->network, "Roaming");
4909 if (roaming == service->roaming)
4912 stats_enable = stats_enabled(service);
4913 if (stats_enable == TRUE)
4914 stats_stop(service);
4916 service->roaming = roaming;
4918 if (stats_enable == TRUE)
4919 stats_start(service);
4921 roaming_changed(service);
4923 iter = g_hash_table_lookup(service_hash, service->identifier);
4925 g_sequence_sort_changed(iter, service_compare, NULL);
4928 void __connman_service_remove_from_network(struct connman_network *network)
4930 struct connman_service *service;
4932 DBG("network %p", network);
4934 service = __connman_service_lookup_from_network(network);
4935 if (service == NULL)
4938 __connman_service_put(service);
4941 void __connman_service_reset_from_networks(struct connman_service *service,
4942 GHashTable *networks)
4944 struct connman_network *network;
4945 GHashTableIter iter;
4946 gpointer key, value;
4948 DBG("service %p", service);
4952 g_hash_table_iter_init(&iter, networks);
4954 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4955 struct connman_network *iter_network = value;
4956 uint8_t strength, iter_strength;
4958 if (network == NULL) {
4959 network = iter_network;
4963 strength = connman_network_get_strength(network);
4964 iter_strength = connman_network_get_strength(iter_network);
4966 if (iter_strength > strength)
4967 network = iter_network;
4970 if (network == NULL) {
4971 service->network = NULL;
4975 __connman_service_create_from_network(network);
4979 * __connman_service_create_from_provider:
4980 * @provider: provider structure
4982 * Look up service by provider and if not found, create one
4984 struct connman_service *
4985 __connman_service_create_from_provider(struct connman_provider *provider)
4987 struct connman_service *service;
4988 const char *ident, *str;
4990 int index = connman_provider_get_index(provider);
4992 DBG("provider %p", provider);
4994 ident = __connman_provider_get_ident(provider);
4998 name = g_strdup_printf("vpn_%s", ident);
4999 service = service_get(name);
5002 if (service == NULL)
5005 service->type = CONNMAN_SERVICE_TYPE_VPN;
5006 service->provider = connman_provider_ref(provider);
5007 service->autoconnect = FALSE;
5009 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5011 str = connman_provider_get_string(provider, "Name");
5013 g_free(service->name);
5014 service->name = g_strdup(str);
5015 service->hidden = FALSE;
5017 g_free(service->name);
5018 service->name = NULL;
5019 service->hidden = TRUE;
5022 service->strength = 0;
5024 if (service->ipconfig_ipv4 == NULL)
5025 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5027 if (service->ipconfig_ipv6 == NULL)
5028 setup_ip6config(service, index);
5030 service_register(service);
5032 __connman_notifier_service_add(service);
5037 static int service_load(struct connman_service *service)
5039 const char *ident = service->profile;
5041 GError *error = NULL;
5042 gchar *pathname, *data = NULL;
5045 connman_bool_t autoconnect;
5046 unsigned int ssid_len;
5049 DBG("service %p", service);
5054 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5055 if (pathname == NULL)
5058 keyfile = g_key_file_new();
5060 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
5067 if (g_key_file_load_from_data(keyfile, data, length,
5068 0, NULL) == FALSE) {
5075 switch (service->type) {
5076 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5077 case CONNMAN_SERVICE_TYPE_SYSTEM:
5078 case CONNMAN_SERVICE_TYPE_ETHERNET:
5079 case CONNMAN_SERVICE_TYPE_GPS:
5080 case CONNMAN_SERVICE_TYPE_VPN:
5081 case CONNMAN_SERVICE_TYPE_GADGET:
5083 case CONNMAN_SERVICE_TYPE_WIFI:
5084 if (service->name == NULL) {
5087 name = g_key_file_get_string(keyfile,
5088 service->identifier, "Name", NULL);
5090 g_free(service->name);
5091 service->name = name;
5094 if (service->network != NULL)
5095 connman_network_set_name(service->network,
5099 if (service->network &&
5100 connman_network_get_blob(service->network,
5101 "WiFi.SSID", &ssid_len) == NULL) {
5104 hex_ssid = g_key_file_get_string(keyfile,
5105 service->identifier,
5108 if (hex_ssid != NULL) {
5110 unsigned int i, j = 0, hex;
5111 size_t hex_ssid_len = strlen(hex_ssid);
5113 ssid = g_try_malloc0(hex_ssid_len / 2);
5120 for (i = 0; i < hex_ssid_len; i += 2) {
5121 sscanf(hex_ssid + i, "%02x", &hex);
5125 connman_network_set_blob(service->network,
5126 "WiFi.SSID", ssid, hex_ssid_len / 2);
5133 case CONNMAN_SERVICE_TYPE_WIMAX:
5134 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5135 case CONNMAN_SERVICE_TYPE_CELLULAR:
5136 service->favorite = g_key_file_get_boolean(keyfile,
5137 service->identifier, "Favorite", NULL);
5139 autoconnect = g_key_file_get_boolean(keyfile,
5140 service->identifier, "AutoConnect", &error);
5142 service->autoconnect = autoconnect;
5143 g_clear_error(&error);
5145 str = g_key_file_get_string(keyfile,
5146 service->identifier, "Failure", NULL);
5148 if (service->favorite == FALSE)
5149 service->state_ipv4 = service->state_ipv6 =
5150 CONNMAN_SERVICE_STATE_FAILURE;
5151 service->error = string2error(str);
5156 str = g_key_file_get_string(keyfile,
5157 service->identifier, "Modified", NULL);
5159 g_time_val_from_iso8601(str, &service->modified);
5163 str = g_key_file_get_string(keyfile,
5164 service->identifier, "Passphrase", NULL);
5166 g_free(service->passphrase);
5167 service->passphrase = str;
5170 if (service->ipconfig_ipv4 != NULL)
5171 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5172 service->identifier, "IPv4.");
5174 if (service->ipconfig_ipv6 != NULL)
5175 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5176 service->identifier, "IPv6.");
5178 service->nameservers_config = g_key_file_get_string_list(keyfile,
5179 service->identifier, "Nameservers", &length, NULL);
5180 if (service->nameservers_config != NULL && length == 0) {
5181 g_strfreev(service->nameservers_config);
5182 service->nameservers_config = NULL;
5185 service->domains = g_key_file_get_string_list(keyfile,
5186 service->identifier, "Domains", &length, NULL);
5187 if (service->domains != NULL && length == 0) {
5188 g_strfreev(service->domains);
5189 service->domains = NULL;
5192 str = g_key_file_get_string(keyfile,
5193 service->identifier, "Proxy.Method", NULL);
5195 service->proxy_config = string2proxymethod(str);
5199 service->proxies = g_key_file_get_string_list(keyfile,
5200 service->identifier, "Proxy.Servers", &length, NULL);
5201 if (service->proxies != NULL && length == 0) {
5202 g_strfreev(service->proxies);
5203 service->proxies = NULL;
5206 service->excludes = g_key_file_get_string_list(keyfile,
5207 service->identifier, "Proxy.Excludes", &length, NULL);
5208 if (service->excludes != NULL && length == 0) {
5209 g_strfreev(service->excludes);
5210 service->excludes = NULL;
5213 str = g_key_file_get_string(keyfile,
5214 service->identifier, "Proxy.URL", NULL);
5216 g_free(service->pac);
5221 g_key_file_free(keyfile);
5226 static int service_save(struct connman_service *service)
5228 const char *ident = service->profile;
5230 gchar *pathname, *data = NULL;
5233 const char *cst_str = NULL;
5236 DBG("service %p", service);
5241 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5242 if (pathname == NULL)
5245 keyfile = g_key_file_new();
5247 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5251 if (g_key_file_load_from_data(keyfile, data, length,
5259 if (service->name != NULL)
5260 g_key_file_set_string(keyfile, service->identifier,
5261 "Name", service->name);
5263 switch (service->type) {
5264 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5265 case CONNMAN_SERVICE_TYPE_SYSTEM:
5266 case CONNMAN_SERVICE_TYPE_ETHERNET:
5267 case CONNMAN_SERVICE_TYPE_GPS:
5268 case CONNMAN_SERVICE_TYPE_VPN:
5269 case CONNMAN_SERVICE_TYPE_GADGET:
5271 case CONNMAN_SERVICE_TYPE_WIFI:
5272 if (service->network) {
5273 const unsigned char *ssid;
5274 unsigned int ssid_len = 0;
5276 ssid = connman_network_get_blob(service->network,
5277 "WiFi.SSID", &ssid_len);
5279 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5280 char *identifier = service->identifier;
5284 str = g_string_sized_new(ssid_len * 2);
5290 for (i = 0; i < ssid_len; i++)
5291 g_string_append_printf(str,
5294 g_key_file_set_string(keyfile, identifier,
5297 g_string_free(str, TRUE);
5302 case CONNMAN_SERVICE_TYPE_WIMAX:
5303 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5304 case CONNMAN_SERVICE_TYPE_CELLULAR:
5305 g_key_file_set_boolean(keyfile, service->identifier,
5306 "Favorite", service->favorite);
5308 if (service->favorite == TRUE)
5309 g_key_file_set_boolean(keyfile, service->identifier,
5310 "AutoConnect", service->autoconnect);
5312 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5313 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5314 const char *failure = error2string(service->error);
5315 if (failure != NULL)
5316 g_key_file_set_string(keyfile,
5317 service->identifier,
5318 "Failure", failure);
5320 g_key_file_remove_key(keyfile, service->identifier,
5326 str = g_time_val_to_iso8601(&service->modified);
5328 g_key_file_set_string(keyfile, service->identifier,
5333 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5334 g_key_file_set_string(keyfile, service->identifier,
5335 "Passphrase", service->passphrase);
5337 g_key_file_remove_key(keyfile, service->identifier,
5338 "Passphrase", NULL);
5340 if (service->ipconfig_ipv4 != NULL)
5341 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5342 service->identifier, "IPv4.");
5344 if (service->ipconfig_ipv6 != NULL)
5345 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5346 service->identifier, "IPv6.");
5348 if (service->nameservers_config != NULL) {
5349 guint len = g_strv_length(service->nameservers_config);
5351 g_key_file_set_string_list(keyfile, service->identifier,
5353 (const gchar **) service->nameservers_config, len);
5355 g_key_file_remove_key(keyfile, service->identifier,
5356 "Nameservers", NULL);
5358 if (service->domains != NULL) {
5359 guint len = g_strv_length(service->domains);
5361 g_key_file_set_string_list(keyfile, service->identifier,
5363 (const gchar **) service->domains, len);
5365 g_key_file_remove_key(keyfile, service->identifier,
5368 cst_str = proxymethod2string(service->proxy_config);
5369 if (cst_str != NULL)
5370 g_key_file_set_string(keyfile, service->identifier,
5371 "Proxy.Method", cst_str);
5373 if (service->proxies != NULL) {
5374 guint len = g_strv_length(service->proxies);
5376 g_key_file_set_string_list(keyfile, service->identifier,
5378 (const gchar **) service->proxies, len);
5380 g_key_file_remove_key(keyfile, service->identifier,
5381 "Proxy.Servers", NULL);
5383 if (service->excludes != NULL) {
5384 guint len = g_strv_length(service->excludes);
5386 g_key_file_set_string_list(keyfile, service->identifier,
5388 (const gchar **) service->excludes, len);
5390 g_key_file_remove_key(keyfile, service->identifier,
5391 "Proxy.Excludes", NULL);
5393 if (service->pac != NULL && strlen(service->pac) > 0)
5394 g_key_file_set_string(keyfile, service->identifier,
5395 "Proxy.URL", service->pac);
5397 g_key_file_remove_key(keyfile, service->identifier,
5400 data = g_key_file_to_data(keyfile, &length, NULL);
5402 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5403 connman_error("Failed to store service information");
5408 g_key_file_free(keyfile);
5415 static struct connman_storage service_storage = {
5417 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5418 .service_load = service_load,
5419 .service_save = service_save,
5422 int __connman_service_init(void)
5426 connection = connman_dbus_get_connection();
5428 if (connman_storage_register(&service_storage) < 0)
5429 connman_error("Failed to register service storage");
5431 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5434 service_list = g_sequence_new(service_free);
5439 void __connman_service_cleanup(void)
5445 list = service_list;
5446 service_list = NULL;
5447 g_sequence_free(list);
5449 g_hash_table_destroy(service_hash);
5450 service_hash = NULL;
5452 g_slist_free(counter_list);
5453 counter_list = NULL;
5455 connman_storage_unregister(&service_storage);
5457 dbus_connection_unref(connection);