//
// ip/impl/address_v6.ipp
// ~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef ASIO_IP_IMPL_ADDRESS_V6_IPP
#define ASIO_IP_IMPL_ADDRESS_V6_IPP

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

#include "asio/detail/config.hpp"
#include <cstring>
#include <stdexcept>
#include <typeinfo>
#include <boost/throw_exception.hpp>
#include "asio/detail/socket_ops.hpp"
#include "asio/detail/throw_error.hpp"
#include "asio/error.hpp"
#include "asio/ip/address_v6.hpp"

#include "asio/detail/push_options.hpp"

namespace asio {
namespace ip {

ASIO_DECL
address_v6::address_v6()
  : scope_id_(0)
{
  asio::detail::in6_addr_type tmp_addr = IN6ADDR_ANY_INIT;
  addr_ = tmp_addr;
}

ASIO_DECL
address_v6::address_v6(const address_v6::bytes_type& bytes,
    unsigned long scope_id)
  : scope_id_(scope_id)
{
#if UCHAR_MAX > 0xFF
  for (std::size_t i = 0; i < bytes.size(); ++i)
  {
    if (bytes[i] > 0xFF)
    {
      std::out_of_range ex("address_v6 from bytes_type");
      boost::throw_exception(ex);
    }
  }
#endif // UCHAR_MAX > 0xFF

  using namespace std; // For memcpy.
  memcpy(addr_.s6_addr, bytes.elems, 16);
}

ASIO_DECL
address_v6::address_v6(const address_v6& other)
  : addr_(other.addr_),
    scope_id_(other.scope_id_)
{
}

ASIO_DECL
address_v6& address_v6::operator=(const address_v6& other)
{
  addr_ = other.addr_;
  scope_id_ = other.scope_id_;
  return *this;
}

ASIO_DECL
address_v6::bytes_type address_v6::to_bytes() const
{
  using namespace std; // For memcpy.
  bytes_type bytes;
  memcpy(bytes.elems, addr_.s6_addr, 16);
  return bytes;
}

ASIO_DECL
std::string address_v6::to_string() const
{
  asio::error_code ec;
  std::string addr = to_string(ec);
  asio::detail::throw_error(ec);
  return addr;
}

ASIO_DECL
std::string address_v6::to_string(asio::error_code& ec) const
{
  char addr_str[asio::detail::max_addr_v6_str_len];
  const char* addr =
    asio::detail::socket_ops::inet_ntop(AF_INET6, &addr_, addr_str,
        asio::detail::max_addr_v6_str_len, scope_id_, ec);
  if (addr == 0)
    return std::string();
  return addr;
}

ASIO_DECL
address_v6 address_v6::from_string(const char* str)
{
  asio::error_code ec;
  address_v6 addr = from_string(str, ec);
  asio::detail::throw_error(ec);
  return addr;
}

ASIO_DECL
address_v6 address_v6::from_string(
    const char* str, asio::error_code& ec)
{
  address_v6 tmp;
  if (asio::detail::socket_ops::inet_pton(
        AF_INET6, str, &tmp.addr_, &tmp.scope_id_, ec) <= 0)
    return address_v6();
  return tmp;
}

ASIO_DECL
address_v6 address_v6::from_string(const std::string& str)
{
  return from_string(str.c_str());
}

ASIO_DECL
address_v6 address_v6::from_string(
    const std::string& str, asio::error_code& ec)
{
  return from_string(str.c_str(), ec);
}

ASIO_DECL
address_v4 address_v6::to_v4() const
{
  if (!is_v4_mapped() && !is_v4_compatible())
  {
    std::bad_cast ex;
    boost::throw_exception(ex);
  }

  address_v4::bytes_type v4_bytes = { { addr_.s6_addr[12],
    addr_.s6_addr[13], addr_.s6_addr[14], addr_.s6_addr[15] } };
  return address_v4(v4_bytes);
}

ASIO_DECL
bool address_v6::is_loopback() const
{
#if defined(__BORLANDC__)
  return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
      && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
      && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
      && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
      && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
      && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
      && (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
      && (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 1));
#else
  using namespace asio::detail;
  return IN6_IS_ADDR_LOOPBACK(&addr_) != 0;
#endif
}

ASIO_DECL
bool address_v6::is_unspecified() const
{
#if defined(__BORLANDC__)
  return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
      && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
      && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
      && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
      && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
      && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
      && (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
      && (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 0));
#else
  using namespace asio::detail;
  return IN6_IS_ADDR_UNSPECIFIED(&addr_) != 0;
#endif
}

ASIO_DECL
bool address_v6::is_link_local() const
{
  using namespace asio::detail;
  return IN6_IS_ADDR_LINKLOCAL(&addr_) != 0;
}

ASIO_DECL
bool address_v6::is_site_local() const
{
  using namespace asio::detail;
  return IN6_IS_ADDR_SITELOCAL(&addr_) != 0;
}

ASIO_DECL
bool address_v6::is_v4_mapped() const
{
  using namespace asio::detail;
  return IN6_IS_ADDR_V4MAPPED(&addr_) != 0;
}

ASIO_DECL
bool address_v6::is_v4_compatible() const
{
  using namespace asio::detail;
  return IN6_IS_ADDR_V4COMPAT(&addr_) != 0;
}

ASIO_DECL
bool address_v6::is_multicast() const
{
  using namespace asio::detail;
  return IN6_IS_ADDR_MULTICAST(&addr_) != 0;
}

ASIO_DECL
bool address_v6::is_multicast_global() const
{
  using namespace asio::detail;
  return IN6_IS_ADDR_MC_GLOBAL(&addr_) != 0;
}

ASIO_DECL
bool address_v6::is_multicast_link_local() const
{
  using namespace asio::detail;
  return IN6_IS_ADDR_MC_LINKLOCAL(&addr_) != 0;
}

ASIO_DECL
bool address_v6::is_multicast_node_local() const
{
  using namespace asio::detail;
  return IN6_IS_ADDR_MC_NODELOCAL(&addr_) != 0;
}

ASIO_DECL
bool address_v6::is_multicast_org_local() const
{
  using namespace asio::detail;
  return IN6_IS_ADDR_MC_ORGLOCAL(&addr_) != 0;
}

ASIO_DECL
bool address_v6::is_multicast_site_local() const
{
  using namespace asio::detail;
  return IN6_IS_ADDR_MC_SITELOCAL(&addr_) != 0;
}

ASIO_DECL
bool operator==(const address_v6& a1, const address_v6& a2)
{
  using namespace std; // For memcmp.
  return memcmp(&a1.addr_, &a2.addr_,
      sizeof(asio::detail::in6_addr_type)) == 0
    && a1.scope_id_ == a2.scope_id_;
}

ASIO_DECL
bool operator<(const address_v6& a1, const address_v6& a2)
{
  using namespace std; // For memcmp.
  int memcmp_result = memcmp(&a1.addr_, &a2.addr_,
      sizeof(asio::detail::in6_addr_type));
  if (memcmp_result < 0)
    return true;
  if (memcmp_result > 0)
    return false;
  return a1.scope_id_ < a2.scope_id_;
}

ASIO_DECL
address_v6 address_v6::loopback()
{
  address_v6 tmp;
  asio::detail::in6_addr_type tmp_addr = IN6ADDR_LOOPBACK_INIT;
  tmp.addr_ = tmp_addr;
  return tmp;
}

ASIO_DECL
address_v6 address_v6::v4_mapped(const address_v4& addr)
{
  address_v4::bytes_type v4_bytes = addr.to_bytes();
  bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF,
    v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
  return address_v6(v6_bytes);
}

ASIO_DECL
address_v6 address_v6::v4_compatible(const address_v4& addr)
{
  address_v4::bytes_type v4_bytes = addr.to_bytes();
  bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
  return address_v6(v6_bytes);
}

} // namespace ip
} // namespace asio

#include "asio/detail/pop_options.hpp"

#endif // ASIO_IP_IMPL_ADDRESS_V6_IPP
