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 int echo; /* echo characters back to this cnum */
55 struct termios t; /* any termios associated with this cnum */
58 char *node_addr = "localhost"; /* the node tcp address */
59 int node_port = 27754; /* the tcp port of the node at the above address */
60 char *call; /* the caller's callsign */
61 char *connsort; /* the type of connection */
62 fcb_t *in; /* the fcb of 'stdin' that I shall use */
63 fcb_t *node; /* the fcb of the msg system */
64 char nl = '\n'; /* line end character */
65 char ending = 0; /* set this to end the program */
66 char send_Z = 1; /* set a Z record to the node on termination */
67 char echo = 1; /* echo characters on stdout from stdin */
72 * utility routines - various
75 void die(char *s, ...)
83 fprintf(stderr,"%s\n", buf);
87 char *strupper(char *s)
89 char *d = malloc(strlen(s)+1);
93 die("out of room in strupper");
94 while (*p++ = toupper(*s++)) ;
98 char *strlower(char *s)
100 char *d = malloc(strlen(s)+1);
104 die("out of room in strlower");
105 while (*p++ = tolower(*s++)) ;
109 int eq(char *a, char *b)
111 return (strcmp(a, b) == 0);
115 * higher level send and receive routines
118 fcb_t *fcb_new(int cnum, int sort)
120 fcb_t *f = malloc(sizeof(fcb_t));
122 die("no room in fcb_new");
123 memset (f, 0, sizeof(fcb_t));
126 f->inq = chain_new();
127 f->outq = chain_new();
131 void send_text(fcb_t *f, char *s, int l)
134 mp = cmsg_new(l+1, f->sort, f);
135 memcpy(mp->inp, s, l);
138 cmsg_send(f->outq, mp, 0);
139 f->sp->flags |= SEL_OUTPUT;
142 void send_msg(fcb_t *f, char let, char *s, int l)
146 int myl = strlen(call)+2+l;
148 mp = cmsg_new(myl+4, f->sort, f);
150 memcpy(mp->inp, &ln, 4);
153 strcpy(mp->inp, call);
154 mp->inp += strlen(call);
157 memcpy(mp->inp, s, l);
161 cmsg_send(f->outq, mp, 0);
162 f->sp->flags |= SEL_OUTPUT;
165 int fcb_handler(sel_t *sp, int in, int out, int err)
172 char *p, buf[MAXBUFL];
175 /* read what we have into a buffer */
176 r = read(f->cnum, buf, MAXBUFL);
196 dbgdump(DBUF, "in ->", buf, r);
198 /* create a new message buffer if required */
200 f->in = cmsg_new(MAXBUFL, f->sort, f);
207 omp = cmsg_new(3*r, f->sort, f);
208 while (r > 0 && p < &buf[r]) {
210 /* echo processing */
215 strcpy(omp->inp, "\b \b");
216 omp->inp += strlen(omp->inp);
223 /* character processing */
227 if (mp->inp > mp->data)
233 if (mp->inp == mp->data)
235 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
236 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
237 cmsg_send(f->inq, mp, 0);
238 f->in = mp = cmsg_new(MAXBUFL, f->sort, f);
241 if (mp->inp < &mp->data[MAXBUFL])
250 /* queue any echo text */
252 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
253 cmsg_send(f->outq, omp, 0);
254 f->sp->flags |= SEL_OUTPUT;
261 while (r > 0 && p < &buf[r]) {
263 /* build up the size into the likely message length (yes I know it's a short) */
271 mp->size = (mp->size << 8) | (*p++ & 0xff);
275 if (mp->inp - mp->data < mp->size) {
278 if (mp->inp - mp->data >= mp->size) {
279 /* kick it upstairs */
280 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
281 cmsg_send(f->inq, mp, 0);
282 mp = f->in = cmsg_new(MAXBUFL, f->sort, f);
289 die("invalid sort (%d) in input handler", f->sort);
299 mp = f->out = cmsg_next(f->outq);
301 sp->flags &= ~SEL_OUTPUT;
306 l = mp->size - (mp->inp - mp->data);
309 dbgdump(DBUF, "<-out", mp->inp, l);
311 r = write(f->cnum, mp->inp, l);
328 die("got negative length in handler on node");
329 if (mp->inp - mp->data >= mp->size) {
330 cmsg_callback(mp, 0);
332 /* if (is_chain_empty(f->outq))
333 sp->flags &= ~SEL_OUTPUT; */
341 * things to do with initialisation
344 void initargs(int argc, char *argv[])
348 while ((c = getopt(argc, argv, "x:")) > 0) {
352 dbgset(atoi(optarg));
362 die("usage: client [-x nn] <call>|login [local|telnet|ax25]");
366 call = strupper(argv[optind]);
367 if (eq(call, "LOGIN"))
368 die("login not implemented (yet)");
372 die("Must have at least a callsign (for now)");
375 connsort = strlower(argv[optind]);
376 if (eq(connsort, "telnet") || eq(connsort, "local")) {
379 } else if (eq(connsort, "ax25")) {
383 die("2nd argument must be \"telnet\" or \"ax25\" or \"local\"");
392 void connect_to_node()
394 struct hostent *hp, *gethostbyname();
395 struct sockaddr_in server;
399 if ((hp = gethostbyname(node_addr)) == 0)
400 die("Unknown host tcp host %s for printer", node_addr);
402 memset(&server, 0, sizeof server);
403 server.sin_family = AF_INET;
404 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
405 server.sin_port = htons(node_port);
407 nodef = socket(AF_INET, SOCK_STREAM, 0);
409 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
411 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
412 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
414 node = fcb_new(nodef, MSG);
415 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
420 * things to do with going away
423 void term_timeout(int i)
425 /* none of this is going to be reused so don't bother cleaning up properly */
427 tcsetattr(0, TCSANOW, &in->t);
434 void terminate(int i)
436 if (send_Z && call) {
437 send_msg(node, 'Z', "", 0);
440 signal(SIGALRM, term_timeout);
443 while ((in && !is_chain_empty(in->outq)) ||
444 (node && !is_chain_empty(node->outq))) {
448 tcsetattr(0, TCSANOW, &in->t);
455 * things to do with ongoing processing of inputs
460 cmsg_t *mp = cmsg_next(in->inq);
462 dbg(DMSG, "MSG size: %d", mp->size);
464 if (mp->size > 0 && mp->inp > mp->data) {
465 send_msg(node, 'I', mp->data, mp->size);
467 cmsg_callback(mp, 0);
473 cmsg_t *mp = cmsg_next(node->inq);
475 dbg(DMSG, "MSG size: %d", mp->size);
477 if (mp->size > 0 && mp->inp > mp->data) {
478 char *p = strchr(mp->data, '|');
481 switch (mp->data[0]) {
492 int l = mp->inp - (unsigned char *) p;
500 cmsg_callback(mp, 0);
505 * the program itself....
508 main(int argc, char *argv[])
510 initargs(argc, argv);
511 sel_init(10, 0, 10000);
513 signal(SIGHUP, SIG_IGN);
515 signal(SIGINT, terminate);
516 signal(SIGQUIT, terminate);
517 signal(SIGTERM, terminate);
518 signal(SIGPWR, terminate);
520 /* connect up stdin, stdout and message system */
521 in = fcb_new(0, TEXT);
522 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
523 if (tcgetattr(0, &in->t) < 0)
524 die("tcgetattr (%d)", errno);
526 struct termios t = in->t;
527 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
528 if (tcsetattr(0, TCSANOW, &t) < 0)
529 die("tcsetattr (%d)", errno);
534 /* tell the cluster who I am */
535 send_msg(node, 'A', connsort, strlen(connsort));
537 /* main processing loop */