(* Wardialer *) (* $Id: wardialer.ml,v 1.1 2003/01/12 15:33:00 berke Exp $ *) (* By Berke Durak *) open Unix let modem_device = "/dev/cua01" let escape_string s = let m = String.length s in let b = Buffer.create m in for i = 0 to m - 1 do Buffer.add_string b (Char.escaped s.[i]) done; Buffer.contents b let swap a i j = let x = a.(i) in a.(i) <- a.(j); a.(j) <- x let shuffle_array a = let m = Array.length a in for i = 0 to m - 2 do let j = i + (Random.int (m - 1 - i)) in swap a i j done let _ = let fd = openfile modem_device [O_RDWR] 0 in let ta = tcgetattr fd in let ta' = { ta with c_istrip = false; c_inlcr = false; c_igncr = false; c_icrnl = false; c_ixoff = false; c_obaud = 115200; c_ibaud = 115200; c_csize = 8; c_cstopb = 1; c_cread = true; c_parenb = false; c_hupcl = true; c_clocal = false; c_isig = false; c_icanon = false; c_noflsh = true; c_echo = false; c_echoe = false; c_echok = false; c_echonl = false; c_vmin = 4096; c_vtime = 5; } in tcsetattr fd TCSAFLUSH ta'; let ic = in_channel_of_descr fd and oc = out_channel_of_descr fd and fl = Pervasives.flush in let expect x = let y = input_line ic in Printf.eprintf "modem >>> [%s]\n" (escape_string y); fl Pervasives.stderr; if not (List.mem y x) then failwith ("Not expecting ["^(escape_string y)^"]") and usleep x = ignore (select [] [] [] x) in let out x = Printf.eprintf "[%s] >>> modem\n" (escape_string x); fl Pervasives.stderr; output_string oc x; fl oc; ignore (input_line ic) in out "ATZ\r\n"; expect ["OK\r"]; out "ATM0S40=2\r\n"; expect ["OK\r"]; Random.self_init (); let a = Array.init 100 (fun x -> Printf.sprintf "%04d" (2000 + x)) in shuffle_array a; Array.iter (fun x -> Printf.printf "%s\n" x) a; fl Pervasives.stdout; while true do Array.iter (fun n -> out (Printf.sprintf "ATDT%s\r\n" n); usleep 4.0; out "\r\n"; expect ["NO CARRIER\r";"BUSY\r"]; usleep (1.0 +. (Random.float 3.0))) a done; tcsetattr fd TCSAFLUSH ta; close fd