From 970e217380aea6cec1819815a0f7922d9fda81a1 Mon Sep 17 00:00:00 2001 From: Davide Beatrici Date: Thu, 5 Apr 2018 22:28:23 +0200 Subject: [PATCH] OpenVPN: merge OvsParseOptions() and OvsParsePeerInfo() into a single function --- src/Cedar/Interop_OpenVPN.c | 47 +++++-------------------------------- src/Cedar/Interop_OpenVPN.h | 7 ++++-- 2 files changed, 11 insertions(+), 43 deletions(-) diff --git a/src/Cedar/Interop_OpenVPN.c b/src/Cedar/Interop_OpenVPN.c index 7576d96a..9ebc1c33 100644 --- a/src/Cedar/Interop_OpenVPN.c +++ b/src/Cedar/Interop_OpenVPN.c @@ -707,7 +707,7 @@ void OvsBeginIPCAsyncConnectionIfEmpty(OPENVPN_SERVER *s, OPENVPN_SESSION *se, O // if the option --push-peer-info is enabled. // It also sends all of the client's environment // variables whose names start with "UV_". - pi = OvsParsePeerInfo(c->ClientKey.PeerInfo); + pi = OvsParseData(c->ClientKey.PeerInfo, OPENVPN_DATA_PEERINFO); // Check presence of custom hostname if (OvsHasEntry(pi, "UV_HOSTNAME")) @@ -832,7 +832,7 @@ void OvsSetupSessionParameters(OPENVPN_SERVER *s, OPENVPN_SESSION *se, OPENVPN_C StrCpy(opt_str, sizeof(opt_str), s->Cedar->OpenVPNDefaultClientOption); } - o = OvsParseOptions(opt_str); + o = OvsParseData(opt_str, OPENVPN_DATA_OPTIONS); if (se->Mode == OPENVPN_MODE_UNKNOWN) { @@ -984,13 +984,13 @@ MD *OvsGetMd(char *name) return m; } -// Parse the option string -LIST *OvsParseOptions(char *str) +// Parse data string +LIST *OvsParseData(char *str, int type) { LIST *o = NewListFast(NULL); TOKEN_LIST *t; - t = ParseTokenWithoutNullStr(str, ","); + t = ParseTokenWithoutNullStr(str, type == OPENVPN_DATA_OPTIONS ? "," : "\n"); if (t != NULL) { UINT i; @@ -1002,42 +1002,7 @@ LIST *OvsParseOptions(char *str) char *line = t->Token[i]; Trim(line); - if (GetKeyAndValue(line, key, sizeof(key), value, sizeof(value), " \t")) - { - INI_ENTRY *e = ZeroMalloc(sizeof(INI_ENTRY)); - - e->Key = CopyStr(key); - e->Value = CopyStr(value); - - Add(o, e); - } - } - - FreeToken(t); - } - - return o; -} - -// Parse the peer info string -LIST *OvsParsePeerInfo(char *str) -{ - LIST *o = NewListFast(NULL); - TOKEN_LIST *t; - - t = ParseTokenWithoutNullStr(str, "\n"); - if (t != NULL) - { - UINT i; - - for (i = 0;i < t->NumTokens;i++) - { - char key[MAX_SIZE]; - char value[MAX_SIZE]; - char *line = t->Token[i]; - Trim(line); - - if (GetKeyAndValue(line, key, sizeof(key), value, sizeof(value), "=\t")) + if (GetKeyAndValue(line, key, sizeof(key), value, sizeof(value), type == OPENVPN_DATA_OPTIONS ? " \t" : "=\t")) { INI_ENTRY *e = ZeroMalloc(sizeof(INI_ENTRY)); diff --git a/src/Cedar/Interop_OpenVPN.h b/src/Cedar/Interop_OpenVPN.h index ddd2244c..c31d3d24 100644 --- a/src/Cedar/Interop_OpenVPN.h +++ b/src/Cedar/Interop_OpenVPN.h @@ -190,6 +190,10 @@ #define OPENVPN_MODE_L2 1 // TAP (Ethernet) #define OPENVPN_MODE_L3 2 // TUN (IP) +// Data +#define OPENVPN_DATA_OPTIONS 0 +#define OPENVPN_DATA_PEERINFO 1 + //// Type @@ -361,8 +365,7 @@ void OvsSetupSessionParameters(OPENVPN_SERVER *s, OPENVPN_SESSION *se, OPENVPN_C BUF *OvsBuildKeyMethod2(OPENVPN_KEY_METHOD_2 *d); void OvsWriteStringToBuf(BUF *b, char *str, UINT max_size); -LIST *OvsParseOptions(char *str); -LIST *OvsParsePeerInfo(char *str); +LIST *OvsParseData(char *str, int type); void OvsFreeList(LIST *o); LIST *OvsNewList(); void OvsAddEntry(LIST *o, char *key, char *value);