]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
factor out common dump_buf_hex() and free_optlist() utility functions
authorDaniel Lenski <dlenski@gmail.com>
Sat, 20 May 2017 22:43:22 +0000 (15:43 -0700)
committerDavid Woodhouse <dwmw2@infradead.org>
Mon, 14 Aug 2017 14:26:20 +0000 (15:26 +0100)
These will be used in GlobalProtect protocol support, so it makes sense
to factor them out into shared utility functions rather than use slight
variants for each protocol.

Signed-off-by: Daniel Lenski <dlenski@gmail.com>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
cstp.c
http.c
library.c
oncp.c
openconnect-internal.h

diff --git a/cstp.c b/cstp.c
index 2fd7a62e3085f4661654eaf56c082e70243cdb65..5477c5c82469ea4920b64b4f208a80a80775bb78 100644 (file)
--- a/cstp.c
+++ b/cstp.c
@@ -609,20 +609,8 @@ static int start_cstp_connection(struct openconnect_info *vpninfo)
                }
        }
 
-       while (old_dtls_opts) {
-               struct oc_vpn_option *tmp = old_dtls_opts;
-               old_dtls_opts = old_dtls_opts->next;
-               free(tmp->value);
-               free(tmp->option);
-               free(tmp);
-       }
-       while (old_cstp_opts) {
-               struct oc_vpn_option *tmp = old_cstp_opts;
-               old_cstp_opts = old_cstp_opts->next;
-               free(tmp->value);
-               free(tmp->option);
-               free(tmp);
-       }
+       free_optlist(old_dtls_opts);
+       free_optlist(old_cstp_opts);
        vpn_progress(vpninfo, PRG_INFO, _("CSTP connected. DPD %d, Keepalive %d\n"),
                     vpninfo->ssl_times.dpd, vpninfo->ssl_times.keepalive);
        vpn_progress(vpninfo, PRG_DEBUG, _("CSTP Ciphersuite: %s\n"),
diff --git a/http.c b/http.c
index 6166bb3a178aa88476574d5e9cde62ccbf524eb0..59f93e50f77247a799d518be53d5042a84a50be9 100644 (file)
--- 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
index 2f0392b6b7341653fe78a18d71f20e2913863b57..41e164a277edbd0f34dbe6e6f50cf86fbd2f6e10 100644 (file)
--- a/library.c
+++ b/library.c
@@ -257,7 +257,7 @@ int openconnect_set_mobile_info(struct openconnect_info *vpninfo,
        return 0;
 }
 
-static void free_optlist(struct oc_vpn_option *opt)
+void free_optlist(struct oc_vpn_option *opt)
 {
        struct oc_vpn_option *next;
 
@@ -1127,4 +1127,3 @@ retry:
 
        return ret;
 }
-
diff --git a/oncp.c b/oncp.c
index 0155f416e4e04a49113002a9dfc11424adeeed6b..59cfa4be277ca3e9d7232a1d1940026d45ec41cd 100644 (file)
--- 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 };
 
@@ -503,7 +487,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;
        }
 
@@ -663,7 +647,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) {
@@ -681,7 +665,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"),
@@ -709,7 +693,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;
        }
@@ -814,7 +798,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;
@@ -1091,8 +1076,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"),
@@ -1111,8 +1096,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,
index 9d98590dfd3923abc920c1913c07de7eff500c83..f8f92492714eec3cf51326c4edfb2511f7f5b0a4 100644 (file)
@@ -995,6 +995,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, ...);
@@ -1045,6 +1046,7 @@ int digest_authorization(struct openconnect_info *vpninfo, int proxy, struct htt
 
 /* library.c */
 void nuke_opt_values(struct oc_form_opt *opt);
+void free_optlist(struct oc_vpn_option *opt);
 int process_auth_form(struct openconnect_info *vpninfo, struct oc_auth_form *form);
 /* This is private for now since we haven't yet worked out what the API will be */
 void openconnect_set_juniper(struct openconnect_info *vpninfo);