mnl_sys/
lib.rs

1// Copyright 2025 Mullvad VPN AB.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Low level FFI bindings to [`libmnl`]. A minimalistic user-space library oriented to Netlink
10//! developers. See [`mnl`] for a higher level safe abstraction.
11//!
12//! These bindings were generated with bindgen. See the `generate_bindings.sh` script in the
13//! repository.
14//!
15//! # Linking to libmnl
16//!
17//! By default this crate uses pkg-config to find and link to [`libmnl`]. To manually configure
18//! where to look for the library, set the environment variable `LIBMNL_LIB_DIR` to point to the
19//! directory where `libmnl.so` or `libmnl.a` resides.
20//!
21//! # Selecting version of `libmnl`
22//!
23//! This crate has bindings for multiple versions of [`libmnl`]. All bindings are generated by
24//! [`bindgen`] via the `generate_bindings.sh` script in this repository.
25//!
26//! Only one version of `libmnl` can be exposed via this crate. By default the crate exports the
27//! bindings for the oldest supported version (`libmnl-1.0.3`). To get newer versions activate the
28//! corresponding features. See `Cargo.toml` for available features/versions.
29//!
30//! So for example, to get bindings to `libmnl-1.0.4` depend on this crate like this:
31//! ```toml
32//! [dependencies]
33//! mnl-sys = { version = "0.1", features = ["mnl-1-0-4"] }
34//! ```
35//!
36//! [`libmnl`]: https://netfilter.org/projects/libmnl/
37//! [`mnl`]: https://crates.io/crates/mnl
38//! [`bindgen`]: https://crates.io/crates/bindgen
39
40#![no_std]
41#![cfg(target_os = "linux")]
42
43pub use libc;
44
45#[allow(non_snake_case)]
46pub fn MNL_SOCKET_BUFFER_SIZE() -> libc::c_long {
47    const MAX: libc::c_long = 8192;
48    let pagesize = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
49    ::core::cmp::min(pagesize, MAX)
50}
51
52#[allow(non_snake_case)]
53pub fn MNL_ALIGN(len: i32) -> i32 {
54    ((len) + MNL_ALIGNTO - 1) & !(MNL_ALIGNTO - 1)
55}
56
57#[allow(non_camel_case_types)]
58mod bindings;
59pub use crate::bindings::*;