summaryrefslogtreecommitdiff
path: root/stacks/terminal/amiga/max25_terminal_amiga.c
blob: 3b0f513647fd1106e6bf3a4038b4b8056a2a52ed (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * max25-terminal — AmigaOS 3.9+ reduced operator client (TCP/M25/1 only).
 *
 * HyBBX-style thin client: no ncurses/F10 menu; connects to Linux max25d.
 */
#define _DEFAULT_SOURCE 1

#include "../max25_proto.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>

#ifdef __AMIGA__
#include <sys/socket.h>
#endif

typedef struct {
    char host[256];
    unsigned port;
    char tcp_password[128];
    int ax25_ui;
    int verbose;
    int probe;
} amiga_config_t;

static void config_defaults(amiga_config_t *cfg)
{
    const char *env;

    memset(cfg, 0, sizeof(*cfg));
    strncpy(cfg->host, "127.0.0.1", sizeof(cfg->host) - 1);
    cfg->port = MAX25_DEFAULT_PORT;
    cfg->ax25_ui = 1;

    env = getenv("MAX25_HOST");
    if (env != NULL && env[0] != '\0') {
        strncpy(cfg->host, env, sizeof(cfg->host) - 1);
    }
    env = getenv("MAX25_PORT");
    if (env != NULL && env[0] != '\0') {
        cfg->port = (unsigned)strtoul(env, NULL, 10);
    }
    env = getenv("MAX25_TCP_PASSWORD");
    if (env != NULL) {
        strncpy(cfg->tcp_password, env, sizeof(cfg->tcp_password) - 1);
    }
}

static void usage(const char *prog)
{
    fprintf(stderr,
            "MAX25 Terminal (AmigaOS) — reduced CLI client for max25d\n\n"
            "Usage: %s [options] [host] [port]\n\n"
            "Options:\n"
            "  -H HOST        max25d TCP host (default 127.0.0.1)\n"
            "  -p PORT        max25d TCP port (default %u)\n"
            "  -P PASSWORD    TCP auth password (plain-text AUTH)\n"
            "      --ax25-ui  AX.25 UI mode (default on)\n"
            "      --no-ax25-ui\n"
            "  -v             Verbose protocol log on stderr\n"
            "      --probe    Connect, print STATUS, exit\n"
            "  -h             Help\n\n"
            "Session: type a line to SEND; /status /callerid /callid /connect /quit\n"
            "Environment: MAX25_HOST, MAX25_PORT, MAX25_TCP_PASSWORD\n\n",
            prog, MAX25_DEFAULT_PORT);
}

static int parse_args(int argc, char **argv, amiga_config_t *cfg)
{
    int i;
    int positional = 0;

    for (i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
            usage(argv[0]);
            return 1;
        }
        if (strcmp(argv[i], "-H") == 0 && i + 1 < argc) {
            strncpy(cfg->host, argv[++i], sizeof(cfg->host) - 1);
            continue;
        }
        if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
            cfg->port = (unsigned)strtoul(argv[++i], NULL, 10);
            continue;
        }
        if ((strcmp(argv[i], "-P") == 0 || strcmp(argv[i], "--password") == 0) &&
            i + 1 < argc) {
            strncpy(cfg->tcp_password, argv[++i], sizeof(cfg->tcp_password) - 1);
            continue;
        }
        if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
            cfg->verbose = 1;
            continue;
        }
        if (strcmp(argv[i], "--probe") == 0) {
            cfg->probe = 1;
            continue;
        }
        if (strcmp(argv[i], "--ax25-ui") == 0) {
            cfg->ax25_ui = 1;
            continue;
        }
        if (strcmp(argv[i], "--no-ax25-ui") == 0) {
            cfg->ax25_ui = 0;
            continue;
        }
        if (argv[i][0] == '-') {
            fprintf(stderr, "%s: unknown option %s\n", argv[0], argv[i]);
            return -1;
        }
        if (positional == 0) {
            strncpy(cfg->host, argv[i], sizeof(cfg->host) - 1);
        } else if (positional == 1) {
            cfg->port = (unsigned)strtoul(argv[i], NULL, 10);
        }
        positional++;
    }
    return 0;
}

static void print_rx(const char *line, int verbose)
{
    if (verbose) {
        fprintf(stderr, "max25-terminal: << %s\n", line);
    }
    if (strncmp(line, "RX ", 3) == 0) {
        printf("<< %s\n", line + 3);
    } else if (strncmp(line, "ERR ", 4) == 0) {
        fprintf(stderr, "!! %s\n", line);
    } else if (strncmp(line, "EVENT ", 6) == 0) {
        printf("-- %s\n", line);
    }
    fflush(stdout);
    fflush(stderr);
}

static int drain_socket(max25_client_t *client, int verbose)
{
    char line[MAX25_LINE_MAX];
    int got = 0;

    for (;;) {
        fd_set rfds;
        struct timeval tv;
        int fd = max25_client_fd(client);

        FD_ZERO(&rfds);
        FD_SET(fd, &rfds);
        tv.tv_sec = 0;
        tv.tv_usec = 0;
        if (select(fd + 1, &rfds, NULL, NULL, &tv) <= 0) {
            break;
        }
        if (max25_client_read_line(client, line, sizeof(line)) != 0) {
            return -1;
        }
        print_rx(line, verbose);
        got = 1;
        if (strcmp(line, "OK") == 0) {
            break;
        }
    }
    return got ? 0 : 0;
}

static int send_cmd_wait(max25_client_t *client, const char *cmd, int verbose)
{
    char line[MAX25_LINE_MAX];

    if (verbose) {
        fprintf(stderr, "max25-terminal: >> %s\n", cmd);
    }
    if (max25_client_write(client, cmd) != 0) {
        return -1;
    }
    for (;;) {
        if (max25_client_read_line(client, line, sizeof(line)) != 0) {
            return -1;
        }
        print_rx(line, verbose);
        if (strncmp(line, "STATUS ", 7) == 0) {
            continue;
        }
        if (strncmp(line, "RX ", 3) == 0) {
            continue;
        }
        if (strncmp(line, "EVENT ", 6) == 0) {
            continue;
        }
        return strcmp(line, "OK") == 0 ? 0 : -1;
    }
}

static int run_session(max25_client_t *client, amiga_config_t *cfg,
                       max25_status_t *status)
{
    char line[MAX25_LINE_MAX];

    printf("MAX25 Terminal (Amiga) — connected to %s:%u\n", cfg->host, cfg->port);
    printf("Type text to SEND. Commands: /status /callerid X /callid X /connect /quit\n");

    if (!status->connected) {
        send_cmd_wait(client, "CONNECT", cfg->verbose);
        status->connected = 1;
    }

    while (fgets(line, sizeof(line), stdin) != NULL) {
        line[strcspn(line, "\r\n")] = '\0';
        if (line[0] == '\0') {
            continue;
        }
        if (strcmp(line, "/quit") == 0 || strcmp(line, "/exit") == 0) {
            break;
        }
        if (strcmp(line, "/status") == 0) {
            send_cmd_wait(client, "GET STATUS", cfg->verbose);
            continue;
        }
        if (strcmp(line, "/connect") == 0) {
            send_cmd_wait(client, "CONNECT", cfg->verbose);
            status->connected = 1;
            continue;
        }
        if (strcmp(line, "/disconnect") == 0) {
            send_cmd_wait(client, "DISCONNECT", cfg->verbose);
            status->connected = 0;
            continue;
        }
        if (strncmp(line, "/callerid ", 10) == 0) {
            char cmd[MAX25_LINE_MAX];
            if (!max25_valid_callsign(line + 10)) {
                fprintf(stderr, "invalid CALLERID\n");
                continue;
            }
            snprintf(cmd, sizeof(cmd), "SET CALLERID %s", line + 10);
            send_cmd_wait(client, cmd, cfg->verbose);
            continue;
        }
        if (strncmp(line, "/callid ", 8) == 0) {
            char cmd[MAX25_LINE_MAX];
            if (!max25_valid_callsign(line + 8)) {
                fprintf(stderr, "invalid CALLID\n");
                continue;
            }
            snprintf(cmd, sizeof(cmd), "SET CALLID %s", line + 8);
            send_cmd_wait(client, cmd, cfg->verbose);
            continue;
        }
        {
            char cmd[MAX25_LINE_MAX + 8];
            snprintf(cmd, sizeof(cmd), "SEND %s", line);
            send_cmd_wait(client, cmd, cfg->verbose);
        }
        (void)drain_socket(client, cfg->verbose);
    }
    return 0;
}

int main(int argc, char **argv)
{
    amiga_config_t cfg;
    max25_client_t *client = NULL;
    max25_status_t status;
    char reply[MAX25_LINE_MAX];
    int rc;

    config_defaults(&cfg);
    rc = parse_args(argc, argv, &cfg);
    if (rc != 0) {
        return rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
    }

    if (max25_client_connect(cfg.host, cfg.port, "", 1, cfg.tcp_password,
                             &client, &status) != 0) {
        fprintf(stderr, "max25-terminal: connect failed (tcp %s:%u)\n",
                cfg.host, cfg.port);
        return EXIT_FAILURE;
    }

    if (cfg.probe) {
        printf("STATUS hardware=%s device=%s mode=%s callerid=%s callid=%s "
               "ax25_ui=%s connected=%s stack=%s\n",
               status.hardware, status.device, status.mode,
               status.callerid, status.callid,
               status.ax25_ui ? "on" : "off",
               status.connected ? "yes" : "no",
               status.stack);
        max25_client_write(client, "DISCONNECT");
        max25_client_free(client);
        return EXIT_SUCCESS;
    }

    if (cfg.ax25_ui) {
        max25_client_command(client, "SET AX25_UI on", reply, sizeof(reply));
        status.ax25_ui = 1;
    } else {
        max25_client_command(client, "SET AX25_UI off", reply, sizeof(reply));
        status.ax25_ui = 0;
    }

    run_session(client, &cfg, &status);
    max25_client_write(client, "DISCONNECT");
    max25_client_free(client);
    return EXIT_SUCCESS;
}
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com