/*
 * inetntop: return the : notation of a given IPv6 internet number.
 *           or the dotted-decimal notation for IPv4
 *           make sure the compressed representation (rfc 1884) isn't used.
 *
 * (c) 2000 Kurt Roeckx <q@ping.be>, Licensed under GNU GPL v2.
 */
char *inetntop(int af, const void *in, char *out, size_t the_size)
{
        static char local_dummy[128];

        if (the_size > sizeof(local_dummy))
        {
                the_size = sizeof(local_dummy);
        }

        if (!inet_ntop(af, in, local_dummy, the_size))
        {
                /* good that every function calling this one
                 * checks the return value ... NOT */
                return NULL;
        }       
        /* quick and dirty hack to give ipv4 just ipv4 instead of
         * ::ffff:ipv4 - Q */
        if (af == AF_INET6 && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)in))
        {
                char    *p;

                if (!(p = strstr(local_dummy, ":ffff:")) &&
                        !(p = strstr(local_dummy, ":FFFF:")))
                {
                        return NULL;    /* crash and burn */
                }
                strcpy(out, p + 6);
                return out;
        }
        if (strstr(local_dummy, "::"))
            {
                char cnt = 0, *cp = local_dummy, *op = out;

                while (*cp)
                    {
                        if (*cp == ':')
                                cnt += 1;
                        if (*cp++ == '.')
                            {
                                cnt += 1;
                                break;
                            }
                    }
                cp = local_dummy;
                while (*cp)
                    {
                        *op++ = *cp++;
                        if (*(cp-1) == ':' && *cp == ':')
                            {
                                if ((cp-1) == local_dummy)
                                    {
                                        op--;
                                        *op++ = '0';
                                        *op++ = ':';
                                    }

                                *op++ = '0';
                                while (cnt++ < 7)
                                    {
                                        *op++ = ':';
                                        *op++ = '0';
                                    }
                            }
                    }
                if (*(op-1)==':') *op++ = '0';
                *op = '\0';
            }
        else
                bcopy(local_dummy, out, the_size);

        return out;
}