2001-10-06 14:13:28 +00:00
|
|
|
#include "CNetworkAddress.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// CNetworkAddress
|
|
|
|
//
|
|
|
|
|
2001-10-14 16:58:01 +00:00
|
|
|
CNetworkAddress::CNetworkAddress(UInt16 port)
|
2001-10-06 14:13:28 +00: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;
|
2001-11-19 00:33:36 +00:00
|
|
|
inetAddress->sin_port = CNetwork::swaphtons(port);
|
2001-10-06 14:13:28 +00:00
|
|
|
inetAddress->sin_addr.s_addr = INADDR_ANY;
|
2001-10-25 22:17:17 +00:00
|
|
|
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CNetworkAddress::CNetworkAddress(const CString& hostname, UInt16 port)
|
|
|
|
{
|
|
|
|
if (port == 0)
|
|
|
|
throw XSocketAddress(XSocketAddress::kBadPort, hostname, port);
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
struct hostent* hent = CNetwork::gethostbyname(hostname.c_str());
|
2001-10-06 14:13:28 +00:00
|
|
|
if (hent == NULL) {
|
2001-11-19 00:33:36 +00:00
|
|
|
switch (CNetwork::gethosterror()) {
|
2002-04-29 14:40:01 +00:00
|
|
|
case CNetwork::kHOST_NOT_FOUND:
|
2001-10-06 14:13:28 +00:00
|
|
|
throw XSocketAddress(XSocketAddress::kNotFound, hostname, port);
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
case CNetwork::kNO_DATA:
|
2001-10-06 14:13:28 +00:00
|
|
|
throw XSocketAddress(XSocketAddress::kNoAddress, hostname, port);
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
case CNetwork::kNO_RECOVERY:
|
|
|
|
case CNetwork::kTRY_AGAIN:
|
|
|
|
default:
|
2001-10-06 14:13:28 +00:00
|
|
|
throw XSocketAddress(XSocketAddress::kUnknown, hostname, port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in* inetAddress = reinterpret_cast<struct sockaddr_in*>(&m_address);
|
|
|
|
inetAddress->sin_family = hent->h_addrtype;
|
2001-11-19 00:33:36 +00:00
|
|
|
inetAddress->sin_port = CNetwork::swaphtons(port);
|
2001-10-25 22:17:17 +00: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 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CNetworkAddress::~CNetworkAddress()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
const CNetwork::Address* CNetworkAddress::getAddress() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return &m_address;
|
|
|
|
}
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
CNetwork::AddressLength CNetworkAddress::getAddressLength() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return sizeof(m_address);
|
|
|
|
}
|