This function was duplicated across oncp.c, cstp.c, and also makes an appearance in the GlobalProtect fork? so I de-duplicated it. -Dan On Wed, Apr 19, 2017 at 2:45 PM, Daniel Lenski <dlenski at gmail.com> wrote: > Also adds loglevel and prefix character parameters > > Signed-off-by: Daniel Lenski <dlenski at gmail.com> > --- > http.c | 16 ++++++++++++++++ > oncp.c | 36 +++++++++++------------------------- > openconnect-internal.h | 1 + > 3 files changed, 28 insertions(+), 25 deletions(-) > > diff --git a/http.c b/http.c > index 6166bb3..59f93e5 100644 > --- a/http.c > +++ b/http.c > @@ -781,6 +781,22 @@ void dump_buf(struct openconnect_info *vpninfo, char prefix, char *buf) > } > } > > +void dump_buf_hex(struct openconnect_info *vpninfo, int loglevel, char prefix, unsigned char *buf, int len) > +{ > + char linebuf[80]; > + int i; > + > + for (i = 0; i < len; i++) { > + if (i % 16 == 0) { > + if (i) > + vpn_progress(vpninfo, loglevel, "%c %s\n", prefix, linebuf); > + sprintf(linebuf, "%04x:", i); > + } > + sprintf(linebuf + strlen(linebuf), " %02x", buf[i]); > + } > + vpn_progress(vpninfo, loglevel, "%c %s\n", prefix, linebuf); > +} > + > /* Inputs: > * method: GET or POST > * vpninfo->hostname: Host DNS name > diff --git a/oncp.c b/oncp.c > index 3c7cfa1..f27d96a 100644 > --- a/oncp.c > +++ b/oncp.c > @@ -110,22 +110,6 @@ static void buf_append_tlv_be32(struct oc_text_buf *buf, uint16_t val, uint32_t > buf_append_tlv(buf, val, 4, d); > } > > -static void buf_hexdump(struct openconnect_info *vpninfo, unsigned char *d, int len) > -{ > - char linebuf[80]; > - int i; > - > - for (i = 0; i < len; i++) { > - if (i % 16 == 0) { > - if (i) > - vpn_progress(vpninfo, PRG_DEBUG, "%s\n", linebuf); > - sprintf(linebuf, "%04x:", i); > - } > - sprintf(linebuf + strlen(linebuf), " %02x", d[i]); > - } > - vpn_progress(vpninfo, PRG_DEBUG, "%s\n", linebuf); > -} > - > static const char authpkt_head[] = { 0x00, 0x04, 0x00, 0x00, 0x00 }; > static const char authpkt_tail[] = { 0xbb, 0x01, 0x00, 0x00, 0x00, 0x00 }; > > @@ -497,7 +481,7 @@ static int parse_conf_pkt(struct openconnect_info *vpninfo, unsigned char *bytes > eparse: > vpn_progress(vpninfo, PRG_ERR, > _("Failed to parse KMP message\n")); > - buf_hexdump(vpninfo, bytes, pktlen); > + dump_buf_hex(vpninfo, PRG_ERR, '<', bytes, pktlen); > return -EINVAL; > } > > @@ -648,7 +632,7 @@ int oncp_connect(struct openconnect_info *vpninfo) > ret = buf_error(reqbuf); > goto out; > } > - buf_hexdump(vpninfo, (void *)reqbuf->data, reqbuf->pos); > + dump_buf_hex(vpninfo, PRG_DEBUG, '>', (void *)reqbuf->data, reqbuf->pos); > ret = vpninfo->ssl_write(vpninfo, reqbuf->data, reqbuf->pos); > if (ret != reqbuf->pos) { > if (ret >= 0) { > @@ -666,7 +650,7 @@ int oncp_connect(struct openconnect_info *vpninfo) > goto out; > vpn_progress(vpninfo, PRG_TRACE, > _("Read %d bytes of SSL record\n"), ret); > - > + > if (ret != 3 || bytes[0] != 1 || bytes[1] != 0) { > vpn_progress(vpninfo, PRG_ERR, > _("Unexpected response of size %d after hostname packet\n"), > @@ -694,7 +678,7 @@ int oncp_connect(struct openconnect_info *vpninfo) > if (len < 0x16 || load_le16(bytes) + 2 != len) { > vpn_progress(vpninfo, PRG_ERR, > _("Invalid packet waiting for KMP 301\n")); > - buf_hexdump(vpninfo, bytes, len); > + dump_buf_hex(vpninfo, PRG_ERR, '<', bytes, len); > ret = -EINVAL; > goto out; > } > @@ -798,7 +782,8 @@ int oncp_connect(struct openconnect_info *vpninfo) > /* Length at the start of the packet is little-endian */ > store_le16(reqbuf->data, reqbuf->pos - 2); > > - buf_hexdump(vpninfo, (void *)reqbuf->data, reqbuf->pos); > + vpn_progress(vpninfo, PRG_DEBUG, _("oNCP negotiation request outgoing:\n")); > + dump_buf_hex(vpninfo, PRG_DEBUG, '>', (void *)reqbuf->data, reqbuf->pos); > ret = vpninfo->ssl_write(vpninfo, reqbuf->data, reqbuf->pos); > if (ret == reqbuf->pos) > ret = 0; > @@ -1070,8 +1055,8 @@ int oncp_mainloop(struct openconnect_info *vpninfo, int *timeout) > unknown_pkt: > vpn_progress(vpninfo, PRG_ERR, > _("Unknown KMP message %d of size %d:\n"), kmp, kmplen); > - buf_hexdump(vpninfo, vpninfo->cstp_pkt->oncp.kmp, > - vpninfo->cstp_pkt->len); > + dump_buf_hex(vpninfo, PRG_ERR, '<', vpninfo->cstp_pkt->oncp.kmp, > + vpninfo->cstp_pkt->len); > if (kmplen + 20 != vpninfo->cstp_pkt->len) > vpn_progress(vpninfo, PRG_DEBUG, > _(".... + %d more bytes unreceived\n"), > @@ -1090,8 +1075,9 @@ int oncp_mainloop(struct openconnect_info *vpninfo, int *timeout) > unmonitor_write_fd(vpninfo, ssl); > > vpn_progress(vpninfo, PRG_TRACE, _("Packet outgoing:\n")); > - buf_hexdump(vpninfo, vpninfo->current_ssl_pkt->oncp.rec, > - vpninfo->current_ssl_pkt->len + 22); > + dump_buf_hex(vpninfo, PRG_TRACE, '>', > + vpninfo->current_ssl_pkt->oncp.rec, > + vpninfo->current_ssl_pkt->len + 22); > > ret = ssl_nonblock_write(vpninfo, > vpninfo->current_ssl_pkt->oncp.rec, > diff --git a/openconnect-internal.h b/openconnect-internal.h > index a24a9e4..2bf0015 100644 > --- a/openconnect-internal.h > +++ b/openconnect-internal.h > @@ -987,6 +987,7 @@ int can_gen_tokencode(struct openconnect_info *vpninfo, > /* http.c */ > struct oc_text_buf *buf_alloc(void); > void dump_buf(struct openconnect_info *vpninfo, char prefix, char *buf); > +void dump_buf_hex(struct openconnect_info *vpninfo, int loglevel, char prefix, unsigned char *buf, int len); > int buf_ensure_space(struct oc_text_buf *buf, int len); > void __attribute__ ((format (printf, 2, 3))) > buf_append(struct oc_text_buf *buf, const char *fmt, ...); > -- > 2.7.4 >