summaryrefslogtreecommitdiff
path: root/src/core/networks.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/networks.c')
-rw-r--r--src/core/networks.c191
1 files changed, 191 insertions, 0 deletions
diff --git a/src/core/networks.c b/src/core/networks.c
new file mode 100644
index 0000000..c52554d
--- /dev/null
+++ b/src/core/networks.c
@@ -0,0 +1,191 @@
+#include "hybbx/networks.h"
+#include "hybbx/instance.h"
+#include "hybbx/config.h"
+#include "hybbx/util.h"
+#include "hybbx/log.h"
+
+#include <stdio.h>
+#include <string.h>
+
+static int str_ieq(const char *a, const char *b)
+{
+ if (a == NULL || b == NULL) {
+ return 0;
+ }
+
+ while (*a != '\0' && *b != '\0') {
+ char ca = (char)(*a >= 'A' && *a <= 'Z' ? *a + 32 : *a);
+ char cb = (char)(*b >= 'A' && *b <= 'Z' ? *b + 32 : *b);
+
+ if (ca != cb) {
+ return 0;
+ }
+ a++;
+ b++;
+ }
+
+ return *a == '\0' && *b == '\0';
+}
+
+static int networks_has_key(const hybbx_config_t *config, const char *key)
+{
+ const char *value;
+
+ if (config == NULL || key == NULL) {
+ return 0;
+ }
+
+ value = hybbx_config_get(config, "networks", key, NULL);
+ return value != NULL && value[0] != '\0';
+}
+
+void hybbx_networks_config_defaults(hybbx_networks_config_t *networks)
+{
+ if (networks == NULL) {
+ return;
+ }
+
+ networks->telnet = 1;
+ networks->ax25 = 0;
+ networks->baycom = 0;
+ networks->ardop = 0;
+ networks->crdop = 0;
+ networks->ssh = 0;
+ networks->websocket = 0;
+ networks->circuit = 1;
+ networks->mains_proxy = 0;
+}
+
+void hybbx_networks_config_apply(hybbx_networks_config_t *networks,
+ const hybbx_config_t *config)
+{
+ if (networks == NULL) {
+ return;
+ }
+
+ hybbx_networks_config_defaults(networks);
+
+ if (config == NULL) {
+ return;
+ }
+
+ if (networks_has_key(config, "telnet")) {
+ networks->telnet = hybbx_config_get_bool(config, "networks",
+ "telnet", 1);
+ }
+
+ networks->ax25 = hybbx_config_get_bool(config, "networks", "ax25", 0);
+
+ if (networks_has_key(config, "baycom")) {
+ networks->baycom = hybbx_config_get_bool(config, "networks", "baycom", 0);
+ }
+
+ if (networks_has_key(config, "ardop")) {
+ networks->ardop = hybbx_config_get_bool(config, "networks", "ardop", 0);
+ }
+
+ if (networks_has_key(config, "crdop")) {
+ networks->crdop = hybbx_config_get_bool(config, "networks", "crdop", 0);
+ }
+
+ networks->ssh = hybbx_config_get_bool(config, "networks", "ssh", 0);
+
+ networks->websocket = hybbx_config_get_bool(config, "networks",
+ "websocket", 0);
+
+ if (networks_has_key(config, "circuit")) {
+ networks->circuit = hybbx_config_get_bool(config, "networks",
+ "circuit", 1);
+ } else {
+ networks->circuit = hybbx_config_get_bool(config, "circuit", "enabled",
+ 1);
+ }
+
+ if (networks_has_key(config, "mains_proxy")) {
+ networks->mains_proxy = hybbx_config_get_bool(config, "networks",
+ "mains_proxy", 0);
+ }
+
+ hybbx_log_info("[networks] telnet=%s ssh=%s ax25=%s baycom=%s ardop=%s "
+ "crdop=%s websocket=%s circuit=%s mains_proxy=%s",
+ hybbx_bool_to_string(networks->telnet),
+ hybbx_bool_to_string(networks->ssh),
+ hybbx_bool_to_string(networks->ax25),
+ hybbx_bool_to_string(networks->baycom),
+ hybbx_bool_to_string(networks->ardop),
+ hybbx_bool_to_string(networks->crdop),
+ hybbx_bool_to_string(networks->websocket),
+ hybbx_bool_to_string(networks->circuit),
+ hybbx_bool_to_string(networks->mains_proxy));
+}
+
+int hybbx_networks_is_static_transport(const char *plugin_name)
+{
+ if (plugin_name == NULL || plugin_name[0] == '\0') {
+ return 0;
+ }
+
+ switch (hybbx_instance_role()) {
+ case HYBBX_INSTANCE_MAIN:
+ return str_ieq(plugin_name, "websocket");
+ case HYBBX_INSTANCE_SECONDARY:
+ return str_ieq(plugin_name, "mains_proxy");
+ case HYBBX_INSTANCE_PROXY:
+ default:
+ return 0;
+ }
+}
+
+int hybbx_networks_transport_wanted(const char *plugin_name,
+ const hybbx_networks_config_t *networks)
+{
+ if (plugin_name == NULL || plugin_name[0] == '\0') {
+ return 0;
+ }
+
+ if (!hybbx_instance_plugin_allowed(plugin_name)) {
+ return 0;
+ }
+
+ if (hybbx_networks_is_static_transport(plugin_name)) {
+ return 1;
+ }
+
+ if (networks == NULL) {
+ return 0;
+ }
+
+ if (str_ieq(plugin_name, "telnet")) {
+ return networks->telnet;
+ }
+
+ if (str_ieq(plugin_name, "packet_radio")) {
+ return networks->ax25;
+ }
+
+ if (str_ieq(plugin_name, "baycom")) {
+ return networks->baycom;
+ }
+
+ if (str_ieq(plugin_name, "ardop")) {
+ return networks->ardop;
+ }
+
+ if (str_ieq(plugin_name, "crdop")) {
+ return networks->crdop;
+ }
+
+ if (str_ieq(plugin_name, "ssh")) {
+ return networks->ssh;
+ }
+
+ if (str_ieq(plugin_name, "websocket")) {
+ return networks->websocket;
+ }
+
+ if (str_ieq(plugin_name, "mains_proxy")) {
+ return networks->mains_proxy;
+ }
+
+ return 0;
+}
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com