summaryrefslogtreecommitdiffstats
path: root/libktorrent/torrent/authenticatebase.cpp
diff options
context:
space:
mode:
authortpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-01-20 02:37:40 +0000
committertpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-01-20 02:37:40 +0000
commit9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0 (patch)
treed088b5210e77d9fa91d954d8550e00e372b47378 /libktorrent/torrent/authenticatebase.cpp
downloadktorrent-9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0.tar.gz
ktorrent-9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0.zip
Updated to final KDE3 ktorrent release (2.2.6)
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/ktorrent@1077377 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'libktorrent/torrent/authenticatebase.cpp')
-rw-r--r--libktorrent/torrent/authenticatebase.cpp159
1 files changed, 159 insertions, 0 deletions
diff --git a/libktorrent/torrent/authenticatebase.cpp b/libktorrent/torrent/authenticatebase.cpp
new file mode 100644
index 0000000..9ee2ad7
--- /dev/null
+++ b/libktorrent/torrent/authenticatebase.cpp
@@ -0,0 +1,159 @@
+/***************************************************************************
+ * Copyright (C) 2005 by Joris Guisson *
+ * joris.guisson@gmail.com *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program 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 General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#include <mse/streamsocket.h>
+#include <util/sha1hash.h>
+#include <util/log.h>
+#include <kademlia/dhtbase.h>
+#include "globals.h"
+#include "peerid.h"
+#include "authenticatebase.h"
+
+namespace bt
+{
+
+
+
+ AuthenticateBase::AuthenticateBase(mse::StreamSocket* s) : sock(s),finished(false),local(false)
+ {
+ connect(&timer,SIGNAL(timeout()),this,SLOT(onTimeout()));
+ timer.start(20000,true);
+ memset(handshake,0x00,68);
+ bytes_of_handshake_recieved = 0;
+ ext_support = 0;
+ poll_index = -1;
+ }
+
+
+ AuthenticateBase::~AuthenticateBase()
+ {
+ if (sock)
+ sock->deleteLater();
+ }
+
+ void AuthenticateBase::sendHandshake(const SHA1Hash & info_hash,const PeerID & our_peer_id)
+ {
+ // Out() << "AuthenticateBase::sendHandshake" << endl;
+ if (!sock) return;
+
+ Uint8 hs[68];
+ makeHandshake(hs,info_hash,our_peer_id);
+ sock->sendData(hs,68);
+ }
+
+ void AuthenticateBase::makeHandshake(Uint8* hs,const SHA1Hash & info_hash,const PeerID & our_peer_id)
+ {
+ const char* pstr = "BitTorrent protocol";
+ hs[0] = 19;
+ memcpy(hs+1,pstr,19);
+ memset(hs+20,0x00,8);
+ if (Globals::instance().getDHT().isRunning())
+ hs[27] |= 0x01; // DHT support
+
+ hs[25] |= 0x10; // extension protocol
+ hs[27] |= 0x04; // fast extensions
+ memcpy(hs+28,info_hash.getData(),20);
+ memcpy(hs+48,our_peer_id.data(),20);
+ }
+
+ void AuthenticateBase::onReadyRead()
+ {
+ Uint32 ba = sock->bytesAvailable();
+ // Out() << "AuthenticateBase::onReadyRead " << ba << endl;
+ if (ba == 0)
+ {
+ onFinish(false);
+ return;
+ }
+
+ if (!sock || finished || ba < 48)
+ return;
+
+ // first see if we already have some bytes from the handshake
+ if (bytes_of_handshake_recieved == 0)
+ {
+ if (ba < 68)
+ {
+ // read partial
+ sock->readData(handshake,ba);
+ bytes_of_handshake_recieved += ba;
+ if (ba >= 27 && handshake[27] & 0x01)
+ ext_support |= bt::DHT_SUPPORT;
+ // tell subclasses of a partial handshake
+ handshakeRecieved(false);
+ return;
+ }
+ else
+ {
+ // read full handshake
+ sock->readData(handshake,68);
+ }
+ }
+ else
+ {
+ // read remaining part
+ Uint32 to_read = 68 - bytes_of_handshake_recieved;
+ sock->readData(handshake + bytes_of_handshake_recieved,to_read);
+ }
+
+ if (handshake[0] != 19)
+ {
+ onFinish(false);
+ return;
+ }
+
+ const char* pstr = "BitTorrent protocol";
+ if (memcmp(pstr,handshake+1,19) != 0)
+ {
+ onFinish(false);
+ return;
+ }
+
+ if (Globals::instance().getDHT().isRunning() && (handshake[27] & 0x01))
+ ext_support |= bt::DHT_SUPPORT;
+
+ if (handshake[27] & 0x04)
+ ext_support |= bt::FAST_EXT_SUPPORT;
+
+ if (handshake[25] & 0x10)
+ ext_support |= bt::EXT_PROT_SUPPORT;
+
+ handshakeRecieved(true);
+ }
+
+ void AuthenticateBase::onError(int)
+ {
+ if (finished)
+ return;
+ onFinish(false);
+ }
+
+ void AuthenticateBase::onTimeout()
+ {
+ if (finished)
+ return;
+
+ Out(SYS_CON|LOG_DEBUG) << "Timeout occurred" << endl;
+ onFinish(false);
+ }
+
+ void AuthenticateBase::onReadyWrite()
+ {}
+}
+#include "authenticatebase.moc"