diff --git a/Jamfile b/Jamfile index 2ab5129e2..4caec8e60 100755 --- a/Jamfile +++ b/Jamfile @@ -49,6 +49,11 @@ rule linking ( properties * ) } } + if shared in $(properties) + { + result += GeoIP ; + } + # socket functions on windows require winsock libraries if windows in $(properties) || cygwin in $(properties) @@ -120,6 +125,11 @@ rule building ( properties * ) result += src/assert.cpp ; } + if static in $(properties) + { + result += src/GeoIP.c ; + } + if off in $(properties) { result += src/sha1.cpp ; @@ -149,6 +159,9 @@ rule building ( properties * ) return $(result) ; } +feature geoip : off static shared : composite propagated link-incompatible ; +feature.compose off : TORRENT_DISABLE_GEO_IP ; + feature bandwidth-limit-logging : off on : composite propagated link-incompatible ; feature.compose on : TORRENT_VERBOSE_BANDWIDTH_LIMIT ; diff --git a/bindings/python/src/peer_info.cpp b/bindings/python/src/peer_info.cpp index cb4c8c311..3c22820fe 100755 --- a/bindings/python/src/peer_info.cpp +++ b/bindings/python/src/peer_info.cpp @@ -68,6 +68,10 @@ void bind_peer_info() .def_readonly("num_hashfails", &peer_info::num_hashfails) #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES .add_property("country", get_country) +#endif +#ifndef TORRENT_DISABLE_GEO_IP + .def_readonly("inet_as_name", &peer_info::inet_as_name) + .def_readonly("inet_as", &peer_info::inet_as) #endif .def_readonly("load_balancing", &peer_info::load_balancing) .def_readonly("download_queue_length", &peer_info::download_queue_length) diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index 3b4a891c0..413046576 100755 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -250,6 +250,11 @@ void bind_session() .def("set_pe_settings", allow_threads(&session::set_pe_settings), session_set_pe_settings_doc) .def("get_pe_settings", allow_threads(&session::get_pe_settings), return_value_policy()) #endif +#ifndef TORRENT_DISABLE_GEO_IP + .def("load_asnum_db", allow_threads(&session::load_asnum_db)) +#endif + .def("load_state", allow_threads(&session::load_state)) + .def("state", allow_threads(&session::state)) .def( "set_severity_level", allow_threads(&session::set_severity_level) , session_set_severity_level_doc diff --git a/docs/building.html b/docs/building.html index 7f84b5ea9..5f4977f4f 100644 --- a/docs/building.html +++ b/docs/building.html @@ -240,6 +240,18 @@ with the libtorrent package. +geoip +
    +
  • off - geo ip lookups disabled
  • +
  • static - MaxMind geo ip lookup code linked +in statically. Note that this code is under +LGPL license.
  • +
  • shared - The MaxMind geo ip lookup library +is expected to be installed on the system and +it will be used.
  • +
+ + upnp-logging
  • off - default. Does not log UPnP traffic.
  • @@ -349,7 +361,7 @@ character-set=ansi character-set=unicode

    building with autotools

    First of all, you need to install automake and autoconf. Many unix/linux systems comes with these preinstalled.

    -

    The prerequisites for building libtorrent is boost.thread, boost.date_time +

    The prerequisites for building libtorrent are boost.thread, boost.date_time and boost.filesystem. Those are the compiled boost libraries needed. The headers-only libraries needed include (but is not necessarily limited to) boost.bind, boost.ref, boost.multi_index, boost.optional, boost.lexical_cast, @@ -505,6 +517,11 @@ structs and class layouts and sizes. events, such as tracker announces and incoming connections (as well as blocked connections). +TORRENT_DISABLE_GEO_IP +This is defined by default by the Jamfile. It +disables the GeoIP features, and avoids linking +against LGPL:ed code. + TORRENT_VERBOSE_LOGGING If you define this macro, every peer connection will log its traffic to a log file as well as diff --git a/docs/building.rst b/docs/building.rst index 5a05e8a62..18b735ba5 100644 --- a/docs/building.rst +++ b/docs/building.rst @@ -235,6 +235,14 @@ Build features: | | * ``shipped`` - links against the zlib bundled | | | with the libtorrent package. | +------------------------+----------------------------------------------------+ +| ``geoip`` | * ``off`` - geo ip lookups disabled | +| | * ``static`` - MaxMind_ geo ip lookup code linked | +| | in statically. Note that this code is under | +| | LGPL license. | +| | * ``shared`` - The MaxMind_ geo ip lookup library | +| | is expected to be installed on the system and | +| | it will be used. | ++------------------------+----------------------------------------------------+ | ``upnp-logging`` | * ``off`` - default. Does not log UPnP traffic. | | | * ``on`` - creates "upnp.log" with the messages | | | sent to and received from UPnP devices. | @@ -290,6 +298,8 @@ Build features: | | * ``off`` - default for release builds. | +------------------------+----------------------------------------------------+ +.. _MaxMind: http://www.maxmind.com/app/api + The ``variant`` feature is *implicit*, which means you don't need to specify the name of the feature, just the value. @@ -320,7 +330,7 @@ building with autotools First of all, you need to install ``automake`` and ``autoconf``. Many unix/linux systems comes with these preinstalled. -The prerequisites for building libtorrent is boost.thread, boost.date_time +The prerequisites for building libtorrent are boost.thread, boost.date_time and boost.filesystem. Those are the *compiled* boost libraries needed. The headers-only libraries needed include (but is not necessarily limited to) boost.bind, boost.ref, boost.multi_index, boost.optional, boost.lexical_cast, @@ -483,6 +493,10 @@ defines you can use to control the build. | | events, such as tracker announces and incoming | | | connections (as well as blocked connections). | +---------------------------------------+-------------------------------------------------+ +| ``TORRENT_DISABLE_GEO_IP`` | This is defined by default by the Jamfile. It | +| | disables the GeoIP features, and avoids linking | +| | against LGPL:ed code. | ++---------------------------------------+-------------------------------------------------+ | ``TORRENT_VERBOSE_LOGGING`` | If you define this macro, every peer connection | | | will log its traffic to a log file as well as | | | the session log. | diff --git a/docs/manual.html b/docs/manual.html index a98eb675a..4a277a6aa 100644 --- a/docs/manual.html +++ b/docs/manual.html @@ -16,218 +16,221 @@ Author: -Arvid Norberg, arvid@rasterbar.com +Arvid Norberg, arvid@rasterbar.com Version: 0.13

    -

    Table of contents

    +

    Table of contents

    -
    -

    overview

    +
    +

    overview

    The interface of libtorrent consists of a few classes. The main class is the session, it contains the main loop that serves all torrents.

    The basic usage is as follows:

    Each class and function is described in this manual.

    -
    -

    network primitives

    +
    +

    network primitives

    There are a few typedefs in the libtorrent namespace which pulls in network types from the asio namespace. These are:

    @@ -245,10 +248,10 @@ udp::endpoint
     

    Which are the endpoint types used in libtorrent. An endpoint is an address with an associated port.

    -

    For documentation on these types, please refer to the asio documentation.

    +

    For documentation on these types, please refer to the asio documentation.

    -
    -

    session

    +
    +

    session

    The session class has the following synopsis:

     class session: public boost::noncopyable
    @@ -318,6 +321,11 @@ class session: public boost::noncopyable
             int num_uploads() const;
             int num_connections() const;
     
    +        bool load_asnum_db(char const* file);
    +
    +        void load_state(entry const& ses_state);
    +        entry state() const;
    +
             void set_ip_filter(ip_filter const& f);
     
             session_status status() const;
    @@ -348,8 +356,8 @@ class session: public boost::noncopyable
     

    Once it's created, the session object will spawn the main thread that will do all the work. The main thread will be idle as long it doesn't have any torrents to participate in.

    -
    -

    session()

    +
    +

    session()

     session(fingerprint const& print
    @@ -362,22 +370,22 @@ session(fingerprint const& print
     

    If the fingerprint in the first overload is omited, the client will get a default fingerprint stating the version of libtorrent. The fingerprint is a short string that will be used in the peer-id to identify the client and the client's version. For more details see the -fingerprint class. The constructor that only takes a fingerprint will not open a +fingerprint class. The constructor that only takes a fingerprint will not open a listen port for the session, to get it running you'll have to call session::listen_on(). The other constructor, that takes a port range and an interface as well as the fingerprint will automatically try to listen on a port on the given interface. For more information about the parameters, see listen_on() function.

    -
    -

    ~session()

    +
    +

    ~session()

    The destructor of session will notify all trackers that our torrents have been shut down. If some trackers are down, they will time out. All this before the destructor of session returns. So, it's advised that any kind of interface (such as windows) are closed before destructing the session object. Because it can take a few second for it to finish. The timeout can be set with set_settings().

    -
    -

    abort()

    +
    +

    abort()

     session_proxy abort();
     
    @@ -399,8 +407,8 @@ public: };
    -
    -

    add_torrent()

    +
    +

    add_torrent()

     typedef storage_interface* (&storage_constructor_type)(
    @@ -434,10 +442,10 @@ want to save the files. The save_
     structure in the torrent-file.

    If the torrent you are trying to add already exists in the session (is either queued for checking, being checked or downloading) add_torrent() will throw -duplicate_torrent which derives from std::exception.

    +duplicate_torrent which derives from std::exception.

    The optional parameter, resume_data can be given if up to date fast-resume data is available. The fast-resume data can be acquired from a running torrent by calling -torrent_handle::write_resume_data(). See fast resume.

    +torrent_handle::write_resume_data(). See fast resume.

    The storage_mode parameter refers to the layout of the storage for this torrent. There are 3 different modes:

    @@ -453,7 +461,7 @@ filesystems that don't support sparse files. are rearranged to finally be in their correct places once the entire torrent has been downloaded.
    -

    For more information, see storage allocation.

    +

    For more information, see storage allocation.

    paused is a boolean that specifies whether or not the torrent is to be started in a paused state. I.e. it won't connect to the tracker or any of the peers until it's resumed. This is typically a good way of avoiding race conditions when setting @@ -462,11 +470,11 @@ configuration options on torrents before starting them.

    storage will simply write the data to the files it belongs to, but it could be overridden to save everything to a single file at a specific location or encrypt the content on disk for instance. For more information about the storage_interface -that needs to be implemented for a custom storage, see storage_interface.

    -

    The torrent_handle returned by add_torrent() can be used to retrieve information +that needs to be implemented for a custom storage, see storage_interface.

    +

    The torrent_handle returned by add_torrent() can be used to retrieve information about the torrent's progress, its peers etc. It is also used to abort a torrent.

    The userdata parameter is optional and will be passed on to the extension -constructor functions, if any (see add_extension()).

    +constructor functions, if any (see add_extension()).

    The second overload that takes a tracker url and an info-hash instead of metadata (torrent_info) can be used with torrents where (at least some) peers support the metadata extension. For the overload to be available, libtorrent must be built @@ -477,8 +485,8 @@ have metadata. See torrent_handle

    If the torrent doesn't have a tracker, but relies on the DHT to find peers, the tracker_url can be 0.

    -
    -

    remove_torrent()

    +
    +

    remove_torrent()

     void remove_torrent(torrent_handle const& h, int options = none);
    @@ -489,10 +497,10 @@ the tracker that we've stopped participating in the swarm. The optional second a
     options can be used to delete all the files downloaded by this torrent. To do this, pass
     in the value session::delete_files. The removal of the torrent is asyncronous, there is
     no guarantee that adding the same torrent immediately after it was removed will not throw
    -a duplicate_torrent exception.

    +a duplicate_torrent exception.

    -
    -

    find_torrent() get_torrents()

    +
    +

    find_torrent() get_torrents()

     torrent_handle find_torrent(sha_hash const& ih);
    @@ -506,8 +514,8 @@ In case the torrent cannot be found, an invalid torrent_handle is returned.

    get_torrents() returns a vector of torrent_handles to all the torrents currently in the session.

    -
    -

    set_upload_rate_limit() set_download_rate_limit() upload_rate_limit() download_rate_limit()

    +
    +

    set_upload_rate_limit() set_download_rate_limit() upload_rate_limit() download_rate_limit()

     void set_upload_rate_limit(int bytes_per_second);
    @@ -524,8 +532,8 @@ of upload rate.
     download_rate_limit() and upload_rate_limit() returns the previously
     set limits.

    -
    -

    set_max_uploads() set_max_connections()

    +
    +

    set_max_uploads() set_max_connections()

     void set_max_uploads(int limit);
    @@ -538,8 +546,8 @@ minimum of at least two connections per torrent, so if you set a too low
     connections limit, and open too many torrents, the limit will not be met. The
     number of uploads is at least one per torrent.

    -
    -

    num_uploads() num_connections()

    +
    +

    num_uploads() num_connections()

     int num_uploads() const;
    @@ -549,8 +557,8 @@ int num_connections() const;
     

    Returns the number of currently unchoked peers and the number of connections (including half-open ones) respectively.

    -
    -

    set_max_half_open_connections() max_half_open_connections()

    +
    +

    set_max_half_open_connections() max_half_open_connections()

     void set_max_half_open_connections(int limit);
    @@ -569,8 +577,31 @@ their turn to get connected.

    max_half_open_connections() returns the set limit. This limit defaults to 8 on windows.

    -
    -

    set_ip_filter()

    +
    +

    load_asnum_db()

    +
    +
    +bool load_asnum_db(char const* file);
    +
    +
    +

    This function is not available if TORRENT_DISABLE_GEO_IP is defined. This +expects a path to the MaxMind ASN database. This will be used to look up +which AS peers belong to.

    +
    +
    +

    load_state() state()

    +
    +
    +void load_state(entry const& ses_state);
    +entry state() const;
    +
    +
    +

    These functions loads and save session state. Currently, the only state +that's stored is peak download rates for ASes. This map is used to +determine which order to connect to peers.

    +
    +
    +

    set_ip_filter()

     void set_ip_filter(ip_filter const& filter);
    @@ -579,12 +610,12 @@ void set_ip_filter(ip_filter const& filter);
     

    Sets a filter that will be used to reject and accept incoming as well as outgoing connections based on their originating ip address. The default filter will allow connections to any ip address. To build a set of rules for which addresses are -accepted and not, see ip_filter.

    -

    Each time a peer is blocked because of the IP filter, a peer_blocked_alert is +accepted and not, see ip_filter.

    +

    Each time a peer is blocked because of the IP filter, a peer_blocked_alert is generated.

    -
    -

    status()

    +
    +

    status()

     session_status status() const;
    @@ -646,8 +677,8 @@ becomes unresponsive.

    dht_global_nodes is an estimation of the total number of nodes in the DHT network.

    -
    -

    get_cache_status()

    +
    +

    get_cache_status()

     cache_status get_cache_status() const;
    @@ -684,8 +715,8 @@ for the read cache.

    This includes both read and write cache.

    read_cache_size is the number of 16KiB blocks in the read cache.

    -
    -

    get_cache_info()

    +
    +

    get_cache_info()

     void get_cache_info(sha1_hash const& ih
    @@ -711,8 +742,8 @@ the data for that block being in the disk cache and last_use is the time when a block was last written to this piece. The older
     a piece is, the more likely it is to be flushed to disk.

    -
    -

    is_listening() listen_port() listen_on()

    +
    +

    is_listening() listen_port() listen_on()

     bool is_listening() const;
    @@ -736,7 +767,7 @@ the range and so on. The interface parameter can be left as 0, in that case the
     os will decide which interface to listen on, otherwise it should be the ip-address
     of the interface you want the listener socket bound to. listen_on() returns true
     if it managed to open the socket, and false if it failed. If it fails, it will also
    -generate an appropriate alert (listen_failed_alert).

    +generate an appropriate alert (listen_failed_alert).

    The interface parameter can also be a hostname that will resolve to the device you want to listen on.

    If you're also starting the DHT, it is a good idea to do that after you've called @@ -755,8 +786,8 @@ with a DHT ping packet, and connect to those that responds first. On windows one can only connect to a few peers at a time because of a built in limitation (in XP Service pack 2).

    -
    -

    pop_alert() set_severity_level()

    +
    +

    pop_alert() set_severity_level()

     std::auto_ptr<alert> pop_alert();
    @@ -765,10 +796,10 @@ void set_severity_level(alert::severity_t s);
     

    pop_alert() is used to ask the session if any errors or events has occurred. With set_severity_level() you can filter how serious the event has to be for you to -receive it through pop_alert(). For information, see alerts.

    +receive it through pop_alert(). For information, see alerts.

    -
    -

    add_extension()

    +
    +

    add_extension()

     void add_extension(boost::function<
    @@ -778,7 +809,7 @@ void add_extension(boost::function<
     

    This function adds an extension to this session. The argument is a function object that is called with a torrent* and which should return a boost::shared_ptr<torrent_plugin>. To write custom plugins, see -libtorrent plugins. The main plugins implemented in libtorrent are:

    +libtorrent plugins. The main plugins implemented in libtorrent are:

    metadata extension
    Allows peers to download the metadata (.torren files) from the swarm @@ -816,8 +847,8 @@ eliminate most problems on poisoned torrents.
    ses.add_extension(&libtorrent::create_smart_ban_plugin);
    -
    -

    set_settings() set_pe_settings()

    +
    +

    set_settings() set_pe_settings()

     void set_settings(session_settings const& settings);
    @@ -825,11 +856,11 @@ void set_pe_settings(pe_settings const& settings);
     

    Sets the session settings and the packet encryption settings respectively. -See session_settings and pe_settings for more information on available +See session_settings and pe_settings for more information on available options.

    -
    -

    set_peer_proxy() set_web_seed_proxy() set_tracker_proxy() set_dht_proxy()

    +
    +

    set_peer_proxy() set_web_seed_proxy() set_tracker_proxy() set_dht_proxy()

     void set_peer_proxy(proxy_settings const& s);
    @@ -842,16 +873,16 @@ void set_dht_proxy(proxy_settings const& s);
     sets the proxy settings for different kinds of connections, bittorrent peers,
     web seeds, trackers and the DHT traffic.

    set_peer_proxy affects regular bittorrent peers. set_web_seed_proxy -affects only web seeds. see HTTP seeding.

    +affects only web seeds. see HTTP seeding.

    set_tracker_proxy only affects HTTP tracker connections (UDP tracker connections are affected if the given proxy supports UDP, e.g. SOCKS5).

    set_dht_proxy affects the DHT messages. Since they are sent over UDP, it only has any effect if the proxy supports UDP.

    For more information on what settings are available for proxies, see -proxy_settings.

    +proxy_settings.

    -
    -

    peer_proxy() web_seed_proxy() tracker_proxy() dht_proxy()

    +
    +

    peer_proxy() web_seed_proxy() tracker_proxy() dht_proxy()

     proxy_settings const& peer_proxy() const;
    @@ -863,8 +894,8 @@ proxy_settings const& dht_proxy() const;
     

    These functions returns references to their respective current settings.

    The dht_proxy is not available when DHT is disabled.

    -
    -

    start_dht() stop_dht() set_dht_settings() dht_state()

    +
    +

    start_dht() stop_dht() set_dht_settings() dht_state()

     void start_dht(entry const& startup_state);
    @@ -924,8 +955,8 @@ that are ready to replace a failing node, it will be replaced immediately,
     this limit is only used to clear out nodes that don't have any node that can
     replace them.

    -
    -

    add_dht_node() add_dht_router()

    +
    +

    add_dht_node() add_dht_router()

     void add_dht_node(std::pair<std::string, int> const& node);
    @@ -944,8 +975,8 @@ for bootstrapping, to keep the load off them.

    router.bittorrent.com.

    -
    -

    entry

    +
    +

    entry

    The entry class represents one node in a bencoded hierarchy. It works as a variant type, it can be either a list, a dictionary (std::map), an integer or a string. This is its synopsis:

    @@ -1008,8 +1039,8 @@ public: };

    TODO: finish documentation of entry.

    -
    -

    integer() string() list() dict() type()

    +
    +

    integer() string() list() dict() type()

     integer_type& integer();
    @@ -1024,7 +1055,7 @@ dictionary_type const& dict() const;
     

    The integer(), string(), list() and dict() functions are accessors that return the respective type. If the entry object isn't of the -type you request, the accessor will throw type_error (which derives from +type you request, the accessor will throw type_error (which derives from std::runtime_error). You can ask an entry for its type through the type() function.

    The print() function is there for debug purposes only.

    @@ -1058,11 +1089,11 @@ if (entry* i = torrent_file.find_key("announce")) std::cout << tracker_url << "\n"; } -

    To make it easier to extract information from a torrent file, the class torrent_info +

    To make it easier to extract information from a torrent file, the class torrent_info exists.

    -
    -

    operator[]

    +
    +

    operator[]

     entry& operator[](char const* key);
    @@ -1080,8 +1111,8 @@ given key, a reference to a newly inserted element at that key.

    existing element at the given key. If the key is not found, it will throw libtorrent::type_error.

    -
    -

    find_key()

    +
    +

    find_key()

     entry* find_key(char const* key);
    @@ -1095,8 +1126,8 @@ element cannot be found, they will return 0. If an element with the given
     key is found, the return a pointer to it.

    -
    -

    torrent_info

    +
    +

    torrent_info

    The torrent_info has the following synopsis:

     class torrent_info
    @@ -1162,8 +1193,8 @@ public:
             sha1_hash const& hash_for_piece(unsigned int index) const;
     };
     
    -
    -

    torrent_info()

    +
    +

    torrent_info()

     torrent_info();
    @@ -1184,10 +1215,10 @@ as soon as it has been downloaded from the swarm.

    The last constructor is the one that is used in most cases. It will create a torrent_info object from the information found in the given torrent_file. The entry represents a tree node in an bencoded file. To load an ordinary .torrent file into an entry, use bdecode(), -see bdecode() bencode().

    +see bdecode() bencode().

    -
    -

    set_comment() set_piece_size() set_creator() set_hash() add_tracker() add_file()

    +
    +

    set_comment() set_piece_size() set_creator() set_hash() add_tracker() add_file()

     void set_comment(char const* str);
    @@ -1207,35 +1238,35 @@ size is given in number of bytes.

    set_creator() is an optional attribute that can be used to identify your application that was used to create the torrent file. The string should be UTF-8 encoded.

    set_hash() writes the hash for the piece with the given piece-index. You have to call -this function for every piece in the torrent. Usually the hasher is used to calculate +this function for every piece in the torrent. Usually the hasher is used to calculate the sha1-hash for a piece.

    add_tracker() adds a tracker to the announce-list. The tier determines the order in -which the trackers are to be tried. For more information see trackers().

    +which the trackers are to be tried. For more information see trackers().

    add_file() adds a file to the torrent. The order in which you add files will determine the order in which they are placed in the torrent file. You have to add at least one file to the torrent. The path you give has to be a relative path from the root directory of the torrent. The size is given in bytes.

    When you have added all the files and hashes to your torrent, you can generate an entry -which then can be encoded as a .torrent file. You do this by calling create_torrent().

    -

    For a complete example of how to create a torrent from a file structure, see make_torrent.

    +which then can be encoded as a .torrent file. You do this by calling create_torrent().

    +

    For a complete example of how to create a torrent from a file structure, see make_torrent.

    -
    -

    create_torrent()

    +
    +

    create_torrent()

     entry create_torrent();
     

    Returns an entry representing the bencoded tree of data that makes up a .torrent file. -You can save this data as a torrent file with bencode() (see bdecode() bencode()), for a -complete example, see make_torrent.

    +You can save this data as a torrent file with bencode() (see bdecode() bencode()), for a +complete example, see make_torrent.

    This function is not const because it will also set the info-hash of the torrent_info object.

    Note that a torrent file must include at least one file, and it must have at least one tracker url or at least one DHT node.

    -
    -

    remap_files()

    +
    +

    remap_files()

     bool remap_files(std::vector<file_entry> const& map);
    @@ -1254,8 +1285,8 @@ match, false will be returned (this is the only case this function may fail).

    Changing this mapping for an existing torrent will not move or rename files. If some files should be renamed, this can be done before the torrent is added.

    -
    -

    begin_files() end_files() rbegin_files() rend_files()

    +
    +

    begin_files() end_files() rbegin_files() rend_files()

     file_iterator begin_files(bool storage = false) const;
    @@ -1272,7 +1303,7 @@ iterators with the type file_entr
     is false, which means you will see the content of the torrent file. If set to
     true, you will see the file that the storage class uses to save the files to
     disk. Typically these views are the same, but in case the files have been
    -remapped, they may differ. For more info, see remap_files().

    +remapped, they may differ. For more info, see remap_files().

     struct file_entry
     {
    @@ -1302,8 +1333,8 @@ the original string is preserved in 
    -

    num_files() file_at()

    +
    +

    num_files() file_at()

     int num_files(bool storage = false) const;
    @@ -1316,10 +1347,10 @@ to access files using indices.

    is false, which means you will see the content of the torrent file. If set to true, you will see the file that the storage class uses to save the files to disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see remap_files().

    +remapped, they may differ. For more info, see remap_files().

    -
    -

    map_block()

    +
    +

    map_block()

     std::vector<file_slice> map_block(int piece, size_type offset
    @@ -1347,10 +1378,10 @@ will never be greater than the file size.

    is false, which means you will see the content of the torrent file. If set to true, you will see the file that the storage class uses to save the files to disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see remap_files().

    +remapped, they may differ. For more info, see remap_files().

    -
    -

    map_file()

    +
    +

    map_file()

     peer_request map_file(int file_index, size_type file_offset
    @@ -1377,8 +1408,8 @@ struct peer_request
     + size is not allowed to be greater than the file size. file_index
     must refer to a valid file, i.e. it cannot be >= num_files().

    -
    -

    url_seeds() add_url_seed()

    +
    +

    url_seeds() add_url_seed()

     std::vector<std::string> const& url_seeds() const;
    @@ -1393,11 +1424,11 @@ supported for the url is http.

    is false, which means you will see the content of the torrent file. If set to true, you will see the file that the storage class uses to save the files to disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see remap_files().

    -

    See HTTP seeding for more information.

    +remapped, they may differ. For more info, see remap_files().

    +

    See HTTP seeding for more information.

    -
    -

    print()

    +
    +

    print()

     void print(std::ostream& os) const;
    @@ -1407,8 +1438,8 @@ void print(std::ostream& os) const;
     the torrent file to the given outstream. This function has been deprecated and will
     be removed from future releases.

    -
    -

    trackers()

    +
    +

    trackers()

     std::vector<announce_entry> const& trackers() const;
    @@ -1427,8 +1458,8 @@ struct announce_entry
     };
     
    -
    -

    total_size() piece_length() piece_size() num_pieces()

    +
    +

    total_size() piece_length() piece_size() num_pieces()

     size_type total_size() const;
    @@ -1445,8 +1476,8 @@ the piece index as argument and gives you the exact size of that piece. It will
     be the same as piece_length() except in the case of the last piece, which may
     be smaller.

    -
    -

    hash_for_piece() info_hash()

    +
    +

    hash_for_piece() info_hash()

     size_type piece_size(unsigned int index) const;
    @@ -1455,12 +1486,12 @@ sha1_hash const& hash_for_piece(unsigned int index) const;
     

    hash_for_piece() takes a piece-index and returns the 20-bytes sha1-hash for that piece and info_hash() returns the 20-bytes sha1-hash for the info-section of the -torrent file. For more information on the sha1_hash, see the big_number class. +torrent file. For more information on the sha1_hash, see the big_number class. info_hash() will only return a valid hash if the torrent_info was read from a .torrent file or if an entry was created from it (through create_torrent).

    -
    -

    name() comment() creation_date() creator()

    +
    +

    name() comment() creation_date() creator()

     std::string const& name() const;
    @@ -1470,15 +1501,15 @@ boost::optional<boost::posix_time::ptime> creation_date() const;
     

    name() returns the name of the torrent.

    comment() returns the comment associated with the torrent. If there's no comment, -it will return an empty string. creation_date() returns a boost::posix_time::ptime +it will return an empty string. creation_date() returns a boost::posix_time::ptime object, representing the time when this torrent file was created. If there's no time stamp in the torrent file, this will return a date of January 1:st 1970.

    Both the name and the comment is UTF-8 encoded strings.

    creator() returns the creator string in the torrent. If there is no creator string it will return an empty string.

    -
    -

    priv() set_priv()

    +
    +

    priv() set_priv()

     bool priv() const;
    @@ -1489,8 +1520,8 @@ void set_priv(bool v);
     distributed on the trackerless network (the kademlia DHT).

    set_priv() sets or clears the private flag on this torrent.

    -
    -

    nodes()

    +
    +

    nodes()

     std::vector<std::pair<std::string, int> > const& nodes() const;
    @@ -1499,8 +1530,8 @@ std::vector<std::pair<std::string, int> > const& nodes() const;
     

    If this torrent contains any DHT nodes, they are put in this vector in their original form (host name and port number).

    -
    -

    add_node()

    +
    +

    add_node()

     void add_node(std::pair<std::string, int> const& node);
    @@ -1510,8 +1541,8 @@ void add_node(std::pair<std::string, int> const& node);
     be used, by the client, to bootstrap into the DHT network.

    -
    -

    torrent_handle

    +
    +

    torrent_handle

    You will usually have to store your torrent handles somewhere, since it's the object through which you retrieve information about the torrent and aborts the torrent. Its declaration looks like this:

    @@ -1600,14 +1631,14 @@ valid handle. If you try to perform any operation on an uninitialized handle, it will throw invalid_handle.

    Warning

    -

    All operations on a torrent_handle may throw invalid_handle +

    All operations on a torrent_handle may throw invalid_handle exception, in case the handle is no longer refering to a torrent. There are two exceptions, info_hash() and is_valid() will never throw. Since the torrents are processed by a background thread, there is no guarantee that a handle will remain valid between two calls.

    -
    -

    piece_priority() prioritize_pieces() piece_priorities() prioritize_files()

    +
    +

    piece_priority() prioritize_pieces() piece_priorities() prioritize_files()

     void piece_priority(int index, int priority) const;
    @@ -1651,8 +1682,8 @@ torrent. Each element is the current priority of that piece.

    files in the torrent. Each entry is the priority of that file. The function sets the priorities of all the pieces in the torrent based on the vector.

    -
    -

    file_progress()

    +
    +

    file_progress()

     void file_progress(std::vector<float>& fp);
    @@ -1660,21 +1691,21 @@ void file_progress(std::vector<float>& fp);
     

    This function fills in the supplied vector with the progress (a value in the range [0, 1]) describing the download progress of each file in this torrent. -The progress values are ordered the same as the files in the torrent_info. +The progress values are ordered the same as the files in the torrent_info. This operation is not very cheap.

    -
    -

    save_path()

    +
    +

    save_path()

     boost::filesystem::path save_path() const;
     
    -

    save_path() returns the path that was given to add_torrent() when this torrent +

    save_path() returns the path that was given to add_torrent() when this torrent was started.

    -
    -

    move_storage()

    +
    +

    move_storage()

     void move_storage(boost::filesystem::path const& save_path) const;
    @@ -1686,8 +1717,8 @@ the same drive as the original save path. Since disk IO is performed in a separa
     thread, this operation is also asynchronous. Once the operation completes, the
     storage_moved_alert is generated, with the new path as the message.

    -
    -

    force_reannounce()

    +
    +

    force_reannounce()

     void force_reannounce() const;
    @@ -1698,8 +1729,8 @@ void force_reannounce(boost::posix_time::time_duration) const;
     peers. The second overload of force_reannounce that takes a time_duration as
     argument will schedule a reannounce in that amount of time from now.

    -
    -

    scrape_tracker()

    +
    +

    scrape_tracker()

     void scrape_tracker() const;
    @@ -1709,11 +1740,11 @@ void scrape_tracker() const;
     tracker for statistics such as total number of incomplete peers, complete peers, number of
     downloads etc.

    This request will specifically update the num_complete and num_incomplete fields in -the torrent_status struct once it completes. When it completes, it will generate a -scrape_reply_alert. If it fails, it will generate a scrape_failed_alert.

    +the torrent_status struct once it completes. When it completes, it will generate a +scrape_reply_alert. If it fails, it will generate a scrape_failed_alert.

    -
    -

    connect_peer()

    +
    +

    connect_peer()

     void connect_peer(asio::ip::tcp::endpoint const& adr, int source = 0) const;
    @@ -1723,12 +1754,12 @@ void connect_peer(asio::ip::tcp::endpoint const& adr, int source = 0) const;
     torrent. If the peer does not respond, or is not a member of this torrent, it will simply
     be disconnected. No harm can be done by using this other than an unnecessary connection
     attempt is made. If the torrent is uninitialized or in queued or checking mode, this
    -will throw invalid_handle. The second (optional) argument will be bitwised ORed into
    -the source mask of this peer. Typically this is one of the source flags in peer_info.
    +will throw invalid_handle. The second (optional) argument will be bitwised ORed into
    +the source mask of this peer. Typically this is one of the source flags in peer_info.
     i.e. tracker, pex, dht etc.

    -
    -

    name()

    +
    +

    name()

     std::string name() const;
    @@ -1738,8 +1769,8 @@ std::string name() const;
     case the torrent was started without metadata, and hasn't completely received it yet,
     it returns the name given to it when added to the session. See session::add_torrent.

    -
    -

    set_ratio()

    +
    +

    set_ratio()

     void set_ratio(float ratio) const;
    @@ -1753,8 +1784,8 @@ attempt to upload in return for each download. e.g. if set to 2, the client will
     2 bytes for every byte received. The default setting for this is 0, which will make it work
     as a standard client.

    -
    -

    set_upload_limit() set_download_limit() upload_limit() download_limit()

    +
    +

    set_upload_limit() set_download_limit() upload_limit() download_limit()

     void set_upload_limit(int limit) const;
    @@ -1772,8 +1803,8 @@ limit.

    upload_limit and download_limit will return the current limit setting, for upload and download, respectively.

    -
    -

    set_sequential_download()

    +
    +

    set_sequential_download()

     void set_sequential_download(bool sd);
    @@ -1784,8 +1815,8 @@ instead of rarest first.

    Enabling sequential download will affect the piece distribution negatively in the swarm. It should be used sparingly.

    -
    -

    set_peer_upload_limit() set_peer_download_limit()

    +
    +

    set_peer_upload_limit() set_peer_download_limit()

     void set_peer_upload_limit(asio::ip::tcp::endpoint ip, int limit) const;
    @@ -1795,8 +1826,8 @@ void set_peer_download_limit(asio::ip::tcp::endpoint ip, int limit) const;
     

    Works like set_upload_limit and set_download_limit respectively, but controls individual peer instead of the whole torrent.

    -
    -

    pause() resume() is_paused()

    +
    +

    pause() resume() is_paused()

     void pause() const;
    @@ -1808,10 +1839,10 @@ bool is_paused() const;
     When a torrent is paused, it will however remember all share ratios to all peers and remember
     all potential (not connected) peers. You can use is_paused() to determine if a torrent
     is currently paused. Torrents may be paused automatically if there is a file error (e.g. disk full)
    -or something similar. See file_error_alert.

    +or something similar. See file_error_alert.

    -
    -

    resolve_countries()

    +
    +

    resolve_countries()

     void resolve_countries(bool r);
    @@ -1819,12 +1850,12 @@ bool resolve_countries() const;
     

    Sets or gets the flag that derermines if countries should be resolved for the peers of this -torrent. It defaults to false. If it is set to true, the peer_info structure for the peers -in this torrent will have their country member set. See peer_info for more information +torrent. It defaults to false. If it is set to true, the peer_info structure for the peers +in this torrent will have their country member set. See peer_info for more information on how to interpret this field.

    -
    -

    is_seed()

    +
    +

    is_seed()

     bool is_seed() const;
    @@ -1832,8 +1863,8 @@ bool is_seed() const;
     

    Returns true if the torrent is in seed mode (i.e. if it has finished downloading).

    -
    -

    has_metadata()

    +
    +

    has_metadata()

     bool has_metadata() const;
    @@ -1842,10 +1873,10 @@ bool has_metadata() const;
     

    Returns true if this torrent has metadata (either it was started from a .torrent file or the metadata has been downloaded). The only scenario where this can return false is when the torrent was started torrent-less (i.e. with just an info-hash and tracker ip). Note that if the torrent -doesn't have metadata, the member get_torrent_info() will throw.

    +doesn't have metadata, the member get_torrent_info() will throw.

    -
    -

    set_tracker_login()

    +
    +

    set_tracker_login()

     void set_tracker_login(std::string const& username
    @@ -1855,8 +1886,8 @@ void set_tracker_login(std::string const& username
     

    set_tracker_login() sets a username and password that will be sent along in the HTTP-request of the tracker announce. Set this if the tracker requires authorization.

    -
    -

    trackers() replace_trackers()

    +
    +

    trackers() replace_trackers()

     std::vector<announce_entry> const& trackers() const;
    @@ -1870,10 +1901,10 @@ which this tracker is tried. If you want libtorrent to use another list of
     trackers for this torrent, you can use replace_trackers() which takes
     a list of the same form as the one returned from trackers() and will
     replace it. If you want an immediate effect, you have to call
    -force_reannounce().

    +force_reannounce().

    -
    -

    add_url_seed() remove_url_seed() url_seeds()

    +
    +

    add_url_seed() remove_url_seed() url_seeds()

     void add_url_seed(std::string const& url);
    @@ -1888,22 +1919,22 @@ paused, queued, checking or seeding. url_seeds() return a set of the url seeds
     currently in this torrent. Note that urls that fails may be removed
     automatically from the list.

    -

    See HTTP seeding for more information.

    +

    See HTTP seeding for more information.

    -
    -

    use_interface()

    +
    +

    use_interface()

     void use_interface(char const* net_interface) const;
     

    use_interface() sets the network interface this torrent will use when it opens outgoing -connections. By default, it uses the same interface as the session uses to listen on. The +connections. By default, it uses the same interface as the session uses to listen on. The parameter must be a string containing an ip-address (either an IPv4 or IPv6 address). If the string does not conform to this format and exception is thrown.

    -
    -

    info_hash()

    +
    +

    info_hash()

     sha1_hash info_hash() const;
    @@ -1911,8 +1942,8 @@ sha1_hash info_hash() const;
     

    info_hash() returns the info-hash for the torrent.

    -
    -

    set_max_uploads() set_max_connections()

    +
    +

    set_max_uploads() set_max_connections()

     void set_max_uploads(int max_uploads) const;
    @@ -1926,15 +1957,15 @@ connections are used up, incoming connections may be refused or poor connections
     This must be at least 2. The default is unlimited number of connections. If -1 is given to the
     function, it means unlimited.

    -
    -

    write_resume_data()

    +
    +

    write_resume_data()

     entry write_resume_data() const;
     
    -

    write_resume_data() generates fast-resume data and returns it as an entry. This entry -is suitable for being bencoded. For more information about how fast-resume works, see fast resume.

    +

    write_resume_data() generates fast-resume data and returns it as an entry. This entry +is suitable for being bencoded. For more information about how fast-resume works, see fast resume.

    There are three cases where this function will just return an empty entry:

      @@ -1942,7 +1973,7 @@ is suitable for being bencoded. For more information about how fast-resume works
    1. The torrent is checking (or is queued for checking) its storage, it will obviously not be ready to write resume data.
    2. The torrent hasn't received valid metadata and was started without metadata -(see libtorrent's metadata from peers extension)
    3. +(see libtorrent's metadata from peers extension)

    Note that by the time this function returns, the resume data may already be invalid if the torrent @@ -1960,19 +1991,19 @@ the entire file.

    It is still a good idea to save resume data periodically during download as well as when closing down.

    -
    -

    status()

    +
    +

    status()

     torrent_status status() const;
     

    status() will return a structure with information about the status of this -torrent. If the torrent_handle is invalid, it will throw invalid_handle exception. -See torrent_status.

    +torrent. If the torrent_handle is invalid, it will throw invalid_handle exception. +See torrent_status.

    -
    -

    get_download_queue()

    +
    +

    get_download_queue()

     void get_download_queue(std::vector<partial_piece_info>& queue) const;
    @@ -2024,8 +2055,8 @@ a state (state) which
     is 0 or 1, but at the end of the torrent blocks may be requested by more peers in parallel to
     speed things up.

    -
    -

    get_peer_info()

    +
    +

    get_peer_info()

     void get_peer_info(std::vector<peer_info>&) const;
    @@ -2033,25 +2064,25 @@ void get_peer_info(std::vector<peer_info>&) const;
     

    get_peer_info() takes a reference to a vector that will be cleared and filled with one entry for each peer connected to this torrent, given the handle is valid. If the -torrent_handle is invalid, it will throw invalid_handle exception. Each entry in -the vector contains information about that particular peer. See peer_info.

    +torrent_handle is invalid, it will throw invalid_handle exception. Each entry in +the vector contains information about that particular peer. See peer_info.

    -
    -

    get_torrent_info()

    +
    +

    get_torrent_info()

     torrent_info const& get_torrent_info() const;
     
    -

    Returns a const reference to the torrent_info object associated with this torrent. -This reference is valid as long as the torrent_handle is valid, no longer. If the -torrent_handle is invalid or if it doesn't have any metadata, invalid_handle +

    Returns a const reference to the torrent_info object associated with this torrent. +This reference is valid as long as the torrent_handle is valid, no longer. If the +torrent_handle is invalid or if it doesn't have any metadata, invalid_handle exception will be thrown. The torrent may be in a state without metadata only if it was started without a .torrent file, i.e. by using the libtorrent extension of just supplying a tracker and info-hash.

    -
    -

    is_valid()

    +
    +

    is_valid()

     bool is_valid() const;
    @@ -2061,13 +2092,13 @@ bool is_valid() const;
     or if the torrent it refers to has been aborted. Note that a handle may become invalid after
     it has been added to the session. Usually this is because the storage for the torrent is
     somehow invalid or if the filenames are not allowed (and hence cannot be opened/created) on
    -your filesystem. If such an error occurs, a file_error_alert is generated and all handles
    +your filesystem. If such an error occurs, a file_error_alert is generated and all handles
     that refers to that torrent will become invalid.

    TODO: document storage

    -
    -

    torrent_status

    +
    +

    torrent_status

    It contains the following fields:

     struct torrent_status
    @@ -2112,6 +2143,11 @@ struct torrent_status
             int num_complete;
             int num_incomplete;
     
    +        int list_seeds;
    +        int list_peers;
    +
    +        int connect_candidates;
    +
             const std::vector<bool>* pieces;
             int num_pieces;
     
    @@ -2220,12 +2256,21 @@ be slightly smaller than the other rates, but if projected over a long time
     

    num_peers is the number of peers this torrent currently is connected to. Peer connections that are in the half-open state (is attempting to connect) or are queued for later connection attempt do not count. Although they are -visible in the peer list when you call get_peer_info().

    +visible in the peer list when you call get_peer_info().

    num_complete and num_incomplete are set to -1 if the tracker did not send any scrape data in its announce reply. This data is optional and may not be available from all trackers. If these are not -1, they are the total number of peers that are seeding (complete) and the total number of peers that are still downloading (incomplete) this torrent.

    +

    list_seeds and list_peers are the number of seeds in our peer list +and the total number of peers (including seeds) respectively. We are not +necessarily connected to all the peers in our peer list. This is the number +of peers we know of in total, including banned peers and peers that we have +failed to connect to.

    +

    connect_candidates is the number of peers in this torrent's peer list +that is a candidate to be connected to. i.e. It has fewer connect attempts +than the max fail count, it is not a seed if we are a seed, it is not banned +etc. If this is 0, it means we don't know of any more peers that we can try.

    total_done is the total number of bytes of the file(s) that we have. All this does not necessarily has to be downloaded during this session (that's total_payload_download).

    @@ -2249,7 +2294,7 @@ copies is set to -1.

    block_size is the size of a block, in bytes. A block is a sub piece, it is the number of bytes that each piece request asks for and the number of bytes that each bit in the partial_piece_info's bitset represents -(see get_download_queue()). This is typically 16 kB, but it may be +(see get_download_queue()). This is typically 16 kB, but it may be larger if the pieces are larger.

    num_uploads is the number of unchoked peers in this torrent.

    num_connections is the number of peer connections this torrent has, including @@ -2260,8 +2305,8 @@ always <= num_peerscompact_mode is true if this torrent was started with compact allocation mode for its storage. False means it was started in full allocation mode.

    -
    -

    peer_info

    +
    +

    peer_info

    It contains the following fields:

     struct peer_info
    @@ -2323,6 +2368,9 @@ struct peer_info
     
             char country[2];
     
    +        std::string inet_as_name;
    +        int inet_as;
    +
             size_type load_balancing;
     
             int download_queue_length;
    @@ -2352,6 +2400,10 @@ struct peer_info
             int receive_quota;
     
             int rtt;
    +
    +        int download_rate_peak;
    +        int upload_rate_peak;
    +
     };
     

    The flags attribute tells you in which state the peer is. It is set to @@ -2376,7 +2428,7 @@ any combination of the enums above. The following table describes each flag:

    support_extensions means that this peer supports the -extension protocol. +extension protocol. local_connection The connection was initiated by us, the peer has a @@ -2492,7 +2544,7 @@ limits.

    The ip field is the IP-address to this peer. The type is an asio endpoint. For -more info, see the asio documentation.

    +more info, see the asio documentation.

    up_speed and down_speed contains the current upload and download speed we have to and from this peer (including any protocol messages). The transfer rates of payload data only are found in payload_up_speed and payload_down_speed. @@ -2518,12 +2570,15 @@ to this peer and since any transfer occurred with this peer, respectively.

    and used for the peer's send buffer, respectively.

    num_hashfails is the number of pieces this peer has participated in sending us that turned out to fail the hash check.

    -

    country is the two letter ISO 3166 country code for the country the peer +

    country is the two letter ISO 3166 country code for the country the peer is connected from. If the country hasn't been resolved yet, both chars are set to 0. If the resolution failed for some reason, the field is set to "--". If the resolution service returns an invalid country code, it is set to "!!". The countries.nerd.dk service is used to look up countries. This field will -remain set to 0 unless the torrent is set to resolve countries, see resolve_countries().

    +remain set to 0 unless the torrent is set to resolve countries, see resolve_countries().

    +

    inet_as_name is the name of the AS this peer is located in. This might be +an empty string if there is no name in the geo ip database.

    +

    inet_as is the AS number the peer is located in.

    load_balancing is a measurement of the balancing of free download (that we get) and free upload that we give. Every peer gets a certain amount of free upload, but this member says how much extra free upload this peer has got. If it is a negative @@ -2561,9 +2616,12 @@ assigned to be allowed to send and receive until it has to request more quota from the bandwidth manager.

    rtt is an estimated round trip time to this peer, in milliseconds. It is estimated by timing the the tcp connect(). It may be 0 for incoming connections.

    +

    download_rate_peak and upload_rate_peak are the highest download and upload +rates seen on this connection. They are given in bytes per second. This number is +reset to 0 on reconnect.

    -
    -

    session_settings

    +
    +

    session_settings

    You have some control over tracker requests through the session_settings object. You create it and fill it with your settings and then use session::set_settings() to apply them. You have control over proxy and authorization settings and also the user-agent @@ -2742,9 +2800,9 @@ message or a time out.

    are kept in memory after the torrent becomes a seed or not. If it is set to true the hashes are freed once the torrent is a seed (they're not needed anymore since the torrent won't download anything more). If it's set -to false they are not freed. If they are freed, the torrent_info returned +to false they are not freed. If they are freed, the torrent_info returned by get_torrent_info() will return an object that may be incomplete, that -cannot be passed back to add_torrent() for instance.

    +cannot be passed back to add_torrent() for instance.

    upnp_ignore_nonrouters indicates whether or not the UPnP implementation should ignore any broadcast response from a device whose address is not the configured router for this machine. i.e. it's a way to not talk to other @@ -2772,10 +2830,10 @@ to peers if a previous socket to that peer and port is in peer_tos determines the TOS byte set in the IP header of every packet sent to peers (including web seeds). The default value for this is 0x0 (no marking). One potentially useful TOS mark is 0x20, this represents -the QBone scavenger service. For more details, see QBSS.

    +the QBone scavenger service. For more details, see QBSS.

    -
    -

    pe_settings

    +
    +

    pe_settings

    The pe_settings structure is used to control the settings related to peer protocol encryption:

    @@ -2831,8 +2889,8 @@ remains unchanged.
     

    prefer_rc4 can be set to true if you want to prefer the RC4 encrypted stream.

    -
    -

    proxy_settings

    +
    +

    proxy_settings

    The proxy_settings structs contains the information needed to direct certain traffic to a proxy.

    @@ -2870,16 +2928,16 @@ options are available:

    • none - This is the default, no proxy server is used, all other fields are ignored.
    • -
    • socks4 - The server is assumed to be a SOCKS4 server that +
    • socks4 - The server is assumed to be a SOCKS4 server that requires a username.
    • -
    • socks5 - The server is assumed to be a SOCKS5 server (RFC 1928) that +
    • socks5 - The server is assumed to be a SOCKS5 server (RFC 1928) that does not require any authentication. The username and password are ignored.
    • socks5_pw - The server is assumed to be a SOCKS5 server that supports -plain text username and password authentication (RFC 1929). The username +plain text username and password authentication (RFC 1929). The username and password specified may be sent to the proxy if it requires.
    • http - The server is assumed to be an HTTP proxy. If the transport used for the connection is non-HTTP, the server is assumed to support the -CONNECT method. i.e. for web seeds and HTTP trackers, a plain proxy will +CONNECT method. i.e. for web seeds and HTTP trackers, a plain proxy will suffice. The proxy is assumed to not require authorization. The username and password will not be used.
    • http_pw - The server is assumed to be an HTTP proxy that requires @@ -2887,8 +2945,8 @@ user authorization. The username and password will be sent to the proxy.
    -
    -

    ip_filter

    +
    +

    ip_filter

    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 @@ -2920,8 +2978,8 @@ public: }; -

    -

    ip_filter()

    +
    +

    ip_filter()

     ip_filter()
    @@ -2931,8 +2989,8 @@ ip_filter()
     

    postcondition: access(x) == 0 for every x

    -
    -

    add_rule()

    +
    +

    add_rule()

     void add_rule(address first, address last, int flags);
    @@ -2949,8 +3007,8 @@ means disallowed.

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

    -
    -

    access()

    +
    +

    access()

     int access(address const& addr) const;
    @@ -2961,8 +3019,8 @@ can currently be 0 or ip_filter::
     is O(log n), where n is the minimum number of non-overlapping ranges to describe
     the current filter.

    -
    -

    export_filter()

    +
    +

    export_filter()

     boost::tuple<std::vector<ip_range<address_v4> >
    @@ -2977,8 +3035,8 @@ entry in the returned vector is a range with the access control specified in its
     and one for IPv6 addresses.

    -
    -

    big_number

    +
    +

    big_number

    Both the peer_id and sha1_hash types are typedefs of the class big_number. It represents 20 bytes of data. Its synopsis follows:

    @@ -2998,8 +3056,8 @@ public:
     

    The iterators gives you access to individual bytes.

    -
    -

    hasher

    +
    +

    hasher

    This class creates sha1-hashes. Its declaration looks like this:

     class hasher
    @@ -3025,8 +3083,8 @@ call reset() to reini
     

    The sha1-algorithm used was implemented by Steve Reid and released as public domain. For more info, see src/sha1.cpp.

    -
    -

    fingerprint

    +
    +

    fingerprint

    The fingerprint class represents information about a client and its version. It is used to encode this information into the client's peer id.

    This is the class declaration:

    @@ -3083,15 +3141,15 @@ sure not to clash with anybody else. Here are some taken id's:

    -

    There's currently an informal directory of client id's here.

    +

    There's currently an informal directory of client id's here.

    The major, minor, revision and tag parameters are used to identify the version of your client. All these numbers must be within the range [0, 9].

    to_string() will generate the actual string put in the peer-id, and return it.

    -
    -

    free functions

    -
    -

    identify_client()

    +
    +

    free functions

    +
    +

    identify_client()

     std::string identify_client(peer_id const& id);
    @@ -3101,8 +3159,8 @@ std::string identify_client(peer_id const& id);
     to extract a string describing a client version from its peer-id. It will recognize most clients
     that have this kind of identification in the peer-id.

    -
    -

    client_fingerprint()

    +
    +

    client_fingerprint()

     boost::optional<fingerprint> client_fingerprint(peer_id const& p);
    @@ -3113,23 +3171,23 @@ to automate the identification of clients. It will not be able to identify peers
     standard encodings. Only Azureus style, Shadow's style and Mainline style. This function is
     declared in the header <libtorrent/identify_client.hpp>.

    -
    -

    bdecode() bencode()

    +
    +

    bdecode() bencode()

     template<class InIt> entry bdecode(InIt start, InIt end);
     template<class OutIt> void bencode(OutIt out, const entry& e);
     
    -

    These functions will encode data to bencoded or decode bencoded data.

    -

    The entry class is the internal representation of the bencoded data -and it can be used to retrieve information, an entry can also be build by +

    These functions will encode data to bencoded or decode bencoded data.

    +

    The entry class is the internal representation of the bencoded data +and it can be used to retrieve information, an entry can also be build by the program and given to bencode() to encode it into the OutIt iterator.

    The OutIt and InIt are iterators -(InputIterator and OutputIterator respectively). They -are templates and are usually instantiated as ostream_iterator, -back_insert_iterator or istream_iterator. These +(InputIterator and OutputIterator respectively). They +are templates and are usually instantiated as ostream_iterator, +back_insert_iterator or istream_iterator. These functions will assume that the iterator refers to a character (char). So, if you want to encode entry e into a buffer in memory, you can do it like this:

    @@ -3149,12 +3207,12 @@ const char* buf; // ... entry e = bdecode(buf, buf + data_size); -

    Now we just need to know how to retrieve information from the entry.

    +

    Now we just need to know how to retrieve information from the entry.

    If bdecode() encounters invalid encoded data in the range given to it -it will throw invalid_encoding.

    +it will throw invalid_encoding.

    -
    -

    supports_sparse_files()

    +
    +

    supports_sparse_files()

     bool supports_sparse_files(boost::filesystem::path const&);
    @@ -3168,8 +3226,8 @@ of sparse files, but rather the support for seeking passed end of file and
     write data there, with expected behavior.

    -
    -

    alerts

    +
    +

    alerts

    The pop_alert() function on session is the interface for retrieving alerts, warnings, messages and errors from libtorrent. If there hasn't occurred any errors (matching your severity level) pop_alert() will @@ -3226,7 +3284,7 @@ any events at all.

    pop_alert() from time to time. If you don't do that, the alert queue will just grow.

    When you get an alert, you can use typeid() or dynamic_cast<> to get more detailed information on exactly which type it is. i.e. what kind of error it is. You can also use a -dispatcher mechanism that's available in libtorrent.

    +dispatcher mechanism that's available in libtorrent.

    All alert types are defined in the <libtorrent/alert_types.hpp> header file.

    The alert class is the base class that specific messages are derived from. This is its synopsis:

    @@ -3260,10 +3318,26 @@ struct torrent_alert: alert };

    The specific alerts, that all derives from alert, are:

    -
    -

    listen_failed_alert

    +
    +

    external_ip_alert

    +

    Whenever libtorrent learns about the machines external IP, this alert is +generated. The external IP address can be acquired from the tracker (if it +supports that) or from peers that supports the extension protocol. +The address can be accessed through the external_address member. +This alert is generated as severity level info.

    +
    +struct external_ip_alert: alert
    +{
    +        external_ip_alert(address const& ip, const std::string& msg);
    +        address external_address;
    +        virtual std::auto_ptr<alert> clone() const;
    +};
    +
    +
    +
    +

    listen_failed_alert

    This alert is generated when none of the ports, given in the port range, to -session can be opened for listening. This alert is generated as severity +session can be opened for listening. This alert is generated as severity level fatal.

     struct listen_failed_alert: alert
    @@ -3273,8 +3347,8 @@ struct listen_failed_alert: alert
     };
     
    -
    -

    portmap_error_alert

    +
    +

    portmap_error_alert

    This alert is generated when a NAT router was successfully found but some part of the port mapping request failed. It contains a text message that may help the user figure out what is wrong. This alert is not generated in @@ -3291,8 +3365,8 @@ struct portmap_error_alert: alert };

    -
    -

    portmap_alert

    +
    +

    portmap_alert

    This alert is generated when a NAT router was successfully found and a port was successfully mapped on it. On a NAT:ed network with a NAT-PMP capable router, this is typically generated once when mapping the TCP @@ -3306,8 +3380,8 @@ struct portmap_alert: alert };

    -
    -

    file_error_alert

    +
    +

    file_error_alert

    If the storage fails to read or write files that it needs access to, this alert is generated and the torrent is paused. It is generated as severity level fatal.

    @@ -3321,8 +3395,8 @@ struct file_error_alert: torrent_alert
     };
     
    -
    -

    tracker_announce_alert

    +
    +

    tracker_announce_alert

    This alert is generated each time a tracker announce is sent (or attempted to be sent). It is generated at severity level info.

    @@ -3336,8 +3410,8 @@ struct tracker_announce_alert: torrent_alert
     };
     
    -
    -

    tracker_alert

    +
    +

    tracker_alert

    This alert is generated on tracker time outs, premature disconnects, invalid response or a HTTP response other than "200 OK". From the alert you can get the handle to the torrent the tracker belongs to. This alert is generated as severity level warning.

    @@ -3357,8 +3431,8 @@ struct tracker_alert: torrent_alert };
    -
    -

    tracker_reply_alert

    +
    +

    tracker_reply_alert

    This alert is only for informational purpose. It is generated when a tracker announce succeeds. It is generated regardless what kind of tracker was used, be it UDP, HTTP or the DHT. It is generated with severity level info.

    @@ -3377,8 +3451,8 @@ struct tracker_reply_alert: torrent_alert

    The num_peers tells how many peers were returned from the tracker. This is not necessarily all new peers, some of them may already be connected.

    -
    -

    tracker_warning_alert

    +
    +

    tracker_warning_alert

    This alert is triggered if the tracker reply contains a warning field. Usually this means that the tracker announce was successful, but the tracker has a message to the client. The message string in the alert will contain the warning message from @@ -3393,8 +3467,8 @@ struct tracker_warning_alert: torrent_alert };

    -
    -

    scrape_reply_alert

    +
    +

    scrape_reply_alert

     struct scrape_reply_alert: torrent_alert
     {
    @@ -3413,8 +3487,8 @@ struct scrape_reply_alert: torrent_alert
     and complete is the data returned in the scrape response. These numbers
     may be -1 if the reponse was malformed.

    -
    -

    scrape_failed_alert

    +
    +

    scrape_failed_alert

     struct scrape_failed_alert: torrent_alert
     {
    @@ -3428,8 +3502,8 @@ struct scrape_failed_alert: torrent_alert
     to the tracker timing out, refusing connection or returning an http response
     code indicating an error.

    -
    -

    url_seed_alert

    +
    +

    url_seed_alert

    This alert is generated when a HTTP seed name lookup fails. This alert is generated as severity level warning.

    It contains url to the HTTP seed that failed along with an error message.

    @@ -3444,8 +3518,8 @@ struct url_seed_alert: torrent_alert };
    -
    -

    hash_failed_alert

    +
    +

    hash_failed_alert

    This alert is generated when a finished piece fails its hash check. You can get the handle to the torrent which got the failed piece and the index of the piece itself from the alert. This alert is generated as severity level info.

    @@ -3463,10 +3537,10 @@ struct hash_failed_alert: torrent_alert };
    -
    -

    peer_ban_alert

    +
    +

    peer_ban_alert

    This alert is generated when a peer is banned because it has sent too many corrupt pieces -to us. It is generated at severity level info. The handle member is a torrent_handle +to us. It is generated at severity level info. The handle member is a torrent_handle to the torrent that this peer was a member of.

     struct peer_ban_alert: torrent_alert
    @@ -3482,8 +3556,8 @@ struct peer_ban_alert: torrent_alert
     };
     
    -
    -

    peer_error_alert

    +
    +

    peer_error_alert

    This alert is generated when a peer sends invalid data over the peer-peer protocol. The peer will be disconnected, but you get its ip address from the alert, to identify it. This alert is generated as severity level debug.

    @@ -3501,10 +3575,10 @@ struct peer_error_alert: alert };
    -
    -

    invalid_request_alert

    +
    +

    invalid_request_alert

    This is a debug alert that is generated by an incoming invalid piece request. The handle -is a handle to the torrent the peer is a member of. Ïp is the address of the peer and the +is a handle to the torrent the peer is a member of. ìp is the address of the peer and the request is the actual incoming request from the peer. The alert is generated as severity level debug.

    @@ -3537,8 +3611,8 @@ struct peer_request
     the index of the piece it want data from, start is the offset within the piece where the data
     should be read, and length is the amount of data it wants.

    -
    -

    torrent_finished_alert

    +
    +

    torrent_finished_alert

    This alert is generated when a torrent switches from being a downloader to a seed. It will only be generated once per torrent. It contains a torrent_handle to the torrent in question. This alert is generated as severity level info.

    @@ -3553,8 +3627,8 @@ struct torrent_finished_alert: torrent_alert };
    -
    -

    metadata_failed_alert

    +
    +

    metadata_failed_alert

    This alert is generated when the metadata has been completely received and the info-hash failed to match it. i.e. the metadata that was received was corrupt. libtorrent will automatically retry to fetch it in this case. This is only relevant when running a @@ -3571,8 +3645,8 @@ struct metadata_failed_alert: torrent_alert };

    -
    -

    metadata_received_alert

    +
    +

    metadata_received_alert

    This alert is generated when the metadata has been completely received and the torrent can start downloading. It is not generated on torrents that are started with metadata, but only those that needs to download it from peers (when utilizing the libtorrent extension). @@ -3588,8 +3662,8 @@ struct metadata_received_alert: torrent_alert };

    -
    -

    fastresume_rejected_alert

    +
    +

    fastresume_rejected_alert

    This alert is generated when a fastresume file has been passed to add_torrent but the files on disk did not match the fastresume file. The string explains the reason why the resume file was rejected. It is generated at severity level warning.

    @@ -3603,8 +3677,8 @@ struct fastresume_rejected_alert: torrent_alert };
    -
    -

    peer_blocked_alert

    +
    +

    peer_blocked_alert

    This alert is generated when a peer is blocked by the IP filter. It has the severity leve info. The ip member is the address that was blocked.

    @@ -3619,8 +3693,8 @@ struct peer_blocked_alert: alert
     };
     
    -
    -

    storage_moved_alert

    +
    +

    storage_moved_alert

    The storage_moved_alert is generated when all the disk IO has completed and the files have been moved, as an effect of a call to torrent_handle::move_storage. This is useful to synchronize with the actual disk.

    @@ -3632,8 +3706,8 @@ struct storage_moved_alert: torrent_alert };
    -
    -

    torrent_paused_alert

    +
    +

    torrent_paused_alert

    This alert is generated as a response to a torrent_handle::pause request. It is generated once all disk IO is complete and the files in the torrent have been closed. This is useful for synchronizing with the disk.

    @@ -3645,8 +3719,8 @@ struct torrent_paused_alert: torrent_alert };
    -
    -

    dispatcher

    +
    +

    dispatcher

    The handle_alert class is defined in <libtorrent/alert.hpp>.

    Examples usage:

    @@ -3691,13 +3765,13 @@ parameters to select between more types. If the number of types are more than
     including <libtorrent/alert.hpp>.

    -
    -

    exceptions

    +
    +

    exceptions

    There are a number of exceptions that can be thrown from different places in libtorrent, here's a complete list with description.

    -
    -

    invalid_handle

    -

    This exception is thrown when querying information from a torrent_handle that hasn't +

    +

    invalid_handle

    +

    This exception is thrown when querying information from a torrent_handle that hasn't been initialized or that has become invalid.

     struct invalid_handle: std::exception
    @@ -3706,10 +3780,10 @@ struct invalid_handle: std::exception
     };
     
    -
    -

    duplicate_torrent

    -

    This is thrown by add_torrent() if the torrent already has been added to -the session. Since remove_torrent() is asynchronous, this exception may +

    +

    duplicate_torrent

    +

    This is thrown by add_torrent() if the torrent already has been added to +the session. Since remove_torrent() is asynchronous, this exception may be thrown if the torrent is removed and then immediately added again.

     struct duplicate_torrent: std::exception
    @@ -3718,8 +3792,8 @@ struct duplicate_torrent: std::exception
     };
     
    -
    -

    invalid_encoding

    +
    +

    invalid_encoding

    This is thrown by bdecode() if the input data is not a valid bencoding.

     struct invalid_encoding: std::exception
    @@ -3728,8 +3802,8 @@ struct invalid_encoding: std::exception
     };
     
    -
    -

    type_error

    +
    +

    type_error

    This is thrown from the accessors of entry if the data type of the entry doesn't match the type you want to extract from it.

    @@ -3739,8 +3813,8 @@ struct type_error: std::runtime_error
     };
     
    -
    -

    invalid_torrent_file

    +
    +

    invalid_torrent_file

    This exception is thrown from the constructor of torrent_info if the given bencoded information doesn't meet the requirements on what information has to be present in a torrent file.

    @@ -3751,8 +3825,8 @@ struct invalid_torrent_file: std::exception
     
    -
    -

    storage_interface

    +
    +

    storage_interface

    The storage interface is a pure virtual class that can be implemented to change the behavior of the actual file storage. The interface looks like this:

    @@ -3774,8 +3848,8 @@ struct storage_interface virtual ~storage_interface() {} }; -
    -

    initialize()

    +
    +

    initialize()

     void initialize(bool allocate_files) = 0;
    @@ -3785,8 +3859,8 @@ void initialize(bool allocate_files) = 0;
     will create directories and empty files at this point. If allocate_files is true,
     it will also ftruncate all files to their target size.

    -
    -

    read()

    +
    +

    read()

     size_type read(char* buf, int slot, int offset, int size) = 0;
    @@ -3796,8 +3870,8 @@ size_type read(char* buf, int slot, int offset, int size) = 0;
     and size number of bytes. The data is to be copied to buf.

    The return value is the number of bytes actually read.

    -
    -

    write()

    +
    +

    write()

     void write(const char* buf, int slot, int offset, int size) = 0;
    @@ -3806,8 +3880,8 @@ void write(const char* buf, int slot, int offset, int size) = 0;
     

    This function should write the data in buf to the given slot (slot) at offset offset in that slot. The buffer size is size.

    -
    -

    move_storage()

    +
    +

    move_storage()

     bool move_storage(fs::path save_path) = 0;
    @@ -3818,8 +3892,8 @@ The default storage moves the single file or the directory of the torrent.

    Before moving the files, any open file handles may have to be closed, like release_files().

    -
    -

    verify_resume_data()

    +
    +

    verify_resume_data()

     bool verify_resume_data(entry& rd, std::string& error) = 0;
    @@ -3830,8 +3904,8 @@ on disk. If the resume data seems to be up-to-date, return true. If
     not, set error to a description of what mismatched and return false.

    The default storage may compare file sizes and time stamps of the files.

    -
    -

    write_resume_data( )

    +
    +

    write_resume_data( )

     void write_resume_data(entry& rd) const = 0;
    @@ -3841,8 +3915,8 @@ void write_resume_data(entry& rd) const = 0;
     storage, in rd. The default storage adds file timestamps and
     sizes.

    -
    -

    move_slot()

    +
    +

    move_slot()

     void move_slot(int src_slot, int dst_slot) = 0;
    @@ -3853,8 +3927,8 @@ the slot dst_slot. Th
     

    If the storage caches slots, this could be implemented more efficient than reading and writing the data.

    -
    -

    swap_slots()

    +
    +

    swap_slots()

     void swap_slots(int slot1, int slot2) = 0;
    @@ -3865,8 +3939,8 @@ storage uses a scratch buffer to read the data into, then moving the other
     slot and finally writing back the temporary slot's data

    This is only used in compact mode.

    -
    -

    swap_slots3()

    +
    +

    swap_slots3()

     void swap_slots3(int slot1, int slot2, int slot3) = 0;
    @@ -3877,8 +3951,8 @@ should move to slot2,
     should be moved to slot1.

    This is only used in compact mode.

    -
    -

    hash_for_slot()

    +
    +

    hash_for_slot()

     sha1_hash hash_for_slot(int slot, partial_hash& h, int piece_size) = 0;
    @@ -3899,8 +3973,8 @@ struct partial_hash
     that is stored in the given slot.

    The function should return the hash of the piece stored in the slot.

    -
    -

    release_files()

    +
    +

    release_files()

     void release_files() = 0;
    @@ -3910,8 +3984,8 @@ void release_files() = 0;
     belonging to this storage. The default implementation just calls
     file_pool::release_files(this).

    -
    -

    delete_files()

    +
    +

    delete_files()

     void delete_files() = 0;
    @@ -3920,22 +3994,22 @@ void delete_files() = 0;
     

    This function should delete all files and directories belonging to this storage.

    -
    -

    fast resume

    +
    +

    fast resume

    The fast resume mechanism is a way to remember which pieces are downloaded and where they are put between sessions. You can generate fast resume data by -calling torrent_handle::write_resume_data() on torrent_handle. You can +calling torrent_handle::write_resume_data() on torrent_handle. You can then save this data to disk and use it when resuming the torrent. libtorrent will not check the piece hashes then, and rely on the information given in the fast-resume data. The fast-resume data also contains information about which blocks, in the unfinished pieces, were downloaded, so it will not have to start from scratch on the partially downloaded pieces.

    -

    To use the fast-resume data you simply give it to add_torrent(), and it +

    To use the fast-resume data you simply give it to add_torrent(), and it will skip the time consuming checks. It may have to do the checking anyway, if the fast-resume data is corrupt or doesn't fit the storage for that torrent, then it will not trust the fast-resume data and just do the checking.

    -
    -

    file format

    +
    +

    file format

    The file format is a bencoded dictionary containing the following fields:

    @@ -4040,8 +4114,8 @@ last resume data checkpoint.
    -
    -

    threads

    +
    +

    threads

    libtorrent starts 2 or 3 threads.

      @@ -4059,8 +4133,8 @@ non-blocking host name resolution to simulate non-blocking behavior.
    -
    -

    storage allocation

    +
    +

    storage allocation

    There are three modes in which storage (files on disk) are allocated in libtorrent.

    1. The traditional full allocation mode, where the entire files are filled up with @@ -4074,11 +4148,11 @@ pieces that have been downloaded. This is the default allocation mode in libtorr to where they belong. This is the recommended (and default) mode.

    The allocation mode is selected when a torrent is started. It is passed as an -argument to session::add_torrent() (see add_torrent()).

    +argument to session::add_torrent() (see add_torrent()).

    The decision to use full allocation or compact allocation typically depends on whether any files are filtered and if the filesystem supports sparse files.

    -
    -

    sparse allocation

    +
    +

    sparse allocation

    On filesystems that supports sparse files, this allocation mode will only use as much space as has been downloaded.

    @@ -4089,9 +4163,9 @@ as much space as has been downloaded.

-
-

full allocation

-

When a torrent is started in full allocation mode, the checker thread (see threads) +

+

full allocation

+

When a torrent is started in full allocation mode, the checker thread (see threads) will make sure that the entire storage is allocated, and fill any gaps with zeros. This will be skipped if the filesystem supports sparse files or automatic zero filling. It will of course still check for existing pieces and fast resume data. The main @@ -4121,8 +4195,8 @@ out of date data, since pieces won't move around.

-
-

compact allocation

+
+

compact allocation

The compact allocation will only allocate as much storage as it needs to keep the pieces downloaded so far. This means that pieces will be moved around to be placed at their final position in the files while downloading (to make sure the completed @@ -4170,9 +4244,9 @@ contain any piece), return that slot index.

-
-

extensions

-

These extensions all operates within the extension protocol. The +

+

extensions

+

These extensions all operates within the extension protocol. The name of the extension is the name used in the extension-list packets, and the payload is the data in the extended message (not counting the length-prefix, message-id nor extension-id).

@@ -4180,8 +4254,8 @@ length-prefix, message-id nor extension-id).

handshake, it may be incompatible with future versions of the mainline bittorrent client.

These are the extensions that are currently implemented.

-
-

metadata from peers

+
+

metadata from peers

Extension name: "LT_metadata"

The point with this extension is that you don't have to distribute the metadata (.torrent-file) separately. The metadata can be distributed @@ -4299,9 +4373,9 @@ doesn't have any metadata.

-
-

HTTP seeding

-

The HTTP seed extension implements this specification.

+
+

HTTP seeding

+

The HTTP seed extension implements this specification.

The libtorrent implementation assumes that, if the URL ends with a slash ('/'), the filename should be appended to it in order to request pieces from that file. The way this works is that if the torrent is a single-file torrent, @@ -4310,8 +4384,8 @@ torrent's name '/' the file name is appended. This is the same directory structure that libtorrent will download torrents into.

-
-

filename checks

+
+

filename checks

Boost.Filesystem will by default check all its paths to make sure they conform to filename requirements on many platforms. If you don't want this check, you can set it to either only check for native filesystem requirements or turn it off @@ -4319,19 +4393,19 @@ altogether. You can use:

 boost::filesystem::path::default_name_check(boost::filesystem::native);
 
-

for example. For more information, see the Boost.Filesystem docs.

+

for example. For more information, see the Boost.Filesystem docs.

-
-

acknowledgments

+
+

acknowledgments

Written by Arvid Norberg. Copyright © 2003-2006

Contributions by Magnus Jonsson, Daniel Wallin and Cory Nelson

Lots of testing, suggestions and contributions by Massaroddel and Tianhao Qiu.

Big thanks to Michael Wojciechowski and Peter Koeleman for making the autotools scripts.

Thanks to Reimond Retz for bugfixes, suggestions and testing

-

Thanks to University of Ume for providing development and test hardware.

+

Thanks to University of Umeå for providing development and test hardware.

Project is hosted by sourceforge.

-

sf_logo

+

sf_logo

diff --git a/docs/manual.rst b/docs/manual.rst index 118cebf77..598ac9c38 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -130,6 +130,11 @@ The ``session`` class has the following synopsis:: int num_uploads() const; int num_connections() const; + bool load_asnum_db(char const* file); + + void load_state(entry const& ses_state); + entry state() const; + void set_ip_filter(ip_filter const& f); session_status status() const; @@ -404,7 +409,31 @@ their turn to get connected. ``max_half_open_connections()`` returns the set limit. This limit defaults to 8 on windows. +load_asnum_db() +--------------- + :: + + bool load_asnum_db(char const* file); + +This function is not available if ``TORRENT_DISABLE_GEO_IP`` is defined. This +expects a path to the `MaxMind ASN database`_. This will be used to look up +which AS peers belong to. + +.. _`MaxMind ASN database`: http://www.maxmind.com/app/asnum + +load_state() state() +-------------------- + + :: + + void load_state(entry const& ses_state); + entry state() const; + +These functions loads and save session state. Currently, the only state +that's stored is peak download rates for ASes. This map is used to +determine which order to connect to peers. + set_ip_filter() --------------- @@ -2336,6 +2365,9 @@ It contains the following fields:: char country[2]; + std::string inet_as_name; + int inet_as; + size_type load_balancing; int download_queue_length; @@ -2523,6 +2555,11 @@ remain set to 0 unless the torrent is set to resolve countries, see `resolve_cou __ http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html +``inet_as_name`` is the name of the AS this peer is located in. This might be +an empty string if there is no name in the geo ip database. + +``inet_as`` is the AS number the peer is located in. + ``load_balancing`` is a measurement of the balancing of free download (that we get) and free upload that we give. Every peer gets a certain amount of free upload, but this member says how much *extra* free upload this peer has got. If it is a negative diff --git a/docs/python_binding.html b/docs/python_binding.html index 0e89bc2c0..3d978f728 100644 --- a/docs/python_binding.html +++ b/docs/python_binding.html @@ -16,18 +16,18 @@ Author: -Arvid Norberg, arvid@rasterbar.com +Arvid Norberg, arvid@rasterbar.com -
-

building

+
+

building

Building the libtorrent python bindings will produce a shared library (DLL) which is a python module that can be imported in a python program.

The only supported build system for the bindings are currently boost build. To @@ -45,10 +45,10 @@ using python : 2.3 : /usr ;

The bindings require at least python version 2.2.

For more information on how to install and set up boost-build, see the -building libtorrent section.

+building libtorrent section.

Once you have boost-build set up, you cd to the bindings/python directory and invoke bjam with the apropriate settings. For the available -build variants, see libtorrent build options.

+build variants, see libtorrent build options.

For example:

 $ bjam dht-support=on boost=source release link=static
@@ -58,10 +58,26 @@ $ bjam dht-support=on boost=source release link=static
 bin/darwin-4.0/release/dht-support-on/link-static/logging-none/threading-multi/libtorrent.so
 
-
-

using

+
+

using libtorrent in python

The python interface is nearly identical to the C++ interface. Please refer to -the main library reference.

+the main library reference. The main differences are:

+
+
asio::tcp::endpoint
+
The endpoint type is represented as a tuple of a string (as the address) and an int for +the port number. E.g. ('127.0.0.1', 6881) represents the localhost port 6881.
+
libtorrent::time_duration
+
The time duration is represented as a number of seconds in a regular integer.
+
+

The following functions takes a reference to a container that is filled with +entries by the function. The python equivalent of these functions instead returns +a list of entries.

+
    +
  • torrent_handle::get_peer_info
  • +
  • torrent_handle::file_progress
  • +
  • torrent_handle::get_download_queue
  • +
  • torrent_handle::piece_availability
  • +

For an example python program, see client.py in the bindings/python directory.

A very simple example usage of the module would be something like this:

diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 4a1d2f11f..4cfe92297 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -152,6 +152,21 @@ void clear_home() #endif +bool print_peers = false; +bool print_log = false; +bool print_downloads = false; +bool print_piece_bar = false; +bool print_file_progress = false; +bool sequential_download = false; + +bool print_ip = true; +bool print_as = false; +bool print_timers = false; +bool print_block = false; +bool print_peer_rate = false; +bool print_fails = false; +bool print_send_bufs = true; + char const* esc(char const* code) { #ifdef ANSI_TERMINAL_COLORS @@ -327,11 +342,21 @@ int peer_index(libtorrent::tcp::endpoint addr, std::vector const& peers) { using namespace libtorrent; - out << "IP down (total) up (total) sent-req recv flags source fail hshf sndb inactive wait disk quota rtt block-progress " -#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES - "country " + if (print_ip) out << "IP "; +#ifndef TORRENT_DISABLE_GEO_IP + if (print_as) out << "AS "; #endif - "peer-rate client \n"; + out << "down (total | peak ) up (total | peak ) sent-req recv flags source "; + if (print_fails) out << "fail hshf "; + if (print_send_bufs) out << "sndb quota "; + if (print_timers) out << "inactive wait "; + out << "disk rtt "; + if (print_block) out << "block-progress "; +#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES + out << "country "; +#endif + if (print_peer_rate) out << "peer-rate "; + out << "client \n"; for (std::vector::const_iterator i = peers.begin(); i != peers.end(); ++i) @@ -340,15 +365,30 @@ void print_peer_info(std::ostream& out, std::vector const continue; out.fill(' '); - std::stringstream ip; - ip << i->ip.address().to_string() << ":" << i->ip.port(); - out.width(22); - out << ip.str() << " "; + if (print_ip) + { + std::stringstream ip; + ip << i->ip.address().to_string() << ":" << i->ip.port(); + out.width(22); + out << ip.str() << " "; + } + +#ifndef TORRENT_DISABLE_GEO_IP + if (print_as) + { + std::string as_name = i->inet_as_name; + if (as_name.size() > 42) as_name.resize(42); + out.width(42); + out << as_name << " "; + } +#endif out.width(2); out << esc("32") << (i->down_speed > 0 ? add_suffix(i->down_speed) + "/s " : " ") - << "(" << add_suffix(i->total_download) << ") " << esc("0") + << "(" << (i->total_download > 0 ? add_suffix(i->total_download) : " ") << "|" + << (i->download_rate_peak > 0 ? add_suffix(i->download_rate_peak) + "/s" : " ") << ") " << esc("0") << esc("31") << (i->up_speed > 0 ? add_suffix(i->up_speed) + "/s ": " ") - << "(" << add_suffix(i->total_upload) << ") " << esc("0") + << "(" << (i->total_upload > 0 ? add_suffix(i->total_upload) : " ") << "|" + << (i->upload_rate_peak > 0 ? add_suffix(i->upload_rate_peak) + "/s" : " ") << ") " << esc("0") << to_string(i->download_queue_length, 3) << " (" << to_string(i->target_dl_queue_length, 3) << ") " << to_string(i->upload_queue_length, 3) << " " @@ -378,24 +418,36 @@ void print_peer_info(std::ostream& out, std::vector const << ((i->source & peer_info::pex)?"P":"_") << ((i->source & peer_info::dht)?"D":"_") << ((i->source & peer_info::lsd)?"L":"_") - << ((i->source & peer_info::resume_data)?"R":"_") << " " - << to_string(i->failcount, 2) << " " - << to_string(i->num_hashfails, 2) << " " - << to_string(i->used_send_buffer, 6) << " ("<< add_suffix(i->send_buffer_size) << ") " - << to_string(total_seconds(i->last_active), 8) << " " - << to_string(total_seconds(i->last_request), 4) << " " - << add_suffix(i->pending_disk_bytes) << " " - << to_string(i->send_quota, 5) << " " + << ((i->source & peer_info::resume_data)?"R":"_") << " "; + if (print_fails) + { + out << to_string(i->failcount, 3) << " " + << to_string(i->num_hashfails, 3) << " "; + } + if (print_send_bufs) + { + out << to_string(i->used_send_buffer, 6) << " ("<< add_suffix(i->send_buffer_size) << ") " + << to_string(i->send_quota, 5) << " "; + } + if (print_timers) + { + out << to_string(total_seconds(i->last_active), 8) << " " + << to_string(total_seconds(i->last_request), 4) << " "; + } + out << add_suffix(i->pending_disk_bytes) << " " << to_string(i->rtt, 4) << " "; - if (i->downloading_piece_index >= 0) + if (print_block) { - out << progress_bar( - i->downloading_progress / float(i->downloading_total), 14); - } - else - { - out << progress_bar(0.f, 14); + if (i->downloading_piece_index >= 0) + { + out << progress_bar( + i->downloading_progress / float(i->downloading_total), 14); + } + else + { + out << progress_bar(0.f, 14); + } } #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES @@ -408,7 +460,8 @@ void print_peer_info(std::ostream& out, std::vector const out << " " << i->country[0] << i->country[1]; } #endif - out << " " << (i->remote_dl_rate > 0 ? add_suffix(i->remote_dl_rate) + "/s ": " ") << " "; + if (print_peer_rate) out << " " << (i->remote_dl_rate > 0 ? add_suffix(i->remote_dl_rate) + "/s ": " "); + out << " "; if (i->flags & peer_info::handshake) { @@ -754,6 +807,9 @@ int main(int ac, char* av[]) // monitor when they're not in the directory anymore. handles_t handles; session ses; +#ifndef TORRENT_DISABLE_GEO_IP + ses.load_asnum_db("GeoIPASNum.dat"); +#endif // UPnP port mapping ses.start_upnp(); // NAT-PMP port mapping @@ -785,6 +841,17 @@ int main(int ac, char* av[]) else ses.set_severity_level(alert::info); + try + { + boost::filesystem::ifstream ses_state_file(".ses_state" + , std::ios_base::binary); + ses_state_file.unsetf(std::ios_base::skipws); + ses.load_state(bdecode( + std::istream_iterator(ses_state_file) + , std::istream_iterator())); + } + catch (std::exception&) {} + #ifndef TORRENT_DISABLE_DHT settings.use_dht_as_fallback = false; @@ -912,14 +979,6 @@ int main(int ac, char* av[]) std::vector peers; std::vector queue; - bool print_peers = false; - bool print_cache = false; - bool print_log = false; - bool print_downloads = false; - bool print_piece_bar = false; - bool print_file_progress = false; - bool sequential_download = false; - for (;;) { char c; @@ -979,12 +1038,20 @@ int main(int ac, char* av[]) , bind(&handles_t::value_type::second, _1))); } + // toggle displays if (c == 'i') print_peers = !print_peers; - if (c == 'c') print_cache = !print_cache; if (c == 'l') print_log = !print_log; if (c == 'd') print_downloads = !print_downloads; if (c == 'f') print_file_progress = !print_file_progress; if (c == 'a') print_piece_bar = !print_piece_bar; + // toggle columns + if (c == '1') print_ip = !print_ip; + if (c == '2') print_as = !print_as; + if (c == '3') print_timers = !print_timers; + if (c == '4') print_block = !print_block; + if (c == '5') print_peer_rate = !print_peer_rate; + if (c == '6') print_fails = !print_fails; + if (c == '7') print_send_bufs = !print_send_bufs; } int terminal_width = 80; @@ -1260,13 +1327,17 @@ int main(int ac, char* av[]) " cache size: " << add_suffix(cs.cache_size * 16 * 1024) << " (" << add_suffix(cs.read_cache_size * 16 * 1024) << ")" " ====" << std::endl; + out << "[q] quit [i] toggle peers [d] toggle downloading pieces [p] pause all " + "[u] unpause all [a] toggle piece bar [s] toggle download sequential [f] toggle files\n" + "[1] toggle IP [2] toggle AS [3] toggle timers [4] toggle block progress " + "[5] toggle peer rate [6] toggle failures [7] toggle send buffers"; if (print_log) { for (std::deque::iterator i = events.begin(); i != events.end(); ++i) { - out << *i << "\n"; + out << "\n" << *i; } } @@ -1283,6 +1354,14 @@ int main(int ac, char* av[]) } } + { + entry session_state = ses.state(); + boost::filesystem::ofstream out(".ses_state" + , std::ios_base::binary); + out.unsetf(std::ios_base::skipws); + bencode(std::ostream_iterator(out), session_state); + } + #ifndef TORRENT_DISABLE_DHT dht_state = ses.dht_state(); boost::filesystem::ofstream out(".dht_state" diff --git a/include/libtorrent/GeoIP.h b/include/libtorrent/GeoIP.h new file mode 100644 index 000000000..60b2c9750 --- /dev/null +++ b/include/libtorrent/GeoIP.h @@ -0,0 +1,184 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* GeoIP.h + * + * Copyright (C) 2006 MaxMind LLC + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef GEOIP_H +#define GEOIP_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include /* for fstat */ +#include /* for fstat */ + +#define SEGMENT_RECORD_LENGTH 3 +#define STANDARD_RECORD_LENGTH 3 +#define ORG_RECORD_LENGTH 4 +#define MAX_RECORD_LENGTH 4 +#define NUM_DB_TYPES 20 + +typedef struct GeoIPTag { + FILE *GeoIPDatabase; + char *file_path; + unsigned char *cache; + unsigned char *index_cache; + unsigned int *databaseSegments; + char databaseType; + time_t mtime; + int flags; + off_t size; + char record_length; + int charset; /* 0 iso-8859-1 1 utf8 */ + int record_iter; /* used in GeoIP_next_record */ + int netmask; /* netmask of last lookup - set using depth in _GeoIP_seek_record */ +} GeoIP; + + +typedef enum { + GEOIP_CHARSET_ISO_8859_1 = 0, + GEOIP_CHARSET_UTF8 = 1 +} GeoIPCharset; + +typedef struct GeoIPRegionTag { + char country_code[3]; + char region[3]; +} GeoIPRegion; + +typedef enum { + GEOIP_STANDARD = 0, + GEOIP_MEMORY_CACHE = 1, + GEOIP_CHECK_CACHE = 2, + GEOIP_INDEX_CACHE = 4, + GEOIP_MMAP_CACHE = 8, +} GeoIPOptions; + +typedef enum { + GEOIP_COUNTRY_EDITION = 1, + GEOIP_REGION_EDITION_REV0 = 7, + GEOIP_CITY_EDITION_REV0 = 6, + GEOIP_ORG_EDITION = 5, + GEOIP_ISP_EDITION = 4, + GEOIP_CITY_EDITION_REV1 = 2, + GEOIP_REGION_EDITION_REV1 = 3, + GEOIP_PROXY_EDITION = 8, + GEOIP_ASNUM_EDITION = 9, + GEOIP_NETSPEED_EDITION = 10, + GEOIP_DOMAIN_EDITION = 11 +} GeoIPDBTypes; + +typedef enum { + GEOIP_ANON_PROXY = 1, + GEOIP_HTTP_X_FORWARDED_FOR_PROXY = 2, + GEOIP_HTTP_CLIENT_IP_PROXY = 3, +} GeoIPProxyTypes; + +typedef enum { + GEOIP_UNKNOWN_SPEED = 0, + GEOIP_DIALUP_SPEED = 1, + GEOIP_CABLEDSL_SPEED = 2, + GEOIP_CORPORATE_SPEED = 3, +} GeoIPNetspeedValues; + +extern char **GeoIPDBFileName; +extern const char * GeoIPDBDescription[NUM_DB_TYPES]; +extern const char *GeoIPCountryDBFileName; +extern const char *GeoIPRegionDBFileName; +extern const char *GeoIPCityDBFileName; +extern const char *GeoIPOrgDBFileName; +extern const char *GeoIPISPDBFileName; + +extern const char GeoIP_country_code[253][3]; +extern const char GeoIP_country_code3[253][4]; +extern const char * GeoIP_country_name[253]; +extern const char GeoIP_country_continent[253][3]; + +#ifdef DLL +#define GEOIP_API __declspec(dllexport) +#else +#define GEOIP_API +#endif /* DLL */ + +GEOIP_API void GeoIP_setup_custom_directory(char *dir); +GEOIP_API GeoIP* GeoIP_open_type (int type, int flags); +GEOIP_API GeoIP* GeoIP_new(int flags); +GEOIP_API GeoIP* GeoIP_open(const char * filename, int flags); +GEOIP_API int GeoIP_db_avail(int type); +GEOIP_API void GeoIP_delete(GeoIP* gi); +GEOIP_API const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr); +GEOIP_API const char *GeoIP_country_code_by_name (GeoIP* gi, const char *host); +GEOIP_API const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr); +GEOIP_API const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *host); +GEOIP_API const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr); +GEOIP_API const char *GeoIP_country_name_by_name (GeoIP* gi, const char *host); +GEOIP_API const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum); +GEOIP_API const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum); +GEOIP_API const char *GeoIP_country_code3_by_ipnum (GeoIP* gi, unsigned long ipnum); + +/* Deprecated - for backwards compatibility only */ +GEOIP_API int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr); +GEOIP_API int GeoIP_country_id_by_name (GeoIP* gi, const char *host); +GEOIP_API char *GeoIP_org_by_addr (GeoIP* gi, const char *addr); +GEOIP_API char *GeoIP_org_by_name (GeoIP* gi, const char *host); +/* End deprecated */ + +GEOIP_API int GeoIP_id_by_addr (GeoIP* gi, const char *addr); +GEOIP_API int GeoIP_id_by_name (GeoIP* gi, const char *host); +GEOIP_API int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum); + +GEOIP_API GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr); +GEOIP_API GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *host); +GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum (GeoIP *gi, unsigned long ipnum); + +/* Warning - don't call this after GeoIP_assign_region_by_inetaddr calls */ +GEOIP_API void GeoIPRegion_delete (GeoIPRegion *gir); + +GEOIP_API void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *gir); + +/* Used to query GeoIP Organization, ISP and AS Number databases */ +GEOIP_API char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum); +GEOIP_API char *GeoIP_name_by_addr (GeoIP* gi, const char *addr); +GEOIP_API char *GeoIP_name_by_name (GeoIP* gi, const char *host); + +GEOIP_API char *GeoIP_database_info (GeoIP* gi); +GEOIP_API unsigned char GeoIP_database_edition (GeoIP* gi); + +GEOIP_API int GeoIP_charset (GeoIP* gi); +GEOIP_API int GeoIP_set_charset (GeoIP* gi, int charset); + +GEOIP_API int GeoIP_last_netmask (GeoIP* gi); + +/* Convert region code to region name */ +GEOIP_API const char * GeoIP_region_name_by_code(const char *country_code, const char *region_code); + +/* Get timezone from country and region code */ +GEOIP_API const char * GeoIP_time_zone_by_country_and_region(const char *country_code, const char *region_code); + +#ifdef BSD +#define memcpy(dest, src, n) bcopy(src, dest, n) +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* GEOIP_H */ diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 5eefd7f66..7a4ac720d 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -40,6 +40,10 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#ifndef TORRENT_DISABLE_GEO_IP +#include "libtorrent/GeoIP.h" +#endif + #ifdef _MSC_VER #pragma warning(push, 1) #endif @@ -285,6 +289,17 @@ namespace libtorrent { return m_dht_proxy; } #endif +#ifndef TORRENT_DISABLE_GEO_IP + std::string as_name_for_ip(address const& a); + int as_for_ip(address const& a); + std::pair* lookup_as(int as); + bool load_asnum_db(char const* file); + bool has_asnum_db() const { return m_geoip_db; } +#endif + + void load_state(entry const& ses_state); + entry state() const; + #ifdef TORRENT_STATS void log_buffer_usage() { @@ -566,6 +581,16 @@ namespace libtorrent extension_list_t m_extensions; #endif +#ifndef TORRENT_DISABLE_GEO_IP + GeoIP* m_geoip_db; + + // maps AS number to the peak download rate + // we've seen from it. Entries are never removed + // from this map. Pointers to its elements + // are kept in the policy::peer structures. + std::map m_as_peak; +#endif + // the main working thread boost::scoped_ptr m_thread; }; diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 1cdc07484..4d3942c98 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -781,11 +781,14 @@ namespace libtorrent // they sent us size_type m_downloaded_at_last_unchoke; +#ifndef TORRENT_DISABLE_GEO_IP + std::string m_inet_as_name; +#endif + // max transfer rates seen on this peer int m_download_rate_peak; int m_upload_rate_peak; - #ifndef NDEBUG public: bool m_in_constructor; diff --git a/include/libtorrent/peer_info.hpp b/include/libtorrent/peer_info.hpp index 1560717d3..2ec9742a7 100755 --- a/include/libtorrent/peer_info.hpp +++ b/include/libtorrent/peer_info.hpp @@ -122,6 +122,12 @@ namespace libtorrent char country[2]; #endif +#ifndef TORRENT_DISABLE_GEO_IP + // atonomous system this peer belongs to + std::string inet_as_name; + int inet_as; +#endif + size_type load_balancing; // this is the number of requests diff --git a/include/libtorrent/policy.hpp b/include/libtorrent/policy.hpp index 0bb894b5f..e952b4f02 100755 --- a/include/libtorrent/policy.hpp +++ b/include/libtorrent/policy.hpp @@ -126,18 +126,23 @@ namespace libtorrent struct peer { enum connection_type { not_connectable, connectable }; - peer(tcp::endpoint const& ip, connection_type t, int src); size_type total_download() const; size_type total_upload() const; // the ip/port pair this peer is or was connected on - // if it was a remote (incoming) connection, type is - // set thereafter. If it was a peer we got from the - // tracker, type is set to local_connection. tcp::endpoint ip; - connection_type type; + +#ifndef TORRENT_DISABLE_GEO_IP +#ifndef NDEBUG + // only used in debug mode to assert that + // the first entry in the AS pair keeps the same + boost::uint16_t inet_as_num; +#endif + // The AS this peer belongs to + std::pair* inet_as; +#endif // the number of failed connection attempts // this peer has @@ -159,9 +164,14 @@ namespace libtorrent // part of a piece that failed the hash check boost::uint8_t hashfails; + // type specifies if the connection was incoming + // or outgoing. If we ever saw this peer as connectable + // it will remain as connectable + unsigned type:4; + // the number of times we have allowed a fast // reconnect for this peer. - boost::uint8_t fast_reconnects:4; + unsigned fast_reconnects:4; #ifndef TORRENT_DISABLE_ENCRYPTION // Hints encryption support of peer. Only effective diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 402630b2f..667c69133 100755 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -200,6 +200,13 @@ namespace libtorrent void add_extension(boost::function(torrent*, void*)> ext); #endif +#ifndef TORRENT_DISABLE_GEO_IP + bool load_asnum_db(char const* file); +#endif + + void load_state(entry const& ses_state); + entry state() const; + void set_ip_filter(ip_filter const& f); void set_port_filter(port_filter const& f); void set_peer_id(peer_id const& pid); diff --git a/src/GeoIP.c b/src/GeoIP.c new file mode 100644 index 000000000..3bf2b9d25 --- /dev/null +++ b/src/GeoIP.c @@ -0,0 +1,1031 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* GeoIP.c + * + * Copyright (C) 2006 MaxMind LLC + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "GeoIP.h" + +#ifndef WIN32 +#include +#include +#include /* For ntohl */ +#include + +#include + +#else +#include +#define snprintf _snprintf +#endif +#include +#include +#include +#include +#include +#include /* for fstat */ +#include /* for fstat */ + +#ifdef HAVE_STDINT_H +#include /* For uint32_t */ +#endif + +#ifndef INADDR_NONE +#define INADDR_NONE -1 +#endif + +#define COUNTRY_BEGIN 16776960 +#define STATE_BEGIN_REV0 16700000 +#define STATE_BEGIN_REV1 16000000 +#define STRUCTURE_INFO_MAX_SIZE 20 +#define DATABASE_INFO_MAX_SIZE 100 +#define MAX_ORG_RECORD_LENGTH 300 +#define US_OFFSET 1 +#define CANADA_OFFSET 677 +#define WORLD_OFFSET 1353 +#define FIPS_RANGE 360 + +#define CHECK_ERR(err, msg) { \ + if (err != Z_OK) { \ + fprintf(stderr, "%s error: %d\n", msg, err); \ + exit(1); \ + } \ +} + +const char GeoIP_country_code[253][3] = { "--","AP","EU","AD","AE","AF","AG","AI","AL","AM","AN", + "AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB", + "BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO", + "BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD", + "CF","CG","CH","CI","CK","CL","CM","CN","CO","CR", + "CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO", + "DZ","EC","EE","EG","EH","ER","ES","ET","FI","FJ", + "FK","FM","FO","FR","FX","GA","GB","GD","GE","GF", + "GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT", + "GU","GW","GY","HK","HM","HN","HR","HT","HU","ID", + "IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO", + "JP","KE","KG","KH","KI","KM","KN","KP","KR","KW", + "KY","KZ","LA","LB","LC","LI","LK","LR","LS","LT", + "LU","LV","LY","MA","MC","MD","MG","MH","MK","ML", + "MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV", + "MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI", + "NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF", + "PG","PH","PK","PL","PM","PN","PR","PS","PT","PW", + "PY","QA","RE","RO","RU","RW","SA","SB","SC","SD", + "SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO", + "SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH", + "TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW", + "TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE", + "VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA", + "ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE", + "BL","MF"}; + +const char GeoIP_country_code3[253][4] = { "--","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","ANT", + "AGO","AQ","ARG","ASM","AUT","AUS","ABW","AZE","BIH","BRB", + "BGD","BEL","BFA","BGR","BHR","BDI","BEN","BMU","BRN","BOL", + "BRA","BHS","BTN","BV","BWA","BLR","BLZ","CAN","CC","COD", + "CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI", + "CUB","CPV","CX","CYP","CZE","DEU","DJI","DNK","DMA","DOM", + "DZA","ECU","EST","EGY","ESH","ERI","ESP","ETH","FIN","FJI", + "FLK","FSM","FRO","FRA","FX","GAB","GBR","GRD","GEO","GUF", + "GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","GS","GTM", + "GUM","GNB","GUY","HKG","HM","HND","HRV","HTI","HUN","IDN", + "IRL","ISR","IND","IO","IRQ","IRN","ISL","ITA","JAM","JOR", + "JPN","KEN","KGZ","KHM","KIR","COM","KNA","PRK","KOR","KWT", + "CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU", + "LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI", + "MMR","MNG","MAC","MNP","MTQ","MRT","MSR","MLT","MUS","MDV", + "MWI","MEX","MYS","MOZ","NAM","NCL","NER","NFK","NGA","NIC", + "NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER","PYF", + "PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW", + "PRY","QAT","REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN", + "SWE","SGP","SHN","SVN","SJM","SVK","SLE","SMR","SEN","SOM", + "SUR","STP","SLV","SYR","SWZ","TCA","TCD","TF","TGO","THA", + "TJK","TKL","TKM","TUN","TON","TLS","TUR","TTO","TUV","TWN", + "TZA","UKR","UGA","UM","USA","URY","UZB","VAT","VCT","VEN", + "VGB","VIR","VNM","VUT","WLF","WSM","YEM","YT","SRB","ZAF", + "ZMB","MNE","ZWE","A1","A2","O1","ALA","GGY","IMN","JEY", + "BLM","MAF"}; + +const char * GeoIP_country_name[253] = {"N/A","Asia/Pacific Region","Europe","Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia","Netherlands Antilles", + "Angola","Antarctica","Argentina","American Samoa","Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina","Barbados", + "Bangladesh","Belgium","Burkina Faso","Bulgaria","Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia", + "Brazil","Bahamas","Bhutan","Bouvet Island","Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands","Congo, The Democratic Republic of the", + "Central African Republic","Congo","Switzerland","Cote D'Ivoire","Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica", + "Cuba","Cape Verde","Christmas Island","Cyprus","Czech Republic","Germany","Djibouti","Denmark","Dominica","Dominican Republic", + "Algeria","Ecuador","Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland","Fiji", + "Falkland Islands (Malvinas)","Micronesia, Federated States of","Faroe Islands","France","France, Metropolitan","Gabon","United Kingdom","Grenada","Georgia","French Guiana", + "Ghana","Gibraltar","Greenland","Gambia","Guinea","Guadeloupe","Equatorial Guinea","Greece","South Georgia and the South Sandwich Islands","Guatemala", + "Guam","Guinea-Bissau","Guyana","Hong Kong","Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary","Indonesia", + "Ireland","Israel","India","British Indian Ocean Territory","Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica","Jordan", + "Japan","Kenya","Kyrgyzstan","Cambodia","Kiribati","Comoros","Saint Kitts and Nevis","Korea, Democratic People's Republic of","Korea, Republic of","Kuwait", + "Cayman Islands","Kazakhstan","Lao People's Democratic Republic","Lebanon","Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho","Lithuania", + "Luxembourg","Latvia","Libyan Arab Jamahiriya","Morocco","Monaco","Moldova, Republic of","Madagascar","Marshall Islands","Macedonia","Mali", + "Myanmar","Mongolia","Macau","Northern Mariana Islands","Martinique","Mauritania","Montserrat","Malta","Mauritius","Maldives", + "Malawi","Mexico","Malaysia","Mozambique","Namibia","New Caledonia","Niger","Norfolk Island","Nigeria","Nicaragua", + "Netherlands","Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama","Peru","French Polynesia", + "Papua New Guinea","Philippines","Pakistan","Poland","Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico","Palestinian Territory","Portugal","Palau", + "Paraguay","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia","Solomon Islands","Seychelles","Sudan", + "Sweden","Singapore","Saint Helena","Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino","Senegal","Somalia","Suriname", + "Sao Tome and Principe","El Salvador","Syrian Arab Republic","Swaziland","Turks and Caicos Islands","Chad","French Southern Territories","Togo","Thailand", + "Tajikistan","Tokelau","Turkmenistan","Tunisia","Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu","Taiwan", + "Tanzania, United Republic of","Ukraine","Uganda","United States Minor Outlying Islands","United States","Uruguay","Uzbekistan","Holy See (Vatican City State)","Saint Vincent and the Grenadines","Venezuela", + "Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa", + "Zambia","Montenegro","Zimbabwe","Anonymous Proxy","Satellite Provider","Other","Aland Islands","Guernsey","Isle of Man","Jersey", + "Saint Barthelemy","Saint Martin"}; + +/* Possible continent codes are AF, AS, EU, NA, OC, SA for Africa, Asia, Europe, North America, Oceania +and South America. */ + +const char GeoIP_country_continent[253][3] = {"--","AS","EU","EU","AS","AS","SA","SA","EU","AS","SA", + "AF","AN","SA","OC","EU","OC","SA","AS","EU","SA", + "AS","EU","AF","EU","AS","AF","AF","SA","AS","SA", + "SA","SA","AS","AF","AF","EU","SA","NA","AS","AF", + "AF","AF","EU","AF","OC","SA","AF","AS","SA","SA", + "SA","AF","AS","AS","EU","EU","AF","EU","SA","SA", + "AF","SA","EU","AF","AF","AF","EU","AF","EU","OC", + "SA","OC","EU","EU","EU","AF","EU","SA","AS","SA", + "AF","EU","SA","AF","AF","SA","AF","EU","SA","SA", + "OC","AF","SA","AS","AF","SA","EU","SA","EU","AS", + "EU","AS","AS","AS","AS","AS","EU","EU","SA","AS", + "AS","AF","AS","AS","OC","AF","SA","AS","AS","AS", + "SA","AS","AS","AS","SA","EU","AS","AF","AF","EU", + "EU","EU","AF","AF","EU","EU","AF","OC","EU","AF", + "AS","AS","AS","OC","SA","AF","SA","EU","AF","AS", + "AF","NA","AS","AF","AF","OC","AF","OC","AF","SA", + "EU","EU","AS","OC","OC","OC","AS","SA","SA","OC", + "OC","AS","AS","EU","SA","OC","SA","AS","EU","OC", + "SA","AS","AF","EU","AS","AF","AS","OC","AF","AF", + "EU","AS","AF","EU","EU","EU","AF","EU","AF","AF", + "SA","AF","SA","AS","AF","SA","AF","AF","AF","AS", + "AS","OC","AS","AF","OC","AS","AS","SA","OC","AS", + "AF","EU","AF","OC","NA","SA","AS","EU","SA","SA", + "SA","SA","AS","OC","OC","OC","AS","AF","EU","AF", + "AF","EU","AF","--","--","--","EU","EU","EU","EU", + "SA","SA"}; + +const char * GeoIPDBDescription[NUM_DB_TYPES] = {NULL, "GeoIP Country Edition", "GeoIP City Edition, Rev 1", "GeoIP Region Edition, Rev 1", "GeoIP ISP Edition", "GeoIP Organization Edition", "GeoIP City Edition, Rev 0", "GeoIP Region Edition, Rev 0","GeoIP Proxy Edition","GeoIP ASNum Edition","GeoIP Netspeed Edition","GeoIP Domain Name Edition"}; + +char * custom_directory = NULL; + +void GeoIP_setup_custom_directory (char * dir) { + custom_directory = dir; +} +/* +char *_GeoIP_full_path_to(const char *file_name) { + int len; + char *path = malloc(sizeof(char) * 1024); + + if (custom_directory == NULL){ +#ifndef WIN32 + memset(path, 0, sizeof(char) * 1024); + snprintf(path, sizeof(char) * 1024 - 1, "%s/%s", GEOIPDATADIR, file_name); +#else + char buf[MAX_PATH], *p, *q = NULL; + memset(buf, 0, sizeof(buf)); + len = GetModuleFileName(GetModuleHandle(NULL), buf, sizeof(buf) - 1); + for (p = buf + len; p > buf; p--) + if (*p == '\\') + { + if (!q) + q = p; + else + *p = '/'; + } + *q = 0; + memset(path, 0, sizeof(char) * 1024); + snprintf(path, sizeof(char) * 1024 - 1, "%s/%s", buf, file_name); +#endif + } else { + len = strlen(custom_directory); + if (custom_directory[len-1] != '/') { + snprintf(path, sizeof(char) * 1024 - 1, "%s/%s",custom_directory, file_name); + } else { + snprintf(path, sizeof(char) * 1024 - 1, "%s%s", custom_directory, file_name); + } + } + return path; +} + +char ** GeoIPDBFileName = NULL; + +void _GeoIP_setup_dbfilename() { + if (NULL == GeoIPDBFileName) { + GeoIPDBFileName = malloc(sizeof(char *) * NUM_DB_TYPES); + memset(GeoIPDBFileName, 0, sizeof(char *) * NUM_DB_TYPES); + + GeoIPDBFileName[GEOIP_COUNTRY_EDITION] = _GeoIP_full_path_to("GeoIP.dat"); + GeoIPDBFileName[GEOIP_REGION_EDITION_REV0] = _GeoIP_full_path_to("GeoIPRegion.dat"); + GeoIPDBFileName[GEOIP_REGION_EDITION_REV1] = _GeoIP_full_path_to("GeoIPRegion.dat"); + GeoIPDBFileName[GEOIP_CITY_EDITION_REV0] = _GeoIP_full_path_to("GeoIPCity.dat"); + GeoIPDBFileName[GEOIP_CITY_EDITION_REV1] = _GeoIP_full_path_to("GeoIPCity.dat"); + GeoIPDBFileName[GEOIP_ISP_EDITION] = _GeoIP_full_path_to("GeoIPISP.dat"); + GeoIPDBFileName[GEOIP_ORG_EDITION] = _GeoIP_full_path_to("GeoIPOrg.dat"); + GeoIPDBFileName[GEOIP_PROXY_EDITION] = _GeoIP_full_path_to("GeoIPProxy.dat"); + GeoIPDBFileName[GEOIP_ASNUM_EDITION] = _GeoIP_full_path_to("GeoIPASNum.dat"); + GeoIPDBFileName[GEOIP_NETSPEED_EDITION] = _GeoIP_full_path_to("GeoIPNetSpeed.dat"); + GeoIPDBFileName[GEOIP_DOMAIN_EDITION] = _GeoIP_full_path_to("GeoIPDomain.dat"); + } +} +*/ + +static +int _file_exists(const char *file_name) { + struct stat file_stat; + return( (stat(file_name, &file_stat) == 0) ? 1:0); +} +/* +int GeoIP_db_avail(int type) { + const char * filePath; + if (type < 0 || type >= NUM_DB_TYPES) { + return 0; + } + _GeoIP_setup_dbfilename(); + filePath = GeoIPDBFileName[type]; + if (NULL == filePath) { + return 0; + } + return _file_exists(filePath); +} +*/ +static +void _setup_segments(GeoIP * gi) { + int i, j; + unsigned char delim[3]; + unsigned char buf[SEGMENT_RECORD_LENGTH]; + + gi->databaseSegments = NULL; + + /* default to GeoIP Country Edition */ + gi->databaseType = GEOIP_COUNTRY_EDITION; + gi->record_length = STANDARD_RECORD_LENGTH; + fseek(gi->GeoIPDatabase, -3l, SEEK_END); + for (i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) { + fread(delim, 1, 3, gi->GeoIPDatabase); + if (delim[0] == 255 && delim[1] == 255 && delim[2] == 255) { + fread(&gi->databaseType, 1, 1, gi->GeoIPDatabase); + if (gi->databaseType >= 106) { + /* backwards compatibility with databases from April 2003 and earlier */ + gi->databaseType -= 105; + } + + if (gi->databaseType == GEOIP_REGION_EDITION_REV0) { + /* Region Edition, pre June 2003 */ + gi->databaseSegments = malloc(sizeof(int)); + gi->databaseSegments[0] = STATE_BEGIN_REV0; + } else if (gi->databaseType == GEOIP_REGION_EDITION_REV1) { + /* Region Edition, post June 2003 */ + gi->databaseSegments = malloc(sizeof(int)); + gi->databaseSegments[0] = STATE_BEGIN_REV1; + } else if (gi->databaseType == GEOIP_CITY_EDITION_REV0 || + gi->databaseType == GEOIP_CITY_EDITION_REV1 || + gi->databaseType == GEOIP_ORG_EDITION || + gi->databaseType == GEOIP_ISP_EDITION || + gi->databaseType == GEOIP_ASNUM_EDITION) { + /* City/Org Editions have two segments, read offset of second segment */ + gi->databaseSegments = malloc(sizeof(int)); + gi->databaseSegments[0] = 0; + fread(buf, SEGMENT_RECORD_LENGTH, 1, gi->GeoIPDatabase); + for (j = 0; j < SEGMENT_RECORD_LENGTH; j++) { + gi->databaseSegments[0] += (buf[j] << (j * 8)); + } + if (gi->databaseType == GEOIP_ORG_EDITION || + gi->databaseType == GEOIP_ISP_EDITION) + gi->record_length = ORG_RECORD_LENGTH; + } + break; + } else { + fseek(gi->GeoIPDatabase, -4l, SEEK_CUR); + } + } + if (gi->databaseType == GEOIP_COUNTRY_EDITION || + gi->databaseType == GEOIP_PROXY_EDITION || + gi->databaseType == GEOIP_NETSPEED_EDITION) { + gi->databaseSegments = malloc(sizeof(int)); + gi->databaseSegments[0] = COUNTRY_BEGIN; + } +} + +static +int _check_mtime(GeoIP *gi) { + struct stat buf; + if (gi->flags & GEOIP_CHECK_CACHE) { + if (stat(gi->file_path, &buf) != -1) { + if (buf.st_mtime != gi->mtime) { + /* GeoIP Database file updated */ + if (gi->flags & (GEOIP_MEMORY_CACHE | GEOIP_MMAP_CACHE)) { + if ( gi->flags & GEOIP_MMAP_CACHE) { + munmap(gi->cache, gi->size); + gi->cache = NULL; + } else { + /* reload database into memory cache */ + if ((gi->cache = (unsigned char*) realloc(gi->cache, buf.st_size)) == NULL) { + fprintf(stderr,"Out of memory when reloading %s\n",gi->file_path); + return -1; + } + } + } + /* refresh filehandle */ + fclose(gi->GeoIPDatabase); + gi->GeoIPDatabase = fopen(gi->file_path,"rb"); + if (gi->GeoIPDatabase == NULL) { + fprintf(stderr,"Error Opening file %s when reloading\n",gi->file_path); + return -1; + } + gi->mtime = buf.st_mtime; + gi->size = buf.st_size; + + if ( gi->flags & GEOIP_MMAP_CACHE) { + gi->cache = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fileno(gi->GeoIPDatabase), 0); + if ( gi->cache == MAP_FAILED ) { + + fprintf(stderr,"Error remapping file %s when reloading\n",gi->file_path); + gi->cache = 0; + return -1; + } + } else if ( gi->flags & GEOIP_MEMORY_CACHE ) { + if (fread(gi->cache, sizeof(unsigned char), buf.st_size, gi->GeoIPDatabase) != (size_t) buf.st_size) { + fprintf(stderr,"Error reading file %s when reloading\n",gi->file_path); + return -1; + } + } + if (gi->databaseSegments != NULL) { + free(gi->databaseSegments); + gi->databaseSegments = NULL; + } + _setup_segments(gi); + if (gi->databaseSegments == NULL) { + fprintf(stderr, "Error reading file %s -- corrupt\n", gi->file_path); + return -1; + } + if (gi->flags & GEOIP_INDEX_CACHE) { + gi->index_cache = (unsigned char *) realloc(gi->index_cache, sizeof(unsigned char) * ((gi->databaseSegments[0] * (long)gi->record_length * 2))); + if (gi->index_cache != NULL) { + fseek(gi->GeoIPDatabase, 0, SEEK_SET); + if (fread(gi->index_cache, sizeof(unsigned char), gi->databaseSegments[0] * (long)gi->record_length * 2, gi->GeoIPDatabase) != (size_t) (gi->databaseSegments[0]*(long)gi->record_length * 2)) { + fprintf(stderr,"Error reading file %s where reloading\n",gi->file_path); + return -1; + } + } + } + } + } + } + return 0; +} + +unsigned int _GeoIP_seek_record (GeoIP *gi, unsigned long ipnum) { + int depth; + unsigned int x; + unsigned char stack_buffer[2 * MAX_RECORD_LENGTH]; + const unsigned char *buf = (gi->cache == NULL) ? stack_buffer : NULL; + unsigned int offset = 0; + + const unsigned char * p; + int j; + + _check_mtime(gi); + for (depth = 31; depth >= 0; depth--) { + if (gi->cache == NULL && gi->index_cache == NULL) { + /* read from disk */ + fseek(gi->GeoIPDatabase, (long)gi->record_length * 2 * offset, SEEK_SET); + fread(stack_buffer,gi->record_length,2,gi->GeoIPDatabase); + } else if (gi->index_cache == NULL) { + /* simply point to record in memory */ + buf = gi->cache + (long)gi->record_length * 2 *offset; + } else { + buf = gi->index_cache + (long)gi->record_length * 2 * offset; + } + + if (ipnum & (1 << depth)) { + /* Take the right-hand branch */ + if ( gi->record_length == 3 ) { + /* Most common case is completely unrolled and uses constants. */ + x = (buf[3*1 + 0] << (0*8)) + + (buf[3*1 + 1] << (1*8)) + + (buf[3*1 + 2] << (2*8)); + + } else { + /* General case */ + j = gi->record_length; + p = &buf[2*j]; + x = 0; + do { + x <<= 8; + x += *(--p); + } while ( --j ); + } + + } else { + /* Take the left-hand branch */ + if ( gi->record_length == 3 ) { + /* Most common case is completely unrolled and uses constants. */ + x = (buf[3*0 + 0] << (0*8)) + + (buf[3*0 + 1] << (1*8)) + + (buf[3*0 + 2] << (2*8)); + } else { + /* General case */ + j = gi->record_length; + p = &buf[1*j]; + x = 0; + do { + x <<= 8; + x += *(--p); + } while ( --j ); + } + } + + if (x >= gi->databaseSegments[0]) { + gi->netmask = 32 - depth; + return x; + } + offset = x; + } + + /* shouldn't reach here */ + fprintf(stderr,"Error Traversing Database for ipnum = %lu - Perhaps database is corrupt?\n",ipnum); + return 0; +} + +unsigned long +_GeoIP_addr_to_num(const char *addr) +{ + unsigned int c, octet, t; + unsigned long ipnum; + int i = 3; + + octet = ipnum = 0; + while ((c = *addr++)) { + if (c == '.') { + if (octet > 255) + return 0; + ipnum <<= 8; + ipnum += octet; + i--; + octet = 0; + } else { + t = octet; + octet <<= 3; + octet += t; + octet += t; + c -= '0'; + if (c > 9) + return 0; + octet += c; + } + } + if ((octet > 255) || (i != 0)) + return 0; + ipnum <<= 8; + return ipnum + octet; +} +/* +GeoIP* GeoIP_open_type (int type, int flags) { + GeoIP * gi; + const char * filePath; + if (type < 0 || type >= NUM_DB_TYPES) { + printf("Invalid database type %d\n", type); + return NULL; + } + _GeoIP_setup_dbfilename(); + filePath = GeoIPDBFileName[type]; + if (filePath == NULL) { + printf("Invalid database type %d\n", type); + return NULL; + } + gi = GeoIP_open (filePath, flags); + return gi; +} + +GeoIP* GeoIP_new (int flags) { + GeoIP * gi; + _GeoIP_setup_dbfilename(); + gi = GeoIP_open (GeoIPDBFileName[GEOIP_COUNTRY_EDITION], flags); + return gi; +} +*/ +GeoIP* GeoIP_open (const char * filename, int flags) { + struct stat buf; + GeoIP * gi; + size_t len; + +#ifdef WIN32 + WSADATA wsa; + if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0) + return NULL; +#endif + + gi = (GeoIP *)malloc(sizeof(GeoIP)); + if (gi == NULL) + return NULL; + len = sizeof(char) * (strlen(filename)+1); + gi->file_path = malloc(len); + if (gi->file_path == NULL) { + free(gi); + return NULL; + } + strncpy(gi->file_path, filename, len); + gi->GeoIPDatabase = fopen(filename,"rb"); + if (gi->GeoIPDatabase == NULL) { + fprintf(stderr,"Error Opening file %s\n",filename); + free(gi->file_path); + free(gi); + return NULL; + } else { + if (flags & (GEOIP_MEMORY_CACHE | GEOIP_MMAP_CACHE) ) { + if (fstat(fileno(gi->GeoIPDatabase), &buf) == -1) { + fprintf(stderr,"Error stating file %s\n",filename); + free(gi->file_path); + free(gi); + return NULL; + } + gi->mtime = buf.st_mtime; + gi->size = buf.st_size; + /* MMAP added my Peter Shipley */ + if ( flags & GEOIP_MMAP_CACHE) { + gi->cache = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fileno(gi->GeoIPDatabase), 0); + if ( gi->cache == MAP_FAILED ) { + fprintf(stderr,"Error mmaping file %s\n",filename); + free(gi->file_path); + free(gi); + return NULL; + } + } else { + gi->cache = (unsigned char *) malloc(sizeof(unsigned char) * buf.st_size); + + if (gi->cache != NULL) { + if (fread(gi->cache, sizeof(unsigned char), buf.st_size, gi->GeoIPDatabase) != (size_t) buf.st_size) { + fprintf(stderr,"Error reading file %s\n",filename); + free(gi->cache); + free(gi->file_path); + free(gi); + return NULL; + } + } + } + } else { + if (flags & GEOIP_CHECK_CACHE) { + if (fstat(fileno(gi->GeoIPDatabase), &buf) == -1) { + fprintf(stderr,"Error stating file %s\n",filename); + free(gi->file_path); + free(gi); + return NULL; + } + gi->mtime = buf.st_mtime; + } + gi->cache = NULL; + } + gi->flags = flags; + gi->charset = GEOIP_CHARSET_ISO_8859_1; + + _setup_segments(gi); + if (flags & GEOIP_INDEX_CACHE) { + gi->index_cache = (unsigned char *) malloc(sizeof(unsigned char) * ((gi->databaseSegments[0] * (long)gi->record_length * 2))); + if (gi->index_cache != NULL) { + fseek(gi->GeoIPDatabase, 0, SEEK_SET); + if (fread(gi->index_cache, sizeof(unsigned char), gi->databaseSegments[0] * (long)gi->record_length * 2, gi->GeoIPDatabase) != (size_t) (gi->databaseSegments[0]*(long)gi->record_length * 2)) { + fprintf(stderr,"Error reading file %s\n",filename); + free(gi->databaseSegments); + free(gi->index_cache); + free(gi); + return NULL; + } + } + } else { + gi->index_cache = NULL; + } + return gi; + } +} + +void GeoIP_delete (GeoIP *gi) { + if (gi == NULL ) + return; + if (gi->GeoIPDatabase != NULL) + fclose(gi->GeoIPDatabase); + if (gi->cache != NULL) { + if ( gi->flags & GEOIP_MMAP_CACHE) { + munmap(gi->cache, gi->size); + } else { + free(gi->cache); + } + gi->cache = NULL; + } + if (gi->index_cache != NULL) + free(gi->index_cache); + if (gi->file_path != NULL) + free(gi->file_path); + if (gi->databaseSegments != NULL) + free(gi->databaseSegments); + free(gi); +} + +const char *GeoIP_country_code_by_name (GeoIP* gi, const char *name) { + int country_id; + country_id = GeoIP_id_by_name(gi, name); + return (country_id > 0) ? GeoIP_country_code[country_id] : NULL; +} + +const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *name) { + int country_id; + country_id = GeoIP_id_by_name(gi, name); + return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL; +} + +const char *GeoIP_country_name_by_name (GeoIP* gi, const char *name) { + int country_id; + country_id = GeoIP_id_by_name(gi, name); + return (country_id > 0) ? GeoIP_country_name[country_id] : NULL; +} + +unsigned long _GeoIP_lookupaddress (const char *host) { + unsigned long addr = inet_addr(host); + struct hostent phe2; + struct hostent * phe = &phe2; + char *buf = NULL; + int buflength = 16384; + int herr = 0; + int result = 0; +#ifdef HAVE_GETHOSTBYNAME_R + buf = malloc(buflength); +#endif + if (addr == INADDR_NONE) { +#ifdef HAVE_GETHOSTBYNAME_R + while (1) { + /* we use gethostbyname_r here because it is thread-safe and gethostbyname is not */ +#ifdef GETHOSTBYNAME_R_RETURNS_INT + result = gethostbyname_r(host,&phe2,buf,buflength,&phe,&herr); +#else + phe = gethostbyname_r(host,&phe2,buf,buflength,&herr); +#endif + if (herr != ERANGE) + break; + if (result == 0) + break; + /* double the buffer if the buffer is too small */ + buflength = buflength * 2; + buf = realloc(buf,buflength); + } +#endif +#ifndef HAVE_GETHOSTBYNAME_R + /* Some systems do not support gethostbyname_r, such as Mac OS X */ + phe = gethostbyname(host); +#endif + if (!phe || result != 0) { + free(buf); + return 0; + } + addr = *((unsigned long *) phe->h_addr_list[0]); + } +#ifdef HAVE_GETHOSTBYNAME_R + free(buf); +#endif + return ntohl(addr); +} + +int GeoIP_id_by_name (GeoIP* gi, const char *name) { + unsigned long ipnum; + int ret; + if (name == NULL) { + return 0; + } + if (gi->databaseType != GEOIP_COUNTRY_EDITION && gi->databaseType != GEOIP_PROXY_EDITION && gi->databaseType != GEOIP_NETSPEED_EDITION) { + printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_COUNTRY_EDITION]); + return 0; + } + if (!(ipnum = _GeoIP_lookupaddress(name))) + return 0; + ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN; + return ret; + +} + +const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr) { + int country_id; + country_id = GeoIP_id_by_addr(gi, addr); + return (country_id > 0) ? GeoIP_country_code[country_id] : NULL; +} + +const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr) { + int country_id; + country_id = GeoIP_id_by_addr(gi, addr); + return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL; + return GeoIP_country_code3[country_id]; +} + +const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr) { + int country_id; + country_id = GeoIP_id_by_addr(gi, addr); + return (country_id > 0) ? GeoIP_country_name[country_id] : NULL; + return GeoIP_country_name[country_id]; +} + +const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum) { + int country_id; + country_id = GeoIP_id_by_ipnum(gi, ipnum); + return (country_id > 0) ? GeoIP_country_name[country_id] : NULL; +} + +const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum) { + int country_id; + country_id = GeoIP_id_by_ipnum(gi, ipnum); + return (country_id > 0) ? GeoIP_country_code[country_id] : NULL; +} + +const char *GeoIP_country_code3_by_ipnum (GeoIP* gi, unsigned long ipnum) { + int country_id; + country_id = GeoIP_id_by_ipnum(gi, ipnum); + return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL; +} + +int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr) { + return GeoIP_id_by_addr(gi, addr); +} + +int GeoIP_country_id_by_name (GeoIP* gi, const char *host) { + return GeoIP_id_by_name(gi, host); +} + +int GeoIP_id_by_addr (GeoIP* gi, const char *addr) { + unsigned long ipnum; + int ret; + if (addr == NULL) { + return 0; + } + if (gi->databaseType != GEOIP_COUNTRY_EDITION && + gi->databaseType != GEOIP_PROXY_EDITION && + gi->databaseType != GEOIP_NETSPEED_EDITION) { + printf("Invalid database type %s, expected %s\n", + GeoIPDBDescription[(int)gi->databaseType], + GeoIPDBDescription[GEOIP_COUNTRY_EDITION]); + return 0; + } + ipnum = _GeoIP_addr_to_num(addr); + ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN; + return ret; +} + +int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum) { + int ret; + if (ipnum == 0) { + return 0; + } + if (gi->databaseType != GEOIP_COUNTRY_EDITION && + gi->databaseType != GEOIP_PROXY_EDITION && + gi->databaseType != GEOIP_NETSPEED_EDITION) { + printf("Invalid database type %s, expected %s\n", + GeoIPDBDescription[(int)gi->databaseType], + GeoIPDBDescription[GEOIP_COUNTRY_EDITION]); + return 0; + } + ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN; + return ret; +} + +char *GeoIP_database_info (GeoIP* gi) { + int i; + unsigned char buf[3]; + char *retval; + int hasStructureInfo = 0; + + if(gi == NULL) + return NULL; + + _check_mtime(gi); + fseek(gi->GeoIPDatabase, -3l, SEEK_END); + + /* first get past the database structure information */ + for (i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) { + fread(buf, 1, 3, gi->GeoIPDatabase); + if (buf[0] == 255 && buf[1] == 255 && buf[2] == 255) { + hasStructureInfo = 1; + break; + } + fseek(gi->GeoIPDatabase, -4l, SEEK_CUR); + } + if (hasStructureInfo == 1) { + fseek(gi->GeoIPDatabase, -6l, SEEK_CUR); + } else { + /* no structure info, must be pre Sep 2002 database, go back to end */ + fseek(gi->GeoIPDatabase, -3l, SEEK_END); + } + + for (i = 0; i < DATABASE_INFO_MAX_SIZE; i++) { + fread(buf, 1, 3, gi->GeoIPDatabase); + if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0) { + retval = malloc(sizeof(char) * (i+1)); + if (retval == NULL) { + return NULL; + } + fread(retval, 1, i, gi->GeoIPDatabase); + retval[i] = '\0'; + return retval; + } + fseek(gi->GeoIPDatabase, -4l, SEEK_CUR); + } + return NULL; +} + +/* GeoIP Region Edition functions */ + +void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *region) { + unsigned int seek_region; + + /* This also writes in the terminating NULs (if you decide to + * keep them) and clear any fields that are not set. */ + memset(region, 0, sizeof(GeoIPRegion)); + + seek_region = _GeoIP_seek_record(gi, ntohl(inetaddr)); + + if (gi->databaseType == GEOIP_REGION_EDITION_REV0) { + /* Region Edition, pre June 2003 */ + seek_region -= STATE_BEGIN_REV0; + if (seek_region >= 1000) { + region->country_code[0] = 'U'; + region->country_code[1] = 'S'; + region->region[0] = (char) ((seek_region - 1000)/26 + 65); + region->region[1] = (char) ((seek_region - 1000)%26 + 65); + } else { + memcpy(region->country_code, GeoIP_country_code[seek_region], 2); + } + } else if (gi->databaseType == GEOIP_REGION_EDITION_REV1) { + /* Region Edition, post June 2003 */ + seek_region -= STATE_BEGIN_REV1; + if (seek_region < US_OFFSET) { + /* Unknown */ + /* we don't need to do anything here b/c we memset region to 0 */ + } else if (seek_region < CANADA_OFFSET) { + /* USA State */ + region->country_code[0] = 'U'; + region->country_code[1] = 'S'; + region->region[0] = (char) ((seek_region - US_OFFSET)/26 + 65); + region->region[1] = (char) ((seek_region - US_OFFSET)%26 + 65); + } else if (seek_region < WORLD_OFFSET) { + /* Canada Province */ + region->country_code[0] = 'C'; + region->country_code[1] = 'A'; + region->region[0] = (char) ((seek_region - CANADA_OFFSET)/26 + 65); + region->region[1] = (char) ((seek_region - CANADA_OFFSET)%26 + 65); + } else { + /* Not US or Canada */ + memcpy(region->country_code, GeoIP_country_code[(seek_region - WORLD_OFFSET) / FIPS_RANGE], 2); + } + } +} + +static +GeoIPRegion * _get_region(GeoIP* gi, unsigned long ipnum) { + GeoIPRegion * region; + + region = malloc(sizeof(GeoIPRegion)); + if (region) { + GeoIP_assign_region_by_inetaddr(gi, htonl(ipnum), region); + } + return region; +} + +GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr) { + unsigned long ipnum; + if (addr == NULL) { + return 0; + } + if (gi->databaseType != GEOIP_REGION_EDITION_REV0 && + gi->databaseType != GEOIP_REGION_EDITION_REV1) { + printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]); + return 0; + } + ipnum = _GeoIP_addr_to_num(addr); + return _get_region(gi, ipnum); +} + +GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *name) { + unsigned long ipnum; + if (name == NULL) { + return 0; + } + if (gi->databaseType != GEOIP_REGION_EDITION_REV0 && + gi->databaseType != GEOIP_REGION_EDITION_REV1) { + printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]); + return 0; + } + if (!(ipnum = _GeoIP_lookupaddress(name))) + return 0; + return _get_region(gi, ipnum); +} + +GeoIPRegion * GeoIP_region_by_ipnum (GeoIP* gi, unsigned long ipnum) { + if (gi->databaseType != GEOIP_REGION_EDITION_REV0 && + gi->databaseType != GEOIP_REGION_EDITION_REV1) { + printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]); + return 0; + } + return _get_region(gi, ipnum); +} + +void GeoIPRegion_delete (GeoIPRegion *gir) { + free(gir); +} + +/* GeoIP Organization, ISP and AS Number Edition private method */ +static +char *_get_name (GeoIP* gi, unsigned long ipnum) { + int seek_org; + char buf[MAX_ORG_RECORD_LENGTH]; + char * org_buf, * buf_pointer; + int record_pointer; + size_t len; + + if (gi->databaseType != GEOIP_ORG_EDITION && + gi->databaseType != GEOIP_ISP_EDITION && + gi->databaseType != GEOIP_ASNUM_EDITION) { + printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_ORG_EDITION]); + return 0; + } + + seek_org = _GeoIP_seek_record(gi, ipnum); + if (seek_org == gi->databaseSegments[0]) + return NULL; + + record_pointer = seek_org + (2 * gi->record_length - 1) * gi->databaseSegments[0]; + + if (gi->cache == NULL) { + fseek(gi->GeoIPDatabase, record_pointer, SEEK_SET); + fread(buf, sizeof(char), MAX_ORG_RECORD_LENGTH, gi->GeoIPDatabase); + len = sizeof(char) * (strlen(buf)+1); + org_buf = malloc(len); + strncpy(org_buf, buf, len); + } else { + buf_pointer = gi->cache + (long)record_pointer; + len = sizeof(char) * (strlen(buf_pointer)+1); + org_buf = malloc(len); + strncpy(org_buf, buf_pointer, len); + } + return org_buf; +} + +char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum) { + return _get_name(gi,ipnum); +} + +char *GeoIP_name_by_addr (GeoIP* gi, const char *addr) { + unsigned long ipnum; + if (addr == NULL) { + return 0; + } + ipnum = _GeoIP_addr_to_num(addr); + return _get_name(gi, ipnum); +} + +char *GeoIP_name_by_name (GeoIP* gi, const char *name) { + unsigned long ipnum; + if (name == NULL) { + return 0; + } + if (!(ipnum = _GeoIP_lookupaddress(name))) + return 0; + return _get_name(gi, ipnum); +} + +char *GeoIP_org_by_ipnum (GeoIP* gi, unsigned long ipnum) { + return GeoIP_name_by_ipnum(gi, ipnum); +} + +char *GeoIP_org_by_addr (GeoIP* gi, const char *addr) { + return GeoIP_name_by_addr(gi, addr); +} + +char *GeoIP_org_by_name (GeoIP* gi, const char *name) { + return GeoIP_name_by_name(gi, name); +} + +unsigned char GeoIP_database_edition (GeoIP* gi) { + return gi->databaseType; +} + +int GeoIP_charset( GeoIP* gi){ + return gi->charset; +} + +int GeoIP_set_charset( GeoIP* gi, int charset ){ + int old_charset = gi->charset; + gi->charset = charset; + return old_charset; +} + +int GeoIP_last_netmask (GeoIP* gi) { + return gi->netmask; +} + diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index f208bfed1..4e7520fc0 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -141,6 +141,9 @@ namespace libtorrent #ifndef NDEBUG piece_failed = false; #endif +#ifndef TORRENT_DISABLE_GEO_IP + m_inet_as_name = m_ses.as_name_for_ip(m_remote.address()); +#endif std::fill(m_peer_id.begin(), m_peer_id.end(), 0); } @@ -237,6 +240,9 @@ namespace libtorrent if (m_remote.address().is_v4()) m_socket->set_option(type_of_service(ses.settings().peer_tos), ec); +#ifndef TORRENT_DISABLE_GEO_IP + m_inet_as_name = m_ses.as_name_for_ip(m_remote.address()); +#endif #ifndef NDEBUG piece_failed = false; #endif @@ -2218,6 +2224,9 @@ namespace libtorrent p.pending_disk_bytes = m_outstanding_writing_bytes; p.send_quota = m_bandwidth_limit[upload_channel].quota_left(); p.receive_quota = m_bandwidth_limit[download_channel].quota_left(); +#ifndef TORRENT_DISABLE_GEO_IP + p.inet_as_name = m_inet_as_name; +#endif #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES p.country[0] = m_country[0]; @@ -2270,12 +2279,15 @@ namespace libtorrent p.flags |= is_seed() ? peer_info::seed : 0; if (peer_info_struct()) { - p.source = peer_info_struct()->source; - p.failcount = peer_info_struct()->failcount; - p.num_hashfails = peer_info_struct()->hashfails; - p.flags |= peer_info_struct()->on_parole ? peer_info::on_parole : 0; - p.flags |= peer_info_struct()->optimistically_unchoked ? peer_info::optimistic_unchoke : 0; - p.remote_dl_rate = m_remote_dl_rate; + policy::peer* pi = peer_info_struct(); + p.source = pi->source; + p.failcount = pi->failcount; + p.num_hashfails = pi->hashfails; + p.flags |= pi->on_parole ? peer_info::on_parole : 0; + p.flags |= pi->optimistically_unchoked ? peer_info::optimistic_unchoke : 0; +#ifndef TORRENT_DISABLE_GEO_IP + p.inet_as = pi->inet_as->first; +#endif } else { @@ -2283,8 +2295,12 @@ namespace libtorrent p.failcount = 0; p.num_hashfails = 0; p.remote_dl_rate = 0; +#ifndef TORRENT_DISABLE_GEO_IP + p.inet_as = 0xffff; +#endif } + p.remote_dl_rate = m_remote_dl_rate; p.send_buffer_size = m_send_buffer.capacity(); p.used_send_buffer = m_send_buffer.size(); p.write_state = m_channel_state[upload_channel]; @@ -2347,6 +2363,14 @@ namespace libtorrent if (m_statistics.download_payload_rate() > m_download_rate_peak) { m_download_rate_peak = m_statistics.download_payload_rate(); +#ifndef TORRENT_DISABLE_GEO_IP + if (peer_info_struct()) + { + std::pair* as_stats = peer_info_struct()->inet_as; + if (as_stats && as_stats->second < m_download_rate_peak) + as_stats->second = m_download_rate_peak; + } +#endif } if (!t->valid_metadata()) return; diff --git a/src/policy.cpp b/src/policy.cpp index 23c2318bd..b15412fab 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -458,11 +458,13 @@ namespace libtorrent iterator candidate = m_peers.end(); int min_reconnect_time = m_torrent->settings().min_reconnect_time; - int min_cidr_distance = (std::numeric_limits::max)(); bool finished = m_torrent->is_finished(); + + int min_cidr_distance = (std::numeric_limits::max)(); address external_ip = m_torrent->session().external_address(); - if (external_ip == address()) + // don't bias any particular peers when seeding + if (finished || external_ip == address()) { // set external_ip to a random value, to // radomize which peers we prefer @@ -471,6 +473,11 @@ namespace libtorrent external_ip = address_v4(bytes); } +#ifndef TORRENT_DISABLE_GEO_IP + int max_inet_as_rate = -1; + bool has_db = m_torrent->session().has_asnum_db(); +#endif + int connect_candidates = 0; int seeds = 0; for (iterator i = m_peers.begin(); i != m_peers.end(); ++i) @@ -498,10 +505,26 @@ namespace libtorrent continue; if (i->second.connected > min_connect_time) continue; - int distance = cidr_distance(external_ip, i->second.ip.address()); - if (distance > min_cidr_distance) continue; - min_cidr_distance = distance; +#ifndef TORRENT_DISABLE_GEO_IP + if (!finished && has_db) + { + // don't bias fast peers when seeding + std::pair* inet_as = i->second.inet_as; + int peak_rate = inet_as ? inet_as->second : 0; + if (peak_rate <= max_inet_as_rate) continue; + max_inet_as_rate = peak_rate; + } + + if (max_inet_as_rate <= 0) +#endif + { + int distance = cidr_distance(external_ip, i->second.ip.address()); + if (distance > min_cidr_distance) continue; + + min_cidr_distance = distance; + } + min_connect_time = i->second.connected; candidate = i; } @@ -684,8 +707,10 @@ namespace libtorrent asio::error_code ec; TORRENT_ASSERT(c.remote() == c.get_socket()->remote_endpoint(ec) || ec); + aux::session_impl& ses = m_torrent->session(); + if (m_torrent->num_peers() >= m_torrent->max_connections() - && m_torrent->session().num_connections() >= m_torrent->session().max_connections() + && ses.num_connections() >= ses.max_connections() && c.remote().address() != m_torrent->current_tracker().address()) { c.disconnect("too many connections, refusing incoming connection"); @@ -753,6 +778,13 @@ namespace libtorrent peer p(c.remote(), peer::not_connectable, 0); i = m_peers.insert(std::make_pair(c.remote().address(), p)); +#ifndef TORRENT_DISABLE_GEO_IP + int as = ses.as_for_ip(c.remote().address()); +#ifndef NDEBUG + i->second.inet_as_num = as; +#endif + i->second.inet_as = ses.lookup_as(as); +#endif } c.set_peer_info(&i->second); @@ -879,6 +911,14 @@ namespace libtorrent i->second.seed = true; ++m_num_seeds; } + +#ifndef TORRENT_DISABLE_GEO_IP + int as = ses.as_for_ip(remote.address()); +#ifndef NDEBUG + i->second.inet_as_num = as; +#endif + i->second.inet_as = ses.lookup_as(as); +#endif } else { @@ -985,6 +1025,8 @@ namespace libtorrent , boost::bind(std::equal_to(), bind(&peer::connection , bind(&iterator::value_type::second, _1)), &c)) != m_peers.end()); + aux::session_impl& ses = m_torrent->session(); + // if the peer is choked and we have upload slots left, // then unchoke it. Another condition that has to be met // is that the torrent doesn't keep track of the individual @@ -996,23 +1038,23 @@ namespace libtorrent // In that case we don't care if people are leeching, they // can't pay for their downloads anyway. if (c.is_choked() - && m_torrent->session().num_uploads() < m_torrent->session().max_uploads() + && ses.num_uploads() < ses.max_uploads() && (m_torrent->ratio() == 0 || c.share_diff() >= -free_upload_amount || m_torrent->is_finished())) { - m_torrent->session().unchoke_peer(c); + ses.unchoke_peer(c); } #if defined TORRENT_VERBOSE_LOGGING else if (c.is_choked()) { std::string reason; - if (m_torrent->session().num_uploads() >= m_torrent->session().max_uploads()) + if (ses.num_uploads() >= ses.max_uploads()) { reason = "the number of uploads (" - + boost::lexical_cast(m_torrent->session().num_uploads()) + + boost::lexical_cast(ses.num_uploads()) + ") is more than or equal to the limit (" - + boost::lexical_cast(m_torrent->session().max_uploads()) + + boost::lexical_cast(ses.max_uploads()) + ")"; } else @@ -1231,6 +1273,9 @@ namespace libtorrent i != m_peers.end(); ++i) { peer const& p = i->second; +#ifndef TORRENT_DISABLE_GEO_IP + TORRENT_ASSERT(p.inet_as == 0 || p.inet_as->first == p.inet_as_num); +#endif if (!m_torrent->settings().allow_multiple_connections_per_ip) { TORRENT_ASSERT(m_peers.count(p.ip.address()) == 1); @@ -1316,11 +1361,14 @@ namespace libtorrent policy::peer::peer(const tcp::endpoint& ip_, peer::connection_type t, int src) : ip(ip_) - , type(t) +#ifndef TORRENT_DISABLE_GEO_IP + , inet_as(0) +#endif , failcount(0) , trust_points(0) , source(src) , hashfails(0) + , type(t) , fast_reconnects(0) #ifndef TORRENT_DISABLE_ENCRYPTION , pe_support(true) diff --git a/src/session.cpp b/src/session.cpp index 87d22ae94..72db59f34 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -175,6 +175,23 @@ namespace libtorrent m_impl->add_extension(ext); } +#ifndef TORRENT_DISABLE_GEO_IP + bool session::load_asnum_db(char const* file) + { + return m_impl->load_asnum_db(file); + } +#endif + + void session::load_state(entry const& ses_state) + { + m_impl->load_state(ses_state); + } + + entry session::state() const + { + return m_impl->state(); + } + void session::set_ip_filter(ip_filter const& f) { m_impl->set_ip_filter(f); diff --git a/src/session_impl.cpp b/src/session_impl.cpp index e1d5719da..3235f1b9d 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -169,6 +169,9 @@ namespace aux { , m_next_connect_torrent(0) #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING , m_logpath(logpath) +#endif +#ifndef TORRENT_DISABLE_GEO_IP + , m_geoip_db(0) #endif { #ifdef WIN32 @@ -243,6 +246,99 @@ namespace aux { m_thread.reset(new boost::thread(boost::ref(*this))); } +#ifndef TORRENT_DISABLE_GEO_IP + namespace + { + struct free_ptr + { + void* ptr_; + free_ptr(void* p): ptr_(p) {} + ~free_ptr() { free(ptr_); } + }; + } + + int session_impl::as_for_ip(address const& a) + { + if (!a.is_v4() || m_geoip_db == 0) return 0; + char* name = GeoIP_name_by_ipnum(m_geoip_db, a.to_v4().to_ulong()); + if (name == 0) return 0; + free_ptr p(name); + // GeoIP returns the name as AS??? where ? is the AS-number + return atoi(name + 2); + } + + std::string session_impl::as_name_for_ip(address const& a) + { + if (!a.is_v4() || m_geoip_db == 0) return std::string(); + char* name = GeoIP_name_by_ipnum(m_geoip_db, a.to_v4().to_ulong()); + if (name == 0) return std::string(); + free_ptr p(name); + char* tmp = std::strchr(name, ' '); + if (tmp == 0) return std::string(); + return tmp + 1; + } + + std::pair* session_impl::lookup_as(int as) + { + std::map::iterator i = m_as_peak.lower_bound(as); + + if (i == m_as_peak.end() || i->first != as) + { + // we don't have any data for this AS, insert a new entry + i = m_as_peak.insert(i, std::pair(as, 0)); + } + return &(*i); + } + + bool session_impl::load_asnum_db(char const* file) + { + mutex_t::scoped_lock l(m_mutex); + if (m_geoip_db) GeoIP_delete(m_geoip_db); + m_geoip_db = GeoIP_open(file, GEOIP_STANDARD); + return m_geoip_db; + } + +#endif + + void session_impl::load_state(entry const& ses_state) + { + if (ses_state.type() != entry::dictionary_t) return; + mutex_t::scoped_lock l(m_mutex); +#ifndef TORRENT_DISABLE_GEO_IP + entry const* as_map = ses_state.find_key("AS map"); + if (as_map && as_map->type() == entry::dictionary_t) + { + entry::dictionary_type const& as_peak = as_map->dict(); + for (entry::dictionary_type::const_iterator i = as_peak.begin() + , end(as_peak.end()); i != end; ++i) + { + int as_num = atoi(i->first.c_str()); + if (i->second.type() != entry::int_t || i->second.integer() == 0) continue; + int& peak = m_as_peak[as_num]; + if (peak < i->second.integer()) peak = i->second.integer(); + } + } +#endif + } + + entry session_impl::state() const + { + mutex_t::scoped_lock l(m_mutex); + entry ret; +#ifndef TORRENT_DISABLE_GEO_IP + entry::dictionary_type& as_map = ret["AS map"].dict(); + char buf[10]; + for (std::map::const_iterator i = m_as_peak.begin() + , end(m_as_peak.end()); i != end; ++i) + { + if (i->second == 0) continue; + sprintf(buf, "%05d", i->first); + as_map[buf] = i->second; + } +#endif + return ret; + } + #ifndef TORRENT_DISABLE_EXTENSIONS void session_impl::add_extension( boost::function(torrent*, void*)> ext) @@ -1825,6 +1921,9 @@ namespace aux { #endif abort(); +#ifndef TORRENT_DISABLE_GEO_IP + if (m_geoip_db) GeoIP_delete(m_geoip_db); +#endif #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) (*m_logger) << time_now_string() << " waiting for main thread\n"; #endif