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);
273 if (mp->size > MAXBUFL)
274 die("Message size too big from node (%d > %d)", mp->size, MAXBUFL);
278 if (mp->inp - mp->data < mp->size) {
281 if (mp->inp - mp->data >= mp->size) {
282 /* kick it upstairs */
283 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
284 cmsg_send(f->inq, mp, 0);
285 mp = f->in = cmsg_new(MAXBUFL, f->sort, f);
292 die("invalid sort (%d) in input handler", f->sort);
302 mp = f->out = cmsg_next(f->outq);
304 sp->flags &= ~SEL_OUTPUT;
309 l = mp->size - (mp->inp - mp->data);
312 dbgdump(DBUF, "<-out", mp->inp, l);
314 r = write(f->cnum, mp->inp, l);
331 die("got negative length in handler on node");
332 if (mp->inp - mp->data >= mp->size) {
333 cmsg_callback(mp, 0);
335 /* if (is_chain_empty(f->outq))
336 sp->flags &= ~SEL_OUTPUT; */
344 * things to do with initialisation
347 void initargs(int argc, char *argv[])
351 while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
357 node_port = atoi(optarg);
361 dbgset(atoi(optarg));
371 die("usage: client [-x nn] <call>|login [local|telnet|ax25]");
375 call = strupper(argv[optind]);
376 if (eq(call, "LOGIN"))
377 die("login not implemented (yet)");
381 die("Must have at least a callsign (for now)");
384 connsort = strlower(argv[optind]);
385 if (eq(connsort, "telnet") || eq(connsort, "local")) {
388 } else if (eq(connsort, "ax25")) {
392 die("2nd argument must be \"telnet\" or \"ax25\" or \"local\"");
401 void connect_to_node()
403 struct hostent *hp, *gethostbyname();
404 struct sockaddr_in server;
408 if ((hp = gethostbyname(node_addr)) == 0)
409 die("Unknown host tcp host %s for printer", node_addr);
411 memset(&server, 0, sizeof server);
412 server.sin_family = AF_INET;
413 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
414 server.sin_port = htons(node_port);
416 nodef = socket(AF_INET, SOCK_STREAM, 0);
418 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
420 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
421 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
423 node = fcb_new(nodef, MSG);
424 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
429 * things to do with going away
432 void term_timeout(int i)
434 /* none of this is going to be reused so don't bother cleaning up properly */
436 tcsetattr(0, TCSANOW, &in->t);
443 void terminate(int i)
445 if (send_Z && call) {
446 send_msg(node, 'Z', "", 0);
449 signal(SIGALRM, term_timeout);
452 while ((in && !is_chain_empty(in->outq)) ||
453 (node && !is_chain_empty(node->outq))) {
457 tcsetattr(0, TCSANOW, &in->t);
464 * things to do with ongoing processing of inputs
469 cmsg_t *mp = cmsg_next(in->inq);
471 dbg(DMSG, "MSG size: %d", mp->size);
473 if (mp->size > 0 && mp->inp > mp->data) {
474 send_msg(node, 'I', mp->data, mp->size);
476 cmsg_callback(mp, 0);
482 cmsg_t *mp = cmsg_next(node->inq);
484 dbg(DMSG, "MSG size: %d", mp->size);
486 if (mp->size > 0 && mp->inp > mp->data) {
487 char *p = strchr(mp->data, '|');
490 switch (mp->data[0]) {
501 int l = mp->inp - (unsigned char *) p;
509 cmsg_callback(mp, 0);
514 * the program itself....
517 main(int argc, char *argv[])
519 initargs(argc, argv);
520 sel_init(10, 0, 10000);
522 signal(SIGHUP, SIG_IGN);
524 signal(SIGINT, terminate);
525 signal(SIGQUIT, terminate);
526 signal(SIGTERM, terminate);
528 signal(SIGPWR, terminate);
531 /* connect up stdin, stdout and message system */
532 in = fcb_new(0, TEXT);
533 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
534 if (tcgetattr(0, &in->t) < 0) {
538 struct termios t = in->t;
539 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
540 if (tcsetattr(0, TCSANOW, &t) < 0)
541 die("tcsetattr (%d)", errno);
547 /* tell the cluster who I am */
548 send_msg(node, 'A', connsort, strlen(connsort));
550 /* main processing loop */