From 583c55c289bdd70df3452022fa0b8e04994cd0e6 Mon Sep 17 00:00:00 2001 From: ygrek Date: Tue, 23 Mar 2010 20:02:40 +0200 Subject: [PATCH] separate udp trackers code to bTUdpTracker.mlp --- src/networks/bittorrent/bTClients.ml | 2 +- .../bittorrent/{bTProtocol.mlp => bTProtocol.ml} | 92 ------------------ src/networks/bittorrent/bTUdpTracker.mlp | 104 +++++++++++++++++++++ 3 files changed, 105 insertions(+), 93 deletions(-) rename src/networks/bittorrent/{bTProtocol.mlp => bTProtocol.ml} (88%) create mode 100644 src/networks/bittorrent/bTUdpTracker.mlp diff --git a/src/networks/bittorrent/bTClients.ml b/src/networks/bittorrent/bTClients.ml index 87beaa85..dda52336 100644 --- a/src/networks/bittorrent/bTClients.ml +++ b/src/networks/bittorrent/bTClients.ml @@ -97,7 +97,7 @@ let resume_clients_hook = ref (fun _ -> assert false) include struct (* open modules locally *) -open UdpTracker +open BTUdpTracker open UdpSocket let string_of_event = function diff --git a/src/networks/bittorrent/bTProtocol.mlp b/src/networks/bittorrent/bTProtocol.ml similarity index 88% rename from src/networks/bittorrent/bTProtocol.mlp rename to src/networks/bittorrent/bTProtocol.ml index 1abd26ce..4da844e7 100644 --- a/src/networks/bittorrent/bTProtocol.mlp +++ b/src/networks/bittorrent/bTProtocol.ml @@ -637,95 +637,3 @@ let send_init client_uid file_id sock = let s = Buffer.contents buf in write_string sock s -(** UDP trackers - http://www.bittorrent.org/beps/bep_0015.html *) -module UdpTracker = struct - -open Bitstring - -let of_bits = string_of_bitstring -let bits = bitstring_of_string - -(* -Choose a random transaction ID. -Fill the connect request structure. -Send the packet. -*) -let connect_request txn = - of_bits ( BITSTRING { 0x41727101980L : 64 ; 0l : 32 ; txn : 32 } ) - -exception Error of string - -let fail fmt = Printf.ksprintf (fun s -> raise (Error s)) fmt - -(* -Receive the packet. -Check whether the packet is at least 16 bytes. -Check whether the transaction ID is equal to the one you chose. -Check whether the action is connect. -Store the connection ID for future use. -*) -let connect_response s exp_txn = - bitmatch bits s with - | { 0l : 32 ; txn : 32 ; conn_id : 64 } -> - if txn = exp_txn then conn_id else fail "error connect_response txn %ld expected %ld" txn exp_txn - | { 3l : 32 ; txn : 32 ; msg : -1 : string } -> fail "error connect_response txn %ld : %s" txn msg - | { _ } -> fail "error connect_response" - -(* -Choose a random transaction ID. -Fill the announce request structure. -Send the packet. -*) -let announce_request conn txn ~info_hash ~peer_id (downloaded,left,uploaded) event ?(key=0l) ~numwant port = - of_bits (BITSTRING { - conn : 64 ; - 1l : 32 ; - txn : 32 ; - info_hash : 20 * 8 : string; - peer_id : 20 * 8 : string; - downloaded : 64 ; - left : 64 ; - uploaded : 64 ; - event : 32 ; - 0l : 32 ; (* ip *) - key : 32 ; (* key *) - numwant : 32 ; (* key *) - port : 16 }) - -(* -Receive the packet. -Check whether the packet is at least 20 bytes. -Check whether the transaction ID is equal to the one you chose. -Check whether the action is announce. -Do not announce again until interval seconds have passed or an event has occurred. -*) -let announce_response s exp_txn = - let rec clients rest l = - bitmatch rest with - | { ip : 32 ; port : 16 ; rest : -1 : bitstring } -> clients rest ((ip,port)::l) - | { _ } -> l - in - bitmatch bits s with - | { 1l : 32 ; txn : 32 ; interval : 32 ; leechers : 32 ; seeders : 32 ; - rest : -1 : bitstring } -> - if txn = exp_txn then - (interval,clients rest []) - else - fail "error announce_response txn %ld expected %ld" txn exp_txn - | { 3l : 32 ; txn : 32 ; msg : -1 : string } -> fail "error announce_response txn %ld : %s" txn msg - | { _ } -> fail "error announce_response" - -(* -If the tracker encounters an error, it might send an error packet. -Receive the packet. -Check whether the packet is at least 8 bytes. -Check whether the transaction ID is equal to the one you chose. -*) -let error_response s = - bitmatch bits s with - | { 3l : 32 ; txn : 32 ; msg : -1 : string } -> Some (txn, msg) - | { _ } -> None - -end - diff --git a/src/networks/bittorrent/bTUdpTracker.mlp b/src/networks/bittorrent/bTUdpTracker.mlp new file mode 100644 index 00000000..c086bf17 --- /dev/null +++ b/src/networks/bittorrent/bTUdpTracker.mlp @@ -0,0 +1,104 @@ + +(* +open BasicSocket +open CommonTypes +open Printf2 +open CommonOptions +open Options +open Md4 +open CommonGlobals +open BigEndian +open TcpBufferedSocket +open AnyEndian +open BTTypes +*) + +(** UDP trackers + http://www.bittorrent.org/beps/bep_0015.html *) + +open Bitstring + +let of_bits = string_of_bitstring +let bits = bitstring_of_string + +(* +Choose a random transaction ID. +Fill the connect request structure. +Send the packet. +*) +let connect_request txn = + of_bits ( BITSTRING { 0x41727101980L : 64 ; 0l : 32 ; txn : 32 } ) + +exception Error of string + +let fail fmt = Printf.ksprintf (fun s -> raise (Error s)) fmt + +(* +Receive the packet. +Check whether the packet is at least 16 bytes. +Check whether the transaction ID is equal to the one you chose. +Check whether the action is connect. +Store the connection ID for future use. +*) +let connect_response s exp_txn = + bitmatch bits s with + | { 0l : 32 ; txn : 32 ; conn_id : 64 } -> + if txn = exp_txn then conn_id else fail "error connect_response txn %ld expected %ld" txn exp_txn + | { 3l : 32 ; txn : 32 ; msg : -1 : string } -> fail "error connect_response txn %ld : %s" txn msg + | { _ } -> fail "error connect_response" + +(* +Choose a random transaction ID. +Fill the announce request structure. +Send the packet. +*) +let announce_request conn txn ~info_hash ~peer_id (downloaded,left,uploaded) event ?(key=0l) ~numwant port = + of_bits (BITSTRING { + conn : 64 ; + 1l : 32 ; + txn : 32 ; + info_hash : 20 * 8 : string; + peer_id : 20 * 8 : string; + downloaded : 64 ; + left : 64 ; + uploaded : 64 ; + event : 32 ; + 0l : 32 ; (* ip *) + key : 32 ; (* key *) + numwant : 32 ; (* key *) + port : 16 }) + +(* +Receive the packet. +Check whether the packet is at least 20 bytes. +Check whether the transaction ID is equal to the one you chose. +Check whether the action is announce. +Do not announce again until interval seconds have passed or an event has occurred. +*) +let announce_response s exp_txn = + let rec clients rest l = + bitmatch rest with + | { ip : 32 ; port : 16 ; rest : -1 : bitstring } -> clients rest ((ip,port)::l) + | { _ } -> l + in + bitmatch bits s with + | { 1l : 32 ; txn : 32 ; interval : 32 ; leechers : 32 ; seeders : 32 ; + rest : -1 : bitstring } -> + if txn = exp_txn then + (interval,clients rest []) + else + fail "error announce_response txn %ld expected %ld" txn exp_txn + | { 3l : 32 ; txn : 32 ; msg : -1 : string } -> fail "error announce_response txn %ld : %s" txn msg + | { _ } -> fail "error announce_response" + +(* +If the tracker encounters an error, it might send an error packet. +Receive the packet. +Check whether the packet is at least 8 bytes. +Check whether the transaction ID is equal to the one you chose. +*) +let error_response s = + bitmatch bits s with + | { 3l : 32 ; txn : 32 ; msg : -1 : string } -> Some (txn, msg) + | { _ } -> None + -- 2.11.4.GIT