diff --git a/apps/enclave/libsockthread/src/platform.hpp b/apps/enclave/libsockthread/src/platform.hpp index 72fbc41bc..d9b83e66e 100644 --- a/apps/enclave/libsockthread/src/platform.hpp +++ b/apps/enclave/libsockthread/src/platform.hpp @@ -27,13 +27,13 @@ * 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: platform.hpp,v 1.4 2004/07/16 23:54:45 mpc Exp $ */ /* * Global includes and platform configuration. This is used to compile the * library, but is not intended for use by users of the library in their - * programs. + * own programs. */ #ifndef LIBSOCKTHREAD_PLATFORM_HPP @@ -43,20 +43,25 @@ * Operating system */ #define FREEBSD 0 // FreeBSD -#define MINGW 1 // Windows native (Mingw) +#define WIN32 1 // Windows #define LINUX 2 // Linux -#define CYGWIN 3 // Cygwin -#if OS == MINGW +#if OS == WIN32 #define WINSOCK #define WINTHREAD #endif +#ifndef WINSOCK + #include +#endif #include #include #include #include #include +#ifndef WINSOCK + #include +#endif #ifndef WINTHREAD #include #endif diff --git a/apps/enclave/libsockthread/src/socket.cpp b/apps/enclave/libsockthread/src/socket.cpp new file mode 100644 index 000000000..08247c250 --- /dev/null +++ b/apps/enclave/libsockthread/src/socket.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2004, Matthew P. Cashdollar + * 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.hpp" +using namespace Libsockthread; diff --git a/apps/enclave/libsockthread/src/socket.hpp b/apps/enclave/libsockthread/src/socket.hpp new file mode 100644 index 000000000..7d8ec2623 --- /dev/null +++ b/apps/enclave/libsockthread/src/socket.hpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2004, Matthew P. Cashdollar + * 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_HPP +#define LIBSOCKTHREAD_SOCKET_HPP + +namespace Libsockthread { + class Socket { + public: + Socket(Socket_addr& addr) + : addr(addr); + + 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_blocking(bool blocking); + private: + Socket_addr addr; + }; +} + +#endif // LIBSOCKTHREAD_SOCKET_HPP diff --git a/apps/enclave/libsockthread/src/socket_addr.cpp b/apps/enclave/libsockthread/src/socket_addr.cpp new file mode 100644 index 000000000..4e2d51ee5 --- /dev/null +++ b/apps/enclave/libsockthread/src/socket_addr.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2004, Matthew P. Cashdollar + * 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_error.hpp" +#include "socket_addr.hpp" +using namespace Libsockthread; + +Socket_addr& Socket_addr::operator=(const Socket_addr& rhs) +{ + if (this == &rhs) // check for self-assignment: a = a + return *this; + + 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; + + return *this; +} + +void Socket_addr::resolve() +{ + hostent* hent = gethostbyname(host.c_str()); + if (hent == NULL) + throw Socket_error(hstrerror(h_errno)); + assert(hent->h_addrtype == AF_INET || hent->h_addrtype == AF_INET6); + domain = hent->h_addrtype; + if (domain == AF_INET) { + ip = new char[INET_ADDRSTRLEN]; + else + ip = new char[INET6_ADDRSTRLEN]; + strcpy(ip, hent->h_addr_list[0]); +} diff --git a/apps/enclave/libsockthread/src/socket_addr.hpp b/apps/enclave/libsockthread/src/socket_addr.hpp new file mode 100644 index 000000000..6a2b9e414 --- /dev/null +++ b/apps/enclave/libsockthread/src/socket_addr.hpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2004, Matthew P. Cashdollar + * 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_ADDR_HPP +#define LIBSOCKTHREAD_SOCKET_ADDR_HPP + +namespace Libsockthread { + class Socket_addr { + public: + 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() + { delete[] ip; } + + int get_domain() const // Has nothing to do with DNS domain + { return domain; } + 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; + Socket_addr& Socket_addr::operator=(const Socket_addr& rhs); + void set_domain(int domain) + { this->domain = domain; } + void set_host(string& host) // throws Socket_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(); + + int domain; + string host; + char* ip; + uint16_t port; + int type; + }; +} + +#endif // LIBSOCKTHREAD_SOCKET_ADDR_HPP diff --git a/apps/enclave/libsockthread/src/socket_error.hpp b/apps/enclave/libsockthread/src/socket_error.hpp new file mode 100644 index 000000000..8c06478f3 --- /dev/null +++ b/apps/enclave/libsockthread/src/socket_error.hpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2004, Matthew P. Cashdollar + * 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. + */ + +#ifndef LIBSOCKTHREAD_SOCKET_ERROR_HPP +#define LIBSOCKTHREAD_SOCKET_ERROR_HPP + +namespace Libsockthread { + class Socket_error : public runtime_error { + public: + Socket_error(const string& s) + : runtime_error(s) { } + }; +} + +#endif // LIBSOCKTHREAD_SOCKET_ERROR_HPP