]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
Skip dump_buf_hex() when the log level is low enough
authorDimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
Sat, 26 Mar 2022 20:12:56 +0000 (21:12 +0100)
committerDimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
Sat, 9 Apr 2022 10:02:55 +0000 (12:02 +0200)
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
openconnect-internal.h

diff --git a/http.c b/http.c
index fc6538c16c2b839502249eec8d74edc718be6e73..02f0a135037d16c3c11913417d6a5178ef2bf780 100644 (file)
--- 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++) {
index 644f51bf0e88eef11dba6f60d41aa3298e32578a..8eb105b1f1bca20611235c590116a76c4f08d743 100644 (file)
@@ -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);