From 5c3b39340d365f5ff37a79424956591e87b44816 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 26 Jun 2018 12:58:54 -0400 Subject: init --- .gitignore | 16 ++ CMakeLists.txt | 56 ++++ LICENSE | 21 ++ cmake/Modules/FindLibevent.cmake | 49 ++++ include/salticidae/conn.h | 235 +++++++++++++++++ include/salticidae/crypto.h | 78 ++++++ include/salticidae/msg.h | 253 ++++++++++++++++++ include/salticidae/netaddr.h | 115 ++++++++ include/salticidae/network.h | 552 +++++++++++++++++++++++++++++++++++++++ include/salticidae/ref.h | 202 ++++++++++++++ include/salticidae/stream.h | 274 +++++++++++++++++++ include/salticidae/type.h | 67 +++++ include/salticidae/util.h | 292 +++++++++++++++++++++ src/conn.cpp | 277 ++++++++++++++++++++ src/util.cpp | 244 +++++++++++++++++ test/.gitignore | 1 + test/CMakeLists.txt | 24 ++ test/Makefile | 180 +++++++++++++ test/test_msg.cpp | 74 ++++++ 19 files changed, 3010 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 LICENSE create mode 100644 cmake/Modules/FindLibevent.cmake create mode 100644 include/salticidae/conn.h create mode 100644 include/salticidae/crypto.h create mode 100644 include/salticidae/msg.h create mode 100644 include/salticidae/netaddr.h create mode 100644 include/salticidae/network.h create mode 100644 include/salticidae/ref.h create mode 100644 include/salticidae/stream.h create mode 100644 include/salticidae/type.h create mode 100644 include/salticidae/util.h create mode 100644 src/conn.cpp create mode 100644 src/util.cpp create mode 100644 test/.gitignore create mode 100644 test/CMakeLists.txt create mode 100644 test/Makefile create mode 100644 test/test_msg.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..475a195 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +CMakeFiles/ +cmake_install.cmake +CMakeDoxygenDefaults.cmake +CMakeDoxyfile.in +CMakeCache.txt +cmake-build-debug/ +libsalticidae.a +src/*.swo +src/*.swp +*.a +*.o +*.la +*.lo +*.so +*.gch +/Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5931322 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,56 @@ +# Copyright (c) 2018 Cornell University. +# +# Author: Ted Yin +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is furnished to do +# so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +cmake_minimum_required(VERSION 3.9) +project(Salticidae) +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") + +find_package(Libevent REQUIRED) +find_package(OpenSSL REQUIRED) + +include_directories(include) +add_library(salticidae + src/util.cpp + src/conn.cpp) +target_link_libraries(salticidae event crypto) + +add_subdirectory(test) + +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + set(CMAKE_BUILD_TYPE "Release") +endif() + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + add_definitions(-DHOTSTUFF_DEBUG_LOG) +elseif(CMAKE_BUILD_TYPE STREQUAL "Release") + add_definitions(-DHOTSTUFF_NORMAL_LOG) +endif() + +#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -no-pie -pg") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -W -Wall -Wextra -pedantic") + +macro(remove_cxx_flag flag) + string(REPLACE "${flag}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") +endmacro() + +remove_cxx_flag("-DNDEBUG") diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..11712af --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +Copyright (c) 2018 Cornell University. + +Author: Ted Yin + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/cmake/Modules/FindLibevent.cmake b/cmake/Modules/FindLibevent.cmake new file mode 100644 index 0000000..e874e67 --- /dev/null +++ b/cmake/Modules/FindLibevent.cmake @@ -0,0 +1,49 @@ +# Copyright (c) 2018 Cornell University. +# +# Author: Ted Yin +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is furnished to do +# so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +# This module will define: +# - Libevent_FOUND - if the library is found +# - LIBEVENT_INC - include directories +# - LIBEVENT_LIB - the libraries required to use libevent + +set(LIBEVENT_PREFIXES /usr/local /opt/local) + +foreach(prefix ${LIBEVENT_PREFIXES}) + list(APPEND LIBEVENT_LIB_PATH "${prefix}/lib") + list(APPEND LIBEVENT_INC_PATH "${prefix}/include") +endforeach() + +find_library(LIBEVENT_LIB NAMES event PATHS ${LIBEVENT_LIB_PATH}) +find_path(LIBEVENT_INC event.h PATHS ${LIBEVENT_INC_PATH}) + +if (LIBEVENT_LIB AND LIBEVENT_INC) + set(Libevent_FOUND TRUE) + if (NOT Libevent_FIND_QUIETLY) + message(STATUS "Found libevent: ${LIBEVENT_LIB}") + endif () +else () + set(Libevent_FOUND FALSE) + if (Libevent_FIND_REQUIRED) + message(FATAL_ERROR "Could NOT find libevent.") + endif () + message(STATUS "libevent NOT found.") +endif () diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h new file mode 100644 index 0000000..40facc9 --- /dev/null +++ b/include/salticidae/conn.h @@ -0,0 +1,235 @@ +/** + * Copyright (c) 2018 Cornell University. + * + * Author: Ted Yin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _SALTICIDAE_CONN_H +#define _SALTICIDAE_CONN_H + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "salticidae/type.h" +#include "salticidae/ref.h" +#include "salticidae/util.h" +#include "salticidae/netaddr.h" +#include "salticidae/msg.h" + +const int MAX_LISTEN_BACKLOG = 10; +const size_t BUFF_SEG_SIZE = 4096; +const size_t MAX_MSG_HANDLER = 64; +const double TRY_CONN_DELAY = 2; +const double CONN_SERVER_TIMEOUT = 2; + +namespace salticidae { + +inline double gen_rand_timeout(double base_timeout) { + return base_timeout + rand() / (double)RAND_MAX * 0.5 * base_timeout; +} + +class RingBuffer { + struct buffer_entry_t { + bytearray_t data; + bytearray_t::iterator offset; + buffer_entry_t(bytearray_t &&_data): data(std::move(_data)) { + offset = data.begin(); + } + + buffer_entry_t(buffer_entry_t &&other) { + size_t _offset = other.offset - other.data.begin(); + data = std::move(other.data); + offset = data.begin() + _offset; + } + + buffer_entry_t(const buffer_entry_t &other): data(other.data) { + offset = data.begin() + (other.offset - other.data.begin()); + } + + size_t length() const { return data.end() - offset; } + }; + std::list ring; + size_t _size; + + public: + RingBuffer(): _size(0) {} + ~RingBuffer() { clear(); } + RingBuffer &operator=(const RingBuffer &other) = delete; + RingBuffer(const RingBuffer &other) = delete; + RingBuffer &operator=(RingBuffer &&other) { + ring = std::move(other.ring); + _size = other._size; + other._size = 0; + return *this; + } + + void push(bytearray_t &&data) { + _size += data.size(); + ring.push_back(buffer_entry_t(std::move(data))); + } + + bytearray_t pop(size_t len) { + bytearray_t res; + auto i = ring.begin(); + while (len && i != ring.end()) + { + size_t copy_len = std::min(i->length(), len); + res.insert(res.end(), i->offset, i->offset + copy_len); + i->offset += copy_len; + len -= copy_len; + if (i->offset == i->data.end()) + i++; + } + ring.erase(ring.begin(), i); + _size -= res.size(); + return std::move(res); + } + + size_t size() const { return _size; } + + void clear() { + ring.clear(); + _size = 0; + } +}; + +class ConnPoolError: public SalticidaeError { + using SalticidaeError::SalticidaeError; +}; + +/** The connection pool. */ +class ConnPool { + public: + class Conn; + using conn_t = RcObj; + /** The abstraction for a bi-directional connection. */ + class Conn { + public: + enum ConnMode { + ACTIVE, /**< the connection is established by connect() */ + PASSIVE, /**< the connection is established by accept() */ + }; + + private: + conn_t self_ref; + int fd; + ConnPool *cpool; + ConnMode mode; + NetAddr addr; + + RingBuffer send_buffer; + RingBuffer recv_buffer; + + Event ev_read; + Event ev_write; + Event ev_connect; + /** does not need to wait if true */ + bool ready_send; + + void recv_data(evutil_socket_t, short); + void send_data(evutil_socket_t, short); + void conn_server(evutil_socket_t, short); + void try_conn(evutil_socket_t, short); + + public: + friend ConnPool; + Conn(): self_ref(this) {} + + virtual ~Conn() { + SALTICIDAE_LOG_INFO("destroyed connection %s", std::string(*this).c_str()); + } + + conn_t self() { return self_ref; } + operator std::string() const; + int get_fd() const { return fd; } + const NetAddr &get_addr() const { return addr; } + ConnMode get_mode() const { return mode; } + RingBuffer &read() { return recv_buffer; } + + void write(bytearray_t &&data) { + send_buffer.push(std::move(data)); + if (ready_send) + send_data(fd, EV_WRITE); + } + + void move_send_buffer(conn_t other) { + send_buffer = std::move(other->send_buffer); + } + + void terminate(); + + protected: + /** close the connection and free all on-going or planned events. */ + virtual void close() { + ev_read.clear(); + ev_write.clear(); + ev_connect.clear(); + ::close(fd); + fd = -1; + } + + virtual void on_read() = 0; + virtual void on_setup() = 0; + virtual void on_teardown() = 0; + }; + + private: + std::unordered_map pool; + int listen_fd; + Event ev_listen; + + void accept_client(evutil_socket_t, short); + conn_t add_conn(conn_t conn); + + protected: + struct event_base *eb; + virtual conn_t create_conn() = 0; + + public: + friend Conn; + ConnPool(struct event_base *eb): eb(eb) {} + + ~ConnPool() { + for (auto it: pool) + { + conn_t conn = it.second; + conn->close(); + } + } + + /** create an active mode connection to addr */ + conn_t create_conn(const NetAddr &addr); + /** setup and start listening */ + void init(NetAddr listen_addr); +}; + +} + +#endif diff --git a/include/salticidae/crypto.h b/include/salticidae/crypto.h new file mode 100644 index 0000000..c329c63 --- /dev/null +++ b/include/salticidae/crypto.h @@ -0,0 +1,78 @@ +/** + * Copyright (c) 2018 Cornell University. + * + * Author: Ted Yin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _SALTICIDAE_CRYPTO_H +#define _SALTICIDAE_CRYPTO_H + +#include "salticidae/type.h" +#include + +namespace salticidae { + +class SHA256 { + SHA256_CTX *ctx; + + public: + SHA256(): ctx(new SHA256_CTX()) { reset(); } + ~SHA256() { delete ctx; } + + void reset() { + if (!SHA256_Init(ctx)) + throw std::runtime_error("openssl SHA256 init error"); + } + + template + void update(const T &data) { + update(reinterpret_cast(&*data.begin()), data.size()); + } + + void update(const bytearray_t::const_iterator &it, size_t length) { + update(&*it, length); + } + + void update(const uint8_t *ptr, size_t length) { + if (!SHA256_Update(ctx, ptr, length)) + throw std::runtime_error("openssl SHA256 update error"); + } + + void _digest(bytearray_t &md) { + if (!SHA256_Final(&*md.begin(), ctx)) + throw std::runtime_error("openssl SHA256 error"); + } + + void digest(bytearray_t &md) { + md.resize(32); + _digest(md); + } + + bytearray_t digest() { + bytearray_t md(32); + _digest(md); + return std::move(md); + } +}; + +} + +#endif diff --git a/include/salticidae/msg.h b/include/salticidae/msg.h new file mode 100644 index 0000000..62fc33b --- /dev/null +++ b/include/salticidae/msg.h @@ -0,0 +1,253 @@ +/** + * Copyright (c) 2018 Cornell University. + * + * Author: Ted Yin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _SALTICIDAE_MSG_H +#define _SALTICIDAE_MSG_H + +#include +#include +#include +#include + +#include "salticidae/type.h" +#include "salticidae/stream.h" +#include "salticidae/netaddr.h" + +namespace salticidae { + +template +class MsgBase { + public: + using opcode_t = OpcodeType; + static const opcode_t OPCODE_PING = PING; + static const opcode_t OPCODE_PONG = PONG; + static const size_t header_size; + + private: + /* header */ + /* all integers are encoded in little endian in the protocol */ + uint32_t magic; + opcode_t opcode; + uint32_t length; + uint32_t checksum; + + mutable bytearray_t payload; + mutable bool no_payload; + + public: + MsgBase(): magic(0x0), no_payload(true) {} + + MsgBase(const MsgBase &other): + magic(other.magic), + opcode(other.opcode), + length(other.length), + checksum(other.checksum), + payload(other.payload), + no_payload(other.no_payload) {} + + MsgBase(MsgBase &&other): + magic(other.magic), + opcode(std::move(other.opcode)), + length(other.length), + checksum(other.checksum), + payload(std::move(other.payload)), + no_payload(other.no_payload) {} + + MsgBase(const uint8_t *raw_header) { + uint32_t _magic; + opcode_t _opcode; + uint32_t _length; + uint32_t _checksum; + DataStream s(raw_header, raw_header + MsgBase::header_size); + + s >> _magic + >> _opcode + >> _length + >> _checksum; + magic = letoh(_magic); + opcode = _opcode; + length = letoh(_length); + checksum = letoh(_checksum); + } + + MsgBase &operator=(const MsgBase &other) { + magic = other.magic; + opcode = other.opcode; + length = other.length; + checksum = other.checksum; + payload = other.payload; + no_payload = other.no_payload; + return *this; + } + + MsgBase &operator=(MsgBase &&other) { + magic = other.magic; + opcode = std::move(other.opcode); + length = other.length; + checksum = other.checksum; + payload = std::move(other.payload); + no_payload = other.no_payload; + return *this; + } + + ~MsgBase() {} + + size_t get_length() const { return length; } + + const opcode_t &get_opcode() const { return opcode; } + + void set_opcode(const opcode_t &_opcode) { + opcode = _opcode; + } + + bytearray_t &&get_payload() const { +#ifndef SALTICIDAE_NOCHECK + if (no_payload) + throw std::runtime_error("payload not available"); + no_payload = true; +#endif + return std::move(payload); + } + + void set_payload(DataStream &&s) { + set_payload(bytearray_t(std::move(s))); + } + + void set_payload(bytearray_t &&_payload) { + payload = std::move(_payload); + length = payload.size(); + checksum = get_checksum(); +#ifndef SALTICIDAE_NOCHECK + no_payload = false; +#endif + } + + operator std::string() const { + DataStream s; + s << "<" + << "magic=" << get_hex(magic) << " " + << "opcode=" << get_hex(opcode) << " " + << "length=" << get_hex(length) << " " + << "checksum=" << get_hex(checksum) << " " + << "payload=" << get_hex(payload) << ">"; + + //std::string opcode_hex = get_hex(opcode); + //char *buff = new char[128 + opcode_hex.size()]; + //size_t ret = sprintf(buff, + // ""; + //delete [] buff; + //return std::move(res); + return std::string(s); + } + + uint32_t get_checksum() const { + static class SHA256 sha256; + uint32_t res; + bytearray_t tmp; + sha256.reset(); + sha256.update(payload); + sha256.digest(tmp); + sha256.reset(); + sha256.update(tmp); + sha256.digest(tmp); + memmove(&res, &*tmp.begin(), 4); + return res; + } + + bool verify_checksum() const { + return checksum == get_checksum(); + } + + bytearray_t serialize() const { + DataStream s; + s << htole(magic) + << opcode + << htole(length) + << htole(checksum) + << payload; + return std::move(s); + } + + void gen_ping(uint16_t port) { + DataStream s; + set_opcode(OPCODE_PING); + s << htole(port); + set_payload(std::move(s)); + } + + void parse_ping(uint16_t &port) const { + DataStream s(get_payload()); + s >> port; + port = letoh(port); + } + + void gen_pong(uint16_t port) { + DataStream s; + set_opcode(OPCODE_PONG); + s << htole(port); + set_payload(std::move(s)); + } + + void parse_pong(uint16_t &port) const { + DataStream s(get_payload()); + s >> port; + port = letoh(port); + } + + void gen_hash_list(DataStream &s, + const std::vector &hashes) { + uint32_t size = htole((uint32_t)hashes.size()); + s << size; + for (const auto &h: hashes) s << h; + } + + void parse_hash_list(DataStream &s, + std::vector &hashes) const { + uint32_t size; + hashes.clear(); + + s >> size; + size = letoh(size); + + hashes.resize(size); + for (auto &hash: hashes) s >> hash; + } + +}; + +template +const size_t MsgBase::header_size = + sizeof(MsgBase) - + sizeof(MsgBase::payload) - + sizeof(MsgBase::no_payload); +} + +#endif diff --git a/include/salticidae/netaddr.h b/include/salticidae/netaddr.h new file mode 100644 index 0000000..c166c3a --- /dev/null +++ b/include/salticidae/netaddr.h @@ -0,0 +1,115 @@ +/** + * Copyright (c) 2018 Cornell University. + * + * Author: Ted Yin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _SALTICIDAE_NETADDR_H +#define _SALTICIDAE_NETADDR_H + +#include +#include +#include +#include + +#include "salticidae/util.h" + +namespace salticidae { + +/* TODO: IPv6 support */ + +struct NetAddr { + uint32_t ip; + uint16_t port; + /* construct from human-readable format */ + NetAddr(): ip(0), port(0) {} + + NetAddr(uint32_t ip, uint16_t port): ip(ip), port(port) {} + + NetAddr(const std::string &_addr, uint16_t _port) { + set_by_ip_port(_addr, _port); + } + + void set_by_ip_port(const std::string &_addr, uint16_t _port) { + struct hostent *h; + if ((h = gethostbyname(_addr.c_str())) == nullptr) + throw SalticidaeError("gethostbyname failed"); + memmove(&ip, h->h_addr_list[0], sizeof(in_addr_t)); + port = htons(_port); + } + + NetAddr(const std::string &ip_port_addr) { + size_t pos = ip_port_addr.find(":"); + if (pos == std::string::npos) + throw SalticidaeError("invalid port format"); + std::string ip_str = ip_port_addr.substr(0, pos); + std::string port_str = ip_port_addr.substr(pos + 1); + long port; + try { + port = std::stol(port_str.c_str()); + } catch (std::logic_error) { + throw SalticidaeError("invalid port format"); + } + if (port < 0) + throw SalticidaeError("negative port number"); + if (port > 0xffff) + throw SalticidaeError("port number greater than 0xffff"); + set_by_ip_port(ip_str, (uint16_t)port); + } + /* construct from unix socket format */ + NetAddr(const struct sockaddr_in *addr_sock) { + ip = addr_sock->sin_addr.s_addr; + port = addr_sock->sin_port; + } + + bool operator==(const NetAddr &other) const { + return ip == other.ip && port == other.port; + } + + operator std::string() const { + struct in_addr in; + in.s_addr = ip; + return ""; + } + + bool is_null() const { return ip == 0 && port == 0; } +}; + +} + +namespace std { + template <> + struct hash { + size_t operator()(const salticidae::NetAddr &k) const { + return k.ip ^ k.port; + } + }; + + template <> + struct hash { + size_t operator()(const salticidae::NetAddr &k) const { + return k.ip ^ k.port; + } + }; +} + +#endif diff --git a/include/salticidae/network.h b/include/salticidae/network.h new file mode 100644 index 0000000..3b82927 --- /dev/null +++ b/include/salticidae/network.h @@ -0,0 +1,552 @@ +/** + * Copyright (c) 2018 Cornell University. + * + * Author: Ted Yin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _SALTICIDAE_NETWORK_H +#define _SALTICIDAE_NETWORK_H + +#include "salticidae/netaddr.h" +#include "salticidae/msg.h" +#include "salticidae/conn.h" + +namespace salticidae { + +/** Network of nodes who can send async messages. */ +template +class MsgNetwork: public ConnPool { + public: + class Conn: public ConnPool::Conn { + enum MsgState { + HEADER, + PAYLOAD + }; + MsgType msg; + MsgState msg_state; + MsgNetwork *mn; + + protected: + mutable size_t nsent; + mutable size_t nrecv; + + public: + friend MsgNetwork; + Conn(MsgNetwork *mn): msg_state(HEADER), mn(mn), nsent(0), nrecv(0) {} + size_t get_nsent() const { return nsent; } + size_t get_nrecv() const { return nrecv; } + void clear_nsent() const { nsent = 0; } + void clear_nrecv() const { nrecv = 0; } + + protected: + void on_read() override; + void on_setup() override {} + void on_teardown() override {} + }; + + using conn_t = RcObj; + using msg_callback_t = std::function; + class msg_stat_by_opcode_t: + public std::unordered_map> { + public: + void add(const MsgType &msg) { + auto &p = this->operator[](msg.get_opcode()); + p.first++; + p.second += msg.get_length(); + } + }; + + private: + std::unordered_map handler_map; + + protected: + mutable msg_stat_by_opcode_t sent_by_opcode; + mutable msg_stat_by_opcode_t recv_by_opcode; + uint16_t listen_port; + + ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); } + + public: + MsgNetwork(struct event_base *eb): ConnPool(eb) {} + void reg_handler(typename MsgType::opcode_t opcode, msg_callback_t handler); + void send_msg(const MsgType &msg, conn_t conn); + void init(NetAddr listen_addr); + msg_stat_by_opcode_t &get_sent_by_opcode() const { + return sent_by_opcode; + } + msg_stat_by_opcode_t &get_recv_by_opcode() const { + return recv_by_opcode; + } +}; + +/** Simple network that handles client-server requests. */ +template +class ClientNetwork: public MsgNetwork { + using MsgNet = MsgNetwork; + std::unordered_map addr2conn; + + public: + class Conn: public MsgNet::Conn { + ClientNetwork *cn; + + public: + Conn(ClientNetwork *cn): + MsgNet::Conn(static_cast(cn)), + cn(cn) {} + + protected: + void on_setup() override; + void on_teardown() override; + }; + + protected: + ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); } + + public: + ClientNetwork(struct event_base *eb): MsgNet(eb) {} + void send_msg(const MsgType &msg, const NetAddr &addr); +}; + +class PeerNetworkError: public SalticidaeError { + using SalticidaeError::SalticidaeError; +}; + +/** Peer-to-peer network where any two nodes could hold a bi-diretional message + * channel, established by either side. */ +template +class PeerNetwork: public MsgNetwork { + using MsgNet= MsgNetwork; + public: + enum IdentityMode { + IP_BASED, + IP_PORT_BASED + }; + + class Conn: public MsgNet::Conn { + NetAddr peer_id; + Event ev_timeout; + PeerNetwork *pn; + + public: + friend PeerNetwork; + const NetAddr &get_peer() { return peer_id; } + Conn(PeerNetwork *pn): + MsgNet::Conn(static_cast(pn)), + pn(pn) {} + + protected: + void close() override { + ev_timeout.clear(); + MsgNet::Conn::close(); + } + + void on_setup() override; + void on_teardown() override; + }; + + using conn_t = RcObj; + + private: + struct Peer { + /** connection addr, may be different due to passive mode */ + NetAddr addr; + /** the underlying connection, may be invalid when connected = false */ + conn_t conn; + PeerNetwork *pn; + Event ev_ping_timer; + bool ping_timer_ok; + bool pong_msg_ok; + bool connected; + + Peer() = delete; + Peer(NetAddr addr, conn_t conn, PeerNetwork *pn, struct event_base *eb): + addr(addr), conn(conn), pn(pn), + ev_ping_timer( + Event(eb, -1, 0, std::bind(&Peer::ping_timer, this, _1, _2))), + connected(false) {} + ~Peer() {} + Peer &operator=(const Peer &) = delete; + Peer(const Peer &) = delete; + + void ping_timer(evutil_socket_t, short); + void reset_ping_timer(); + void send_ping(); + void clear_all_events() { + if (ev_ping_timer) + ev_ping_timer.del(); + } + void reset_conn(conn_t conn); + }; + + std::unordered_map id2peer; + std::vector peer_list; + + IdentityMode id_mode; + double ping_period; + double conn_timeout; + + void msg_ping(const MsgType &msg, ConnPool::conn_t conn); + void msg_pong(const MsgType &msg, ConnPool::conn_t conn); + void reset_conn_timeout(conn_t conn); + bool check_new_conn(conn_t conn, uint16_t port); + void start_active_conn(const NetAddr &paddr); + + protected: + ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); } + + public: + PeerNetwork(struct event_base *eb, + double ping_period = 30, + double conn_timeout = 180, + IdentityMode id_mode = IP_PORT_BASED): + MsgNet(eb), + id_mode(id_mode), + ping_period(ping_period), + conn_timeout(conn_timeout) {} + + void add_peer(const NetAddr &paddr); + const conn_t get_peer_conn(const NetAddr &paddr) const; + void send_msg(const MsgType &msg, const Peer *peer); + void send_msg(const MsgType &msg, const NetAddr &paddr); + void init(NetAddr listen_addr); + bool has_peer(const NetAddr &paddr) const; + const std::vector &all_peers() const; + using ConnPool::create_conn; +}; + +template +void MsgNetwork::Conn::on_read() { + auto &recv_buffer = read(); + auto conn = static_pointer_cast(self()); + while (get_fd() != -1) + { + if (msg_state == Conn::HEADER) + { + if (recv_buffer.size() < MsgType::header_size) break; + /* new header available */ + bytearray_t data = recv_buffer.pop(MsgType::header_size); + msg = MsgType(data.data()); + msg_state = Conn::PAYLOAD; + } + if (msg_state == Conn::PAYLOAD) + { + size_t len = msg.get_length(); + if (recv_buffer.size() < len) break; + /* new payload available */ + bytearray_t data = recv_buffer.pop(len); + msg.set_payload(std::move(data)); + msg_state = Conn::HEADER; + if (!msg.verify_checksum()) + { + SALTICIDAE_LOG_WARN("checksums do not match, dropping the message"); + return; + } + auto it = mn->handler_map.find(msg.get_opcode()); + if (it == mn->handler_map.end()) + SALTICIDAE_LOG_WARN("unknown command: %s", get_hex(msg.get_opcode())); + else /* call the handler */ + { + SALTICIDAE_LOG_DEBUG("got message %s from %s", + std::string(msg).c_str(), + std::string(*this).c_str()); + it->second(msg, conn); + nrecv++; + mn->recv_by_opcode.add(msg); + } + } + } +} + +template +void PeerNetwork::Peer::reset_conn(conn_t new_conn) { + if (conn != new_conn) + { + if (conn) + { + SALTICIDAE_LOG_DEBUG("moving send buffer"); + new_conn->move_send_buffer(conn); + SALTICIDAE_LOG_INFO("terminating old connection %s", std::string(*conn).c_str()); + conn->terminate(); + } + addr = new_conn->get_addr(); + conn = new_conn; + } + clear_all_events(); +} + +template +void PeerNetwork::Conn::on_setup() { + assert(!ev_timeout); + ev_timeout = Event(pn->eb, -1, 0, [this](evutil_socket_t, short) { + SALTICIDAE_LOG_INFO("peer ping-pong timeout"); + this->terminate(); + }); + /* the initial ping-pong to set up the connection */ + MsgType ping; + ping.gen_ping(pn->listen_port); + auto conn = static_pointer_cast(this->self()); + pn->reset_conn_timeout(conn); + pn->MsgNet::send_msg(ping, conn); +} + +template +void PeerNetwork::Conn::on_teardown() { + auto it = pn->id2peer.find(peer_id); + if (it == pn->id2peer.end()) return; + Peer *p = it->second; + if (this != p->conn) return; + p->ev_ping_timer.del(); + p->connected = false; + p->conn = nullptr; + SALTICIDAE_LOG_INFO("connection lost %s for %s", + std::string(*this).c_str(), + std::string(peer_id).c_str()); + pn->start_active_conn(peer_id); +} + +template +bool PeerNetwork::check_new_conn(conn_t conn, uint16_t port) { + if (conn->peer_id.is_null()) + { /* passive connections can eventually have ids after getting the port + number in IP_BASED_PORT mode */ + assert(id_mode == IP_PORT_BASED); + conn->peer_id.ip = conn->get_addr().ip; + conn->peer_id.port = port; + } + Peer *p = id2peer.find(conn->peer_id)->second; + if (p->connected) + { + if (conn != p->conn) + { + conn->terminate(); + return true; + } + return false; + } + p->reset_conn(conn); + p->connected = true; + p->reset_ping_timer(); + p->send_ping(); + if (p->connected) + SALTICIDAE_LOG_INFO("PeerNetwork: established connection with id %s via %s", + std::string(conn->peer_id).c_str(), std::string(*conn).c_str()); + return false; +} + +template +void PeerNetwork::msg_ping(const MsgType &msg, ConnPool::conn_t conn_) { + auto conn = static_pointer_cast(conn_); + uint16_t port; + msg.parse_ping(port); + SALTICIDAE_LOG_INFO("ping from %s, port %u", std::string(*conn).c_str(), ntohs(port)); + if (check_new_conn(conn, port)) return; + Peer *p = id2peer.find(conn->peer_id)->second; + MsgType pong; + pong.gen_pong(this->listen_port); + send_msg(pong, p); +} + +template +void PeerNetwork::msg_pong(const MsgType &msg, ConnPool::conn_t conn_) { + auto conn = static_pointer_cast(conn_); + auto it = id2peer.find(conn->peer_id); + if (it == id2peer.end()) + { + SALTICIDAE_LOG_WARN("pong message discarded"); + return; + } + Peer *p = it->second; + uint16_t port; + msg.parse_pong(port); + if (check_new_conn(conn, port)) return; + p->pong_msg_ok = true; + if (p->ping_timer_ok) + { + p->reset_ping_timer(); + p->send_ping(); + } +} + +template +void MsgNetwork::init(NetAddr listen_addr) { + listen_port = listen_addr.port; + ConnPool::init(listen_addr); +} + +template +void PeerNetwork::init(NetAddr listen_addr) { + MsgNet::init(listen_addr); + this->reg_handler(MsgType::OPCODE_PING, + std::bind(&PeerNetwork::msg_ping, this, _1, _2)); + this->reg_handler(MsgType::OPCODE_PONG, + std::bind(&PeerNetwork::msg_pong, this, _1, _2)); +} + +template +void PeerNetwork::start_active_conn(const NetAddr &addr) { + Peer *p = id2peer.find(addr)->second; + if (p->connected) return; + auto conn = static_pointer_cast(create_conn(addr)); + assert(p->conn == nullptr); + p->conn = conn; + conn->peer_id = addr; + if (id_mode == IP_BASED) + conn->peer_id.port = 0; +} + +template +void PeerNetwork::add_peer(const NetAddr &addr) { + auto it = id2peer.find(addr); + if (it != id2peer.end()) + throw PeerNetworkError("peer already exists"); + id2peer.insert(std::make_pair(addr, new Peer(addr, nullptr, this, this->eb))); + peer_list.push_back(addr); + start_active_conn(addr); +} + +template +const typename PeerNetwork::conn_t +PeerNetwork::get_peer_conn(const NetAddr &paddr) const { + auto it = id2peer.find(paddr); + if (it == id2peer.end()) + throw PeerNetworkError("peer does not exist"); + return it->second->conn; +} + +template +bool PeerNetwork::has_peer(const NetAddr &paddr) const { + return id2peer.count(paddr); +} + +template +void MsgNetwork::reg_handler(typename MsgType::opcode_t opcode, + msg_callback_t handler) { + handler_map[opcode] = handler; +} + +template +void MsgNetwork::send_msg(const MsgType &msg, conn_t conn) { + bytearray_t msg_data = msg.serialize(); + SALTICIDAE_LOG_DEBUG("wrote message %s to %s", + std::string(msg).c_str(), + std::string(*conn).c_str()); + conn->write(std::move(msg_data)); + conn->nsent++; + sent_by_opcode.add(msg); +} + +template +void PeerNetwork::send_msg(const MsgType &msg, const Peer *peer) { + bytearray_t msg_data = msg.serialize(); + SALTICIDAE_LOG_DEBUG("wrote message %s to %s", + std::string(msg).c_str(), + std::string(peer->addr).c_str()); + if (peer->connected) + { + SALTICIDAE_LOG_DEBUG("wrote to ConnPool"); + peer->conn->write(std::move(msg_data)); + } + else + { + SALTICIDAE_LOG_DEBUG("dropped"); + } + peer->conn->nsent++; + this->sent_by_opcode.add(msg); +} + +template +void PeerNetwork::send_msg(const MsgType &msg, const NetAddr &addr) { + auto it = id2peer.find(addr); + if (it == id2peer.end()) + { + SALTICIDAE_LOG_ERROR("sending to non-existing peer: %s", std::string(addr).c_str()); + throw PeerNetworkError("peer does not exist"); + } + send_msg(msg, it->second); +} + +template +void PeerNetwork::Peer::reset_ping_timer() { + assert(ev_ping_timer); + ev_ping_timer.del(); + ev_ping_timer.add_with_timeout(gen_rand_timeout(pn->ping_period)); +} + +template +void PeerNetwork::reset_conn_timeout(conn_t conn) { + assert(conn->ev_timeout); + conn->ev_timeout.del(); + conn->ev_timeout.add_with_timeout(conn_timeout); +} + +template +void PeerNetwork::Peer::send_ping() { + ping_timer_ok = false; + pong_msg_ok = false; + MsgType ping; + ping.gen_ping(pn->listen_port); + pn->reset_conn_timeout(conn); + pn->send_msg(ping, this); +} + +template +void PeerNetwork::Peer::ping_timer(evutil_socket_t, short) { + ping_timer_ok = true; + if (pong_msg_ok) + { + reset_ping_timer(); + send_ping(); + } +} + +template +const std::vector &PeerNetwork::all_peers() const { + return peer_list; +} + +template +void ClientNetwork::Conn::on_setup() { + assert(this->get_mode() == Conn::PASSIVE); + const auto &addr = this->get_addr(); + cn->addr2conn.erase(addr); + cn->addr2conn.insert( + std::make_pair(addr, + static_pointer_cast(this->self()))); +} + +template +void ClientNetwork::Conn::on_teardown() { + assert(this->get_mode() == Conn::PASSIVE); + cn->addr2conn.erase(this->get_addr()); +} + +template +void ClientNetwork::send_msg(const MsgType &msg, const NetAddr &addr) { + auto it = addr2conn.find(addr); + if (it == addr2conn.end()) return; + MsgNet::send_msg(msg, it->second); +} + +} + +#endif diff --git a/include/salticidae/ref.h b/include/salticidae/ref.h new file mode 100644 index 0000000..5f54da2 --- /dev/null +++ b/include/salticidae/ref.h @@ -0,0 +1,202 @@ +/** + * Copyright (c) 2018 Cornell University. + * + * Author: Ted Yin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _SALTICIDAE_REF_H +#define _SALTICIDAE_REF_H + +#include +#include + +namespace salticidae { + +struct _RCCtl { + size_t ref_cnt; + size_t weak_cnt; + void add_ref() { ref_cnt++; } + void add_weak() { weak_cnt++; } + bool release_ref() { + if (--ref_cnt) return false; + if (weak_cnt) return true; + delete this; + return true; + } + void release_weak() { + if (--weak_cnt) return; + if (ref_cnt) return; + delete this; + } + size_t get_cnt() { return ref_cnt; } + _RCCtl(): ref_cnt(1), weak_cnt(0) {} + ~_RCCtl() {} +}; + +struct _ARCCtl { + std::atomic_size_t ref_cnt; + std::atomic_size_t weak_cnt; + std::atomic_uint8_t dcnt; + void add_ref() { ref_cnt++; } + void add_weak() { weak_cnt++; } + bool release_ref() { + dcnt++; + if (--ref_cnt) { dcnt--; return false; } + if (weak_cnt) { dcnt--; return true; } + if (!--dcnt) delete this; + return true; + } + void release_weak() { + dcnt++; + if (--weak_cnt) { dcnt--; return; } + if (ref_cnt) { dcnt--; return; } + if (!--dcnt) delete this; + } + size_t get_cnt() { return ref_cnt.load(); } + _ARCCtl(): ref_cnt(1), weak_cnt(0), dcnt(0) {} + ~_ARCCtl() {} +}; + +template class RcObjBase; + +template +class WeakObjBase { + R *ctl; + void release() { if (ctl) ctl->release_weak(); } + public: + friend RcObjBase; + friend std::hash>; + WeakObjBase(): ctl(nullptr) {} + WeakObjBase &operator=(const WeakObjBase &other) { + release(); + ctl = other.ctl; + ctl->add_weak(); + return *this; + } + + WeakObjBase(const WeakObjBase &other): ctl(other.ctl) { + ctl->add_weak(); + } + + WeakObjBase(WeakObjBase &&other): ctl(other.ctl) { + other.ctl = nullptr; + } + + WeakObjBase(const RcObjBase &other); + + ~WeakObjBase() { release(); } +}; + +template +class RcObjBase { + T *obj; + R *ctl; + void release() { + if (ctl && ctl->release_ref()) + delete obj; + } + public: + friend WeakObjBase; + friend std::hash>; + template + friend RcObjBase static_pointer_cast(const RcObjBase &other); + template friend class RcObjBase; + + operator T*() const { return obj; } + T *operator->() const { return obj; } + RcObjBase(): obj(nullptr), ctl(nullptr) {} + RcObjBase(T *obj): obj(obj), ctl(new R()) {} + RcObjBase &operator=(const RcObjBase &other) { + release(); + obj = other.obj; + ctl = other.ctl; + ctl->add_ref(); + return *this; + } + + RcObjBase(const RcObjBase &other): + obj(other.obj), ctl(other.ctl) { + ctl->add_ref(); + } + + template + RcObjBase(const RcObjBase &other): + obj(other.obj), ctl(other.ctl) { + ctl->add_ref(); + } + + RcObjBase(RcObjBase &&other): + obj(other.obj), ctl(other.ctl) { + other.ctl = nullptr; + } + + RcObjBase(const WeakObjBase &other) { + if (other.ctl && other.ctl->ref_cnt) + { + obj = other.obj; + ctl = other.ctl; + ctl->add_ref(); + } + else + { + obj = nullptr; + ctl = nullptr; + } + } + + ~RcObjBase() { release(); } + + size_t get_cnt() const { return ctl ? ctl->get_cnt() : 0; } +}; + +template +RcObjBase static_pointer_cast(const RcObjBase &other) { + RcObjBase rc{}; + rc.obj = static_cast(other.obj); + rc.ctl = other.ctl; + rc.ctl->add_ref(); + return std::move(rc); +} + +template +inline WeakObjBase::WeakObjBase(const RcObjBase &other): + ctl(other.ctl) { + ctl->add_weak(); +} + +template using RcObj = RcObjBase; +template using WeakObj = WeakObjBase; + +template using ArcObj = RcObjBase; +template using AweakObj = WeakObjBase; + +} + +namespace std { + template + struct hash> { + size_t operator()(const salticidae::RcObjBase &k) const { + return (size_t)k.obj; + } + }; +} + +#endif diff --git a/include/salticidae/stream.h b/include/salticidae/stream.h new file mode 100644 index 0000000..4259773 --- /dev/null +++ b/include/salticidae/stream.h @@ -0,0 +1,274 @@ +/** + * Copyright (c) 2018 Cornell University. + * + * Author: Ted Yin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _SALTICIDAE_STREAM_H +#define _SALTICIDAE_STREAM_H + +#include "salticidae/type.h" +#include "salticidae/crypto.h" + +namespace salticidae { + +template class Blob; +using uint256_t = Blob<256, uint64_t>; + +class DataStream { + bytearray_t buffer; + size_t offset; + + public: + DataStream(): offset(0) {} + DataStream(const uint8_t *begin, const uint8_t *end): buffer(begin, end), offset(0) {} + DataStream(bytearray_t &&data): buffer(std::move(data)), offset(0) {} + DataStream(const bytearray_t &data): buffer(data), offset(0) {} + + DataStream(DataStream &&other): + buffer(std::move(other.buffer)), + offset(other.offset) {} + + DataStream(const DataStream &other): + buffer(other.buffer), + offset(other.offset) {} + + DataStream &operator=(const DataStream &other) { + buffer = other.buffer; + offset = other.offset; + return *this; + } + + DataStream &operator=(DataStream &&other) { + buffer = std::move(other.buffer); + offset = std::move(other.offset); + return *this; + } + + uint8_t *data() { return &buffer[offset]; } + + void clear() { + buffer.clear(); + offset = 0; + } + + size_t size() const { + return buffer.size() - offset; + } + + template + typename std::enable_if::value, DataStream &>::type + operator<<(T d) { + buffer.resize(buffer.size() + sizeof(T)); + *(reinterpret_cast(&*buffer.end() - sizeof(T))) = d; + return *this; + } + + template + typename std::enable_if::value, DataStream &>::type + operator<<(const T &d) { + buffer.insert(buffer.end(), d.begin(), d.end()); + return *this; + } + + void put_data(uint8_t *begin, uint8_t *end) { + size_t len = end - begin; + buffer.resize(buffer.size() + len); + memmove(&*buffer.end() - len, begin, len); + } + + template + typename std::enable_if::value && + !std::is_integral::value, DataStream &>::type + operator<<(const T &obj) { + obj.serialize(*this); + return *this; + } + + DataStream &operator<<(const char *cstr) { + put_data((uint8_t *)cstr, (uint8_t *)cstr + strlen(cstr)); + return *this; + } + + template + typename std::enable_if::value, DataStream &>::type + operator>>(T &d) { +#ifndef SALTICIDAE_NOCHECK + if (offset >= buffer.size()) + throw std::ios_base::failure("insufficient buffer"); +#endif + d = *(reinterpret_cast(&buffer[offset])); + offset += sizeof(T); + return *this; + } + + template + typename std::enable_if::value, DataStream &>::type + operator>>(T &obj) { + obj.unserialize(*this); + return *this; + } + + std::string get_hex() const { + char buf[3]; + DataStream s; + for (auto it = buffer.begin() + offset; it != buffer.end(); it++) + { + sprintf(buf, "%02x", *it); + s.put_data((uint8_t *)buf, (uint8_t *)buf + 2); + } + return std::string(s.buffer.begin(), s.buffer.end()); + } + + void load_hex(const std::string &hexstr) { + size_t len = hexstr.size(); + const char *p; + uint8_t *bp; + unsigned int tmp; + if (len & 1) + throw std::runtime_error("not a valid hex string"); + buffer.resize(len >> 1); + offset = 0; + for (p = hexstr.data(), bp = &*buffer.begin(); + p < hexstr.data() + len; p += 2, bp++) + { + if (sscanf(p, "%02x", &tmp) != 1) + throw std::runtime_error("not a valid hex string"); + *bp = tmp; + } + } + + operator bytearray_t () const & { + return bytearray_t(buffer.begin() + offset, buffer.end()); + } + + operator bytearray_t () const && { + return std::move(buffer); + } + + operator std::string () const & { + return std::string(buffer.begin() + offset, buffer.end()); + } + + inline uint256_t get_hash() const; +}; + +template +class Blob { + using _impl_type = T; + static const size_t bit_per_datum = sizeof(_impl_type) * 8; + static_assert(!(N % bit_per_datum), "N must be divisible by bit_per_datum"); + static const auto _len = N / bit_per_datum; + _impl_type data[_len]; + bool loaded; + + public: + + Blob(): loaded(false) {} + Blob(const bytearray_t &arr) { + if (arr.size() != N / 8) + throw std::runtime_error("incorrect Blob size"); + load(&*arr.begin()); + } + + Blob(const uint8_t *arr) { load(arr); } + + void load(const uint8_t *arr) { + arr += N / 8; + for (_impl_type *ptr = data + _len; ptr > data;) + { + _impl_type x = 0; + for (unsigned j = 0; j < sizeof(_impl_type); j++) + x = (x << 8) | *(--arr); + *(--ptr) = x; + } + loaded = true; + } + + bool is_null() const { return !loaded; } + + bool operator==(const Blob &other) const { + for (size_t i = 0; i < _len; i++) + if (data[i] != other.data[i]) + return false; + return true; + } + + bool operator!=(const Blob &other) const { + return !(data == other); + } + + size_t cheap_hash() const { return *data; } + + void serialize(DataStream &s) const { + for (const _impl_type *ptr = data; ptr < data + _len; ptr++) + s << htole(*ptr); + } + + void unserialize(DataStream &s) { + for (_impl_type *ptr = data; ptr < data + _len; ptr++) + { + _impl_type x; + s >> x; + *ptr = letoh(x); + } + } +}; + +const size_t ENT_HASH_LENGTH = 256 / 8; + +uint256_t DataStream::get_hash() const { + class SHA256 d; + d.update(buffer.begin() + offset, size()); + return d.digest(); +} + +template inline uint256_t get_hash(const T &x) { + DataStream s; + s << x; + return s.get_hash(); +} + +template inline std::string get_hex(const T &x) { + DataStream s; + s << x; + return s.get_hex(); +} + +} + +namespace std { + template <> + struct hash { + size_t operator()(const salticidae::uint256_t &k) const { + return (size_t)k.cheap_hash(); + } + }; + + template <> + struct hash { + size_t operator()(const salticidae::uint256_t &k) const { + return (size_t)k.cheap_hash(); + } + }; +} + +#endif diff --git a/include/salticidae/type.h b/include/salticidae/type.h new file mode 100644 index 0000000..edf38a6 --- /dev/null +++ b/include/salticidae/type.h @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2018 Cornell University. + * + * Author: Ted Yin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _SALTICIDAE_TYPE_H +#define _SALTICIDAE_TYPE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace salticidae { + +const auto _1 = std::placeholders::_1; +const auto _2 = std::placeholders::_2; + +using bytearray_t = std::vector; +using mutex_lg_t = std::lock_guard; +using mutex_ul_t = std::unique_lock; + +template T htole(T) = delete; +template<> inline uint16_t htole(uint16_t x) { return htole16(x); } +template<> inline uint32_t htole(uint32_t x) { return htole32(x); } +template<> inline uint64_t htole(uint64_t x) { return htole64(x); } + +template T letoh(T) = delete; +template<> inline uint16_t letoh(uint16_t x) { return le16toh(x); } +template<> inline uint32_t letoh(uint32_t x) { return le32toh(x); } +template<> inline uint64_t letoh(uint64_t x) { return le64toh(x); } + +template +struct is_ranged : std::false_type {}; + +template +struct is_ranged().begin()), + decltype(std::declval().end())>> : std::true_type {}; + +} + +#endif diff --git a/include/salticidae/util.h b/include/salticidae/util.h new file mode 100644 index 0000000..b3eb991 --- /dev/null +++ b/include/salticidae/util.h @@ -0,0 +1,292 @@ +/** + * Copyright (c) 2018 Cornell University. + * + * Author: Ted Yin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PART