src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 20 Aug 2022 22:12:25 +0200
branchv_0
changeset 29 97fe991ad2cb
parent 20 ad21bff45051
permissions -rw-r--r--
fix connection string parsing: IPv6 literals without the interface name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    21
#include <codecvt>
6
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    22
#include <regex>
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/common/type/typedefs.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/cli/CLI.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <relpipe/cli/RelpipeCLIException.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include "Configuration.h"
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace relpipe {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
namespace out {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace socket {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
class CLIParser {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
private:
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    36
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
		if (i < arguments.size()) return arguments[i++];
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	/**
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	 * TODO: use a common method
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	 */
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	bool parseBoolean(const relpipe::common::type::StringX& value) {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		if (value == L"true") return true;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		else if (value == L"false") return false;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
6
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    52
	void setIfMissing(std::vector<Configuration::SocketOption>& options, const std::string& name, const std::string& value, bool includeEmptyValues = true) {
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    53
		auto n = convertor.from_bytes(name);
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    54
		auto v = convertor.from_bytes(value);
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    55
		for (auto o : options) if (o.name == n) return;
6
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    56
		if (value.size() || includeEmptyValues) options.push_back({n, v});
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    57
	}
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    58
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
public:
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	static const relpipe::common::type::StringX OPTION_RELATION;
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
	static const relpipe::common::type::StringX OPTION_CONNECTION_STRING;
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
	static const relpipe::common::type::StringX OPTION_CONNECTION_OPTION;
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		Configuration c;
6
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    67
		relpipe::common::type::StringX connectionString;
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		for (int i = 0; i < arguments.size();) {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
			relpipe::common::type::StringX option = readNext(arguments, i);
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
			if (option == OPTION_RELATION) {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
				c.relation = readNext(arguments, i);
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    74
			} else if (option == OPTION_CONNECTION_STRING) {
6
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    75
				connectionString = readNext(arguments, i);
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
			} else if (option == OPTION_CONNECTION_OPTION) {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
				auto name = readNext(arguments, i);
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
				auto value = readNext(arguments, i);
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    79
				c.options.push_back({name, value});
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    83
		using namespace options;
6
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    84
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    85
		// Parse the connection string and convert it to options:
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    86
		if (connectionString.size()) {
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    87
			std::string connectionStringBytes = convertor.to_bytes(connectionString);
29
97fe991ad2cb fix connection string parsing: IPv6 literals without the interface name
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    88
			std::regex pattern("(tcp|udp|sctp)://(([^:]+)|\\[([0-9a-fA-F:]+(%[a-zA-Z0-9]+)?)\\]):([0-9]+)|(uds)://(.*)");
20
ad21bff45051 support IPv6 link local addresses not only in the „host“ option but also in the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    89
			//                  1                23          4             5                    6        7       8
6
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    90
			std::smatch match;
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    91
			if (std::regex_match(connectionStringBytes, match, pattern)) {
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    92
				setIfMissing(c.options, OPTION_PROTOCOL, match[1], false);
20
ad21bff45051 support IPv6 link local addresses not only in the „host“ option but also in the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    93
				setIfMissing(c.options, OPTION_PROTOCOL, match[7], false);
17
b9dcb7aa75e1 support literal IPv6 addresses in connection strings
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    94
				setIfMissing(c.options, OPTION_HOST, match[3], false);
b9dcb7aa75e1 support literal IPv6 addresses in connection strings
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    95
				setIfMissing(c.options, OPTION_HOST, match[4], false);
20
ad21bff45051 support IPv6 link local addresses not only in the „host“ option but also in the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    96
				setIfMissing(c.options, OPTION_PORT, match[6], false);
ad21bff45051 support IPv6 link local addresses not only in the „host“ option but also in the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    97
				setIfMissing(c.options, OPTION_PATH, match[8], false);
6
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    98
				if (match[1] == PROTOCOL_TCP) setIfMissing(c.options, OPTION_MODE, MODE_STREAM);
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    99
			} else {
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   100
				throw relpipe::cli::RelpipeCLIException(L"Invalid connection string: " + connectionString, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   101
			}
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   102
		}
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   103
f0c51bc4d164 configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   104
		// Set defaults when values are missing:
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   105
		setIfMissing(c.options, OPTION_PROTOCOL, PROTOCOL_UDP);
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   106
		setIfMissing(c.options, OPTION_ROLE, ROLE_CLIENT);
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   107
		setIfMissing(c.options, OPTION_MODE, MODE_DATAGRAM);
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   108
		setIfMissing(c.options, OPTION_HOST, "127.0.0.1");
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   109
		setIfMissing(c.options, OPTION_PORT, "64000");
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   110
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
		return c;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
	virtual ~CLIParser() {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
};
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   119
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_STRING = L"--connection-string";
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   120
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_OPTION = L"--connection-option";
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
}