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"
- On Linux:
License#
MIT License. See LICENSE.md for details.