From 783753f1d218454bd24c3f33f12ddf6cde8aed78 Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Wed, 13 May 2020 00:05:55 -0700 Subject: [PATCH] save four bytes in HDLC malloc Signed-off-by: Daniel Lenski --- openconnect-internal.h | 2 +- ppp.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/openconnect-internal.h b/openconnect-internal.h index 8a04992b..f616f1b3 100644 --- a/openconnect-internal.h +++ b/openconnect-internal.h @@ -152,8 +152,8 @@ struct pkt { } pulse; struct { uint32_t hlen; /* variable-length */ - unsigned char pad[18]; uint16_t proto; + unsigned char hdr[18]; } ppp; }; unsigned char data[]; diff --git a/ppp.c b/ppp.c index 54646d49..350985df 100644 --- a/ppp.c +++ b/ppp.c @@ -72,11 +72,14 @@ static struct pkt *hdlc_into_new_pkt(struct openconnect_info *vpninfo, unsigned const unsigned char *inp = bytes, *endp = bytes + len; unsigned char *outp; uint16_t fcs = PPPINITFCS16; - struct pkt *p = malloc(sizeof(struct pkt) + len*2 + 6); + /* Every byte in payload and 2-byte FCS potentially expands to two bytes, + * plus 2 for flag (0x7e) at start and end. We know that we will output + * at least 4 bytes so we can stash those in the header. */ + struct pkt *p = malloc(sizeof(struct pkt) + len*2 + 2); if (!p) return NULL; - outp = p->data; + outp = p->data - 4; *outp++ = 0x7e; for (; inp < endp; inp++) { @@ -90,7 +93,7 @@ static struct pkt *hdlc_into_new_pkt(struct openconnect_info *vpninfo, unsigned HDLC_OUT(outp, fcs >> 8, asyncmap); *outp++ = 0x7e; - p->ppp.hlen = 0; + p->ppp.hlen = 4; p->len = outp - p->data; return p; } -- 2.49.0