19 #include <boost/property_tree/ptree.hpp> 20 #include <boost/property_tree/json_parser.hpp> 36 #include "stunserver.txtcode" 37 #include "stunserver_lite.txtcode" 47 const char* psz = fSummaryUsage ? stunserver_lite_text : stunserver_text;
65 sprintf(msg,
"Unknown error %d", err);
80 const char* pMsg = NULL;
96 if (err == EADDRINUSE)
99 "This error likely means another application is listening on one\n" 100 "or more of the same ports you are attempting to configure this\n" 101 "server to listen on. Run \"netstat -a -p -t -u\" to see a list\n" 102 "of all ports in use and associated process id for each");
107 if (fGotMsg ==
false)
134 #define PRINTARG(member) Logging::LogMsg(LL_DEBUG, "%s = %s", #member, args.member.length() ? args.member.c_str() : "<empty>"); 159 std::string strSocket;
208 if (strAdapterName.length() == 0)
215 size_t x = strAdapterName.find_last_of(
':');
216 if (x != std::string::npos)
218 strAdapterName = strAdapterName.substr(0, x);
237 int family = AF_INET;
241 bool fHasAtLeastTwoAdapters =
false;
243 int nMaxConnections = 0;
252 ServerMode mode=Basic;
290 else if (args.
strMode ==
"full")
338 if (config.
fTCP ==
false)
340 Logging::LogMsg(
LL_ALWAYS,
"Max connections parameter has no meaning in UDP mode. Did you mean to specify \"--protocol=tcp ?\"");
379 if (nPrimaryPort == nAltPort)
393 uint16_t port = (uint16_t)((int16_t)nPrimaryPort);
397 if (family == AF_INET)
402 else if (family == AF_INET6)
404 sockaddr_in6 addr6 = {};
405 addr6.sin6_family = AF_INET6;
428 uint16_t portPrimary = (uint16_t)((int16_t)nPrimaryPort);
429 uint16_t portAlternate = (uint16_t)((int16_t)nAltPort);
433 if (fHasAtLeastTwoAdapters ==
false)
453 if (addrPrimary.
IsSameIP(addrAlternate))
455 Logging::LogMsg(
LL_ALWAYS,
"Error - Primary interface and Alternate Interface appear to have the same IP address. Full mode requires two IP addresses that are unique");
460 config.
addrPP = addrPrimary;
464 config.
addrPA = addrPrimary;
468 config.
addrAP = addrAlternate;
472 config.
addrAA = addrAlternate;
487 Logging::LogMsg(
LL_ALWAYS,
"Error with --primaryadvertised. %s is not a valid IP address string", pszPrimaryAdvertised);
511 *pConfigOut = config;
550 std::string errorMessage;
575 read_json(filename, tree);
576 ptree root = tree.get_child(
"configurations");
578 for (ptree::iterator itor = root.begin(); itor != root.end(); itor++)
580 const ptree& child = itor->second;
583 args.
strMode = child.get(
"mode",
"");
590 args.
strFamily = child.get(
"family",
"");
595 configurations.push_back(args);
598 catch(ptree_error ex1)
604 if (!error && configurations.size() == 0)
612 configurations.clear();
626 sigemptyset(&signalset);
627 sigemptyset(&oldset);
629 sigaddset(&signalset, sig);
632 result = pthread_sigmask(SIG_BLOCK, &signalset, &oldset);
652 sigaddset(&sigs, SIGINT);
653 sigaddset(&sigs, SIGTERM);
656 int ret = sigwait(&sigs, &sig);
658 if ((sig == SIGINT) || (sig == SIGTERM))
679 hr = spServer->
Start();
702 hr = spTCPServer->
Start();
714 int main(
int argc,
char** argv)
718 std::vector<StartupArgs> argsVector;
724 std::vector<UdpServerPtr> udpServers;
725 std::vector<TcpServerPtr> tcpServers;
728 signal(SIGPIPE, SIG_IGN);
758 int loglevel = (uint32_t)atoi(args.
strVerbosity.c_str());
777 argsVector.push_back(args);
782 for (std::vector<StartupArgs>::iterator itor = argsVector.begin(); itor != argsVector.end(); itor++)
801 TcpServerPtr spTcpServer;
806 tcpServers.push_back(spTcpServer);
811 UdpServerPtr spUdpServer;
815 udpServers.push_back(spUdpServer);
837 for (std::vector<UdpServerPtr>::iterator itor = udpServers.begin(); itor != udpServers.end(); itor++)
840 UdpServerPtr server = *itor;
844 for (std::vector<TcpServerPtr>::iterator itor = tcpServers.begin(); itor != tcpServers.end(); itor++)
847 TcpServerPtr server = *itor;
std::string strPrimaryAdvertised
int ValidateNumberString(const char *psz, int nMinValue, int nMaxValue, int *pnResult)
void PrintUsage(bool fSummaryUsage)
int main(int argc, char **argv)
void LogMsg(uint32_t level, const char *pszFormat,...)
CSocketAddress addrAlternateAdvertised
void DumpConfig(CStunServerConfig &config)
bool IsNullOrEmpty(const char *psz)
#define ERRNO_TO_HRESULT(err)
#define ChkIf(expr, hrerror)
std::string strMaxConnections
HRESULT BuildServerConfigurationFromArgs(StartupArgs &argsIn, CStunServerConfig *pConfigOut)
HRESULT GetSocketAddressForAdapter(int family, const char *pszAdapterName, uint16_t port, CSocketAddress *pSocketAddr)
std::string strPrimaryPort
std::string strPrimaryInterface
HRESULT StartTCP(CRefCountedPtr< CTCPServer > &spTCPServer, CStunServerConfig &config)
HRESULT GetBestAddressForSocketBind(bool fPrimary, int family, uint16_t port, CSocketAddress *pSocketAddr)
HRESULT NumericIPToAddress(int family, const char *pszIP, CSocketAddress *pAddr)
HRESULT ParseCommandLineArgs(int argc, char **argv, int startindex, StartupArgs *pStartupArgs)
void SetLogLevel(uint32_t level)
void ToLower(std::string &str)
HRESULT LoadConfigsFromFile(const std::string &filename, std::vector< StartupArgs > &configurations)
bool IsSameIP(const CSocketAddress &other) const
void Trim(std::string &str)
const uint16_t DEFAULT_STUN_PORT
HRESULT ParseCommandLine(int argc, char **argv, int startindex, bool *fParseError)
char * strerror_helper(int result, char *msg, int err)
void ToString(std::string *pStr) const
std::string strAltInterface
void LogHR(uint16_t level, HRESULT hr)
HRESULT StartUDP(CRefCountedPtr< CStunServer > &spServer, CStunServerConfig &config)
std::string strAlternateAdvertised
void WaitForAppExitSignal()
CSocketAddress addrPrimaryAdvertised
void DumpStartupArgs(StartupArgs &args)
bool HasAtLeastTwoAdapters(int family)
HRESULT AddOption(const char *pszName, int has_arg, std::string *pStrResult)
bool fEnableDosProtection
std::string strConfigFile
std::string strDosProtect
static HRESULT CreateInstance(I **ppI)
HRESULT ResolveAdapterName(bool fPrimary, int family, std::string &strAdapterName, uint16_t port, CSocketAddress *pAddr)
bool IsIPAddressZero() const
#define ChkIfA(expr, hrerror)
void PrettyPrint(const char *pszInput, size_t width)
HRESULT BlockSignal(int sig)
#define HRESULT_FACILITY(hr)