Implement ZMQ_TCP_ACCEPT_FILTER setsockopt() for listening TCP sockets.

Assign arbitrary number of filters that will be applied for each new TCP transport
connection on a listening socket.
If no filters applied, then TCP transport allows connections from any ip.
If at least one filter is applied then new connection source ip should be matched.
To clear all filters call zmq_setsockopt(socket, ZMQ_TCP_ACCEPT_FILTER, NULL, 0).
Filter is a null-terminated string with ipv6 or ipv4 CIDR.

For example:
localhost
127.0.0.1
mail.ru/24
::1
::1/128
3ffe:1::
3ffe:1::/56

Returns -1 if the filter couldn't be assigned(format error or ipv6 filter with ZMQ_IPV4ONLY set)

P.S.
The only thing that worries me is that I had to re-enable 'default assign by reference constructor/operator'
for 'tcp_address_t' (and for my inherited class tcp_address_mask_t) to store it in std::vector in 'options_t'...
This commit is contained in:
Sergey KHripchenko
2012-04-12 18:37:14 +04:00
parent e276df2bdf
commit acba6bdd6c
8 changed files with 202 additions and 8 deletions

View File

@@ -287,6 +287,32 @@ int zmq::options_t::setsockopt (int option_, const void *optval_,
#endif
return 0;
}
case ZMQ_TCP_ACCEPT_FILTER:
{
if (optvallen_ == 0 && optval_ == NULL) {
tcp_accept_filters.clear ();
return 0;
}
else
if (optvallen_ < 1 || optvallen_ > 255 || optval_ == NULL || *((const char*) optval_) == 0) {
errno = EINVAL;
return -1;
}
else {
std::string filter_str ((const char*) optval_, optvallen_);
tcp_address_mask_t filter;
int rc = filter.resolve (filter_str.c_str (), ipv4only ? true : false);
if (rc != 0) {
errno = EINVAL;
return -1;
}
tcp_accept_filters.push_back(filter);
return 0;
}
}
}
errno = EINVAL;
return -1;