arduino-audio-tools
Loading...
Searching...
No Matches
IPAddress.h
Go to the documentation of this file.
1#pragma once
2#include "stdint.h"
3#include <cstdint>
4#include <cstring>
5#include <cstdio>
6#include <string>
7
8namespace audio_tools {
9
28class IPAddress {
29 public:
30 // -------------------------------------------------------------------------
31 // Constructors
32 // -------------------------------------------------------------------------
33
35 IPAddress() { _addr.dword = 0; }
36
39 _addr.bytes[0] = a;
40 _addr.bytes[1] = b;
41 _addr.bytes[2] = c;
42 _addr.bytes[3] = d;
43 }
44
46 explicit IPAddress(uint32_t dword) { _addr.dword = dword; }
47
49 explicit IPAddress(const uint8_t octets[4]) {
50 std::memcpy(_addr.bytes, octets, 4);
51 }
52
54 explicit IPAddress(const char* str) {
55 _addr.dword = 0;
56 fromString(str);
57 }
58
59 // -------------------------------------------------------------------------
60 // Assignment
61 // -------------------------------------------------------------------------
62
64 _addr.dword = dword;
65 return *this;
66 }
67
69 std::memcpy(_addr.bytes, octets, 4);
70 return *this;
71 }
72
73 IPAddress& operator=(const char* str) {
74 fromString(str);
75 return *this;
76 }
77
78 // -------------------------------------------------------------------------
79 // Accessors
80 // -------------------------------------------------------------------------
81
83 uint8_t& operator[](int i) { return _addr.bytes[i]; }
84 uint8_t operator[](int i) const { return _addr.bytes[i]; }
85
87 operator uint32_t() const { return _addr.dword; }
88
90 explicit operator bool() const { return _addr.dword != 0; }
91
92 // -------------------------------------------------------------------------
93 // Comparison
94 // -------------------------------------------------------------------------
95
96 bool operator==(const IPAddress& other) const {
97 return _addr.dword == other._addr.dword;
98 }
99
100 bool operator!=(const IPAddress& other) const {
101 return !(*this == other);
102 }
103
104 bool operator==(uint32_t dword) const { return _addr.dword == dword; }
105 bool operator!=(uint32_t dword) const { return _addr.dword != dword; }
106
107 bool operator==(const uint8_t octets[4]) const {
108 return std::memcmp(_addr.bytes, octets, 4) == 0;
109 }
110
112 std::string toString() const {
113 char buf[16];
114 std::snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
115 _addr.bytes[0], _addr.bytes[1],
116 _addr.bytes[2], _addr.bytes[3]);
117 return buf;
118 }
119
122 bool fromString(const char* str) {
123 if (!str) return false;
124 unsigned a, b, c, d;
125 if (std::sscanf(str, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) return false;
126 if (a > 255 || b > 255 || c > 255 || d > 255) return false;
127 _addr.bytes[0] = (uint8_t)a;
128 _addr.bytes[1] = (uint8_t)b;
129 _addr.bytes[2] = (uint8_t)c;
130 _addr.bytes[3] = (uint8_t)d;
131 return true;
132 }
133
135 const uint8_t* raw() const { return _addr.bytes; }
136 uint8_t* raw() { return _addr.bytes; }
137
138 // -------------------------------------------------------------------------
139 // Well-known addresses
140 // -------------------------------------------------------------------------
141
142 static IPAddress any() { return IPAddress(0u); }
143 static IPAddress broadcast() { return IPAddress(255, 255, 255, 255); }
144 static IPAddress loopback() { return IPAddress(127, 0, 0, 1); }
145
146 private:
147 union {
150 } _addr;
151};
152
153const IPAddress INADDR_NONE(0,0,0,0);
154
155} // namespace audio_tools
Arduino-compatible IPAddress class implemented in pure C++.
Definition IPAddress.h:28
uint32_t dword
Definition IPAddress.h:149
IPAddress()
0.0.0.0
Definition IPAddress.h:35
IPAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
From four octets.
Definition IPAddress.h:38
std::string toString() const
Returns dotted-decimal string e.g. "192.168.1.1".
Definition IPAddress.h:112
bool operator==(uint32_t dword) const
Definition IPAddress.h:104
bool fromString(const char *str)
Definition IPAddress.h:122
uint8_t * raw()
Definition IPAddress.h:136
static IPAddress broadcast()
Definition IPAddress.h:143
uint8_t & operator[](int i)
Octet access (read/write)
Definition IPAddress.h:83
IPAddress(const char *str)
From a dotted-decimal string; leaves address as 0.0.0.0 on parse failure.
Definition IPAddress.h:54
IPAddress(uint32_t dword)
From a raw uint32 in host byte order (same as Arduino)
Definition IPAddress.h:46
bool operator!=(uint32_t dword) const
Definition IPAddress.h:105
uint8_t operator[](int i) const
Definition IPAddress.h:84
static IPAddress any()
Definition IPAddress.h:142
IPAddress & operator=(const char *str)
Definition IPAddress.h:73
IPAddress & operator=(const uint8_t octets[4])
Definition IPAddress.h:68
uint8_t bytes[4]
Definition IPAddress.h:148
IPAddress & operator=(uint32_t dword)
Definition IPAddress.h:63
bool operator==(const IPAddress &other) const
Definition IPAddress.h:96
bool operator!=(const IPAddress &other) const
Definition IPAddress.h:100
IPAddress(const uint8_t octets[4])
From a 4-byte array.
Definition IPAddress.h:49
bool operator==(const uint8_t octets[4]) const
Definition IPAddress.h:107
const uint8_t * raw() const
Raw byte pointer (read-only)
Definition IPAddress.h:135
static IPAddress loopback()
Definition IPAddress.h:144
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
const IPAddress INADDR_NONE(0, 0, 0, 0)
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508