libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
ipv4address.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
11#include <cstring>
13#include <promeki/core/string.h>
14#include <promeki/core/result.h>
16
17#if defined(PROMEKI_PLATFORM_WINDOWS)
18# include <winsock2.h>
19# include <ws2tcpip.h>
20#elif !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
21# include <netinet/in.h>
22#endif
23
25
26class Ipv6Address;
27class MacAddress;
28class TextStream;
29
52 public:
60
69
71 static Ipv4Address any() { return Ipv4Address(); }
72
74 static Ipv4Address loopback() { return Ipv4Address(127, 0, 0, 1); }
75
77 static Ipv4Address broadcast() { return Ipv4Address(255, 255, 255, 255); }
78
80 Ipv4Address() : _addr(0) { }
81
87
96 _addr(static_cast<uint32_t>(a) << 24 |
97 static_cast<uint32_t>(b) << 16 |
98 static_cast<uint32_t>(c) << 8 |
100 {
101 }
102
104 bool isNull() const { return _addr == 0; }
105
107 bool isLoopback() const { return octet(0) == 127; }
108
110 bool isMulticast() const { return (octet(0) & 0xF0) == 0xE0; }
111
113 bool isLinkLocal() const { return octet(0) == 169 && octet(1) == 254; }
114
120 bool isPrivate() const {
121 uint8_t a = octet(0);
122 uint8_t b = octet(1);
123 if(a == 10) return true;
124 if(a == 172 && (b & 0xF0) == 16) return true;
125 if(a == 192 && b == 168) return true;
126 return false;
127 }
128
130 bool isBroadcast() const { return _addr == 0xFFFFFFFF; }
131
138 bool isInSubnet(Ipv4Address network, Ipv4Address mask) const {
139 return (_addr & mask._addr) == (network._addr & mask._addr);
140 }
141
148 bool isInSubnet(Ipv4Address network, int prefixLen) const;
149
154 uint32_t toUint32() const { return _addr; }
155
161 uint8_t octet(int index) const {
162 if(index < 0 || index > 3) return 0;
163 return static_cast<uint8_t>((_addr >> (24 - index * 8)) & 0xFF);
164 }
165
171
177
193
194#if !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
202
213#endif
214
216 bool operator==(const Ipv4Address &other) const { return _addr == other._addr; }
218 bool operator!=(const Ipv4Address &other) const { return _addr != other._addr; }
220 bool operator<(const Ipv4Address &other) const { return _addr < other._addr; }
221
222 private:
223 uint32_t _addr;
224};
225
228
Lightweight error code wrapper for the promeki library.
Definition error.h:39
IPv4 network address.
Definition ipv4address.h:51
bool isInSubnet(Ipv4Address network, int prefixLen) const
Returns true if this address is within the given subnet.
static Ipv4Address loopback()
Returns the loopback address (127.0.0.1).
Definition ipv4address.h:74
static Result< Ipv4Address > fromSockAddr(const struct sockaddr_in *sa)
Constructs from a sockaddr_in structure.
bool isPrivate() const
Returns true if the address is in a private range.
Definition ipv4address.h:120
uint32_t toUint32() const
Returns the address as a 32-bit integer in network byte order.
Definition ipv4address.h:154
static Ipv4Address any()
Returns the INADDR_ANY address (0.0.0.0).
Definition ipv4address.h:71
uint8_t octet(int index) const
Returns a single octet of the address.
Definition ipv4address.h:161
bool isMulticast() const
Returns true if the address is in the 224.0.0.0/4 multicast range.
Definition ipv4address.h:110
bool isNull() const
Returns true if the address is 0.0.0.0.
Definition ipv4address.h:104
static Ipv4Address fromUint32(uint32_t networkOrder)
Creates an address from a 32-bit integer in network byte order.
Definition ipv4address.h:66
bool operator<(const Ipv4Address &other) const
Less-than comparison for ordering.
Definition ipv4address.h:220
Ipv6Address toIpv6Mapped() const
Converts to an IPv4-mapped IPv6 address (::%ffff:a.b.c.d).
Ipv4Address()
Default constructor. Creates a null (0.0.0.0) address.
Definition ipv4address.h:80
String toString() const
Returns a dotted-quad string representation.
Error toSockAddr(struct sockaddr_in *sa) const
Fills a sockaddr_in with this address.
bool isLinkLocal() const
Returns true if the address is in the 169.254.0.0/16 link-local range.
Definition ipv4address.h:113
bool isBroadcast() const
Returns true if the address is 255.255.255.255.
Definition ipv4address.h:130
Ipv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
Constructs from four octets.
Definition ipv4address.h:95
bool operator!=(const Ipv4Address &other) const
Returns true if the addresses are not equal.
Definition ipv4address.h:218
static Result< Ipv4Address > fromString(const String &str)
Parses an IPv4 address from dotted-quad notation.
bool operator==(const Ipv4Address &other) const
Returns true if both addresses are equal.
Definition ipv4address.h:216
bool isInSubnet(Ipv4Address network, Ipv4Address mask) const
Returns true if this address is within the given subnet.
Definition ipv4address.h:138
MacAddress multicastMac() const
Returns the Ethernet multicast MAC address for this IPv4 multicast address.
Ipv4Address(uint32_t networkOrder)
Constructs from a 32-bit integer in network byte order.
Definition ipv4address.h:86
static Ipv4Address broadcast()
Returns the broadcast address (255.255.255.255).
Definition ipv4address.h:77
bool isLoopback() const
Returns true if the address is in the 127.0.0.0/8 range.
Definition ipv4address.h:107
IPv6 network address.
Definition ipv6address.h:52
Dynamic array container wrapping std::vector.
Definition list.h:40
IEEE 802 MAC (Ethernet hardware) address.
Definition macaddress.h:44
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
Formatted text I/O with encoding awareness.
Definition textstream.h:54
TextStream & operator<<(TextStream &stream, const Ipv4Address &addr)
Writes a dotted-quad IPv4 address to the stream.
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19