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 int cnum; /* the connection number */
56 int sort; /* the type of connection either text or msg */
57 cmsg_t *in; /* current input message being built up */
58 cmsg_t *out; /* current output message being sent */
59 cmsg_t *obuf; /* current output being buffered */
60 reft *inq; /* input queue */
61 reft *outq; /* output queue */
62 sel_t *sp; /* my select fcb address */
63 struct termios t; /* any termios associated with this cnum */
64 char echo; /* echo characters back to this cnum */
65 char t_set; /* the termios structure is valid */
66 char buffer_it; /* buffer outgoing packets for paclen */
76 char *node_addr = "localhost"; /* the node tcp address, can be overridden by DXSPIDER_HOST */
77 int node_port = 27754; /* the tcp port of the node at the above address can be overidden by DXSPIDER_PORT*/
78 char *call; /* the caller's callsign */
79 char *connsort; /* the type of connection */
80 fcb_t *in; /* the fcb of 'stdin' that I shall use */
81 fcb_t *node; /* the fcb of the msg system */
82 char nl = '\n'; /* line end character */
83 char ending = 0; /* set this to end the program */
84 char send_Z = 1; /* set a Z record to the node on termination */
85 char echo = 1; /* echo characters on stdout from stdin */
86 char int_tabs = 0; /* interpret tabs -> spaces */
87 char *root = "/spider"; /* root of data tree, can be overridden by DXSPIDER_ROOT */
88 int timeout = 60; /* default timeout for logins and things */
89 int paclen = DEFPACLEN; /* default buffer size for outgoing packets */
90 int tabsize = 8; /* default tabsize for text messages */
92 myregex_t iscallreg[] = { /* regexes to determine whether this is a reasonable callsign */
94 "^[A-Z]+[0-9]+[A-Z]+", 0
97 "^[0-9]+[A-Z]+[0-9]+[A-Z]+", 0
107 * utility routines - various
110 void die(char *s, ...)
116 vsnprintf(buf, sizeof(buf)-1, s, ap);
118 fprintf(stderr,"%s\n", buf);
122 char *strupper(char *s)
124 char *d = malloc(strlen(s)+1);
128 die("out of room in strupper");
129 while (*p++ = toupper(*s++)) ;
133 char *strlower(char *s)
135 char *d = malloc(strlen(s)+1);
139 die("out of room in strlower");
140 while (*p++ = tolower(*s++)) ;
144 int eq(char *a, char *b)
146 return (strcmp(a, b) == 0);
149 int xopen(char *dir, char *name, int mode)
151 char fn[MAXPATHLEN+1];
152 snprintf(fn, MAXPATHLEN, "%s/%s/%s", root, dir, name);
153 return open(fn, mode);
156 int iscallsign(char *s)
159 for (rp = iscallreg; rp->in; ++rp) {
160 if (regexec(rp->regex, s, 0, 0, 0) == 0)
167 * higher level send and receive routines
170 fcb_t *fcb_new(int cnum, int sort)
172 fcb_t *f = malloc(sizeof(fcb_t));
174 die("no room in fcb_new");
175 memset (f, 0, sizeof(fcb_t));
178 f->inq = chain_new();
179 f->outq = chain_new();
183 void flush_text(fcb_t *f)
186 cmsg_send(f->outq, f->obuf, 0);
187 f->sp->flags |= SEL_OUTPUT;
192 void send_text(fcb_t *f, char *s, int l)
197 if (f->buffer_it && f->obuf) {
200 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
203 for (p = s; p < s+l; ) {
204 if (mp->inp >= mp->data + paclen) {
206 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
210 if (mp->inp >= mp->data + paclen) {
212 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
219 void send_msg(fcb_t *f, char let, char *s, int l)
223 int myl = strlen(call)+2+l;
225 mp = cmsg_new(myl+4+1, f->sort, f);
227 memcpy(mp->inp, &ln, 4);
230 strcpy(mp->inp, call);
231 mp->inp += strlen(call);
234 memcpy(mp->inp, s, l);
238 cmsg_send(f->outq, mp, 0);
239 f->sp->flags |= SEL_OUTPUT;
243 * the callback (called by sel_run) that handles all the inputs and outputs
246 int fcb_handler(sel_t *sp, int in, int out, int err)
253 char *p, buf[MAXBUFL];
256 /* read what we have into a buffer */
257 r = read(f->cnum, buf, MAXBUFL);
277 dbgdump(DBUF, "in ->", buf, r);
279 /* create a new message buffer if required */
281 f->in = cmsg_new(MAXBUFL+1, f->sort, f);
288 omp = cmsg_new(3*r+1, f->sort, f);
289 while (r > 0 && p < &buf[r]) {
291 /* echo processing */
296 strcpy(omp->inp, "\b \b");
297 omp->inp += strlen(omp->inp);
304 /* character processing */
308 memset(mp->inp, ' ', tabsize);
317 if (mp->inp > mp->data)
322 if (nl == '\n' && *p == '\r') { /* ignore \r in telnet mode (ugh) */
324 } else if (*p == nl) {
325 if (mp->inp == mp->data)
327 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
328 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
329 cmsg_send(f->inq, mp, 0);
330 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
333 if (mp->inp < &mp->data[MAXBUFL-8])
342 /* queue any echo text */
344 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
345 cmsg_send(f->outq, omp, 0);
346 f->sp->flags |= SEL_OUTPUT;
353 while (r > 0 && p < &buf[r]) {
355 /* build up the size into the likely message length (yes I know it's a short) */
363 mp->size = (mp->size << 8) | (*p++ & 0xff);
364 if (mp->size > MAXBUFL)
365 die("Message size too big from node (%d > %d)", mp->size, MAXBUFL);
369 if (mp->inp - mp->data < mp->size) {
372 if (mp->inp - mp->data >= mp->size) {
373 /* kick it upstairs */
374 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
375 cmsg_send(f->inq, mp, 0);
376 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
383 die("invalid sort (%d) in input handler", f->sort);
393 mp = f->out = cmsg_next(f->outq);
395 sp->flags &= ~SEL_OUTPUT;
400 l = mp->size - (mp->inp - mp->data);
403 dbgdump(DBUF, "<-out", mp->inp, l);
405 r = write(f->cnum, mp->inp, l);
422 die("got negative length in handler on node");
423 if (mp->inp - mp->data >= mp->size) {
424 cmsg_callback(mp, 0);
433 * things to do with initialisation
436 void initargs(int argc, char *argv[])
440 while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
446 paclen = atoi(optarg);
449 if (paclen > MAXPACLEN)
453 node_port = atoi(optarg);
457 dbgset(atoi(optarg));
467 die("usage: client [-x n|-h<host>|-p<port>|-l<paclen>] <call>|login [local|telnet|ax25]");
471 call = strupper(argv[optind]);
475 die("Must have at least a callsign (for now)");
478 connsort = strlower(argv[optind]);
479 if (eq(connsort, "telnet") || eq(connsort, "local")) {
482 } else if (eq(connsort, "ax25")) {
486 die("2nd argument must be \"telnet\" or \"ax25\" or \"local\"");
494 /* this is kludgy, but hey so is the rest of this! */
495 if (!eq(connsort, "ax25") && paclen == DEFPACLEN) {
500 void connect_to_node()
502 struct hostent *hp, *gethostbyname();
503 struct sockaddr_in server;
507 if ((hp = gethostbyname(node_addr)) == 0)
508 die("Unknown host tcp host %s for printer", node_addr);
510 memset(&server, 0, sizeof server);
511 server.sin_family = AF_INET;
512 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
513 server.sin_port = htons(node_port);
515 nodef = socket(AF_INET, SOCK_STREAM, 0);
517 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
519 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
520 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
522 node = fcb_new(nodef, MSG);
523 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
528 * things to do with going away
531 void term_timeout(int i)
533 /* none of this is going to be reused so don't bother cleaning up properly */
535 tcsetattr(0, TCSANOW, &in->t);
542 void terminate(int i)
544 if (node && send_Z && call) {
545 send_msg(node, 'Z', "", 0);
548 signal(SIGALRM, term_timeout);
551 while ((in && !is_chain_empty(in->outq)) ||
552 (node && !is_chain_empty(node->outq))) {
556 tcsetattr(0, TCSADRAIN, &in->t);
562 void login_timeout(int i)
564 write(0, "Timed Out", 10);
566 sel_run(); /* force a coordination */
568 tcsetattr(0, TCSANOW, &in->t);
573 * things to do with ongoing processing of inputs
578 cmsg_t *mp = cmsg_next(in->inq);
580 dbg(DMSG, "MSG size: %d", mp->size);
582 if (mp->size > 0 && mp->inp > mp->data) {
583 send_msg(node, 'I', mp->data, mp->size);
585 cmsg_callback(mp, 0);
591 cmsg_t *mp = cmsg_next(node->inq);
593 dbg(DMSG, "MSG size: %d", mp->size);
595 if (mp->size > 0 && mp->inp > mp->data) {
596 char *p = strchr(mp->data, '|');
599 switch (mp->data[0]) {
610 in->buffer_it = *p - '0';
614 int l = mp->inp - (unsigned char *) p;
622 cmsg_callback(mp, 0);
629 * the program itself....
632 main(int argc, char *argv[])
634 /* set up environment */
636 char *p = getenv("DXSPIDER_ROOT");
639 p = getenv("DXSPIDER_HOST");
642 p = getenv("DXSPIDER_PORT");
645 p = getenv("DXSPIDER_PACLEN");
650 if (paclen > MAXPACLEN)
655 /* get program arguments, initialise stuff */
656 initargs(argc, argv);
657 sel_init(10, 0, 10000);
660 signal(SIGHUP, SIG_IGN);
661 signal(SIGINT, terminate);
662 signal(SIGQUIT, terminate);
663 signal(SIGTERM, terminate);
665 signal(SIGPWR, terminate);
668 /* compile regexes for iscallsign */
671 for (rp = iscallreg; rp->in; ++rp) {
673 int r = regcomp(®, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
675 die("regcomp returned %d for '%s'", r, rp->in);
676 rp->regex = malloc(sizeof(regex_t));
678 die("out of room - compiling regexes");
683 /* is this a login? */
684 if (eq(call, "LOGIN")) {
685 char buf[MAXPACLEN+1];
687 int f = xopen("data", "issue", 0);
689 while ((r = read(f, buf, paclen)) > 0) {
692 for (p = buf; p < &buf[r]; ++p) {
701 signal(SIGALRM, login_timeout);
703 write(0, "login: ", 7);
704 r = read(0, buf, 20);
706 die("No login or error (%d)", errno);
707 signal(SIGALRM, SIG_IGN);
710 if (buf[r-1] == ' ' || buf[r-1] == '\r' || buf[r-1] == '\n')
716 call = strupper(buf);
717 if (!iscallsign(call)) {
718 die("Sorry, %s isn't a valid callsign", buf);
722 /* connect up stdin */
723 in = fcb_new(0, TEXT);
724 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
725 if (tcgetattr(0, &in->t) < 0) {
729 struct termios t = in->t;
730 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
731 if (tcsetattr(0, TCSANOW, &t) < 0)
732 die("tcsetattr (%d)", errno);
738 /* connect up node */
741 /* tell the cluster who I am */
742 send_msg(node, 'A', connsort, strlen(connsort));
744 /* main processing loop */