2014-11-23 07:23:49 +01:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2014-2018, Arvid Norberg, Steven Siloti
|
2014-11-23 07:23:49 +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 TORRENT_RECEIVE_BUFFER_HPP_INCLUDED
|
|
|
|
#define TORRENT_RECEIVE_BUFFER_HPP_INCLUDED
|
|
|
|
|
2017-08-28 11:37:53 +02:00
|
|
|
#include "libtorrent/buffer.hpp"
|
|
|
|
#include "libtorrent/disk_buffer_holder.hpp"
|
|
|
|
#include "libtorrent/sliding_average.hpp"
|
2018-10-02 14:42:40 +02:00
|
|
|
#include "libtorrent/aux_/numeric_cast.hpp"
|
2014-11-23 07:23:49 +01:00
|
|
|
|
2016-08-29 14:31:23 +02:00
|
|
|
#include <climits>
|
|
|
|
|
2014-11-23 07:23:49 +01:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2016-03-04 05:53:24 +01:00
|
|
|
struct TORRENT_EXTRA_EXPORT receive_buffer
|
2014-11-23 07:23:49 +01:00
|
|
|
{
|
|
|
|
friend struct crypto_receive_buffer;
|
|
|
|
|
|
|
|
int packet_size() const { return m_packet_size; }
|
|
|
|
int packet_bytes_remaining() const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_recv_start == 0);
|
2016-03-04 05:53:24 +01:00
|
|
|
TORRENT_ASSERT(m_packet_size > 0);
|
2014-11-23 07:23:49 +01:00
|
|
|
return m_packet_size - m_recv_pos;
|
|
|
|
}
|
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
int max_receive() const;
|
2014-11-23 07:23:49 +01:00
|
|
|
|
|
|
|
bool packet_finished() const { return m_packet_size <= m_recv_pos; }
|
|
|
|
int pos() const { return m_recv_pos; }
|
2018-10-02 14:42:40 +02:00
|
|
|
int capacity() const { return aux::numeric_cast<int>(m_recv_buffer.size()); }
|
|
|
|
int watermark() const { return aux::numeric_cast<int>(m_watermark.mean()); }
|
2014-11-23 07:23:49 +01:00
|
|
|
|
2016-08-21 19:37:11 +02:00
|
|
|
span<char> reserve(int size);
|
2016-06-27 02:41:03 +02:00
|
|
|
void grow(int limit);
|
2014-11-23 07:23:49 +01:00
|
|
|
|
2016-05-26 19:34:13 +02:00
|
|
|
// tell the buffer we just received more bytes at the end of it. This will
|
2016-03-04 05:53:24 +01:00
|
|
|
// advance the end cursor
|
2014-11-23 07:23:49 +01:00
|
|
|
void received(int bytes_transferred)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_packet_size > 0);
|
|
|
|
m_recv_end += bytes_transferred;
|
2016-06-15 14:04:53 +02:00
|
|
|
TORRENT_ASSERT(m_recv_pos <= int(m_recv_buffer.size()));
|
2014-11-23 07:23:49 +01:00
|
|
|
}
|
|
|
|
|
2016-03-04 05:53:24 +01:00
|
|
|
// tell the buffer we consumed some bytes of it. This will advance the read
|
|
|
|
// cursor
|
2014-11-23 07:23:49 +01:00
|
|
|
int advance_pos(int bytes);
|
2016-03-04 05:53:24 +01:00
|
|
|
|
|
|
|
// has the read cursor reached the end cursor?
|
2014-11-23 07:23:49 +01:00
|
|
|
bool pos_at_end() { return m_recv_pos == m_recv_end; }
|
|
|
|
|
|
|
|
// size = the packet size to remove from the receive buffer
|
|
|
|
// packet_size = the next packet size to receive in the buffer
|
|
|
|
// offset = the offset into the receive buffer where to remove `size` bytes
|
|
|
|
void cut(int size, int packet_size, int offset = 0);
|
|
|
|
|
2016-03-04 05:53:24 +01:00
|
|
|
// return the interval between the start of the buffer to the read cursor.
|
|
|
|
// This is the "current" packet.
|
2016-07-28 04:31:08 +02:00
|
|
|
span<char const> get() const;
|
2014-11-23 07:23:49 +01:00
|
|
|
|
2018-07-29 00:22:10 +02:00
|
|
|
#if !defined TORRENT_DISABLE_ENCRYPTION
|
2016-07-26 08:15:31 +02:00
|
|
|
// returns the buffer from the current packet start position to the last
|
|
|
|
// received byte (possibly part of another packet)
|
|
|
|
span<char> mutable_buffer();
|
2016-06-15 14:04:53 +02:00
|
|
|
|
2014-11-23 07:23:49 +01:00
|
|
|
// returns the last 'bytes' from the receive buffer
|
2016-07-28 04:31:08 +02:00
|
|
|
span<char> mutable_buffer(int bytes);
|
2014-11-23 07:23:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// the purpose of this function is to free up and cut off all messages
|
|
|
|
// in the receive buffer that have been parsed and processed.
|
2016-06-27 02:41:03 +02:00
|
|
|
void normalize(int force_shrink = 0);
|
2014-11-23 07:23:49 +01:00
|
|
|
bool normalized() const { return m_recv_start == 0; }
|
|
|
|
|
|
|
|
void reset(int packet_size);
|
|
|
|
|
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
void check_invariant() const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_recv_end >= m_recv_start);
|
2016-07-26 08:15:31 +02:00
|
|
|
TORRENT_ASSERT(m_recv_end <= int(m_recv_buffer.size()));
|
|
|
|
TORRENT_ASSERT(m_recv_start <= int(m_recv_buffer.size()));
|
|
|
|
TORRENT_ASSERT(m_recv_start + m_recv_pos <= int(m_recv_buffer.size()));
|
2014-11-23 07:23:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
2015-04-27 04:21:12 +02:00
|
|
|
// explicitly disallow assignment, to silence msvc warning
|
|
|
|
receive_buffer& operator=(receive_buffer const&);
|
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
// m_recv_buffer.data() (start of actual receive buffer)
|
2014-11-23 07:23:49 +01:00
|
|
|
// |
|
2016-07-26 08:15:31 +02:00
|
|
|
// | m_recv_start (start of current packet)
|
2014-11-23 07:23:49 +01:00
|
|
|
// | |
|
|
|
|
// | | m_recv_pos (number of bytes consumed
|
|
|
|
// | | | by upper layer, from logical receive buffer)
|
|
|
|
// | | |
|
|
|
|
// | x---------x
|
2016-06-27 02:41:03 +02:00
|
|
|
// | | | m_recv_buffer.size() (end of actual receive buffer)
|
2014-11-23 07:23:49 +01:00
|
|
|
// | | | |
|
|
|
|
// v v v v
|
|
|
|
// *------==========---------
|
|
|
|
// ^
|
|
|
|
// |
|
|
|
|
// |
|
|
|
|
// ------------------->x m_recv_end (end of received data,
|
|
|
|
// beyond this point is garbage)
|
|
|
|
// m_recv_buffer
|
|
|
|
|
|
|
|
// the start of the logical receive buffer
|
2016-06-27 02:41:03 +02:00
|
|
|
int m_recv_start = 0;
|
2014-11-23 07:23:49 +01:00
|
|
|
|
|
|
|
// the number of valid, received bytes in m_recv_buffer
|
2016-06-27 02:41:03 +02:00
|
|
|
int m_recv_end = 0;
|
2014-11-23 07:23:49 +01:00
|
|
|
|
|
|
|
// the byte offset in m_recv_buffer that we have
|
|
|
|
// are passing on to the upper layer. This is
|
|
|
|
// always <= m_recv_end
|
2016-06-27 02:41:03 +02:00
|
|
|
int m_recv_pos = 0;
|
2014-11-23 07:23:49 +01:00
|
|
|
|
|
|
|
// the size (in bytes) of the bittorrent message
|
|
|
|
// we're currently receiving
|
2016-06-27 02:41:03 +02:00
|
|
|
int m_packet_size = 0;
|
2014-11-23 07:23:49 +01:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
// keep track of how much of the receive buffer we use, if we're not using
|
2016-08-15 22:17:13 +02:00
|
|
|
// enough of it we shrink it
|
2018-11-01 23:05:30 +01:00
|
|
|
sliding_average<std::ptrdiff_t, 20> m_watermark;
|
2014-11-23 07:23:49 +01:00
|
|
|
|
|
|
|
buffer m_recv_buffer;
|
|
|
|
};
|
|
|
|
|
2018-07-29 00:22:10 +02:00
|
|
|
#if !defined TORRENT_DISABLE_ENCRYPTION
|
2014-11-23 07:23:49 +01:00
|
|
|
// Wraps a receive_buffer to provide the ability to inject
|
|
|
|
// possibly authenticated crypto beneath the bittorrent protocol.
|
|
|
|
// When authenticated crypto is in use the wrapped receive_buffer
|
2016-07-24 00:54:39 +02:00
|
|
|
// holds the receive state of the crypto layer while this class
|
2014-11-23 07:23:49 +01:00
|
|
|
// tracks the state of the bittorrent protocol.
|
|
|
|
struct crypto_receive_buffer
|
|
|
|
{
|
2016-10-08 20:07:11 +02:00
|
|
|
explicit crypto_receive_buffer(receive_buffer& next)
|
2016-06-27 02:41:03 +02:00
|
|
|
: m_connection_buffer(next)
|
2014-11-23 07:23:49 +01:00
|
|
|
{}
|
|
|
|
|
2016-07-26 08:15:31 +02:00
|
|
|
span<char> mutable_buffer() { return m_connection_buffer.mutable_buffer(); }
|
2014-11-23 07:23:49 +01:00
|
|
|
|
|
|
|
bool packet_finished() const;
|
|
|
|
|
|
|
|
bool crypto_packet_finished() const
|
|
|
|
{
|
2017-11-12 13:24:27 +01:00
|
|
|
return m_recv_pos == (std::numeric_limits<int>::max)()
|
|
|
|
|| m_connection_buffer.packet_finished();
|
2014-11-23 07:23:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int packet_size() const;
|
|
|
|
|
|
|
|
int crypto_packet_size() const
|
|
|
|
{
|
2017-11-12 13:24:27 +01:00
|
|
|
TORRENT_ASSERT(m_recv_pos != (std::numeric_limits<int>::max)());
|
2014-11-23 07:23:49 +01:00
|
|
|
return m_connection_buffer.packet_size() - m_recv_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pos() const;
|
|
|
|
|
|
|
|
void cut(int size, int packet_size, int offset = 0);
|
|
|
|
|
|
|
|
void crypto_cut(int size, int packet_size)
|
|
|
|
{
|
2017-11-12 13:24:27 +01:00
|
|
|
TORRENT_ASSERT(m_recv_pos != (std::numeric_limits<int>::max)());
|
2014-11-23 07:23:49 +01:00
|
|
|
m_connection_buffer.cut(size, m_recv_pos + packet_size, m_recv_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(int packet_size);
|
|
|
|
void crypto_reset(int packet_size);
|
|
|
|
|
|
|
|
int advance_pos(int bytes);
|
|
|
|
|
2016-07-28 04:31:08 +02:00
|
|
|
span<char const> get() const;
|
2014-11-23 07:23:49 +01:00
|
|
|
|
2018-11-01 23:05:30 +01:00
|
|
|
span<char> mutable_buffer(int bytes);
|
2014-11-23 07:23:49 +01:00
|
|
|
|
|
|
|
private:
|
2015-04-27 04:21:12 +02:00
|
|
|
// explicitly disallow assignment, to silence msvc warning
|
|
|
|
crypto_receive_buffer& operator=(crypto_receive_buffer const&);
|
|
|
|
|
2017-11-12 13:24:27 +01:00
|
|
|
int m_recv_pos = (std::numeric_limits<int>::max)();
|
2016-06-27 02:41:03 +02:00
|
|
|
int m_packet_size = 0;
|
2014-11-23 07:23:49 +01:00
|
|
|
receive_buffer& m_connection_buffer;
|
|
|
|
};
|
|
|
|
#endif // TORRENT_DISABLE_ENCRYPTION
|
|
|
|
|
|
|
|
} // namespace libtorrent
|
|
|
|
|
|
|
|
#endif // #ifndef TORRENT_RECEIVE_BUFFER_HPP_INCLUDED
|