summaryrefslogtreecommitdiffstats
path: root/libktorrent/torrent/authenticatebase.cpp
blob: 9ee2ad77b723bbf23b044f7794c6a93630dafff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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"