mirror of https://github.com/aria2/aria2
Throw exception if more than BtHandshakeMessage::MESSAGE_LENGTH is
buffered when receiving handshake. Detect preset buffer without setting prevPeek_ = true.pull/1/head
parent
e5c27034f3
commit
320ca4c5a1
|
@ -177,15 +177,20 @@ bool PeerConnection::receiveMessage(unsigned char* data, size_t& dataLength) {
|
||||||
|
|
||||||
bool PeerConnection::receiveHandshake(unsigned char* data, size_t& dataLength,
|
bool PeerConnection::receiveHandshake(unsigned char* data, size_t& dataLength,
|
||||||
bool peek) {
|
bool peek) {
|
||||||
assert(BtHandshakeMessage::MESSAGE_LENGTH >= resbufLength_);
|
if(BtHandshakeMessage::MESSAGE_LENGTH < resbufLength_) {
|
||||||
|
throw DL_ABORT_EX
|
||||||
|
("More than BtHandshakeMessage::MESSAGE_LENGTH bytes are buffered.");
|
||||||
|
}
|
||||||
bool retval = true;
|
bool retval = true;
|
||||||
if(prevPeek_ && resbufLength_) {
|
if(((!prevPeek_ && peek) || (prevPeek_ && !peek)) && resbufLength_) {
|
||||||
// We have data in previous peek.
|
// We have data in previous peek.
|
||||||
// There is a chance that socket is readable because of EOF, for example,
|
// There is a chance that socket is readable because of EOF, for example,
|
||||||
// official bttrack shutdowns socket after sending first 48 bytes of
|
// official bttrack shutdowns socket after sending first 48 bytes of
|
||||||
// handshake in its NAT checking.
|
// handshake in its NAT checking.
|
||||||
// So if there are data in resbuf_, return it without checking socket
|
// So if there are data in resbuf_, return it without checking socket
|
||||||
// status.
|
// status.
|
||||||
|
//
|
||||||
|
// (!prevPeek_ && peek) effectively returns preset buffer.
|
||||||
prevPeek_ = false;
|
prevPeek_ = false;
|
||||||
retval = BtHandshakeMessage::MESSAGE_LENGTH <= resbufLength_;
|
retval = BtHandshakeMessage::MESSAGE_LENGTH <= resbufLength_;
|
||||||
} else {
|
} else {
|
||||||
|
@ -246,9 +251,6 @@ void PeerConnection::presetBuffer(const unsigned char* data, size_t length)
|
||||||
size_t nwrite = std::min((size_t)MAX_PAYLOAD_LEN, length);
|
size_t nwrite = std::min((size_t)MAX_PAYLOAD_LEN, length);
|
||||||
memcpy(resbuf_, data, nwrite);
|
memcpy(resbuf_, data, nwrite);
|
||||||
resbufLength_ = length;
|
resbufLength_ = length;
|
||||||
if(resbufLength_ > 0) {
|
|
||||||
prevPeek_ = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PeerConnection::sendBufferIsEmpty() const
|
bool PeerConnection::sendBufferIsEmpty() const
|
||||||
|
|
Loading…
Reference in New Issue