aboutsummaryrefslogtreecommitdiffstats
path: root/propagate/src/pg.c
blob: f3c3c8d1e0e789940441ecb186145e513908cd89 (plain) (blame)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* XXX
 * we should inspire from netcat
 * http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/nc/
 * see readwrite(), atomicio()
 */

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

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <sys/socket.h>
#include <poll.h>

#include "pg.h"

extern char *__progname;

static int usage();
static void process(void);
static int pfds_create(struct pollfd *);
static int cmd_init(struct conn *, int, struct msg_header *);
static int cmd_exec(struct conn *, int, struct msg_header *);
static int cmd_kill(struct conn *, int, struct msg_header *);
static int cmd_read(struct conn *, int, struct msg_header *);
static int cmd_write(struct conn *, int, struct msg_header *);
static int cmd_data_conn(struct conn *, int, struct msg_header *);
static int cmd_data_route(struct route *, int, struct msg_header *);
static int cmd_ok(struct route *, int, struct msg_header *);
static int cmd_err(struct route *, int, struct msg_header *);

int
main(int argc, char *argv[]) {
    int opt;
    int loglevel = 1, logstdout = 0;
    int deamonize = 0;
    int server = 0;
    char dest = 0;
    char *cmd = NULL;

    while ((opt = getopt(argc, argv, "dhltv")) != -1) {
        switch (opt) {
            case 'd':
                logstdout = 1;
                deamonize = 0;
                break;
            case 'h':
                usage();
                break;
            case 'l':
                server = 1;
                break;
            case 't':
                dest = optarg[0];
                break;
            case 'v':
                loglevel++;
                break;
            default:
                usage();
                break;
        }
    }

    argc -= optind;
    argv += optind;

    if ((server && argc != 0) ||
            (!server && argc != 1) ||
            (server && conf.client_dest))
        usage();
    if (!server) {
        cmd = argv[0];
        if (dest == 0)
            dest = 'B';
        conf.client_dest = dest;
    }
    
    log_init(loglevel, logstdout);
    log_warn("** Starting propagate v%.1f", VERSION);
    log_warn("**   using message version %d", MSG_VERSION);
    log_warn("**   loglevel %d", loglevel);

    conf.server = server;
    LIST_INIT(&routes);
    routes_count = 0;
    LIST_INIT(&listeners);
    listeners_count = 0;

    /* XXX load conf */
    if (server) {
        conf.me = 'B';
        listener_add(LISTENER_UNIX, "/tmp/propagate_sock");
    } else {
        conf.me = 'A';
        route_add('B', ROUTE_PROC, "nc 127.0.0.1 3333", 0, NULL, 0);
    }

    if (server)
        log_info("starting server %c", conf.me);
    else
        log_info("running client %c, dest %c, command %s", conf.me, conf.client_dest, cmd);

    if (deamonize)
        log_info("XXX deamonize not implemented yet");

    if (!server)
        send_cmd(conf.client_dest, MSG_EXEC, 0, (uint8_t *)cmd, strlen(cmd));

    process();

    return 0;
}

static int
usage() {
    printf("Usage: %s [-dhv] [-t destination] command\n", __progname);
    printf("       %s -l [-dhv]\n", __progname);

    exit(1);
}

static void
process(void) {
    struct listener *l;
    struct conn *c;
    struct route *r;
    uint8_t *buf;
    struct msg_header *hdr;
    struct sockaddr_storage cliaddr;
    socklen_t slen = sizeof(cliaddr);
    int len, n, fd, cfd;
    struct pollfd pfds[POLLER_MAX];
    int pfds_count;
    /* keep in sync with pg.h MSG enum */
    int (*cmd_conn[MSG_MAX+1])
            (struct conn *, int, struct msg_header *) = {
        &cmd_init,
        &cmd_init,
        &cmd_kill,
        &cmd_exec,
        &cmd_read,
        &cmd_write,
        &cmd_data_conn,
        NULL,
        NULL
    };
    int (*cmd_route[MSG_MAX+1])
            (struct route *, int, struct msg_header *) = {
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        &cmd_data_route,
        &cmd_ok,
        &cmd_err
    };


    /* OLD TODO
     * fork frontend and open pipe_fe
     * poll()
     *  - pipe_fe input -> write stdout
     *  - stdin -> append send_buf
     * sigalarm() every second
     *  - MSG_READ and write on stdout 
     *  - if size(send_buf) > 0: MSG_WRITE send_buf
     * handle network errors / retransmission in fe
     * MSG_[READ|WRITE] do not expect MSG_OK, but warns on MSG_ERROR
     * MSG_OK only for MSG_INIT and MSG_EXEC
     */

    for (;;) {
        pfds_count = pfds_create(pfds);
        n = poll(pfds, pfds_count, POLL_TIMEOUT);
        log_tmp("end of poll, %d fds on %d", n, pfds_count);
        if (n < 0) {
            log_warn("polling error"); // XXX fatal ?
            continue;
        }

        for (n=0; n<pfds_count; n++) {
            if ((pfds[n].revents & POLLIN) == 0)
                continue;
            fd = pfds[n].fd;
            log_tmp("  fd %d got something", fd);

            if ((l = listener_find(fd))) {
                log_debug("got listener new connection");
                cfd = accept(fd, (struct sockaddr *)&cliaddr, &slen);
                listener_conn_add(l, cfd);
            } else if ((c = listener_conn_find_exec(fd))) {
                log_debug("got exec data");
                if (c->async)
                    listener_conn_exec_bufferize(c, fd);
                else
                    msg_send_from_fd(c->fd, MSG_DATA, c->orig, 0, fd);
            } else if (fd == fileno(stdin)) {
                log_debug("got stdin data");
                r = route_find(conf.client_dest);
                if (r->proc.async)
                    route_bufferize(r, fd);
                else
                    msg_send_from_fd(r->proc.fd[1], MSG_DATA, r->dest, 0, fd);
            } else {
                log_debug("got command");
                len = readbuf(fd, &buf, sizeof(MSG_MAGIC));
                if (len < sizeof(MSG_MAGIC)) {
                    log_info("Magic number too short");
                    continue;
                }
                if (memcmp(buf, MSG_MAGIC, sizeof(MSG_MAGIC))) {
                    log_info("Invalid magic number %4x", buf);
                    continue;
                }
                len = readbuf(fd, &buf, MSG_HEADER_SIZE_ENCODED);
                if (len < MSG_HEADER_SIZE_ENCODED) {
                    log_warn("Message header too short");
                    continue;
                }
                hdr = msg_unpack_header(buf);
                if (!hdr) {
                    log_warn("Invalid message header");
                    continue;
                }

                if (hdr->dest != conf.me) {
                    route_fw(fd, hdr, buf);
                    free(hdr);
                    continue;
                }

                if ((c = listener_conn_find(fd))) {
                    cmd_conn[hdr->type](c, fd, hdr);
                    break;
                } else if ((r = route_find(hdr->orig))) {
                    cmd_route[hdr->type](r, fd, hdr);
                    break;
                } else
                    log_warn("host %c has no reference ! ignoring command...",
                             hdr->orig);
                free(hdr);
            }
        }
    }
}

static int
pfds_create(struct pollfd *pfds) {
    struct route *r;
    struct listener *l;
    struct conn *c;
    int count = 0;

#define ADDFD(myfd) { \
pfds[count].fd = myfd; \
pfds[count].events = POLLIN; \
count++; \
}

    if (!conf.server)
        ADDFD(fileno(stdin))

    LIST_FOREACH(r, &routes, entry) {
        switch (r->type) {
        case ROUTE_PROC:
            ADDFD(r->proc.fd[0])
        }
    }
        
    LIST_FOREACH(l, &listeners, entry) {
        ADDFD(l->sock)
        LIST_FOREACH(c, &l->conns, entry) {
            ADDFD(c->fd)
            if (c->exec.cmd)
                ADDFD(c->exec.fd[0])
        }
    }

    return count;
}

static int
cmd_init(struct conn *c, int fd, struct msg_header *hdr) {
    struct conn *oldc;
    
    log_debug("received INIT from %c :)", hdr->orig);
    if (c->state == CONN_READY) {
        log_warn("received INIT on an already open connection !");
        send_cmd(hdr->orig, MSG_ERR, 0, NULL, 0);
        return -1;
    }

    /* checking if this client already has a connection opened on
     * another fd */
    oldc = listener_conn_find_orig(hdr->orig);
    if (oldc)
        listener_conn_move(oldc, c);

    c->orig = hdr->orig;
    c->state = CONN_READY;

    switch (hdr->type) {
    case MSG_INIT:
        c->async = 0;
        break;
    case MSG_INIT_ASYNC:
        c->async = 1;
        break;
    }

    send_cmd(hdr->orig, MSG_OK, 0, NULL, 0);

    return 0;
}

static int
cmd_exec(struct conn *c, int fd, struct msg_header *hdr) {
    char *cmd = NULL;
    int cmdlen;
    char **argv = NULL;
    int argc;

    cmdlen = readbuf(fd, (uint8_t **)&cmd, hdr->datalen);
    if (cmdlen <= 0 || !cmd)
        goto err;
    argv = explode(cmd, cmdlen, " ", &argc);
    if (!argv)
        goto err;
    if (listener_conn_exec(c, argv[0], argv) < 0)
        goto err;

    send_cmd(hdr->orig, MSG_OK, 0, NULL, 0);

    return 0;

err:
    log_debug("exec failed");
    if (cmd)
        free(cmd);
    if (argv)
        free(argv);
    send_cmd(hdr->orig, MSG_ERR, 0, NULL, 0);

    return -1;
}

static int
cmd_kill(struct conn *c, int fd, struct msg_header *hdr) {
    listener_conn_exec_kill(c);
    send_cmd(hdr->orig, MSG_OK, 0, NULL, 0);

    return 0;
}

static int
cmd_read(struct conn *c, int fd, struct msg_header *hdr) {
    int len;

    if (!c->exec.cmd) {
        log_warn("cmd_read: no exec in progress !");
        goto err;
    }
    if (c->async == 0) {
        log_warn("cmd_read: not in async mode !");
        goto err;
    }

    len = send_cmd(c->orig, MSG_DATA, 0,
                   c->exec.async_writebuf, c->exec.async_writebuf_size);
    if (len < 0)
        goto err;
    c->exec.async_writebuf_size -= len;
    c->exec.async_writebuf = realloc(c->exec.async_writebuf,
                                     c->exec.async_writebuf_size);
    return 0;

err:
    send_cmd(c->orig, MSG_ERR, 0, NULL, 0);
    return -1;
}

static int
cmd_write(struct conn *c, int fd, struct msg_header *hdr) {
    if (!c->exec.cmd) {
        log_warn("cmd_write: no exec in progress !");
        goto err;
    }
    if (c->async == 0) {
        log_warn("cmd_write: not in async mode !");
        goto err;
    }

    if (msg_read_data_to_fd(fd, c->exec.fd[1], hdr->datalen) < 0) {
        goto err;
    }

    return 0;

err:
    send_cmd(c->orig, MSG_ERR, 0, NULL, 0);
    return -1;
}

static int
cmd_data_conn(struct conn *c, int fd, struct msg_header *hdr) {
    log_debug("received DATA from conn %c !", hdr->orig);

    if (msg_read_data_to_fd(fd, c->exec.fd[1], hdr->datalen) < 0) {
        log_warn("cmd_data: recvwrite failed");
        return -1;
    }

    return 0;
}

static int
cmd_data_route(struct route *r, int fd, struct msg_header *hdr) {
    log_debug("received DATA from route %c !", hdr->orig);

    if (msg_read_data_to_fd(fd, fileno(stdout), hdr->datalen) < 0) {
        log_warn("cmd_data: recvwrite failed");
        return -1;
    }

    return 0;
}

static int
cmd_ok(struct route *r, int fd, struct msg_header *hdr) {
    log_info("received OK from %c !", hdr->orig);

    return 0;
}

static int
cmd_err(struct route *r, int fd, struct msg_header *hdr) {
    log_info("received ERROR from %c !", hdr->orig);

    return 0;
}