From bbc5f6588a55f1dda4455878848704829ca0271e Mon Sep 17 00:00:00 2001 From: mpc <mpc> Date: Sun, 4 Jul 2004 01:53:35 +0000 Subject: [PATCH] *** empty log message *** --- apps/enclave/libsockthread/LICENSE | 27 ++++++ apps/enclave/libsockthread/src/platform.hpp | 64 +++++++++++++- apps/enclave/libsockthread/src/socket.cpp | 56 ++++++++++--- apps/enclave/libsockthread/src/socket.hpp | 17 ++-- .../enclave/libsockthread/src/socket_addr.cpp | 83 ++++++++++++++++++ .../enclave/libsockthread/src/socket_addr.hpp | 49 +++++++++++ apps/enclave/libsockthread/src/strl.c | 84 +++++++++++++++++++ apps/enclave/libsockthread/src/strl.h | 47 +++++++++++ apps/enclave/libsockthread/src/time.cpp | 5 +- 9 files changed, 409 insertions(+), 23 deletions(-) create mode 100644 apps/enclave/libsockthread/LICENSE create mode 100644 apps/enclave/libsockthread/src/socket_addr.cpp create mode 100644 apps/enclave/libsockthread/src/socket_addr.hpp create mode 100644 apps/enclave/libsockthread/src/strl.c create mode 100644 apps/enclave/libsockthread/src/strl.h diff --git a/apps/enclave/libsockthread/LICENSE b/apps/enclave/libsockthread/LICENSE new file mode 100644 index 0000000000..ed1daacf69 --- /dev/null +++ b/apps/enclave/libsockthread/LICENSE @@ -0,0 +1,27 @@ +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. diff --git a/apps/enclave/libsockthread/src/platform.hpp b/apps/enclave/libsockthread/src/platform.hpp index cd42fdae1c..328d8e0c00 100644 --- a/apps/enclave/libsockthread/src/platform.hpp +++ b/apps/enclave/libsockthread/src/platform.hpp @@ -1 +1,63 @@ -#undef WINTHREAD +/* + * 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. + */ + +#ifndef LIBSOCKTHREAD_PLATFORM_HPP +#define LIBSOCKTHREAD_PLATFORM_HPP + +/* + * Operating system + */ +#define FREEBSD 0 // FreeBSD (untested) +#define MINGW 1 // Windows native (Mingw) +#define LINUX 2 // Linux +#define CYGWIN 3 // Cygwin + +#if OS == MINGW + #define INET_ADDRSTRLEN 16 + #define NO_GETHOSTBYNAME2 + #define NO_INET_ATON /* implies NO_INET_PTON */ + #define NO_INET_NTOP + #define WINSOCK + #define WINTHREAD +#endif + +#if OS == LINUX + #define NO_GETHOSTBYNAME2 +#endif + +#if OS == CYGWIN + #define FAST32_IS_LONG + #define INET_ADDRSTRLEN 16 + #define NO_GETHOSTBYNAME2 + #define NO_INET_NTOP + #define NO_INET_PTON +#endif + +#endif // LIBSOCKTHREAD_PLATFORM_HPP diff --git a/apps/enclave/libsockthread/src/socket.cpp b/apps/enclave/libsockthread/src/socket.cpp index 468f8b11c8..8e89ec056a 100644 --- a/apps/enclave/libsockthread/src/socket.cpp +++ b/apps/enclave/libsockthread/src/socket.cpp @@ -28,8 +28,11 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <cassert> +using namespace std; #include "platform.hpp" #include "socket.hpp" +using namespace Libsockthread; size_t Socket::total = 0; // the total number of sockets in use @@ -39,15 +42,20 @@ size_t Socket::total = 0; // the total number of sockets in use Socket::Socket(void) { ++total; + try { #ifdef WINSOCK - if (total == 1) - winsock_startup(); + if (total == 1) + winsock_startup(); #endif - socket(PF_INET, SOCK_STREAM); + create_socket(PF_INET, SOCK_STREAM); + } catch (const Socket_error& x) { + --total; + throw; + } } /* - * Constructs a socket + * Constructs the socket * * domain - either PF_INET or PF_INET6 * type - either SOCK_STREAM or SOCK_DGRAM @@ -55,24 +63,50 @@ Socket::Socket(void) Socket::Socket(int domain, int type) { ++total; + try { #ifdef WINSOCK - if (total == 1) - winsock_startup(); + if (total == 1) + winsock_startup(); #endif - socket(domain, type); + create_socket(domain, type); + } catch {const Socket_error& x) { + --total; + throw; + } } /* - * Destroys a socket + * Destroys the socket */ Socket::~Socket(void) { - total--; - // TODO: finish me + close(); + --total; + assert(total >= 0); +#ifdef WINSOCK + if (total == 0) + winsock_cleanup(); +#endif +} + +/* + * Closes the socket + */ +void Socket::close(void) +{ +#ifdef WINSOCK + if (closesocket(sock) == SOCKET_ERROR) { + LERROR("closesocket() failed: %s", winsock_strerror(WSAGetLastError())); + } +#else + if (close(sock) == -1) { + LERROR("close() failed: %s", strerror(errno)); + } +#endif } /* - * Creates a socket + * Creates the socket * * domain - either PF_INET or PF_INET6 * type - either SOCK_STREAM or SOCK_DGRAM diff --git a/apps/enclave/libsockthread/src/socket.hpp b/apps/enclave/libsockthread/src/socket.hpp index a59b495932..98ad3ad932 100644 --- a/apps/enclave/libsockthread/src/socket.hpp +++ b/apps/enclave/libsockthread/src/socket.hpp @@ -32,13 +32,18 @@ #define LIBSOCKTHREAD_SOCKET_HPP namespace Libsockthread { + class Socket_error : public runtime_error { + public: + Socket_error(const string& s) + : runtime_error(s) { } + }; class Socket { public: - Socket(void); // throws socket error + Socket(void); // throws Socket_error Socket(int domain, int type); // throws Socket_error ~Socket(void); - void func(void); + void close(void); private: #ifdef WINSOCK typedef SOCKET socket_t; @@ -52,13 +57,7 @@ namespace Libsockthread { void create_socket(int domain, int type); // throws Socket_error socket_t sock; - static size_t total; - }; - - class Socket_error : public runtime_error { - public: - Socket_error(const string& s) - : runtime_error(s) { } + static size_t total; // the total number of sockets in memory }; } diff --git a/apps/enclave/libsockthread/src/socket_addr.cpp b/apps/enclave/libsockthread/src/socket_addr.cpp new file mode 100644 index 0000000000..2e30d54748 --- /dev/null +++ b/apps/enclave/libsockthread/src/socket_addr.cpp @@ -0,0 +1,83 @@ +/* + * 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. + */ + +#include <arpa/inet.h> +#include <cassert> +using namespace std; +#include "platform.hpp" +#include "socket.hpp" +#include "socket_addr.hpp" +using namespace Libsockthread; + +Socket_addr::Socket_addr(int domain, const string& host, uint16_t port) + : domain(domain), host(host), port(port) +{ + memset(&hostaddr, 0, sizeof hostaddr); + hostaddr.sin_family = domain; + hostaddr.sin_port = htons(port); + resolve(host.c_str(), ipaddr); + int rc; +#ifdef NO_INET_ATON + rc = hostaddr.sin_addr.s_addr = inet_addr(ipaddr); +#elif defined NO_INET_PTON + rc = inet_aton(ipaddr, &hostaddr.sin_addr); +#else + rc = inet_pton(AF_INET, ipaddr, &hostaddr.sin_addr); +#endif + assert(rc != 0 && rc != -1); +} + +/* + * Performs a DNS lookup on `hostname' and puts the result in `ipaddr' + */ +bool Socket::resolve(const char* hostname, char* ipaddr) +{ + struct hostent *h; +#ifdef NO_GETHOSTBYNAME2 + h = gethostbyname(hostname); +#else + h = gethostbyname2(hostname, domain); +#endif + if (h == 0) { + LWARN("DNS resolution failed for %s", hostname); + throw Socket_error("DNS resolution failed"); + } + struct in_addr a; + a.s_addr = ((struct in_addr *)h->h_addr)->s_addr; +#ifdef NO_INET_NTOP + char *tmp; + tmp = inet_ntoa(a); + assert(tmp != 0); + strlcpy(ipaddr, tmp, INET_ADDRSTRLEN); // inet_ntoa() was very poorly designed +#else + int rc = inet_ntop(domain, &a, ipaddr, INET_ADDRSTRLEN); + assert(rc != 0); +#endif +} diff --git a/apps/enclave/libsockthread/src/socket_addr.hpp b/apps/enclave/libsockthread/src/socket_addr.hpp new file mode 100644 index 0000000000..dcf89311f3 --- /dev/null +++ b/apps/enclave/libsockthread/src/socket_addr.hpp @@ -0,0 +1,49 @@ +/* + * 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. + */ + +#ifndef LIBSOCKTHREAD_SOCKET_ADDR_HPP +#define LIBSOCKTHREAD_SOCKET_ADDR_HPP + +namespace Libsockthread { + class Socket_addr { + public: + Socket_addr(int domain, const string& host, uint16_t port); + private: + bool resolve(const char* hostname, char* ipaddr); + + int domain; // PF_INET or PF_INET6 + string host; + char ipaddr[INET_ADDRSTRLEN]; + struct sockaddr_in hostaddr; + uint16_t port; + }; +} + +#endif // LIBSOCKTHREAD_SOCKET_ADDR_HPP diff --git a/apps/enclave/libsockthread/src/strl.c b/apps/enclave/libsockthread/src/strl.c new file mode 100644 index 0000000000..792ba5ae90 --- /dev/null +++ b/apps/enclave/libsockthread/src/strl.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <stddef.h> +#include <string.h> + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t siz) +{ + register char *d = dst; + register const char *s = src; + register size_t n = siz; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + + return(dlen + (s - src)); /* count does not include NUL */ +} + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t siz) +{ + register char *d = dst; + register const char *s = src; + register size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} diff --git a/apps/enclave/libsockthread/src/strl.h b/apps/enclave/libsockthread/src/strl.h new file mode 100644 index 0000000000..9fd935c78a --- /dev/null +++ b/apps/enclave/libsockthread/src/strl.h @@ -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. + */ + +/* + * Note: The strl.c file retains its original license (at the top of strl.c) + */ + +#ifndef LIBSOCKTHREAD_STRL_H +#define LIBSOCKTHREAD_STRL_H +#ifdef __cplusplus +extern "C" { +#endif + +extern size_t strlcat(char *dst, const char *src, size_t siz); +extern size_t strlcpy(char *dst, const char *src, size_t siz); + +#ifdef __cplusplus +} +#endif +#endif /* LIBSOCKTHREAD_STRL_H */ diff --git a/apps/enclave/libsockthread/src/time.cpp b/apps/enclave/libsockthread/src/time.cpp index 50927307ce..e9c89f52ed 100644 --- a/apps/enclave/libsockthread/src/time.cpp +++ b/apps/enclave/libsockthread/src/time.cpp @@ -35,8 +35,9 @@ using namespace std; using namespace Libsockthread; /* - * Converts the time to an ISO 8601 standard time and date and puts it in a string - * Example: 2004-07-01 19:03:47Z + * Converts the time to an ISO 8601 standard time and date and puts it in a + * string + * Example: 2004-07-01T19:03:47Z */ string& Time::utc(string &s) const { -- GitLab