12#ifndef OPENVPN_COMMON_HOSTPORT_H
13#define OPENVPN_COMMON_HOSTPORT_H
29inline bool is_valid_port(
const std::string &port,
unsigned int *value =
nullptr)
31 return parse_number_validate<unsigned int>(port, 5, 1, 65535, value);
34inline void validate_port(
const std::string &port,
const std::string &title,
unsigned int *value =
nullptr)
40inline void validate_port(
const unsigned int port,
const std::string &title)
43 OPENVPN_THROW(host_port_error,
"bad " << title <<
" port number: " << port);
46inline unsigned short parse_port(
const std::string &port,
const std::string &title)
50 return static_cast<unsigned short>(ret);
56 return (c >=
'a' && c <=
'z')
57 || (c >=
'A' && c <=
'Z')
58 || (c >=
'0' && c <=
'9')
66 if (host.empty() || host.length() > 256)
68 for (
const auto &c : host)
78 return c >= 0x21 && c <= 0x7E;
83 if (host.empty() || host.length() > 256)
85 for (
const auto &c : host)
93inline void validate_host(
const std::string &host,
const std::string &title)
102 const std::string &default_port,
103 const bool allow_unix,
104 unsigned int *port_save =
nullptr)
108 const size_t fpos = str.find_first_of(
':');
109 const size_t lpos = str.find_last_of(
':');
110 const size_t cb = str.find_last_of(
']');
111 if (lpos != std::string::npos
112 && (cb == std::string::npos || cb + 1 == lpos)
113 && (cb != std::string::npos || fpos == lpos))
116 host = str.substr(0, lpos);
117 port = str.substr(lpos + 1);
119 else if (!default_port.empty())
129 if (host.length() >= 2 && host[0] ==
'[' && host[host.length() - 1] ==
']')
130 host = host.substr(1, host.length() - 2);
132 if (allow_unix && port ==
"unix")
#define OPENVPN_EXCEPTION(C)
#define OPENVPN_THROW(exc, stuff)
void validate_host(const std::string &host, const std::string &title)
void validate_port(const std::string &port, const std::string &title, unsigned int *value=nullptr)
bool is_valid_port(const unsigned int port)
bool is_valid_unix_sock(const std::string &host)
bool split_host_port(const std::string &str, std::string &host, std::string &port, const std::string &default_port, const bool allow_unix, unsigned int *port_save=nullptr)
bool is_valid_host_char(const char c)
unsigned short parse_port(const std::string &port, const std::string &title)
bool is_valid_host(const std::string &host)
bool is_valid_unix_sock_char(const unsigned char c)
STRING utf8_printable(const STRING &str, size_t max_len_flags)