Changeset View
Changeset View
Standalone View
Standalone View
src/torcontrol.cpp
Show First 20 Lines • Show All 323 Lines • ▼ Show 20 Lines | |||||
* If an error occurred, status will be false, otherwise status will be true and | * If an error occurred, status will be false, otherwise status will be true and | ||||
* the data will be returned in string. | * the data will be returned in string. | ||||
* | * | ||||
* @param maxsize Puts a maximum size limit on the file that is read. If the | * @param maxsize Puts a maximum size limit on the file that is read. If the | ||||
* file is larger than this, truncated data | * file is larger than this, truncated data | ||||
* (with len > maxsize) will be returned. | * (with len > maxsize) will be returned. | ||||
*/ | */ | ||||
static std::pair<bool, std::string> | static std::pair<bool, std::string> | ||||
ReadBinaryFile(const std::string &filename, | ReadBinaryFile(const fs::path &filename, | ||||
size_t maxsize = std::numeric_limits<size_t>::max()) { | size_t maxsize = std::numeric_limits<size_t>::max()) { | ||||
FILE *f = fopen(filename.c_str(), "rb"); | FILE *f = fsbridge::fopen(filename, "rb"); | ||||
if (f == nullptr) return std::make_pair(false, ""); | if (f == nullptr) { | ||||
return std::make_pair(false, ""); | |||||
} | |||||
std::string retval; | std::string retval; | ||||
char buffer[128]; | char buffer[128]; | ||||
size_t n; | size_t n; | ||||
while ((n = fread(buffer, 1, sizeof(buffer), f)) > 0) { | while ((n = fread(buffer, 1, sizeof(buffer), f)) > 0) { | ||||
retval.append(buffer, buffer + n); | retval.append(buffer, buffer + n); | ||||
if (retval.size() > maxsize) break; | if (retval.size() > maxsize) break; | ||||
} | } | ||||
fclose(f); | fclose(f); | ||||
return std::make_pair(true, retval); | return std::make_pair(true, retval); | ||||
} | } | ||||
/** | /** | ||||
* Write contents of std::string to a file. | * Write contents of std::string to a file. | ||||
* @return true on success. | * @return true on success. | ||||
*/ | */ | ||||
static bool WriteBinaryFile(const std::string &filename, | static bool WriteBinaryFile(const fs::path &filename, const std::string &data) { | ||||
const std::string &data) { | FILE *f = fsbridge::fopen(filename, "wb"); | ||||
FILE *f = fopen(filename.c_str(), "wb"); | |||||
if (f == nullptr) return false; | if (f == nullptr) return false; | ||||
if (fwrite(data.data(), 1, data.size(), f) != data.size()) { | if (fwrite(data.data(), 1, data.size(), f) != data.size()) { | ||||
fclose(f); | fclose(f); | ||||
return false; | return false; | ||||
} | } | ||||
fclose(f); | fclose(f); | ||||
return true; | return true; | ||||
} | } | ||||
/****** Bitcoin specific TorController implementation ********/ | /****** Bitcoin specific TorController implementation ********/ | ||||
/** | /** | ||||
* Controller that connects to Tor control socket, authenticate, then create | * Controller that connects to Tor control socket, authenticate, then create | ||||
* and maintain a ephemeral hidden service. | * and maintain a ephemeral hidden service. | ||||
*/ | */ | ||||
class TorController { | class TorController { | ||||
public: | public: | ||||
TorController(struct event_base *base, const std::string &target); | TorController(struct event_base *base, const std::string &target); | ||||
~TorController(); | ~TorController(); | ||||
/** Get name fo file to store private key in */ | /** Get name fo file to store private key in */ | ||||
std::string GetPrivateKeyFile(); | fs::path GetPrivateKeyFile(); | ||||
/** Reconnect, after getting disconnected */ | /** Reconnect, after getting disconnected */ | ||||
void Reconnect(); | void Reconnect(); | ||||
private: | private: | ||||
struct event_base *base; | struct event_base *base; | ||||
std::string target; | std::string target; | ||||
TorControlConnection conn; | TorControlConnection conn; | ||||
▲ Show 20 Lines • Show All 325 Lines • ▼ Show 20 Lines | if (!conn.Connect(target, | ||||
boost::bind(&TorController::connected_cb, this, _1), | boost::bind(&TorController::connected_cb, this, _1), | ||||
boost::bind(&TorController::disconnected_cb, this, _1))) { | boost::bind(&TorController::disconnected_cb, this, _1))) { | ||||
LogPrintf( | LogPrintf( | ||||
"tor: Re-initiating connection to Tor control port %s failed\n", | "tor: Re-initiating connection to Tor control port %s failed\n", | ||||
target); | target); | ||||
} | } | ||||
} | } | ||||
std::string TorController::GetPrivateKeyFile() { | fs::path TorController::GetPrivateKeyFile() { | ||||
return (GetDataDir() / "onion_private_key").string(); | return GetDataDir() / "onion_private_key"; | ||||
} | } | ||||
void TorController::reconnect_cb(evutil_socket_t fd, short what, void *arg) { | void TorController::reconnect_cb(evutil_socket_t fd, short what, void *arg) { | ||||
TorController *self = (TorController *)arg; | TorController *self = (TorController *)arg; | ||||
self->Reconnect(); | self->Reconnect(); | ||||
} | } | ||||
/****** Thread ********/ | /****** Thread ********/ | ||||
Show All 40 Lines |