You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
4.2 KiB
233 lines
4.2 KiB
|
7 years ago
|
#include "../types.h"
|
||
|
|
|
||
|
7 years ago
|
using namespace dvlnet;
|
||
|
|
|
||
|
7 years ago
|
static constexpr bool disable_encryption = false;
|
||
|
|
|
||
|
7 years ago
|
const buffer_t& packet::data()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted || !have_encrypted)
|
||
|
|
ABORT();
|
||
|
|
return encrypted_buffer;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
packet_type packet::type()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
return m_type;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
plr_t packet::src()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
return m_src;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
plr_t packet::dest()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
return m_dest;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
const buffer_t &packet::message()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (m_type != PT_MESSAGE)
|
||
|
7 years ago
|
throw packet_exception();
|
||
|
7 years ago
|
return m_message;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
turn_t packet::turn()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (m_type != PT_TURN)
|
||
|
7 years ago
|
throw packet_exception();
|
||
|
7 years ago
|
return m_turn;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
cookie_t packet::cookie()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (m_type != PT_JOIN_REQUEST && m_type != PT_JOIN_ACCEPT)
|
||
|
7 years ago
|
throw packet_exception();
|
||
|
7 years ago
|
return m_cookie;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
plr_t packet::newplr()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (m_type != PT_JOIN_ACCEPT)
|
||
|
7 years ago
|
throw packet_exception();
|
||
|
7 years ago
|
return m_newplr;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
plr_t packet::oldplr()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (m_type != PT_LEAVE_GAME)
|
||
|
7 years ago
|
throw packet_exception();
|
||
|
7 years ago
|
return m_oldplr;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
const buffer_t &packet::info()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (m_type != PT_JOIN_ACCEPT)
|
||
|
7 years ago
|
throw packet_exception();
|
||
|
7 years ago
|
return m_info;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
void packet_in::create(buffer_t buf)
|
||
|
7 years ago
|
{
|
||
|
|
if (have_encrypted || have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
encrypted_buffer = std::move(buf);
|
||
|
|
have_encrypted = true;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
void packet_in::decrypt()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_encrypted)
|
||
|
|
ABORT();
|
||
|
|
if (have_decrypted)
|
||
|
|
return;
|
||
|
|
if (!disable_encryption) {
|
||
|
|
if (encrypted_buffer.size() < crypto_secretbox_NONCEBYTES +
|
||
|
|
crypto_secretbox_MACBYTES + sizeof(packet_type) + 2 * sizeof(plr_t))
|
||
|
7 years ago
|
throw packet_exception();
|
||
|
7 years ago
|
auto pktlen = encrypted_buffer.size() - crypto_secretbox_NONCEBYTES - crypto_secretbox_MACBYTES;
|
||
|
|
decrypted_buffer.resize(pktlen);
|
||
|
|
if (crypto_secretbox_open_easy(decrypted_buffer.data(),
|
||
|
|
encrypted_buffer.data() + crypto_secretbox_NONCEBYTES,
|
||
|
|
encrypted_buffer.size() - crypto_secretbox_NONCEBYTES,
|
||
|
|
encrypted_buffer.data(),
|
||
|
|
key.data()))
|
||
|
7 years ago
|
throw packet_exception();
|
||
|
7 years ago
|
} else {
|
||
|
|
if (encrypted_buffer.size() < sizeof(packet_type) + 2 * sizeof(plr_t))
|
||
|
7 years ago
|
throw packet_exception();
|
||
|
7 years ago
|
decrypted_buffer = encrypted_buffer;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
process_data();
|
||
|
7 years ago
|
|
||
|
|
have_decrypted = true;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
void packet_out::create(packet_type t,
|
||
|
|
plr_t s,
|
||
|
|
plr_t d,
|
||
|
|
buffer_t m)
|
||
|
7 years ago
|
{
|
||
|
|
if (have_encrypted || have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (t != PT_MESSAGE)
|
||
|
|
ABORT();
|
||
|
|
have_decrypted = true;
|
||
|
|
m_type = t;
|
||
|
|
m_src = s;
|
||
|
|
m_dest = d;
|
||
|
|
m_message = std::move(m);
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
void packet_out::create(packet_type t,
|
||
|
|
plr_t s,
|
||
|
|
plr_t d,
|
||
|
|
turn_t u)
|
||
|
7 years ago
|
{
|
||
|
|
if (have_encrypted || have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (t != PT_TURN)
|
||
|
|
ABORT();
|
||
|
|
have_decrypted = true;
|
||
|
|
m_type = t;
|
||
|
|
m_src = s;
|
||
|
|
m_dest = d;
|
||
|
|
m_turn = u;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
void packet_out::create(packet_type t,
|
||
|
|
plr_t s,
|
||
|
|
plr_t d,
|
||
|
|
cookie_t c)
|
||
|
7 years ago
|
{
|
||
|
|
if (have_encrypted || have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (t != PT_JOIN_REQUEST)
|
||
|
|
ABORT();
|
||
|
|
have_decrypted = true;
|
||
|
|
m_type = t;
|
||
|
|
m_src = s;
|
||
|
|
m_dest = d;
|
||
|
|
m_cookie = c;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
void packet_out::create(packet_type t,
|
||
|
|
plr_t s,
|
||
|
|
plr_t d,
|
||
|
|
cookie_t c,
|
||
|
|
plr_t n,
|
||
|
|
buffer_t i)
|
||
|
7 years ago
|
{
|
||
|
|
if (have_encrypted || have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (t != PT_JOIN_ACCEPT)
|
||
|
|
ABORT();
|
||
|
|
have_decrypted = true;
|
||
|
|
m_type = t;
|
||
|
|
m_src = s;
|
||
|
|
m_dest = d;
|
||
|
|
m_cookie = c;
|
||
|
|
m_newplr = n;
|
||
|
|
m_info = i;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
void packet_out::create(packet_type t,
|
||
|
|
plr_t s,
|
||
|
|
plr_t d,
|
||
|
|
plr_t o)
|
||
|
7 years ago
|
{
|
||
|
|
if (have_encrypted || have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (t != PT_LEAVE_GAME)
|
||
|
|
ABORT();
|
||
|
|
have_decrypted = true;
|
||
|
|
m_type = t;
|
||
|
|
m_src = s;
|
||
|
|
m_dest = d;
|
||
|
|
m_oldplr = o;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
void packet_out::encrypt()
|
||
|
7 years ago
|
{
|
||
|
|
if (!have_decrypted)
|
||
|
|
ABORT();
|
||
|
|
if (have_encrypted)
|
||
|
|
return;
|
||
|
|
|
||
|
7 years ago
|
process_data();
|
||
|
7 years ago
|
|
||
|
|
if (!disable_encryption) {
|
||
|
|
auto len_cleartext = encrypted_buffer.size();
|
||
|
|
encrypted_buffer.insert(encrypted_buffer.begin(), crypto_secretbox_NONCEBYTES, 0);
|
||
|
|
encrypted_buffer.insert(encrypted_buffer.end(), crypto_secretbox_MACBYTES, 0);
|
||
|
|
randombytes_buf(encrypted_buffer.data(), crypto_secretbox_NONCEBYTES);
|
||
|
|
if (crypto_secretbox_easy(encrypted_buffer.data() + crypto_secretbox_NONCEBYTES,
|
||
|
|
encrypted_buffer.data() + crypto_secretbox_NONCEBYTES,
|
||
|
|
len_cleartext,
|
||
|
|
encrypted_buffer.data(),
|
||
|
|
key.data()))
|
||
|
|
ABORT();
|
||
|
|
}
|
||
|
|
have_encrypted = true;
|
||
|
|
}
|