diff options
author | Zhang zhengguang <zhengguang.zhang@intel.com> | 2014-07-17 10:37:39 +0800 |
---|---|---|
committer | Zhang zhengguang <zhengguang.zhang@intel.com> | 2014-07-17 10:37:39 +0800 |
commit | 1b9d0a62f59bb48c8deb2f0b98d9acdffdd9abe7 (patch) | |
tree | 6e991827d28537f7f40f20786c2354fd04a9fdad /src/service.c | |
parent | fbe905ab58ecc31fe64c410c5f580cadc30e7f04 (diff) | |
download | connman-1b9d0a62f59bb48c8deb2f0b98d9acdffdd9abe7.tar.gz connman-1b9d0a62f59bb48c8deb2f0b98d9acdffdd9abe7.tar.bz2 connman-1b9d0a62f59bb48c8deb2f0b98d9acdffdd9abe7.zip |
Imported Upstream version 1.24upstream/1.24
Diffstat (limited to 'src/service.c')
-rw-r--r-- | src/service.c | 2408 |
1 files changed, 1343 insertions, 1065 deletions
diff --git a/src/service.c b/src/service.c index 9c4f3fbc..cbca669e 100644 --- a/src/service.c +++ b/src/service.c @@ -2,7 +2,7 @@ * * Connection Manager * - * Copyright (C) 2007-2012 Intel Corporation. All rights reserved. + * Copyright (C) 2007-2014 Intel Corporation. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -45,26 +45,26 @@ static GList *service_list = NULL; static GHashTable *service_hash = NULL; static GSList *counter_list = NULL; static unsigned int autoconnect_timeout = 0; +static unsigned int vpn_autoconnect_timeout = 0; static struct connman_service *current_default = NULL; -static connman_bool_t services_dirty = FALSE; +static bool services_dirty = false; struct connman_stats { - connman_bool_t valid; - connman_bool_t enabled; + bool valid; + bool enabled; struct connman_stats_data data_last; struct connman_stats_data data; GTimer *timer; }; struct connman_stats_counter { - connman_bool_t append_all; + bool append_all; struct connman_stats stats; struct connman_stats stats_roaming; }; struct connman_service { int refcount; - int session_usage_count; char *identifier; char *path; enum connman_service_type type; @@ -73,19 +73,18 @@ struct connman_service { enum connman_service_state state_ipv4; enum connman_service_state state_ipv6; enum connman_service_error error; + enum connman_service_connect_reason connect_reason; uint8_t strength; - connman_bool_t favorite; - connman_bool_t immutable; - connman_bool_t hidden; - connman_bool_t ignore; - connman_bool_t autoconnect; - connman_bool_t userconnect; + bool favorite; + bool immutable; + bool hidden; + bool ignore; + bool autoconnect; GTimeVal modified; unsigned int order; char *name; char *passphrase; - char *agent_passphrase; - connman_bool_t roaming; + bool roaming; struct connman_ipconfig *ipconfig_ipv4; struct connman_ipconfig *ipconfig_ipv6; struct connman_network *network; @@ -94,6 +93,7 @@ struct connman_service { char **nameservers_config; char **nameservers_auto; char **domains; + char *hostname; char *domainname; char **timeservers; char **timeservers_config; @@ -117,16 +117,16 @@ struct connman_service { char **proxies; char **excludes; char *pac; - connman_bool_t wps; + bool wps; int online_check_count; - connman_bool_t do_split_routing; - connman_bool_t new_service; - connman_bool_t hidden_service; + bool do_split_routing; + bool new_service; + bool hidden_service; char *config_file; char *config_entry; }; -static connman_bool_t allow_property_changed(struct connman_service *service); +static bool allow_property_changed(struct connman_service *service); static struct connman_ipconfig *create_ip4config(struct connman_service *service, int index, enum connman_ipconfig_method method); @@ -144,7 +144,7 @@ static void compare_path(gpointer value, gpointer user_data) struct connman_service *service = value; struct find_data *data = user_data; - if (data->service != NULL) + if (data->service) return; if (g_strcmp0(service->path, data->path) == 0) @@ -162,6 +162,23 @@ static struct connman_service *find_service(const char *path) return data.service; } +static const char *reason2string(enum connman_service_connect_reason reason) +{ + + switch (reason) { + case CONNMAN_SERVICE_CONNECT_REASON_NONE: + return "none"; + case CONNMAN_SERVICE_CONNECT_REASON_USER: + return "user"; + case CONNMAN_SERVICE_CONNECT_REASON_AUTO: + return "auto"; + case CONNMAN_SERVICE_CONNECT_REASON_SESSION: + return "session"; + } + + return "unknown"; +} + const char *__connman_service_type2string(enum connman_service_type type) { switch (type) { @@ -183,6 +200,8 @@ const char *__connman_service_type2string(enum connman_service_type type) return "vpn"; case CONNMAN_SERVICE_TYPE_GADGET: return "gadget"; + case CONNMAN_SERVICE_TYPE_P2P: + return "p2p"; } return NULL; @@ -190,7 +209,7 @@ const char *__connman_service_type2string(enum connman_service_type type) enum connman_service_type __connman_service_string2type(const char *str) { - if (str == NULL) + if (!str) return CONNMAN_SERVICE_TYPE_UNKNOWN; if (strcmp(str, "ethernet") == 0) @@ -209,6 +228,8 @@ enum connman_service_type __connman_service_string2type(const char *str) return CONNMAN_SERVICE_TYPE_GPS; if (strcmp(str, "system") == 0) return CONNMAN_SERVICE_TYPE_SYSTEM; + if (strcmp(str, "p2p") == 0) + return CONNMAN_SERVICE_TYPE_P2P; return CONNMAN_SERVICE_TYPE_UNKNOWN; } @@ -321,67 +342,120 @@ static enum connman_service_proxy_method string2proxymethod(const char *method) return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN; } +int __connman_service_load_modifiable(struct connman_service *service) +{ + GKeyFile *keyfile; + GError *error = NULL; + gchar *str; + bool autoconnect; + + DBG("service %p", service); + + keyfile = connman_storage_load_service(service->identifier); + if (!keyfile) + return -EIO; + + switch (service->type) { + case CONNMAN_SERVICE_TYPE_UNKNOWN: + case CONNMAN_SERVICE_TYPE_SYSTEM: + case CONNMAN_SERVICE_TYPE_GPS: + case CONNMAN_SERVICE_TYPE_P2P: + break; + case CONNMAN_SERVICE_TYPE_VPN: + service->do_split_routing = g_key_file_get_boolean(keyfile, + service->identifier, "SplitRouting", NULL); + /* fall through */ + case CONNMAN_SERVICE_TYPE_WIFI: + case CONNMAN_SERVICE_TYPE_GADGET: + case CONNMAN_SERVICE_TYPE_BLUETOOTH: + case CONNMAN_SERVICE_TYPE_CELLULAR: + case CONNMAN_SERVICE_TYPE_ETHERNET: + autoconnect = g_key_file_get_boolean(keyfile, + service->identifier, "AutoConnect", &error); + if (!error) + service->autoconnect = autoconnect; + g_clear_error(&error); + break; + } + + str = g_key_file_get_string(keyfile, + service->identifier, "Modified", NULL); + if (str) { + g_time_val_from_iso8601(str, &service->modified); + g_free(str); + } + + g_key_file_free(keyfile); + + return 0; +} + static int service_load(struct connman_service *service) { GKeyFile *keyfile; GError *error = NULL; gsize length; gchar *str; - connman_bool_t autoconnect; + bool autoconnect; unsigned int ssid_len; int err = 0; DBG("service %p", service); keyfile = connman_storage_load_service(service->identifier); - if (keyfile == NULL) { - service->new_service = TRUE; + if (!keyfile) { + service->new_service = true; return -EIO; } else - service->new_service = FALSE; + service->new_service = false; switch (service->type) { case CONNMAN_SERVICE_TYPE_UNKNOWN: case CONNMAN_SERVICE_TYPE_SYSTEM: case CONNMAN_SERVICE_TYPE_GPS: - case CONNMAN_SERVICE_TYPE_GADGET: + case CONNMAN_SERVICE_TYPE_P2P: break; case CONNMAN_SERVICE_TYPE_VPN: service->do_split_routing = g_key_file_get_boolean(keyfile, service->identifier, "SplitRouting", NULL); + autoconnect = g_key_file_get_boolean(keyfile, + service->identifier, "AutoConnect", &error); + if (!error) + service->autoconnect = autoconnect; + g_clear_error(&error); break; case CONNMAN_SERVICE_TYPE_WIFI: - if (service->name == NULL) { + if (!service->name) { gchar *name; name = g_key_file_get_string(keyfile, service->identifier, "Name", NULL); - if (name != NULL) { + if (name) { g_free(service->name); service->name = name; } - if (service->network != NULL) + if (service->network) connman_network_set_name(service->network, name); } if (service->network && - connman_network_get_blob(service->network, - "WiFi.SSID", &ssid_len) == NULL) { + !connman_network_get_blob(service->network, + "WiFi.SSID", &ssid_len)) { gchar *hex_ssid; hex_ssid = g_key_file_get_string(keyfile, service->identifier, "SSID", NULL); - if (hex_ssid != NULL) { + if (hex_ssid) { gchar *ssid; unsigned int i, j = 0, hex; size_t hex_ssid_len = strlen(hex_ssid); ssid = g_try_malloc0(hex_ssid_len / 2); - if (ssid == NULL) { + if (!ssid) { g_free(hex_ssid); err = -ENOMEM; goto done; @@ -400,6 +474,7 @@ static int service_load(struct connman_service *service) } /* fall through */ + case CONNMAN_SERVICE_TYPE_GADGET: case CONNMAN_SERVICE_TYPE_BLUETOOTH: case CONNMAN_SERVICE_TYPE_CELLULAR: service->favorite = g_key_file_get_boolean(keyfile, @@ -407,8 +482,8 @@ static int service_load(struct connman_service *service) str = g_key_file_get_string(keyfile, service->identifier, "Failure", NULL); - if (str != NULL) { - if (service->favorite == FALSE) + if (str) { + if (!service->favorite) service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_FAILURE; service->error = string2error(str); @@ -419,7 +494,7 @@ static int service_load(struct connman_service *service) case CONNMAN_SERVICE_TYPE_ETHERNET: autoconnect = g_key_file_get_boolean(keyfile, service->identifier, "AutoConnect", &error); - if (error == NULL) + if (!error) service->autoconnect = autoconnect; g_clear_error(&error); break; @@ -427,71 +502,71 @@ static int service_load(struct connman_service *service) str = g_key_file_get_string(keyfile, service->identifier, "Modified", NULL); - if (str != NULL) { + if (str) { g_time_val_from_iso8601(str, &service->modified); g_free(str); } str = g_key_file_get_string(keyfile, service->identifier, "Passphrase", NULL); - if (str != NULL) { + if (str) { g_free(service->passphrase); service->passphrase = str; } - if (service->ipconfig_ipv4 != NULL) + if (service->ipconfig_ipv4) __connman_ipconfig_load(service->ipconfig_ipv4, keyfile, service->identifier, "IPv4."); - if (service->ipconfig_ipv6 != NULL) + if (service->ipconfig_ipv6) __connman_ipconfig_load(service->ipconfig_ipv6, keyfile, service->identifier, "IPv6."); service->nameservers_config = g_key_file_get_string_list(keyfile, service->identifier, "Nameservers", &length, NULL); - if (service->nameservers_config != NULL && length == 0) { + if (service->nameservers_config && length == 0) { g_strfreev(service->nameservers_config); service->nameservers_config = NULL; } service->timeservers_config = g_key_file_get_string_list(keyfile, service->identifier, "Timeservers", &length, NULL); - if (service->timeservers_config != NULL && length == 0) { + if (service->timeservers_config && length == 0) { g_strfreev(service->timeservers_config); service->timeservers_config = NULL; } service->domains = g_key_file_get_string_list(keyfile, service->identifier, "Domains", &length, NULL); - if (service->domains != NULL && length == 0) { + if (service->domains && length == 0) { g_strfreev(service->domains); service->domains = NULL; } str = g_key_file_get_string(keyfile, service->identifier, "Proxy.Method", NULL); - if (str != NULL) + if (str) service->proxy_config = string2proxymethod(str); g_free(str); service->proxies = g_key_file_get_string_list(keyfile, service->identifier, "Proxy.Servers", &length, NULL); - if (service->proxies != NULL && length == 0) { + if (service->proxies && length == 0) { g_strfreev(service->proxies); service->proxies = NULL; } service->excludes = g_key_file_get_string_list(keyfile, service->identifier, "Proxy.Excludes", &length, NULL); - if (service->excludes != NULL && length == 0) { + if (service->excludes && length == 0) { g_strfreev(service->excludes); service->excludes = NULL; } str = g_key_file_get_string(keyfile, service->identifier, "Proxy.URL", NULL); - if (str != NULL) { + if (str) { g_free(service->pac); service->pac = str; } @@ -515,14 +590,14 @@ static int service_save(struct connman_service *service) DBG("service %p new %d", service, service->new_service); - if (service->new_service == TRUE) + if (service->new_service) return -ESRCH; keyfile = __connman_storage_open_service(service->identifier); - if (keyfile == NULL) + if (!keyfile) return -EIO; - if (service->name != NULL) + if (service->name) g_key_file_set_string(keyfile, service->identifier, "Name", service->name); @@ -530,11 +605,14 @@ static int service_save(struct connman_service *service) case CONNMAN_SERVICE_TYPE_UNKNOWN: case CONNMAN_SERVICE_TYPE_SYSTEM: case CONNMAN_SERVICE_TYPE_GPS: - case CONNMAN_SERVICE_TYPE_GADGET: + case CONNMAN_SERVICE_TYPE_P2P: break; case CONNMAN_SERVICE_TYPE_VPN: g_key_file_set_boolean(keyfile, service->identifier, "SplitRouting", service->do_split_routing); + if (service->favorite) + g_key_file_set_boolean(keyfile, service->identifier, + "AutoConnect", service->autoconnect); break; case CONNMAN_SERVICE_TYPE_WIFI: if (service->network) { @@ -544,13 +622,13 @@ static int service_save(struct connman_service *service) ssid = connman_network_get_blob(service->network, "WiFi.SSID", &ssid_len); - if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') { + if (ssid && ssid_len > 0 && ssid[0] != '\0') { char *identifier = service->identifier; GString *ssid_str; unsigned int i; ssid_str = g_string_sized_new(ssid_len * 2); - if (ssid_str == NULL) { + if (!ssid_str) { err = -ENOMEM; goto done; } @@ -571,6 +649,7 @@ static int service_save(struct connman_service *service) } /* fall through */ + case CONNMAN_SERVICE_TYPE_GADGET: case CONNMAN_SERVICE_TYPE_BLUETOOTH: case CONNMAN_SERVICE_TYPE_CELLULAR: g_key_file_set_boolean(keyfile, service->identifier, @@ -579,7 +658,7 @@ static int service_save(struct connman_service *service) if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE || service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) { const char *failure = error2string(service->error); - if (failure != NULL) + if (failure) g_key_file_set_string(keyfile, service->identifier, "Failure", failure); @@ -590,35 +669,35 @@ static int service_save(struct connman_service *service) /* fall through */ case CONNMAN_SERVICE_TYPE_ETHERNET: - if (service->favorite == TRUE) + if (service->favorite) g_key_file_set_boolean(keyfile, service->identifier, "AutoConnect", service->autoconnect); break; } str = g_time_val_to_iso8601(&service->modified); - if (str != NULL) { + if (str) { g_key_file_set_string(keyfile, service->identifier, "Modified", str); g_free(str); } - if (service->passphrase != NULL && strlen(service->passphrase) > 0) + if (service->passphrase && strlen(service->passphrase) > 0) g_key_file_set_string(keyfile, service->identifier, "Passphrase", service->passphrase); else g_key_file_remove_key(keyfile, service->identifier, "Passphrase", NULL); - if (service->ipconfig_ipv4 != NULL) + if (service->ipconfig_ipv4) __connman_ipconfig_save(service->ipconfig_ipv4, keyfile, service->identifier, "IPv4."); - if (service->ipconfig_ipv6 != NULL) + if (service->ipconfig_ipv6) __connman_ipconfig_save(service->ipconfig_ipv6, keyfile, service->identifier, "IPv6."); - if (service->nameservers_config != NULL) { + if (service->nameservers_config) { guint len = g_strv_length(service->nameservers_config); g_key_file_set_string_list(keyfile, service->identifier, @@ -628,7 +707,7 @@ static int service_save(struct connman_service *service) g_key_file_remove_key(keyfile, service->identifier, "Nameservers", NULL); - if (service->timeservers_config != NULL) { + if (service->timeservers_config) { guint len = g_strv_length(service->timeservers_config); g_key_file_set_string_list(keyfile, service->identifier, @@ -638,7 +717,7 @@ static int service_save(struct connman_service *service) g_key_file_remove_key(keyfile, service->identifier, "Timeservers", NULL); - if (service->domains != NULL) { + if (service->domains) { guint len = g_strv_length(service->domains); g_key_file_set_string_list(keyfile, service->identifier, @@ -649,11 +728,11 @@ static int service_save(struct connman_service *service) "Domains", NULL); cst_str = proxymethod2string(service->proxy_config); - if (cst_str != NULL) + if (cst_str) g_key_file_set_string(keyfile, service->identifier, "Proxy.Method", cst_str); - if (service->proxies != NULL) { + if (service->proxies) { guint len = g_strv_length(service->proxies); g_key_file_set_string_list(keyfile, service->identifier, @@ -663,7 +742,7 @@ static int service_save(struct connman_service *service) g_key_file_remove_key(keyfile, service->identifier, "Proxy.Servers", NULL); - if (service->excludes != NULL) { + if (service->excludes) { guint len = g_strv_length(service->excludes); g_key_file_set_string_list(keyfile, service->identifier, @@ -673,22 +752,22 @@ static int service_save(struct connman_service *service) g_key_file_remove_key(keyfile, service->identifier, "Proxy.Excludes", NULL); - if (service->pac != NULL && strlen(service->pac) > 0) + if (service->pac && strlen(service->pac) > 0) g_key_file_set_string(keyfile, service->identifier, "Proxy.URL", service->pac); else g_key_file_remove_key(keyfile, service->identifier, "Proxy.URL", NULL); - if (service->hidden_service == TRUE) + if (service->hidden_service) g_key_file_set_boolean(keyfile, service->identifier, "Hidden", TRUE); - if (service->config_file != NULL && strlen(service->config_file) > 0) + if (service->config_file && strlen(service->config_file) > 0) g_key_file_set_string(keyfile, service->identifier, "Config.file", service->config_file); - if (service->config_entry != NULL && + if (service->config_entry && strlen(service->config_entry) > 0) g_key_file_set_string(keyfile, service->identifier, "Config.ident", service->config_entry); @@ -703,6 +782,9 @@ done: void __connman_service_save(struct connman_service *service) { + if (!service) + return; + service_save(service); } @@ -793,14 +875,14 @@ done: return result; } -static connman_bool_t is_connecting_state(struct connman_service *service, +static bool is_connecting_state(struct connman_service *service, enum connman_service_state state) { switch (state) { case CONNMAN_SERVICE_STATE_UNKNOWN: case CONNMAN_SERVICE_STATE_IDLE: case CONNMAN_SERVICE_STATE_FAILURE: - if (service->network != NULL) + if (service->network) return connman_network_get_connecting(service->network); case CONNMAN_SERVICE_STATE_DISCONNECT: case CONNMAN_SERVICE_STATE_READY: @@ -808,13 +890,13 @@ static connman_bool_t is_connecting_state(struct connman_service *service, break; case CONNMAN_SERVICE_STATE_ASSOCIATION: case CONNMAN_SERVICE_STATE_CONFIGURATION: - return TRUE; + return true; } - return FALSE; + return false; } -static connman_bool_t is_connected_state(const struct connman_service *service, +static bool is_connected_state(const struct connman_service *service, enum connman_service_state state) { switch (state) { @@ -827,13 +909,13 @@ static connman_bool_t is_connected_state(const struct connman_service *service, break; case CONNMAN_SERVICE_STATE_READY: case CONNMAN_SERVICE_STATE_ONLINE: - return TRUE; + return true; } - return FALSE; + return false; } -static connman_bool_t is_idle_state(const struct connman_service *service, +static bool is_idle_state(const struct connman_service *service, enum connman_service_state state) { switch (state) { @@ -846,31 +928,24 @@ static connman_bool_t is_idle_state(const struct connman_service *service, case CONNMAN_SERVICE_STATE_FAILURE: break; case CONNMAN_SERVICE_STATE_IDLE: - return TRUE; + return true; } - return FALSE; + return false; } -static connman_bool_t is_connecting(struct connman_service *service) +static bool is_connecting(struct connman_service *service) { return is_connecting_state(service, service->state); } -static connman_bool_t is_connected(struct connman_service *service) +static bool is_connected(struct connman_service *service) { return is_connected_state(service, service->state); } static int nameserver_get_index(struct connman_service *service) { - int index; - - index = __connman_service_get_index(service); - - if (index < 0) - return -1; - switch (combine_state(service->state_ipv4, service->state_ipv6)) { case CONNMAN_SERVICE_STATE_UNKNOWN: case CONNMAN_SERVICE_STATE_IDLE: @@ -884,7 +959,7 @@ static int nameserver_get_index(struct connman_service *service) break; } - return index; + return __connman_service_get_index(service); } static void remove_nameservers(struct connman_service *service, @@ -892,7 +967,7 @@ static void remove_nameservers(struct connman_service *service, { int i; - if (ns == NULL) + if (!ns) return; if (index < 0) @@ -901,7 +976,7 @@ static void remove_nameservers(struct connman_service *service, if (index < 0) return; - for (i = 0; ns[i] != NULL; i++) + for (i = 0; ns[i]; i++) connman_resolver_remove(index, NULL, ns[i]); } @@ -910,7 +985,7 @@ static void remove_searchdomains(struct connman_service *service, { int i; - if (sd == NULL) + if (!sd) return; if (index < 0) @@ -919,13 +994,29 @@ static void remove_searchdomains(struct connman_service *service, if (index < 0) return; - for (i = 0; sd[i] != NULL; i++) + for (i = 0; sd[i]; i++) connman_resolver_remove(index, sd[i], NULL); } +static bool nameserver_available(struct connman_service *service, char *ns) +{ + int family; + + family = connman_inet_check_ipaddress(ns); + + if (family == AF_INET) + return is_connected_state(service, service->state_ipv4); + + if (family == AF_INET6) + return is_connected_state(service, service->state_ipv6); + + return false; +} + static void update_nameservers(struct connman_service *service) { int index; + char *ns; index = __connman_service_get_index(service); if (index < 0) @@ -946,7 +1037,7 @@ static void update_nameservers(struct connman_service *service) break; } - if (service->nameservers_config != NULL) { + if (service->nameservers_config) { int i; remove_nameservers(service, index, service->nameservers); @@ -954,21 +1045,29 @@ static void update_nameservers(struct connman_service *service) i = g_strv_length(service->nameservers_config); while (i != 0) { i--; - connman_resolver_append(index, NULL, - service->nameservers_config[i]); + + ns = service->nameservers_config[i]; + + if (nameserver_available(service, ns)) + connman_resolver_append(index, NULL, ns); } - } else if (service->nameservers != NULL) { + } else if (service->nameservers) { int i; + remove_nameservers(service, index, service->nameservers); + i = g_strv_length(service->nameservers); while (i != 0) { i--; - connman_resolver_append(index, NULL, - service->nameservers[i]); + + ns = service->nameservers[i]; + + if (nameserver_available(service, ns)) + connman_resolver_append(index, NULL, ns); } } - if (service->domains != NULL) { + if (service->domains) { char *searchdomains[2] = {NULL, NULL}; int i; @@ -981,7 +1080,7 @@ static void update_nameservers(struct connman_service *service) connman_resolver_append(index, service->domains[i], NULL); } - } else if (service->domainname != NULL) + } else if (service->domainname) connman_resolver_append(index, service->domainname, NULL); connman_resolver_flush(); @@ -993,26 +1092,26 @@ static void update_nameservers(struct connman_service *service) * for details) and not through service.c */ int __connman_service_nameserver_append(struct connman_service *service, - const char *nameserver, gboolean is_auto) + const char *nameserver, bool is_auto) { char **nameservers; int len, i; DBG("service %p nameserver %s auto %d", service, nameserver, is_auto); - if (nameserver == NULL) + if (!nameserver) return -EINVAL; - if (is_auto == TRUE) + if (is_auto) nameservers = service->nameservers_auto; else nameservers = service->nameservers; - for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++) + for (i = 0; nameservers && nameservers[i]; i++) if (g_strcmp0(nameservers[i], nameserver) == 0) return -EEXIST; - if (nameservers != NULL) { + if (nameservers) { len = g_strv_length(nameservers); nameservers = g_try_renew(char *, nameservers, len + 2); } else { @@ -1020,16 +1119,16 @@ int __connman_service_nameserver_append(struct connman_service *service, nameservers = g_try_new0(char *, len + 2); } - if (nameservers == NULL) + if (!nameservers) return -ENOMEM; nameservers[len] = g_strdup(nameserver); - if (nameservers[len] == NULL) + if (!nameservers[len]) return -ENOMEM; nameservers[len + 1] = NULL; - if (is_auto == TRUE) { + if (is_auto) { service->nameservers_auto = nameservers; } else { service->nameservers = nameservers; @@ -1040,39 +1139,39 @@ int __connman_service_nameserver_append(struct connman_service *service, } int __connman_service_nameserver_remove(struct connman_service *service, - const char *nameserver, gboolean is_auto) + const char *nameserver, bool is_auto) { char **servers, **nameservers; - gboolean found = FALSE; + bool found = false; int len, i, j; DBG("service %p nameserver %s auto %d", service, nameserver, is_auto); - if (nameserver == NULL) + if (!nameserver) return -EINVAL; - if (is_auto == TRUE) + if (is_auto) nameservers = service->nameservers_auto; else nameservers = service->nameservers; - if (nameservers == NULL) + if (!nameservers) return 0; - for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++) + for (i = 0; nameservers && nameservers[i]; i++) if (g_strcmp0(nameservers[i], nameserver) == 0) { - found = TRUE; + found = true; break; } - if (found == FALSE) + if (!found) return 0; len = g_strv_length(nameservers); if (len == 1) { g_strfreev(nameservers); - if (is_auto == TRUE) + if (is_auto) service->nameservers_auto = NULL; else service->nameservers = NULL; @@ -1081,13 +1180,13 @@ int __connman_service_nameserver_remove(struct connman_service *service, } servers = g_try_new0(char *, len); - if (servers == NULL) + if (!servers) return -ENOMEM; for (i = 0, j = 0; i < len; i++) { if (g_strcmp0(nameservers[i], nameserver) != 0) { servers[j] = g_strdup(nameservers[i]); - if (servers[j] == NULL) + if (!servers[j]) return -ENOMEM; j++; } @@ -1097,7 +1196,7 @@ int __connman_service_nameserver_remove(struct connman_service *service, g_strfreev(nameservers); nameservers = servers; - if (is_auto == TRUE) { + if (is_auto) { service->nameservers_auto = nameservers; } else { service->nameservers = nameservers; @@ -1120,7 +1219,7 @@ static void add_nameserver_route(int family, int index, char *nameserver, { switch (family) { case AF_INET: - if (connman_inet_compare_subnet(index, nameserver) == TRUE) + if (connman_inet_compare_subnet(index, nameserver)) break; if (connman_inet_add_host_route(index, nameserver, gw) < 0) @@ -1142,7 +1241,7 @@ static void nameserver_add_routes(int index, char **nameservers, { int i, family; - for (i = 0; nameservers[i] != NULL; i++) { + for (i = 0; nameservers[i]; i++) { family = connman_inet_check_ipaddress(nameservers[i]); if (family < 0) continue; @@ -1156,7 +1255,7 @@ static void nameserver_del_routes(int index, char **nameservers, { int i, family; - for (i = 0; nameservers[i] != NULL; i++) { + for (i = 0; nameservers[i]; i++) { family = connman_inet_check_ipaddress(nameservers[i]); if (family < 0) continue; @@ -1179,23 +1278,20 @@ static void nameserver_del_routes(int index, char **nameservers, void __connman_service_nameserver_add_routes(struct connman_service *service, const char *gw) { - int index = -1; + int index; - if (service == NULL) + if (!service) return; - if (service->network != NULL) - index = connman_network_get_index(service->network); - else if (service->provider != NULL) - index = connman_provider_get_index(service->provider); + index = __connman_service_get_index(service); - if (service->nameservers_config != NULL) { + if (service->nameservers_config) { /* * Configured nameserver takes preference over the * discoverd nameserver gathered from DHCP, VPN, etc. */ nameserver_add_routes(index, service->nameservers_config, gw); - } else if (service->nameservers != NULL) { + } else if (service->nameservers) { /* * We add nameservers host routes for nameservers that * are not on our subnet. For those who are, the subnet @@ -1210,32 +1306,29 @@ void __connman_service_nameserver_add_routes(struct connman_service *service, void __connman_service_nameserver_del_routes(struct connman_service *service, enum connman_ipconfig_type type) { - int index = -1; + int index; - if (service == NULL) + if (!service) return; - if (service->network != NULL) - index = connman_network_get_index(service->network); - else if (service->provider != NULL) - index = connman_provider_get_index(service->provider); + index = __connman_service_get_index(service); - if (service->nameservers_config != NULL) + if (service->nameservers_config) nameserver_del_routes(index, service->nameservers_config, type); - else if (service->nameservers != NULL) + else if (service->nameservers) nameserver_del_routes(index, service->nameservers, type); } static struct connman_stats *stats_get(struct connman_service *service) { - if (service->roaming == TRUE) + if (service->roaming) return &service->stats_roaming; else return &service->stats; } -static connman_bool_t stats_enabled(struct connman_service *service) +static bool stats_enabled(struct connman_service *service) { struct connman_stats *stats = stats_get(service); @@ -1248,10 +1341,10 @@ static void stats_start(struct connman_service *service) DBG("service %p", service); - if (stats->timer == NULL) + if (!stats->timer) return; - stats->enabled = TRUE; + stats->enabled = true; stats->data_last.time = stats->data.time; g_timer_start(stats->timer); @@ -1264,10 +1357,10 @@ static void stats_stop(struct connman_service *service) DBG("service %p", service); - if (stats->timer == NULL) + if (!stats->timer) return; - if (stats->enabled == FALSE) + if (!stats->enabled) return; g_timer_stop(stats->timer); @@ -1275,7 +1368,7 @@ static void stats_stop(struct connman_service *service) seconds = g_timer_elapsed(stats->timer, NULL); stats->data.time = stats->data_last.time + seconds; - stats->enabled = FALSE; + stats->enabled = false; } static void reset_stats(struct connman_service *service) @@ -1283,7 +1376,7 @@ static void reset_stats(struct connman_service *service) DBG("service %p", service); /* home */ - service->stats.valid = FALSE; + service->stats.valid = false; service->stats.data.rx_packets = 0; service->stats.data.tx_packets = 0; @@ -1299,7 +1392,7 @@ static void reset_stats(struct connman_service *service) g_timer_reset(service->stats.timer); /* roaming */ - service->stats_roaming.valid = FALSE; + service->stats_roaming.valid = false; service->stats_roaming.data.rx_packets = 0; service->stats_roaming.data.tx_packets = 0; @@ -1319,17 +1412,29 @@ struct connman_service *__connman_service_get_default(void) { struct connman_service *service; - if (service_list == NULL) + if (!service_list) return NULL; service = service_list->data; - if (is_connected(service) == FALSE) + if (!is_connected(service)) return NULL; return service; } +bool __connman_service_index_is_default(int index) +{ + struct connman_service *service; + + if (index < 0) + return false; + + service = __connman_service_get_default(); + + return __connman_service_get_index(service) == index; +} + static void default_changed(void) { struct connman_service *service = __connman_service_get_default(); @@ -1337,10 +1442,23 @@ static void default_changed(void) if (service == current_default) return; + DBG("current default %p %s", current_default, + current_default ? current_default->identifier : ""); + DBG("new default %p %s", service, service ? service->identifier : ""); + __connman_service_timeserver_changed(current_default, NULL); current_default = service; + if (service) { + if (service->hostname && + connman_setting_get_bool("AllowHostnameUpdates")) + __connman_utsname_set_hostname(service->hostname); + + if (service->domainname) + __connman_utsname_set_domainname(service->domainname); + } + __connman_notifier_default_changed(service); } @@ -1351,10 +1469,10 @@ static void state_changed(struct connman_service *service) __connman_notifier_service_state_changed(service, service->state); str = state2string(service->state); - if (str == NULL) + if (!str) return; - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_basic(service->path, @@ -1367,7 +1485,7 @@ static void strength_changed(struct connman_service *service) if (service->strength == 0) return; - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_basic(service->path, @@ -1377,54 +1495,66 @@ static void strength_changed(struct connman_service *service) static void favorite_changed(struct connman_service *service) { - if (service->path == NULL) + dbus_bool_t favorite; + + if (!service->path) return; - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; + favorite = service->favorite; connman_dbus_property_changed_basic(service->path, CONNMAN_SERVICE_INTERFACE, "Favorite", - DBUS_TYPE_BOOLEAN, &service->favorite); + DBUS_TYPE_BOOLEAN, &favorite); } static void immutable_changed(struct connman_service *service) { - if (service->path == NULL) + dbus_bool_t immutable; + + if (!service->path) return; - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; + immutable = service->immutable; connman_dbus_property_changed_basic(service->path, CONNMAN_SERVICE_INTERFACE, "Immutable", - DBUS_TYPE_BOOLEAN, &service->immutable); + DBUS_TYPE_BOOLEAN, &immutable); } static void roaming_changed(struct connman_service *service) { - if (service->path == NULL) + dbus_bool_t roaming; + + if (!service->path) return; - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; + roaming = service->roaming; connman_dbus_property_changed_basic(service->path, CONNMAN_SERVICE_INTERFACE, "Roaming", - DBUS_TYPE_BOOLEAN, &service->roaming); + DBUS_TYPE_BOOLEAN, &roaming); } static void autoconnect_changed(struct connman_service *service) { - if (service->path == NULL) + dbus_bool_t autoconnect; + + if (!service->path) return; - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; + autoconnect = service->autoconnect; connman_dbus_property_changed_basic(service->path, CONNMAN_SERVICE_INTERFACE, "AutoConnect", - DBUS_TYPE_BOOLEAN, &service->autoconnect); + DBUS_TYPE_BOOLEAN, &autoconnect); } static void append_security(DBusMessageIter *iter, void *user_data) @@ -1433,7 +1563,7 @@ static void append_security(DBusMessageIter *iter, void *user_data) const char *str; str = security2string(service->security); - if (str != NULL) + if (str) dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str); @@ -1442,14 +1572,14 @@ static void append_security(DBusMessageIter *iter, void *user_data) * are configured as open or no security, so filter * appropriately. */ - if (service->wps == TRUE) { + if (service->wps) { switch (service->security) { case CONNMAN_SERVICE_SECURITY_PSK: case CONNMAN_SERVICE_SECURITY_WPA: case CONNMAN_SERVICE_SECURITY_RSN: str = "wps"; dbus_message_iter_append_basic(iter, - DBUS_TYPE_STRING, &str); + DBUS_TYPE_STRING, &str); break; case CONNMAN_SERVICE_SECURITY_UNKNOWN: case CONNMAN_SERVICE_SECURITY_NONE: @@ -1464,10 +1594,10 @@ static void append_ethernet(DBusMessageIter *iter, void *user_data) { struct connman_service *service = user_data; - if (service->ipconfig_ipv4 != NULL) + if (service->ipconfig_ipv4) __connman_ipconfig_append_ethernet(service->ipconfig_ipv4, iter); - else if (service->ipconfig_ipv6 != NULL) + else if (service->ipconfig_ipv6) __connman_ipconfig_append_ethernet(service->ipconfig_ipv6, iter); } @@ -1476,13 +1606,10 @@ static void append_ipv4(DBusMessageIter *iter, void *user_data) { struct connman_service *service = user_data; - DBG("ipv4 %p state %s", service->ipconfig_ipv4, - state2string(service->state_ipv4)); - - if (is_connected_state(service, service->state_ipv4) == FALSE) + if (!is_connected_state(service, service->state_ipv4)) return; - if (service->ipconfig_ipv4 != NULL) + if (service->ipconfig_ipv4) __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter); } @@ -1490,13 +1617,10 @@ static void append_ipv6(DBusMessageIter *iter, void *user_data) { struct connman_service *service = user_data; - DBG("ipv6 %p state %s", service->ipconfig_ipv6, - state2string(service->state_ipv6)); - - if (is_connected_state(service, service->state_ipv6) == FALSE) + if (!is_connected_state(service, service->state_ipv6)) return; - if (service->ipconfig_ipv6 != NULL) + if (service->ipconfig_ipv6) __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter, service->ipconfig_ipv4); } @@ -1505,7 +1629,7 @@ static void append_ipv4config(DBusMessageIter *iter, void *user_data) { struct connman_service *service = user_data; - if (service->ipconfig_ipv4 != NULL) + if (service->ipconfig_ipv4) __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4, iter); } @@ -1514,20 +1638,25 @@ static void append_ipv6config(DBusMessageIter *iter, void *user_data) { struct connman_service *service = user_data; - if (service->ipconfig_ipv6 != NULL) + if (service->ipconfig_ipv6) __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6, iter); } -static void append_nameservers(DBusMessageIter *iter, char **servers) +static void append_nameservers(DBusMessageIter *iter, + struct connman_service *service, char **servers) { int i; + bool available = true; - DBG("%p", servers); + for (i = 0; servers[i]; i++) { + if (service) + available = nameserver_available(service, servers[i]); - for (i = 0; servers[i] != NULL; i++) { - DBG("servers[%d] %s", i, servers[i]); - dbus_message_iter_append_basic(iter, + DBG("servers[%d] %s available %d", i, servers[i], available); + + if (available) + dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &servers[i]); } } @@ -1536,18 +1665,30 @@ static void append_dns(DBusMessageIter *iter, void *user_data) { struct connman_service *service = user_data; - if (is_connected(service) == FALSE) + if (!is_connected(service)) return; - if (service->nameservers_config != NULL) { - append_nameservers(iter, service->nameservers_config); + if (service->nameservers_config) { + append_nameservers(iter, service, service->nameservers_config); return; } else { - if (service->nameservers != NULL) - append_nameservers(iter, service->nameservers); + if (service->nameservers) + append_nameservers(iter, service, + service->nameservers); + + if (service->nameservers_auto) + append_nameservers(iter, service, + service->nameservers_auto); + + if (!service->nameservers && !service->nameservers_auto) { + char **ns; - if (service->nameservers_auto != NULL) - append_nameservers(iter, service->nameservers_auto); + DBG("append fallback nameservers"); + + ns = connman_setting_get_string_list("FallbackNameservers"); + if (ns) + append_nameservers(iter, service, ns); + } } } @@ -1555,20 +1696,20 @@ static void append_dnsconfig(DBusMessageIter *iter, void *user_data) { struct connman_service *service = user_data; - if (service->nameservers_config == NULL) + if (!service->nameservers_config) return; - append_nameservers(iter, service->nameservers_config); + append_nameservers(iter, NULL, service->nameservers_config); } static void append_ts(DBusMessageIter *iter, void *user_data) { GSList *list = user_data; - while (list != NULL) { + while (list) { char *timeserver = list->data; - if (timeserver != NULL) + if (timeserver) dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, ×erver); @@ -1581,7 +1722,7 @@ static void append_tsconfig(DBusMessageIter *iter, void *user_data) struct connman_service *service = user_data; int i; - if (service->timeservers_config == NULL) + if (!service->timeservers_config) return; for (i = 0; service->timeservers_config[i]; i++) { @@ -1596,7 +1737,7 @@ static void append_domainconfig(DBusMessageIter *iter, void *user_data) struct connman_service *service = user_data; int i; - if (service->domains == NULL) + if (!service->domains) return; for (i = 0; service->domains[i]; i++) @@ -1608,13 +1749,13 @@ static void append_domain(DBusMessageIter *iter, void *user_data) { struct connman_service *service = user_data; - if (is_connected(service) == FALSE && - is_connecting(service) == FALSE) + if (!is_connected(service) && + !is_connecting(service)) return; - if (service->domains != NULL) + if (service->domains) append_domainconfig(iter, user_data); - else if (service->domainname != NULL) + else if (service->domainname) dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &service->domainname); } @@ -1624,7 +1765,7 @@ static void append_proxies(DBusMessageIter *iter, void *user_data) struct connman_service *service = user_data; int i; - if (service->proxies == NULL) + if (!service->proxies) return; for (i = 0; service->proxies[i]; i++) @@ -1637,7 +1778,7 @@ static void append_excludes(DBusMessageIter *iter, void *user_data) struct connman_service *service = user_data; int i; - if (service->excludes == NULL) + if (!service->excludes) return; for (i = 0; service->excludes[i]; i++) @@ -1653,9 +1794,7 @@ static void append_proxy(DBusMessageIter *iter, void *user_data) const char *method = proxymethod2string( CONNMAN_SERVICE_PROXY_METHOD_DIRECT); - DBG(""); - - if (is_connected(service) == FALSE) + if (!is_connected(service)) return; proxy = connman_service_get_proxy_method(service); @@ -1676,17 +1815,17 @@ static void append_proxy(DBusMessageIter *iter, void *user_data) break; case CONNMAN_SERVICE_PROXY_METHOD_AUTO: /* Maybe DHCP, or WPAD, has provided an url for a pac file */ - if (service->ipconfig_ipv4 != NULL) + if (service->ipconfig_ipv4) pac = __connman_ipconfig_get_proxy_autoconfig( service->ipconfig_ipv4); - else if (service->ipconfig_ipv6 != NULL) + else if (service->ipconfig_ipv6) pac = __connman_ipconfig_get_proxy_autoconfig( service->ipconfig_ipv6); - if (service->pac == NULL && pac == NULL) + if (!service->pac && !pac) goto done; - if (service->pac != NULL) + if (service->pac) pac = service->pac; connman_dbus_dict_append_basic(iter, "URL", @@ -1715,18 +1854,18 @@ static void append_proxyconfig(DBusMessageIter *iter, void *user_data) case CONNMAN_SERVICE_PROXY_METHOD_DIRECT: break; case CONNMAN_SERVICE_PROXY_METHOD_MANUAL: - if (service->proxies != NULL) + if (service->proxies) connman_dbus_dict_append_array(iter, "Servers", DBUS_TYPE_STRING, append_proxies, service); - if (service->excludes != NULL) + if (service->excludes) connman_dbus_dict_append_array(iter, "Excludes", DBUS_TYPE_STRING, append_excludes, service); break; case CONNMAN_SERVICE_PROXY_METHOD_AUTO: - if (service->pac != NULL) + if (service->pac) connman_dbus_dict_append_basic(iter, "URL", DBUS_TYPE_STRING, &service->pac); break; @@ -1742,12 +1881,10 @@ static void append_provider(DBusMessageIter *iter, void *user_data) { struct connman_service *service = user_data; - DBG("%p %p", service, service->provider); - - if (is_connected(service) == FALSE) + if (!is_connected(service)) return; - if (service->provider != NULL) + if (service->provider) __connman_provider_append_properties(service->provider, iter); } @@ -1757,7 +1894,7 @@ static void settings_changed(struct connman_service *service, { enum connman_ipconfig_type type; - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; type = __connman_ipconfig_get_config_type(ipconfig); @@ -1776,7 +1913,7 @@ static void settings_changed(struct connman_service *service, static void ipv4_configuration_changed(struct connman_service *service) { - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_dict(service->path, @@ -1788,7 +1925,7 @@ static void ipv4_configuration_changed(struct connman_service *service) static void ipv6_configuration_changed(struct connman_service *service) { - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_dict(service->path, @@ -1800,7 +1937,7 @@ static void ipv6_configuration_changed(struct connman_service *service) static void dns_changed(struct connman_service *service) { - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_array(service->path, @@ -1810,7 +1947,7 @@ static void dns_changed(struct connman_service *service) static void dns_configuration_changed(struct connman_service *service) { - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_array(service->path, @@ -1823,7 +1960,7 @@ static void dns_configuration_changed(struct connman_service *service) static void domain_changed(struct connman_service *service) { - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_array(service->path, @@ -1833,7 +1970,7 @@ static void domain_changed(struct connman_service *service) static void domain_configuration_changed(struct connman_service *service) { - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_array(service->path, @@ -1844,7 +1981,7 @@ static void domain_configuration_changed(struct connman_service *service) static void proxy_changed(struct connman_service *service) { - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_dict(service->path, @@ -1854,7 +1991,7 @@ static void proxy_changed(struct connman_service *service) static void proxy_configuration_changed(struct connman_service *service) { - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_dict(service->path, @@ -1866,7 +2003,7 @@ static void proxy_configuration_changed(struct connman_service *service) static void timeservers_configuration_changed(struct connman_service *service) { - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_array(service->path, @@ -1878,7 +2015,7 @@ static void timeservers_configuration_changed(struct connman_service *service) static void link_changed(struct connman_service *service) { - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_dict(service->path, @@ -1889,7 +2026,7 @@ static void link_changed(struct connman_service *service) static void stats_append_counters(DBusMessageIter *dict, struct connman_stats_data *stats, struct connman_stats_data *counters, - connman_bool_t append_all) + bool append_all) { if (counters->rx_packets != stats->rx_packets || append_all) { counters->rx_packets = stats->rx_packets; @@ -1949,7 +2086,7 @@ static void stats_append_counters(DBusMessageIter *dict, static void stats_append(struct connman_service *service, const char *counter, struct connman_stats_counter *counters, - connman_bool_t append_all) + bool append_all) { DBusMessageIter array, dict; DBusMessage *msg; @@ -1957,7 +2094,7 @@ static void stats_append(struct connman_service *service, DBG("service %p counter %s", service, counter); msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL); - if (msg == NULL) + if (!msg) return; dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, @@ -1997,7 +2134,7 @@ static void stats_update(struct connman_service *service, DBG("service %p", service); - if (stats->valid == TRUE) { + if (stats->valid) { data->rx_packets += rx_packets - data_last->rx_packets; data->tx_packets += @@ -2015,7 +2152,7 @@ static void stats_update(struct connman_service *service, data->tx_dropped += tx_dropped - data_last->tx_dropped; } else { - stats->valid = TRUE; + stats->valid = true; } data_last->rx_packets = rx_packets; @@ -2044,10 +2181,10 @@ void __connman_service_notify(struct connman_service *service, struct connman_stats_data *data; int err; - if (service == NULL) + if (!service) return; - if (is_connected(service) == FALSE) + if (!is_connected(service)) return; stats_update(service, @@ -2068,7 +2205,7 @@ void __connman_service_notify(struct connman_service *service, counters = value; stats_append(service, counter, counters, counters->append_all); - counters->append_all = FALSE; + counters->append_all = false; } } @@ -2082,14 +2219,14 @@ int __connman_service_counter_register(const char *counter) counter_list = g_slist_prepend(counter_list, (gpointer)counter); - for (list = service_list; list != NULL; list = list->next) { + for (list = service_list; list; list = list->next) { service = list->data; counters = g_try_new0(struct connman_stats_counter, 1); - if (counters == NULL) + if (!counters) return -ENOMEM; - counters->append_all = TRUE; + counters->append_all = true; g_hash_table_replace(service->counter_table, (gpointer)counter, counters); @@ -2105,7 +2242,7 @@ void __connman_service_counter_unregister(const char *counter) DBG("counter %s", counter); - for (list = service_list; list != NULL; list = list->next) { + for (list = service_list; list; list = list->next) { service = list->data; g_hash_table_remove(service->counter_table, counter); @@ -2118,42 +2255,24 @@ int __connman_service_iterate_services(service_iterate_cb cb, void *user_data) { GList *list; - for (list = service_list; list != NULL; list = list->next) { + for (list = service_list; list; list = list->next) { struct connman_service *service = list->data; - cb(service, service->name, service->state, user_data); + cb(service, user_data); } return 0; } -void __connman_service_session_inc(struct connman_service *service) -{ - DBG("service %p ref count %d", service, - service->session_usage_count + 1); - - __sync_fetch_and_add(&service->session_usage_count, 1); -} - -connman_bool_t __connman_service_session_dec(struct connman_service *service) -{ - DBG("service %p ref count %d", service, - service->session_usage_count - 1); - - if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1) - return FALSE; - - return TRUE; -} - static void append_properties(DBusMessageIter *dict, dbus_bool_t limited, struct connman_service *service) { + dbus_bool_t val; const char *str; GSList *list; str = __connman_service_type2string(service->type); - if (str != NULL) + if (str) connman_dbus_dict_append_basic(dict, "Type", DBUS_TYPE_STRING, &str); @@ -2161,12 +2280,12 @@ static void append_properties(DBusMessageIter *dict, dbus_bool_t limited, DBUS_TYPE_STRING, append_security, service); str = state2string(service->state); - if (str != NULL) + if (str) connman_dbus_dict_append_basic(dict, "State", DBUS_TYPE_STRING, &str); str = error2string(service->error); - if (str != NULL) + if (str) connman_dbus_dict_append_basic(dict, "Error", DBUS_TYPE_STRING, &str); @@ -2174,20 +2293,23 @@ static void append_properties(DBusMessageIter *dict, dbus_bool_t limited, connman_dbus_dict_append_basic(dict, "Strength", DBUS_TYPE_BYTE, &service->strength); + val = service->favorite; connman_dbus_dict_append_basic(dict, "Favorite", - DBUS_TYPE_BOOLEAN, &service->favorite); + DBUS_TYPE_BOOLEAN, &val); + val = service->immutable; connman_dbus_dict_append_basic(dict, "Immutable", - DBUS_TYPE_BOOLEAN, &service->immutable); + DBUS_TYPE_BOOLEAN, &val); - if (service->favorite == TRUE) - connman_dbus_dict_append_basic(dict, "AutoConnect", - DBUS_TYPE_BOOLEAN, &service->autoconnect); + if (service->favorite) + val = service->autoconnect; else - connman_dbus_dict_append_basic(dict, "AutoConnect", - DBUS_TYPE_BOOLEAN, &service->favorite); + val = service->favorite; + + connman_dbus_dict_append_basic(dict, "AutoConnect", + DBUS_TYPE_BOOLEAN, &val); - if (service->name != NULL) + if (service->name) connman_dbus_dict_append_basic(dict, "Name", DBUS_TYPE_STRING, &service->name); @@ -2196,11 +2318,12 @@ static void append_properties(DBusMessageIter *dict, dbus_bool_t limited, case CONNMAN_SERVICE_TYPE_SYSTEM: case CONNMAN_SERVICE_TYPE_GPS: case CONNMAN_SERVICE_TYPE_VPN: - case CONNMAN_SERVICE_TYPE_GADGET: + case CONNMAN_SERVICE_TYPE_P2P: break; case CONNMAN_SERVICE_TYPE_CELLULAR: + val = service->roaming; connman_dbus_dict_append_basic(dict, "Roaming", - DBUS_TYPE_BOOLEAN, &service->roaming); + DBUS_TYPE_BOOLEAN, &val); connman_dbus_dict_append_dict(dict, "Ethernet", append_ethernet, service); @@ -2208,6 +2331,7 @@ static void append_properties(DBusMessageIter *dict, dbus_bool_t limited, case CONNMAN_SERVICE_TYPE_WIFI: case CONNMAN_SERVICE_TYPE_ETHERNET: case CONNMAN_SERVICE_TYPE_BLUETOOTH: + case CONNMAN_SERVICE_TYPE_GADGET: connman_dbus_dict_append_dict(dict, "Ethernet", append_ethernet, service); break; @@ -2270,7 +2394,7 @@ static void append_struct_service(DBusMessageIter *iter, &service->path); connman_dbus_dict_open(&entry, &dict); - if (function != NULL) + if (function) function(&dict, service); connman_dbus_dict_close(&entry, &dict); @@ -2289,7 +2413,7 @@ static void append_struct(gpointer value, gpointer user_data) struct connman_service *service = value; DBusMessageIter *iter = user_data; - if (service->path == NULL) + if (!service->path) return; append_struct_service(iter, append_dict_properties, service); @@ -2300,25 +2424,39 @@ void __connman_service_list_struct(DBusMessageIter *iter) g_list_foreach(service_list, append_struct, iter); } -connman_bool_t __connman_service_is_hidden(struct connman_service *service) +bool __connman_service_is_hidden(struct connman_service *service) { return service->hidden; } -connman_bool_t +bool __connman_service_is_split_routing(struct connman_service *service) { return service->do_split_routing; } +bool __connman_service_index_is_split_routing(int index) +{ + struct connman_service *service; + + if (index < 0) + return false; + + service = __connman_service_lookup_from_index(index); + if (!service) + return false; + + return __connman_service_is_split_routing(service); +} + int __connman_service_get_index(struct connman_service *service) { - if (service == NULL) + if (!service) return -1; - if (service->network != NULL) + if (service->network) return connman_network_get_index(service->network); - else if (service->provider != NULL) + else if (service->provider) return connman_provider_get_index(service->provider); return -1; @@ -2326,16 +2464,34 @@ int __connman_service_get_index(struct connman_service *service) void __connman_service_set_hidden(struct connman_service *service) { - if (service == NULL || service->hidden == TRUE) + if (!service || service->hidden) return; - service->hidden_service = TRUE; + service->hidden_service = true; +} + +void __connman_service_set_hostname(struct connman_service *service, + const char *hostname) +{ + if (!service || service->hidden) + return; + + g_free(service->hostname); + service->hostname = g_strdup(hostname); +} + +const char *__connman_service_get_hostname(struct connman_service *service) +{ + if (!service) + return NULL; + + return service->hostname; } void __connman_service_set_domainname(struct connman_service *service, const char *domainname) { - if (service == NULL || service->hidden == TRUE) + if (!service || service->hidden) return; g_free(service->domainname); @@ -2346,10 +2502,10 @@ void __connman_service_set_domainname(struct connman_service *service, const char *connman_service_get_domainname(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; - if (service->domains != NULL) + if (service->domains) return service->domains[0]; else return service->domainname; @@ -2357,23 +2513,23 @@ const char *connman_service_get_domainname(struct connman_service *service) char **connman_service_get_nameservers(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; - if (service->nameservers_config != NULL) + if (service->nameservers_config) return g_strdupv(service->nameservers_config); - else if (service->nameservers != NULL || - service->nameservers_auto != NULL) { + else if (service->nameservers || + service->nameservers_auto) { int len = 0, len_auto = 0, i; char **nameservers; - if (service->nameservers != NULL) + if (service->nameservers) len = g_strv_length(service->nameservers); - if (service->nameservers_auto != NULL) + if (service->nameservers_auto) len_auto = g_strv_length(service->nameservers_auto); nameservers = g_try_new0(char *, len + len_auto + 1); - if (nameservers == NULL) + if (!nameservers) return NULL; for (i = 0; i < len; i++) @@ -2386,12 +2542,12 @@ char **connman_service_get_nameservers(struct connman_service *service) return nameservers; } - return NULL; + return g_strdupv(connman_setting_get_string_list("FallbackNameservers")); } char **connman_service_get_timeservers_config(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; return service->timeservers_config; @@ -2399,19 +2555,16 @@ char **connman_service_get_timeservers_config(struct connman_service *service) char **connman_service_get_timeservers(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; - if (service->timeservers != NULL) - return service->timeservers; - - return NULL; + return service->timeservers; } void connman_service_set_proxy_method(struct connman_service *service, enum connman_service_proxy_method method) { - if (service == NULL || service->hidden == TRUE) + if (!service || service->hidden) return; service->proxy = method; @@ -2425,12 +2578,12 @@ void connman_service_set_proxy_method(struct connman_service *service, enum connman_service_proxy_method connman_service_get_proxy_method( struct connman_service *service) { - if (service == NULL) + if (!service) return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN; if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) { if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO && - service->pac == NULL) + !service->pac) return service->proxy; return service->proxy_config; @@ -2451,7 +2604,7 @@ char **connman_service_get_proxy_excludes(struct connman_service *service) const char *connman_service_get_proxy_url(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; return service->pac; @@ -2460,7 +2613,7 @@ const char *connman_service_get_proxy_url(struct connman_service *service) void __connman_service_set_proxy_autoconfig(struct connman_service *service, const char *url) { - if (service == NULL || service->hidden == TRUE) + if (!service || service->hidden) return; service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO; @@ -2483,7 +2636,7 @@ void __connman_service_set_proxy_autoconfig(struct connman_service *service, const char *connman_service_get_proxy_autoconfig(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; if (service->ipconfig_ipv4) @@ -2500,13 +2653,13 @@ void __connman_service_set_timeservers(struct connman_service *service, { int i; - if (service == NULL) + if (!service) return; g_strfreev(service->timeservers); service->timeservers = NULL; - for (i = 0; timeservers != NULL && timeservers[i] != NULL; i++) + for (i = 0; timeservers && timeservers[i]; i++) __connman_service_timeserver_append(service, timeservers[i]); } @@ -2517,13 +2670,13 @@ int __connman_service_timeserver_append(struct connman_service *service, DBG("service %p timeserver %s", service, timeserver); - if (timeserver == NULL) + if (!timeserver) return -EINVAL; - if (service->timeservers != NULL) { + if (service->timeservers) { int i; - for (i = 0; service->timeservers[i] != NULL; i++) + for (i = 0; service->timeservers[i]; i++) if (g_strcmp0(service->timeservers[i], timeserver) == 0) return -EEXIST; @@ -2535,7 +2688,7 @@ int __connman_service_timeserver_append(struct connman_service *service, service->timeservers = g_try_new0(char *, len + 2); } - if (service->timeservers == NULL) + if (!service->timeservers) return -ENOMEM; service->timeservers[len] = g_strdup(timeserver); @@ -2552,14 +2705,14 @@ int __connman_service_timeserver_remove(struct connman_service *service, DBG("service %p timeserver %s", service, timeserver); - if (timeserver == NULL) + if (!timeserver) return -EINVAL; - if (service->timeservers == NULL) + if (!service->timeservers) return 0; - for (i = 0; service->timeservers != NULL && - service->timeservers[i] != NULL; i++) + for (i = 0; service->timeservers && + service->timeservers[i]; i++) if (g_strcmp0(service->timeservers[i], timeserver) == 0) { found = 1; break; @@ -2578,13 +2731,13 @@ int __connman_service_timeserver_remove(struct connman_service *service, } servers = g_try_new0(char *, len); - if (servers == NULL) + if (!servers) return -ENOMEM; for (i = 0, j = 0; i < len; i++) { if (g_strcmp0(service->timeservers[i], timeserver) != 0) { servers[j] = g_strdup(service->timeservers[i]); - if (servers[j] == NULL) + if (!servers[j]) return -ENOMEM; j++; } @@ -2600,10 +2753,10 @@ int __connman_service_timeserver_remove(struct connman_service *service, void __connman_service_timeserver_changed(struct connman_service *service, GSList *ts_list) { - if (service == NULL) + if (!service) return; - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_array(service->path, @@ -2614,7 +2767,7 @@ void __connman_service_timeserver_changed(struct connman_service *service, void __connman_service_set_pac(struct connman_service *service, const char *pac) { - if (service->hidden == TRUE) + if (service->hidden) return; g_free(service->pac); service->pac = g_strdup(pac); @@ -2625,13 +2778,13 @@ void __connman_service_set_pac(struct connman_service *service, void __connman_service_set_identity(struct connman_service *service, const char *identity) { - if (service->immutable || service->hidden == TRUE) + if (service->immutable || service->hidden) return; g_free(service->identity); service->identity = g_strdup(identity); - if (service->network != NULL) + if (service->network) connman_network_set_string(service->network, "WiFi.Identity", service->identity); @@ -2640,12 +2793,12 @@ void __connman_service_set_identity(struct connman_service *service, void __connman_service_set_agent_identity(struct connman_service *service, const char *agent_identity) { - if (service->hidden == TRUE) + if (service->hidden) return; g_free(service->agent_identity); service->agent_identity = g_strdup(agent_identity); - if (service->network != NULL) + if (service->network) connman_network_set_string(service->network, "WiFi.AgentIdentity", service->agent_identity); @@ -2658,12 +2811,12 @@ static int check_passphrase(struct connman_service *service, guint i; gsize length; - if (passphrase == NULL) { + if (!passphrase) { /* * This will prevent __connman_service_set_passphrase() to * wipe the passphrase out in case of -ENOKEY error for a * favorite service. */ - if (service->favorite == TRUE) + if (service->favorite) return 1; else return 0; @@ -2713,7 +2866,7 @@ int __connman_service_set_passphrase(struct connman_service *service, { int err = 0; - if (service->immutable == TRUE || service->hidden == TRUE) + if (service->immutable || service->hidden) return -EINVAL; err = check_passphrase(service, service->security, passphrase); @@ -2722,7 +2875,7 @@ int __connman_service_set_passphrase(struct connman_service *service, g_free(service->passphrase); service->passphrase = g_strdup(passphrase); - if (service->network != NULL) + if (service->network) connman_network_set_string(service->network, "WiFi.Passphrase", service->passphrase); @@ -2734,26 +2887,12 @@ int __connman_service_set_passphrase(struct connman_service *service, const char *__connman_service_get_passphrase(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; return service->passphrase; } -void __connman_service_set_agent_passphrase(struct connman_service *service, - const char *agent_passphrase) -{ - if (service->hidden == TRUE) - return; - g_free(service->agent_passphrase); - service->agent_passphrase = g_strdup(agent_passphrase); - - if (service->network != NULL) - connman_network_set_string(service->network, - "WiFi.AgentPassphrase", - service->agent_passphrase); -} - static DBusMessage *get_properties(DBusConnection *conn, DBusMessage *msg, void *user_data) { @@ -2764,7 +2903,7 @@ static DBusMessage *get_properties(DBusConnection *conn, DBG("service %p", service); reply = dbus_message_new_method_return(msg); - if (reply == NULL) + if (!reply) return NULL; dbus_message_iter_init_append(reply, &array); @@ -2809,7 +2948,7 @@ static int update_proxy_configuration(struct connman_service *service, type = dbus_message_iter_get_arg_type(&variant); - if (g_str_equal(key, "Method") == TRUE) { + if (g_str_equal(key, "Method")) { const char *val; if (type != DBUS_TYPE_STRING) @@ -2817,19 +2956,19 @@ static int update_proxy_configuration(struct connman_service *service, dbus_message_iter_get_basic(&variant, &val); method = string2proxymethod(val); - } else if (g_str_equal(key, "URL") == TRUE) { + } else if (g_str_equal(key, "URL")) { if (type != DBUS_TYPE_STRING) goto error; dbus_message_iter_get_basic(&variant, &url); - } else if (g_str_equal(key, "Servers") == TRUE) { + } else if (g_str_equal(key, "Servers")) { DBusMessageIter str_array; if (type != DBUS_TYPE_ARRAY) goto error; servers_str = g_string_new(NULL); - if (servers_str == NULL) + if (!servers_str) goto error; dbus_message_iter_recurse(&variant, &str_array); @@ -2848,14 +2987,14 @@ static int update_proxy_configuration(struct connman_service *service, dbus_message_iter_next(&str_array); } - } else if (g_str_equal(key, "Excludes") == TRUE) { + } else if (g_str_equal(key, "Excludes")) { DBusMessageIter str_array; if (type != DBUS_TYPE_ARRAY) goto error; excludes_str = g_string_new(NULL); - if (excludes_str == NULL) + if (!excludes_str) goto error; dbus_message_iter_recurse(&variant, &str_array); @@ -2883,10 +3022,10 @@ static int update_proxy_configuration(struct connman_service *service, case CONNMAN_SERVICE_PROXY_METHOD_DIRECT: break; case CONNMAN_SERVICE_PROXY_METHOD_MANUAL: - if (servers_str == NULL && service->proxies == NULL) + if (!servers_str && !service->proxies) goto error; - if (servers_str != NULL) { + if (servers_str) { g_strfreev(service->proxies); if (servers_str->len > 0) @@ -2896,7 +3035,7 @@ static int update_proxy_configuration(struct connman_service *service, service->proxies = NULL; } - if (excludes_str != NULL) { + if (excludes_str) { g_strfreev(service->excludes); if (excludes_str->len > 0) @@ -2906,14 +3045,14 @@ static int update_proxy_configuration(struct connman_service *service, service->excludes = NULL; } - if (service->proxies == NULL) + if (!service->proxies) method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT; break; case CONNMAN_SERVICE_PROXY_METHOD_AUTO: g_free(service->pac); - if (url != NULL && strlen(url) > 0) + if (url && strlen(url) > 0) service->pac = g_strdup(url); else service->pac = NULL; @@ -2929,10 +3068,10 @@ static int update_proxy_configuration(struct connman_service *service, goto error; } - if (servers_str != NULL) + if (servers_str) g_string_free(servers_str, TRUE); - if (excludes_str != NULL) + if (excludes_str) g_string_free(excludes_str, TRUE); service->proxy_config = method; @@ -2940,10 +3079,10 @@ static int update_proxy_configuration(struct connman_service *service, return 0; error: - if (servers_str != NULL) + if (servers_str) g_string_free(servers_str, TRUE); - if (excludes_str != NULL) + if (excludes_str) g_string_free(excludes_str, TRUE); return -EINVAL; @@ -2969,7 +3108,7 @@ int __connman_service_reset_ipconfig(struct connman_service *service, } else return -EINVAL; - if (ipconfig == NULL) + if (!ipconfig) return -ENXIO; old_method = __connman_ipconfig_get_method(ipconfig); @@ -2981,7 +3120,7 @@ int __connman_service_reset_ipconfig(struct connman_service *service, else new_ipconfig = create_ip6config(service, index); - if (array != NULL) { + if (array) { err = __connman_ipconfig_set_config(new_ipconfig, array); if (err < 0) { __connman_ipconfig_unref(new_ipconfig); @@ -2996,25 +3135,24 @@ int __connman_service_reset_ipconfig(struct connman_service *service, __connman_network_clear_ipconfig(service->network, ipconfig); __connman_ipconfig_unref(ipconfig); - if (type == CONNMAN_IPCONFIG_TYPE_IPV4) { + if (type == CONNMAN_IPCONFIG_TYPE_IPV4) service->ipconfig_ipv4 = new_ipconfig; - } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) { + else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) service->ipconfig_ipv6 = new_ipconfig; - } __connman_ipconfig_enable(new_ipconfig); - if (new_state != NULL && new_method != old_method) { + if (new_state && new_method != old_method) { if (type == CONNMAN_IPCONFIG_TYPE_IPV4) *new_state = service->state_ipv4; else *new_state = service->state_ipv6; - __connman_service_auto_connect(); + __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO); } DBG("err %d ipconfig %p type %d method %d state %s", err, new_ipconfig, type, new_method, - new_state == NULL ? "-" : state2string(*new_state)); + !new_state ? "-" : state2string(*new_state)); return err; } @@ -3029,7 +3167,7 @@ static DBusMessage *set_property(DBusConnection *conn, DBG("service %p", service); - if (dbus_message_iter_init(msg, &iter) == FALSE) + if (!dbus_message_iter_init(msg, &iter)) return __connman_error_invalid_arguments(msg); if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) @@ -3045,13 +3183,13 @@ static DBusMessage *set_property(DBusConnection *conn, type = dbus_message_iter_get_arg_type(&value); - if (g_str_equal(name, "AutoConnect") == TRUE) { - connman_bool_t autoconnect; + if (g_str_equal(name, "AutoConnect")) { + dbus_bool_t autoconnect; if (type != DBUS_TYPE_BOOLEAN) return __connman_error_invalid_arguments(msg); - if (service->favorite == FALSE) + if (!service->favorite) return __connman_error_invalid_service(msg); dbus_message_iter_get_basic(&value, &autoconnect); @@ -3063,30 +3201,28 @@ static DBusMessage *set_property(DBusConnection *conn, autoconnect_changed(service); - if (autoconnect == TRUE) - __connman_service_auto_connect(); + if (autoconnect) + __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO); service_save(service); - } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) { + } else if (g_str_equal(name, "Nameservers.Configuration")) { DBusMessageIter entry; GString *str; int index; const char *gw; - if (service->immutable == TRUE) + if (__connman_provider_is_immutable(service->provider) || + service->immutable) return __connman_error_not_supported(msg); if (type != DBUS_TYPE_ARRAY) return __connman_error_invalid_arguments(msg); str = g_string_new(NULL); - if (str == NULL) + if (!str) return __connman_error_invalid_arguments(msg); - if (service->type == CONNMAN_SERVICE_TYPE_VPN) - index = connman_provider_get_index(service->provider); - else - index = connman_network_get_index(service->network); + index = __connman_service_get_index(service); gw = __connman_ipconfig_get_gateway_from_index(index, CONNMAN_IPCONFIG_TYPE_ALL); @@ -3137,12 +3273,12 @@ static DBusMessage *set_property(DBusConnection *conn, CONNMAN_IPCONFIG_TYPE_IPV6); service_save(service); - } else if (g_str_equal(name, "Timeservers.Configuration") == TRUE) { + } else if (g_str_equal(name, "Timeservers.Configuration")) { DBusMessageIter entry; GSList *list = NULL; int count = 0; - if (service->immutable == TRUE) + if (service->immutable) return __connman_error_not_supported(msg); if (type != DBUS_TYPE_ARRAY) @@ -3168,10 +3304,10 @@ static DBusMessage *set_property(DBusConnection *conn, g_strfreev(service->timeservers_config); service->timeservers_config = NULL; - if (list != NULL) { + if (list) { service->timeservers_config = g_new0(char *, count+1); - while (list != NULL) { + while (list) { count--; service->timeservers_config[count] = list->data; list = g_slist_delete_link(list, list); @@ -3184,18 +3320,18 @@ static DBusMessage *set_property(DBusConnection *conn, if (service == __connman_service_get_default()) __connman_timeserver_sync(service); - } else if (g_str_equal(name, "Domains.Configuration") == TRUE) { + } else if (g_str_equal(name, "Domains.Configuration")) { DBusMessageIter entry; GString *str; - if (service->immutable == TRUE) + if (service->immutable) return __connman_error_not_supported(msg); if (type != DBUS_TYPE_ARRAY) return __connman_error_invalid_arguments(msg); str = g_string_new(NULL); - if (str == NULL) + if (!str) return __connman_error_invalid_arguments(msg); dbus_message_iter_recurse(&value, &entry); @@ -3225,10 +3361,10 @@ static DBusMessage *set_property(DBusConnection *conn, domain_changed(service); service_save(service); - } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) { + } else if (g_str_equal(name, "Proxy.Configuration")) { int err; - if (service->immutable == TRUE) + if (service->immutable) return __connman_error_not_supported(msg); if (type != DBUS_TYPE_ARRAY) @@ -3244,7 +3380,7 @@ static DBusMessage *set_property(DBusConnection *conn, __connman_notifier_proxy_changed(service); service_save(service); - } else if (g_str_equal(name, "IPv4.Configuration") == TRUE || + } else if (g_str_equal(name, "IPv4.Configuration") || g_str_equal(name, "IPv6.Configuration")) { enum connman_service_state state = @@ -3253,16 +3389,17 @@ static DBusMessage *set_property(DBusConnection *conn, CONNMAN_IPCONFIG_TYPE_UNKNOWN; int err = 0; - if (service->immutable == TRUE) + if (service->type == CONNMAN_SERVICE_TYPE_VPN || + service->immutable) return __connman_error_not_supported(msg); DBG("%s", name); - if (service->ipconfig_ipv4 == NULL && - service->ipconfig_ipv6 == NULL) + if (!service->ipconfig_ipv4 && + !service->ipconfig_ipv6) return __connman_error_invalid_property(msg); - if (g_str_equal(name, "IPv4.Configuration") == TRUE) + if (g_str_equal(name, "IPv4.Configuration")) type = CONNMAN_IPCONFIG_TYPE_IPV4; else type = CONNMAN_IPCONFIG_TYPE_IPV6; @@ -3306,15 +3443,15 @@ static void set_error(struct connman_service *service, service->error = error; - if (service->path == NULL) + if (!service->path) return; str = error2string(service->error); - if (str == NULL) + if (!str) str = ""; - if (allow_property_changed(service) == FALSE) + if (!allow_property_changed(service)) return; connman_dbus_property_changed_basic(service->path, @@ -3322,14 +3459,6 @@ static void set_error(struct connman_service *service, DBUS_TYPE_STRING, &str); } -static void set_idle(struct connman_service *service) -{ - service->state = service->state_ipv4 = service->state_ipv6 = - CONNMAN_SERVICE_STATE_IDLE; - set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN); - state_changed(service); -} - static DBusMessage *clear_property(DBusConnection *conn, DBusMessage *msg, void *user_data) { @@ -3341,8 +3470,8 @@ static DBusMessage *clear_property(DBusConnection *conn, dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID); - if (g_str_equal(name, "Error") == TRUE) { - set_idle(service); + if (g_str_equal(name, "Error")) { + set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN); g_get_current_time(&service->modified); service_save(service); @@ -3352,34 +3481,102 @@ static DBusMessage *clear_property(DBusConnection *conn, return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); } -static connman_bool_t is_ipconfig_usable(struct connman_service *service) +static bool is_ipconfig_usable(struct connman_service *service) { - if (__connman_ipconfig_is_usable(service->ipconfig_ipv4) == FALSE && - __connman_ipconfig_is_usable(service->ipconfig_ipv6) - == FALSE) - return FALSE; + if (!__connman_ipconfig_is_usable(service->ipconfig_ipv4) && + !__connman_ipconfig_is_usable(service->ipconfig_ipv6)) + return false; - return TRUE; + return true; } -static connman_bool_t is_ignore(struct connman_service *service) +static bool is_ignore(struct connman_service *service) { - if (service->autoconnect == FALSE) - return TRUE; + if (!service->autoconnect) + return true; - if (service->roaming == TRUE) - return TRUE; + if (service->roaming) + return true; - if (service->ignore == TRUE) - return TRUE; + if (service->ignore) + return true; if (service->state == CONNMAN_SERVICE_STATE_FAILURE) - return TRUE; + return true; - if (is_ipconfig_usable(service) == FALSE) - return TRUE; + if (!is_ipconfig_usable(service)) + return true; - return FALSE; + return false; +} + +static void disconnect_on_last_session(enum connman_service_type type) +{ + GList *list; + + for (list = service_list; list; list = list->next) { + struct connman_service *service = list->data; + + if (service->type != type) + continue; + + if (service->connect_reason != CONNMAN_SERVICE_CONNECT_REASON_SESSION) + continue; + + __connman_service_disconnect(service); + return; + } +} + +static int active_sessions[MAX_CONNMAN_SERVICE_TYPES] = {}; +static int active_count = 0; + +void __connman_service_set_active_session(bool enable, GSList *list) +{ + if (!list) + return; + + if (enable) + active_count++; + else + active_count--; + + while (list != NULL) { + enum connman_service_type type = GPOINTER_TO_INT(list->data); + + switch (type) { + case CONNMAN_SERVICE_TYPE_ETHERNET: + case CONNMAN_SERVICE_TYPE_WIFI: + case CONNMAN_SERVICE_TYPE_BLUETOOTH: + case CONNMAN_SERVICE_TYPE_CELLULAR: + case CONNMAN_SERVICE_TYPE_GADGET: + if (enable) + active_sessions[type]++; + else + active_sessions[type]--; + break; + + case CONNMAN_SERVICE_TYPE_UNKNOWN: + case CONNMAN_SERVICE_TYPE_SYSTEM: + case CONNMAN_SERVICE_TYPE_GPS: + case CONNMAN_SERVICE_TYPE_VPN: + case CONNMAN_SERVICE_TYPE_P2P: + break; + } + + if (active_sessions[type] == 0) + disconnect_on_last_session(type); + + list = g_slist_next(list); + } + + DBG("eth %d wifi %d bt %d cellular %d gadget %d sessions %d", + active_sessions[CONNMAN_SERVICE_TYPE_ETHERNET], + active_sessions[CONNMAN_SERVICE_TYPE_WIFI], + active_sessions[CONNMAN_SERVICE_TYPE_BLUETOOTH], + active_sessions[CONNMAN_SERVICE_TYPE_CELLULAR], + active_sessions[CONNMAN_SERVICE_TYPE_GADGET], + active_count); } struct preferred_tech_data { @@ -3401,25 +3598,26 @@ static void preferred_tech_add_by_type(gpointer data, gpointer user_data) } } -static GList* preferred_tech_list_get(void) +static GList *preferred_tech_list_get(void) { unsigned int *tech_array; struct preferred_tech_data tech_data = { 0, }; int i; tech_array = connman_setting_get_uint_list("PreferredTechnologies"); - if (tech_array == NULL) + if (!tech_array) return NULL; - if (connman_setting_get_bool("SingleConnectedTechnology") == TRUE) { + if (connman_setting_get_bool("SingleConnectedTechnology")) { GList *list; - for (list = service_list; list != NULL; list = list->next) { + for (list = service_list; list; list = list->next) { struct connman_service *service = list->data; - if (is_connected(service) == FALSE) + if (!is_connected(service)) break; - if (service->userconnect == TRUE) { + if (service->connect_reason == + CONNMAN_SERVICE_CONNECT_REASON_USER) { DBG("service %p name %s is user connected", service, service->name); return NULL; @@ -3436,87 +3634,162 @@ static GList* preferred_tech_list_get(void) return tech_data.preferred_list; } -static connman_bool_t auto_connect_service(GList *services, - connman_bool_t preferred) +static bool auto_connect_service(GList *services, + enum connman_service_connect_reason reason, + bool preferred) { struct connman_service *service = NULL; + bool ignore[MAX_CONNMAN_SERVICE_TYPES] = { }; + bool autoconnecting = false; GList *list; - for (list = services; list != NULL; list = list->next) { + DBG("preferred %d sessions %d reason %s", preferred, active_count, + reason2string(reason)); + + ignore[CONNMAN_SERVICE_TYPE_VPN] = true; + + for (list = services; list; list = list->next) { service = list->data; - if (service->pending != NULL) - return TRUE; + if (ignore[service->type]) { + DBG("service %p type %s ignore", service, + __connman_service_type2string(service->type)); + continue; + } + + if (service->pending || + is_connecting(service) || + is_connected(service)) { + if (!active_count) + return true; - if (is_connecting(service) == TRUE) - return TRUE; + ignore[service->type] = true; + autoconnecting = true; - if (service->favorite == FALSE) { - if (preferred == TRUE) - goto next_service; - return FALSE; + DBG("service %p type %s busy", service, + __connman_service_type2string(service->type)); + + continue; } - if (is_connected(service) == TRUE) - return TRUE; + if (!service->favorite) { + if (preferred) + continue; - if (is_ignore(service) == FALSE && service->state == - CONNMAN_SERVICE_STATE_IDLE) - break; + return autoconnecting; + } - next_service: - service = NULL; - } + if (is_ignore(service) || service->state != + CONNMAN_SERVICE_STATE_IDLE) + continue; - if (service != NULL) { + if (autoconnecting && !active_sessions[service->type]) { + DBG("service %p type %s has no users", service, + __connman_service_type2string(service->type)); + continue; + } DBG("service %p %s %s", service, service->name, - (preferred == TRUE)? "preferred": "auto"); + (preferred) ? "preferred" : reason2string(reason)); + + __connman_service_connect(service, reason); - service->userconnect = FALSE; - __connman_service_connect(service); - return TRUE; + if (!active_count) + return true; + + ignore[service->type] = true; } - return FALSE; + + return autoconnecting; } static gboolean run_auto_connect(gpointer data) { - GList *list = NULL, *preferred_tech; + enum connman_service_connect_reason reason = GPOINTER_TO_UINT(data); + bool autoconnecting = false; + GList *preferred_tech; autoconnect_timeout = 0; DBG(""); preferred_tech = preferred_tech_list_get(); - if (preferred_tech != NULL) - list = preferred_tech; - - if (list == NULL || auto_connect_service(list, TRUE) == FALSE) - list = service_list; - - if (list != NULL) - auto_connect_service(list, FALSE); - - if (preferred_tech != NULL) + if (preferred_tech) { + autoconnecting = auto_connect_service(preferred_tech, reason, + true); g_list_free(preferred_tech); + } + + if (!autoconnecting || active_count) + auto_connect_service(service_list, reason, false); return FALSE; } -void __connman_service_auto_connect(void) +void __connman_service_auto_connect(enum connman_service_connect_reason reason) { DBG(""); - if (__connman_session_mode() == TRUE) { - DBG("Session mode enabled: auto connect disabled"); + if (autoconnect_timeout != 0) return; + + if (!__connman_session_policy_autoconnect(reason)) + return; + + autoconnect_timeout = g_timeout_add_seconds(0, run_auto_connect, + GUINT_TO_POINTER(reason)); +} + +static gboolean run_vpn_auto_connect(gpointer data) { + GList *list; + bool need_split = false; + + vpn_autoconnect_timeout = 0; + + for (list = service_list; list; list = list->next) { + struct connman_service *service = list->data; + int res; + + if (service->type != CONNMAN_SERVICE_TYPE_VPN) + continue; + + if (is_connected(service) || is_connecting(service)) { + if (!service->do_split_routing) + need_split = true; + continue; + } + + if (is_ignore(service) || !service->favorite) + continue; + + if (need_split && !service->do_split_routing) { + DBG("service %p no split routing", service); + continue; + } + + DBG("service %p %s %s", service, service->name, + service->do_split_routing ? + "split routing" : ""); + + res = __connman_service_connect(service, + CONNMAN_SERVICE_CONNECT_REASON_AUTO); + if (res < 0 && res != -EINPROGRESS) + continue; + + if (!service->do_split_routing) + need_split = true; } - if (autoconnect_timeout != 0) + return FALSE; +} + +static void vpn_auto_connect(void) +{ + if (vpn_autoconnect_timeout) return; - autoconnect_timeout = g_timeout_add_seconds(0, run_auto_connect, NULL); + vpn_autoconnect_timeout = + g_timeout_add_seconds(0, run_vpn_auto_connect, NULL); } static void remove_timeout(struct connman_service *service) @@ -3530,18 +3803,18 @@ static void remove_timeout(struct connman_service *service) void __connman_service_reply_dbus_pending(DBusMessage *pending, int error, const char *path) { - if (pending != NULL) { + if (pending) { if (error > 0) { DBusMessage *reply; reply = __connman_error_failed(pending, error); - if (reply != NULL) + if (reply) g_dbus_send_message(connection, reply); } else { const char *sender; sender = dbus_message_get_interface(pending); - if (path == NULL) + if (!path) path = dbus_message_get_path(pending); DBG("sender %s path %s", sender, path); @@ -3563,35 +3836,35 @@ static void reply_pending(struct connman_service *service, int error) { remove_timeout(service); - if (service->pending != NULL) { + if (service->pending) { __connman_service_reply_dbus_pending(service->pending, error, NULL); service->pending = NULL; } - if (service->provider_pending != NULL) { + if (service->provider_pending) { __connman_service_reply_dbus_pending(service->provider_pending, error, service->path); service->provider_pending = NULL; } } -connman_bool_t +bool __connman_service_is_provider_pending(struct connman_service *service) { - if (service == NULL) - return FALSE; + if (!service) + return false; - if (service->provider_pending != NULL) - return TRUE; + if (service->provider_pending) + return true; - return FALSE; + return false; } void __connman_service_set_provider_pending(struct connman_service *service, DBusMessage *msg) { - if (service->provider_pending != NULL) { + if (service->provider_pending) { DBG("service %p provider pending msg %p already exists", service, service->provider_pending); return; @@ -3603,7 +3876,7 @@ void __connman_service_set_provider_pending(struct connman_service *service, static void check_pending_msg(struct connman_service *service) { - if (service->pending == NULL) + if (!service->pending) return; DBG("service %p pending msg %p already exists", service, @@ -3618,7 +3891,7 @@ void __connman_service_set_hidden_data(struct connman_service *service, DBG("service %p pending %p", service, pending); - if (pending == NULL) + if (!pending) return; check_pending_msg(service); @@ -3639,15 +3912,15 @@ void __connman_service_return_error(struct connman_service *service, static gboolean connect_timeout(gpointer user_data) { struct connman_service *service = user_data; - connman_bool_t autoconnect = FALSE; + bool autoconnect = false; DBG("service %p", service); service->timeout = 0; - if (service->network != NULL) + if (service->network) __connman_network_disconnect(service->network); - else if (service->provider != NULL) + else if (service->provider) connman_provider_disconnect(service->provider); __connman_ipconfig_disable(service->ipconfig_ipv4); @@ -3655,17 +3928,17 @@ static gboolean connect_timeout(gpointer user_data) __connman_stats_service_unregister(service); - if (service->pending != NULL) { + if (service->pending) { DBusMessage *reply; reply = __connman_error_operation_timeout(service->pending); - if (reply != NULL) + if (reply) g_dbus_send_message(connection, reply); dbus_message_unref(service->pending); service->pending = NULL; } else - autoconnect = TRUE; + autoconnect = true; __connman_service_ipconfig_indicate_state(service, CONNMAN_SERVICE_STATE_FAILURE, @@ -3674,77 +3947,50 @@ static gboolean connect_timeout(gpointer user_data) CONNMAN_SERVICE_STATE_FAILURE, CONNMAN_IPCONFIG_TYPE_IPV6); - if (autoconnect == TRUE && service->userconnect == FALSE) - __connman_service_auto_connect(); + if (autoconnect && + service->connect_reason != + CONNMAN_SERVICE_CONNECT_REASON_USER) + __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO); return FALSE; } -static void set_reconnect_state(struct connman_service *service, - connman_bool_t reconnect) -{ - struct connman_device *device; - - if (service->network == NULL) - return; - - device = connman_network_get_device(service->network); - if (device == NULL) - return; - - __connman_device_set_reconnect(device, reconnect); -} - -static connman_bool_t get_reconnect_state(struct connman_service *service) -{ - struct connman_device *device; - - if (service->network == NULL) - return FALSE; - - device = connman_network_get_device(service->network); - if (device == NULL) - return FALSE; - - return __connman_device_get_reconnect(device); -} - -static connman_bool_t is_interface_available(struct connman_service *service, +static bool is_interface_available(struct connman_service *service, struct connman_service *other_service) { unsigned int index = 0, other_index = 0; - if (service->ipconfig_ipv4 != NULL) + if (service->ipconfig_ipv4) index = __connman_ipconfig_get_index(service->ipconfig_ipv4); - else if (service->ipconfig_ipv6 != NULL) + else if (service->ipconfig_ipv6) index = __connman_ipconfig_get_index(service->ipconfig_ipv6); - if (other_service->ipconfig_ipv4 != NULL) + if (other_service->ipconfig_ipv4) other_index = __connman_ipconfig_get_index( other_service->ipconfig_ipv4); - else if (other_service->ipconfig_ipv6 != NULL) + else if (other_service->ipconfig_ipv6) other_index = __connman_ipconfig_get_index( other_service->ipconfig_ipv6); if (index > 0 && other_index != index) - return TRUE; + return true; - return FALSE; + return false; } static DBusMessage *connect_service(DBusConnection *conn, DBusMessage *msg, void *user_data) { struct connman_service *service = user_data; + int err = 0; GList *list; - int err; DBG("service %p", service); - if (service->pending != NULL) + if (service->pending) return __connman_error_in_progress(msg); - for (list = service_list; list != NULL; list = list->next) { + for (list = service_list; list; list = list->next) { struct connman_service *temp = list->data; /* @@ -3752,47 +3998,35 @@ static DBusMessage *connect_service(DBusConnection *conn, * interfaces for a given technology type (like having * more than one wifi card). */ - if (service->type == temp->type && - is_connecting(temp) == TRUE && - is_interface_available(service, - temp) == FALSE) { - if (temp->pending != NULL) - __connman_service_return_error(temp, - ECONNABORTED, - NULL); + if (!is_connecting(temp) && !is_connected(temp)) + break; - err = __connman_service_disconnect(temp); - if (err < 0 && err != -EINPROGRESS) - return __connman_error_in_progress(msg); - else { - set_idle(temp); - break; - } + if (service->type != temp->type) + continue; + + if(!is_interface_available(service, temp)) { + if (__connman_service_disconnect(temp) == -EINPROGRESS) + err = -EINPROGRESS; } } + if (err == -EINPROGRESS) + return __connman_error_in_progress(msg); - service->ignore = FALSE; - - service->userconnect = TRUE; + service->ignore = false; service->pending = dbus_message_ref(msg); - set_reconnect_state(service, FALSE); - - err = __connman_service_connect(service); - if (err < 0) { - if (service->pending == NULL) - return NULL; + err = __connman_service_connect(service, + CONNMAN_SERVICE_CONNECT_REASON_USER); - if (err != -EINPROGRESS) { - dbus_message_unref(service->pending); - service->pending = NULL; + if (err == -EINPROGRESS) + return NULL; - return __connman_error_failed(msg, -err); - } + dbus_message_unref(service->pending); + service->pending = NULL; - return NULL; - } + if (err < 0) + return __connman_error_failed(msg, -err); return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); } @@ -3805,11 +4039,7 @@ static DBusMessage *disconnect_service(DBusConnection *conn, DBG("service %p", service); - reply_pending(service, ECONNABORTED); - - service->ignore = TRUE; - - set_reconnect_state(service, FALSE); + service->ignore = true; err = __connman_service_disconnect(service); if (err < 0) { @@ -3820,28 +4050,25 @@ static DBusMessage *disconnect_service(DBusConnection *conn, return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); } -gboolean __connman_service_remove(struct connman_service *service) +bool __connman_service_remove(struct connman_service *service) { - if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET) - return FALSE; + if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET || + service->type == CONNMAN_SERVICE_TYPE_GADGET) + return false; - if (service->immutable == TRUE || service->hidden == TRUE) - return FALSE; + if (service->immutable || service->hidden || + __connman_provider_is_immutable(service->provider)) + return false; - if (service->favorite == FALSE && service->state != + if (!service->favorite && service->state != CONNMAN_SERVICE_STATE_FAILURE) - return FALSE; - - set_reconnect_state(service, FALSE); + return false; __connman_service_disconnect(service); g_free(service->passphrase); service->passphrase = NULL; - g_free(service->agent_passphrase); - service->agent_passphrase = NULL; - g_free(service->identity); service->identity = NULL; @@ -3851,13 +4078,13 @@ gboolean __connman_service_remove(struct connman_service *service) g_free(service->eap); service->eap = NULL; - set_idle(service); + service->error = CONNMAN_SERVICE_ERROR_UNKNOWN; - __connman_service_set_favorite(service, FALSE); + __connman_service_set_favorite(service, false); service_save(service); - return TRUE; + return true; } static DBusMessage *remove_service(DBusConnection *conn, @@ -3867,13 +4094,13 @@ static DBusMessage *remove_service(DBusConnection *conn, DBG("service %p", service); - if (__connman_service_remove(service) == FALSE) + if (!__connman_service_remove(service)) return __connman_error_not_supported(msg); return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); } -static gboolean check_suitable_state(enum connman_service_state a, +static bool check_suitable_state(enum connman_service_state a, enum connman_service_state b) { /* @@ -3884,14 +4111,14 @@ static gboolean check_suitable_state(enum connman_service_state a, b == CONNMAN_SERVICE_STATE_READY) || (b == CONNMAN_SERVICE_STATE_ONLINE && a == CONNMAN_SERVICE_STATE_READY)) - return TRUE; + return true; return a == b; } static void downgrade_state(struct connman_service *service) { - if (service == NULL) + if (!service) return; DBG("service %p state4 %d state6 %d", service, service->state_ipv4, @@ -3913,13 +4140,14 @@ static void apply_relevant_default_downgrade(struct connman_service *service) struct connman_service *def_service; def_service = __connman_service_get_default(); - if (def_service == NULL) + if (!def_service) return; if (def_service == service && def_service->state == CONNMAN_SERVICE_STATE_ONLINE) { def_service->state = CONNMAN_SERVICE_STATE_READY; __connman_notifier_leave_online(def_service->type); + state_changed(def_service); } } @@ -3946,7 +4174,7 @@ static void switch_default_service(struct connman_service *default_service, static DBusMessage *move_service(DBusConnection *conn, DBusMessage *msg, void *user_data, - gboolean before) + bool before) { struct connman_service *service = user_data; struct connman_service *target; @@ -3959,11 +4187,11 @@ static DBusMessage *move_service(DBusConnection *conn, dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID); - if (service->favorite == FALSE) + if (!service->favorite) return __connman_error_not_supported(msg); target = find_service(path); - if (target == NULL || target->favorite == FALSE || target == service) + if (!target || !target->favorite || target == service) return __connman_error_invalid_service(msg); if (target->type == CONNMAN_SERVICE_TYPE_VPN) { @@ -3971,19 +4199,18 @@ static DBusMessage *move_service(DBusConnection *conn, * We only allow VPN route splitting if there are * routes defined for a given VPN. */ - if (__connman_provider_check_routes(target->provider) - == FALSE) { + if (!__connman_provider_check_routes(target->provider)) { connman_info("Cannot move service. " "No routes defined for provider %s", __connman_provider_get_ident(target->provider)); return __connman_error_invalid_service(msg); } - target->do_split_routing = TRUE; + target->do_split_routing = true; } else - target->do_split_routing = FALSE; + target->do_split_routing = false; - service->do_split_routing = FALSE; + service->do_split_routing = false; target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4); target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6); @@ -4004,32 +4231,32 @@ static DBusMessage *move_service(DBusConnection *conn, */ if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) { if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) { - if (check_suitable_state(target->state_ipv6, - service->state_ipv6) == FALSE) + if (!check_suitable_state(target->state_ipv6, + service->state_ipv6)) return __connman_error_invalid_service(msg); } } if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) { if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) { - if (check_suitable_state(target->state_ipv4, - service->state_ipv4) == FALSE) + if (!check_suitable_state(target->state_ipv4, + service->state_ipv4)) return __connman_error_invalid_service(msg); } } if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) { if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) { - if (check_suitable_state(target->state_ipv6, - service->state_ipv6) == FALSE) + if (!check_suitable_state(target->state_ipv6, + service->state_ipv6)) return __connman_error_invalid_service(msg); } } if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) { if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) { - if (check_suitable_state(target->state_ipv4, - service->state_ipv4) == FALSE) + if (!check_suitable_state(target->state_ipv4, + service->state_ipv4)) return __connman_error_invalid_service(msg); } } @@ -4044,7 +4271,7 @@ static DBusMessage *move_service(DBusConnection *conn, * the service which goes up, needs to recompute its state which * is triggered via downgrading it - if relevant - to state ready. */ - if (before == TRUE) + if (before) switch_default_service(target, service); else switch_default_service(service, target); @@ -4057,13 +4284,13 @@ static DBusMessage *move_service(DBusConnection *conn, static DBusMessage *move_before(DBusConnection *conn, DBusMessage *msg, void *user_data) { - return move_service(conn, msg, user_data, TRUE); + return move_service(conn, msg, user_data, true); } static DBusMessage *move_after(DBusConnection *conn, DBusMessage *msg, void *user_data) { - return move_service(conn, msg, user_data, FALSE); + return move_service(conn, msg, user_data, false); } static DBusMessage *reset_counters(DBusConnection *conn, @@ -4087,12 +4314,12 @@ static void service_append_added_foreach(gpointer data, gpointer user_data) struct connman_service *service = data; DBusMessageIter *iter = user_data; - if (service == NULL || service->path == NULL) { + if (!service || !service->path) { DBG("service %p or path is NULL", service); return; } - if (g_hash_table_lookup(services_notify->add, service->path) != NULL) { + if (g_hash_table_lookup(services_notify->add, service->path)) { DBG("new %s", service->path); append_struct(service, iter); @@ -4118,10 +4345,14 @@ static void append_removed(gpointer key, gpointer value, gpointer user_data) dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath); } +static void service_append_removed(DBusMessageIter *iter, void *user_data) +{ + g_hash_table_foreach(services_notify->remove, append_removed, iter); +} + static gboolean service_send_changed(gpointer data) { DBusMessage *signal; - DBusMessageIter iter, array; DBG(""); @@ -4129,19 +4360,13 @@ static gboolean service_send_changed(gpointer data) signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE, "ServicesChanged"); - if (signal == NULL) + if (!signal) return FALSE; __connman_dbus_append_objpath_dict_array(signal, - service_append_ordered, NULL); - - dbus_message_iter_init_append(signal, &iter); - dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, - DBUS_TYPE_OBJECT_PATH_AS_STRING, &array); - - g_hash_table_foreach(services_notify->remove, append_removed, &array); - - dbus_message_iter_close_container(&iter, &array); + service_append_ordered, NULL); + __connman_dbus_append_objpath_array(signal, + service_append_removed, NULL); dbus_connection_send(connection, signal, NULL); dbus_message_unref(signal); @@ -4174,7 +4399,7 @@ static void service_schedule_removed(struct connman_service *service) { DBG("service %p %s", service, service->path); - if (service == NULL || service->path == NULL) { + if (!service || !service->path) { DBG("service %p or path is NULL", service); return; } @@ -4186,15 +4411,15 @@ static void service_schedule_removed(struct connman_service *service) service_schedule_changed(); } -static connman_bool_t allow_property_changed(struct connman_service *service) +static bool allow_property_changed(struct connman_service *service) { if (g_hash_table_lookup_extended(services_notify->add, service->path, - NULL, NULL) == TRUE) { + NULL, NULL)) { DBG("no property updates for service %p", service); - return FALSE; + return false; } - return TRUE; + return true; } static const GDBusMethodTable service_methods[] = { @@ -4245,7 +4470,7 @@ static void service_free(gpointer user_data) service->path = NULL; - if (path != NULL) { + if (path) { __connman_connection_update_gateway(); g_dbus_unregister_interface(connection, path, @@ -4255,23 +4480,23 @@ static void service_free(gpointer user_data) g_hash_table_destroy(service->counter_table); - if (service->network != NULL) { + if (service->network) { __connman_network_disconnect(service->network); connman_network_unref(service->network); service->network = NULL; } - if (service->provider != NULL) + if (service->provider) connman_provider_unref(service->provider); - if (service->ipconfig_ipv4 != NULL) { + if (service->ipconfig_ipv4) { __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL); __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL); __connman_ipconfig_unref(service->ipconfig_ipv4); service->ipconfig_ipv4 = NULL; } - if (service->ipconfig_ipv6 != NULL) { + if (service->ipconfig_ipv6) { __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL); __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL); __connman_ipconfig_unref(service->ipconfig_ipv6); @@ -4287,11 +4512,11 @@ static void service_free(gpointer user_data) g_strfreev(service->proxies); g_strfreev(service->excludes); + g_free(service->hostname); g_free(service->domainname); g_free(service->pac); g_free(service->name); g_free(service->passphrase); - g_free(service->agent_passphrase); g_free(service->identifier); g_free(service->eap); g_free(service->identity); @@ -4304,24 +4529,27 @@ static void service_free(gpointer user_data) g_free(service->config_file); g_free(service->config_entry); - if (service->stats.timer != NULL) + if (service->stats.timer) g_timer_destroy(service->stats.timer); - if (service->stats_roaming.timer != NULL) + if (service->stats_roaming.timer) g_timer_destroy(service->stats_roaming.timer); + if (current_default == service) + current_default = NULL; + g_free(service); } static void stats_init(struct connman_service *service) { /* home */ - service->stats.valid = FALSE; - service->stats.enabled = FALSE; + service->stats.valid = false; + service->stats.enabled = false; service->stats.timer = g_timer_new(); /* roaming */ - service->stats_roaming.valid = FALSE; - service->stats_roaming.enabled = FALSE; + service->stats_roaming.valid = false; + service->stats_roaming.enabled = false; service->stats_roaming.timer = g_timer_new(); } @@ -4330,7 +4558,6 @@ static void service_initialize(struct connman_service *service) DBG("service %p", service); service->refcount = 1; - service->session_usage_count = 0; service->error = CONNMAN_SERVICE_ERROR_UNKNOWN; @@ -4341,13 +4568,13 @@ static void service_initialize(struct connman_service *service) service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN; service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN; - service->favorite = FALSE; - service->immutable = FALSE; - service->hidden = FALSE; + service->favorite = false; + service->immutable = false; + service->hidden = false; - service->ignore = FALSE; + service->ignore = false; - service->userconnect = FALSE; + service->connect_reason = CONNMAN_SERVICE_CONNECT_REASON_NONE; service->order = 0; @@ -4355,7 +4582,7 @@ static void service_initialize(struct connman_service *service) service->provider = NULL; - service->wps = FALSE; + service->wps = false; } /** @@ -4374,7 +4601,7 @@ struct connman_service *connman_service_create(void) struct connman_service *service; service = g_try_new0(struct connman_service, 1); - if (service == NULL) + if (!service) return NULL; DBG("service %p", service); @@ -4386,13 +4613,13 @@ struct connman_service *connman_service_create(void) counter = list->data; counters = g_try_new0(struct connman_stats_counter, 1); - if (counters == NULL) { + if (!counters) { g_hash_table_destroy(service->counter_table); g_free(service); return NULL; } - counters->append_all = TRUE; + counters->append_all = true; g_hash_table_replace(service->counter_table, (gpointer)counter, counters); @@ -4439,7 +4666,6 @@ void connman_service_unref_debug(struct connman_service *service, service_list = g_list_remove(service_list, service); - reply_pending(service, ECONNABORTED); __connman_service_disconnect(service); g_hash_table_remove(service_hash, service->identifier); @@ -4450,15 +4676,24 @@ static gint service_compare(gconstpointer a, gconstpointer b) struct connman_service *service_a = (void *) a; struct connman_service *service_b = (void *) b; enum connman_service_state state_a, state_b; + bool a_connected, b_connected; + gint strength; state_a = service_a->state; state_b = service_b->state; + a_connected = is_connected(service_a); + b_connected = is_connected(service_b); - if (state_a != state_b) { - gboolean a_connected = is_connected(service_a); - gboolean b_connected = is_connected(service_b); + if (a_connected && b_connected) { + if (service_a->order > service_b->order) + return -1; + + if (service_a->order < service_b->order) + return 1; + } - if (a_connected == TRUE && b_connected == TRUE) { + if (state_a != state_b) { + if (a_connected && b_connected) { /* We prefer online over ready state */ if (state_a == CONNMAN_SERVICE_STATE_ONLINE) return -1; @@ -4467,47 +4702,69 @@ static gint service_compare(gconstpointer a, gconstpointer b) return 1; } - if (a_connected == TRUE) + if (a_connected) return -1; - if (b_connected == TRUE) + if (b_connected) return 1; - if (is_connecting(service_a) == TRUE) + if (is_connecting(service_a)) return -1; - if (is_connecting(service_b) == TRUE) + if (is_connecting(service_b)) return 1; } - if (service_a->order > service_b->order) + if (service_a->favorite && !service_b->favorite) return -1; - if (service_a->order < service_b->order) + if (!service_a->favorite && service_b->favorite) return 1; - if (service_a->favorite == TRUE && service_b->favorite == FALSE) - return -1; + if (service_a->type != service_b->type) { - if (service_a->favorite == FALSE && service_b->favorite == TRUE) - return 1; + if (service_a->type == CONNMAN_SERVICE_TYPE_ETHERNET) + return -1; + if (service_b->type == CONNMAN_SERVICE_TYPE_ETHERNET) + return 1; - if (service_a->type != service_b->type) { - switch (service_a->type) { - case CONNMAN_SERVICE_TYPE_UNKNOWN: - case CONNMAN_SERVICE_TYPE_SYSTEM: - case CONNMAN_SERVICE_TYPE_ETHERNET: - case CONNMAN_SERVICE_TYPE_GPS: - case CONNMAN_SERVICE_TYPE_VPN: - case CONNMAN_SERVICE_TYPE_GADGET: - break; - case CONNMAN_SERVICE_TYPE_WIFI: + if (service_a->type == CONNMAN_SERVICE_TYPE_WIFI) + return -1; + if (service_b->type == CONNMAN_SERVICE_TYPE_WIFI) return 1; - case CONNMAN_SERVICE_TYPE_BLUETOOTH: - case CONNMAN_SERVICE_TYPE_CELLULAR: + + if (service_a->type == CONNMAN_SERVICE_TYPE_CELLULAR) return -1; - } + if (service_b->type == CONNMAN_SERVICE_TYPE_CELLULAR) + return 1; + + if (service_a->type == CONNMAN_SERVICE_TYPE_BLUETOOTH) + return -1; + if (service_b->type == CONNMAN_SERVICE_TYPE_BLUETOOTH) + return 1; + + if (service_a->type == CONNMAN_SERVICE_TYPE_VPN) + return -1; + if (service_b->type == CONNMAN_SERVICE_TYPE_VPN) + return 1; + + if (service_a->type == CONNMAN_SERVICE_TYPE_GADGET) + return -1; + if (service_b->type == CONNMAN_SERVICE_TYPE_GADGET) + return 1; } - return (gint) service_b->strength - (gint) service_a->strength; + strength = (gint) service_b->strength - (gint) service_a->strength; + if (strength) + return strength; + + return g_strcmp0(service_a->name, service_b->name); +} + +static void service_list_sort(void) +{ + if (service_list && service_list->next) { + service_list = g_list_sort(service_list, service_compare); + service_schedule_changed(); + } } /** @@ -4518,7 +4775,7 @@ static gint service_compare(gconstpointer a, gconstpointer b) */ enum connman_service_type connman_service_get_type(struct connman_service *service) { - if (service == NULL) + if (!service) return CONNMAN_SERVICE_TYPE_UNKNOWN; return service->type; @@ -4534,26 +4791,10 @@ char *connman_service_get_interface(struct connman_service *service) { int index; - if (service == NULL) + if (!service) return NULL; - if (service->type == CONNMAN_SERVICE_TYPE_VPN) { - if (service->ipconfig_ipv4) - index = __connman_ipconfig_get_index( - service->ipconfig_ipv4); - else if (service->ipconfig_ipv6) - index = __connman_ipconfig_get_index( - service->ipconfig_ipv6); - else - return NULL; - - return connman_inet_ifname(index); - } - - if (service->network == NULL) - return NULL; - - index = connman_network_get_index(service->network); + index = __connman_service_get_index(service); return connman_inet_ifname(index); } @@ -4567,7 +4808,7 @@ char *connman_service_get_interface(struct connman_service *service) struct connman_network * __connman_service_get_network(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; return service->network; @@ -4576,7 +4817,7 @@ __connman_service_get_network(struct connman_service *service) struct connman_ipconfig * __connman_service_get_ip4config(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; return service->ipconfig_ipv4; @@ -4585,7 +4826,7 @@ __connman_service_get_ip4config(struct connman_service *service) struct connman_ipconfig * __connman_service_get_ip6config(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; return service->ipconfig_ipv6; @@ -4603,11 +4844,11 @@ __connman_service_get_ipconfig(struct connman_service *service, int family) } -connman_bool_t __connman_service_is_connected_state(struct connman_service *service, +bool __connman_service_is_connected_state(struct connman_service *service, enum connman_ipconfig_type type) { - if (service == NULL) - return FALSE; + if (!service) + return false; switch (type) { case CONNMAN_IPCONFIG_TYPE_UNKNOWN: @@ -4618,11 +4859,12 @@ connman_bool_t __connman_service_is_connected_state(struct connman_service *serv return is_connected_state(service, service->state_ipv6); } - return FALSE; + return false; } -enum connman_service_security __connman_service_get_security(struct connman_service *service) +enum connman_service_security __connman_service_get_security( + struct connman_service *service) { - if (service == NULL) + if (!service) return CONNMAN_SERVICE_SECURITY_UNKNOWN; return service->security; @@ -4630,24 +4872,24 @@ enum connman_service_security __connman_service_get_security(struct connman_serv const char *__connman_service_get_phase2(struct connman_service *service) { - if (service == NULL) + if (!service) return NULL; return service->phase2; } -connman_bool_t __connman_service_wps_enabled(struct connman_service *service) +bool __connman_service_wps_enabled(struct connman_service *service) { - if (service == NULL) - return FALSE; + if (!service) + return false; return service->wps; } -void __connman_service_mark_dirty() - { - services_dirty = TRUE; - } +void __connman_service_mark_dirty(void) +{ + services_dirty = true; +} /** * __connman_service_set_favorite_delayed: @@ -4658,10 +4900,10 @@ void __connman_service_mark_dirty() * Change the favorite setting of service */ int __connman_service_set_favorite_delayed(struct connman_service *service, - connman_bool_t favorite, - gboolean delay_ordering) + bool favorite, + bool delay_ordering) { - if (service->hidden == TRUE) + if (service->hidden) return -EOPNOTSUPP; if (service->favorite == favorite) @@ -4669,18 +4911,14 @@ int __connman_service_set_favorite_delayed(struct connman_service *service, service->favorite = favorite; - if (delay_ordering == FALSE) - service->order = __connman_service_get_order(service); + if (!delay_ordering) + __connman_service_get_order(service); favorite_changed(service); - if (delay_ordering == FALSE) { + if (!delay_ordering) { - if (service_list->next != NULL) { - service_list = g_list_sort(service_list, - service_compare); - service_schedule_changed(); - } + service_list_sort(); __connman_connection_update_gateway(); } @@ -4696,21 +4934,26 @@ int __connman_service_set_favorite_delayed(struct connman_service *service, * Change the favorite setting of service */ int __connman_service_set_favorite(struct connman_service *service, - connman_bool_t favorite) + bool favorite) { return __connman_service_set_favorite_delayed(service, favorite, - FALSE); + false); } -connman_bool_t connman_service_get_favorite(struct connman_service *service) +bool connman_service_get_favorite(struct connman_service *service) { - return service->favorite; + return service->favorite; +} + +bool connman_service_get_autoconnect(struct connman_service *service) +{ + return service->autoconnect; } int __connman_service_set_immutable(struct connman_service *service, - connman_bool_t immutable) + bool immutable) { - if (service->hidden == TRUE) + if (service->hidden) return -EOPNOTSUPP; if (service->immutable == immutable) @@ -4724,9 +4967,9 @@ int __connman_service_set_immutable(struct connman_service *service, } int __connman_service_set_ignore(struct connman_service *service, - connman_bool_t ignore) + bool ignore) { - if (service == NULL) + if (!service) return -EINVAL; service->ignore = ignore; @@ -4737,42 +4980,35 @@ int __connman_service_set_ignore(struct connman_service *service, void __connman_service_set_string(struct connman_service *service, const char *key, const char *value) { - if (service->hidden == TRUE) + if (service->hidden) return; - if (g_str_equal(key, "EAP") == TRUE) { + if (g_str_equal(key, "EAP")) { g_free(service->eap); service->eap = g_strdup(value); - } else if (g_str_equal(key, "Identity") == TRUE) { + } else if (g_str_equal(key, "Identity")) { g_free(service->identity); service->identity = g_strdup(value); - } else if (g_str_equal(key, "CACertFile") == TRUE) { + } else if (g_str_equal(key, "CACertFile")) { g_free(service->ca_cert_file); service->ca_cert_file = g_strdup(value); - } else if (g_str_equal(key, "ClientCertFile") == TRUE) { + } else if (g_str_equal(key, "ClientCertFile")) { g_free(service->client_cert_file); service->client_cert_file = g_strdup(value); - } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) { + } else if (g_str_equal(key, "PrivateKeyFile")) { g_free(service->private_key_file); service->private_key_file = g_strdup(value); - } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) { + } else if (g_str_equal(key, "PrivateKeyPassphrase")) { g_free(service->private_key_passphrase); service->private_key_passphrase = g_strdup(value); - } else if (g_str_equal(key, "Phase2") == TRUE) { + } else if (g_str_equal(key, "Phase2")) { g_free(service->phase2); service->phase2 = g_strdup(value); - } else if (g_str_equal(key, "Passphrase") == TRUE) { + } else if (g_str_equal(key, "Passphrase")) { g_free(service->passphrase); service->passphrase = g_strdup(value); } } -void __connman_service_set_userconnect(struct connman_service *service, - connman_bool_t userconnect) -{ - if (service != NULL) - service->userconnect = userconnect; -} - void __connman_service_set_search_domains(struct connman_service *service, char **domains) { @@ -4782,7 +5018,7 @@ void __connman_service_set_search_domains(struct connman_service *service, if (index < 0) return; - if (service->domains != NULL) { + if (service->domains) { remove_searchdomains(service, index, service->domains); g_strfreev(service->domains); @@ -4792,24 +5028,37 @@ void __connman_service_set_search_domains(struct connman_service *service, } } +/* + * This variant is used when domain search list is updated via + * dhcp and in that case the service is not yet fully connected so + * we cannot do same things as what the set() variant is doing. + */ +void __connman_service_update_search_domains(struct connman_service *service, + char **domains) +{ + g_strfreev(service->domains); + service->domains = g_strdupv(domains); +} + static void service_complete(struct connman_service *service) { reply_pending(service, EIO); - if (service->userconnect == FALSE) - __connman_service_auto_connect(); + if (service->connect_reason != CONNMAN_SERVICE_CONNECT_REASON_USER) + __connman_service_auto_connect(service->connect_reason); g_get_current_time(&service->modified); service_save(service); } -static void report_error_cb(void *user_context, gboolean retry, +static void report_error_cb(void *user_context, bool retry, void *user_data) { struct connman_service *service = user_context; - if (retry == TRUE) - __connman_service_connect(service); + if (retry) + __connman_service_connect(service, + CONNMAN_SERVICE_CONNECT_REASON_USER); else { /* It is not relevant to stay on Failure state * when failing is due to wrong user input */ @@ -4828,12 +5077,10 @@ int __connman_service_add_passphrase(struct connman_service *service, switch (service->security) { case CONNMAN_SERVICE_SECURITY_WEP: case CONNMAN_SERVICE_SECURITY_PSK: - err = __connman_service_set_passphrase(service, passphrase); - break; case CONNMAN_SERVICE_SECURITY_8021X: - __connman_service_set_agent_passphrase(service, - passphrase); + err = __connman_service_set_passphrase(service, passphrase); break; + case CONNMAN_SERVICE_SECURITY_UNKNOWN: case CONNMAN_SERVICE_SECURITY_NONE: case CONNMAN_SERVICE_SECURITY_WPA: @@ -4852,7 +5099,7 @@ static int check_wpspin(struct connman_service *service, const char *wpspin) int length; guint i; - if (wpspin == NULL) + if (!wpspin) return 0; length = strlen(wpspin); @@ -4879,53 +5126,56 @@ static int check_wpspin(struct connman_service *service, const char *wpspin) return 0; } -static void request_input_cb (struct connman_service *service, - connman_bool_t values_received, +static void request_input_cb(struct connman_service *service, + bool values_received, const char *name, int name_len, const char *identity, const char *passphrase, - gboolean wps, const char *wpspin, + bool wps, const char *wpspin, const char *error, void *user_data) { struct connman_device *device; + const char *security; int err = 0; - DBG ("RequestInput return, %p", service); + DBG("RequestInput return, %p", service); - if (error != NULL) { + if (error) { DBG("error: %s", error); if (g_strcmp0(error, "net.connman.Agent.Error.Canceled") == 0) { err = -EINVAL; - if (service->hidden == TRUE) + if (service->hidden) __connman_service_return_error(service, ECANCELED, user_data); goto done; } else { - if (service->hidden == TRUE) + if (service->hidden) __connman_service_return_error(service, ETIMEDOUT, user_data); } } - if (service->hidden == TRUE && name_len > 0 && name_len <= 32) { + if (service->hidden && name_len > 0 && name_len <= 32) { device = connman_network_get_device(service->network); + security = connman_network_get_string(service->network, + "WiFi.Security"); err = __connman_device_request_hidden_scan(device, name, name_len, identity, passphrase, - user_data); + security, user_data); if (err < 0) __connman_service_return_error(service, -err, user_data); } - if (values_received == FALSE || service->hidden == TRUE) { + if (!values_received || service->hidden) { err = -EINVAL; goto done; } - if (wps == TRUE && service->network != NULL) { + if (wps && service->network) { err = check_wpspin(service, wpspin); if (err < 0) goto done; @@ -4933,10 +5183,10 @@ static void request_input_cb (struct connman_service *service, connman_network_set_bool(service->network, "WiFi.UseWPS", wps); } - if (identity != NULL) + if (identity) __connman_service_set_agent_identity(service, identity); - if (passphrase != NULL) + if (passphrase) err = __connman_service_add_passphrase(service, passphrase); done: @@ -4944,11 +5194,9 @@ static void request_input_cb (struct connman_service *service, /* We forget any previous error. */ set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN); - __connman_service_connect(service); + __connman_service_connect(service, + CONNMAN_SERVICE_CONNECT_REASON_USER); - /* Never cache agent provided credentials */ - __connman_service_set_agent_identity(service, NULL); - __connman_service_set_agent_passphrase(service, NULL); } else if (err == -ENOKEY) { __connman_service_indicate_error(service, CONNMAN_SERVICE_ERROR_INVALID_KEY); @@ -4957,7 +5205,7 @@ static void request_input_cb (struct connman_service *service, * when failing is due to wrong user input */ service->state = CONNMAN_SERVICE_STATE_IDLE; - if (service->hidden == FALSE) { + if (!service->hidden) { /* * If there was a real error when requesting * hidden scan, then that error is returned already @@ -4978,10 +5226,10 @@ static void downgrade_connected_services(void) struct connman_service *up_service; GList *list; - for (list = service_list; list != NULL; list = list->next) { + for (list = service_list; list; list = list->next) { up_service = list->data; - if (is_connected(up_service) == FALSE) + if (!is_connected(up_service)) continue; if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE) @@ -4998,12 +5246,12 @@ static int service_update_preferred_order(struct connman_service *default_servic unsigned int *tech_array; int i; - if (default_service == NULL || default_service == new_service || - default_service->state != new_state ) + if (!default_service || default_service == new_service || + default_service->state != new_state) return 0; tech_array = connman_setting_get_uint_list("PreferredTechnologies"); - if (tech_array != NULL) { + if (tech_array) { for (i = 0; tech_array[i] != 0; i += 1) { if (default_service->type == tech_array[i]) @@ -5029,10 +5277,10 @@ static void single_connected_tech(struct connman_service *allowed) DBG("keeping %p %s", allowed, allowed->path); - for (iter = service_list; iter != NULL; iter = iter->next) { + for (iter = service_list; iter; iter = iter->next) { service = iter->data; - if (is_connected(service) == FALSE) + if (!is_connected(service)) break; if (service == allowed) @@ -5041,7 +5289,7 @@ static void single_connected_tech(struct connman_service *allowed) services = g_slist_prepend(services, service); } - for (list = services; list != NULL; list = list->next) { + for (list = services; list; list = list->next) { service = list->data; DBG("disconnecting %p %s", service, service->path); @@ -5051,13 +5299,21 @@ static void single_connected_tech(struct connman_service *allowed) g_slist_free(services); } +static const char *get_dbus_sender(struct connman_service *service) +{ + if (!service->pending) + return NULL; + + return dbus_message_get_sender(service->pending); +} + static int service_indicate_state(struct connman_service *service) { enum connman_service_state old_state, new_state; struct connman_service *def_service; int result; - if (service == NULL) + if (!service) return -EINVAL; old_state = service->state; @@ -5090,63 +5346,55 @@ static int service_indicate_state(struct connman_service *service) if (new_state == CONNMAN_SERVICE_STATE_IDLE && old_state != CONNMAN_SERVICE_STATE_DISCONNECT) { - reply_pending(service, ECONNABORTED); __connman_service_disconnect(service); } if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) { - if (service->new_service == FALSE && + if (!service->new_service && __connman_stats_service_register(service) == 0) { /* * For new services the statistics are updated after * we have successfully connected. */ - __connman_stats_get(service, FALSE, + __connman_stats_get(service, false, &service->stats.data); - __connman_stats_get(service, TRUE, + __connman_stats_get(service, true, &service->stats_roaming.data); } } - if (new_state == CONNMAN_SERVICE_STATE_IDLE) { - connman_bool_t reconnect; - - reconnect = get_reconnect_state(service); - if (reconnect == TRUE) - __connman_service_auto_connect(); - } - if (new_state == CONNMAN_SERVICE_STATE_READY) { enum connman_ipconfig_method method; - if (service->new_service == TRUE && + if (service->new_service && __connman_stats_service_register(service) == 0) { /* * This is normally done after configuring state * but for new service do this after we have connected * successfully. */ - __connman_stats_get(service, FALSE, + __connman_stats_get(service, false, &service->stats.data); - __connman_stats_get(service, TRUE, + __connman_stats_get(service, true, &service->stats_roaming.data); } - service->new_service = FALSE; + service->new_service = false; - service_update_preferred_order(def_service, service, new_state); + default_changed(); + + def_service = __connman_service_get_default(); - set_reconnect_state(service, TRUE); + service_update_preferred_order(def_service, service, new_state); - __connman_service_set_favorite(service, TRUE); + __connman_service_set_favorite(service, true); reply_pending(service, 0); g_get_current_time(&service->modified); service_save(service); - update_nameservers(service); dns_changed(service); domain_changed(service); proxy_changed(service); @@ -5156,7 +5404,7 @@ static int service_indicate_state(struct connman_service *service) if (service->type == CONNMAN_SERVICE_TYPE_WIFI && connman_network_get_bool(service->network, - "WiFi.UseWPS") == TRUE) { + "WiFi.UseWPS")) { const char *pass; pass = connman_network_get_string(service->network, @@ -5165,26 +5413,25 @@ static int service_indicate_state(struct connman_service *service) __connman_service_set_passphrase(service, pass); connman_network_set_bool(service->network, - "WiFi.UseWPS", FALSE); + "WiFi.UseWPS", false); } - default_changed(); - method = __connman_ipconfig_get_method(service->ipconfig_ipv6); if (method == CONNMAN_IPCONFIG_METHOD_OFF) __connman_ipconfig_disable_ipv6( service->ipconfig_ipv6); - if (connman_setting_get_bool("SingleConnectedTechnology") - == TRUE) + if (connman_setting_get_bool("SingleConnectedTechnology")) single_connected_tech(service); + else if (service->type != CONNMAN_SERVICE_TYPE_VPN) + vpn_auto_connect(); } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) { def_service = __connman_service_get_default(); - if (__connman_notifier_is_connected() == FALSE && - def_service != NULL && - def_service->provider != NULL) + if (!__connman_notifier_is_connected() && + def_service && + def_service->provider) connman_provider_disconnect(def_service->provider); default_changed(); @@ -5193,38 +5440,44 @@ static int service_indicate_state(struct connman_service *service) __connman_wpad_stop(service); - update_nameservers(service); dns_changed(service); domain_changed(service); proxy_changed(service); - __connman_notifier_disconnect(service->type); - /* * Previous services which are connected and which states * are set to online should reset relevantly ipconfig_state * to ready so wispr/portal will be rerun on those */ downgrade_connected_services(); + + __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO); } if (new_state == CONNMAN_SERVICE_STATE_FAILURE) { - if (service->userconnect == TRUE && + + if (service->connect_reason == CONNMAN_SERVICE_CONNECT_REASON_USER && connman_agent_report_error(service, service->path, error2string(service->error), - report_error_cb, NULL) == -EINPROGRESS) + report_error_cb, + get_dbus_sender(service), + NULL) == -EINPROGRESS) return 0; service_complete(service); } else set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN); - if (service_list->next != NULL) { - service_list = g_list_sort(service_list, service_compare); - service_schedule_changed(); - } + service_list_sort(); __connman_connection_update_gateway(); + if ((old_state == CONNMAN_SERVICE_STATE_ONLINE && + new_state != CONNMAN_SERVICE_STATE_READY) || + (old_state == CONNMAN_SERVICE_STATE_READY && + new_state != CONNMAN_SERVICE_STATE_ONLINE)) { + __connman_notifier_disconnect(service->type); + } + if (new_state == CONNMAN_SERVICE_STATE_ONLINE) { __connman_notifier_enter_online(service->type); default_changed(); @@ -5238,14 +5491,21 @@ int __connman_service_indicate_error(struct connman_service *service, { DBG("service %p error %d", service, error); - if (service == NULL) + if (!service) return -EINVAL; set_error(service, error); - if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY) + /* + * Supplicant does not always return invalid key error for + * WPA-EAP so clear the credentials always. + */ + if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY || + service->security == CONNMAN_SERVICE_SECURITY_8021X) __connman_service_set_passphrase(service, NULL); + __connman_service_set_agent_identity(service, NULL); + __connman_service_ipconfig_indicate_state(service, CONNMAN_SERVICE_STATE_FAILURE, CONNMAN_IPCONFIG_TYPE_IPV4); @@ -5259,7 +5519,7 @@ int __connman_service_clear_error(struct connman_service *service) { DBG("service %p", service); - if (service == NULL) + if (!service) return -EINVAL; if (service->state != CONNMAN_SERVICE_STATE_FAILURE) @@ -5269,9 +5529,6 @@ int __connman_service_clear_error(struct connman_service *service) CONNMAN_SERVICE_STATE_UNKNOWN; set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN); - if (service->favorite == TRUE) - set_reconnect_state(service, TRUE); - __connman_service_ipconfig_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE, CONNMAN_IPCONFIG_TYPE_IPV6); @@ -5291,7 +5548,16 @@ int __connman_service_clear_error(struct connman_service *service) int __connman_service_indicate_default(struct connman_service *service) { - DBG("service %p", service); + DBG("service %p state %s", service, state2string(service->state)); + + if (!is_connected(service)) { + /* + * If service is not yet fully connected, then we must not + * change the default yet. The default gw will be changed + * after the service state is in ready. + */ + return -EINPROGRESS; + } default_changed(); @@ -5302,7 +5568,7 @@ enum connman_service_state __connman_service_ipconfig_get_state( struct connman_service *service, enum connman_ipconfig_type type) { - if (service == NULL) + if (!service) return CONNMAN_SERVICE_STATE_UNKNOWN; if (type == CONNMAN_IPCONFIG_TYPE_IPV4) @@ -5327,7 +5593,7 @@ static void check_proxy_setup(struct connman_service *service) if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN && (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO || - service->pac != NULL)) + service->pac)) goto done; if (__connman_wpad_start(service) < 0) { @@ -5352,7 +5618,7 @@ static int connected_networks_count; static int original_rp_filter; static void service_rp_filter(struct connman_service *service, - gboolean connected) + bool connected) { enum connman_ipconfig_method method; @@ -5369,7 +5635,7 @@ static void service_rp_filter(struct connman_service *service, break; } - if (connected == TRUE) { + if (connected) { if (connected_networks_count == 1) { int filter_value; filter_value = __connman_ipconfig_set_rp_filter(); @@ -5398,8 +5664,13 @@ static void service_rp_filter(struct connman_service *service, static gboolean redo_wispr(gpointer user_data) { struct connman_service *service = user_data; + int refcount = service->refcount - 1; - DBG(""); + connman_service_unref(service); + if (refcount == 0) { + DBG("Service %p already removed", service); + return FALSE; + } __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6); @@ -5427,7 +5698,7 @@ int __connman_service_online_check_failed(struct connman_service *service, * necessary IPv6 router advertisement messages that might have * DNS data etc. */ - g_timeout_add_seconds(1, redo_wispr, service); + g_timeout_add_seconds(1, redo_wispr, connman_service_ref(service)); return EAGAIN; } @@ -5438,9 +5709,9 @@ int __connman_service_ipconfig_indicate_state(struct connman_service *service, { struct connman_ipconfig *ipconfig = NULL; enum connman_service_state old_state; - int ret; + enum connman_ipconfig_method method; - if (service == NULL) + if (!service) return -EINVAL; if (type == CONNMAN_IPCONFIG_TYPE_IPV4) { @@ -5451,7 +5722,7 @@ int __connman_service_ipconfig_indicate_state(struct connman_service *service, ipconfig = service->ipconfig_ipv6; } - if (ipconfig == NULL) + if (!ipconfig) return -EINVAL; /* Any change? */ @@ -5475,11 +5746,9 @@ int __connman_service_ipconfig_indicate_state(struct connman_service *service, __connman_ipconfig_enable(ipconfig); break; case CONNMAN_SERVICE_STATE_READY: - update_nameservers(service); - if (type == CONNMAN_IPCONFIG_TYPE_IPV4) { check_proxy_setup(service); - service_rp_filter(service, TRUE); + service_rp_filter(service, true); } else { service->online_check_count = 1; __connman_wispr_start(service, type); @@ -5492,48 +5761,42 @@ int __connman_service_ipconfig_indicate_state(struct connman_service *service, return -EINVAL; if (type == CONNMAN_IPCONFIG_TYPE_IPV4) - service_rp_filter(service, FALSE); + service_rp_filter(service, false); break; case CONNMAN_SERVICE_STATE_FAILURE: break; } - /* We keep that state */ - if (type == CONNMAN_IPCONFIG_TYPE_IPV4) - service->state_ipv4 = new_state; - else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) - service->state_ipv6 = new_state; - - ret = service_indicate_state(service); - - /* - * If the ipconfig method is OFF, then we set the state to IDLE - * so that it will not affect the combined state in the future. + /* Keep that state, but if the ipconfig method is OFF, then we set + the state to IDLE so that it will not affect the combined state + in the future. */ if (type == CONNMAN_IPCONFIG_TYPE_IPV4) { - enum connman_ipconfig_method method; method = __connman_ipconfig_get_method(service->ipconfig_ipv4); + if (method == CONNMAN_IPCONFIG_METHOD_OFF || - method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) { - service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE; - ret = service_indicate_state(service); - } + method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) + new_state = CONNMAN_SERVICE_STATE_IDLE; + + service->state_ipv4 = new_state; } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) { - enum connman_ipconfig_method method; method = __connman_ipconfig_get_method(service->ipconfig_ipv6); + if (method == CONNMAN_IPCONFIG_METHOD_OFF || - method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) { - service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE; - ret = service_indicate_state(service); - } + method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) + new_state = CONNMAN_SERVICE_STATE_IDLE; + + service->state_ipv6 = new_state; } - return ret; + update_nameservers(service); + + return service_indicate_state(service); } -static connman_bool_t prepare_network(struct connman_service *service) +static bool prepare_network(struct connman_service *service) { enum connman_network_type type; unsigned int ssid_len; @@ -5543,56 +5806,57 @@ static connman_bool_t prepare_network(struct connman_service *service) switch (type) { case CONNMAN_NETWORK_TYPE_UNKNOWN: case CONNMAN_NETWORK_TYPE_VENDOR: - return FALSE; + return false; case CONNMAN_NETWORK_TYPE_WIFI: - if (connman_network_get_blob(service->network, "WiFi.SSID", - &ssid_len) == NULL) - return FALSE; + if (!connman_network_get_blob(service->network, "WiFi.SSID", + &ssid_len)) + return false; - if (service->passphrase != NULL) + if (service->passphrase) connman_network_set_string(service->network, "WiFi.Passphrase", service->passphrase); break; case CONNMAN_NETWORK_TYPE_ETHERNET: + case CONNMAN_NETWORK_TYPE_GADGET: case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN: case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN: case CONNMAN_NETWORK_TYPE_CELLULAR: break; } - return TRUE; + return true; } static void prepare_8021x(struct connman_service *service) { - if (service->eap != NULL) + if (service->eap) connman_network_set_string(service->network, "WiFi.EAP", service->eap); - if (service->identity != NULL) + if (service->identity) connman_network_set_string(service->network, "WiFi.Identity", service->identity); - if (service->ca_cert_file != NULL) + if (service->ca_cert_file) connman_network_set_string(service->network, "WiFi.CACertFile", service->ca_cert_file); - if (service->client_cert_file != NULL) + if (service->client_cert_file) connman_network_set_string(service->network, "WiFi.ClientCertFile", service->client_cert_file); - if (service->private_key_file != NULL) + if (service->private_key_file) connman_network_set_string(service->network, "WiFi.PrivateKeyFile", service->private_key_file); - if (service->private_key_passphrase != NULL) + if (service->private_key_passphrase) connman_network_set_string(service->network, "WiFi.PrivateKeyPassphrase", service->private_key_passphrase); - if (service->phase2 != NULL) + if (service->phase2) connman_network_set_string(service->network, "WiFi.Phase2", service->phase2); } @@ -5601,16 +5865,17 @@ static int service_connect(struct connman_service *service) { int err; - if (service->hidden == TRUE) + if (service->hidden) return -EPERM; switch (service->type) { case CONNMAN_SERVICE_TYPE_UNKNOWN: case CONNMAN_SERVICE_TYPE_SYSTEM: case CONNMAN_SERVICE_TYPE_GPS: - case CONNMAN_SERVICE_TYPE_GADGET: + case CONNMAN_SERVICE_TYPE_P2P: return -EINVAL; case CONNMAN_SERVICE_TYPE_ETHERNET: + case CONNMAN_SERVICE_TYPE_GADGET: case CONNMAN_SERVICE_TYPE_BLUETOOTH: case CONNMAN_SERVICE_TYPE_CELLULAR: case CONNMAN_SERVICE_TYPE_VPN: @@ -5624,28 +5889,26 @@ static int service_connect(struct connman_service *service) case CONNMAN_SERVICE_SECURITY_PSK: case CONNMAN_SERVICE_SECURITY_WPA: case CONNMAN_SERVICE_SECURITY_RSN: - if (service->passphrase == NULL) { - if (service->network == NULL) + if (!service->passphrase) { + if (!service->network) return -EOPNOTSUPP; - if (service->wps == FALSE || - connman_network_get_bool( - service->network, - "WiFi.UseWPS") == FALSE) + if (!service->wps || + !connman_network_get_bool(service->network, "WiFi.UseWPS")) return -ENOKEY; } else if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY) return -ENOKEY; break; case CONNMAN_SERVICE_SECURITY_8021X: - if (service->eap == NULL) + if (!service->eap) return -EINVAL; /* * never request credentials if using EAP-TLS * (EAP-TLS networks need to be fully provisioned) */ - if (g_str_equal(service->eap, "tls") == TRUE) + if (g_str_equal(service->eap, "tls")) break; /* @@ -5653,10 +5916,9 @@ static int service_connect(struct connman_service *service) * missing. Agent provided credentials can be used as * fallback if needed. */ - if ((service->identity == NULL && - service->agent_identity == NULL) || - (service->passphrase == NULL && - service->agent_passphrase == NULL)) + if ((!service->identity && + !service->agent_identity) || + !service->passphrase) return -ENOKEY; break; @@ -5664,8 +5926,8 @@ static int service_connect(struct connman_service *service) break; } - if (service->network != NULL) { - if (prepare_network(service) == FALSE) + if (service->network) { + if (!prepare_network(service)) return -EINVAL; switch (service->security) { @@ -5682,9 +5944,9 @@ static int service_connect(struct connman_service *service) } if (__connman_stats_service_register(service) == 0) { - __connman_stats_get(service, FALSE, + __connman_stats_get(service, false, &service->stats.data); - __connman_stats_get(service, TRUE, + __connman_stats_get(service, true, &service->stats_roaming.data); } @@ -5695,7 +5957,7 @@ static int service_connect(struct connman_service *service) err = __connman_network_connect(service->network); } else if (service->type == CONNMAN_SERVICE_TYPE_VPN && - service->provider != NULL) + service->provider) err = __connman_provider_connect(service->provider); else return -EOPNOTSUPP; @@ -5711,32 +5973,36 @@ static int service_connect(struct connman_service *service) return err; } - -int __connman_service_connect(struct connman_service *service) +int __connman_service_connect(struct connman_service *service, + enum connman_service_connect_reason reason) { int err; - DBG("service %p state %s", service, state2string(service->state)); + DBG("service %p state %s connect reason %s -> %s", + service, state2string(service->state), + reason2string(service->connect_reason), + reason2string(reason)); - if (is_connected(service) == TRUE) + if (is_connected(service)) return -EISCONN; - if (is_connecting(service) == TRUE) + if (is_connecting(service)) return -EALREADY; switch (service->type) { case CONNMAN_SERVICE_TYPE_UNKNOWN: case CONNMAN_SERVICE_TYPE_SYSTEM: case CONNMAN_SERVICE_TYPE_GPS: - case CONNMAN_SERVICE_TYPE_GADGET: + case CONNMAN_SERVICE_TYPE_P2P: return -EINVAL; default: - if (is_ipconfig_usable(service) == FALSE) + if (!is_ipconfig_usable(service)) return -ENOLINK; err = service_connect(service); } + service->connect_reason = reason; if (err >= 0) { set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN); return 0; @@ -5750,13 +6016,13 @@ int __connman_service_connect(struct connman_service *service) return -EINPROGRESS; } - if (service->network != NULL) + if (service->network) __connman_network_disconnect(service->network); else if (service->type == CONNMAN_SERVICE_TYPE_VPN && - service->provider != NULL) + service->provider) connman_provider_disconnect(service->provider); - if (service->userconnect == TRUE) { + if (service->connect_reason == CONNMAN_SERVICE_CONNECT_REASON_USER) { if (err == -ENOKEY || err == -EPERM) { DBusMessage *pending = NULL; @@ -5766,14 +6032,16 @@ int __connman_service_connect(struct connman_service *service) * after the real hidden network is connected or * connection failed. */ - if (service->hidden == TRUE) { + if (service->hidden) { pending = service->pending; service->pending = NULL; } err = __connman_agent_request_passphrase_input(service, - request_input_cb, pending); - if (service->hidden == TRUE && err != -EINPROGRESS) + request_input_cb, + get_dbus_sender(service), + pending); + if (service->hidden && err != -EINPROGRESS) service->pending = pending; return err; @@ -5790,14 +6058,17 @@ int __connman_service_disconnect(struct connman_service *service) DBG("service %p", service); - service->userconnect = FALSE; + service->connect_reason = CONNMAN_SERVICE_CONNECT_REASON_NONE; + service->proxy = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN; connman_agent_cancel(service); - if (service->network != NULL) { + reply_pending(service, ECONNABORTED); + + if (service->network) { err = __connman_network_disconnect(service->network); } else if (service->type == CONNMAN_SERVICE_TYPE_VPN && - service->provider != NULL) + service->provider) err = connman_provider_disconnect(service->provider); else return -EOPNOTSUPP; @@ -5836,21 +6107,19 @@ int __connman_service_disconnect_all(void) DBG(""); - for (iter = service_list; iter != NULL; iter = iter->next) { + for (iter = service_list; iter; iter = iter->next) { service = iter->data; - if (is_connected(service) == FALSE) + if (!is_connected(service)) break; services = g_slist_prepend(services, service); } - for (list = services; list != NULL; list = list->next) { + for (list = services; list; list = list->next) { struct connman_service *service = list->data; - service->ignore = TRUE; - - set_reconnect_state(service, FALSE); + service->ignore = true; __connman_service_disconnect(service); } @@ -5902,14 +6171,10 @@ int __connman_service_provision_changed(const char *ident) * Because the provision_changed() might have set some services * as favorite, we must sort the sequence now. */ - if (services_dirty == TRUE) { - services_dirty = FALSE; + if (services_dirty) { + services_dirty = false; - if (service_list->next != NULL) { - service_list = g_list_sort(service_list, - service_compare); - service_schedule_changed(); - } + service_list_sort(); __connman_connection_update_gateway(); } @@ -5920,7 +6185,7 @@ int __connman_service_provision_changed(const char *ident) void __connman_service_set_config(struct connman_service *service, const char *file_id, const char *entry) { - if (service == NULL) + if (!service) return; g_free(service->config_file); @@ -5941,13 +6206,13 @@ static struct connman_service *service_get(const char *identifier) struct connman_service *service; service = g_hash_table_lookup(service_hash, identifier); - if (service != NULL) { + if (service) { connman_service_ref(service); return service; } service = connman_service_create(); - if (service == NULL) + if (!service) return NULL; DBG("service %p", service); @@ -5966,7 +6231,7 @@ static int service_register(struct connman_service *service) { DBG("service %p", service); - if (service->path != NULL) + if (service->path) return -EALREADY; service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH, @@ -5974,74 +6239,75 @@ static int service_register(struct connman_service *service) DBG("path %s", service->path); - __connman_config_provision_service(service); - - service_load(service); + if (__connman_config_provision_service(service) < 0) + service_load(service); g_dbus_register_interface(connection, service->path, CONNMAN_SERVICE_INTERFACE, service_methods, service_signals, NULL, service, NULL); - if (service_list->next != NULL) { - service_list = g_list_sort(service_list, service_compare); - service_schedule_changed(); - } + service_list_sort(); __connman_connection_update_gateway(); return 0; } -static void service_up(struct connman_ipconfig *ipconfig) +static void service_up(struct connman_ipconfig *ipconfig, + const char *ifname) { struct connman_service *service = __connman_ipconfig_get_data(ipconfig); - DBG("%s up", __connman_ipconfig_get_ifname(ipconfig)); + DBG("%s up", ifname); link_changed(service); - service->stats.valid = FALSE; - service->stats_roaming.valid = FALSE; + service->stats.valid = false; + service->stats_roaming.valid = false; } -static void service_down(struct connman_ipconfig *ipconfig) +static void service_down(struct connman_ipconfig *ipconfig, + const char *ifname) { - DBG("%s down", __connman_ipconfig_get_ifname(ipconfig)); + DBG("%s down", ifname); } -static void service_lower_up(struct connman_ipconfig *ipconfig) +static void service_lower_up(struct connman_ipconfig *ipconfig, + const char *ifname) { struct connman_service *service = __connman_ipconfig_get_data(ipconfig); - DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig)); + DBG("%s lower up", ifname); stats_start(service); } -static void service_lower_down(struct connman_ipconfig *ipconfig) +static void service_lower_down(struct connman_ipconfig *ipconfig, + const char *ifname) { struct connman_service *service = __connman_ipconfig_get_data(ipconfig); - DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig)); + DBG("%s lower down", ifname); - if (is_idle_state(service, service->state_ipv4) == FALSE) + if (!is_idle_state(service, service->state_ipv4)) __connman_ipconfig_disable(service->ipconfig_ipv4); - if (is_idle_state(service, service->state_ipv6) == FALSE) + if (!is_idle_state(service, service->state_ipv6)) __connman_ipconfig_disable(service->ipconfig_ipv6); stats_stop(service); service_save(service); } -static void service_ip_bound(struct connman_ipconfig *ipconfig) +static void service_ip_bound(struct connman_ipconfig *ipconfig, + const char *ifname) { struct connman_service *service = __connman_ipconfig_get_data(ipconfig); enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN; enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN; - DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig)); + DBG("%s ip bound", ifname); type = __connman_ipconfig_get_config_type(ipconfig); method = __connman_ipconfig_get_method(ipconfig); @@ -6058,13 +6324,14 @@ static void service_ip_bound(struct connman_ipconfig *ipconfig) settings_changed(service, ipconfig); } -static void service_ip_release(struct connman_ipconfig *ipconfig) +static void service_ip_release(struct connman_ipconfig *ipconfig, + const char *ifname) { struct connman_service *service = __connman_ipconfig_get_data(ipconfig); enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN; enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN; - DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig)); + DBG("%s ip release", ifname); type = __connman_ipconfig_get_config_type(ipconfig); method = __connman_ipconfig_get_method(ipconfig); @@ -6087,11 +6354,12 @@ static void service_ip_release(struct connman_ipconfig *ipconfig) settings_changed(service, ipconfig); } -static void service_route_changed(struct connman_ipconfig *ipconfig) +static void service_route_changed(struct connman_ipconfig *ipconfig, + const char *ifname) { struct connman_service *service = __connman_ipconfig_get_data(ipconfig); - DBG("%s route changed", __connman_ipconfig_get_ifname(ipconfig)); + DBG("%s route changed", ifname); settings_changed(service, ipconfig); } @@ -6114,7 +6382,7 @@ static struct connman_ipconfig *create_ip4config(struct connman_service *service ipconfig_ipv4 = __connman_ipconfig_create(index, CONNMAN_IPCONFIG_TYPE_IPV4); - if (ipconfig_ipv4 == NULL) + if (!ipconfig_ipv4) return NULL; __connman_ipconfig_set_method(ipconfig_ipv4, method); @@ -6133,7 +6401,7 @@ static struct connman_ipconfig *create_ip6config(struct connman_service *service ipconfig_ipv6 = __connman_ipconfig_create(index, CONNMAN_IPCONFIG_TYPE_IPV6); - if (ipconfig_ipv6 == NULL) + if (!ipconfig_ipv6) return NULL; __connman_ipconfig_set_data(ipconfig_ipv6, service); @@ -6147,11 +6415,11 @@ void __connman_service_read_ip4config(struct connman_service *service) { GKeyFile *keyfile; - if (service->ipconfig_ipv4 == NULL) + if (!service->ipconfig_ipv4) return; keyfile = connman_storage_load_service(service->identifier); - if (keyfile == NULL) + if (!keyfile) return; __connman_ipconfig_load(service->ipconfig_ipv4, keyfile, @@ -6165,7 +6433,7 @@ void connman_service_create_ip4config(struct connman_service *service, { DBG("ipv4 %p", service->ipconfig_ipv4); - if (service->ipconfig_ipv4 != NULL) + if (service->ipconfig_ipv4) return; service->ipconfig_ipv4 = create_ip4config(service, index, @@ -6177,11 +6445,11 @@ void __connman_service_read_ip6config(struct connman_service *service) { GKeyFile *keyfile; - if (service->ipconfig_ipv6 == NULL) + if (!service->ipconfig_ipv6) return; keyfile = connman_storage_load_service(service->identifier); - if (keyfile == NULL) + if (!keyfile) return; __connman_ipconfig_load(service->ipconfig_ipv6, keyfile, @@ -6195,7 +6463,7 @@ void connman_service_create_ip6config(struct connman_service *service, { DBG("ipv6 %p", service->ipconfig_ipv6); - if (service->ipconfig_ipv6 != NULL) + if (service->ipconfig_ipv6) return; service->ipconfig_ipv6 = create_ip6config(service, index); @@ -6215,17 +6483,15 @@ struct connman_service *connman_service_lookup_from_network(struct connman_netwo const char *ident, *group; char *name; - DBG("network %p", network); - - if (network == NULL) + if (!network) return NULL; ident = __connman_network_get_ident(network); - if (ident == NULL) + if (!ident) return NULL; group = connman_network_get_group(network); - if (group == NULL) + if (!group) return NULL; name = g_strdup_printf("%s_%s_%s", @@ -6241,7 +6507,7 @@ struct connman_service *__connman_service_lookup_from_index(int index) struct connman_service *service; GList *list; - for (list = service_list; list != NULL; list = list->next) { + for (list = service_list; list; list = list->next) { service = list->data; if (__connman_ipconfig_get_index(service->ipconfig_ipv4) @@ -6271,34 +6537,46 @@ const char *__connman_service_get_path(struct connman_service *service) return service->path; } +const char *__connman_service_get_name(struct connman_service *service) +{ + return service->name; +} + +enum connman_service_state __connman_service_get_state(struct connman_service *service) +{ + return service->state; +} + unsigned int __connman_service_get_order(struct connman_service *service) { - if (service == NULL) + unsigned int order = 0; + + if (!service) return 0; - if (service->favorite == FALSE) { - service->order = 0; - goto done; - } + service->order = 0; + + if (!service->favorite) + return 0; if (service == service_list->data) - service->order = 1; - else if (service->type == CONNMAN_SERVICE_TYPE_VPN && - service->do_split_routing == FALSE) + order = 1; + + if (service->type == CONNMAN_SERVICE_TYPE_VPN && + !service->do_split_routing) { service->order = 10; - else - service->order = 0; + order = 10; + } DBG("service %p name %s order %d split %d", service, service->name, - service->order, service->do_split_routing); + order, service->do_split_routing); -done: - return service->order; + return order; } void __connman_service_update_ordering(void) { - if (service_list != NULL && service_list->next != NULL) + if (service_list && service_list->next) service_list = g_list_sort(service_list, service_compare); } @@ -6319,6 +6597,8 @@ static enum connman_service_type convert_network_type(struct connman_network *ne return CONNMAN_SERVICE_TYPE_BLUETOOTH; case CONNMAN_NETWORK_TYPE_CELLULAR: return CONNMAN_SERVICE_TYPE_CELLULAR; + case CONNMAN_NETWORK_TYPE_GADGET: + return CONNMAN_SERVICE_TYPE_GADGET; } return CONNMAN_SERVICE_TYPE_UNKNOWN; @@ -6326,19 +6606,19 @@ static enum connman_service_type convert_network_type(struct connman_network *ne static enum connman_service_security convert_wifi_security(const char *security) { - if (security == NULL) + if (!security) return CONNMAN_SERVICE_SECURITY_UNKNOWN; - else if (g_str_equal(security, "none") == TRUE) + else if (g_str_equal(security, "none")) return CONNMAN_SERVICE_SECURITY_NONE; - else if (g_str_equal(security, "wep") == TRUE) + else if (g_str_equal(security, "wep")) return CONNMAN_SERVICE_SECURITY_WEP; - else if (g_str_equal(security, "psk") == TRUE) + else if (g_str_equal(security, "psk")) return CONNMAN_SERVICE_SECURITY_PSK; - else if (g_str_equal(security, "ieee8021x") == TRUE) + else if (g_str_equal(security, "ieee8021x")) return CONNMAN_SERVICE_SECURITY_8021X; - else if (g_str_equal(security, "wpa") == TRUE) + else if (g_str_equal(security, "wpa")) return CONNMAN_SERVICE_SECURITY_WPA; - else if (g_str_equal(security, "rsn") == TRUE) + else if (g_str_equal(security, "rsn")) return CONNMAN_SERVICE_SECURITY_RSN; else return CONNMAN_SERVICE_SECURITY_UNKNOWN; @@ -6352,21 +6632,21 @@ static void update_from_network(struct connman_service *service, DBG("service %p network %p", service, network); - if (is_connected(service) == TRUE) + if (is_connected(service)) return; - if (is_connecting(service) == TRUE) + if (is_connecting(service)) return; str = connman_network_get_string(network, "Name"); - if (str != NULL) { + if (str) { g_free(service->name); service->name = g_strdup(str); - service->hidden = FALSE; + service->hidden = false; } else { g_free(service->name); service->name = NULL; - service->hidden = TRUE; + service->hidden = true; } service->strength = connman_network_get_strength(network); @@ -6386,20 +6666,17 @@ static void update_from_network(struct connman_service *service, if (service->type == CONNMAN_SERVICE_TYPE_WIFI) service->wps = connman_network_get_bool(network, "WiFi.WPS"); - if (service->strength > strength && service->network != NULL) { + if (service->strength > strength && service->network) { connman_network_unref(service->network); service->network = connman_network_ref(network); strength_changed(service); } - if (service->network == NULL) + if (!service->network) service->network = connman_network_ref(network); - if (service_list->next != NULL) { - service_list = g_list_sort(service_list, service_compare); - service_schedule_changed(); - } + service_list_sort(); } /** @@ -6419,15 +6696,15 @@ struct connman_service * __connman_service_create_from_network(struct connman_ne DBG("network %p", network); - if (network == NULL) + if (!network) return NULL; ident = __connman_network_get_ident(network); - if (ident == NULL) + if (!ident) return NULL; group = connman_network_get_group(network); - if (group == NULL) + if (!group) return NULL; name = g_strdup_printf("%s_%s_%s", @@ -6435,13 +6712,13 @@ struct connman_service * __connman_service_create_from_network(struct connman_ne service = service_get(name); g_free(name); - if (service == NULL) + if (!service) return NULL; - if (__connman_network_get_weakness(network) == TRUE) + if (__connman_network_get_weakness(network)) return service; - if (service->path != NULL) { + if (service->path) { update_from_network(service, network); __connman_connection_update_gateway(); return service; @@ -6450,11 +6727,11 @@ struct connman_service * __connman_service_create_from_network(struct connman_ne service->type = convert_network_type(network); auto_connect_types = connman_setting_get_uint_list("DefaultAutoConnectTechnologies"); - service->autoconnect = FALSE; - for (i = 0; auto_connect_types != NULL && + service->autoconnect = false; + for (i = 0; auto_connect_types && auto_connect_types[i] != 0; i++) { if (service->type == auto_connect_types[i]) { - service->autoconnect = TRUE; + service->autoconnect = true; break; } } @@ -6468,9 +6745,10 @@ struct connman_service * __connman_service_create_from_network(struct connman_ne case CONNMAN_SERVICE_TYPE_GADGET: case CONNMAN_SERVICE_TYPE_WIFI: case CONNMAN_SERVICE_TYPE_CELLULAR: + case CONNMAN_SERVICE_TYPE_P2P: break; case CONNMAN_SERVICE_TYPE_ETHERNET: - service->favorite = TRUE; + service->favorite = true; break; } @@ -6481,19 +6759,19 @@ struct connman_service * __connman_service_create_from_network(struct connman_ne index = connman_network_get_index(network); - if (service->ipconfig_ipv4 == NULL) + if (!service->ipconfig_ipv4) service->ipconfig_ipv4 = create_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP); - if (service->ipconfig_ipv6 == NULL) + if (!service->ipconfig_ipv6) service->ipconfig_ipv6 = create_ip6config(service, index); service_register(service); - if (service->favorite == TRUE) { + if (service->favorite) { device = connman_network_get_device(service->network); - if (device && connman_device_get_scanning(device) == FALSE) - __connman_service_auto_connect(); + if (device && !connman_device_get_scanning(device)) + __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO); } __connman_notifier_service_add(service, service->name); @@ -6504,20 +6782,18 @@ struct connman_service * __connman_service_create_from_network(struct connman_ne void __connman_service_update_from_network(struct connman_network *network) { - connman_bool_t need_sort = FALSE; + bool need_sort = false; struct connman_service *service; uint8_t strength; - connman_bool_t roaming; + bool roaming; const char *name; - connman_bool_t stats_enable; - - DBG("network %p", network); + bool stats_enable; service = connman_service_lookup_from_network(network); - if (service == NULL) + if (!service) return; - if (service->network == NULL) + if (!service->network) return; name = connman_network_get_string(service->network, "Name"); @@ -6525,7 +6801,7 @@ void __connman_service_update_from_network(struct connman_network *network) g_free(service->name); service->name = g_strdup(name); - if (allow_property_changed(service) == TRUE) + if (allow_property_changed(service)) connman_dbus_property_changed_basic(service->path, CONNMAN_SERVICE_INTERFACE, "Name", DBUS_TYPE_STRING, &service->name); @@ -6539,7 +6815,7 @@ void __connman_service_update_from_network(struct connman_network *network) goto roaming; service->strength = strength; - need_sort = TRUE; + need_sort = true; strength_changed(service); @@ -6549,24 +6825,20 @@ roaming: goto sorting; stats_enable = stats_enabled(service); - if (stats_enable == TRUE) + if (stats_enable) stats_stop(service); service->roaming = roaming; - need_sort = TRUE; + need_sort = true; - if (stats_enable == TRUE) + if (stats_enable) stats_start(service); roaming_changed(service); sorting: - if (need_sort == TRUE) { - if (service_list->next != NULL) { - service_list = g_list_sort(service_list, - service_compare); - service_schedule_changed(); - } + if (need_sort) { + service_list_sort(); } } @@ -6574,13 +6846,14 @@ void __connman_service_remove_from_network(struct connman_network *network) { struct connman_service *service; - DBG("network %p", network); - service = connman_service_lookup_from_network(network); - if (service == NULL) + + DBG("network %p service %p", network, service); + + if (!service) return; - service->ignore = TRUE; + service->ignore = true; __connman_connection_gateway_remove(service, CONNMAN_IPCONFIG_TYPE_ALL); @@ -6605,42 +6878,42 @@ __connman_service_create_from_provider(struct connman_provider *provider) DBG("provider %p", provider); ident = __connman_provider_get_ident(provider); - if (ident == NULL) + if (!ident) return NULL; name = g_strdup_printf("vpn_%s", ident); service = service_get(name); g_free(name); - if (service == NULL) + if (!service) return NULL; service->type = CONNMAN_SERVICE_TYPE_VPN; service->provider = connman_provider_ref(provider); - service->autoconnect = FALSE; - service->userconnect = TRUE; + service->autoconnect = false; + service->favorite = true; service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE; service->state = combine_state(service->state_ipv4, service->state_ipv6); str = connman_provider_get_string(provider, "Name"); - if (str != NULL) { + if (str) { g_free(service->name); service->name = g_strdup(str); - service->hidden = FALSE; + service->hidden = false; } else { g_free(service->name); service->name = NULL; - service->hidden = TRUE; + service->hidden = true; } service->strength = 0; - if (service->ipconfig_ipv4 == NULL) + if (!service->ipconfig_ipv4) service->ipconfig_ipv4 = create_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL); - if (service->ipconfig_ipv6 == NULL) + if (!service->ipconfig_ipv6) service->ipconfig_ipv6 = create_ip6config(service, index); service_register(service); @@ -6651,7 +6924,7 @@ __connman_service_create_from_provider(struct connman_provider *provider) return service; } -static void remove_unprovisioned_services() +static void remove_unprovisioned_services(void) { gchar **services; GKeyFile *keyfile, *configkeyfile; @@ -6659,29 +6932,29 @@ static void remove_unprovisioned_services() int i = 0; services = connman_storage_get_services(); - if (services == NULL) + if (!services) return; - for (;services[i] != NULL; i++) { + for (; services[i]; i++) { file = section = NULL; keyfile = configkeyfile = NULL; keyfile = connman_storage_load_service(services[i]); - if (keyfile == NULL) + if (!keyfile) continue; file = g_key_file_get_string(keyfile, services[i], "Config.file", NULL); - if (file == NULL) + if (!file) goto next; section = g_key_file_get_string(keyfile, services[i], "Config.ident", NULL); - if (section == NULL) + if (!section) goto next; configkeyfile = __connman_storage_load_config(file); - if (configkeyfile == NULL) { + if (!configkeyfile) { /* * Config file is missing, remove the provisioned * service. @@ -6690,7 +6963,7 @@ static void remove_unprovisioned_services() goto next; } - if (g_key_file_has_group(configkeyfile, section) == FALSE) + if (!g_key_file_has_group(configkeyfile, section)) /* * Config section is missing, remove the provisioned * service. @@ -6698,10 +6971,10 @@ static void remove_unprovisioned_services() __connman_storage_remove_service(services[i]); next: - if (keyfile != NULL) + if (keyfile) g_key_file_free(keyfile); - if (configkeyfile != NULL) + if (configkeyfile) g_key_file_free(configkeyfile); g_free(section); @@ -6777,11 +7050,18 @@ void __connman_service_cleanup(void) { DBG(""); + if (vpn_autoconnect_timeout) { + g_source_remove(vpn_autoconnect_timeout); + vpn_autoconnect_timeout = 0; + } + if (autoconnect_timeout != 0) { g_source_remove(autoconnect_timeout); autoconnect_timeout = 0; } + connman_agent_driver_unregister(&agent_driver); + g_list_free(service_list); service_list = NULL; @@ -6799,7 +7079,5 @@ void __connman_service_cleanup(void) } g_free(services_notify); - connman_agent_driver_unregister(&agent_driver); - dbus_connection_unref(connection); } |