qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
toxoptions.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "toxoptions.h"
21 
22 #include "src/core/icoresettings.h"
23 #include "src/core/toxlogger.h"
24 
25 #include <tox/tox.h>
26 
27 #include <QByteArray>
28 #include <QDebug>
29 
36 ToxOptions::ToxOptions(Tox_Options* options, const QByteArray& proxyAddrData)
37  : options(options)
38  , proxyAddrData(proxyAddrData)
39 {}
40 
42 {
43  tox_options_free(options);
44 }
45 
47 {
48  options = from.options;
49  proxyAddrData.swap(from.proxyAddrData);
50  from.options = nullptr;
51  from.proxyAddrData.clear();
52 }
53 
54 const char* ToxOptions::getProxyAddrData() const
55 {
56  return proxyAddrData.constData();
57 }
58 
59 ToxOptions::operator Tox_Options*()
60 {
61  return options;
62 }
63 
69 std::unique_ptr<ToxOptions> ToxOptions::makeToxOptions(const QByteArray& savedata,
70  const ICoreSettings& s)
71 {
72  Tox_Options* tox_opts = tox_options_new(nullptr);
73 
74  if (!tox_opts) {
75  qWarning() << "Failed to create Tox_Options";
76  return {};
77  }
78 
79  // need to init proxyAddr here, because we need it to construct ToxOptions
80  const QString proxyAddr = s.getProxyAddr();
81 
82  auto toxOptions = std::unique_ptr<ToxOptions>(new ToxOptions(tox_opts, proxyAddr.toUtf8()));
83  // register log first, to get messages as early as possible
84  tox_options_set_log_callback(*toxOptions, ToxLogger::onLogMessage);
85 
86  // savedata
87  tox_options_set_savedata_type(*toxOptions, savedata.isNull() ? TOX_SAVEDATA_TYPE_NONE
88  : TOX_SAVEDATA_TYPE_TOX_SAVE);
89  tox_options_set_savedata_data(*toxOptions, reinterpret_cast<const uint8_t*>(savedata.data()),
90  savedata.size());
91 
92  // IPv6 needed for LAN discovery, but can crash some weird routers. On by default, can be
93  // disabled in options.
94  const bool enableIPv6 = s.getEnableIPv6();
95  bool forceTCP = s.getForceTCP();
96  // LAN requiring UDP is a toxcore limitation, ideally wouldn't be related
97  const bool enableLanDiscovery = s.getEnableLanDiscovery() && !forceTCP;
98  ICoreSettings::ProxyType proxyType = s.getProxyType();
99  quint16 proxyPort = s.getProxyPort();
100 
101  if (!enableLanDiscovery) {
102  qWarning() << "Core starting without LAN discovery. Peers can only be found through DHT.";
103  }
104  if (enableIPv6) {
105  qDebug() << "Core starting with IPv6 enabled";
106  } else if (enableLanDiscovery) {
107  qWarning() << "Core starting with IPv6 disabled. LAN discovery may not work properly.";
108  }
109 
110  // No proxy by default
111  tox_options_set_proxy_type(*toxOptions, TOX_PROXY_TYPE_NONE);
112  tox_options_set_proxy_host(*toxOptions, nullptr);
113  tox_options_set_proxy_port(*toxOptions, 0);
114 
115  if (proxyType != ICoreSettings::ProxyType::ptNone) {
116  if (static_cast<uint32_t>(proxyAddr.length()) > tox_max_hostname_length()) {
117  qWarning() << "Proxy address" << proxyAddr << "is too long";
118  } else if (!proxyAddr.isEmpty() && proxyPort > 0) {
119  qDebug() << "Using proxy" << proxyAddr << ":" << proxyPort;
120  // protection against changings in Tox_Proxy_Type enum
121  if (proxyType == ICoreSettings::ProxyType::ptSOCKS5) {
122  tox_options_set_proxy_type(*toxOptions, TOX_PROXY_TYPE_SOCKS5);
123  } else if (proxyType == ICoreSettings::ProxyType::ptHTTP) {
124  tox_options_set_proxy_type(*toxOptions, TOX_PROXY_TYPE_HTTP);
125  }
126 
127  tox_options_set_proxy_host(*toxOptions, toxOptions->getProxyAddrData());
128  tox_options_set_proxy_port(*toxOptions, proxyPort);
129 
130  if (!forceTCP) {
131  qDebug() << "Proxy and UDP enabled. This is a security risk. Forcing TCP only.";
132  forceTCP = true;
133  }
134  }
135  }
136 
137  // network options
138  tox_options_set_udp_enabled(*toxOptions, !forceTCP);
139  tox_options_set_ipv6_enabled(*toxOptions, enableIPv6);
140  tox_options_set_local_discovery_enabled(*toxOptions, enableLanDiscovery);
141  tox_options_set_start_port(*toxOptions, 0);
142  tox_options_set_end_port(*toxOptions, 0);
143 
144  return toxOptions;
145 }
146 
148 {
149  return tox_options_get_ipv6_enabled(options);
150 }
151 
152 void ToxOptions::setIPv6Enabled(bool enabled)
153 {
154  tox_options_set_ipv6_enabled(options, enabled);
155 }
ToxOptions::ToxOptions
ToxOptions(ToxOptions &&from)
Definition: toxoptions.cpp:46
ICoreSettings::getEnableLanDiscovery
virtual bool getEnableLanDiscovery() const =0
ICoreSettings::ProxyType
ProxyType
Definition: icoresettings.h:30
ToxOptions
Definition: toxoptions.h:29
icoresettings.h
ToxOptions::~ToxOptions
~ToxOptions()
Definition: toxoptions.cpp:41
ToxOptions::makeToxOptions
static std::unique_ptr< ToxOptions > makeToxOptions(const QByteArray &savedata, const ICoreSettings &s)
Initializes a ToxOptions instance.
Definition: toxoptions.cpp:69
ICoreSettings::getEnableIPv6
virtual bool getEnableIPv6() const =0
ICoreSettings::getForceTCP
virtual bool getForceTCP() const =0
ICoreSettings::getProxyType
virtual ProxyType getProxyType() const =0
toxoptions.h
ToxOptions::getIPv6Enabled
bool getIPv6Enabled() const
Definition: toxoptions.cpp:147
ToxOptions::proxyAddrData
QByteArray proxyAddrData
Definition: toxoptions.h:46
ToxOptions::getProxyAddrData
const char * getProxyAddrData() const
Definition: toxoptions.cpp:54
ICoreSettings::getProxyAddr
virtual QString getProxyAddr() const =0
ICoreSettings::ProxyType::ptHTTP
@ ptHTTP
ICoreSettings::ProxyType::ptNone
@ ptNone
ICoreSettings::ProxyType::ptSOCKS5
@ ptSOCKS5
ToxOptions::setIPv6Enabled
void setIPv6Enabled(bool enabled)
Definition: toxoptions.cpp:152
ToxOptions::options
Tox_Options * options
Definition: toxoptions.h:45
ICoreSettings
Definition: icoresettings.h:28
ToxLogger::onLogMessage
void onLogMessage(Tox *tox, Tox_Log_Level level, const char *file, uint32_t line, const char *func, const char *message, void *user_data)
Log message handler for toxcore log messages.
Definition: toxlogger.cpp:47
ICoreSettings::getProxyPort
virtual quint16 getProxyPort() const =0
toxlogger.h