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
55 #define UC unsigned char
59 int cnum; /* the connection number */
60 int sort; /* the type of connection either text or msg */
61 cmsg_t *in; /* current input message being built up */
62 cmsg_t *out; /* current output message being sent */
63 cmsg_t *obuf; /* current output being buffered */
64 reft *inq; /* input queue */
65 reft *outq; /* output queue */
66 sel_t *sp; /* my select fcb address */
67 struct termios t; /* any termios associated with this cnum */
68 char echo; /* echo characters back to this cnum */
69 char t_set; /* the termios structure is valid */
70 char buffer_it; /* buffer outgoing packets for paclen */
80 char *node_addr = "localhost"; /* the node tcp address, can be overridden by DXSPIDER_HOST */
81 int node_port = 27754; /* the tcp port of the node at the above address can be overidden by DXSPIDER_PORT*/
82 char *call; /* the caller's callsign */
83 char *connsort; /* the type of connection */
84 fcb_t *in; /* the fcb of 'stdin' that I shall use */
85 fcb_t *node; /* the fcb of the msg system */
86 char nl = '\n'; /* line end character */
87 char mode = 1; /* 0 - ax25, 1 - normal telnet, 2 - nlonly telnet */
88 char ending = 0; /* set this to end the program */
89 char echo = 1; /* echo characters on stdout from stdin */
90 char int_tabs = 0; /* interpret tabs -> spaces */
91 char *root = "/spider"; /* root of data tree, can be overridden by DXSPIDER_ROOT */
92 int timeout = 60; /* default timeout for logins and things */
93 int paclen = DEFPACLEN; /* default buffer size for outgoing packets */
94 int tabsize = 8; /* default tabsize for text messages */
95 char *connsort = "local"; /* the connection variety */
96 int state = 0; /* the current state of the connection */
97 int laststate = 0; /* the last state we were in */
101 #define CONNECTED 100
107 myregex_t iscallreg[] = { /* regexes to determine whether this is a reasonable callsign */
109 "^[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* G1TLH G1TLH1 */
112 "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* 2E0AAA 2E0AAA1 */
115 "^[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0 /* G1TLH-2 */
118 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0 /* 2E0AAA-2 */
121 "^[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* G1TLH-11 */
124 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* 2E0AAA-11 */
134 * utility routines - various
137 void chgstate(int new)
141 dbg(DSTS, "chg state %d->%d", laststate, state);
144 void die(char *s, ...)
150 vsnprintf(buf, sizeof(buf)-1, s, ap);
152 fprintf(stderr,"%s\n", buf);
156 char *strupper(char *s)
158 char *d = malloc(strlen(s)+1);
162 die("out of room in strupper");
163 while (*p++ = toupper(*s++)) ;
167 char *strlower(char *s)
169 char *d = malloc(strlen(s)+1);
173 die("out of room in strlower");
174 while (*p++ = tolower(*s++)) ;
178 int eq(char *a, char *b)
180 return (strcmp(a, b) == 0);
183 FILE *xopen(char *dir, char *name, char *mode)
185 char fn[MAXPATHLEN+1];
186 snprintf(fn, MAXPATHLEN, "%s/%s/%s", root, dir, name);
187 return fopen(fn, mode);
190 int iscallsign(char *s)
194 if (strlen(s) > MAXCALLSIGN)
197 for (rp = iscallreg; rp->in; ++rp) {
198 if (regexec(rp->regex, s, 0, 0, 0) == 0)
205 * higher level send and receive routines
208 fcb_t *fcb_new(int cnum, int sort)
210 fcb_t *f = malloc(sizeof(fcb_t));
212 die("no room in fcb_new");
213 memset (f, 0, sizeof(fcb_t));
216 f->inq = chain_new();
217 f->outq = chain_new();
221 void flush_text(fcb_t *f)
224 cmsg_send(f->outq, f->obuf, 0);
225 f->sp->flags |= SEL_OUTPUT;
230 void send_text(fcb_t *f, char *s, int l, int nlreq)
235 if (f->buffer_it && f->obuf) {
238 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
241 /* remove trailing spaces */
242 while (l > 0 &&isspace(s[l-1]))
245 for (p = s; p < s+l; ) {
246 if (mp->inp >= mp->data + paclen) {
248 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
252 if (mp->inp >= mp->data + paclen) {
254 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
269 void send_msg(fcb_t *f, char let, UC *s, int l)
273 int myl = strlen(call)+2+l;
275 mp = cmsg_new(myl+4+1, f->sort, f);
277 strcpy(mp->inp, call);
278 mp->inp += strlen(call);
282 for (p = s; p < s+l; ++p) {
283 if (mp->inp >= mp->data + (myl - 4)) {
284 int off = mp->inp - mp->data;
286 mp = realloc(mp, myl);
287 mp->inp = mp->data + off;
290 if (*p < 0x20 || *p > 0x7e || *p == '%') {
291 sprintf(mp->inp, "%%%02X", *p & 0xff);
292 mp->inp += strlen(mp->inp);
299 cmsg_send(f->outq, mp, 0);
300 f->sp->flags |= SEL_OUTPUT;
304 * the callback (called by sel_run) that handles all the inputs and outputs
307 int fcb_handler(sel_t *sp, int in, int out, int err)
314 if (ending == 0 && in) {
315 char *p, buf[MAXBUFL];
318 /* read what we have into a buffer */
319 r = read(f->cnum, buf, MAXBUFL);
327 dbg(DBUF,"got errno %d in input", errno);
332 dbg(DBUF, "ending normally");
337 dbgdump(DBUF, "in ->", buf, r);
339 /* create a new message buffer if required */
341 f->in = cmsg_new(MAXBUFL+1, f->sort, f);
348 omp = cmsg_new(3*r+1, f->sort, f);
349 while (r > 0 && p < &buf[r]) {
351 /* echo processing */
356 strcpy(omp->inp, "\b \b");
357 omp->inp += strlen(omp->inp);
362 strcpy(omp->inp, "\r\n");
370 /* character processing */
374 memset(mp->inp, ' ', tabsize);
383 if (mp->inp > mp->data)
388 if (nl == '\n' && *p == '\r') { /* ignore \r in telnet mode (ugh) */
390 } else if (nl == '\r' && *p == '\n') { /* and ignore \n in ax25 mode (double ugh) */
392 } else if (*p == nl) {
393 if (mp->inp == mp->data)
395 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
396 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
397 cmsg_send(f->inq, mp, 0);
398 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
401 if (mp->inp < &mp->data[MAXBUFL-8])
410 /* queue any echo text */
412 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
413 cmsg_send(f->outq, omp, 0);
414 f->sp->flags |= SEL_OUTPUT;
421 while (r > 0 && p < &buf[r]) {
424 if (mp->inp >= mp->data + (MAXBUFL-1)) {
427 dbg(DMSG, "Message longer than %d received", MAXBUFL);
435 } else if (ch == '\n') {
436 /* kick it upstairs */
438 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
439 cmsg_send(f->inq, mp, 0);
440 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
441 } else if (ch < 0x20 || ch > 0x7e) {
442 dbg(DMSG, "Illegal character (0x%02X) received", *p);
451 if (ch >= '0' && ch <= '9')
453 else if (ch >= 'A' && ch <= 'F')
454 c = (ch - 'A' + 10) << 4;
455 else if (ch >= 'a' && ch <= 'a')
456 c = (ch - 'a' + 10) << 4;
458 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
465 if (ch >= '0' && ch <= '9')
466 *mp->inp++ = c | (ch - '0');
467 else if (ch >= 'A' && ch <= 'F')
468 *mp->inp++ = c | (ch - 'A' + 10);
469 else if (ch >= 'a' && ch <= 'a')
470 *mp->inp++ = c | (ch - 'a' + 10);
472 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
481 die("invalid sort (%d) in input handler", f->sort);
491 mp = f->out = cmsg_next(f->outq);
493 sp->flags &= ~SEL_OUTPUT;
498 l = mp->size - (mp->inp - mp->data);
501 dbgdump(DBUF, "<-out", mp->inp, l);
503 r = write(f->cnum, mp->inp, l);
511 dbg(DBUF,"got errno %d in output", errno);
519 die("got negative length in handler on node");
520 if (mp->inp - mp->data >= mp->size) {
521 cmsg_callback(mp, 0);
530 * set up the various mode flags, NL endings and things
532 void setmode(char *m)
534 connsort = strlower(m);
535 if (eq(connsort, "telnet") || eq(connsort, "local") || eq(connsort, "nlonly")) {
538 mode = eq(connsort, "nlonly") ? 2 : 1;
539 } else if (eq(connsort, "ax25")) {
543 } else if (eq(connsort, "connect")) {
548 die("Connection type must be \"telnet\", \"nlonly\", \"ax25\", \"login\" or \"local\"");
554 * things to do with ongoing processing of inputs
559 cmsg_t *mp = cmsg_next(in->inq);
560 char *p, hasa, hasn, i;
561 char callsign[MAXCALLSIGN+1];
564 dbg(DMSG, "MSG size: %d", mp->size);
569 send_msg(node, 'I', mp->data, mp->size);
573 for (i = 0; i < mp->size; ++i) {
575 if (i < MAXCALLSIGN) {
580 if (isalnum(ch) || ch == '-')
583 die("invalid callsign");
585 die("invalid callsign");
588 if (strlen(callsign) < 3)
589 die("invalid callsign");
593 die("invalid callsign");
594 call = strupper(callsign);
596 /* check the callsign against the regexes */
597 if (!iscallsign(call)) {
598 die("Sorry, %s isn't a valid callsign", call);
602 signal(SIGALRM, SIG_IGN);
604 /* tell the cluster who I am */
605 send_msg(node, 'A', connsort, strlen(connsort));
610 cmsg_callback(mp, 0);
617 cmsg_t *mp = cmsg_next(node->inq);
619 dbg(DMSG, "MSG size: %d", mp->size);
621 if (mp->size > 0 && mp->inp > mp->data) {
622 char *p = strchr(mp->data, '|');
625 switch (mp->data[0]) {
635 in->buffer_it = *p - '0';
639 int l = mp->inp - (UC *) p;
640 send_text(in, p, l, 1);
647 cmsg_callback(mp, 0);
654 * things to do with going away
657 void term_timeout(int i)
659 /* none of this is going to be reused so don't bother cleaning up properly */
661 tcsetattr(0, TCSANOW, &in->t);
663 shutdown(node->cnum, 3);
669 void terminate(int i)
671 signal(SIGALRM, term_timeout);
674 while ((in && !is_chain_empty(in->outq)) ||
675 (node && !is_chain_empty(node->outq))) {
679 tcsetattr(0, TCSADRAIN, &in->t);
681 shutdown(node->cnum, 3);
687 void login_timeout(int i)
689 write(0, "Timed Out", 10);
691 sel_run(); /* force a coordination */
693 tcsetattr(0, TCSANOW, &in->t);
698 * things to do with initialisation
701 void initargs(int argc, char *argv[])
705 while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
711 paclen = atoi(optarg);
714 if (paclen > MAXPACLEN)
718 node_port = atoi(optarg);
722 dbgset(atoi(optarg));
732 die("usage: client [-x n|-h<host>|-p<port>|-l<paclen>] <call>|login [local|telnet|ax25]");
736 call = strupper(argv[optind]);
740 die("Must have at least a callsign (for now)");
743 setmode(argv[optind]);
748 /* this is kludgy, but hey so is the rest of this! */
749 if (mode != 0 && paclen == DEFPACLEN) {
754 void connect_to_node()
756 struct hostent *hp, *gethostbyname();
757 struct sockaddr_in server;
761 if ((hp = gethostbyname(node_addr)) == 0)
762 die("Unknown host tcp host %s for printer", node_addr);
764 memset(&server, 0, sizeof server);
765 server.sin_family = AF_INET;
766 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
767 server.sin_port = htons(node_port);
769 nodef = socket(AF_INET, SOCK_STREAM, 0);
771 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
773 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
774 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
776 node = fcb_new(nodef, MSG);
777 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
782 * the program itself....
785 main(int argc, char *argv[])
787 /* compile regexes for iscallsign */
790 for (rp = iscallreg; rp->in; ++rp) {
792 int r = regcomp(®, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
794 die("regcomp returned %d for '%s'", r, rp->in);
795 rp->regex = malloc(sizeof(regex_t));
797 die("out of room - compiling regexes");
802 /* set up environment */
804 char *p = getenv("DXSPIDER_ROOT");
807 p = getenv("DXSPIDER_HOST");
810 p = getenv("DXSPIDER_PORT");
813 p = getenv("DXSPIDER_PACLEN");
818 if (paclen > MAXPACLEN)
823 /* get program arguments, initialise stuff */
824 initargs(argc, argv);
825 sel_init(10, 0, 10000);
828 signal(SIGHUP, SIG_IGN);
829 signal(SIGINT, terminate);
830 signal(SIGQUIT, terminate);
831 signal(SIGTERM, terminate);
833 signal(SIGPWR, terminate);
836 /* connect up stdin */
837 in = fcb_new(0, TEXT);
838 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
839 if (tcgetattr(0, &in->t) < 0) {
844 struct termios t = in->t;
845 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
847 if (tcsetattr(0, TCSANOW, &t) < 0)
848 die("tcsetattr (%d)", errno);
854 /* connect up node */
857 /* is this a login? */
858 if (eq(call, "LOGIN") || eq(call, "login")) {
860 char buf[MAXPACLEN+1];
862 FILE *f = xopen("data", "issue", "r");
864 while (fgets(buf, paclen, f)) {
866 if (i && buf[i-1] == '\n')
868 send_text(in, buf, i, 1);
872 signal(SIGALRM, login_timeout);
874 send_text(in, "login: ", 7, 0);
878 /* check the callsign against the regexes */
879 if (!iscallsign(call)) {
880 die("Sorry, %s isn't a valid callsign", call);
883 /* tell the cluster who I am */
884 send_msg(node, 'A', connsort, strlen(connsort));
890 /* main processing loop */
891 while (ending == 0) {