]> gb7djk.dxcluster.net Git - spider.git/blob - src/client.c
ci changes file
[spider.git] / src / client.c
1 /*
2  * C Client for the DX Spider cluster program
3  *
4  * Eventually this program will be a complete replacement
5  * for the perl version.
6  *
7  * This program provides the glue necessary to talk between
8  * an input (eg from telnet or ax25) and the perl DXSpider
9  * node.
10  *
11  * Currently, this program connects STDIN/STDOUT to the
12  * message system used by cluster.pl
13  *
14  * Copyright (c) 2000 Dirk Koopman G1TLH
15  *
16  * $Id$
17  */
18
19 #include <stdio.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <netdb.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <termios.h>
33 #include <regex.h>
34
35 #include "sel.h"
36 #include "cmsg.h"
37 #include "debug.h"
38
39 #define TEXT 1
40 #define MSG 2
41 #define MAXBUFL 1024
42
43 #ifndef MAXPATHLEN 
44 #define MAXPATHLEN 256
45 #endif
46
47 #define DEFPACLEN 236
48 #define MAXPACLEN 236
49 #define MAXCALLSIGN 9
50
51 #define DBUF 1
52 #define DMSG 2
53 #define DSTS 4
54
55 #define UC unsigned char
56
57 typedef struct 
58 {
59         int cnum;                                       /* the connection number */
60         int sort;                                       /* the type of connection either text or msg */
61         cmsg_t *in;                                     /* current input message being built up */
62         cmsg_t *out;                            /* current output message being sent */
63         cmsg_t *obuf;                           /* current output being buffered */
64         reft *inq;                                      /* input queue */
65         reft *outq;                                     /* output queue */
66         sel_t *sp;                                      /* my select fcb address */
67         struct termios t;                       /* any termios associated with this cnum */
68         char echo;                                      /* echo characters back to this cnum */
69         char t_set;                                     /* the termios structure is valid */
70         char buffer_it;                         /* buffer outgoing packets for paclen */
71 } fcb_t;
72
73 typedef struct 
74 {
75         char *in;
76         regex_t *regex;
77 } myregex_t;
78
79
80 char *node_addr = "localhost";  /* the node tcp address, can be overridden by DXSPIDER_HOST */
81 int node_port = 27754;                  /* the tcp port of the node at the above address can be overidden by DXSPIDER_PORT*/
82 char *call;                                             /* the caller's callsign */
83 char *connsort;                                 /* the type of connection */
84 fcb_t *in;                                              /* the fcb of 'stdin' that I shall use */
85 fcb_t *node;                                    /* the fcb of the msg system */
86 char nl = '\n';                                 /* line end character */
87 char mode = 1;                  /* 0 - ax25, 1 - normal telnet, 2 - nlonly telnet */
88 char ending = 0;                                /* set this to end the program */
89 char echo = 1;                                  /* echo characters on stdout from stdin */
90 char int_tabs = 0;                              /* interpret tabs -> spaces */
91 char *root = "/spider";         /* root of data tree, can be overridden by DXSPIDER_ROOT  */
92 int timeout = 60;                               /* default timeout for logins and things */
93 int paclen = DEFPACLEN;                 /* default buffer size for outgoing packets */
94 int tabsize = 8;                                /* default tabsize for text messages */
95 char *connsort = "local";               /* the connection variety */
96 int state = 0;                                  /* the current state of the connection */
97 int laststate = 0;                              /* the last state we were in */
98
99
100
101 #define CONNECTED 100
102 #define WAITLOGIN 1
103 #define WAITPASSWD 2
104 #define WAITINPUT 10
105
106
107 myregex_t iscallreg[] = {               /* regexes to determine whether this is a reasonable callsign */
108         {
109                 "^[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0                /* G1TLH G1TLH1 */
110         },
111         {
112                 "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0          /* 2E0AAA 2E0AAA1 */
113         },
114         {
115                 "^[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0                /* G1TLH-2 */
116         },
117         {
118                 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0          /* 2E0AAA-2 */
119         },
120         {
121                 "^[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0               /* G1TLH-11 */
122         },
123         {
124                 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0         /* 2E0AAA-11 */
125         },
126         {
127                 0, 0
128         }
129 };
130
131 void terminate(int);
132
133 /*
134  * utility routines - various
135  */
136
137 void chgstate(int new)
138 {
139         laststate = state;
140         state = new;
141         dbg(DSTS, "chg state %d->%d", laststate, state);
142 }
143
144 void die(char *s, ...)
145 {
146         char buf[2000];
147         
148         va_list ap;
149         va_start(ap, s);
150         vsnprintf(buf, sizeof(buf)-1, s, ap);
151         va_end(ap);
152         fprintf(stderr,"%s\n", buf);
153         terminate(-1);
154 }
155
156 char *strupper(char *s)
157 {
158         char *d = malloc(strlen(s)+1);
159         char *p = d;
160         
161         if (!d)
162                 die("out of room in strupper");
163         while (*p++ = toupper(*s++)) ;
164         return d;
165 }
166
167 char *strlower(char *s)
168 {
169         char *d = malloc(strlen(s)+1);
170         char *p = d;
171         
172         if (!d)
173                 die("out of room in strlower");
174         while (*p++ = tolower(*s++)) ;
175         return d;
176 }
177
178 int eq(char *a, char *b)
179 {
180         return (strcmp(a, b) == 0);
181 }
182
183 FILE *xopen(char *dir, char *name, char *mode)
184 {
185         char fn[MAXPATHLEN+1];
186         snprintf(fn, MAXPATHLEN, "%s/%s/%s", root, dir, name);
187         return fopen(fn, mode);
188 }
189
190 int iscallsign(char *s)
191 {
192         myregex_t *rp;
193
194         if (strlen(s) > MAXCALLSIGN)
195                 return 0;
196         
197         for (rp = iscallreg; rp->in; ++rp) {
198                 if (regexec(rp->regex, s, 0, 0, 0) == 0)
199                         return 1;
200         }
201         return 0;
202 }
203
204 /*
205  * higher level send and receive routines
206  */
207
208 fcb_t *fcb_new(int cnum, int sort)
209 {
210         fcb_t *f = malloc(sizeof(fcb_t));
211         if (!f)
212                 die("no room in fcb_new");
213         memset (f, 0, sizeof(fcb_t));
214         f->cnum = cnum;
215         f->sort = sort;
216         f->inq = chain_new();
217         f->outq = chain_new();
218         return f;
219 }
220
221 void flush_text(fcb_t *f)
222 {
223         if (f->obuf) {
224                 cmsg_send(f->outq, f->obuf, 0);
225                 f->sp->flags |= SEL_OUTPUT;
226                 f->obuf = 0;
227         }
228 }
229
230 void send_text(fcb_t *f, char *s, int l, int nlreq)
231 {
232         cmsg_t *mp;
233         char *p;
234         
235         if (f->buffer_it && f->obuf) {
236                 mp = f->obuf;
237         } else {
238                 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
239         }
240
241         /* remove trailing spaces  */
242         while (l > 0 &&isspace(s[l-1]))
243                 --l;
244
245         for (p = s; p < s+l; ) {
246                 if (mp->inp >= mp->data + paclen) {
247                         flush_text(f);
248                         f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
249                 }
250                 *mp->inp++ = *p++;
251         }
252         if (mp->inp >= mp->data + paclen) {
253                 flush_text(f);
254                 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
255         }
256         if (nlreq) {
257                 if (nl == '\r')
258                         *mp->inp++ = nl;
259                 else {
260                         if (mode != 2)
261                                 *mp->inp++ = '\r';
262                         *mp->inp++ = '\n';
263                 }
264         }
265         if (!f->buffer_it)
266                 flush_text(f);
267 }
268
269 void send_msg(fcb_t *f, char let, UC *s, int l)
270 {
271         cmsg_t *mp;
272         int ln;
273         int myl = strlen(call)+2+l;
274
275         mp = cmsg_new(myl+4+1, f->sort, f);
276         *mp->inp++ = let;
277         strcpy(mp->inp, call);
278         mp->inp += strlen(call);
279         *mp->inp++ = '|';
280         if (l > 0) {
281                 UC *p;
282                 for (p = s; p < s+l; ++p) {
283                         if (mp->inp >= mp->data + (myl - 4)) {
284                                 int off = mp->inp - mp->data;
285                                 myl += 256;
286                                 mp = realloc(mp, myl);
287                                 mp->inp = mp->data + off;
288                         }
289                         
290                         if (*p < 0x20 || *p > 0x7e || *p == '%') {
291                                 sprintf(mp->inp, "%%%02X", *p & 0xff);
292                                 mp->inp += strlen(mp->inp);
293                         } else 
294                                 *mp->inp++ = *p;
295                 }
296         } 
297         *mp->inp++ = '\n';
298         *mp->inp = 0;
299         cmsg_send(f->outq, mp, 0);
300         f->sp->flags |= SEL_OUTPUT;
301 }
302
303 /* 
304  * send a file out to the user
305  */
306 void send_file(char *name)
307 {
308         int i;
309         char buf[MAXPACLEN+1];
310         
311         FILE *f = xopen("data", name, "r");
312         if (f) {
313                 while (fgets(buf, paclen, f)) {
314                         i = strlen(buf);
315                         if (i && buf[i-1] == '\n') 
316                                 buf[--i] = 0;
317                         send_text(in, buf, i, 1);
318                 }
319                 fclose(f);
320         }
321 }
322
323 /*
324  * the callback (called by sel_run) that handles all the inputs and outputs
325  */
326
327 int fcb_handler(sel_t *sp, int in, int out, int err)
328 {
329         fcb_t *f = sp->fcb;
330         cmsg_t *mp, *omp;
331         UC c;
332         
333         /* input modes */
334         if (ending == 0 && in) {
335                 char *p, buf[MAXBUFL];
336                 int r;
337
338                 /* read what we have into a buffer */
339                 r = read(f->cnum, buf, MAXBUFL);
340                 if (r < 0) {
341                         switch (errno) {
342                         case EINTR:
343                         case EINPROGRESS:
344                         case EAGAIN:
345                                 goto lout;
346                         default:
347                                 dbg(DBUF,"got errno %d in input", errno);
348                                 ending++;
349                                 return 0;
350                         }
351                 } else if (r == 0) {
352                         dbg(DBUF, "ending normally");
353                         ending++;
354                         return 0;
355                 }
356
357                 dbgdump(DBUF, "in ->", buf, r);
358                 
359                 /* create a new message buffer if required */
360                 if (!f->in)
361                         f->in = cmsg_new(MAXBUFL+1, f->sort, f);
362                 mp = f->in;
363
364                 switch (f->sort) {
365                 case TEXT:
366                         p = buf;
367                         if (f->echo)
368                                 omp = cmsg_new(3*r+1, f->sort, f);
369                         while (r > 0 && p < &buf[r]) {
370
371                                 /* echo processing */
372                                 if (f->echo) {
373                                         switch (*p) {
374                                         case '\b':
375                                         case 0x7f:
376                                                 strcpy(omp->inp, "\b \b");
377                                                 omp->inp += strlen(omp->inp);
378                                                 break;
379                                         case '\r':
380                                                 break;
381                                         case '\n':
382                                                 strcpy(omp->inp, "\r\n");
383                                                 omp->inp += 2;
384                                                 break;
385                                         default:
386                                                 *omp->inp++ = *p;
387                                         }
388                                 }
389                                 
390                                 /* character processing */
391                                 switch (*p) {
392                                 case '\t':
393                                         if (int_tabs) {
394                                                 memset(mp->inp, ' ', tabsize);
395                                                 mp->inp += tabsize;
396                                                 ++p;
397                                         } else {
398                                                 *mp->inp++ = *p++;
399                                         }
400                                         break;
401                                 case 0x08:
402                                 case 0x7f:
403                                         if (mp->inp > mp->data)
404                                                 mp->inp--;
405                                         ++p;
406                                         break;
407                                 default:
408                                         if (nl == '\n' && *p == '\r') {   /* ignore \r in telnet mode (ugh) */
409                                                 p++;
410                                         } else if (nl == '\r' && *p == '\n') {  /* and ignore \n in ax25 mode (double ugh) */
411                                                 p++;
412                                         } else if (*p == nl) {
413                                                 if (mp->inp == mp->data)
414                                                         *mp->inp++ = ' ';
415                                                 *mp->inp = 0;              /* zero terminate it, but don't include it in the length */
416                                                 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
417                                                 cmsg_send(f->inq, mp, 0);
418                                                 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
419                                                 ++p;
420                                         } else {
421                                                 if (mp->inp < &mp->data[MAXBUFL-8])
422                                                         *mp->inp++ = *p++;
423                                                 else {
424                                                         mp->inp = mp->data;
425                                                 }
426                                         }
427                                 }
428                         }
429                         
430                         /* queue any echo text */
431                         if (f->echo) {
432                                 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
433                                 cmsg_send(f->outq, omp, 0);
434                                 f->sp->flags |= SEL_OUTPUT;
435                         }
436                         
437                         break;
438
439                 case MSG:
440                         p = buf;
441                         while (r > 0 && p < &buf[r]) {
442                                 UC ch = *p++;
443                                 
444                                 if (mp->inp >= mp->data + (MAXBUFL-1)) {
445                                         mp->state = 0;
446                                         mp->inp = mp->data;
447                                         dbg(DMSG, "Message longer than %d received", MAXBUFL);
448                                 }
449
450                                 switch (mp->state) {
451                                 case 0: 
452                                         if (ch == '%') {
453                                                 c = 0;
454                                                 mp->state = 1;
455                                         } else if (ch == '\n') {
456                                                 /* kick it upstairs */
457                                                 *mp->inp = 0;
458                                                 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
459                                                 cmsg_send(f->inq, mp, 0);
460                                                 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
461                                         } else if (ch < 0x20 || ch > 0x7e) {
462                                                 dbg(DMSG, "Illegal character (0x%02X) received", *p);
463                                                 mp->inp = mp->data;
464                                         } else {
465                                                 *mp->inp++ = ch;
466                                         }
467                                         break;
468
469                                 case 1:
470                                         mp->state = 2;
471                                         if (ch >= '0' && ch <= '9') 
472                                                 c = (ch - '0') << 4;
473                                         else if (ch >= 'A' && ch <= 'F')
474                                                 c = (ch - 'A' + 10) << 4;
475                                         else {
476                                                 dbg(DMSG, "Illegal hex char (%c) received in state 1", ch);
477                                                 mp->inp = mp->data;
478                                                 mp->state = 0;
479                                         }
480                                         break;
481                                         
482                                 case 2:
483                                         if (ch >= '0' && ch <= '9') 
484                                                 *mp->inp++ = c | (ch - '0');
485                                         else if (ch >= 'A' && ch <= 'F')
486                                                 *mp->inp++ = c | (ch - 'A' + 10);
487                                         else {
488                                                 dbg(DMSG, "Illegal hex char (%c) received in state 2", ch);
489                                                 mp->inp = mp->data;
490                                         }
491                                         mp->state = 0;
492                                 }
493                         }
494                         break;
495                         
496                 default:
497                         die("invalid sort (%d) in input handler", f->sort);
498                 }
499         }
500         
501         /* output modes */
502 lout:;
503         if (out) {
504                 int l, r;
505                 
506                 if (!f->out) {
507                         mp = f->out = cmsg_next(f->outq);
508                         if (!mp) {
509                                 sp->flags &= ~SEL_OUTPUT;
510                                 return 0;
511                         }
512                         mp->inp = mp->data;
513                 }
514                 l = mp->size - (mp->inp - mp->data);
515                 if (l > 0) {
516                         
517                         dbgdump(DBUF, "<-out", mp->inp, l);
518                         
519                         r = write(f->cnum, mp->inp, l);
520                         if (r < 0) {
521                                 switch (errno) {
522                                 case EINTR:
523                                 case EINPROGRESS:
524                                 case EAGAIN:
525                                         goto lend;
526                                 default:
527                                         dbg(DBUF,"got errno %d in output", errno);
528                                         ending++;
529                                         return;
530                                 }
531                         } else if (r > 0) {
532                                 mp->inp += r;
533                         }
534                 } else if (l < 0) 
535                         die("got negative length in handler on node");
536                 if (mp->inp - mp->data >= mp->size) {
537                         cmsg_callback(mp, 0);
538                         f->out = 0;
539                 }
540         }
541 lend:;
542         return 0;
543 }
544
545 /* 
546  * set up the various mode flags, NL endings and things
547  */
548 void setmode(char *m)
549 {
550         connsort = strlower(m);
551         if (eq(connsort, "telnet") || eq(connsort, "local") || eq(connsort, "nlonly")) {
552                 nl = '\n';
553                 echo = 1;
554                 mode = eq(connsort, "nlonly") ? 2 : 1;
555         } else if (eq(connsort, "ax25")) {
556                 nl = '\r';
557                 echo = 0;
558                 mode = 0;
559         } else if (eq(connsort, "connect")) {
560                 nl = '\n';
561                 echo = 0;
562                 mode = 3;
563         } else {
564                 die("Connection type must be \"telnet\", \"nlonly\", \"ax25\", \"login\" or \"local\"");
565         }
566 }
567
568
569 /*
570  * things to do with ongoing processing of inputs
571  */
572
573 void process_stdin()
574 {
575         cmsg_t *mp = cmsg_next(in->inq);
576         char *p, hasa, hasn, i;
577         char callsign[MAXCALLSIGN+1];
578         
579         if (mp) {
580                 dbg(DMSG, "MSG size: %d", mp->size);
581
582                 switch (state) {
583                 case CONNECTED:
584                         if (mp->size > 0) {
585                                 send_msg(node, 'I', mp->data, mp->size);
586                         }
587                         break;
588                 case WAITLOGIN:
589                         for (i = 0; i < mp->size; ++i) {
590                                 UC ch = mp->data[i];
591                                 if (i < MAXCALLSIGN) {
592                                         if (isalpha(ch))
593                                                 ++hasa;
594                                         if (isdigit(ch))
595                                                 ++hasn;
596                                     if (isalnum(ch) || ch == '-')
597                                                 callsign[i] = ch;
598                                         else
599                                                 die("invalid callsign");
600                                 } else 
601                                         die("invalid callsign");
602                         }
603                         callsign[i]= 0;
604                         if (strlen(callsign) < 3)
605                                 die("invalid callsign");
606                         if (hasa && hasn)
607                                 ;
608                         else
609                                 die("invalid callsign");
610                         call = strupper(callsign);
611                         
612                         /* check the callsign against the regexes */
613                         if (!iscallsign(call)) {
614                                 die("Sorry, %s isn't a valid callsign", call);
615                         }
616
617                         alarm(0);
618                         signal(SIGALRM, SIG_IGN);
619                         
620                         /* tell the cluster who I am */
621                         send_msg(node, 'A', connsort, strlen(connsort));
622                         
623                         chgstate(CONNECTED);
624                         send_file("connected");
625                 }
626
627                 cmsg_callback(mp, 0);
628
629         }
630 }
631
632 void process_node()
633 {
634         cmsg_t *mp = cmsg_next(node->inq);
635         if (mp) {
636                 dbg(DMSG, "MSG size: %d", mp->size);
637         
638                 if (mp->size > 0 && mp->inp > mp->data) {
639                         char *p = strchr(mp->data, '|');
640                         if (p)
641                                 p++;
642                         switch (mp->data[0]) {
643                         case 'Z':
644                                 ending++;
645                                 return;
646                         case 'E':
647                                 if (isdigit(*p))
648                                         in->echo = *p - '0';
649                                 break;
650                         case 'B':
651                                 if (isdigit(*p))
652                                         in->buffer_it = *p - '0';
653                                 break;
654                         case 'D':
655                                 if (p) {
656                                         int l = mp->inp - (UC *) p;
657                                         send_text(in, p, l, 1);
658                                 }
659                                 break;
660                         default:
661                                 break;
662                         }
663                 }
664                 cmsg_callback(mp, 0);
665         } else {
666                 flush_text(in);
667         }
668 }
669
670 /*
671  * things to do with going away
672  */
673
674 void term_timeout(int i)
675 {
676         /* none of this is going to be reused so don't bother cleaning up properly */
677         if (in && in->t_set)
678                 tcsetattr(0, TCSANOW, &in->t);
679         if (node) {
680                 shutdown(node->cnum, 3);
681                 close(node->cnum);
682         }
683         exit(i);
684 }
685
686 void terminate(int i)
687 {
688         signal(SIGALRM, term_timeout);
689         alarm(10);
690         
691         while ((in && !is_chain_empty(in->outq)) ||
692                    (node && !is_chain_empty(node->outq))) {
693                 sel_run();
694         }
695         if (in && in->t_set)
696                 tcsetattr(0, TCSADRAIN, &in->t);
697         if (node) {
698                 shutdown(node->cnum, 3);
699                 close(node->cnum);
700         }
701         exit(i);
702 }
703
704 void login_timeout(int i)
705 {
706         write(0, "Timed Out", 10);
707         write(0, &nl, 1);
708         sel_run();                                      /* force a coordination */
709         if (in && in->t_set)
710                 tcsetattr(0, TCSANOW, &in->t);
711         exit(i);
712 }
713
714 /*
715  * things to do with initialisation
716  */
717
718 void initargs(int argc, char *argv[])
719 {
720         int i, c, err = 0;
721
722         while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
723                 switch (c) {
724                 case 'h':
725                         node_addr = optarg;
726                         break;
727                 case 'l':
728                         paclen = atoi(optarg);
729                         if (paclen < 80)
730                                 paclen = 80;
731                         if (paclen > MAXPACLEN)
732                                 paclen = MAXPACLEN;
733                         break;
734                 case 'p':
735                         node_port = atoi(optarg);
736                         break;
737                 case 'x':
738                         dbginit("client");
739                         dbgset(atoi(optarg));
740                         break;
741                 default:
742                         ++err;
743                         goto lerr;
744                 }
745         }
746
747 lerr:
748         if (err) {
749                 die("usage: client [-x n|-h<host>|-p<port>|-l<paclen>] <call>|login [local|telnet|ax25]");
750         }
751         
752         if (optind < argc) {
753                 call = strupper(argv[optind]);
754                 ++optind;
755         }
756         if (!call)
757                 die("Must have at least a callsign (for now)");
758
759         if (optind < argc) {
760                 setmode(argv[optind]);          
761         } else {
762                 setmode("local");
763         }
764
765         /* this is kludgy, but hey so is the rest of this! */
766         if (mode != 0 && paclen == DEFPACLEN) {
767                 paclen = MAXPACLEN;
768         }
769 }
770
771 void connect_to_node()
772 {
773         struct hostent *hp, *gethostbyname();
774         struct sockaddr_in server;
775         int nodef;
776         sel_t *sp;
777                                 
778         if ((hp = gethostbyname(node_addr)) == 0) 
779                 die("Unknown host tcp host %s for printer", node_addr);
780
781         memset(&server, 0, sizeof server);
782         server.sin_family = AF_INET;
783         memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
784         server.sin_port = htons(node_port);
785                                                 
786         nodef = socket(AF_INET, SOCK_STREAM, 0);
787         if (nodef < 0) 
788                 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
789
790         if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
791                 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
792         }
793         node = fcb_new(nodef, MSG);
794         node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
795         
796 }
797
798 /*
799  * the program itself....
800  */
801
802 main(int argc, char *argv[])
803 {
804         /* compile regexes for iscallsign */
805         {
806                 myregex_t *rp;
807                 for (rp = iscallreg; rp->in; ++rp) {
808                         regex_t reg;
809                         int r = regcomp(&reg, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
810                         if (r)
811                                 die("regcomp returned %d for '%s'", r, rp->in);
812                         rp->regex = malloc(sizeof(regex_t));
813                         if (!rp->regex)
814                                 die("out of room - compiling regexes");
815                         *rp->regex = reg;
816                 }
817         }
818         
819         /* set up environment */
820         {
821                 char *p = getenv("DXSPIDER_ROOT");
822                 if (p)
823                         root = p;
824                 p = getenv("DXSPIDER_HOST");
825                 if (p)
826                         node_addr = p;
827                 p = getenv("DXSPIDER_PORT");
828                 if (p)
829                         node_port = atoi(p);
830                 p = getenv("DXSPIDER_PACLEN");
831                 if (p) {
832                         paclen = atoi(p);
833                         if (paclen < 80)
834                                 paclen = 80;
835                         if (paclen > MAXPACLEN)
836                                 paclen = MAXPACLEN;
837                 }
838         }
839         
840         /* get program arguments, initialise stuff */
841         initargs(argc, argv);
842         sel_init(10, 0, 10000);
843
844         /* trap signals */
845         signal(SIGHUP, SIG_IGN);
846         signal(SIGINT, terminate);
847         signal(SIGQUIT, terminate);
848         signal(SIGTERM, terminate);
849 #ifdef SIGPWR
850         signal(SIGPWR, terminate);
851 #endif
852
853         /* connect up stdin */
854         in = fcb_new(0, TEXT);
855         in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
856         if (tcgetattr(0, &in->t) < 0) {
857 /*              echo = 0; */
858                 in->echo = echo;
859                 in->t_set = 0;
860         } else {
861                 struct termios t = in->t;
862                 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
863                 t.c_oflag = 0;
864                 if (tcsetattr(0, TCSANOW, &t) < 0) 
865                         die("tcsetattr (%d)", errno);
866                 in->echo = echo;
867                 in->t_set = 1;
868         }
869         in->buffer_it = 1;
870
871         /* connect up node */
872         connect_to_node();
873
874         /* is this a login? */
875         if (eq(call, "LOGIN") || eq(call, "login")) {
876                 send_file("issue");
877                 signal(SIGALRM, login_timeout);
878                 alarm(timeout);
879                 send_text(in, "login: ", 7, 0);
880                 chgstate(WAITLOGIN);
881         } else {
882
883                 /* check the callsign against the regexes */
884                 if (!iscallsign(call)) {
885                         die("Sorry, %s isn't a valid callsign", call);
886                 }
887
888                 /* tell the cluster who I am */
889                 send_msg(node, 'A', connsort, strlen(connsort));
890         
891                 chgstate(CONNECTED);
892                 send_file("connected");
893         }
894         
895
896         /* main processing loop */
897         while (ending == 0) {
898                 sel_run();
899                 if (ending == 0) {
900                         process_stdin();
901                         process_node();
902                 }
903         }
904         terminate(0);
905 }
906
907
908
909
910
911