#include 
#include 
#include 
#include 
#include &#34;sys/socket.h&#34;
#include 
#include 
#include 
#include 
#include 
#include 

int select_ipv4_address_on_host(struct sockaddr_in *ipv4_addr) {
    struct ifaddrs *addrs = NULL;
    if (getifaddrs(&amp;addrs) != 0) {
        perror(&#34;could not get interface addresses&#34;);
        return -1;
    }
    int ret_val = -1;
    // iterate over them and choose a non-loopback address from among them
    for (struct ifaddrs *ifaddr = addrs; ifaddr != NULL; ifaddr = ifaddr-&gt;ifa_next) {
        if (ifaddr-&gt;ifa_addr == NULL || (ifaddr-&gt;ifa_flags &amp; IFF_UP) != IFF_UP) {
            // only interested in interfaces that are UP
            continue;
        }
        if (ifaddr-&gt;ifa_flags &amp; IFF_LOOPBACK) {
            // skip loopback address
            continue;
        }
        if (ifaddr-&gt;ifa_addr-&gt;sa_family == AF_INET) {
            // found an IPv4 address to use
            printf(&#34;chose %s network interface\n&#34;, ifaddr-&gt;ifa_name);
            ipv4_addr-&gt;sin_addr = ((struct sockaddr_in *) ifaddr-&gt;ifa_addr)-&gt;sin_addr;
            ipv4_addr-&gt;sin_port = 0;
            ipv4_addr-&gt;sin_family = ((struct sockaddr_in *) ifaddr-&gt;ifa_addr)-&gt;sin_family;
            ipv4_addr-&gt;sin_len = ((struct sockaddr_in *) ifaddr-&gt;ifa_addr)-&gt;sin_len;
            ret_val = 0;
        }
    }
    if (addrs != NULL) {
        freeifaddrs(addrs);
    }
    return ret_val;
}

int bind_to_ipv4_mapped_ipv6addr(int sock_fd, struct sockaddr_in ipv4addr, struct sockaddr_in6 *bound_addr) {
    const long addr_as_long = ntohl(ipv4addr.sin_addr.s_addr);
    struct sockaddr_in6 sa6;
    memset((char *)&amp;sa6, 0, sizeof(sa6));
    char caddr[16];
    memset((char *)caddr, 0, 16);
    caddr[10] = 0xff; // mapped IPv4 address
    caddr[11] = 0xff; // mapped IPv4 address
    caddr[12] = addr_as_long &gt;&gt; 24 &amp; 0xff;
    caddr[13] = addr_as_long &gt;&gt; 16 &amp; 0xff;
    caddr[14] = addr_as_long &gt;&gt; 8 &amp; 0xff;
    caddr[15] = addr_as_long &amp; 0xff;
    sa6.sin6_family = AF_INET6;
    sa6.sin6_port = 0;
    memcpy((void *)&amp;sa6.sin6_addr, caddr, sizeof(struct in6_addr));
    const int res = bind(sock_fd, (struct sockaddr *) &amp;sa6, sizeof(sa6));
    if (res &lt; 0) {
        perror(&#34;failed to bind&#34;);
        return -1;
    }

    socklen_t len = sizeof(struct sockaddr_in6);
    if (getsockname(sock_fd, (struct sockaddr *) bound_addr, &amp;len) &lt; 0) {
        perror(&#34;Failed to get socket addr&#34;);
        return -1;
    }
    return 0;
}

int send_greeting(const char *greeting, int sock_fd, struct sockaddr *dest_addr, size_t dest_addr_len) {
    const size_t msg_len = strlen(greeting) + 1;
    const size_t num_bytes_sent = sendto(sock_fd, greeting, msg_len, 0, dest_addr, dest_addr_len);
    if (num_bytes_sent == -1) {
        perror(&#34;sendto() failed&#34;);
        return -1;
    }
    printf(&#34;sendto() succeeded, sent %lu bytes\n&#34;, num_bytes_sent);
    return 0;
}

// Creates a AF_INET6 datagram socket, marks it as dual socket (i.e. IPV6_V6ONLY = 0),
// then binds the socket to a IPv4-mapped IPv6 address (chosen on the host where this test runs).
//
// The test then uses sendto() to send some bytes. For the sake of this test, it uses the same IPv4-mapped
// IPv6 address as the destination address to sendto(). The test then waits for (a maximum of) 15 seconds to
// receive that sent message by calling recvfrom().
//
// The test passes on macos (x64 and aarch64) hosts of versions 12.x, 13.x, 14.x and 15.x upto 15.5.
// Only on macos 15.6.1 and the recent macos 26, the test fails. Specifically, the first message that is
// sent using sendto() is never sent (and thus the recvfrom()) times out. sendto() however returns 0,
// incorrectly indicating a successful send. Interesting, if you repeat sendto() a second message from the
// same bound socket to the exact same destination address, the send message is indeed correctly sent and
// received immediately by the recvfrom(). It&#39;s only the first message which goes missing (the test uses
// unique content in each message to be sure which exact message was received and it has been observed that
// only the second message is received and the first one lost).
//
// Logs collected using &#34;sudo log collect --last 2m&#34; (after the test program returns) shows the following log
// message, which seem relevant:
// ...
// default kernel  cfil_hash_entry_log:6088 :
//          [86868 a.out] 
//          lport 65051 fport 65051 laddr 192.168.1.2 faddr 192.168.1.2 hash 201AAC1
// default kernel  cfil_service_inject_queue:4472 CFIL: sosend() failed 22
// ...
//
int main() {
    printf(&#34;current process id: %ld parent process id: %ld\n&#34;, (long) getpid(), (long) getppid());
    int sock_fd = -1;
    int exit_code = EXIT_FAILURE;
    struct sockaddr_in chosen_ipv4_addr;
    if (select_ipv4_address_on_host(&amp;chosen_ipv4_addr) &lt; 0) {
        printf(&#34;failed to select an IPv4 address to bind to\n&#34;);
        goto end;
    }

    // create a (AF_INET6) datagram socket
    sock_fd = socket(AF_INET6, SOCK_DGRAM, 0);
    if (sock_fd &lt; 0) {
        perror(&#34;socket creation failed&#34;);
        goto end;
    }

    // mark it as dual socket
    int ipv6_only = 0; // dual socket
    if (setsockopt(sock_fd, IPPROTO_IPV6, IPV6_V6ONLY, &amp;ipv6_only, sizeof(int)) &lt; 0) {
        perror(&#34;failed to mark socket as dual socket&#34;);
        goto end;
    }
    // bind to the chosen IPv4 address represented as an IPv4 mapped IPv6 address
    struct sockaddr_in6 bound_addr;
    if (bind_to_ipv4_mapped_ipv6addr(sock_fd, chosen_ipv4_addr, &amp;bound_addr) &lt; 0) {
        printf(&#34;failed to bind datagram socket\n&#34;);
        goto end;
    }

    char bound_addr_str[INET6_ADDRSTRLEN] = {0};
    inet_ntop(AF_INET6, &amp;bound_addr.sin6_addr, (char *) &amp;bound_addr_str, INET6_ADDRSTRLEN);
    printf(&#34;created and bound a datagram dual socket to %s:%d\n&#34;, bound_addr_str, ntohs(bound_addr.sin6_port));

    // configure a receive timeout on the socket
    struct timeval timeout;
    timeout.tv_sec = 15;
    timeout.tv_usec = 0;
    if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &amp;timeout, sizeof(struct timeval)) &lt; 0) {
        perror(&#34;failed to set timeout&#34;);
        goto end;
    }

    // send (to self) and then expect to receive the message
    printf(&#34;%s:%d sendto() %s:%d\n&#34;,
           bound_addr_str, ntohs(bound_addr.sin6_port),
           bound_addr_str, ntohs(bound_addr.sin6_port));
    const int num_times_to_send = 2;
    int num_successful_receives = 0;
    for (int i = 1; i &lt;= num_times_to_send; i++) {
        printf(&#34;---- Attempt %d ----\n&#34;, i);
        char *greeting;
        asprintf(&amp;greeting, &#34;hello %d&#34;, i);
        if (greeting == NULL) {
            printf(&#34;failed to create a greeting&#34;);
            goto end;
        }
        printf(&#34;sending greeting \&#34;%s\&#34;\n&#34;, greeting);
        struct sockaddr_in6 dest_addr = bound_addr; // send to self
        if (send_greeting(greeting, sock_fd, (struct sockaddr *) &amp;dest_addr, sizeof(dest_addr)) &lt; 0) {
            free(greeting);
            goto end;
        }
        free(greeting);
        printf(&#34;calling recvfrom()\n&#34;);
        int bufsize = 10;
        void *buffer = malloc(bufsize);
        struct sockaddr from;
        socklen_t from_size = sizeof(struct sockaddr);
        const ssize_t num_rcved = recvfrom(sock_fd, buffer, bufsize, 0, &amp;from, &amp;from_size);
        if (num_rcved &lt; 0) {
            if (errno == EAGAIN) {
                printf(&#34;receive timed out\n&#34;);
            } else {
                perror(&#34;receive failed&#34;);
            }
        } else {
            num_successful_receives++;
            printf(&#34;received %ld bytes: \&#34;%s\&#34;\n&#34;, num_rcved, (char *) buffer);
        }
        free(buffer);
        printf(&#34;---------------------\n&#34;);
    }
    exit_code = num_successful_receives == num_times_to_send ? EXIT_SUCCESS : EXIT_FAILURE;
end:
    if (sock_fd != -1) {
        close(sock_fd);
    }
    printf(exit_code != EXIT_SUCCESS ? &#34;TEST FAILED\n&#34; : &#34;TEST SUCCEEDED\n&#34;);
    return exit_code;
}
