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 */
93 char *connsort = "local"; /* the connection variety */
96 myregex_t iscallreg[] = { /* regexes to determine whether this is a reasonable callsign */
98 "^[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* G1TLH G1TLH1 */
101 "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* 2E0AAA 2E0AAA1 */
104 "^[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0 /* G1TLH-2 */
107 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0 /* 2E0AAA-2 */
110 "^[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* G1TLH-11 */
113 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* 2E0AAA-11 */
123 * utility routines - various
126 void die(char *s, ...)
132 vsnprintf(buf, sizeof(buf)-1, s, ap);
134 fprintf(stderr,"%s\n", buf);
138 char *strupper(char *s)
140 char *d = malloc(strlen(s)+1);
144 die("out of room in strupper");
145 while (*p++ = toupper(*s++)) ;
149 char *strlower(char *s)
151 char *d = malloc(strlen(s)+1);
155 die("out of room in strlower");
156 while (*p++ = tolower(*s++)) ;
160 int eq(char *a, char *b)
162 return (strcmp(a, b) == 0);
165 int xopen(char *dir, char *name, int mode)
167 char fn[MAXPATHLEN+1];
168 snprintf(fn, MAXPATHLEN, "%s/%s/%s", root, dir, name);
169 return open(fn, mode);
172 int iscallsign(char *s)
176 if (strlen(s) > MAXCALLSIGN)
179 for (rp = iscallreg; rp->in; ++rp) {
180 if (regexec(rp->regex, s, 0, 0, 0) == 0)
187 * higher level send and receive routines
190 fcb_t *fcb_new(int cnum, int sort)
192 fcb_t *f = malloc(sizeof(fcb_t));
194 die("no room in fcb_new");
195 memset (f, 0, sizeof(fcb_t));
198 f->inq = chain_new();
199 f->outq = chain_new();
203 void flush_text(fcb_t *f)
206 cmsg_send(f->outq, f->obuf, 0);
207 f->sp->flags |= SEL_OUTPUT;
212 void send_text(fcb_t *f, char *s, int l)
217 if (f->buffer_it && f->obuf) {
220 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
223 /* remove trailing spaces */
224 while (l > 0 &&isspace(s[l-1]))
227 for (p = s; p < s+l; ) {
228 if (mp->inp >= mp->data + paclen) {
230 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
234 if (mp->inp >= mp->data + paclen) {
236 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
249 void send_msg(fcb_t *f, char let, unsigned char *s, int l)
253 int myl = strlen(call)+2+l;
255 mp = cmsg_new(myl+4+1, f->sort, f);
257 strcpy(mp->inp, call);
258 mp->inp += strlen(call);
262 for (p = s; p < s+l; ++p) {
263 if (mp->inp >= mp->data + (myl - 4)) {
264 int off = mp->inp - mp->data;
266 mp = realloc(mp, myl);
267 mp->inp = mp->data + off;
270 if (*p < 0x20 || *p > 0x7e || *p == '%') {
271 sprintf(mp->inp, "%%%02X", *p & 0xff);
272 mp->inp += strlen(mp->inp);
279 cmsg_send(f->outq, mp, 0);
280 f->sp->flags |= SEL_OUTPUT;
284 * the callback (called by sel_run) that handles all the inputs and outputs
287 int fcb_handler(sel_t *sp, int in, int out, int err)
294 if (ending == 0 && in) {
295 char *p, buf[MAXBUFL];
298 /* read what we have into a buffer */
299 r = read(f->cnum, buf, MAXBUFL);
307 /* if (f->sort == MSG)
309 dbg(DBUF,"got errno %d in input", errno);
314 /* if (f->sort == MSG)
316 dbg(DBUF, "ending normally");
321 dbgdump(DBUF, "in ->", buf, r);
323 /* create a new message buffer if required */
325 f->in = cmsg_new(MAXBUFL+1, f->sort, f);
332 omp = cmsg_new(3*r+1, f->sort, f);
333 while (r > 0 && p < &buf[r]) {
335 /* echo processing */
340 strcpy(omp->inp, "\b \b");
341 omp->inp += strlen(omp->inp);
348 /* character processing */
352 memset(mp->inp, ' ', tabsize);
361 if (mp->inp > mp->data)
366 if (nl == '\n' && *p == '\r') { /* ignore \r in telnet mode (ugh) */
368 } else if (*p == nl) {
369 if (mp->inp == mp->data)
371 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
372 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
373 cmsg_send(f->inq, mp, 0);
374 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
377 if (mp->inp < &mp->data[MAXBUFL-8])
386 /* queue any echo text */
388 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
389 cmsg_send(f->outq, omp, 0);
390 f->sp->flags |= SEL_OUTPUT;
397 while (r > 0 && p < &buf[r]) {
398 unsigned char ch = *p++;
400 if (mp->inp >= mp->data + (MAXBUFL-1)) {
403 dbg(DMSG, "Message longer than %d received", MAXBUFL);
411 } else if (ch == '\n') {
412 /* kick it upstairs */
414 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
415 cmsg_send(f->inq, mp, 0);
416 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
417 } else if (ch < 0x20 || ch > 0x7e) {
418 dbg(DMSG, "Illegal character (0x%02X) received", *p);
427 if (ch >= '0' && ch <= '9')
429 else if (ch >= 'A' && ch <= 'F')
430 c = (ch - 'A' + 10) << 4;
431 else if (ch >= 'a' && ch <= 'a')
432 c = (ch - 'a' + 10) << 4;
434 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
441 if (ch >= '0' && ch <= '9')
442 *mp->inp++ = c | (ch - '0');
443 else if (ch >= 'A' && ch <= 'F')
444 *mp->inp++ = c | (ch - 'A' + 10);
445 else if (ch >= 'a' && ch <= 'a')
446 *mp->inp++ = c | (ch - 'a' + 10);
448 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
457 die("invalid sort (%d) in input handler", f->sort);
467 mp = f->out = cmsg_next(f->outq);
469 sp->flags &= ~SEL_OUTPUT;
474 l = mp->size - (mp->inp - mp->data);
477 dbgdump(DBUF, "<-out", mp->inp, l);
479 r = write(f->cnum, mp->inp, l);
487 /* if (f->sort == MSG)
489 dbg(DBUF,"got errno %d in output", errno);
497 die("got negative length in handler on node");
498 if (mp->inp - mp->data >= mp->size) {
499 cmsg_callback(mp, 0);
508 * set up the various mode flags, NL endings and things
510 void setmode(char *m)
512 connsort = strlower(m);
513 if (eq(connsort, "telnet") || eq(connsort, "local") || eq(connsort, "nlonly")) {
516 mode = eq(connsort, "nlonly") ? 2 : 1;
517 } else if (eq(connsort, "ax25")) {
521 } else if (eq(connsort, "connect")) {
526 die("Connection type must be \"telnet\", \"nlonly\", \"ax25\", \"login\" or \"local\"");
531 * things to do with initialisation
534 void initargs(int argc, char *argv[])
538 while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
544 paclen = atoi(optarg);
547 if (paclen > MAXPACLEN)
551 node_port = atoi(optarg);
555 dbgset(atoi(optarg));
565 die("usage: client [-x n|-h<host>|-p<port>|-l<paclen>] <call>|login [local|telnet|ax25]");
569 call = strupper(argv[optind]);
573 die("Must have at least a callsign (for now)");
576 setmode(argv[optind]);
581 /* this is kludgy, but hey so is the rest of this! */
582 if (mode != 0 && paclen == DEFPACLEN) {
587 void connect_to_node()
589 struct hostent *hp, *gethostbyname();
590 struct sockaddr_in server;
594 if ((hp = gethostbyname(node_addr)) == 0)
595 die("Unknown host tcp host %s for printer", node_addr);
597 memset(&server, 0, sizeof server);
598 server.sin_family = AF_INET;
599 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
600 server.sin_port = htons(node_port);
602 nodef = socket(AF_INET, SOCK_STREAM, 0);
604 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
606 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
607 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
609 node = fcb_new(nodef, MSG);
610 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
615 * things to do with going away
618 void term_timeout(int i)
620 /* none of this is going to be reused so don't bother cleaning up properly */
622 tcsetattr(0, TCSANOW, &in->t);
624 shutdown(node->cnum, 3);
630 void terminate(int i)
633 if (node && send_Z && call) {
634 send_msg(node, 'Z', "bye", 3);
638 signal(SIGALRM, term_timeout);
641 while ((in && !is_chain_empty(in->outq)) ||
642 (node && !is_chain_empty(node->outq))) {
646 tcsetattr(0, TCSADRAIN, &in->t);
648 shutdown(node->cnum, 3);
654 void login_timeout(int i)
656 write(0, "Timed Out", 10);
658 sel_run(); /* force a coordination */
660 tcsetattr(0, TCSANOW, &in->t);
665 * things to do with ongoing processing of inputs
670 cmsg_t *mp = cmsg_next(in->inq);
672 dbg(DMSG, "MSG size: %d", mp->size);
674 if (mp->size > 0 && mp->inp > mp->data) {
675 send_msg(node, 'I', mp->data, mp->size);
677 cmsg_callback(mp, 0);
683 cmsg_t *mp = cmsg_next(node->inq);
685 dbg(DMSG, "MSG size: %d", mp->size);
687 if (mp->size > 0 && mp->inp > mp->data) {
688 char *p = strchr(mp->data, '|');
691 switch (mp->data[0]) {
702 in->buffer_it = *p - '0';
706 int l = mp->inp - (unsigned char *) p;
714 cmsg_callback(mp, 0);
721 * the program itself....
724 main(int argc, char *argv[])
726 /* set up environment */
728 char *p = getenv("DXSPIDER_ROOT");
731 p = getenv("DXSPIDER_HOST");
734 p = getenv("DXSPIDER_PORT");
737 p = getenv("DXSPIDER_PACLEN");
742 if (paclen > MAXPACLEN)
747 /* get program arguments, initialise stuff */
748 initargs(argc, argv);
749 sel_init(10, 0, 10000);
752 signal(SIGHUP, SIG_IGN);
753 signal(SIGINT, terminate);
754 signal(SIGQUIT, terminate);
755 signal(SIGTERM, terminate);
757 signal(SIGPWR, terminate);
760 /* compile regexes for iscallsign */
763 for (rp = iscallreg; rp->in; ++rp) {
765 int r = regcomp(®, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
767 die("regcomp returned %d for '%s'", r, rp->in);
768 rp->regex = malloc(sizeof(regex_t));
770 die("out of room - compiling regexes");
775 /* is this a login? */
776 if (eq(call, "LOGIN") || eq(call, "login")) {
778 char buf[MAXPACLEN+1];
779 char callsign[MAXCALLSIGN+1];
781 int f = xopen("data", "issue", 0);
783 while ((r = read(f, buf, paclen)) > 0) {
786 for (p = buf; p < &buf[r]; ++p) {
795 signal(SIGALRM, login_timeout);
797 write(0, "login: ", 7);
798 dbgdump(DBUF, "<-out", "login: ", 7);
801 r = read(0, buf, 20);
802 dbgdump(DBUF, "in ->", buf, r);
804 die("No login or error (%d)", errno);
806 dbgdump(DBUF, "<-out", buf, r);
807 for (p = buf; p < buf+r; ++p) {
808 if (i < MAXCALLSIGN) {
809 if (*p == '\r' || *p == '\n')
811 else if (isalnum(*p) || *p == '-')
814 die("%c is not a valid callsign character", *p);
816 die("callsign entered is too long");
820 signal(SIGALRM, SIG_IGN);
823 call = strupper(callsign);
826 /* check the callsign */
827 if (!iscallsign(call)) {
828 die("Sorry, %s isn't a valid callsign", call);
831 /* connect up stdin */
832 in = fcb_new(0, TEXT);
833 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
834 if (tcgetattr(0, &in->t) < 0) {
839 struct termios t = in->t;
840 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
841 if (tcsetattr(0, TCSANOW, &t) < 0)
842 die("tcsetattr (%d)", errno);
848 /* connect up node */
851 /* tell the cluster who I am */
852 send_msg(node, 'A', connsort, strlen(connsort));
854 /* main processing loop */
855 while (ending == 0) {