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+1, 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;
167 * the callback (called by sel_run) that handles all the inputs and outputs
170 int fcb_handler(sel_t *sp, int in, int out, int err)
177 char *p, buf[MAXBUFL];
180 /* read what we have into a buffer */
181 r = read(f->cnum, buf, MAXBUFL);
201 dbgdump(DBUF, "in ->", buf, r);
203 /* create a new message buffer if required */
205 f->in = cmsg_new(MAXBUFL+1, f->sort, f);
212 omp = cmsg_new(3*r+1, f->sort, f);
213 while (r > 0 && p < &buf[r]) {
215 /* echo processing */
220 strcpy(omp->inp, "\b \b");
221 omp->inp += strlen(omp->inp);
228 /* character processing */
232 if (mp->inp > mp->data)
237 if (nl == '\n' && *p == '\r') { /* ignore \r in telnet mode (ugh) */
239 } else if (*p == nl) {
240 if (mp->inp == mp->data)
242 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
243 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
244 cmsg_send(f->inq, mp, 0);
245 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
248 if (mp->inp < &mp->data[MAXBUFL])
257 /* queue any echo text */
259 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
260 cmsg_send(f->outq, omp, 0);
261 f->sp->flags |= SEL_OUTPUT;
268 while (r > 0 && p < &buf[r]) {
270 /* build up the size into the likely message length (yes I know it's a short) */
278 mp->size = (mp->size << 8) | (*p++ & 0xff);
279 if (mp->size > MAXBUFL)
280 die("Message size too big from node (%d > %d)", mp->size, MAXBUFL);
284 if (mp->inp - mp->data < mp->size) {
287 if (mp->inp - mp->data >= mp->size) {
288 /* kick it upstairs */
289 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
290 cmsg_send(f->inq, mp, 0);
291 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
298 die("invalid sort (%d) in input handler", f->sort);
308 mp = f->out = cmsg_next(f->outq);
310 sp->flags &= ~SEL_OUTPUT;
315 l = mp->size - (mp->inp - mp->data);
318 dbgdump(DBUF, "<-out", mp->inp, l);
320 r = write(f->cnum, mp->inp, l);
337 die("got negative length in handler on node");
338 if (mp->inp - mp->data >= mp->size) {
339 cmsg_callback(mp, 0);
348 * things to do with initialisation
351 void initargs(int argc, char *argv[])
355 while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
361 node_port = atoi(optarg);
365 dbgset(atoi(optarg));
375 die("usage: client [-x n|-h<host>|-p<port>] <call>|login [local|telnet|ax25]");
379 call = strupper(argv[optind]);
380 if (eq(call, "LOGIN"))
381 die("login not implemented (yet)");
385 die("Must have at least a callsign (for now)");
388 connsort = strlower(argv[optind]);
389 if (eq(connsort, "telnet") || eq(connsort, "local")) {
392 } else if (eq(connsort, "ax25")) {
396 die("2nd argument must be \"telnet\" or \"ax25\" or \"local\"");
405 void connect_to_node()
407 struct hostent *hp, *gethostbyname();
408 struct sockaddr_in server;
412 if ((hp = gethostbyname(node_addr)) == 0)
413 die("Unknown host tcp host %s for printer", node_addr);
415 memset(&server, 0, sizeof server);
416 server.sin_family = AF_INET;
417 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
418 server.sin_port = htons(node_port);
420 nodef = socket(AF_INET, SOCK_STREAM, 0);
422 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
424 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
425 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
427 node = fcb_new(nodef, MSG);
428 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
433 * things to do with going away
436 void term_timeout(int i)
438 /* none of this is going to be reused so don't bother cleaning up properly */
440 tcsetattr(0, TCSANOW, &in->t);
447 void terminate(int i)
449 if (send_Z && call) {
450 send_msg(node, 'Z', "", 0);
453 signal(SIGALRM, term_timeout);
456 while ((in && !is_chain_empty(in->outq)) ||
457 (node && !is_chain_empty(node->outq))) {
461 tcsetattr(0, TCSANOW, &in->t);
468 * things to do with ongoing processing of inputs
473 cmsg_t *mp = cmsg_next(in->inq);
475 dbg(DMSG, "MSG size: %d", mp->size);
477 if (mp->size > 0 && mp->inp > mp->data) {
478 send_msg(node, 'I', mp->data, mp->size);
480 cmsg_callback(mp, 0);
486 cmsg_t *mp = cmsg_next(node->inq);
488 dbg(DMSG, "MSG size: %d", mp->size);
490 if (mp->size > 0 && mp->inp > mp->data) {
491 char *p = strchr(mp->data, '|');
494 switch (mp->data[0]) {
505 int l = mp->inp - (unsigned char *) p;
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 */