Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
code
Browse files Browse the repository at this point in the history
  • Loading branch information
depler committed Nov 27, 2022
1 parent e7b3512 commit 3df1e01
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 65 deletions.
1 change: 1 addition & 0 deletions app/TransmissionAndroid.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Global
{A0BCE607-F35D-49B2-9249-F418BAA14EAC}.Release|ARM.Build.0 = Release|Any CPU
{A0BCE607-F35D-49B2-9249-F418BAA14EAC}.Release|ARM.Deploy.0 = Release|Any CPU
{EA8CA5B8-7658-491F-970F-61DF8F5582F6}.Debug|ARM.ActiveCfg = Debug|ARM
{EA8CA5B8-7658-491F-970F-61DF8F5582F6}.Debug|ARM.Build.0 = Debug|ARM
{EA8CA5B8-7658-491F-970F-61DF8F5582F6}.Release|ARM.ActiveCfg = Release|ARM
{EA8CA5B8-7658-491F-970F-61DF8F5582F6}.Release|ARM.Build.0 = Release|ARM
EndGlobalSection
Expand Down
8 changes: 6 additions & 2 deletions lib/libtransmission/announcer-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ struct tr_announce_request
/* the port we listen for incoming peers on */
tr_port port;

/* per-session key */
int key = 0;
/* Per-session key. BEP 0015 defines this key as a 32-bit number and
* explains its purpose: "Clients that resolve hostnames to v4 and v6
* and then announce to both should use the same key for both so that
* trackers that care about accurate statistics-keeping can match the
* two announces." */
uint32_t key;

/* the number of peers we'd like to get back in the response */
int numwant = 0;
Expand Down
6 changes: 2 additions & 4 deletions lib/libtransmission/announcer-udp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include "announcer.h"
#include "announcer-common.h"
#include "crypto-utils.h" /* tr_rand_buffer() */
#include "crypto-utils.h" // for tr_rand_obj()
#include "log.h"
#include "peer-io.h"
#include "peer-mgr.h" // for tr_pex::fromCompact4()
Expand All @@ -52,9 +52,7 @@ static auto constexpr TauConnectionTtlSecs = int{ 60 };

static tau_transaction_t tau_transaction_new()
{
auto tmp = tau_transaction_t{};
tr_rand_buffer(&tmp, sizeof(tau_transaction_t));
return tmp;
return tr_rand_obj<tau_transaction_t>();
}

// used in the "action" field of a request. Values defined in bep 15.
Expand Down
3 changes: 1 addition & 2 deletions lib/libtransmission/announcer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <algorithm>
#include <array>
#include <cinttypes> // PRIu64
#include <climits> // INT_MAX
#include <cstdio>
#include <ctime>
Expand Down Expand Up @@ -213,7 +212,7 @@ class tr_announcer_impl final : public tr_announcer

tr_session* const session;

int const key = tr_rand_int(INT_MAX);
uint32_t const key = tr_rand_obj<uint32_t>();

private:
void flushCloseMessages()
Expand Down
3 changes: 1 addition & 2 deletions lib/libtransmission/crypto-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ std::string tr_ssha1(std::string_view plaintext)
auto constexpr Salter = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"sv;
static_assert(std::size(Salter) == 64);
auto constexpr SaltSize = size_t{ 8 };
auto salt = std::array<char, SaltSize>{};
tr_rand_buffer(std::data(salt), std::size(salt));
auto salt = tr_rand_obj<std::array<char, SaltSize>>();
std::transform(
std::begin(salt),
std::end(salt),
Expand Down
8 changes: 8 additions & 0 deletions lib/libtransmission/crypto-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ void tr_x509_cert_free(tr_x509_cert_t handle);
*/
bool tr_rand_buffer(void* buffer, size_t length);

template<typename T>
T tr_rand_obj()
{
auto t = T{};
tr_rand_buffer(&t, sizeof(T));
return t;
}

/**
* @brief Generate a SSHA password from its plaintext source.
*/
Expand Down
8 changes: 7 additions & 1 deletion lib/libtransmission/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

using namespace std::literals;

#ifdef _WIN32
static auto constexpr NativeEol = "\r\n"sv;
#else
static auto constexpr NativeEol = "\n"sv;
#endif

bool tr_sys_file_write_line(tr_sys_file_t handle, std::string_view buffer, tr_error** error)
{
TR_ASSERT(handle != TR_BAD_SYS_FILE);
Expand All @@ -21,7 +27,7 @@ bool tr_sys_file_write_line(tr_sys_file_t handle, std::string_view buffer, tr_er

if (ret)
{
ret = tr_sys_file_write(handle, TR_NATIVE_EOL_STR, TR_NATIVE_EOL_STR_SIZE, nullptr, error);
ret = tr_sys_file_write(handle, std::data(NativeEol), std::size(NativeEol), nullptr, error);
}

return ret;
Expand Down
6 changes: 0 additions & 6 deletions lib/libtransmission/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,13 @@ using tr_sys_file_t = int;
#define TR_BAD_SYS_FILE (-1)
/** @brief Platform-specific directory descriptor type. */
using tr_sys_dir_t = void*;
/** @brief Platform-specific end-of-line sequence. */
#define TR_NATIVE_EOL_STR "\n"
/** @brief Platform-specific end-of-line sequence length. */
#define TR_NATIVE_EOL_STR_SIZE 1

#else

using tr_sys_file_t = HANDLE;
#define TR_BAD_SYS_FILE INVALID_HANDLE_VALUE
struct tr_sys_dir_win32;
using tr_sys_dir_t = tr_sys_dir_win32*;
#define TR_NATIVE_EOL_STR "\r\n"
#define TR_NATIVE_EOL_STR_SIZE 2

#endif

Expand Down
27 changes: 24 additions & 3 deletions lib/libtransmission/mime-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct mime_type_suffix
std::string_view mime_type;
};

inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1235>{
{ { "123", "application/vnd.lotus-1-2-3" },
{ "1km", "application/vnd.1000minds.decision-model+xml" },
{ "3dml", "text/vnd.in3d.3dml" },
Expand All @@ -26,6 +26,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "3mf", "model/3mf" },
{ "7z", "application/x-7z-compressed" },
{ "aab", "application/x-authorware-bin" },
{ "aac", "audio/aac" },
{ "aac", "audio/x-aac" },
{ "aam", "application/x-authorware-map" },
{ "aas", "application/x-authorware-seg" },
Expand All @@ -37,6 +38,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "acu", "application/vnd.acucobol" },
{ "acutc", "application/vnd.acucorp" },
{ "adp", "audio/adpcm" },
{ "adts", "audio/aac" },
{ "aep", "application/vnd.audiograph" },
{ "afm", "application/x-font-type1" },
{ "afp", "application/vnd.ibm.modcap" },
Expand All @@ -52,11 +54,13 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "amr", "audio/amr" },
{ "apk", "application/vnd.android.package-archive" },
{ "apng", "image/apng" },
{ "apng", "image/vnd.mozilla.apng" },
{ "appcache", "text/cache-manifest" },
{ "application", "application/x-ms-application" },
{ "apr", "application/vnd.lotus-approach" },
{ "arc", "application/x-freearc" },
{ "arj", "application/x-arj" },
{ "asc", "application/pgp-keys" },
{ "asc", "application/pgp-signature" },
{ "asf", "video/x-ms-asf" },
{ "asm", "text/x-asm" },
Expand All @@ -69,6 +73,8 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "atomsvc", "application/atomsvc+xml" },
{ "atx", "application/vnd.antix.game-component" },
{ "au", "audio/basic" },
{ "avci", "image/avci" },
{ "avcs", "image/avcs" },
{ "avi", "video/x-msvideo" },
{ "avif", "image/avif" },
{ "aw", "application/applixware" },
Expand Down Expand Up @@ -97,6 +103,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "boz", "application/x-bzip2" },
{ "bpk", "application/octet-stream" },
{ "bsp", "model/vnd.valve.source.compiled-map" },
{ "btf", "image/prs.btif" },
{ "btif", "image/prs.btif" },
{ "buffer", "application/octet-stream" },
{ "bz", "application/x-bzip" },
Expand Down Expand Up @@ -163,6 +170,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "com", "application/x-msdownload" },
{ "conf", "text/plain" },
{ "cpio", "application/x-cpio" },
{ "cpl", "application/cpl+xml" },
{ "cpp", "text/x-c" },
{ "cpt", "application/mac-compactpro" },
{ "crd", "application/x-mscardfile" },
Expand All @@ -179,6 +187,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "csv", "text/csv" },
{ "cu", "application/cu-seeme" },
{ "curl", "text/vnd.curl" },
{ "cwl", "application/cwl" },
{ "cww", "application/prs.cww" },
{ "cxt", "application/x-director" },
{ "cxx", "text/x-c" },
Expand All @@ -202,6 +211,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "der", "application/x-x509-ca-cert" },
{ "dfac", "application/vnd.dreamfactory" },
{ "dgc", "application/x-dgc-compressed" },
{ "dib", "image/bmp" },
{ "dic", "text/x-c" },
{ "dir", "application/x-director" },
{ "dis", "application/vnd.mobius.dis" },
Expand Down Expand Up @@ -262,7 +272,6 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "eot", "application/vnd.ms-fontobject" },
{ "eps", "application/postscript" },
{ "epub", "application/epub+zip" },
{ "es", "application/ecmascript" },
{ "es3", "application/vnd.eszigno3+xml" },
{ "esa", "application/vnd.osgi.subsystem" },
{ "esf", "application/vnd.epson.esf" },
Expand All @@ -287,6 +296,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "fbs", "image/vnd.fastbidsheet" },
{ "fcdt", "application/vnd.adobe.formscentral.fcdt" },
{ "fcs", "application/vnd.isac.fcs" },
{ "fdf", "application/fdf" },
{ "fdf", "application/vnd.fdf" },
{ "fdt", "application/fdt+xml" },
{ "fe_launch", "application/vnd.denovo.fcselayout-link" },
Expand Down Expand Up @@ -453,6 +463,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "jpm", "video/jpm" },
{ "jpx", "image/jpx" },
{ "js", "application/javascript" },
{ "js", "text/javascript" },
{ "json", "application/json" },
{ "json5", "application/json5" },
{ "jsonld", "application/ld+json" },
Expand Down Expand Up @@ -563,7 +574,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "mime", "message/rfc822" },
{ "mj2", "video/mj2" },
{ "mjp2", "video/mj2" },
{ "mjs", "application/javascript" },
{ "mjs", "text/javascript" },
{ "mk3d", "video/x-matroska" },
{ "mka", "audio/x-matroska" },
{ "mkd", "text/x-markdown" },
Expand Down Expand Up @@ -593,6 +604,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "mpd", "application/dash+xml" },
{ "mpe", "video/mpeg" },
{ "mpeg", "video/mpeg" },
{ "mpf", "application/media-policy-dataset+xml" },
{ "mpg", "video/mpeg" },
{ "mpg4", "video/mp4" },
{ "mpga", "audio/mpeg" },
Expand Down Expand Up @@ -774,6 +786,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "pqa", "application/vnd.palm" },
{ "prc", "application/x-mobipocket-ebook" },
{ "prc", "application/x-pilot" },
{ "prc", "model/prc" },
{ "pre", "application/vnd.lotus-freelance" },
{ "prf", "application/pics-rules" },
{ "provx", "application/provenance+xml" },
Expand All @@ -788,6 +801,8 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "pvb", "application/vnd.3gpp.pic-bw-var" },
{ "pwn", "application/vnd.3m.post-it-notes" },
{ "pya", "audio/vnd.ms-playready.media.pya" },
{ "pyo", "model/vnd.pytha.pyox" },
{ "pyox", "model/vnd.pytha.pyox" },
{ "pyv", "video/vnd.ms-playready.media.pyv" },
{ "qam", "application/vnd.epson.quickanime" },
{ "qbo", "application/vnd.intu.qbo" },
Expand Down Expand Up @@ -1015,6 +1030,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "txf", "application/vnd.mobius.txf" },
{ "txt", "text/plain" },
{ "u32", "application/x-authorware-bin" },
{ "u3d", "model/u3d" },
{ "u8dsn", "message/global-delivery-status" },
{ "u8hdr", "message/global-headers" },
{ "u8mdn", "message/global-disposition-notification" },
Expand All @@ -1026,6 +1042,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "ulx", "application/x-glulx" },
{ "umj", "application/vnd.umajin" },
{ "unityweb", "application/vnd.unity" },
{ "uo", "application/vnd.uoml+xml" },
{ "uoml", "application/vnd.uoml+xml" },
{ "uri", "text/uri-list" },
{ "uris", "text/uri-list" },
Expand Down Expand Up @@ -1111,6 +1128,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "webp", "image/webp" },
{ "wg", "application/vnd.pmi.widget" },
{ "wgt", "application/widget" },
{ "wif", "application/watcherinfo+xml" },
{ "wks", "application/vnd.ms-works" },
{ "wm", "video/x-ms-wm" },
{ "wma", "audio/x-ms-wma" },
Expand Down Expand Up @@ -1167,8 +1185,10 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "xenc", "application/xenc+xml" },
{ "xer", "application/patch-ops-error+xml" },
{ "xfdf", "application/vnd.adobe.xfdf" },
{ "xfdf", "application/xfdf" },
{ "xfdl", "application/vnd.xfdl" },
{ "xht", "application/xhtml+xml" },
{ "xhtm", "application/vnd.pwg-xhtml-print+xml" },
{ "xhtml", "application/xhtml+xml" },
{ "xhvml", "application/xv+xml" },
{ "xif", "image/vnd.xiff" },
Expand Down Expand Up @@ -1200,6 +1220,7 @@ inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, 1214>{
{ "xpw", "application/vnd.intercon.formnet" },
{ "xpx", "application/vnd.intercon.formnet" },
{ "xsd", "application/xml" },
{ "xsf", "application/prs.xsf+xml" },
{ "xsl", "application/xml" },
{ "xsl", "application/xslt+xml" },
{ "xslt", "application/xslt+xml" },
Expand Down
10 changes: 9 additions & 1 deletion lib/libtransmission/peer-io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,15 @@ static void utp_on_state_change(tr_peerIo* const io, int const state)

static void utp_on_error(tr_peerIo* const io, int const errcode)
{
tr_logAddDebugIo(io, fmt::format("utp_on_error -- errcode is {}", errcode));
if (errcode == UTP_ETIMEDOUT)
{
// high frequency error: we log as trace
tr_logAddTraceIo(io, fmt::format("utp_on_error -- UTP_ETIMEDOUT"));
}
else
{
tr_logAddDebugIo(io, fmt::format("utp_on_error -- {}", utp_error_code_names[errcode]));
}

if (io->gotError != nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/libtransmission/peer-mgr-wishlist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "transmission.h"

#include "crypto-utils.h" // tr_rand_buffer()
#include "crypto-utils.h" // for tr_salt_shaker
#include "peer-mgr-wishlist.h"
#include "tr-assert.h"

Expand Down
4 changes: 1 addition & 3 deletions lib/libtransmission/peer-mse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ namespace tr_message_stream_encryption

[[nodiscard]] DH::private_key_bigend_t DH::randomPrivateKey() noexcept
{
auto key = DH::private_key_bigend_t{};
tr_rand_buffer(std::data(key), std::size(key));
return key;
return tr_rand_obj<DH::private_key_bigend_t>();
}

[[nodiscard]] auto generatePublicKey(DH::private_key_bigend_t const& private_key) noexcept
Expand Down
2 changes: 1 addition & 1 deletion lib/libtransmission/rpc-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#include "transmission.h"

#include "crypto-utils.h" /* tr_rand_buffer(), tr_ssha1_matches() */
#include "crypto-utils.h" /* tr_ssha1_matches() */
#include "error.h"
#include "log.h"
#include "net.h"
Expand Down
5 changes: 2 additions & 3 deletions lib/libtransmission/session-id.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "session-id.h"

#include "crypto-utils.h" // for tr_rand_buf()
#include "crypto-utils.h" // for tr_rand_obj()
#include "error-types.h"
#include "error.h"
#include "file.h"
Expand Down Expand Up @@ -103,8 +103,7 @@ auto constexpr WouldBlock = ERROR_LOCK_VIOLATION;

tr_session_id::session_id_t tr_session_id::make_session_id()
{
auto session_id = session_id_t{};
tr_rand_buffer(std::data(session_id), std::size(session_id));
auto session_id = tr_rand_obj<session_id_t>();
static auto constexpr Pool = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"sv;
for (auto& chr : session_id)
{
Expand Down
3 changes: 3 additions & 0 deletions lib/libtransmission/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ void tr_sessionGetSettings(tr_session const* session, tr_variant* setme_dictiona
session->settings_.save(setme_dictionary);
session->alt_speeds_.save(setme_dictionary);
session->rpc_server_->save(setme_dictionary);

tr_variantDictRemove(setme_dictionary, TR_KEY_message_level);
tr_variantDictAddInt(setme_dictionary, TR_KEY_message_level, tr_logGetLevel());
}

static void getSettingsFilename(tr_pathbuf& setme, char const* config_dir, char const* appname)
Expand Down
3 changes: 1 addition & 2 deletions lib/libtransmission/tr-dht.cc
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,7 @@ class tr_dht_impl final : public tr_dht
{
// Note that DHT ids need to be distributed uniformly,
// so it should be something truly random
auto id = Id{};
tr_rand_buffer(std::data(id), std::size(id));
auto id = tr_rand_obj<Id>();

auto nodes = Nodes{};

Expand Down
Loading

0 comments on commit 3df1e01

Please sign in to comment.