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