xtap is a Rust crate used with LD_PRELOAD to hook bind() and connect() syscalls to reroute v4 and v6 addresses with environment variables.
- Intercepts
bind()andconnect()syscalls using runtime hooking viadlsym - Automatically binds sockets to an interface
- Supports IPv4 and IPv6
- Configurable via multiple environment variables
- Minimal runtime overhead
xtap should only be given one environment variable. If more are given the interface will take precedence over IP and be ignored. When an IP is specified it will use the corresponding interface.
| Variable | Example |
|---|---|
XTAP_IFACE |
eth0 |
XTAP_INTERFACE |
tun0 |
XTAP_IP |
192.168.1.100 |
BIND_IP |
10.0.0.2 |
BIND_SRC |
10.0.0.3 |
XTAP_ADDR |
172.16.0.1 |
XTAP_BIND |
192.168.2.1 |
XTAP_BIND_ADDR |
192.168.3.1 |
cargo build --release
-
Set environment variables to specify interface or IP:
export XTAP_IFACE=eth0 export XTAP_IP=192.168.1.100 -
Run your binary using
LD_PRELOAD. Socket operations will be transparently bound as configured.connect()calls will automatically inject abind()andsetsockopt()call.LD_PRELOAD=/usr/lib/xtap/libxtap.so \ XTAP_IFACE=tun0 \ curl -4 -L "http://cloudflare.com/cdn-cgi/trace" -
If integrating the
xtapcrate, ensure your binary uses the providedbind()andconnect()hooks:pub fn bind(sockfd: i32, addr: *const sockaddr, addrlen: socklen_t) -> i32 { xtap::hooks::Bind::bind(sockfd, addr, addrlen) } pub fn connect(sockfd: i32, addr: *const sockaddr, addrlen socklen_t) -> i32 { xtap::hooks::Connect::connect(sockfd, addr, addrlen) }
Enable debug logging by compiling with debug assertions:
cargo build
Run each test per-process to prevent race conditions from environment variables and side effects caused by global static mutations:
cargo nextest run --status-level=all
libcandsocket2for socket handling and syscall hookingnetdevto discover network interfacesscopeguardfor scoped cleanup and guard patterns
This project is available under multiple licenses. You may choose to use it under one of the following:
Inspired by bindhack
Contributions are welcome! Open issues or PRs on the repository.