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>
47 int cnum; /* the connection number */
48 int sort; /* the type of connection either text or msg */
49 cmsg_t *in; /* current input message being built up */
50 cmsg_t *out; /* current output message being sent */
51 reft *inq; /* input queue */
52 reft *outq; /* output queue */
53 sel_t *sp; /* my select fcb address */
54 struct termios t; /* any termios associated with this cnum */
55 char echo; /* echo characters back to this cnum */
56 char t_set; /* the termios structure is valid */
59 char *node_addr = "localhost"; /* the node tcp address */
60 int node_port = 27754; /* the tcp port of the node at the above address */
61 char *call; /* the caller's callsign */
62 char *connsort; /* the type of connection */
63 fcb_t *in; /* the fcb of 'stdin' that I shall use */
64 fcb_t *node; /* the fcb of the msg system */
65 char nl = '\n'; /* line end character */
66 char ending = 0; /* set this to end the program */
67 char send_Z = 1; /* set a Z record to the node on termination */
68 char echo = 1; /* echo characters on stdout from stdin */
73 * utility routines - various
76 void die(char *s, ...)
84 fprintf(stderr,"%s\n", buf);
88 char *strupper(char *s)
90 char *d = malloc(strlen(s)+1);
94 die("out of room in strupper");
95 while (*p++ = toupper(*s++)) ;
99 char *strlower(char *s)
101 char *d = malloc(strlen(s)+1);
105 die("out of room in strlower");
106 while (*p++ = tolower(*s++)) ;
110 int eq(char *a, char *b)
112 return (strcmp(a, b) == 0);
116 * higher level send and receive routines
119 fcb_t *fcb_new(int cnum, int sort)
121 fcb_t *f = malloc(sizeof(fcb_t));
123 die("no room in fcb_new");
124 memset (f, 0, sizeof(fcb_t));
127 f->inq = chain_new();
128 f->outq = chain_new();
132 void send_text(fcb_t *f, char *s, int l)
135 mp = cmsg_new(l+1, f->sort, f);
136 memcpy(mp->inp, s, l);
139 cmsg_send(f->outq, mp, 0);
140 f->sp->flags |= SEL_OUTPUT;
143 void send_msg(fcb_t *f, char let, char *s, int l)
147 int myl = strlen(call)+2+l;
149 mp = cmsg_new(myl+4, f->sort, f);
151 memcpy(mp->inp, &ln, 4);
154 strcpy(mp->inp, call);
155 mp->inp += strlen(call);
158 memcpy(mp->inp, s, l);
162 cmsg_send(f->outq, mp, 0);
163 f->sp->flags |= SEL_OUTPUT;
166 int fcb_handler(sel_t *sp, int in, int out, int err)
173 char *p, buf[MAXBUFL];
176 /* read what we have into a buffer */
177 r = read(f->cnum, buf, MAXBUFL);
197 dbgdump(DBUF, "in ->", buf, r);
199 /* create a new message buffer if required */
201 f->in = cmsg_new(MAXBUFL, f->sort, f);
208 omp = cmsg_new(3*r, f->sort, f);
209 while (r > 0 && p < &buf[r]) {
211 /* echo processing */
216 strcpy(omp->inp, "\b \b");
217 omp->inp += strlen(omp->inp);
224 /* character processing */
228 if (mp->inp > mp->data)
233 if (nl == '\n' && *p == '\r') {
235 } else if (*p == nl) {
236 if (mp->inp == mp->data)
238 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
239 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
240 cmsg_send(f->inq, mp, 0);
241 f->in = mp = cmsg_new(MAXBUFL, f->sort, f);
244 if (mp->inp < &mp->data[MAXBUFL])
253 /* queue any echo text */
255 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
256 cmsg_send(f->outq, omp, 0);
257 f->sp->flags |= SEL_OUTPUT;
264 while (r > 0 && p < &buf[r]) {
266 /* build up the size into the likely message length (yes I know it's a short) */
274 mp->size = (mp->size << 8) | (*p++ & 0xff);
275 if (mp->size > MAXBUFL)
276 die("Message size too big from node (%d > %d)", mp->size, MAXBUFL);
280 if (mp->inp - mp->data < mp->size) {
283 if (mp->inp - mp->data >= mp->size) {
284 /* kick it upstairs */
285 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
286 cmsg_send(f->inq, mp, 0);
287 mp = f->in = cmsg_new(MAXBUFL, f->sort, f);
294 die("invalid sort (%d) in input handler", f->sort);
304 mp = f->out = cmsg_next(f->outq);
306 sp->flags &= ~SEL_OUTPUT;
311 l = mp->size - (mp->inp - mp->data);
314 dbgdump(DBUF, "<-out", mp->inp, l);
316 r = write(f->cnum, mp->inp, l);
333 die("got negative length in handler on node");
334 if (mp->inp - mp->data >= mp->size) {
335 cmsg_callback(mp, 0);
337 /* if (is_chain_empty(f->outq))
338 sp->flags &= ~SEL_OUTPUT; */
346 * things to do with initialisation
349 void initargs(int argc, char *argv[])
353 while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
359 node_port = atoi(optarg);
363 dbgset(atoi(optarg));
373 die("usage: client [-x nn] <call>|login [local|telnet|ax25]");
377 call = strupper(argv[optind]);
378 if (eq(call, "LOGIN"))
379 die("login not implemented (yet)");
383 die("Must have at least a callsign (for now)");
386 connsort = strlower(argv[optind]);
387 if (eq(connsort, "telnet") || eq(connsort, "local")) {
390 } else if (eq(connsort, "ax25")) {
394 die("2nd argument must be \"telnet\" or \"ax25\" or \"local\"");
403 void connect_to_node()
405 struct hostent *hp, *gethostbyname();
406 struct sockaddr_in server;
410 if ((hp = gethostbyname(node_addr)) == 0)
411 die("Unknown host tcp host %s for printer", node_addr);
413 memset(&server, 0, sizeof server);
414 server.sin_family = AF_INET;
415 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
416 server.sin_port = htons(node_port);
418 nodef = socket(AF_INET, SOCK_STREAM, 0);
420 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
422 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
423 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
425 node = fcb_new(nodef, MSG);
426 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
431 * things to do with going away
434 void term_timeout(int i)
436 /* none of this is going to be reused so don't bother cleaning up properly */
438 tcsetattr(0, TCSANOW, &in->t);
445 void terminate(int i)
447 if (send_Z && call) {
448 send_msg(node, 'Z', "", 0);
451 signal(SIGALRM, term_timeout);
454 while ((in && !is_chain_empty(in->outq)) ||
455 (node && !is_chain_empty(node->outq))) {
459 tcsetattr(0, TCSANOW, &in->t);
466 * things to do with ongoing processing of inputs
471 cmsg_t *mp = cmsg_next(in->inq);
473 dbg(DMSG, "MSG size: %d", mp->size);
475 if (mp->size > 0 && mp->inp > mp->data) {
476 send_msg(node, 'I', mp->data, mp->size);
478 cmsg_callback(mp, 0);
484 cmsg_t *mp = cmsg_next(node->inq);
486 dbg(DMSG, "MSG size: %d", mp->size);
488 if (mp->size > 0 && mp->inp > mp->data) {
489 char *p = strchr(mp->data, '|');
492 switch (mp->data[0]) {
503 int l = mp->inp - (unsigned char *) p;
504 if (nl == '\n' && l >= 1 && p[l-1] == '\r') /* kludge for GB7DXM */
513 cmsg_callback(mp, 0);
518 * the program itself....
521 main(int argc, char *argv[])
523 initargs(argc, argv);
524 sel_init(10, 0, 10000);
526 signal(SIGHUP, SIG_IGN);
528 signal(SIGINT, terminate);
529 signal(SIGQUIT, terminate);
530 signal(SIGTERM, terminate);
532 signal(SIGPWR, terminate);
535 /* connect up stdin, stdout and message system */
536 in = fcb_new(0, TEXT);
537 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
538 if (tcgetattr(0, &in->t) < 0) {
542 struct termios t = in->t;
543 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
544 if (tcsetattr(0, TCSANOW, &t) < 0)
545 die("tcsetattr (%d)", errno);
551 /* tell the cluster who I am */
552 send_msg(node, 'A', connsort, strlen(connsort));
554 /* main processing loop */