fn udp_connect_helper(
    fd_client: libc::c_int,
    fd_server: libc::c_int,
    connect: bool,
) -> libc::sockaddr_in {
    // 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);
    }

    // connect to the server address
    if connect {
        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);
    }

    server_addr
}
/// A helper function for UDP sockets to bind the server fd and optionally connect
/// the client fd to the server fd. Returns the address that the server is bound to.
A helper function for UDP sockets to bind the server fd and optionally connect
the client fd to the server fd. Returns the address that the server is bound to.
A helper function for UDP sockets to bind the server fd and optionally connect the client fd to the server fd . Returns the address that the server is bound to .
fn udp_connect_helper(
    fd_client: libc::c_int,
    fd_server: libc::c_int,
    connect: bool,
) -> libc::sockaddr_in {
    // 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);
    }
    // connect to the server address
    if connect {
        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);
    }
    server_addr
}
fn udp_connect_helper ( fd_client : libc :: c_int , fd_server : libc :: c_int , connect : bool , ) -> libc :: sockaddr_in { // 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 ) ; } // connect to the server address if connect { 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 ) ; } server_addr }
A helper function for UDP sockets to bind the server fd and optionally connect
the client fd to the server fd.
A helper function for UDP sockets to bind the server fd and optionally connect the client fd to the server fd .
fd_client
libc::c_int
fd_server
libc::c_int
connect
bool
fd_client
libc::c_int
fd_server
libc::c_int
connect
bool