barrier/net/CNetworkAddress.cpp

61 lines
1.6 KiB
C++
Raw Normal View History

2001-10-06 14:13:28 +00:00
#include "CNetworkAddress.h"
//
// CNetworkAddress
//
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;
inetAddress->sin_port = CNetwork::swaphtons(port);
2001-10-06 14:13:28 +00:00
inetAddress->sin_addr.s_addr = INADDR_ANY;
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);
struct hostent* hent = CNetwork::gethostbyname(hostname.c_str());
2001-10-06 14:13:28 +00:00
if (hent == NULL) {
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;
inetAddress->sin_port = CNetwork::swaphtons(port);
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
}
const CNetwork::Address* CNetworkAddress::getAddress() const
2001-10-06 14:13:28 +00:00
{
return &m_address;
}
CNetwork::AddressLength CNetworkAddress::getAddressLength() const
2001-10-06 14:13:28 +00:00
{
return sizeof(m_address);
}