使用 TCP 协议将原始套接字绑定到地址会终止进程

问题描述 投票:0回答:0

使用

socket2
https://crates.io/crates/socket2 使用 TCP 简单地绑定到具有原始套接字的地址会终止进程。如果我创建一个套接字并简单地尝试通过它发送,我会收到操作系统错误
An invalid argument was supplied. (os error 10022)

假设这不是板条箱问题而是 Windows 问题,它的原因是什么?

use socket2::{Domain, Protocol, SockAddr, Socket, Type};
use std::{env, net::SocketAddr, process::{Command, Stdio}, thread, time::Duration, io::{BufReader, BufRead}};

fn main() -> std::io::Result<()> {
    let mut args: Vec<String> = std::env::args().collect();
    args.remove(0);

    if args.len() == 0 {
        let file_path = env::current_exe().unwrap();

        let file_path = file_path.to_str().unwrap();

        let (command, cmd_args) = (
            "powershell.exe",
            vec![
                String::from("-NoProfile"),
                String::from("-NoExit"),
                String::from("-Command"),
                String::from("Start-Process"),
                String::from(file_path),
                String::from("-Verb"),
                String::from("RunAs"),
                String::from("-ArgumentList"),
                String::from("'bind'"),
            ],
        );

        let mut cmd = Command::new(command)
            .args(cmd_args)
            .stderr(Stdio::piped())
            .spawn()
            .unwrap();

        let err = cmd.stderr.take().unwrap();
        let reader = BufReader::new(err);

        for line in reader.lines() {
            println!("{}", line.unwrap());
        }

        println!("DONE");
    } else {
        thread::sleep(Duration::from_secs(5));

        let address = SockAddr::from(SocketAddr::new([0, 0, 0, 0].into(), 0));

        let send_socket = Socket::new(Domain::IPV4, Type::RAW, Some(Protocol::TCP))?;

        send_socket.bind(&address)?;

        loop {}
    }

    Ok(())
}
windows sockets rust raw-sockets
© www.soinside.com 2019 - 2024. All rights reserved.