Specify interface as cmd arg

This commit is contained in:
2024-08-24 14:17:01 +02:00
parent 5950058c76
commit a7ddc6227a
3 changed files with 227 additions and 2 deletions

View File

@@ -11,7 +11,12 @@ use dhcproto::{
ORO,
},
};
use std::net::SocketAddrV6;
use ipnetwork;
use pnet;
use std::{
env,
net::SocketAddrV6,
};
use tokio::net::UdpSocket;
const scope_id: u32 = 15;
@@ -23,7 +28,31 @@ const DHCP_Relay_Agent_and_Server_port: u16 = 547;
#[tokio::main]
async fn main() {
let socket_addr = SocketAddrV6::new("fe80::e3cf:4290:9165:505d".parse().unwrap(), DHCP_Client_port, 0, scope_id);
let mut args = env::args();
args.next(); // Skip executable name
// First argument is interface name
let interface_name = args.next().expect("No interface name specified");
let all_interfaces = pnet::datalink::interfaces();
let selected_interface = all_interfaces
.iter()
.find(|i| i.name == interface_name)
.expect(format!("No interface found with name: {}", interface_name).as_str());
let ipv6_addresses = selected_interface.ips
.iter()
.filter_map(|a| match a {
ipnetwork::IpNetwork::V6(a) => Some(a),
_ => None,
});
let mut ipv6_link_local_addresses = ipv6_addresses
.filter(|a| a.is_subnet_of("fe80::/10".parse().unwrap()));
// Just take the first address found on the interface
let selected_address = ipv6_link_local_addresses.next()
.expect("No IPv6 link local address assigned to this interface");
let socket_addr = SocketAddrV6::new(selected_address.ip(), DHCP_Client_port, 0, selected_interface.index.clone());
let sock = UdpSocket::bind(socket_addr).await.unwrap();
let remote_addr = SocketAddrV6::new(All_DHCP_Relay_Agents_and_Servers.parse().unwrap(), DHCP_Relay_Agent_and_Server_port, 0, scope_id);