eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

network.c (7988B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2017-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <ell/ell.h>
     28 
     29 #include "client/dbus-proxy.h"
     30 #include "client/display.h"
     31 #include "client/network.h"
     32 
     33 struct network {
     34 	bool connected;
     35 	char *identity;
     36 	char *name;
     37 	char *type;
     38 	struct l_queue *bss_list;
     39 	const struct proxy_interface *device;
     40 };
     41 
     42 static void check_errors_method_callback(struct l_dbus_message *message,
     43 								void *user_data)
     44 {
     45 	dbus_message_has_error(message);
     46 }
     47 
     48 const struct proxy_interface *network_get_proxy(const char *path)
     49 {
     50 	return proxy_interface_find(IWD_NETWORK_INTERFACE, path);
     51 }
     52 
     53 bool network_is_connected(const struct proxy_interface *network_proxy)
     54 {
     55 	const struct network *network = proxy_interface_get_data(network_proxy);
     56 
     57 	return network->connected;
     58 }
     59 
     60 const char *network_get_type(const struct proxy_interface *network_proxy)
     61 {
     62 	const struct network *network = proxy_interface_get_data(network_proxy);
     63 
     64 	return network->type;
     65 }
     66 
     67 const char *network_get_name(const struct proxy_interface *network_proxy)
     68 {
     69 	const struct network *network = proxy_interface_get_data(network_proxy);
     70 
     71 	return network->name;
     72 }
     73 
     74 void network_connect(const struct proxy_interface *proxy)
     75 {
     76 	if (!proxy)
     77 		return;
     78 
     79 	proxy_interface_method_call(proxy, "Connect", "",
     80 						check_errors_method_callback);
     81 }
     82 
     83 static const char *get_name(const void *data)
     84 {
     85 	const struct network *network = data;
     86 
     87 	return network->name;
     88 }
     89 
     90 static void update_name(void *data, struct l_dbus_message_iter *variant)
     91 {
     92 	struct network *network = data;
     93 	const char *value;
     94 
     95 	l_free(network->name);
     96 
     97 	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
     98 		network->name = NULL;
     99 
    100 		return;
    101 	}
    102 
    103 	network->name = l_strdup(value);
    104 }
    105 
    106 static void update_connected(void *data, struct l_dbus_message_iter *variant)
    107 {
    108 	struct network *network = data;
    109 	bool value;
    110 
    111 	if (!l_dbus_message_iter_get_variant(variant, "b", &value)) {
    112 		network->connected = false;
    113 
    114 		return;
    115 	}
    116 
    117 	network->connected = value;
    118 }
    119 
    120 static void update_device(void *data, struct l_dbus_message_iter *variant)
    121 {
    122 	struct network *network = data;
    123 	const char *path;
    124 
    125 	if (!l_dbus_message_iter_get_variant(variant, "o", &path)) {
    126 		network->device = NULL;
    127 
    128 		return;
    129 	}
    130 
    131 	network->device = proxy_interface_find(IWD_DEVICE_INTERFACE, path);
    132 }
    133 
    134 static void update_type(void *data, struct l_dbus_message_iter *variant)
    135 {
    136 	struct network *network = data;
    137 	const char *value;
    138 
    139 	l_free(network->type);
    140 
    141 	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
    142 		network->type = NULL;
    143 
    144 		return;
    145 	}
    146 
    147 	network->type = l_strdup(value);
    148 }
    149 
    150 static bool match_path(const void *a, const void *user_data)
    151 {
    152 	const char *path1 = a;
    153 	const char *path2 = user_data;
    154 
    155 	return !strcmp(path1, path2);
    156 }
    157 
    158 static void update_ess(void *data, struct l_dbus_message_iter *variant)
    159 {
    160 	struct network *network = data;
    161 	struct l_dbus_message_iter array;
    162 	const char *path;
    163 
    164 	if (!network->bss_list)
    165 		network->bss_list = l_queue_new();
    166 
    167 	if (!l_dbus_message_iter_get_variant(variant, "ao", &array))
    168 		return;
    169 
    170 	while (l_dbus_message_iter_next_entry(&array, &path)) {
    171 		l_free(l_queue_remove_if(network->bss_list, match_path, path));
    172 		l_queue_push_head(network->bss_list, l_strdup(path));
    173 	}
    174 }
    175 
    176 static const char *get_ess(const void *data)
    177 {
    178 	const struct network *network = data;
    179 	static char count[10];
    180 
    181 	snprintf(count, 10, "Count %u", l_queue_length(network->bss_list));
    182 
    183 	return count;
    184 }
    185 
    186 struct l_queue *network_get_bss_list(
    187 				const struct proxy_interface *network_proxy)
    188 {
    189 	const struct network *network = proxy_interface_get_data(network_proxy);
    190 	if (!network)
    191 		return NULL;
    192 
    193 	return network->bss_list;
    194 }
    195 
    196 static const struct proxy_interface_property network_properties[] = {
    197 	{ "Name",       "s", update_name, get_name },
    198 	{ "Connected",  "b", update_connected},
    199 	{ "Device",     "o", update_device},
    200 	{ "Type",       "s", update_type},
    201 	{ "ExtendedServiceSet",  "ao", update_ess, get_ess },
    202 	{ }
    203 };
    204 
    205 static const char *network_identity(void *data)
    206 {
    207 	struct network *network = data;
    208 
    209 	if (!network->identity)
    210 		network->identity =
    211 			l_strdup_printf("%s %s", network->name, network->type);
    212 
    213 	return network->identity;
    214 }
    215 
    216 static void network_display_inline(const char *margin, const void *data)
    217 {
    218 	const struct network *network = data;
    219 
    220 	display("%s%s %s %s\n", margin, network->name ? network->name : "",
    221 			network->type ? network->type : "",
    222 			network->connected ? "connected" : "disconnected");
    223 }
    224 
    225 static void *network_create(void)
    226 {
    227 	return l_new(struct network, 1);
    228 }
    229 
    230 static void network_destroy(void *data)
    231 {
    232 	struct network *network = data;
    233 
    234 	l_free(network->name);
    235 	l_free(network->type);
    236 	l_free(network->identity);
    237 	l_queue_destroy(network->bss_list, l_free);
    238 
    239 	network->device = NULL;
    240 
    241 	l_free(network);
    242 }
    243 
    244 static const struct proxy_interface_type_ops ops = {
    245 	.create = network_create,
    246 	.destroy = network_destroy,
    247 	.display = network_display_inline,
    248 	.identity = network_identity,
    249 };
    250 
    251 static struct proxy_interface_type network_interface_type = {
    252 	.interface = IWD_NETWORK_INTERFACE,
    253 	.properties = network_properties,
    254 	.ops = &ops,
    255 };
    256 
    257 struct completion_search_parameters {
    258 	const char *text;
    259 	const struct proxy_interface *device;
    260 };
    261 
    262 static bool match_by_partial_name(const void *a, const void *b)
    263 {
    264 	const struct network *network = a;
    265 	const struct completion_search_parameters *params = b;
    266 	const char *name;
    267 	const char *text;
    268 
    269 	if (!proxy_interface_is_same(network->device, params->device))
    270 		return false;
    271 
    272 	for (text = params->text, name = network->name; *text && *name;
    273 							name++, text++) {
    274 		if (*name == *text)
    275 			continue;
    276 
    277 		return false;
    278 	}
    279 
    280 	if (*text)
    281 		return false;
    282 
    283 	return true;
    284 }
    285 
    286 char *network_name_completion(const struct proxy_interface *device,
    287 						const char *text, int state)
    288 {
    289 	const struct completion_search_parameters params = {
    290 		.text = text, .device = device,
    291 	};
    292 
    293 	return proxy_property_str_completion(&network_interface_type,
    294 						match_by_partial_name, "Name",
    295 						&params, state, NULL);
    296 }
    297 
    298 struct network_search_parameters {
    299 	const struct network_args *args;
    300 	const struct proxy_interface *device;
    301 };
    302 
    303 static bool match_by_device_and_args(const void *a, const void *b)
    304 {
    305 	const struct network *network = a;
    306 	const struct network_search_parameters *params = b;
    307 
    308 	if (!proxy_interface_is_same(network->device, params->device))
    309 		return false;
    310 
    311 	if (strcmp(network->name, params->args->name))
    312 		return false;
    313 
    314 	if (params->args->type && strcmp(network->type, params->args->type))
    315 		return false;
    316 
    317 	return true;
    318 }
    319 
    320 struct l_queue *network_match_by_device_and_args(
    321 					const struct proxy_interface *device,
    322 					const struct network_args *args)
    323 {
    324 	struct network_search_parameters params = {
    325 		.args = args, .device = device
    326 	};
    327 
    328 	return proxy_interface_find_all(network_interface_type.interface,
    329 					match_by_device_and_args, &params);
    330 }
    331 
    332 static int network_interface_init(void)
    333 {
    334 	proxy_interface_type_register(&network_interface_type);
    335 
    336 	return 0;
    337 }
    338 
    339 static void network_interface_exit(void)
    340 {
    341 	proxy_interface_type_unregister(&network_interface_type);
    342 }
    343 
    344 INTERFACE_TYPE(network_interface_type, network_interface_init,
    345 						network_interface_exit)