日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

利用Rust實現一個簡單的Ping應用_Rust語言

作者:qingwave ? 更新時間: 2023-01-03 編程語言

這兩年Rust火的一塌糊涂,甚至都燒到了前端,再不學習怕是要落伍了。最近翻了翻文檔,寫了個簡單的Ping應用練練手,被所有權折騰的夠嗆,相比起Golang上手難度大很多,現將開發中的一些問題總結如下,所有源碼見ring。

目標

實現一個Ping,功能包含:

命令行解析

實現ICMP協議,pnet包中已經包含了ICMP包定義,可以使用socket2庫發送

周期性發送Ping,通過多線程發送,再匯總結果

監聽退出信號

命令行解析

系統庫std::env::args可以解析命令行參數,但對于一些復雜的參數使用起來比較繁瑣,更推薦clap。利用clap的注解,通過結構體定義命令行參數

/// ping but with rust, rust + ping -> ring
#[derive(Parser, Debug, Clone)] // Parser生成clap命令行解析方法
#[command(author, version, about, long_about = None)]
pub struct Args {
    /// Count of ping times
    #[arg(short, default_value_t = 4)] // short表示開啟短命名,默認為第一個字母,可以指定;default_value_t設置默認值
    count: u16,

    /// Ping packet size
    #[arg(short = 's', default_value_t = 64)]
    packet_size: usize,

    /// Ping ttl
    #[arg(short = 't', default_value_t = 64)]
    ttl: u32,

    /// Ping timeout seconds
    #[arg(short = 'w', default_value_t = 1)]
    timeout: u64,

    /// Ping interval duration milliseconds
    #[arg(short = 'i', default_value_t = 1000)]
    interval: u64,

    /// Ping destination, ip or domain
    #[arg(value_parser=Address::parse)] // 自定義解析
    destination: Address,
}

clap可以方便的指定參數命名、默認值、解析方法等,運行結果如下

? ?ring git:(main) cargo run -- -h
? ?Compiling ring v0.1.0 (/home/i551329/work/ring)
? ? Finished dev [unoptimized + debuginfo] target(s) in 1.72s
? ? ?Running `target/debug/ring -h`
ping but with rust, rust + ping -> ring

Usage: ring [OPTIONS] <DESTINATION>

Arguments:
? <DESTINATION> ?Ping destination, ip or domain

Options:
? -c <COUNT> ? ? ? ? ? ?Count of ping times [default: 4]
? -s <PACKET_SIZE> ? ? ?Ping packet size [default: 64]
? -t <TTL> ? ? ? ? ? ? ?Ping ttl [default: 64]
? -w <TIMEOUT> ? ? ? ? ?Ping timeout seconds [default: 1]
? -i <INTERVAL> ? ? ? ? Ping interval duration milliseconds [default: 1000]
? -h, --help ? ? ? ? ? ?Print help information
? -V, --version ? ? ? ? Print version information

實現Ping

pnet中提供了ICMP包的定義,socket2可以將定義好的ICMP包發送給目標IP,另一種實現是通過pnet_transport::transport_channel發送原始數據包,但需要過濾結果而且權限要求較高。

首先定義ICMP包

let mut buf = vec![0; self.config.packet_size];
let mut icmp = MutableEchoRequestPacket::new(&mut buf[..]).ok_or(RingError::InvalidBufferSize)?;
icmp.set_icmp_type(IcmpTypes::EchoRequest); // 設置為EchoRequest類型
icmp.set_icmp_code(IcmpCodes::NoCode);
icmp.set_sequence_number(self.config.sequence + seq_offset); // 序列號
icmp.set_identifier(self.config.id);
icmp.set_checksum(util::checksum(icmp.packet(), 1)); // 校驗函數

通過socket2發送請求

let socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::ICMPV4))?;
let src = SocketAddr::new(net::IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0);
socket.bind(&src.into())?; // 綁定源地址
socket.set_ttl(config.ttl)?;
socket.set_read_timeout(Some(Duration::from_secs(config.timeout)))?; // 超時配置
socket.set_write_timeout(Some(Duration::from_secs(config.timeout)))?;

???????// 發送
socket.send_to(icmp.packet_mut(), &self.dest.into())?;

最后處理相應,轉換成pnet中的EchoReplyPacket

let mut mem_buf = unsafe { &mut *(buf.as_mut_slice() as *mut [u8] as *mut [std::mem::MaybeUninit<u8>]) };
let (size, _) = self.socket.recv_from(&mut mem_buf)?;

???????// 轉換成EchoReply
let reply = EchoReplyPacket::new(&buf).ok_or(RingError::InvalidPacket)?;

至此,一次Ping請求完成。

周期性發送

Ping需要周期性的發送請求,比如秒秒請求一次,如果直接通過循環實現,一次請求卡住將影響主流程,必須通過多線程來保證固定周期的發送。

發送請求

let send = Arc::new(AtomicU64::new(0)); // 統計發送次數
let _send = send.clone();
let this = Arc::new(self.clone());
let (sx, rx) = bounded(this.config.count as usize); // channel接受線程handler
thread::spawn(move || {
    for i in 0..this.config.count {
        let _this = this.clone();
        sx.send(thread::spawn(move || _this.ping(i))).unwrap(); // 線程中運行ping,并將handler發送到channel中

        _send.fetch_add(1, Ordering::SeqCst); // 發送一次,send加1

        if i < this.config.count - 1 {
            thread::sleep(Duration::from_millis(this.config.interval));
        }
    }
    drop(sx); // 發送完成關閉channel
});
  • thread::spawn可以快速創建線程,但需要注意所有權的轉移,如果在線程內部調用方法獲取變量,需要通過Arc原子引用計數
  • send變量用來統計發送數,原子類型,并且用Arc包裹;this是當前類的Arc克隆,會轉移到線程中
  • 第一個線程內周期性調用ping(),并且其在單獨線程中運行
  • 通過bounded來定義channel(類似Golang中的chan),用來處理結果,發送完成關閉

處理結果

let success = Arc::new(AtomicU64::new(0)); // 定義請求成功的請求
let _success = success.clone();
let (summary_s, summary_r) = bounded(1); // channel來判斷是否處理完成
thread::spawn(move || {
    for handle in rx.iter() {
        if let Some(res) = handle.join().ok() {
            if res.is_ok() {
                _success.fetch_add(1, Ordering::SeqCst); // 如果handler結果正常,success加1
            }
        }
    }
    summary_s.send(()).unwrap(); // 處理完成
});

第二個線程用來統計結果,channel通道取出ping線程的handler,如果返回正常則加1

處理信號

let stop = signal_notify()?; // 監聽退出信號
select!(
    recv(stop) -> sig => {
        if let Some(s) = sig.ok() { // 收到退出信號
            println!("Receive signal {:?}", s);
        }
    },
    recv(summary_r) -> summary => { // 任務完成
        if let Some(e) = summary.err() {
            println!("Error on summary: {}", e);
        }
    },
);

通過select來處理信號(類似Golang中的select),到收到退出信號或者任務完成時繼續往下執行。

信號處理

Golang中可以很方便的處理信號,但在Rust中官方庫沒有提供類似功能,可以通過signal_hook與crossbeam_channel實現監聽退出信號

fn signal_notify() -> std::io::Result<Receiver<i32>> {
    let (s, r) = bounded(1); // 定義channel,用來異步接受退出信號

    let mut signals = signal_hook::iterator::Signals::new(&[SIGINT, SIGTERM])?; // 創建信號

    thread::spawn(move || {
        for signal in signals.forever() { // 如果結果到信號發送到channel中
            s.send(signal).unwrap();
            break;
        }
    });

    Ok(r) // 返回接受channel
}

其他

很多吐槽人Golang的錯誤處理,Rust也不遑多讓,不過提供了?語法糖,也可以配合anyhow與thiserror來簡化錯誤處理。

驗證

Ping域名/IP

ring git:(main) ?cargo run -- www.baidu.com?

PING www.baidu.com(103.235.46.40)
64 bytes from 103.235.46.40: icmp_seq=1 ttl=64 time=255.85ms
64 bytes from 103.235.46.40: icmp_seq=2 ttl=64 time=254.17ms
64 bytes from 103.235.46.40: icmp_seq=3 ttl=64 time=255.41ms
64 bytes from 103.235.46.40: icmp_seq=4 ttl=64 time=256.50ms

--- www.baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3257.921ms

測試退出信息,運行中通過Ctrl+C中止

cargo run 8.8.8.8 -c 10

PING 8.8.8.8(8.8.8.8)
64 bytes from 8.8.8.8: icmp_seq=1 ttl=64 time=4.32ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=64 time=3.02ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=64 time=3.24ms
^CReceive signal 2

???????--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2365.104ms

總結

Rust為了安全高效,通過引入所有權來解決GC問題,也帶來了許多不便,編程時必須要考慮到變量的聲明周期、借用等問題,所有語言都是在方便、性能、安全之間做權衡,要么程序員不方便,要么編譯器多做點功。換一個角度來說Bug總是不可避免的,在編譯階段出現總好過運行階段。

原文鏈接:https://blog.csdn.net/u012986012/article/details/128101040

欄目分類
最近更新