i2pd/i2p.cpp

145 lines
3.0 KiB
C++
Raw Normal View History

2013-12-10 17:05:42 +04:00
#include <iostream>
2014-01-12 23:35:51 +04:00
#include <thread>
2013-12-10 17:05:42 +04:00
#include <cryptopp/integer.h>
#include <boost/filesystem.hpp>
2014-02-05 03:50:13 +04:00
#ifndef _WIN32
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
2014-02-05 03:50:13 +04:00
#endif
2013-12-10 17:05:42 +04:00
#include "Log.h"
#include "base64.h"
#include "Transports.h"
#include "NTCPSession.h"
#include "RouterInfo.h"
#include "RouterContext.h"
#include "Tunnel.h"
#include "NetDb.h"
#include "HTTPServer.h"
2014-01-30 04:56:48 +04:00
#include "util.h"
2013-12-10 17:05:42 +04:00
2014-02-05 04:28:18 +04:00
// Global
int running = 1;
2014-02-05 03:50:13 +04:00
#ifndef _WIN32
void handle_sighup(int n)
{
if (i2p::util::config::GetArg("daemon", 0) == 1)
{
static bool first=true;
if (first)
{
first=false;
return;
}
}
LogPrint("Reloading config.");
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
}
2014-02-05 04:28:18 +04:00
void handle_shutdown(int sig)
{
running = 0; // Exit loop
}
2014-02-05 03:50:13 +04:00
#endif
2014-02-05 04:28:18 +04:00
2014-01-30 04:56:48 +04:00
int main( int argc, char* argv[] )
2013-12-10 17:05:42 +04:00
{
i2p::util::config::OptionParser(argc,argv);
#ifdef _WIN32
setlocale(LC_CTYPE, "");
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
setlocale(LC_ALL, "Russian");
#endif
LogPrint("\n\n\n\ni2pd starting\n");
2014-02-01 07:51:58 +04:00
LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string());
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
2014-02-05 03:50:13 +04:00
#ifndef _WIN32
struct sigaction sa;
sa.sa_handler = handle_sighup;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGHUP,&sa,0) == -1)
{
LogPrint("Failed to install SIGHUP handler.");
}
if (i2p::util::config::GetArg("-daemon", 0) == 1)
{
pid_t pid;
pid = fork();
if (pid > 0)
{
g_Log.Stop();
return 0;
}
if (pid < 0)
{
return -1;
}
umask(0);
int sid = setsid();
if (sid < 0)
{
LogPrint("Error, could not create process group.");
return -1;
}
}
2014-02-05 04:28:18 +04:00
// Handle shutdown
signal(SIGABRT, &handle_shutdown);
signal(SIGTERM, &handle_shutdown);
signal(SIGINT, &handle_shutdown);
2014-02-05 03:50:13 +04:00
#endif
if (i2p::util::config::GetArg("-log", 0) == 1)
{
std::string logfile = i2p::util::filesystem::GetDataDir().string();
2014-02-05 03:50:13 +04:00
#ifndef _WIN32
logfile.append("/debug.log");
2014-02-05 03:50:13 +04:00
#else
logfile.append("\\debug.log");
#endif
LogPrint("Logging to file enabled.");
freopen(logfile.c_str(),"a",stdout);
}
//TODO: This is an ugly workaround. fix it.
//TODO: Autodetect public IP.
i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"),
i2p::util::config::GetArg("-port", 17070));
2014-01-30 04:56:48 +04:00
2014-02-05 04:28:18 +04:00
i2p::util::HTTPServer httpServer (i2p::util::config::GetArg("-httpport", 7070));
2013-12-10 17:05:42 +04:00
httpServer.Start ();
i2p::data::netdb.Start ();
i2p::transports.Start ();
i2p::tunnel::tunnels.Start ();
while (running)
{
2014-02-05 03:50:13 +04:00
//TODO Meeh: Find something better to do here.
2014-02-05 04:28:18 +04:00
std::this_thread::sleep_for (std::chrono::seconds(1));
}
2014-02-05 04:28:18 +04:00
LogPrint("Shutdown started.");
2013-12-10 17:05:42 +04:00
i2p::tunnel::tunnels.Stop ();
i2p::transports.Stop ();
i2p::data::netdb.Stop ();
httpServer.Stop ();
2014-02-05 03:50:13 +04:00
if (i2p::util::config::GetArg("-log", 0) == 1)
{
fclose (stdout);
}
2013-12-10 17:05:42 +04:00
return 0;
}