[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

(usagi-users 02105) multiproto bind() ?



hey,

 i am trying to bind on all available ipv4 and ipv6 addresses.
i tried,

-- snippet of code --

        hints.ai_flags = AI_PASSIVE;
        hints.ai_family = PF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        if ((n = getaddrinfo(NULL, "7710", &hints, &res)) != 0) {
                printf("%s\n", gai_strerror(n));
        }
        do {

            listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

            if (listenfd < 0)
               continue;

           setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &len, sizeof(len));

           if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
                 break;

        } while ((res = res->ai_next) != NULL);

it works fine, and i can telnet to my-host:7710 from ipv4 only and ipv6
only hosts also, but when i try to print to address family, it always
gives 28c on freebsd and 10c on Linux indicating AF_INET6

i tried :

-- snippet of code

 listen (listenfd,1024);
        caddr = malloc(res->ai_addrlen);

        for(;;) {

         slen  = res->ai_addrlen;
         ptr  = malloc(sizeof(int));
         *ptr = accept(listenfd, caddr, &slen);

       // using res->ai_family 'd give same result.
         printf("%uc\n",caddr->sa_family);
       }

i want to distinguish between clients coming to ipv4 or ipv6
and print client address ? if i would get sa_family correct
i can switch(family) { and case for AF_INET and AF_INET6 and
use inet_ntop to print address ? but i am getting same family
always (28c/bsd and 10c/linux --> AF_INET6)

any hints?

-- basit