[PATCH 1/8] factor out common dump_buf_hex() and free_optlist() utility functions

Daniel Lenski dlenski at gmail.com
Sat May 20 15:43:22 PDT 2017


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 at gmail.com>
---
 cstp.c                 | 16 ++--------------
 http.c                 | 16 ++++++++++++++++
 library.c              |  3 +--
 oncp.c                 | 36 +++++++++++-------------------------
 openconnect-internal.h |  2 ++
 5 files changed, 32 insertions(+), 41 deletions(-)

diff --git a/cstp.c b/cstp.c
index 2fd7a62..5477c5c 100644
--- 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 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/library.c b/library.c
index 2f0392b..41e164a 100644
--- 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 0155f41..59cfa4b 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 };
 
@@ -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,
diff --git a/openconnect-internal.h b/openconnect-internal.h
index 67b73f4..1a67715 100644
--- a/openconnect-internal.h
+++ b/openconnect-internal.h
@@ -1004,6 +1004,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, ...);
@@ -1054,6 +1055,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);
-- 
2.7.4




More information about the openconnect-devel mailing list