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 * send a file out to the user
306 void send_file(char *name)
309 char buf[MAXPACLEN+1];
311 FILE *f = xopen("data", name, "r");
313 while (fgets(buf, paclen, f)) {
315 if (i && buf[i-1] == '\n')
317 send_text(in, buf, i, 1);
324 * the callback (called by sel_run) that handles all the inputs and outputs
327 int fcb_handler(sel_t *sp, int in, int out, int err)
334 if (ending == 0 && in) {
335 char *p, buf[MAXBUFL];
338 /* read what we have into a buffer */
339 r = read(f->cnum, buf, MAXBUFL);
347 dbg(DBUF,"got errno %d in input", errno);
352 dbg(DBUF, "ending normally");
357 dbgdump(DBUF, "in ->", buf, r);
359 /* create a new message buffer if required */
361 f->in = cmsg_new(MAXBUFL+1, f->sort, f);
368 omp = cmsg_new(3*r+1, f->sort, f);
369 while (r > 0 && p < &buf[r]) {
371 /* echo processing */
376 strcpy(omp->inp, "\b \b");
377 omp->inp += strlen(omp->inp);
382 strcpy(omp->inp, "\r\n");
390 /* character processing */
394 memset(mp->inp, ' ', tabsize);
403 if (mp->inp > mp->data)
408 if (nl == '\n' && *p == '\r') { /* ignore \r in telnet mode (ugh) */
410 } else if (nl == '\r' && *p == '\n') { /* and ignore \n in ax25 mode (double ugh) */
412 } else if (*p == nl) {
413 if (mp->inp == mp->data)
415 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
416 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
417 cmsg_send(f->inq, mp, 0);
418 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
421 if (mp->inp < &mp->data[MAXBUFL-8])
430 /* queue any echo text */
432 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
433 cmsg_send(f->outq, omp, 0);
434 f->sp->flags |= SEL_OUTPUT;
441 while (r > 0 && p < &buf[r]) {
444 if (mp->inp >= mp->data + (MAXBUFL-1)) {
447 dbg(DMSG, "Message longer than %d received", MAXBUFL);
455 } else if (ch == '\n') {
456 /* kick it upstairs */
458 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
459 cmsg_send(f->inq, mp, 0);
460 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
461 } else if (ch < 0x20 || ch > 0x7e) {
462 dbg(DMSG, "Illegal character (0x%02X) received", *p);
471 if (ch >= '0' && ch <= '9')
473 else if (ch >= 'A' && ch <= 'F')
474 c = (ch - 'A' + 10) << 4;
476 dbg(DMSG, "Illegal hex char (%c) received in state 1", ch);
483 if (ch >= '0' && ch <= '9')
484 *mp->inp++ = c | (ch - '0');
485 else if (ch >= 'A' && ch <= 'F')
486 *mp->inp++ = c | (ch - 'A' + 10);
488 dbg(DMSG, "Illegal hex char (%c) received in state 2", ch);
497 die("invalid sort (%d) in input handler", f->sort);
507 mp = f->out = cmsg_next(f->outq);
509 sp->flags &= ~SEL_OUTPUT;
514 l = mp->size - (mp->inp - mp->data);
517 dbgdump(DBUF, "<-out", mp->inp, l);
519 r = write(f->cnum, mp->inp, l);
527 dbg(DBUF,"got errno %d in output", errno);
535 die("got negative length in handler on node");
536 if (mp->inp - mp->data >= mp->size) {
537 cmsg_callback(mp, 0);
546 * set up the various mode flags, NL endings and things
548 void setmode(char *m)
550 connsort = strlower(m);
551 if (eq(connsort, "telnet") || eq(connsort, "local") || eq(connsort, "nlonly")) {
554 mode = eq(connsort, "nlonly") ? 2 : 1;
555 } else if (eq(connsort, "ax25")) {
559 } else if (eq(connsort, "connect")) {
564 die("Connection type must be \"telnet\", \"nlonly\", \"ax25\", \"login\" or \"local\"");
570 * things to do with ongoing processing of inputs
575 cmsg_t *mp = cmsg_next(in->inq);
576 char *p, hasa, hasn, i;
577 char callsign[MAXCALLSIGN+1];
580 dbg(DMSG, "MSG size: %d", mp->size);
585 send_msg(node, 'I', mp->data, mp->size);
589 for (i = 0; i < mp->size; ++i) {
591 if (i < MAXCALLSIGN) {
596 if (isalnum(ch) || ch == '-')
599 die("invalid callsign");
601 die("invalid callsign");
604 if (strlen(callsign) < 3)
605 die("invalid callsign");
609 die("invalid callsign");
610 call = strupper(callsign);
612 /* check the callsign against the regexes */
613 if (!iscallsign(call)) {
614 die("Sorry, %s isn't a valid callsign", call);
618 signal(SIGALRM, SIG_IGN);
620 /* tell the cluster who I am */
621 send_msg(node, 'A', connsort, strlen(connsort));
624 send_file("connected");
627 cmsg_callback(mp, 0);
634 cmsg_t *mp = cmsg_next(node->inq);
636 dbg(DMSG, "MSG size: %d", mp->size);
638 if (mp->size > 0 && mp->inp > mp->data) {
639 char *p = strchr(mp->data, '|');
642 switch (mp->data[0]) {
652 in->buffer_it = *p - '0';
656 int l = mp->inp - (UC *) p;
657 send_text(in, p, l, 1);
664 cmsg_callback(mp, 0);
671 * things to do with going away
674 void term_timeout(int i)
676 /* none of this is going to be reused so don't bother cleaning up properly */
678 tcsetattr(0, TCSANOW, &in->t);
680 shutdown(node->cnum, 3);
686 void terminate(int i)
688 signal(SIGALRM, term_timeout);
691 while ((in && !is_chain_empty(in->outq)) ||
692 (node && !is_chain_empty(node->outq))) {
696 tcsetattr(0, TCSADRAIN, &in->t);
698 shutdown(node->cnum, 3);
704 void login_timeout(int i)
706 write(0, "Timed Out", 10);
708 sel_run(); /* force a coordination */
710 tcsetattr(0, TCSANOW, &in->t);
715 * things to do with initialisation
718 void initargs(int argc, char *argv[])
722 while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
728 paclen = atoi(optarg);
731 if (paclen > MAXPACLEN)
735 node_port = atoi(optarg);
739 dbgset(atoi(optarg));
749 die("usage: client [-x n|-h<host>|-p<port>|-l<paclen>] <call>|login [local|telnet|ax25]");
753 call = strupper(argv[optind]);
757 die("Must have at least a callsign (for now)");
760 setmode(argv[optind]);
765 /* this is kludgy, but hey so is the rest of this! */
766 if (mode != 0 && paclen == DEFPACLEN) {
771 void connect_to_node()
773 struct hostent *hp, *gethostbyname();
774 struct sockaddr_in server;
778 if ((hp = gethostbyname(node_addr)) == 0)
779 die("Unknown host tcp host %s for printer", node_addr);
781 memset(&server, 0, sizeof server);
782 server.sin_family = AF_INET;
783 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
784 server.sin_port = htons(node_port);
786 nodef = socket(AF_INET, SOCK_STREAM, 0);
788 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
790 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
791 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
793 node = fcb_new(nodef, MSG);
794 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
799 * the program itself....
802 main(int argc, char *argv[])
804 /* compile regexes for iscallsign */
807 for (rp = iscallreg; rp->in; ++rp) {
809 int r = regcomp(®, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
811 die("regcomp returned %d for '%s'", r, rp->in);
812 rp->regex = malloc(sizeof(regex_t));
814 die("out of room - compiling regexes");
819 /* set up environment */
821 char *p = getenv("DXSPIDER_ROOT");
824 p = getenv("DXSPIDER_HOST");
827 p = getenv("DXSPIDER_PORT");
830 p = getenv("DXSPIDER_PACLEN");
835 if (paclen > MAXPACLEN)
840 /* get program arguments, initialise stuff */
841 initargs(argc, argv);
842 sel_init(10, 0, 10000);
845 signal(SIGHUP, SIG_IGN);
846 signal(SIGINT, terminate);
847 signal(SIGQUIT, terminate);
848 signal(SIGTERM, terminate);
850 signal(SIGPWR, terminate);
853 /* connect up stdin */
854 in = fcb_new(0, TEXT);
855 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
856 if (tcgetattr(0, &in->t) < 0) {
861 struct termios t = in->t;
862 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
864 if (tcsetattr(0, TCSANOW, &t) < 0)
865 die("tcsetattr (%d)", errno);
871 /* connect up node */
874 /* is this a login? */
875 if (eq(call, "LOGIN") || eq(call, "login")) {
877 signal(SIGALRM, login_timeout);
879 send_text(in, "login: ", 7, 0);
883 /* check the callsign against the regexes */
884 if (!iscallsign(call)) {
885 die("Sorry, %s isn't a valid callsign", call);
888 /* tell the cluster who I am */
889 send_msg(node, 'A', connsort, strlen(connsort));
892 send_file("connected");
896 /* main processing loop */
897 while (ending == 0) {