From 69981e4d78ed6b8b039e802b04dec46d7ac2664a Mon Sep 17 00:00:00 2001 From: mpc <mpc> Date: Fri, 23 Jul 2004 03:08:20 +0000 Subject: [PATCH] *** empty log message *** --- apps/enclave/libsockthread/src/socket.cpp | 11 ++++- apps/enclave/libsockthread/src/socket.hpp | 16 +++---- .../enclave/libsockthread/src/socket_addr.cpp | 43 ++++++++++------- .../enclave/libsockthread/src/socket_addr.hpp | 27 ++++++----- .../libsockthread/src/socket_connector.cpp | 35 ++++++++++++++ .../libsockthread/src/socket_connector.hpp | 46 ++++++++++++++++++ .../libsockthread/src/socket_error.hpp | 5 ++ .../libsockthread/src/socket_listener.cpp | 35 ++++++++++++++ .../libsockthread/src/socket_listener.hpp | 47 +++++++++++++++++++ 9 files changed, 227 insertions(+), 38 deletions(-) create mode 100644 apps/enclave/libsockthread/src/socket_connector.cpp create mode 100644 apps/enclave/libsockthread/src/socket_connector.hpp create mode 100644 apps/enclave/libsockthread/src/socket_listener.cpp create mode 100644 apps/enclave/libsockthread/src/socket_listener.hpp diff --git a/apps/enclave/libsockthread/src/socket.cpp b/apps/enclave/libsockthread/src/socket.cpp index 08247c2505..5eb552defa 100644 --- a/apps/enclave/libsockthread/src/socket.cpp +++ b/apps/enclave/libsockthread/src/socket.cpp @@ -27,9 +27,18 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * 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 "socket_error.hpp" #include "socket.hpp" 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); +} diff --git a/apps/enclave/libsockthread/src/socket.hpp b/apps/enclave/libsockthread/src/socket.hpp index 7d8ec2623f..d226d6c298 100644 --- a/apps/enclave/libsockthread/src/socket.hpp +++ b/apps/enclave/libsockthread/src/socket.hpp @@ -27,7 +27,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * 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 @@ -36,19 +36,19 @@ namespace Libsockthread { class Socket { public: - Socket(Socket_addr& addr) - : addr(addr); + Socket(Socket_addr& addr) // throws Socket_error + : addr(addr) { setup_socket(); } - void accept(); void close(); - void connect(); - void listen(); size_t read(string& buf, size_t max); - void set_addr(Socket_addr& addr) - { this->addr = addr; } + void set_addr(Socket_addr& addr) // throws Socket_error + { this->addr = addr; setup_socket(); } void set_blocking(bool blocking); private: + void setup_socket(); // throws Socket_error + Socket_addr addr; + int sock; }; } diff --git a/apps/enclave/libsockthread/src/socket_addr.cpp b/apps/enclave/libsockthread/src/socket_addr.cpp index 53f5167d70..7f80a37298 100644 --- a/apps/enclave/libsockthread/src/socket_addr.cpp +++ b/apps/enclave/libsockthread/src/socket_addr.cpp @@ -27,7 +27,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * 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" @@ -38,14 +38,17 @@ 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; + if (rhs.resolved) { + if (rhs.family == AF_INET) { + ip = new char[INET_ADDRSTRLEN]; + else + ip = new char[INET6_ADDRSTRLEN]; + strcpy(ip, rhs.ip); + } + family = rhs.family; host = rhs.host; - strcpy(ip, rhs.ip); port = rhs.port; + resolved = rhs.resolved; type = rhs.type; } @@ -55,13 +58,15 @@ Socket_addr& Socket_addr::operator=(const Socket_addr& rhs) return *this; delete[] ip; - if (rhs.domain == AF_INET) - ip = new char[INET_ADDRSTRLEN]; - else - ip = new char[INET6_ADDRSTRLEN]; - domain = rhs.domain; + if (rhs.resolved) { + if (rhs.family == AF_INET) + ip = new char[INET_ADDRSTRLEN]; + else + ip = new char[INET6_ADDRSTRLEN]; + strcpy(ip, rhs.ip); + } + family = rhs.family; host = rhs.host; - strcpy(ip, rhs.ip); port = rhs.port; type = rhs.type; @@ -73,25 +78,29 @@ Socket_addr& Socket_addr::operator=(const Socket_addr& rhs) */ 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()); 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); - domain = hent->h_addrtype; + family = hent->h_addrtype; delete[] ip; - if (domain == AF_INET) { + if (family == AF_INET) { ip = new char[INET_ADDRSTRLEN]; else ip = new char[INET6_ADDRSTRLEN]; strcpy(ip, hent->h_addr_list[0]); + resolved = true; } bool Socket_addr::operator==(const Socket_addr& rhs) { - if (rhs.domain == domain + if (rhs.family == family && rhs.host == host && strcmp(rhs.ip, ip) == 0 && rhs.port == port + && rhs.resolved == resolved && rhs.type == type) return true; else diff --git a/apps/enclave/libsockthread/src/socket_addr.hpp b/apps/enclave/libsockthread/src/socket_addr.hpp index 0fa769a75a..12115fc603 100644 --- a/apps/enclave/libsockthread/src/socket_addr.hpp +++ b/apps/enclave/libsockthread/src/socket_addr.hpp @@ -27,7 +27,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * 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 @@ -36,39 +36,42 @@ namespace Libsockthread { class Socket_addr { public: + Socket_addr() + : family(AF_INET), resolved(false) { } Socket_addr(Socket_addr& rhs); - Socket_addr(int domain, int type, string& host, uint16_t port) - : domain(domain), host(host), type(type), port(port) - { resolve(); } // throws Socket_error + Socket_addr(int type, string& host, uint16_t port) + : family(AF_INET), host(host), type(type), port(port) + { resolve(); } // throws Dns_error ~Socket_addr() { delete[] ip; } - int get_domain() const // Has nothing to do with DNS domain - - { return domain; } // returns either AF_INET or AF_INET6 + int get_family() const + { return family; } const char* get_ip() const // Warning! This can be NULL! { return ip; } uint16_t get_port() const { return port; } int get_type() const { return type; + bool is_ready() const + { return resolved; } Socket_addr& operator=(const Socket_addr& rhs); bool operator==(const Socket_addr& rhs); - void set_domain(int domain) - { this->domain = domain; } - void set_host(string& host) // throws Socket_error + void set_host(string& host) // throws Dns_error { this->host = host; resolve(); } void set_port(uint16_t port) { this->port = port; } void set_type(int type) { this->type = type; } private: - void resolve(); // throws Socket_error + void resolve(); // throws Dns_error - int domain; + int family; // AF_INET or AF_INET6 string host; char* ip; uint16_t port; - int type; + bool resolved; + int type; // SOCK_STREAM or SOCK_DGRAM }; } diff --git a/apps/enclave/libsockthread/src/socket_connector.cpp b/apps/enclave/libsockthread/src/socket_connector.cpp new file mode 100644 index 0000000000..5039f0d26b --- /dev/null +++ b/apps/enclave/libsockthread/src/socket_connector.cpp @@ -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; diff --git a/apps/enclave/libsockthread/src/socket_connector.hpp b/apps/enclave/libsockthread/src/socket_connector.hpp new file mode 100644 index 0000000000..4e4026963d --- /dev/null +++ b/apps/enclave/libsockthread/src/socket_connector.hpp @@ -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 diff --git a/apps/enclave/libsockthread/src/socket_error.hpp b/apps/enclave/libsockthread/src/socket_error.hpp index 8c06478f3b..b09d3d71df 100644 --- a/apps/enclave/libsockthread/src/socket_error.hpp +++ b/apps/enclave/libsockthread/src/socket_error.hpp @@ -37,6 +37,11 @@ namespace Libsockthread { Socket_error(const string& s) : runtime_error(s) { } }; + class Dns_error : public Socket_error { + public: + Dns_error(const string& s) + : Socket_error(s) { } + }; } #endif // LIBSOCKTHREAD_SOCKET_ERROR_HPP diff --git a/apps/enclave/libsockthread/src/socket_listener.cpp b/apps/enclave/libsockthread/src/socket_listener.cpp new file mode 100644 index 0000000000..15cad33899 --- /dev/null +++ b/apps/enclave/libsockthread/src/socket_listener.cpp @@ -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; diff --git a/apps/enclave/libsockthread/src/socket_listener.hpp b/apps/enclave/libsockthread/src/socket_listener.hpp new file mode 100644 index 0000000000..c8f1a60a62 --- /dev/null +++ b/apps/enclave/libsockthread/src/socket_listener.hpp @@ -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 -- GitLab