Skip to content

Commit 221e221

Browse files
committed
Enhance error handling
1 parent 9c32062 commit 221e221

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/main.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ use std::process;
1212
use std::thread;
1313
use std::time::Instant;
1414

15+
fn is_root_user() -> bool {
16+
std::env::var("USER").unwrap_or(String::from("")) == String::from("root")
17+
}
18+
1519
fn main() {
1620

17-
let username = std::env::var("USER").unwrap_or(String::from(""));
18-
if username != String::from("root") {
19-
println!("Should run this binary as root");
21+
if !is_root_user() {
22+
eprintln!("Should run this binary as root");
2023
process::exit(1);
2124
}
2225

@@ -30,7 +33,7 @@ fn main() {
3033
let interface_name = match matches.value_of("interface") {
3134
Some(name) => name,
3235
None => {
33-
println!("Interface name required");
36+
eprintln!("Interface name required");
3437
process::exit(1);
3538
}
3639
};
@@ -47,20 +50,20 @@ fn main() {
4750
let selected_interface: &datalink::NetworkInterface = interfaces.iter()
4851
.find(|interface| { interface.name == interface_name && interface.is_up() && !interface.is_loopback() })
4952
.unwrap_or_else(|| {
50-
println!("Could not find interface with name {}", interface_name);
53+
eprintln!("Could not find interface with name {}", interface_name);
5154
process::exit(1);
5255
});
5356

5457
let ip_network = match selected_interface.ips.first() {
5558
Some(ip_network) => ip_network,
5659
None => {
57-
println!("Expects a valid IP on the interface");
60+
eprintln!("Expects a valid IP on the interface");
5861
process::exit(1);
5962
}
6063
};
6164

6265
if !ip_network.is_ipv4() {
63-
println!("Only IPv4 supported");
66+
eprintln!("Only IPv4 supported");
6467
process::exit(1);
6568
}
6669

@@ -84,9 +87,16 @@ fn main() {
8487
break;
8588
}
8689

87-
let arp_buffer = rx.next().unwrap();
90+
let arp_buffer = rx.next().unwrap_or_else(|error| {
91+
eprintln!("Failed to receive ARP requests ({})", error);
92+
process::exit(1);
93+
});
8894

89-
let ethernet_packet = EthernetPacket::new(&arp_buffer[..]).unwrap();
95+
let ethernet_packet = match EthernetPacket::new(&arp_buffer[..]) {
96+
Some(packet) => packet,
97+
None => continue
98+
};
99+
90100
let is_arp = match ethernet_packet.get_ethertype() {
91101
EtherTypes::Arp => true,
92102
_ => false
@@ -116,7 +126,10 @@ fn main() {
116126

117127
// ------------------
118128

119-
responses.join().unwrap();
129+
responses.join().unwrap_or_else(|error| {
130+
eprintln!("Failed to close receive thread ({:?})", error);
131+
process::exit(1);
132+
});
120133
}
121134

122135
fn send_arp_request(tx: &mut Box<dyn pnet::datalink::DataLinkSender>, interface: &datalink::NetworkInterface, target_ip: Ipv4Addr) {
@@ -125,7 +138,10 @@ fn send_arp_request(tx: &mut Box<dyn pnet::datalink::DataLinkSender>, interface:
125138
let mut ethernet_packet = MutableEthernetPacket::new(&mut ethernet_buffer).unwrap();
126139

127140
let target_mac = datalink::MacAddr::broadcast();
128-
let source_mac = interface.mac.unwrap();
141+
let source_mac = interface.mac.unwrap_or_else(|| {
142+
eprintln!("Interface should have a MAC address");
143+
process::exit(1);
144+
});
129145

130146
ethernet_packet.set_destination(target_mac);
131147
ethernet_packet.set_source(source_mac);
@@ -134,7 +150,10 @@ fn send_arp_request(tx: &mut Box<dyn pnet::datalink::DataLinkSender>, interface:
134150
let mut arp_buffer = [0u8; 28];
135151
let mut arp_packet = MutableArpPacket::new(&mut arp_buffer).unwrap();
136152

137-
let source_ip = interface.ips.first().unwrap().ip();
153+
let source_ip = interface.ips.first().unwrap_or_else(|| {
154+
eprintln!("Interface should have an IP address");
155+
process::exit(1);
156+
}).ip();
138157

139158
let source_ipv4 = match source_ip {
140159
IpAddr::V4(ipv4_addr) => Some(ipv4_addr),

0 commit comments

Comments
 (0)