*** empty log message ***

This commit is contained in:
mpc
2004-07-23 00:10:59 +00:00
committed by zzz
parent e8d19439f8
commit a857c6a88f
2 changed files with 41 additions and 8 deletions

View File

@ -27,7 +27,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id$ * $Id: socket_addr.cpp,v 1.3 2004/07/22 03:54:01 mpc Exp $
*/ */
#include "platform.hpp" #include "platform.hpp"
@ -35,12 +35,27 @@
#include "socket_addr.hpp" #include "socket_addr.hpp"
using namespace Libsockthread; using namespace Libsockthread;
Socket_addr::Socket_addr(Socket_addr& rhs)
{
delete[] ip;
if (rhs.domain == AF_INET) {
ip = new char[INET_ADDRSTRLEN];
else
ip = new char[INET6_ADDRSTRLEN];
domain = rhs.domain;
host = rhs.host;
strcpy(ip, rhs.ip);
port = rhs.port;
type = rhs.type;
}
Socket_addr& Socket_addr::operator=(const Socket_addr& rhs) Socket_addr& Socket_addr::operator=(const Socket_addr& rhs)
{ {
if (this == &rhs) // check for self-assignment: a = a if (this == &rhs) // check for self-assignment: a = a
return *this; return *this;
if (rhs.domain == AF_INET) { delete[] ip;
if (rhs.domain == AF_INET)
ip = new char[INET_ADDRSTRLEN]; ip = new char[INET_ADDRSTRLEN];
else else
ip = new char[INET6_ADDRSTRLEN]; ip = new char[INET6_ADDRSTRLEN];
@ -53,6 +68,9 @@ Socket_addr& Socket_addr::operator=(const Socket_addr& rhs)
return *this; return *this;
} }
/*
* Performs a DNS lookup
*/
void Socket_addr::resolve() void Socket_addr::resolve()
{ {
hostent* hent = gethostbyname(host.c_str()); hostent* hent = gethostbyname(host.c_str());
@ -60,9 +78,22 @@ void Socket_addr::resolve()
throw Socket_error(hstrerror(h_errno)); throw Socket_error(hstrerror(h_errno));
assert(hent->h_addrtype == AF_INET || hent->h_addrtype == AF_INET6); assert(hent->h_addrtype == AF_INET || hent->h_addrtype == AF_INET6);
domain = hent->h_addrtype; domain = hent->h_addrtype;
delete[] ip;
if (domain == AF_INET) { if (domain == AF_INET) {
ip = new char[INET_ADDRSTRLEN]; ip = new char[INET_ADDRSTRLEN];
else else
ip = new char[INET6_ADDRSTRLEN]; ip = new char[INET6_ADDRSTRLEN];
strcpy(ip, hent->h_addr_list[0]); strcpy(ip, hent->h_addr_list[0]);
} }
bool Socket_addr::operator==(const Socket_addr& rhs)
{
if (rhs.domain == domain
&& rhs.host == host
&& strcmp(rhs.ip, ip) == 0
&& rhs.port == port
&& rhs.type == type)
return true;
else
return false;
}

View File

@ -27,7 +27,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id$ * $Id: socket_addr.hpp,v 1.3 2004/07/22 03:54:01 mpc Exp $
*/ */
#ifndef LIBSOCKTHREAD_SOCKET_ADDR_HPP #ifndef LIBSOCKTHREAD_SOCKET_ADDR_HPP
@ -36,21 +36,23 @@
namespace Libsockthread { namespace Libsockthread {
class Socket_addr { class Socket_addr {
public: public:
Socket_addr(Socket_addr& rhs);
Socket_addr(int domain, int type, string& host, uint16_t port) Socket_addr(int domain, int type, string& host, uint16_t port)
: domain(domain), host(host), type(type), port(port) : domain(domain), host(host), type(type), port(port)
{ resolve(); } // throws Socket_error { resolve(); } // throws Socket_error
~Socket_addr() ~Socket_addr()
{ delete[] ip; } { delete[] ip; }
int get_domain() const // Has nothing to do with DNS domain int get_domain() const // Has nothing to do with DNS domain -
{ return domain; } { return domain; } // returns either AF_INET or AF_INET6
const char* get_ip() const // Warning! This can be NULL! const char* get_ip() const // Warning! This can be NULL!
{ return ip; } { return ip; }
uint16_t get_port() const uint16_t get_port() const
{ return port; } { return port; }
int get_type() const int get_type() const
{ return type; { return type;
Socket_addr& Socket_addr::operator=(const Socket_addr& rhs); Socket_addr& operator=(const Socket_addr& rhs);
bool operator==(const Socket_addr& rhs);
void set_domain(int domain) void set_domain(int domain)
{ this->domain = domain; } { this->domain = domain; }
void set_host(string& host) // throws Socket_error void set_host(string& host) // throws Socket_error
@ -60,7 +62,7 @@ namespace Libsockthread {
void set_type(int type) void set_type(int type)
{ this->type = type; } { this->type = type; }
private: private:
void resolve(); void resolve(); // throws Socket_error
int domain; int domain;
string host; string host;