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/config.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/config.c')
-rw-r--r-- | src/config.c | 540 |
1 files changed, 317 insertions, 223 deletions
diff --git a/src/config.c b/src/config.c index e5c9c7ec..330ae817 100644 --- a/src/config.c +++ b/src/config.c @@ -2,7 +2,7 @@ * * Connection Manager * - * Copyright (C) 2007-2012 Intel Corporation. All rights reserved. + * Copyright (C) 2007-2013 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 @@ -55,8 +55,8 @@ struct connman_config_service { GSList *service_identifiers; char *config_ident; /* file prefix */ char *config_entry; /* entry name */ - connman_bool_t hidden; - connman_bool_t virtual; + bool hidden; + bool virtual; char *virtual_file; char *ipv4_address; char *ipv4_netmask; @@ -81,7 +81,7 @@ struct connman_config { static GHashTable *config_table = NULL; -static connman_bool_t cleanup = FALSE; +static bool cleanup = false; /* Definition of possible strings in the .config files */ #define CONFIG_KEY_NAME "Name" @@ -162,38 +162,40 @@ static void unregister_service(gpointer data) char *service_id; GSList *list; - if (cleanup == TRUE) + if (cleanup) goto free_only; connman_info("Removing service configuration %s", config_service->ident); - if (config_service->virtual == TRUE) + if (config_service->virtual) goto free_only; - for (list = config_service->service_identifiers; list != NULL; + for (list = config_service->service_identifiers; list; list = list->next) { service_id = list->data; service = __connman_service_lookup_from_ident(service_id); - if (service != NULL) { - __connman_service_set_immutable(service, FALSE); + if (service) { + __connman_service_set_immutable(service, false); __connman_service_set_config(service, NULL, NULL); __connman_service_remove(service); /* - * Ethernet service cannot be removed by + * Ethernet or gadget service cannot be removed by * __connman_service_remove() so reset the ipconfig * here. */ if (connman_service_get_type(service) == - CONNMAN_SERVICE_TYPE_ETHERNET) { + CONNMAN_SERVICE_TYPE_ETHERNET || + connman_service_get_type(service) == + CONNMAN_SERVICE_TYPE_GADGET) { __connman_service_disconnect(service); __connman_service_reset_ipconfig(service, CONNMAN_IPCONFIG_TYPE_IPV4, NULL, NULL); __connman_service_reset_ipconfig(service, CONNMAN_IPCONFIG_TYPE_IPV6, NULL, NULL); - __connman_service_set_ignore(service, TRUE); + __connman_service_set_ignore(service, true); /* * After these operations, user needs to @@ -203,7 +205,7 @@ static void unregister_service(gpointer data) } } - if (__connman_storage_remove_service(service_id) == FALSE) + if (!__connman_storage_remove_service(service_id)) DBG("Could not remove all files for service %s", service_id); } @@ -247,7 +249,7 @@ static void check_keys(GKeyFile *keyfile, const char *group, gsize nb_avail_keys, i, j; avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL); - if (avail_keys == NULL) + if (!avail_keys) return; /* @@ -259,7 +261,7 @@ static void check_keys(GKeyFile *keyfile, const char *group, if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0) break; - if (possible_keys[j] == NULL) + if (!possible_keys[j]) connman_warn("Unknown configuration key %s in [%s]", avail_keys[i], group); } @@ -313,11 +315,11 @@ static int parse_address(const char *address_str, int address_family, char **route; route = g_strsplit(address_str, "/", 0); - if (route == NULL) + if (!route) return -EINVAL; addr_str = route[0]; - if (addr_str == NULL || addr_str[0] == '\0') { + if (!addr_str || addr_str[0] == '\0') { err = -EINVAL; goto out; } @@ -326,20 +328,17 @@ static int parse_address(const char *address_str, int address_family, goto out; mask_str = route[1]; - if (mask_str == NULL || mask_str[0] == '\0') { + if (!mask_str || mask_str[0] == '\0') { err = -EINVAL; goto out; } gw_str = route[2]; - if (gw_str == NULL || gw_str[0] == '\0') { - err = -EINVAL; - goto out; + if (gw_str && gw_str[0]) { + if ((err = check_family(gw_str, address_family)) < 0) + goto out; } - if ((err = check_family(gw_str, address_family)) < 0) - goto out; - g_free(*address); *address = g_strdup(addr_str); @@ -349,7 +348,10 @@ static int parse_address(const char *address_str, int address_family, g_free(*gateway); *gateway = g_strdup(gw_str); - DBG("address %s/%s via %s", *address, *netmask, *gateway); + if (*gateway) + DBG("address %s/%s via %s", *address, *netmask, *gateway); + else + DBG("address %s/%s", *address, *netmask); out: g_strfreev(route); @@ -357,19 +359,19 @@ out: return err; } -static connman_bool_t check_address(char *address_str, char **address) +static bool check_address(char *address_str, char **address) { if (g_ascii_strcasecmp(address_str, "auto") == 0 || g_ascii_strcasecmp(address_str, "dhcp") == 0 || g_ascii_strcasecmp(address_str, "off") == 0) { *address = address_str; - return FALSE; + return false; } - return TRUE; + return true; } -static connman_bool_t load_service_generic(GKeyFile *keyfile, +static bool load_service_generic(GKeyFile *keyfile, const char *group, struct connman_config *config, struct connman_config_service *service) { @@ -377,8 +379,8 @@ static connman_bool_t load_service_generic(GKeyFile *keyfile, char **strlist; gsize length; - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv4, NULL); - if (str != NULL && check_address(str, &service->ipv4_address) == TRUE) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv4, NULL); + if (str && check_address(str, &service->ipv4_address)) { mask = NULL; if (parse_address(str, AF_INET, &service->ipv4_address, @@ -389,7 +391,7 @@ static connman_bool_t load_service_generic(GKeyFile *keyfile, goto err; } - if (g_strrstr(mask, ".") == NULL) { + if (!g_strrstr(mask, ".")) { /* We have netmask length */ in_addr_t addr; struct in_addr netmask_in; @@ -412,8 +414,8 @@ static connman_bool_t load_service_generic(GKeyFile *keyfile, g_free(str); } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv6, NULL); - if (str != NULL && check_address(str, &service->ipv6_address) == TRUE) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv6, NULL); + if (str && check_address(str, &service->ipv6_address)) { long int value; char *ptr; @@ -437,29 +439,29 @@ static connman_bool_t load_service_generic(GKeyFile *keyfile, g_free(str); } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv6_PRIVACY, + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv6_PRIVACY, NULL); - if (str != NULL) { + if (str) { g_free(service->ipv6_privacy); service->ipv6_privacy = str; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_MAC, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_MAC, NULL); + if (str) { g_free(service->mac); service->mac = str; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_DOMAIN, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_DOMAIN, NULL); + if (str) { g_free(service->domain_name); service->domain_name = str; } - strlist = g_key_file_get_string_list(keyfile, group, + strlist = __connman_config_get_string_list(keyfile, group, SERVICE_KEY_NAMESERVERS, &length, NULL); - if (strlist != NULL) { + if (strlist) { if (length != 0) { g_strfreev(service->nameservers); service->nameservers = strlist; @@ -467,10 +469,10 @@ static connman_bool_t load_service_generic(GKeyFile *keyfile, g_strfreev(strlist); } - strlist = g_key_file_get_string_list(keyfile, group, + strlist = __connman_config_get_string_list(keyfile, group, SERVICE_KEY_SEARCH_DOMAINS, &length, NULL); - if (strlist != NULL) { + if (strlist) { if (length != 0) { g_strfreev(service->search_domains); service->search_domains = strlist; @@ -478,10 +480,10 @@ static connman_bool_t load_service_generic(GKeyFile *keyfile, g_strfreev(strlist); } - strlist = g_key_file_get_string_list(keyfile, group, + strlist = __connman_config_get_string_list(keyfile, group, SERVICE_KEY_TIMESERVERS, &length, NULL); - if (strlist != NULL) { + if (strlist) { if (length != 0) { g_strfreev(service->timeservers); service->timeservers = strlist; @@ -489,7 +491,7 @@ static connman_bool_t load_service_generic(GKeyFile *keyfile, g_strfreev(strlist); } - return TRUE; + return true; err: g_free(service->ident); @@ -502,39 +504,39 @@ err: g_free(service->mac); g_free(service); - return FALSE; + return false; } -static connman_bool_t load_service(GKeyFile *keyfile, const char *group, +static bool load_service(GKeyFile *keyfile, const char *group, struct connman_config *config) { struct connman_config_service *service; const char *ident; char *str, *hex_ssid; - gboolean service_created = FALSE; + bool service_created = false; /* Strip off "service_" prefix */ ident = group + 8; if (strlen(ident) < 1) - return FALSE; + return false; /* Verify that provided keys are good */ check_keys(keyfile, group, service_possible_keys); service = g_hash_table_lookup(config->service_table, ident); - if (service == NULL) { + if (!service) { service = g_try_new0(struct connman_config_service, 1); - if (service == NULL) - return FALSE; + if (!service) + return false; service->ident = g_strdup(ident); - service_created = TRUE; + service_created = true; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL); + if (str) { g_free(service->type); service->type = str; } else { @@ -543,8 +545,8 @@ static connman_bool_t load_service(GKeyFile *keyfile, const char *group, goto err; } - if (load_service_generic(keyfile, group, config, service) == FALSE) - return FALSE; + if (!load_service_generic(keyfile, group, config, service)) + return false; if (g_strcmp0(str, "ethernet") == 0) { service->config_ident = g_strdup(config->ident); @@ -553,24 +555,24 @@ static connman_bool_t load_service(GKeyFile *keyfile, const char *group, g_hash_table_insert(config->service_table, service->ident, service); - return 0; + return true; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_NAME, NULL); + if (str) { g_free(service->name); service->name = str; } - hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID, + hex_ssid = __connman_config_get_string(keyfile, group, SERVICE_KEY_SSID, NULL); - if (hex_ssid != NULL) { + if (hex_ssid) { char *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); goto err; } @@ -590,13 +592,13 @@ static connman_bool_t load_service(GKeyFile *keyfile, const char *group, g_free(service->ssid); service->ssid = ssid; service->ssid_len = hex_ssid_len / 2; - } else if (service->name != NULL) { + } else if (service->name) { char *ssid; unsigned int ssid_len; ssid_len = strlen(service->name); ssid = g_try_malloc0(ssid_len); - if (ssid == NULL) + if (!ssid) goto err; memcpy(ssid, service->name, ssid_len); @@ -605,59 +607,59 @@ static connman_bool_t load_service(GKeyFile *keyfile, const char *group, service->ssid_len = ssid_len; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_EAP, NULL); + if (str) { g_free(service->eap); service->eap = str; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL); + if (str) { g_free(service->ca_cert_file); service->ca_cert_file = str; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL); + if (str) { g_free(service->client_cert_file); service->client_cert_file = str; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL); + if (str) { g_free(service->private_key_file); service->private_key_file = str; } - str = g_key_file_get_string(keyfile, group, + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PRV_KEY_PASS, NULL); - if (str != NULL) { + if (str) { g_free(service->private_key_passphrase); service->private_key_passphrase = str; } - str = g_key_file_get_string(keyfile, group, + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL); - if (str != NULL) { + if (str) { g_free(service->private_key_passphrase_type); service->private_key_passphrase_type = str; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL); + if (str) { g_free(service->identity); service->identity = str; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL); + if (str) { g_free(service->phase2); service->phase2 = str; } - str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE, + str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE, NULL); - if (str != NULL) { + if (str) { g_free(service->passphrase); service->passphrase = str; } @@ -665,7 +667,7 @@ static connman_bool_t load_service(GKeyFile *keyfile, const char *group, service->config_ident = g_strdup(config->ident); service->config_entry = g_strdup_printf("service_%s", service->ident); - service->hidden = g_key_file_get_boolean(keyfile, group, + service->hidden = __connman_config_get_bool(keyfile, group, SERVICE_KEY_HIDDEN, NULL); if (service_created) @@ -674,10 +676,10 @@ static connman_bool_t load_service(GKeyFile *keyfile, const char *group, connman_info("Adding service configuration %s", service->ident); - return TRUE; + return true; err: - if (service_created == TRUE) { + if (service_created) { g_free(service->ident); g_free(service->type); g_free(service->name); @@ -685,23 +687,23 @@ err: g_free(service); } - return FALSE; + return false; } -static connman_bool_t load_service_from_keyfile(GKeyFile *keyfile, +static bool load_service_from_keyfile(GKeyFile *keyfile, struct connman_config *config) { - connman_bool_t found = FALSE; + bool found = false; char **groups; int i; groups = g_key_file_get_groups(keyfile, NULL); - for (i = 0; groups[i] != NULL; i++) { - if (g_str_has_prefix(groups[i], "service_") == FALSE) + for (i = 0; groups[i]; i++) { + if (!g_str_has_prefix(groups[i], "service_")) continue; - if (load_service(keyfile, groups[i], config) == TRUE) - found = TRUE; + if (load_service(keyfile, groups[i], config)) + found = true; } g_strfreev(groups); @@ -717,25 +719,27 @@ static int load_config(struct connman_config *config) DBG("config %p", config); keyfile = __connman_storage_load_config(config->ident); - if (keyfile == NULL) + if (!keyfile) return -EIO; + g_key_file_set_list_separator(keyfile, ','); + /* Verify keys validity of the global section */ check_keys(keyfile, "global", config_possible_keys); - str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL); + if (str) { g_free(config->name); config->name = str; } - str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL); - if (str != NULL) { + str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL); + if (str) { g_free(config->description); config->description = str; } - if (load_service_from_keyfile(keyfile, config) == FALSE) + if (!load_service_from_keyfile(keyfile, config)) connman_warn("Config file %s/%s.config does not contain any " "configuration that can be provisioned!", STORAGEDIR, config->ident); @@ -751,11 +755,11 @@ static struct connman_config *create_config(const char *ident) DBG("ident %s", ident); - if (g_hash_table_lookup(config_table, ident) != NULL) + if (g_hash_table_lookup(config_table, ident)) return NULL; config = g_try_new0(struct connman_config, 1); - if (config == NULL) + if (!config) return NULL; config->ident = g_strdup(ident); @@ -770,18 +774,18 @@ static struct connman_config *create_config(const char *ident) return config; } -static connman_bool_t validate_ident(const char *ident) +static bool validate_ident(const char *ident) { unsigned int i; - if (ident == NULL) - return FALSE; + if (!ident) + return false; for (i = 0; i < strlen(ident); i++) - if (g_ascii_isprint(ident[i]) == FALSE) - return FALSE; + if (!g_ascii_isprint(ident[i])) + return false; - return TRUE; + return true; } static int read_configs(void) @@ -791,31 +795,31 @@ static int read_configs(void) DBG(""); dir = g_dir_open(STORAGEDIR, 0, NULL); - if (dir != NULL) { + if (dir) { const gchar *file; - while ((file = g_dir_read_name(dir)) != NULL) { + while ((file = g_dir_read_name(dir))) { GString *str; gchar *ident; - if (g_str_has_suffix(file, ".config") == FALSE) + if (!g_str_has_suffix(file, ".config")) continue; ident = g_strrstr(file, ".config"); - if (ident == NULL) + if (!ident) continue; str = g_string_new_len(file, ident - file); - if (str == NULL) + if (!str) continue; ident = g_string_free(str, FALSE); - if (validate_ident(ident) == TRUE) { + if (validate_ident(ident)) { struct connman_config *config; config = create_config(ident); - if (config != NULL) + if (config) load_config(config); } else { connman_error("Invalid config ident %s", ident); @@ -834,19 +838,19 @@ static void config_notify_handler(struct inotify_event *event, { char *ext; - if (ident == NULL) + if (!ident) return; - if (g_str_has_suffix(ident, ".config") == FALSE) + if (!g_str_has_suffix(ident, ".config")) return; ext = g_strrstr(ident, ".config"); - if (ext == NULL) + if (!ext) return; *ext = '\0'; - if (validate_ident(ident) == FALSE) { + if (!validate_ident(ident)) { connman_error("Invalid config ident %s", ident); return; } @@ -858,7 +862,7 @@ static void config_notify_handler(struct inotify_event *event, struct connman_config *config; config = g_hash_table_lookup(config_table, ident); - if (config != NULL) { + if (config) { int ret; g_hash_table_remove_all(config->service_table); @@ -896,14 +900,61 @@ void __connman_config_cleanup(void) { DBG(""); - cleanup = TRUE; + cleanup = true; connman_inotify_unregister(STORAGEDIR, config_notify_handler); g_hash_table_destroy(config_table); config_table = NULL; - cleanup = FALSE; + cleanup = false; +} + +char *__connman_config_get_string(GKeyFile *key_file, + const char *group_name, const char *key, GError **error) +{ + char *str = g_key_file_get_string(key_file, group_name, key, error); + if (!str) + return NULL; + + return g_strchomp(str); +} + +char **__connman_config_get_string_list(GKeyFile *key_file, + const char *group_name, const char *key, gsize *length, GError **error) +{ + char **p; + char **strlist = g_key_file_get_string_list(key_file, group_name, key, + length, error); + if (!strlist) + return NULL; + + p = strlist; + while (*p) { + *p = g_strstrip(*p); + p++; + } + + return strlist; +} + +bool __connman_config_get_bool(GKeyFile *key_file, + const char *group_name, const char *key, GError **error) +{ + char *valstr; + bool val = false; + + valstr = g_key_file_get_value(key_file, group_name, key, error); + if (!valstr) + return false; + + valstr = g_strchomp(valstr); + if (strcmp(valstr, "true") == 0 || strcmp(valstr, "1") == 0) + val = true; + + g_free(valstr); + + return val; } static char *config_pem_fsid(const char *pem_file) @@ -912,7 +963,7 @@ static char *config_pem_fsid(const char *pem_file) unsigned *fsid = (unsigned *) &buf.f_fsid; unsigned long long fsid64; - if (pem_file == NULL) + if (!pem_file) return NULL; if (statfs(pem_file, &buf) < 0) { @@ -926,44 +977,43 @@ static char *config_pem_fsid(const char *pem_file) return g_strdup_printf("%llx", fsid64); } -static void provision_service_wifi(gpointer key, - struct connman_config_service *config, +static void provision_service_wifi(struct connman_config_service *config, struct connman_service *service, struct connman_network *network, const void *ssid, unsigned int ssid_len) { - if (config->eap != NULL) + if (config->eap) __connman_service_set_string(service, "EAP", config->eap); - if (config->identity != NULL) + if (config->identity) __connman_service_set_string(service, "Identity", config->identity); - if (config->ca_cert_file != NULL) + if (config->ca_cert_file) __connman_service_set_string(service, "CACertFile", config->ca_cert_file); - if (config->client_cert_file != NULL) + if (config->client_cert_file) __connman_service_set_string(service, "ClientCertFile", config->client_cert_file); - if (config->private_key_file != NULL) + if (config->private_key_file) __connman_service_set_string(service, "PrivateKeyFile", config->private_key_file); if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 && - config->private_key_file != NULL) { + config->private_key_file) { char *fsid; fsid = config_pem_fsid(config->private_key_file); - if (fsid == NULL) + if (!fsid) return; g_free(config->private_key_passphrase); config->private_key_passphrase = fsid; } - if (config->private_key_passphrase != NULL) { + if (config->private_key_passphrase) { __connman_service_set_string(service, "PrivateKeyPassphrase", config->private_key_passphrase); /* @@ -975,13 +1025,14 @@ static void provision_service_wifi(gpointer key, */ } - if (config->phase2 != NULL) + if (config->phase2) __connman_service_set_string(service, "Phase2", config->phase2); - if (config->passphrase != NULL) - __connman_service_set_string(service, "Passphrase", config->passphrase); + if (config->passphrase) + __connman_service_set_string(service, "Passphrase", + config->passphrase); - if (config->hidden == TRUE) + if (config->hidden) __connman_service_set_hidden(service); } @@ -994,7 +1045,8 @@ static gboolean remove_virtual_config(gpointer user_data) { struct connect_virtual *virtual = user_data; - __connman_service_connect(virtual->service); + __connman_service_connect(virtual->service, + CONNMAN_SERVICE_CONNECT_REASON_AUTO); g_hash_table_remove(config_table, virtual->vfile); g_free(virtual); @@ -1002,11 +1054,9 @@ static gboolean remove_virtual_config(gpointer user_data) return FALSE; } -static void provision_service(gpointer key, gpointer value, - gpointer user_data) +static int try_provision_service(struct connman_config_service *config, + struct connman_service *service) { - struct connman_service *service = user_data; - struct connman_config_service *config = value; struct connman_network *network; const void *service_id; enum connman_service_type type; @@ -1016,32 +1066,36 @@ static void provision_service(gpointer key, gpointer value, type = connman_service_get_type(service); if (type == CONNMAN_SERVICE_TYPE_WIFI && g_strcmp0(config->type, "wifi") != 0) - return; + return -ENOENT; if (type == CONNMAN_SERVICE_TYPE_ETHERNET && g_strcmp0(config->type, "ethernet") != 0) - return; + return -ENOENT; + + if (type == CONNMAN_SERVICE_TYPE_GADGET && + g_strcmp0(config->type, "gadget") != 0) + return -ENOENT; DBG("service %p ident %s", service, __connman_service_get_ident(service)); network = __connman_service_get_network(service); - if (network == NULL) { + if (!network) { connman_error("Service has no network set"); - return; + return -EINVAL; } DBG("network %p ident %s", network, connman_network_get_identifier(network)); - if (config->mac != NULL) { + if (config->mac) { struct connman_device *device; const char *device_addr; device = connman_network_get_device(network); - if (device == NULL) { + if (!device) { connman_error("Network device is missing"); - return; + return -ENODEV; } device_addr = connman_device_get_string(device, "Address"); @@ -1049,26 +1103,26 @@ static void provision_service(gpointer key, gpointer value, DBG("wants %s has %s", config->mac, device_addr); if (g_ascii_strcasecmp(device_addr, config->mac) != 0) - return; + return -ENOENT; } if (g_strcmp0(config->type, "wifi") == 0 && type == CONNMAN_SERVICE_TYPE_WIFI) { ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len); - if (ssid == NULL) { + if (!ssid) { connman_error("Network SSID not set"); - return; + return -EINVAL; } - if (config->ssid == NULL || ssid_len != config->ssid_len) - return; + if (!config->ssid || ssid_len != config->ssid_len) + return -ENOENT; if (memcmp(config->ssid, ssid, ssid_len) != 0) - return; + return -ENOENT; } - if (config->ipv6_address == NULL) { + if (!config->ipv6_address) { connman_network_set_ipv6_method(network, CONNMAN_IPCONFIG_METHOD_AUTO); } else if (g_ascii_strcasecmp(config->ipv6_address, "off") == 0) { @@ -1081,15 +1135,14 @@ static void provision_service(gpointer key, gpointer value, } else { struct connman_ipaddress *address; - if (config->ipv6_prefix_length == 0 || - config->ipv6_gateway == NULL) { - DBG("IPv6 prefix or gateway missing"); - return; + if (config->ipv6_prefix_length == 0) { + DBG("IPv6 prefix missing"); + return -EINVAL; } address = connman_ipaddress_alloc(AF_INET6); - if (address == NULL) - return; + if (!address) + return -ENOENT; connman_ipaddress_set_ipv6(address, config->ipv6_address, config->ipv6_prefix_length, @@ -1105,16 +1158,16 @@ static void provision_service(gpointer key, gpointer value, connman_ipaddress_free(address); } - if (config->ipv6_privacy != NULL) { + if (config->ipv6_privacy) { struct connman_ipconfig *ipconfig; ipconfig = __connman_service_get_ip6config(service); - if (ipconfig != NULL) + if (ipconfig) __connman_ipconfig_ipv6_set_privacy(ipconfig, config->ipv6_privacy); } - if (config->ipv4_address == NULL) { + if (!config->ipv4_address) { connman_network_set_ipv4_method(network, CONNMAN_IPCONFIG_METHOD_DHCP); } else if (g_ascii_strcasecmp(config->ipv4_address, "off") == 0) { @@ -1127,15 +1180,14 @@ static void provision_service(gpointer key, gpointer value, } else { struct connman_ipaddress *address; - if (config->ipv4_netmask == 0 || - config->ipv4_gateway == NULL) { - DBG("IPv4 netmask or gateway missing"); - return; + if (!config->ipv4_netmask) { + DBG("IPv4 netmask missing"); + return -EINVAL; } address = connman_ipaddress_alloc(AF_INET); - if (address == NULL) - return; + if (!address) + return -ENOENT; connman_ipaddress_set_ipv4(address, config->ipv4_address, config->ipv4_netmask, @@ -1158,48 +1210,49 @@ static void provision_service(gpointer key, gpointer value, g_slist_prepend(config->service_identifiers, g_strdup(service_id)); - if (config->virtual == FALSE) - __connman_service_set_immutable(service, TRUE); + if (!config->virtual) + __connman_service_set_immutable(service, true); - __connman_service_set_favorite_delayed(service, TRUE, TRUE); + __connman_service_set_favorite_delayed(service, true, true); __connman_service_set_config(service, config->config_ident, config->config_entry); - if (config->domain_name != NULL) + if (config->domain_name) __connman_service_set_domainname(service, config->domain_name); - if (config->nameservers != NULL) { + if (config->nameservers) { int i; __connman_service_nameserver_clear(service); - for (i = 0; config->nameservers[i] != NULL; i++) { + for (i = 0; config->nameservers[i]; i++) { __connman_service_nameserver_append(service, - config->nameservers[i], FALSE); + config->nameservers[i], false); } } - if (config->search_domains != NULL) + if (config->search_domains) __connman_service_set_search_domains(service, config->search_domains); - if (config->timeservers != NULL) + if (config->timeservers) __connman_service_set_timeservers(service, config->timeservers); if (g_strcmp0(config->type, "wifi") == 0 && type == CONNMAN_SERVICE_TYPE_WIFI) { - provision_service_wifi(key, config, service, network, + provision_service_wifi(config, service, network, ssid, ssid_len); } else - __connman_service_connect(service); + __connman_service_connect(service, + CONNMAN_SERVICE_CONNECT_REASON_AUTO); __connman_service_mark_dirty(); - __connman_service_save(service); + __connman_service_load_modifiable(service); - if (config->virtual == TRUE) { + if (config->virtual) { struct connect_virtual *virtual; virtual = g_malloc0(sizeof(struct connect_virtual)); @@ -1208,34 +1261,47 @@ static void provision_service(gpointer key, gpointer value, g_timeout_add(0, remove_virtual_config, virtual); } else - __connman_service_auto_connect(); + __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO); + + return 0; +} + +static int find_and_provision_service(struct connman_service *service) +{ + GHashTableIter iter, iter_service; + gpointer value, key, value_service, key_service; + + g_hash_table_iter_init(&iter, config_table); + + while (g_hash_table_iter_next(&iter, &key, &value)) { + struct connman_config *config = value; + + g_hash_table_iter_init(&iter_service, config->service_table); + while (g_hash_table_iter_next(&iter_service, &key_service, + &value_service)) { + if (!try_provision_service(value_service, service)) + return 0; + } + } + + return -ENOENT; } int __connman_config_provision_service(struct connman_service *service) { enum connman_service_type type; - GHashTableIter iter; - gpointer value, key; - /* For now only WiFi and Ethernet services are supported */ + /* For now only WiFi, Gadget and Ethernet services are supported */ type = connman_service_get_type(service); DBG("service %p type %d", service, type); if (type != CONNMAN_SERVICE_TYPE_WIFI && - type != CONNMAN_SERVICE_TYPE_ETHERNET) + type != CONNMAN_SERVICE_TYPE_ETHERNET && + type != CONNMAN_SERVICE_TYPE_GADGET) return -ENOSYS; - g_hash_table_iter_init(&iter, config_table); - - while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) { - struct connman_config *config = value; - - g_hash_table_foreach(config->service_table, - provision_service, service); - } - - return 0; + return find_and_provision_service(service); } int __connman_config_provision_service_ident(struct connman_service *service, @@ -1245,20 +1311,21 @@ int __connman_config_provision_service_ident(struct connman_service *service, struct connman_config *config; int ret = 0; - /* For now only WiFi and Ethernet services are supported */ + /* For now only WiFi, Gadget and Ethernet services are supported */ type = connman_service_get_type(service); DBG("service %p type %d", service, type); if (type != CONNMAN_SERVICE_TYPE_WIFI && - type != CONNMAN_SERVICE_TYPE_ETHERNET) + type != CONNMAN_SERVICE_TYPE_ETHERNET && + type != CONNMAN_SERVICE_TYPE_GADGET) return -ENOSYS; config = g_hash_table_lookup(config_table, ident); - if (config != NULL) { + if (config) { GHashTableIter iter; gpointer value, key; - gboolean found = FALSE; + bool found = false; g_hash_table_iter_init(&iter, config->service_table); @@ -1266,9 +1333,9 @@ int __connman_config_provision_service_ident(struct connman_service *service, * Check if we need to remove individual service if it * is missing from config file. */ - if (file != NULL && entry != NULL) { + if (file && entry) { while (g_hash_table_iter_next(&iter, &key, - &value) == TRUE) { + &value)) { struct connman_config_service *config_service; config_service = value; @@ -1281,14 +1348,14 @@ int __connman_config_provision_service_ident(struct connman_service *service, entry) != 0) continue; - found = TRUE; + found = true; break; } DBG("found %d ident %s file %s entry %s", found, ident, file, entry); - if (found == FALSE) { + if (!found) { /* * The entry+8 will skip "service_" prefix */ @@ -1298,8 +1365,7 @@ int __connman_config_provision_service_ident(struct connman_service *service, } } - g_hash_table_foreach(config->service_table, - provision_service, service); + find_and_provision_service(service); } return ret; @@ -1337,16 +1403,16 @@ int connman_config_provision_mutable_service(GKeyFile *keyfile) vfile = g_strdup_printf("service_mutable_%s.config", rstr); config = create_config(vfile); - if (config == NULL) + if (!config) return -ENOMEM; - if (load_service_from_keyfile(keyfile, config) == FALSE) + if (!load_service_from_keyfile(keyfile, config)) goto error; group = g_key_file_get_start_group(keyfile); service_config = g_hash_table_lookup(config->service_table, group+8); - if (service_config == NULL) + if (!service_config) goto error; /* Specific to non file based config: */ @@ -1355,7 +1421,7 @@ int connman_config_provision_mutable_service(GKeyFile *keyfile) g_free(service_config->config_entry); service_config->config_entry = NULL; - service_config->virtual = TRUE; + service_config->virtual = true; service_config->virtual_file = vfile; __connman_service_provision_changed(vfile); @@ -1381,35 +1447,35 @@ struct connman_config_entry **connman_config_get_entries(const char *type) int i = 0, count; g_hash_table_iter_init(&iter_file, config_table); - while (g_hash_table_iter_next(&iter_file, &key, &value) == TRUE) { + while (g_hash_table_iter_next(&iter_file, &key, &value)) { struct connman_config *config_file = value; count = g_hash_table_size(config_file->service_table); entries = g_try_realloc(entries, (i + count + 1) * sizeof(struct connman_config_entry *)); - if (entries == NULL) + if (!entries) return NULL; g_hash_table_iter_init(&iter_config, config_file->service_table); while (g_hash_table_iter_next(&iter_config, &key, - &value) == TRUE) { + &value)) { struct connman_config_service *config = value; - if (type != NULL && + if (type && g_strcmp0(config->type, type) != 0) continue; entries[i] = g_try_new0(struct connman_config_entry, 1); - if (entries[i] == NULL) + if (!entries[i]) goto cleanup; entries[i]->ident = g_strdup(config->ident); entries[i]->name = g_strdup(config->name); entries[i]->ssid = g_try_malloc0(config->ssid_len + 1); - if (entries[i]->ssid == NULL) + if (!entries[i]->ssid) goto cleanup; memcpy(entries[i]->ssid, config->ssid, @@ -1421,10 +1487,10 @@ struct connman_config_entry **connman_config_get_entries(const char *type) } } - if (entries != NULL) { + if (entries) { entries = g_try_realloc(entries, (i + 1) * sizeof(struct connman_config_entry *)); - if (entries == NULL) + if (!entries) return NULL; entries[i] = NULL; @@ -1443,7 +1509,7 @@ void connman_config_free_entries(struct connman_config_entry **entries) { int i; - if (entries == NULL) + if (!entries) return; for (i = 0; entries[i]; i++) { @@ -1456,3 +1522,31 @@ void connman_config_free_entries(struct connman_config_entry **entries) g_free(entries); return; } + +bool __connman_config_address_provisioned(const char *address, + const char *netmask) +{ + GHashTableIter iter, siter; + gpointer value, key, svalue, skey; + + if (!address || !netmask) + return false; + + g_hash_table_iter_init(&iter, config_table); + + while (g_hash_table_iter_next(&iter, &key, &value)) { + struct connman_config *config = value; + + g_hash_table_iter_init(&siter, config->service_table); + while (g_hash_table_iter_next(&siter, &skey, &svalue)) { + struct connman_config_service *service = svalue; + + if (!g_strcmp0(address, service->ipv4_address) && + !g_strcmp0(netmask, + service->ipv4_netmask)) + return true; + } + } + + return false; +} |