├── .gitignore ├── .merlin ├── README.md ├── config.ml └── unikernel.ml /.gitignore: -------------------------------------------------------------------------------- 1 | .mirage.config 2 | Makefile 3 | _build 4 | key_gen.ml 5 | main.ml 6 | main.native 7 | mirage*opam 8 | myocamlbuild.ml 9 | chill_netcat 10 | -------------------------------------------------------------------------------- /.merlin: -------------------------------------------------------------------------------- 1 | PKG lwt mirage mirage-types mirage-types-lwt tcpip mirage-http conduit.mirage mirage-console 2 | PKG cohttp mirage-net-unix mirage-unix mirage-clock-unix functoria 3 | PKG tcpip.ethif tcpip.arpv4 tls logs mirage-logs 4 | PKG magic-mime 5 | S **/ 6 | B **/_build/** 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Chill-Netcat (work in progress) 2 | 3 | A deliberately limited [Netcat](https://en.wikipedia.org/wiki/Netcat) implementation ([UDP](https://en.wikipedia.org/wiki/User_Datagram_Protocol) only). 4 | 5 | An exercise in building a network enabled [systems program](https://en.wikipedia.org/wiki/System_programming) / [unikernel](https://en.wikipedia.org/wiki/Unikernel) with [OCaml](http://ocaml.org) / [MirageOS](https://mirage.io). 6 | 7 | With minimal network related dependencies included the resulting binary has a size of ~3.3MB (on MacOS). 8 | 9 | This project is based on the highly instructional ["Hello MirageOS World"](https://mirage.io/wiki/hello-world) guide and [mirage-skeleton](https://github.com/mirage/mirage-skeleton) code examples. 10 | 11 | You can also watch [my talk](https://youtu.be/37cLNsnLozE) at the first Vienna ReasonML meetup. 12 | 13 | # Setup 14 | 15 | Check the official [MirageOS installation](https://mirage.io/wiki/install) guide. 16 | 17 | # Configure and build 18 | 19 | Configure (e.g. MacOS): 20 | ```shell 21 | mirage configure -t macosx 22 | ``` 23 | 24 | Get and build dependencies: 25 | ```shell 26 | make depend 27 | ``` 28 | 29 | Build binary: 30 | ```shell 31 | make 32 | ``` 33 | 34 | # Run 35 | 36 | On MacOS: 37 | ```shell 38 | ./chill_netcat 39 | ``` 40 | 41 | On Xen (not yet..) 42 | -------------------------------------------------------------------------------- /config.ml: -------------------------------------------------------------------------------- 1 | open Mirage 2 | 3 | let key = 4 | let doc = Key.Arg.info ~doc: "Listen on port." ["port"] in 5 | Key.(create "port" Arg.(opt string "9000" doc)) 6 | 7 | let main = 8 | foreign 9 | ~keys:[Key.abstract key] 10 | "Unikernel.ChillNetcat" 11 | (console @-> network @-> ethernet @-> ipv4 @-> job) 12 | 13 | let ethif = etif default_network 14 | 15 | let ipv4 = 16 | let config = { 17 | network = Ipaddr.V4.Prefix.of_string_exn "127.0.0.0/24", Ipaddr.V4.of_string_exn "127.0.0.1" ; 18 | gateway = Ipaddr.V4.of_string "192.168.1.1"; 19 | } in 20 | create_ipv4 ~config ethif (Mirage.farp ethif) 21 | 22 | let () = 23 | register "chill-netcat" [ main $ default_console $ default_network $ ethif $ ipv4 ] 24 | -------------------------------------------------------------------------------- /unikernel.ml: -------------------------------------------------------------------------------- 1 | open Lwt.Infix 2 | open Mirage_types_lwt 3 | 4 | module ChillNetcat (C: CONSOLE) (N: NETWORK) (E: ETHIF) (I: IPV4) = struct 5 | let start c n e i = 6 | let port = Key_gen.port () in 7 | N.listen n 8 | (E.input 9 | ~arpv4:(fun _ -> C.log c "ARPV4 package registered") 10 | ~ipv4:(I.input 11 | ~tcp:(fun ~src:_ ~dst:_ _ -> C.log c "TCP package registered") 12 | ~udp:(fun ~src:_ ~dst:_ packet -> C.write c packet 13 | >>= function 14 | | Result.Ok () -> Lwt.return_unit 15 | | Result.Error _ -> C.log c "CONSOLE WRITE ERROR") 16 | ~default:(fun ~proto ~src:_ ~dst:_ _data -> C.log c (string_of_int proto)) 17 | i) 18 | ~ipv6:(fun _ -> C.log c "IPV6 package registered") 19 | e) 20 | >>= function 21 | | Result.Ok () -> C.log c "done!" 22 | | Result.Error _ -> C.log c "failed" 23 | end 24 | --------------------------------------------------------------------------------