3 releases

0.1.2 May 21, 2024
0.1.1 Nov 16, 2023
0.1.0 Apr 19, 2023

#881 in Network programming

Download history 112/week @ 2024-07-22 175/week @ 2024-07-29 248/week @ 2024-08-05 218/week @ 2024-08-12 381/week @ 2024-08-19 337/week @ 2024-08-26 443/week @ 2024-09-02 414/week @ 2024-09-09 299/week @ 2024-09-16 481/week @ 2024-09-23 361/week @ 2024-09-30 440/week @ 2024-10-07 149/week @ 2024-10-14 144/week @ 2024-10-21 244/week @ 2024-10-28 309/week @ 2024-11-04

911 downloads per month

Custom license

48KB
1K SLoC

github crates-io rust-docs Dependency Status


conntrack-rs

An API providing access to the Conntrack subsystem of the Linux kernel written in rust 🦀

This library provides access to the conntrack subsystem in the linux kernel leveraging netlink support via the neli library.

The current version only supplies Dump() functionality for the Conntrack table. Leveraging the conntrack-tools utility in linux, the Dump() behavior is equivalent to: conntrack -L. Most of the model and attribute parsing supported in this library extends beyond the dump() command, which allows this library to eventually cover the full feature set of the conntrack subsystem.

You can enable byte and packet counters using sysctl -w net.netfilter.nf_conntrack_acct=1

Privileges

You need the CAP_NET_ADMIN capability in order to allow your application to receive events from and to send commands to kernel-space, excepting the conntrack table dumping operation.

WSL2 Conntrack

Note that in order to enable connection tracking via conntrack on WSL2, you'll need to add the following iptable entry:

iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

Example

use conntrack::*;

fn main() -> Result<()> {
    // Create the Conntrack table via netfilter socket syscall
    let mut ct = Conntrack::connect()?;

    // Dump conntrack table as a Vec<Flow>
    let flows = ct.dump()?;

    for flow in flows {
        log::info!("{flow:?}");
    }

    Ok(())
}

Dependencies

~1.7–2.7MB
~52K SLoC