barrier/net/CNetworkAddress.cpp
crs 6aba3a6f57 removed exception specifications. thread exceptions weren't
being listed and they'd have to be added to every one.  just
doesn't seem worth the trouble.
2001-10-14 16:58:01 +00:00

65 lines
1.6 KiB
C++

#include "CNetworkAddress.h"
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
//
// CNetworkAddress
//
CNetworkAddress::CNetworkAddress(UInt16 port)
{
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;
::memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
}
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);
::memcpy(&inetAddress->sin_addr, hent->h_addr_list[0], hent->h_length);
::memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
}
CNetworkAddress::~CNetworkAddress()
{
// do nothing
}
const struct sockaddr* CNetworkAddress::getAddress() const
{
return &m_address;
}
int CNetworkAddress::getAddressLength() const
{
return sizeof(m_address);
}