X-Git-Url: http://dxcluster.org/gitweb/gitweb.cgi?a=blobdiff_plain;f=src%2Fclient.c;h=8c52d53ffa921839d491a76ed137c0aeccfef971;hb=632bda2671a8b0cf73b1a0bffa7b906c8744b14f;hp=640c2b4467dc648e655b990ffd164d66ebd9e893;hpb=8804833a62667fe6a33655351790b241256e5980;p=spider.git diff --git a/src/client.c b/src/client.c index 640c2b44..8c52d53f 100644 --- a/src/client.c +++ b/src/client.c @@ -29,35 +29,92 @@ #include #include #include +#include +#include #include "sel.h" #include "cmsg.h" +#include "debug.h" #define TEXT 1 #define MSG 2 #define MAXBUFL 1024 +#ifndef MAXPATHLEN +#define MAXPATHLEN 256 +#endif + +#define DEFPACLEN 128 +#define MAXPACLEN 236 +#define MAXCALLSIGN 9 + +#define DBUF 1 +#define DMSG 2 + typedef struct { int cnum; /* the connection number */ int sort; /* the type of connection either text or msg */ cmsg_t *in; /* current input message being built up */ cmsg_t *out; /* current output message being sent */ + cmsg_t *obuf; /* current output being buffered */ reft *inq; /* input queue */ reft *outq; /* output queue */ sel_t *sp; /* my select fcb address */ + struct termios t; /* any termios associated with this cnum */ + char echo; /* echo characters back to this cnum */ + char t_set; /* the termios structure is valid */ + char buffer_it; /* buffer outgoing packets for paclen */ } fcb_t; -char *node_addr = "localhost"; /* the node tcp address */ -int node_port = 27754; /* the tcp port of the node at the above address */ +typedef struct +{ + char *in; + regex_t *regex; +} myregex_t; + + +char *node_addr = "localhost"; /* the node tcp address, can be overridden by DXSPIDER_HOST */ +int node_port = 27754; /* the tcp port of the node at the above address can be overidden by DXSPIDER_PORT*/ char *call; /* the caller's callsign */ char *connsort; /* the type of connection */ fcb_t *in; /* the fcb of 'stdin' that I shall use */ -fcb_t *out; /* the fcb of 'stdout' that I shall use */ fcb_t *node; /* the fcb of the msg system */ char nl = '\n'; /* line end character */ char ending = 0; /* set this to end the program */ char send_Z = 1; /* set a Z record to the node on termination */ +char echo = 1; /* echo characters on stdout from stdin */ +char int_tabs = 0; /* interpret tabs -> spaces */ +char *root = "/spider"; /* root of data tree, can be overridden by DXSPIDER_ROOT */ +int timeout = 60; /* default timeout for logins and things */ +int paclen = DEFPACLEN; /* default buffer size for outgoing packets */ +int tabsize = 8; /* default tabsize for text messages */ + +myregex_t iscallreg[] = { /* regexes to determine whether this is a reasonable callsign */ + { + "^[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* G1TLH G1TLH1 */ + }, + { + "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0 /* 2E0AAA 2E0AAA1 */ + }, + { + "^[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0 /* G1TLH-2 */ + }, + { + "^[0-9]+[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0 /* 2E0AAA-2 */ + }, + { + "^[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* G1TLH-11 */ + }, + { + "^[0-9]+[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0 /* 2E0AAA-11 */ + }, + { + 0, 0 + } +}; + +void terminate(int); /* * utility routines - various @@ -69,10 +126,10 @@ void die(char *s, ...) va_list ap; va_start(ap, s); - vsprintf(buf, s, ap); + vsnprintf(buf, sizeof(buf)-1, s, ap); va_end(ap); - fprintf(stderr, buf); - exit(-1); + fprintf(stderr,"%s\n", buf); + terminate(-1); } char *strupper(char *s) @@ -102,6 +159,27 @@ int eq(char *a, char *b) return (strcmp(a, b) == 0); } +int xopen(char *dir, char *name, int mode) +{ + char fn[MAXPATHLEN+1]; + snprintf(fn, MAXPATHLEN, "%s/%s/%s", root, dir, name); + return open(fn, mode); +} + +int iscallsign(char *s) +{ + myregex_t *rp; + + if (strlen(s) > MAXCALLSIGN) + return 0; + + for (rp = iscallreg; rp->in; ++rp) { + if (regexec(rp->regex, s, 0, 0, 0) == 0) + return 1; + } + return 0; +} + /* * higher level send and receive routines */ @@ -119,15 +197,44 @@ fcb_t *fcb_new(int cnum, int sort) return f; } +void flush_text(fcb_t *f) +{ + if (f->obuf) { + cmsg_send(f->outq, f->obuf, 0); + f->sp->flags |= SEL_OUTPUT; + f->obuf = 0; + } +} + void send_text(fcb_t *f, char *s, int l) { cmsg_t *mp; - mp = cmsg_new(l+1, f->sort, f); - memcpy(mp->inp, s, l); - mp->inp += l; + char *p; + + if (f->buffer_it && f->obuf) { + mp = f->obuf; + } else { + f->obuf = mp = cmsg_new(paclen+1, f->sort, f); + } + + /* remove trailing spaces */ + while (l > 0 &&isspace(s[l-1])) + --l; + + for (p = s; p < s+l; ) { + if (mp->inp >= mp->data + paclen) { + flush_text(f); + f->obuf = mp = cmsg_new(paclen+1, f->sort, f); + } + *mp->inp++ = *p++; + } + if (mp->inp >= mp->data + paclen) { + flush_text(f); + f->obuf = mp = cmsg_new(paclen+1, f->sort, f); + } *mp->inp++ = nl; - cmsg_send(f->outq, mp, 0); - f->sp->flags |= SEL_OUTPUT; + if (!f->buffer_it) + flush_text(f); } void send_msg(fcb_t *f, char let, char *s, int l) @@ -136,7 +243,7 @@ void send_msg(fcb_t *f, char let, char *s, int l) int ln; int myl = strlen(call)+2+l; - mp = cmsg_new(myl+4, f->sort, f); + mp = cmsg_new(myl+4+1, f->sort, f); ln = htonl(myl); memcpy(mp->inp, &ln, 4); mp->inp += 4; @@ -144,7 +251,7 @@ void send_msg(fcb_t *f, char let, char *s, int l) strcpy(mp->inp, call); mp->inp += strlen(call); *mp->inp++ = '|'; - if (l) { + if (l > 0) { memcpy(mp->inp, s, l); mp->inp += l; } @@ -153,10 +260,14 @@ void send_msg(fcb_t *f, char let, char *s, int l) f->sp->flags |= SEL_OUTPUT; } +/* + * the callback (called by sel_run) that handles all the inputs and outputs + */ + int fcb_handler(sel_t *sp, int in, int out, int err) { fcb_t *f = sp->fcb; - cmsg_t *mp; + cmsg_t *mp, *omp; /* input modes */ if (in) { @@ -184,36 +295,78 @@ int fcb_handler(sel_t *sp, int in, int out, int err) return 0; } + dbgdump(DBUF, "in ->", buf, r); + /* create a new message buffer if required */ if (!f->in) - f->in = cmsg_new(MAXBUFL, sp->sort, f); + f->in = cmsg_new(MAXBUFL+1, f->sort, f); mp = f->in; switch (f->sort) { case TEXT: p = buf; + if (f->echo) + omp = cmsg_new(3*r+1, f->sort, f); while (r > 0 && p < &buf[r]) { + + /* echo processing */ + if (f->echo) { + switch (*p) { + case '\b': + case 0x7f: + strcpy(omp->inp, "\b \b"); + omp->inp += strlen(omp->inp); + break; + default: + *omp->inp++ = *p; + } + } - /* - * if we have a nl then send the message upstairs - * start a new message - */ - - if (*p == nl) { - if (mp->inp == mp->data) - *mp->inp++ = ' '; - *mp->inp = 0; /* zero terminate it, but don't include it in the length */ - cmsg_send(f->inq, mp, 0); - f->in = mp = cmsg_new(MAXBUFL, sp->sort, f); - ++p; - } else { - if (mp->inp < &mp->data[MAXBUFL]) + /* character processing */ + switch (*p) { + case '\t': + if (int_tabs) { + memset(mp->inp, ' ', tabsize); + mp->inp += tabsize; + ++p; + } else { *mp->inp++ = *p++; - else { - mp->inp = mp->data; + } + break; + case '\b': + case 0x7f: + if (mp->inp > mp->data) + mp->inp--; + ++p; + break; + default: + if (nl == '\n' && *p == '\r') { /* ignore \r in telnet mode (ugh) */ + p++; + } else if (*p == nl) { + if (mp->inp == mp->data) + *mp->inp++ = ' '; + *mp->inp = 0; /* zero terminate it, but don't include it in the length */ + dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data); + cmsg_send(f->inq, mp, 0); + f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f); + ++p; + } else { + if (mp->inp < &mp->data[MAXBUFL-8]) + *mp->inp++ = *p++; + else { + mp->inp = mp->data; + } } } } + + /* queue any echo text */ + if (f->echo) { + dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data); + cmsg_send(f->outq, omp, 0); + f->sp->flags |= SEL_OUTPUT; + } + break; case MSG: @@ -224,18 +377,24 @@ int fcb_handler(sel_t *sp, int in, int out, int err) switch (mp->state) { case 0: case 1: + mp->state++; + break; case 2: case 3: - mp->size = (mp->size << 8) | *p++; + mp->size = (mp->size << 8) | (*p++ & 0xff); + if (mp->size > MAXBUFL) + die("Message size too big from node (%d > %d)", mp->size, MAXBUFL); mp->state++; break; default: if (mp->inp - mp->data < mp->size) { *mp->inp++ = *p++; - } else { + } + if (mp->inp - mp->data >= mp->size) { /* kick it upstairs */ + dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data); cmsg_send(f->inq, mp, 0); - mp = f->in = cmsg_new(MAXBUFL, f->sort, f); + mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f); } } } @@ -261,6 +420,9 @@ lout:; } l = mp->size - (mp->inp - mp->data); if (l > 0) { + + dbgdump(DBUF, "<-out", mp->inp, l); + r = write(f->cnum, mp->inp, l); if (r < 0) { switch (errno) { @@ -282,8 +444,6 @@ lout:; if (mp->inp - mp->data >= mp->size) { cmsg_callback(mp, 0); f->out = 0; - if (!is_chain_empty(f->outq)) - sp->flags &= ~SEL_OUTPUT; } } lend:; @@ -296,26 +456,65 @@ lend:; void initargs(int argc, char *argv[]) { - int i; - if (argc >= 2) { - call = strupper(argv[1]); - if (eq(call, "LOGIN")) - die("login not implemented (yet)"); + int i, c, err = 0; + + while ((c = getopt(argc, argv, "h:p:x:")) > 0) { + switch (c) { + case 'h': + node_addr = optarg; + break; + case 'l': + paclen = atoi(optarg); + if (paclen < 80) + paclen = 80; + if (paclen > MAXPACLEN) + paclen = MAXPACLEN; + break; + case 'p': + node_port = atoi(optarg); + break; + case 'x': + dbginit("client"); + dbgset(atoi(optarg)); + break; + default: + ++err; + goto lerr; + } + } + +lerr: + if (err) { + die("usage: client [-x n|-h|-p|-l] |login [local|telnet|ax25]"); + } + + if (optind < argc) { + call = strupper(argv[optind]); + ++optind; } if (!call) die("Must have at least a callsign (for now)"); - if (argc >= 3) { - connsort = strlower(argv[2]); + if (optind < argc) { + connsort = strlower(argv[optind]); if (eq(connsort, "telnet") || eq(connsort, "local")) { nl = '\n'; + echo = 1; } else if (eq(connsort, "ax25")) { nl = '\r'; + echo = 0; } else { die("2nd argument must be \"telnet\" or \"ax25\" or \"local\""); } } else { connsort = "local"; + nl = '\n'; + echo = 1; + } + + /* this is kludgy, but hey so is the rest of this! */ + if (!eq(connsort, "ax25") && paclen == DEFPACLEN) { + paclen = MAXPACLEN; } } @@ -353,33 +552,44 @@ void connect_to_node() void term_timeout(int i) { /* none of this is going to be reused so don't bother cleaning up properly */ - if (out) - out = 0; + if (in && in->t_set) + tcsetattr(0, TCSANOW, &in->t); if (node) { close(node->cnum); - node = 0; } exit(i); } void terminate(int i) { - if (send_Z && call) { + if (node && send_Z && call) { send_msg(node, 'Z', "", 0); } signal(SIGALRM, term_timeout); alarm(10); - while ((out && !is_chain_empty(out->outq)) || + while ((in && !is_chain_empty(in->outq)) || (node && !is_chain_empty(node->outq))) { sel_run(); } + if (in && in->t_set) + tcsetattr(0, TCSADRAIN, &in->t); if (node) close(node->cnum); exit(i); } +void login_timeout(int i) +{ + write(0, "Timed Out", 10); + write(0, &nl, 1); + sel_run(); /* force a coordination */ + if (in && in->t_set) + tcsetattr(0, TCSANOW, &in->t); + exit(i); +} + /* * things to do with ongoing processing of inputs */ @@ -388,7 +598,11 @@ void process_stdin() { cmsg_t *mp = cmsg_next(in->inq); if (mp) { - send_msg(node, 'I', mp->data, mp->size); + dbg(DMSG, "MSG size: %d", mp->size); + + if (mp->size > 0 && mp->inp > mp->data) { + send_msg(node, 'I', mp->data, mp->size); + } cmsg_callback(mp, 0); } } @@ -397,27 +611,39 @@ void process_node() { cmsg_t *mp = cmsg_next(node->inq); if (mp) { - char *p = strchr(mp->data, '|'); - if (p) - p++; - switch (mp->data[0]) { - case 'Z': - send_Z = 0; - ending++; - return; - case 'D': - if (p) { - int l = mp->inp - (unsigned char *) p; - send_text(out, p, l); + dbg(DMSG, "MSG size: %d", mp->size); + + if (mp->size > 0 && mp->inp > mp->data) { + char *p = strchr(mp->data, '|'); + if (p) + p++; + switch (mp->data[0]) { + case 'Z': + send_Z = 0; + ending++; + return; + case 'E': + if (isdigit(*p)) + in->echo = *p - '0'; + break; + case 'B': + if (isdigit(*p)) + in->buffer_it = *p - '0'; + break; + case 'D': + if (p) { + int l = mp->inp - (unsigned char *) p; + send_text(in, p, l); + } + break; + default: + break; } - break; - default: - break; - } + } cmsg_callback(mp, 0); + } else { + flush_text(in); } - if (is_chain_empty(out->outq)) - fsync(out->cnum); } /* @@ -426,21 +652,113 @@ void process_node() main(int argc, char *argv[]) { + /* set up environment */ + { + char *p = getenv("DXSPIDER_ROOT"); + if (p) + root = p; + p = getenv("DXSPIDER_HOST"); + if (p) + node_addr = p; + p = getenv("DXSPIDER_PORT"); + if (p) + node_port = atoi(p); + p = getenv("DXSPIDER_PACLEN"); + if (p) { + paclen = atoi(p); + if (paclen < 80) + paclen = 80; + if (paclen > MAXPACLEN) + paclen = MAXPACLEN; + } + } + + /* get program arguments, initialise stuff */ initargs(argc, argv); sel_init(10, 0, 10000); + /* trap signals */ signal(SIGHUP, SIG_IGN); - signal(SIGINT, terminate); signal(SIGQUIT, terminate); signal(SIGTERM, terminate); +#ifdef SIGPWR signal(SIGPWR, terminate); +#endif + + /* compile regexes for iscallsign */ + { + myregex_t *rp; + for (rp = iscallreg; rp->in; ++rp) { + regex_t reg; + int r = regcomp(®, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB); + if (r) + die("regcomp returned %d for '%s'", r, rp->in); + rp->regex = malloc(sizeof(regex_t)); + if (!rp->regex) + die("out of room - compiling regexes"); + *rp->regex = reg; + } + } + + /* is this a login? */ + if (eq(call, "LOGIN")) { + char buf[MAXPACLEN+1]; + int r; + int f = xopen("data", "issue", 0); + if (f > 0) { + while ((r = read(f, buf, paclen)) > 0) { + if (nl != '\n') { + char *p; + for (p = buf; p < &buf[r]; ++p) { + if (*p == '\n') + *p = nl; + } + } + write(0, buf, r); + } + close(f); + } + signal(SIGALRM, login_timeout); + alarm(timeout); + write(0, "login: ", 7); + r = read(0, buf, 20); + if (r <= 0) + die("No login or error (%d)", errno); + signal(SIGALRM, SIG_IGN); + alarm(0); + while (r > 0) { + if (buf[r-1] == ' ' || buf[r-1] == '\r' || buf[r-1] == '\n') + --r; + else + break; + } + buf[r] = 0; + call = strupper(buf); + } - /* connect up stdin, stdout and message system */ + /* check the callsign */ + if (!iscallsign(call)) { + die("Sorry, %s isn't a valid callsign", call); + } + + /* connect up stdin */ in = fcb_new(0, TEXT); in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT); - out = fcb_new(1, TEXT); - out->sp = sel_open(1, out, "STDOUT", fcb_handler, TEXT, 0); + if (tcgetattr(0, &in->t) < 0) { + echo = 0; + in->t_set = 0; + } else { + struct termios t = in->t; + t.c_lflag &= ~(ECHO|ECHONL|ICANON); + if (tcsetattr(0, TCSANOW, &t) < 0) + die("tcsetattr (%d)", errno); + in->echo = echo; + in->t_set = 1; + } + in->buffer_it = 1; + + /* connect up node */ connect_to_node(); /* tell the cluster who I am */ @@ -461,3 +779,4 @@ main(int argc, char *argv[]) +