]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
save four bytes in HDLC malloc
authorDaniel Lenski <dlenski@gmail.com>
Wed, 13 May 2020 07:05:55 +0000 (00:05 -0700)
committerDaniel Lenski <dlenski@gmail.com>
Wed, 13 May 2020 14:23:06 +0000 (07:23 -0700)
Signed-off-by: Daniel Lenski <dlenski@gmail.com>
openconnect-internal.h
ppp.c

index 8a04992b6a4ce3d5c7b48e7a4aa7f4a38c65c55d..f616f1b35bdbe4572bb3659712fd37735ecca249 100644 (file)
@@ -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 54646d49fbaaf1e540acc50188be299f712ba8b2..350985dfa932b1b8aa708a3012e3c78e010d313c 100644 (file)
--- 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;
 }