Advertisement
Guest User

Untitled

a guest
Oct 10th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.56 KB | None | 0 0
  1. open V1_LWT
  2. open Lwt
  3.  
  4. let red fmt    = Printf.sprintf ("\027[31m"^^fmt^^"\027[m")
  5. let green fmt  = Printf.sprintf ("\027[32m"^^fmt^^"\027[m")
  6. let yellow fmt = Printf.sprintf ("\027[33m"^^fmt^^"\027[m")
  7. let blue fmt   = Printf.sprintf ("\027[36m"^^fmt^^"\027[m")
  8.  
  9. module Main (C: CONSOLE) (N: NETWORK) = struct
  10.  
  11.   module E = Ethif.Make(N)
  12.   module I = Ipv4.Make(E)
  13.   module U = Udpv4.Make(I)
  14.   module T = Tcpv4.Flow.Make(I)(OS.Time)(Clock)(Random)
  15.   module D = Dhcp_clientv4.Make(C)(OS.Time)(Random)(E)(I)(U)
  16.  
  17.   let or_error c name fn t =
  18.     fn t
  19.     >>= function
  20.     | `Error e -> fail (Failure ("Error starting " ^ name))
  21.     | `Ok t -> return t
  22.  
  23.   let start c net =
  24.     or_error c "Ethif" E.connect net
  25.     >>= fun e ->
  26.  
  27.     or_error c "Ipv4" I.connect e
  28.     >>= fun i ->
  29.  
  30.     I.set_ipv4 i (Ipaddr.V4.of_string_exn "10.0.0.2")
  31.     >>= fun () ->
  32.     I.set_ipv4_netmask i (Ipaddr.V4.of_string_exn "255.255.255.0")
  33.     >>= fun () ->
  34.     I.set_ipv4_gateways i [Ipaddr.V4.of_string_exn "10.0.0.1"]
  35.     >>= fun () ->
  36.     or_error c "UDPv4" U.connect i
  37.     >>= fun udp ->
  38.  
  39.     let dhcp, offers = D.create c i udp in
  40.     or_error c "TCPv4" T.connect i
  41.     >>= fun tcp ->
  42.  
  43.     N.listen net (
  44.       E.input
  45.         ~ipv4:(
  46.           I.input
  47.             ~tcp:(
  48.               T.input tcp ~listeners:
  49.                 (function
  50.                   | 80 -> Some (fun flow ->
  51.                       let dst, dst_port = T.get_dest flow in
  52.                       C.log_s c
  53.                         (green "new tcp from %s %d"
  54.                           (Ipaddr.V4.to_string dst) dst_port
  55.                         )
  56.                       >>= fun () ->
  57.  
  58.                       T.read flow
  59.                       >>= function
  60.                       | `Ok b ->
  61.                         C.log_s c
  62.                           (yellow "read: %d\n%s"
  63.                             (Cstruct.len b) (Cstruct.to_string b)
  64.                           )
  65.                         >>= fun () ->
  66.                         T.close flow
  67.                       | `Eof -> C.log_s c (red "read: eof")
  68.                       | `Error e -> C.log_s c (red "read: error"))
  69.                   | _ -> None
  70.                 ))
  71.             ~udp:(
  72.               U.input ~listeners:
  73.                 (fun ~dst_port ->
  74.                    C.log c (blue "udp packet on port %d" dst_port);
  75.                    D.listen dhcp ~dst_port)
  76.                 udp
  77.             )
  78.             ~default:(fun ~proto ~src ~dst _ -> return ())
  79.             i
  80.         )
  81.         ~ipv6:(fun b -> C.log_s c (yellow "ipv6")) e
  82.     )
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement