• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdenetwork
  • Sitemap
  • Contact Us
 

kget

authenticatebase.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Joris Guisson                                   *
00003  *   joris.guisson@gmail.com                                               *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
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     //  Out() << "AuthenticateBase::sendHandshake" << endl;
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; // DHT support
00069         hs[25] |= 0x10; // extension protocol
00070         hs[27] |= 0x04; // fast extensions
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     //  Out() << "AuthenticateBase::onReadyRead " << ba << endl;
00079         if (ba == 0)
00080         {
00081             onFinish(false);
00082             return;
00083         }
00084         
00085         if (!sock || finished || ba < 48)
00086             return;
00087         
00088         // first see if we already have some bytes from the handshake
00089         if (bytes_of_handshake_received == 0)
00090         {
00091             if (ba < 68)
00092             {
00093                 // read partial
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                 // tell subclasses of a partial handshake
00099                 handshakeReceived(false);
00100                 return;
00101             }
00102             else
00103             {
00104                 // read full handshake
00105                 sock->readData(handshake,68);
00106             }
00107         }
00108         else
00109         {
00110             // read remaining part
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"

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdenetwork

Skip menu "kdenetwork"
  • kget
  • kopete
  •   kopete
  •   libkopete
  •       libpapillon
  • krfb
Generated for kdenetwork by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal