Fixed compile errors

This commit is contained in:
WolverinDEV 2020-02-02 21:54:37 +01:00
parent 3dcfad1038
commit 25173d5745
7 changed files with 464 additions and 417 deletions

View File

@ -15,8 +15,6 @@
#define MSG_DONTWAIT (0)
#else
#include <unistd.h>
#include <unbound.h>
#include <unbound-event.h>
#include <sys/socket.h>
#endif

View File

@ -58,6 +58,9 @@ void ProtocolHandler::reset() {
buffer.reset();
}
this->crypt_setupped = false;
for(auto& calculator : this->incoming_generation_estimators)
calculator.reset();
this->_packet_id_manager.reset();
this->crypt_handler.reset();
@ -174,8 +177,9 @@ void ProtocolHandler::progress_packet(const pipes::buffer_view &buffer) {
}
auto& read_queue = this->_packet_buffers[packet_type.type()];
packet->generationId(read_queue.generation(packet_id));
auto& gen_calc = this->incoming_generation_estimators[packet_type.type()];
packet->generationId(gen_calc.visit_packet(packet_id));
auto gen = packet->generationId();
if(ordered) {
unique_lock queue_lock(read_queue.buffer_lock);
auto result = read_queue.accept_index(packet_id);
@ -197,18 +201,40 @@ void ProtocolHandler::progress_packet(const pipes::buffer_view &buffer) {
}
//NOTICE I found out that the Compressed flag is set if the packet contains an audio header
string error = "success";
if(!this->crypt_handler.progressPacketIn(packet.get(), error, false)){
if(!this->crypt_handler.use_default()) {
if(!this->crypt_handler.progressPacketIn(packet.get(), error, true)){
log_error(category::connection, tr("Failed to decrypt packet ({}), even with default key: {}"), packet_type.name(), error);
if(packet->isEncrypted()) {
std::string error;
ts::connection::CryptHandler::key_t crypt_key{};
ts::connection::CryptHandler::nonce_t crypt_nonce{};
bool decrypt_result;
if(!this->crypt_setupped) {
crypt_key = ts::connection::CryptHandler::default_key;
crypt_nonce = ts::connection::CryptHandler::default_nonce;
} else {
if(!this->crypt_handler.generate_key_nonce(false, packet_type.type(), packet->packetId(), packet->generationId(), crypt_key, crypt_nonce)) {
log_error(category::connection, tr("Failed to generate crypt key/nonce. This should never happen! Dropping packet."));
return;
} else {
log_error(category::connection, tr("Successfully decrypt packet ({} | {}) with default key."), packet_type.name(), packet_id);
//FIXME Test if we're in init high
}
} else {
log_error(category::connection, tr("Failed to decrypt packet ({}) with default key: {}"), packet_type.name(), error);
}
auto mac_ptr = packet->mac().data_ptr<void>();
auto header_ptr = packet->header().data_ptr<void>();
auto data_ptr = packet->data().data_ptr<void>();
decrypt_result = this->crypt_handler.decrypt(
header_ptr, packet->header_length(),
data_ptr, packet->data_length(),
mac_ptr,
crypt_key, crypt_nonce,
error
);
if(!decrypt_result) {
if(!this->crypt_setupped)
log_error(category::connection, tr("Failed to decrypt packet ({}), with default key."), packet_type.name());
else
log_trace(category::connection, tr("Failed to decrypt packet {}."), packet_type.name());
return;
}
}
@ -452,10 +478,31 @@ bool ProtocolHandler::create_datagram_packets(std::vector<pipes::buffer> &result
}
//log_trace(category::connection, tr("Packet {} got packet id {}"), packet->type().name(), packet->packetId());
}
if(!this->crypt_handler.progressPacketOut(packet.get(), error, false)) {
if(packet->has_flag(PacketFlag::Unencrypted)) {
this->crypt_handler.write_default_mac(packet->mac().data_ptr());
} else {
ts::connection::CryptHandler::key_t crypt_key{};
ts::connection::CryptHandler::nonce_t crypt_nonce{};
if(!this->crypt_setupped) {
crypt_key = ts::connection::CryptHandler::default_key;
crypt_nonce = ts::connection::CryptHandler::default_nonce;
} else {
if(!this->crypt_handler.generate_key_nonce(true, packet->type().type(), packet->packetId(), packet->generationId(), crypt_key, crypt_nonce)) {
log_error(category::connection, tr("Failed to generate crypt key/nonce. Dropping packet"), error);
return false;
}
}
auto crypt_result = this->crypt_handler.encrypt(packet->header().data_ptr(), packet->header().length(),
packet->data().data_ptr(), packet->data().length(),
packet->mac().data_ptr(),
crypt_key, crypt_nonce, error);
if(!crypt_result){
log_error(category::connection, tr("Failed to encrypt packet: {}"), error);
return false;
}
}
/*
#ifndef CONNECTION_NO_STATISTICS

View File

@ -10,9 +10,10 @@
#include <protocol/ringbuffer.h>
#include <protocol/Packet.h>
#include <protocol/CryptionHandler.h>
#include <protocol/CryptHandler.h>
#include <protocol/CompressionHandler.h>
#include <protocol/AcknowledgeManager.h>
#include <protocol/generation.h>
#include "ServerConnection.h"
namespace ts {
@ -124,9 +125,11 @@ namespace tc {
uint16_t client_id = 0;
ts::protocol::PacketIdManager _packet_id_manager;
packet_buffers_t _packet_buffers;
std::array<ts::protocol::generation_estimator, 9> incoming_generation_estimators{}; /* implementation is thread save */
uint8_t _packet_buffers_index = 0;
ts::connection::CryptionHandler crypt_handler;
bool crypt_setupped{false};
ts::connection::CryptHandler crypt_handler;
ts::connection::CompressionHandler compression_handler;
ts::connection::AcknowledgeManager acknowledge_handler;

View File

@ -1,8 +1,6 @@
#include "ProtocolHandler.h"
#include "ServerConnection.h"
#include "../logger.h"
#include <protocol/buffers.h>
#include <thread>
using namespace std;

View File

@ -86,6 +86,7 @@ void ProtocolHandler::handleCommandInitIVExpend(ts::Command &cmd) {
log_error(category::connection, tr("Failed to setup crypto ({})"), error);
return;
}
this->crypt_setupped = true;
if(this->server_type == server_type::UNKNOWN) {
if(cmd[0].has("teaspeak") && cmd["teaspeak"].as<bool>()) {
@ -201,6 +202,7 @@ void ProtocolHandler::handleCommandInitIVExpend2(ts::Command &cmd) {
this->send_command(response, [&](bool success){
if(success) {
/* trigger connected; because the connection has been established on protocol layer */
this->crypt_setupped = true;
this->handle->call_connect_result.call(0, true);
this->connection_state = connection_state::CONNECTING;
}

View File

@ -6,7 +6,6 @@
#include <iostream>
#include <tomcrypt.h>
#include <tommath.h>
#include <misc/base64.h>
#include <misc/endianness.h>
#include <protocol/Packet.h>
#include "audio/VoiceConnection.h"
@ -22,11 +21,11 @@ using namespace ts;
void ProtocolHandler::handlePacketAck(const std::shared_ptr<ts::protocol::ServerPacket> &ack) {
string error;
log_trace(category::connection, tr("Handle packet acknowledge for {}"), be2le16(&ack->data()[0]));
if(!this->acknowledge_handler.process_acknowledge(*ack, error)) { }
this->acknowledge_handler.process_acknowledge(ack->type().type(), ack->data(), error);
}
void ProtocolHandler::handlePacketCommand(const std::shared_ptr<ts::protocol::ServerPacket> &packet) {
//cout << "Received command: " << packet->data().string() << endl;
cout << "Received command: " << packet->data().string() << endl;
std::unique_ptr<Command> command;
try {

View File

@ -141,7 +141,7 @@ connection.callback_disconnect = reason => {
const do_connect = () => {
connection.connect({
timeout: 5000,
remote_port: 9988,
remote_port: 9987,
//remote_host: "188.40.240.20", /* twerion */
remote_host: "localhost",
//remote_host: "ts.teaspeak.de",