| libtorrent

# Filter

Author: | Arvid Norberg, arvid@libtorrent.org
Version: | 1.0.11

Table of contents

- ip_filter
- port_filter

# ip_filter

Declared in "libtorrent/ip_filter.hpp"

The `ip_filter` class is a set of rules that uniquely categorizes all ip addresses as allowed or disallowed. The default constructor creates a single rule that allows all addresses (0.0.0.0 - 255.255.255.255 for the IPv4 range, and the equivalent range covering all addresses for the IPv6 range).

A default constructed ip_filter does not filter any address.

    struct ip_filter
    {
       void add_rule (address first, address last, int flags);
       int access (address const& addr) const;
       filter_tuple_t export_filter () const;

       enum access_flags
       {
          blocked,
       };
    };

## add_rule()

    void add_rule (address first, address last, int flags);

Adds a rule to the filter. `first` and `last` defines a range of ip addresses that will be marked with the given flags. The `flags` can currently be 0, which means allowed, or `ip_filter::blocked`, which means disallowed.

precondition: `first.is_v4() == last.is_v4() && first.is_v6() == last.is_v6()`

postcondition: `access(x) == flags` for every `x` in the range [`first`, `last`]

This means that in a case of overlapping ranges, the last one applied takes precedence.

## access()

    int access (address const& addr) const;

Returns the access permissions for the given address (`addr`). The permission can currently be 0 or `ip_filter::blocked`. The complexity of this operation is O(`log` n), where n is the minimum number of non-overlapping ranges to describe the current filter.

## export_filter()

    filter_tuple_t export_filter () const;

This function will return the current state of the filter in the minimum number of ranges possible. They are sorted from ranges in low addresses to high addresses. Each entry in the returned vector is a range with the access control specified in its `flags` field.

The return value is a tuple containing two range-lists. One for IPv4 addresses and one for IPv6 addresses.

## enum access_flags

Declared in "libtorrent/ip_filter.hpp"

name | value | description
--- | --- | ---
blocked | 1 | indicates that IPs in this range should not be connected to nor accepted as incoming connections

# port_filter

Declared in "libtorrent/ip_filter.hpp"

the port filter maps non-overlapping port ranges to flags. This is primarily used to indicate whether a range of ports should be connected to or not. The default is to have the full port range (0-65535) set to flag 0.

    class port_filter
    {
       void add_rule (boost::uint16_t first, boost::uint16_t last, int flags);
       int access (boost::uint16_t port) const;

       enum access_flags
       {
          blocked,
       };
    };

## add_rule()

    void add_rule (boost::uint16_t first, boost::uint16_t last, int flags);

set the flags for the specified port range (`first`, `last`) to `flags` overwriting any existing rule for those ports. The range is inclusive, i.e. the port `last` also has the flag set on it.

## access()

    int access (boost::uint16_t port) const;

test the specified port (`port`) for whether it is blocked or not. The returned value is the flags set for this port. see acces_flags.

## enum access_flags

Declared in "libtorrent/ip_filter.hpp"

name | value | description
--- | --- | ---
blocked | 1 | this flag indicates that destination ports in the range should not be connected to