diff --git a/apps/enclave/libsockthread/src/socket.cpp b/apps/enclave/libsockthread/src/socket.cpp index 08247c250527aceb6fbc3de03bd2b042a402c190..5eb552defa69789773e6fb362a4bf5960a71baec 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 7d8ec2623fee7a08356eaa04a8ac0780966d7f42..d226d6c2989aba5fa9084078f233b6fd9fcc88c5 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 53f5167d709902609b9f76c39b76d85af532d5da..7f80a372983a6106059010da242419133dd1f7de 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 0fa769a75ab52d6df4a8095fb1b2ef9e0b483705..12115fc603114e608fe9319b38b5f4fe276b1ac2 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 0000000000000000000000000000000000000000..5039f0d26b91e69b2dade62fa0990d5a5beebfd5 --- /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 0000000000000000000000000000000000000000..4e4026963d654a2c217f7e25faaa5cea1d1e9dee --- /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 8c06478f3b3e976f802bfd905e5816b358ec0d8b..b09d3d71df527267171cd9a4ccadfb9567e76bc7 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 0000000000000000000000000000000000000000..15cad33899955924c96f23d92cc2754450c2c915 --- /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 0000000000000000000000000000000000000000..c8f1a60a6262c30ec0bd8731c03fa762083f1f06 --- /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