https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=291145

            Bug ID: 291145
           Summary: UDP connected sockets cannot be disconnected
           Product: Base System
           Version: 14.3-RELEASE
          Hardware: amd64
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: kern
          Assignee: [email protected]
          Reporter: [email protected]

Hello,

I've been trying to use the libc connect syscall to change the association of a
UDP datagram socket in a Rust application, but I have had trouble with
successfully invoking connect on the same socket fd multiple times. I read the
connect(2) man page says this:

       Generally, stream sockets may successfully connect() only  once;  data-
       gram  sockets may use connect() multiple times to change their associa-
       tion.  Datagram sockets may dissolve the association by  connecting  to
       an invalid address, such as a null address.

However, I have not been successful in disconnecting and dissolving the socket
association when I end a connection from a UDP client. The following code works
as expected on macOS Tahoe 26.1 and removes the association with a single
address:

    {
        // sockaddr WITH sa_len on these platforms
        let addr = libc::sockaddr {
            sa_len: std::mem::size_of::<libc::sockaddr>() as u8,
            sa_family: libc::AF_UNSPEC as libc::sa_family_t,
            sa_data: [0; 14],
        };
        let rc = unsafe {
            libc::connect(
                fd,
                &addr as *const libc::sockaddr,
                addr.sa_len as libc::socklen_t,
            )
        };
        if ok_or_eafnosupport(rc).is_ok() {
            return Ok(());
        }

        // Fallback: connect(fd, NULL, 0)
        let rc2 = unsafe { libc::connect(fd, std::ptr::null(), 0) };
        return ok_or_eafnosupport(rc2);
    }

macOS will return EAFNOSUPPORT from the AF_UNSPEC call and stop filtering to a
single address, but FreeBSD 14.3 returns EAFNOSUPPORT from AF_UNSPEC and does
not change the socket behavior. The call with the NULL address also returns
EINVAL on FreeBSD and does not dissolve the association like the man page would
imply. Can you please advise if this is the expected behavior?

Thank you

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to