knownnetwork: track/optimize UUID for known networks

The UUID was being generated every time we synced which is wasteful.
Instead we can track the UUID inside network_info and only generate
it once when needed.

Two new network_info APIs were added:
network_info_set_uuid
network_info_get_uuid

The setter is used when the frequency file is loaded. If a valid UUID
is found in the frequency file this UUID is set and used.

network_info_get_uuid will not just get the UUID, but actually generate
it if one has not been set yet. This will allow other modules to
get/generate the UUID if one has no been loaded from the frequency
file.
This commit is contained in:
James Prestwood 2019-09-13 10:27:26 -07:00 committed by Denis Kenzior
parent 0f337ceb51
commit f57b73898b
3 changed files with 45 additions and 14 deletions

View File

@ -231,6 +231,36 @@ const struct iovec *network_info_get_extra_ies(const struct network_info *info,
return info->ops->get_extra_ies(info, bss, num_elems);
}
const uint8_t *network_info_get_uuid(struct network_info *info)
{
char *file_path;
/*
* 16 bytes of randomness. Since we only care about a unique value there
* is no need to use any special pre-defined namespace.
*/
static const uint8_t nsid[16] = {
0xfd, 0x88, 0x6f, 0x1e, 0xdf, 0x02, 0xd7, 0x8b,
0xc4, 0x90, 0x30, 0x59, 0x73, 0x8a, 0x86, 0x0d
};
if (info->has_uuid)
return info->uuid;
file_path = info->ops->get_file_path(info);
l_uuid_v5(nsid, file_path, strlen(file_path), info->uuid);
info->has_uuid = true;
return info->uuid;
}
void network_info_set_uuid(struct network_info *info, const uint8_t *uuid)
{
memcpy(info->uuid, uuid, 16);
info->has_uuid = true;
}
bool network_info_match_hessid(const struct network_info *info,
const uint8_t *hessid)
{
@ -786,6 +816,7 @@ static int known_network_frequencies_load(void)
char **groups;
struct l_queue *known_frequencies;
uint32_t i;
uint8_t uuid[16];
known_freqs = storage_known_frequencies_load();
if (!known_freqs) {
@ -816,7 +847,12 @@ static int known_network_frequencies_load(void)
if (!known_frequencies)
goto next;
if (!l_uuid_from_string(groups[i], uuid))
goto next;
network_info_set_uuid(info, uuid);
info->known_frequencies = known_frequencies;
next:
l_free(freq_list);
}
@ -829,20 +865,11 @@ next:
/*
* Syncs a single network_info frequency to the global frequency file
*/
void known_network_frequency_sync(const struct network_info *info)
void known_network_frequency_sync(struct network_info *info)
{
char *freq_list_str;
char *file_path;
char group[37];
uint8_t uuid[16];
/*
* 16 bytes of randomness. Since we only care about a unique value there
* is no need to use any special pre-defined namespace.
*/
static const uint8_t nsid[16] = {
0xfd, 0x88, 0x6f, 0x1e, 0xdf, 0x02, 0xd7, 0x8b,
0xc4, 0x90, 0x30, 0x59, 0x73, 0x8a, 0x86, 0x0d
};
if (!info->known_frequencies)
return;
@ -854,8 +881,7 @@ void known_network_frequency_sync(const struct network_info *info)
file_path = info->ops->get_file_path(info);
l_uuid_v5(nsid, file_path, strlen(file_path), uuid);
l_uuid_to_string(uuid, group, sizeof(group));
l_uuid_to_string(network_info_get_uuid(info), group, sizeof(group));
l_settings_set_value(known_freqs, group, "name", file_path);
l_settings_set_value(known_freqs, group, "list", freq_list_str);

View File

@ -61,9 +61,11 @@ struct network_info {
struct l_queue *known_frequencies;
uint64_t connected_time; /* Time last connected */
int seen_count; /* Ref count for network.info */
uint8_t uuid[16];
bool is_hidden:1;
bool is_autoconnectable:1;
bool is_hotspot:1;
bool has_uuid:1;
};
typedef bool (*known_networks_foreach_func_t)(const struct network_info *info,
@ -88,7 +90,7 @@ struct network_info *known_networks_find(const char *ssid,
struct scan_freq_set *known_networks_get_recent_frequencies(
uint8_t num_networks_tosearch);
int known_network_add_frequency(struct network_info *info, uint32_t frequency);
void known_network_frequency_sync(const struct network_info *info);
void known_network_frequency_sync(struct network_info *info);
uint32_t known_networks_watch_add(known_networks_watch_func_t func,
void *user_data,
@ -103,6 +105,9 @@ const char *network_info_get_type(const struct network_info *info);
const struct iovec *network_info_get_extra_ies(const struct network_info *info,
struct scan_bss *bss,
size_t *num_elems);
/* Gets the UUID, or generates one if not yet created */
const uint8_t *network_info_get_uuid(struct network_info *info);
void network_info_set_uuid(struct network_info *info, const uint8_t *uuid);
bool network_info_match_hessid(const struct network_info *info,
const uint8_t *hessid);

View File

@ -1427,7 +1427,7 @@ static void known_networks_changed(enum known_networks_event event,
station_foreach(match_known_network, (void *) info);
/* Syncs frequencies of newly known network */
known_network_frequency_sync(info);
known_network_frequency_sync((struct network_info *)info);
break;
case KNOWN_NETWORKS_EVENT_REMOVED:
station_foreach(disconnect_no_longer_known, (void *) info);