2001-10-06 18:13:28 +04:00
|
|
|
#include "CNetworkAddress.h"
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
//
|
|
|
|
// CNetworkAddress
|
|
|
|
//
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
CNetworkAddress::CNetworkAddress(UInt16 port)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
if (port == 0)
|
|
|
|
throw XSocketAddress(XSocketAddress::kBadPort, CString(), port);
|
|
|
|
|
|
|
|
struct sockaddr_in* inetAddress = reinterpret_cast<struct sockaddr_in*>(&m_address);
|
|
|
|
inetAddress->sin_family = AF_INET;
|
|
|
|
inetAddress->sin_port = htons(port);
|
|
|
|
inetAddress->sin_addr.s_addr = INADDR_ANY;
|
2001-10-26 02:17:17 +04:00
|
|
|
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CNetworkAddress::CNetworkAddress(const CString& hostname, UInt16 port)
|
|
|
|
{
|
|
|
|
if (port == 0)
|
|
|
|
throw XSocketAddress(XSocketAddress::kBadPort, hostname, port);
|
|
|
|
|
|
|
|
struct hostent* hent = gethostbyname(hostname.c_str());
|
|
|
|
if (hent == NULL) {
|
|
|
|
switch (h_errno) {
|
|
|
|
case HOST_NOT_FOUND:
|
|
|
|
throw XSocketAddress(XSocketAddress::kNotFound, hostname, port);
|
|
|
|
|
|
|
|
case NO_DATA:
|
|
|
|
throw XSocketAddress(XSocketAddress::kNoAddress, hostname, port);
|
|
|
|
|
|
|
|
case NO_RECOVERY:
|
|
|
|
case TRY_AGAIN:
|
|
|
|
default:
|
|
|
|
throw XSocketAddress(XSocketAddress::kUnknown, hostname, port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in* inetAddress = reinterpret_cast<struct sockaddr_in*>(&m_address);
|
|
|
|
inetAddress->sin_family = hent->h_addrtype;
|
|
|
|
inetAddress->sin_port = htons(port);
|
2001-10-26 02:17:17 +04:00
|
|
|
memcpy(&inetAddress->sin_addr, hent->h_addr_list[0], hent->h_length);
|
|
|
|
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CNetworkAddress::~CNetworkAddress()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
const struct sockaddr* CNetworkAddress::getAddress() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
return &m_address;
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
int CNetworkAddress::getAddressLength() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
return sizeof(m_address);
|
|
|
|
}
|