diff --git a/main.cpp b/main.cpp index ecc03777..1ec87b57 100644 --- a/main.cpp +++ b/main.cpp @@ -54,8 +54,8 @@ int main(int argc, char *argv[]) { PIString s2; s2 = PIString::fromUTF8(ba); piCout << s2;*/ - PIEthernet::Address a("127.1", -1); - piCout << a << PICoutManipulators::Hex << a.ip(); + piCout << PIEthernet::Address::resolve("www.ya.ru:22"); + piCout << PIEthernet::Address::resolve("www.ya.ru", 22); return 0; } diff --git a/src_main/io/piethernet.cpp b/src_main/io/piethernet.cpp index 8686c5b5..16317bcc 100755 --- a/src_main/io/piethernet.cpp +++ b/src_main/io/piethernet.cpp @@ -162,34 +162,32 @@ void PIEthernet::Address::clear() { } -PIString PIEthernet::Address::resolve(const PIString & host) { - PIString ip(host), port; - int i = host.find(':'); - if (i >= 0) { - ip = host.left(i); - port = host.right(host.length() - i - 1); - } - PIString ret = PIStringAscii("0.0.0.0"); - if (!port.isEmpty()) ret += PIStringAscii(":") + port; -//#ifdef WINDOWS - hostent * he = gethostbyname(ip.dataAscii()); +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; - struct in_addr addr; - if (he->h_addr_list[0]) { - addr.s_addr = *((uint*)(he->h_addr_list[0])); - ret = inet_ntoa(addr); - if (!port.isEmpty()) ret += PIStringAscii(":") + port; - } -//#else -//#endif + 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.find(":"); + 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(); } @@ -288,7 +286,7 @@ bool PIEthernet::init() { else sock_s = sock; if (sock == -1 || sock_s == -1) { - piCoutObj << "Can`t create socket, " << ethErrorString(); + piCoutObj << "Can`t create socket," << ethErrorString(); return false; } if (params[PIEthernet::ReuseAddress]) ethSetsockoptBool(sock, SOL_SOCKET, SO_REUSEADDR); @@ -377,7 +375,7 @@ bool PIEthernet::openDevice() { tries++; } if (tries == 2) { - piCoutObj << "Can`t bind to " << addr_r << ", " << ethErrorString(); + piCoutObj << "Can`t bind to" << addr_r << "," << ethErrorString(); return false; } opened_ = true; @@ -496,7 +494,7 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) { //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) { - piCoutObj << "Can`t join multicast group " << group << ", " << ethErrorString(); + piCoutObj << "Can`t join multicast group" << group << "," << ethErrorString(); return false; } if (params[PIEthernet::MulticastLoop]) ethSetsockoptInt(sock, IPPROTO_IP, IP_MULTICAST_LOOP); @@ -536,7 +534,7 @@ bool PIEthernet::leaveMulticastGroup(const PIString & group) { else mreq.imr_address.s_addr = addr_r.ip(); mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii()); if (ethSetsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { - piCoutObj << "Can`t leave multicast group " << group << ", " << ethErrorString(); + piCoutObj << "Can`t leave multicast group" << group << "," << ethErrorString(); return false; } mcast_groups.removeAll(group); @@ -597,11 +595,11 @@ bool PIEthernet::listen(bool threaded) { tries++; } if (tries == 2) { - piCoutObj << "Can`t bind to " << addr_r << ", " << ethErrorString(); + piCoutObj << "Can`t bind to" << addr_r << "," << ethErrorString(); return false; } if (::listen(sock, 64) == -1) { - piCoutObj << "Can`t listen on "<< addr_r << ", " << ethErrorString(); + piCoutObj << "Can`t listen on"<< addr_r << "," << ethErrorString(); return false; } opened_ = server_bounded = true; @@ -641,8 +639,8 @@ int PIEthernet::readDevice(void * read_to, int max_size) { init(); qDebug() << "init() in read thread";*/ #endif - memset(&PRIVATE->addr_, 0, sizeof(PRIVATE->addr_)); addr_r.set(path()); + memset(&PRIVATE->addr_, 0, sizeof(PRIVATE->addr_)); PRIVATE->addr_.sin_port = htons(addr_r.port()); PRIVATE->addr_.sin_addr.s_addr = addr_r.ip(); PRIVATE->addr_.sin_family = AF_INET; @@ -655,7 +653,7 @@ int PIEthernet::readDevice(void * read_to, int max_size) { //piCoutObj << "connect to " << ip_ << ":" << port_ << connected_; //qDebug() << "connect to " << ip_.data() << ":" << port_ << connected_; if (!connected_) - piCoutObj << "Can`t connect to " << addr_r << ", " << ethErrorString(); + piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString(); opened_ = connected_; if (connected_) { connecting_ = false; @@ -769,7 +767,7 @@ int PIEthernet::writeDevice(const void * data, int max_size) { //piCoutObj << "connect to " << ip << ":" << port_; connected_ = (::connect(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == 0); if (!connected_) - piCoutObj << "Can`t connect to " << addr_r << ", " << ethErrorString(); + piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString(); opened_ = connected_; if (connected_) { connecting_ = false; @@ -823,7 +821,7 @@ void PIEthernet::server_func(void * eth) { piMSleep(10); return; } - if (ce->debug()) piCout << "[PIEthernet] Can`t accept new connection, " << ethErrorString(); + if (ce->debug()) piCout << "[PIEthernet] Can`t accept new connection," << ethErrorString(); piMSleep(10); return; } @@ -950,7 +948,7 @@ PIEthernet::InterfaceList PIEthernet::interfaces() { pAdapter = pAdapter->Next; } } else - piCout << "[PIEthernet] GetAdaptersInfo failed with error: " << ret; + piCout << "[PIEthernet] GetAdaptersInfo failed with error:" << ret; if (pAdapterInfo) HeapFree(GetProcessHeap(), 0, (pAdapterInfo)); #else diff --git a/src_main/io/piethernet.h b/src_main/io/piethernet.h index 99b4885f..31c1e34e 100755 --- a/src_main/io/piethernet.h +++ b/src_main/io/piethernet.h @@ -62,30 +62,63 @@ public: }; - //! \brief IPv4 network address, ip and port + //! \brief IPv4 network address, IP and port class Address { friend class PIEthernet; friend inline PIByteArray & operator <<(PIByteArray & s, const PIEthernet::Address & v); friend inline PIByteArray & operator >>(PIByteArray & s, PIEthernet::Address & v); public: - //! Contructs %Address with binary representation of ip and port + + //! 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(); - //! Resolve hostname "host" and return it IPv4 address or "0.0.0.0" on error - static PIString resolve(const PIString & host); + //! 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: @@ -227,6 +260,9 @@ public: //! Connect to TCP server with address "ip_port". Use only for TCP_Client bool connect(const PIString & ip_port) {setPath(ip_port); return connect();} + + //! Connect to TCP server with address "addr". Use only for TCP_Client + bool connect(const Address & addr) {setPath(addr.toString()); return connect();} //! Returns if %PIEthernet connected to TCP server. Use only for TCP_Client bool isConnected() const {return connected_;} @@ -244,6 +280,9 @@ public: //! Start listen for incoming TCP connections on address "ip_port". Use only for TCP_Server bool listen(const PIString & ip_port, bool threaded = false) {setReadAddress(ip_port); return listen(threaded);} + //! Start listen for incoming TCP connections on address "addr". Use only for TCP_Server + bool listen(const Address & addr, bool threaded = false) {setReadAddress(addr); return listen(threaded);} + PIEthernet * client(int index) {return clients_[index];} int clientsCount() const {return clients_.size_s();} PIVector clients() const {return clients_;}