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>
42 int cnum; /* the connection number */
43 int sort; /* the type of connection either text or msg */
44 cmsg_t *in; /* current input message being built up */
45 cmsg_t *out; /* current output message being sent */
46 reft *inq; /* input queue */
47 reft *outq; /* output queue */
48 sel_t *sp; /* my select fcb address */
51 char *node_addr = "localhost"; /* the node tcp address */
52 int node_port = 27754; /* the tcp port of the node at the above address */
53 char *call; /* the caller's callsign */
54 char *connsort; /* the type of connection */
55 fcb_t *in; /* the fcb of 'stdin' that I shall use */
56 fcb_t *out; /* the fcb of 'stdout' that I shall use */
57 fcb_t *node; /* the fcb of the msg system */
58 char nl = '\n'; /* line end character */
59 char ending = 0; /* set this to end the program */
60 char send_Z = 1; /* set a Z record to the node on termination */
63 * utility routines - various
66 void die(char *s, ...)
78 char *strupper(char *s)
80 char *d = malloc(strlen(s)+1);
84 die("out of room in strupper");
85 while (*p++ = toupper(*s++)) ;
89 char *strlower(char *s)
91 char *d = malloc(strlen(s)+1);
95 die("out of room in strlower");
96 while (*p++ = tolower(*s++)) ;
100 int eq(char *a, char *b)
102 return (strcmp(a, b) == 0);
106 * higher level send and receive routines
109 fcb_t *fcb_new(int cnum, int sort)
111 fcb_t *f = malloc(sizeof(fcb_t));
113 die("no room in fcb_new");
114 memset (f, 0, sizeof(fcb_t));
117 f->inq = chain_new();
118 f->outq = chain_new();
122 void send_text(fcb_t *f, char *s, int l)
125 mp = cmsg_new(l+1, f->sort, f);
126 memcpy(mp->inp, s, l);
129 cmsg_send(f->outq, mp, 0);
130 f->sp->flags |= SEL_OUTPUT;
133 void send_msg(fcb_t *f, char let, char *s, int l)
137 int myl = strlen(call)+2+l;
139 mp = cmsg_new(myl+4, f->sort, f);
141 memcpy(mp->inp, &ln, 4);
144 strcpy(mp->inp, call);
145 mp->inp += strlen(call);
148 memcpy(mp->inp, s, l);
152 cmsg_send(f->outq, mp, 0);
153 f->sp->flags |= SEL_OUTPUT;
156 int fcb_handler(sel_t *sp, int in, int out, int err)
163 char *p, buf[MAXBUFL];
166 /* read what we have into a buffer */
167 r = read(f->cnum, buf, MAXBUFL);
187 /* create a new message buffer if required */
189 f->in = cmsg_new(MAXBUFL, sp->sort, f);
195 while (r > 0 && p < &buf[r]) {
198 * if we have a nl then send the message upstairs
199 * start a new message
203 if (mp->inp == mp->data)
205 *mp->inp = 0; /* zero terminate it, but don't include it in the length */
206 cmsg_send(f->inq, mp, 0);
207 f->in = mp = cmsg_new(MAXBUFL, sp->sort, f);
210 if (mp->inp < &mp->data[MAXBUFL])
221 while (r > 0 && p < &buf[r]) {
223 /* build up the size into the likely message length (yes I know it's a short) */
229 mp->size = (mp->size << 8) | *p++;
233 if (mp->inp - mp->data < mp->size) {
236 /* kick it upstairs */
237 cmsg_send(f->inq, mp, 0);
238 mp = f->in = cmsg_new(MAXBUFL, f->sort, f);
245 die("invalid sort (%d) in input handler", f->sort);
255 mp = f->out = cmsg_next(f->outq);
257 sp->flags &= ~SEL_OUTPUT;
262 l = mp->size - (mp->inp - mp->data);
264 r = write(f->cnum, mp->inp, l);
281 die("got negative length in handler on node");
282 if (mp->inp - mp->data >= mp->size) {
283 cmsg_callback(mp, 0);
285 if (!is_chain_empty(f->outq))
286 sp->flags &= ~SEL_OUTPUT;
294 * things to do with initialisation
297 void initargs(int argc, char *argv[])
301 call = strupper(argv[1]);
302 if (eq(call, "LOGIN"))
303 die("login not implemented (yet)");
306 die("Must have at least a callsign (for now)");
309 connsort = strlower(argv[2]);
310 if (eq(connsort, "telnet") || eq(connsort, "local")) {
312 } else if (eq(connsort, "ax25")) {
315 die("2nd argument must be \"telnet\" or \"ax25\" or \"local\"");
322 void connect_to_node()
324 struct hostent *hp, *gethostbyname();
325 struct sockaddr_in server;
329 if ((hp = gethostbyname(node_addr)) == 0)
330 die("Unknown host tcp host %s for printer", node_addr);
332 memset(&server, 0, sizeof server);
333 server.sin_family = AF_INET;
334 memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
335 server.sin_port = htons(node_port);
337 nodef = socket(AF_INET, SOCK_STREAM, 0);
339 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
341 if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
342 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
344 node = fcb_new(nodef, MSG);
345 node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
350 * things to do with going away
353 void term_timeout(int i)
355 /* none of this is going to be reused so don't bother cleaning up properly */
365 void terminate(int i)
367 if (send_Z && call) {
368 send_msg(node, 'Z', "", 0);
371 signal(SIGALRM, term_timeout);
374 while ((out && !is_chain_empty(out->outq)) ||
375 (node && !is_chain_empty(node->outq))) {
384 * things to do with ongoing processing of inputs
389 cmsg_t *mp = cmsg_next(in->inq);
391 send_msg(node, 'I', mp->data, mp->size);
392 cmsg_callback(mp, 0);
398 cmsg_t *mp = cmsg_next(node->inq);
400 char *p = strchr(mp->data, '|');
403 switch (mp->data[0]) {
410 int l = mp->inp - (unsigned char *) p;
411 send_text(out, p, l);
417 cmsg_callback(mp, 0);
419 if (is_chain_empty(out->outq))
424 * the program itself....
427 main(int argc, char *argv[])
429 initargs(argc, argv);
430 sel_init(10, 0, 10000);
432 signal(SIGHUP, SIG_IGN);
434 signal(SIGINT, terminate);
435 signal(SIGQUIT, terminate);
436 signal(SIGTERM, terminate);
437 signal(SIGPWR, terminate);
439 /* connect up stdin, stdout and message system */
440 in = fcb_new(0, TEXT);
441 in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
442 out = fcb_new(1, TEXT);
443 out->sp = sel_open(1, out, "STDOUT", fcb_handler, TEXT, 0);
446 /* tell the cluster who I am */
447 send_msg(node, 'A', connsort, strlen(connsort));
449 /* main processing loop */