nix icon indicating copy to clipboard operation
nix copied to clipboard

Support for socket Address Family 80

Open JCBurnside opened this issue 3 years ago • 4 comments

Hello so I am trying to work with x11rb inside wsl and have a working setup for an xserver (I have confirmed this through the weston program and was able to successful make a window using the xcb crate) however trying to use the following code results in an "thread 'main' panicked at 'unexpected address family 80', /home/digital/.cargo/registry/src/github.com-1ecc6299db9ec823/nix-0.23.0/src/sys/socket/mod.rs:1878:15"

use std::net::TcpStream;
use std::os::unix::net::UnixStream;
use std::os::unix::io::{AsRawFd, RawFd};
use std::str::FromStr;
use nix::sys::socket::{recvmsg, sendmsg, MsgFlags};
use nix::sys::uio::IoVec;

// A X11 setup request
const TO_SEND: [u8; 12] = [
    0x6c, // Byte order: little endian
    0, 11, 0, 0, 0, // padding and protocol version
    0, 0, 0, 0, 0, 0, // We don't send any authorisation + padding
];

fn test_stream(fd: RawFd) -> Result<(), Box<dyn std::error::Error>> {
    let iov = [IoVec::from_slice(&TO_SEND[..])];
    sendmsg(fd, &iov, &[], MsgFlags::empty(), None)?;

    let mut buf = [0; 10];
    let mut cmsg = nix::cmsg_space!([RawFd; 16]);
    let result = recvmsg(fd, &[IoVec::from_mut_slice(&mut buf[..])], Some(&mut cmsg), MsgFlags::empty())?;
    println!("recvmsg() returned {:?}", result);
    Ok(())
}

fn try_tcp(display: u16) -> Result<(), Box<dyn std::error::Error>> {
    test_stream(TcpStream::connect(("localhost", 6000 + display))?.as_raw_fd())
}

fn try_unix(display: u16) -> Result<(), Box<dyn std::error::Error>> {
    let file = format!("/tmp/.X11-unix/X{}", display);
    test_stream(UnixStream::connect(file)?.as_raw_fd())
}

fn main() {
    // This should contain the value from the env:
    //   $ echo $DISPLAY
    //   :0
    let display = std::env::args().skip(1).next().as_ref().and_then(|s| u16::from_str(s).ok()).unwrap_or(0);
    println!("Connecting to DISPLAY=:{}", display);

    println!("unix: {:?}", try_unix(display));
    println!("tcp: {:?}", try_tcp(display));
}

not quite sure what is going on

JCBurnside avatar Nov 19 '21 20:11 JCBurnside

The error is simply that you have a sockaddr whose address family isn't known to Nix. It isn't AF_UNIX, AF_INET, etc. I can't find any address family defined to 80 on my Linux machine, so this might be specific to WSL.

asomers avatar Nov 19 '21 20:11 asomers

well my WSL is using unbuntu. But still a strange issue. What distro are you using?

JCBurnside avatar Nov 19 '21 20:11 JCBurnside

I'm looking at the header files on an Ubuntu machine, but I don't think they're distro-dependent. That address family might be for a special socket type created by Window's linux emulation layer.

asomers avatar Nov 19 '21 20:11 asomers

I see. Welp VM it is.

JCBurnside avatar Nov 19 '21 20:11 JCBurnside