Lines 103-156
Link Here
|
103 |
open BTUdpTracker |
103 |
open BTUdpTracker |
104 |
open UdpSocket |
104 |
open UdpSocket |
105 |
|
105 |
|
106 |
|
|
|
107 |
(* some stupid temporary copypasta because i couldnt figure out how to import from btinteractive *) |
108 |
(* let hack_op_file_cancel file = *) |
109 |
(* CommonSwarming.remove_swarmer file.file_swarmer; *) |
110 |
(* file.file_swarmer <- None; *) |
111 |
(* (\* forward declarations turned out difficult. wtf?*\) *) |
112 |
(* (\* file_stop file;*\) *) |
113 |
(* remove_file file; *) |
114 |
(* (\*disconnect_clients file;*\) *) |
115 |
(* (\*remove_all_clients file;*\) *) |
116 |
(* if Sys.file_exists file.file_torrent_diskname then Sys.remove file.file_torrent_diskname *) |
117 |
|
118 |
let load_torrent_string s user group = |
119 |
if !verbose then lprintf_nl "load_torrent_string"; |
120 |
let file_id, torrent = BTTorrent.decode_torrent s in |
121 |
|
122 |
(* Save the torrent, because we later want to put |
123 |
it in the seeded directory. *) |
124 |
let torrent_diskname = CommonFile.concat_file downloads_directory (torrent.torrent_name ^ ".torrent") in |
125 |
if Sys.file_exists torrent_diskname then |
126 |
begin |
127 |
if !verbose then lprintf_nl "load_torrent_string: %s already exists, ignoring" torrent_diskname; |
128 |
raise (Torrent_already_exists torrent.torrent_name) |
129 |
end; |
130 |
File.from_string torrent_diskname s; |
131 |
|
132 |
if !verbose then |
133 |
lprintf_nl "Starting torrent download with diskname: %s" |
134 |
torrent_diskname; |
135 |
let file = new_download file_id torrent torrent_diskname user group in |
136 |
(* talk_to_tracker file true; TODO stupid fwd declaration issue. OTOH bep9 torrents arent tracker based anyway*) |
137 |
CommonInteractive.start_download (file_find (file_num file)); |
138 |
file |
139 |
|
140 |
let load_torrent_file filename user group = |
141 |
if !verbose then |
142 |
lprintf_nl "load_torrent_file %s" filename; |
143 |
let s = File.to_string filename in |
144 |
(* Delete the torrent if it is in the downloads dir. because it gets saved |
145 |
again under the torrent name and we don't want to clutter up this dir. .*) |
146 |
if Sys.file_exists filename |
147 |
&& (Filename.dirname filename) = downloads_directory then |
148 |
Sys.remove filename; |
149 |
ignore (load_torrent_string s user group) |
150 |
(* end copypasta *) |
151 |
|
152 |
|
153 |
|
154 |
let string_of_event = function |
106 |
let string_of_event = function |
155 |
| READ_DONE -> "READ_DONE" |
107 |
| READ_DONE -> "READ_DONE" |
156 |
| WRITE_DONE -> "WRITE_DONE" |
108 |
| WRITE_DONE -> "WRITE_DONE" |
Lines 621-629
Link Here
|
621 |
*) |
573 |
*) |
622 |
|
574 |
|
623 |
let send_bitfield c = |
575 |
let send_bitfield c = |
624 |
if c.client_file.file_metadata_downloading then |
576 |
if not c.client_file.file_metadata_downloading then |
625 |
lprintf_nl "dont send bitmap, we are in metadata state" |
|
|
626 |
else |
627 |
send_client c (BitField |
577 |
send_client c (BitField |
628 |
( |
578 |
( |
629 |
match c.client_file.file_swarmer with |
579 |
match c.client_file.file_swarmer with |
Lines 667-673
Link Here
|
667 |
let msg = (B.encode (B.Dictionary [(* "e",B.Int 0L; *) |
617 |
let msg = (B.encode (B.Dictionary [(* "e",B.Int 0L; *) |
668 |
"m", (B.Dictionary ["ut_metadata", B.Int 1L]); |
618 |
"m", (B.Dictionary ["ut_metadata", B.Int 1L]); |
669 |
(* "metadata_size", B.Int (-1L) *)])) in begin |
619 |
(* "metadata_size", B.Int (-1L) *)])) in begin |
670 |
lprintf_file_nl (as_file file) "send extended handshake msg %s" msg; |
|
|
671 |
send_client c (Extended (Int64.to_int 0L, msg)); |
620 |
send_client c (Extended (Int64.to_int 0L, msg)); |
672 |
end |
621 |
end |
673 |
|
622 |
|
Lines 675-681
Link Here
|
675 |
let module B = Bencode in |
624 |
let module B = Bencode in |
676 |
let msg = (B.encode (B.Dictionary ["msg_type", B.Int 0L; (* 0 is request subtype*) |
625 |
let msg = (B.encode (B.Dictionary ["msg_type", B.Int 0L; (* 0 is request subtype*) |
677 |
"piece", B.Int piece; ])) in begin |
626 |
"piece", B.Int piece; ])) in begin |
678 |
lprintf_file_nl (as_file file) "send extended request for piece:%Ld msgid:%Ld msg:%s" piece c.client_ut_metadata_msg msg; |
|
|
679 |
send_client c (Extended (Int64.to_int c.client_ut_metadata_msg, msg)); |
627 |
send_client c (Extended (Int64.to_int c.client_ut_metadata_msg, msg)); |
680 |
end |
628 |
end |
681 |
|
629 |
|
Lines 1043-1049
Link Here
|
1043 |
@param msg The message sent by the client |
991 |
@param msg The message sent by the client |
1044 |
*) |
992 |
*) |
1045 |
and client_to_client c sock msg = |
993 |
and client_to_client c sock msg = |
1046 |
(* if !verbose_msg_clients then begin *) |
994 |
if !verbose_msg_clients then begin |
1047 |
let (ip,port) = (TcpBufferedSocket.peer_addr sock) in |
995 |
let (ip,port) = (TcpBufferedSocket.peer_addr sock) in |
1048 |
let (timeout, next) = get_rtimeout sock in |
996 |
let (timeout, next) = get_rtimeout sock in |
1049 |
lprintf_nl "CLIENT %d(%s:%d): (%d, %d,%d) Received %s" |
997 |
lprintf_nl "CLIENT %d(%s:%d): (%d, %d,%d) Received %s" |
Lines 1052-1058
Link Here
|
1052 |
(int_of_float timeout) |
1000 |
(int_of_float timeout) |
1053 |
(int_of_float next) |
1001 |
(int_of_float next) |
1054 |
(TcpMessages.to_string msg); |
1002 |
(TcpMessages.to_string msg); |
1055 |
(* end; *) |
1003 |
end; |
1056 |
|
1004 |
|
1057 |
let file = c.client_file in |
1005 |
let file = c.client_file in |
1058 |
|
1006 |
|
Lines 1154-1164
Link Here
|
1154 |
|
1102 |
|
1155 |
| BitField p -> |
1103 |
| BitField p -> |
1156 |
(*A bitfield is a summary of what a client have*) |
1104 |
(*A bitfield is a summary of what a client have*) |
1157 |
lprintf_file_nl (as_file file) "Bitfield message, metadata state %B" c.client_file.file_metadata_downloading ; |
1105 |
if !verbose_msg_clients then |
1158 |
if c.client_file.file_metadata_downloading then |
1106 |
lprintf_file_nl (as_file file) "Bitfield message, metadata state %B" c.client_file.file_metadata_downloading; |
1159 |
lprintf_file_nl (as_file file) "ignoring Bitfield message, we are in metadata state" |
1107 |
if not c.client_file.file_metadata_downloading then |
1160 |
else |
|
|
1161 |
|
1162 |
begin |
1108 |
begin |
1163 |
match c.client_file.file_swarmer with |
1109 |
match c.client_file.file_swarmer with |
1164 |
None -> () |
1110 |
None -> () |
Lines 1205-1213
Link Here
|
1205 |
|
1151 |
|
1206 |
| Have n -> |
1152 |
| Have n -> |
1207 |
(* A client can send a "Have" without sending a Bitfield *) |
1153 |
(* A client can send a "Have" without sending a Bitfield *) |
1208 |
if c.client_file.file_metadata_downloading then |
1154 |
if not c.client_file.file_metadata_downloading then |
1209 |
lprintf_file_nl (as_file file) "ignoring Have message, we are in metadata state" |
|
|
1210 |
else |
1211 |
begin |
1155 |
begin |
1212 |
match c.client_file.file_swarmer with |
1156 |
match c.client_file.file_swarmer with |
1213 |
None -> () |
1157 |
None -> () |
Lines 1343-1367
Link Here
|
1343 |
this should of course be moved but I dont know where yet. |
1287 |
this should of course be moved but I dont know where yet. |
1344 |
also we shouldnt send more than one handshake of course... |
1288 |
also we shouldnt send more than one handshake of course... |
1345 |
*) |
1289 |
*) |
1346 |
lprintf_file_nl (as_file file) "Got extended msg: %d %s" extmsg payload; |
1290 |
if !verbose_msg_clients then |
|
|
1291 |
lprintf_file_nl (as_file file) "Got extended msg: %d %s" extmsg (String.escaped payload); |
1347 |
|
1292 |
|
1348 |
match extmsg with |
1293 |
match extmsg with |
1349 |
0x0 -> |
1294 |
0x0 -> |
1350 |
lprintf_file_nl (as_file file) "Got extended handshake "; |
1295 |
if !verbose_msg_clients then |
|
|
1296 |
lprintf_file_nl (as_file file) "Got extended handshake"; |
1351 |
let dict = Bencode.decode payload in begin |
1297 |
let dict = Bencode.decode payload in begin |
1352 |
match dict with |
1298 |
match dict with |
1353 |
Dictionary list -> |
1299 |
Dictionary list -> |
1354 |
List.iter (fun (key,value) -> |
1300 |
List.iter (fun (key,value) -> |
1355 |
match key, value with |
1301 |
match key, value with |
1356 |
"metadata_size", Int n -> |
1302 |
"metadata_size", Int n -> |
1357 |
lprintf_file_nl (as_file file) "Got metadata size %Ld" n; |
1303 |
if !verbose_msg_clients then |
|
|
1304 |
lprintf_file_nl (as_file file) "Got metadata size %Ld" n; |
1358 |
c.client_file.file_metadata_size <- n; |
1305 |
c.client_file.file_metadata_size <- n; |
1359 |
| "m", Dictionary mdict -> |
1306 |
| "m", Dictionary mdict -> |
1360 |
lprintf_file_nl (as_file file) "Got meta dict "; |
1307 |
if !verbose_msg_clients then |
|
|
1308 |
lprintf_file_nl (as_file file) "Got meta dict"; |
1361 |
List.iter (fun (key,value) -> |
1309 |
List.iter (fun (key,value) -> |
1362 |
match key, value with |
1310 |
match key, value with |
1363 |
"ut_metadata", Int n -> |
1311 |
"ut_metadata", Int n -> |
1364 |
lprintf_file_nl (as_file file) "ut_metadata is %Ld " n; |
1312 |
if !verbose_msg_clients then |
|
|
1313 |
lprintf_file_nl (as_file file) "ut_metadata is %Ld " n; |
1365 |
c.client_ut_metadata_msg <- n; |
1314 |
c.client_ut_metadata_msg <- n; |
1366 |
| _ -> (); |
1315 |
| _ -> (); |
1367 |
) mdict; |
1316 |
) mdict; |
Lines 1390-1396
Link Here
|
1390 |
after the dict comes the actual piece |
1339 |
after the dict comes the actual piece |
1391 |
|
1340 |
|
1392 |
*) |
1341 |
*) |
1393 |
lprintf_file_nl (as_file file) "Got extended ut_metadata message "; |
1342 |
if !verbose_msg_clients then |
|
|
1343 |
lprintf_file_nl (as_file file) "Got extended ut_metadata message"; |
1394 |
let dict = Bencode.decode payload in |
1344 |
let dict = Bencode.decode payload in |
1395 |
let msgtype = ref 0L in begin |
1345 |
let msgtype = ref 0L in begin |
1396 |
begin |
1346 |
begin |
Lines 1399-1411
Link Here
|
1399 |
List.iter (fun (key,value) -> |
1349 |
List.iter (fun (key,value) -> |
1400 |
match key, value with |
1350 |
match key, value with |
1401 |
"msg_type", Int n -> |
1351 |
"msg_type", Int n -> |
1402 |
lprintf_file_nl (as_file file) "msg_type %Ld" n; |
1352 |
if !verbose_msg_clients then |
|
|
1353 |
lprintf_file_nl (as_file file) "msg_type %Ld" n; |
1403 |
msgtype := n; |
1354 |
msgtype := n; |
1404 |
| "piece", Int n -> |
1355 |
| "piece", Int n -> |
1405 |
lprintf_file_nl (as_file file) "piece %Ld" n; |
1356 |
if !verbose_msg_clients then |
|
|
1357 |
lprintf_file_nl (as_file file) "piece %Ld" n; |
1406 |
file.file_metadata_piece <- n; |
1358 |
file.file_metadata_piece <- n; |
1407 |
| "total_size", Int n -> |
1359 |
| "total_size", Int n -> |
1408 |
lprintf_file_nl (as_file file) "total_size %Ld" n; (* should always be the same as received in the initial handshake i suppose *) |
1360 |
if !verbose_msg_clients then |
|
|
1361 |
lprintf_file_nl (as_file file) "total_size %Ld" n; (* should always be the same as received in the initial handshake i suppose *) |
1409 |
|_ -> () ; |
1362 |
|_ -> () ; |
1410 |
) list; |
1363 |
) list; |
1411 |
|_ -> () ; |
1364 |
|_ -> () ; |
Lines 1413-1430
Link Here
|
1413 |
match !msgtype with |
1366 |
match !msgtype with |
1414 |
1L -> |
1367 |
1L -> |
1415 |
let last_piece_index = (Int64.div file.file_metadata_size 16384L) in |
1368 |
let last_piece_index = (Int64.div file.file_metadata_size 16384L) in |
1416 |
lprintf_file_nl (as_file file) "handling metadata piece %Ld of %Ld" |
1369 |
if !verbose_msg_clients then |
1417 |
file.file_metadata_piece |
1370 |
lprintf_file_nl (as_file file) "handling metadata piece %Ld of %Ld" |
1418 |
last_piece_index; |
1371 |
file.file_metadata_piece |
|
|
1372 |
last_piece_index; |
1419 |
(* store the metadata piece in memory *) |
1373 |
(* store the metadata piece in memory *) |
1420 |
file.file_metadata_chunks.(1 + (Int64.to_int file.file_metadata_piece)) <- payload; |
1374 |
file.file_metadata_chunks.(1 + (Int64.to_int file.file_metadata_piece)) <- payload; |
1421 |
(* possibly write metadata to disk *) |
1375 |
(* possibly write metadata to disk *) |
1422 |
if file.file_metadata_piece >= |
1376 |
if file.file_metadata_piece >= |
1423 |
(Int64.div file.file_metadata_size 16384L) then begin |
1377 |
(Int64.div file.file_metadata_size 16384L) then begin |
1424 |
lprintf_file_nl (as_file file) "this was the last piece "; |
1378 |
if !verbose_msg_clients then |
|
|
1379 |
lprintf_file_nl (as_file file) "this was the last piece"; |
1425 |
(* here we should simply delete the current download, and wait for mld to pick up the new torrent file *) |
1380 |
(* here we should simply delete the current download, and wait for mld to pick up the new torrent file *) |
1426 |
(* the entire payload is currently in the array, TODO *) |
1381 |
(* the entire payload is currently in the array, TODO *) |
1427 |
let newtorrentfile = (Printf.sprintf "/tmp/BT-%s.torrent" |
1382 |
let newtorrentfile = (Printf.sprintf "%s/BT-%s.torrent" |
|
|
1383 |
(Filename2.temp_dir_name ()) |
1428 |
(Sha1.to_string file.file_id)) in |
1384 |
(Sha1.to_string file.file_id)) in |
1429 |
let fd = Unix32.create_rw newtorrentfile in |
1385 |
let fd = Unix32.create_rw newtorrentfile in |
1430 |
let fileindex = ref 0L in |
1386 |
let fileindex = ref 0L in |
Lines 1447-1485
Link Here
|
1447 |
) file.file_metadata_chunks; |
1403 |
) file.file_metadata_chunks; |
1448 |
with e -> begin |
1404 |
with e -> begin |
1449 |
(* TODO ignoring errors for now, the array isnt really set up right anyway yet *) |
1405 |
(* TODO ignoring errors for now, the array isnt really set up right anyway yet *) |
|
|
1406 |
(* |
1450 |
lprintf_file_nl (as_file file) "Error %s saving metadata" |
1407 |
lprintf_file_nl (as_file file) "Error %s saving metadata" |
1451 |
(Printexc2.to_string e) |
1408 |
(Printexc2.to_string e) |
|
|
1409 |
*) () |
1452 |
end; |
1410 |
end; |
1453 |
(* Yay, now the new torrent is on disk! amazing! However, now we need to kill the dummy torrent |
1411 |
(* Yay, now the new torrent is on disk! amazing! However, now we need to kill the dummy torrent |
1454 |
and restart it with the fresh real torrent *) |
1412 |
and restart it with the fresh real torrent *) |
1455 |
|
1413 |
|
1456 |
(* it seems we need to use the dynamic interface... *) |
1414 |
(* it seems we need to use the dynamic interface... *) |
1457 |
lprintf_file_nl (as_file file) "cancelling metadata download "; |
1415 |
if !verbose then |
|
|
1416 |
lprintf_file_nl (as_file file) "cancelling metadata download "; |
1458 |
let owner = file.file_file.impl_file_owner in |
1417 |
let owner = file.file_file.impl_file_owner in |
1459 |
let group = file.file_file.impl_file_group in begin |
1418 |
let group = file.file_file.impl_file_group in begin |
1460 |
CommonInteractive.file_cancel (as_file file) owner ; |
1419 |
CommonInteractive.file_cancel (as_file file) owner ; |
1461 |
(* hack_op_file_cancel c.client_file; *) |
1420 |
(* hack_op_file_cancel c.client_file; *) |
1462 |
lprintf_file_nl (as_file file) "starting download from metadata torrent %s" newtorrentfile ; |
1421 |
if !verbose then |
1463 |
load_torrent_file newtorrentfile owner group; |
1422 |
lprintf_file_nl (as_file file) "starting download from metadata torrent %s" newtorrentfile ; |
|
|
1423 |
ignore(CommonNetwork.network_parse_url BTGlobals.network newtorrentfile owner group); |
1464 |
end; |
1424 |
end; |
|
|
1425 |
(try Sys.remove newtorrentfile with _ -> ()) |
1465 |
end; |
1426 |
end; |
1466 |
|
1427 |
|
1467 |
end |
1428 |
end |
1468 |
else begin |
1429 |
else begin |
1469 |
(* now ask for the next metadata piece, if any *) |
1430 |
(* now ask for the next metadata piece, if any *) |
1470 |
let module B = Bencode in |
1431 |
let module B = Bencode in |
1471 |
let nextpiece = (Int64.succ file.file_metadata_piece) in begin |
1432 |
let nextpiece = (Int64.succ file.file_metadata_piece) in begin |
1472 |
lprintf_file_nl (as_file file) "asking for the next piece %Ld" nextpiece; |
1433 |
if !verbose_msg_clients then |
|
|
1434 |
lprintf_file_nl (as_file file) "asking for the next piece %Ld" nextpiece; |
1473 |
send_extended_piece_request c nextpiece file; |
1435 |
send_extended_piece_request c nextpiece file; |
1474 |
end; |
1436 |
end; |
1475 |
end; |
1437 |
end; |
1476 |
|_ -> |
1438 |
|_ -> |
1477 |
lprintf_file_nl (as_file file) "unmatched extended subtype" ; |
1439 |
if !verbose_msg_clients then |
|
|
1440 |
lprintf_file_nl (as_file file) "unmatched extended subtype" ; |
1478 |
end; |
1441 |
end; |
1479 |
|
1442 |
|
1480 |
|
|
|
1481 |
| _ -> |
1443 |
| _ -> |
1482 |
lprintf_file_nl (as_file file) "Got extended other msg "; |
1444 |
if !verbose_msg_clients then |
|
|
1445 |
lprintf_file_nl (as_file file) "Got extended other msg "; |
1483 |
end; |
1446 |
end; |
1484 |
|
1447 |
|
1485 |
| DHT_Port port -> |
1448 |
| DHT_Port port -> |