bennyz / nbd

An attempt at an NBD server in Rust
Apache License 2.0
0 stars 0 forks source link

Support UNIX socket #4

Open bennyz opened 2 years ago

bennyz commented 2 years ago

Supporting UNIX socket should be fairly easy, the following snippet can be used:

    #[clap(long)]
    unix: bool,
}

fn main() {
    let args = Args::parse();
    if !Path::exists(Path::new(&args.file)) {
        panic!("{} does not exist!", args.file);
    }

    let mut export = Export {
        name: args.name,
        description: args.description,
        path: args.file,
        multiconn: true,
        ..Default::default()
    };

    export.init_export().unwrap();

    if args.unix {
        println!("Starting unix socket server");
        let server: Arc<Server<UnixStream>> = Arc::new(nbd::Server::new(export));

        let listener = UnixListener::bind("/tmp/nbd.sock").unwrap();
        for conn in listener.incoming() {
            match conn {
                Ok(stream) => {
                    let client_addr = stream.local_addr().unwrap();
                    println!("client_addr {:?}", client_addr);
                    let client = Client::new(
                        stream,
                        client_addr
                            .as_pathname()
                            .unwrap()
                            .to_str()
                            .unwrap()
                            .to_string(),
                    );
                    let clone = Arc::clone(&server);
                    thread::spawn(move || {
                        clone.handle(client).unwrap();
                    });
                }
                Err(e) => {
                    eprintln!("error: {}", e)
                }
            }
        }
bennyz commented 2 years ago

Initial support introduced in https://github.com/bennyz/nbd/commit/4babb5c03318966ad8e078a0d20e6bf2a86b86a7 But the code is need of a good cleanup