Skip to content

IPV6_V6ONLY socket option should fail if the socket is bound #982

@ericeil

Description

@ericeil

On build 14905:

Run the following C++ code:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>

int main(void)
{
    printf("Creating socket\n");
    int s = socket(AF_INET6, SOCK_STREAM, 0);
    if (s < 0)
        err(1, "%d", errno);

    printf("Seting V6ONLY=false\n");
    int v6only = 0;
    if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0)
        err(1, "%d", errno);

    printf("Binding socket\n");
    sockaddr_in6 addr;
    addr.sin6_family = AF_INET6;
    addr.sin6_addr = in6addr_any;
    addr.sin6_port = 0;
    addr.sin6_scope_id = 0;
    if (bind(s, (sockaddr*)&addr, sizeof(addr)) != 0)
        err(1, "%d", errno);

    printf("Setting V6ONLY=true.  We expect this to fail with EINVAL\n");
    v6only = 1;
    if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) == 0 || errno != EINVAL)
    {
        err(1, "Should have failed with EINVAL, but got %d", errno);
    }

    printf("Success!\n");
    return 0;
}

On Linux, attempting to change IPV6_V6ONLY after the socket is bound results in an EINVAL error. On Windows, this call returns success.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions