kget
authenticatebase.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "authenticatebase.h"
00021 #include <mse/streamsocket.h>
00022 #include <util/sha1hash.h>
00023 #include <util/log.h>
00024 #include <dht/dhtbase.h>
00025 #include <torrent/globals.h>
00026 #include <peer/peerid.h>
00027
00028 namespace bt
00029 {
00030
00031
00032
00033 AuthenticateBase::AuthenticateBase(mse::StreamSocket* s) : sock(s),finished(false),local(false)
00034 {
00035 connect(&timer,SIGNAL(timeout()),this,SLOT(onTimeout()));
00036 timer.setSingleShot(true);
00037 timer.start(20000);
00038 memset(handshake,0x00,68);
00039 bytes_of_handshake_received = 0;
00040 ext_support = 0;
00041 poll_index = -1;
00042 }
00043
00044
00045 AuthenticateBase::~AuthenticateBase()
00046 {
00047 if (sock)
00048 sock->deleteLater();
00049 }
00050
00051 void AuthenticateBase::sendHandshake(const SHA1Hash & info_hash,const PeerID & our_peer_id)
00052 {
00053
00054 if (!sock) return;
00055
00056 Uint8 hs[68];
00057 makeHandshake(hs,info_hash,our_peer_id);
00058 sock->sendData(hs,68);
00059 }
00060
00061 void AuthenticateBase::makeHandshake(Uint8* hs,const SHA1Hash & info_hash,const PeerID & our_peer_id)
00062 {
00063 const char* pstr = "BitTorrent protocol";
00064 hs[0] = 19;
00065 memcpy(hs+1,pstr,19);
00066 memset(hs+20,0x00,8);
00067 if (Globals::instance().getDHT().isRunning())
00068 hs[27] |= 0x01;
00069 hs[25] |= 0x10;
00070 hs[27] |= 0x04;
00071 memcpy(hs+28,info_hash.getData(),20);
00072 memcpy(hs+48,our_peer_id.data(),20);
00073 }
00074
00075 void AuthenticateBase::onReadyRead()
00076 {
00077 Uint32 ba = sock->bytesAvailable();
00078
00079 if (ba == 0)
00080 {
00081 onFinish(false);
00082 return;
00083 }
00084
00085 if (!sock || finished || ba < 48)
00086 return;
00087
00088
00089 if (bytes_of_handshake_received == 0)
00090 {
00091 if (ba < 68)
00092 {
00093
00094 sock->readData(handshake,ba);
00095 bytes_of_handshake_received += ba;
00096 if (ba >= 27 && handshake[27] & 0x01)
00097 ext_support |= bt::DHT_SUPPORT;
00098
00099 handshakeReceived(false);
00100 return;
00101 }
00102 else
00103 {
00104
00105 sock->readData(handshake,68);
00106 }
00107 }
00108 else
00109 {
00110
00111 Uint32 to_read = 68 - bytes_of_handshake_received;
00112 sock->readData(handshake + bytes_of_handshake_received,to_read);
00113 }
00114
00115 if (handshake[0] != 19)
00116 {
00117 onFinish(false);
00118 return;
00119 }
00120
00121 const char* pstr = "BitTorrent protocol";
00122 if (memcmp(pstr,handshake+1,19) != 0)
00123 {
00124 onFinish(false);
00125 return;
00126 }
00127
00128 if (Globals::instance().getDHT().isRunning() && (handshake[27] & 0x01))
00129 ext_support |= bt::DHT_SUPPORT;
00130
00131 if (handshake[27] & 0x04)
00132 ext_support |= bt::FAST_EXT_SUPPORT;
00133
00134 if (handshake[25] & 0x10)
00135 ext_support |= bt::EXT_PROT_SUPPORT;
00136
00137 handshakeReceived(true);
00138 }
00139
00140 void AuthenticateBase::onError(int)
00141 {
00142 if (finished)
00143 return;
00144 onFinish(false);
00145 }
00146
00147 void AuthenticateBase::onTimeout()
00148 {
00149 if (finished)
00150 return;
00151
00152 Out(SYS_CON|LOG_DEBUG) << "Timeout occurred" << endl;
00153 onFinish(false);
00154 }
00155
00156 void AuthenticateBase::onReadyWrite()
00157 {}
00158 }
00159 #include "authenticatebase.moc"