From 9cb2f0c0fa4878e98182b07910172475d2355d92 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 4 Feb 2018 18:29:38 +0000 Subject: [PATCH] libnetwork: Add objects for 802.11 PHYs Signed-off-by: Michael Tremer --- Makefile.am | 6 +- src/libnetwork/libnetwork.sym | 3 + src/libnetwork/network/phy.h | 33 ++++++++++ src/libnetwork/phy.c | 109 ++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 src/libnetwork/network/phy.h create mode 100644 src/libnetwork/phy.c diff --git a/Makefile.am b/Makefile.am index fd13ea6..f3157a7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -247,7 +247,8 @@ LIBNETWORK_AGE=0 pkginclude_HEADERS = \ src/libnetwork/network/interface.h \ src/libnetwork/network/libnetwork.h \ - src/libnetwork/network/logging.h + src/libnetwork/network/logging.h \ + src/libnetwork/network/phy.h lib_LTLIBRARIES = \ src/libnetwork.la @@ -255,7 +256,8 @@ lib_LTLIBRARIES = \ src_libnetwork_la_SOURCES = \ src/libnetwork/interface.c \ src/libnetwork/libnetwork-private.h \ - src/libnetwork/libnetwork.c + src/libnetwork/libnetwork.c \ + src/libnetwork/phy.c src_libnetwork_la_LIBADD = \ $(LIBNL_LIBS) diff --git a/src/libnetwork/libnetwork.sym b/src/libnetwork/libnetwork.sym index 5742649..89ed303 100644 --- a/src/libnetwork/libnetwork.sym +++ b/src/libnetwork/libnetwork.sym @@ -5,6 +5,9 @@ global: network_interface_ref; network_interface_unref; network_new; + network_phy_new; + network_phy_ref; + network_phy_unref; network_ref; network_unref; network_version; diff --git a/src/libnetwork/network/phy.h b/src/libnetwork/network/phy.h new file mode 100644 index 0000000..7adb259 --- /dev/null +++ b/src/libnetwork/network/phy.h @@ -0,0 +1,33 @@ +/*############################################################################# +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2018 IPFire Network Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef NETWORK_PHY_H +#define NETWORK_PHY_H + +#include + +struct network_phy; + +int network_phy_new(struct network_ctx*, struct network_phy** phy, const char* name); + +struct network_phy* network_phy_ref(struct network_phy* phy); +struct network_phy* network_phy_unref(struct network_phy* phy); + +#endif /* NETWORK_PHY_H */ diff --git a/src/libnetwork/phy.c b/src/libnetwork/phy.c new file mode 100644 index 0000000..9e364de --- /dev/null +++ b/src/libnetwork/phy.c @@ -0,0 +1,109 @@ +/*############################################################################# +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2018 IPFire Network Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include +#include + +#include +#include +#include "libnetwork-private.h" + +struct network_phy { + struct network_ctx* ctx; + int refcount; + + int index; + char* name; +}; + +static int phy_get_index(const char* name) { + char path[1024]; + char index[8]; + + snprintf(path, sizeof(path), "/sys/class/ieee80211/%s/index", name); + + FILE* f = fopen(path, "r"); + if (!f) + return -1; + + int p = fread(index, 1, sizeof(index), f); + if (p < 0) { + fclose(f); + return -1; + } + + // Terminate buffer + index[p] = '\0'; + fclose(f); + + return atoi(index); +} + +NETWORK_EXPORT int network_phy_new(struct network_ctx* ctx, struct network_phy** phy, + const char* name) { + if (!name) + return -EINVAL; + + int index = phy_get_index(name); + if (index < 0) + return -ENODEV; + + struct network_phy* p = calloc(1, sizeof(*p)); + if (!p) + return -ENOMEM; + + // Initalise object + p->ctx = network_ref(ctx); + p->refcount = 1; + + DEBUG(p->ctx, "Allocated phy at %p\n", p); + *phy = p; + return 0; +} + +NETWORK_EXPORT struct network_phy* network_phy_ref(struct network_phy* phy) { + if (!phy) + return NULL; + + phy->refcount++; + return phy; +} + +static void network_phy_free(struct network_phy* phy) { + DEBUG(phy->ctx, "Releasing phy at %p\n", phy); + + if (phy->name) + free(phy->name); + + network_unref(phy->ctx); + free(phy); +} + +NETWORK_EXPORT struct network_phy* network_phy_unref(struct network_phy* phy) { + if (!phy) + return NULL; + + if (--phy->refcount > 0) + return phy; + + network_phy_free(phy); + return NULL; +} -- 2.47.3