Change in osmo-sysmon[master]: openvpn: Fix garbage printed after remote port

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Fri Mar 15 19:59:02 UTC 2019


Pau Espin Pedrol has uploaded this change for review. ( https://gerrit.osmocom.org/13286


Change subject: openvpn: Fix garbage printed after remote port
......................................................................

openvpn: Fix garbage printed after remote port

Last strtok() returns chunk from last "," until end of strig. Since
openvpn sends a trailing new line + "END" string, that was being copied
into the remote port.

Change-Id: Ie24fed9c64bc482fd5af8bc9d03d37937c6471bf
---
M src/osysmon_openvpn.c
1 file changed, 18 insertions(+), 3 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-sysmon refs/changes/86/13286/1

diff --git a/src/osysmon_openvpn.c b/src/osysmon_openvpn.c
index 672fd51..3c03099 100644
--- a/src/osysmon_openvpn.c
+++ b/src/osysmon_openvpn.c
@@ -59,11 +59,19 @@
 	bool connected;
 };
 
+/*
+ * The string format is documented in https://openvpn.net/community-resources/management-interface/
+ * Conversation loop looks like this (non-null terminated strings):
+ * CLI -> SRV: "state"
+ * SRV -> CLI: "1552674378,CONNECTED,SUCCESS,10.8.0.11,144.76.43.77,1194,," {0x0d, 0xa} "END" {0x0d, 0xa}
+ * More or less comma-separated fields are returned depending on OpenVPN version.
+ * v2.1.3: Sends only up to field 4 incl, eg: "1552674378,CONNECTED,SUCCESS,10.8.0.11,144.76.43.77" {0x0d, 0xa} "END" {0x0d, 0xa}
+ */
 static char *parse_state(struct msgb *msg, struct openvpn_client *vpn)
 {
 	char tmp[128], buf[128];
 	char *tok;
-	unsigned int i = 0;
+	unsigned int i;
 	uint8_t *m = msgb_data(msg);
 	unsigned int truncated_len = OSMO_MIN(sizeof(tmp) - 1, msgb_length(msg));
 
@@ -81,8 +89,15 @@
 	memcpy(tmp, m, truncated_len);
 	tmp[truncated_len] = '\0';
 
-	for (tok = strtok(tmp, ","); tok && i < MAX_RESP_COMPONENTS; tok = strtok(NULL, ",")) {
-		/* The string format is documented in https://openvpn.net/community-resources/management-interface/ */
+	/* Only interested in first line; Remove return carriage, line feed + new line string "END" */
+	for (i = 0; i < truncated_len; i++) {
+		if (!isprint(tmp[i])) {
+			tmp[i] = '\0';
+			break;
+		}
+	}
+
+	for (tok = strtok(tmp, ","), i = 0; tok && i < MAX_RESP_COMPONENTS; tok = strtok(NULL, ",")) {
 		if (tok) { /* Parse csv string and pick interesting tokens while ignoring the rest. */
 			switch (i++) {
 			/* case 0: unix/date time, not needed */

-- 
To view, visit https://gerrit.osmocom.org/13286
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-sysmon
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie24fed9c64bc482fd5af8bc9d03d37937c6471bf
Gerrit-Change-Number: 13286
Gerrit-PatchSet: 1
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190315/e419442e/attachment.html>


More information about the gerrit-log mailing list