1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

fix(toxoptions): handle nullptr in parameters

Use the c-toxcore default settings in case we don't have own settings.
This commit is contained in:
sudden6 2018-10-31 16:28:25 +01:00
parent c6a197feca
commit 6cce0dae83
No known key found for this signature in database
GPG Key ID: 279509B499E032B9

View File

@ -3,12 +3,11 @@
#include "src/core/icoresettings.h"
#include "src/core/toxlogger.h"
#include <tox/tox.h>
#include <QByteArray>
#include <QDebug>
// TODO(sudden6): replace this constant with the function from toxcore 0.2.3
static const int MAX_PROXY_ADDRESS_LENGTH = 255;
/**
* @brief The ToxOptions class wraps the Tox_Options struct and the matching
* proxy address data. This is needed to ensure both have equal lifetime and
@ -51,6 +50,31 @@ ToxOptions::operator Tox_Options*()
std::unique_ptr<ToxOptions> ToxOptions::makeToxOptions(const QByteArray& savedata,
const ICoreSettings* s)
{
Tox_Options* tox_opts = tox_options_new(nullptr);
if (!tox_opts) {
qWarning() << "failed to create Tox_Options";
return {};
}
// need to init proxyAddr here, because we need it to construct ToxOptions
const QString proxyAddr = s == nullptr ? QString{} : s->getProxyAddr();
auto toxOptions = std::unique_ptr<ToxOptions>(new ToxOptions(tox_opts, proxyAddr.toUtf8()));
// register log first, to get messages as early as possible
tox_options_set_log_callback(*toxOptions, ToxLogger::onLogMessage);
// savedata
tox_options_set_savedata_type(*toxOptions, savedata.isNull() ? TOX_SAVEDATA_TYPE_NONE
: TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(*toxOptions, reinterpret_cast<const uint8_t*>(savedata.data()),
savedata.size());
if(s == nullptr) {
qDebug() << "Using Tox default settings";
return toxOptions;
}
// IPv6 needed for LAN discovery, but can crash some weird routers. On by default, can be
// disabled in options.
const bool enableIPv6 = s->getEnableIPv6();
@ -59,7 +83,6 @@ std::unique_ptr<ToxOptions> ToxOptions::makeToxOptions(const QByteArray& savedat
const bool enableLanDiscovery = s->getEnableLanDiscovery() && !forceTCP;
ICoreSettings::ProxyType proxyType = s->getProxyType();
quint16 proxyPort = s->getProxyPort();
QString proxyAddr = s->getProxyAddr();
if (!enableLanDiscovery) {
qWarning() << "Core starting without LAN discovery. Peers can only be found through DHT.";
@ -70,28 +93,13 @@ std::unique_ptr<ToxOptions> ToxOptions::makeToxOptions(const QByteArray& savedat
qWarning() << "Core starting with IPv6 disabled. LAN discovery may not work properly.";
}
Tox_Options* tox_opts = tox_options_new(nullptr);
if (!tox_opts) {
return {};
}
auto toxOptions = std::unique_ptr<ToxOptions>(new ToxOptions(tox_opts, proxyAddr.toUtf8()));
// register log first, to get messages as early as possible
tox_options_set_log_callback(*toxOptions, ToxLogger::onLogMessage);
// savedata
tox_options_set_savedata_type(*toxOptions, !savedata.isNull() ? TOX_SAVEDATA_TYPE_TOX_SAVE
: TOX_SAVEDATA_TYPE_NONE);
tox_options_set_savedata_data(*toxOptions, reinterpret_cast<const uint8_t*>(savedata.data()),
savedata.size());
// No proxy by default
tox_options_set_proxy_type(*toxOptions, TOX_PROXY_TYPE_NONE);
tox_options_set_proxy_host(*toxOptions, nullptr);
tox_options_set_proxy_port(*toxOptions, 0);
if (proxyType != ICoreSettings::ProxyType::ptNone) {
if (proxyAddr.length() > MAX_PROXY_ADDRESS_LENGTH) {
if (static_cast<uint32_t>(proxyAddr.length()) > tox_max_hostname_length()) {
qWarning() << "proxy address" << proxyAddr << "is too long";
} else if (!proxyAddr.isEmpty() && proxyPort > 0) {
qDebug() << "using proxy" << proxyAddr << ":" << proxyPort;