nl_wireguard/lib.rs
1// SPDX-License-Identifier: MIT
2
3//! This crate provides methods to manipulate wireguard link via the generic
4//! netlink protocol.
5//!
6//! To query wireguard interface:
7//!
8//! ```no_run
9//! async fn print_wireguard_config(
10//! iface_name: &str,
11//! ) -> Result<(), Box<dyn std::error::Error>> {
12//! let (conn, mut handle, _) = nl_wireguard::new_connection()?;
13//! tokio::spawn(conn);
14//!
15//! println!("{:?}", handle.get_by_name(iface_name).await?);
16//! Ok(())
17//! }
18//! ```
19//!
20//! To set wireguard configuration.
21//! You need to use `rtnetlink` crate to create a interface with `wireguard`
22//! interface type before.
23//!
24//! ```no_run
25//! use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
26//!
27//! use nl_wireguard::{
28//! WireguardIpAddress, WireguardParsed, WireguardPeerParsed
29//! };
30//!
31//! async fn set_wireguard_config(
32//! iface_name: &str,
33//! ) -> Result<(), Box<dyn std::error::Error>> {
34//! let mut peer_config = WireguardPeerParsed::default();
35//! peer_config.endpoint = Some(SocketAddr::new(
36//! IpAddr::V4(Ipv4Addr::new(10, 10, 10, 1)),
37//! 51820,
38//! ));
39//! peer_config.public_key =
40//! Some("8bdQrVLqiw3ZoHCucNh1YfH0iCWuyStniRr8t7H24Fk=".to_string());
41//! peer_config.allowed_ips = Some(vec![
42//! WireguardIpAddress {
43//! ip_addr: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
44//! prefix_length: 0,
45//! },
46//! WireguardIpAddress {
47//! ip_addr: IpAddr::V6(Ipv6Addr::UNSPECIFIED),
48//! prefix_length: 0,
49//! },
50//! ]);
51//!
52//! let mut config = WireguardParsed::default();
53//! config.iface_name = Some(iface_name.to_string());
54//! config.public_key =
55//! Some("JKossUAjywXuJ2YVcaeD6PaHs+afPmIthDuqEVlspwA=".to_string());
56//! config.private_key =
57//! Some("6LTHiAM4vgKEgi5vm30f/EBIEWFDmySkTc9EWCcIqEs=".to_string());
58//! config.listen_port = Some(51820);
59//! config.fwmark = Some(0);
60//! config.peers = Some(vec![peer_config]);
61//!
62//! let (conn, mut handle, _) = nl_wireguard::new_connection()?;
63//! tokio::spawn(conn);
64//! handle.set(config).await?;
65//! Ok(())
66//! }
67//! ```
68
69mod connection;
70mod error;
71mod handle;
72mod parsed;
73mod peer_parsed;
74
75// Re-export netlink-packet-wireguard data types allowing crate use to
76// depend on this crate only for full functionality.
77pub use netlink_packet_wireguard::{
78 WireguardAddressFamily, WireguardAllowedIp, WireguardAllowedIpAttr,
79 WireguardAttribute, WireguardCmd, WireguardMessage, WireguardPeer,
80 WireguardPeerAttribute, WireguardTimeSpec,
81};
82
83#[cfg(feature = "tokio_socket")]
84pub use self::connection::new_connection;
85pub use self::{
86 connection::new_connection_with_socket,
87 error::{ErrorKind, WireguardError},
88 handle::WireguardHandle,
89 parsed::WireguardParsed,
90 peer_parsed::{WireguardIpAddress, WireguardPeerParsed},
91};