eiwd

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

network.c (5001B)


      1 /*
      2  *
      3  *  Ethernet 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 #define _GNU_SOURCE
     28 #include <errno.h>
     29 #include <dirent.h>
     30 #include <sys/types.h>
     31 #include <sys/stat.h>
     32 #include <ell/ell.h>
     33 
     34 #include "src/module.h"
     35 #include "wired/network.h"
     36 
     37 #define STORAGEFILE_SUFFIX	".8021x"
     38 #define STORAGEFILE_SUFFIX_LEN	6
     39 
     40 struct network {
     41 	char *name;
     42 };
     43 
     44 static struct l_queue *network_list;
     45 static struct l_dir_watch *storage_watch;
     46 static char *storage_path;
     47 
     48 static char *network_name_from_filename(const char *filename)
     49 {
     50 	if (!l_str_has_suffix(filename, STORAGEFILE_SUFFIX))
     51 		return NULL;
     52 
     53 	return l_strndup(filename, strlen(filename) - STORAGEFILE_SUFFIX_LEN);
     54 }
     55 
     56 static struct network *network_new(const char *name)
     57 {
     58 	struct network *net;
     59 
     60 	l_debug("Creating network '%s'", name);
     61 
     62 	net = l_new(struct network, 1);
     63 	net->name = l_strdup(name);
     64 
     65 	return net;
     66 }
     67 
     68 static void network_free(void *data)
     69 {
     70 	struct network *net = data;
     71 
     72 	l_debug("Freeing network '%s'", net->name);
     73 
     74 	l_free(net->name);
     75 	l_free(net);
     76 }
     77 
     78 static bool network_match(const void *a, const void *b)
     79 {
     80 	const struct network *net = a;
     81 	const char *name = b;
     82 
     83 	return !strcmp(net->name, name);
     84 }
     85 
     86 static struct network *network_lookup(const char *name)
     87 {
     88 	return l_queue_find(network_list, network_match, name);
     89 }
     90 
     91 static void network_create(const char *name)
     92 {
     93 	struct network *net;
     94 
     95 	net = network_lookup(name);
     96 	if (net) {
     97 		l_debug("Refresh network '%s'", net->name);
     98 		return;
     99 	}
    100 
    101 	net = network_new(name);
    102 	l_queue_push_tail(network_list, net);
    103 }
    104 
    105 static void network_remove(const char *name)
    106 {
    107 	struct network *net;
    108 
    109 	net = l_queue_remove_if(network_list, network_match, name);
    110 	if (net)
    111 		network_free(net);
    112 }
    113 
    114 static void network_reload(const char *name)
    115 {
    116 	struct network *net;
    117 
    118 	net = network_lookup(name);
    119 	if (net) {
    120 		l_debug("Refresh network '%s'", net->name);
    121 		return;
    122 	}
    123 }
    124 
    125 struct l_settings *network_lookup_security(const char *network)
    126 {
    127 	struct l_settings *conf;
    128 	char *path;
    129 
    130 	path = l_strdup_printf("%s/%s%s", storage_path, network,
    131 							STORAGEFILE_SUFFIX);
    132 
    133 	l_debug("Loading %s", path);
    134 
    135 	conf = l_settings_new();
    136 	if (!l_settings_load_from_file(conf, path)) {
    137 		l_settings_free(conf);
    138 		conf = NULL;
    139 	}
    140 
    141 	l_free(path);
    142 
    143 	return conf;
    144 }
    145 
    146 static void network_storage_watch_cb(const char *filename,
    147 					enum l_dir_watch_event event,
    148 					void *user_data)
    149 {
    150 	char *name;
    151 
    152 	name = network_name_from_filename(filename);
    153 	if (!name)
    154 		return;
    155 
    156 	switch (event) {
    157 	case L_DIR_WATCH_EVENT_CREATED:
    158 		network_create(name);
    159 		break;
    160 	case L_DIR_WATCH_EVENT_REMOVED:
    161 		network_remove(name);
    162 		break;
    163 	case L_DIR_WATCH_EVENT_MODIFIED:
    164 		network_reload(name);
    165 		break;
    166 	case L_DIR_WATCH_EVENT_ACCESSED:
    167 	case L_DIR_WATCH_EVENT_ATTRIB:
    168 		break;
    169 	}
    170 
    171 	l_free(name);
    172 }
    173 
    174 static void network_storage_watch_destroy(void *user_data)
    175 {
    176 	storage_watch = NULL;
    177 }
    178 
    179 static int network_init(void)
    180 {
    181 	DIR *dir;
    182 	struct dirent *dirent;
    183 	const char *state_dir;
    184 	char **state_dirs;
    185 
    186 	state_dir = getenv("STATE_DIRECTORY");
    187 	if (!state_dir)
    188 		state_dir = WIRED_STORAGEDIR;
    189 
    190 	l_debug("Using state directory %s", state_dir);
    191 
    192 	state_dirs = l_strsplit(state_dir, ':');
    193 	if (!state_dirs[0]) {
    194 		l_strv_free(state_dirs);
    195 		return -EINVAL;
    196 	}
    197 
    198 	storage_path = l_strdup(state_dirs[0]);
    199 	l_strv_free(state_dirs);
    200 
    201 	dir = opendir(storage_path);
    202 	if (!dir) {
    203 		l_info("Unable to open %s: %s", storage_path, strerror(errno));
    204 		return -EINVAL;
    205 	}
    206 
    207 	network_list = l_queue_new();
    208 
    209 	while ((dirent = readdir(dir))) {
    210 		struct network *net;
    211 		char *name;
    212 
    213 		if (dirent->d_type != DT_REG && dirent->d_type != DT_LNK)
    214 			continue;
    215 
    216 		name = network_name_from_filename(dirent->d_name);
    217 		if (!name)
    218 			continue;
    219 
    220 		net = network_new(name);
    221 		l_queue_push_tail(network_list, net);
    222 
    223 		l_free(name);
    224 	}
    225 
    226 	closedir(dir);
    227 
    228 	storage_watch = l_dir_watch_new(storage_path,
    229 					network_storage_watch_cb, NULL,
    230 					network_storage_watch_destroy);
    231 
    232 	return 0;
    233 }
    234 
    235 static void network_exit(void)
    236 {
    237 	l_dir_watch_destroy(storage_watch);
    238 
    239 	l_queue_destroy(network_list, network_free);
    240 	network_list = NULL;
    241 
    242 	l_free(storage_path);
    243 }
    244 
    245 IWD_MODULE(network, network_init, network_exit);
    246 IWD_MODULE_DEPENDS(network, eap);