separate PIEthernet::Address to PINetworkAddress, typedef PIEthernet::Address to PINetworkAddress and mark as deprecated

PIValueTree new attributes for File and Dir
This commit is contained in:
2022-12-19 14:29:18 +03:00
parent 6c3f305562
commit 2ac215c19e
18 changed files with 505 additions and 328 deletions

View File

@@ -104,112 +104,6 @@ PIString getSockAddr(sockaddr * s) {
#endif
PIEthernet::Address::Address(uint _ip, ushort _port) {
set(_ip, _port);
}
PIEthernet::Address::Address(const PIString & ip_port) {
set(ip_port);
}
PIEthernet::Address::Address(const PIString & _ip, ushort _port) {
set(_ip, _port);
}
PIString PIEthernet::Address::ipString() const {
PIString ret = PIString::fromNumber(ip_b[0]);
ret += "." + PIString::fromNumber(ip_b[1]);
ret += "." + PIString::fromNumber(ip_b[2]);
ret += "." + PIString::fromNumber(ip_b[3]);
return ret;
}
PIString PIEthernet::Address::toString() const {
return ipString() + ":" + PIString::fromNumber(port_);
}
void PIEthernet::Address::setIP(uint _ip) {
ip_ = _ip;
}
void PIEthernet::Address::setIP(const PIString & _ip) {
initIP(_ip);
}
void PIEthernet::Address::setPort(ushort _port) {
port_ = _port;
}
void PIEthernet::Address::set(const PIString & ip_port) {
PIString _ip;
int p(0);
splitIPPort(ip_port, &_ip, &p);
port_ = p;
initIP(_ip);
}
void PIEthernet::Address::set(const PIString & _ip, ushort _port) {
initIP(_ip);
port_ = _port;
}
void PIEthernet::Address::set(uint _ip, ushort _port) {
ip_ = _ip;
port_ = _port;
}
void PIEthernet::Address::clear() {
ip_ = 0;
port_ = 0;
}
bool PIEthernet::Address::isNull() const {
return (ip_ == 0) && (port_ == 0);
}
PIEthernet::Address PIEthernet::Address::resolve(const PIString & host_port) {
PIString host;
int port(0);
splitIPPort(host_port, &host, &port);
return resolve(host, port);
}
PIEthernet::Address PIEthernet::Address::resolve(const PIString & host, ushort port) {
Address ret(0, port);
hostent * he = gethostbyname(host.dataAscii());
if (!he) return ret;
if (he->h_addr_list[0]) ret.setIP(*((uint *)(he->h_addr_list[0])));
return ret;
}
void PIEthernet::Address::splitIPPort(const PIString & ipp, PIString * _ip, int * _port) {
// piCout << "parse" << ipp;
int sp = ipp.findLast(":");
if (_ip != 0) *_ip = (sp >= 0 ? ipp.left(sp) : ipp);
if (_port != 0 && sp >= 0) *_port = ipp.right(ipp.length() - ipp.find(":") - 1).toInt();
}
void PIEthernet::Address::initIP(const PIString & _ip) {
ip_ = inet_addr(_ip.dataAscii());
}
REGISTER_DEVICE(PIEthernet)
@@ -344,8 +238,8 @@ PIString PIEthernet::applyMask(const PIString & ip, const PIString & mask) {
}
PIEthernet::Address PIEthernet::applyMask(const PIEthernet::Address & ip, const PIEthernet::Address & mask) {
Address ret(ip);
PINetworkAddress PIEthernet::applyMask(const PINetworkAddress & ip, const PINetworkAddress & mask) {
PINetworkAddress ret(ip);
ret.ip_ &= mask.ip_;
return ret;
}
@@ -358,8 +252,8 @@ PIString PIEthernet::getBroadcast(const PIString & ip, const PIString & mask) {
}
PIEthernet::Address PIEthernet::getBroadcast(const PIEthernet::Address & ip, const PIEthernet::Address & mask) {
Address ret(ip);
PINetworkAddress PIEthernet::getBroadcast(const PINetworkAddress & ip, const PINetworkAddress & mask) {
PINetworkAddress ret(ip);
ret.ip_ |= ~mask.ip_;
return ret;
}
@@ -630,7 +524,7 @@ bool PIEthernet::listen(bool threaded) {
}
bool PIEthernet::listen(const PIEthernet::Address & addr, bool threaded) {
bool PIEthernet::listen(const PINetworkAddress & addr, bool threaded) {
setReadAddress(addr);
return listen(threaded);
}
@@ -645,16 +539,16 @@ bool PIEthernet::send(const void * data, int size, bool threaded) {
}
bool PIEthernet::send(const PIEthernet::Address & addr, const void * data, int size, bool threaded) {
bool PIEthernet::send(const PINetworkAddress & addr, const void * data, int size, bool threaded) {
addr_s = addr;
if (threaded) {
writeThreaded(data, size);
return true;
}
Address pa = addr_s;
addr_s = addr;
int wr = write(data, size);
addr_s = pa;
PINetworkAddress pa = addr_s;
addr_s = addr;
int wr = write(data, size);
addr_s = pa;
return (wr == size);
}
@@ -668,15 +562,15 @@ bool PIEthernet::send(const PIByteArray & data, bool threaded) {
}
bool PIEthernet::send(const PIEthernet::Address & addr, const PIByteArray & data, bool threaded) {
bool PIEthernet::send(const PINetworkAddress & addr, const PIByteArray & data, bool threaded) {
if (threaded) {
writeThreaded(data);
return true;
}
Address pa = addr_s;
addr_s = addr;
int wr = write(data);
addr_s = pa;
PINetworkAddress pa = addr_s;
addr_s = addr;
int wr = write(data);
addr_s = pa;
return (wr == data.size_s());
}
@@ -1302,10 +1196,10 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
}
PIEthernet::Address PIEthernet::interfaceAddress(const PIString & interface_) {
PINetworkAddress PIEthernet::interfaceAddress(const PIString & interface_) {
#if defined(WINDOWS) || defined(MICRO_PIP)
piCout << "[PIEthernet] Not implemented, use \"PIEthernet::allAddresses\" or \"PIEthernet::interfaces\" instead";
return Address();
return PINetworkAddress();
#else
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
@@ -1314,23 +1208,23 @@ PIEthernet::Address PIEthernet::interfaceAddress(const PIString & interface_) {
ioctl(s, SIOCGIFADDR, &ifr);
::close(s);
struct sockaddr_in * sa = (struct sockaddr_in *)&ifr.ifr_addr;
return Address(uint(sa->sin_addr.s_addr));
return PINetworkAddress(uint(sa->sin_addr.s_addr));
#endif
}
PIVector<PIEthernet::Address> PIEthernet::allAddresses() {
PIVector<PINetworkAddress> PIEthernet::allAddresses() {
PIEthernet::InterfaceList il = interfaces();
PIVector<Address> ret;
PIVector<PINetworkAddress> ret;
bool has_127 = false;
piForeachC(PIEthernet::Interface & i, il) {
if (i.address == "127.0.0.1") has_127 = true;
Address a(i.address);
PINetworkAddress a(i.address);
if (a.ip() == 0) continue;
ret << a;
}
// piCout << "[PIEthernet::allAddresses]" << al;
if (!has_127) ret << Address("127.0.0.1");
if (!has_127) ret << PINetworkAddress("127.0.0.1");
return ret;
}

View File

@@ -27,7 +27,7 @@
#define PIETHERNET_H
#include "piiodevice.h"
#include "pitimer.h"
#include "pinetworkaddress.h"
#ifdef ANDROID
struct
@@ -63,74 +63,7 @@ public:
DisonnectOnTimeout /** Disconnect TCP connection on read timeout expired. Disabled by default */ = 0x20
};
//! \brief IPv4 network address, IP and port
class PIP_EXPORT Address {
friend class PIEthernet;
public:
//! Contructs %Address with binary representation of IP and port
Address(uint ip = 0, ushort port = 0);
//! Contructs %Address with string representation "i.i.i.i:p"
Address(const PIString & ip_port);
//! Contructs %Address with IP string representation "i.i.i.i" and port
Address(const PIString & ip, ushort port);
//! Returns binary IP
uint ip() const { return ip_; }
//! Returns port
ushort port() const { return port_; }
//! Returns string IP
PIString ipString() const;
//! Returns string representation of IP and port "i.i.i.i:p"
PIString toString() const;
//! Set address IP
void setIP(uint ip);
//! Set address IP
void setIP(const PIString & ip);
//! Set address port
void setPort(ushort port);
//! Set address IP and port, "i.i.i.i:p"
void set(const PIString & ip_port);
//! Set address IP and port, "i.i.i.i"
void set(const PIString & ip, ushort port);
//! Set address binary IP and port
void set(uint ip, ushort port);
//! Set IP and port to 0
void clear();
//! Returns if IP and port is 0
bool isNull() const;
//! Resolve hostname "host:port" and return it address or null address on error
static Address resolve(const PIString & host_port);
//! Resolve hostname "host" with port "port" and return it address or null address on error
static Address resolve(const PIString & host, ushort port);
static void splitIPPort(const PIString & ipp, PIString * ip, int * port);
private:
void initIP(const PIString & _ip);
union {
uint ip_;
uchar ip_b[4];
};
ushort port_;
};
typedef ::PINetworkAddress Address DEPRECATEDM("use PINetworkAddress instead");
//! Contructs %PIEthernet with type "type", read address "ip_port" and parameters "params"
explicit PIEthernet(Type type,
@@ -154,7 +87,7 @@ public:
}
//! Set read address
void setReadAddress(const Address & addr) {
void setReadAddress(const PINetworkAddress & addr) {
addr_r = addr;
setPath(addr_r.toString());
}
@@ -179,7 +112,7 @@ public:
void setSendAddress(const PIString & ip_port) { addr_s.set(ip_port); }
//! Set send address
void setSendAddress(const Address & addr) { addr_s = addr; }
void setSendAddress(const PINetworkAddress & addr) { addr_s = addr; }
//! Set send IP
void setSendIP(const PIString & ip) { addr_s.setIP(ip); }
@@ -189,7 +122,7 @@ public:
//! Returns read address in format "i.i.i.i:p"
Address readAddress() const { return addr_r; }
PINetworkAddress readAddress() const { return addr_r; }
//! Returns read IP
PIString readIP() const { return addr_r.ipString(); }
@@ -199,7 +132,7 @@ public:
//! Returns send address in format "i.i.i.i:p"
Address sendAddress() const { return addr_s; }
PINetworkAddress sendAddress() const { return addr_s; }
//! Returns send IP
PIString sendIP() const { return addr_s.ipString(); }
@@ -209,7 +142,7 @@ public:
//! Returns address of last received UDP packet in format "i.i.i.i:p"
Address lastReadAddress() const { return addr_lr; }
PINetworkAddress lastReadAddress() const { return addr_lr; }
//! Returns IP of last received UDP packet
PIString lastReadIP() const { return addr_lr.ipString(); }
@@ -287,7 +220,7 @@ public:
}
//! Connect to TCP server with address "addr". Use only for TCP_Client
bool connect(const Address & addr, bool threaded = true) {
bool connect(const PINetworkAddress & addr, bool threaded = true) {
setPath(addr.toString());
return connect(threaded);
}
@@ -303,13 +236,13 @@ public:
bool listen(bool threaded = false);
//! Start listen for incoming TCP connections on address "ip":"port". Use only for TCP_Server
bool listen(const PIString & ip, int port, bool threaded = false) { return listen(PIEthernet::Address(ip, port), threaded); }
bool listen(const PIString & ip, int port, bool threaded = false) { return listen(PINetworkAddress(ip, port), threaded); }
//! Start listen for incoming TCP connections on address "ip_port". Use only for TCP_Server
bool listen(const PIString & ip_port, bool threaded = false) { return listen(PIEthernet::Address(ip_port), threaded); }
bool listen(const PIString & ip_port, bool threaded = false) { return listen(PINetworkAddress(ip_port), threaded); }
//! Start listen for incoming TCP connections on address "addr". Use only for TCP_Server
bool listen(const Address & addr, bool threaded = false);
bool listen(const PINetworkAddress & addr, bool threaded = false);
PIEthernet * client(int index) { return clients_[index]; }
int clientsCount() const { return clients_.size_s(); }
@@ -321,32 +254,32 @@ public:
//! Send data "data" with size "size" to address "ip":"port"
bool send(const PIString & ip, int port, const void * data, int size, bool threaded = false) {
return send(PIEthernet::Address(ip, port), data, size, threaded);
return send(PINetworkAddress(ip, port), data, size, threaded);
}
//! Send data "data" with size "size" to address "ip_port"
bool send(const PIString & ip_port, const void * data, int size, bool threaded = false) {
return send(PIEthernet::Address(ip_port), data, size, threaded);
return send(PINetworkAddress(ip_port), data, size, threaded);
}
//! Send data "data" with size "size" to address "addr"
bool send(const Address & addr, const void * data, int size, bool threaded = false);
bool send(const PINetworkAddress & addr, const void * data, int size, bool threaded = false);
//! Send data "data" to address \a sendAddress() for UDP or \a readAddress() for TCP_Client
bool send(const PIByteArray & data, bool threaded = false);
//! Send data "data" to address "ip":"port" for UDP
bool send(const PIString & ip, int port, const PIByteArray & data, bool threaded = false) {
return send(PIEthernet::Address(ip, port), data, threaded);
return send(PINetworkAddress(ip, port), data, threaded);
}
//! Send data "data" to address "ip_port" for UDP
bool send(const PIString & ip_port, const PIByteArray & data, bool threaded = false) {
return send(PIEthernet::Address(ip_port), data, threaded);
return send(PINetworkAddress(ip_port), data, threaded);
}
//! Send data "data" to address "addr" for UDP
bool send(const Address & addr, const PIByteArray & data, bool threaded = false);
bool send(const PINetworkAddress & addr, const PIByteArray & data, bool threaded = false);
bool canWrite() const override { return mode() & WriteOnly; }
@@ -460,17 +393,17 @@ public:
//! Returns all system network interfaces
static InterfaceList interfaces();
static PIEthernet::Address interfaceAddress(const PIString & interface_);
static PINetworkAddress interfaceAddress(const PIString & interface_);
//! Returns all system network IP addresses
static PIVector<PIEthernet::Address> allAddresses();
static PIVector<PINetworkAddress> allAddresses();
static PIString macFromBytes(const PIByteArray & mac);
static PIByteArray macToBytes(const PIString & mac);
static PIString applyMask(const PIString & ip, const PIString & mask);
static Address applyMask(const Address & ip, const Address & mask);
static PINetworkAddress applyMask(const PINetworkAddress & ip, const PINetworkAddress & mask);
static PIString getBroadcast(const PIString & ip, const PIString & mask);
static Address getBroadcast(const Address & ip, const Address & mask);
static PINetworkAddress getBroadcast(const PINetworkAddress & ip, const PINetworkAddress & mask);
//! \events
//! \{
@@ -540,7 +473,7 @@ protected:
PRIVATE_DECLARATION(PIP_EXPORT)
int sock, sock_s;
std::atomic_bool connected_, connecting_, listen_threaded, server_bounded;
mutable Address addr_r, addr_s, addr_lr;
mutable PINetworkAddress addr_r, addr_s, addr_lr;
Type eth_type;
PIThread server_thread_;
PIMutex clients_mutex;
@@ -580,19 +513,5 @@ inline bool operator==(const PIEthernet::Interface & v0, const PIEthernet::Inter
inline bool operator!=(const PIEthernet::Interface & v0, const PIEthernet::Interface & v1) {
return (v0.name != v1.name || v0.address != v1.address || v0.netmask != v1.netmask);
}
inline PICout operator<<(PICout s, const PIEthernet::Address & v) {
s.space();
s.saveAndSetControls(0);
s << "Address(" << v.toString() << ")";
s.restoreControls();
return s;
}
inline bool operator==(const PIEthernet::Address & v0, const PIEthernet::Address & v1) {
return (v0.ip() == v1.ip() && v0.port() == v1.port());
}
inline bool operator!=(const PIEthernet::Address & v0, const PIEthernet::Address & v1) {
return (v0.ip() != v1.ip() || v0.port() != v1.port());
}
#endif // PIETHERNET_H

View File

@@ -119,7 +119,7 @@ void PIPeer::PeerData::setDist(int dist) {
}
PIPeer::PeerInfo::PeerAddress::PeerAddress(const PIEthernet::Address & a, const PIEthernet::Address & m): address(a), netmask(m) {
PIPeer::PeerInfo::PeerAddress::PeerAddress(const PINetworkAddress & a, const PINetworkAddress & m): address(a), netmask(m) {
ping = -1.;
wait_ping = false;
last_ping = PISystemTime::current(true);
@@ -150,9 +150,9 @@ void PIPeer::PeerInfo::destroy() {
}
PIEthernet::Address PIPeer::PeerInfo::fastestAddress() const {
PINetworkAddress PIPeer::PeerInfo::fastestAddress() const {
double mp = -1.;
PIEthernet::Address ret;
PINetworkAddress ret;
piForeachC(PeerAddress & a, addresses) {
if (a.ping <= 0.) continue;
if ((mp < 0) || (mp > a.ping)) {
@@ -356,7 +356,7 @@ void PIPeer::initMBcasts(PIStringList al) {
eth_tcp_cli.startThreadedRead();
if (eths_mcast.isEmpty() && eths_bcast.isEmpty() && !eth_lo.isOpened())
piCoutObj << "Warning! Can`t find suitable network interface for multicast receive, check for exists at least one interface with "
"multicasting enabled!";
"multicasting enabled!";
// piCoutObj << "initMBcasts ok";
}
@@ -474,7 +474,7 @@ bool PIPeer::dataRead(const uchar * readed, ssize_t size) {
eth_mutex.lock();
// piCout << "dataRead lock";
if (type == 5) { // ping request
PIEthernet::Address addr;
PINetworkAddress addr;
PISystemTime time;
ba >> to >> from >> addr >> time;
// piCout << "ping request" << to << from << addr;
@@ -497,7 +497,7 @@ bool PIPeer::dataRead(const uchar * readed, ssize_t size) {
return true;
}
if (type == 6) { // ping request
PIEthernet::Address addr;
PINetworkAddress addr;
PISystemTime time, ptime, ctime = PISystemTime::current(true);
ba >> to >> from >> addr >> time;
// piCout << "ping reply" << to << from << addr;
@@ -725,7 +725,7 @@ bool PIPeer::mbcastRead(const uchar * data, ssize_t size) {
bool PIPeer::sendToNeighbour(PIPeer::PeerInfo * peer, const PIByteArray & ba) {
PIEthernet::Address addr = peer->fastestAddress();
PINetworkAddress addr = peer->fastestAddress();
// piCout << "[PIPeer] sendToNeighbour" << peer->name << addr << ba.size_s() << "bytes ...";
send_mutex.lock();
bool ok = eth_send.send(addr, ba);
@@ -1025,9 +1025,9 @@ void PIPeer::initNetwork() {
piMSleep(100);
// piCoutObj << self_info.addresses.size();
self_info.addresses.clear();
PIVector<PIEthernet::Address> al = PIEthernet::allAddresses();
PIVector<PINetworkAddress> al = PIEthernet::allAddresses();
PIStringList sl;
for (const PIEthernet::Address & a: al) {
for (const PINetworkAddress & a: al) {
sl << a.ipString();
}
initEths(sl);

View File

@@ -53,11 +53,10 @@ public:
~PeerInfo() {}
struct PIP_EXPORT PeerAddress {
PeerAddress(const PIEthernet::Address & a = PIEthernet::Address(),
const PIEthernet::Address & m = PIEthernet::Address("255.255.255.0"));
PeerAddress(const PINetworkAddress & a = PINetworkAddress(), const PINetworkAddress & m = PINetworkAddress("255.255.255.0"));
bool isAvailable() const { return ping > 0; }
PIEthernet::Address address;
PIEthernet::Address netmask;
PINetworkAddress address;
PINetworkAddress netmask;
double ping; // ms
bool wait_ping;
PISystemTime last_ping;
@@ -70,7 +69,7 @@ public:
bool isNeighbour() const { return dist == 0; }
int ping() const;
PIEthernet::Address fastestAddress() const;
PINetworkAddress fastestAddress() const;
protected:
void addNeighbour(const PIString & n) {