00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "CNetworkAddress.h"
00020 #include "XSocket.h"
00021 #include "CArch.h"
00022 #include "XArch.h"
00023 #include <cstdlib>
00024
00025
00026
00027
00028
00029
00030
00031 CNetworkAddress::CNetworkAddress() :
00032 m_address(NULL),
00033 m_hostname(),
00034 m_port(0)
00035 {
00036
00037
00038 }
00039
00040 CNetworkAddress::CNetworkAddress(int port) :
00041 m_address(NULL),
00042 m_hostname(),
00043 m_port(port)
00044 {
00045 checkPort();
00046 m_address = ARCH->newAnyAddr(IArchNetwork::kINET);
00047 ARCH->setAddrPort(m_address, m_port);
00048 }
00049
00050 CNetworkAddress::CNetworkAddress(const CNetworkAddress& addr) :
00051 m_address(addr.m_address != NULL ? ARCH->copyAddr(addr.m_address) : NULL),
00052 m_hostname(addr.m_hostname),
00053 m_port(addr.m_port)
00054 {
00055
00056 }
00057
00058 CNetworkAddress::CNetworkAddress(const CString& hostname, int port) :
00059 m_address(NULL),
00060 m_hostname(hostname),
00061 m_port(port)
00062 {
00063
00064 CString::size_type i = m_hostname.rfind(':');
00065 if (i != CString::npos && i + 1 < m_hostname.size()) {
00066
00067 bool colonNotation = false;
00068 bool dotNotation = false;
00069 bool doubleColon = false;
00070 for (CString::size_type j = 0; j < i; ++j) {
00071 if (m_hostname[j] == ':') {
00072 colonNotation = true;
00073 dotNotation = false;
00074 if (m_hostname[j + 1] == ':') {
00075 doubleColon = true;
00076 }
00077 }
00078 else if (m_hostname[j] == '.' && colonNotation) {
00079 dotNotation = true;
00080 }
00081 }
00082
00083
00084
00085
00086
00087
00088 if ((!doubleColon || dotNotation) || !colonNotation) {
00089
00090 char* end;
00091 const char* chostname = m_hostname.c_str();
00092 long suffixPort = strtol(chostname + i + 1, &end, 10);
00093 if (end == chostname + i + 1 || *end != '\0') {
00094 throw XSocketAddress(XSocketAddress::kBadPort,
00095 m_hostname, m_port);
00096 }
00097
00098
00099 m_hostname.erase(i);
00100
00101
00102 m_port = static_cast<int>(suffixPort);
00103 }
00104 }
00105
00106
00107 checkPort();
00108 }
00109
00110 CNetworkAddress::~CNetworkAddress()
00111 {
00112 if (m_address != NULL) {
00113 ARCH->closeAddr(m_address);
00114 }
00115 }
00116
00117 CNetworkAddress&
00118 CNetworkAddress::operator=(const CNetworkAddress& addr)
00119 {
00120 CArchNetAddress newAddr = NULL;
00121 if (addr.m_address != NULL) {
00122 newAddr = ARCH->copyAddr(addr.m_address);
00123 }
00124 if (m_address != NULL) {
00125 ARCH->closeAddr(m_address);
00126 }
00127 m_address = newAddr;
00128 m_hostname = addr.m_hostname;
00129 m_port = addr.m_port;
00130 return *this;
00131 }
00132
00133 void
00134 CNetworkAddress::resolve()
00135 {
00136
00137 if (m_address != NULL) {
00138 ARCH->closeAddr(m_address);
00139 m_address = NULL;
00140 }
00141
00142 try {
00143
00144
00145 if (m_hostname.empty()) {
00146 m_address = ARCH->newAnyAddr(IArchNetwork::kINET);
00147 }
00148 else {
00149 m_address = ARCH->nameToAddr(m_hostname);
00150 }
00151 }
00152 catch (XArchNetworkNameUnknown&) {
00153 throw XSocketAddress(XSocketAddress::kNotFound, m_hostname, m_port);
00154 }
00155 catch (XArchNetworkNameNoAddress&) {
00156 throw XSocketAddress(XSocketAddress::kNoAddress, m_hostname, m_port);
00157 }
00158 catch (XArchNetworkNameUnsupported&) {
00159 throw XSocketAddress(XSocketAddress::kUnsupported, m_hostname, m_port);
00160 }
00161 catch (XArchNetworkName&) {
00162 throw XSocketAddress(XSocketAddress::kUnknown, m_hostname, m_port);
00163 }
00164
00165
00166 ARCH->setAddrPort(m_address, m_port);
00167 }
00168
00169 bool
00170 CNetworkAddress::operator==(const CNetworkAddress& addr) const
00171 {
00172 return ARCH->isEqualAddr(m_address, addr.m_address);
00173 }
00174
00175 bool
00176 CNetworkAddress::operator!=(const CNetworkAddress& addr) const
00177 {
00178 return !operator==(addr);
00179 }
00180
00181 bool
00182 CNetworkAddress::isValid() const
00183 {
00184 return (m_address != NULL);
00185 }
00186
00187 const CArchNetAddress&
00188 CNetworkAddress::getAddress() const
00189 {
00190 return m_address;
00191 }
00192
00193 int
00194 CNetworkAddress::getPort() const
00195 {
00196 return m_port;
00197 }
00198
00199 CString
00200 CNetworkAddress::getHostname() const
00201 {
00202 return m_hostname;
00203 }
00204
00205 void
00206 CNetworkAddress::checkPort()
00207 {
00208
00209 if (m_port <= 0 || m_port > 65535) {
00210 throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port);
00211 }
00212 }