UniRec 3.3.2
Loading...
Searching...
No Matches
ipAddress.cpp
Go to the documentation of this file.
1
10
11#include <stdexcept>
12
13namespace Nemea {
14
16 : ip(ip)
17{
18}
19
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
28bool IpAddress::isIpv4() const { return ip_is4(&ip); }
29
30bool IpAddress::isIpv6() const { return ip_is6(&ip); }
31
33{
34 return ip.ui64[0] == other.ip.ui64[0] && ip.ui64[1] == other.ip.ui64[1];
35}
36
38{
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
45std::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
uint64_t ui64[2]
Definition ipaddr.h:121
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
INLINE_IMPL int ip_from_str(const char *str, ip_addr_t *addr)
Definition ipaddr.h:301
INLINE_IMPL int ip_is6(const ip_addr_t *addr)
Definition ipaddr.h:143
Header file containing the definition of the IpAddress class.
constexpr ur_field_type_t getExpectedUnirecType()
Determines the expected UniRec field type for a given C++ type T.
A struct representing an IP address with associated operations.
Definition ipAddress.hpp:29
bool operator==(const IpAddress &other) const
Equality operator to compare two IpAddress objects.
Definition ipAddress.cpp:32
bool isIpv6() const
Check if the stored IP address is an IPv6 address.
Definition ipAddress.cpp:30
IpAddress operator&(const IpAddress &other) const
Bitwise AND operator to perform a bitwise AND operation on two IpAddress objects.
Definition ipAddress.cpp:37
std::ostream & operator<<(std::ostream &os)
Output stream operator to stream an IpAddress object to an output stream.
Definition ipAddress.cpp:45
bool isIpv4() const
Check if the stored IP address is an IPv4 address.
Definition ipAddress.cpp:28
IpAddress(ip_addr_t ip=EMPTY_IP_ADDRESS)
Constructor to initialize IpAddress with an ip_addr_t value.
Definition ipAddress.cpp:15