/**
* Copyright (c) 2019 Ava Labs, Inc.
*
* Author: Ted Yin <tederminant@gmail.com>
*
* 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.
*/
#include <cstdio>
#include <cstdint>
#include <string>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <unistd.h>
#include <signal.h>
#include "salticidae/msg.h"
#include "salticidae/event.h"
#include "salticidae/network.h"
#include "salticidae/stream.h"
using salticidae::NetAddr;
using salticidae::DataStream;
using salticidae::htole;
using salticidae::letoh;
using salticidae::EventContext;
using salticidae::ThreadCall;
using salticidae::PKey;
using std::placeholders::_1;
using std::placeholders::_2;
using PeerNetwork = salticidae::PeerNetwork<uint8_t>;
struct MsgText {
static const uint8_t opcode = 0x0;
DataStream serialized;
uint64_t id;
std::string text;
MsgText(uint64_t id, const std::string &text) {
serialized << salticidae::htole(id) << salticidae::htole((uint32_t)text.length()) << text;
}
MsgText(DataStream &&s) {
uint32_t len;
s >> id;
id = salticidae::letoh(id);
s >> len;
len = salticidae::letoh(len);
text = std::string((const char *)s.get_data_inplace(len), len);
}
};
const uint8_t MsgText::opcode;
void masksigs() {
sigset_t mask;
sigemptyset(&mask);
sigfillset(&mask);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
}
std::unordered_set<uint256_t> valid_certs;
struct Net {
uint64_t id;
EventContext ec;
ThreadCall tc;
std::thread th;
PeerNetwork *net;
const std::string listen_addr;
uint256_t cert;
Net(uint64_t id, uint16_t port): id(id), tc(ec), listen_addr("127.0.0.1:"+ std::to_string(port)) {
auto tls_key = new PKey(PKey::create_privkey_rsa(2048));
auto tls_cert = new salticidae::X509(salticidae::X509::create_self_signed_from_pubkey(*tls_key));
tls_key->save_privkey_to_file(std::to_string(port) + "_pkey.pem");
tls_cert->save_to_file(std::to_string(port) + ".pem");
cert = salticidae::get_hash(tls_cert->get_der());
valid_certs.insert(cert);
net = new PeerNetwork(ec, PeerNetwork::Config(salticidae::ConnPool::Config()
.enable_tls(true)
.tls_key(tls_key)
.tls_cert(tls_cert)
).conn_timeout(5)
.ping_period(2)
.id_mode(PeerNetwork::IdentityMode::ADDR_BASED));
net->reg_handler([this](const MsgText &msg, const PeerNetwork::conn_t &) {
fprintf(stdout, "net %lu: peer %lu says %s\n", this->id, msg.id, msg.text.c_str());
});
net->reg_conn_handler([this](const salticidae::ConnPool::conn_t &conn, bool connected) {
if (connected)
{
auto cert_hash = salticidae::get_hash(conn->get_peer_cert()->get_der());
return valid_certs.count(cert_hash) > 0;
}
return true;
});
net->reg_error_handler([this](const std::exception_ptr _err, bool fatal, int32_t async_id) {
try {
std::rethrow_exception(_err);
} catch (const std::exception &err) {
fprintf(stdout, "net %lu: captured %s error during an async call %d: %s\n",
this->id, fatal ? "fatal" : "recoverable", async_id, err.what());
}
});
net->reg_peer_handler([this](const PeerNetwork::conn_t &conn, bool connected) {
fprintf(stdout, "net %lu: %s peer %s (cert: %s)\n", this->id,
connected ? "connected to" : "disconnected from",
std::string(conn->get_peer_addr()).c_str(),
salticidae::get_hash(conn->get_peer_cert()->get_der()).to_hex().c_str());
});
net->reg_unknown_peer_handler([this](const NetAddr &claimed_addr, const salticidae::X509 *) {
fprintf(stdout, "net %lu: unknown peer %s attempts to connnect\n",
this->id, std::string(claimed_addr).c_str());
});
th = std::thread([=](){
masksigs();
try {
net->start();
net->listen(NetAddr(listen_addr));
fprintf(stdout, "net %lu: listen to %s\n", id, listen_addr.c_str());
ec.dispatch();
}