From: Michael Tremer Date: Fri, 10 Feb 2023 16:15:46 +0000 (+0000) Subject: networkd: Add scaffolding for ports X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=abeab06924b22765dbc410c563f27077f348b5e9;p=network.git networkd: Add scaffolding for ports Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 024bf457..4bd39753 100644 --- a/Makefile.am +++ b/Makefile.am @@ -324,6 +324,8 @@ dist_networkd_SOURCES = \ src/networkd/links.h \ src/networkd/logging.h \ src/networkd/main.c \ + src/networkd/port.c \ + src/networkd/port.h \ src/networkd/string.h \ src/networkd/zones.c \ src/networkd/zones.h \ diff --git a/src/networkd/port.c b/src/networkd/port.c new file mode 100644 index 00000000..019521f6 --- /dev/null +++ b/src/networkd/port.c @@ -0,0 +1,84 @@ +/*############################################################################# +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2023 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 "config.h" +#include "string.h" +#include "port.h" + +struct nw_port { + int nrefs; + + char name[IF_NAMESIZE]; + + // Configuration + struct nw_config *config; +}; + +static void nw_port_free(struct nw_port* port) { + if (port->config) + nw_config_unref(port->config); + + free(port); +} + +int nw_port_create(struct nw_port** port, const char* name) { + int r; + + // Allocate a new object + struct nw_port* p = calloc(1, sizeof(*p)); + if (!p) + return 1; + + // Initialize reference counter + p->nrefs = 1; + + // Store the name + r = nw_string_set(p->name, name); + if (r) + goto ERROR; + + *port = p; + return 0; + +ERROR: + nw_port_free(p); + return r; +} + +struct nw_port* nw_port_ref(struct nw_port* port) { + port->nrefs++; + + return port; +} + +struct nw_port* nw_port_unref(struct nw_port* port) { + if (--port->nrefs > 0) + return port; + + nw_port_free(port); + return NULL; +} + +const char* nw_port_name(struct nw_port* port) { + return port->name; +} diff --git a/src/networkd/port.h b/src/networkd/port.h new file mode 100644 index 00000000..cea62037 --- /dev/null +++ b/src/networkd/port.h @@ -0,0 +1,33 @@ +/*############################################################################# +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2023 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 NETWORKD_PORT_H +#define NETWORKD_PORT_H + +struct nw_port; + +int nw_port_create(struct nw_port** port, const char* name); + +struct nw_port* nw_port_ref(struct nw_port* port); +struct nw_port* nw_port_unref(struct nw_port* port); + +const char* nw_port_name(struct nw_port* port); + +#endif /* NETWORKD_PORT_H */