strange bind(2) behavior

Discussion in 'Linux Networking' started by Mischa Diehm, May 11, 2004.

  1. Mischa Diehm

    Mischa Diehm Guest


    I was very suprised when I found out that it is possible to create a
    socket for AF_INET, build a sockaddr struct for a v6 address (take
    getaddrinfo for example) and give all that to bind.

    n = getaddrinfo("2::2", "4321", &hints, &res);
    socketfd = socket(PF_INET, SOCK_STREAM, 6);
    if(bind(socketfd, res->ai_addr, res->ai_addrlen)) {

    What I expected was that the kernel returns with an error but what I got
    was an IPv4 socket listening on The problem is that the
    sockaddr_in and sockaddr_in6 structures first differ in sin_addr(v4) and
    flowinfo (v6). As flowinfo is exactly 32bit and set to zero the kernel
    doesn't care and takes the v6 struct as a v4 struct. I think bind should
    exactly check if the length of the given parameter is correct. This little
    patch would go for a more precise check:


    < if (addr_len != sizeof(struct sockaddr_in))
    I don't know why there is a '<' comparison? Maybe someone of you can give me
    a hint.


    Mischa Diehm, May 11, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.