PIEthernet:resolve()
git-svn-id: svn://db.shs.com.ru/pip@171 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -30,24 +30,26 @@
|
||||
# endif
|
||||
# define ip_mreqn ip_mreq
|
||||
# define imr_address imr_interface
|
||||
#endif
|
||||
#ifdef WINDOWS
|
||||
# include <io.h>
|
||||
# include <winsock2.h>
|
||||
# include <iphlpapi.h>
|
||||
# include <psapi.h>
|
||||
# include <ws2tcpip.h>
|
||||
# define ip_mreqn ip_mreq
|
||||
# define imr_address imr_interface
|
||||
#else
|
||||
# include <fcntl.h>
|
||||
# include <sys/ioctl.h>
|
||||
# include <netinet/in.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <sys/socket.h>
|
||||
# include <net/if.h>
|
||||
# ifndef ANDROID
|
||||
# include <ifaddrs.h>
|
||||
# ifdef WINDOWS
|
||||
# include <io.h>
|
||||
# include <winsock2.h>
|
||||
# include <iphlpapi.h>
|
||||
# include <psapi.h>
|
||||
# include <ws2tcpip.h>
|
||||
# define ip_mreqn ip_mreq
|
||||
# define imr_address imr_interface
|
||||
# else
|
||||
# include <fcntl.h>
|
||||
# include <sys/ioctl.h>
|
||||
# include <netinet/in.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <sys/socket.h>
|
||||
# include <net/if.h>
|
||||
# include <netdb.h>
|
||||
# ifndef ANDROID
|
||||
# include <ifaddrs.h>
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
#include <errno.h>
|
||||
@@ -1008,7 +1010,32 @@ PIStringList PIEthernet::allAddresses() {
|
||||
piForeachC (PIEthernet::Interface & i, il)
|
||||
al << i.address;
|
||||
return al.removeStrings("0.0.0.0");
|
||||
//#endif
|
||||
}
|
||||
|
||||
|
||||
PIString PIEthernet::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());
|
||||
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
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -329,6 +329,9 @@ public:
|
||||
//! Returns all system network IP addresses
|
||||
static PIStringList allAddresses();
|
||||
|
||||
//! Resolve hostname "host" and return it IPv4 address or "0.0.0.0" on error
|
||||
static PIString resolve(const PIString & host);
|
||||
|
||||
static void parseAddress(const PIString & ipp, PIString * ip, int * port);
|
||||
static PIString macFromBytes(const PIByteArray & mac);
|
||||
static PIByteArray macToBytes(const PIString & mac);
|
||||
|
||||
Reference in New Issue
Block a user