UniRec  3.3.2
ipAddress.cpp
Go to the documentation of this file.
1 
9 #include "unirec++/ipAddress.hpp"
10 
11 #include <stdexcept>
12 
13 namespace Nemea {
14 
16  : ip(ip)
17 {
18 }
19 
20 IpAddress::IpAddress(const std::string& ipAddressAsString)
21 {
22  int ret = ip_from_str(ipAddressAsString.c_str(), &ip);
23  if (ret != 1) {
24  throw std::runtime_error("Invalid IP address");
25  }
26 }
27 
28 bool IpAddress::isIpv4() const { return ip_is4(&ip); }
29 
30 bool IpAddress::isIpv6() const { return ip_is6(&ip); }
31 
32 bool IpAddress::operator==(const IpAddress& other) const
33 {
34  return ip.ui64[0] == other.ip.ui64[0] && ip.ui64[1] == other.ip.ui64[1];
35 }
36 
37 IpAddress IpAddress::operator&(const IpAddress& other) const
38 {
39  ip_addr_t result;
40  result.ui64[0] = ip.ui64[0] & other.ip.ui64[0];
41  result.ui64[1] = ip.ui64[1] & other.ip.ui64[1];
42  return result;
43 }
44 
45 std::ostream& IpAddress::operator<<(std::ostream& os)
46 {
47  char buffer[INET6_ADDRSTRLEN];
48  ip_to_str(&ip, buffer);
49  os << buffer;
50  return os;
51 }
52 
53 } // namespace Nemea
bool isIpv6() const
Check if the stored IP address is an IPv6 address.
Definition: ipAddress.cpp:30
INLINE_IMPL int ip_from_str(const char *str, ip_addr_t *addr)
Definition: ipaddr.h:301
A struct representing an IP address with associated operations.
Definition: ipAddress.hpp:29
ip_addr_t ip
Definition: ipAddress.hpp:30
IpAddress(ip_addr_t ip=EMPTY_IP_ADDRESS)
Constructor to initialize IpAddress with an ip_addr_t value.
Definition: ipAddress.cpp:15
bool operator==(const IpAddress &other) const
Equality operator to compare two IpAddress objects.
Definition: ipAddress.cpp:32
IpAddress operator &(const IpAddress &other) const
Bitwise AND operator to perform a bitwise AND operation on two IpAddress objects. ...
Header file containing the definition of the IpAddress class.
INLINE_IMPL int ip_is6(const ip_addr_t *addr)
Definition: ipaddr.h:143
std::ostream & operator<<(std::ostream &os)
Output stream operator to stream an IpAddress object to an output stream.
Definition: ipAddress.cpp:45
INLINE_IMPL void ip_to_str(const ip_addr_t *addr, char *str)
Definition: ipaddr.h:325
INLINE_IMPL int ip_is4(const ip_addr_t *addr)
Definition: ipaddr.h:131
uint64_t ui64[2]
Definition: ipaddr.h:121
bool isIpv4() const
Check if the stored IP address is an IPv4 address.
Definition: ipAddress.cpp:28