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)
234 if (mp->inp == mp->data)
236 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
237 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
238 cmsg_send(f->inq, mp, 0);
239 f->in = mp = cmsg_new(MAXBUFL, f->sort, f);
242 if (mp->inp < &mp->data[MAXBUFL])
251 /* queue any echo text */
253 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
254 cmsg_send(f->outq, omp, 0);
255 f->sp->flags |= SEL_OUTPUT;
262 while (r > 0 && p < &buf[r]) {
264 /* build up the size into the likely message length (yes I know it's a short) */
272 mp->size = (mp->size << 8) | (*p++ & 0xff);
276 if (mp->inp - mp->data < mp->size) {
279 if (mp->inp - mp->data >= mp->size) {
280 /* kick it upstairs */
281 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
282 cmsg_send(f->inq, mp, 0);
283 mp = f->in = cmsg_new(MAXBUFL, f->sort, f);
290 die("invalid sort (%d) in input handler", f->sort);
300 mp = f->out = cmsg_next(f->outq);
302 sp->flags &= ~SEL_OUTPUT;
307 l = mp->size - (mp->inp - mp->data);
310 dbgdump(DBUF, "<-out", mp->inp, l);
312 r = write(f->cnum, mp->inp, l);
329 die("got negative length in handler on node");
330 if (mp->inp - mp->data >= mp->size) {
331 cmsg_callback(mp, 0);
333 /* if (is_chain_empty(f->outq))
334 sp->flags &= ~SEL_OUTPUT; */
342 * things to do with initialisation
345 void initargs(int argc, char *argv[])
349 while ((c = getopt(argc, argv, "h:x:")) > 0) {
356 dbgset(atoi(optarg));
366 die("usage: client [-x nn] <call>|login [local|telnet|ax25]");
370 call = strupper(argv[optind]);
371 if (eq(call, "LOGIN"))
372 die("login not implemented (yet)");
376 die("Must have at least a callsign (for now)");
379 connsort = strlower(argv[optind]);
380 if (eq(connsort, "telnet") || eq(connsort, "local")) {
383 } else if (eq(connsort, "ax25")) {
387 die("2nd argument must be \"telnet\" or \"ax25\" or \"local\"");
396 void connect_to_node()
398 struct hostent *hp, *gethostbyname();
399 struct sockaddr_in server;
403 if ((hp = gethostbyname(node_addr)) == 0)
404 die("Unknown host tcp host %s for printer", node_addr);
406 memset(&server, 0, sizeof server);
407 server.sin_family = AF_INET;
408 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
409 server.sin_port = htons(node_port);
411 nodef = socket(AF_INET, SOCK_STREAM, 0);
413 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
415 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
416 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
418 node = fcb_new(nodef, MSG);
419 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
424 * things to do with going away
427 void term_timeout(int i)
429 /* none of this is going to be reused so don't bother cleaning up properly */
431 tcsetattr(0, TCSANOW, &in->t);
438 void terminate(int i)
440 if (send_Z && call) {
441 send_msg(node, 'Z', "", 0);
444 signal(SIGALRM, term_timeout);
447 while ((in && !is_chain_empty(in->outq)) ||
448 (node && !is_chain_empty(node->outq))) {
452 tcsetattr(0, TCSANOW, &in->t);
459 * things to do with ongoing processing of inputs
464 cmsg_t *mp = cmsg_next(in->inq);
466 dbg(DMSG, "MSG size: %d", mp->size);
468 if (mp->size > 0 && mp->inp > mp->data) {
469 send_msg(node, 'I', mp->data, mp->size);
471 cmsg_callback(mp, 0);
477 cmsg_t *mp = cmsg_next(node->inq);
479 dbg(DMSG, "MSG size: %d", mp->size);
481 if (mp->size > 0 && mp->inp > mp->data) {
482 char *p = strchr(mp->data, '|');
485 switch (mp->data[0]) {
496 int l = mp->inp - (unsigned char *) p;
504 cmsg_callback(mp, 0);
509 * the program itself....
512 main(int argc, char *argv[])
514 initargs(argc, argv);
515 sel_init(10, 0, 10000);
517 signal(SIGHUP, SIG_IGN);
519 signal(SIGINT, terminate);
520 signal(SIGQUIT, terminate);
521 signal(SIGTERM, terminate);
522 signal(SIGPWR, terminate);
524 /* connect up stdin, stdout and message system */
525 in = fcb_new(0, TEXT);
526 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
527 if (tcgetattr(0, &in->t) < 0) {
531 struct termios t = in->t;
532 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
533 if (tcsetattr(0, TCSANOW, &t) < 0)
534 die("tcsetattr (%d)", errno);
540 /* tell the cluster who I am */
541 send_msg(node, 'A', connsort, strlen(connsort));
543 /* main processing loop */