mn_basic_ip6_address.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef __MINLIBNET_BASIC_IP6_ADDRESS_H__
19 #define __MINLIBNET_BASIC_IP6_ADDRESS_H__
20 
21 #include "../mn_config.hpp"
22 
23 #if MN_THREAD_CONFIG_NET_IPADDRESS6_ENABLE == MN_THREAD_CONFIG_YES
24 
25 #define MNNET_IPV6_ADDRESS_BYTES 16
26 #define MNNET_NUMBER_OF_LABELS 8
27 
28 #define MNNET_IPV6_NEW_ARRAY_NULL new uint8_t[MNNET_IPV6_ADDRESS_BYTES]{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
29 #define MNNET_IPV6_NEW_ARRAY_LOOP new uint8_t[MNNET_IPV6_ADDRESS_BYTES]{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }
30 
31 #if MN_THREAD_CONFIG_NET_IPADDRESS6_USE_SCOPEID == MN_THREAD_CONFIG_YES
32 #define MNNET_IPV6_ADDRESS_ANY mn::net::basic_ip6_address( MNNET_IPV6_NEW_ARRAY_NULL, 0 )
33 #define MNNET_IPV6_ADDRESS_LOOPBACK mn::net::basic_ip6_address( MNNET_IPV6_NEW_ARRAY_LOOP, 0 )
34 #else
35 #define MNNET_IPV6_ADDRESS_ANY mn::net::basic_ip6_address( MNNET_IPV6_NEW_ARRAY_NULL )
36 #define MNNET_IPV6_ADDRESS_LOOPBACK mn::net::basic_ip6_address( MNNET_IPV6_NEW_ARRAY_LOOP )
37 #endif // MN_THREAD_CONFIG_NET_IPADDRESS6_USE_SCOPEID
38 
39 #define MNNET_IPV6_ADDRESS_NONE MNNET_IPV6_ADDRESS_ANY
40 
41 #include "mn_basic_ip4_address.hpp"
42 
43 namespace mn {
44  namespace net {
50  protected:
54  union {
55  uint8_t m_Numbers[MNNET_IPV6_ADDRESS_BYTES];
56  uint16_t as_short[8];
57  uint32_t as_int[4];
58  };
59  #if MN_THREAD_CONFIG_NET_IPADDRESS6_USE_SCOPEID == MN_THREAD_CONFIG_YES
63  long m_ScopeId;
64  #endif
65  public:
71 
76  basic_ip6_address(uint32_t ax, uint32_t bx, uint32_t cx, uint32_t dx) noexcept;
77 
82  basic_ip6_address(uint32_t iarray[4]) noexcept;
87 #if MN_THREAD_CONFIG_NET_IPADDRESS6_USE_SCOPEID == MN_THREAD_CONFIG_YES
88  basic_ip6_address(uint8_t address[16], int scopid) noexcept;
89 #else
90  basic_ip6_address(uint8_t address[16]) noexcept;
91 #endif
97  basic_ip6_address(const char* str_ip) noexcept;
103  basic_ip6_address(const basic_ip6_address& ipOther) noexcept;
104 
105  #if MN_THREAD_CONFIG_NET_IPADDRESS6_USE_SCOPEID == MN_THREAD_CONFIG_YES
110  long get_scopeid()
111  { return m_ScopeId; }
116  void set_scopeid(long scope_id)
117  { m_ScopeId = id; }
118  #endif
123  const char* to_string();
128  uint8_t* get_bytes();
133  uint32_t* get_array() { return as_int; }
138  int length() const;
144  virtual uint32_t get_int(int index) { return index < 4 ? as_int[index] : 0; }
145 
151  virtual uint32_t get_int(int index) const { return index < 4 ? as_int[index] : 0; }
152 
156  inline void set_zero()
157  { as_int[0] = 0; as_int[1] = 0; as_int[2] = 0; as_int[3] = 0; }
161  inline void set_loopback()
162  { as_int[0] = 0; as_int[1] = 0; as_int[2] = 0; as_int[3] = PP_HTONL(0x00000001UL);; }
163 
168  virtual bool is_loopback() { return (*this == MNNET_IPV6_ADDRESS_LOOPBACK); }
172  virtual bool is_broadcast() { return false; }
177  virtual bool is_multicast() { return ( (as_short[0] & 0xFF00 ) == 0xFF00 ); }
178 
179  virtual bool is_any() { return *this == MNNET_IPV6_ADDRESS_ANY; }
184  virtual bool is_linklocal() { return ( (as_short[0] & 0xFFC0 ) == 0xFE80 ); }
189  virtual bool is_sitelocal() { return ( (as_short[0] & 0xFFC0 ) == 0xFEC0 ); }
194  virtual bool is_toredo() { return ( as_short[0] == 0x2001 && as_short[1] == 0 ); }
199  virtual bool is_ip4mapped();
204  virtual bool is_equel(const basic_ip6_address& ipOther);
210  virtual bool is_equel(const basic_ip6_address& ipOther, const basic_ip6_address& mask);
211 
213 
214  uint8_t operator[](int index) const { return m_Numbers[index]; }
215  uint8_t& operator[](int index) { return m_Numbers[index]; }
216  bool operator == (const basic_ip6_address& ipOther){ return is_equel(ipOther); }
217  bool operator != (const basic_ip6_address& ipOther){ return !is_equel(ipOther); }
218 
219  basic_ip6_address operator&(const basic_ip6_address& other)const;
220  basic_ip6_address operator|(const basic_ip6_address& other)const;
221  basic_ip6_address operator^(const basic_ip6_address& other)const;
223 
224 
231  return basic_ip4_address(lwip_ntohl(ip6.as_int[3]) );
232  }
239  return basic_ip6_address(0x00000000, 0x00000000, htonl(0xffff), (uint32_t)ip4);
240  }
244  static basic_ip6_address netmask(int mask);
245 
246  virtual void swap(basic_ip6_address& rhs) noexcept {
248  mn::swap<uint32_t>(as_int[0], rhs.as_int[0]);
249  mn::swap<uint32_t>(as_int[1], rhs.as_int[1]);
250  mn::swap<uint32_t>(as_int[2], rhs.as_int[2]);
251  mn::swap<uint32_t>(as_int[3], rhs.as_int[3]);
252  }
253  };
254  }
255 }
256 
257 #endif // MN_THREAD_CONFIG_NET_IPADDRESS6_ENABLE
258 
259 #endif // __MINLIBNET_BASIC_IP6_ADDRESS_H__
This class represents an internet (IP) version 6 host address.
Definition: mn_basic_ip4_address.hpp:38
This class represents an internet (IP) version 6 host address.
Definition: mn_basic_ip6_address.hpp:49
virtual bool is_toredo()
Definition: mn_basic_ip6_address.hpp:194
virtual bool is_any()
Definition: mn_basic_ip6_address.hpp:179
virtual uint32_t get_int(int index) const
Get the ip section on the given position as int.
Definition: mn_basic_ip6_address.hpp:151
virtual void swap(basic_ip6_address &rhs) noexcept
Definition: mn_basic_ip6_address.hpp:246
basic_ip6_address operator^(const basic_ip6_address &other) const
Definition: mn_basic_ip6_address.cpp:256
virtual bool is_ip4mapped()
Definition: mn_basic_ip6_address.cpp:156
uint8_t & operator[](int index)
Definition: mn_basic_ip6_address.hpp:215
virtual uint32_t get_int(int index)
Get the ip section on the given position as int.
Definition: mn_basic_ip6_address.hpp:144
uint8_t * get_bytes()
Definition: mn_basic_ip6_address.cpp:133
static basic_ip6_address netmask(int mask)
Definition: mn_basic_ip6_address.cpp:205
bool operator!=(const basic_ip6_address &ipOther)
Definition: mn_basic_ip6_address.hpp:217
basic_ip6_address()
construct a ip6 address (any)
Definition: mn_basic_ip6_address.cpp:30
uint32_t * get_array()
Definition: mn_basic_ip6_address.hpp:133
uint8_t operator[](int index) const
Definition: mn_basic_ip6_address.hpp:214
basic_ip6_address(uint8_t address[16]) noexcept
construct a ip6 address from array
basic_ip6_address operator|(const basic_ip6_address &other) const
Definition: mn_basic_ip6_address.cpp:246
static basic_ip4_address to_ip4(const basic_ip6_address &ip6)
Definition: mn_basic_ip6_address.hpp:230
basic_ip6_address & operator=(const basic_ip6_address &ip)
Definition: mn_basic_ip6_address.cpp:193
virtual bool is_multicast()
Definition: mn_basic_ip6_address.hpp:177
basic_ip6_address operator~() const
Definition: mn_basic_ip6_address.cpp:266
virtual bool is_linklocal()
Definition: mn_basic_ip6_address.hpp:184
virtual bool is_broadcast()
Definition: mn_basic_ip6_address.hpp:172
virtual bool is_loopback()
Definition: mn_basic_ip6_address.hpp:168
bool operator==(const basic_ip6_address &ipOther)
Definition: mn_basic_ip6_address.hpp:216
const char * to_string()
Definition: mn_basic_ip6_address.cpp:223
int length() const
Returns the length in bytes of the ip address.
Definition: mn_basic_ip6_address.cpp:149
basic_ip6_address operator&(const basic_ip6_address &other) const
Definition: mn_basic_ip6_address.cpp:236
void set_zero()
set the ip to zero
Definition: mn_basic_ip6_address.hpp:156
virtual bool is_sitelocal()
Definition: mn_basic_ip6_address.hpp:189
virtual bool is_equel(const basic_ip6_address &ipOther)
Definition: mn_basic_ip6_address.cpp:167
void set_loopback()
set the ip to loopback
Definition: mn_basic_ip6_address.hpp:161
static basic_ip6_address from_ip4(const basic_ip4_address &ip4)
Create a new basic_ip6_address from a given IPv4 address.
Definition: mn_basic_ip6_address.hpp:238
Wrapper class around lwip implementation of a ip address.
Definition: mn_basic_ip_address.hpp:65
virtual void swap(basic_ip_address &rhs) noexcept
Definition: mn_basic_ip_address.hpp:108
#define MNNET_IPV6_ADDRESS_ANY
Definition: mn_basic_ip6_address.hpp:35
#define MNNET_IPV6_ADDRESS_LOOPBACK
Definition: mn_basic_ip6_address.hpp:36
#define MNNET_IPV6_ADDRESS_BYTES
Definition: mn_basic_ip6_address.hpp:25
Definition: mn_allocator_typetraits.hpp:25