From 5a5c346c5dac343ec8242adcd04f1fbe542640ff Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 5 Jun 2023 16:47:14 +0000 Subject: [PATCH] ports: Implement a function the generally fetches the parent port Signed-off-by: Michael Tremer --- src/networkd/json.h | 89 +++++++++++++++++++++++++++++++++++++++++++++ src/networkd/port.c | 17 +++++---- src/networkd/port.h | 2 + 3 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 src/networkd/json.h diff --git a/src/networkd/json.h b/src/networkd/json.h new file mode 100644 index 00000000..33e237aa --- /dev/null +++ b/src/networkd/json.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef NETWORKD_JSON_H +#define NETWORKD_JSON_H + +#include + +#include + +// 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 */ diff --git a/src/networkd/port.c b/src/networkd/port.c index b44cc882..c9440d4f 100644 --- a/src/networkd/port.c +++ b/src/networkd/port.c @@ -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); diff --git a/src/networkd/port.h b/src/networkd/port.h index 21e2a121..38d19d1c 100644 --- a/src/networkd/port.h +++ b/src/networkd/port.h @@ -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); -- 2.47.3