]> git.ipfire.org Git - network.git/commitdiff
ports: Implement a function the generally fetches the parent port
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 5 Jun 2023 16:47:14 +0000 (16:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 5 Jun 2023 16:47:14 +0000 (16:47 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/json.h [new file with mode: 0644]
src/networkd/port.c
src/networkd/port.h

diff --git a/src/networkd/json.h b/src/networkd/json.h
new file mode 100644 (file)
index 0000000..33e237a
--- /dev/null
@@ -0,0 +1,89 @@
+/*#############################################################################
+#                                                                             #
+# 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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef NETWORKD_JSON_H
+#define NETWORKD_JSON_H
+
+#include <errno.h>
+
+#include <json.h>
+
+// Give some sane names to the reference count functions
+#define json_object_ref json_object_get
+#define json_object_unref json_object_put
+
+static inline int __json_object_add(struct json_object* o,
+               const char* key, struct json_object* value) {
+       int r;
+
+       // Add the object
+       r = json_object_object_add(o, key, value);
+       if (r < 0) {
+               if (value)
+                       json_object_unref(value);
+       }
+
+       return r;
+}
+
+static inline int json_object_add_string(struct json_object* o,
+               const char* key, const char* value) {
+       struct json_object* element = NULL;
+
+       // Create a JSON object from the string
+       element = json_object_new_string(value);
+       if (!element)
+               return -errno;
+
+       return __json_object_add(o, key, element);
+}
+
+static inline int json_object_add_int64(struct json_object* o,
+               const char* key, const int64_t value) {
+       struct json_object* element = NULL;
+
+       // Create a JSON object
+       element = json_object_new_int64(value);
+       if (!element)
+               return -errno;
+
+       return __json_object_add(o, key, element);
+}
+
+static inline int json_to_string(struct json_object* o, char** s, size_t* l) {
+       // Format JSON to string
+       const char* buffer = json_object_to_json_string_ext(o,
+               JSON_C_TO_STRING_PRETTY|JSON_C_TO_STRING_PRETTY_TAB);
+       if (!buffer)
+               return -errno;
+
+       // Copy the string to the heap
+       *s = strdup(buffer);
+       if (!*s)
+               return -errno;
+
+       // If requested, store the length of the string
+       if (l)
+               *l = strlen(*s);
+
+       return 0;
+}
+
+#endif /* NETWORKD_JSON_H */
index b44cc8822fef7bfc0e68c076b994c2b0a45359fd..c9440d4f4300a7ad394a5be312c0ca8f786c8bb5 100644 (file)
@@ -441,23 +441,26 @@ static int nw_port_is_disabled(nw_port* port) {
        return nw_config_get_bool(port->config, "DISABLED");
 }
 
+nw_port* nw_port_get_parent_port(nw_port* port) {
+       if (!NW_PORT_OPS(port)->get_parent_port)
+               return NULL;
+
+       return NW_PORT_OPS(port)->get_parent_port(port);
+}
+
 static nw_link* nw_port_get_parent_link(nw_port* port) {
        nw_port* parent = NULL;
        nw_link* link = NULL;
 
-       // Do nothing if not implemented
-       if (!NW_PORT_OPS(port)->get_parent_port)
-               goto ERROR;
-
        // Fetch the parent
-       parent = NW_PORT_OPS(port)->get_parent_port(port);
+       parent = nw_port_get_parent_port(port);
        if (!parent)
-               goto ERROR;
+               return NULL;
 
        // Fetch the link
        link = nw_port_get_link(parent);
 
-ERROR:
+       // Cleanup
        if (parent)
                nw_port_unref(parent);
 
index 21e2a121340302cad5e49ecc0745855374efbdb5..38d19d1c4904a3b4394ffb2b58ceb87febb1d220 100644 (file)
@@ -119,6 +119,8 @@ int __nw_port_drop_link(nw_daemon* daemon, nw_port* port, void* data);
 
 const nw_address_t* nw_port_get_address(nw_port* port);
 
+nw_port* nw_port_get_parent_port(nw_port* port);
+
 int nw_port_reconfigure(nw_port* port);
 
 int nw_port_has_carrier(nw_port* port);