*** empty log message ***

This commit is contained in:
mpc
2004-07-23 03:08:20 +00:00
committed by zzz
parent a857c6a88f
commit 69981e4d78
9 changed files with 227 additions and 38 deletions

View File

@ -27,9 +27,18 @@
* 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.cpp,v 1.7 2004/07/22 03:54:01 mpc Exp $
*/ */
#include "platform.hpp" #include "platform.hpp"
#include "socket_error.hpp"
#include "socket.hpp" #include "socket.hpp"
using namespace Libsockthread; using namespace Libsockthread;
void Socket::setup_socket()
{
if (!addr.is_ready())
throw Socket_error("Socket isn't ready");
sock = socket(addr.get_family(), get_type(), 0);
}

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.hpp,v 1.7 2004/07/22 03:54:01 mpc Exp $
*/ */
#ifndef LIBSOCKTHREAD_SOCKET_HPP #ifndef LIBSOCKTHREAD_SOCKET_HPP
@ -36,19 +36,19 @@
namespace Libsockthread { namespace Libsockthread {
class Socket { class Socket {
public: public:
Socket(Socket_addr& addr) Socket(Socket_addr& addr) // throws Socket_error
: addr(addr); : addr(addr) { setup_socket(); }
void accept();
void close(); void close();
void connect();
void listen();
size_t read(string& buf, size_t max); size_t read(string& buf, size_t max);
void set_addr(Socket_addr& addr) void set_addr(Socket_addr& addr) // throws Socket_error
{ this->addr = addr; } { this->addr = addr; setup_socket(); }
void set_blocking(bool blocking); void set_blocking(bool blocking);
private: private:
void setup_socket(); // throws Socket_error
Socket_addr addr; Socket_addr addr;
int sock;
}; };
} }

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: socket_addr.cpp,v 1.3 2004/07/22 03:54:01 mpc Exp $ * $Id: socket_addr.cpp,v 1.4 2004/07/22 19:10:59 mpc Exp $
*/ */
#include "platform.hpp" #include "platform.hpp"
@ -38,14 +38,17 @@ using namespace Libsockthread;
Socket_addr::Socket_addr(Socket_addr& rhs) Socket_addr::Socket_addr(Socket_addr& rhs)
{ {
delete[] ip; delete[] ip;
if (rhs.domain == AF_INET) { if (rhs.resolved) {
ip = new char[INET_ADDRSTRLEN]; if (rhs.family == AF_INET) {
else ip = new char[INET_ADDRSTRLEN];
ip = new char[INET6_ADDRSTRLEN]; else
domain = rhs.domain; ip = new char[INET6_ADDRSTRLEN];
strcpy(ip, rhs.ip);
}
family = rhs.family;
host = rhs.host; host = rhs.host;
strcpy(ip, rhs.ip);
port = rhs.port; port = rhs.port;
resolved = rhs.resolved;
type = rhs.type; type = rhs.type;
} }
@ -55,13 +58,15 @@ Socket_addr& Socket_addr::operator=(const Socket_addr& rhs)
return *this; return *this;
delete[] ip; delete[] ip;
if (rhs.domain == AF_INET) if (rhs.resolved) {
ip = new char[INET_ADDRSTRLEN]; if (rhs.family == AF_INET)
else ip = new char[INET_ADDRSTRLEN];
ip = new char[INET6_ADDRSTRLEN]; else
domain = rhs.domain; ip = new char[INET6_ADDRSTRLEN];
strcpy(ip, rhs.ip);
}
family = rhs.family;
host = rhs.host; host = rhs.host;
strcpy(ip, rhs.ip);
port = rhs.port; port = rhs.port;
type = rhs.type; type = rhs.type;
@ -73,25 +78,29 @@ Socket_addr& Socket_addr::operator=(const Socket_addr& rhs)
*/ */
void Socket_addr::resolve() void Socket_addr::resolve()
{ {
resolved = false; // in case they already had a host name but just set a
// new one with set_host()
hostent* hent = gethostbyname(host.c_str()); hostent* hent = gethostbyname(host.c_str());
if (hent == NULL) if (hent == NULL)
throw Socket_error(hstrerror(h_errno)); throw Dns_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; family = hent->h_addrtype;
delete[] ip; delete[] ip;
if (domain == AF_INET) { if (family == 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]);
resolved = true;
} }
bool Socket_addr::operator==(const Socket_addr& rhs) bool Socket_addr::operator==(const Socket_addr& rhs)
{ {
if (rhs.domain == domain if (rhs.family == family
&& rhs.host == host && rhs.host == host
&& strcmp(rhs.ip, ip) == 0 && strcmp(rhs.ip, ip) == 0
&& rhs.port == port && rhs.port == port
&& rhs.resolved == resolved
&& rhs.type == type) && rhs.type == type)
return true; return true;
else else

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: socket_addr.hpp,v 1.3 2004/07/22 03:54:01 mpc Exp $ * $Id: socket_addr.hpp,v 1.4 2004/07/22 19:10:59 mpc Exp $
*/ */
#ifndef LIBSOCKTHREAD_SOCKET_ADDR_HPP #ifndef LIBSOCKTHREAD_SOCKET_ADDR_HPP
@ -36,39 +36,42 @@
namespace Libsockthread { namespace Libsockthread {
class Socket_addr { class Socket_addr {
public: public:
Socket_addr()
: family(AF_INET), resolved(false) { }
Socket_addr(Socket_addr& rhs); Socket_addr(Socket_addr& rhs);
Socket_addr(int domain, int type, string& host, uint16_t port) Socket_addr(int type, string& host, uint16_t port)
: domain(domain), host(host), type(type), port(port) : family(AF_INET), host(host), type(type), port(port)
{ resolve(); } // throws Socket_error { resolve(); } // throws Dns_error
~Socket_addr() ~Socket_addr()
{ delete[] ip; } { delete[] ip; }
int get_domain() const // Has nothing to do with DNS domain - int get_family() const
{ return domain; } // returns either AF_INET or AF_INET6 { return family; }
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;
bool is_ready() const
{ return resolved; }
Socket_addr& operator=(const Socket_addr& rhs); Socket_addr& operator=(const Socket_addr& rhs);
bool operator==(const Socket_addr& rhs); bool operator==(const Socket_addr& rhs);
void set_domain(int domain) void set_host(string& host) // throws Dns_error
{ this->domain = domain; }
void set_host(string& host) // throws Socket_error
{ this->host = host; resolve(); } { this->host = host; resolve(); }
void set_port(uint16_t port) void set_port(uint16_t port)
{ this->port = port; } { this->port = port; }
void set_type(int type) void set_type(int type)
{ this->type = type; } { this->type = type; }
private: private:
void resolve(); // throws Socket_error void resolve(); // throws Dns_error
int domain; int family; // AF_INET or AF_INET6
string host; string host;
char* ip; char* ip;
uint16_t port; uint16_t port;
int type; bool resolved;
int type; // SOCK_STREAM or SOCK_DGRAM
}; };
} }

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2004, Matthew P. Cashdollar <mpc@innographx.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the author nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
#include "platform.hpp"
#include "socket_connector.hpp"
using namespace Libsockthread;

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2004, Matthew P. Cashdollar <mpc@innographx.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the author nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
#ifndef LIBSOCKTHREAD_SOCKET_CONNECTOR_HPP
#define LIBSOCKTHREAD_SOCKET_CONNECTOR_HPP
namespace Libsockthread {
class Socket_connector : public Socket {
public:
Socket_connector(Socket_addr& addr)
: Socket(addr);
void connect();
};
}
#endif // LIBSOCKTHREAD_SOCKET_CONNECTOR_HPP

View File

@ -37,6 +37,11 @@ namespace Libsockthread {
Socket_error(const string& s) Socket_error(const string& s)
: runtime_error(s) { } : runtime_error(s) { }
}; };
class Dns_error : public Socket_error {
public:
Dns_error(const string& s)
: Socket_error(s) { }
};
} }
#endif // LIBSOCKTHREAD_SOCKET_ERROR_HPP #endif // LIBSOCKTHREAD_SOCKET_ERROR_HPP

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2004, Matthew P. Cashdollar <mpc@innographx.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the author nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
#include "platform.hpp"
#include "socket_listener.hpp"
using namespace Libsockthread;

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2004, Matthew P. Cashdollar <mpc@innographx.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the author nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
#ifndef LIBSOCKTHREAD_SOCKET_LISTENER_HPP
#define LIBSOCKTHREAD_SOCKET_LISTENER_HPP
namespace Libsockthread {
class Socket_listener {
public:
Socket_listener(Socket_addr& addr)
: Socket(addr);
void accept();
void listen();
};
}
#endif // LIBSOCKTHREAD_SOCKET_LISTENER_HPP