fn connect_helper(fd_client: libc::c_int, fd_server: libc::c_int) -> libc::c_int {
    // the server address
    let mut server_addr = libc::sockaddr_in {
        sin_family: libc::AF_INET as u16,
        sin_port: 0u16.to_be(),
        sin_addr: libc::in_addr {
            s_addr: libc::INADDR_LOOPBACK.to_be(),
        },
        sin_zero: [0; 8],
    };

    // bind on the server address
    {
        let rv = unsafe {
            libc::bind(
                fd_server,
                &server_addr as *const libc::sockaddr_in as *const libc::sockaddr,
                std::mem::size_of_val(&server_addr) as u32,
            )
        };
        assert_eq!(rv, 0);
    }

    // get the assigned port number
    {
        let mut server_addr_size = std::mem::size_of_val(&server_addr) as u32;
        let rv = unsafe {
            libc::getsockname(
                fd_server,
                &mut server_addr as *mut libc::sockaddr_in as *mut libc::sockaddr,
                &mut server_addr_size as *mut libc::socklen_t,
            )
        };
        assert_eq!(rv, 0);
        assert_eq!(server_addr_size, std::mem::size_of_val(&server_addr) as u32);
    }

    // listen for connections
    {
        let rv = unsafe { libc::listen(fd_server, 10) };
        assert_eq!(rv, 0);
    }

    // connect to the server address
    {
        let rv = unsafe {
            libc::connect(
                fd_client,
                &server_addr as *const libc::sockaddr_in as *const libc::sockaddr,
                std::mem::size_of_val(&server_addr) as u32,
            )
        };
        assert_eq!(rv, 0);
    }

    // shadow needs to run events, otherwise the accept call won't know it
    // has an incoming connection (SYN packet)
    {
        let rv = unsafe { libc::usleep(10000) };
        assert_eq!(rv, 0);
    }

    // accept the connection
    let fd = unsafe { libc::accept(fd_server, std::ptr::null_mut(), std::ptr::null_mut()) };
    assert!(fd >= 0);

    fd
}
/// A helper function to start a server on one fd and connect another fd to it. Returns the accepted fd.
A helper function to start a server on one fd and connect another fd to it. Returns the accepted fd.
A helper function to start a server on one fd and connect another fd to it . Returns the accepted fd .
fn connect_helper(fd_client: libc::c_int, fd_server: libc::c_int) -> libc::c_int {
    // the server address
    let mut server_addr = libc::sockaddr_in {
        sin_family: libc::AF_INET as u16,
        sin_port: 0u16.to_be(),
        sin_addr: libc::in_addr {
            s_addr: libc::INADDR_LOOPBACK.to_be(),
        },
        sin_zero: [0; 8],
    };
    // bind on the server address
    {
        let rv = unsafe {
            libc::bind(
                fd_server,
                &server_addr as *const libc::sockaddr_in as *const libc::sockaddr,
                std::mem::size_of_val(&server_addr) as u32,
            )
        };
        assert_eq!(rv, 0);
    }
    // get the assigned port number
    {
        let mut server_addr_size = std::mem::size_of_val(&server_addr) as u32;
        let rv = unsafe {
            libc::getsockname(
                fd_server,
                &mut server_addr as *mut libc::sockaddr_in as *mut libc::sockaddr,
                &mut server_addr_size as *mut libc::socklen_t,
            )
        };
        assert_eq!(rv, 0);
        assert_eq!(server_addr_size, std::mem::size_of_val(&server_addr) as u32);
    }
    // listen for connections
    {
        let rv = unsafe { libc::listen(fd_server, 10) };
        assert_eq!(rv, 0);
    }
    // connect to the server address
    {
        let rv = unsafe {
            libc::connect(
                fd_client,
                &server_addr as *const libc::sockaddr_in as *const libc::sockaddr,
                std::mem::size_of_val(&server_addr) as u32,
            )
        };
        assert_eq!(rv, 0);
    }
    // shadow needs to run events, otherwise the accept call won't know it
    // has an incoming connection (SYN packet)
    {
        let rv = unsafe { libc::usleep(10000) };
        assert_eq!(rv, 0);
    }
    // accept the connection
    let fd = unsafe { libc::accept(fd_server, std::ptr::null_mut(), std::ptr::null_mut()) };
    assert!(fd >= 0);
    fd
}
fn connect_helper ( fd_client : libc :: c_int , fd_server : libc :: c_int ) -> libc :: c_int { // the server address let mut server_addr = libc :: sockaddr_in { sin_family : libc :: AF_INET as u16 , sin_port : 0u16 . to_be ( ) , sin_addr : libc :: in_addr { s_addr : libc :: INADDR_LOOPBACK . to_be ( ) , } , sin_zero : [ 0 ; 8 ] , } ; // bind on the server address { let rv = unsafe { libc :: bind ( fd_server , & server_addr as * const libc :: sockaddr_in as * const libc :: sockaddr , std :: mem :: size_of_val ( & server_addr ) as u32 , ) } ; assert_eq ! ( rv 0 ) ; } // get the assigned port number { let mut server_addr_size = std :: mem :: size_of_val ( & server_addr ) as u32 ; let rv = unsafe { libc :: getsockname ( fd_server , & mut server_addr as * mut libc :: sockaddr_in as * mut libc :: sockaddr , & mut server_addr_size as * mut libc :: socklen_t , ) } ; assert_eq ! ( rv 0 ) ; assert_eq ! ( server_addr_size std mem size_of_val ( server_addr ) as u32 ) ; } // listen for connections { let rv = unsafe { libc :: listen ( fd_server , 10 ) } ; assert_eq ! ( rv 0 ) ; } // connect to the server address { let rv = unsafe { libc :: connect ( fd_client , & server_addr as * const libc :: sockaddr_in as * const libc :: sockaddr , std :: mem :: size_of_val ( & server_addr ) as u32 , ) } ; assert_eq ! ( rv 0 ) ; } // shadow needs to run events, otherwise the accept call won't know it // has an incoming connection (SYN packet) { let rv = unsafe { libc :: usleep ( 10000 ) } ; assert_eq ! ( rv 0 ) ; } // accept the connection let fd = unsafe { libc :: accept ( fd_server , std :: ptr :: null_mut ( ) , std :: ptr :: null_mut ( ) ) } ; assert ! ( fd 0 ) ; fd }
A helper function to start a server on one fd and connect another fd to it.
A helper function to start a server on one fd and connect another fd to it .
fd_client
libc::c_int
fd_server
libc::c_int
fd_client
libc::c_int
fd_server
libc::c_int