From ea463573d490d1c44c57ec87b63d3d5d08489e96 Mon Sep 17 00:00:00 2001
From: ygrek <ygrek@autistici.org>
Date: Sun, 9 Mar 2014 11:43:21 +0800
Subject: [PATCH] BT/DHT: update public router addresses

bt_dht_node: specify bootstrap peers from command-line
---
 src/networks/bittorrent/bTComplexOptions.ml |   25 +++++++++++++++++++++++--
 src/networks/bittorrent/bTOptions.ml        |    3 +--
 tools/bt_dht_node.ml                        |   18 ++++++++++++++----
 3 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/src/networks/bittorrent/bTComplexOptions.ml b/src/networks/bittorrent/bTComplexOptions.ml
index 0d8095d..eee1d9f 100644
--- a/src/networks/bittorrent/bTComplexOptions.ml
+++ b/src/networks/bittorrent/bTComplexOptions.ml
@@ -314,12 +314,33 @@ let save _ =
 
 let guptime () = !!guptime - !diff_time
 
-let _ =
+let rec update_options () =
+  let update v =
+      lprintf_nl "Updating options to version %i" v;
+      options_version =:= v;
+      update_options ()
+  in
+
+  match !!options_version with
+  | 0 ->
+      let present = ref false in
+      (* drop obsolete addresses, add new *)
+      dht_bootstrap_nodes =:= List.filter (function
+        | "router.utorrent.com", 6881 -> false
+        | "router.transmission.com", 6881 -> false
+        | "router.bittorrent.com", 8991 -> present := true; true
+        | _ -> true) !!dht_bootstrap_nodes;
+      if not !present then
+        dht_bootstrap_nodes =:= ("router.bittorrent.com", 8991) :: !!dht_bootstrap_nodes;
+      update 1
+  | _ -> ()
+
+let () =
   network.op_network_file_of_option <- value_to_file;
   file_ops.op_file_to_option <- file_to_value;
 
   (* Shut up message "Network.save/load_complex_options not implemented by BitTorrent" *)
   network.op_network_load_complex_options <- load;
   network.op_network_save_complex_options <- save;
-  network.op_network_update_options <- (fun _ -> ());
+  network.op_network_update_options <- update_options;
   network.op_network_save_sources <- (fun _ -> ())
diff --git a/src/networks/bittorrent/bTOptions.ml b/src/networks/bittorrent/bTOptions.ml
index 64064c7..2373777 100644
--- a/src/networks/bittorrent/bTOptions.ml
+++ b/src/networks/bittorrent/bTOptions.ml
@@ -177,7 +177,6 @@ let dht_bootstrap_nodes = define_option bittorrent_section ["dht_bootstrap_nodes
   (list_option addr_option)
   [
     "service.ygrek.org.ua",6881;
-    "router.utorrent.com",6881;
-    "router.transmission.com",6881;
+    "router.bittorrent.com", 8991;
   ]
 
diff --git a/tools/bt_dht_node.ml b/tools/bt_dht_node.ml
index 90dd844..abf08ae 100644
--- a/tools/bt_dht_node.ml
+++ b/tools/bt_dht_node.ml
@@ -20,6 +20,15 @@ let store file (t:Kademlia.table) =
   with exn ->
     lprintf_nl ~exn "write to %S failed" file; Sys.remove temp
 
+let parse_peer s =
+  try
+    match String2.split s ':' with
+    | [addr;port] -> addr, int_of_string port
+    | _ -> raise Not_found
+  with _ ->
+    Printf.eprintf "E: bad peer %S, expecting <addr>:<port>\n%!" s;
+    exit 2
+
 let init file = try load file with _ -> Kademlia.create ()
 
 let run_queries =
@@ -42,8 +51,9 @@ let run_queries =
 let () =
   Random.self_init ();
   try
-    match Sys.argv with
-    | [|_;file;port|] ->
+    match List.tl (Array.to_list Sys.argv) with
+    | file::port::peers ->
+      let peers = List.map parse_peer peers in
       let bw = UdpSocket.new_bandwidth_controler 
         (TcpBufferedSocket.create_write_bandwidth_controler "UNLIMIT" 0) in
       let dht = start (init file) (int_of_string port) bw in
@@ -53,10 +63,10 @@ let () =
       Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> show dht));
       BasicSocket.add_infinite_timer 1800. (fun () -> run_queries dht);
       BasicSocket.add_infinite_timer 3600. (fun () -> store file dht.M.rt);
-      let routers = ["router.utorrent.com", 6881; "router.transmission.com",6881] in
+      let routers = ["router.bittorrent.com", 8991] @ peers in
       bootstrap dht ~routers;
       BasicSocket.loop ()
-    | _ -> Printf.eprintf "Usage : %s <storage> <port>\n" Sys.argv.(0)
+    | _ -> Printf.eprintf "Usage : %s <storage> <port> [<peer_addr:port>]*\n" Sys.argv.(0)
   with
     exn -> lprintf_nl "main : %s" (Printexc.to_string exn)
 
-- 
1.7.6.6.GIT

