2014-02-05 05:14:07 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define _CRT_SECURE_NO_WARNINGS // to use freopen
|
|
|
|
#endif
|
|
|
|
|
2013-12-10 13:05:42 +00:00
|
|
|
#include <iostream>
|
2014-01-12 19:35:51 +00:00
|
|
|
#include <thread>
|
2013-12-10 13:05:42 +00:00
|
|
|
#include <cryptopp/integer.h>
|
2014-02-01 03:09:55 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2014-02-04 23:50:13 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2014-02-04 23:43:50 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2014-02-04 23:50:13 +00:00
|
|
|
#endif
|
|
|
|
|
2013-12-10 13:05:42 +00: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 00:56:48 +00:00
|
|
|
#include "util.h"
|
2013-12-10 13:05:42 +00:00
|
|
|
|
2014-02-05 00:28:18 +00:00
|
|
|
|
|
|
|
// Global
|
2014-02-08 21:06:01 +00:00
|
|
|
volatile int running = 1;
|
|
|
|
volatile int isDaemon;
|
2014-02-05 00:28:18 +00:00
|
|
|
|
2014-02-04 23:50:13 +00:00
|
|
|
#ifndef _WIN32
|
2014-02-05 08:45:57 +00:00
|
|
|
void handle_signal(int sig)
|
2014-02-04 23:43:50 +00:00
|
|
|
{
|
2014-02-05 08:45:57 +00:00
|
|
|
switch (sig)
|
2014-02-04 23:43:50 +00:00
|
|
|
{
|
2014-02-05 08:45:57 +00:00
|
|
|
case SIGHUP:
|
|
|
|
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);
|
|
|
|
break;
|
|
|
|
case SIGABRT:
|
|
|
|
case SIGTERM:
|
|
|
|
case SIGINT:
|
|
|
|
running = 0; // Exit loop
|
|
|
|
break;
|
2014-02-04 23:43:50 +00:00
|
|
|
}
|
2014-02-05 00:28:18 +00:00
|
|
|
}
|
2014-02-04 23:50:13 +00:00
|
|
|
#endif
|
2014-02-04 23:43:50 +00:00
|
|
|
|
2014-02-05 00:28:18 +00:00
|
|
|
|
2014-01-30 00:56:48 +00:00
|
|
|
int main( int argc, char* argv[] )
|
2013-12-10 13:05:42 +00:00
|
|
|
{
|
2014-02-01 03:09:55 +00:00
|
|
|
i2p::util::config::OptionParser(argc,argv);
|
2014-02-08 21:06:01 +00:00
|
|
|
volatile int isDaemon = i2p::util::config::GetArg("-daemon", 0);
|
2014-01-25 06:58:11 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
SetConsoleCP(1251);
|
|
|
|
SetConsoleOutputCP(1251);
|
|
|
|
setlocale(LC_ALL, "Russian");
|
|
|
|
#endif
|
|
|
|
|
2014-02-04 23:43:50 +00:00
|
|
|
|
2014-02-01 03:09:55 +00:00
|
|
|
LogPrint("\n\n\n\ni2pd starting\n");
|
2014-02-01 03:51:58 +00:00
|
|
|
LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string());
|
2014-02-01 03:09:55 +00:00
|
|
|
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
|
|
|
|
|
2014-02-08 21:06:01 +00:00
|
|
|
volatile int isLogging = i2p::util::config::GetArg("-log", 0);
|
2014-02-08 02:16:52 +00:00
|
|
|
if (isLogging == 1)
|
|
|
|
{
|
|
|
|
std::string logfile = i2p::util::filesystem::GetDataDir().string();
|
|
|
|
#ifndef _WIN32
|
|
|
|
logfile.append("/debug.log");
|
|
|
|
#else
|
|
|
|
logfile.append("\\debug.log");
|
|
|
|
#endif
|
|
|
|
freopen(logfile.c_str(),"a",stdout);
|
|
|
|
LogPrint("Logging to file enabled.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-04 23:50:13 +00:00
|
|
|
#ifndef _WIN32
|
2014-02-08 02:16:52 +00:00
|
|
|
if (isDaemon == 1)
|
2014-02-04 23:43:50 +00:00
|
|
|
{
|
|
|
|
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 08:45:57 +00:00
|
|
|
chdir(i2p::util::filesystem::GetDataDir().string().c_str());
|
2014-02-04 23:43:50 +00:00
|
|
|
}
|
2014-02-05 00:28:18 +00:00
|
|
|
|
2014-02-05 08:45:57 +00:00
|
|
|
// Pidfile
|
|
|
|
std::string pidfile = i2p::util::filesystem::GetDataDir().string();
|
|
|
|
pidfile.append("/i2pd.pid");
|
|
|
|
int pidFilehandle = open(pidfile.c_str(), O_RDWR|O_CREAT, 0600);
|
|
|
|
if (pidFilehandle == -1 )
|
|
|
|
{
|
|
|
|
LogPrint("Error, could not create pid file (", pidfile, ")\nIs an instance already running?");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (lockf(pidFilehandle,F_TLOCK,0) == -1)
|
|
|
|
{
|
2014-02-05 12:47:00 +00:00
|
|
|
LogPrint("Error, could not lock pid file (", pidfile, ")\nIs an instance already running?");
|
2014-02-05 08:45:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char pid[10];
|
|
|
|
sprintf(pid,"%d\n",getpid());
|
|
|
|
write(pidFilehandle, pid, strlen(pid));
|
|
|
|
|
|
|
|
// Signal handler
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_handler = handle_signal;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sigaction(SIGHUP,&sa,0);
|
|
|
|
sigaction(SIGABRT,&sa,0);
|
|
|
|
sigaction(SIGTERM,&sa,0);
|
|
|
|
sigaction(SIGINT,&sa,0);
|
2014-02-04 23:50:13 +00:00
|
|
|
#endif
|
2014-02-04 23:43:50 +00:00
|
|
|
|
2014-01-31 00:39:16 +00:00
|
|
|
//TODO: This is an ugly workaround. fix it.
|
|
|
|
//TODO: Autodetect public IP.
|
2014-02-01 03:09:55 +00:00
|
|
|
i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"),
|
|
|
|
i2p::util::config::GetArg("-port", 17070));
|
2014-01-30 00:56:48 +00:00
|
|
|
|
2014-02-05 00:28:18 +00:00
|
|
|
i2p::util::HTTPServer httpServer (i2p::util::config::GetArg("-httpport", 7070));
|
2013-12-10 13:05:42 +00:00
|
|
|
|
2014-02-05 08:45:57 +00:00
|
|
|
httpServer.Start ();
|
2013-12-10 13:05:42 +00:00
|
|
|
i2p::data::netdb.Start ();
|
2014-02-05 08:45:57 +00:00
|
|
|
i2p::transports.Start ();
|
2014-02-04 23:43:50 +00:00
|
|
|
i2p::tunnel::tunnels.Start ();
|
|
|
|
|
|
|
|
while (running)
|
|
|
|
{
|
2014-02-04 23:50:13 +00:00
|
|
|
//TODO Meeh: Find something better to do here.
|
2014-02-05 00:28:18 +00:00
|
|
|
std::this_thread::sleep_for (std::chrono::seconds(1));
|
2014-02-04 23:43:50 +00:00
|
|
|
}
|
2014-02-05 00:28:18 +00:00
|
|
|
LogPrint("Shutdown started.");
|
2014-02-04 23:43:50 +00:00
|
|
|
|
2014-02-05 08:45:57 +00:00
|
|
|
i2p::tunnel::tunnels.Stop ();
|
|
|
|
i2p::transports.Stop ();
|
|
|
|
i2p::data::netdb.Stop ();
|
2014-02-04 23:43:50 +00:00
|
|
|
httpServer.Stop ();
|
2014-02-04 23:50:13 +00:00
|
|
|
|
2014-02-08 02:16:52 +00:00
|
|
|
if (isLogging == 1)
|
2014-02-04 23:43:50 +00:00
|
|
|
{
|
|
|
|
fclose (stdout);
|
|
|
|
}
|
2014-02-05 08:45:57 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
close(pidFilehandle);
|
|
|
|
unlink(pidfile.c_str());
|
|
|
|
#endif
|
2013-12-10 13:05:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|