Commit f749f2d2 authored by Dhammika Pathirana's avatar Dhammika Pathirana Committed by Martin Sustrik

add basic uri validations

Signed-off-by: 's avatarDhammika Pathirana <dhammika@gmail.com>
parent 22b2b9a2
...@@ -141,6 +141,26 @@ void zmq::socket_base_t::stop () ...@@ -141,6 +141,26 @@ void zmq::socket_base_t::stop ()
send_stop (); send_stop ();
} }
int zmq::socket_base_t::parse_uri (const char *uri_,
std::string &protocol_, std::string &address_)
{
zmq_assert (uri_ != NULL);
std::string uri (uri_);
std::string::size_type pos = uri.find ("://");
if (pos == std::string::npos) {
errno = EINVAL;
return -1;
}
protocol_ = uri.substr (0, pos);
address_ = uri.substr (pos + 3);
if (protocol_.empty () || address_.empty ()) {
errno = EINVAL;
return -1;
}
return 0;
}
int zmq::socket_base_t::check_protocol (const std::string &protocol_) int zmq::socket_base_t::check_protocol (const std::string &protocol_)
{ {
// First check out whether the protcol is something we are aware of. // First check out whether the protcol is something we are aware of.
...@@ -272,18 +292,11 @@ int zmq::socket_base_t::bind (const char *addr_) ...@@ -272,18 +292,11 @@ int zmq::socket_base_t::bind (const char *addr_)
// Parse addr_ string. // Parse addr_ string.
std::string protocol; std::string protocol;
std::string address; std::string address;
{ int rc = parse_uri (addr_, protocol, address);
std::string addr (addr_); if (rc != 0)
std::string::size_type pos = addr.find ("://"); return -1;
if (pos == std::string::npos) {
errno = EINVAL;
return -1;
}
protocol = addr.substr (0, pos);
address = addr.substr (pos + 3);
}
int rc = check_protocol (protocol); rc = check_protocol (protocol);
if (rc != 0) if (rc != 0)
return -1; return -1;
...@@ -334,18 +347,11 @@ int zmq::socket_base_t::connect (const char *addr_) ...@@ -334,18 +347,11 @@ int zmq::socket_base_t::connect (const char *addr_)
// Parse addr_ string. // Parse addr_ string.
std::string protocol; std::string protocol;
std::string address; std::string address;
{ int rc = parse_uri (addr_, protocol, address);
std::string addr (addr_); if (rc != 0)
std::string::size_type pos = addr.find ("://"); return -1;
if (pos == std::string::npos) {
errno = EINVAL;
return -1;
}
protocol = addr.substr (0, pos);
address = addr.substr (pos + 3);
}
int rc = check_protocol (protocol); rc = check_protocol (protocol);
if (rc != 0) if (rc != 0)
return -1; return -1;
......
...@@ -128,6 +128,10 @@ namespace zmq ...@@ -128,6 +128,10 @@ namespace zmq
// where it doesn't intersect the object being destroyed. // where it doesn't intersect the object being destroyed.
bool destroyed; bool destroyed;
// Parse URI string.
int parse_uri (const char *uri_, std::string &protocol_,
std::string &address_);
// Check whether transport protocol, as specified in connect or // Check whether transport protocol, as specified in connect or
// bind, is available and compatible with the socket type. // bind, is available and compatible with the socket type.
int check_protocol (const std::string &protocol_); int check_protocol (const std::string &protocol_);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment