2013-12-27 05:28:25 +01:00
|
|
|
/*
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
Copyright (c) 2013, Steven Siloti, Arvid Norberg
|
2013-12-27 05:28:25 +01:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LIBTORRENT_ITEM_HPP
|
|
|
|
#define LIBTORRENT_ITEM_HPP
|
|
|
|
|
|
|
|
#include <libtorrent/sha1_hash.hpp>
|
2015-03-12 06:20:12 +01:00
|
|
|
#include <libtorrent/bdecode.hpp>
|
2013-12-27 05:28:25 +01:00
|
|
|
#include <libtorrent/entry.hpp>
|
2016-07-24 00:57:04 +02:00
|
|
|
#include <libtorrent/span.hpp>
|
|
|
|
#include <libtorrent/kademlia/types.hpp>
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent { namespace dht {
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2014-03-03 00:35:35 +01:00
|
|
|
// calculate the target hash for an immutable item.
|
2016-09-13 14:18:47 +02:00
|
|
|
TORRENT_EXTRA_EXPORT sha1_hash item_target_id(span<char const> v);
|
2014-03-03 00:35:35 +01:00
|
|
|
|
|
|
|
// calculate the target hash for a mutable item.
|
2016-09-13 14:18:47 +02:00
|
|
|
TORRENT_EXTRA_EXPORT sha1_hash item_target_id(span<char const> salt
|
2016-07-24 00:57:04 +02:00
|
|
|
, public_key const& pk);
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2016-09-13 14:18:47 +02:00
|
|
|
TORRENT_EXTRA_EXPORT bool verify_mutable_item(
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> v
|
|
|
|
, span<char const> salt
|
|
|
|
, sequence_number seq
|
|
|
|
, public_key const& pk
|
|
|
|
, signature const& sig);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2014-02-24 01:31:13 +01:00
|
|
|
// TODO: since this is a public function, it should probably be moved
|
|
|
|
// out of this header and into one with other public functions.
|
|
|
|
|
|
|
|
// given a byte range ``v`` and an optional byte range ``salt``, a
|
|
|
|
// sequence number, public key ``pk`` (must be 32 bytes) and a secret key
|
|
|
|
// ``sk`` (must be 64 bytes), this function produces a signature which
|
|
|
|
// is written into a 64 byte buffer pointed to by ``sig``. The caller
|
|
|
|
// is responsible for allocating the destination buffer that's passed in
|
|
|
|
// as the ``sig`` argument. Typically it would be allocated on the stack.
|
2016-09-13 14:18:47 +02:00
|
|
|
TORRENT_EXPORT signature sign_mutable_item(
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> v
|
|
|
|
, span<char const> salt
|
|
|
|
, sequence_number seq
|
|
|
|
, public_key const& pk
|
2016-08-30 08:37:51 +02:00
|
|
|
, secret_key const& sk);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
|
|
|
class TORRENT_EXTRA_EXPORT item
|
|
|
|
{
|
|
|
|
public:
|
2018-11-14 12:30:27 +01:00
|
|
|
item() {}
|
2016-07-24 00:57:04 +02:00
|
|
|
item(public_key const& pk, span<char const> salt);
|
2016-10-08 20:07:11 +02:00
|
|
|
explicit item(entry v);
|
2016-07-24 00:57:04 +02:00
|
|
|
item(entry v
|
|
|
|
, span<char const> salt
|
|
|
|
, sequence_number seq
|
|
|
|
, public_key const& pk
|
|
|
|
, secret_key const& sk);
|
2016-10-08 20:07:11 +02:00
|
|
|
explicit item(bdecode_node const& v);
|
2016-07-24 00:57:04 +02:00
|
|
|
|
|
|
|
void assign(entry v);
|
|
|
|
void assign(entry v, span<char const> salt
|
|
|
|
, sequence_number seq
|
|
|
|
, public_key const& pk
|
|
|
|
, secret_key const& sk);
|
|
|
|
void assign(bdecode_node const& v);
|
|
|
|
bool assign(bdecode_node const& v, span<char const> salt
|
|
|
|
, sequence_number seq
|
|
|
|
, public_key const& pk
|
|
|
|
, signature const& sig);
|
|
|
|
void assign(entry v, span<char const> salt
|
|
|
|
, sequence_number seq
|
|
|
|
, public_key const& pk
|
|
|
|
, signature const& sig);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
|
|
|
void clear() { m_value = entry(); }
|
|
|
|
bool empty() const { return m_value.type() == entry::undefined_t; }
|
|
|
|
|
|
|
|
bool is_mutable() const { return m_mutable; }
|
|
|
|
|
|
|
|
entry const& value() const { return m_value; }
|
2016-07-24 00:57:04 +02:00
|
|
|
public_key const& pk() const
|
2014-03-03 00:35:35 +01:00
|
|
|
{ return m_pk; }
|
2016-07-24 00:57:04 +02:00
|
|
|
signature const& sig() const
|
2014-03-03 00:35:35 +01:00
|
|
|
{ return m_sig; }
|
2016-07-24 00:57:04 +02:00
|
|
|
sequence_number seq() const { return m_seq; }
|
2014-02-24 01:31:13 +01:00
|
|
|
std::string const& salt() const { return m_salt; }
|
2013-12-27 05:28:25 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
entry m_value;
|
2014-01-03 05:18:46 +01:00
|
|
|
std::string m_salt;
|
2016-07-24 00:57:04 +02:00
|
|
|
public_key m_pk;
|
|
|
|
signature m_sig;
|
2018-11-14 12:30:27 +01:00
|
|
|
sequence_number m_seq{0};
|
|
|
|
bool m_mutable = false;
|
2013-12-27 05:28:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|
|
|
|
#endif // LIBTORRENT_ITEM_HPP
|