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
19 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
46 #define MAXPATHLEN 256
57 #define UC unsigned char
61 int cnum; /* the connection number */
62 int sort; /* the type of connection either text or msg */
63 cmsg_t *in; /* current input message being built up */
64 cmsg_t *out; /* current output message being sent */
65 cmsg_t *obuf; /* current output being buffered */
66 reft *inq; /* input queue */
67 reft *outq; /* output queue */
68 sel_t *sp; /* my select fcb address */
69 struct termios t; /* any termios associated with this cnum */
70 char echo; /* echo characters back to this cnum */
71 char t_set; /* the termios structure is valid */
72 char buffer_it; /* buffer outgoing packets for paclen */
82 char *node_addr = "127.0.0.1"; /* the node tcp address, can be overridden by DXSPIDER_HOST */
83 int node_port = 27754; /* the tcp port of the node at the above address can be overidden by DXSPIDER_PORT*/
84 char *call; /* the caller's callsign */
85 char *connsort; /* the type of connection */
86 fcb_t *in; /* the fcb of 'stdin' that I shall use */
87 fcb_t *node; /* the fcb of the msg system */
88 char nl = '\n'; /* line end character */
89 char mode = 1; /* 0 - ax25, 1 - normal telnet, 2 - nlonly telnet */
90 char ending = 0; /* set this to end the program */
91 char echo = 1; /* echo characters on stdout from stdin */
92 char int_tabs = 0; /* interpret tabs -> spaces */
93 char *root = "/spider"; /* root of data tree, can be overridden by DXSPIDER_ROOT */
94 int timeout = 60; /* default timeout for logins and things */
95 int paclen = DEFPACLEN; /* default buffer size for outgoing packets */
96 int tabsize = 8; /* default tabsize for text messages */
97 char *connsort = "local"; /* the connection variety */
98 int state = 0; /* the current state of the connection */
99 int laststate = 0; /* the last state we were in */
100 char echocancel = 1; /* echo cancelling */
101 reft echobase; /* the anti echo queue */
102 int maxecho = 5; /* the depth of the anti echo queue */
103 int echon; /* no of entries in the anti echo queue */
105 #define CONNECTED 100
111 myregex_t iscallreg[] = { /* regexes to determine whether this is a reasonable callsign */
113 "^[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* G1TLH G1TLH1 */
116 "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* 2E0AAA 2E0AAA1 */
119 "^[A-Z]+[0-9]+[A-Z]+-[0-9]$", 0 /* G1TLH-2 */
122 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-[0-9]$", 0 /* 2E0AAA-2 */
125 "^[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* G1TLH-11 */
128 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* 2E0AAA-11 */
138 * utility routines - various
141 void chgstate(int new)
145 dbg(DSTS, "chg state %d->%d", laststate, state);
148 void die(char *s, ...)
154 vsnprintf(buf, sizeof(buf)-1, s, ap);
156 fprintf(stderr,"%s\n", buf);
160 char *strupper(char *s)
162 char *d = malloc(strlen(s)+1);
166 die("out of room in strupper");
167 while (*p++ = toupper(*s++)) ;
171 char *strlower(char *s)
173 char *d = malloc(strlen(s)+1);
177 die("out of room in strlower");
178 while (*p++ = tolower(*s++)) ;
182 int eq(char *a, char *b)
184 return (strcmp(a, b) == 0);
187 FILE *xopen(char *dir, char *name, char *mode)
189 char fn[MAXPATHLEN+1];
190 snprintf(fn, MAXPATHLEN, "%s/%s/%s", root, dir, name);
191 return fopen(fn, mode);
194 int iscallsign(char *s)
198 if (strlen(s) > MAXCALLSIGN)
201 for (rp = iscallreg; rp->in; ++rp) {
202 if (regexec(rp->regex, s, 0, 0, 0) == 0)
211 while ((mypid = waitpid(-1, 0, WNOHANG)) > 0) {
217 * higher level send and receive routines
220 fcb_t *fcb_new(int cnum, int sort)
222 fcb_t *f = malloc(sizeof(fcb_t));
224 die("no room in fcb_new");
225 memset (f, 0, sizeof(fcb_t));
228 f->inq = chain_new();
229 f->outq = chain_new();
233 void flush_text(fcb_t *f)
236 /* save this onto the front of the echo chain */
237 cmsg_t *imp = f->obuf;
238 int size = imp->inp - imp->data;
239 cmsg_t *emp = cmsg_new(size, imp->sort, imp->portp);
242 memcpy(emp->data, imp->data, size);
243 emp->inp = emp->data + size; /* just in case */
245 chain_add(&echobase, emp);
246 if (++echon > maxecho) {
247 emp = cmsg_prev(&echobase);
252 /* queue it for sending */
253 cmsg_send(f->outq, imp, 0);
254 f->sp->flags |= SEL_OUTPUT;
259 void send_text(fcb_t *f, char *s, int l, int nlreq)
264 if (f->buffer_it && f->obuf) {
267 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
270 /* remove trailing spaces */
271 while (l > 0 &&isspace(s[l-1]))
274 for (p = s; p < s+l; ) {
275 if (mp->inp >= mp->data + paclen) {
277 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
281 if (mp->inp >= mp->data + paclen) {
283 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
298 void send_msg(fcb_t *f, char let, UC *s, int l)
302 int myl = strlen(call)+2+l;
304 mp = cmsg_new(myl+4+1, f->sort, f);
306 strcpy(mp->inp, call);
307 mp->inp += strlen(call);
311 for (p = s; p < s+l; ++p) {
312 if (mp->inp >= mp->data + (myl - 4)) {
313 int off = mp->inp - mp->data;
315 mp = realloc(mp, myl);
316 mp->inp = mp->data + off;
319 if (*p < 0x20 || *p > 0x7e || *p == '%') {
320 sprintf(mp->inp, "%%%02X", *p & 0xff);
321 mp->inp += strlen(mp->inp);
328 cmsg_send(f->outq, mp, 0);
329 f->sp->flags |= SEL_OUTPUT;
333 * send a file out to the user
335 void send_file(char *name)
338 char buf[MAXPACLEN+1];
340 FILE *f = xopen("data", name, "r");
342 while (fgets(buf, paclen, f)) {
344 if (i && buf[i-1] == '\n')
346 send_text(in, buf, i, 1);
353 * the callback (called by sel_run) that handles all the inputs and outputs
356 int fcb_handler(sel_t *sp, int in, int out, int err)
363 if (ending == 0 && in) {
364 char *p, buf[MAXBUFL];
367 /* read what we have into a buffer */
368 r = read(f->cnum, buf, MAXBUFL);
376 dbg(DBUF,"got errno %d in input", errno);
381 dbg(DBUF, "ending normally");
386 dbgdump(DBUF, "in ->", buf, r);
388 /* create a new message buffer if required */
390 f->in = cmsg_new(MAXBUFL+1, f->sort, f);
397 omp = cmsg_new(3*r+1, f->sort, f);
398 while (r > 0 && p < &buf[r]) {
400 /* echo processing */
405 strcpy(omp->inp, "\b \b");
406 omp->inp += strlen(omp->inp);
411 strcpy(omp->inp, "\r\n");
419 /* character processing */
423 memset(mp->inp, ' ', tabsize);
432 if (mp->inp > mp->data)
437 if (nl == '\n' && *p == '\r') { /* ignore \r in telnet mode (ugh) */
439 } else if (nl == '\r' && *p == '\n') { /* and ignore \n in ax25 mode (double ugh) */
441 } else if (*p == nl) {
442 if (mp->inp == mp->data)
444 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
445 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
446 cmsg_send(f->inq, mp, 0);
447 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
450 if (mp->inp < &mp->data[MAXBUFL-8])
459 /* queue any echo text */
461 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
462 cmsg_send(f->outq, omp, 0);
463 f->sp->flags |= SEL_OUTPUT;
470 while (r > 0 && p < &buf[r]) {
473 if (mp->inp >= mp->data + (MAXBUFL-1)) {
476 dbg(DMSG, "Message longer than %d received", MAXBUFL);
484 } else if (ch == '\n') {
485 /* kick it upstairs */
487 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
488 cmsg_send(f->inq, mp, 0);
489 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
490 } else if (ch < 0x20 || ch > 0x7e) {
491 dbg(DMSG, "Illegal character (0x%02X) received", *p);
500 if (ch >= '0' && ch <= '9')
502 else if (ch >= 'A' && ch <= 'F')
503 c = (ch - 'A' + 10) << 4;
505 dbg(DMSG, "Illegal hex char (%c) received in state 1", ch);
512 if (ch >= '0' && ch <= '9')
513 *mp->inp++ = c | (ch - '0');
514 else if (ch >= 'A' && ch <= 'F')
515 *mp->inp++ = c | (ch - 'A' + 10);
517 dbg(DMSG, "Illegal hex char (%c) received in state 2", ch);
526 die("invalid sort (%d) in input handler", f->sort);
536 mp = f->out = cmsg_next(f->outq);
538 sp->flags &= ~SEL_OUTPUT;
543 l = mp->size - (mp->inp - mp->data);
546 dbgdump(DBUF, "<-out", mp->inp, l);
548 r = write(f->cnum, mp->inp, l);
556 dbg(DBUF,"got errno %d in output", errno);
564 die("got negative length in handler on node");
565 if (mp->inp - mp->data >= mp->size) {
566 cmsg_callback(mp, 0);
575 * set up the various mode flags, NL endings and things
577 void setconntype(char *m)
579 connsort = strlower(m);
580 if (eq(connsort, "telnet") || eq(connsort, "local") || eq(connsort, "nlonly")) {
583 mode = eq(connsort, "nlonly") ? 2 : 1;
584 } else if (eq(connsort, "ax25")) {
588 } else if (eq(connsort, "connect")) {
593 die("Connection type must be \"telnet\", \"nlonly\", \"ax25\", \"login\" or \"local\"");
599 * things to do with ongoing processing of inputs
604 cmsg_t *wmp, *mp = cmsg_next(in->inq);
605 char *p, hasa, hasn, i;
606 char callsign[MAXCALLSIGN+1];
609 dbg(DMSG, "MSG size: %d", mp->size);
611 /* check for echos */
613 for (wmp = 0; wmp = chain_get_next(&echobase, wmp); ) {
614 if (!memcmp(wmp->data, mp->data, wmp->size)) {
615 cmsg_callback(mp, 0);
624 send_msg(node, 'I', mp->data, mp->size);
628 for (i = 0; i < mp->size; ++i) {
630 if (i < MAXCALLSIGN) {
635 if (isalnum(ch) || ch == '-')
638 die("invalid callsign");
640 die("invalid callsign");
643 if (strlen(callsign) < 3)
644 die("invalid callsign");
648 die("invalid callsign");
649 call = strupper(callsign);
651 /* check the callsign against the regexes */
652 if (!iscallsign(call)) {
653 die("Sorry, %s isn't a valid callsign", call);
656 /* strip off a '-0' at the end */
658 if (call[i-1] == '0' && call[i-2] == '-')
662 signal(SIGALRM, SIG_IGN);
664 /* tell the cluster who I am */
665 send_msg(node, 'A', connsort, strlen(connsort));
668 send_file("connected");
676 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]) {
701 in->buffer_it = *p - '0';
705 int l = mp->inp - (UC *) p;
706 send_text(in, p, l, 1);
713 cmsg_callback(mp, 0);
720 * things to do with going away
723 void term_timeout(int i)
725 /* none of this is going to be reused so don't bother cleaning up properly */
727 tcflush(0, TCIOFLUSH);
728 kill(getpid(), 9); /* commit suicide */
731 void terminate(int i)
733 signal(SIGALRM, term_timeout);
736 if (node && node->sp->sort) {
739 while (in && in->sp->sort && !is_chain_empty(in->outq)) {
751 tcsetattr(0, TCSADRAIN, &in->t);
755 void login_timeout(int i)
757 write(0, "Timed Out", 10);
763 * things to do with initialisation
766 void initargs(int argc, char *argv[])
770 while ((c = getopt(argc, argv, "eh:l:p:x:")) > 0) {
779 paclen = atoi(optarg);
782 if (paclen > MAXPACLEN)
786 node_port = atoi(optarg);
790 dbgset(atoi(optarg));
800 die("usage: client [-e|-x n|-h<host>|-p<port>|-l<paclen>] <call>|login [local|telnet|ax25]");
804 call = strupper(argv[optind]);
808 die("Must have at least a callsign (for now)");
811 setconntype(argv[optind]);
813 setconntype("local");
816 /* this is kludgy, but hey so is the rest of this! */
817 if (mode != 0 && paclen == DEFPACLEN) {
822 void connect_to_node()
824 struct hostent *hp, *gethostbyname();
825 struct sockaddr_in server;
831 if ((hp = gethostbyname(node_addr)) == 0)
832 die("Unknown host tcp host %s for printer", node_addr);
834 memset(&server, 0, sizeof server);
835 server.sin_family = AF_INET;
836 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
837 server.sin_port = htons(node_port);
839 nodef = socket(AF_INET, SOCK_STREAM, 0);
841 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
843 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
844 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
847 memset(&lg, 0, sizeof lg);
848 if (setsockopt(nodef, SOL_SOCKET, SO_LINGER, &lg, sizeof lg) < 0) {
849 die("Error on SO_LINGER to %s port %d (%d)", node_addr, node_port, errno);
851 if (setsockopt(nodef, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one) < 0) {
852 die("Error on SO_KEEPALIVE to %s port %d (%d)", node_addr, node_port, errno);
855 node = fcb_new(nodef, MSG);
856 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
861 * the program itself....
864 main(int argc, char *argv[])
866 /* compile regexes for iscallsign */
869 for (rp = iscallreg; rp->in; ++rp) {
871 int r = regcomp(®, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
873 die("regcomp returned %d for '%s'", r, rp->in);
874 rp->regex = malloc(sizeof(regex_t));
876 die("out of room - compiling regexes");
881 /* set up environment */
883 char *p = getenv("DXSPIDER_ROOT");
886 p = getenv("DXSPIDER_HOST");
889 p = getenv("DXSPIDER_PORT");
892 p = getenv("DXSPIDER_PACLEN");
897 if (paclen > MAXPACLEN)
902 /* get program arguments, initialise stuff */
903 initargs(argc, argv);
904 sel_init(10, 0, 10000);
907 signal(SIGHUP, SIG_IGN);
908 signal(SIGINT, terminate);
909 signal(SIGQUIT, terminate);
910 signal(SIGTERM, terminate);
912 signal(SIGPWR, terminate);
915 /* init a few things */
916 chain_init(&echobase);
918 /* connect up stdin */
919 in = fcb_new(0, TEXT);
920 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
921 if (!isatty(0) || tcgetattr(0, &in->t) < 0) {
925 struct termios t = in->t;
926 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
928 if (tcsetattr(0, TCSANOW, &t) < 0)
929 die("tcsetattr (%d)", errno);
935 /* connect up node */
938 /* is this a login? */
939 if (eq(call, "LOGIN") || eq(call, "login")) {
941 signal(SIGALRM, login_timeout);
943 send_text(in, "login: ", 7, 0);
948 /* check the callsign against the regexes */
949 if (!iscallsign(call)) {
950 die("Sorry, %s isn't a valid callsign", call);
953 /* strip off a '-0' at the end */
955 if (call[i-1] == '0' && call[i-2] == '-')
958 /* tell the cluster who I am */
959 send_msg(node, 'A', connsort, strlen(connsort));
962 send_file("connected");
966 /* main processing loop */
967 while (ending == 0) {