Handle the detection of frames even if we read them

with more than one read().  When we detect one, don't
forget to pass it to async_Input() and drop our
terminal back into command mode.

Don't output an extraneous \r if we're passed \r\n
to prompt_vprintf in raw mode.
This commit is contained in:
Brian Somers 1999-04-03 11:54:00 +00:00
parent b8ec435e6b
commit e304484545
5 changed files with 72 additions and 47 deletions

View File

@ -17,7 +17,7 @@
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id: hdlc.c,v 1.39 1999/02/11 10:14:08 brian Exp $
* $Id: hdlc.c,v 1.40 1999/03/29 08:21:26 brian Exp $
*
* TODO:
*/
@ -558,35 +558,41 @@ hdlc_Input(struct bundle *bundle, struct mbuf * bp, struct physical *physical)
}
/*
* Detect a HDLC frame
* Detect a HDLC frame
*/
static const char *FrameHeaders[] = {
"\176\377\003\300\041",
"\176\377\175\043\300\041",
"\176\177\175\043\100\041",
"\176\175\337\175\043\300\041",
"\176\175\137\175\043\100\041",
NULL,
static const struct frameheader {
const u_char *data;
int len;
} FrameHeaders[] = {
{ "\176\377\003\300\041", 5 },
{ "\176\377\175\043\300\041", 6 },
{ "\176\177\175\043\100\041", 6 },
{ "\176\175\337\175\043\300\041", 7 },
{ "\176\175\137\175\043\100\041", 7 },
{ NULL, 0 }
};
u_char *
hdlc_Detect(struct physical *physical, u_char *cp, int n)
int
hdlc_Detect(u_char const **cp, int n, int issync)
{
const char *fp, **hp;
char *ptr;
const struct frameheader *fh;
const u_char *h;
size_t len, cmp;
cp[n] = '\0'; /* be sure to null terminate */
ptr = NULL;
for (hp = FrameHeaders; *hp; hp++) {
fp = *hp;
if (physical_IsSync(physical))
fp++;
ptr = strstr((char *)cp, fp); /* XXX: cp may have embedded NULs */
if (ptr)
break;
while (n) {
for (fh = FrameHeaders; fh->len; fh++) {
h = issync ? fh->data + 1 : fh->data;
len = issync ? fh->len - 1 : fh->len;
cmp = n >= len ? len : n;
if (memcmp(*cp, h, cmp) == 0)
return cmp == len;
}
n--;
(*cp)++;
}
return (u_char *)ptr;
return 0;
}
int

View File

@ -15,7 +15,7 @@
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id: hdlc.h,v 1.14.2.12 1998/05/01 19:24:39 brian Exp $
* $Id: hdlc.h,v 1.15 1998/05/21 21:45:30 brian Exp $
*
* TODO:
*/
@ -112,4 +112,4 @@ extern void hdlc_DecodePacket(struct bundle *, u_short, struct mbuf *,
extern void hdlc_Input(struct bundle *, struct mbuf *, struct physical *);
extern void hdlc_Output(struct link *, int, u_short, struct mbuf *bp);
extern u_short hdlc_Fcs(u_short, u_char *, int);
extern u_char *hdlc_Detect(struct physical *, u_char *, int);
extern int hdlc_Detect(u_char const **, int, int);

View File

@ -17,7 +17,7 @@
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id: modem.c,v 1.106 1999/03/07 01:41:27 brian Exp $
* $Id: modem.c,v 1.107 1999/03/07 20:58:48 brian Exp $
*
* TODO:
*/
@ -122,7 +122,7 @@ modem_Create(struct datalink *dl, int type)
p->out = NULL;
p->connect_count = 0;
p->dl = dl;
p->input.sz = 0;
*p->name.full = '\0';
p->name.base = p->name.full;
@ -489,6 +489,7 @@ modem_Found(struct physical *modem, struct bundle *bundle)
throughput_start(&modem->link.throughput, "modem throughput",
Enabled(bundle, OPT_THROUGHPUT));
modem->connect_count++;
modem->input.sz = 0;
log_Printf(LogPHASE, "%s: Connected!\n", modem->link.name);
}
@ -852,16 +853,15 @@ modem_DescriptorWrite(struct descriptor *d, struct bundle *bundle,
const fd_set *fdset)
{
struct physical *modem = descriptor2physical(d);
int nb, nw, result = 0;
int nw, result = 0;
if (modem->out == NULL)
modem->out = link_Dequeue(&modem->link);
if (modem->out) {
nb = modem->out->cnt;
nw = physical_Write(modem, MBUF_CTOP(modem->out), nb);
nw = physical_Write(modem, MBUF_CTOP(modem->out), modem->out->cnt);
log_Printf(LogDEBUG, "%s: DescriptorWrite: wrote %d(%d) to %d\n",
modem->link.name, nw, nb, modem->fd);
modem->link.name, nw, modem->out->cnt, modem->fd);
if (nw > 0) {
modem->out->cnt -= nw;
modem->out->offset += nw;
@ -963,13 +963,16 @@ modem_DescriptorRead(struct descriptor *d, struct bundle *bundle,
const fd_set *fdset)
{
struct physical *p = descriptor2physical(d);
u_char rbuff[MAX_MRU], *cp;
int n;
u_char *rbuff;
int n, found;
size_t sz;
rbuff = p->input.buf + p->input.sz;
/* something to read from modem */
n = physical_Read(p, rbuff, sizeof rbuff);
log_Printf(LogDEBUG, "%s: DescriptorRead: read %d from %d\n",
p->link.name, n, p->fd);
n = physical_Read(p, rbuff, sizeof p->input.buf - p->input.sz);
log_Printf(LogDEBUG, "%s: DescriptorRead: read %d/%d from %d\n",
p->link.name, n, (int)(sizeof p->input.buf - p->input.sz), p->fd);
if (n <= 0) {
if (n < 0)
log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd,
@ -980,23 +983,32 @@ modem_DescriptorRead(struct descriptor *d, struct bundle *bundle,
datalink_Down(p->dl, CLOSE_NORMAL);
return;
}
log_DumpBuff(LogASYNC, "ReadFromModem", rbuff, n);
rbuff -= p->input.sz;
n += p->input.sz;
if (p->link.lcp.fsm.state <= ST_CLOSED) {
/* In -dedicated mode, we just discard input until LCP is started */
if (p->type != PHYS_DEDICATED) {
cp = hdlc_Detect(p, rbuff, n);
if (cp) {
found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p));
if (rbuff != p->input.buf)
log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf),
p->input.buf);
p->input.sz = n - (rbuff - p->input.buf);
if (found) {
/* LCP packet is detected. Turn ourselves into packet mode */
if (cp != rbuff)
/* Get rid of the bit before the HDLC header */
log_WritePrompts(p->dl, "%.*s\r\n", (int)(cp - rbuff), rbuff);
log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n",
p->link.name);
log_SetTtyCommandMode(p->dl);
datalink_Up(p->dl, 0, 1);
async_Input(bundle, rbuff, p->input.sz, p);
p->input.sz = 0;
} else
log_WritePrompts(p->dl, "%.*s", n, rbuff);
}
bcopy(rbuff, p->input.buf, p->input.sz);
} else
/* In -dedicated mode, we just discard input until LCP is started */
p->input.sz = 0;
} else if (n > 0)
async_Input(bundle, rbuff, n, p);
}
@ -1062,6 +1074,7 @@ iov2modem(struct datalink *dl, struct iovec *iov, int *niov, int maxiov, int fd)
throughput_start(&p->link.throughput, "modem throughput",
Enabled(dl->bundle, OPT_THROUGHPUT));
p->input.sz = 0;
if (p->Timer.state != TIMER_STOPPED) {
p->Timer.state = TIMER_STOPPED; /* Special - see modem2iov() */
modem_StartTimer(dl->bundle, p); /* XXX: Should we set cd.required ? */

View File

@ -16,7 +16,7 @@
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id: physical.h,v 1.5 1999/01/10 01:26:30 brian Exp $
* $Id: physical.h,v 1.6 1999/02/16 00:16:56 brian Exp $
*
*/
@ -35,6 +35,11 @@ struct physical {
int connect_count;
struct datalink *dl; /* my owner */
struct {
u_char buf[MAX_MRU]; /* Our input data buffer */
size_t sz;
} input;
struct {
char full[40];
char *base;

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: prompt.c,v 1.12 1998/08/26 17:39:37 brian Exp $
* $Id: prompt.c,v 1.13 1999/01/28 01:56:34 brian Exp $
*/
#include <sys/param.h>
@ -380,7 +380,8 @@ prompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
/* Stuff '\r' in front of '\n' 'cos we're in raw mode */
int len = strlen(fmt);
if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n') {
if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n' &&
(len == 1 || fmt[len-2] != '\r')) {
strcpy(nfmt, fmt);
strcpy(nfmt + len - 1, "\r\n");
pfmt = nfmt;