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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use anyhow::Result;
use num_cpus;
use pnet_datalink::{MacAddr, NetworkInterface};
use rand::Rng;
use std::error::Error;
use std::fmt;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::time::Duration;
use subnetwork::Ipv4Pool;
// use subnetwork::Ipv4Pool;
use pnet::packet::ip::IpNextHeaderProtocols;
use pnet::transport::TransportChannelType::Layer3;
use pnet::transport::TransportChannelType::Layer4;
use pnet::transport::TransportProtocol::{Ipv4, Ipv6};
use pnet::transport::{transport_channel, TransportReceiver, TransportSender};
use threadpool::ThreadPool;

const DEFAILT_MAX_LOOP: usize = 32;
const DEFAILT_TIMEOUT: f32 = 1.5;

pub const IP_TTL: u8 = 64;

pub const BUFF_SIZE: usize = 4096;
pub const TCP_BUFF_SIZE: usize = 4096;
pub const UDP_BUFF_SIZE: usize = 4096;

pub const IPV4_HEADER_LEN: usize = 20;
pub const ICMP_BUFF_SIZE: usize = 4096;

pub const TCP_HEADER_LEN: usize = 20;
pub const TCP_DATA_LEN: usize = 0;

pub const UDP_HEADER_LEN: usize = 8;
pub const UDP_DATA_LEN: usize = 0;

pub const ICMP_HEADER_LEN: usize = 8;
pub const ICMP_DATA_LEN: usize = 0;

/* FindInterfaceError */
#[derive(Debug, Clone)]
pub struct FindInterfaceError {
    interface: String,
}

impl fmt::Display for FindInterfaceError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "can not found interface {}", self.interface)
    }
}

impl FindInterfaceError {
    pub fn new(interface: &str) -> FindInterfaceError {
        let interface = interface.to_string();
        FindInterfaceError { interface }
    }
}

impl Error for FindInterfaceError {}

/* GetInterfaceIPError */
#[derive(Debug, Clone)]
pub struct GetInterfaceIPError {
    interface: String,
}

impl fmt::Display for GetInterfaceIPError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "can not get ip from interface {}", self.interface)
    }
}

impl GetInterfaceIPError {
    pub fn new(interface: &str) -> GetInterfaceIPError {
        let interface = interface.to_string();
        GetInterfaceIPError { interface }
    }
}

impl Error for GetInterfaceIPError {}

/* GetInterfaceIPError */
#[derive(Debug, Clone)]
pub struct GetInterfaceMACError {
    interface: String,
}

impl fmt::Display for GetInterfaceMACError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "can not get mac from interface {}", self.interface)
    }
}

impl GetInterfaceMACError {
    pub fn new(interface: &str) -> GetInterfaceMACError {
        let interface = interface.to_string();
        GetInterfaceMACError { interface }
    }
}

impl Error for GetInterfaceMACError {}

/* CODE */

pub fn get_host_interfaces() -> Vec<NetworkInterface> {
    pnet_datalink::interfaces()
}

/// Returns an interface that matches the subnet
pub fn find_interface_by_subnet(subnet: &Ipv4Pool) -> Option<NetworkInterface> {
    let interfaces = get_host_interfaces();
    for interface in &interfaces {
        for ip in &interface.ips {
            match ip.ip() {
                IpAddr::V4(i) => {
                    if subnet.contain(i) {
                        return Some(interface.clone());
                    }
                }
                _ => (),
            }
        }
    }
    None
}

/// Returns an interface that matches the name
pub fn find_interface_by_name(interface_name: &str) -> Option<NetworkInterface> {
    for interface in pnet_datalink::interfaces() {
        // println!("{}", interface)
        if interface.name == interface_name {
            return Some(interface);
        }
    }
    None
}

/// Returns source ip of host machine interfaces
pub fn get_interface_ip(interface: &NetworkInterface) -> Option<Ipv4Addr> {
    for i in &interface.ips {
        if i.is_ipv4() {
            match i.ip() {
                IpAddr::V4(ip) => return Some(ip),
                _ => (),
            }
        }
    }
    None
}

/// Returns source ip(v6) of host machine interfaces
pub fn get_interface_ip6(interface: &NetworkInterface) -> Option<Ipv6Addr> {
    for i in &interface.ips {
        if i.is_ipv4() {
            match i.ip() {
                IpAddr::V6(ip) => return Some(ip),
                _ => (),
            }
        }
    }
    None
}

/// Convert user input interface and return (NetworkInterface, Ipv4Addr, MacAddr)
pub fn parse_interface(interface: Option<&str>) -> Result<(NetworkInterface, Ipv4Addr, MacAddr)> {
    let i = match interface {
        Some(name) => match find_interface_by_name(name) {
            Some(i) => i,
            _ => return Err(FindInterfaceError::new(name).into()),
        },
        _ => return Err(FindInterfaceError::new("please set interface").into()),
    };
    let ipv4 = match get_interface_ip(&i) {
        Some(ip) => ip,
        _ => return Err(GetInterfaceIPError::new(&i.to_string()).into()),
    };
    let mac = match i.mac {
        Some(m) => m,
        _ => return Err(GetInterfaceMACError::new(&i.to_string()).into()),
    };

    Ok((i, ipv4, mac))
}

pub fn parse_interface6(interface: Option<&str>) -> Result<(NetworkInterface, Ipv6Addr, MacAddr)> {
    let i = match interface {
        Some(name) => match find_interface_by_name(name) {
            Some(i) => i,
            _ => return Err(FindInterfaceError::new(name).into()),
        },
        _ => return Err(FindInterfaceError::new("please set interface").into()),
    };
    let ipv6 = match get_interface_ip6(&i) {
        Some(ip) => ip,
        _ => return Err(GetInterfaceIPError::new(&i.to_string()).into()),
    };
    let mac = match i.mac {
        Some(m) => m,
        _ => return Err(GetInterfaceMACError::new(&i.to_string()).into()),
    };

    Ok((i, ipv6, mac))
}

/// Convert user input subnet and return (NetworkInterface, Ipv4Addr, MacAddr)
pub fn parse_interface_by_subnet(
    subnet: Ipv4Pool,
) -> Result<(NetworkInterface, Ipv4Addr, MacAddr)> {
    let i = match find_interface_by_subnet(&subnet) {
        Some(i) => i,
        _ => return Err(FindInterfaceError::new("counld not find interface by subnet").into()),
    };
    let ipv4 = match get_interface_ip(&i) {
        Some(ip) => ip,
        _ => return Err(GetInterfaceIPError::new(&i.to_string()).into()),
    };
    let mac = match i.mac {
        Some(m) => m,
        _ => return Err(GetInterfaceMACError::new(&i.to_string()).into()),
    };

    Ok((i, ipv4, mac))
}

/// Returns the random u16
pub fn random_u16() -> u16 {
    let mut rng = rand::thread_rng();
    rng.gen()
}

/// Returns the random port
pub fn random_port() -> u16 {
    let mut rng = rand::thread_rng();
    rng.gen_range(1024..=65535)
}

/// Returns the number of CPUs in the machine
pub fn get_cpu_num() -> usize {
    num_cpus::get()
}

pub fn get_threads_pool(threads_num: usize) -> ThreadPool {
    let pool = if threads_num > 0 {
        ThreadPool::new(threads_num)
    } else {
        let cpus = get_cpu_num();
        ThreadPool::new(cpus)
    };
    pool
}

pub fn get_max_loop(max_loop: Option<usize>) -> usize {
    match max_loop {
        Some(m) => m,
        _ => DEFAILT_MAX_LOOP,
    }
}

pub fn get_timeout(timeout: Option<Duration>) -> Duration {
    match timeout {
        Some(t) => t,
        _ => Duration::from_secs_f32(DEFAILT_TIMEOUT),
    }
}

pub fn return_layer4_tcp_channel(
    buffer_size: usize,
) -> Result<(TransportSender, TransportReceiver)> {
    let tcp_protocol = Layer4(Ipv4(IpNextHeaderProtocols::Tcp));
    match transport_channel(buffer_size, tcp_protocol) {
        Ok((tx, rx)) => Ok((tx, rx)),
        Err(e) => return Err(e.into()),
    }
}

pub fn return_layer4_tcp6_channel(
    buffer_size: usize,
) -> Result<(TransportSender, TransportReceiver)> {
    let tcp_protocol = Layer4(Ipv6(IpNextHeaderProtocols::Tcp));
    match transport_channel(buffer_size, tcp_protocol) {
        Ok((tx, rx)) => Ok((tx, rx)),
        Err(e) => return Err(e.into()),
    }
}

pub fn return_layer4_udp_channel(
    buffer_size: usize,
) -> Result<(TransportSender, TransportReceiver)> {
    let udp_protocol = Layer4(Ipv4(IpNextHeaderProtocols::Udp));
    match transport_channel(buffer_size, udp_protocol) {
        Ok((tx, rx)) => Ok((tx, rx)),
        Err(e) => return Err(e.into()),
    }
}

pub fn return_layer4_udp6_channel(
    buffer_size: usize,
) -> Result<(TransportSender, TransportReceiver)> {
    let udp_protocol = Layer4(Ipv6(IpNextHeaderProtocols::Udp));
    match transport_channel(buffer_size, udp_protocol) {
        Ok((tx, rx)) => Ok((tx, rx)),
        Err(e) => return Err(e.into()),
    }
}

pub fn return_layer3_icmp_channel(
    buffer_size: usize,
) -> Result<(TransportSender, TransportReceiver)> {
    let icmp_protocol = Layer3(IpNextHeaderProtocols::Icmp);
    match transport_channel(buffer_size, icmp_protocol) {
        Ok((tx, rx)) => Ok((tx, rx)),
        Err(e) => return Err(e.into()),
    }
}

pub fn return_layer4_icmp_channel(
    buffer_size: usize,
) -> Result<(TransportSender, TransportReceiver)> {
    let icmp_protocol = Layer4(Ipv4(IpNextHeaderProtocols::Icmp));
    match transport_channel(buffer_size, icmp_protocol) {
        Ok((tx, rx)) => Ok((tx, rx)),
        Err(e) => return Err(e.into()),
    }
}

pub fn return_layer4_icmp6_channel(
    buffer_size: usize,
) -> Result<(TransportSender, TransportReceiver)> {
    let icmp_protocol = Layer4(Ipv6(IpNextHeaderProtocols::Icmpv6));
    match transport_channel(buffer_size, icmp_protocol) {
        Ok((tx, rx)) => Ok((tx, rx)),
        Err(e) => return Err(e.into()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_list_interfaces() {
        for interface in pnet_datalink::interfaces() {
            // println!("{}", interface)
            let ips = interface.ips;
            for ip in ips {
                match ip.ip() {
                    IpAddr::V4(i) => {
                        println!("{}", i);
                    }
                    _ => (),
                }
            }
        }
    }
    #[test]
    fn test_get_cpus() {
        let cpus = get_cpu_num();
        println!("{}", cpus);
    }
}