static bool AsciiToIpv6Host()

in simulation/src/network/utils/ipv6-address.cc [150:279]


static bool AsciiToIpv6Host (const char *address, uint8_t addr[16])
{
  NS_LOG_FUNCTION (address << &addr);
  static const char xdigits_l[] = "0123456789abcdef";
  static const char xdigits_u[] = "0123456789ABCDEF";
  unsigned char tmp[16];
  unsigned char* tp = tmp;
  unsigned char* const endp = tp + 16;
  unsigned char* colonp = 0;
  const char* xdigits = 0;
#if 0
  const char* curtok = 0;
#endif
  int ch = 0;
  int seen_xdigits = 0;
  unsigned int val = 0;

  memset (tp, 0x00, 16);

  /* Leading :: requires some special handling. */
  if (*address == ':')
    {
      if (*++address != ':')
        {
          return (0);
        }
    }
#if 0
  curtok = address;
#endif
  while ((ch = *address++) != '\0')
    {
      const char *pch = 0;

      if ((pch = strchr ((xdigits = xdigits_l), ch)) == 0)
        {
          pch = strchr ((xdigits = xdigits_u), ch);
        }

      if (pch != 0)
        {
          val <<= 4;
          val |= (pch - xdigits);

          if (++seen_xdigits > 4)
            {
              return (0);
            }
          continue;
        }
      if (ch == ':')
        {
#if 0
          curtok = address;
#endif

          if (!seen_xdigits)
            {
              if (colonp)
                return (0);
              colonp = tp;
              continue;
            }

          if (endp - tp < 2)
            {
              return (0);
            }

          *tp++ = (unsigned char)(val >> 8) & 0xff;
          *tp++ = (unsigned char) val & 0xff;
          seen_xdigits = 0;
          val = 0;
          continue;
        }

      /* \todo Handle IPv4 mapped address (2001::192.168.0.1) */
#if 0
      if (ch == '.' && (endp - tp > 3 /* NS_INADDRSZ - 1 */)) &&
          inet_pton4 (curtok, tp) > 0)
        {
          tp += 4 /*NS_INADDRSZ*/;
          seen_xdigits = 0;
          break; /* '\0' was seen by inet_pton4(). */
        }
#endif
      return (0);
    }

  if (seen_xdigits)
    {
      if ( endp - tp < 2)
        {
          return (0);
        }
      *tp++ = (unsigned char)(val >> 8) & 0xff;
      *tp++ = (unsigned char) val & 0xff;
    }

  if (colonp != 0)
    {
      /*
       * Since some memmove ()'s erroneously fail to handle
       * overlapping regions, we'll do the shift by hand.
       */
      const int n = tp - colonp;
      int i = 0;

      if (tp == endp)
        {
          return (0);
        }

      for (i = 1; i <= n; i++)
        {
          endp[-i] = colonp[n - i];
          colonp[n - i] = 0;
        }

      tp = endp;
    }

  if (tp != endp)
    {
      return (0);
    }

  memcpy (addr, tmp, 16);
  return (1);
}