From 76eae4aef7c10412c18a73ff702595809f9fffb2 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com> Date: Sat, 26 Mar 2022 21:12:56 +0100 Subject: [PATCH] Skip dump_buf_hex() when the log level is low enough This is a performance fix. Indeed, dump_buf_hex() is heavy enough to significantly slow down OpenConnect. So avoid calling dump_buf_hex() when the log level does not require it. Before this patch, the whole body of dump_buf_hex() was executed, even when vpn_progress() wouldn't print anything. Same for dump_buf(), although the performance hit is less important. Signed-off-by: Dimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com> --- http.c | 8 +++++--- openconnect-internal.h | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/http.c b/http.c index fc6538c1..02f0a135 100644 --- a/http.c +++ b/http.c @@ -699,7 +699,7 @@ int handle_redirect(struct openconnect_info *vpninfo) } } -void dump_buf(struct openconnect_info *vpninfo, char prefix, char *buf) +void do_dump_buf(struct openconnect_info *vpninfo, char prefix, char *buf) { while (*buf) { char *eol = buf; @@ -725,12 +725,14 @@ 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) +void do_dump_buf_hex(struct openconnect_info *vpninfo, int loglevel, char prefix, unsigned char *buf, int len) { struct oc_text_buf *line = buf_alloc(); - int i, j; + int i; for (i = 0; i < len; i+=16) { + int j; + buf_truncate(line); buf_append(line, "%04x:", i); for (j = i; j < i+16; j++) { diff --git a/openconnect-internal.h b/openconnect-internal.h index 644f51bf..8eb105b1 100644 --- a/openconnect-internal.h +++ b/openconnect-internal.h @@ -892,8 +892,8 @@ static inline void free_pkt(struct openconnect_info *vpninfo, struct pkt *pkt) free(pkt); } -#define vpn_progress(_v, lvl, ...) do { \ - if ((_v)->verbose >= (lvl)) \ +#define vpn_progress(_v, lvl, ...) do { \ + if ((_v)->verbose >= (lvl)) \ (_v)->progress((_v)->cbdata, lvl, __VA_ARGS__); \ } while(0) #define vpn_perror(vpninfo, msg) vpn_progress((vpninfo), PRG_ERR, "%s: %s\n", (msg), strerror(errno)) @@ -1520,8 +1520,8 @@ void buf_append_from_utf16le(struct oc_text_buf *buf, const void *utf16); void buf_append_base64(struct oc_text_buf *buf, const void *bytes, int len, int line_len); /* http.c */ -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); +void do_dump_buf(struct openconnect_info *vpninfo, char prefix, char *buf); +void do_dump_buf_hex(struct openconnect_info *vpninfo, int loglevel, char prefix, unsigned char *buf, int len); char *openconnect_create_useragent(const char *base); int process_proxy(struct openconnect_info *vpninfo, int ssl_sock); int internal_parse_url(const char *url, char **res_proto, char **res_host, @@ -1539,6 +1539,16 @@ int process_http_response(struct openconnect_info *vpninfo, int connect, struct oc_text_buf *body); int handle_redirect(struct openconnect_info *vpninfo); void http_common_headers(struct openconnect_info *vpninfo, struct oc_text_buf *buf); +#define dump_buf(vpninfo, prefix, buf) do { \ + if ((vpninfo)->verbose >= PRG_DEBUG) { \ + do_dump_buf(vpninfo, prefix, buf); \ + } \ + } while(0) +#define dump_buf_hex(vpninfo, loglevel, prefix, buf, len) do { \ + if ((vpninfo)->verbose >= (loglevel)) { \ + do_dump_buf_hex(vpninfo, loglevel, prefix, buf, len); \ + } \ + } while(0) /* http-auth.c */ void *openconnect_base64_decode(int *len, const char *in); -- 2.50.1