BIG deep clean
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Ethernet, UDP/TCP Broadcast/Multicast
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
Ethernet, UDP/TCP Broadcast/Multicast
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "piincludes_p.h"
|
||||
#include "piethernet.h"
|
||||
@@ -216,7 +216,6 @@ PIEthernet::PIEthernet(): PIIODevice("", ReadWrite) {
|
||||
construct();
|
||||
setType(UDP);
|
||||
setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop | PIEthernet::KeepConnection);
|
||||
//if (type_ != UDP) init();
|
||||
}
|
||||
|
||||
|
||||
@@ -297,13 +296,9 @@ bool PIEthernet::init() {
|
||||
}
|
||||
if (params[PIEthernet::ReuseAddress]) ethSetsockoptBool(sock, SOL_SOCKET, SO_REUSEADDR);
|
||||
if (params[PIEthernet::Broadcast]) ethSetsockoptBool(sock, SOL_SOCKET, SO_BROADCAST);
|
||||
//if (type() == PIEthernet::TCP_Client) ethSetsockoptBool(sock, SOL_SOCKET, SO_KEEPALIVE);
|
||||
applyTimeouts();
|
||||
applyOptInt(IPPROTO_IP, IP_TTL, TTL());
|
||||
// piCoutObj << "inited" << path();
|
||||
//cout << "inited " << sock << ": bc = " << params << endl;
|
||||
//fcntl(sock, F_SETFL, 0/*O_NONBLOCK*/);
|
||||
//piCoutObj << "init" << sock;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -360,7 +355,6 @@ bool PIEthernet::openDevice() {
|
||||
init();
|
||||
if (sock == -1 || path().isEmpty()) return false;
|
||||
addr_r.set(path());
|
||||
//Address::splitIPPort(path(), &ip_, &port_);
|
||||
if (type() == TCP_Client)
|
||||
connecting_ = true;
|
||||
if (type() != UDP || mode() == PIIODevice::WriteOnly)
|
||||
@@ -387,7 +381,6 @@ bool PIEthernet::openDevice() {
|
||||
opened_ = true;
|
||||
while (!mcast_queue.isEmpty())
|
||||
joinMulticastGroup(mcast_queue.dequeue());
|
||||
//cout << "!" << endl;
|
||||
applyTimeouts();
|
||||
applyOptInt(IPPROTO_IP, IP_TTL, TTL());
|
||||
addr_lr.clear();
|
||||
@@ -479,9 +472,6 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) {
|
||||
return true;
|
||||
}
|
||||
PIFlags<Parameters> params = parameters();
|
||||
//#ifndef QNX
|
||||
//if (!params[Broadcast])
|
||||
//;piCoutObj << "Warning: \"Broadcast\" parameter not set, \"joinMulticastGroup(\"" << group << "\")\" may be useless!";
|
||||
addr_r.set(path());
|
||||
#ifndef FREERTOS
|
||||
struct ip_mreqn mreq;
|
||||
@@ -508,11 +498,6 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) {
|
||||
mreq.imr_interface.s_addr = addr_r.ip();
|
||||
#endif
|
||||
|
||||
/*#ifndef WINDOWS
|
||||
PIEthernet::InterfaceList il = interfaces();
|
||||
const PIEthernet::Interface * ci = il.getByAddress(ip_);
|
||||
if (ci != 0) mreq.imr_ifindex = ci->index;
|
||||
#endif*/
|
||||
//piCout << "join group" << group << "ip" << ip_ << "with index" << mreq.imr_ifindex << "socket" << sock;
|
||||
mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii());
|
||||
if (ethSetsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) {
|
||||
@@ -522,20 +507,6 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) {
|
||||
if (params[PIEthernet::MulticastLoop]) ethSetsockoptInt(sock, IPPROTO_IP, IP_MULTICAST_LOOP);
|
||||
applyOptInt(IPPROTO_IP, IP_MULTICAST_TTL, multicastTTL());
|
||||
if (!mcast_groups.contains(group)) mcast_groups << group;
|
||||
//#else
|
||||
// parseAddress(path(), &ip_, &port_);
|
||||
// struct ip_mreq mreq;
|
||||
// memset(&mreq, 0, sizeof(mreq));
|
||||
// mreq.imr_interface.s_addr = inet_addr(ip_.dataAscii());
|
||||
// mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii());
|
||||
// if (ethSetsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) {
|
||||
// piCoutObj << "Can`t join multicast group " << group << ", " << ethErrorString();
|
||||
// return false;
|
||||
// }
|
||||
// if (params[PIEthernet::MulticastLoop]) ethSetsockoptInt(sock, IPPROTO_IP, IP_MULTICAST_LOOP);
|
||||
// applyOptInt(IPPROTO_IP, IP_MULTICAST_TTL, multicastTTL());
|
||||
// if (!mcast_groups.contains(group)) mcast_groups << group;
|
||||
//#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -601,22 +572,6 @@ bool PIEthernet::connect(bool threaded) {
|
||||
connected();
|
||||
}
|
||||
return connected_;
|
||||
/*if (sock == -1) return false;
|
||||
memset(addr_, 0, sizeof(*addr_));
|
||||
parseAddress(path_, &ip_, &port_);
|
||||
PRIVATE->addr_.sin_port = htons(port_);
|
||||
PRIVATE->addr_.sin_addr.s_addr = inet_addr(ip_.data());
|
||||
PRIVATE->addr_.sin_family = AF_INET;
|
||||
#ifdef QNX
|
||||
PRIVATE->addr_.sin_len = sizeof(*addr_);
|
||||
#endif
|
||||
//piCoutObj << "connect to " << ip << ":" << port_;
|
||||
connected_ = (::connect(sock, (sockaddr * )addr_, sizeof(*addr_)) == 0);
|
||||
if (!connected_)
|
||||
piCoutObj << "Can`t connect to " << ip_ << ":" << port_ << ", " << ethErrorString();
|
||||
opened_ = connected_;
|
||||
if (connected_) connected();
|
||||
return connected_;*/
|
||||
}
|
||||
|
||||
|
||||
@@ -664,7 +619,7 @@ bool PIEthernet::listen(bool threaded) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//#include <QDebug>
|
||||
|
||||
int PIEthernet::readDevice(void * read_to, int max_size) {
|
||||
//piCout << "read" << sock;
|
||||
if (sock == -1) init();
|
||||
@@ -704,10 +659,8 @@ int PIEthernet::readDevice(void * read_to, int max_size) {
|
||||
PRIVATE->addr_.sin_len = sizeof(PRIVATE->addr_);
|
||||
#endif
|
||||
//piCoutObj << "connect to " << ip_ << ":" << port_ << "...";
|
||||
//qDebug() << "connect to " << ip_.data() << ":" << port_ << sock << PRIVATE->addr_.sin_addr.s_addr << "...";
|
||||
connected_ = (::connect(sock, (sockaddr * )&(PRIVATE->addr_), sizeof(PRIVATE->addr_)) == 0);
|
||||
//piCoutObj << "connect to " << ip_ << ":" << port_ << connected_;
|
||||
//qDebug() << "connect to " << ip_.data() << ":" << port_ << connected_;
|
||||
if (!connected_)
|
||||
piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
|
||||
opened_ = connected_;
|
||||
@@ -717,13 +670,11 @@ int PIEthernet::readDevice(void * read_to, int max_size) {
|
||||
} else
|
||||
piMSleep(10);
|
||||
//piCout << "connected to" << path();
|
||||
//qDebug() << "connected to" << path().data();
|
||||
}
|
||||
if (!connected_) return -1;
|
||||
errorClear();
|
||||
rs = ethRecv(sock, read_to, max_size);
|
||||
//piCoutObj << "readed" << rs;
|
||||
//qDebug() << "readed" << rs;
|
||||
if (rs <= 0) {
|
||||
lerr = ethErrorCore();
|
||||
//piCoutObj << "readed error" << lerr << errorString().data() << parameters()[DisonnectOnTimeout];
|
||||
@@ -735,7 +686,6 @@ int PIEthernet::readDevice(void * read_to, int max_size) {
|
||||
if ((lerr == EWOULDBLOCK || lerr == EAGAIN) && !parameters()[DisonnectOnTimeout]) {
|
||||
#endif
|
||||
//piCoutObj << errorString();
|
||||
//piMSleep(10);
|
||||
return -1;
|
||||
}
|
||||
if (connected_) {
|
||||
@@ -749,7 +699,6 @@ int PIEthernet::readDevice(void * read_to, int max_size) {
|
||||
//piCoutObj << "eth" << ip_ << "disconnected";
|
||||
}
|
||||
if (rs > 0) received(read_to, rs);
|
||||
//qDebug() << "return from read" << rs;
|
||||
return rs;
|
||||
case UDP:
|
||||
memset(&PRIVATE->raddr_, 0, sizeof(PRIVATE->raddr_));
|
||||
@@ -757,14 +706,11 @@ int PIEthernet::readDevice(void * read_to, int max_size) {
|
||||
if (rs > 0) {
|
||||
addr_lr.set(uint(PRIVATE->raddr_.sin_addr.s_addr), ntohs(PRIVATE->raddr_.sin_port));
|
||||
//piCoutObj << "read from" << ip_r << ":" << port_r << rs << "bytes";
|
||||
//piCout << "received from" << lastReadAddress();
|
||||
received(read_to, rs);
|
||||
}
|
||||
//else piCoutObj << "read returt" << rs << ", error" << ethErrorString();
|
||||
return rs;
|
||||
//return ::read(sock, read_to, max_size);
|
||||
default: break;
|
||||
//return ::read(sock, (char * )read_to, max_size);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -801,8 +747,7 @@ int PIEthernet::writeDevice(const void * data, int max_size) {
|
||||
return ret;
|
||||
case UDP:
|
||||
PRIVATE->saddr_.sin_port = htons(addr_s.port());
|
||||
/*if (params[PIEthernet::Broadcast]) PRIVATE->saddr_.sin_addr.s_addr = INADDR_BROADCAST;
|
||||
else*/ PRIVATE->saddr_.sin_addr.s_addr = addr_s.ip();
|
||||
PRIVATE->saddr_.sin_addr.s_addr = addr_s.ip();
|
||||
PRIVATE->saddr_.sin_family = AF_INET;
|
||||
//piCoutObj << "write to" << ip_s << ":" << port_s << "socket" << sock_s << max_size << "bytes ...";
|
||||
return ethSendto(sock_s, data, max_size,
|
||||
@@ -843,7 +788,6 @@ int PIEthernet::writeDevice(const void * data, int max_size) {
|
||||
}
|
||||
return ret;
|
||||
default: break;
|
||||
//return ::read(sock, read_to, max_size);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -907,10 +851,6 @@ void PIEthernet::server_func(void * eth) {
|
||||
ce->clients_mutex.unlock();
|
||||
ce->newConnection(e);
|
||||
//cout << "connected " << ip << endl;
|
||||
//char d[256];
|
||||
//cout << " recv " << recv(s, d, 256, 0) << endl;
|
||||
//cout << recv(ce->clients_.back()->sock, d, 256, 0) << endl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1029,8 +969,6 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
ci.index = pAdapter->Index;
|
||||
ci.mac = macFromBytes(PIByteArray(pAdapter->Address, pAdapter->AddressLength));
|
||||
ci.flags = PIEthernet::ifActive | PIEthernet::ifRunning;
|
||||
//if (ret->ifa_flags & IFF_BROADCAST) ci.flags |= PIEthernet::ifBroadcast;
|
||||
//if (ret->ifa_flags & IFF_MULTICAST) ci.flags |= PIEthernet::ifMulticast;
|
||||
if (pAdapter->Type == MIB_IF_TYPE_PPP) ci.flags |= PIEthernet::ifPTP;
|
||||
if (pAdapter->Type == MIB_IF_TYPE_LOOPBACK) ci.flags |= PIEthernet::ifLoopback;
|
||||
ci.broadcast.clear();
|
||||
@@ -1047,10 +985,6 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
il << ci;
|
||||
as = as->Next;
|
||||
}
|
||||
/*if (ci.flags[PIEthernet::ifBroadcast])
|
||||
ci.broadcast = getSockAddr(ret->ifa_broadaddr);
|
||||
if (ci.flags[PIEthernet::ifPTP])
|
||||
ci.ptp = getSockAddr(ret->ifa_dstaddr);*/
|
||||
pAdapter = pAdapter->Next;
|
||||
}
|
||||
} else
|
||||
@@ -1060,40 +994,6 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
#else
|
||||
#ifdef FREERTOS
|
||||
#else
|
||||
/*# ifdef QNX
|
||||
PIStringList il, sl;
|
||||
PIProcess proc;
|
||||
proc.setGrabOutput(true);
|
||||
proc.exec(ifconfigPath.c_str(), "-l");
|
||||
if (!proc.waitForFinish(1000)) return sl;
|
||||
PIString out(proc.readOutput());
|
||||
il = out.split(" ");
|
||||
il.removeAll("");
|
||||
piForeachC (PIString & i, il) {
|
||||
proc.exec(ifconfigPath.c_str(), i);
|
||||
if (!proc.waitForFinish(1000)) return il;
|
||||
sl << i.trimmed();
|
||||
out = proc.readOutput();
|
||||
int al = out.length();
|
||||
al = (al - out.replaceAll("alias", "").length()) / 5;
|
||||
for (int j = 0; j < al; ++j)
|
||||
sl << i.trimmed() + ":" + PIString::fromNumber(j);
|
||||
}
|
||||
return sl;
|
||||
# else
|
||||
PIStringList sl;
|
||||
PIProcess proc;
|
||||
proc.setGrabOutput(true);
|
||||
proc.exec(ifconfigPath.c_str(), "-s");
|
||||
if (!proc.waitForFinish(1000)) return sl;
|
||||
PIString out(proc.readOutput());
|
||||
out.cutLeft(out.find('\n') + 1);
|
||||
while (!out.isEmpty()) {
|
||||
sl << out.left(out.find(' '));
|
||||
out.cutLeft(out.find('\n') + 1);
|
||||
}
|
||||
return sl;
|
||||
# endif*/
|
||||
# ifdef ANDROID
|
||||
struct ifconf ifc;
|
||||
int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
||||
@@ -1222,38 +1122,6 @@ PIEthernet::Address PIEthernet::interfaceAddress(const PIString & interface_) {
|
||||
|
||||
|
||||
PIVector<PIEthernet::Address> PIEthernet::allAddresses() {
|
||||
/*#ifdef WINDOWS
|
||||
PIStringList al;
|
||||
PIString ca;
|
||||
PIP_ADAPTER_INFO pAdapterInfo, pAdapter = 0;
|
||||
int ret = 0;
|
||||
ulong ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
||||
pAdapterInfo = (IP_ADAPTER_INFO * ) HeapAlloc(GetProcessHeap(), 0, (sizeof (IP_ADAPTER_INFO)));
|
||||
if (pAdapterInfo == 0) {
|
||||
piCout << "[PIEthernet] Error allocating memory needed to call GetAdaptersinfo";
|
||||
return PIStringList();
|
||||
}
|
||||
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
|
||||
HeapFree(GetProcessHeap(), 0, (pAdapterInfo));
|
||||
pAdapterInfo = (IP_ADAPTER_INFO *) HeapAlloc(GetProcessHeap(), 0, (ulOutBufLen));
|
||||
if (pAdapterInfo == 0) {
|
||||
piCout << "[PIEthernet] Error allocating memory needed to call GetAdaptersinfo";
|
||||
return PIStringList();
|
||||
}
|
||||
}
|
||||
if ((ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
|
||||
pAdapter = pAdapterInfo;
|
||||
while (pAdapter) {
|
||||
ca = PIString(pAdapter->IpAddressList.IpAddress.String);
|
||||
if (ca != "0.0.0.0") al << ca;
|
||||
pAdapter = pAdapter->Next;
|
||||
}
|
||||
} else
|
||||
piCout << "[PIEthernet] GetAdaptersInfo failed with error: " << ret;
|
||||
if (pAdapterInfo)
|
||||
HeapFree(GetProcessHeap(), 0, (pAdapterInfo));
|
||||
return al;
|
||||
#else*/
|
||||
PIEthernet::InterfaceList il = interfaces();
|
||||
PIVector<Address> ret;
|
||||
bool has_127 = false;
|
||||
@@ -1266,13 +1134,11 @@ PIVector<PIEthernet::Address> PIEthernet::allAddresses() {
|
||||
// piCout << "[PIEthernet::allAddresses]" << al;
|
||||
if (!has_127) ret << Address("127.0.0.1");
|
||||
return ret;
|
||||
//#endif
|
||||
}
|
||||
|
||||
|
||||
// System wrap
|
||||
|
||||
|
||||
int PIEthernet::ethErrorCore() {
|
||||
#ifdef WINDOWS
|
||||
return WSAGetLastError();
|
||||
|
||||
Reference in New Issue
Block a user