mn_basic_ip4_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 __MINLIB_BASIC_IP4_ADDRESS_H__
19 #define __MINLIB_BASIC_IP4_ADDRESS_H__
20 
21 #include "../mn_config.hpp"
22 
23 #define MNNET_IPV4_ADDRESS_BYTES 4
24 
25 #define MNNET_IPV4_ADDRESS_ANY mn::net::basic_ip4_address( IPADDR_ANY )
26 #define MNNET_IPV4_ADDRESS_LOOPBACK mn::net::basic_ip4_address( IPADDR_LOOPBACK )
27 #define MNNET_IPV4_ADDRESS_BROADCAST mn::net::basic_ip4_address( IPADDR_BROADCAST )
28 #define MNNET_IPV4_ADDRESS_NONE mn::net::basic_ip4_address( IPADDR_NONE )
29 
30 #include "mn_basic_ip_address.hpp"
31 
32 namespace mn {
33  namespace net {
39  protected:
43  union {
44  uint8_t as_array[MNNET_IPV4_ADDRESS_BYTES];
45  uint32_t as_int32;
46  };
47  public:
56  explicit basic_ip4_address(uint32_t newAddress) noexcept;
57 
62  basic_ip4_address(uint8_t adress[MNNET_IPV4_ADDRESS_BYTES]) noexcept;
70  basic_ip4_address(uint8_t a, uint8_t b, uint8_t c, uint8_t d ) noexcept;
74  basic_ip4_address(const char* str_ip) noexcept;
80  basic_ip4_address(const basic_ip4_address& other) noexcept;
81 
86  uint8_t* get_bytes();
87 
92  int length() const;
96  inline void set_zero() { as_int32 = 0; }
100  inline void set_any() { as_int32 = IPADDR_ANY; }
104  inline void set_broadcast() { as_int32 = IPADDR_BROADCAST; }
108  inline void set_loop() { as_int32 = IPADDR_LOOPBACK; }
109 
114  virtual bool is_equel(const basic_ip4_address& ipOther);
119  inline bool is_loopback() { return (*this == MNNET_IPV4_ADDRESS_LOOPBACK); }
124  inline bool is_broadcast() { return (*this == MNNET_IPV4_ADDRESS_BROADCAST); }
129  inline bool is_multicast() { return ( as_int32 & PP_HTONL(0xf0000000UL) ) == PP_HTONL(0xe0000000UL); }
134  inline bool is_linklocal() { return ( as_int32 & PP_HTONL(0xf0000000UL) ) == PP_HTONL(0xa9fe0000UL); }
135 
140  virtual const char* to_string();
156  static uint8_t get_subnet_cidr(const basic_ip4_address& subnet);
157 
161  uint8_t operator[](int index) const { return as_array[index]; }
165  uint8_t& operator[](int index) { return as_array[index]; }
169  bool operator == (const basic_ip4_address& ipOther){ return is_equel(ipOther); }
173  bool operator != (const basic_ip4_address& ipOther){ return !is_equel(ipOther); }
177  bool operator == (const uint8_t* addr);
178 
182  basic_ip4_address& operator=(const uint32_t& address);
191  basic_ip4_address operator&(const basic_ip4_address& other)const;
196  basic_ip4_address operator|(const basic_ip4_address& other)const;
201  basic_ip4_address operator^(const basic_ip4_address& other)const;
210  operator uint32_t() const { return as_int32; }
211 
212  virtual void swap(basic_ip4_address& rhs) noexcept {
214  mn::swap<uint32_t>(as_int32, rhs.as_int32);
215  }
216  };
217  }
218 }
219 
220 #endif // __MINLIB_BASIC_IP4_ADDRESS_H__
This class represents an internet (IP) version 6 host address.
Definition: mn_basic_ip4_address.hpp:38
uint8_t * get_bytes()
Get the ip4 address as array.
Definition: mn_basic_ip4_address.cpp:96
bool is_linklocal()
Definition: mn_basic_ip4_address.hpp:134
void set_broadcast()
Set the ip as BROADCAST Address.
Definition: mn_basic_ip4_address.hpp:104
void set_any()
Set the ip as ANY Address.
Definition: mn_basic_ip4_address.hpp:100
basic_ip4_address calc_broadcast(const basic_ip4_address &subnet)
Definition: mn_basic_ip4_address.cpp:181
bool operator==(const basic_ip4_address &ipOther)
equel operator
Definition: mn_basic_ip4_address.hpp:169
void set_zero()
Get the ip4 address as array.
Definition: mn_basic_ip4_address.hpp:96
basic_ip4_address operator~() const
arametic NICHT operator
Definition: mn_basic_ip4_address.cpp:163
bool is_loopback()
Definition: mn_basic_ip4_address.hpp:119
virtual bool is_equel(const basic_ip4_address &ipOther)
Definition: mn_basic_ip4_address.cpp:110
void set_loop()
Set the ip as LOOPBACK Address.
Definition: mn_basic_ip4_address.hpp:108
basic_ip4_address calc_network_id(const basic_ip4_address &subnet)
Definition: mn_basic_ip4_address.cpp:193
basic_ip4_address operator|(const basic_ip4_address &other) const
arametic OR operator
Definition: mn_basic_ip4_address.cpp:149
basic_ip4_address & operator=(const uint32_t &address)
Assigns an basic_ip4_address.
Definition: mn_basic_ip4_address.cpp:118
bool is_multicast()
Definition: mn_basic_ip4_address.hpp:129
static uint8_t get_subnet_cidr(const basic_ip4_address &subnet)
Get the subnet_cidr from the given netmask.
Definition: mn_basic_ip4_address.cpp:205
int length() const
Returns the length in bytes of the ip address.
Definition: mn_basic_ip4_address.cpp:103
basic_ip4_address operator&(const basic_ip4_address &other) const
arametic AND operator
Definition: mn_basic_ip4_address.cpp:142
bool operator!=(const basic_ip4_address &ipOther)
unequel operator
Definition: mn_basic_ip4_address.hpp:173
uint8_t operator[](int index) const
array get opertor on the uint8_t array[4]
Definition: mn_basic_ip4_address.hpp:161
basic_ip4_address()
Construct a none IPv4 basic_ip4_address.
Definition: mn_basic_ip4_address.cpp:27
uint8_t & operator[](int index)
array get opertor on the uint8_t array[4]
Definition: mn_basic_ip4_address.hpp:165
bool is_broadcast()
Definition: mn_basic_ip4_address.hpp:124
basic_ip4_address operator^(const basic_ip4_address &other) const
arametic XOR operator
Definition: mn_basic_ip4_address.cpp:156
virtual const char * to_string()
Returns a string containing a representation of the address in presentation format.
Definition: mn_basic_ip4_address.cpp:170
virtual void swap(basic_ip4_address &rhs) noexcept
Definition: mn_basic_ip4_address.hpp:212
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_IPV4_ADDRESS_LOOPBACK
Definition: mn_basic_ip4_address.hpp:26
#define MNNET_IPV4_ADDRESS_BROADCAST
Definition: mn_basic_ip4_address.hpp:27
#define MNNET_IPV4_ADDRESS_BYTES
Definition: mn_basic_ip4_address.hpp:23
Definition: mn_allocator_typetraits.hpp:25