[MDEV-18010] Add classes Inet4 and Inet6 Created: 2018-12-14  Updated: 2019-10-07  Resolved: 2018-12-15

Status: Closed
Project: MariaDB Server
Component/s: Data types
Fix Version/s: 10.4.1

Type: Task Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Blocks
blocks MDEV-274 The data type for IPv6/IPv4 addresses... Closed

 Description   

To implement MDEV-274 easier, let's wrap the code in item_inetfunc.cc into classes Inet4 and Inet6.

The handler for the coming soon INET6 data type will reuse the new class Inet6.

The implementation for Inet6 will look approximately like this:

class Inet6
{
  char m_buffer[IN6_ADDR_SIZE];
  bool str_to_ipv6(const char *str, size_t str_length, CHARSET_INFO *cs);
public:
  // Initialize from a text representation
  Inet6(bool *error, const char *str, size_t length, CHARSET_INFO *cs)
  {
    *error= !str_to_ipv6(str, length, cs);
  }
  // Initialize from a binary representation
  Inet6(bool *error, const char *str, size_t length)
  {
    if (!(*error= length != sizeof(m_buffer)))
      memcpy(m_buffer, str, length);
  }
  Inet6(bool *error, Item *item);
  bool to_binary(String *to) const
  {
    return to->copy(m_buffer, sizeof(m_buffer), &my_charset_bin);
  }
  size_t to_string(char *dst, size_t dstsize) const;
  bool to_string(String *to) const
  {
    to->set_charset(&my_charset_latin1);
    if (to->alloc(INET6_ADDRSTRLEN))
      return true;
    to->length((uint32) to_string((char*) to->ptr(), INET6_ADDRSTRLEN));
    return false;
  }
  bool is_v4compat() const
  {
    static_assert(sizeof(in6_addr) == IN6_ADDR_SIZE, "unexpected in6_addr size");
    return IN6_IS_ADDR_V4COMPAT((struct in6_addr *) m_buffer);
  }
  bool is_v4mapped() const
  {
    static_assert(sizeof(in6_addr) == IN6_ADDR_SIZE, "unexpected in6_addr size");
    return IN6_IS_ADDR_V4MAPPED((struct in6_addr *) m_buffer);
  }
};

The implementation for Inet4 will look very similar.


Generated at Thu Feb 08 08:40:49 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.