Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/tor/TorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,10 @@
#include <fstream>
#include <stdio.h>

// This works on linux only. I have no clue how to do that on windows. Anyway, this
// is only needed for an assert that should normaly never be triggered.

#if !defined(_WIN32) && !defined(__MINGW32__)
#include <sys/syscall.h>
#endif

#include "util/rsdir.h"
#include "retroshare/rsinit.h"
#include <thread>

#include "TorManager.h"
#include "TorProcess.h"
Expand Down Expand Up @@ -159,11 +154,15 @@ std::string TorManager::torDataDirectory() const

void TorManager::setTorDataDirectory(const std::string &path)
{
assert(RsDirUtil::checkCreateDirectory(std::string(path)));
if(!RsDirUtil::checkCreateDirectory(path))
{
RsErr() << "TorManager::setTorDataDirectory() cannot create directory: " << path ;
return ;
}

d->dataDir = path;

if (!d->dataDir.empty() && !ByteArray(d->dataDir).endsWith('/'))
if (!d->dataDir.empty() && !(d->dataDir.back() == '/'))
d->dataDir += '/';
}

Expand Down Expand Up @@ -719,6 +718,12 @@ std::string TorManagerPrivate::torExecutablePath() const
#ifdef __APPLE__
// on MacOS, try traditional brew installation path

path = "/opt/homebrew/opt/tor/bin" ;
tor_exe_path = RsDirUtil::makePath(path,filename);

if (RsDirUtil::fileExists(tor_exe_path))
return tor_exe_path;

path = "/usr/local/opt/tor/bin" ;
tor_exe_path = RsDirUtil::makePath(path,filename);

Expand Down Expand Up @@ -936,10 +941,17 @@ void RsTor::setHiddenServiceDirectory(const std::string& dir)
instance()->setHiddenServiceDirectory(dir);
}

#ifdef __APPLE__
#include <pthread.h>
#endif

TorManager *RsTor::instance()
{
#if !defined(_WIN32) && !defined(__MINGW32__)
assert(getpid() == syscall(SYS_gettid));// make sure we're not in a thread
#ifdef __APPLE__
assert(pthread_main_np() != 0); // On macOS, ensure we are on the main thread
#else
static std::thread::id main_thread_id = std::this_thread::get_id();
assert(std::this_thread::get_id() == main_thread_id); // make sure we're not in a different thread
#endif

if(rsTor == nullptr)
Expand Down
29 changes: 18 additions & 11 deletions src/tor/bytearray.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class ByteArray: public std::vector<unsigned char>
public:
ByteArray() =default;
explicit ByteArray(int n) : std::vector<unsigned char>(n) {}
explicit ByteArray(const unsigned char *d,int n) : std::vector<unsigned char>(n) { memcpy(data(),d,n); }
explicit ByteArray(const unsigned char *d,int n) : std::vector<unsigned char>(n) { if (n > 0 && d) memcpy(data(),d,n); }
virtual ~ByteArray() =default;

ByteArray(const std::string& c) { resize(c.size()); memcpy(data(),c.c_str(),c.size()); }
const ByteArray& operator=(const std::string& c) { resize(c.size()); memcpy(data(),c.c_str(),c.size()); return *this; }
ByteArray(const std::string& c) { resize(c.size()); if(c.size() > 0) memcpy(data(),c.c_str(),c.size()); }
const ByteArray& operator=(const std::string& c) { resize(c.size()); if(c.size() > 0) memcpy(data(),c.c_str(),c.size()); return *this; }

bool isNull() const { return empty(); }
ByteArray toHex() const { return ByteArray(RsUtil::BinToHex(data(),size(),0)); }
Expand Down Expand Up @@ -53,13 +53,13 @@ class ByteArray: public std::vector<unsigned char>

return res;
}
bool endsWith(const ByteArray& b) const { return size() >= b.size() && !memcmp(&data()[size()-b.size()],b.data(),b.size()); }
bool endsWith(const ByteArray& b) const { return size() >= b.size() && (b.empty() || !memcmp(data() + (size() - b.size()), b.data(), b.size())); }
bool endsWith(char b) const { return size() > 0 && back()==b; }
bool startsWith(const ByteArray& b) const { return b.size() <= size() && !strncmp((char*)b.data(),(char*)data(),std::min(size(),b.size())); }
bool startsWith(const ByteArray& b) const { return size() >= b.size() && (b.empty() || !memcmp(data(), b.data(), b.size())); }
bool startsWith(const char *b) const
{
for(uint32_t n=0;b[n]!=0;++n)
if(n >= size() || b[n]!=(*this)[n])
if(n >= size() || (unsigned char)b[n]!=(*this)[n])
return false;

return true;
Expand All @@ -69,16 +69,19 @@ class ByteArray: public std::vector<unsigned char>
{
uint32_t n;
for(n=0;b[n]!=0;++n)
if(n >= size() || b[n]!=(*this)[n])
if(n >= size() || (unsigned char)b[n]!=(*this)[n])
return false;

return n==size();
}

ByteArray mid(uint32_t n,int s=-1) const
ByteArray mid(uint32_t n, int s = -1) const
{
ByteArray res((s>=0)?s:(size()-n));
memcpy(res.data(),&data()[n],res.size());
if (n >= size()) return ByteArray();
uint32_t max_len = (uint32_t)size() - n;
uint32_t len = (s >= 0) ? std::min((uint32_t)s, max_len) : max_len;
ByteArray res(len);
if (len > 0) memcpy(res.data(), data() + n, len);
return res;
}

Expand All @@ -99,7 +102,8 @@ class ByteArray: public std::vector<unsigned char>
}
ByteArray res ;

for(uint32_t i=0;i+b1.size()<=size();)
uint32_t i=0;
for(;i+b1.size()<=size();)
if(!memcmp(&(*this)[i],b1.data(),b1.size()))
{
res.append(b2);
Expand All @@ -108,6 +112,9 @@ class ByteArray: public std::vector<unsigned char>
else
res.push_back((*this)[i++]);

for(;i<size();++i)
res.push_back((*this)[i]);

return res;
}

Expand Down
Loading