2 * C Client for the DX Spider cluster program
4 * Eventually this program will be a complete replacement
5 * for the perl version.
7 * This program provides the glue necessary to talk between
8 * an input (eg from telnet or ax25) and the perl DXSpider
11 * Currently, this program connects STDIN/STDOUT to the
12 * message system used by cluster.pl
14 * Copyright (c) 2000 Dirk Koopman G1TLH
21 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
44 #define MAXPATHLEN 256
56 int cnum; /* the connection number */
57 int sort; /* the type of connection either text or msg */
58 cmsg_t *in; /* current input message being built up */
59 cmsg_t *out; /* current output message being sent */
60 cmsg_t *obuf; /* current output being buffered */
61 reft *inq; /* input queue */
62 reft *outq; /* output queue */
63 sel_t *sp; /* my select fcb address */
64 struct termios t; /* any termios associated with this cnum */
65 char echo; /* echo characters back to this cnum */
66 char t_set; /* the termios structure is valid */
67 char buffer_it; /* buffer outgoing packets for paclen */
77 char *node_addr = "localhost"; /* the node tcp address, can be overridden by DXSPIDER_HOST */
78 int node_port = 27754; /* the tcp port of the node at the above address can be overidden by DXSPIDER_PORT*/
79 char *call; /* the caller's callsign */
80 char *connsort; /* the type of connection */
81 fcb_t *in; /* the fcb of 'stdin' that I shall use */
82 fcb_t *node; /* the fcb of the msg system */
83 char nl = '\n'; /* line end character */
84 char ending = 0; /* set this to end the program */
85 char send_Z = 1; /* set a Z record to the node on termination */
86 char echo = 1; /* echo characters on stdout from stdin */
87 char int_tabs = 0; /* interpret tabs -> spaces */
88 char *root = "/spider"; /* root of data tree, can be overridden by DXSPIDER_ROOT */
89 int timeout = 60; /* default timeout for logins and things */
90 int paclen = DEFPACLEN; /* default buffer size for outgoing packets */
91 int tabsize = 8; /* default tabsize for text messages */
93 myregex_t iscallreg[] = { /* regexes to determine whether this is a reasonable callsign */
95 "^[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* G1TLH G1TLH1 */
98 "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* 2E0AAA 2E0AAA1 */
101 "^[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0 /* G1TLH-2 */
104 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0 /* 2E0AAA-2 */
107 "^[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* G1TLH-11 */
110 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* 2E0AAA-11 */
120 * utility routines - various
123 void die(char *s, ...)
129 vsnprintf(buf, sizeof(buf)-1, s, ap);
131 fprintf(stderr,"%s\n", buf);
135 char *strupper(char *s)
137 char *d = malloc(strlen(s)+1);
141 die("out of room in strupper");
142 while (*p++ = toupper(*s++)) ;
146 char *strlower(char *s)
148 char *d = malloc(strlen(s)+1);
152 die("out of room in strlower");
153 while (*p++ = tolower(*s++)) ;
157 int eq(char *a, char *b)
159 return (strcmp(a, b) == 0);
162 int xopen(char *dir, char *name, int mode)
164 char fn[MAXPATHLEN+1];
165 snprintf(fn, MAXPATHLEN, "%s/%s/%s", root, dir, name);
166 return open(fn, mode);
169 int iscallsign(char *s)
173 if (strlen(s) > MAXCALLSIGN)
176 for (rp = iscallreg; rp->in; ++rp) {
177 if (regexec(rp->regex, s, 0, 0, 0) == 0)
184 * higher level send and receive routines
187 fcb_t *fcb_new(int cnum, int sort)
189 fcb_t *f = malloc(sizeof(fcb_t));
191 die("no room in fcb_new");
192 memset (f, 0, sizeof(fcb_t));
195 f->inq = chain_new();
196 f->outq = chain_new();
200 void flush_text(fcb_t *f)
203 cmsg_send(f->outq, f->obuf, 0);
204 f->sp->flags |= SEL_OUTPUT;
209 void send_text(fcb_t *f, char *s, int l)
214 if (f->buffer_it && f->obuf) {
217 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
220 /* remove trailing spaces */
221 while (l > 0 &&isspace(s[l-1]))
224 for (p = s; p < s+l; ) {
225 if (mp->inp >= mp->data + paclen) {
227 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
231 if (mp->inp >= mp->data + paclen) {
233 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
240 void send_msg(fcb_t *f, char let, unsigned char *s, int l)
244 int myl = strlen(call)+2+l;
246 mp = cmsg_new(myl+4+1, f->sort, f);
248 strcpy(mp->inp, call);
249 mp->inp += strlen(call);
253 for (p = s; p < s+l; ++p) {
254 if (mp->inp >= mp->data + (myl - 4)) {
255 int off = mp->inp - mp->data;
257 mp = realloc(mp, myl);
258 mp->inp = mp->data + off;
261 if (*p < 0x20 || *p > 0x7e || *p == '%') {
262 sprintf(mp->inp, "%%%02X", *p & 0xff);
263 mp->inp += strlen(mp->inp);
270 cmsg_send(f->outq, mp, 0);
271 f->sp->flags |= SEL_OUTPUT;
275 * the callback (called by sel_run) that handles all the inputs and outputs
278 int fcb_handler(sel_t *sp, int in, int out, int err)
286 char *p, buf[MAXBUFL];
289 /* read what we have into a buffer */
290 r = read(f->cnum, buf, MAXBUFL);
298 /* if (f->sort == MSG)
304 /* if (f->sort == MSG)
310 dbgdump(DBUF, "in ->", buf, r);
312 /* create a new message buffer if required */
314 f->in = cmsg_new(MAXBUFL+1, f->sort, f);
321 omp = cmsg_new(3*r+1, f->sort, f);
322 while (r > 0 && p < &buf[r]) {
324 /* echo processing */
329 strcpy(omp->inp, "\b \b");
330 omp->inp += strlen(omp->inp);
337 /* character processing */
341 memset(mp->inp, ' ', tabsize);
350 if (mp->inp > mp->data)
355 if (nl == '\n' && *p == '\r') { /* ignore \r in telnet mode (ugh) */
357 } else if (*p == nl) {
358 if (mp->inp == mp->data)
360 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
361 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
362 cmsg_send(f->inq, mp, 0);
363 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
366 if (mp->inp < &mp->data[MAXBUFL-8])
375 /* queue any echo text */
377 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
378 cmsg_send(f->outq, omp, 0);
379 f->sp->flags |= SEL_OUTPUT;
386 while (r > 0 && p < &buf[r]) {
387 unsigned char ch = *p++;
389 if (mp->inp >= mp->data + (MAXBUFL-1)) {
392 dbg(DMSG, "Message longer than %d received", MAXBUFL);
400 } else if (ch == '\n') {
401 /* kick it upstairs */
403 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
404 cmsg_send(f->inq, mp, 0);
405 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
406 } else if (ch < 0x20 || ch > 0x7e) {
407 dbg(DMSG, "Illegal character (0x%02X) received", *p);
416 if (ch >= '0' && ch <= '9')
418 else if (ch >= 'A' && ch <= 'F')
419 c = (ch - 'A' + 10) << 4;
420 else if (ch >= 'a' && ch <= 'a')
421 c = (ch - 'a' + 10) << 4;
423 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
430 if (ch >= '0' && ch <= '9')
431 *mp->inp++ = c | (ch - '0');
432 else if (ch >= 'A' && ch <= 'F')
433 *mp->inp++ = c | (ch - 'A' + 10);
434 else if (ch >= 'a' && ch <= 'a')
435 *mp->inp++ = c | (ch - 'a' + 10);
437 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
446 die("invalid sort (%d) in input handler", f->sort);
456 mp = f->out = cmsg_next(f->outq);
458 sp->flags &= ~SEL_OUTPUT;
463 l = mp->size - (mp->inp - mp->data);
466 dbgdump(DBUF, "<-out", mp->inp, l);
468 r = write(f->cnum, mp->inp, l);
476 /* if (f->sort == MSG)
485 die("got negative length in handler on node");
486 if (mp->inp - mp->data >= mp->size) {
487 cmsg_callback(mp, 0);
496 * things to do with initialisation
499 void initargs(int argc, char *argv[])
503 while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
509 paclen = atoi(optarg);
512 if (paclen > MAXPACLEN)
516 node_port = atoi(optarg);
520 dbgset(atoi(optarg));
530 die("usage: client [-x n|-h<host>|-p<port>|-l<paclen>] <call>|login [local|telnet|ax25]");
534 call = strupper(argv[optind]);
538 die("Must have at least a callsign (for now)");
541 connsort = strlower(argv[optind]);
542 if (eq(connsort, "telnet") || eq(connsort, "local")) {
545 } else if (eq(connsort, "ax25")) {
549 die("2nd argument must be \"telnet\" or \"ax25\" or \"local\"");
557 /* this is kludgy, but hey so is the rest of this! */
558 if (!eq(connsort, "ax25") && paclen == DEFPACLEN) {
563 void connect_to_node()
565 struct hostent *hp, *gethostbyname();
566 struct sockaddr_in server;
570 if ((hp = gethostbyname(node_addr)) == 0)
571 die("Unknown host tcp host %s for printer", node_addr);
573 memset(&server, 0, sizeof server);
574 server.sin_family = AF_INET;
575 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
576 server.sin_port = htons(node_port);
578 nodef = socket(AF_INET, SOCK_STREAM, 0);
580 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
582 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
583 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
585 node = fcb_new(nodef, MSG);
586 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
591 * things to do with going away
594 void term_timeout(int i)
596 /* none of this is going to be reused so don't bother cleaning up properly */
598 tcsetattr(0, TCSANOW, &in->t);
600 shutdown(node->cnum, 3);
606 void terminate(int i)
608 if (node && send_Z && call) {
609 send_msg(node, 'Z', "bye", 3);
612 signal(SIGALRM, term_timeout);
615 while ((in && !is_chain_empty(in->outq)) ||
616 (node && !is_chain_empty(node->outq))) {
620 tcsetattr(0, TCSADRAIN, &in->t);
622 shutdown(node->cnum, 3);
628 void login_timeout(int i)
630 write(0, "Timed Out", 10);
632 sel_run(); /* force a coordination */
634 tcsetattr(0, TCSANOW, &in->t);
639 * things to do with ongoing processing of inputs
644 cmsg_t *mp = cmsg_next(in->inq);
646 dbg(DMSG, "MSG size: %d", mp->size);
648 if (mp->size > 0 && mp->inp > mp->data) {
649 send_msg(node, 'I', mp->data, mp->size);
651 cmsg_callback(mp, 0);
657 cmsg_t *mp = cmsg_next(node->inq);
659 dbg(DMSG, "MSG size: %d", mp->size);
661 if (mp->size > 0 && mp->inp > mp->data) {
662 char *p = strchr(mp->data, '|');
665 switch (mp->data[0]) {
676 in->buffer_it = *p - '0';
680 int l = mp->inp - (unsigned char *) p;
688 cmsg_callback(mp, 0);
695 * the program itself....
698 main(int argc, char *argv[])
700 /* set up environment */
702 char *p = getenv("DXSPIDER_ROOT");
705 p = getenv("DXSPIDER_HOST");
708 p = getenv("DXSPIDER_PORT");
711 p = getenv("DXSPIDER_PACLEN");
716 if (paclen > MAXPACLEN)
721 /* get program arguments, initialise stuff */
722 initargs(argc, argv);
723 sel_init(10, 0, 10000);
726 signal(SIGHUP, SIG_IGN);
727 signal(SIGINT, terminate);
728 signal(SIGQUIT, terminate);
729 signal(SIGTERM, terminate);
731 signal(SIGPWR, terminate);
734 /* compile regexes for iscallsign */
737 for (rp = iscallreg; rp->in; ++rp) {
739 int r = regcomp(®, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
741 die("regcomp returned %d for '%s'", r, rp->in);
742 rp->regex = malloc(sizeof(regex_t));
744 die("out of room - compiling regexes");
749 /* is this a login? */
750 if (eq(call, "LOGIN")) {
751 char buf[MAXPACLEN+1];
753 int f = xopen("data", "issue", 0);
755 while ((r = read(f, buf, paclen)) > 0) {
758 for (p = buf; p < &buf[r]; ++p) {
767 signal(SIGALRM, login_timeout);
769 write(0, "login: ", 7);
770 r = read(0, buf, 20);
772 die("No login or error (%d)", errno);
773 signal(SIGALRM, SIG_IGN);
776 if (buf[r-1] == ' ' || buf[r-1] == '\r' || buf[r-1] == '\n')
782 call = strupper(buf);
785 /* check the callsign */
786 if (!iscallsign(call)) {
787 die("Sorry, %s isn't a valid callsign", call);
790 /* connect up stdin */
791 in = fcb_new(0, TEXT);
792 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
793 if (tcgetattr(0, &in->t) < 0) {
797 struct termios t = in->t;
798 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
799 if (tcsetattr(0, TCSANOW, &t) < 0)
800 die("tcsetattr (%d)", errno);
806 /* connect up node */
809 /* tell the cluster who I am */
810 send_msg(node, 'A', connsort, strlen(connsort));
812 /* main processing loop */