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 mode = 1; /* 0 - ax25, 1 - normal telnet, 2 - nlonly telnet */
85 char ending = 0; /* set this to end the program */
86 char send_Z = 1; /* set a Z record to the node on termination */
87 char echo = 1; /* echo characters on stdout from stdin */
88 char int_tabs = 0; /* interpret tabs -> spaces */
89 char *root = "/spider"; /* root of data tree, can be overridden by DXSPIDER_ROOT */
90 int timeout = 60; /* default timeout for logins and things */
91 int paclen = DEFPACLEN; /* default buffer size for outgoing packets */
92 int tabsize = 8; /* default tabsize for text messages */
94 myregex_t iscallreg[] = { /* regexes to determine whether this is a reasonable callsign */
96 "^[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* G1TLH G1TLH1 */
99 "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* 2E0AAA 2E0AAA1 */
102 "^[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0 /* G1TLH-2 */
105 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0 /* 2E0AAA-2 */
108 "^[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* G1TLH-11 */
111 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* 2E0AAA-11 */
121 * utility routines - various
124 void die(char *s, ...)
130 vsnprintf(buf, sizeof(buf)-1, s, ap);
132 fprintf(stderr,"%s\n", buf);
136 char *strupper(char *s)
138 char *d = malloc(strlen(s)+1);
142 die("out of room in strupper");
143 while (*p++ = toupper(*s++)) ;
147 char *strlower(char *s)
149 char *d = malloc(strlen(s)+1);
153 die("out of room in strlower");
154 while (*p++ = tolower(*s++)) ;
158 int eq(char *a, char *b)
160 return (strcmp(a, b) == 0);
163 int xopen(char *dir, char *name, int mode)
165 char fn[MAXPATHLEN+1];
166 snprintf(fn, MAXPATHLEN, "%s/%s/%s", root, dir, name);
167 return open(fn, mode);
170 int iscallsign(char *s)
174 if (strlen(s) > MAXCALLSIGN)
177 for (rp = iscallreg; rp->in; ++rp) {
178 if (regexec(rp->regex, s, 0, 0, 0) == 0)
185 * higher level send and receive routines
188 fcb_t *fcb_new(int cnum, int sort)
190 fcb_t *f = malloc(sizeof(fcb_t));
192 die("no room in fcb_new");
193 memset (f, 0, sizeof(fcb_t));
196 f->inq = chain_new();
197 f->outq = chain_new();
201 void flush_text(fcb_t *f)
204 cmsg_send(f->outq, f->obuf, 0);
205 f->sp->flags |= SEL_OUTPUT;
210 void send_text(fcb_t *f, char *s, int l)
215 if (f->buffer_it && f->obuf) {
218 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
221 /* remove trailing spaces */
222 while (l > 0 &&isspace(s[l-1]))
225 for (p = s; p < s+l; ) {
226 if (mp->inp >= mp->data + paclen) {
228 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
232 if (mp->inp >= mp->data + paclen) {
234 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
247 void send_msg(fcb_t *f, char let, unsigned char *s, int l)
251 int myl = strlen(call)+2+l;
253 mp = cmsg_new(myl+4+1, f->sort, f);
255 strcpy(mp->inp, call);
256 mp->inp += strlen(call);
260 for (p = s; p < s+l; ++p) {
261 if (mp->inp >= mp->data + (myl - 4)) {
262 int off = mp->inp - mp->data;
264 mp = realloc(mp, myl);
265 mp->inp = mp->data + off;
268 if (*p < 0x20 || *p > 0x7e || *p == '%') {
269 sprintf(mp->inp, "%%%02X", *p & 0xff);
270 mp->inp += strlen(mp->inp);
277 cmsg_send(f->outq, mp, 0);
278 f->sp->flags |= SEL_OUTPUT;
282 * the callback (called by sel_run) that handles all the inputs and outputs
285 int fcb_handler(sel_t *sp, int in, int out, int err)
293 char *p, buf[MAXBUFL];
296 /* read what we have into a buffer */
297 r = read(f->cnum, buf, MAXBUFL);
305 /* if (f->sort == MSG)
307 dbg(DBUF,"got errno %d in input", errno);
312 /* if (f->sort == MSG)
314 dbg(DBUF, "ending normally");
319 dbgdump(DBUF, "in ->", buf, r);
321 /* create a new message buffer if required */
323 f->in = cmsg_new(MAXBUFL+1, f->sort, f);
330 omp = cmsg_new(3*r+1, f->sort, f);
331 while (r > 0 && p < &buf[r]) {
333 /* echo processing */
338 strcpy(omp->inp, "\b \b");
339 omp->inp += strlen(omp->inp);
346 /* character processing */
350 memset(mp->inp, ' ', tabsize);
359 if (mp->inp > mp->data)
364 if (nl == '\n' && *p == '\r') { /* ignore \r in telnet mode (ugh) */
366 } else if (*p == nl) {
367 if (mp->inp == mp->data)
369 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
370 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
371 cmsg_send(f->inq, mp, 0);
372 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
375 if (mp->inp < &mp->data[MAXBUFL-8])
384 /* queue any echo text */
386 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
387 cmsg_send(f->outq, omp, 0);
388 f->sp->flags |= SEL_OUTPUT;
395 while (r > 0 && p < &buf[r]) {
396 unsigned char ch = *p++;
398 if (mp->inp >= mp->data + (MAXBUFL-1)) {
401 dbg(DMSG, "Message longer than %d received", MAXBUFL);
409 } else if (ch == '\n') {
410 /* kick it upstairs */
412 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
413 cmsg_send(f->inq, mp, 0);
414 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
415 } else if (ch < 0x20 || ch > 0x7e) {
416 dbg(DMSG, "Illegal character (0x%02X) received", *p);
425 if (ch >= '0' && ch <= '9')
427 else if (ch >= 'A' && ch <= 'F')
428 c = (ch - 'A' + 10) << 4;
429 else if (ch >= 'a' && ch <= 'a')
430 c = (ch - 'a' + 10) << 4;
432 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
439 if (ch >= '0' && ch <= '9')
440 *mp->inp++ = c | (ch - '0');
441 else if (ch >= 'A' && ch <= 'F')
442 *mp->inp++ = c | (ch - 'A' + 10);
443 else if (ch >= 'a' && ch <= 'a')
444 *mp->inp++ = c | (ch - 'a' + 10);
446 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
455 die("invalid sort (%d) in input handler", f->sort);
465 mp = f->out = cmsg_next(f->outq);
467 sp->flags &= ~SEL_OUTPUT;
472 l = mp->size - (mp->inp - mp->data);
475 dbgdump(DBUF, "<-out", mp->inp, l);
477 r = write(f->cnum, mp->inp, l);
485 /* if (f->sort == MSG)
487 dbg(DBUF,"got errno %d in output", errno);
495 die("got negative length in handler on node");
496 if (mp->inp - mp->data >= mp->size) {
497 cmsg_callback(mp, 0);
506 * set up the various mode flags, NL endings and things
508 void setmode(char *m)
510 char *connsort = strlower(m);
511 if (eq(connsort, "telnet") || eq(connsort, "local") || eq(connsort, "nlonly") {
514 mode = eq(connsort, "nlonly") 2 : 1;
515 } else if (eq(connsort, "ax25")) {
519 } else if (eq(connsort, "connect")) {
524 die("Connection type must be \"telnet\", \"nlonly\", \"ax25\", \"login\" or \"local\"");
529 * things to do with initialisation
532 void initargs(int argc, char *argv[])
536 while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
542 paclen = atoi(optarg);
545 if (paclen > MAXPACLEN)
549 node_port = atoi(optarg);
553 dbgset(atoi(optarg));
563 die("usage: client [-x n|-h<host>|-p<port>|-l<paclen>] <call>|login [local|telnet|ax25]");
567 call = strupper(argv[optind]);
571 die("Must have at least a callsign (for now)");
574 setmode(argv[optind]);
579 /* this is kludgy, but hey so is the rest of this! */
580 if (!eq(connsort, "ax25") && paclen == DEFPACLEN) {
585 void connect_to_node()
587 struct hostent *hp, *gethostbyname();
588 struct sockaddr_in server;
592 if ((hp = gethostbyname(node_addr)) == 0)
593 die("Unknown host tcp host %s for printer", node_addr);
595 memset(&server, 0, sizeof server);
596 server.sin_family = AF_INET;
597 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
598 server.sin_port = htons(node_port);
600 nodef = socket(AF_INET, SOCK_STREAM, 0);
602 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
604 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
605 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
607 node = fcb_new(nodef, MSG);
608 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
613 * things to do with going away
616 void term_timeout(int i)
618 /* none of this is going to be reused so don't bother cleaning up properly */
620 tcsetattr(0, TCSANOW, &in->t);
622 shutdown(node->cnum, 3);
628 void terminate(int i)
630 if (node && send_Z && call) {
631 send_msg(node, 'Z', "bye", 3);
634 signal(SIGALRM, term_timeout);
637 while ((in && !is_chain_empty(in->outq)) ||
638 (node && !is_chain_empty(node->outq))) {
642 tcsetattr(0, TCSADRAIN, &in->t);
644 shutdown(node->cnum, 3);
650 void login_timeout(int i)
652 write(0, "Timed Out", 10);
654 sel_run(); /* force a coordination */
656 tcsetattr(0, TCSANOW, &in->t);
661 * things to do with ongoing processing of inputs
666 cmsg_t *mp = cmsg_next(in->inq);
668 dbg(DMSG, "MSG size: %d", mp->size);
670 if (mp->size > 0 && mp->inp > mp->data) {
671 send_msg(node, 'I', mp->data, mp->size);
673 cmsg_callback(mp, 0);
679 cmsg_t *mp = cmsg_next(node->inq);
681 dbg(DMSG, "MSG size: %d", mp->size);
683 if (mp->size > 0 && mp->inp > mp->data) {
684 char *p = strchr(mp->data, '|');
687 switch (mp->data[0]) {
698 in->buffer_it = *p - '0';
702 int l = mp->inp - (unsigned char *) p;
710 cmsg_callback(mp, 0);
717 * the program itself....
720 main(int argc, char *argv[])
722 /* set up environment */
724 char *p = getenv("DXSPIDER_ROOT");
727 p = getenv("DXSPIDER_HOST");
730 p = getenv("DXSPIDER_PORT");
733 p = getenv("DXSPIDER_PACLEN");
738 if (paclen > MAXPACLEN)
743 /* get program arguments, initialise stuff */
744 initargs(argc, argv);
745 sel_init(10, 0, 10000);
748 signal(SIGHUP, SIG_IGN);
749 signal(SIGINT, terminate);
750 signal(SIGQUIT, terminate);
751 signal(SIGTERM, terminate);
753 signal(SIGPWR, terminate);
756 /* compile regexes for iscallsign */
759 for (rp = iscallreg; rp->in; ++rp) {
761 int r = regcomp(®, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
763 die("regcomp returned %d for '%s'", r, rp->in);
764 rp->regex = malloc(sizeof(regex_t));
766 die("out of room - compiling regexes");
771 /* is this a login? */
772 if (eq(call, "LOGIN") || eq(call, "login")) {
776 char buf[MAXPACLEN+1];
777 char callsign[MAXCALLSIGN+1];
779 int f = xopen("data", "issue", 0);
781 while ((r = read(f, buf, paclen)) > 0) {
784 for (p = buf; p < &buf[r]; ++p) {
793 signal(SIGALRM, login_timeout);
795 write(0, "login: ", 7);
796 dbgdump(DBUF, "<-out", "login: ", 7);
799 r = read(0, buf, 20);
800 dbgdump(DBUF, "in ->", buf, r);
802 die("No login or error (%d)", errno);
804 dbgdump(DBUF, "<-out", buf, r);
805 for (p = buf; p < buf+r; ++p) {
806 if (i < MAXCALLSIGN) {
807 if (*p == '\r' || *p == '\n')
809 else if (isalnum(*p) || *p == '-')
812 die("%c is not a valid callsign character", *p);
814 die("callsign entered is too long");
818 signal(SIGALRM, SIG_IGN);
821 call = strupper(callsign);
824 /* check the callsign */
825 if (!iscallsign(call)) {
826 die("Sorry, %s isn't a valid callsign", call);
829 /* connect up stdin */
830 in = fcb_new(0, TEXT);
831 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
832 if (tcgetattr(0, &in->t) < 0) {
837 struct termios t = in->t;
838 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
839 if (tcsetattr(0, TCSANOW, &t) < 0)
840 die("tcsetattr (%d)", errno);
846 /* connect up node */
849 /* tell the cluster who I am */
850 send_msg(node, 'A', connsort, strlen(connsort));
852 /* main processing loop */