]> git.ipfire.org Git - people/jschlag/network.git/commitdiff
libnetwork: Add objects for 802.11 PHYs
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 4 Feb 2018 18:29:38 +0000 (18:29 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 4 Feb 2018 18:51:43 +0000 (18:51 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libnetwork/libnetwork.sym
src/libnetwork/network/phy.h [new file with mode: 0644]
src/libnetwork/phy.c [new file with mode: 0644]

index fd13ea6196754c58c37a582978642a62289979e4..f3157a772ad73aeb06694eba9ec30337e729c0d9 100644 (file)
@@ -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)
index 57426492effe4f93421e4e37d390fdd27e879930..89ed3034ec5295f1d94f106c5f5a06a8a2855730 100644 (file)
@@ -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 (file)
index 0000000..7adb259
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef NETWORK_PHY_H
+#define NETWORK_PHY_H
+
+#include <network/libnetwork.h>
+
+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 (file)
index 0000000..9e364de
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <network/libnetwork.h>
+#include <network/logging.h>
+#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;
+}