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 g_sequence_foreach(service_list, append_path, iter);
132 struct connman_service *service;
135 static void compare_path(gpointer value, gpointer user_data)
137 struct connman_service *service = value;
138 struct find_data *data = user_data;
140 if (data->service != NULL)
143 if (g_strcmp0(service->path, data->path) == 0)
144 data->service = service;
147 static struct connman_service *find_service(const char *path)
149 struct find_data data = { .path = path, .service = NULL };
151 DBG("path %s", path);
153 g_sequence_foreach(service_list, compare_path, &data);
158 const char *__connman_service_type2string(enum connman_service_type type)
161 case CONNMAN_SERVICE_TYPE_UNKNOWN:
163 case CONNMAN_SERVICE_TYPE_SYSTEM:
165 case CONNMAN_SERVICE_TYPE_ETHERNET:
167 case CONNMAN_SERVICE_TYPE_WIFI:
169 case CONNMAN_SERVICE_TYPE_WIMAX:
171 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
173 case CONNMAN_SERVICE_TYPE_CELLULAR:
175 case CONNMAN_SERVICE_TYPE_GPS:
177 case CONNMAN_SERVICE_TYPE_VPN:
179 case CONNMAN_SERVICE_TYPE_GADGET:
186 static const char *security2string(enum connman_service_security security)
189 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
191 case CONNMAN_SERVICE_SECURITY_NONE:
193 case CONNMAN_SERVICE_SECURITY_WEP:
195 case CONNMAN_SERVICE_SECURITY_PSK:
197 case CONNMAN_SERVICE_SECURITY_8021X:
199 case CONNMAN_SERVICE_SECURITY_WPA:
201 case CONNMAN_SERVICE_SECURITY_RSN:
208 static const char *state2string(enum connman_service_state state)
211 case CONNMAN_SERVICE_STATE_UNKNOWN:
213 case CONNMAN_SERVICE_STATE_IDLE:
215 case CONNMAN_SERVICE_STATE_ASSOCIATION:
216 return "association";
217 case CONNMAN_SERVICE_STATE_CONFIGURATION:
218 return "configuration";
219 case CONNMAN_SERVICE_STATE_READY:
221 case CONNMAN_SERVICE_STATE_ONLINE:
223 case CONNMAN_SERVICE_STATE_DISCONNECT:
225 case CONNMAN_SERVICE_STATE_FAILURE:
232 static const char *error2string(enum connman_service_error error)
235 case CONNMAN_SERVICE_ERROR_UNKNOWN:
237 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
238 return "out-of-range";
239 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
240 return "pin-missing";
241 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
242 return "dhcp-failed";
243 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
244 return "connect-failed";
245 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
246 return "login-failed";
247 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
248 return "auth-failed";
249 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
250 return "invalid-key";
256 static enum connman_service_error string2error(const char *error)
258 if (g_strcmp0(error, "dhcp-failed") == 0)
259 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
260 else if (g_strcmp0(error, "pin-missing") == 0)
261 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
262 else if (g_strcmp0(error, "invalid-key") == 0)
263 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
265 return CONNMAN_SERVICE_ERROR_UNKNOWN;
268 static const char *proxymethod2string(enum connman_service_proxy_method method)
271 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
273 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
275 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
277 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
284 static enum connman_service_proxy_method string2proxymethod(const char *method)
286 if (g_strcmp0(method, "direct") == 0)
287 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
288 else if (g_strcmp0(method, "auto") == 0)
289 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
290 else if (g_strcmp0(method, "manual") == 0)
291 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
293 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
296 static enum connman_service_state combine_state(
297 enum connman_service_state state_a,
298 enum connman_service_state state_b)
300 enum connman_service_state result;
302 if (state_a == state_b) {
307 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
312 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
317 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
322 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
327 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
328 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
329 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
330 state_b == CONNMAN_SERVICE_STATE_READY)
337 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
338 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
339 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
340 state_a == CONNMAN_SERVICE_STATE_READY)
347 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
348 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
349 state_b == CONNMAN_SERVICE_STATE_READY)
356 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
357 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
358 state_a == CONNMAN_SERVICE_STATE_READY)
365 if (state_a == CONNMAN_SERVICE_STATE_READY) {
366 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
367 state_b == CONNMAN_SERVICE_STATE_DISCONNECT)
374 if (state_b == CONNMAN_SERVICE_STATE_READY) {
375 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
376 state_a == CONNMAN_SERVICE_STATE_DISCONNECT)
383 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
384 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT)
391 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
392 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT)
399 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
404 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
409 result = CONNMAN_SERVICE_STATE_FAILURE;
415 static connman_bool_t is_connecting_state(struct connman_service *service,
416 enum connman_service_state state)
419 case CONNMAN_SERVICE_STATE_UNKNOWN:
420 case CONNMAN_SERVICE_STATE_IDLE:
421 case CONNMAN_SERVICE_STATE_FAILURE:
422 if (service->network != NULL)
423 return connman_network_get_connecting(service->network);
424 case CONNMAN_SERVICE_STATE_DISCONNECT:
425 case CONNMAN_SERVICE_STATE_READY:
426 case CONNMAN_SERVICE_STATE_ONLINE:
428 case CONNMAN_SERVICE_STATE_ASSOCIATION:
429 case CONNMAN_SERVICE_STATE_CONFIGURATION:
436 static connman_bool_t is_connected_state(const struct connman_service *service,
437 enum connman_service_state state)
440 case CONNMAN_SERVICE_STATE_UNKNOWN:
441 case CONNMAN_SERVICE_STATE_IDLE:
442 case CONNMAN_SERVICE_STATE_ASSOCIATION:
443 case CONNMAN_SERVICE_STATE_CONFIGURATION:
444 case CONNMAN_SERVICE_STATE_DISCONNECT:
445 case CONNMAN_SERVICE_STATE_FAILURE:
447 case CONNMAN_SERVICE_STATE_READY:
448 case CONNMAN_SERVICE_STATE_ONLINE:
455 static connman_bool_t is_connecting(struct connman_service *service)
457 return is_connecting_state(service, service->state);
460 static connman_bool_t is_connected(struct connman_service *service)
462 return is_connected_state(service, service->state);
465 static void update_nameservers(struct connman_service *service)
469 if (service->ipconfig_ipv4)
470 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
471 else if (service->ipconfig_ipv6)
472 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
479 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
480 case CONNMAN_SERVICE_STATE_UNKNOWN:
481 case CONNMAN_SERVICE_STATE_IDLE:
482 case CONNMAN_SERVICE_STATE_ASSOCIATION:
483 case CONNMAN_SERVICE_STATE_CONFIGURATION:
485 case CONNMAN_SERVICE_STATE_FAILURE:
486 case CONNMAN_SERVICE_STATE_DISCONNECT:
487 connman_resolver_remove_all(ifname);
489 case CONNMAN_SERVICE_STATE_READY:
490 case CONNMAN_SERVICE_STATE_ONLINE:
494 connman_resolver_remove_all(ifname);
496 if (service->nameservers_config != NULL) {
499 for (i = 0; service->nameservers_config[i] != NULL; i++) {
500 connman_resolver_append(ifname, NULL,
501 service->nameservers_config[i]);
503 } else if (service->nameservers != NULL) {
506 for (i = 0; service->nameservers[i] != NULL; i++) {
507 connman_resolver_append(ifname, NULL,
508 service->nameservers[i]);
512 if (service->domains != NULL) {
515 for (i = 0; service->domains[i]; i++)
516 connman_resolver_append(ifname, service->domains[i],
518 } else if (service->domainname != NULL)
519 connman_resolver_append(ifname, service->domainname, NULL);
521 connman_resolver_flush();
524 int __connman_service_nameserver_append(struct connman_service *service,
525 const char *nameserver)
529 DBG("service %p nameserver %s", service, nameserver);
531 if (nameserver == NULL)
534 if (service->nameservers != NULL) {
537 for (i = 0; service->nameservers[i] != NULL; i++)
538 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
541 len = g_strv_length(service->nameservers);
542 service->nameservers = g_try_renew(char *, service->nameservers,
546 service->nameservers = g_try_new0(char *, len + 2);
549 if (service->nameservers == NULL)
552 service->nameservers[len] = g_strdup(nameserver);
553 service->nameservers[len + 1] = NULL;
555 update_nameservers(service);
560 int __connman_service_nameserver_remove(struct connman_service *service,
561 const char *nameserver)
566 DBG("service %p nameserver %s", service, nameserver);
568 if (nameserver == NULL)
571 if (service->nameservers == NULL)
574 len = g_strv_length(service->nameservers);
576 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
579 g_strfreev(service->nameservers);
580 service->nameservers = NULL;
585 servers = g_try_new0(char *, len - 1);
589 for (i = 0, j = 0; i < len; i++) {
590 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
591 servers[j] = g_strdup(service->nameservers[i]);
595 servers[len - 2] = NULL;
597 g_strfreev(service->nameservers);
598 service->nameservers = servers;
600 update_nameservers(service);
605 void __connman_service_nameserver_clear(struct connman_service *service)
607 g_strfreev(service->nameservers);
608 service->nameservers = NULL;
610 update_nameservers(service);
613 static void nameserver_add_routes(int index, char **nameservers,
617 struct addrinfo hints;
618 struct addrinfo *addr;
620 for (i = 0; nameservers[i] != NULL; i++) {
621 memset(&hints, 0, sizeof(struct addrinfo));
622 hints.ai_flags = AI_NUMERICHOST;
625 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
626 if (ret == EAI_NONAME)
627 family = AF_INET; /* use the IPv4 as a default */
631 family = addr->ai_family;
633 if (family == AF_INET) {
634 if (connman_inet_compare_subnet(index,
635 nameservers[i]) != TRUE)
636 connman_inet_add_host_route(index,
638 } else if (family == AF_INET6)
639 connman_inet_add_ipv6_host_route(index,
646 static void nameserver_del_routes(int index, char **nameservers)
649 struct addrinfo hints;
650 struct addrinfo *addr;
652 for (i = 0; nameservers[i] != NULL; i++) {
653 memset(&hints, 0, sizeof(struct addrinfo));
654 hints.ai_flags = AI_NUMERICHOST;
657 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
658 if (ret == EAI_NONAME)
659 family = AF_INET; /* use the IPv4 as a default */
663 family = addr->ai_family;
665 if (family == AF_INET)
666 connman_inet_del_host_route(index, nameservers[i]);
667 else if (family == AF_INET6)
668 connman_inet_del_ipv6_host_route(index,
675 void __connman_service_nameserver_add_routes(struct connman_service *service,
683 if (service->network != NULL)
684 index = connman_network_get_index(service->network);
685 else if (service->provider != NULL)
686 index = connman_provider_get_index(service->provider);
688 if (service->nameservers_config != NULL) {
690 * Configured nameserver takes preference over the
691 * discoverd nameserver gathered from DHCP, VPN, etc.
693 nameserver_add_routes(index, service->nameservers_config, gw);
694 } else if (service->nameservers != NULL) {
696 * We add nameservers host routes for nameservers that
697 * are not on our subnet. For those who are, the subnet
698 * route will be installed by the time the dns proxy code
699 * tries to reach them. The subnet route is installed
700 * when setting the interface IP address.
702 nameserver_add_routes(index, service->nameservers, gw);
706 void __connman_service_nameserver_del_routes(struct connman_service *service)
713 if (service->network != NULL)
714 index = connman_network_get_index(service->network);
715 else if (service->provider != NULL)
716 index = connman_provider_get_index(service->provider);
718 if (service->nameservers_config != NULL)
719 nameserver_del_routes(index, service->nameservers_config);
720 else if (service->nameservers != NULL)
721 nameserver_del_routes(index, service->nameservers);
724 static struct connman_stats *stats_get(struct connman_service *service)
726 if (service->roaming == TRUE)
727 return &service->stats_roaming;
729 return &service->stats;
732 static connman_bool_t stats_enabled(struct connman_service *service)
734 struct connman_stats *stats = stats_get(service);
736 return stats->enabled;
739 static void stats_start(struct connman_service *service)
741 struct connman_stats *stats = stats_get(service);
743 DBG("service %p", service);
745 if (stats->timer == NULL)
748 stats->enabled = TRUE;
749 stats->data_last.time = stats->data.time;
751 g_timer_start(stats->timer);
754 static void stats_stop(struct connman_service *service)
756 struct connman_stats *stats = stats_get(service);
757 unsigned int seconds;
759 DBG("service %p", service);
761 if (stats->timer == NULL)
764 if (stats->enabled == FALSE)
767 g_timer_stop(stats->timer);
769 seconds = g_timer_elapsed(stats->timer, NULL);
770 stats->data.time = stats->data_last.time + seconds;
772 stats->enabled = FALSE;
775 static void reset_stats(struct connman_service *service)
777 DBG("service %p", service);
780 service->stats.valid = FALSE;
782 service->stats.data.rx_packets = 0;
783 service->stats.data.tx_packets = 0;
784 service->stats.data.rx_bytes = 0;
785 service->stats.data.tx_bytes = 0;
786 service->stats.data.rx_errors = 0;
787 service->stats.data.tx_errors = 0;
788 service->stats.data.rx_dropped = 0;
789 service->stats.data.tx_dropped = 0;
790 service->stats.data.time = 0;
791 service->stats.data_last.time = 0;
793 g_timer_reset(service->stats.timer);
796 service->stats_roaming.valid = FALSE;
798 service->stats_roaming.data.rx_packets = 0;
799 service->stats_roaming.data.tx_packets = 0;
800 service->stats_roaming.data.rx_bytes = 0;
801 service->stats_roaming.data.tx_bytes = 0;
802 service->stats_roaming.data.rx_errors = 0;
803 service->stats_roaming.data.tx_errors = 0;
804 service->stats_roaming.data.rx_dropped = 0;
805 service->stats_roaming.data.tx_dropped = 0;
806 service->stats_roaming.data.time = 0;
807 service->stats_roaming.data_last.time = 0;
809 g_timer_reset(service->stats_roaming.timer);
812 static struct connman_service *get_default(void)
814 struct connman_service *service;
817 iter = g_sequence_get_begin_iter(service_list);
819 if (g_sequence_iter_is_end(iter) == TRUE)
822 service = g_sequence_get(iter);
824 if (is_connected(service) == FALSE)
830 static void default_changed(void)
832 struct connman_service *service = get_default();
834 __connman_notifier_default_changed(service);
837 const char *__connman_service_default(void)
839 struct connman_service *service;
841 service = get_default();
845 return __connman_service_type2string(service->type);
848 static void state_changed(struct connman_service *service)
852 __connman_notifier_service_state_changed(service, service->state);
854 str = state2string(service->state);
858 connman_dbus_property_changed_basic(service->path,
859 CONNMAN_SERVICE_INTERFACE, "State",
860 DBUS_TYPE_STRING, &str);
863 static void strength_changed(struct connman_service *service)
865 if (service->strength == 0)
868 connman_dbus_property_changed_basic(service->path,
869 CONNMAN_SERVICE_INTERFACE, "Strength",
870 DBUS_TYPE_BYTE, &service->strength);
873 static void favorite_changed(struct connman_service *service)
875 if (service->path == NULL)
878 connman_dbus_property_changed_basic(service->path,
879 CONNMAN_SERVICE_INTERFACE, "Favorite",
880 DBUS_TYPE_BOOLEAN, &service->favorite);
883 static void immutable_changed(struct connman_service *service)
885 if (service->path == NULL)
888 connman_dbus_property_changed_basic(service->path,
889 CONNMAN_SERVICE_INTERFACE, "Immutable",
890 DBUS_TYPE_BOOLEAN, &service->immutable);
893 static void roaming_changed(struct connman_service *service)
895 if (service->path == NULL)
898 connman_dbus_property_changed_basic(service->path,
899 CONNMAN_SERVICE_INTERFACE, "Roaming",
900 DBUS_TYPE_BOOLEAN, &service->roaming);
903 static void autoconnect_changed(struct connman_service *service)
905 if (service->path == NULL)
908 connman_dbus_property_changed_basic(service->path,
909 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
910 DBUS_TYPE_BOOLEAN, &service->autoconnect);
913 static void passphrase_changed(struct connman_service *service)
915 dbus_bool_t required;
917 switch (service->type) {
918 case CONNMAN_SERVICE_TYPE_UNKNOWN:
919 case CONNMAN_SERVICE_TYPE_SYSTEM:
920 case CONNMAN_SERVICE_TYPE_ETHERNET:
921 case CONNMAN_SERVICE_TYPE_WIMAX:
922 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
923 case CONNMAN_SERVICE_TYPE_CELLULAR:
924 case CONNMAN_SERVICE_TYPE_GPS:
925 case CONNMAN_SERVICE_TYPE_VPN:
926 case CONNMAN_SERVICE_TYPE_GADGET:
928 case CONNMAN_SERVICE_TYPE_WIFI:
931 switch (service->security) {
932 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
933 case CONNMAN_SERVICE_SECURITY_NONE:
935 case CONNMAN_SERVICE_SECURITY_WEP:
936 case CONNMAN_SERVICE_SECURITY_PSK:
937 case CONNMAN_SERVICE_SECURITY_WPA:
938 case CONNMAN_SERVICE_SECURITY_RSN:
939 if (service->passphrase == NULL)
942 case CONNMAN_SERVICE_SECURITY_8021X:
948 connman_dbus_property_changed_basic(service->path,
949 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
950 DBUS_TYPE_BOOLEAN, &required);
953 static void login_changed(struct connman_service *service)
955 dbus_bool_t required = service->login_required;
957 if (service->path == NULL)
960 connman_dbus_property_changed_basic(service->path,
961 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
962 DBUS_TYPE_BOOLEAN, &required);
965 static void append_security(DBusMessageIter *iter, void *user_data)
967 struct connman_service *service = user_data;
970 str = security2string(service->security);
972 dbus_message_iter_append_basic(iter,
973 DBUS_TYPE_STRING, &str);
976 if (service->wps == TRUE)
977 dbus_message_iter_append_basic(iter,
978 DBUS_TYPE_STRING, &str);
981 static void append_ethernet(DBusMessageIter *iter, void *user_data)
983 struct connman_service *service = user_data;
985 if (service->ipconfig_ipv4 != NULL)
986 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
988 else if (service->ipconfig_ipv6 != NULL)
989 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
993 static void append_ipv4(DBusMessageIter *iter, void *user_data)
995 struct connman_service *service = user_data;
997 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
998 state2string(service->state_ipv4));
1000 if (is_connected_state(service, service->state_ipv4) == FALSE)
1003 if (service->ipconfig_ipv4 != NULL)
1004 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1007 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1009 struct connman_service *service = user_data;
1011 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1012 state2string(service->state_ipv6));
1014 if (is_connected_state(service, service->state_ipv6) == FALSE)
1017 if (service->ipconfig_ipv6 != NULL)
1018 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1019 service->ipconfig_ipv4);
1022 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1024 struct connman_service *service = user_data;
1026 if (service->ipconfig_ipv4 != NULL)
1027 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1031 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1033 struct connman_service *service = user_data;
1035 if (service->ipconfig_ipv6 != NULL)
1036 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1040 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1045 servers = *nameservers;
1047 for (i = 0; servers[i] != NULL; i++) {
1048 dbus_message_iter_append_basic(iter,
1049 DBUS_TYPE_STRING, &servers[i]);
1053 static void append_dns(DBusMessageIter *iter, void *user_data)
1055 struct connman_service *service = user_data;
1057 if (is_connected(service) == FALSE)
1060 if (service->nameservers_config != NULL) {
1061 append_nameserver(iter, &service->nameservers_config);
1063 } else if (service->nameservers != NULL) {
1064 append_nameserver(iter, &service->nameservers);
1069 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1071 struct connman_service *service = user_data;
1074 if (service->nameservers_config == NULL)
1077 for (i = 0; service->nameservers_config[i]; i++) {
1078 dbus_message_iter_append_basic(iter,
1080 &service->nameservers_config[i]);
1084 static void append_domain(DBusMessageIter *iter, void *user_data)
1086 struct connman_service *service = user_data;
1088 if (is_connected(service) == FALSE &&
1089 is_connecting(service) == FALSE)
1092 if (service->domainname == NULL)
1095 dbus_message_iter_append_basic(iter,
1096 DBUS_TYPE_STRING, &service->domainname);
1099 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1101 struct connman_service *service = user_data;
1104 if (service->domains == NULL)
1107 for (i = 0; service->domains[i]; i++)
1108 dbus_message_iter_append_basic(iter,
1109 DBUS_TYPE_STRING, &service->domains[i]);
1112 static void append_proxies(DBusMessageIter *iter, void *user_data)
1114 struct connman_service *service = user_data;
1117 if (service->proxies == NULL)
1120 for (i = 0; service->proxies[i]; i++)
1121 dbus_message_iter_append_basic(iter,
1122 DBUS_TYPE_STRING, &service->proxies[i]);
1125 static void append_excludes(DBusMessageIter *iter, void *user_data)
1127 struct connman_service *service = user_data;
1130 if (service->excludes == NULL)
1133 for (i = 0; service->excludes[i]; i++)
1134 dbus_message_iter_append_basic(iter,
1135 DBUS_TYPE_STRING, &service->excludes[i]);
1138 static void append_proxy(DBusMessageIter *iter, void *user_data)
1140 struct connman_service *service = user_data;
1141 enum connman_service_proxy_method proxy;
1142 const char *pac = NULL;
1143 const char *method = proxymethod2string(
1144 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1148 if (is_connected(service) == FALSE)
1151 proxy = connman_service_get_proxy_method(service);
1154 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1156 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1158 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1159 connman_dbus_dict_append_array(iter, "Servers",
1160 DBUS_TYPE_STRING, append_proxies,
1163 connman_dbus_dict_append_array(iter, "Excludes",
1164 DBUS_TYPE_STRING, append_excludes,
1167 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1168 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1169 if (service->ipconfig_ipv4 != NULL)
1170 pac = __connman_ipconfig_get_proxy_autoconfig(
1171 service->ipconfig_ipv4);
1172 else if (service->ipconfig_ipv6 != NULL)
1173 pac = __connman_ipconfig_get_proxy_autoconfig(
1174 service->ipconfig_ipv6);
1176 if (service->pac == NULL && pac == NULL)
1179 if (service->pac != NULL)
1182 connman_dbus_dict_append_basic(iter, "URL",
1183 DBUS_TYPE_STRING, &pac);
1187 method = proxymethod2string(proxy);
1190 connman_dbus_dict_append_basic(iter, "Method",
1191 DBUS_TYPE_STRING, &method);
1194 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1196 struct connman_service *service = user_data;
1199 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1202 switch (service->proxy_config) {
1203 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1205 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1207 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1208 if (service->proxies != NULL)
1209 connman_dbus_dict_append_array(iter, "Servers",
1211 append_proxies, service);
1213 if (service->excludes != NULL)
1214 connman_dbus_dict_append_array(iter, "Excludes",
1216 append_excludes, service);
1218 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1219 if (service->pac != NULL)
1220 connman_dbus_dict_append_basic(iter, "URL",
1221 DBUS_TYPE_STRING, &service->pac);
1225 method = proxymethod2string(service->proxy_config);
1227 connman_dbus_dict_append_basic(iter, "Method",
1228 DBUS_TYPE_STRING, &method);
1231 static void append_provider(DBusMessageIter *iter, void *user_data)
1233 struct connman_service *service = user_data;
1235 DBG("%p %p", service, service->provider);
1237 if (is_connected(service) == FALSE)
1240 if (service->provider != NULL)
1241 __connman_provider_append_properties(service->provider, iter);
1245 static void settings_changed(struct connman_service *service,
1246 struct connman_ipconfig *ipconfig)
1248 connman_dbus_property_changed_dict(service->path,
1249 CONNMAN_SERVICE_INTERFACE, "IPv4",
1250 append_ipv4, service);
1252 connman_dbus_property_changed_dict(service->path,
1253 CONNMAN_SERVICE_INTERFACE, "IPv6",
1254 append_ipv6, service);
1256 __connman_notifier_ipconfig_changed(service, ipconfig);
1259 static void ipv4_configuration_changed(struct connman_service *service)
1261 connman_dbus_property_changed_dict(service->path,
1262 CONNMAN_SERVICE_INTERFACE,
1263 "IPv4.Configuration",
1268 static void ipv6_configuration_changed(struct connman_service *service)
1270 connman_dbus_property_changed_dict(service->path,
1271 CONNMAN_SERVICE_INTERFACE,
1272 "IPv6.Configuration",
1277 static void dns_changed(struct connman_service *service)
1279 connman_dbus_property_changed_array(service->path,
1280 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1281 DBUS_TYPE_STRING, append_dns, service);
1284 static void dns_configuration_changed(struct connman_service *service)
1286 connman_dbus_property_changed_array(service->path,
1287 CONNMAN_SERVICE_INTERFACE,
1288 "Nameservers.Configuration",
1289 DBUS_TYPE_STRING, append_dnsconfig, service);
1291 dns_changed(service);
1294 static void domain_changed(struct connman_service *service)
1296 connman_dbus_property_changed_array(service->path,
1297 CONNMAN_SERVICE_INTERFACE, "Domains",
1298 DBUS_TYPE_STRING, append_domain, service);
1301 static void domain_configuration_changed(struct connman_service *service)
1303 connman_dbus_property_changed_array(service->path,
1304 CONNMAN_SERVICE_INTERFACE,
1305 "Domains.Configuration",
1306 DBUS_TYPE_STRING, append_domainconfig, service);
1309 static void proxy_changed(struct connman_service *service)
1311 connman_dbus_property_changed_dict(service->path,
1312 CONNMAN_SERVICE_INTERFACE, "Proxy",
1313 append_proxy, service);
1316 static void proxy_configuration_changed(struct connman_service *service)
1318 connman_dbus_property_changed_dict(service->path,
1319 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1320 append_proxyconfig, service);
1322 proxy_changed(service);
1325 static void link_changed(struct connman_service *service)
1327 connman_dbus_property_changed_dict(service->path,
1328 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1329 append_ethernet, service);
1332 static void stats_append_counters(DBusMessageIter *dict,
1333 struct connman_stats_data *stats,
1334 struct connman_stats_data *counters,
1335 connman_bool_t append_all)
1337 if (counters->rx_packets != stats->rx_packets || append_all) {
1338 counters->rx_packets = stats->rx_packets;
1339 connman_dbus_dict_append_basic(dict, "RX.Packets",
1340 DBUS_TYPE_UINT32, &stats->rx_packets);
1343 if (counters->tx_packets != stats->tx_packets || append_all) {
1344 counters->tx_packets = stats->tx_packets;
1345 connman_dbus_dict_append_basic(dict, "TX.Packets",
1346 DBUS_TYPE_UINT32, &stats->tx_packets);
1349 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1350 counters->rx_bytes = stats->rx_bytes;
1351 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1352 DBUS_TYPE_UINT32, &stats->rx_bytes);
1355 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1356 counters->tx_bytes = stats->tx_bytes;
1357 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1358 DBUS_TYPE_UINT32, &stats->tx_bytes);
1361 if (counters->rx_errors != stats->rx_errors || append_all) {
1362 counters->rx_errors = stats->rx_errors;
1363 connman_dbus_dict_append_basic(dict, "RX.Errors",
1364 DBUS_TYPE_UINT32, &stats->rx_errors);
1367 if (counters->tx_errors != stats->tx_errors || append_all) {
1368 counters->tx_errors = stats->tx_errors;
1369 connman_dbus_dict_append_basic(dict, "TX.Errors",
1370 DBUS_TYPE_UINT32, &stats->tx_errors);
1373 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1374 counters->rx_dropped = stats->rx_dropped;
1375 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1376 DBUS_TYPE_UINT32, &stats->rx_dropped);
1379 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1380 counters->tx_dropped = stats->tx_dropped;
1381 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1382 DBUS_TYPE_UINT32, &stats->tx_dropped);
1385 if (counters->time != stats->time || append_all) {
1386 counters->time = stats->time;
1387 connman_dbus_dict_append_basic(dict, "Time",
1388 DBUS_TYPE_UINT32, &stats->time);
1392 static void stats_append(struct connman_service *service,
1393 const char *counter,
1394 struct connman_stats_counter *counters,
1395 connman_bool_t append_all)
1397 DBusMessageIter array, dict;
1400 DBG("service %p counter %s", service, counter);
1402 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1406 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1407 &service->path, DBUS_TYPE_INVALID);
1409 dbus_message_iter_init_append(msg, &array);
1412 connman_dbus_dict_open(&array, &dict);
1414 stats_append_counters(&dict, &service->stats.data,
1415 &counters->stats.data, append_all);
1417 connman_dbus_dict_close(&array, &dict);
1419 /* roaming counter */
1420 connman_dbus_dict_open(&array, &dict);
1422 stats_append_counters(&dict, &service->stats_roaming.data,
1423 &counters->stats_roaming.data, append_all);
1425 connman_dbus_dict_close(&array, &dict);
1427 __connman_counter_send_usage(counter, msg);
1430 static void stats_update(struct connman_service *service,
1431 unsigned int rx_packets, unsigned int tx_packets,
1432 unsigned int rx_bytes, unsigned int tx_bytes,
1433 unsigned int rx_errors, unsigned int tx_errors,
1434 unsigned int rx_dropped, unsigned int tx_dropped)
1436 struct connman_stats *stats = stats_get(service);
1437 struct connman_stats_data *data_last = &stats->data_last;
1438 struct connman_stats_data *data = &stats->data;
1439 unsigned int seconds;
1441 DBG("service %p", service);
1443 if (stats->valid == TRUE) {
1445 rx_packets - data_last->rx_packets;
1447 tx_packets - data_last->tx_packets;
1449 rx_bytes - data_last->rx_bytes;
1451 tx_bytes - data_last->tx_bytes;
1453 rx_errors - data_last->rx_errors;
1455 tx_errors - data_last->tx_errors;
1457 rx_dropped - data_last->rx_dropped;
1459 tx_dropped - data_last->tx_dropped;
1461 stats->valid = TRUE;
1464 data_last->rx_packets = rx_packets;
1465 data_last->tx_packets = tx_packets;
1466 data_last->rx_bytes = rx_bytes;
1467 data_last->tx_bytes = tx_bytes;
1468 data_last->rx_errors = rx_errors;
1469 data_last->tx_errors = tx_errors;
1470 data_last->rx_dropped = rx_dropped;
1471 data_last->tx_dropped = tx_dropped;
1473 seconds = g_timer_elapsed(stats->timer, NULL);
1474 stats->data.time = stats->data_last.time + seconds;
1477 void __connman_service_notify(struct connman_service *service,
1478 unsigned int rx_packets, unsigned int tx_packets,
1479 unsigned int rx_bytes, unsigned int tx_bytes,
1480 unsigned int rx_errors, unsigned int tx_errors,
1481 unsigned int rx_dropped, unsigned int tx_dropped)
1483 GHashTableIter iter;
1484 gpointer key, value;
1485 const char *counter;
1486 struct connman_stats_counter *counters;
1487 struct connman_stats_data *data;
1490 if (service == NULL)
1493 if (is_connected(service) == FALSE)
1496 stats_update(service,
1497 rx_packets, tx_packets,
1499 rx_errors, tx_errors,
1500 rx_dropped, tx_dropped);
1502 data = &stats_get(service)->data;
1503 err = __connman_stats_update(service, service->roaming, data);
1505 connman_error("Failed to store statistics for %s",
1506 service->identifier);
1508 g_hash_table_iter_init(&iter, service->counter_table);
1509 while (g_hash_table_iter_next(&iter, &key, &value)) {
1513 stats_append(service, counter, counters, counters->append_all);
1514 counters->append_all = FALSE;
1518 int __connman_service_counter_register(const char *counter)
1520 struct connman_service *service;
1521 GSequenceIter *iter;
1522 struct connman_stats_counter *counters;
1524 DBG("counter %s", counter);
1526 counter_list = g_slist_append(counter_list, (gpointer)counter);
1528 iter = g_sequence_get_begin_iter(service_list);
1530 while (g_sequence_iter_is_end(iter) == FALSE) {
1531 service = g_sequence_get(iter);
1533 counters = g_try_new0(struct connman_stats_counter, 1);
1534 if (counters == NULL)
1537 counters->append_all = TRUE;
1539 g_hash_table_replace(service->counter_table, (gpointer)counter,
1542 iter = g_sequence_iter_next(iter);
1548 void __connman_service_counter_unregister(const char *counter)
1550 struct connman_service *service;
1551 GSequenceIter *iter;
1553 DBG("counter %s", counter);
1555 iter = g_sequence_get_begin_iter(service_list);
1557 while (g_sequence_iter_is_end(iter) == FALSE) {
1558 service = g_sequence_get(iter);
1560 g_hash_table_remove(service->counter_table, counter);
1562 iter = g_sequence_iter_next(iter);
1565 counter_list = g_slist_remove(counter_list, counter);
1568 GSequence *__connman_service_get_list(struct connman_session *session,
1569 service_match_cb service_match,
1570 create_service_entry_cb create_service_entry,
1571 GDestroyNotify destroy_service_entry)
1574 GSequenceIter *iter;
1575 struct connman_service *service;
1576 struct service_entry *entry;
1578 list = g_sequence_new(destroy_service_entry);
1582 iter = g_sequence_get_begin_iter(service_list);
1584 while (g_sequence_iter_is_end(iter) == FALSE) {
1585 service = g_sequence_get(iter);
1587 if (service_match(session, service) == TRUE) {
1588 entry = create_service_entry(service, service->name,
1593 g_sequence_append(list, entry);
1596 iter = g_sequence_iter_next(iter);
1602 void __connman_service_session_inc(struct connman_service *service)
1604 DBG("service %p", service);
1606 g_atomic_int_inc(&service->session_usage_count);
1609 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1611 connman_bool_t in_use;
1613 if (g_atomic_int_dec_and_test(&service->session_usage_count) == TRUE)
1618 DBG("service %p last %d", service, in_use);
1623 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1624 struct connman_service *service)
1626 dbus_bool_t required;
1629 str = __connman_service_type2string(service->type);
1631 connman_dbus_dict_append_basic(dict, "Type",
1632 DBUS_TYPE_STRING, &str);
1634 connman_dbus_dict_append_array(dict, "Security",
1635 DBUS_TYPE_STRING, append_security, service);
1637 str = state2string(service->state);
1639 connman_dbus_dict_append_basic(dict, "State",
1640 DBUS_TYPE_STRING, &str);
1642 str = error2string(service->error);
1644 connman_dbus_dict_append_basic(dict, "Error",
1645 DBUS_TYPE_STRING, &str);
1647 if (service->strength > 0)
1648 connman_dbus_dict_append_basic(dict, "Strength",
1649 DBUS_TYPE_BYTE, &service->strength);
1651 connman_dbus_dict_append_basic(dict, "Favorite",
1652 DBUS_TYPE_BOOLEAN, &service->favorite);
1654 connman_dbus_dict_append_basic(dict, "Immutable",
1655 DBUS_TYPE_BOOLEAN, &service->immutable);
1657 if (service->favorite == TRUE)
1658 connman_dbus_dict_append_basic(dict, "AutoConnect",
1659 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1661 connman_dbus_dict_append_basic(dict, "AutoConnect",
1662 DBUS_TYPE_BOOLEAN, &service->favorite);
1664 if (service->name != NULL)
1665 connman_dbus_dict_append_basic(dict, "Name",
1666 DBUS_TYPE_STRING, &service->name);
1668 connman_dbus_dict_append_basic(dict, "LoginRequired",
1669 DBUS_TYPE_BOOLEAN, &service->login_required);
1671 switch (service->type) {
1672 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1673 case CONNMAN_SERVICE_TYPE_SYSTEM:
1674 case CONNMAN_SERVICE_TYPE_GPS:
1675 case CONNMAN_SERVICE_TYPE_VPN:
1676 case CONNMAN_SERVICE_TYPE_GADGET:
1678 case CONNMAN_SERVICE_TYPE_CELLULAR:
1679 connman_dbus_dict_append_basic(dict, "Roaming",
1680 DBUS_TYPE_BOOLEAN, &service->roaming);
1683 connman_dbus_dict_append_basic(dict, "SetupRequired",
1684 DBUS_TYPE_BOOLEAN, &required);
1685 connman_dbus_dict_append_dict(dict, "Ethernet",
1686 append_ethernet, service);
1688 case CONNMAN_SERVICE_TYPE_WIFI:
1689 if (service->passphrase != NULL && limited == FALSE)
1690 connman_dbus_dict_append_basic(dict, "Passphrase",
1691 DBUS_TYPE_STRING, &service->passphrase);
1695 switch (service->security) {
1696 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1697 case CONNMAN_SERVICE_SECURITY_NONE:
1699 case CONNMAN_SERVICE_SECURITY_WEP:
1700 case CONNMAN_SERVICE_SECURITY_PSK:
1701 case CONNMAN_SERVICE_SECURITY_WPA:
1702 case CONNMAN_SERVICE_SECURITY_RSN:
1703 if (service->passphrase == NULL)
1706 case CONNMAN_SERVICE_SECURITY_8021X:
1710 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1711 DBUS_TYPE_BOOLEAN, &required);
1714 case CONNMAN_SERVICE_TYPE_ETHERNET:
1715 case CONNMAN_SERVICE_TYPE_WIMAX:
1716 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1717 connman_dbus_dict_append_dict(dict, "Ethernet",
1718 append_ethernet, service);
1722 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1724 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1725 append_ipv4config, service);
1727 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1729 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1730 append_ipv6config, service);
1732 connman_dbus_dict_append_array(dict, "Nameservers",
1733 DBUS_TYPE_STRING, append_dns, service);
1735 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1736 DBUS_TYPE_STRING, append_dnsconfig, service);
1738 connman_dbus_dict_append_array(dict, "Domains",
1739 DBUS_TYPE_STRING, append_domain, service);
1741 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1742 DBUS_TYPE_STRING, append_domainconfig, service);
1744 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1746 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1747 append_proxyconfig, service);
1749 connman_dbus_dict_append_dict(dict, "Provider",
1750 append_provider, service);
1753 static void append_struct(gpointer value, gpointer user_data)
1755 struct connman_service *service = value;
1756 DBusMessageIter *iter = user_data;
1757 DBusMessageIter entry, dict;
1759 if (service->path == NULL || service->hidden == TRUE)
1762 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1764 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1767 connman_dbus_dict_open(&entry, &dict);
1768 append_properties(&dict, TRUE, service);
1769 connman_dbus_dict_close(&entry, &dict);
1771 dbus_message_iter_close_container(iter, &entry);
1774 void __connman_service_list_struct(DBusMessageIter *iter)
1776 g_sequence_foreach(service_list, append_struct, iter);
1779 int __connman_service_get_index(struct connman_service *service)
1781 if (service == NULL)
1784 if (service->network != NULL)
1785 return connman_network_get_index(service->network);
1786 else if (service->provider != NULL)
1787 return connman_provider_get_index(service->provider);
1792 void __connman_service_set_domainname(struct connman_service *service,
1793 const char *domainname)
1795 if (service == NULL)
1798 g_free(service->domainname);
1799 service->domainname = g_strdup(domainname);
1801 domain_changed(service);
1804 const char *connman_service_get_domainname(struct connman_service *service)
1806 if (service == NULL)
1809 if (service->domains != NULL)
1810 return service->domains[0];
1812 return service->domainname;
1815 char **connman_service_get_nameservers(struct connman_service *service)
1817 if (service == NULL)
1820 if (service->nameservers_config != NULL)
1821 return service->nameservers_config;
1822 else if (service->nameservers != NULL)
1823 return service->nameservers;
1828 void connman_service_set_proxy_method(struct connman_service *service,
1829 enum connman_service_proxy_method method)
1831 if (service == NULL)
1834 service->proxy = method;
1836 proxy_changed(service);
1838 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1839 __connman_notifier_proxy_changed(service);
1842 enum connman_service_proxy_method connman_service_get_proxy_method(
1843 struct connman_service *service)
1845 if (service == NULL)
1846 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1848 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1849 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1850 service->pac == NULL)
1851 return service->proxy;
1853 return service->proxy_config;
1856 return service->proxy;
1859 char **connman_service_get_proxy_servers(struct connman_service *service)
1861 return g_strdupv(service->proxies);
1864 char **connman_service_get_proxy_excludes(struct connman_service *service)
1866 return g_strdupv(service->excludes);
1869 const char *connman_service_get_proxy_url(struct connman_service *service)
1871 if (service == NULL)
1874 return service->pac;
1877 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1880 if (service == NULL)
1883 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1885 if (service->ipconfig_ipv4) {
1886 if (__connman_ipconfig_set_proxy_autoconfig(
1887 service->ipconfig_ipv4, url) < 0)
1889 } else if (service->ipconfig_ipv6) {
1890 if (__connman_ipconfig_set_proxy_autoconfig(
1891 service->ipconfig_ipv6, url) < 0)
1896 proxy_changed(service);
1898 __connman_notifier_proxy_changed(service);
1901 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1903 if (service == NULL)
1906 if (service->ipconfig_ipv4)
1907 return __connman_ipconfig_get_proxy_autoconfig(
1908 service->ipconfig_ipv4);
1909 else if (service->ipconfig_ipv6)
1910 return __connman_ipconfig_get_proxy_autoconfig(
1911 service->ipconfig_ipv6);
1915 static void update_timeservers(struct connman_service *service)
1919 if (service->timeservers == NULL)
1922 switch (service->state) {
1923 case CONNMAN_SERVICE_STATE_UNKNOWN:
1924 case CONNMAN_SERVICE_STATE_IDLE:
1925 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1926 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1928 case CONNMAN_SERVICE_STATE_FAILURE:
1929 case CONNMAN_SERVICE_STATE_DISCONNECT:
1930 for (i = 0; service->timeservers[i] != NULL; i++)
1931 connman_timeserver_remove(service->timeservers[i]);
1933 case CONNMAN_SERVICE_STATE_READY:
1934 case CONNMAN_SERVICE_STATE_ONLINE:
1938 for (i = 0; service->timeservers[i] != NULL; i++)
1939 connman_timeserver_append(service->timeservers[i]);
1942 int __connman_service_timeserver_append(struct connman_service *service,
1943 const char *timeserver)
1947 DBG("service %p timeserver %s", service, timeserver);
1949 if (timeserver == NULL)
1952 if (service->timeservers != NULL) {
1955 for (i = 0; service->timeservers[i] != NULL; i++)
1956 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1959 len = g_strv_length(service->timeservers);
1960 service->timeservers = g_try_renew(char *, service->timeservers,
1964 service->timeservers = g_try_new0(char *, len + 2);
1967 if (service->timeservers == NULL)
1970 service->timeservers[len] = g_strdup(timeserver);
1971 service->timeservers[len + 1] = NULL;
1973 update_timeservers(service);
1978 int __connman_service_timeserver_remove(struct connman_service *service,
1979 const char *timeserver)
1984 DBG("service %p timeserver %s", service, timeserver);
1986 if (timeserver == NULL)
1989 if (service->timeservers == NULL)
1992 len = g_strv_length(service->timeservers);
1994 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
1997 g_strfreev(service->timeservers);
1998 service->timeservers = NULL;
2003 servers = g_try_new0(char *, len - 1);
2004 if (servers == NULL)
2007 for (i = 0, j = 0; i < len; i++) {
2008 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2009 servers[j] = g_strdup(service->timeservers[i]);
2013 servers[len - 2] = NULL;
2015 g_strfreev(service->timeservers);
2016 service->timeservers = servers;
2018 update_timeservers(service);
2023 void __connman_service_set_pac(struct connman_service *service,
2029 g_free(service->pac);
2030 service->pac = g_strdup(pac);
2033 void __connman_service_set_identity(struct connman_service *service,
2034 const char *identity)
2036 if (service->immutable)
2039 g_free(service->identity);
2040 service->identity = g_strdup(identity);
2042 if (service->network != NULL)
2043 connman_network_set_string(service->network,
2048 void __connman_service_set_passphrase(struct connman_service *service,
2049 const char* passphrase)
2051 if (service->immutable == TRUE)
2054 g_free(service->passphrase);
2055 service->passphrase = g_strdup(passphrase);
2057 passphrase_changed(service);
2059 if (service->network != NULL)
2060 connman_network_set_string(service->network,
2062 service->passphrase);
2064 __connman_storage_save_service(service);
2067 static DBusMessage *get_properties(DBusConnection *conn,
2068 DBusMessage *msg, void *user_data)
2070 struct connman_service *service = user_data;
2072 DBusMessageIter array, dict;
2074 DBG("service %p", service);
2076 reply = dbus_message_new_method_return(msg);
2080 dbus_message_iter_init_append(reply, &array);
2082 connman_dbus_dict_open(&array, &dict);
2083 append_properties(&dict, FALSE, service);
2084 connman_dbus_dict_close(&array, &dict);
2089 static int update_proxy_configuration(struct connman_service *service,
2090 DBusMessageIter *array)
2092 DBusMessageIter dict;
2093 enum connman_service_proxy_method method;
2094 GString *servers_str = NULL;
2095 GString *excludes_str = NULL;
2096 const char *url = NULL;
2098 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2100 dbus_message_iter_recurse(array, &dict);
2102 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2103 DBusMessageIter entry, variant;
2107 dbus_message_iter_recurse(&dict, &entry);
2109 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2112 dbus_message_iter_get_basic(&entry, &key);
2113 dbus_message_iter_next(&entry);
2115 if (dbus_message_iter_get_arg_type(&entry) !=
2119 dbus_message_iter_recurse(&entry, &variant);
2120 type = dbus_message_iter_get_arg_type(&variant);
2122 if (g_str_equal(key, "Method") == TRUE) {
2125 if (type != DBUS_TYPE_STRING)
2128 dbus_message_iter_get_basic(&variant, &val);
2129 method = string2proxymethod(val);
2130 } else if (g_str_equal(key, "URL") == TRUE) {
2131 if (type != DBUS_TYPE_STRING)
2134 dbus_message_iter_get_basic(&variant, &url);
2135 } else if (g_str_equal(key, "Servers") == TRUE) {
2136 DBusMessageIter str_array;
2138 if (type != DBUS_TYPE_ARRAY)
2141 servers_str = g_string_new(NULL);
2142 if (servers_str == NULL)
2145 dbus_message_iter_recurse(&variant, &str_array);
2147 while (dbus_message_iter_get_arg_type(&str_array) ==
2151 dbus_message_iter_get_basic(&str_array, &val);
2153 if (servers_str->len > 0)
2154 g_string_append_printf(servers_str,
2157 g_string_append(servers_str, val);
2159 dbus_message_iter_next(&str_array);
2161 } else if (g_str_equal(key, "Excludes") == TRUE) {
2162 DBusMessageIter str_array;
2164 if (type != DBUS_TYPE_ARRAY)
2167 excludes_str = g_string_new(NULL);
2168 if (excludes_str == NULL)
2171 dbus_message_iter_recurse(&variant, &str_array);
2173 while (dbus_message_iter_get_arg_type(&str_array) ==
2177 dbus_message_iter_get_basic(&str_array, &val);
2179 if (excludes_str->len > 0)
2180 g_string_append_printf(excludes_str,
2183 g_string_append(excludes_str, val);
2185 dbus_message_iter_next(&str_array);
2189 dbus_message_iter_next(&dict);
2193 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2195 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2196 if (servers_str == NULL && service->proxies == NULL)
2199 if (servers_str != NULL) {
2200 g_strfreev(service->proxies);
2202 if (servers_str->len > 0)
2203 service->proxies = g_strsplit_set(
2204 servers_str->str, " ", 0);
2206 service->proxies = NULL;
2209 if (excludes_str != NULL) {
2210 g_strfreev(service->excludes);
2212 if (excludes_str->len > 0)
2213 service->excludes = g_strsplit_set(
2214 excludes_str->str, " ", 0);
2216 service->excludes = NULL;
2219 if (service->proxies == NULL)
2220 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2223 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2224 g_free(service->pac);
2226 if (url != NULL && strlen(url) > 0)
2227 service->pac = g_strdup(url);
2229 service->pac = NULL;
2231 /* if we are connected:
2232 - if service->pac == NULL
2233 - if __connman_ipconfig_get_proxy_autoconfig(
2234 service->ipconfig) == NULL
2235 --> We should start WPAD */
2238 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2242 if (servers_str != NULL)
2243 g_string_free(servers_str, TRUE);
2245 if (excludes_str != NULL)
2246 g_string_free(excludes_str, TRUE);
2248 service->proxy_config = method;
2253 if (servers_str != NULL)
2254 g_string_free(servers_str, TRUE);
2256 if (excludes_str != NULL)
2257 g_string_free(excludes_str, TRUE);
2262 static int set_ipconfig(struct connman_service *service,
2263 struct connman_ipconfig *ipconfig,
2264 DBusMessageIter *array,
2265 enum connman_service_state state,
2266 enum connman_service_state *new_state)
2268 enum connman_ipconfig_method old_method;
2269 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2270 enum connman_ipconfig_type type;
2273 old_method = __connman_ipconfig_get_method(ipconfig);
2275 if (is_connecting_state(service, state) ||
2276 is_connected_state(service, state))
2277 __connman_network_clear_ipconfig(service->network, ipconfig);
2279 err = __connman_ipconfig_set_config(ipconfig, array);
2280 method = __connman_ipconfig_get_method(ipconfig);
2281 type = __connman_ipconfig_get_config_type(ipconfig);
2283 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2284 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2285 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2286 *new_state = service->state_ipv4 =
2287 CONNMAN_SERVICE_STATE_CONFIGURATION;
2288 __connman_ipconfig_enable(ipconfig);
2291 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2292 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2293 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2294 *new_state = service->state_ipv6;
2295 __connman_ipconfig_enable(ipconfig);
2299 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2300 type, method, state2string(*new_state));
2305 static DBusMessage *set_property(DBusConnection *conn,
2306 DBusMessage *msg, void *user_data)
2308 struct connman_service *service = user_data;
2309 DBusMessageIter iter, value;
2313 DBG("service %p", service);
2315 if (dbus_message_iter_init(msg, &iter) == FALSE)
2316 return __connman_error_invalid_arguments(msg);
2318 dbus_message_iter_get_basic(&iter, &name);
2319 dbus_message_iter_next(&iter);
2320 dbus_message_iter_recurse(&iter, &value);
2322 type = dbus_message_iter_get_arg_type(&value);
2324 if (g_str_equal(name, "AutoConnect") == TRUE) {
2325 connman_bool_t autoconnect;
2327 if (type != DBUS_TYPE_BOOLEAN)
2328 return __connman_error_invalid_arguments(msg);
2330 if (service->favorite == FALSE)
2331 return __connman_error_invalid_service(msg);
2333 dbus_message_iter_get_basic(&value, &autoconnect);
2335 if (service->autoconnect == autoconnect)
2336 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2338 service->autoconnect = autoconnect;
2340 autoconnect_changed(service);
2342 __connman_storage_save_service(service);
2343 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2344 const char *passphrase;
2346 if (type != DBUS_TYPE_STRING)
2347 return __connman_error_invalid_arguments(msg);
2349 if (service->immutable == TRUE)
2350 return __connman_error_not_supported(msg);
2352 dbus_message_iter_get_basic(&value, &passphrase);
2354 __connman_service_set_passphrase(service, passphrase);
2355 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2356 DBusMessageIter entry;
2361 if (type != DBUS_TYPE_ARRAY)
2362 return __connman_error_invalid_arguments(msg);
2364 str = g_string_new(NULL);
2366 return __connman_error_invalid_arguments(msg);
2368 index = connman_network_get_index(service->network);
2369 gw = __connman_ipconfig_get_gateway_from_index(index);
2371 if (gw && strlen(gw))
2372 __connman_service_nameserver_del_routes(service);
2374 dbus_message_iter_recurse(&value, &entry);
2376 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2378 dbus_message_iter_get_basic(&entry, &val);
2379 dbus_message_iter_next(&entry);
2381 g_string_append_printf(str, " %s", val);
2383 g_string_append(str, val);
2386 g_strfreev(service->nameservers_config);
2389 service->nameservers_config =
2390 g_strsplit_set(str->str, " ", 0);
2392 service->nameservers_config = NULL;
2395 g_string_free(str, TRUE);
2397 if (gw && strlen(gw))
2398 __connman_service_nameserver_add_routes(service, gw);
2400 update_nameservers(service);
2401 dns_configuration_changed(service);
2403 __connman_storage_save_service(service);
2404 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2405 DBusMessageIter entry;
2408 if (type != DBUS_TYPE_ARRAY)
2409 return __connman_error_invalid_arguments(msg);
2411 str = g_string_new(NULL);
2413 return __connman_error_invalid_arguments(msg);
2415 dbus_message_iter_recurse(&value, &entry);
2417 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2419 dbus_message_iter_get_basic(&entry, &val);
2420 dbus_message_iter_next(&entry);
2422 g_string_append_printf(str, " %s", val);
2424 g_string_append(str, val);
2427 g_strfreev(service->domains);
2430 service->domains = g_strsplit_set(str->str, " ", 0);
2432 service->domains = NULL;
2434 g_string_free(str, TRUE);
2436 update_nameservers(service);
2437 domain_configuration_changed(service);
2439 __connman_storage_save_service(service);
2440 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2443 if (type != DBUS_TYPE_ARRAY)
2444 return __connman_error_invalid_arguments(msg);
2446 err = update_proxy_configuration(service, &value);
2449 return __connman_error_failed(msg, -err);
2451 proxy_configuration_changed(service);
2453 __connman_storage_save_service(service);
2454 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2455 g_str_equal(name, "IPv6.Configuration")) {
2457 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2458 enum connman_service_state state =
2459 CONNMAN_SERVICE_STATE_UNKNOWN;
2464 if (service->ipconfig_ipv4 == NULL &&
2465 service->ipconfig_ipv6 == NULL)
2466 return __connman_error_invalid_property(msg);
2468 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2469 ipv4 = service->ipconfig_ipv4;
2470 err = set_ipconfig(service, ipv4, &value,
2471 service->state_ipv4, &state);
2473 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2474 ipv6 = service->ipconfig_ipv6;
2475 err = set_ipconfig(service, ipv6, &value,
2476 service->state_ipv6, &state);
2480 if (is_connected_state(service, state) ||
2481 is_connecting_state(service, state))
2482 __connman_network_set_ipconfig(service->network,
2484 return __connman_error_failed(msg, -err);
2488 ipv4_configuration_changed(service);
2490 ipv6_configuration_changed(service);
2492 if (is_connecting(service) || is_connected(service))
2493 __connman_network_set_ipconfig(service->network,
2496 __connman_storage_save_service(service);
2498 return __connman_error_invalid_property(msg);
2500 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2503 static void set_idle(struct connman_service *service)
2505 service->state = service->state_ipv4 = service->state_ipv6 =
2506 CONNMAN_SERVICE_STATE_IDLE;
2507 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2508 state_changed(service);
2511 static DBusMessage *clear_property(DBusConnection *conn,
2512 DBusMessage *msg, void *user_data)
2514 struct connman_service *service = user_data;
2517 DBG("service %p", service);
2519 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2522 if (g_str_equal(name, "Error") == TRUE) {
2525 g_get_current_time(&service->modified);
2526 __connman_storage_save_service(service);
2527 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2528 if (service->immutable == TRUE)
2529 return __connman_error_not_supported(msg);
2531 g_free(service->passphrase);
2532 service->passphrase = NULL;
2534 passphrase_changed(service);
2536 __connman_storage_save_service(service);
2538 return __connman_error_invalid_property(msg);
2540 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2543 static connman_bool_t is_ignore(struct connman_service *service)
2545 if (service->autoconnect == FALSE)
2548 if (service->roaming == TRUE)
2551 if (service->ignore == TRUE)
2554 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2560 void __connman_service_auto_connect(void)
2562 struct connman_service *service = NULL;
2563 GSequenceIter *iter;
2567 if (__connman_session_mode() == TRUE) {
2568 DBG("Session mode enabled: auto connect disabled");
2572 iter = g_sequence_get_begin_iter(service_list);
2574 while (g_sequence_iter_is_end(iter) == FALSE) {
2575 service = g_sequence_get(iter);
2577 if (service->pending != NULL)
2580 if (is_connecting(service) == TRUE)
2583 if (service->favorite == FALSE)
2586 if (is_connected(service) == TRUE)
2589 if (is_ignore(service) == FALSE && service->state ==
2590 CONNMAN_SERVICE_STATE_IDLE)
2595 iter = g_sequence_iter_next(iter);
2598 if (service != NULL) {
2599 service->userconnect = FALSE;
2600 __connman_service_connect(service);
2604 static void remove_timeout(struct connman_service *service)
2606 if (service->timeout > 0) {
2607 g_source_remove(service->timeout);
2608 service->timeout = 0;
2612 static void reply_pending(struct connman_service *service, int error)
2614 remove_timeout(service);
2616 if (service->pending != NULL) {
2620 reply = __connman_error_failed(service->pending,
2623 g_dbus_send_message(connection, reply);
2627 sender = dbus_message_get_interface(service->pending);
2629 DBG("sender %s", sender);
2631 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2632 g_dbus_send_reply(connection, service->pending,
2633 DBUS_TYPE_OBJECT_PATH, &service->path,
2636 g_dbus_send_reply(connection, service->pending,
2640 dbus_message_unref(service->pending);
2641 service->pending = NULL;
2645 static gboolean connect_timeout(gpointer user_data)
2647 struct connman_service *service = user_data;
2648 connman_bool_t autoconnect = FALSE;
2650 DBG("service %p", service);
2652 service->timeout = 0;
2654 if (service->network != NULL)
2655 __connman_network_disconnect(service->network);
2657 __connman_ipconfig_disable(service->ipconfig_ipv4);
2658 __connman_ipconfig_disable(service->ipconfig_ipv6);
2660 __connman_stats_service_unregister(service);
2662 if (service->pending != NULL) {
2665 reply = __connman_error_operation_timeout(service->pending);
2667 g_dbus_send_message(connection, reply);
2669 dbus_message_unref(service->pending);
2670 service->pending = NULL;
2674 __connman_service_ipconfig_indicate_state(service,
2675 CONNMAN_SERVICE_STATE_FAILURE,
2676 CONNMAN_IPCONFIG_TYPE_IPV4);
2677 __connman_service_ipconfig_indicate_state(service,
2678 CONNMAN_SERVICE_STATE_FAILURE,
2679 CONNMAN_IPCONFIG_TYPE_IPV6);
2681 if (autoconnect == TRUE && service->userconnect == FALSE)
2682 __connman_service_auto_connect();
2687 static void set_reconnect_state(struct connman_service *service,
2688 connman_bool_t reconnect)
2690 struct connman_device *device;
2692 if (service->network == NULL)
2695 device = connman_network_get_device(service->network);
2699 __connman_device_set_reconnect(device, reconnect);
2702 static connman_bool_t get_reconnect_state(struct connman_service *service)
2704 struct connman_device *device;
2706 if (service->network == NULL)
2709 device = connman_network_get_device(service->network);
2713 return __connman_device_get_reconnect(device);
2716 static void request_input_cb (struct connman_service *service,
2717 const char *identity, const char *passphrase,
2720 DBG ("RequestInput return, %p", service);
2722 if (identity == NULL && passphrase == NULL && service->wps == FALSE)
2725 if (identity != NULL)
2726 __connman_service_set_identity(service, identity);
2728 if (passphrase != NULL)
2729 __connman_service_set_passphrase(service, passphrase);
2731 __connman_service_connect(service);
2734 static DBusMessage *connect_service(DBusConnection *conn,
2735 DBusMessage *msg, void *user_data)
2737 struct connman_service *service = user_data;
2738 GSequenceIter *iter;
2741 DBG("service %p", service);
2743 if (service->pending != NULL)
2744 return __connman_error_in_progress(msg);
2746 iter = g_sequence_get_begin_iter(service_list);
2748 while (g_sequence_iter_is_end(iter) == FALSE) {
2749 struct connman_service *temp = g_sequence_get(iter);
2751 if (service->type == temp->type && is_connecting(temp) == TRUE)
2752 return __connman_error_in_progress(msg);
2754 iter = g_sequence_iter_next(iter);
2757 service->ignore = FALSE;
2759 service->userconnect = TRUE;
2761 service->pending = dbus_message_ref(msg);
2763 set_reconnect_state(service, FALSE);
2765 err = __connman_service_connect(service);
2767 if (err == -ENOKEY) {
2768 if (__connman_agent_request_input(service,
2774 if (service->pending == NULL)
2777 if (err != -EINPROGRESS) {
2778 dbus_message_unref(service->pending);
2779 service->pending = NULL;
2781 return __connman_error_failed(msg, -err);
2787 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2790 static DBusMessage *disconnect_service(DBusConnection *conn,
2791 DBusMessage *msg, void *user_data)
2793 struct connman_service *service = user_data;
2796 DBG("service %p", service);
2798 reply_pending(service, ECONNABORTED);
2800 service->ignore = TRUE;
2802 set_reconnect_state(service, FALSE);
2804 err = __connman_service_disconnect(service);
2806 if (err != -EINPROGRESS)
2807 return __connman_error_failed(msg, -err);
2812 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2815 static DBusMessage *remove_service(DBusConnection *conn,
2816 DBusMessage *msg, void *user_data)
2818 struct connman_service *service = user_data;
2820 DBG("service %p", service);
2822 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2823 return __connman_error_not_supported(msg);
2825 if (service->immutable == TRUE)
2826 return __connman_error_not_supported(msg);
2828 if (service->favorite == FALSE && service->state !=
2829 CONNMAN_SERVICE_STATE_FAILURE)
2830 return __connman_error_not_supported(msg);
2832 if (service->network != NULL) {
2833 set_reconnect_state(service, FALSE);
2835 __connman_network_disconnect(service->network);
2838 g_free(service->passphrase);
2839 service->passphrase = NULL;
2841 passphrase_changed(service);
2845 __connman_service_set_favorite(service, FALSE);
2846 __connman_storage_save_service(service);
2848 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2851 static DBusMessage *move_service(DBusConnection *conn,
2852 DBusMessage *msg, void *user_data,
2855 struct connman_service *service = user_data;
2856 struct connman_service *target;
2858 GSequenceIter *src, *dst;
2860 DBG("service %p", service);
2862 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2865 if (service->favorite == FALSE)
2866 return __connman_error_not_supported(msg);
2868 target = find_service(path);
2869 if (target == NULL || target->favorite == FALSE || target == service)
2870 return __connman_error_invalid_service(msg);
2872 DBG("target %s", target->identifier);
2874 if (target->state_ipv4 != service->state_ipv4 &&
2875 target->state_ipv6 != service->state_ipv6)
2876 return __connman_error_invalid_service(msg);
2878 g_get_current_time(&service->modified);
2879 __connman_storage_save_service(service);
2881 src = g_hash_table_lookup(service_hash, service->identifier);
2882 dst = g_hash_table_lookup(service_hash, target->identifier);
2884 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2886 __connman_profile_changed(FALSE);
2888 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2891 static DBusMessage *move_before(DBusConnection *conn,
2892 DBusMessage *msg, void *user_data)
2894 return move_service(conn, msg, user_data, TRUE);
2897 static DBusMessage *move_after(DBusConnection *conn,
2898 DBusMessage *msg, void *user_data)
2900 return move_service(conn, msg, user_data, FALSE);
2903 static DBusMessage *reset_counters(DBusConnection *conn,
2904 DBusMessage *msg, void *user_data)
2906 struct connman_service *service = user_data;
2908 reset_stats(service);
2910 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2913 static GDBusMethodTable service_methods[] = {
2914 { "GetProperties", "", "a{sv}", get_properties },
2915 { "SetProperty", "sv", "", set_property },
2916 { "ClearProperty", "s", "", clear_property },
2917 { "Connect", "", "", connect_service,
2918 G_DBUS_METHOD_FLAG_ASYNC },
2919 { "Disconnect", "", "", disconnect_service },
2920 { "Remove", "", "", remove_service },
2921 { "MoveBefore", "o", "", move_before },
2922 { "MoveAfter", "o", "", move_after },
2923 { "ResetCounters", "", "", reset_counters },
2927 static GDBusSignalTable service_signals[] = {
2928 { "PropertyChanged", "sv" },
2932 static void service_free(gpointer user_data)
2934 struct connman_service *service = user_data;
2935 char *path = service->path;
2937 DBG("service %p", service);
2939 reply_pending(service, ENOENT);
2941 g_hash_table_remove(service_hash, service->identifier);
2943 __connman_notifier_service_remove(service);
2945 stats_stop(service);
2946 __connman_storage_save_service(service);
2948 service->path = NULL;
2951 __connman_profile_changed(FALSE);
2953 g_dbus_unregister_interface(connection, path,
2954 CONNMAN_SERVICE_INTERFACE);
2958 g_hash_table_destroy(service->counter_table);
2960 if (service->network != NULL) {
2961 if (service->network_created == TRUE)
2962 connman_network_unregister(service->network);
2963 connman_network_unref(service->network);
2966 if (service->provider != NULL)
2967 connman_provider_unref(service->provider);
2969 if (service->ipconfig_ipv4 != NULL) {
2970 connman_ipconfig_unref(service->ipconfig_ipv4);
2971 service->ipconfig_ipv4 = NULL;
2974 if (service->ipconfig_ipv6 != NULL) {
2975 connman_ipconfig_unref(service->ipconfig_ipv6);
2976 service->ipconfig_ipv6 = NULL;
2979 if (service->location != NULL)
2980 connman_location_unref(service->location);
2982 g_strfreev(service->nameservers);
2983 g_strfreev(service->nameservers_config);
2984 g_strfreev(service->domains);
2985 g_strfreev(service->proxies);
2986 g_strfreev(service->excludes);
2988 g_free(service->domainname);
2989 g_free(service->pac);
2990 g_free(service->profile);
2991 g_free(service->name);
2992 g_free(service->passphrase);
2993 g_free(service->identifier);
2994 g_free(service->eap);
2995 g_free(service->identity);
2996 g_free(service->ca_cert_file);
2997 g_free(service->client_cert_file);
2998 g_free(service->private_key_file);
2999 g_free(service->private_key_passphrase);
3000 g_free(service->phase2);
3002 if (service->stats.timer != NULL)
3003 g_timer_destroy(service->stats.timer);
3004 if (service->stats_roaming.timer != NULL)
3005 g_timer_destroy(service->stats_roaming.timer);
3011 * __connman_service_put:
3012 * @service: service structure
3014 * Release service if no longer needed
3016 void __connman_service_put(struct connman_service *service)
3018 DBG("service %p", service);
3020 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
3021 GSequenceIter *iter;
3023 iter = g_hash_table_lookup(service_hash, service->identifier);
3025 reply_pending(service, ECONNABORTED);
3027 __connman_service_disconnect(service);
3029 g_sequence_remove(iter);
3031 service_free(service);
3035 static void stats_init(struct connman_service *service)
3038 service->stats.valid = FALSE;
3039 service->stats.enabled = FALSE;
3040 service->stats.timer = g_timer_new();
3043 service->stats_roaming.valid = FALSE;
3044 service->stats_roaming.enabled = FALSE;
3045 service->stats_roaming.timer = g_timer_new();
3048 static void service_initialize(struct connman_service *service)
3050 DBG("service %p", service);
3052 service->refcount = 1;
3053 service->session_usage_count = 0;
3055 service->network_created = FALSE;
3057 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3058 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3060 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3061 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3062 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3064 service->favorite = FALSE;
3065 service->immutable = FALSE;
3066 service->hidden = FALSE;
3068 service->ignore = FALSE;
3070 service->userconnect = FALSE;
3074 stats_init(service);
3076 service->provider = NULL;
3078 service->wps = FALSE;
3082 * connman_service_create:
3084 * Allocate a new service.
3086 * Returns: a newly-allocated #connman_service structure
3088 struct connman_service *connman_service_create(void)
3091 struct connman_stats_counter *counters;
3092 const char *counter;
3094 struct connman_service *service;
3096 service = g_try_new0(struct connman_service, 1);
3097 if (service == NULL)
3100 DBG("service %p", service);
3102 service->counter_table = g_hash_table_new_full(g_str_hash,
3103 g_str_equal, NULL, g_free);
3105 for (list = counter_list; list; list = list->next) {
3106 counter = list->data;
3108 counters = g_try_new0(struct connman_stats_counter, 1);
3109 if (counters == NULL) {
3110 g_hash_table_destroy(service->counter_table);
3115 counters->append_all = TRUE;
3117 g_hash_table_replace(service->counter_table, (gpointer)counter,
3121 service_initialize(service);
3123 service->location = __connman_location_create(service);
3128 struct connman_location *__connman_service_get_location(struct connman_service *service)
3130 return service->location;
3134 * connman_service_ref:
3135 * @service: service structure
3137 * Increase reference counter of service
3139 struct connman_service *connman_service_ref(struct connman_service *service)
3143 g_atomic_int_inc(&service->refcount);
3149 * connman_service_unref:
3150 * @service: service structure
3152 * Decrease reference counter of service
3154 void connman_service_unref(struct connman_service *service)
3156 __connman_service_put(service);
3159 static gint service_compare(gconstpointer a, gconstpointer b,
3162 struct connman_service *service_a = (void *) a;
3163 struct connman_service *service_b = (void *) b;
3164 enum connman_service_state state_a, state_b;
3166 state_a = service_a->state;
3167 state_b = service_b->state;
3169 if (state_a != state_b) {
3170 if (is_connected(service_a) == TRUE)
3172 if (is_connected(service_b) == TRUE)
3175 if (is_connecting(service_a) == TRUE)
3177 if (is_connecting(service_b) == TRUE)
3181 if (service_a->order > service_b->order)
3184 if (service_a->order < service_b->order)
3187 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3190 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3193 if (service_a->type != service_b->type) {
3194 switch (service_a->type) {
3195 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3196 case CONNMAN_SERVICE_TYPE_SYSTEM:
3197 case CONNMAN_SERVICE_TYPE_ETHERNET:
3198 case CONNMAN_SERVICE_TYPE_GPS:
3199 case CONNMAN_SERVICE_TYPE_VPN:
3200 case CONNMAN_SERVICE_TYPE_GADGET:
3202 case CONNMAN_SERVICE_TYPE_WIFI:
3204 case CONNMAN_SERVICE_TYPE_WIMAX:
3205 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3206 case CONNMAN_SERVICE_TYPE_CELLULAR:
3211 return (gint) service_b->strength - (gint) service_a->strength;
3215 * connman_service_get_type:
3216 * @service: service structure
3218 * Get the type of service
3220 enum connman_service_type connman_service_get_type(struct connman_service *service)
3222 if (service == NULL)
3223 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3225 return service->type;
3229 * connman_service_get_interface:
3230 * @service: service structure
3232 * Get network interface of service
3234 char *connman_service_get_interface(struct connman_service *service)
3238 if (service == NULL)
3241 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3242 if (service->ipconfig_ipv4)
3243 index = connman_ipconfig_get_index(
3244 service->ipconfig_ipv4);
3245 else if (service->ipconfig_ipv6)
3246 index = connman_ipconfig_get_index(
3247 service->ipconfig_ipv6);
3251 return connman_inet_ifname(index);
3254 if (service->network == NULL)
3257 index = connman_network_get_index(service->network);
3259 return connman_inet_ifname(index);
3263 * connman_service_get_network:
3264 * @service: service structure
3266 * Get the service network
3268 struct connman_network *
3269 __connman_service_get_network(struct connman_service *service)
3271 if (service == NULL)
3274 return service->network;
3277 struct connman_ipconfig *
3278 __connman_service_get_ip4config(struct connman_service *service)
3280 if (service == NULL)
3283 return service->ipconfig_ipv4;
3286 struct connman_ipconfig *
3287 __connman_service_get_ip6config(struct connman_service *service)
3289 if (service == NULL)
3292 return service->ipconfig_ipv6;
3295 struct connman_ipconfig *
3296 __connman_service_get_ipconfig(struct connman_service *service, int family)
3298 if (family == AF_INET)
3299 return __connman_service_get_ip4config(service);
3300 else if (family == AF_INET6)
3301 return __connman_service_get_ip6config(service);
3307 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3309 if (service == NULL)
3310 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3312 return service->security;
3315 const char *__connman_service_get_phase2(struct connman_service *service)
3317 if (service == NULL)
3320 return service->phase2;
3323 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3325 if (service == NULL)
3328 return service->wps;
3332 * __connman_service_set_favorite:
3333 * @service: service structure
3334 * @favorite: favorite value
3336 * Change the favorite setting of service
3338 int __connman_service_set_favorite(struct connman_service *service,
3339 connman_bool_t favorite)
3341 GSequenceIter *iter;
3343 iter = g_hash_table_lookup(service_hash, service->identifier);
3347 if (service->favorite == favorite)
3350 service->favorite = favorite;
3352 favorite_changed(service);
3354 g_sequence_sort_changed(iter, service_compare, NULL);
3356 __connman_profile_changed(FALSE);
3361 int __connman_service_set_immutable(struct connman_service *service,
3362 connman_bool_t immutable)
3364 service->immutable = immutable;
3366 immutable_changed(service);
3371 void __connman_service_set_string(struct connman_service *service,
3372 const char *key, const char *value)
3374 if (g_str_equal(key, "EAP") == TRUE) {
3375 g_free(service->eap);
3376 service->eap = g_strdup(value);
3377 } else if (g_str_equal(key, "Identity") == TRUE) {
3378 g_free(service->identity);
3379 service->identity = g_strdup(value);
3380 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3381 g_free(service->ca_cert_file);
3382 service->ca_cert_file = g_strdup(value);
3383 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3384 g_free(service->client_cert_file);
3385 service->client_cert_file = g_strdup(value);
3386 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3387 g_free(service->private_key_file);
3388 service->private_key_file = g_strdup(value);
3389 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3390 g_free(service->private_key_passphrase);
3391 service->private_key_passphrase = g_strdup(value);
3392 } else if (g_str_equal(key, "Phase2") == TRUE) {
3393 g_free(service->phase2);
3394 service->phase2 = g_strdup(value);
3395 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3396 g_free(service->passphrase);
3397 service->passphrase = g_strdup(value);
3401 static void service_complete(struct connman_service *service)
3403 reply_pending(service, EIO);
3405 if (service->userconnect == FALSE)
3406 __connman_service_auto_connect();
3408 g_get_current_time(&service->modified);
3409 __connman_storage_save_service(service);
3412 static void report_error_cb(struct connman_service *service,
3413 gboolean retry, void *user_data)
3416 __connman_service_connect(service);
3418 service_complete(service);
3419 __connman_profile_changed(FALSE);
3420 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3424 static int __connman_service_indicate_state(struct connman_service *service)
3426 enum connman_service_state old_state, new_state;
3427 GSequenceIter *iter;
3429 if (service == NULL)
3432 old_state = service->state;
3433 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3435 if (old_state == new_state)
3438 DBG("service %p old %s - new %s/%s => %s",
3440 state2string(old_state),
3441 state2string(service->state_ipv4),
3442 state2string(service->state_ipv6),
3443 state2string(new_state));
3445 service->state = new_state;
3446 state_changed(service);
3448 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3449 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3450 reply_pending(service, ECONNABORTED);
3452 __connman_service_disconnect(service);
3455 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3456 if (__connman_stats_service_register(service) == 0) {
3457 __connman_stats_get(service, FALSE,
3458 &service->stats.data);
3459 __connman_stats_get(service, TRUE,
3460 &service->stats_roaming.data);
3464 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3465 if (service->login_required == TRUE) {
3466 service->login_required = FALSE;
3467 login_changed(service);
3470 connman_timeserver_sync();
3475 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
3476 connman_bool_t reconnect;
3478 reconnect = get_reconnect_state(service);
3479 if (reconnect == TRUE)
3480 __connman_service_auto_connect();
3482 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3485 if (new_state == CONNMAN_SERVICE_STATE_READY) {
3486 enum connman_ipconfig_method method;
3488 set_reconnect_state(service, TRUE);
3490 __connman_service_set_favorite(service, TRUE);
3492 reply_pending(service, 0);
3494 service->userconnect = FALSE;
3496 g_get_current_time(&service->modified);
3497 __connman_storage_save_service(service);
3499 update_nameservers(service);
3500 dns_changed(service);
3501 domain_changed(service);
3503 __connman_notifier_connect(service->type);
3505 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3506 connman_network_get_bool(service->network,
3507 "WiFi.UseWPS") == TRUE) {
3510 pass = connman_network_get_string(service->network,
3513 __connman_service_set_passphrase(service, pass);
3515 connman_network_set_bool(service->network,
3516 "WiFi.UseWPS", FALSE);
3521 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3522 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3523 __connman_ipconfig_disable_ipv6(
3524 service->ipconfig_ipv6);
3526 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3527 struct connman_service *def_service = get_default();
3529 if (__connman_notifier_count_connected() == 0 &&
3530 def_service != NULL &&
3531 def_service->provider != NULL)
3532 __connman_provider_disconnect(def_service->provider);
3534 __connman_location_finish(service);
3538 __connman_wpad_stop(service);
3540 update_nameservers(service);
3541 dns_changed(service);
3542 domain_changed(service);
3544 __connman_notifier_disconnect(service->type);
3547 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
3548 if (service->userconnect == TRUE &&
3549 __connman_agent_report_error(service,
3550 error2string(service->error),
3551 report_error_cb, NULL) == -EIO)
3553 service_complete(service);
3555 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3557 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3559 iter = g_hash_table_lookup(service_hash, service->identifier);
3561 g_sequence_sort_changed(iter, service_compare, NULL);
3563 __connman_profile_changed(FALSE);
3568 int __connman_service_indicate_error(struct connman_service *service,
3569 enum connman_service_error error)
3571 DBG("service %p error %d", service, error);
3573 if (service == NULL)
3576 service->error = error;
3578 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3579 __connman_service_set_passphrase(service, NULL);
3581 return __connman_service_ipconfig_indicate_state(service,
3582 CONNMAN_SERVICE_STATE_FAILURE,
3583 CONNMAN_IPCONFIG_TYPE_IPV4);
3586 int __connman_service_clear_error(struct connman_service *service)
3588 DBG("service %p", service);
3590 if (service == NULL)
3593 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
3596 service->state_ipv4 = service->state_ipv6 =
3597 CONNMAN_SERVICE_STATE_UNKNOWN;
3598 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3600 if (service->favorite == TRUE)
3601 set_reconnect_state(service, TRUE);
3603 __connman_service_ipconfig_indicate_state(service,
3604 CONNMAN_SERVICE_STATE_IDLE,
3605 CONNMAN_IPCONFIG_TYPE_IPV6);
3608 * Toggling the IPv6 state to IDLE could trigger the auto connect
3609 * machinery and consequently the IPv4 state.
3611 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3612 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3615 return __connman_service_ipconfig_indicate_state(service,
3616 CONNMAN_SERVICE_STATE_IDLE,
3617 CONNMAN_IPCONFIG_TYPE_IPV4);
3620 int __connman_service_indicate_default(struct connman_service *service)
3622 DBG("service %p", service);
3626 __connman_location_detect(service);
3631 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
3632 enum connman_service_state new_state,
3633 enum connman_ipconfig_type type)
3635 struct connman_ipconfig *ipconfig = NULL;
3636 enum connman_service_state old_state;
3637 enum connman_service_proxy_method proxy_config;
3639 if (service == NULL)
3642 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3643 old_state = service->state_ipv4;
3644 ipconfig = service->ipconfig_ipv4;
3645 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3646 old_state = service->state_ipv6;
3647 ipconfig = service->ipconfig_ipv6;
3650 if (ipconfig == NULL)
3654 if (old_state == new_state)
3657 DBG("service %p (%s) state %d (%s) type %d (%s)",
3658 service, service ? service->identifier : NULL,
3659 new_state, state2string(new_state),
3660 type, __connman_ipconfig_type2string(type));
3662 switch (new_state) {
3663 case CONNMAN_SERVICE_STATE_UNKNOWN:
3664 case CONNMAN_SERVICE_STATE_IDLE:
3665 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3668 case CONNMAN_SERVICE_STATE_ASSOCIATION:
3670 case CONNMAN_SERVICE_STATE_CONFIGURATION:
3671 __connman_ipconfig_enable(ipconfig);
3673 case CONNMAN_SERVICE_STATE_READY:
3674 update_nameservers(service);
3676 proxy_config = service->proxy_config;
3679 * We start WPAD if we haven't got a PAC URL from DHCP and
3680 * if our proxy manual configuration is either empty or set
3681 * to AUTO with an empty URL.
3683 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3684 (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3686 CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3687 service->pac == NULL)))
3688 if (__connman_wpad_start(service) < 0) {
3690 CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3692 __connman_notifier_proxy_changed(service);
3696 case CONNMAN_SERVICE_STATE_ONLINE:
3698 case CONNMAN_SERVICE_STATE_DISCONNECT:
3699 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
3702 case CONNMAN_SERVICE_STATE_FAILURE:
3706 /* We keep that state */
3707 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3708 service->state_ipv4 = new_state;
3709 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3710 service->state_ipv6 = new_state;
3712 return __connman_service_indicate_state(service);
3715 int __connman_service_request_login(struct connman_service *service)
3717 DBG("service %p", service);
3719 if (service == NULL)
3722 service->login_required = TRUE;
3723 login_changed(service);
3728 static connman_bool_t prepare_network(struct connman_service *service)
3730 enum connman_network_type type;
3731 unsigned int ssid_len;
3733 type = connman_network_get_type(service->network);
3736 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3737 case CONNMAN_NETWORK_TYPE_VENDOR:
3739 case CONNMAN_NETWORK_TYPE_WIFI:
3740 if (connman_network_get_blob(service->network, "WiFi.SSID",
3744 if (service->passphrase != NULL)
3745 connman_network_set_string(service->network,
3746 "WiFi.Passphrase", service->passphrase);
3748 case CONNMAN_NETWORK_TYPE_ETHERNET:
3749 case CONNMAN_NETWORK_TYPE_WIMAX:
3750 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3751 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3752 case CONNMAN_NETWORK_TYPE_CELLULAR:
3759 static void prepare_8021x(struct connman_service *service)
3761 if (service->eap != NULL)
3762 connman_network_set_string(service->network, "WiFi.EAP",
3765 if (service->identity != NULL)
3766 connman_network_set_string(service->network, "WiFi.Identity",
3769 if (service->ca_cert_file != NULL)
3770 connman_network_set_string(service->network, "WiFi.CACertFile",
3771 service->ca_cert_file);
3773 if (service->client_cert_file != NULL)
3774 connman_network_set_string(service->network,
3775 "WiFi.ClientCertFile",
3776 service->client_cert_file);
3778 if (service->private_key_file != NULL)
3779 connman_network_set_string(service->network,
3780 "WiFi.PrivateKeyFile",
3781 service->private_key_file);
3783 if (service->private_key_passphrase != NULL)
3784 connman_network_set_string(service->network,
3785 "WiFi.PrivateKeyPassphrase",
3786 service->private_key_passphrase);
3788 if (service->phase2 != NULL)
3789 connman_network_set_string(service->network, "WiFi.Phase2",
3793 static int service_connect(struct connman_service *service)
3797 switch (service->type) {
3798 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3799 case CONNMAN_SERVICE_TYPE_SYSTEM:
3800 case CONNMAN_SERVICE_TYPE_GPS:
3801 case CONNMAN_SERVICE_TYPE_GADGET:
3803 case CONNMAN_SERVICE_TYPE_ETHERNET:
3804 case CONNMAN_SERVICE_TYPE_WIMAX:
3805 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3806 case CONNMAN_SERVICE_TYPE_CELLULAR:
3807 case CONNMAN_SERVICE_TYPE_VPN:
3809 case CONNMAN_SERVICE_TYPE_WIFI:
3810 switch (service->security) {
3811 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3812 case CONNMAN_SERVICE_SECURITY_NONE:
3814 case CONNMAN_SERVICE_SECURITY_WEP:
3815 case CONNMAN_SERVICE_SECURITY_PSK:
3816 case CONNMAN_SERVICE_SECURITY_WPA:
3817 case CONNMAN_SERVICE_SECURITY_RSN:
3818 if (service->passphrase == NULL) {
3819 if (service->network == NULL)
3822 if (service->wps == FALSE ||
3823 connman_network_get_bool(
3825 "WiFi.UseWPS") == FALSE)
3829 case CONNMAN_SERVICE_SECURITY_8021X:
3830 if (service->eap == NULL)
3834 * never request credentials if using EAP-TLS
3835 * (EAP-TLS networks need to be fully provisioned)
3837 if (g_str_equal(service->eap, "tls") == TRUE)
3840 if (service->immutable != TRUE)
3848 if (service->network != NULL) {
3849 if (prepare_network(service) == FALSE)
3852 switch (service->security) {
3853 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3854 case CONNMAN_SERVICE_SECURITY_NONE:
3855 case CONNMAN_SERVICE_SECURITY_WEP:
3856 case CONNMAN_SERVICE_SECURITY_PSK:
3857 case CONNMAN_SERVICE_SECURITY_WPA:
3858 case CONNMAN_SERVICE_SECURITY_RSN:
3860 case CONNMAN_SERVICE_SECURITY_8021X:
3861 prepare_8021x(service);
3865 if (__connman_stats_service_register(service) == 0) {
3866 __connman_stats_get(service, FALSE,
3867 &service->stats.data);
3868 __connman_stats_get(service, TRUE,
3869 &service->stats_roaming.data);
3872 if (service->ipconfig_ipv4)
3873 __connman_ipconfig_enable(service->ipconfig_ipv4);
3874 if (service->ipconfig_ipv6)
3875 __connman_ipconfig_enable(service->ipconfig_ipv6);
3877 err = __connman_network_connect(service->network);
3878 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3879 service->provider != NULL)
3880 err = __connman_provider_connect(service->provider);
3885 if (err != -EINPROGRESS) {
3886 __connman_ipconfig_disable(service->ipconfig_ipv4);
3887 __connman_ipconfig_disable(service->ipconfig_ipv6);
3888 __connman_stats_service_unregister(service);
3896 int __connman_service_connect(struct connman_service *service)
3900 DBG("service %p state %s", service, state2string(service->state));
3902 if (is_connected(service) == TRUE)
3905 if (is_connecting(service) == TRUE)
3908 switch (service->type) {
3909 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3910 case CONNMAN_SERVICE_TYPE_SYSTEM:
3911 case CONNMAN_SERVICE_TYPE_GPS:
3912 case CONNMAN_SERVICE_TYPE_GADGET:
3915 err = service_connect(service);
3921 if (err == -EINPROGRESS) {
3922 if (service->timeout == 0)
3923 service->timeout = g_timeout_add_seconds(
3924 CONNECT_TIMEOUT, connect_timeout, service);
3926 return -EINPROGRESS;
3932 if (service->userconnect == TRUE)
3933 reply_pending(service, err);
3935 __connman_service_ipconfig_indicate_state(service,
3936 CONNMAN_SERVICE_STATE_FAILURE,
3937 CONNMAN_IPCONFIG_TYPE_IPV4);
3938 __connman_service_ipconfig_indicate_state(service,
3939 CONNMAN_SERVICE_STATE_FAILURE,
3940 CONNMAN_IPCONFIG_TYPE_IPV6);
3945 int __connman_service_disconnect(struct connman_service *service)
3949 DBG("service %p", service);
3951 if (service->network != NULL) {
3952 err = __connman_network_disconnect(service->network);
3953 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3954 service->provider != NULL)
3955 err = __connman_provider_disconnect(service->provider);
3959 if (err < 0 && err != -EINPROGRESS)
3962 __connman_6to4_remove(service->ipconfig_ipv4);
3964 if (service->ipconfig_ipv4)
3965 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3968 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3971 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3972 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3974 __connman_ipconfig_disable(service->ipconfig_ipv4);
3975 __connman_ipconfig_disable(service->ipconfig_ipv6);
3977 __connman_stats_service_unregister(service);
3982 int __connman_service_disconnect_all(void)
3984 GSequenceIter *iter;
3988 iter = g_sequence_get_begin_iter(service_list);
3990 while (g_sequence_iter_is_end(iter) == FALSE) {
3991 struct connman_service *service = g_sequence_get(iter);
3993 service->ignore = TRUE;
3995 set_reconnect_state(service, FALSE);
3997 __connman_service_disconnect(service);
3999 iter = g_sequence_iter_next(iter);
4007 * __connman_service_lookup:
4008 * @pattern: search pattern
4009 * @path: return object path
4011 * Look up a service path from a search pattern
4013 int __connman_service_lookup(const char *pattern, const char **path)
4015 GHashTableIter iter;
4016 gpointer key, value;
4018 g_hash_table_iter_init(&iter, service_hash);
4020 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4021 GSequenceIter *iter = value;
4022 struct connman_service *service = g_sequence_get(iter);
4024 if (g_strcmp0(service->identifier, pattern) == 0 ||
4025 g_strcmp0(service->name, pattern) == 0) {
4026 *path = (const char *) service->path;
4035 * lookup_by_identifier:
4036 * @identifier: service identifier
4038 * Look up a service by identifier (reference count will not be increased)
4040 static struct connman_service *lookup_by_identifier(const char *identifier)
4042 GSequenceIter *iter;
4044 iter = g_hash_table_lookup(service_hash, identifier);
4046 return g_sequence_get(iter);
4051 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4052 const char *ssid, const char *mode, const char *security)
4054 struct connman_network *network;
4057 unsigned int i, ssid_len;
4059 ssid_len = strlen(ssid);
4063 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
4064 if (network == NULL)
4067 connman_network_register(network);
4069 connman_network_set_blob(network, "WiFi.SSID",
4070 (unsigned char *) ssid, ssid_len);
4072 connman_network_set_string(network, "WiFi.Mode", mode);
4073 connman_network_set_string(network, "WiFi.Security", security);
4075 name = g_try_malloc0(ssid_len + 1);
4077 connman_network_unregister(network);
4078 connman_network_unref(network);
4082 for (i = 0; i < ssid_len; i++) {
4083 if (g_ascii_isprint(ssid[i]))
4089 connman_network_set_name(network, name);
4093 index = connman_device_get_index(device);
4094 connman_network_set_index(network, index);
4096 if (connman_device_add_network(device, network) < 0) {
4097 connman_network_unregister(network);
4098 connman_network_unref(network);
4102 connman_network_set_available(network, TRUE);
4107 int __connman_service_create_and_connect(DBusMessage *msg)
4109 struct connman_service *service;
4110 struct connman_network *network;
4111 struct connman_device *device;
4112 DBusMessageIter iter, array;
4113 const char *mode = "managed", *security = "none", *group_security;
4114 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4115 unsigned int ssid_len = 0;
4120 dbus_message_iter_init(msg, &iter);
4121 dbus_message_iter_recurse(&iter, &array);
4123 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4124 DBusMessageIter entry, value;
4127 dbus_message_iter_recurse(&array, &entry);
4128 dbus_message_iter_get_basic(&entry, &key);
4130 dbus_message_iter_next(&entry);
4131 dbus_message_iter_recurse(&entry, &value);
4133 switch (dbus_message_iter_get_arg_type(&value)) {
4134 case DBUS_TYPE_STRING:
4135 if (g_str_equal(key, "Type") == TRUE)
4136 dbus_message_iter_get_basic(&value, &type);
4137 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4138 g_str_equal(key, "Mode") == TRUE)
4139 dbus_message_iter_get_basic(&value, &mode);
4140 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4141 g_str_equal(key, "Security") == TRUE)
4142 dbus_message_iter_get_basic(&value, &security);
4143 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4144 g_str_equal(key, "Passphrase") == TRUE)
4145 dbus_message_iter_get_basic(&value, &passphrase);
4146 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4147 g_str_equal(key, "SSID") == TRUE)
4148 dbus_message_iter_get_basic(&value, &ssid);
4151 dbus_message_iter_next(&array);
4157 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4163 ssid_len = strlen(ssid);
4167 if (g_strcmp0(security, "none") != 0 &&
4168 g_strcmp0(security, "wep") != 0 &&
4169 g_strcmp0(security, "psk") != 0 &&
4170 g_strcmp0(security, "wpa") != 0 &&
4171 g_strcmp0(security, "rsn") != 0 &&
4172 g_strcmp0(security, "ieee8021x") != 0)
4175 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4179 ident = connman_device_get_ident(device);
4184 if (!g_strcmp0(security, "wpa") ||
4185 !g_strcmp0(security, "rsn"))
4186 group_security = "psk";
4188 group_security = security;
4190 group = connman_wifi_build_group_name((unsigned char *) ssid,
4191 ssid_len, mode, group_security);
4195 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4197 service = lookup_by_identifier(name);
4199 if (service != NULL)
4202 network = create_hidden_wifi(device, ssid, mode, security);
4203 if (network != NULL) {
4204 connman_network_set_group(network, group);
4205 service->network_created = TRUE;
4208 service = lookup_by_identifier(name);
4214 if (service == NULL) {
4219 if (is_connected(service) == TRUE) {
4224 if (is_connecting(service) == TRUE) {
4229 set_reconnect_state(service, FALSE);
4231 __connman_device_disconnect(device);
4233 if (passphrase != NULL) {
4234 g_free(service->passphrase);
4235 service->passphrase = g_strdup(passphrase);
4238 service->userconnect = TRUE;
4240 err = __connman_service_connect(service);
4241 if (err < 0 && err != -EINPROGRESS)
4244 service->pending = dbus_message_ref(msg);
4249 if (service != NULL && service->network_created == TRUE) {
4250 struct connman_network *network = service->network;
4252 if (network != NULL) {
4253 connman_network_set_available(network, FALSE);
4254 __connman_device_cleanup_networks(device);
4256 __connman_service_put(service);
4262 static void provision_changed(gpointer value, gpointer user_data)
4264 struct connman_service *service = value;
4265 char *path = user_data;
4267 __connman_config_provision_service_ident(service, path);
4270 void __connman_service_provision_changed(const char *ident)
4272 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4275 int __connman_service_provision(DBusMessage *msg)
4277 GKeyFile *keyfile = NULL;
4278 const char *config_str = NULL;
4279 char *group = NULL, *ident = NULL;
4281 struct connman_service *service;
4285 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4288 if (config_str == NULL || strlen(config_str) == 0)
4291 keyfile = g_key_file_new();
4293 /* populate GKeyFile with config_str */
4294 if (g_key_file_load_from_data(keyfile, config_str,
4295 strlen(config_str), 0, NULL) == FALSE) {
4301 * read only one group of settings (only one service supported, no
4304 group = g_key_file_get_start_group(keyfile);
4306 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4311 err = __connman_config_load_service(keyfile, group, TRUE);
4315 ident = group + strlen("service_");
4317 /* trigger service provisioning if service exists */
4318 service = lookup_by_identifier(ident);
4319 if (service != NULL)
4320 __connman_config_provision_service(service);
4322 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4328 if (keyfile != NULL)
4329 g_key_file_free(keyfile);
4335 * __connman_service_get:
4336 * @identifier: service identifier
4338 * Look up a service by identifier or create a new one if not found
4340 static struct connman_service *service_get(const char *identifier)
4342 struct connman_service *service;
4343 GSequenceIter *iter;
4345 iter = g_hash_table_lookup(service_hash, identifier);
4347 service = g_sequence_get(iter);
4348 if (service != NULL)
4349 connman_service_ref(service);
4353 service = connman_service_create();
4354 if (service == NULL)
4357 DBG("service %p", service);
4359 service->identifier = g_strdup(identifier);
4361 service->profile = g_strdup(__connman_profile_active_ident());
4363 iter = g_sequence_insert_sorted(service_list, service,
4364 service_compare, NULL);
4366 g_hash_table_insert(service_hash, service->identifier, iter);
4371 static int service_register(struct connman_service *service)
4373 const char *path = __connman_profile_active_path();
4374 GSequenceIter *iter;
4376 DBG("service %p", service);
4378 if (service->path != NULL)
4381 service->path = g_strdup_printf("%s/%s", path, service->identifier);
4383 DBG("path %s", service->path);
4385 __connman_config_provision_service(service);
4387 __connman_storage_load_service(service);
4389 g_dbus_register_interface(connection, service->path,
4390 CONNMAN_SERVICE_INTERFACE,
4391 service_methods, service_signals,
4392 NULL, service, NULL);
4394 iter = g_hash_table_lookup(service_hash, service->identifier);
4396 g_sequence_sort_changed(iter, service_compare, NULL);
4398 __connman_profile_changed(TRUE);
4403 static void service_up(struct connman_ipconfig *ipconfig)
4405 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4407 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4409 link_changed(service);
4411 service->stats.valid = FALSE;
4412 service->stats_roaming.valid = FALSE;
4415 static void service_down(struct connman_ipconfig *ipconfig)
4417 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4420 static void service_lower_up(struct connman_ipconfig *ipconfig)
4422 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4424 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4426 stats_start(service);
4429 static void service_lower_down(struct connman_ipconfig *ipconfig)
4431 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4433 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4435 stats_stop(service);
4436 __connman_storage_save_service(service);
4439 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4441 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4442 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4443 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4445 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4447 type = __connman_ipconfig_get_config_type(ipconfig);
4448 method = __connman_ipconfig_get_method(ipconfig);
4450 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4453 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4454 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4455 __connman_service_ipconfig_indicate_state(service,
4456 CONNMAN_SERVICE_STATE_READY,
4457 CONNMAN_IPCONFIG_TYPE_IPV6);
4459 settings_changed(service, ipconfig);
4462 static void service_ip_release(struct connman_ipconfig *ipconfig)
4464 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4465 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4466 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4468 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4470 type = __connman_ipconfig_get_config_type(ipconfig);
4471 method = __connman_ipconfig_get_method(ipconfig);
4473 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4476 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4477 method == CONNMAN_IPCONFIG_METHOD_OFF)
4478 __connman_service_ipconfig_indicate_state(service,
4479 CONNMAN_SERVICE_STATE_DISCONNECT,
4480 CONNMAN_IPCONFIG_TYPE_IPV6);
4482 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4483 method == CONNMAN_IPCONFIG_METHOD_OFF)
4484 __connman_service_ipconfig_indicate_state(service,
4485 CONNMAN_SERVICE_STATE_DISCONNECT,
4486 CONNMAN_IPCONFIG_TYPE_IPV4);
4488 settings_changed(service, ipconfig);
4491 static const struct connman_ipconfig_ops service_ops = {
4493 .down = service_down,
4494 .lower_up = service_lower_up,
4495 .lower_down = service_lower_down,
4496 .ip_bound = service_ip_bound,
4497 .ip_release = service_ip_release,
4500 static void setup_ip4config(struct connman_service *service, int index,
4501 enum connman_ipconfig_method method)
4506 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4507 CONNMAN_IPCONFIG_TYPE_IPV4);
4508 if (service->ipconfig_ipv4 == NULL)
4511 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4513 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4515 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4518 static void setup_ip6config(struct connman_service *service, int index)
4523 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4524 CONNMAN_IPCONFIG_TYPE_IPV6);
4525 if (service->ipconfig_ipv6 == NULL)
4528 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4530 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4533 void __connman_service_create_ip4config(struct connman_service *service,
4536 const char *ident = service->profile;
4539 DBG("ipv4 %p", service->ipconfig_ipv4);
4541 if (service->ipconfig_ipv4 != NULL)
4544 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4549 keyfile = __connman_storage_open_profile(ident);
4550 if (keyfile == NULL)
4553 if (service->ipconfig_ipv4)
4554 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4555 service->identifier, "IPv4.");
4556 g_key_file_free(keyfile);
4559 void __connman_service_create_ip6config(struct connman_service *service,
4562 const char *ident = service->profile;
4565 DBG("ipv6 %p", service->ipconfig_ipv6);
4567 if (service->ipconfig_ipv6 != NULL)
4570 setup_ip6config(service, index);
4575 keyfile = __connman_storage_open_profile(ident);
4576 if (keyfile == NULL)
4579 if (service->ipconfig_ipv6 != NULL)
4580 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4581 service->identifier, "IPv6.");
4583 g_key_file_free(keyfile);
4587 * __connman_service_lookup_from_network:
4588 * @network: network structure
4590 * Look up a service by network (reference count will not be increased)
4592 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4594 struct connman_service *service;
4595 const char *ident, *group;
4598 DBG("network %p", network);
4600 ident = __connman_network_get_ident(network);
4604 group = connman_network_get_group(network);
4608 name = g_strdup_printf("%s_%s_%s",
4609 __connman_network_get_type(network), ident, group);
4610 service = lookup_by_identifier(name);
4616 struct connman_service *__connman_service_lookup_from_index(int index)
4618 struct connman_service *service;
4619 GSequenceIter *iter;
4621 iter = g_sequence_get_begin_iter(service_list);
4623 while (g_sequence_iter_is_end(iter) == FALSE) {
4624 service = g_sequence_get(iter);
4626 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4630 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4634 iter = g_sequence_iter_next(iter);
4640 const char *__connman_service_get_ident(struct connman_service *service)
4642 return service->identifier;
4645 const char *__connman_service_get_path(struct connman_service *service)
4647 return service->path;
4650 unsigned int __connman_service_get_order(struct connman_service *service)
4652 GSequenceIter *iter;
4654 if (service == NULL)
4657 if (service->favorite == FALSE) {
4662 iter = g_hash_table_lookup(service_hash, service->identifier);
4664 if (g_sequence_iter_get_position(iter) == 0)
4666 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4667 service->order = 10;
4673 return service->order;
4676 static enum connman_service_type convert_network_type(struct connman_network *network)
4678 enum connman_network_type type = connman_network_get_type(network);
4681 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4682 case CONNMAN_NETWORK_TYPE_VENDOR:
4684 case CONNMAN_NETWORK_TYPE_ETHERNET:
4685 return CONNMAN_SERVICE_TYPE_ETHERNET;
4686 case CONNMAN_NETWORK_TYPE_WIFI:
4687 return CONNMAN_SERVICE_TYPE_WIFI;
4688 case CONNMAN_NETWORK_TYPE_WIMAX:
4689 return CONNMAN_SERVICE_TYPE_WIMAX;
4690 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4691 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4692 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4693 case CONNMAN_NETWORK_TYPE_CELLULAR:
4694 return CONNMAN_SERVICE_TYPE_CELLULAR;
4697 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4700 static enum connman_service_security convert_wifi_security(const char *security)
4702 if (security == NULL)
4703 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4704 else if (g_str_equal(security, "none") == TRUE)
4705 return CONNMAN_SERVICE_SECURITY_NONE;
4706 else if (g_str_equal(security, "wep") == TRUE)
4707 return CONNMAN_SERVICE_SECURITY_WEP;
4708 else if (g_str_equal(security, "psk") == TRUE)
4709 return CONNMAN_SERVICE_SECURITY_PSK;
4710 else if (g_str_equal(security, "ieee8021x") == TRUE)
4711 return CONNMAN_SERVICE_SECURITY_8021X;
4712 else if (g_str_equal(security, "wpa") == TRUE)
4713 return CONNMAN_SERVICE_SECURITY_WPA;
4714 else if (g_str_equal(security, "rsn") == TRUE)
4715 return CONNMAN_SERVICE_SECURITY_RSN;
4717 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4720 static void update_from_network(struct connman_service *service,
4721 struct connman_network *network)
4723 connman_uint8_t strength = service->strength;
4724 GSequenceIter *iter;
4727 DBG("service %p network %p", service, network);
4729 if (is_connected(service) == TRUE)
4732 if (is_connecting(service) == TRUE)
4735 str = connman_network_get_string(network, "Name");
4737 g_free(service->name);
4738 service->name = g_strdup(str);
4739 service->hidden = FALSE;
4741 g_free(service->name);
4742 service->name = NULL;
4743 service->hidden = TRUE;
4746 service->strength = connman_network_get_strength(network);
4747 service->roaming = connman_network_get_bool(network, "Roaming");
4749 if (service->strength == 0) {
4751 * Filter out 0-values; it's unclear what they mean
4752 * and they cause anomalous sorting of the priority list.
4754 service->strength = strength;
4757 str = connman_network_get_string(network, "WiFi.Security");
4758 service->security = convert_wifi_security(str);
4760 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4761 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4763 if (service->strength > strength && service->network != NULL) {
4764 connman_network_unref(service->network);
4765 service->network = connman_network_ref(network);
4767 strength_changed(service);
4770 if (service->network == NULL)
4771 service->network = connman_network_ref(network);
4773 iter = g_hash_table_lookup(service_hash, service->identifier);
4775 g_sequence_sort_changed(iter, service_compare, NULL);
4779 * __connman_service_create_from_network:
4780 * @network: network structure
4782 * Look up service by network and if not found, create one
4784 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4786 struct connman_service *service;
4787 struct connman_device *device;
4788 const char *ident, *group;
4792 DBG("network %p", network);
4794 ident = __connman_network_get_ident(network);
4798 group = connman_network_get_group(network);
4802 name = g_strdup_printf("%s_%s_%s",
4803 __connman_network_get_type(network), ident, group);
4804 service = service_get(name);
4807 if (service == NULL)
4810 if (__connman_network_get_weakness(network) == TRUE)
4813 if (service->path != NULL) {
4814 update_from_network(service, network);
4815 __connman_profile_changed(TRUE);
4819 service->type = convert_network_type(network);
4821 switch (service->type) {
4822 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4823 case CONNMAN_SERVICE_TYPE_SYSTEM:
4824 case CONNMAN_SERVICE_TYPE_ETHERNET:
4825 case CONNMAN_SERVICE_TYPE_WIMAX:
4826 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4827 case CONNMAN_SERVICE_TYPE_GPS:
4828 case CONNMAN_SERVICE_TYPE_VPN:
4829 case CONNMAN_SERVICE_TYPE_GADGET:
4830 service->autoconnect = FALSE;
4832 case CONNMAN_SERVICE_TYPE_WIFI:
4833 case CONNMAN_SERVICE_TYPE_CELLULAR:
4834 service->autoconnect = TRUE;
4838 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4839 service->state = combine_state(service->state_ipv4, service->state_ipv6);
4841 update_from_network(service, network);
4843 index = connman_network_get_index(network);
4845 if (service->ipconfig_ipv4 == NULL)
4846 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4848 if (service->ipconfig_ipv6 == NULL)
4849 setup_ip6config(service, index);
4851 service_register(service);
4853 if (service->favorite == TRUE) {
4854 device = connman_network_get_device(service->network);
4855 if (device && __connman_device_scanning(device) == FALSE)
4856 __connman_service_auto_connect();
4859 __connman_notifier_service_add(service);
4864 void __connman_service_update_from_network(struct connman_network *network)
4866 struct connman_service *service;
4867 connman_uint8_t strength;
4868 connman_bool_t roaming;
4869 GSequenceIter *iter;
4871 connman_bool_t stats_enable;
4873 DBG("network %p", network);
4875 service = __connman_service_lookup_from_network(network);
4876 if (service == NULL)
4879 if (service->network == NULL)
4882 name = connman_network_get_string(service->network, "Name");
4883 if (g_strcmp0(service->name, name) != 0) {
4884 g_free(service->name);
4885 service->name = g_strdup(name);
4886 connman_dbus_property_changed_basic(service->path,
4887 CONNMAN_SERVICE_INTERFACE, "Name",
4888 DBUS_TYPE_STRING, &service->name);
4891 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4892 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4894 strength = connman_network_get_strength(service->network);
4895 if (strength == service->strength)
4898 service->strength = strength;
4900 strength_changed(service);
4903 roaming = connman_network_get_bool(service->network, "Roaming");
4904 if (roaming == service->roaming)
4907 stats_enable = stats_enabled(service);
4908 if (stats_enable == TRUE)
4909 stats_stop(service);
4911 service->roaming = roaming;
4913 if (stats_enable == TRUE)
4914 stats_start(service);
4916 roaming_changed(service);
4918 iter = g_hash_table_lookup(service_hash, service->identifier);
4920 g_sequence_sort_changed(iter, service_compare, NULL);
4923 void __connman_service_remove_from_network(struct connman_network *network)
4925 struct connman_service *service;
4927 DBG("network %p", network);
4929 service = __connman_service_lookup_from_network(network);
4930 if (service == NULL)
4933 __connman_service_put(service);
4937 * __connman_service_create_from_provider:
4938 * @provider: provider structure
4940 * Look up service by provider and if not found, create one
4942 struct connman_service *
4943 __connman_service_create_from_provider(struct connman_provider *provider)
4945 struct connman_service *service;
4946 const char *ident, *str;
4948 int index = connman_provider_get_index(provider);
4950 DBG("provider %p", provider);
4952 ident = __connman_provider_get_ident(provider);
4956 name = g_strdup_printf("vpn_%s", ident);
4957 service = service_get(name);
4960 if (service == NULL)
4963 service->type = CONNMAN_SERVICE_TYPE_VPN;
4964 service->provider = connman_provider_ref(provider);
4965 service->autoconnect = FALSE;
4967 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4969 str = connman_provider_get_string(provider, "Name");
4971 g_free(service->name);
4972 service->name = g_strdup(str);
4973 service->hidden = FALSE;
4975 g_free(service->name);
4976 service->name = NULL;
4977 service->hidden = TRUE;
4980 service->strength = 0;
4982 if (service->ipconfig_ipv4 == NULL)
4983 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
4985 if (service->ipconfig_ipv6 == NULL)
4986 setup_ip6config(service, index);
4988 service_register(service);
4990 __connman_notifier_service_add(service);
4995 static int service_load(struct connman_service *service)
4997 const char *ident = service->profile;
4999 GError *error = NULL;
5000 gchar *pathname, *data = NULL;
5003 connman_bool_t autoconnect;
5004 unsigned int ssid_len;
5007 DBG("service %p", service);
5012 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5013 if (pathname == NULL)
5016 keyfile = g_key_file_new();
5018 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
5025 if (g_key_file_load_from_data(keyfile, data, length,
5026 0, NULL) == FALSE) {
5033 switch (service->type) {
5034 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5035 case CONNMAN_SERVICE_TYPE_SYSTEM:
5036 case CONNMAN_SERVICE_TYPE_ETHERNET:
5037 case CONNMAN_SERVICE_TYPE_GPS:
5038 case CONNMAN_SERVICE_TYPE_VPN:
5039 case CONNMAN_SERVICE_TYPE_GADGET:
5041 case CONNMAN_SERVICE_TYPE_WIFI:
5042 if (service->name == NULL) {
5045 name = g_key_file_get_string(keyfile,
5046 service->identifier, "Name", NULL);
5048 g_free(service->name);
5049 service->name = name;
5052 if (service->network != NULL)
5053 connman_network_set_name(service->network,
5057 if (service->network &&
5058 connman_network_get_blob(service->network,
5059 "WiFi.SSID", &ssid_len) == NULL) {
5062 hex_ssid = g_key_file_get_string(keyfile,
5063 service->identifier,
5066 if (hex_ssid != NULL) {
5068 unsigned int i, j = 0, hex;
5069 size_t hex_ssid_len = strlen(hex_ssid);
5071 ssid = g_try_malloc0(hex_ssid_len / 2);
5078 for (i = 0; i < hex_ssid_len; i += 2) {
5079 sscanf(hex_ssid + i, "%02x", &hex);
5083 connman_network_set_blob(service->network,
5084 "WiFi.SSID", ssid, hex_ssid_len / 2);
5091 case CONNMAN_SERVICE_TYPE_WIMAX:
5092 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5093 case CONNMAN_SERVICE_TYPE_CELLULAR:
5094 service->favorite = g_key_file_get_boolean(keyfile,
5095 service->identifier, "Favorite", NULL);
5097 autoconnect = g_key_file_get_boolean(keyfile,
5098 service->identifier, "AutoConnect", &error);
5100 service->autoconnect = autoconnect;
5101 g_clear_error(&error);
5103 str = g_key_file_get_string(keyfile,
5104 service->identifier, "Failure", NULL);
5106 if (service->favorite == FALSE)
5107 service->state_ipv4 = service->state_ipv6 =
5108 CONNMAN_SERVICE_STATE_FAILURE;
5109 service->error = string2error(str);
5114 str = g_key_file_get_string(keyfile,
5115 service->identifier, "Modified", NULL);
5117 g_time_val_from_iso8601(str, &service->modified);
5121 str = g_key_file_get_string(keyfile,
5122 service->identifier, "Passphrase", NULL);
5124 g_free(service->passphrase);
5125 service->passphrase = str;
5128 if (service->ipconfig_ipv4 != NULL)
5129 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5130 service->identifier, "IPv4.");
5132 if (service->ipconfig_ipv6 != NULL)
5133 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5134 service->identifier, "IPv6.");
5136 service->nameservers_config = g_key_file_get_string_list(keyfile,
5137 service->identifier, "Nameservers", &length, NULL);
5138 if (service->nameservers_config != NULL && length == 0) {
5139 g_strfreev(service->nameservers_config);
5140 service->nameservers_config = NULL;
5143 service->domains = g_key_file_get_string_list(keyfile,
5144 service->identifier, "Domains", &length, NULL);
5145 if (service->domains != NULL && length == 0) {
5146 g_strfreev(service->domains);
5147 service->domains = NULL;
5150 str = g_key_file_get_string(keyfile,
5151 service->identifier, "Proxy.Method", NULL);
5153 service->proxy_config = string2proxymethod(str);
5157 service->proxies = g_key_file_get_string_list(keyfile,
5158 service->identifier, "Proxy.Servers", &length, NULL);
5159 if (service->proxies != NULL && length == 0) {
5160 g_strfreev(service->proxies);
5161 service->proxies = NULL;
5164 service->excludes = g_key_file_get_string_list(keyfile,
5165 service->identifier, "Proxy.Excludes", &length, NULL);
5166 if (service->excludes != NULL && length == 0) {
5167 g_strfreev(service->excludes);
5168 service->excludes = NULL;
5171 str = g_key_file_get_string(keyfile,
5172 service->identifier, "Proxy.URL", NULL);
5174 g_free(service->pac);
5179 g_key_file_free(keyfile);
5184 static int service_save(struct connman_service *service)
5186 const char *ident = service->profile;
5188 gchar *pathname, *data = NULL;
5191 const char *cst_str = NULL;
5194 DBG("service %p", service);
5199 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5200 if (pathname == NULL)
5203 keyfile = g_key_file_new();
5205 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5209 if (g_key_file_load_from_data(keyfile, data, length,
5217 if (service->name != NULL)
5218 g_key_file_set_string(keyfile, service->identifier,
5219 "Name", service->name);
5221 switch (service->type) {
5222 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5223 case CONNMAN_SERVICE_TYPE_SYSTEM:
5224 case CONNMAN_SERVICE_TYPE_ETHERNET:
5225 case CONNMAN_SERVICE_TYPE_GPS:
5226 case CONNMAN_SERVICE_TYPE_VPN:
5227 case CONNMAN_SERVICE_TYPE_GADGET:
5229 case CONNMAN_SERVICE_TYPE_WIFI:
5230 if (service->network) {
5231 const unsigned char *ssid;
5232 unsigned int ssid_len = 0;
5234 ssid = connman_network_get_blob(service->network,
5235 "WiFi.SSID", &ssid_len);
5237 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5238 char *identifier = service->identifier;
5242 str = g_string_sized_new(ssid_len * 2);
5248 for (i = 0; i < ssid_len; i++)
5249 g_string_append_printf(str,
5252 g_key_file_set_string(keyfile, identifier,
5255 g_string_free(str, TRUE);
5260 case CONNMAN_SERVICE_TYPE_WIMAX:
5261 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5262 case CONNMAN_SERVICE_TYPE_CELLULAR:
5263 g_key_file_set_boolean(keyfile, service->identifier,
5264 "Favorite", service->favorite);
5266 if (service->favorite == TRUE)
5267 g_key_file_set_boolean(keyfile, service->identifier,
5268 "AutoConnect", service->autoconnect);
5270 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5271 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5272 const char *failure = error2string(service->error);
5273 if (failure != NULL)
5274 g_key_file_set_string(keyfile,
5275 service->identifier,
5276 "Failure", failure);
5278 g_key_file_remove_key(keyfile, service->identifier,
5284 str = g_time_val_to_iso8601(&service->modified);
5286 g_key_file_set_string(keyfile, service->identifier,
5291 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5292 g_key_file_set_string(keyfile, service->identifier,
5293 "Passphrase", service->passphrase);
5295 g_key_file_remove_key(keyfile, service->identifier,
5296 "Passphrase", NULL);
5298 if (service->ipconfig_ipv4 != NULL)
5299 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5300 service->identifier, "IPv4.");
5302 if (service->ipconfig_ipv6 != NULL)
5303 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5304 service->identifier, "IPv6.");
5306 if (service->nameservers_config != NULL) {
5307 guint len = g_strv_length(service->nameservers_config);
5309 g_key_file_set_string_list(keyfile, service->identifier,
5311 (const gchar **) service->nameservers_config, len);
5313 g_key_file_remove_key(keyfile, service->identifier,
5314 "Nameservers", NULL);
5316 if (service->domains != NULL) {
5317 guint len = g_strv_length(service->domains);
5319 g_key_file_set_string_list(keyfile, service->identifier,
5321 (const gchar **) service->domains, len);
5323 g_key_file_remove_key(keyfile, service->identifier,
5326 cst_str = proxymethod2string(service->proxy_config);
5327 if (cst_str != NULL)
5328 g_key_file_set_string(keyfile, service->identifier,
5329 "Proxy.Method", cst_str);
5331 if (service->proxies != NULL) {
5332 guint len = g_strv_length(service->proxies);
5334 g_key_file_set_string_list(keyfile, service->identifier,
5336 (const gchar **) service->proxies, len);
5338 g_key_file_remove_key(keyfile, service->identifier,
5339 "Proxy.Servers", NULL);
5341 if (service->excludes != NULL) {
5342 guint len = g_strv_length(service->excludes);
5344 g_key_file_set_string_list(keyfile, service->identifier,
5346 (const gchar **) service->excludes, len);
5348 g_key_file_remove_key(keyfile, service->identifier,
5349 "Proxy.Excludes", NULL);
5351 if (service->pac != NULL && strlen(service->pac) > 0)
5352 g_key_file_set_string(keyfile, service->identifier,
5353 "Proxy.URL", service->pac);
5355 g_key_file_remove_key(keyfile, service->identifier,
5358 data = g_key_file_to_data(keyfile, &length, NULL);
5360 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5361 connman_error("Failed to store service information");
5366 g_key_file_free(keyfile);
5373 static struct connman_storage service_storage = {
5375 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
5376 .service_load = service_load,
5377 .service_save = service_save,
5380 int __connman_service_init(void)
5384 connection = connman_dbus_get_connection();
5386 if (connman_storage_register(&service_storage) < 0)
5387 connman_error("Failed to register service storage");
5389 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5392 service_list = g_sequence_new(service_free);
5397 void __connman_service_cleanup(void)
5403 list = service_list;
5404 service_list = NULL;
5405 g_sequence_free(list);
5407 g_hash_table_destroy(service_hash);
5408 service_hash = NULL;
5410 g_slist_free(counter_list);
5411 counter_list = NULL;
5413 connman_storage_unregister(&service_storage);
5415 dbus_connection_unref(connection);