Network utilities for Eio-based OCaml applications
OCaml 92.6%
Dune 2.7%
Other 4.7%
3 1 0

Clone this repository

https://tangled.org/gazagnaire.org/ocaml-net https://tangled.org/did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-net
git@git.recoil.org:gazagnaire.org/ocaml-net git@git.recoil.org:did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-net

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

net#

Network utilities for Eio-based OCaml applications.

Overview#

This library provides network utilities for Eio applications:

  • Icmp: ICMP ping using unprivileged sockets (no root required)
  • Tcp: TCP connection testing and banner grabbing
  • Arp: ARP table lookup (find devices on local network)
  • Http: HTTP client with timeout and TLS support
  • Cache: TTL-based caching with jitter for scan results

Installation#

opam install net

Usage#

ICMP Ping#

let () =
  Eio_main.run @@ fun env ->
  let clock = Eio.Stdenv.clock env in
  let icmp = Net.Icmp.create ~clock in
  let result = Net.Icmp.ping icmp ~count:3 "192.168.1.1" in
  if result.reachable then
    Printf.printf "Host reachable, TTL=%d, latency=%.2fms\n"
      (Option.value ~default:0 result.ttl)
      (Option.value ~default:0.0 result.latency_ms)

TCP Connection Test#

let () =
  Eio_main.run @@ fun env ->
  let net = Eio.Stdenv.net env in
  let clock = Eio.Stdenv.clock env in
  let tcp = Net.Tcp.create ~net ~clock in
  if Net.Tcp.connect tcp "192.168.1.1" 22 then
    Printf.printf "SSH port is open\n"

ARP Table Lookup#

let () =
  Eio_main.run @@ fun env ->
  let proc = Eio.Stdenv.process_mgr env in
  let arp = Net.Arp.create ~proc_mgr:proc in
  let entries = Net.Arp.get_table arp in
  List.iter (fun e ->
    Printf.printf "%s -> %s\n" e.ip e.mac
  ) entries

HTTP Client#

let () =
  Eio_main.run @@ fun env ->
  let net = Eio.Stdenv.net env in
  let clock = Eio.Stdenv.clock env in
  Eio.Switch.run @@ fun sw ->
  let http = Net.Http.create ~net ~clock in
  match Net.Http.get_body http ~sw ~https:true "https://example.com" with
  | Ok body -> print_endline body
  | Error (`Msg e) -> Printf.eprintf "Error: %s\n" e

Requirements#

  • OCaml 5.1+
  • Eio
  • For ICMP: unprivileged ICMP sockets (Linux 3.0+, macOS)
    • On Linux: sysctl net.ipv4.ping_group_range="0 65535"

License#

MIT License. See LICENSE.md for details.