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_unregister(service->network);
2966 connman_network_unref(service->network);
2969 if (service->provider != NULL)
2970 connman_provider_unref(service->provider);
2972 if (service->ipconfig_ipv4 != NULL) {
2973 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
2974 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
2975 connman_ipconfig_unref(service->ipconfig_ipv4);
2976 service->ipconfig_ipv4 = NULL;
2979 if (service->ipconfig_ipv6 != NULL) {
2980 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
2981 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
2982 connman_ipconfig_unref(service->ipconfig_ipv6);
2983 service->ipconfig_ipv6 = NULL;
2986 if (service->location != NULL)
2987 connman_location_unref(service->location);
2989 g_strfreev(service->nameservers);
2990 g_strfreev(service->nameservers_config);
2991 g_strfreev(service->domains);
2992 g_strfreev(service->proxies);
2993 g_strfreev(service->excludes);
2995 g_free(service->domainname);
2996 g_free(service->pac);
2997 g_free(service->profile);
2998 g_free(service->name);
2999 g_free(service->passphrase);
3000 g_free(service->identifier);
3001 g_free(service->eap);
3002 g_free(service->identity);
3003 g_free(service->ca_cert_file);
3004 g_free(service->client_cert_file);
3005 g_free(service->private_key_file);
3006 g_free(service->private_key_passphrase);
3007 g_free(service->phase2);
3009 if (service->stats.timer != NULL)
3010 g_timer_destroy(service->stats.timer);
3011 if (service->stats_roaming.timer != NULL)
3012 g_timer_destroy(service->stats_roaming.timer);
3018 * __connman_service_put:
3019 * @service: service structure
3021 * Release service if no longer needed
3023 void __connman_service_put(struct connman_service *service)
3025 DBG("service %p", service);
3027 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
3028 GSequenceIter *iter;
3030 iter = g_hash_table_lookup(service_hash, service->identifier);
3032 reply_pending(service, ECONNABORTED);
3034 __connman_service_disconnect(service);
3036 g_sequence_remove(iter);
3038 service_free(service);
3042 static void stats_init(struct connman_service *service)
3045 service->stats.valid = FALSE;
3046 service->stats.enabled = FALSE;
3047 service->stats.timer = g_timer_new();
3050 service->stats_roaming.valid = FALSE;
3051 service->stats_roaming.enabled = FALSE;
3052 service->stats_roaming.timer = g_timer_new();
3055 static void service_initialize(struct connman_service *service)
3057 DBG("service %p", service);
3059 service->refcount = 1;
3060 service->session_usage_count = 0;
3062 service->network_created = FALSE;
3064 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3065 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3067 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3068 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3069 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3071 service->favorite = FALSE;
3072 service->immutable = FALSE;
3073 service->hidden = FALSE;
3075 service->ignore = FALSE;
3077 service->userconnect = FALSE;
3081 stats_init(service);
3083 service->provider = NULL;
3085 service->wps = FALSE;
3089 * connman_service_create:
3091 * Allocate a new service.
3093 * Returns: a newly-allocated #connman_service structure
3095 struct connman_service *connman_service_create(void)
3098 struct connman_stats_counter *counters;
3099 const char *counter;
3101 struct connman_service *service;
3103 service = g_try_new0(struct connman_service, 1);
3104 if (service == NULL)
3107 DBG("service %p", service);
3109 service->counter_table = g_hash_table_new_full(g_str_hash,
3110 g_str_equal, NULL, g_free);
3112 for (list = counter_list; list; list = list->next) {
3113 counter = list->data;
3115 counters = g_try_new0(struct connman_stats_counter, 1);
3116 if (counters == NULL) {
3117 g_hash_table_destroy(service->counter_table);
3122 counters->append_all = TRUE;
3124 g_hash_table_replace(service->counter_table, (gpointer)counter,
3128 service_initialize(service);
3130 service->location = __connman_location_create(service);
3135 struct connman_location *__connman_service_get_location(struct connman_service *service)
3137 return service->location;
3141 * connman_service_ref:
3142 * @service: service structure
3144 * Increase reference counter of service
3146 struct connman_service *connman_service_ref(struct connman_service *service)
3150 g_atomic_int_inc(&service->refcount);
3156 * connman_service_unref:
3157 * @service: service structure
3159 * Decrease reference counter of service
3161 void connman_service_unref(struct connman_service *service)
3163 __connman_service_put(service);
3166 static gint service_compare(gconstpointer a, gconstpointer b,
3169 struct connman_service *service_a = (void *) a;
3170 struct connman_service *service_b = (void *) b;
3171 enum connman_service_state state_a, state_b;
3173 state_a = service_a->state;
3174 state_b = service_b->state;
3176 if (state_a != state_b) {
3177 if (is_connected(service_a) == TRUE)
3179 if (is_connected(service_b) == TRUE)
3182 if (is_connecting(service_a) == TRUE)
3184 if (is_connecting(service_b) == TRUE)
3188 if (service_a->order > service_b->order)
3191 if (service_a->order < service_b->order)
3194 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3197 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3200 if (service_a->type != service_b->type) {
3201 switch (service_a->type) {
3202 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3203 case CONNMAN_SERVICE_TYPE_SYSTEM:
3204 case CONNMAN_SERVICE_TYPE_ETHERNET:
3205 case CONNMAN_SERVICE_TYPE_GPS:
3206 case CONNMAN_SERVICE_TYPE_VPN:
3207 case CONNMAN_SERVICE_TYPE_GADGET:
3209 case CONNMAN_SERVICE_TYPE_WIFI:
3211 case CONNMAN_SERVICE_TYPE_WIMAX:
3212 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3213 case CONNMAN_SERVICE_TYPE_CELLULAR:
3218 return (gint) service_b->strength - (gint) service_a->strength;
3222 * connman_service_get_type:
3223 * @service: service structure
3225 * Get the type of service
3227 enum connman_service_type connman_service_get_type(struct connman_service *service)
3229 if (service == NULL)
3230 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3232 return service->type;
3236 * connman_service_get_interface:
3237 * @service: service structure
3239 * Get network interface of service
3241 char *connman_service_get_interface(struct connman_service *service)
3245 if (service == NULL)
3248 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3249 if (service->ipconfig_ipv4)
3250 index = connman_ipconfig_get_index(
3251 service->ipconfig_ipv4);
3252 else if (service->ipconfig_ipv6)
3253 index = connman_ipconfig_get_index(
3254 service->ipconfig_ipv6);
3258 return connman_inet_ifname(index);
3261 if (service->network == NULL)
3264 index = connman_network_get_index(service->network);
3266 return connman_inet_ifname(index);
3270 * connman_service_get_network:
3271 * @service: service structure
3273 * Get the service network
3275 struct connman_network *
3276 __connman_service_get_network(struct connman_service *service)
3278 if (service == NULL)
3281 return service->network;
3284 struct connman_ipconfig *
3285 __connman_service_get_ip4config(struct connman_service *service)
3287 if (service == NULL)
3290 return service->ipconfig_ipv4;
3293 struct connman_ipconfig *
3294 __connman_service_get_ip6config(struct connman_service *service)
3296 if (service == NULL)
3299 return service->ipconfig_ipv6;
3302 struct connman_ipconfig *
3303 __connman_service_get_ipconfig(struct connman_service *service, int family)
3305 if (family == AF_INET)
3306 return __connman_service_get_ip4config(service);
3307 else if (family == AF_INET6)
3308 return __connman_service_get_ip6config(service);
3314 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3316 if (service == NULL)
3317 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3319 return service->security;
3322 const char *__connman_service_get_phase2(struct connman_service *service)
3324 if (service == NULL)
3327 return service->phase2;
3330 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3332 if (service == NULL)
3335 return service->wps;
3339 * __connman_service_set_favorite:
3340 * @service: service structure
3341 * @favorite: favorite value
3343 * Change the favorite setting of service
3345 int __connman_service_set_favorite(struct connman_service *service,
3346 connman_bool_t favorite)
3348 GSequenceIter *iter;
3350 iter = g_hash_table_lookup(service_hash, service->identifier);
3354 if (service->favorite == favorite)
3357 service->favorite = favorite;
3359 favorite_changed(service);
3361 g_sequence_sort_changed(iter, service_compare, NULL);
3363 __connman_profile_changed(FALSE);
3368 int __connman_service_set_immutable(struct connman_service *service,
3369 connman_bool_t immutable)
3371 service->immutable = immutable;
3373 immutable_changed(service);
3378 void __connman_service_set_string(struct connman_service *service,
3379 const char *key, const char *value)
3381 if (g_str_equal(key, "EAP") == TRUE) {
3382 g_free(service->eap);
3383 service->eap = g_strdup(value);
3384 } else if (g_str_equal(key, "Identity") == TRUE) {
3385 g_free(service->identity);
3386 service->identity = g_strdup(value);
3387 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3388 g_free(service->ca_cert_file);
3389 service->ca_cert_file = g_strdup(value);
3390 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3391 g_free(service->client_cert_file);
3392 service->client_cert_file = g_strdup(value);
3393 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3394 g_free(service->private_key_file);
3395 service->private_key_file = g_strdup(value);
3396 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3397 g_free(service->private_key_passphrase);
3398 service->private_key_passphrase = g_strdup(value);
3399 } else if (g_str_equal(key, "Phase2") == TRUE) {
3400 g_free(service->phase2);
3401 service->phase2 = g_strdup(value);
3402 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3403 g_free(service->passphrase);
3404 service->passphrase = g_strdup(value);
3408 static void service_complete(struct connman_service *service)
3410 reply_pending(service, EIO);
3412 if (service->userconnect == FALSE)
3413 __connman_service_auto_connect();
3415 g_get_current_time(&service->modified);
3416 __connman_storage_save_service(service);
3419 static void report_error_cb(struct connman_service *service,
3420 gboolean retry, void *user_data)
3423 __connman_service_connect(service);
3425 service_complete(service);
3426 __connman_profile_changed(FALSE);
3427 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3431 static int __connman_service_indicate_state(struct connman_service *service)
3433 enum connman_service_state old_state, new_state;
3434 GSequenceIter *iter;
3436 if (service == NULL)
3439 old_state = service->state;
3440 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3442 if (old_state == new_state)
3445 DBG("service %p old %s - new %s/%s => %s",
3447 state2string(old_state),
3448 state2string(service->state_ipv4),
3449 state2string(service->state_ipv6),
3450 state2string(new_state));
3452 service->state = new_state;
3453 state_changed(service);
3455 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3456 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3457 reply_pending(service, ECONNABORTED);
3459 __connman_service_disconnect(service);
3462 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3463 if (__connman_stats_service_register(service) == 0) {
3464 __connman_stats_get(service, FALSE,
3465 &service->stats.data);
3466 __connman_stats_get(service, TRUE,
3467 &service->stats_roaming.data);
3471 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3472 if (service->login_required == TRUE) {
3473 service->login_required = FALSE;
3474 login_changed(service);
3477 connman_timeserver_sync();
3482 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
3483 connman_bool_t reconnect;
3485 reconnect = get_reconnect_state(service);
3486 if (reconnect == TRUE)
3487 __connman_service_auto_connect();
3489 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3492 if (new_state == CONNMAN_SERVICE_STATE_READY) {
3493 enum connman_ipconfig_method method;
3495 set_reconnect_state(service, TRUE);
3497 __connman_service_set_favorite(service, TRUE);
3499 reply_pending(service, 0);
3501 service->userconnect = FALSE;
3503 g_get_current_time(&service->modified);
3504 __connman_storage_save_service(service);
3506 update_nameservers(service);
3507 dns_changed(service);
3508 domain_changed(service);
3510 __connman_notifier_connect(service->type);
3512 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3513 connman_network_get_bool(service->network,
3514 "WiFi.UseWPS") == TRUE) {
3517 pass = connman_network_get_string(service->network,
3520 __connman_service_set_passphrase(service, pass);
3522 connman_network_set_bool(service->network,
3523 "WiFi.UseWPS", FALSE);
3528 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3529 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3530 __connman_ipconfig_disable_ipv6(
3531 service->ipconfig_ipv6);
3533 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3534 struct connman_service *def_service = get_default();
3536 if (__connman_notifier_count_connected() == 0 &&
3537 def_service != NULL &&
3538 def_service->provider != NULL)
3539 __connman_provider_disconnect(def_service->provider);
3541 __connman_location_finish(service);
3545 __connman_wpad_stop(service);
3547 update_nameservers(service);
3548 dns_changed(service);
3549 domain_changed(service);
3551 __connman_notifier_disconnect(service->type);
3554 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
3555 if (service->userconnect == TRUE &&
3556 __connman_agent_report_error(service,
3557 error2string(service->error),
3558 report_error_cb, NULL) == -EIO)
3560 service_complete(service);
3562 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3564 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3566 iter = g_hash_table_lookup(service_hash, service->identifier);
3568 g_sequence_sort_changed(iter, service_compare, NULL);
3570 __connman_profile_changed(FALSE);
3575 int __connman_service_indicate_error(struct connman_service *service,
3576 enum connman_service_error error)
3578 DBG("service %p error %d", service, error);
3580 if (service == NULL)
3583 service->error = error;
3585 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3586 __connman_service_set_passphrase(service, NULL);
3588 return __connman_service_ipconfig_indicate_state(service,
3589 CONNMAN_SERVICE_STATE_FAILURE,
3590 CONNMAN_IPCONFIG_TYPE_IPV4);
3593 int __connman_service_clear_error(struct connman_service *service)
3595 DBG("service %p", service);
3597 if (service == NULL)
3600 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
3603 service->state_ipv4 = service->state_ipv6 =
3604 CONNMAN_SERVICE_STATE_UNKNOWN;
3605 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3607 if (service->favorite == TRUE)
3608 set_reconnect_state(service, TRUE);
3610 __connman_service_ipconfig_indicate_state(service,
3611 CONNMAN_SERVICE_STATE_IDLE,
3612 CONNMAN_IPCONFIG_TYPE_IPV6);
3615 * Toggling the IPv6 state to IDLE could trigger the auto connect
3616 * machinery and consequently the IPv4 state.
3618 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3619 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3622 return __connman_service_ipconfig_indicate_state(service,
3623 CONNMAN_SERVICE_STATE_IDLE,
3624 CONNMAN_IPCONFIG_TYPE_IPV4);
3627 int __connman_service_indicate_default(struct connman_service *service)
3629 DBG("service %p", service);
3633 __connman_location_detect(service);
3638 static void check_proxy_setup(struct connman_service *service)
3641 * We start WPAD if we haven't got a PAC URL from DHCP and
3642 * if our proxy manual configuration is either empty or set
3643 * to AUTO with an empty URL.
3646 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
3649 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3650 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
3651 service->pac != NULL))
3654 if (__connman_wpad_start(service) < 0) {
3655 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3656 __connman_notifier_proxy_changed(service);
3660 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
3661 enum connman_service_state new_state,
3662 enum connman_ipconfig_type type)
3664 struct connman_ipconfig *ipconfig = NULL;
3665 enum connman_service_state old_state;
3667 if (service == NULL)
3670 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3671 old_state = service->state_ipv4;
3672 ipconfig = service->ipconfig_ipv4;
3673 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3674 old_state = service->state_ipv6;
3675 ipconfig = service->ipconfig_ipv6;
3678 if (ipconfig == NULL)
3682 if (old_state == new_state)
3685 DBG("service %p (%s) state %d (%s) type %d (%s)",
3686 service, service ? service->identifier : NULL,
3687 new_state, state2string(new_state),
3688 type, __connman_ipconfig_type2string(type));
3690 switch (new_state) {
3691 case CONNMAN_SERVICE_STATE_UNKNOWN:
3692 case CONNMAN_SERVICE_STATE_IDLE:
3693 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3696 case CONNMAN_SERVICE_STATE_ASSOCIATION:
3698 case CONNMAN_SERVICE_STATE_CONFIGURATION:
3699 __connman_ipconfig_enable(ipconfig);
3701 case CONNMAN_SERVICE_STATE_READY:
3702 update_nameservers(service);
3704 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3705 check_proxy_setup(service);
3707 case CONNMAN_SERVICE_STATE_ONLINE:
3709 case CONNMAN_SERVICE_STATE_DISCONNECT:
3710 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
3713 case CONNMAN_SERVICE_STATE_FAILURE:
3717 /* We keep that state */
3718 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3719 service->state_ipv4 = new_state;
3720 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3721 service->state_ipv6 = new_state;
3723 return __connman_service_indicate_state(service);
3726 int __connman_service_request_login(struct connman_service *service)
3728 DBG("service %p", service);
3730 if (service == NULL)
3733 service->login_required = TRUE;
3734 login_changed(service);
3739 static connman_bool_t prepare_network(struct connman_service *service)
3741 enum connman_network_type type;
3742 unsigned int ssid_len;
3744 type = connman_network_get_type(service->network);
3747 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3748 case CONNMAN_NETWORK_TYPE_VENDOR:
3750 case CONNMAN_NETWORK_TYPE_WIFI:
3751 if (connman_network_get_blob(service->network, "WiFi.SSID",
3755 if (service->passphrase != NULL)
3756 connman_network_set_string(service->network,
3757 "WiFi.Passphrase", service->passphrase);
3759 case CONNMAN_NETWORK_TYPE_ETHERNET:
3760 case CONNMAN_NETWORK_TYPE_WIMAX:
3761 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3762 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3763 case CONNMAN_NETWORK_TYPE_CELLULAR:
3770 static void prepare_8021x(struct connman_service *service)
3772 if (service->eap != NULL)
3773 connman_network_set_string(service->network, "WiFi.EAP",
3776 if (service->identity != NULL)
3777 connman_network_set_string(service->network, "WiFi.Identity",
3780 if (service->ca_cert_file != NULL)
3781 connman_network_set_string(service->network, "WiFi.CACertFile",
3782 service->ca_cert_file);
3784 if (service->client_cert_file != NULL)
3785 connman_network_set_string(service->network,
3786 "WiFi.ClientCertFile",
3787 service->client_cert_file);
3789 if (service->private_key_file != NULL)
3790 connman_network_set_string(service->network,
3791 "WiFi.PrivateKeyFile",
3792 service->private_key_file);
3794 if (service->private_key_passphrase != NULL)
3795 connman_network_set_string(service->network,
3796 "WiFi.PrivateKeyPassphrase",
3797 service->private_key_passphrase);
3799 if (service->phase2 != NULL)
3800 connman_network_set_string(service->network, "WiFi.Phase2",
3804 static int service_connect(struct connman_service *service)
3808 switch (service->type) {
3809 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3810 case CONNMAN_SERVICE_TYPE_SYSTEM:
3811 case CONNMAN_SERVICE_TYPE_GPS:
3812 case CONNMAN_SERVICE_TYPE_GADGET:
3814 case CONNMAN_SERVICE_TYPE_ETHERNET:
3815 case CONNMAN_SERVICE_TYPE_WIMAX:
3816 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3817 case CONNMAN_SERVICE_TYPE_CELLULAR:
3818 case CONNMAN_SERVICE_TYPE_VPN:
3820 case CONNMAN_SERVICE_TYPE_WIFI:
3821 switch (service->security) {
3822 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3823 case CONNMAN_SERVICE_SECURITY_NONE:
3825 case CONNMAN_SERVICE_SECURITY_WEP:
3826 case CONNMAN_SERVICE_SECURITY_PSK:
3827 case CONNMAN_SERVICE_SECURITY_WPA:
3828 case CONNMAN_SERVICE_SECURITY_RSN:
3829 if (service->passphrase == NULL) {
3830 if (service->network == NULL)
3833 if (service->wps == FALSE ||
3834 connman_network_get_bool(
3836 "WiFi.UseWPS") == FALSE)
3840 case CONNMAN_SERVICE_SECURITY_8021X:
3841 if (service->eap == NULL)
3845 * never request credentials if using EAP-TLS
3846 * (EAP-TLS networks need to be fully provisioned)
3848 if (g_str_equal(service->eap, "tls") == TRUE)
3851 if (service->immutable != TRUE)
3859 if (service->network != NULL) {
3860 if (prepare_network(service) == FALSE)
3863 switch (service->security) {
3864 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3865 case CONNMAN_SERVICE_SECURITY_NONE:
3866 case CONNMAN_SERVICE_SECURITY_WEP:
3867 case CONNMAN_SERVICE_SECURITY_PSK:
3868 case CONNMAN_SERVICE_SECURITY_WPA:
3869 case CONNMAN_SERVICE_SECURITY_RSN:
3871 case CONNMAN_SERVICE_SECURITY_8021X:
3872 prepare_8021x(service);
3876 if (__connman_stats_service_register(service) == 0) {
3877 __connman_stats_get(service, FALSE,
3878 &service->stats.data);
3879 __connman_stats_get(service, TRUE,
3880 &service->stats_roaming.data);
3883 if (service->ipconfig_ipv4)
3884 __connman_ipconfig_enable(service->ipconfig_ipv4);
3885 if (service->ipconfig_ipv6)
3886 __connman_ipconfig_enable(service->ipconfig_ipv6);
3888 err = __connman_network_connect(service->network);
3889 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3890 service->provider != NULL)
3891 err = __connman_provider_connect(service->provider);
3896 if (err != -EINPROGRESS) {
3897 __connman_ipconfig_disable(service->ipconfig_ipv4);
3898 __connman_ipconfig_disable(service->ipconfig_ipv6);
3899 __connman_stats_service_unregister(service);
3907 int __connman_service_connect(struct connman_service *service)
3911 DBG("service %p state %s", service, state2string(service->state));
3913 if (is_connected(service) == TRUE)
3916 if (is_connecting(service) == TRUE)
3919 switch (service->type) {
3920 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3921 case CONNMAN_SERVICE_TYPE_SYSTEM:
3922 case CONNMAN_SERVICE_TYPE_GPS:
3923 case CONNMAN_SERVICE_TYPE_GADGET:
3926 err = service_connect(service);
3932 if (err == -EINPROGRESS) {
3933 if (service->timeout == 0)
3934 service->timeout = g_timeout_add_seconds(
3935 CONNECT_TIMEOUT, connect_timeout, service);
3937 return -EINPROGRESS;
3943 if (service->userconnect == TRUE)
3944 reply_pending(service, err);
3946 __connman_service_ipconfig_indicate_state(service,
3947 CONNMAN_SERVICE_STATE_FAILURE,
3948 CONNMAN_IPCONFIG_TYPE_IPV4);
3949 __connman_service_ipconfig_indicate_state(service,
3950 CONNMAN_SERVICE_STATE_FAILURE,
3951 CONNMAN_IPCONFIG_TYPE_IPV6);
3956 int __connman_service_disconnect(struct connman_service *service)
3960 DBG("service %p", service);
3962 if (service->network != NULL) {
3963 err = __connman_network_disconnect(service->network);
3964 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3965 service->provider != NULL)
3966 err = __connman_provider_disconnect(service->provider);
3970 if (err < 0 && err != -EINPROGRESS)
3973 __connman_6to4_remove(service->ipconfig_ipv4);
3975 if (service->ipconfig_ipv4)
3976 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3979 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3982 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3983 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3985 __connman_ipconfig_disable(service->ipconfig_ipv4);
3986 __connman_ipconfig_disable(service->ipconfig_ipv6);
3988 __connman_stats_service_unregister(service);
3993 int __connman_service_disconnect_all(void)
3995 GSequenceIter *iter;
3999 iter = g_sequence_get_begin_iter(service_list);
4001 while (g_sequence_iter_is_end(iter) == FALSE) {
4002 struct connman_service *service = g_sequence_get(iter);
4004 service->ignore = TRUE;
4006 set_reconnect_state(service, FALSE);
4008 __connman_service_disconnect(service);
4010 iter = g_sequence_iter_next(iter);
4018 * __connman_service_lookup:
4019 * @pattern: search pattern
4020 * @path: return object path
4022 * Look up a service path from a search pattern
4024 int __connman_service_lookup(const char *pattern, const char **path)
4026 GHashTableIter iter;
4027 gpointer key, value;
4029 g_hash_table_iter_init(&iter, service_hash);
4031 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4032 GSequenceIter *iter = value;
4033 struct connman_service *service = g_sequence_get(iter);
4035 if (g_strcmp0(service->identifier, pattern) == 0 ||
4036 g_strcmp0(service->name, pattern) == 0) {
4037 *path = (const char *) service->path;
4046 * lookup_by_identifier:
4047 * @identifier: service identifier
4049 * Look up a service by identifier (reference count will not be increased)
4051 static struct connman_service *lookup_by_identifier(const char *identifier)
4053 GSequenceIter *iter;
4055 iter = g_hash_table_lookup(service_hash, identifier);
4057 return g_sequence_get(iter);
4062 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4063 const char *ssid, const char *mode, const char *security)
4065 struct connman_network *network;
4068 unsigned int i, ssid_len;
4070 ssid_len = strlen(ssid);
4074 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
4075 if (network == NULL)
4078 connman_network_register(network);
4080 connman_network_set_blob(network, "WiFi.SSID",
4081 (unsigned char *) ssid, ssid_len);
4083 connman_network_set_string(network, "WiFi.Mode", mode);
4084 connman_network_set_string(network, "WiFi.Security", security);
4086 name = g_try_malloc0(ssid_len + 1);
4088 connman_network_unregister(network);
4089 connman_network_unref(network);
4093 for (i = 0; i < ssid_len; i++) {
4094 if (g_ascii_isprint(ssid[i]))
4100 connman_network_set_name(network, name);
4104 index = connman_device_get_index(device);
4105 connman_network_set_index(network, index);
4107 if (connman_device_add_network(device, network) < 0) {
4108 connman_network_unregister(network);
4109 connman_network_unref(network);
4113 connman_network_set_available(network, TRUE);
4118 int __connman_service_create_and_connect(DBusMessage *msg)
4120 struct connman_service *service;
4121 struct connman_network *network;
4122 struct connman_device *device;
4123 DBusMessageIter iter, array;
4124 const char *mode = "managed", *security = "none", *group_security;
4125 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4126 unsigned int ssid_len = 0;
4131 dbus_message_iter_init(msg, &iter);
4132 dbus_message_iter_recurse(&iter, &array);
4134 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4135 DBusMessageIter entry, value;
4138 dbus_message_iter_recurse(&array, &entry);
4139 dbus_message_iter_get_basic(&entry, &key);
4141 dbus_message_iter_next(&entry);
4142 dbus_message_iter_recurse(&entry, &value);
4144 switch (dbus_message_iter_get_arg_type(&value)) {
4145 case DBUS_TYPE_STRING:
4146 if (g_str_equal(key, "Type") == TRUE)
4147 dbus_message_iter_get_basic(&value, &type);
4148 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4149 g_str_equal(key, "Mode") == TRUE)
4150 dbus_message_iter_get_basic(&value, &mode);
4151 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4152 g_str_equal(key, "Security") == TRUE)
4153 dbus_message_iter_get_basic(&value, &security);
4154 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4155 g_str_equal(key, "Passphrase") == TRUE)
4156 dbus_message_iter_get_basic(&value, &passphrase);
4157 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4158 g_str_equal(key, "SSID") == TRUE)
4159 dbus_message_iter_get_basic(&value, &ssid);
4162 dbus_message_iter_next(&array);
4168 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4174 ssid_len = strlen(ssid);
4178 if (g_strcmp0(security, "none") != 0 &&
4179 g_strcmp0(security, "wep") != 0 &&
4180 g_strcmp0(security, "psk") != 0 &&
4181 g_strcmp0(security, "wpa") != 0 &&
4182 g_strcmp0(security, "rsn") != 0 &&
4183 g_strcmp0(security, "ieee8021x") != 0)
4186 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4190 ident = connman_device_get_ident(device);
4195 if (!g_strcmp0(security, "wpa") ||
4196 !g_strcmp0(security, "rsn"))
4197 group_security = "psk";
4199 group_security = security;
4201 group = connman_wifi_build_group_name((unsigned char *) ssid,
4202 ssid_len, mode, group_security);
4206 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4208 service = lookup_by_identifier(name);
4210 if (service != NULL)
4213 network = create_hidden_wifi(device, ssid, mode, security);
4214 if (network != NULL) {
4215 connman_network_set_group(network, group);
4216 service->network_created = TRUE;
4219 service = lookup_by_identifier(name);
4225 if (service == NULL) {
4230 if (is_connected(service) == TRUE) {
4235 if (is_connecting(service) == TRUE) {
4240 set_reconnect_state(service, FALSE);
4242 __connman_device_disconnect(device);
4244 if (passphrase != NULL) {
4245 g_free(service->passphrase);
4246 service->passphrase = g_strdup(passphrase);
4249 service->userconnect = TRUE;
4251 err = __connman_service_connect(service);
4252 if (err < 0 && err != -EINPROGRESS)
4255 service->pending = dbus_message_ref(msg);
4260 if (service != NULL && service->network_created == TRUE) {
4261 struct connman_network *network = service->network;
4263 if (network != NULL) {
4264 connman_network_set_available(network, FALSE);
4265 __connman_device_cleanup_networks(device);
4267 __connman_service_put(service);
4273 static void provision_changed(gpointer value, gpointer user_data)
4275 struct connman_service *service = value;
4276 char *path = user_data;
4278 __connman_config_provision_service_ident(service, path);
4281 void __connman_service_provision_changed(const char *ident)
4283 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4286 int __connman_service_provision(DBusMessage *msg)
4288 GKeyFile *keyfile = NULL;
4289 const char *config_str = NULL;
4290 char *group = NULL, *ident = NULL;
4292 struct connman_service *service;
4296 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4299 if (config_str == NULL || strlen(config_str) == 0)
4302 keyfile = g_key_file_new();
4304 /* populate GKeyFile with config_str */
4305 if (g_key_file_load_from_data(keyfile, config_str,
4306 strlen(config_str), 0, NULL) == FALSE) {
4312 * read only one group of settings (only one service supported, no
4315 group = g_key_file_get_start_group(keyfile);
4317 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4322 err = __connman_config_load_service(keyfile, group, TRUE);
4326 ident = group + strlen("service_");
4328 /* trigger service provisioning if service exists */
4329 service = lookup_by_identifier(ident);
4330 if (service != NULL)
4331 __connman_config_provision_service(service);
4333 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4339 if (keyfile != NULL)
4340 g_key_file_free(keyfile);
4346 * __connman_service_get:
4347 * @identifier: service identifier
4349 * Look up a service by identifier or create a new one if not found
4351 static struct connman_service *service_get(const char *identifier)
4353 struct connman_service *service;
4354 GSequenceIter *iter;
4356 iter = g_hash_table_lookup(service_hash, identifier);
4358 service = g_sequence_get(iter);
4359 if (service != NULL)
4360 connman_service_ref(service);
4364 service = connman_service_create();
4365 if (service == NULL)
4368 DBG("service %p", service);
4370 service->identifier = g_strdup(identifier);
4372 service->profile = g_strdup(__connman_profile_active_ident());
4374 iter = g_sequence_insert_sorted(service_list, service,
4375 service_compare, NULL);
4377 g_hash_table_insert(service_hash, service->identifier, iter);
4382 static int service_register(struct connman_service *service)
4384 const char *path = __connman_profile_active_path();
4385 GSequenceIter *iter;
4387 DBG("service %p", service);
4389 if (service->path != NULL)
4392 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4394 DBG("path %s", service->path);
4396 __connman_config_provision_service(service);
4398 __connman_storage_load_service(service);
4400 g_dbus_register_interface(connection, service->path,
4401 CONNMAN_SERVICE_INTERFACE,
4402 service_methods, service_signals,
4403 NULL, service, NULL);
4405 iter = g_hash_table_lookup(service_hash, service->identifier);
4407 g_sequence_sort_changed(iter, service_compare, NULL);
4409 __connman_profile_changed(TRUE);
4414 static void service_up(struct connman_ipconfig *ipconfig)
4416 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4418 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4420 link_changed(service);
4422 service->stats.valid = FALSE;
4423 service->stats_roaming.valid = FALSE;
4426 static void service_down(struct connman_ipconfig *ipconfig)
4428 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4431 static void service_lower_up(struct connman_ipconfig *ipconfig)
4433 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4435 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4437 stats_start(service);
4440 static void service_lower_down(struct connman_ipconfig *ipconfig)
4442 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4444 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4446 stats_stop(service);
4447 __connman_storage_save_service(service);
4450 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4452 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4453 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4454 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4456 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4458 type = __connman_ipconfig_get_config_type(ipconfig);
4459 method = __connman_ipconfig_get_method(ipconfig);
4461 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4464 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4465 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4466 __connman_service_ipconfig_indicate_state(service,
4467 CONNMAN_SERVICE_STATE_READY,
4468 CONNMAN_IPCONFIG_TYPE_IPV6);
4470 settings_changed(service, ipconfig);
4473 static void service_ip_release(struct connman_ipconfig *ipconfig)
4475 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4476 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4477 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4479 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4481 type = __connman_ipconfig_get_config_type(ipconfig);
4482 method = __connman_ipconfig_get_method(ipconfig);
4484 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4487 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4488 method == CONNMAN_IPCONFIG_METHOD_OFF)
4489 __connman_service_ipconfig_indicate_state(service,
4490 CONNMAN_SERVICE_STATE_DISCONNECT,
4491 CONNMAN_IPCONFIG_TYPE_IPV6);
4493 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4494 method == CONNMAN_IPCONFIG_METHOD_OFF)
4495 __connman_service_ipconfig_indicate_state(service,
4496 CONNMAN_SERVICE_STATE_DISCONNECT,
4497 CONNMAN_IPCONFIG_TYPE_IPV4);
4499 settings_changed(service, ipconfig);
4502 static const struct connman_ipconfig_ops service_ops = {
4504 .down = service_down,
4505 .lower_up = service_lower_up,
4506 .lower_down = service_lower_down,
4507 .ip_bound = service_ip_bound,
4508 .ip_release = service_ip_release,
4511 static void setup_ip4config(struct connman_service *service, int index,
4512 enum connman_ipconfig_method method)
4517 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4518 CONNMAN_IPCONFIG_TYPE_IPV4);
4519 if (service->ipconfig_ipv4 == NULL)
4522 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4524 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4526 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4529 static void setup_ip6config(struct connman_service *service, int index)
4534 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4535 CONNMAN_IPCONFIG_TYPE_IPV6);
4536 if (service->ipconfig_ipv6 == NULL)
4539 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4541 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4544 void __connman_service_create_ip4config(struct connman_service *service,
4547 const char *ident = service->profile;
4550 DBG("ipv4 %p", service->ipconfig_ipv4);
4552 if (service->ipconfig_ipv4 != NULL)
4555 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4560 keyfile = __connman_storage_open_profile(ident);
4561 if (keyfile == NULL)
4564 if (service->ipconfig_ipv4)
4565 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4566 service->identifier, "IPv4.");
4567 g_key_file_free(keyfile);
4570 void __connman_service_create_ip6config(struct connman_service *service,
4573 const char *ident = service->profile;
4576 DBG("ipv6 %p", service->ipconfig_ipv6);
4578 if (service->ipconfig_ipv6 != NULL)
4581 setup_ip6config(service, index);
4586 keyfile = __connman_storage_open_profile(ident);
4587 if (keyfile == NULL)
4590 if (service->ipconfig_ipv6 != NULL)
4591 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4592 service->identifier, "IPv6.");
4594 g_key_file_free(keyfile);
4598 * __connman_service_lookup_from_network:
4599 * @network: network structure
4601 * Look up a service by network (reference count will not be increased)
4603 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4605 struct connman_service *service;
4606 const char *ident, *group;
4609 DBG("network %p", network);
4611 ident = __connman_network_get_ident(network);
4615 group = connman_network_get_group(network);
4619 name = g_strdup_printf("%s_%s_%s",
4620 __connman_network_get_type(network), ident, group);
4621 service = lookup_by_identifier(name);
4627 struct connman_service *__connman_service_lookup_from_index(int index)
4629 struct connman_service *service;
4630 GSequenceIter *iter;
4632 iter = g_sequence_get_begin_iter(service_list);
4634 while (g_sequence_iter_is_end(iter) == FALSE) {
4635 service = g_sequence_get(iter);
4637 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4641 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4645 iter = g_sequence_iter_next(iter);
4651 const char *__connman_service_get_ident(struct connman_service *service)
4653 return service->identifier;
4656 const char *__connman_service_get_path(struct connman_service *service)
4658 return service->path;
4661 unsigned int __connman_service_get_order(struct connman_service *service)
4663 GSequenceIter *iter;
4665 if (service == NULL)
4668 if (service->favorite == FALSE) {
4673 iter = g_hash_table_lookup(service_hash, service->identifier);
4675 if (g_sequence_iter_get_position(iter) == 0)
4677 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4678 service->order = 10;
4684 return service->order;
4687 static enum connman_service_type convert_network_type(struct connman_network *network)
4689 enum connman_network_type type = connman_network_get_type(network);
4692 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4693 case CONNMAN_NETWORK_TYPE_VENDOR:
4695 case CONNMAN_NETWORK_TYPE_ETHERNET:
4696 return CONNMAN_SERVICE_TYPE_ETHERNET;
4697 case CONNMAN_NETWORK_TYPE_WIFI:
4698 return CONNMAN_SERVICE_TYPE_WIFI;
4699 case CONNMAN_NETWORK_TYPE_WIMAX:
4700 return CONNMAN_SERVICE_TYPE_WIMAX;
4701 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4702 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4703 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4704 case CONNMAN_NETWORK_TYPE_CELLULAR:
4705 return CONNMAN_SERVICE_TYPE_CELLULAR;
4708 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4711 static enum connman_service_security convert_wifi_security(const char *security)
4713 if (security == NULL)
4714 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4715 else if (g_str_equal(security, "none") == TRUE)
4716 return CONNMAN_SERVICE_SECURITY_NONE;
4717 else if (g_str_equal(security, "wep") == TRUE)
4718 return CONNMAN_SERVICE_SECURITY_WEP;
4719 else if (g_str_equal(security, "psk") == TRUE)
4720 return CONNMAN_SERVICE_SECURITY_PSK;
4721 else if (g_str_equal(security, "ieee8021x") == TRUE)
4722 return CONNMAN_SERVICE_SECURITY_8021X;
4723 else if (g_str_equal(security, "wpa") == TRUE)
4724 return CONNMAN_SERVICE_SECURITY_WPA;
4725 else if (g_str_equal(security, "rsn") == TRUE)
4726 return CONNMAN_SERVICE_SECURITY_RSN;
4728 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4731 static void update_from_network(struct connman_service *service,
4732 struct connman_network *network)
4734 connman_uint8_t strength = service->strength;
4735 GSequenceIter *iter;
4738 DBG("service %p network %p", service, network);
4740 if (is_connected(service) == TRUE)
4743 if (is_connecting(service) == TRUE)
4746 str = connman_network_get_string(network, "Name");
4748 g_free(service->name);
4749 service->name = g_strdup(str);
4750 service->hidden = FALSE;
4752 g_free(service->name);
4753 service->name = NULL;
4754 service->hidden = TRUE;
4757 service->strength = connman_network_get_strength(network);
4758 service->roaming = connman_network_get_bool(network, "Roaming");
4760 if (service->strength == 0) {
4762 * Filter out 0-values; it's unclear what they mean
4763 * and they cause anomalous sorting of the priority list.
4765 service->strength = strength;
4768 str = connman_network_get_string(network, "WiFi.Security");
4769 service->security = convert_wifi_security(str);
4771 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4772 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4774 if (service->strength > strength && service->network != NULL) {
4775 connman_network_unref(service->network);
4776 service->network = connman_network_ref(network);
4778 strength_changed(service);
4781 if (service->network == NULL)
4782 service->network = connman_network_ref(network);
4784 iter = g_hash_table_lookup(service_hash, service->identifier);
4786 g_sequence_sort_changed(iter, service_compare, NULL);
4790 * __connman_service_create_from_network:
4791 * @network: network structure
4793 * Look up service by network and if not found, create one
4795 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4797 struct connman_service *service;
4798 struct connman_device *device;
4799 const char *ident, *group;
4803 DBG("network %p", network);
4805 ident = __connman_network_get_ident(network);
4809 group = connman_network_get_group(network);
4813 name = g_strdup_printf("%s_%s_%s",
4814 __connman_network_get_type(network), ident, group);
4815 service = service_get(name);
4818 if (service == NULL)
4821 if (__connman_network_get_weakness(network) == TRUE)
4824 if (service->path != NULL) {
4825 update_from_network(service, network);
4826 __connman_profile_changed(TRUE);
4830 service->type = convert_network_type(network);
4832 switch (service->type) {
4833 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4834 case CONNMAN_SERVICE_TYPE_SYSTEM:
4835 case CONNMAN_SERVICE_TYPE_ETHERNET:
4836 case CONNMAN_SERVICE_TYPE_WIMAX:
4837 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4838 case CONNMAN_SERVICE_TYPE_GPS:
4839 case CONNMAN_SERVICE_TYPE_VPN:
4840 case CONNMAN_SERVICE_TYPE_GADGET:
4841 service->autoconnect = FALSE;
4843 case CONNMAN_SERVICE_TYPE_WIFI:
4844 case CONNMAN_SERVICE_TYPE_CELLULAR:
4845 service->autoconnect = TRUE;
4849 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4850 service->state = combine_state(service->state_ipv4, service->state_ipv6);
4852 update_from_network(service, network);
4854 index = connman_network_get_index(network);
4856 if (service->ipconfig_ipv4 == NULL)
4857 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4859 if (service->ipconfig_ipv6 == NULL)
4860 setup_ip6config(service, index);
4862 service_register(service);
4864 if (service->favorite == TRUE) {
4865 device = connman_network_get_device(service->network);
4866 if (device && __connman_device_scanning(device) == FALSE)
4867 __connman_service_auto_connect();
4870 __connman_notifier_service_add(service);
4875 void __connman_service_update_from_network(struct connman_network *network)
4877 struct connman_service *service;
4878 connman_uint8_t strength;
4879 connman_bool_t roaming;
4880 GSequenceIter *iter;
4882 connman_bool_t stats_enable;
4884 DBG("network %p", network);
4886 service = __connman_service_lookup_from_network(network);
4887 if (service == NULL)
4890 if (service->network == NULL)
4893 name = connman_network_get_string(service->network, "Name");
4894 if (g_strcmp0(service->name, name) != 0) {
4895 g_free(service->name);
4896 service->name = g_strdup(name);
4897 connman_dbus_property_changed_basic(service->path,
4898 CONNMAN_SERVICE_INTERFACE, "Name",
4899 DBUS_TYPE_STRING, &service->name);
4902 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4903 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4905 strength = connman_network_get_strength(service->network);
4906 if (strength == service->strength)
4909 service->strength = strength;
4911 strength_changed(service);
4914 roaming = connman_network_get_bool(service->network, "Roaming");
4915 if (roaming == service->roaming)
4918 stats_enable = stats_enabled(service);
4919 if (stats_enable == TRUE)
4920 stats_stop(service);
4922 service->roaming = roaming;
4924 if (stats_enable == TRUE)
4925 stats_start(service);
4927 roaming_changed(service);
4929 iter = g_hash_table_lookup(service_hash, service->identifier);
4931 g_sequence_sort_changed(iter, service_compare, NULL);
4934 void __connman_service_remove_from_network(struct connman_network *network)
4936 struct connman_service *service;
4938 DBG("network %p", network);
4940 service = __connman_service_lookup_from_network(network);
4941 if (service == NULL)
4944 __connman_service_put(service);
4947 void __connman_service_reset_from_networks(struct connman_service *service,
4948 GHashTable *networks)
4950 struct connman_network *network;
4951 GHashTableIter iter;
4952 gpointer key, value;
4954 DBG("service %p", service);
4958 g_hash_table_iter_init(&iter, networks);
4960 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4961 struct connman_network *iter_network = value;
4962 uint8_t strength, iter_strength;
4964 if (network == NULL) {
4965 network = iter_network;
4969 strength = connman_network_get_strength(network);
4970 iter_strength = connman_network_get_strength(iter_network);
4972 if (iter_strength > strength)
4973 network = iter_network;
4976 if (network == NULL) {
4977 service->network = NULL;
4981 __connman_service_create_from_network(network);
4985 * __connman_service_create_from_provider:
4986 * @provider: provider structure
4988 * Look up service by provider and if not found, create one
4990 struct connman_service *
4991 __connman_service_create_from_provider(struct connman_provider *provider)
4993 struct connman_service *service;
4994 const char *ident, *str;
4996 int index = connman_provider_get_index(provider);
4998 DBG("provider %p", provider);
5000 ident = __connman_provider_get_ident(provider);
5004 name = g_strdup_printf("vpn_%s", ident);
5005 service = service_get(name);
5008 if (service == NULL)
5011 service->type = CONNMAN_SERVICE_TYPE_VPN;
5012 service->provider = connman_provider_ref(provider);
5013 service->autoconnect = FALSE;
5015 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5017 str = connman_provider_get_string(provider, "Name");
5019 g_free(service->name);
5020 service->name = g_strdup(str);
5021 service->hidden = FALSE;
5023 g_free(service->name);
5024 service->name = NULL;
5025 service->hidden = TRUE;
5028 service->strength = 0;
5030 if (service->ipconfig_ipv4 == NULL)
5031 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5033 if (service->ipconfig_ipv6 == NULL)
5034 setup_ip6config(service, index);
5036 service_register(service);
5038 __connman_notifier_service_add(service);
5043 static int service_load(struct connman_service *service)
5045 const char *ident = service->profile;
5047 GError *error = NULL;
5048 gchar *pathname, *data = NULL;
5051 connman_bool_t autoconnect;
5052 unsigned int ssid_len;
5055 DBG("service %p", service);
5060 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5061 if (pathname == NULL)
5064 keyfile = g_key_file_new();
5066 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
5073 if (g_key_file_load_from_data(keyfile, data, length,
5074 0, NULL) == FALSE) {
5081 switch (service->type) {
5082 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5083 case CONNMAN_SERVICE_TYPE_SYSTEM:
5084 case CONNMAN_SERVICE_TYPE_ETHERNET:
5085 case CONNMAN_SERVICE_TYPE_GPS:
5086 case CONNMAN_SERVICE_TYPE_VPN:
5087 case CONNMAN_SERVICE_TYPE_GADGET:
5089 case CONNMAN_SERVICE_TYPE_WIFI:
5090 if (service->name == NULL) {
5093 name = g_key_file_get_string(keyfile,
5094 service->identifier, "Name", NULL);
5096 g_free(service->name);
5097 service->name = name;
5100 if (service->network != NULL)
5101 connman_network_set_name(service->network,
5105 if (service->network &&
5106 connman_network_get_blob(service->network,
5107 "WiFi.SSID", &ssid_len) == NULL) {
5110 hex_ssid = g_key_file_get_string(keyfile,
5111 service->identifier,
5114 if (hex_ssid != NULL) {
5116 unsigned int i, j = 0, hex;
5117 size_t hex_ssid_len = strlen(hex_ssid);
5119 ssid = g_try_malloc0(hex_ssid_len / 2);
5126 for (i = 0; i < hex_ssid_len; i += 2) {
5127 sscanf(hex_ssid + i, "%02x", &hex);
5131 connman_network_set_blob(service->network,
5132 "WiFi.SSID", ssid, hex_ssid_len / 2);
5139 case CONNMAN_SERVICE_TYPE_WIMAX:
5140 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5141 case CONNMAN_SERVICE_TYPE_CELLULAR:
5142 service->favorite = g_key_file_get_boolean(keyfile,
5143 service->identifier, "Favorite", NULL);
5145 autoconnect = g_key_file_get_boolean(keyfile,
5146 service->identifier, "AutoConnect", &error);
5148 service->autoconnect = autoconnect;
5149 g_clear_error(&error);
5151 str = g_key_file_get_string(keyfile,
5152 service->identifier, "Failure", NULL);
5154 if (service->favorite == FALSE)
5155 service->state_ipv4 = service->state_ipv6 =
5156 CONNMAN_SERVICE_STATE_FAILURE;
5157 service->error = string2error(str);
5162 str = g_key_file_get_string(keyfile,
5163 service->identifier, "Modified", NULL);
5165 g_time_val_from_iso8601(str, &service->modified);
5169 str = g_key_file_get_string(keyfile,
5170 service->identifier, "Passphrase", NULL);
5172 g_free(service->passphrase);
5173 service->passphrase = str;
5176 if (service->ipconfig_ipv4 != NULL)
5177 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5178 service->identifier, "IPv4.");
5180 if (service->ipconfig_ipv6 != NULL)
5181 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5182 service->identifier, "IPv6.");
5184 service->nameservers_config = g_key_file_get_string_list(keyfile,
5185 service->identifier, "Nameservers", &length, NULL);
5186 if (service->nameservers_config != NULL && length == 0) {
5187 g_strfreev(service->nameservers_config);
5188 service->nameservers_config = NULL;
5191 service->domains = g_key_file_get_string_list(keyfile,
5192 service->identifier, "Domains", &length, NULL);
5193 if (service->domains != NULL && length == 0) {
5194 g_strfreev(service->domains);
5195 service->domains = NULL;
5198 str = g_key_file_get_string(keyfile,
5199 service->identifier, "Proxy.Method", NULL);
5201 service->proxy_config = string2proxymethod(str);
5205 service->proxies = g_key_file_get_string_list(keyfile,
5206 service->identifier, "Proxy.Servers", &length, NULL);
5207 if (service->proxies != NULL && length == 0) {
5208 g_strfreev(service->proxies);
5209 service->proxies = NULL;
5212 service->excludes = g_key_file_get_string_list(keyfile,
5213 service->identifier, "Proxy.Excludes", &length, NULL);
5214 if (service->excludes != NULL && length == 0) {
5215 g_strfreev(service->excludes);
5216 service->excludes = NULL;
5219 str = g_key_file_get_string(keyfile,
5220 service->identifier, "Proxy.URL", NULL);
5222 g_free(service->pac);
5227 g_key_file_free(keyfile);
5232 static int service_save(struct connman_service *service)
5234 const char *ident = service->profile;
5236 gchar *pathname, *data = NULL;
5239 const char *cst_str = NULL;
5242 DBG("service %p", service);
5247 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5248 if (pathname == NULL)
5251 keyfile = g_key_file_new();
5253 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5257 if (g_key_file_load_from_data(keyfile, data, length,
5265 if (service->name != NULL)
5266 g_key_file_set_string(keyfile, service->identifier,
5267 "Name", service->name);
5269 switch (service->type) {
5270 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5271 case CONNMAN_SERVICE_TYPE_SYSTEM:
5272 case CONNMAN_SERVICE_TYPE_ETHERNET:
5273 case CONNMAN_SERVICE_TYPE_GPS:
5274 case CONNMAN_SERVICE_TYPE_VPN:
5275 case CONNMAN_SERVICE_TYPE_GADGET:
5277 case CONNMAN_SERVICE_TYPE_WIFI:
5278 if (service->network) {
5279 const unsigned char *ssid;
5280 unsigned int ssid_len = 0;
5282 ssid = connman_network_get_blob(service->network,
5283 "WiFi.SSID", &ssid_len);
5285 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5286 char *identifier = service->identifier;
5290 str = g_string_sized_new(ssid_len * 2);
5296 for (i = 0; i < ssid_len; i++)
5297 g_string_append_printf(str,
5300 g_key_file_set_string(keyfile, identifier,
5303 g_string_free(str, TRUE);
5308 case CONNMAN_SERVICE_TYPE_WIMAX:
5309 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5310 case CONNMAN_SERVICE_TYPE_CELLULAR:
5311 g_key_file_set_boolean(keyfile, service->identifier,
5312 "Favorite", service->favorite);
5314 if (service->favorite == TRUE)
5315 g_key_file_set_boolean(keyfile, service->identifier,
5316 "AutoConnect", service->autoconnect);
5318 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5319 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5320 const char *failure = error2string(service->error);
5321 if (failure != NULL)
5322 g_key_file_set_string(keyfile,
5323 service->identifier,
5324 "Failure", failure);
5326 g_key_file_remove_key(keyfile, service->identifier,
5332 str = g_time_val_to_iso8601(&service->modified);
5334 g_key_file_set_string(keyfile, service->identifier,
5339 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5340 g_key_file_set_string(keyfile, service->identifier,
5341 "Passphrase", service->passphrase);
5343 g_key_file_remove_key(keyfile, service->identifier,
5344 "Passphrase", NULL);
5346 if (service->ipconfig_ipv4 != NULL)
5347 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5348 service->identifier, "IPv4.");
5350 if (service->ipconfig_ipv6 != NULL)
5351 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5352 service->identifier, "IPv6.");
5354 if (service->nameservers_config != NULL) {
5355 guint len = g_strv_length(service->nameservers_config);
5357 g_key_file_set_string_list(keyfile, service->identifier,
5359 (const gchar **) service->nameservers_config, len);
5361 g_key_file_remove_key(keyfile, service->identifier,
5362 "Nameservers", NULL);
5364 if (service->domains != NULL) {
5365 guint len = g_strv_length(service->domains);
5367 g_key_file_set_string_list(keyfile, service->identifier,
5369 (const gchar **) service->domains, len);
5371 g_key_file_remove_key(keyfile, service->identifier,
5374 cst_str = proxymethod2string(service->proxy_config);
5375 if (cst_str != NULL)
5376 g_key_file_set_string(keyfile, service->identifier,
5377 "Proxy.Method", cst_str);
5379 if (service->proxies != NULL) {
5380 guint len = g_strv_length(service->proxies);
5382 g_key_file_set_string_list(keyfile, service->identifier,
5384 (const gchar **) service->proxies, len);
5386 g_key_file_remove_key(keyfile, service->identifier,
5387 "Proxy.Servers", NULL);
5389 if (service->excludes != NULL) {
5390 guint len = g_strv_length(service->excludes);
5392 g_key_file_set_string_list(keyfile, service->identifier,
5394 (const gchar **) service->excludes, len);
5396 g_key_file_remove_key(keyfile, service->identifier,
5397 "Proxy.Excludes", NULL);
5399 if (service->pac != NULL && strlen(service->pac) > 0)
5400 g_key_file_set_string(keyfile, service->identifier,
5401 "Proxy.URL", service->pac);
5403 g_key_file_remove_key(keyfile, service->identifier,
5406 data = g_key_file_to_data(keyfile, &length, NULL);
5408 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5409 connman_error("Failed to store service information");
5414 g_key_file_free(keyfile);
5421 static struct connman_storage service_storage = {
5423 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5424 .service_load = service_load,
5425 .service_save = service_save,
5428 int __connman_service_init(void)
5432 connection = connman_dbus_get_connection();
5434 if (connman_storage_register(&service_storage) < 0)
5435 connman_error("Failed to register service storage");
5437 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5440 service_list = g_sequence_new(service_free);
5445 void __connman_service_cleanup(void)
5451 list = service_list;
5452 service_list = NULL;
5453 g_sequence_free(list);
5455 g_hash_table_destroy(service_hash);
5456 service_hash = NULL;
5458 g_slist_free(counter_list);
5459 counter_list = NULL;
5461 connman_storage_unregister(&service_storage);
5463 dbus_connection_unref(connection);