summaryrefslogtreecommitdiff
path: root/src/core/circuit_bridge.c
blob: af3bde4c373b80933462414a4f30e34e62606fc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "hybbx/circuit_bridge.h"
#include "hybbx/util.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct bridge_load_ctx {
    hybbx_circuit_bridge_registry_t *reg;
    const hybbx_config_t *reg_config;
} bridge_load_ctx_t;

static int bridge_section_is_numbered_transport(const char *section,
                                               const char *plugin)
{
    char prefix[64];
    size_t prefix_len;
    const char *suffix;

    if (section == NULL || plugin == NULL) {
        return 0;
    }

    snprintf(prefix, sizeof(prefix), "transport.%s", plugin);
    prefix_len = strlen(prefix);
    if (strncmp(section, prefix, prefix_len) != 0) {
        return 0;
    }

    suffix = section + prefix_len;
    if (suffix[0] == '\0') {
        return 0;
    }

    while (*suffix != '\0') {
        if (*suffix < '0' || *suffix > '9') {
            return 0;
        }
        suffix++;
    }

    return 1;
}

static int bridge_section_is_numbered_packet_radio(const char *section)
{
    return bridge_section_is_numbered_transport(section, "packet_radio");
}

static int bridge_section_is_numbered_ardop(const char *section)
{
    return bridge_section_is_numbered_transport(section, "ardop");
}

static int bridge_section_is_numbered_crdop(const char *section)
{
    return bridge_section_is_numbered_transport(section, "crdop");
}

static int bridge_section_is_numbered_baycom(const char *section)
{
    return bridge_section_is_numbered_transport(section, "baycom");
}

static void bridge_load_section(const char *section, void *userdata)
{
    bridge_load_ctx_t *ctx = (bridge_load_ctx_t *)userdata;
    hybbx_circuit_bridge_entry_t *entry;
    const char *value;
    unsigned i;

    if (ctx == NULL || ctx->reg == NULL || section == NULL) {
        return;
    }

    if (!bridge_section_is_numbered_packet_radio(section) &&
        !bridge_section_is_numbered_ardop(section) &&
        !bridge_section_is_numbered_crdop(section) &&
        !bridge_section_is_numbered_baycom(section) &&
        !bridge_section_is_numbered_transport(section, "mains_proxy")) {
        return;
    }

    if (ctx->reg->count >= HYBBX_CIRCUIT_MAX_LINKS) {
        return;
    }

    entry = &ctx->reg->entries[ctx->reg->count];
    memset(entry, 0, sizeof(*entry));

    value = hybbx_config_get(ctx->reg_config, section, "link_id", NULL);
    if (value == NULL || value[0] == '\0') {
        return;
    }
    hybbx_strlcpy(entry->link_id, value, sizeof(entry->link_id));

    for (i = 0; i < ctx->reg->count; i++) {
        if (strcmp(ctx->reg->entries[i].link_id, entry->link_id) == 0) {
            fprintf(stderr,
                    "[circuit] bridge: duplicate link_id=%s in [%s] — skipped\n",
                    entry->link_id, section);
            return;
        }
    }

    value = hybbx_config_get(ctx->reg_config, section, "link_password", NULL);
    if (value != NULL && value[0] != '\0') {
        hybbx_strlcpy(entry->link_password, value, sizeof(entry->link_password));
    }

    value = hybbx_config_get(ctx->reg_config, section, "link_role", NULL);
    if (value != NULL && value[0] != '\0') {
        hybbx_strlcpy(entry->link_role, value, sizeof(entry->link_role));
    } else if (bridge_section_is_numbered_transport(section, "mains_proxy")) {
        hybbx_strlcpy(entry->link_role, "proxy", sizeof(entry->link_role));
    } else {
        hybbx_strlcpy(entry->link_role, "link", sizeof(entry->link_role));
    }

    value = hybbx_config_get(ctx->reg_config, section, "frequency_mhz", NULL);
    if (value == NULL || value[0] == '\0') {
        value = hybbx_config_get(ctx->reg_config, section, "frequency", NULL);
    }
    if (value != NULL && value[0] != '\0') {
        double mhz = strtod(value, NULL);

        if (mhz > 0.0) {
            entry->frequency_mhz = mhz;
        }
    }

    ctx->reg->count++;
}

void hybbx_circuit_bridge_clear(hybbx_circuit_bridge_registry_t *reg)
{
    if (reg == NULL) {
        return;
    }
    memset(reg, 0, sizeof(*reg));
}

hybbx_result_t hybbx_circuit_bridge_load(hybbx_circuit_bridge_registry_t *reg,
                                         const hybbx_config_t *config)
{
    bridge_load_ctx_t ctx;

    if (reg == NULL || config == NULL) {
        return HYBBX_ERR_INVALID;
    }

    hybbx_circuit_bridge_clear(reg);
    ctx.reg = reg;
    ctx.reg_config = config;
    hybbx_config_foreach_section(config, bridge_load_section, &ctx);
    return HYBBX_OK;
}

const hybbx_circuit_bridge_entry_t *hybbx_circuit_bridge_find(
    const hybbx_circuit_bridge_registry_t *reg, const char *link_id)
{
    unsigned i;

    if (reg == NULL || link_id == NULL || link_id[0] == '\0') {
        return NULL;
    }

    for (i = 0; i < reg->count; i++) {
        if (strcmp(reg->entries[i].link_id, link_id) == 0) {
            return &reg->entries[i];
        }
    }

    return NULL;
}
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com