2017-04-21 20:14:51 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-26 03:13:57 +02:00
|
|
|
#define _XOPEN_SOURCE 700
|
|
|
|
|
|
|
|
#include <time.h>
|
2017-04-21 20:14:51 +02:00
|
|
|
#include <ell/ell.h>
|
|
|
|
|
|
|
|
#include "command.h"
|
|
|
|
#include "dbus-proxy.h"
|
2017-04-26 03:13:57 +02:00
|
|
|
#include "display.h"
|
2018-03-28 18:26:34 +02:00
|
|
|
#include "network.h"
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2017-04-21 20:14:55 +02:00
|
|
|
struct known_network {
|
2018-07-31 16:37:10 +02:00
|
|
|
char *identity;
|
2017-04-21 20:14:55 +02:00
|
|
|
char *name;
|
|
|
|
char *type;
|
|
|
|
char *last_connected;
|
|
|
|
};
|
|
|
|
|
2017-04-26 03:13:57 +02:00
|
|
|
static const char *format_iso8601(const char *time_str, const char *format)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
time_t time;
|
|
|
|
static char buf[255];
|
|
|
|
|
|
|
|
if (!time_str)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(&tm, 0, sizeof(struct tm));
|
|
|
|
|
|
|
|
strptime(time_str, "%FT%TZ", &tm);
|
|
|
|
|
|
|
|
time = mktime(&tm);
|
|
|
|
|
|
|
|
strftime(buf, sizeof(buf), format, localtime(&time));
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-07-25 01:17:18 +02:00
|
|
|
static void update_name(void *data, struct l_dbus_message_iter *variant)
|
2017-04-26 03:13:57 +02:00
|
|
|
{
|
|
|
|
struct known_network *network = data;
|
|
|
|
const char *value;
|
|
|
|
|
2018-07-31 16:37:11 +02:00
|
|
|
l_free(network->name);
|
|
|
|
|
2017-04-26 03:13:57 +02:00
|
|
|
if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
|
|
|
|
network->name = NULL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
network->name = l_strdup(value);
|
|
|
|
}
|
|
|
|
|
2018-07-25 01:17:18 +02:00
|
|
|
static void update_type(void *data, struct l_dbus_message_iter *variant)
|
2017-04-26 03:13:57 +02:00
|
|
|
{
|
|
|
|
struct known_network *network = data;
|
|
|
|
const char *value;
|
|
|
|
|
2018-07-31 16:37:11 +02:00
|
|
|
l_free(network->type);
|
|
|
|
|
2017-04-26 03:13:57 +02:00
|
|
|
if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
|
|
|
|
network->type = NULL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
network->type = l_strdup(value);
|
|
|
|
}
|
|
|
|
|
2018-07-25 01:17:18 +02:00
|
|
|
static void update_last_connected(void *data, struct l_dbus_message_iter *variant)
|
2017-04-26 03:13:57 +02:00
|
|
|
{
|
|
|
|
struct known_network *network = data;
|
|
|
|
const char *value;
|
|
|
|
|
2018-07-31 16:37:11 +02:00
|
|
|
l_free(network->last_connected);
|
|
|
|
|
2017-04-26 03:13:57 +02:00
|
|
|
if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
|
|
|
|
network->last_connected = NULL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
network->last_connected = l_strdup(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct proxy_interface_property known_network_properties[] = {
|
2018-07-25 01:17:18 +02:00
|
|
|
{ "Name", "s", update_name },
|
|
|
|
{ "Type", "s", update_type },
|
|
|
|
{ "LastConnectedTime", "s", update_last_connected },
|
2017-04-26 03:13:57 +02:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
static void *known_network_create(void)
|
2017-04-26 03:13:57 +02:00
|
|
|
{
|
2018-07-31 16:37:10 +02:00
|
|
|
return l_new(struct known_network, 1);
|
|
|
|
}
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
static void known_network_destroy(void *data)
|
|
|
|
{
|
|
|
|
struct known_network *network = data;
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
l_free(network->last_connected);
|
|
|
|
l_free(network->name);
|
|
|
|
l_free(network->type);
|
|
|
|
l_free(network->identity);
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
l_free(network);
|
2017-04-26 03:13:57 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
static void known_network_display_inline(const char *margin, const void *data)
|
2017-04-26 03:13:57 +02:00
|
|
|
{
|
2018-07-31 16:37:10 +02:00
|
|
|
const struct known_network *network = data;
|
|
|
|
char *last_connected =
|
|
|
|
l_strdup(format_iso8601(network->last_connected,
|
|
|
|
"%b %e, %l:%M %p"));
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
display("%s%-*s%-*s%-*s\n",
|
|
|
|
margin, 32, network->name, 11, network->type,
|
|
|
|
19, last_connected ? : "-");
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
l_free(last_connected);
|
|
|
|
}
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
static const char *known_network_identity(void *data)
|
|
|
|
{
|
|
|
|
struct known_network *network = data;
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
if (!network->identity)
|
|
|
|
network->identity =
|
|
|
|
l_strdup_printf("%s %s", network->name, network->type);
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
return network->identity;
|
|
|
|
}
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
static const struct proxy_interface_type_ops known_network_ops = {
|
|
|
|
.create = known_network_create,
|
|
|
|
.destroy = known_network_destroy,
|
|
|
|
.display = known_network_display_inline,
|
|
|
|
.identity = known_network_identity,
|
|
|
|
};
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
static struct proxy_interface_type known_network_interface_type = {
|
|
|
|
.interface = IWD_KNOWN_NETWORK_INTREFACE,
|
|
|
|
.properties = known_network_properties,
|
|
|
|
.ops = &known_network_ops,
|
|
|
|
};
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
static bool known_network_match(const void *a, const void *b)
|
2017-04-21 20:14:55 +02:00
|
|
|
{
|
2018-07-31 16:37:10 +02:00
|
|
|
const struct known_network *network = a;
|
|
|
|
const struct network_args *args = b;
|
2017-04-21 20:14:55 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
if (strcmp(network->name, args->name))
|
|
|
|
return false;
|
2017-04-21 20:14:55 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
if (args->type && strcmp(network->type, args->type))
|
|
|
|
return false;
|
2017-04-21 20:14:55 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-04-21 20:14:55 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
static void check_errors_method_callback(struct l_dbus_message *message,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
dbus_message_has_error(message);
|
|
|
|
}
|
2017-04-21 20:14:51 +02:00
|
|
|
|
2018-07-27 21:41:30 +02:00
|
|
|
static enum cmd_status cmd_list(const char *entity, char **args, int argc)
|
2017-04-21 20:14:53 +02:00
|
|
|
{
|
2018-07-31 16:37:10 +02:00
|
|
|
display_table_header("Known Networks", MARGIN, "%-*s%-*s%-*s",
|
|
|
|
32, "Name", 11, "Security",
|
|
|
|
19, "Last connected");
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
proxy_interface_display_list(known_network_interface_type.interface);
|
2017-04-26 03:13:57 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
display_table_footer();
|
2017-04-26 03:13:58 +02:00
|
|
|
|
2018-08-03 23:35:37 +02:00
|
|
|
return CMD_STATUS_DONE;
|
2017-04-21 20:14:53 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:41:30 +02:00
|
|
|
static enum cmd_status cmd_forget(const char *entity, char **argv, int argc)
|
2017-04-21 20:14:53 +02:00
|
|
|
{
|
2018-07-31 16:37:10 +02:00
|
|
|
struct network_args network_args;
|
2017-04-26 03:13:59 +02:00
|
|
|
struct l_queue *match;
|
2018-07-31 16:37:10 +02:00
|
|
|
const struct proxy_interface *known_network_proxy;
|
2017-04-26 03:13:59 +02:00
|
|
|
|
2018-07-27 21:41:30 +02:00
|
|
|
if (argc < 1)
|
2017-04-26 03:13:59 +02:00
|
|
|
return CMD_STATUS_INVALID_ARGS;
|
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
network_args.name = argv[0];
|
|
|
|
network_args.type = argc >= 2 ? argv[1] : NULL;
|
2017-04-26 03:13:59 +02:00
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
match = proxy_interface_find_all(known_network_interface_type.interface,
|
|
|
|
known_network_match,
|
|
|
|
&network_args);
|
2017-04-26 03:13:59 +02:00
|
|
|
|
|
|
|
if (!match) {
|
2018-07-31 16:37:10 +02:00
|
|
|
display("No network with specified parameters was found\n");
|
2017-04-26 03:13:59 +02:00
|
|
|
return CMD_STATUS_INVALID_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l_queue_length(match) > 1) {
|
2018-07-31 16:37:10 +02:00
|
|
|
if (!network_args.type) {
|
2017-04-26 03:13:59 +02:00
|
|
|
display("Provided network name is ambiguous. "
|
|
|
|
"Please specify security type.\n");
|
|
|
|
}
|
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
l_queue_destroy(match, NULL);
|
|
|
|
return CMD_STATUS_INVALID_VALUE;
|
2017-04-26 03:13:59 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
known_network_proxy = l_queue_pop_head(match);
|
2017-04-26 03:13:59 +02:00
|
|
|
l_queue_destroy(match, NULL);
|
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
proxy_interface_method_call(known_network_proxy, "Forget", "",
|
|
|
|
check_errors_method_callback);
|
2017-04-26 03:13:59 +02:00
|
|
|
|
2018-08-03 23:35:37 +02:00
|
|
|
return CMD_STATUS_TRIGGERED;
|
2017-04-21 20:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct command known_networks_commands[] = {
|
|
|
|
{ NULL, "list", NULL, cmd_list, "List known networks", true },
|
2018-03-28 18:26:34 +02:00
|
|
|
{ NULL, "forget", "<\"network name\"> [security]",
|
2017-04-26 03:13:57 +02:00
|
|
|
cmd_forget, "Forget known network" },
|
2017-04-21 20:14:53 +02:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2017-04-21 20:14:54 +02:00
|
|
|
static char *family_arg_completion(const char *text, int state)
|
|
|
|
{
|
|
|
|
static size_t index;
|
|
|
|
static size_t len;
|
|
|
|
const char *cmd;
|
|
|
|
|
|
|
|
if (!state) {
|
|
|
|
index = 0;
|
|
|
|
len = strlen(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((cmd = known_networks_commands[index].cmd)) {
|
|
|
|
if (known_networks_commands[index++].entity)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strncmp(cmd, text, len))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return l_strdup(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-21 20:14:51 +02:00
|
|
|
static struct command_family known_networks_command_family = {
|
|
|
|
.caption = "Known Networks",
|
|
|
|
.name = "known-networks",
|
2017-04-21 20:14:53 +02:00
|
|
|
.command_list = known_networks_commands,
|
2017-04-21 20:14:54 +02:00
|
|
|
.family_arg_completion = family_arg_completion,
|
2017-04-21 20:14:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int known_networks_command_family_init(void)
|
|
|
|
{
|
|
|
|
command_family_register(&known_networks_command_family);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void known_networks_command_family_exit(void)
|
|
|
|
{
|
|
|
|
command_family_unregister(&known_networks_command_family);
|
|
|
|
}
|
|
|
|
|
|
|
|
COMMAND_FAMILY(known_networks_command_family,
|
|
|
|
known_networks_command_family_init,
|
|
|
|
known_networks_command_family_exit)
|
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
static int known_network_interface_init(void)
|
2017-04-21 20:14:51 +02:00
|
|
|
{
|
2018-07-31 16:37:10 +02:00
|
|
|
proxy_interface_type_register(&known_network_interface_type);
|
2017-04-21 20:14:51 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
static void known_network_interface_exit(void)
|
2017-04-21 20:14:51 +02:00
|
|
|
{
|
2018-07-31 16:37:10 +02:00
|
|
|
proxy_interface_type_unregister(&known_network_interface_type);
|
2017-04-21 20:14:51 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 16:37:10 +02:00
|
|
|
INTERFACE_TYPE(known_network_interface_type, known_network_interface_init,
|
|
|
|
known_network_interface_exit)
|