-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-libtorrent.cpp
More file actions
executable file
·1115 lines (897 loc) · 34.7 KB
/
python-libtorrent.cpp
File metadata and controls
executable file
·1115 lines (897 loc) · 34.7 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2006 Alon Zakai ('Kripken') <kripkensteiner@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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Thank You: Some code portions were derived from BSD-licensed work by
* Arvid Norberg, and GPL-licensed work by Christophe Dumez
*/
#include <Python.h>
#include <boost/filesystem/exception.hpp>
#include "libtorrent/entry.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/identify_client.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/ip_filter.hpp"
#include <boost/filesystem/operations.hpp>
//#include <fstream>
//Needed just for debug purposes
#include <stdio.h>
//// Debug only
//#include <iostream.h>
using namespace libtorrent;
using boost::filesystem::path;
//-----------------
// START
//-----------------
#ifdef AMD64
#define pythonLong int
#else
#define pythonLong long
#endif
#define EVENT_NULL 0
#define EVENT_FINISHED 1
#define EVENT_PEER_ERROR 2
#define EVENT_INVALID_REQUEST 3
#define EVENT_FILE_ERROR 4
#define EVENT_HASH_FAILED_ERROR 5
#define EVENT_PEER_BAN_ERROR 6
#define EVENT_FASTRESUME_REJECTED_ERROR 8
#define EVENT_TRACKER 9
#define EVENT_OTHER 10
#define STATE_QUEUED 0
#define STATE_CHECKING 1
#define STATE_CONNECTING 2
#define STATE_DOWNLOADING_META 3
#define STATE_DOWNLOADING 4
#define STATE_FINISHED 5
#define STATE_SEEDING 6
#define STATE_ALLOCATING 7
#define DHT_ROUTER_PORT 6881
#define ERROR_INVALID_ENCODING -10
#define ERROR_FILESYSTEM -20
#define ERROR_DUPLICATE_TORRENT -30
#define ERROR_INVALID_TORRENT -40
typedef std::vector<torrent_handle> handles_t;
typedef std::vector<long> uniqueIDs_t;
typedef handles_t::iterator handles_t_iterator;
typedef uniqueIDs_t::iterator uniqueIDs_t_iterator;
typedef std::vector<bool> filterOut_t;
typedef std::vector<filterOut_t> filterOuts_t;
typedef filterOuts_t::iterator filterOuts_t_iterator;
typedef std::vector<std::string> torrentNames_t;
typedef torrentNames_t::iterator torrentNames_t_iterator;
// Global variables
session_settings *settings = NULL;
session *ses = NULL;
handles_t *handles = NULL;
uniqueIDs_t *uniqueIDs = NULL;
filterOuts_t *filterOuts = NULL;
PyObject *constants = NULL;
long uniqueCounter = 0;
torrentNames_t *torrentNames = NULL;
ip_filter *theFilter = NULL;
// Internal functions
bool empty_name_check(const std::string & name)
{
return 1;
}
long handle_exists(torrent_handle &handle)
{
for (unsigned long i = 0; i < handles->size(); i++)
if ((*handles)[i] == handle)
return 1;
return 0;
}
long get_torrent_index(torrent_handle &handle)
{
for (unsigned long i = 0; i < handles->size(); i++)
if ((*handles)[i] == handle)
{
// printf("Found: %li\r\n", i);
return i;
}
printf("Handle not found!\r\n");
assert(1 == 0);
return -1;
}
void print_uniqueIDs()
{
//#ifdef AMD64
// for (unsigned long i = 0; i < uniqueIDs->size(); i++)
// printf("--uniqueIDs[%ld] = %ld\r\n", i, (*uniqueIDs)[i]);
//#endif
}
long get_index_from_unique(long uniqueID)
{
assert(handles->size() == uniqueIDs->size());
//#ifdef AMD64
// printf("Request for uniqueID: %ld\r\n", uniqueID);
//#endif
print_uniqueIDs();
for (unsigned long i = 0; i < uniqueIDs->size(); i++)
if ((*uniqueIDs)[i] == uniqueID)
return i;
assert(1 == 0);
printf("Critical Error! No such uniqueID (%ld, %ld)\r\n", uniqueID, (long)uniqueIDs->size());
return -1;
}
long internal_add_torrent(std::string const& torrent
, float preferred_ratio
, bool compact_mode
, path const& save_path)
{
std::ifstream in(torrent.c_str(), std::ios_base::binary);
in.unsetf(std::ios_base::skipws);
entry e = bdecode(std::istream_iterator<char>(in), std::istream_iterator<char>());
torrent_info t(e);
// std::cout << t.name() << "\n";
entry resume_data;
try
{
std::stringstream s;
s << torrent << ".fastresume";
// printf("Loading fastresume from: %s\r\n", s.str().c_str());
boost::filesystem::ifstream resume_file(s.str(), std::ios_base::binary);
resume_file.unsetf(std::ios_base::skipws);
resume_data = bdecode(
std::istream_iterator<char>(resume_file)
, std::istream_iterator<char>());
}
catch (invalid_encoding&) {}
catch (boost::filesystem::filesystem_error&) {}
torrent_handle h = ses->add_torrent(t, save_path, resume_data
, compact_mode, 16 * 1024);
handles->push_back(h);
// h.set_max_connections(60); // Setting it only works once...
h.set_max_uploads(-1);
h.set_ratio(preferred_ratio);
uniqueIDs->push_back(uniqueCounter);
uniqueCounter++;
long numFiles = h.get_torrent_info().num_files();
filterOuts->push_back(filterOut_t(numFiles));
filterOut_t &newFilterOut = filterOuts->at(filterOuts->size()-1);
for (long i = 0; i < numFiles; i++)
newFilterOut.at(i) = 0;
assert(handles->size() == uniqueIDs->size());
assert(handles->size() == filterOuts->size());
torrentNames->push_back(torrent);
assert(handles->size() == torrentNames->size());
// printf("Added torrent, uniqueID: %ld\r\n", uniqueCounter - 1);
// print_uniqueIDs();
return (uniqueCounter - 1);
}
void internal_remove_torrent(long index)
{
assert(index < handles->size());
torrent_handle& h = handles->at(index);
// For valid torrents, save fastresume data
if (h.is_valid() && h.has_metadata())
{
h.pause();
entry data = h.write_resume_data();
std::stringstream s;
s << torrentNames->at(index) << ".fastresume";
// printf("Saving fastresume to: %s\r\n", s.str().c_str());
boost::filesystem::ofstream out(s.str(), std::ios_base::binary);
out.unsetf(std::ios_base::skipws);
bencode(std::ostream_iterator<char>(out), data);
}
ses->remove_torrent(h);
handles_t_iterator it = handles->begin() + index;
handles->erase(it);
uniqueIDs_t_iterator it2 = uniqueIDs->begin() + index;
uniqueIDs->erase(it2);
filterOuts_t_iterator it3 = filterOuts->begin() + index;
filterOuts->erase(it3);
torrentNames_t_iterator it4 = torrentNames->begin() + index;
torrentNames->erase(it4);
assert(handles->size() == uniqueIDs->size());
assert(handles->size() == filterOuts->size());
assert(handles->size() == torrentNames->size());
}
long get_peer_index(libtorrent::tcp::endpoint addr, std::vector<peer_info> const& peers)
{
long index = -1;
for (unsigned long i = 0; i < peers.size(); i++)
if (peers[i].ip == addr)
index = i;
return index;
}
// The following function contains code by Christophe Dumez and Arvid Norberg
void internal_add_files(torrent_info& t, path const& p, path const& l)
{
path f(p / l); // change default checker, perhaps?
if (is_directory(f))
{
for (boost::filesystem::directory_iterator i(f), end; i != end; ++i)
internal_add_files(t, p, l / i->leaf());
} else
t.add_file(l, file_size(f));
}
//=====================
// External functions
//=====================
static PyObject *torrent_init(PyObject *self, PyObject *args)
{
printf("python-libtorrent, using libtorrent %s. Compiled with NDEBUG value: %d\r\n",
LIBTORRENT_VERSION,
NDEBUG);
// Tell Boost that we are on *NIX, so bloody '.'s are ok inside a directory name!
path::default_name_check(empty_name_check);
char *clientID, *userAgent;
pythonLong v1,v2,v3,v4;
PyArg_ParseTuple(args, "siiiis", &clientID, &v1, &v2, &v3, &v4, &userAgent);
settings = new session_settings;
ses = new session(libtorrent::fingerprint(clientID, v1, v2, v3, v4));
handles = new handles_t;
uniqueIDs = new uniqueIDs_t;
filterOuts = new filterOuts_t;
torrentNames = new torrentNames_t;
// Init values
handles->reserve(10); // It doesn't cost us too much, 10 handles, does it? Reserve that space.
uniqueIDs->reserve(10);
filterOuts->reserve(10);
torrentNames->reserve(10);
settings->user_agent = std::string(userAgent);// + " (libtorrent " LIBTORRENT_VERSION ")";
// printf("ID: %s\r\n", clientID);
// printf("User Agent: %s\r\n", settings->user_agent.c_str());
// std::deque<std::string> events;
ses->set_max_half_open_connections(-1);
ses->set_download_rate_limit(-1);
ses->set_upload_rate_limit(-1);
// ses->listen_on(std::make_pair(6881, 6889), ""); // 6881, usually
ses->set_settings(*settings);
ses->set_severity_level(alert::debug);
// ses.set_severity_level(alert::warning);
// ses.set_severity_level(alert::fatal);
// ses.set_severity_level(alert::info);
/* // Load old DHT data
std::ifstream dht_file;
dht_file.open("dht.data", std::ios::in | std::ios::ate | std::ios::binary);
if (dht_file.is_open())
{
long size = dht_file.tellg();
char * memblock = new char[size];
dht_file.seekg(0, std::ios::beg);
dht_file.read(memblock, size);
dht_file.close();
std::vector<char> buffer(memblock, memblock+size);
delete[] memblock;
entry old_state = bdecode(buffer.begin(), buffer.end());
ses->start_dht();//old_state);
printf("DHT initialized from file. %d bytes read\r\n", int(size));
} else
printf("No DHT file found.\r\n");
*/
constants = Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i}",
"EVENT_NULL", EVENT_NULL,
"EVENT_FINISHED", EVENT_FINISHED,
"EVENT_PEER_ERROR", EVENT_PEER_ERROR,
"EVENT_INVALID_REQUEST", EVENT_INVALID_REQUEST,
"EVENT_FILE_ERROR", EVENT_FILE_ERROR,
"EVENT_HASH_FAILED_ERROR", EVENT_HASH_FAILED_ERROR,
"EVENT_PEER_BAN_ERROR", EVENT_PEER_BAN_ERROR,
"EVENT_FASTRESUME_REJECTED_ERROR", EVENT_FASTRESUME_REJECTED_ERROR,
"EVENT_TRACKER", EVENT_TRACKER,
"EVENT_OTHER", EVENT_OTHER,
"STATE_QUEUED", STATE_QUEUED,
"STATE_CHECKING", STATE_CHECKING,
"STATE_CONNECTING", STATE_CONNECTING,
"STATE_DOWNLOADING_META", STATE_DOWNLOADING_META,
"STATE_DOWNLOADING", STATE_DOWNLOADING,
"STATE_FINISHED", STATE_FINISHED,
"STATE_SEEDING", STATE_SEEDING,
"STATE_ALLOCATING", STATE_ALLOCATING,
"ERROR_INVALID_ENCODING", ERROR_INVALID_ENCODING,
"ERROR_INVALID_TORRENT", ERROR_INVALID_TORRENT,
"ERROR_FILESYSTEM", ERROR_FILESYSTEM,
"ERROR_DUPLICATE_TORRENT", ERROR_DUPLICATE_TORRENT);
Py_INCREF(Py_None); return Py_None;
};
static PyObject *torrent_quit(PyObject *self, PyObject *args)
{
long Num = handles->size();
// Shut down torrents gracefully
for (long i = 0; i < Num; i++)
internal_remove_torrent(0);
/* // Shut down DHT gracefully, saving the current state
entry curr_state = ses->dht_state();
std::vector<char> buffer;
bencode(std::back_inserter(buffer), curr_state);
std::ofstream dht_file;
dht_file.open("dht.data", std::ios::out | std::ios::trunc | std::ios::binary);
dht_file.write(&buffer[0], buffer.size());
dht_file.close();
printf("DHT saved to file. %d bytes saved\r\n", buffer.size());
ses->stop_dht();
*/
delete ses; // SLOWPOKE because of waiting for the trackers before shutting down
delete settings;
delete handles;
delete uniqueIDs;
Py_DECREF(constants);
Py_INCREF(Py_None); return Py_None;
};
static PyObject *torrent_setMaxHalfOpenConnections(PyObject *self, PyObject *args)
{
pythonLong arg;
PyArg_ParseTuple(args, "i", &arg);
ses->set_max_half_open_connections(arg);
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_setDownloadRateLimit(PyObject *self, PyObject *args)
{
pythonLong arg;
PyArg_ParseTuple(args, "i", &arg);
printf("Capping download to %d bytes per second\r\n", (int)arg);
ses->set_download_rate_limit(arg);
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_setUploadRateLimit(PyObject *self, PyObject *args)
{
pythonLong arg;
PyArg_ParseTuple(args, "i", &arg);
printf("Capping upload to %d bytes per second\r\n", (int)arg);
ses->set_upload_rate_limit(arg);
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_setListenOn(PyObject *self, PyObject *args)
{
pythonLong portStart, portEnd;
PyArg_ParseTuple(args, "ii", &portStart, &portEnd);
ses->listen_on(std::make_pair(portStart, portEnd), "");
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_isListening(PyObject *self, PyObject *args)
{
long ret = (ses->is_listening() != 0);
return Py_BuildValue("i", ret);
}
static PyObject *torrent_listeningPort(PyObject *self, PyObject *args)
{
return Py_BuildValue("i", (pythonLong)ses->listen_port());
}
static PyObject *torrent_setMaxUploads(PyObject *self, PyObject *args)
{
pythonLong max_up;
PyArg_ParseTuple(args, "i", &max_up);
ses->set_max_uploads(max_up);
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_setMaxConnections(PyObject *self, PyObject *args)
{
pythonLong max_conn;
PyArg_ParseTuple(args, "i", &max_conn);
// printf("Setting max connections: %d\r\n", max_conn);
ses->set_max_connections(max_conn);
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_addTorrent(PyObject *self, PyObject *args)
{
const char *name, *saveDir;
pythonLong compact;
PyArg_ParseTuple(args, "ssi", &name, &saveDir, &compact);
path saveDir_2 (saveDir, empty_name_check);
try
{
return Py_BuildValue("i", internal_add_torrent(name, 0, compact, saveDir_2));
}
catch (invalid_encoding&)
{
return Py_BuildValue("i", ERROR_INVALID_ENCODING);
}
catch (invalid_torrent_file&)
{
return Py_BuildValue("i", ERROR_INVALID_TORRENT);
}
catch (boost::filesystem::filesystem_error&)
{
return Py_BuildValue("i", ERROR_FILESYSTEM);
}
catch (duplicate_torrent&)
{
return Py_BuildValue("i", ERROR_DUPLICATE_TORRENT);
}
}
static PyObject *torrent_removeTorrent(PyObject *self, PyObject *args)
{
pythonLong uniqueID;
PyArg_ParseTuple(args, "i", &uniqueID);
long index = get_index_from_unique(uniqueID);
internal_remove_torrent(index);
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_getNumTorrents(PyObject *self, PyObject *args)
{
return Py_BuildValue("i", handles->size());
}
static PyObject *torrent_reannounce(PyObject *self, PyObject *args)
{
pythonLong uniqueID;
PyArg_ParseTuple(args, "i", &uniqueID);
long index = get_index_from_unique(uniqueID);
handles->at(index).force_reannounce();
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_pause(PyObject *self, PyObject *args)
{
pythonLong uniqueID;
PyArg_ParseTuple(args, "i", &uniqueID);
long index = get_index_from_unique(uniqueID);
handles->at(index).pause();
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_resume(PyObject *self, PyObject *args)
{
pythonLong uniqueID;
PyArg_ParseTuple(args, "i", &uniqueID);
long index = get_index_from_unique(uniqueID);
handles->at(index).resume();
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_getName(PyObject *self, PyObject *args)
{
pythonLong uniqueID;
PyArg_ParseTuple(args, "i", &uniqueID);
long index = get_index_from_unique(uniqueID);
return Py_BuildValue("s", handles->at(index).get_torrent_info().name().c_str());
}
static PyObject *torrent_getState(PyObject *self, PyObject *args)
{
pythonLong uniqueID;
PyArg_ParseTuple(args, "i", &uniqueID);
long index = get_index_from_unique(uniqueID);
torrent_status s = handles->at(index).status();
const torrent_info &i = handles->at(index).get_torrent_info();
std::vector<peer_info> peers;
handles->at(index).get_peer_info(peers);
long total_seeds = 0;
long total_peers = 0;
for (unsigned long i = 0; i < peers.size(); i++)
if (peers[i].seed)
total_seeds++;
else
total_peers++;
return Py_BuildValue("{s:l,s:l,s:l,s:f,s:f,s:d,s:f,s:l,s:f,s:l,s:s,s:s,s:f,s:d,s:l,s:l,s:l,s:d,s:l,s:l,s:l,s:l,s:l,s:l,s:d,s:d,s:l,s:l}",
"state", s.state,
"numPeers", s.num_peers,
"numSeeds", s.num_seeds,
"distributedCopies", s.distributed_copies,
"downloadRate", s.download_rate,
"totalDownload", double(s.total_payload_download),
"uploadRate", s.upload_rate,
"totalUpload", long(s.total_payload_upload),
"ratio", float(-1),//float(s.total_payload_download)/float(s.total_payload_upload),
"trackerOK", !s.current_tracker.empty(),
"nextAnnounce", boost::posix_time::to_simple_string(s.next_announce).c_str(),
"tracker", s.current_tracker.c_str(),
"progress", float(s.progress),
"totalDone", double(s.total_done),
"totalPieces", long(s.pieces),
"piecesDone", long(s.num_pieces),
"blockSize", long(s.block_size),
"totalSize", double(i.total_size()),
"pieceLength", long(i.piece_length()),
"numPieces", long(i.num_pieces()),
"totalSeeds", total_seeds,
"totalPeers", total_peers,
"isPaused", long(handles->at(index).is_paused()),
"isSeed", long(handles->at(index).is_seed()),
"totalWanted", double(s.total_wanted),
"totalWantedDone", double(s.total_wanted_done),
"numComplete", long(s.num_complete),
"numIncomplete", long(s.num_incomplete));
};
static PyObject *torrent_popEvent(PyObject *self, PyObject *args)
{
std::auto_ptr<alert> a;
a = ses->pop_alert();
alert *poppedAlert = a.get();
if (!poppedAlert)
{
Py_INCREF(Py_None); return Py_None;
} else if (dynamic_cast<torrent_finished_alert*>(poppedAlert))
{
torrent_handle handle = (dynamic_cast<torrent_finished_alert*>(poppedAlert))->handle;
if (handle_exists(handle))
return Py_BuildValue("{s:i,s:i}", "eventType", EVENT_FINISHED,
"uniqueID", uniqueIDs->at(get_torrent_index(handle)));
else
{ Py_INCREF(Py_None); return Py_None; }
} else if (dynamic_cast<peer_error_alert*>(poppedAlert))
{
peer_id peerID = (dynamic_cast<peer_error_alert*>(poppedAlert))->pid;
std::string peerIP = (dynamic_cast<peer_error_alert*>(poppedAlert))->ip.address().to_string();
return Py_BuildValue("{s:i,s:s,s:s,s:s}", "eventType", EVENT_PEER_ERROR,
"clientID", identify_client(peerID).c_str(),
"ip", peerIP.c_str(),
"message", a->msg().c_str() );
} else if (dynamic_cast<invalid_request_alert*>(poppedAlert))
{
peer_id peerID = (dynamic_cast<invalid_request_alert*>(poppedAlert))->pid;
return Py_BuildValue("{s:i,s:s,s:s}", "eventType", EVENT_INVALID_REQUEST,
"clientID", identify_client(peerID).c_str(),
"message", a->msg().c_str() );
} else if (dynamic_cast<file_error_alert*>(poppedAlert))
{
torrent_handle handle = (dynamic_cast<file_error_alert*>(poppedAlert))->handle;
if (handle_exists(handle))
return Py_BuildValue("{s:i,s:i,s:s}", "eventType", EVENT_FILE_ERROR,
"uniqueID", uniqueIDs->at(get_torrent_index(handle)),
"message", a->msg().c_str() );
else
{ Py_INCREF(Py_None); return Py_None; }
} else if (dynamic_cast<hash_failed_alert*>(poppedAlert))
{
torrent_handle handle = (dynamic_cast<hash_failed_alert*>(poppedAlert))->handle;
if (handle_exists(handle))
return Py_BuildValue("{s:i,s:i,s:i,s:s}", "eventType", EVENT_HASH_FAILED_ERROR,
"uniqueID", uniqueIDs->at(get_torrent_index(handle)),
"pieceIndex", long((dynamic_cast<hash_failed_alert*>(poppedAlert))->piece_index),
"message", a->msg().c_str() );
else
{ Py_INCREF(Py_None); return Py_None; }
} else if (dynamic_cast<peer_ban_alert*>(poppedAlert))
{
torrent_handle handle = (dynamic_cast<peer_ban_alert*>(poppedAlert))->handle;
std::string peerIP = (dynamic_cast<peer_ban_alert*>(poppedAlert))->ip.address().to_string();
if (handle_exists(handle))
return Py_BuildValue("{s:i,s:i,s:s,s:s}", "eventType", EVENT_PEER_BAN_ERROR,
"uniqueID", uniqueIDs->at(get_torrent_index(handle)),
"ip", peerIP.c_str(),
"message", a->msg().c_str() );
else
{ Py_INCREF(Py_None); return Py_None; }
} else if (dynamic_cast<fastresume_rejected_alert*>(poppedAlert))
{
torrent_handle handle = (dynamic_cast<fastresume_rejected_alert*>(poppedAlert))->handle;
if (handle_exists(handle))
return Py_BuildValue("{s:i,s:i,s:s}", "eventType", EVENT_FASTRESUME_REJECTED_ERROR,
"uniqueID", uniqueIDs->at(get_torrent_index(handle)),
"message", a->msg().c_str() );
else
{ Py_INCREF(Py_None); return Py_None; }
} else if (dynamic_cast<tracker_announce_alert*>(poppedAlert))
{
torrent_handle handle = (dynamic_cast<tracker_announce_alert*>(poppedAlert))->handle;
if (handle_exists(handle))
return Py_BuildValue("{s:i,s:i,s:s,s:s}", "eventType", EVENT_TRACKER,
"uniqueID", uniqueIDs->at(get_torrent_index(handle)),
"trackerStatus", "Announce sent",
"message", a->msg().c_str() );
else
{ Py_INCREF(Py_None); return Py_None; }
} else if (dynamic_cast<tracker_alert*>(poppedAlert))
{
torrent_handle handle = (dynamic_cast<tracker_alert*>(poppedAlert))->handle;
if (handle_exists(handle))
return Py_BuildValue("{s:i,s:i,s:s,s:s}", "eventType", EVENT_TRACKER,
"uniqueID", uniqueIDs->at(get_torrent_index(handle)),
"trackerStatus", "Bad response (status code=?)",
"message", a->msg().c_str() );
else
{ Py_INCREF(Py_None); return Py_None; }
} else if (dynamic_cast<tracker_reply_alert*>(poppedAlert))
{
torrent_handle handle = (dynamic_cast<tracker_reply_alert*>(poppedAlert))->handle;
if (handle_exists(handle))
return Py_BuildValue("{s:i,s:i,s:s,s:s}", "eventType", EVENT_TRACKER,
"uniqueID", uniqueIDs->at(get_torrent_index(handle)),
"trackerStatus", "Announce succeeded",
"message", a->msg().c_str() );
else
{ Py_INCREF(Py_None); return Py_None; }
} else if (dynamic_cast<tracker_warning_alert*>(poppedAlert))
{
torrent_handle handle = (dynamic_cast<tracker_warning_alert*>(poppedAlert))->handle;
if (handle_exists(handle))
return Py_BuildValue("{s:i,s:i,s:s,s:s}", "eventType", EVENT_TRACKER,
"uniqueID", uniqueIDs->at(get_torrent_index(handle)),
"trackerStatus", "Warning in response",
"message", a->msg().c_str() );
else
{ Py_INCREF(Py_None); return Py_None; }
}
return Py_BuildValue("{s:i,s:s}", "eventType", EVENT_OTHER,
"message", a->msg().c_str() );
}
static PyObject *torrent_getSessionInfo(PyObject *self, PyObject *args)
{
session_status s = ses->status();
return Py_BuildValue("{s:l,s:f,s:f,s:f,s:f,s:l}",
"hasIncomingConnections", long(s.has_incoming_connections),
"uploadRate", float(s.upload_rate),
"downloadRate", float(s.download_rate),
"payloadUploadRate", float(s.payload_upload_rate),
"payloadDownloadRate", float(s.payload_download_rate),
"numPeers", long(s.num_peers));
}
static PyObject *torrent_getPeerInfo(PyObject *self, PyObject *args)
{
pythonLong uniqueID;
PyArg_ParseTuple(args, "i", &uniqueID);
long index = get_index_from_unique(uniqueID);
std::vector<peer_info> peers;
handles->at(index).get_peer_info(peers);
PyObject *peerInfo;
PyObject *ret = PyTuple_New(peers.size());
for (unsigned long i = 0; i < peers.size(); i++)
{
std::vector<bool> &pieces = peers[i].pieces;
unsigned long pieces_had = 0;
for (unsigned long piece = 0; piece < pieces.size(); piece++)
if (pieces[piece])
pieces_had++;
peerInfo = Py_BuildValue(
"{s:f,s:d,s:f,s:d,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:s,s:i,s:s,s:f}",
"downloadSpeed", float(peers[i].down_speed),
"totalDownload", double(peers[i].total_download),
"uploadSpeed", float(peers[i].up_speed),
"totalUpload", double(peers[i].total_upload),
"downloadQueueLength", long(peers[i].download_queue_length),
"uploadQueueLength", long(peers[i].upload_queue_length),
"isInteresting", long((peers[i].flags & peer_info::interesting) != 0),
"isChoked", long((peers[i].flags & peer_info::choked) != 0),
"isRemoteInterested", long((peers[i].flags & peer_info::remote_interested) != 0),
"isRemoteChoked", long((peers[i].flags & peer_info::remote_choked) != 0),
"SupportsExtensions", long((peers[i].flags & peer_info::supports_extensions) != 0),
"isLocalConnection", long((peers[i].flags & peer_info::local_connection) != 0),
"isAwaitingHandshake", long((peers[i].flags & peer_info::handshake) != 0),
"isConnecting", long((peers[i].flags & peer_info::connecting) != 0),
"isQueued", long((peers[i].flags & peer_info::queued) != 0),
"client", peers[i].client.c_str(),
"isSeed", long(peers[i].seed),
"ip", peers[i].ip.address().to_string().c_str(),
"peerHas", float(float(pieces_had)*100.0/pieces.size())
);
PyTuple_SetItem(ret, i, peerInfo);
};
return ret;
};
static PyObject *torrent_getFileInfo(PyObject *self, PyObject *args)
{
pythonLong uniqueID;
PyArg_ParseTuple(args, "i", &uniqueID);
long index = get_index_from_unique(uniqueID);
std::vector<PyObject *> tempFiles;
PyObject *fileInfo;
std::vector<float> progresses;
handles->at(index).file_progress(progresses);
torrent_info::file_iterator start = handles->at(index).get_torrent_info().begin_files();
torrent_info::file_iterator end = handles->at(index).get_torrent_info().end_files();
long fileIndex = 0;
filterOut_t &filterOut = filterOuts->at(index);
for(torrent_info::file_iterator i = start; i != end; ++i)
{
file_entry const &currFile = (*i);
fileInfo = Py_BuildValue(
"{s:s,s:d,s:d,s:f,s:i}",
"path", currFile.path.string().c_str(),
"offset", double(currFile.offset),
"size", double(currFile.size),
"progress", progresses[i - start]*100.0,
"filteredOut", long(filterOut.at(fileIndex))
);
fileIndex++;
tempFiles.push_back(fileInfo);
};
PyObject *ret = PyTuple_New(tempFiles.size());
for (unsigned long i = 0; i < tempFiles.size(); i++)
PyTuple_SetItem(ret, i, tempFiles[i]);
return ret;
};
static PyObject *torrent_setFilterOut(PyObject *self, PyObject *args)
{
pythonLong uniqueID;
PyObject *filterOutObject;
PyArg_ParseTuple(args, "iO", &uniqueID, &filterOutObject);
long index = get_index_from_unique(uniqueID);
long numFiles = handles->at(index).get_torrent_info().num_files();
assert(PyList_Size(filterOutObject) == numFiles);
for (long i = 0; i < numFiles; i++)
{
filterOuts->at(index).at(i) = PyInt_AsLong(PyList_GetItem(filterOutObject, i));
};
handles->at(index).filter_files(filterOuts->at(index));
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_constants(PyObject *self, PyObject *args)
{
Py_INCREF(constants); return constants;
}
static PyObject *torrent_startDHT(PyObject *self, PyObject *args)
{
const char *DHTpath;
PyArg_ParseTuple(args, "s", &DHTpath);
printf("Loading DHT state from %s\r\n", DHTpath);
path tempPath(DHTpath, empty_name_check);
boost::filesystem::ifstream dht_state_file(tempPath, std::ios_base::binary);
dht_state_file.unsetf(std::ios_base::skipws);
entry dht_state;
try{
dht_state = bdecode(std::istream_iterator<char>(dht_state_file),
std::istream_iterator<char>());
ses->start_dht(dht_state);
} catch (std::exception&) {
printf("No DHT file to resume\r\n");
ses->start_dht();
}
ses->add_dht_router(std::make_pair(std::string("router.bittorrent.com"), DHT_ROUTER_PORT));
ses->add_dht_router(std::make_pair(std::string("router.utorrent.com"), DHT_ROUTER_PORT));
ses->add_dht_router(std::make_pair(std::string("router.bitcomet.com"), DHT_ROUTER_PORT));
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_stopDHT(PyObject *self, PyObject *args)
{
const char *DHTpath;
PyArg_ParseTuple(args, "s", &DHTpath);
printf("Saving DHT state to %s\r\n", DHTpath);
path tempPath = path(DHTpath, empty_name_check);
try {
entry dht_state = ses->dht_state();
boost::filesystem::ofstream out(tempPath, std::ios_base::binary);
out.unsetf(std::ios_base::skipws);
bencode(std::ostream_iterator<char>(out), dht_state);
} catch (std::exception& e) {
printf("An error occured in saving DHT\r\n");
std::cerr << e.what() << "\n";
}
Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_getDHTinfo(PyObject *self, PyObject *args)
{
// printf("Pouring out DHT state:\r\n");
entry DHTstate = ses->dht_state();
// DHTstate.print(cout);
entry *nodes = DHTstate.find_key("nodes");
if (!nodes)
return Py_BuildValue("l", -1); // No nodes - we are just starting up...
entry::list_type &peers = nodes->list();
entry::list_type::const_iterator i;
pythonLong numPeers = 0;
i = peers.begin();
while (i != peers.end())
{
// printf("A:%s\r\n", i->string().c_str());
numPeers++;
i++;
}
// printf("All done.\r\n");
return Py_BuildValue("l", numPeers);
// Py_INCREF(Py_None); return Py_None;
}
// Create Torrents: call with something like:
// createTorrent("mytorrent.torrent", "directory or file to make a torrent out of",
// "tracker1\ntracker2\ntracker3", "no comment", 256, "Deluge");
// That makes a torrent with pieces of 256K, with "Deluge" as the creator string.
//
// The following function contains code by Christophe Dumez and Arvid Norberg
static PyObject *torrent_createTorrent(PyObject *self, PyObject *args)
{
char *destination, *comment, *creator_str, *input, *trackers;
pythonLong piece_size;
PyArg_ParseTuple(args, "ssssis", &destination, &input, &trackers, &comment, &piece_size, &creator_str);
piece_size = piece_size * 1024;
try
{
torrent_info t;
path full_path = complete(path(input));
boost::filesystem::ofstream out(complete(path(destination)), std::ios_base::binary);
internal_add_files(t, full_path.branch_path(), full_path.leaf());
t.set_piece_size(piece_size);
storage st(t, full_path.branch_path());
std::string stdTrackers(trackers);
unsigned long index = 0, next = stdTrackers.find("\n");
while (1 == 1)
{