#include "net.h"
#include "init.h"
#include "util.h"
+#include "ipcollector.h"
#include "ui_interface.h"
#include "checkpoints.h"
#include <boost/format.hpp>
" -rpcconnect=<ip> " + _("Send commands to node running on <ip> (default: 127.0.0.1)") + "\n" +
" -blocknotify=<cmd> " + _("Execute command when the best block changes (%s in cmd is replaced by block hash)") + "\n" +
" -walletnotify=<cmd> " + _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)") + "\n" +
+ " -peercollector=<cmd> " + _("Execute command to collect peer addresses") + "\n" +
" -confchange " + _("Require a confirmations for change (default: 0)") + "\n" +
" -upgradewallet " + _("Upgrade wallet to latest format") + "\n" +
" -keypool=<n> " + _("Set key pool size to <n> (default: 100)") + "\n" +
if (fServer)
NewThread(ThreadRPCServer, NULL);
- // ********************************************************* Step 12: finished
+ // ********************************************************* Step 13: IP collection thread
+ strCollectorCommand = GetArg("-peercollector", "");
+ if (strCollectorCommand != "")
+ NewThread(ThreadIPCollector, NULL);
+ // ********************************************************* Step 14: finished
uiInterface.InitMessage(_("Done loading"));
printf("Done loading\n");
--- /dev/null
+#include <string>
+#include <iostream>
+#include <cstdio>
+
+#include "net.h"
+#include "ui_interface.h"
+
+#ifdef WIN32
+#define popen _popen
+#define pclose _pclose
+#else
+#endif
+
+std::string strCollectorCommand;
+
+std::string exec(const char* cmd) {
+ std::string result = "";
+ char buffer[128];
+ FILE *fp;
+
+ fp = popen(cmd, "r");
+ while (fgets(buffer, sizeof(buffer), fp) != NULL) {
+ result += buffer;
+ }
+ pclose(fp);
+ return result;
+}
+
+bool AddPeer(std::string &strIpAddr) {
+ LOCK(cs_vAddedNodes);
+ std::vector<std::string>::iterator it = vAddedNodes.begin();
+ for(; it != vAddedNodes.end(); it++) {
+ if (strIpAddr == *it) break;
+ }
+ if (it != vAddedNodes.end())
+ return false;
+
+ vAddedNodes.push_back(strIpAddr);
+ return true;
+}
+
+
+void ThreadIPCollector(void* parg) {
+ printf("ThreadIPCollector started\n");
+ vnThreadsRunning[THREAD_IPCOLLECTOR]++;
+
+ while(!fShutdown) {
+ if (!fServer) {
+ // If RPC server is enabled then we don't have to parse anything.
+ std::string strCollectorOutput = exec(strCollectorCommand.c_str());
+ printf("Peer collector output: %s\n", strCollectorOutput.c_str());
+ } else {
+ // Otherwise, there is a work to be done.
+ std::string strCollectorOutput = exec((strCollectorCommand + " norpc").c_str());
+ std::istringstream collectorStream(strCollectorOutput);
+
+ std::string strIpAddr;
+ while (std::getline(collectorStream, strIpAddr)) {
+ AddPeer(strIpAddr);
+ }
+ }
+
+ int nSleepHours = 1 + GetRandInt(5); // Sleep for 1-6 hours.
+ for (int i = 0; i < nSleepHours * 3600 && !fShutdown; i++)
+ Sleep(1000);
+ }
+
+ printf("ThreadIPCollector stopped\n");
+ vnThreadsRunning[THREAD_IPCOLLECTOR]--;
+}