提交 a8037768 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

New single thread design begins.

上级 eb1115e9
...@@ -48,7 +48,6 @@ SYSTEM_LIBS := pthread ...@@ -48,7 +48,6 @@ SYSTEM_LIBS := pthread
# External libraries with pkg-config support. # External libraries with pkg-config support.
PKG_LIBS := PKG_LIBS :=
PKG_LIBS += libuv >= 1.6.1 PKG_LIBS += libuv >= 1.6.1
PKG_LIBS += libconfig++ >= 1.4.8
PKG_LIBS += openssl >= 1.0.1 PKG_LIBS += openssl >= 1.0.1
PKG_LIBS += libsrtp >= 1.5.2 PKG_LIBS += libsrtp >= 1.5.2
......
#ifndef MS_CONTROL_PROTOCOL_MESSAGE_H
#define MS_CONTROL_PROTOCOL_MESSAGE_H
#include "common.h"
namespace ControlProtocol
{
class Message
{
public:
enum class Kind
{
Request = 1,
Response,
Notification
};
public:
Message(Kind);
virtual ~Message();
Kind GetKind();
virtual bool IsValid(); // TODO: Must be pure virtual.
void SetTransaction(int32_t transaction);
int32_t GetTransaction();
virtual void Dump() = 0;
protected:
Kind kind;
int32_t transaction = 0;
};
/* Inlime methods. */
inline
Message::Kind Message::GetKind()
{
return this->kind;
}
inline
void Message::SetTransaction(int32_t transaction)
{
this->transaction = transaction;
}
inline
int32_t Message::GetTransaction()
{
return this->transaction;
}
} // namespace ControlProtocol
#endif
#ifndef MS_CONTROL_PROTOCOL_PARSER_H
#define MS_CONTROL_PROTOCOL_PARSER_H
#include "common.h"
#include "ControlProtocol/Message.h"
namespace ControlProtocol
{
class Parser
{
public:
Parser();
virtual ~Parser();
/**
* Parses the given buffer.
*
* @param buffer The buffer to parse.
* @param len The lenght of the buffer.
*
* @return Returns a pointer to ControlProtocol::Message if parsing succeded,
* or nullptr if the message is not completed or parsing failed.
*/
Message* Parse(const MS_BYTE* buffer, size_t len);
/**
* Checks whether the ongoing parsing has error or not.
*
* @return true if there is parsing error, false otherwise.
*/
bool HasError();
/**
* Get the length of the complete or uncomplete message parsed or being parsed.
*
* @return The number of chars parsed in the current message.
*/
size_t GetParsedLen();
/**
* Resets the parser internals. Must be called before parsing a new message
* (regardless the previous one succeded or not).
*/
void Reset();
/**
* Prints current status of the parser.
*/
void Dump();
protected:
size_t parsedLen;
private:
// Allocated by this:
ControlProtocol::Message* msg = nullptr;
// Others:
size_t cs = 0; // Needed for Ragel.
size_t mark = 0;
};
} // namespace ControlProtocol
#endif
#ifndef MS_CONTROL_PROTOCOL_REQUEST_H
#define MS_CONTROL_PROTOCOL_REQUEST_H
#include "ControlProtocol/Message.h"
namespace ControlProtocol
{
class Request : public ControlProtocol::Message
{
public:
enum class Type
{
Hello = 1,
Authenticate,
CreateConference
};
public:
Request(Type);
virtual ~Request();
virtual void Dump() = 0;
public:
// Allow fast access to type.
Type type;
};
} // namespace ControlProtocol
#endif
#ifndef MS_CONTROL_PROTOCOL_TCP_CONNECTION_H
#define MS_CONTROL_PROTOCOL_TCP_CONNECTION_H
#include "common.h"
#include "handles/TCPConnection.h"
#include "ControlProtocol/Parser.h"
#include "ControlProtocol/Message.h"
namespace ControlProtocol
{
class TCPConnection : public ::TCPConnection
{
public:
class Reader
{
public:
virtual void onControlProtocolMessage(ControlProtocol::TCPConnection* connection, ControlProtocol::Message* msg, const MS_BYTE* raw, size_t len) = 0;
};
public:
TCPConnection(Reader* reader, size_t bufferSize);
virtual ~TCPConnection();
/* Pure virtual methods inherited from ::TCPConnection. */
public:
virtual void userOnTCPConnectionRead(const MS_BYTE* data, size_t len) override;
private:
// Allocated by this:
ControlProtocol::Parser* parser = nullptr;
// Passed by argument:
Reader* reader = nullptr;
// Others:
size_t msgStart = 0; // Where the latest message starts.
};
} // namespace ControlProtocol
#endif
#ifndef MS_CONTROL_PROTOCOL_TCP_SERVER_H
#define MS_CONTROL_PROTOCOL_TCP_SERVER_H
#include "common.h"
#include "handles/TCPServer.h"
#include "handles/TCPConnection.h"
#include "ControlProtocol/TCPConnection.h"
#include "ControlProtocol/Message.h"
#include <string>
namespace ControlProtocol
{
class TCPServer : public ::TCPServer
{
public:
class Listener
{
public:
virtual void onControlProtocolNewTCPConnection(ControlProtocol::TCPServer* tcpServer, ControlProtocol::TCPConnection* connection) = 0;
virtual void onControlProtocolTCPConnectionClosed(ControlProtocol::TCPServer* tcpServer, ControlProtocol::TCPConnection* connection, bool is_closed_by_peer) = 0;
};
public:
TCPServer(Listener* listener, ControlProtocol::TCPConnection::Reader* reader, const std::string &ip, MS_PORT port);
/* Pure virtual methods inherited from ::TCPServer. */
public:
virtual void userOnTCPConnectionAlloc(::TCPConnection** connection) override;
virtual void userOnNewTCPConnection(::TCPConnection* connection) override;
virtual void userOnTCPConnectionClosed(::TCPConnection* connection, bool is_closed_by_peer) override;
virtual void userOnTCPServerClosed() override;
private:
// Passed by argument:
Listener* listener = nullptr;
ControlProtocol::TCPConnection::Reader* reader = nullptr;
};
} // namespace ControlProtocol
#endif
#ifndef MS_CONTROL_PROTOCOL_UNIX_STREAM_SOCKET_H
#define MS_CONTROL_PROTOCOL_UNIX_STREAM_SOCKET_H
#include "common.h"
#include "handles/UnixStreamSocket.h"
#include "ControlProtocol/Parser.h"
#include "ControlProtocol/Message.h"
namespace ControlProtocol
{
class UnixStreamSocket : public ::UnixStreamSocket
{
public:
class Listener
{
public:
virtual void onControlProtocolMessage(ControlProtocol::UnixStreamSocket* unixSocket, ControlProtocol::Message* msg, const MS_BYTE* raw, size_t len) = 0;
virtual void onControlProtocolUnixStreamSocketClosed(ControlProtocol::UnixStreamSocket* unixSocket, bool is_closed_by_peer) = 0;
};
public:
UnixStreamSocket(Listener* listener, int fd);
virtual ~UnixStreamSocket();
/* Pure virtual methods inherited from ::UnixStreamSocket. */
public:
virtual void userOnUnixStreamRead(const MS_BYTE* data, size_t len) override;
virtual void userOnUnixStreamSocketClosed(bool is_closed_by_peer) override;
private:
// Allocated by this:
ControlProtocol::Parser* parser = nullptr;
// Passed by argument:
Listener* listener = nullptr;
// Others:
size_t msgStart = 0; // Where the latest message starts.
};
} // namespace ControlProtocol
#endif
#ifndef MS_CONTROL_PROTOCOL_MESSAGES_H
#define MS_CONTROL_PROTOCOL_MESSAGES_H
#include "messages/RequestHello.h"
#include "messages/RequestAuthenticate.h"
#include "messages/RequestCreateConference.h"
#endif
#ifndef MS_CONTROL_PROTOCOL_REQUEST_AUTHENTICATE_H
#define MS_CONTROL_PROTOCOL_REQUEST_AUTHENTICATE_H
#include "ControlProtocol/Request.h"
#include <string>
namespace ControlProtocol
{
class RequestAuthenticate : public ControlProtocol::Request
{
public:
RequestAuthenticate();
virtual ~RequestAuthenticate();
void SetUser(const char* str, size_t len);
void SetPasswd(const char* str, size_t len);
virtual void Dump() override;
private:
std::string user;
std::string passwd;
};
} // namespace ControlProtocol
#endif
#ifndef MS_CONTROL_PROTOCOL_REQUEST_CREATE_CONFERENCE_H
#define MS_CONTROL_PROTOCOL_REQUEST_CREATE_CONFERENCE_H
#include "ControlProtocol/Request.h"
namespace ControlProtocol
{
class RequestCreateConference : public ControlProtocol::Request
{
public:
RequestCreateConference();
virtual ~RequestCreateConference();
virtual void Dump() override;
};
} // namespace ControlProtocol
#endif
#ifndef MS_CONTROL_PROTOCOL_REQUEST_HELLO_H
#define MS_CONTROL_PROTOCOL_REQUEST_HELLO_H
#include "ControlProtocol/Request.h"
namespace ControlProtocol
{
class RequestHello : public ControlProtocol::Request
{
public:
RequestHello();
virtual ~RequestHello();
virtual void Dump() override;
};
} // namespace ControlProtocol
#endif
#ifndef MS_DAEMON_H
#define MS_DAEMON_H
class Daemon
{
public:
static void Daemonize();
static void SendOKStatusToAncestor();
static void SendErrorStatusToAncestor();
static bool IsDaemonized();
static void End();
private:
static bool WritePIDFile();
static void DeletePIDFile();
static bool WaitForDaemonizedStatus(char* status);
static void SendStatusToAncestor(char status);
private:
static int statusPipe[2];
static bool isDaemonized;
static bool hasWrittenPIDFile;
};
#endif
#ifndef MS_DEP_LIBUV_H #ifndef MS_DEP_LIBUV_H
#define MS_DEP_LIBUV_H #define MS_DEP_LIBUV_H
#include "common.h"
#include <uv.h> #include <uv.h>
class DepLibUV class DepLibUV
{ {
public: public:
static void ClassInit(); static void ClassInit();
static void ThreadInit(); static void ClassDestroy();
static void ThreadDestroy();
static uv_loop_t* GetLoop();
static void RunLoop(); static void RunLoop();
static uv_loop_t* GetLoop();
private: private:
static __thread uv_loop_t* loop; static uv_loop_t* loop;
}; };
/* Inline static methods. */ /* Inline static methods. */
......
#ifndef MS_DISPATCHER_H
#define MS_DISPATCHER_H
#include "common.h"
#include "handles/SignalsHandler.h"
#include "ControlProtocol/TCPServer.h"
#include "ControlProtocol/TCPConnection.h"
#include "ControlProtocol/UnixStreamSocket.h"
#include "ControlProtocol/Message.h"
#include <vector>
class Dispatcher :
public SignalsHandler::Listener,
public ControlProtocol::TCPServer::Listener,
public ControlProtocol::TCPConnection::Reader,
public ControlProtocol::UnixStreamSocket::Listener
{
public:
Dispatcher();
virtual ~Dispatcher();
void Close();
/* Methods inherited from SignalsHandler::Listener. */
public:
virtual void onSignal(SignalsHandler* signalsHandler, int signum) override;
virtual void onSignalsHandlerClosed(SignalsHandler* signalsHandler) override;
/* Methods inherited from ControlProtocol::TCPServer::Listener. */
public:
virtual void onControlProtocolNewTCPConnection(ControlProtocol::TCPServer* tcpServer, ControlProtocol::TCPConnection* connection) override;
virtual void onControlProtocolTCPConnectionClosed(ControlProtocol::TCPServer* tcpServer, ControlProtocol::TCPConnection* connection, bool is_closed_by_peer) override;
/* Methods inherited from ControlProtocol::TCPConnection::Reader. */
public:
virtual void onControlProtocolMessage(ControlProtocol::TCPConnection* connection, ControlProtocol::Message* msg, const MS_BYTE* raw, size_t len) override;
/* Methods inherited from ControlProtocol::UnixStreamSocket::Listener. */
public:
virtual void onControlProtocolMessage(ControlProtocol::UnixStreamSocket* unixSocket, ControlProtocol::Message* msg, const MS_BYTE* raw, size_t len) override;
virtual void onControlProtocolUnixStreamSocketClosed(ControlProtocol::UnixStreamSocket* unixSocket, bool is_closed_by_peer) override;
private:
// Allocated by this:
SignalsHandler* signalsHandler = nullptr;
ControlProtocol::TCPServer* controlProtocolTCPServer = nullptr;
typedef std::vector<ControlProtocol::UnixStreamSocket*> ControlProtocolUnixStreamSockets;
ControlProtocolUnixStreamSockets controlProtocolUnixStreamSockets;
// Others:
bool closed = false;
};
#endif
...@@ -50,34 +50,30 @@ extern "C" ...@@ -50,34 +50,30 @@ extern "C"
/* /*
* This macro must point to a unsigned int function, method or variable that * This macro must point to a unsigned int function, method or variable that
* returns Syslog logging level. * returns the Syslog logging level.
*/ */
#define MS_GET_LOG_LEVEL Settings::configuration.logLevel #define MS_GET_LOG_LEVEL Settings::configuration.logLevel
class Logger class Logger
{ {
public: public:
static void ThreadInit(const std::string name); static void Init(const std::string name);
static const char* GetThreadName(); static const char* GetProcessName();
static void EnableSyslog(); static void EnableSyslog();
static bool IsSyslogEnabled(); static bool IsSyslogEnabled();
static bool HasDebugLevel(); static bool HasDebugLevel();
private: private:
// NOTE: static std::string processName;
// __thread implemented in GCC >= 4.7 and Clang.
// local_thread implemented in GCC >= 4.8.
static __thread std::string* threadName;
static __thread const char* threadNamePtr;
static bool isSyslogEnabled; static bool isSyslogEnabled;
}; };
/* Inline static methods. */ /* Inline static methods. */
inline inline
const char* Logger::GetThreadName() const char* Logger::GetProcessName()
{ {
return Logger::threadNamePtr; return Logger::processName.c_str();
} }
inline inline
...@@ -98,11 +94,11 @@ bool Logger::HasDebugLevel() ...@@ -98,11 +94,11 @@ bool Logger::HasDebugLevel()
#define _MS_LOG_STR "[%s] %s:%d | %s::%s()" #define _MS_LOG_STR "[%s] %s:%d | %s::%s()"
#define _MS_LOG_STR_DESC _MS_LOG_STR " | " #define _MS_LOG_STR_DESC _MS_LOG_STR " | "
#define _MS_FILE (std::strchr(__FILE__, '/') ? std::strchr(__FILE__, '/') + 1 : __FILE__) #define _MS_FILE (std::strchr(__FILE__, '/') ? std::strchr(__FILE__, '/') + 1 : __FILE__)
#define _MS_LOG_ARG Logger::GetThreadName(), _MS_FILE, __LINE__, MS_CLASS, __FUNCTION__ #define _MS_LOG_ARG Logger::GetProcessName(), _MS_FILE, __LINE__, MS_CLASS, __FUNCTION__
#else #else
#define _MS_LOG_STR "[%s] %s::%s()" #define _MS_LOG_STR "[%s] %s::%s()"
#define _MS_LOG_STR_DESC _MS_LOG_STR " | " #define _MS_LOG_STR_DESC _MS_LOG_STR " | "
#define _MS_LOG_ARG Logger::GetThreadName(), MS_CLASS, __FUNCTION__ #define _MS_LOG_ARG Logger::GetProcessName(), MS_CLASS, __FUNCTION__
#endif #endif
#define _MS_TO_STDOUT(prefix) \ #define _MS_TO_STDOUT(prefix) \
......
#ifndef MS_WORKER_H
#define MS_WORKER_H
#include "common.h"
#include "handles/SignalsHandler.h"
class Loop :
public SignalsHandler::Listener
{
public:
Loop();
~Loop();
// TODO: IMHO this can be private.
void Close();
/* Methods inherited from SignalsHandler::Listener. */
public:
virtual void onSignal(SignalsHandler* signalsHandler, int signum) override;
virtual void onSignalsHandlerClosed(SignalsHandler* signalsHandler) override;
private:
// Allocated by this.
SignalsHandler* signalsHandler = nullptr;
// Others.
bool closed = false;
};
#endif
#ifndef MS_MEDIASOUP_H
#define MS_MEDIASOUP_H
/*
* MediaSoup top class.
*
* Entry point of the application.
*/
class MediaSoup
{
public:
// Set the process.
static void SetProcess();
// Run MediaSoup.
static void Run();
// Stop MediaSoup.
static void End();
private:
static void IgnoreSignals();
static void SetKernelLimits();
static void SetUserGroup();
static void ThreadInit();
static void ThreadDestroy();
static void ClassInit();
static void ClassDestroy();
static void RunWorkerThread(void* data);
};
#endif
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
namespace RTC namespace RTC
{ {
class UDPSocket : public ::UDPSocket class UDPSocket :
public ::UDPSocket
{ {
public: public:
class Listener class Listener
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
#include "common.h" #include "common.h"
#include <map> #include <map>
#include <string> #include <string>
#include <vector>
#include <libconfig.h++>
extern "C" extern "C"
{ {
#include <syslog.h> #include <syslog.h>
...@@ -14,28 +12,11 @@ extern "C" ...@@ -14,28 +12,11 @@ extern "C"
class Settings class Settings
{ {
public: public:
// Struct holding command line arguments. // Struct holding the configuration.
struct Arguments
{
std::string configFile;
bool daemonize { false };
std::string pidFile;
std::string user;
std::string group;
};
// Struct holding the configuration (can be overriden by a config file).
struct Configuration struct Configuration
{ {
unsigned int logLevel { LOG_DEBUG }; unsigned int logLevel { LOG_DEBUG };
unsigned int syslogFacility { LOG_USER }; unsigned int syslogFacility { LOG_USER };
int numWorkers { 0 };
struct
{
std::string listenIP { "127.0.0.1" };
MS_PORT listenPort { 4080 };
} ControlProtocol;
struct struct
{ {
...@@ -52,32 +33,19 @@ public: ...@@ -52,32 +33,19 @@ public:
}; };
public: public:
static void ReadArguments(int argc, char* argv[]); static void SetConfiguration(int argc, char* argv[]);
static void PrintHelp(bool error);
static void PrintVersion();
static void SetDefaultConfiguration();
static void ReadConfigurationFile();
static void ConfigurationPostCheck();
static bool ReloadConfigurationFile();
static void PrintConfiguration(); static void PrintConfiguration();
private: private:
static libconfig::Config* ParseConfigFile(); static void SetDefaultRtcListenIP(int requested_family);
static void SetLogLevel(std::string &level); static void SetLogLevel(std::string &level);
static void SetSyslogFacility(std::string &facility); static void SetSyslogFacility(std::string &facility);
static void SetNumWorkers(int numWorkers); static void SetRtcListenIPv4(const std::string &ip);
static void SetDefaultNumWorkers(); static void SetRtcListenIPv6(const std::string &ip);
static void SetControlProtocolListenIP(const std::string &ip); static void SetRtcPorts();
static void SetControlProtocolListenPort(MS_PORT port); static void SetDtlsCertificateAndPrivateKeyFiles();
static void SetRTClistenIPv4(const std::string &ip);
static void SetRTClistenIPv6(const std::string &ip);
static void SetDefaultRTClistenIP(int requested_family);
static bool IsBindableIP(const std::string &ip, int family, int* _bind_err);
static void SetRTCports(MS_PORT minPort, MS_PORT maxPort);
static void SetDtlsCertificateAndPrivateKeyFiles(std::string& dtlsCertificateFile, std::string& dtlsPrivateKeyFile);
public: public:
static struct Arguments arguments;
static struct Configuration configuration; static struct Configuration configuration;
private: private:
......
...@@ -217,17 +217,17 @@ namespace Utils ...@@ -217,17 +217,17 @@ namespace Utils
class Crypto class Crypto
{ {
public: public:
static void ThreadInit(); static void ClassInit();
static void ThreadDestroy(); static void ClassDestroy();
static unsigned int GetRandomUInt(unsigned int min, unsigned int max); static unsigned int GetRandomUInt(unsigned int min, unsigned int max);
static const char* GetRandomHexString(char* str, size_t len); static const char* GetRandomHexString(char* str, size_t len);
static MS_4BYTES CRC32(const MS_BYTE* data, size_t size); static MS_4BYTES GetCRC32(const MS_BYTE* data, size_t size);
static const MS_BYTE* HMAC_SHA1(const std::string &key, const MS_BYTE* data, size_t len); static const MS_BYTE* GetHMAC_SHA1(const std::string &key, const MS_BYTE* data, size_t len);
private: private:
static __thread unsigned int seed; static unsigned int seed;
static __thread HMAC_CTX hmacSha1Ctx; static HMAC_CTX hmacSha1Ctx;
static __thread MS_BYTE hmacSha1Buffer[]; static MS_BYTE hmacSha1Buffer[];
static const MS_4BYTES crc32Table[256]; static const MS_4BYTES crc32Table[256];
}; };
...@@ -262,7 +262,7 @@ namespace Utils ...@@ -262,7 +262,7 @@ namespace Utils
} }
inline inline
MS_4BYTES Crypto::CRC32(const MS_BYTE* data, size_t size) MS_4BYTES Crypto::GetCRC32(const MS_BYTE* data, size_t size)
{ {
MS_4BYTES crc = 0xFFFFFFFF; MS_4BYTES crc = 0xFFFFFFFF;
const MS_BYTE* p = data; const MS_BYTE* p = data;
...@@ -272,6 +272,6 @@ namespace Utils ...@@ -272,6 +272,6 @@ namespace Utils
return crc ^ ~0U; return crc ^ ~0U;
} }
} // namespace Utils }
#endif #endif
#ifndef MS_VERSION_H
#define MS_VERSION_H
#include <string>
class Version
{
public:
static const std::string GetVersion();
static const std::string GetNameAndVersion();
public:
static const int versionMajor;
static const int versionMinor;
static const int versionTiny;
static const std::string name;
static const std::string command;
static const std::string copyright;
};
#endif
#ifndef MS_WORKER_H
#define MS_WORKER_H
#include "common.h"
#include "ControlProtocol/UnixStreamSocket.h"
#include "ControlProtocol/Message.h"
#include <map>
#include <atomic>
// TMP
#include "RTC/Room.h"
class Worker :
public ControlProtocol::UnixStreamSocket::Listener
{
public:
static void ThreadInit(int workerId);
static void ThreadDestroy();
static void SetWorker(int workerId, Worker* worker);
static int CountWorkers();
static bool AreAllWorkersRunning();
static int GetControlProtocolRemoteSocket(int workerId);
private:
typedef std::map <int, Worker*> Workers;
static Workers workers;
static int numWorkers;
static std::atomic<int> numWorkersRunning;
public:
Worker(int workerId);
~Worker();
void Close();
int GetControlProtocolRemoteSocket();
private:
void BlockSignals();
/* Methods inherited from ControlProtocol::UnixStreamSocket::Listener. */
public:
virtual void onControlProtocolMessage(ControlProtocol::UnixStreamSocket* unixSocket, ControlProtocol::Message* msg, const MS_BYTE* raw, size_t len) override;
virtual void onControlProtocolUnixStreamSocketClosed(ControlProtocol::UnixStreamSocket* unixSocket, bool is_closed_by_peer) override;
private:
// Allocated by this:
ControlProtocol::UnixStreamSocket* controlProtocolUnixStreamSocket = nullptr;
// Others:
int workerId = 0;
int controlProtocolRemoteSocket = 0;
// TMP
RTC::Room* room = nullptr;
};
#endif
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include <sys/socket.h> // struct sockaddr, struct sockaddr_storage, AF_INET, AF_INET6 #include <sys/socket.h> // struct sockaddr, struct sockaddr_storage, AF_INET, AF_INET6
#include <netinet/in.h> // sockaddr_in, sockaddr_in6 #include <netinet/in.h> // sockaddr_in, sockaddr_in6
#include <arpa/inet.h> // htonl(), htons(), ntohl(), ntohs() #include <arpa/inet.h> // htonl(), htons(), ntohl(), ntohs()
#include <pthread.h> // __thread
#define MS_BYTE uint8_t #define MS_BYTE uint8_t
#define MS_2BYTES uint16_t #define MS_2BYTES uint16_t
......
#define MS_CLASS "ControlProtocol::Message"
#include "ControlProtocol/Message.h"
#include "Logger.h"
namespace ControlProtocol
{
/* Instance methods. */
Message::Message(Kind kind) :
kind(kind)
{
MS_TRACE();
}
Message::~Message()
{
MS_TRACE();
}
// TMP: must be pure virtual.
bool Message::IsValid()
{
MS_TRACE();
return true;
}
} // namespace ControlProtocol
#line 1 "src/ControlProtocol/Parser.rl"
#define MS_CLASS "ControlProtocol::Parser"
#include "ControlProtocol/Parser.h"
#include "ControlProtocol/messages.h"
#include "Logger.h"
#include <cstdlib> // std::atol()
#define MARK(M, P) (M = (P) - buffer)
#define LEN(AT, P) ((P) - buffer - (AT))
#define LEN_FROM_MARK (p - buffer - this->mark)
#define PTR_TO(F) (buffer + (F))
#define PTR_TO_MARK (const char*)(buffer + this->mark)
namespace ControlProtocol
{
/**
* Ragel: machine definition
*/
#line 57 "src/ControlProtocol/Parser.rl"
/**
* Ragel: %%write data
* This generates Ragel's static variables such as:
* static const int MessageParser_start
*/
#line 33 "src/ControlProtocol/Parser.cpp"
static const int MessageParser_start = 1;
static const int MessageParser_first_final = 90;
static const int MessageParser_error = 0;
#line 65 "src/ControlProtocol/Parser.rl"
Parser::Parser()
{
MS_TRACE();
Reset();
}
Parser::~Parser()
{
MS_TRACE();
}
Message* Parser::Parse(const MS_BYTE* buffer, size_t len)
{
MS_TRACE();
// Used by Ragel:
const unsigned char* p;
const unsigned char* pe;
// const unsigned char* eof;
MS_ASSERT(this->parsedLen <= len, "parsedLen pasts end of buffer");
p = (const unsigned char*)buffer + this->parsedLen;
pe = (const unsigned char*)buffer + len;
// eof = nullptr; // Ragel manual pag 36 (eof variable).
/**
* Ragel: %%write exec
* This updates cs variable needed by Ragel (so this->cs at the end) and p.
*/
#line 75 "src/ControlProtocol/Parser.cpp"
{
if ( p == pe )
goto _test_eof;
switch ( cs )
{
case 1:
if ( (*p) == 82u )
goto st2;
goto st0;
st0:
cs = 0;
goto _out;
st2:
if ( ++p == pe )
goto _test_eof2;
case 2:
if ( (*p) == 101u )
goto st3;
goto st0;
st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
if ( (*p) == 113u )
goto st4;
goto st0;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
if ( (*p) == 117u )
goto st5;
goto st0;
st5:
if ( ++p == pe )
goto _test_eof5;
case 5:
if ( (*p) == 101u )
goto st6;
goto st0;
st6:
if ( ++p == pe )
goto _test_eof6;
case 6:
if ( (*p) == 115u )
goto st7;
goto st0;
st7:
if ( ++p == pe )
goto _test_eof7;
case 7:
if ( (*p) == 116u )
goto st8;
goto st0;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
if ( (*p) == 32u )
goto st9;
goto st0;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
switch( (*p) ) {
case 65u: goto st10;
case 67u: goto st56;
case 72u: goto st83;
}
goto st0;
st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
if ( (*p) == 117u )
goto st11;
goto st0;
st11:
if ( ++p == pe )
goto _test_eof11;
case 11:
if ( (*p) == 116u )
goto st12;
goto st0;
st12:
if ( ++p == pe )
goto _test_eof12;
case 12:
if ( (*p) == 104u )
goto st13;
goto st0;
st13:
if ( ++p == pe )
goto _test_eof13;
case 13:
if ( (*p) == 101u )
goto st14;
goto st0;
st14:
if ( ++p == pe )
goto _test_eof14;
case 14:
if ( (*p) == 110u )
goto st15;
goto st0;
st15:
if ( ++p == pe )
goto _test_eof15;
case 15:
if ( (*p) == 116u )
goto st16;
goto st0;
st16:
if ( ++p == pe )
goto _test_eof16;
case 16:
if ( (*p) == 105u )
goto st17;
goto st0;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
if ( (*p) == 99u )
goto st18;
goto st0;
st18:
if ( ++p == pe )
goto _test_eof18;
case 18:
if ( (*p) == 97u )
goto st19;
goto st0;
st19:
if ( ++p == pe )
goto _test_eof19;
case 19:
if ( (*p) == 116u )
goto st20;
goto st0;
st20:
if ( ++p == pe )
goto _test_eof20;
case 20:
if ( (*p) == 101u )
goto st21;
goto st0;
st21:
if ( ++p == pe )
goto _test_eof21;
case 21:
if ( (*p) == 13u )
goto st22;
goto st0;
st22:
if ( ++p == pe )
goto _test_eof22;
case 22:
if ( (*p) == 10u )
goto st23;
goto st0;
st23:
if ( ++p == pe )
goto _test_eof23;
case 23:
if ( 48u <= (*p) && (*p) <= 57u )
goto tr25;
goto st0;
tr25:
#line 5 "src/ControlProtocol/grammar/RequestAuthenticate.rl"
{
this->msg = new ControlProtocol::RequestAuthenticate();
}
#line 26 "src/ControlProtocol/Parser.rl"
{
MARK(this->mark, p);
}
goto st24;
st24:
if ( ++p == pe )
goto _test_eof24;
case 24:
#line 259 "src/ControlProtocol/Parser.cpp"
if ( (*p) == 13u )
goto tr26;
if ( 48u <= (*p) && (*p) <= 57u )
goto st48;
goto st0;
tr26:
#line 31 "src/ControlProtocol/Parser.rl"
{
int32_t transaction = std::atol(PTR_TO_MARK);
this->msg->SetTransaction(transaction);
}
goto st25;
st25:
if ( ++p == pe )
goto _test_eof25;
case 25:
#line 276 "src/ControlProtocol/Parser.cpp"
if ( (*p) == 10u )
goto st26;
goto st0;
st26:
if ( ++p == pe )
goto _test_eof26;
case 26:
if ( (*p) == 117u )
goto st27;
goto st0;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
if ( (*p) == 115u )
goto st28;
goto st0;
st28:
if ( ++p == pe )
goto _test_eof28;
case 28:
if ( (*p) == 101u )
goto st29;
goto st0;
st29:
if ( ++p == pe )
goto _test_eof29;
case 29:
if ( (*p) == 114u )
goto st30;
goto st0;
st30:
if ( ++p == pe )
goto _test_eof30;
case 30:
if ( (*p) == 58u )
goto st31;
goto st0;
st31:
if ( ++p == pe )
goto _test_eof31;
case 31:
if ( (*p) == 32u )
goto st32;
goto st0;
st32:
if ( ++p == pe )
goto _test_eof32;
case 32:
if ( (*p) == 33u )
goto tr35;
if ( (*p) < 45u ) {
if ( (*p) > 39u ) {
if ( 42u <= (*p) && (*p) <= 43u )
goto tr35;
} else if ( (*p) >= 35u )
goto tr35;
} else if ( (*p) > 46u ) {
if ( (*p) < 65u ) {
if ( 48u <= (*p) && (*p) <= 57u )
goto tr35;
} else if ( (*p) > 90u ) {
if ( 94u <= (*p) && (*p) <= 126u )
goto tr35;
} else
goto tr35;
} else
goto tr35;
goto st0;
tr35:
#line 26 "src/ControlProtocol/Parser.rl"
{
MARK(this->mark, p);
}
goto st33;
st33:
if ( ++p == pe )
goto _test_eof33;
case 33:
#line 356 "src/ControlProtocol/Parser.cpp"
switch( (*p) ) {
case 13u: goto tr36;
case 33u: goto st33;
}
if ( (*p) < 45u ) {
if ( (*p) > 39u ) {
if ( 42u <= (*p) && (*p) <= 43u )
goto st33;
} else if ( (*p) >= 35u )
goto st33;
} else if ( (*p) > 46u ) {
if ( (*p) < 65u ) {
if ( 48u <= (*p) && (*p) <= 57u )
goto st33;
} else if ( (*p) > 90u ) {
if ( 94u <= (*p) && (*p) <= 126u )
goto st33;
} else
goto st33;
} else
goto st33;
goto st0;
tr36:
#line 10 "src/ControlProtocol/grammar/RequestAuthenticate.rl"
{
static_cast<RequestAuthenticate*>(this->msg)->SetUser(PTR_TO_MARK, LEN_FROM_MARK);
}
goto st34;
st34:
if ( ++p == pe )
goto _test_eof34;
case 34:
#line 389 "src/ControlProtocol/Parser.cpp"
if ( (*p) == 10u )
goto st35;
goto st0;
st35:
if ( ++p == pe )
goto _test_eof35;
case 35:
if ( (*p) == 112u )
goto st36;
goto st0;
st36:
if ( ++p == pe )
goto _test_eof36;
case 36:
if ( (*p) == 97u )
goto st37;
goto st0;
st37:
if ( ++p == pe )
goto _test_eof37;
case 37:
if ( (*p) == 115u )
goto st38;
goto st0;
st38:
if ( ++p == pe )
goto _test_eof38;
case 38:
if ( (*p) == 115u )
goto st39;
goto st0;
st39:
if ( ++p == pe )
goto _test_eof39;
case 39:
if ( (*p) == 119u )
goto st40;
goto st0;
st40:
if ( ++p == pe )
goto _test_eof40;
case 40:
if ( (*p) == 100u )
goto st41;
goto st0;
st41:
if ( ++p == pe )
goto _test_eof41;
case 41:
if ( (*p) == 58u )
goto st42;
goto st0;
st42:
if ( ++p == pe )
goto _test_eof42;
case 42:
if ( (*p) == 32u )
goto st43;
goto st0;
st43:
if ( ++p == pe )
goto _test_eof43;
case 43:
if ( (*p) == 33u )
goto tr47;
if ( (*p) < 45u ) {
if ( (*p) > 39u ) {
if ( 42u <= (*p) && (*p) <= 43u )
goto tr47;
} else if ( (*p) >= 35u )
goto tr47;
} else if ( (*p) > 46u ) {
if ( (*p) < 65u ) {
if ( 48u <= (*p) && (*p) <= 57u )
goto tr47;
} else if ( (*p) > 90u ) {
if ( 94u <= (*p) && (*p) <= 126u )
goto tr47;
} else
goto tr47;
} else
goto tr47;
goto st0;
tr47:
#line 26 "src/ControlProtocol/Parser.rl"
{
MARK(this->mark, p);
}
goto st44;
st44:
if ( ++p == pe )
goto _test_eof44;
case 44:
#line 483 "src/ControlProtocol/Parser.cpp"
switch( (*p) ) {
case 13u: goto tr48;
case 33u: goto st44;
}
if ( (*p) < 45u ) {
if ( (*p) > 39u ) {
if ( 42u <= (*p) && (*p) <= 43u )
goto st44;
} else if ( (*p) >= 35u )
goto st44;
} else if ( (*p) > 46u ) {
if ( (*p) < 65u ) {
if ( 48u <= (*p) && (*p) <= 57u )
goto st44;
} else if ( (*p) > 90u ) {
if ( 94u <= (*p) && (*p) <= 126u )
goto st44;
} else
goto st44;
} else
goto st44;
goto st0;
tr78:
#line 31 "src/ControlProtocol/Parser.rl"
{
int32_t transaction = std::atol(PTR_TO_MARK);
this->msg->SetTransaction(transaction);
}
goto st45;
tr48:
#line 15 "src/ControlProtocol/grammar/RequestAuthenticate.rl"
{
static_cast<RequestAuthenticate*>(this->msg)->SetPasswd(PTR_TO_MARK, LEN_FROM_MARK);
}
goto st45;
st45:
if ( ++p == pe )
goto _test_eof45;
case 45:
#line 523 "src/ControlProtocol/Parser.cpp"
if ( (*p) == 10u )
goto st46;
goto st0;
st46:
if ( ++p == pe )
goto _test_eof46;
case 46:
if ( (*p) == 13u )
goto st47;
goto st0;
st47:
if ( ++p == pe )
goto _test_eof47;
case 47:
if ( (*p) == 10u )
goto tr52;
goto st0;
tr52:
#line 37 "src/ControlProtocol/Parser.rl"
{
{p++; cs = 90; goto _out;}
}
goto st90;
st90:
if ( ++p == pe )
goto _test_eof90;
case 90:
#line 551 "src/ControlProtocol/Parser.cpp"
goto st0;
st48:
if ( ++p == pe )
goto _test_eof48;
case 48:
if ( (*p) == 13u )
goto tr26;
if ( 48u <= (*p) && (*p) <= 57u )
goto st49;
goto st0;
st49:
if ( ++p == pe )
goto _test_eof49;
case 49:
if ( (*p) == 13u )
goto tr26;
if ( 48u <= (*p) && (*p) <= 57u )
goto st50;
goto st0;
st50:
if ( ++p == pe )
goto _test_eof50;
case 50:
if ( (*p) == 13u )
goto tr26;
if ( 48u <= (*p) && (*p) <= 57u )
goto st51;
goto st0;
st51:
if ( ++p == pe )
goto _test_eof51;
case 51:
if ( (*p) == 13u )
goto tr26;
if ( 48u <= (*p) && (*p) <= 57u )
goto st52;
goto st0;
st52:
if ( ++p == pe )
goto _test_eof52;
case 52:
if ( (*p) == 13u )
goto tr26;
if ( 48u <= (*p) && (*p) <= 57u )
goto st53;
goto st0;
st53:
if ( ++p == pe )
goto _test_eof53;
case 53:
if ( (*p) == 13u )
goto tr26;
if ( 48u <= (*p) && (*p) <= 57u )
goto st54;
goto st0;
st54:
if ( ++p == pe )
goto _test_eof54;
case 54:
if ( (*p) == 13u )
goto tr26;
if ( 48u <= (*p) && (*p) <= 57u )
goto st55;
goto st0;
st55:
if ( ++p == pe )
goto _test_eof55;
case 55:
if ( (*p) == 13u )
goto tr26;
goto st0;
st56:
if ( ++p == pe )
goto _test_eof56;
case 56:
if ( (*p) == 114u )
goto st57;
goto st0;
st57:
if ( ++p == pe )
goto _test_eof57;
case 57:
if ( (*p) == 101u )
goto st58;
goto st0;
st58:
if ( ++p == pe )
goto _test_eof58;
case 58:
if ( (*p) == 97u )
goto st59;
goto st0;
st59:
if ( ++p == pe )
goto _test_eof59;
case 59:
if ( (*p) == 116u )
goto st60;
goto st0;
st60:
if ( ++p == pe )
goto _test_eof60;
case 60:
if ( (*p) == 101u )
goto st61;
goto st0;
st61:
if ( ++p == pe )
goto _test_eof61;
case 61:
if ( (*p) == 67u )
goto st62;
goto st0;
st62:
if ( ++p == pe )
goto _test_eof62;
case 62:
if ( (*p) == 111u )
goto st63;
goto st0;
st63:
if ( ++p == pe )
goto _test_eof63;
case 63:
if ( (*p) == 110u )
goto st64;
goto st0;
st64:
if ( ++p == pe )
goto _test_eof64;
case 64:
if ( (*p) == 102u )
goto st65;
goto st0;
st65:
if ( ++p == pe )
goto _test_eof65;
case 65:
if ( (*p) == 101u )
goto st66;
goto st0;
st66:
if ( ++p == pe )
goto _test_eof66;
case 66:
if ( (*p) == 114u )
goto st67;
goto st0;
st67:
if ( ++p == pe )
goto _test_eof67;
case 67:
if ( (*p) == 101u )
goto st68;
goto st0;
st68:
if ( ++p == pe )
goto _test_eof68;
case 68:
if ( (*p) == 110u )
goto st69;
goto st0;
st69:
if ( ++p == pe )
goto _test_eof69;
case 69:
if ( (*p) == 99u )
goto st70;
goto st0;
st70:
if ( ++p == pe )
goto _test_eof70;
case 70:
if ( (*p) == 101u )
goto st71;
goto st0;
st71:
if ( ++p == pe )
goto _test_eof71;
case 71:
if ( (*p) == 13u )
goto st72;
goto st0;
st72:
if ( ++p == pe )
goto _test_eof72;
case 72:
if ( (*p) == 10u )
goto st73;
goto st0;
st73:
if ( ++p == pe )
goto _test_eof73;
case 73:
if ( 48u <= (*p) && (*p) <= 57u )
goto tr77;
goto st0;
tr77:
#line 5 "src/ControlProtocol/grammar/RequestCreateConference.rl"
{
this->msg = new ControlProtocol::RequestCreateConference();
}
#line 26 "src/ControlProtocol/Parser.rl"
{
MARK(this->mark, p);
}
goto st74;
tr93:
#line 5 "src/ControlProtocol/grammar/RequestHello.rl"
{
this->msg = new ControlProtocol::RequestHello();
}
#line 26 "src/ControlProtocol/Parser.rl"
{
MARK(this->mark, p);
}
goto st74;
st74:
if ( ++p == pe )
goto _test_eof74;
case 74:
#line 773 "src/ControlProtocol/Parser.cpp"
if ( (*p) == 13u )
goto tr78;
if ( 48u <= (*p) && (*p) <= 57u )
goto st75;
goto st0;
st75:
if ( ++p == pe )
goto _test_eof75;
case 75:
if ( (*p) == 13u )
goto tr78;
if ( 48u <= (*p) && (*p) <= 57u )
goto st76;
goto st0;
st76:
if ( ++p == pe )
goto _test_eof76;
case 76:
if ( (*p) == 13u )
goto tr78;
if ( 48u <= (*p) && (*p) <= 57u )
goto st77;
goto st0;
st77:
if ( ++p == pe )
goto _test_eof77;
case 77:
if ( (*p) == 13u )
goto tr78;
if ( 48u <= (*p) && (*p) <= 57u )
goto st78;
goto st0;
st78:
if ( ++p == pe )
goto _test_eof78;
case 78:
if ( (*p) == 13u )
goto tr78;
if ( 48u <= (*p) && (*p) <= 57u )
goto st79;
goto st0;
st79:
if ( ++p == pe )
goto _test_eof79;
case 79:
if ( (*p) == 13u )
goto tr78;
if ( 48u <= (*p) && (*p) <= 57u )
goto st80;
goto st0;
st80:
if ( ++p == pe )
goto _test_eof80;
case 80:
if ( (*p) == 13u )
goto tr78;
if ( 48u <= (*p) && (*p) <= 57u )
goto st81;
goto st0;
st81:
if ( ++p == pe )
goto _test_eof81;
case 81:
if ( (*p) == 13u )
goto tr78;
if ( 48u <= (*p) && (*p) <= 57u )
goto st82;
goto st0;
st82:
if ( ++p == pe )
goto _test_eof82;
case 82:
if ( (*p) == 13u )
goto tr78;
goto st0;
st83:
if ( ++p == pe )
goto _test_eof83;
case 83:
if ( (*p) == 101u )
goto st84;
goto st0;
st84:
if ( ++p == pe )
goto _test_eof84;
case 84:
if ( (*p) == 108u )
goto st85;
goto st0;
st85:
if ( ++p == pe )
goto _test_eof85;
case 85:
if ( (*p) == 108u )
goto st86;
goto st0;
st86:
if ( ++p == pe )
goto _test_eof86;
case 86:
if ( (*p) == 111u )
goto st87;
goto st0;
st87:
if ( ++p == pe )
goto _test_eof87;
case 87:
if ( (*p) == 13u )
goto st88;
goto st0;
st88:
if ( ++p == pe )
goto _test_eof88;
case 88:
if ( (*p) == 10u )
goto st89;
goto st0;
st89:
if ( ++p == pe )
goto _test_eof89;
case 89:
if ( 48u <= (*p) && (*p) <= 57u )
goto tr93;
goto st0;
}
_test_eof2: cs = 2; goto _test_eof;
_test_eof3: cs = 3; goto _test_eof;
_test_eof4: cs = 4; goto _test_eof;
_test_eof5: cs = 5; goto _test_eof;
_test_eof6: cs = 6; goto _test_eof;
_test_eof7: cs = 7; goto _test_eof;
_test_eof8: cs = 8; goto _test_eof;
_test_eof9: cs = 9; goto _test_eof;
_test_eof10: cs = 10; goto _test_eof;
_test_eof11: cs = 11; goto _test_eof;
_test_eof12: cs = 12; goto _test_eof;
_test_eof13: cs = 13; goto _test_eof;
_test_eof14: cs = 14; goto _test_eof;
_test_eof15: cs = 15; goto _test_eof;
_test_eof16: cs = 16; goto _test_eof;
_test_eof17: cs = 17; goto _test_eof;
_test_eof18: cs = 18; goto _test_eof;
_test_eof19: cs = 19; goto _test_eof;
_test_eof20: cs = 20; goto _test_eof;
_test_eof21: cs = 21; goto _test_eof;
_test_eof22: cs = 22; goto _test_eof;
_test_eof23: cs = 23; goto _test_eof;
_test_eof24: cs = 24; goto _test_eof;
_test_eof25: cs = 25; goto _test_eof;
_test_eof26: cs = 26; goto _test_eof;
_test_eof27: cs = 27; goto _test_eof;
_test_eof28: cs = 28; goto _test_eof;
_test_eof29: cs = 29; goto _test_eof;
_test_eof30: cs = 30; goto _test_eof;
_test_eof31: cs = 31; goto _test_eof;
_test_eof32: cs = 32; goto _test_eof;
_test_eof33: cs = 33; goto _test_eof;
_test_eof34: cs = 34; goto _test_eof;
_test_eof35: cs = 35; goto _test_eof;
_test_eof36: cs = 36; goto _test_eof;
_test_eof37: cs = 37; goto _test_eof;
_test_eof38: cs = 38; goto _test_eof;
_test_eof39: cs = 39; goto _test_eof;
_test_eof40: cs = 40; goto _test_eof;
_test_eof41: cs = 41; goto _test_eof;
_test_eof42: cs = 42; goto _test_eof;
_test_eof43: cs = 43; goto _test_eof;
_test_eof44: cs = 44; goto _test_eof;
_test_eof45: cs = 45; goto _test_eof;
_test_eof46: cs = 46; goto _test_eof;
_test_eof47: cs = 47; goto _test_eof;
_test_eof90: cs = 90; goto _test_eof;
_test_eof48: cs = 48; goto _test_eof;
_test_eof49: cs = 49; goto _test_eof;
_test_eof50: cs = 50; goto _test_eof;
_test_eof51: cs = 51; goto _test_eof;
_test_eof52: cs = 52; goto _test_eof;
_test_eof53: cs = 53; goto _test_eof;
_test_eof54: cs = 54; goto _test_eof;
_test_eof55: cs = 55; goto _test_eof;
_test_eof56: cs = 56; goto _test_eof;
_test_eof57: cs = 57; goto _test_eof;
_test_eof58: cs = 58; goto _test_eof;
_test_eof59: cs = 59; goto _test_eof;
_test_eof60: cs = 60; goto _test_eof;
_test_eof61: cs = 61; goto _test_eof;
_test_eof62: cs = 62; goto _test_eof;
_test_eof63: cs = 63; goto _test_eof;
_test_eof64: cs = 64; goto _test_eof;
_test_eof65: cs = 65; goto _test_eof;
_test_eof66: cs = 66; goto _test_eof;
_test_eof67: cs = 67; goto _test_eof;
_test_eof68: cs = 68; goto _test_eof;
_test_eof69: cs = 69; goto _test_eof;
_test_eof70: cs = 70; goto _test_eof;
_test_eof71: cs = 71; goto _test_eof;
_test_eof72: cs = 72; goto _test_eof;
_test_eof73: cs = 73; goto _test_eof;
_test_eof74: cs = 74; goto _test_eof;
_test_eof75: cs = 75; goto _test_eof;
_test_eof76: cs = 76; goto _test_eof;
_test_eof77: cs = 77; goto _test_eof;
_test_eof78: cs = 78; goto _test_eof;
_test_eof79: cs = 79; goto _test_eof;
_test_eof80: cs = 80; goto _test_eof;
_test_eof81: cs = 81; goto _test_eof;
_test_eof82: cs = 82; goto _test_eof;
_test_eof83: cs = 83; goto _test_eof;
_test_eof84: cs = 84; goto _test_eof;
_test_eof85: cs = 85; goto _test_eof;
_test_eof86: cs = 86; goto _test_eof;
_test_eof87: cs = 87; goto _test_eof;
_test_eof88: cs = 88; goto _test_eof;
_test_eof89: cs = 89; goto _test_eof;
_test_eof: {}
_out: {}
}
#line 98 "src/ControlProtocol/Parser.rl"
this->parsedLen = p - (const unsigned char*)buffer;
MS_ASSERT(p <= pe, "buffer overflow after parsing execute");
MS_ASSERT(this->parsedLen <= len, "parsedLen longer than length");
MS_ASSERT(this->mark < len, "mark is after buffer end");
// Parsing succedded.
if (this->cs == (size_t)MessageParser_first_final)
{
MS_DEBUG("parsing finished OK");
MS_ASSERT(this->msg != nullptr, "parsing OK but msg is NULL");
// Check the message semantics.
if (!this->msg->IsValid())
{
MS_ERROR("invalid message");
delete this->msg;
return (this->msg = nullptr);
}
return this->msg;
}
// Parsing error.
else if (this->cs == (size_t)MessageParser_error)
{
MS_ERROR("parsing error at position %zd", this->parsedLen);
// Delete this->msg if it exists.
if (this->msg)
{
delete this->msg;
this->msg = nullptr;
}
return nullptr;
}
// Parsing not finished.
else
{
MS_DEBUG("parsing not finished");
return nullptr;
}
}
bool Parser::HasError()
{
MS_TRACE();
return (this->cs == (size_t)MessageParser_error);
}
size_t Parser::GetParsedLen()
{
MS_TRACE();
return this->parsedLen;
}
void Parser::Reset()
{
MS_TRACE();
this->parsedLen = 0;
this->msg = nullptr;
this->cs = 0;
this->mark = 0;
/**
* Ragel: %%write init
* This sets cs variable needed by Ragel (so this->cs at the end).
*/
#line 1071 "src/ControlProtocol/Parser.cpp"
{
cs = MessageParser_start;
}
#line 175 "src/ControlProtocol/Parser.rl"
}
void Parser::Dump()
{
MS_DEBUG("[cs: %zu | parsedLen: %zu | mark: %zu | error?: %s | finished?: %s]",
this->cs, this->parsedLen, this->mark,
HasError() ? "true" : "false",
(this->cs >= (size_t)MessageParser_first_final) ? "true" : "false");
}
} // namespace ControlProtocol
#define MS_CLASS "ControlProtocol::Parser"
#include "ControlProtocol/Parser.h"
#include "ControlProtocol/messages.h"
#include "Logger.h"
#include <cstdlib> // std::atol()
#define MARK(M, P) (M = (P) - buffer)
#define LEN(AT, P) ((P) - buffer - (AT))
#define LEN_FROM_MARK (p - buffer - this->mark)
#define PTR_TO(F) (buffer + (F))
#define PTR_TO_MARK (const char*)(buffer + this->mark)
namespace ControlProtocol
{
/**
* Ragel: machine definition
*/
%%{
machine MessageParser;
alphtype unsigned char;
action mark
{
MARK(this->mark, p);
}
action on_transaction
{
int32_t transaction = std::atol(PTR_TO_MARK);
this->msg->SetTransaction(transaction);
}
action on_message
{
fbreak;
}
include core_grammar "../core_grammar.rl";
transaction = ULONG >mark %on_transaction;
include RequestHello "grammar/RequestHello.rl";
include RequestAuthenticate "grammar/RequestAuthenticate.rl";
include RequestCreateConference "grammar/RequestCreateConference.rl";
Request = RequestHello |
RequestAuthenticate |
RequestCreateConference;
#Message = Request | Response | Notification;
Message = Request @on_message;
main := Message;
}%%
/**
* Ragel: %%write data
* This generates Ragel's static variables such as:
* static const int MessageParser_start
*/
%%write data;
Parser::Parser()
{
MS_TRACE();
Reset();
}
Parser::~Parser()
{
MS_TRACE();
}
Message* Parser::Parse(const MS_BYTE* buffer, size_t len)
{
MS_TRACE();
// Used by Ragel:
const unsigned char* p;
const unsigned char* pe;
// const unsigned char* eof;
MS_ASSERT(this->parsedLen <= len, "parsedLen pasts end of buffer");
p = (const unsigned char*)buffer + this->parsedLen;
pe = (const unsigned char*)buffer + len;
// eof = nullptr; // Ragel manual pag 36 (eof variable).
/**
* Ragel: %%write exec
* This updates cs variable needed by Ragel (so this->cs at the end) and p.
*/
%%write exec;
this->parsedLen = p - (const unsigned char*)buffer;
MS_ASSERT(p <= pe, "buffer overflow after parsing execute");
MS_ASSERT(this->parsedLen <= len, "parsedLen longer than length");
MS_ASSERT(this->mark < len, "mark is after buffer end");
// Parsing succedded.
if (this->cs == (size_t)MessageParser_first_final)
{
MS_DEBUG("parsing finished OK");
MS_ASSERT(this->msg != nullptr, "parsing OK but msg is NULL");
// Check the message semantics.
if (!this->msg->IsValid())
{
MS_ERROR("invalid message");
delete this->msg;
return (this->msg = nullptr);
}
return this->msg;
}
// Parsing error.
else if (this->cs == (size_t)MessageParser_error)
{
MS_ERROR("parsing error at position %zd", this->parsedLen);
// Delete this->msg if it exists.
if (this->msg)
{
delete this->msg;
this->msg = nullptr;
}
return nullptr;
}
// Parsing not finished.
else
{
MS_DEBUG("parsing not finished");
return nullptr;
}
}
bool Parser::HasError()
{
MS_TRACE();
return (this->cs == (size_t)MessageParser_error);
}
size_t Parser::GetParsedLen()
{
MS_TRACE();
return this->parsedLen;
}
void Parser::Reset()
{
MS_TRACE();
this->parsedLen = 0;
this->msg = nullptr;
this->cs = 0;
this->mark = 0;
/**
* Ragel: %%write init
* This sets cs variable needed by Ragel (so this->cs at the end).
*/
%%write init;
}
void Parser::Dump()
{
MS_DEBUG("[cs: %zu | parsedLen: %zu | mark: %zu | error?: %s | finished?: %s]",
this->cs, this->parsedLen, this->mark,
HasError() ? "true" : "false",
(this->cs >= (size_t)MessageParser_first_final) ? "true" : "false");
}
} // namespace ControlProtocol
#define MS_CLASS "ControlProtocol::Request"
#include "ControlProtocol/Request.h"
#include "Logger.h"
namespace ControlProtocol
{
/* Instance methods. */
Request::Request(Type type) :
Message(Message::Kind::Request),
type(type)
{
MS_TRACE();
}
Request::~Request()
{
MS_TRACE();
}
} // namespace ControlProtocol
#define MS_CLASS "ControlProtocol::TCPConnection"
#include "ControlProtocol/TCPConnection.h"
#include "Logger.h"
#include <cstring> // std::memmove()
namespace ControlProtocol
{
/* Instance methods. */
TCPConnection::TCPConnection(Reader* reader, size_t bufferSize) :
::TCPConnection(bufferSize),
reader(reader)
{
MS_TRACE();
// Initiate a ControlProtocol::Parser.
this->parser = new ControlProtocol::Parser();
}
TCPConnection::~TCPConnection()
{
MS_TRACE();
if (this->parser)
delete this->parser;
}
void TCPConnection::userOnTCPConnectionRead(const MS_BYTE* data, size_t len)
{
MS_TRACE();
// Be ready to parse more than a single message in a single TCP chunk.
while (true)
{
ControlProtocol::Message* msg;
// Tell the parser to parse the buffer starting from the beginning of the latest
// detected message until the end of the data in the buffer.
msg = this->parser->Parse(this->buffer + this->msgStart, this->bufferDataLen - this->msgStart);
size_t parsed_len = parser->GetParsedLen();
// Message parsed.
if (msg)
{
// Notify the reader.
this->reader->onControlProtocolMessage(this, msg, this->buffer + this->msgStart, parsed_len);
// Free the message.
delete msg;
// If there is no more space available in the buffer and that is because
// the latest parsed message filled it, then empty the full buffer.
if ((this->msgStart + parsed_len) == this->bufferSize)
{
MS_DEBUG("no more space in the buffer, emptying the buffer data");
this->msgStart = 0;
this->bufferDataLen = 0;
}
// If there is still space in the buffer, set the beginning of the next
// parsing to the next position after the parsed message.
else
{
this->msgStart += parsed_len;
}
// Reset the parser.
this->parser->Reset();
// If there is more data in the buffer after the parsed message
// then parse again. Otherwise break here and wait for more data.
if (this->bufferDataLen > this->msgStart)
{
MS_DEBUG("there is more data after the parsed message, continue parsing");
continue;
}
else
{
break;
}
}
// Parsing error. Close the connection.
else if (this->parser->HasError())
{
MS_ERROR("parsing error, closing the connection");
// Close the connection.
Close();
// Exit the parsing loop.
break;
}
// Message not finished.
else
{
// Check if the buffer is full.
if (this->bufferDataLen == this->bufferSize)
{
// First case: the uncomplete message does not begin at position 0 of
// the buffer, so move the message to the position 0.
if (this->msgStart != 0)
{
MS_DEBUG("no more space in the buffer, moving parsed bytes to the beginning of the buffer and wait for more data");
std::memmove(this->buffer, this->buffer + this->msgStart, parsed_len);
this->msgStart = 0;
this->bufferDataLen = parsed_len;
}
// Second case: the uncomplete message begins at position 0 of the buffer.
// The message is too big, so close the connection.
else
{
MS_ERROR("no more space in the buffer for the unfinished message being parsed, closing the connection");
// Close the socket.
Close();
}
}
// The buffer is not full.
else
{
MS_DEBUG("message not finished yet, waiting for more data");
}
// Exit the parsing loop.
break;
}
} // while()
}
} // namespace ControlProtocol
#define MS_CLASS "ControlProtocol::TCPServer"
#include "ControlProtocol/TCPServer.h"
#include "Logger.h"
namespace ControlProtocol
{
/* Instance methods. */
TCPServer::TCPServer(Listener* listener, ControlProtocol::TCPConnection::Reader* reader, const std::string &ip, MS_PORT port) :
::TCPServer::TCPServer(ip, port, 2048),
listener(listener),
reader(reader)
{
MS_TRACE();
}
void TCPServer::userOnTCPConnectionAlloc(::TCPConnection** connection)
{
MS_TRACE();
// Allocate a new ControlProtocol::TCPConnection and pass it our reader.
*connection = new ControlProtocol::TCPConnection(this->reader, 65536);
}
void TCPServer::userOnNewTCPConnection(::TCPConnection* connection)
{
MS_TRACE();
// Notify the listener.
this->listener->onControlProtocolNewTCPConnection(this, static_cast<ControlProtocol::TCPConnection*>(connection));
}
void TCPServer::userOnTCPConnectionClosed(::TCPConnection* connection, bool is_closed_by_peer)
{
MS_TRACE();
// Notify the listener.
this->listener->onControlProtocolTCPConnectionClosed(this, static_cast<ControlProtocol::TCPConnection*>(connection), is_closed_by_peer);
}
void TCPServer::userOnTCPServerClosed()
{
MS_TRACE();
}
} // namespace ControlProtocol
#define MS_CLASS "ControlProtocol::UnixStreamSocket"
#include "ControlProtocol/UnixStreamSocket.h"
#include "Logger.h"
#include <cstring> // std::memmove()
namespace ControlProtocol
{
/* Instance methods. */
UnixStreamSocket::UnixStreamSocket(Listener* listener, int fd) :
::UnixStreamSocket::UnixStreamSocket(fd, 65536),
listener(listener)
{
MS_TRACE();
// Initiate a ControlProtocol::Parser.
this->parser = new ControlProtocol::Parser();
}
UnixStreamSocket::~UnixStreamSocket()
{
MS_TRACE();
if (this->parser)
delete this->parser;
}
void UnixStreamSocket::userOnUnixStreamRead(const MS_BYTE* data, size_t len)
{
MS_TRACE();
// Be ready to parse more than a single message in a single TCP chunk.
while (true)
{
ControlProtocol::Message* msg;
// Tell the parser to parse the buffer starting from the beginning of the latest
// detected message until the end of the data in the buffer.
msg = this->parser->Parse(this->buffer + this->msgStart, this->bufferDataLen - this->msgStart);
size_t parsed_len = parser->GetParsedLen();
// Message parsed.
if (msg)
{
// Notify the listener.
this->listener->onControlProtocolMessage(this, msg, this->buffer + this->msgStart, parsed_len);
// Free the message.
delete msg;
// If there is no more space available in the buffer and that is because
// the latest parsed message filled it, then empty the full buffer.
if ((this->msgStart + parsed_len) == this->bufferSize)
{
MS_DEBUG("no more space in the buffer, emptying the buffer data");
this->msgStart = 0;
this->bufferDataLen = 0;
}
// If there is still space in the buffer, set the beginning of the next
// parsing to the next position after the parsed message.
else
{
this->msgStart += parsed_len;
}
// Reset the parser.
this->parser->Reset();
// If there is more data in the buffer after the parsed message
// then parse again. Otherwise break here and wait for more data.
if (this->bufferDataLen > this->msgStart)
{
MS_DEBUG("there is more data after the parsed message, continue parsing");
continue;
}
else
{
break;
}
}
// Parsing error. Close the pipe.
else if (this->parser->HasError())
{
MS_ERROR("parsing error, closing the pipe");
// Close the pipe.
Close();
// Exit the parsing loop.
break;
}
// Message not finished.
else
{
// Check if the buffer is full.
if (this->bufferDataLen == this->bufferSize)
{
// First case: the uncomplete message does not begin at position 0 of
// the buffer, so move the message to the position 0.
if (this->msgStart != 0)
{
MS_DEBUG("no more space in the buffer, moving parsed bytes to the beginning of the buffer and wait for more data");
std::memmove(this->buffer, this->buffer + this->msgStart, parsed_len);
this->msgStart = 0;
this->bufferDataLen = parsed_len;
}
// Second case: the uncomplete message begins at position 0 of the buffer.
// The message is too big, so close the pipe.
else
{
MS_ERROR("no more space in the buffer for the unfinished message being parsed, closing the pipe");
// Close the socket.
Close();
}
}
// The buffer is not full.
else
{
MS_DEBUG("message not finished yet, waiting for more data");
}
// Exit the parsing loop.
break;
}
} // while()
}
void UnixStreamSocket::userOnUnixStreamSocketClosed(bool is_closed_by_peer)
{
MS_TRACE();
// Notify the listener.
this->listener->onControlProtocolUnixStreamSocketClosed(this, is_closed_by_peer);
}
} // namespace ControlProtocol
#!/usr/bin/env bash
ROOT_DIR="../../"
RAGEL_FILE="src/ControlProtocol/Parser.rl"
RAGEL_COMPILE_TYPE="-G2"
PRODUCTION=false
set -e
which ragel >/dev/null
if [ $? -ne 0 ] ; then
echo ">>> ERROR: ragel binary not found, cannot compile the Ragel grammar" >&2
exit 1
else
ragel -v
fi
cd $ROOT_DIR
cpp_file="${RAGEL_FILE%.rl}.cpp"
echo ">>> INFO: compiling $RAGEL_FILE:"
if [ "$PRODUCTION" == "true" ] ; then
show_ragel_lines="-L"
else
show_ragel_lines=""
fi
ragel $RAGEL_COMPILE_TYPE -C $RAGEL_FILE $show_ragel_lines -o $cpp_file
# Remove unused Ragel variables.
sed -i".delete_me" -E "s/.*static const int MessageParser_en_main.*//g" $cpp_file
echo ">>> DEBUG: Ragel's unused static variables removed from $cpp_file"
rm -f ${cpp_file}.delete_me
echo ">>> INFO: $cpp_file generated"
%%{
machine RequestAuthenticate;
action on_request_type_Authenticate
{
this->msg = new ControlProtocol::RequestAuthenticate();
}
action on_auth_user
{
static_cast<RequestAuthenticate*>(this->msg)->SetUser(PTR_TO_MARK, LEN_FROM_MARK);
}
action on_auth_passwd
{
static_cast<RequestAuthenticate*>(this->msg)->SetPasswd(PTR_TO_MARK, LEN_FROM_MARK);
}
auth_user = token >mark %on_auth_user;
auth_passwd = token >mark %on_auth_passwd;
RequestAuthenticate = "Request" SP "Authenticate" CRLF %on_request_type_Authenticate
transaction CRLF
"user:" SP auth_user CRLF
"passwd:" SP auth_passwd CRLF
CRLF;
}%%
%%{
machine RequestCreateConference;
action on_request_type_CreateConference
{
this->msg = new ControlProtocol::RequestCreateConference();
}
RequestCreateConference = "Request" SP "CreateConference" CRLF %on_request_type_CreateConference
transaction CRLF
CRLF;
}%%
%%{
machine RequestHello;
action on_request_type_Hello
{
this->msg = new ControlProtocol::RequestHello();
}
RequestHello = "Request" SP "Hello" CRLF %on_request_type_Hello
transaction CRLF
CRLF;
}%%
#define MS_CLASS "ControlProtocol::RequestAuthenticate"
#include "ControlProtocol/messages/RequestAuthenticate.h"
#include "Logger.h"
namespace ControlProtocol
{
/* Instance methods. */
RequestAuthenticate::RequestAuthenticate() :
Request(Type::Authenticate)
{
MS_DEBUG("kind: %d, type: %d", (int)this->kind, (int)this->type);
}
RequestAuthenticate::~RequestAuthenticate()
{
MS_TRACE();
}
void RequestAuthenticate::SetUser(const char* str, size_t len)
{
MS_TRACE();
this->user = std::string(str, len);
}
void RequestAuthenticate::SetPasswd(const char* str, size_t len)
{
MS_TRACE();
this->passwd = std::string(str, len);
}
void RequestAuthenticate::Dump()
{
MS_DEBUG("[Request Authenticate]");
MS_DEBUG("- transaction: %zu", (size_t)this->transaction);
MS_DEBUG("- user: %s", this->user.c_str());
MS_DEBUG("- passwd: %s", this->passwd.c_str());
MS_DEBUG("[/Request Authenticate]");
}
} // namespace ControlProtocol
#define MS_CLASS "ControlProtocol::RequestCreateConference"
#include "ControlProtocol/messages/RequestCreateConference.h"
#include "Logger.h"
namespace ControlProtocol
{
/* Instance methods. */
RequestCreateConference::RequestCreateConference() : Request(Type::CreateConference)
{
MS_DEBUG("kind: %d, type: %d", (int)this->kind, (int)this->type);
}
RequestCreateConference::~RequestCreateConference()
{
MS_TRACE();
}
void RequestCreateConference::Dump()
{
MS_DEBUG("[Request CreateConference]");
MS_DEBUG("- transaction: %zu", (size_t)this->transaction);
MS_DEBUG("[/Request CreateConference]");
}
} // namespace ControlProtocol
#define MS_CLASS "ControlProtocol::RequestHello"
#include "ControlProtocol/messages/RequestHello.h"
#include "Logger.h"
namespace ControlProtocol
{
/* Instance methods. */
RequestHello::RequestHello() : Request(Type::Hello)
{
MS_DEBUG("kind: %d, type: %d", (int)this->kind, (int)this->type);
}
RequestHello::~RequestHello()
{
MS_TRACE();
}
void RequestHello::Dump()
{
MS_DEBUG("[Request Hello]");
MS_DEBUG("- transaction: %zu", (size_t)this->transaction);
MS_DEBUG("[/Request Hello]");
}
} // namespace ControlProtocol
#define MS_CLASS "Daemon"
#include "Daemon.h"
#include "Settings.h"
#include "Version.h"
#include "MediaSoupError.h"
#include "Logger.h"
#include <string>
#include <cerrno>
#include <cstdlib> // std::_Exit()
#include <cstdio> // std::setbuf(), std::freopen(), std::fopen(), std::fscanf(), std::fprintf(), std::remove()
#include <csignal> // kill()
#include <unistd.h> // pipe(), fork(), close(), setsid(), getpid()
/* Static variables. */
int Daemon::statusPipe[2] {-1, -1};
bool Daemon::isDaemonized = false;
bool Daemon::hasWrittenPIDFile = false;
/* Static methods. */
void Daemon::Daemonize()
{
MS_TRACE();
/**
* - ancestor process (running in foreground)
* \- intermediary process
* \- daemonized process (main process running in background)
*/
int ret;
int pid;
char status;
// Flush std file descriptors to avoid flushes after fork (same message
// appearing multiple times) and switch to unbuffered mode.
std::setbuf(stdout, 0);
std::setbuf(stderr, 0);
// Set the pipe for the daemonized process to send its status to the ancestor
// process (that waits for it in the foreground).
do
{
ret = pipe(Daemon::statusPipe);
}
while (ret < 0 && errno == EINTR);
if (ret < 0)
MS_THROW_ERROR("pipe() failed: %s", std::strerror(errno));
// NOTE: Don't need to update thread name after fork() calls. fork() copies
// the memory of the calling thread.
// First fork: don't be group leader.
if ((pid = fork()) < 0)
{
MS_THROW_ERROR("first fork() failed: %s", std::strerror(errno));
}
else if (pid > 0)
{
// When fork() returns a positive number, we are in the ancestor process
// and the return value is the PID of the newly created child process (the
// intermediary process).
// Ancestor process wait for the daemonized process to write into the pipe (or to die
// before) so it can exit with the proper exit status.
if (Daemon::WaitForDaemonizedStatus(&status))
{
if ((int)status == EXIT_SUCCESS)
{
MS_NOTICE("%s running as daemon", Version::name.c_str());
std::_Exit(EXIT_SUCCESS);
}
else
{
MS_EXIT_FAILURE("daemonized process failed to start (check Syslog)");
}
}
// The daemonized process died before writing its status on the pipe.
else
{
MS_EXIT_FAILURE("daemonized process died before writing its status (check Syslog)");
}
}
// This is the intermediary process.
// We are now daemonized (even if this is not the definitive forked process).
Daemon::isDaemonized = true;
// Clean unused fds.
if (Daemon::statusPipe[0] != -1)
{
close(Daemon::statusPipe[0]);
Daemon::statusPipe[0] = -1;
}
// Become session leader to drop the ctrl terminal.
if (setsid() < 0)
{
MS_THROW_ERROR("setsid() failed: %s", std::strerror(errno));
}
// Fork again to drop group leadership.
if ((pid = fork()) < 0)
{
MS_THROW_ERROR("second fork() failed: %s", std::strerror(errno));
}
else if (pid > 0) {
// Intermediary process just exits.
std::_Exit(EXIT_SUCCESS);
}
// This is the daemonized process.
// Create a PID file if requested.
if (!Settings::arguments.pidFile.empty())
{
if (!Daemon::WritePIDFile())
MS_THROW_ERROR("error writing the PID file '%s'", Settings::arguments.pidFile.c_str());
}
// Replace stdin, stdout & stderr with /dev/null.
if (std::freopen("/dev/null", "r", stdin) == nullptr)
{
MS_ERROR("error replacing stdin with /dev/null: %s", std::strerror(errno));
}
if (std::freopen("/dev/null", "w", stdout) == nullptr)
{
MS_ERROR("error replacing stdout with /dev/null: %s", std::strerror(errno));
}
if (std::freopen("/dev/null", "w", stderr) == nullptr)
{
MS_ERROR("error replacing stderr with /dev/null: %s", std::strerror(errno));
}
// Close all the possible fds inherited from the ancestor process (all but
// the write side of the pipe as the daemonized process must use it).
for (int fd = 3; fd < 32; fd++)
{
if (fd != Daemon::statusPipe[1])
close(fd);
}
// Enable Syslog logging.
Logger::EnableSyslog();
// First log in Syslog.
MS_NOTICE("%s running as daemon", Version::name.c_str());
}
void Daemon::SendOKStatusToAncestor()
{
MS_TRACE();
MS_DEBUG("sending OK status to the ancestor process");
SendStatusToAncestor(EXIT_SUCCESS);
}
void Daemon::SendErrorStatusToAncestor()
{
MS_TRACE();
MS_DEBUG("sending error status to the ancestor process");
SendStatusToAncestor(EXIT_FAILURE);
}
bool Daemon::IsDaemonized()
{
MS_TRACE();
return Daemon::isDaemonized;
}
void Daemon::End()
{
MS_TRACE();
// Delete PID file.
if (Daemon::hasWrittenPIDFile)
{
int err;
err = std::remove(Settings::arguments.pidFile.c_str());
if (err)
MS_ERROR("cannot delete the PID file '%s': %s", Settings::arguments.pidFile.c_str(), std::strerror(errno));
}
}
bool Daemon::WritePIDFile()
{
MS_TRACE();
FILE *pid_file;
const char* pidFile = Settings::arguments.pidFile.c_str();
unsigned int existing_pid = 0;
pid_t pid;
pid_file = std::fopen(pidFile, "r");
// PID file already exists. Check it.
if (pid_file)
{
MS_DEBUG("a PID file '%s' already exists", pidFile);
if (std::fscanf(pid_file, "%9u", &existing_pid) < 0)
{
MS_ERROR("could not parse existing PID file '%s'", pidFile);
std::fclose(pid_file);
return false;
}
fclose(pid_file);
if (existing_pid == 0)
{
MS_ERROR("existing PID file '%s' does not contain a valid value", pidFile);
return false;
}
if (kill((pid_t)existing_pid, 0) == 0 || errno == EPERM)
{
MS_ERROR("there is a running process with the same PID as in the existing PID file '%s'",
pidFile);
return false;
}
else
{
MS_NOTICE("existing PID file '%s' contains an old PID, replacing its value", pidFile);
}
}
// Get our PID.
pid = getpid();
MS_DEBUG("PID of the daemonized process: %u", (unsigned int)pid);
// Write into the PID file.
pid_file = std::fopen(pidFile, "w");
if (!pid_file)
{
MS_ERROR("cannot open PID file '%s' for writing: %s", pidFile, std::strerror(errno));
return false;
}
std::fprintf(pid_file, "%u\n", (unsigned int)pid);
std::fclose(pid_file);
Daemon::hasWrittenPIDFile = true;
MS_DEBUG("PID file '%s' created with our PID value", pidFile);
return true;
}
bool Daemon::WaitForDaemonizedStatus(char* status)
{
MS_TRACE();
int ret;
MS_DEBUG("waiting for the daemonized process to write its status");
// Close the output side of the pipe.
if (Daemon::statusPipe[1] != -1)
{
close(Daemon::statusPipe[1]);
Daemon::statusPipe[1] = -1;
}
// If the read side of the pipe is closed the daemonized process died even before
// sending status.
if (Daemon::statusPipe[0] == -1)
{
return false;
}
do
{
ret = read(Daemon::statusPipe[0], status, 1);
}
while (ret < 0 && errno == EINTR);
// If a byte has been read return true and the ancestor process will inspect the
// status value.
return (ret == 1) ? true : false;
}
void Daemon::SendStatusToAncestor(char status)
{
MS_TRACE();
int ret;
if (Daemon::statusPipe[1] == -1)
{
MS_ERROR("cannot send status to ancestor process, pipe is closed");
return;
}
do
{
ret = write(Daemon::statusPipe[1], &status, 1);
}
while (ret < 0 && errno == EINTR);
if (ret != 1)
MS_ERROR("cannot send status to ancestor process: %s", std::strerror(errno));
close(Daemon::statusPipe[1]);
Daemon::statusPipe[1] = -1;
}
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
/* Static variables. */ /* Static variables. */
__thread uv_loop_t* DepLibUV::loop = nullptr; uv_loop_t* DepLibUV::loop = nullptr;
/* Static methods. */ /* Static methods. */
...@@ -13,19 +13,10 @@ void DepLibUV::ClassInit() ...@@ -13,19 +13,10 @@ void DepLibUV::ClassInit()
{ {
MS_TRACE(); MS_TRACE();
// Print libuv version.
MS_DEBUG("loaded libuv version: %s", uv_version_string());
}
void DepLibUV::ThreadInit()
{
MS_TRACE();
int err; int err;
// This should never happen. // Print libuv version.
if (DepLibUV::loop) MS_DEBUG("loaded libuv version: %s", uv_version_string());
MS_ABORT("DepLibUV::loop alread allocated in this thread");
DepLibUV::loop = new uv_loop_t; DepLibUV::loop = new uv_loop_t;
err = uv_loop_init(DepLibUV::loop); err = uv_loop_init(DepLibUV::loop);
...@@ -33,13 +24,13 @@ void DepLibUV::ThreadInit() ...@@ -33,13 +24,13 @@ void DepLibUV::ThreadInit()
MS_ABORT("uv_loop_init() failed: %s", uv_strerror(err)); MS_ABORT("uv_loop_init() failed: %s", uv_strerror(err));
} }
void DepLibUV::ThreadDestroy() void DepLibUV::ClassDestroy()
{ {
MS_TRACE(); MS_TRACE();
// This should never happen. // This should never happen.
if (!DepLibUV::loop) if (!DepLibUV::loop)
MS_ABORT("DepLibUV::loop was not allocated in this thread"); MS_ABORT("DepLibUV::loop was not allocated");
uv_loop_close(DepLibUV::loop); uv_loop_close(DepLibUV::loop);
delete DepLibUV::loop; delete DepLibUV::loop;
...@@ -51,7 +42,7 @@ void DepLibUV::RunLoop() ...@@ -51,7 +42,7 @@ void DepLibUV::RunLoop()
// This should never happen. // This should never happen.
if (!DepLibUV::loop) if (!DepLibUV::loop)
MS_ABORT("DepLibUV::loop was not allocated in this thread"); MS_ABORT("DepLibUV::loop was not allocated");
uv_run(DepLibUV::loop, UV_RUN_DEFAULT); uv_run(DepLibUV::loop, UV_RUN_DEFAULT);
} }
#define MS_CLASS "Dispatcher"
#include "Dispatcher.h"
#include "Settings.h"
#include "Daemon.h"
#include "Worker.h"
#include "Utils.h"
#include "DepLibUV.h"
#include "ControlProtocol/messages.h"
#include "MediaSoupError.h"
#include "Logger.h"
#include <string>
#include <csignal> // sigaction(), pthread_sigmask()
#include <cerrno>
/* Instance methods. */
Dispatcher::Dispatcher()
{
MS_TRACE();
// Set the signals handler.
try
{
this->signalsHandler = new SignalsHandler(this);
// Add signals to handle.
this->signalsHandler->AddSignal(SIGUSR1, "USR1");
this->signalsHandler->AddSignal(SIGINT, "INT");
this->signalsHandler->AddSignal(SIGTERM, "TERM");
}
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("error creating the SignalsHandler: %s", error.what());
}
MS_DEBUG("SignalsHandler ready");
// Create a ControlProtocol TCP server.
std::string ip = Settings::configuration.ControlProtocol.listenIP;
MS_PORT port = Settings::configuration.ControlProtocol.listenPort;
try
{
this->controlProtocolTCPServer = new ControlProtocol::TCPServer(this, this, ip, port);
}
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("error creating a ControlProtocol TCP server in %s : %d: %s", ip.c_str(), port, error.what());
}
MS_DEBUG("ControlProtocol TCP server listening into %s : %d", this->controlProtocolTCPServer->GetLocalIP().c_str(), this->controlProtocolTCPServer->GetLocalPort());
// Create the ControlProtocol UnixStream sockets.
int num_workers = Worker::CountWorkers();
// Insert a nullptr value in position 0 of the vector of ControlProtocol::UnixStreamSocket*
// since the first entry will be in position 1.
this->controlProtocolUnixStreamSockets.push_back(nullptr);
for (int workerId = 1; workerId <= num_workers; workerId++)
{
int fd = Worker::GetControlProtocolRemoteSocket(workerId);
ControlProtocol::UnixStreamSocket* controlProtocolUnixStreamSocket;
try
{
controlProtocolUnixStreamSocket = new ControlProtocol::UnixStreamSocket(this, fd);
// Attach the workerId as user data.
int* data = new int(workerId);
controlProtocolUnixStreamSocket->SetUserData((void*)data);
}
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("error creating a ControlProtocol Unix socket for Worker %d: %s", workerId, error.what());
}
// Add to the vector. Note that workerId 1 will be placed in position 1 of the vector.
this->controlProtocolUnixStreamSockets.push_back(controlProtocolUnixStreamSocket);
MS_DEBUG("ControlProtocol Unix socket for Worker #%d ready", workerId);
}
MS_DEBUG("Dispatcher running");
// Write the success status on the pipe if daemonized to tell the ancestor
// process that the daemonized process is properly running.
if (Settings::arguments.daemonize)
Daemon::SendOKStatusToAncestor();
// Run the loop.
DepLibUV::RunLoop();
MS_DEBUG("libuv loop ends");
}
Dispatcher::~Dispatcher()
{
MS_TRACE();
}
void Dispatcher::Close()
{
MS_TRACE();
if (this->closed)
{
MS_ERROR("already closed");
return;
}
this->closed = true;
MS_DEBUG("closing all the services running in the Dispatcher");
// First block all the signals to not be interrupted while closing.
int err;
sigset_t signal_mask;
// Block all the signals.
sigfillset(&signal_mask);
err = pthread_sigmask(SIG_BLOCK, &signal_mask, nullptr);
if (err)
MS_ERROR("pthread_sigmask() failed: %s", std::strerror(errno));
// Close the SignalsHandler.
this->signalsHandler->Close();
// Close the ControlProtocol TCP server.
this->controlProtocolTCPServer->Close();
// Close the ControlProtocol UnixStream sockets.
int num_workers = Worker::CountWorkers();
for (int workerId = 1; workerId <= num_workers; workerId++)
this->controlProtocolUnixStreamSockets[workerId]->Close();
}
void Dispatcher::onSignal(SignalsHandler* signalsHandler, int signum)
{
MS_TRACE();
switch (signum)
{
// SIGUSR1 means configuration reload.
case SIGUSR1:
MS_NOTICE("signal USR1 received, reloading configuration");
if (Settings::ReloadConfigurationFile())
{
// Print new configuration.
Settings::PrintConfiguration();
}
break;
case SIGINT:
MS_NOTICE("signal INT received, exiting");
Close();
break;
case SIGTERM:
MS_NOTICE("signal TERM received, exiting");
Close();
break;
default:
MS_ERROR("received a signal (with signum %d) for which there is no handling code", signum);
}
}
void Dispatcher::onSignalsHandlerClosed(SignalsHandler* signalsHandler)
{
MS_TRACE();
}
void Dispatcher::onControlProtocolNewTCPConnection(ControlProtocol::TCPServer* tcpServer, ControlProtocol::TCPConnection* connection)
{
MS_TRACE();
MS_DEBUG("new ControlProtocol TCP connection:");
tcpServer->Dump();
connection->Dump();
}
void Dispatcher::onControlProtocolTCPConnectionClosed(ControlProtocol::TCPServer* tcpServer, ControlProtocol::TCPConnection* connection, bool is_closed_by_peer)
{
MS_TRACE();
if (is_closed_by_peer)
MS_DEBUG("ControlProtocol TCP connection closed by peer:");
else
MS_DEBUG("ControlProtocol TCP connection closed locally:");
connection->Dump();
tcpServer->Dump();
}
void Dispatcher::onControlProtocolMessage(ControlProtocol::TCPConnection* connection, ControlProtocol::Message* msg, const MS_BYTE* raw, size_t len)
{
MS_TRACE();
MS_DEBUG("ControlProtocol message received from TCP %s : %u", connection->GetPeerIP().c_str(), (unsigned int)connection->GetPeerPort());
// TODO: process the ControlProtocol::Message.
// TEST
bool relayToRandomWorker = true;
bool relayToAllWorkers = false;
bool replyOK = true;
if (relayToRandomWorker)
{
int workerId = (int)Utils::Crypto::GetRandomUInt(1, (unsigned int)Worker::CountWorkers());
MS_INFO("relaying ControlProtocol message to Worker #%d ...", workerId);
ControlProtocol::UnixStreamSocket* controlProtocolUnixStreamSocket = controlProtocolUnixStreamSockets[workerId];
controlProtocolUnixStreamSocket->Write(raw, len);
}
if (relayToAllWorkers)
{
int num_workers = Worker::CountWorkers();
for (int workerId = 1; workerId <= num_workers; workerId++)
{
ControlProtocol::UnixStreamSocket* controlProtocolUnixStreamSocket = controlProtocolUnixStreamSockets[workerId];
MS_INFO("relaying ControlProtocol message to Worker #%d ...", workerId);
controlProtocolUnixStreamSocket->Write(raw, len);
}
}
if (replyOK)
{
MS_INFO("replying 'OK, message received' via TCP ...");
// TEST: Write(std::string &data)
std::string ok_reply_begin = "OK, message received with transaction ";
connection->Write(ok_reply_begin);
// TEST: Write(std::string &data)
std::string transaction_str = std::to_string(msg->GetTransaction());
connection->Write(transaction_str);
// TEST: Write(std::string &data)
std::string ok_reply_end = ":\n---------------------\n";
connection->Write(ok_reply_end);
// TEST: Write(const MS_BYTE* data, size_t len)
connection->Write(raw, len);
// TEST: Write(std::string &data)
std::string ok_reply_real_end = "---------------------\n";
connection->Write(ok_reply_real_end);
}
}
void Dispatcher::onControlProtocolMessage(ControlProtocol::UnixStreamSocket* unixSocket, ControlProtocol::Message* msg, const MS_BYTE* raw, size_t len)
{
MS_TRACE();
// Get the workerId associated to this socket.
int workerId = *(int*)unixSocket->GetUserData();
// TMP
MS_INFO("ControlProtocol message received from Worker #%d", workerId);
// TMP send back to the worker (loop !!!)
unixSocket->Write((MS_BYTE*)raw, len);
// TODO: process the ControlProtocol::Message.
}
void Dispatcher::onControlProtocolUnixStreamSocketClosed(ControlProtocol::UnixStreamSocket* unixSocket, bool is_closed_by_peer)
{
MS_TRACE();
// Get the workerId associated to this socket.
int* userData = (int*)unixSocket->GetUserData();
int workerId = *userData;
// Free the socket's user data and remove the socket from the map.
delete userData;
controlProtocolUnixStreamSockets[workerId] = nullptr;
// A Worker MUST NEVER close the socket. If it does it is due to a parsing error
// that should NEVER happen, so abort.
if (is_closed_by_peer)
MS_ABORT("ControlProtocol Unix socket for Worker #%d remotely closed", workerId);
else
MS_DEBUG("ControlProtocol Unix socket for Worker #%d locally closed", workerId);
}
#define MS_CLASS "Logger" #define MS_CLASS "Logger"
#include "Logger.h" #include "Logger.h"
#include "Version.h"
/* Static variables. */ /* Static variables. */
__thread std::string* Logger::threadName = nullptr; std::string Logger::processName;
__thread const char* Logger::threadNamePtr = nullptr;
bool Logger::isSyslogEnabled = false; bool Logger::isSyslogEnabled = false;
/* Static methods. */ /* Static methods. */
void Logger::ThreadInit(const std::string name) void Logger::Init(const std::string name)
{ {
// Store the name for this thread in thread local storage. Logger::processName = name;
if (Logger::threadName)
delete Logger::threadName;
// NOTE: this std::string pointer is never deallocated.
Logger::threadName = new std::string(name);
Logger::threadNamePtr = Logger::threadName->c_str();
MS_TRACE(); MS_TRACE();
} }
...@@ -28,7 +20,8 @@ void Logger::EnableSyslog() ...@@ -28,7 +20,8 @@ void Logger::EnableSyslog()
{ {
MS_TRACE(); MS_TRACE();
openlog(Version::command.c_str(), (LOG_PID), Settings::configuration.syslogFacility); // TODO: Set the process name/id as well.
openlog("MediaSoup", (LOG_PID), Settings::configuration.syslogFacility);
Logger::isSyslogEnabled = true; Logger::isSyslogEnabled = true;
} }
#define MS_CLASS "Loop"
#include "Loop.h"
#include "DepLibUV.h"
#include "MediaSoupError.h"
#include "Logger.h"
#include <string>
#include <csignal> // sigfillset, pthread_sigmask()
#include <cerrno>
/* Instance methods. */
Loop::Loop()
{
MS_TRACE();
// Set the signals handler.
try
{
this->signalsHandler = new SignalsHandler(this);
// Add signals to handle.
this->signalsHandler->AddSignal(SIGINT, "INT");
this->signalsHandler->AddSignal(SIGTERM, "TERM");
}
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("error creating the SignalsHandler: %s", error.what());
}
MS_DEBUG("SignalsHandler ready");
MS_DEBUG("starting libuv loop");
DepLibUV::RunLoop();
MS_DEBUG("libuv loop ended");
}
Loop::~Loop()
{
MS_TRACE();
}
void Loop::Close()
{
MS_TRACE();
int err;
sigset_t signal_mask;
if (this->closed)
{
MS_ERROR("already closed");
return;
}
this->closed = true;
// First block all the signals to not be interrupted while closing.
sigfillset(&signal_mask);
err = pthread_sigmask(SIG_BLOCK, &signal_mask, nullptr);
if (err)
MS_ERROR("pthread_sigmask() failed: %s", std::strerror(errno));
// Close the SignalsHandler.
this->signalsHandler->Close();
}
void Loop::onSignal(SignalsHandler* signalsHandler, int signum)
{
MS_TRACE();
switch (signum)
{
case SIGINT:
MS_NOTICE("signal INT received, exiting");
Close();
break;
case SIGTERM:
MS_NOTICE("signal TERM received, exiting");
Close();
break;
default:
MS_ERROR("received a signal (with signum %d) for which there is no handling code", signum);
}
}
void Loop::onSignalsHandlerClosed(SignalsHandler* signalsHandler)
{
MS_TRACE();
}
#define MS_CLASS "MediaSoup"
#include "MediaSoup.h"
#include "Settings.h"
#include "Daemon.h"
#include "Dispatcher.h"
#include "Worker.h"
#include "DepLibUV.h"
#include "DepOpenSSL.h"
#include "DepLibSRTP.h"
#include "DepUsrSCTP.h"
#include "Utils.h"
#include "RTC/UDPSocket.h"
#include "RTC/TCPServer.h"
#include "RTC/DTLSHandler.h"
#include "RTC/SRTPSession.h"
#include "MediaSoupError.h"
#include "Logger.h"
#include <vector>
#include <map>
#include <string>
#include <csignal> // sigaction()
#include <cstdlib> // std::strtol()
#include <cerrno>
#include <cstdint> // uint8_t, etc
#include <ctime> // nanosleep()
#include <sys/resource.h> // setrlimit(), setrlimit()
#include <unistd.h> // setgid(), setuid(), initgroups()
#include <pwd.h>
#include <grp.h>
#include <uv.h>
#ifndef MS_RECOMMENDED_NOFILE
#define MS_RECOMMENDED_NOFILE 131072
#endif
/* Static methods. */
void MediaSoup::SetProcess()
{
MS_TRACE();
#if defined(MS_LITTLE_ENDIAN)
MS_DEBUG("detected Little-Endian CPU");
#elif defined(MS_BIG_ENDIAN)
MS_DEBUG("detected Big-Endian CPU");
#endif
#if defined(INTPTR_MAX) && defined(INT32_MAX) && (INTPTR_MAX == INT32_MAX)
MS_DEBUG("detected 32 bits architecture");
#elif defined(INTPTR_MAX) && defined(INT64_MAX) && (INTPTR_MAX == INT64_MAX)
MS_DEBUG("detected 64 bits architecture");
#else
MS_NOTICE("cannot determine whether the architecture is 32 or 64 bits");
#endif
// NOTE: Some of these methods can exit if error if detected.
IgnoreSignals();
SetKernelLimits();
SetUserGroup();
}
void MediaSoup::Run()
{
MS_TRACE();
// Init main thread stuff.
MediaSoup::ThreadInit();
// Init static data.
MediaSoup::ClassInit();
int err;
// A vector for holding the workerId value for each thread.
std::vector<int> worker_ids;
for (int workerId = 1; workerId <= Settings::configuration.numWorkers; workerId++)
{
worker_ids.push_back(workerId);
Worker::SetWorker(workerId, nullptr);
}
// A vector for holding all the worker threads and joining them later.
std::vector<uv_thread_t> worker_threads;
// Build the Worker threads.
for (int i = 0; i < Settings::configuration.numWorkers; i++)
{
MS_DEBUG("running a thread for Worker #%d", worker_ids[i]);
uv_thread_t thread;
err = uv_thread_create(&thread, MediaSoup::RunWorkerThread, (void*)&worker_ids[i]);
if (err)
MS_THROW_ERROR("uv_thread_create() failed: %s", uv_strerror(err));
worker_threads.push_back(thread);
}
// Wait for all the workers to be running.
// Wait 300 rounds of 0.01 seconds each one (max 3 seconds).
MS_DEBUG("waiting for all the Workers to be running...");
bool all_workers_running = false;
struct timespec waiting;
waiting.tv_sec = 0;
waiting.tv_nsec = 10000000; // 10 ms.
for (int i = 1; i <= 300; i++)
{
if (Worker::AreAllWorkersRunning())
{
all_workers_running = true;
break;
}
err = nanosleep(&waiting, nullptr);
if (err)
MS_THROW_ERROR("nanosleep() failed: %s", uv_strerror(err));
}
if (!all_workers_running)
MS_THROW_ERROR("some Worker(s) could not start or died");
MS_DEBUG("all the Workers running, let's run the Dispatcher");
try
{
Dispatcher dispatcher;
}
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("%s", error.what());
}
MS_DEBUG("Dispatcher ends, waiting for Workers to end");
for (auto thread : worker_threads)
{
err = uv_thread_join(&thread);
if (err)
MS_THROW_ERROR("uv_thread_join() failed: %s", uv_strerror(err));
}
MS_DEBUG("all the Workers have ended");
}
void MediaSoup::End()
{
MS_TRACE();
// Tell the Daemon class to cleanup its resources if needed.
if (Daemon::IsDaemonized())
Daemon::End();
// Free static stuff.
MediaSoup::ClassDestroy();
// Free thread stuff.
MediaSoup::ThreadDestroy();
}
void MediaSoup::IgnoreSignals()
{
MS_TRACE();
int err;
struct sigaction act;
// NOTE: Here we ignore also signals that will be later handled by the Dispatcher
// (as the libuv signal handler overrides it).
std::map<std::string, int> ignored_signals =
{
{ "INT", SIGINT },
{ "TERM", SIGTERM },
{ "HUP", SIGHUP },
{ "ALRM", SIGALRM },
{ "USR1", SIGUSR2 },
{ "USR2", SIGUSR1 }
};
act.sa_handler = SIG_IGN;
act.sa_flags = 0;
err = sigfillset(&act.sa_mask);
if (err)
MS_EXIT_FAILURE("sigfillset() failed: %s", std::strerror(errno));
for (auto it = ignored_signals.begin(); it != ignored_signals.end(); ++it)
{
std::string sig_name = it->first;
int sig_id = it->second;
err = sigaction(sig_id, &act, nullptr);
if (err)
MS_EXIT_FAILURE("sigaction() failed for signal %s: %s", sig_name.c_str(), std::strerror(errno));
}
}
void MediaSoup::SetKernelLimits()
{
MS_TRACE();
struct rlimit current_limit;
struct rlimit new_limit;
rlim_t recommended_NOFILE = MS_RECOMMENDED_NOFILE;
int err;
// Get current NOFILE limits.
err = getrlimit(RLIMIT_NOFILE, &current_limit);
if (err)
{
MS_EXIT_FAILURE("getrlimit(RLIMIT_NOFILE) failed: %s", std::strerror(errno));
return;
}
MS_DEBUG("getrlimit(RLIMIT_NOFILE) [soft:%llu, hard:%llu]", (unsigned long long)current_limit.rlim_cur, (unsigned long long)current_limit.rlim_max);
// If soft limit of "open files" is equal or bigger than recommended_NOFILE we are done.
if (current_limit.rlim_cur >= recommended_NOFILE)
goto end;
// If hard limit of "open files" is equal or bigger than recommended_NOFILE then we should
// be able to set the soft limit to recommended_NOFILE.
// NOTE: This is not true on OSX in which hard limit is infinity but it does
// not let the user chaning its soft limit.
if (current_limit.rlim_max >= recommended_NOFILE)
{
new_limit.rlim_cur = recommended_NOFILE;
new_limit.rlim_max = current_limit.rlim_max;
err = setrlimit(RLIMIT_NOFILE, &new_limit);
if (err) {
if (errno == EINVAL)
{
MS_DEBUG("setrlimit(RLIMIT_NOFILE) failed for limits [soft:%llu, hard:%llu]: %s", (unsigned long long)new_limit.rlim_cur, (unsigned long long)new_limit.rlim_max, std::strerror(errno));
}
else
{
MS_WARN("setrlimit(RLIMIT_NOFILE) failed for limits [soft:%llu, hard:%llu]: %s", (unsigned long long)new_limit.rlim_cur, (unsigned long long)new_limit.rlim_max, std::strerror(errno));
}
}
}
// Otherwise try to set both hard and soft limits to recommended_NOFILE.
else
{
new_limit.rlim_cur = recommended_NOFILE;
new_limit.rlim_max = recommended_NOFILE;
err = setrlimit(RLIMIT_NOFILE, &new_limit);
if (err)
MS_WARN("setrlimit(RLIMIT_NOFILE) failed for limits [soft:%llu, hard:%llu]: %s", (unsigned long long)new_limit.rlim_cur, (unsigned long long)new_limit.rlim_max, std::strerror(errno));
}
end:
// Get the resulting NOFILE limits.
err = getrlimit(RLIMIT_NOFILE, &current_limit);
if (err)
{
MS_EXIT_FAILURE("getrlimit(RLIMIT_NOFILE) failed: %s", std::strerror(errno));
return;
}
// If the soft limit is equal or bigger than recommended_NOFILE then ok.
if (current_limit.rlim_cur >= recommended_NOFILE)
{
MS_DEBUG("RLIMIT_NOFILE soft limit (max open files) set to %llu", (unsigned long long)current_limit.rlim_cur);
}
// Otherwise warn the user.
else
{
MS_WARN("RLIMIT_recommended_NOFILE soft limit (max open files) set to %llu, less than the recommended value (%llu)", (unsigned long long)current_limit.rlim_cur, (unsigned long long)recommended_NOFILE);
}
}
void MediaSoup::SetUserGroup()
{
MS_TRACE();
struct group *grp = nullptr;
struct passwd *pwd = nullptr;
// Set the group if requested.
if (!Settings::arguments.group.empty())
{
char *endptr;
const char* group = Settings::arguments.group.c_str();
gid_t g, gid;
// Allow passing a numeric string representing the user's gid.
g = std::strtol(group, &endptr, 10);
if (*endptr == '\0')
{
gid = (gid_t)g;
grp = getgrgid(gid);
if (!grp)
MS_EXIT_FAILURE("cannot get information for group with gid '%d': %s", (int)gid, std::strerror(errno));
}
// Otherwise convert from name to gid.
else
{
grp = getgrnam(group);
if (!grp)
MS_EXIT_FAILURE("group '%s' does not exist", group);
gid = grp->gr_gid;
}
// Set gid as process gid.
MS_DEBUG("setting '%s' (%d) as process group", group, (int)gid);
if (setgid(gid) < 0)
MS_EXIT_FAILURE("cannot change gid to %s (%d): %s", group, (int)gid, std::strerror(errno));
}
// Set the user if requested.
if (!Settings::arguments.user.empty())
{
char *endptr;
const char* user = Settings::arguments.user.c_str();
uid_t u, uid;
// Allow passing a numeric string representing the group's uid.
u = std::strtol(user, &endptr, 10);
if (*endptr == '\0')
{
uid = (uid_t)u;
pwd = getpwuid(uid);
if (!pwd)
MS_EXIT_FAILURE("cannot get information for user with uid '%d': %s", (int)uid, std::strerror(errno));
}
// Otherwise convert from name to uid.
else
{
pwd = getpwnam(user);
if (!pwd)
{
MS_EXIT_FAILURE("user '%s' does not exist", user);
}
uid = pwd->pw_uid;
}
// Set access to all the groups of which the user is member.
gid_t additional_gid = grp ? grp->gr_gid : pwd->pw_gid;
MS_DEBUG("setting supplementary groups for uid %d (using %d as additional gid)", (int)uid, (int)additional_gid);
if (initgroups(pwd->pw_name, additional_gid) < 0)
MS_EXIT_FAILURE("initgroups() for uid %d and gid %d failed: %s", (int)uid, (int)additional_gid, std::strerror(errno));
// Set uid as process uid.
MS_DEBUG("setting '%s' (%d) as process user", user, (int)uid);
if (setuid(uid) < 0)
MS_EXIT_FAILURE("cannot change uid to %s (%d): %s", user, (int)uid, std::strerror(errno));
}
}
void MediaSoup::ThreadInit()
{
MS_TRACE();
// Set (again, don't worry) this thread as main thread.
Logger::ThreadInit("main");
// Load libuv stuff.
DepLibUV::ThreadInit();
// Load the crypto utils.
Utils::Crypto::ThreadInit();
}
void MediaSoup::ThreadDestroy()
{
MS_TRACE();
DepLibUV::ThreadDestroy();
Utils::Crypto::ThreadDestroy();
}
void MediaSoup::ClassInit()
{
MS_TRACE();
// Initialize static stuff.
DepLibUV::ClassInit();
DepOpenSSL::ClassInit();
DepLibSRTP::ClassInit();
DepUsrSCTP::ClassInit();
RTC::UDPSocket::ClassInit();
RTC::TCPServer::ClassInit();
RTC::DTLSHandler::ClassInit();
RTC::SRTPSession::ClassInit();
}
void MediaSoup::ClassDestroy()
{
MS_TRACE();
// Free static stuff.
RTC::DTLSHandler::ClassDestroy();
DepOpenSSL::ClassDestroy();
DepLibSRTP::ClassDestroy();
DepUsrSCTP::ClassDestroy();
}
/* This method is called within a new thread and creates a Worker. */
void MediaSoup::RunWorkerThread(void* data)
{
int workerId = *(int*)data;
// Init thread stuff.
Worker::ThreadInit(workerId);
try
{
MS_DEBUG("initializing Worker #%d", workerId);
Worker worker(workerId);
MS_DEBUG("Worker #%d exits", workerId);
}
catch (const MediaSoupError &error)
{
MS_CRIT("error happened in Worker #%d: %s | Worker ended", workerId, error.what());
Worker::ThreadDestroy();
throw(error);
}
Worker::ThreadDestroy();
}
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
#include "RTC/DTLSHandler.h" #include "RTC/DTLSHandler.h"
#include "Settings.h" #include "Settings.h"
#include "Version.h"
#include "Utils.h" #include "Utils.h"
#include "MediaSoupError.h" #include "MediaSoupError.h"
#include "Logger.h" #include "Logger.h"
...@@ -84,8 +83,8 @@ namespace RTC ...@@ -84,8 +83,8 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// Generate a X509 certificate and private key (unless PEM files are privided). // Generate a X509 certificate and private key (unless PEM files are provided).
if (Settings::configuration.RTC.dtlsCertificateFile.empty()) if (Settings::configuration.RTC.dtlsCertificateFile.empty() || Settings::configuration.RTC.dtlsPrivateKeyFile.empty())
GenerateCertificateAndPrivateKey(); GenerateCertificateAndPrivateKey();
else else
ReadCertificateAndPrivateKeyFromFiles(); ReadCertificateAndPrivateKeyFromFiles();
...@@ -208,8 +207,8 @@ namespace RTC ...@@ -208,8 +207,8 @@ namespace RTC
LOG_OPENSSL_ERROR("X509_get_subject_name() failed"); LOG_OPENSSL_ERROR("X509_get_subject_name() failed");
goto error; goto error;
} }
X509_NAME_add_entry_by_txt(cert_name, "O", MBSTRING_ASC, (unsigned char *)Version::GetNameAndVersion().c_str(), -1, -1, 0); X509_NAME_add_entry_by_txt(cert_name, "O", MBSTRING_ASC, (unsigned char *)"Mediasoup", -1, -1, 0);
X509_NAME_add_entry_by_txt(cert_name, "CN", MBSTRING_ASC, (unsigned char *)Version::name.c_str(), -1, -1, 0); X509_NAME_add_entry_by_txt(cert_name, "CN", MBSTRING_ASC, (unsigned char *)"Mediasoup", -1, -1, 0);
// It is self signed so set the issuer name to be the same as the subject. // It is self signed so set the issuer name to be the same as the subject.
ret = X509_set_issuer_name(DTLSHandler::certificate, cert_name); ret = X509_set_issuer_name(DTLSHandler::certificate, cert_name);
...@@ -1176,4 +1175,4 @@ namespace RTC ...@@ -1176,4 +1175,4 @@ namespace RTC
SendPendingOutgoingDTLSData(); SendPendingOutgoingDTLSData();
SetTimeout(); SetTimeout();
} }
} // namespace RTC }
...@@ -179,7 +179,7 @@ namespace RTC ...@@ -179,7 +179,7 @@ namespace RTC
{ {
// Compute the CRC32 of the received message up to (but excluding) the // Compute the CRC32 of the received message up to (but excluding) the
// FINGERPRINT attribute and XOR it with 0x5354554e. // FINGERPRINT attribute and XOR it with 0x5354554e.
MS_4BYTES computed_fingerprint = Utils::Crypto::CRC32(data, fingerprint_attr_pos) ^ 0x5354554e; MS_4BYTES computed_fingerprint = Utils::Crypto::GetCRC32(data, fingerprint_attr_pos) ^ 0x5354554e;
// Compare with the FINGERPRINT value in the message. // Compare with the FINGERPRINT value in the message.
if (fingerprint != computed_fingerprint) if (fingerprint != computed_fingerprint)
...@@ -254,7 +254,7 @@ namespace RTC ...@@ -254,7 +254,7 @@ namespace RTC
Utils::Byte::Set2Bytes(this->raw, 2, (MS_2BYTES)(this->length - 20 - 8)); Utils::Byte::Set2Bytes(this->raw, 2, (MS_2BYTES)(this->length - 20 - 8));
// Calculate the HMAC-SHA1 of the message according to MESSAGE-INTEGRITY rules. // Calculate the HMAC-SHA1 of the message according to MESSAGE-INTEGRITY rules.
const MS_BYTE* computed_message_integrity = Utils::Crypto::HMAC_SHA1(local_password, this->raw, (this->messageIntegrity - 4) - this->raw); const MS_BYTE* computed_message_integrity = Utils::Crypto::GetHMAC_SHA1(local_password, this->raw, (this->messageIntegrity - 4) - this->raw);
Authentication result; Authentication result;
...@@ -525,7 +525,7 @@ namespace RTC ...@@ -525,7 +525,7 @@ namespace RTC
Utils::Byte::Set2Bytes(this->raw, 2, (MS_2BYTES)(this->length - 20 - 8)); Utils::Byte::Set2Bytes(this->raw, 2, (MS_2BYTES)(this->length - 20 - 8));
// Calculate the HMAC-SHA1 of the message according to MESSAGE-INTEGRITY rules. // Calculate the HMAC-SHA1 of the message according to MESSAGE-INTEGRITY rules.
const MS_BYTE* computed_message_integrity = Utils::Crypto::HMAC_SHA1(this->password, this->raw, pos); const MS_BYTE* computed_message_integrity = Utils::Crypto::GetHMAC_SHA1(this->password, this->raw, pos);
Utils::Byte::Set2Bytes(this->raw, pos, (MS_2BYTES)Attribute::MessageIntegrity); Utils::Byte::Set2Bytes(this->raw, pos, (MS_2BYTES)Attribute::MessageIntegrity);
Utils::Byte::Set2Bytes(this->raw, pos + 2, 20); Utils::Byte::Set2Bytes(this->raw, pos + 2, 20);
...@@ -550,7 +550,7 @@ namespace RTC ...@@ -550,7 +550,7 @@ namespace RTC
{ {
// Compute the CRC32 of the message up to (but excluding) the FINGERPRINT // Compute the CRC32 of the message up to (but excluding) the FINGERPRINT
// attribute and XOR it with 0x5354554e. // attribute and XOR it with 0x5354554e.
MS_4BYTES computed_fingerprint = Utils::Crypto::CRC32(this->raw, pos) ^ 0x5354554e; MS_4BYTES computed_fingerprint = Utils::Crypto::GetCRC32(this->raw, pos) ^ 0x5354554e;
Utils::Byte::Set2Bytes(this->raw, pos, (MS_2BYTES)Attribute::Fingerprint); Utils::Byte::Set2Bytes(this->raw, pos, (MS_2BYTES)Attribute::Fingerprint);
Utils::Byte::Set2Bytes(this->raw, pos + 2, 4); Utils::Byte::Set2Bytes(this->raw, pos + 2, 4);
......
...@@ -35,7 +35,6 @@ namespace RTC ...@@ -35,7 +35,6 @@ namespace RTC
/* Static methods. */ /* Static methods. */
// NOTE: Must be called from MediaSoup::Init().
void UDPSocket::ClassInit() void UDPSocket::ClassInit()
{ {
MS_TRACE(); MS_TRACE();
...@@ -95,11 +94,6 @@ namespace RTC ...@@ -95,11 +94,6 @@ namespace RTC
sockets[1] = new RTC::UDPSocket(uvHandles[1]); sockets[1] = new RTC::UDPSocket(uvHandles[1]);
} }
/*
* By calling this method, two different workers (threads) can access to the
* static map of available ports. No problem since the map keys are not modified,
* and values are a MS_PORT (uint16_t) which is atomic.
*/
void UDPSocket::RandomizePort(int address_family, uv_udp_t* uvHandles[], bool pair) void UDPSocket::RandomizePort(int address_family, uv_udp_t* uvHandles[], bool pair)
{ {
MS_TRACE(); MS_TRACE();
......
#define MS_CLASS "Settings" #define MS_CLASS "Settings"
#include "Settings.h" #include "Settings.h"
#include "Version.h"
#include "Utils.h" #include "Utils.h"
#include "MediaSoupError.h" #include "MediaSoupError.h"
#include "Logger.h" #include "Logger.h"
#include <string> #include <string>
#include <map> #include <map>
#include <algorithm> // std::transform() #include <algorithm> // std::transform()
#include <cstdio> // std::fprintf() #include <cctype> // isprint()
#include <cstdlib> // std::_Exit()
#include <cerrno> #include <cerrno>
#include <unistd.h> // close() #include <unistd.h> // close()
#include <uv.h> #include <uv.h>
...@@ -19,9 +17,13 @@ extern "C" ...@@ -19,9 +17,13 @@ extern "C"
#include <syslog.h> #include <syslog.h>
} }
/* Helpers declaration. */
static
bool IsBindableIP(const std::string &ip, int family, int* _bind_err);
/* Static variables. */ /* Static variables. */
struct Settings::Arguments Settings::arguments;
struct Settings::Configuration Settings::configuration; struct Settings::Configuration Settings::configuration;
std::map<std::string, unsigned int> Settings::string2LogLevel = std::map<std::string, unsigned int> Settings::string2LogLevel =
{ {
...@@ -66,355 +68,219 @@ std::map<unsigned int, std::string> Settings::syslogFacility2String = ...@@ -66,355 +68,219 @@ std::map<unsigned int, std::string> Settings::syslogFacility2String =
/* Static methods. */ /* Static methods. */
void Settings::ReadArguments(int argc, char* argv[]) void Settings::SetConfiguration(int argc, char* argv[])
{ {
MS_TRACE(); MS_TRACE();
/* Set default configuration. */
SetDefaultRtcListenIP(AF_INET);
SetDefaultRtcListenIP(AF_INET6);
/* Variables for getopt. */ /* Variables for getopt. */
int c;
int option_index = 0;
extern char *optarg; extern char *optarg;
extern int optind, opterr, optopt; extern int optind, opterr, optopt;
int c;
// For getopt_long(). int option_index = 0;
// Begin with ":" so we get ':' error in case a valid option requires argument. std::string value_string;
std::string short_options = ":c:dp:u:g:vh";
struct option options[] =
// For getopt_long(). {
struct option long_options[] = { "logLevel", optional_argument, nullptr, 'l' },
{ { "syslogFacility", optional_argument, nullptr, 'f' },
{ "configfile", required_argument, nullptr, 'c' }, { "rtcListenIPv4", optional_argument, nullptr, '4' },
{ "daemonize", no_argument, nullptr, 'd' }, { "rtcListenIPv6", optional_argument, nullptr, '6' },
{ "pidfile", required_argument, nullptr, 'p' }, { "rtcMinPort", optional_argument, nullptr, 'm' },
{ "user", required_argument, nullptr, 'u' }, { "rtcMaxPort", optional_argument, nullptr, 'M' },
{ "group", required_argument, nullptr, 'g' }, { "dtlsCertificateFile", optional_argument, nullptr, 'c' },
{ "version", no_argument, nullptr, 'v' }, { "dtlsPrivateKeyFile", optional_argument, nullptr, 'p' },
{ "help", no_argument, nullptr, 'h' },
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
// A map for associating short and long option names.
std::map<char, std::string> long_option_names =
{
{ 'c', "configfile" },
{ 'd', "daemonize" },
{ 'p', "pidfile" },
{ 'u', "user" },
{ 'g', "group" },
{ 'v', "version" },
{ 'h', "help" }
};
/* Parse command line options. */ /* Parse command line options. */
opterr = 0; // Don't allow getopt to print error messages. opterr = 0; // Don't allow getopt to print error messages.
while ((c = getopt_long(argc, argv, short_options.c_str(), long_options, &option_index)) != -1) while ((c = getopt_long_only(argc, argv, "", options, &option_index)) != -1)
{ {
if (!optarg)
MS_THROW_ERROR("parameters without value not allowed");
switch (c) switch (c)
{ {
case 'c': case 'l':
Settings::arguments.configFile = optarg; value_string = std::string(optarg);
SetLogLevel(value_string);
break; break;
case 'd': case 'f':
Settings::arguments.daemonize = true; value_string = std::string(optarg);
SetSyslogFacility(value_string);
break; break;
case 'p': case '4':
Settings::arguments.pidFile = optarg; value_string = std::string(optarg);
SetRtcListenIPv4(value_string);
break; break;
case 'u': case '6':
Settings::arguments.user = optarg; value_string = std::string(optarg);
SetRtcListenIPv6(value_string);
break; break;
case 'g': case 'm':
Settings::arguments.group = optarg; Settings::configuration.RTC.minPort = std::stoi(optarg);
break; break;
case 'v': case 'M':
Settings::PrintVersion(); Settings::configuration.RTC.maxPort = std::stoi(optarg);
std::_Exit(EXIT_SUCCESS);
break; break;
case 'h': case 'c':
Settings::PrintVersion(); value_string = std::string(optarg);
Settings::PrintHelp(false); Settings::configuration.RTC.dtlsCertificateFile = value_string;
std::_Exit(EXIT_SUCCESS); break;
case 'p':
value_string = std::string(optarg);
Settings::configuration.RTC.dtlsPrivateKeyFile = value_string;
break; break;
// Invalid option. // Invalid option.
case '?': case '?':
if (isprint(optopt)) if (isprint(optopt))
{ MS_THROW_ERROR("invalid option '-%c'", (char)optopt);
MS_ERROR("invalid option '-%c'", (char)optopt);
Settings::PrintHelp(true);
std::_Exit(EXIT_FAILURE);
}
else else
{ MS_THROW_ERROR("unknown long option given as argument");
MS_ERROR("unknown long option given as argument");
Settings::PrintHelp(true);
std::_Exit(EXIT_FAILURE);
}
// Valid option, but it requires and argument that is not given. // Valid option, but it requires and argument that is not given.
case ':': case ':':
MS_ERROR("option '-%c' or '--%s' requires an argument", (char)optopt, long_option_names[(char)optopt].c_str()); MS_THROW_ERROR("option '%c' requires an argument", (char)optopt);
Settings::PrintHelp(true);
std::_Exit(EXIT_FAILURE);
break;
// This should never happen. // This should never happen.
default: default:
MS_ABORT("'default' should never happen"); MS_THROW_ERROR("'default' should never happen");
} }
} }
// Ensure there are no more command line arguments after parsed options. // Ensure there are no more command line arguments after parsed options.
if (optind != argc) if (optind != argc)
MS_EXIT_FAILURE("there are remaining arguments after parsing command line options"); MS_THROW_ERROR("there are remaining arguments after parsing command line options");
// Ensure that PID file is not given when in foreground mode.
if (!Settings::arguments.pidFile.empty() && !Settings::arguments.daemonize)
MS_EXIT_FAILURE("PID file option requires daemon mode");
}
void Settings::PrintHelp(bool error)
{
MS_TRACE();
std::string help;
help.append("\nUsage: "); /* Post configuration. */
help.append(Version::command);
help.append(" [options]\n");
help.append("Options:\n"); // RTC must have at least 'listenIPv4' or 'listenIPv6'.
if (!Settings::configuration.RTC.hasIPv4 && !Settings::configuration.RTC.hasIPv6)
help.append(" -c, --configfile FILE Path to the configuration file\n"); MS_THROW_ERROR("at least RTC.listenIPv4 or RTC.listenIPv6 must be enabled");
help.append(" -d, --daemonize Run in daemon mode\n");
help.append(" -p, --pidfile FILE Create a PID file (requires daemon mode)\n");
help.append(" -u, --user USER Run with the given system user\n");
help.append(" -g, --group GROUP Run with the given system group\n");
help.append(" -v, --version Show version\n");
help.append(" -h, --help Show this message\n");
if (error)
std::fprintf(stderr, "%s", help.c_str());
else
std::fprintf(stdout, "%s", help.c_str());
}
void Settings::PrintVersion()
{
MS_TRACE();
std::fprintf(stdout, "%s\n", Version::GetNameAndVersion().c_str());
std::fprintf(stdout, "%s\n", Version::copyright.c_str());
}
void Settings::SetDefaultConfiguration() // Validate RTC ports.
{ Settings::SetRtcPorts();
MS_TRACE();
SetDefaultNumWorkers(); // Set DTLS certificate files (if provided),
SetDefaultRTClistenIP(AF_INET); Settings::SetDtlsCertificateAndPrivateKeyFiles();
SetDefaultRTClistenIP(AF_INET6);
} }
void Settings::ReadConfigurationFile() void Settings::PrintConfiguration()
{ {
MS_TRACE(); MS_TRACE();
if (Settings::arguments.configFile.empty()) MS_DEBUG("[configuration]");
return;
libconfig::Config* config;
try
{
config = ParseConfigFile();
}
catch (const MediaSoupError &error)
{
MS_EXIT_FAILURE("%s", error.what());
}
std::string str_value;
std::string str_value2;
int int_value;
int int_value2;
bool bool_value;
std::string empty_string;
try
{
/* First level settings. */
if (config->lookupValue("logLevel", str_value))
SetLogLevel(str_value);
if (config->lookupValue("syslogFacility", str_value))
SetSyslogFacility(str_value);
if (config->lookupValue("numWorkers", int_value))
SetNumWorkers(int_value);
/* ControlProtocol section. */
if (config->lookupValue("ControlProtocol.listenIP", str_value)) MS_DEBUG("- logLevel: \"%s\"", Settings::logLevel2String[Settings::configuration.logLevel].c_str());
SetControlProtocolListenIP(str_value); MS_DEBUG("- syslogFacility: \"%s\"", Settings::syslogFacility2String[Settings::configuration.syslogFacility].c_str());
if (config->lookupValue("ControlProtocol.listenPort", int_value)) MS_DEBUG("- RTC:");
SetControlProtocolListenPort(int_value); if (Settings::configuration.RTC.hasIPv4)
MS_DEBUG(" - listenIPv4: \"%s\"", Settings::configuration.RTC.listenIPv4.c_str());
/* RTC section. */ else
MS_DEBUG(" - listenIPv4: (unavailable)");
if (config->lookupValue("RTC.listenIPv4", str_value)) if (Settings::configuration.RTC.hasIPv6)
SetRTClistenIPv4(str_value); MS_DEBUG(" - listenIPv6: \"%s\"", Settings::configuration.RTC.listenIPv6.c_str());
else if ((config->lookupValue("RTC.listenIPv4", bool_value)) && bool_value == false) else
SetRTClistenIPv4(empty_string); MS_DEBUG(" - listenIPv6: (unavailable)");
MS_DEBUG(" - minPort: %d", Settings::configuration.RTC.minPort);
if (config->lookupValue("RTC.listenIPv6", str_value)) MS_DEBUG(" - maxPort: %d", Settings::configuration.RTC.maxPort);
SetRTClistenIPv6(str_value); if (!Settings::configuration.RTC.dtlsCertificateFile.empty())
else if ((config->lookupValue("RTC.listenIPv6", bool_value)) && bool_value == false)
SetRTClistenIPv6(empty_string);
if (config->lookupValue("RTC.minPort", int_value) && config->lookupValue("RTC.maxPort", int_value2))
SetRTCports(int_value, int_value2);
if (config->lookupValue("RTC.dtlsCertificateFile", str_value) && config->lookupValue("RTC.dtlsPrivateKeyFile", str_value2))
SetDtlsCertificateAndPrivateKeyFiles(str_value, str_value2);
}
catch (const MediaSoupError &error)
{ {
delete config; MS_DEBUG(" - dtlsCertificateFile: \"%s\"", Settings::configuration.RTC.dtlsCertificateFile.c_str());
MS_EXIT_FAILURE("error in configuration file: %s", error.what()); MS_DEBUG(" - dtlsPrivateKeyFile: \"%s\"", Settings::configuration.RTC.dtlsPrivateKeyFile.c_str());
} }
delete config; MS_DEBUG("[/configuration]");
} }
bool Settings::ReloadConfigurationFile() void Settings::SetDefaultRtcListenIP(int requested_family)
{ {
MS_TRACE(); MS_TRACE();
if (Settings::arguments.configFile.empty()) int err;
{ uv_interface_address_t* addresses;
MS_ERROR("no configuration file was given in command line options"); int num_addresses;
return false; std::string ipv4;
} std::string ipv6;
int bind_errno;
libconfig::Config* config; err = uv_interface_addresses(&addresses, &num_addresses);
if (err)
MS_ABORT("uv_interface_addresses() failed: %s", uv_strerror(err));
try for (int i = 0; i < num_addresses; i++)
{
config = ParseConfigFile();
}
catch (const MediaSoupError &error)
{ {
MS_ERROR("%s", error.what()); uv_interface_address_t address = addresses[i];
return false;
}
std::string str_value; // Ignore internal addresses.
if (address.is_internal)
continue;
// Just some configuration settings can be reloaded. int family;
MS_PORT port;
std::string ip;
Utils::IP::GetAddressInfo((struct sockaddr*)(&address.address.address4), &family, ip, &port);
try if (family != requested_family)
{ continue;
/* First level settings. */
if (config->lookupValue("logLevel", str_value)) switch (family)
SetLogLevel(str_value);
else
Settings::configuration.logLevel = LOG_DEBUG;
}
catch (const MediaSoupError &error)
{ {
MS_ERROR("error in configuration file: %s", error.what()); case AF_INET:
delete config; // Ignore if already got an IPv4.
return false; if (!ipv4.empty())
} continue;
delete config;
return true;
}
void Settings::PrintConfiguration()
{
MS_TRACE();
MS_INFO("[configuration]");
MS_INFO("- logLevel: \"%s\"", Settings::logLevel2String[Settings::configuration.logLevel].c_str());
MS_INFO("- syslogFacility: \"%s\"", Settings::syslogFacility2String[Settings::configuration.syslogFacility].c_str());
MS_INFO("- numWorkers: %d", Settings::configuration.numWorkers);
MS_INFO("- ControlProtocol:");
MS_INFO(" - listenIP: \"%s\"", Settings::configuration.ControlProtocol.listenIP.c_str());
MS_INFO(" - listenPort: %d", Settings::configuration.ControlProtocol.listenPort);
MS_INFO("- RTC:"); // Check if it is bindable.
if (Settings::configuration.RTC.hasIPv4) if (!IsBindableIP(ip, AF_INET, &bind_errno))
MS_INFO(" - listenIPv4: \"%s\"", Settings::configuration.RTC.listenIPv4.c_str()); continue;
else
MS_INFO(" - listenIPv4: (unavailable)");
if (Settings::configuration.RTC.hasIPv6)
MS_INFO(" - listenIPv6: \"%s\"", Settings::configuration.RTC.listenIPv6.c_str());
else
MS_INFO(" - listenIPv6: (unavailable)");
MS_INFO(" - minPort: %d", Settings::configuration.RTC.minPort);
MS_INFO(" - maxPort: %d", Settings::configuration.RTC.maxPort);
if (!Settings::configuration.RTC.dtlsCertificateFile.empty())
{
MS_INFO(" - dtlsCertificateFile: \"%s\"", Settings::configuration.RTC.dtlsCertificateFile.c_str());
MS_INFO(" - dtlsPrivateKeyFile: \"%s\"", Settings::configuration.RTC.dtlsPrivateKeyFile.c_str());
}
MS_INFO("[/configuration]"); ipv4 = ip;
} break;
libconfig::Config* Settings::ParseConfigFile() case AF_INET6:
{ // Ignore if already got an IPv6.
MS_TRACE(); if (!ipv6.empty())
continue;
const char* config_file = Settings::arguments.configFile.c_str(); // Check if it is bindable.
if (!IsBindableIP(ip, AF_INET6, &bind_errno))
continue;
try ipv6 = ip;
{ break;
Utils::File::CheckFile(config_file);
} }
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("error reading configuration file: %s", error.what());
} }
libconfig::Config* config = new libconfig::Config(); if (!ipv4.empty())
try
{
config->readFile(config_file);
}
catch (const libconfig::ParseException &error)
{
delete config;
MS_THROW_ERROR("parsing error in configuration file '%s': %s in line %d", config_file, error.getError(), error.getLine());
}
catch (const libconfig::FileIOException &error)
{ {
delete config; Settings::configuration.RTC.listenIPv4 = ipv4;
MS_THROW_ERROR("cannot read configuration file '%s'", config_file); Settings::configuration.RTC.hasIPv4 = true;
} }
catch (const libconfig::ConfigException &error)
if (!ipv6.empty())
{ {
delete config; Settings::configuration.RTC.listenIPv6 = ipv6;
MS_THROW_ERROR("error reading configuration file '%s': %s", config_file, error.what()); Settings::configuration.RTC.hasIPv6 = true;
} }
return config; uv_free_interface_addresses(addresses, num_addresses);
} }
void Settings::SetLogLevel(std::string &level) void Settings::SetLogLevel(std::string &level)
...@@ -443,60 +309,7 @@ void Settings::SetSyslogFacility(std::string &facility) ...@@ -443,60 +309,7 @@ void Settings::SetSyslogFacility(std::string &facility)
Settings::configuration.syslogFacility = Settings::string2SyslogFacility[facility]; Settings::configuration.syslogFacility = Settings::string2SyslogFacility[facility];
} }
void Settings::SetNumWorkers(int numWorkers) void Settings::SetRtcListenIPv4(const std::string &ip)
{
MS_TRACE();
if (numWorkers < 0)
MS_THROW_ERROR("numWorkers must be greater or equal than 0");
// If 0 let the already set default value.
else if (numWorkers == 0)
return;
Settings::configuration.numWorkers = numWorkers;
}
void Settings::SetDefaultNumWorkers()
{
MS_TRACE();
int err;
uv_cpu_info_t* cpus;
int num_cpus;
err = uv_cpu_info(&cpus, &num_cpus);
if (err)
MS_ABORT("uv_cpu_info() failed: %s", uv_strerror(err));
uv_free_cpu_info(cpus, num_cpus);
MS_DEBUG("auto-detected value for numWorkers: %d", num_cpus);
Settings::configuration.numWorkers = num_cpus;
}
void Settings::SetControlProtocolListenIP(const std::string &ip)
{
MS_TRACE();
switch (Utils::IP::GetFamily(ip))
{
case AF_INET:
case AF_INET6:
Settings::configuration.ControlProtocol.listenIP = ip;
break;
default:
MS_THROW_ERROR("invalid value '%s' for ControlProtocol.listenIP", ip.c_str());
}
}
void Settings::SetControlProtocolListenPort(MS_PORT port)
{
MS_TRACE();
Settings::configuration.ControlProtocol.listenPort = port;
}
void Settings::SetRTClistenIPv4(const std::string &ip)
{ {
MS_TRACE(); MS_TRACE();
...@@ -521,12 +334,12 @@ void Settings::SetRTClistenIPv4(const std::string &ip) ...@@ -521,12 +334,12 @@ void Settings::SetRTClistenIPv4(const std::string &ip)
MS_THROW_ERROR("invalid value '%s' for RTC.listenIPv4", ip.c_str()); MS_THROW_ERROR("invalid value '%s' for RTC.listenIPv4", ip.c_str());
} }
int _bind_err; int bind_errno;
if (!IsBindableIP(ip, AF_INET, &_bind_err)) if (!IsBindableIP(ip, AF_INET, &bind_errno))
MS_THROW_ERROR("cannot bind on '%s' for RTC.listenIPv4: %s", ip.c_str(), std::strerror(_bind_err)); MS_THROW_ERROR("cannot bind on '%s' for RTC.listenIPv4: %s", ip.c_str(), std::strerror(bind_errno));
} }
void Settings::SetRTClistenIPv6(const std::string &ip) void Settings::SetRtcListenIPv6(const std::string &ip)
{ {
MS_TRACE(); MS_TRACE();
...@@ -551,101 +364,84 @@ void Settings::SetRTClistenIPv6(const std::string &ip) ...@@ -551,101 +364,84 @@ void Settings::SetRTClistenIPv6(const std::string &ip)
MS_THROW_ERROR("invalid value '%s' for RTC.listenIPv6", ip.c_str()); MS_THROW_ERROR("invalid value '%s' for RTC.listenIPv6", ip.c_str());
} }
int _bind_err; int bind_errno;
if (!IsBindableIP(ip, AF_INET6, &_bind_err)) if (!IsBindableIP(ip, AF_INET6, &bind_errno))
MS_THROW_ERROR("cannot bind on '%s' for RTC.listenIPv6: %s", ip.c_str(), std::strerror(_bind_err)); MS_THROW_ERROR("cannot bind on '%s' for RTC.listenIPv6: %s", ip.c_str(), std::strerror(bind_errno));
} }
void Settings::SetDefaultRTClistenIP(int requested_family) void Settings::SetRtcPorts()
{ {
MS_TRACE(); MS_TRACE();
int err; MS_PORT minPort = Settings::configuration.RTC.minPort;
uv_interface_address_t* addresses; MS_PORT maxPort = Settings::configuration.RTC.maxPort;
int num_addresses;
std::string ipv4;
std::string ipv6;
int _bind_err;
err = uv_interface_addresses(&addresses, &num_addresses); if (minPort < 1024)
if (err) MS_THROW_ERROR("RTC.minPort must be greater or equal than 1024");
MS_ABORT("uv_interface_addresses() failed: %s", uv_strerror(err));
for (int i = 0; i < num_addresses; i++) if (maxPort == 0)
{ MS_THROW_ERROR("RTC.maxPort can not be 0");
uv_interface_address_t address = addresses[i];
// Ignore internal addresses. // Make minPort even.
if (address.is_internal) minPort &= ~1;
continue;
int family; // Make maxPort odd.
MS_PORT port; if (maxPort % 2 == 0)
std::string ip; maxPort--;
Utils::IP::GetAddressInfo((struct sockaddr*)(&address.address.address4), &family, ip, &port);
if (family != requested_family) if ((maxPort - minPort) < 99)
continue; MS_THROW_ERROR("RTC.maxPort must be at least 99 ports higher than RTC.minPort");
switch (family) Settings::configuration.RTC.minPort = minPort;
{ Settings::configuration.RTC.maxPort = maxPort;
case AF_INET: }
// Ignore if already got an IPv4.
if (!ipv4.empty())
continue;
// Check if it is bindable. void Settings::SetDtlsCertificateAndPrivateKeyFiles()
if (!IsBindableIP(ip, AF_INET, &_bind_err)) {
MS_TRACE();
if (Settings::configuration.RTC.dtlsCertificateFile.empty() || Settings::configuration.RTC.dtlsPrivateKeyFile.empty())
{ {
MS_DEBUG("ignoring '%s' for RTC.listenIPv4: %s", ip.c_str(), std::strerror(errno)); Settings::configuration.RTC.dtlsCertificateFile = "";
continue; Settings::configuration.RTC.dtlsPrivateKeyFile = "";
return;
} }
MS_DEBUG("auto-discovered '%s' for RTC.listenIPv4", ip.c_str()); std::string dtlsCertificateFile = Settings::configuration.RTC.dtlsCertificateFile;
ipv4 = ip; std::string dtlsPrivateKeyFile = Settings::configuration.RTC.dtlsPrivateKeyFile;
break;
case AF_INET6:
// Ignore if already got an IPv6.
if (!ipv6.empty())
continue;
// Check if it is bindable. try
if (!IsBindableIP(ip, AF_INET6, &_bind_err))
{ {
MS_DEBUG("ignoring '%s' for RTC.listenIPv6: %s", ip.c_str(), std::strerror(errno)); Utils::File::CheckFile(dtlsCertificateFile.c_str());
continue;
}
MS_DEBUG("auto-discovered '%s' for RTC.listenIPv6", ip.c_str());
ipv6 = ip;
break;
} }
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("RTC.dtlsCertificateFile: %s", error.what());
} }
if (!ipv4.empty()) try
{ {
Settings::configuration.RTC.listenIPv4 = ipv4; Utils::File::CheckFile(dtlsPrivateKeyFile.c_str());
Settings::configuration.RTC.hasIPv4 = true;
} }
catch (const MediaSoupError &error)
if (!ipv6.empty())
{ {
Settings::configuration.RTC.listenIPv6 = ipv6; MS_THROW_ERROR("RTC.dtlsPrivateKeyFile: %s", error.what());
Settings::configuration.RTC.hasIPv6 = true;
} }
uv_free_interface_addresses(addresses, num_addresses); Settings::configuration.RTC.dtlsCertificateFile = dtlsCertificateFile;
Settings::configuration.RTC.dtlsPrivateKeyFile = dtlsPrivateKeyFile;
} }
bool Settings::IsBindableIP(const std::string &ip, int family, int* _bind_err) /* Helpers. */
bool IsBindableIP(const std::string &ip, int family, int* bind_errno)
{ {
MS_TRACE(); MS_TRACE();
struct sockaddr_storage bind_addr; struct sockaddr_storage bind_addr;
int bind_socket; int bind_socket;
int err = 0; int err = 0;
bool bind_ok; bool success;
switch (family) switch (family)
{ {
...@@ -678,76 +474,17 @@ bool Settings::IsBindableIP(const std::string &ip, int family, int* _bind_err) ...@@ -678,76 +474,17 @@ bool Settings::IsBindableIP(const std::string &ip, int family, int* _bind_err)
if (err == 0) if (err == 0)
{ {
bind_ok = true; success = true;
} }
else else
{ {
bind_ok = false; success = false;
*_bind_err = errno; *bind_errno = errno;
} }
err = close(bind_socket); err = close(bind_socket);
if (err) if (err)
MS_ABORT("close() failed: %s", std::strerror(errno)); MS_ABORT("close() failed: %s", std::strerror(errno));
return bind_ok; return success;
}
void Settings::SetRTCports(MS_PORT minPort, MS_PORT maxPort)
{
MS_TRACE();
if (minPort < 1024)
MS_THROW_ERROR("RTC.minPort must be greater or equal than 1024");
if (maxPort == 0)
MS_THROW_ERROR("RTC.maxPort can not be 0");
// Make minPort even.
minPort &= ~1;
// Make maxPort odd.
if (maxPort % 2 == 0)
maxPort--;
if ((maxPort - minPort) < 99)
MS_THROW_ERROR("RTC.maxPort must be at least 99 ports higher than RTC.minPort");
Settings::configuration.RTC.minPort = minPort;
Settings::configuration.RTC.maxPort = maxPort;
}
void Settings::SetDtlsCertificateAndPrivateKeyFiles(std::string& dtlsCertificateFile, std::string& dtlsPrivateKeyFile)
{
MS_TRACE();
try
{
Utils::File::CheckFile(dtlsCertificateFile.c_str());
}
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("RTC.dtlsCertificateFile: %s", error.what());
}
try
{
Utils::File::CheckFile(dtlsPrivateKeyFile.c_str());
}
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("RTC.dtlsPrivateKeyFile: %s", error.what());
}
Settings::configuration.RTC.dtlsCertificateFile = dtlsCertificateFile;
Settings::configuration.RTC.dtlsPrivateKeyFile = dtlsPrivateKeyFile;
}
void Settings::ConfigurationPostCheck()
{
MS_TRACE();
// RTC must have at least 'listenIPv4' or 'listenIPv6'.
if (!Settings::configuration.RTC.hasIPv4 && !Settings::configuration.RTC.hasIPv6)
MS_EXIT_FAILURE("at least RTC.listenIPv4 or RTC.listenIPv6 must be enabled");
} }
...@@ -8,9 +8,9 @@ namespace Utils ...@@ -8,9 +8,9 @@ namespace Utils
{ {
/* Static variables. */ /* Static variables. */
__thread unsigned int Crypto::seed; unsigned int Crypto::seed;
__thread HMAC_CTX Crypto::hmacSha1Ctx; HMAC_CTX Crypto::hmacSha1Ctx;
__thread MS_BYTE Crypto::hmacSha1Buffer[20]; // SHA-1 result is 20 bytes long. MS_BYTE Crypto::hmacSha1Buffer[20]; // SHA-1 result is 20 bytes long.
const MS_4BYTES Crypto::crc32Table[] = const MS_4BYTES Crypto::crc32Table[] =
{ {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
...@@ -60,10 +60,11 @@ namespace Utils ...@@ -60,10 +60,11 @@ namespace Utils
/* Static methods. */ /* Static methods. */
void Crypto::ThreadInit() void Crypto::ClassInit()
{ {
MS_TRACE(); MS_TRACE();
// TODO: refactor comment
// Init the seed of each thread with a random number taken from the address // Init the seed of each thread with a random number taken from the address
// of the seed variable itself (which is different for each thread). // of the seed variable itself (which is different for each thread).
Crypto::seed = (unsigned int)(unsigned long)&Crypto::seed; Crypto::seed = (unsigned int)(unsigned long)&Crypto::seed;
...@@ -72,14 +73,14 @@ namespace Utils ...@@ -72,14 +73,14 @@ namespace Utils
HMAC_CTX_init(&Crypto::hmacSha1Ctx); HMAC_CTX_init(&Crypto::hmacSha1Ctx);
} }
void Crypto::ThreadDestroy() void Crypto::ClassDestroy()
{ {
MS_TRACE(); MS_TRACE();
HMAC_CTX_cleanup(&Crypto::hmacSha1Ctx); HMAC_CTX_cleanup(&Crypto::hmacSha1Ctx);
} }
const MS_BYTE* Crypto::HMAC_SHA1(const std::string &key, const MS_BYTE* data, size_t len) const MS_BYTE* Crypto::GetHMAC_SHA1(const std::string &key, const MS_BYTE* data, size_t len)
{ {
MS_TRACE(); MS_TRACE();
...@@ -98,4 +99,4 @@ namespace Utils ...@@ -98,4 +99,4 @@ namespace Utils
return Crypto::hmacSha1Buffer; return Crypto::hmacSha1Buffer;
} }
} // namespace Utils }
...@@ -7,11 +7,10 @@ ...@@ -7,11 +7,10 @@
#include <unistd.h> // access(), R_OK #include <unistd.h> // access(), R_OK
#include <sys/stat.h> // stat() #include <sys/stat.h> // stat()
namespace Utils
namespace Utils { {
void Utils::File::CheckFile(const char* file)
{
void Utils::File::CheckFile(const char* file) {
MS_TRACE(); MS_TRACE();
struct stat file_stat; struct stat file_stat;
...@@ -30,7 +29,5 @@ void Utils::File::CheckFile(const char* file) { ...@@ -30,7 +29,5 @@ void Utils::File::CheckFile(const char* file) {
err = access(file, R_OK); err = access(file, R_OK);
if (err) if (err)
MS_THROW_ERROR("cannot read file '%s': %s", file, std::strerror(errno)); MS_THROW_ERROR("cannot read file '%s': %s", file, std::strerror(errno));
}
} }
} // namespace Utils
...@@ -21,13 +21,12 @@ namespace Utils ...@@ -21,13 +21,12 @@ namespace Utils
#line 37 "src/Utils/IP.rl" #line 37 "src/Utils/IP.rl"
/** /**
* Ragel: %%write data * Ragel: %%write data
* This generates Ragel's static variables. * This generates Ragel's static variables.
*/ */
#line 31 "src/Utils/IP.cpp" #line 30 "src/Utils/IP.cpp"
static const int IPParser_start = 1; static const int IPParser_start = 1;
...@@ -35,7 +34,7 @@ static const int IPParser_start = 1; ...@@ -35,7 +34,7 @@ static const int IPParser_start = 1;
#line 45 "src/Utils/IP.rl" #line 44 "src/Utils/IP.rl"
// Used by Ragel: // Used by Ragel:
size_t cs; size_t cs;
...@@ -49,19 +48,19 @@ static const int IPParser_start = 1; ...@@ -49,19 +48,19 @@ static const int IPParser_start = 1;
* Ragel: %%write init * Ragel: %%write init
*/ */
#line 53 "src/Utils/IP.cpp" #line 52 "src/Utils/IP.cpp"
{ {
cs = IPParser_start; cs = IPParser_start;
} }
#line 58 "src/Utils/IP.rl" #line 57 "src/Utils/IP.rl"
/** /**
* Ragel: %%write exec * Ragel: %%write exec
* This updates cs variable needed by Ragel. * This updates cs variable needed by Ragel.
*/ */
#line 65 "src/Utils/IP.cpp" #line 64 "src/Utils/IP.cpp"
{ {
if ( p == pe ) if ( p == pe )
goto _test_eof; goto _test_eof;
...@@ -169,7 +168,7 @@ st86: ...@@ -169,7 +168,7 @@ st86:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof86; goto _test_eof86;
case 86: case 86:
#line 173 "src/Utils/IP.cpp" #line 172 "src/Utils/IP.cpp"
goto st0; goto st0;
tr21: tr21:
#line 24 "src/Utils/IP.rl" #line 24 "src/Utils/IP.rl"
...@@ -181,7 +180,7 @@ st87: ...@@ -181,7 +180,7 @@ st87:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof87; goto _test_eof87;
case 87: case 87:
#line 185 "src/Utils/IP.cpp" #line 184 "src/Utils/IP.cpp"
if ( 48u <= (*p) && (*p) <= 57u ) if ( 48u <= (*p) && (*p) <= 57u )
goto tr23; goto tr23;
goto st0; goto st0;
...@@ -195,7 +194,7 @@ st88: ...@@ -195,7 +194,7 @@ st88:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof88; goto _test_eof88;
case 88: case 88:
#line 199 "src/Utils/IP.cpp" #line 198 "src/Utils/IP.cpp"
if ( 48u <= (*p) && (*p) <= 57u ) if ( 48u <= (*p) && (*p) <= 57u )
goto tr20; goto tr20;
goto st0; goto st0;
...@@ -209,7 +208,7 @@ st89: ...@@ -209,7 +208,7 @@ st89:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof89; goto _test_eof89;
case 89: case 89:
#line 213 "src/Utils/IP.cpp" #line 212 "src/Utils/IP.cpp"
if ( (*p) == 53u ) if ( (*p) == 53u )
goto tr120; goto tr120;
if ( (*p) > 52u ) { if ( (*p) > 52u ) {
...@@ -228,7 +227,7 @@ st90: ...@@ -228,7 +227,7 @@ st90:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof90; goto _test_eof90;
case 90: case 90:
#line 232 "src/Utils/IP.cpp" #line 231 "src/Utils/IP.cpp"
if ( 48u <= (*p) && (*p) <= 53u ) if ( 48u <= (*p) && (*p) <= 53u )
goto tr20; goto tr20;
goto st0; goto st0;
...@@ -782,7 +781,7 @@ st91: ...@@ -782,7 +781,7 @@ st91:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof91; goto _test_eof91;
case 91: case 91:
#line 786 "src/Utils/IP.cpp" #line 785 "src/Utils/IP.cpp"
if ( 48u <= (*p) && (*p) <= 57u ) if ( 48u <= (*p) && (*p) <= 57u )
goto tr80; goto tr80;
goto st0; goto st0;
...@@ -796,7 +795,7 @@ st92: ...@@ -796,7 +795,7 @@ st92:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof92; goto _test_eof92;
case 92: case 92:
#line 800 "src/Utils/IP.cpp" #line 799 "src/Utils/IP.cpp"
if ( 48u <= (*p) && (*p) <= 57u ) if ( 48u <= (*p) && (*p) <= 57u )
goto tr77; goto tr77;
goto st0; goto st0;
...@@ -810,7 +809,7 @@ st93: ...@@ -810,7 +809,7 @@ st93:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof93; goto _test_eof93;
case 93: case 93:
#line 814 "src/Utils/IP.cpp" #line 813 "src/Utils/IP.cpp"
if ( (*p) == 53u ) if ( (*p) == 53u )
goto tr121; goto tr121;
if ( (*p) > 52u ) { if ( (*p) > 52u ) {
...@@ -829,7 +828,7 @@ st94: ...@@ -829,7 +828,7 @@ st94:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof94; goto _test_eof94;
case 94: case 94:
#line 833 "src/Utils/IP.cpp" #line 832 "src/Utils/IP.cpp"
if ( 48u <= (*p) && (*p) <= 53u ) if ( 48u <= (*p) && (*p) <= 53u )
goto tr77; goto tr77;
goto st0; goto st0;
...@@ -977,7 +976,7 @@ st95: ...@@ -977,7 +976,7 @@ st95:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof95; goto _test_eof95;
case 95: case 95:
#line 981 "src/Utils/IP.cpp" #line 980 "src/Utils/IP.cpp"
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
if ( 48u <= (*p) && (*p) <= 57u ) if ( 48u <= (*p) && (*p) <= 57u )
goto tr122; goto tr122;
...@@ -997,7 +996,7 @@ st96: ...@@ -997,7 +996,7 @@ st96:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof96; goto _test_eof96;
case 96: case 96:
#line 1001 "src/Utils/IP.cpp" #line 1000 "src/Utils/IP.cpp"
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
if ( 48u <= (*p) && (*p) <= 57u ) if ( 48u <= (*p) && (*p) <= 57u )
goto tr123; goto tr123;
...@@ -1017,7 +1016,7 @@ st97: ...@@ -1017,7 +1016,7 @@ st97:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof97; goto _test_eof97;
case 97: case 97:
#line 1021 "src/Utils/IP.cpp" #line 1020 "src/Utils/IP.cpp"
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
if ( 48u <= (*p) && (*p) <= 57u ) if ( 48u <= (*p) && (*p) <= 57u )
goto tr77; goto tr77;
...@@ -1163,7 +1162,7 @@ st98: ...@@ -1163,7 +1162,7 @@ st98:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof98; goto _test_eof98;
case 98: case 98:
#line 1167 "src/Utils/IP.cpp" #line 1166 "src/Utils/IP.cpp"
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
if ( 48u <= (*p) && (*p) <= 57u ) if ( 48u <= (*p) && (*p) <= 57u )
goto tr85; goto tr85;
...@@ -1198,7 +1197,7 @@ st99: ...@@ -1198,7 +1197,7 @@ st99:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof99; goto _test_eof99;
case 99: case 99:
#line 1202 "src/Utils/IP.cpp" #line 1201 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 48u: goto tr90; case 48u: goto tr90;
case 49u: goto tr91; case 49u: goto tr91;
...@@ -1223,7 +1222,7 @@ st100: ...@@ -1223,7 +1222,7 @@ st100:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof100; goto _test_eof100;
case 100: case 100:
#line 1227 "src/Utils/IP.cpp" #line 1226 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st71; case 58u: goto st71;
...@@ -1247,7 +1246,7 @@ st101: ...@@ -1247,7 +1246,7 @@ st101:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof101; goto _test_eof101;
case 101: case 101:
#line 1251 "src/Utils/IP.cpp" #line 1250 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st71; goto st71;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -1269,7 +1268,7 @@ st102: ...@@ -1269,7 +1268,7 @@ st102:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof102; goto _test_eof102;
case 102: case 102:
#line 1273 "src/Utils/IP.cpp" #line 1272 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st71; goto st71;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -1291,7 +1290,7 @@ st103: ...@@ -1291,7 +1290,7 @@ st103:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof103; goto _test_eof103;
case 103: case 103:
#line 1295 "src/Utils/IP.cpp" #line 1294 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st71; goto st71;
goto st0; goto st0;
...@@ -1318,7 +1317,7 @@ st104: ...@@ -1318,7 +1317,7 @@ st104:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof104; goto _test_eof104;
case 104: case 104:
#line 1322 "src/Utils/IP.cpp" #line 1321 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st71; case 58u: goto st71;
...@@ -1342,7 +1341,7 @@ st105: ...@@ -1342,7 +1341,7 @@ st105:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof105; goto _test_eof105;
case 105: case 105:
#line 1346 "src/Utils/IP.cpp" #line 1345 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st71; case 58u: goto st71;
...@@ -1366,7 +1365,7 @@ st106: ...@@ -1366,7 +1365,7 @@ st106:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof106; goto _test_eof106;
case 106: case 106:
#line 1370 "src/Utils/IP.cpp" #line 1369 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st71; case 58u: goto st71;
...@@ -1390,7 +1389,7 @@ st107: ...@@ -1390,7 +1389,7 @@ st107:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof107; goto _test_eof107;
case 107: case 107:
#line 1394 "src/Utils/IP.cpp" #line 1393 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 53u: goto tr130; case 53u: goto tr130;
...@@ -1418,7 +1417,7 @@ st108: ...@@ -1418,7 +1417,7 @@ st108:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof108; goto _test_eof108;
case 108: case 108:
#line 1422 "src/Utils/IP.cpp" #line 1421 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st71; case 58u: goto st71;
...@@ -1445,7 +1444,7 @@ st109: ...@@ -1445,7 +1444,7 @@ st109:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof109; goto _test_eof109;
case 109: case 109:
#line 1449 "src/Utils/IP.cpp" #line 1448 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st71; case 58u: goto st71;
...@@ -1469,7 +1468,7 @@ st110: ...@@ -1469,7 +1468,7 @@ st110:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof110; goto _test_eof110;
case 110: case 110:
#line 1473 "src/Utils/IP.cpp" #line 1472 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st71; case 58u: goto st71;
...@@ -1493,7 +1492,7 @@ st111: ...@@ -1493,7 +1492,7 @@ st111:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof111; goto _test_eof111;
case 111: case 111:
#line 1497 "src/Utils/IP.cpp" #line 1496 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st71; goto st71;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -1515,7 +1514,7 @@ st112: ...@@ -1515,7 +1514,7 @@ st112:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof112; goto _test_eof112;
case 112: case 112:
#line 1519 "src/Utils/IP.cpp" #line 1518 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 48u: goto tr95; case 48u: goto tr95;
case 49u: goto tr96; case 49u: goto tr96;
...@@ -1540,7 +1539,7 @@ st113: ...@@ -1540,7 +1539,7 @@ st113:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof113; goto _test_eof113;
case 113: case 113:
#line 1544 "src/Utils/IP.cpp" #line 1543 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st72; case 58u: goto st72;
...@@ -1564,7 +1563,7 @@ st114: ...@@ -1564,7 +1563,7 @@ st114:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof114; goto _test_eof114;
case 114: case 114:
#line 1568 "src/Utils/IP.cpp" #line 1567 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st72; goto st72;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -1586,7 +1585,7 @@ st115: ...@@ -1586,7 +1585,7 @@ st115:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof115; goto _test_eof115;
case 115: case 115:
#line 1590 "src/Utils/IP.cpp" #line 1589 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st72; goto st72;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -1608,7 +1607,7 @@ st116: ...@@ -1608,7 +1607,7 @@ st116:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof116; goto _test_eof116;
case 116: case 116:
#line 1612 "src/Utils/IP.cpp" #line 1611 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st72; goto st72;
goto st0; goto st0;
...@@ -1640,7 +1639,7 @@ st117: ...@@ -1640,7 +1639,7 @@ st117:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof117; goto _test_eof117;
case 117: case 117:
#line 1644 "src/Utils/IP.cpp" #line 1643 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st72; case 58u: goto st72;
...@@ -1664,7 +1663,7 @@ st118: ...@@ -1664,7 +1663,7 @@ st118:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof118; goto _test_eof118;
case 118: case 118:
#line 1668 "src/Utils/IP.cpp" #line 1667 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st72; case 58u: goto st72;
...@@ -1688,7 +1687,7 @@ st119: ...@@ -1688,7 +1687,7 @@ st119:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof119; goto _test_eof119;
case 119: case 119:
#line 1692 "src/Utils/IP.cpp" #line 1691 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st72; case 58u: goto st72;
...@@ -1712,7 +1711,7 @@ st120: ...@@ -1712,7 +1711,7 @@ st120:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof120; goto _test_eof120;
case 120: case 120:
#line 1716 "src/Utils/IP.cpp" #line 1715 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 53u: goto tr138; case 53u: goto tr138;
...@@ -1740,7 +1739,7 @@ st121: ...@@ -1740,7 +1739,7 @@ st121:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof121; goto _test_eof121;
case 121: case 121:
#line 1744 "src/Utils/IP.cpp" #line 1743 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st72; case 58u: goto st72;
...@@ -1767,7 +1766,7 @@ st122: ...@@ -1767,7 +1766,7 @@ st122:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof122; goto _test_eof122;
case 122: case 122:
#line 1771 "src/Utils/IP.cpp" #line 1770 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st72; case 58u: goto st72;
...@@ -1791,7 +1790,7 @@ st123: ...@@ -1791,7 +1790,7 @@ st123:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof123; goto _test_eof123;
case 123: case 123:
#line 1795 "src/Utils/IP.cpp" #line 1794 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st72; case 58u: goto st72;
...@@ -1815,7 +1814,7 @@ st124: ...@@ -1815,7 +1814,7 @@ st124:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof124; goto _test_eof124;
case 124: case 124:
#line 1819 "src/Utils/IP.cpp" #line 1818 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st72; goto st72;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -1837,7 +1836,7 @@ st125: ...@@ -1837,7 +1836,7 @@ st125:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof125; goto _test_eof125;
case 125: case 125:
#line 1841 "src/Utils/IP.cpp" #line 1840 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 48u: goto tr100; case 48u: goto tr100;
case 49u: goto tr101; case 49u: goto tr101;
...@@ -1862,7 +1861,7 @@ st126: ...@@ -1862,7 +1861,7 @@ st126:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof126; goto _test_eof126;
case 126: case 126:
#line 1866 "src/Utils/IP.cpp" #line 1865 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st73; case 58u: goto st73;
...@@ -1886,7 +1885,7 @@ st127: ...@@ -1886,7 +1885,7 @@ st127:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof127; goto _test_eof127;
case 127: case 127:
#line 1890 "src/Utils/IP.cpp" #line 1889 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st73; goto st73;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -1908,7 +1907,7 @@ st128: ...@@ -1908,7 +1907,7 @@ st128:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof128; goto _test_eof128;
case 128: case 128:
#line 1912 "src/Utils/IP.cpp" #line 1911 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st73; goto st73;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -1930,7 +1929,7 @@ st129: ...@@ -1930,7 +1929,7 @@ st129:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof129; goto _test_eof129;
case 129: case 129:
#line 1934 "src/Utils/IP.cpp" #line 1933 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st73; goto st73;
goto st0; goto st0;
...@@ -1962,7 +1961,7 @@ st130: ...@@ -1962,7 +1961,7 @@ st130:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof130; goto _test_eof130;
case 130: case 130:
#line 1966 "src/Utils/IP.cpp" #line 1965 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st73; case 58u: goto st73;
...@@ -1986,7 +1985,7 @@ st131: ...@@ -1986,7 +1985,7 @@ st131:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof131; goto _test_eof131;
case 131: case 131:
#line 1990 "src/Utils/IP.cpp" #line 1989 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st73; case 58u: goto st73;
...@@ -2010,7 +2009,7 @@ st132: ...@@ -2010,7 +2009,7 @@ st132:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof132; goto _test_eof132;
case 132: case 132:
#line 2014 "src/Utils/IP.cpp" #line 2013 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st73; case 58u: goto st73;
...@@ -2034,7 +2033,7 @@ st133: ...@@ -2034,7 +2033,7 @@ st133:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof133; goto _test_eof133;
case 133: case 133:
#line 2038 "src/Utils/IP.cpp" #line 2037 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 53u: goto tr146; case 53u: goto tr146;
...@@ -2062,7 +2061,7 @@ st134: ...@@ -2062,7 +2061,7 @@ st134:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof134; goto _test_eof134;
case 134: case 134:
#line 2066 "src/Utils/IP.cpp" #line 2065 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st73; case 58u: goto st73;
...@@ -2089,7 +2088,7 @@ st135: ...@@ -2089,7 +2088,7 @@ st135:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof135; goto _test_eof135;
case 135: case 135:
#line 2093 "src/Utils/IP.cpp" #line 2092 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st73; case 58u: goto st73;
...@@ -2113,7 +2112,7 @@ st136: ...@@ -2113,7 +2112,7 @@ st136:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof136; goto _test_eof136;
case 136: case 136:
#line 2117 "src/Utils/IP.cpp" #line 2116 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st73; case 58u: goto st73;
...@@ -2137,7 +2136,7 @@ st137: ...@@ -2137,7 +2136,7 @@ st137:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof137; goto _test_eof137;
case 137: case 137:
#line 2141 "src/Utils/IP.cpp" #line 2140 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st73; goto st73;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -2159,7 +2158,7 @@ st138: ...@@ -2159,7 +2158,7 @@ st138:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof138; goto _test_eof138;
case 138: case 138:
#line 2163 "src/Utils/IP.cpp" #line 2162 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 48u: goto tr105; case 48u: goto tr105;
case 49u: goto tr106; case 49u: goto tr106;
...@@ -2184,7 +2183,7 @@ st139: ...@@ -2184,7 +2183,7 @@ st139:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof139; goto _test_eof139;
case 139: case 139:
#line 2188 "src/Utils/IP.cpp" #line 2187 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st74; case 58u: goto st74;
...@@ -2208,7 +2207,7 @@ st140: ...@@ -2208,7 +2207,7 @@ st140:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof140; goto _test_eof140;
case 140: case 140:
#line 2212 "src/Utils/IP.cpp" #line 2211 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st74; goto st74;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -2230,7 +2229,7 @@ st141: ...@@ -2230,7 +2229,7 @@ st141:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof141; goto _test_eof141;
case 141: case 141:
#line 2234 "src/Utils/IP.cpp" #line 2233 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st74; goto st74;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -2252,7 +2251,7 @@ st142: ...@@ -2252,7 +2251,7 @@ st142:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof142; goto _test_eof142;
case 142: case 142:
#line 2256 "src/Utils/IP.cpp" #line 2255 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st74; goto st74;
goto st0; goto st0;
...@@ -2284,7 +2283,7 @@ st143: ...@@ -2284,7 +2283,7 @@ st143:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof143; goto _test_eof143;
case 143: case 143:
#line 2288 "src/Utils/IP.cpp" #line 2287 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st74; case 58u: goto st74;
...@@ -2308,7 +2307,7 @@ st144: ...@@ -2308,7 +2307,7 @@ st144:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof144; goto _test_eof144;
case 144: case 144:
#line 2312 "src/Utils/IP.cpp" #line 2311 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st74; case 58u: goto st74;
...@@ -2332,7 +2331,7 @@ st145: ...@@ -2332,7 +2331,7 @@ st145:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof145; goto _test_eof145;
case 145: case 145:
#line 2336 "src/Utils/IP.cpp" #line 2335 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st74; case 58u: goto st74;
...@@ -2356,7 +2355,7 @@ st146: ...@@ -2356,7 +2355,7 @@ st146:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof146; goto _test_eof146;
case 146: case 146:
#line 2360 "src/Utils/IP.cpp" #line 2359 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 53u: goto tr154; case 53u: goto tr154;
...@@ -2384,7 +2383,7 @@ st147: ...@@ -2384,7 +2383,7 @@ st147:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof147; goto _test_eof147;
case 147: case 147:
#line 2388 "src/Utils/IP.cpp" #line 2387 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st74; case 58u: goto st74;
...@@ -2411,7 +2410,7 @@ st148: ...@@ -2411,7 +2410,7 @@ st148:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof148; goto _test_eof148;
case 148: case 148:
#line 2415 "src/Utils/IP.cpp" #line 2414 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st74; case 58u: goto st74;
...@@ -2435,7 +2434,7 @@ st149: ...@@ -2435,7 +2434,7 @@ st149:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof149; goto _test_eof149;
case 149: case 149:
#line 2439 "src/Utils/IP.cpp" #line 2438 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st74; case 58u: goto st74;
...@@ -2459,7 +2458,7 @@ st150: ...@@ -2459,7 +2458,7 @@ st150:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof150; goto _test_eof150;
case 150: case 150:
#line 2463 "src/Utils/IP.cpp" #line 2462 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st74; goto st74;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -2481,7 +2480,7 @@ st151: ...@@ -2481,7 +2480,7 @@ st151:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof151; goto _test_eof151;
case 151: case 151:
#line 2485 "src/Utils/IP.cpp" #line 2484 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 48u: goto tr115; case 48u: goto tr115;
case 49u: goto tr116; case 49u: goto tr116;
...@@ -2506,7 +2505,7 @@ st152: ...@@ -2506,7 +2505,7 @@ st152:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof152; goto _test_eof152;
case 152: case 152:
#line 2510 "src/Utils/IP.cpp" #line 2509 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st75; case 58u: goto st75;
...@@ -2530,7 +2529,7 @@ st153: ...@@ -2530,7 +2529,7 @@ st153:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof153; goto _test_eof153;
case 153: case 153:
#line 2534 "src/Utils/IP.cpp" #line 2533 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st75; goto st75;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -2552,7 +2551,7 @@ st154: ...@@ -2552,7 +2551,7 @@ st154:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof154; goto _test_eof154;
case 154: case 154:
#line 2556 "src/Utils/IP.cpp" #line 2555 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st75; goto st75;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -2574,7 +2573,7 @@ st155: ...@@ -2574,7 +2573,7 @@ st155:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof155; goto _test_eof155;
case 155: case 155:
#line 2578 "src/Utils/IP.cpp" #line 2577 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st75; goto st75;
goto st0; goto st0;
...@@ -2606,7 +2605,7 @@ st156: ...@@ -2606,7 +2605,7 @@ st156:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof156; goto _test_eof156;
case 156: case 156:
#line 2610 "src/Utils/IP.cpp" #line 2609 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st75; case 58u: goto st75;
...@@ -2630,7 +2629,7 @@ st157: ...@@ -2630,7 +2629,7 @@ st157:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof157; goto _test_eof157;
case 157: case 157:
#line 2634 "src/Utils/IP.cpp" #line 2633 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st75; case 58u: goto st75;
...@@ -2654,7 +2653,7 @@ st158: ...@@ -2654,7 +2653,7 @@ st158:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof158; goto _test_eof158;
case 158: case 158:
#line 2658 "src/Utils/IP.cpp" #line 2657 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st75; case 58u: goto st75;
...@@ -2678,7 +2677,7 @@ st159: ...@@ -2678,7 +2677,7 @@ st159:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof159; goto _test_eof159;
case 159: case 159:
#line 2682 "src/Utils/IP.cpp" #line 2681 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 53u: goto tr162; case 53u: goto tr162;
...@@ -2706,7 +2705,7 @@ st160: ...@@ -2706,7 +2705,7 @@ st160:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof160; goto _test_eof160;
case 160: case 160:
#line 2710 "src/Utils/IP.cpp" #line 2709 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st75; case 58u: goto st75;
...@@ -2733,7 +2732,7 @@ st161: ...@@ -2733,7 +2732,7 @@ st161:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof161; goto _test_eof161;
case 161: case 161:
#line 2737 "src/Utils/IP.cpp" #line 2736 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st75; case 58u: goto st75;
...@@ -2757,7 +2756,7 @@ st162: ...@@ -2757,7 +2756,7 @@ st162:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof162; goto _test_eof162;
case 162: case 162:
#line 2761 "src/Utils/IP.cpp" #line 2760 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st75; case 58u: goto st75;
...@@ -2781,7 +2780,7 @@ st163: ...@@ -2781,7 +2780,7 @@ st163:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof163; goto _test_eof163;
case 163: case 163:
#line 2785 "src/Utils/IP.cpp" #line 2784 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st75; goto st75;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -2936,7 +2935,7 @@ st164: ...@@ -2936,7 +2935,7 @@ st164:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof164; goto _test_eof164;
case 164: case 164:
#line 2940 "src/Utils/IP.cpp" #line 2939 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 48u: goto tr164; case 48u: goto tr164;
case 49u: goto tr165; case 49u: goto tr165;
...@@ -2961,7 +2960,7 @@ st165: ...@@ -2961,7 +2960,7 @@ st165:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof165; goto _test_eof165;
case 165: case 165:
#line 2965 "src/Utils/IP.cpp" #line 2964 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st84; case 58u: goto st84;
...@@ -2985,7 +2984,7 @@ st166: ...@@ -2985,7 +2984,7 @@ st166:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof166; goto _test_eof166;
case 166: case 166:
#line 2989 "src/Utils/IP.cpp" #line 2988 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st84; goto st84;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -3007,7 +3006,7 @@ st167: ...@@ -3007,7 +3006,7 @@ st167:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof167; goto _test_eof167;
case 167: case 167:
#line 3011 "src/Utils/IP.cpp" #line 3010 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st84; goto st84;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -3029,7 +3028,7 @@ st168: ...@@ -3029,7 +3028,7 @@ st168:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof168; goto _test_eof168;
case 168: case 168:
#line 3033 "src/Utils/IP.cpp" #line 3032 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st84; goto st84;
goto st0; goto st0;
...@@ -3061,7 +3060,7 @@ st169: ...@@ -3061,7 +3060,7 @@ st169:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof169; goto _test_eof169;
case 169: case 169:
#line 3065 "src/Utils/IP.cpp" #line 3064 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st84; case 58u: goto st84;
...@@ -3085,7 +3084,7 @@ st170: ...@@ -3085,7 +3084,7 @@ st170:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof170; goto _test_eof170;
case 170: case 170:
#line 3089 "src/Utils/IP.cpp" #line 3088 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st84; case 58u: goto st84;
...@@ -3109,7 +3108,7 @@ st171: ...@@ -3109,7 +3108,7 @@ st171:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof171; goto _test_eof171;
case 171: case 171:
#line 3113 "src/Utils/IP.cpp" #line 3112 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st84; case 58u: goto st84;
...@@ -3133,7 +3132,7 @@ st172: ...@@ -3133,7 +3132,7 @@ st172:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof172; goto _test_eof172;
case 172: case 172:
#line 3137 "src/Utils/IP.cpp" #line 3136 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 53u: goto tr175; case 53u: goto tr175;
...@@ -3161,7 +3160,7 @@ st173: ...@@ -3161,7 +3160,7 @@ st173:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof173; goto _test_eof173;
case 173: case 173:
#line 3165 "src/Utils/IP.cpp" #line 3164 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st84; case 58u: goto st84;
...@@ -3188,7 +3187,7 @@ st174: ...@@ -3188,7 +3187,7 @@ st174:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof174; goto _test_eof174;
case 174: case 174:
#line 3192 "src/Utils/IP.cpp" #line 3191 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st84; case 58u: goto st84;
...@@ -3212,7 +3211,7 @@ st175: ...@@ -3212,7 +3211,7 @@ st175:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof175; goto _test_eof175;
case 175: case 175:
#line 3216 "src/Utils/IP.cpp" #line 3215 "src/Utils/IP.cpp"
switch( (*p) ) { switch( (*p) ) {
case 46u: goto st46; case 46u: goto st46;
case 58u: goto st84; case 58u: goto st84;
...@@ -3236,7 +3235,7 @@ st176: ...@@ -3236,7 +3235,7 @@ st176:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof176; goto _test_eof176;
case 176: case 176:
#line 3240 "src/Utils/IP.cpp" #line 3239 "src/Utils/IP.cpp"
if ( (*p) == 58u ) if ( (*p) == 58u )
goto st84; goto st84;
if ( (*p) < 65u ) { if ( (*p) < 65u ) {
...@@ -3444,7 +3443,7 @@ case 85: ...@@ -3444,7 +3443,7 @@ case 85:
_out: {} _out: {}
} }
#line 64 "src/Utils/IP.rl" #line 63 "src/Utils/IP.rl"
// Ensure that the parsing has consumed all the given length. // Ensure that the parsing has consumed all the given length.
if (ip_len == (size_t)(p - (const unsigned char*)ip)) if (ip_len == (size_t)(p - (const unsigned char*)ip))
...@@ -3481,4 +3480,4 @@ case 85: ...@@ -3481,4 +3480,4 @@ case 85:
*family = addr->sa_family; *family = addr->sa_family;
ip.assign(_ip); ip.assign(_ip);
} }
} // namespace Utils }
...@@ -36,7 +36,6 @@ namespace Utils ...@@ -36,7 +36,6 @@ namespace Utils
IPv6address @on_ipv6; IPv6address @on_ipv6;
}%% }%%
/** /**
* Ragel: %%write data * Ragel: %%write data
* This generates Ragel's static variables. * This generates Ragel's static variables.
...@@ -97,4 +96,4 @@ namespace Utils ...@@ -97,4 +96,4 @@ namespace Utils
*family = addr->sa_family; *family = addr->sa_family;
ip.assign(_ip); ip.assign(_ip);
} }
} // namespace Utils }
...@@ -85,4 +85,4 @@ namespace Utils ...@@ -85,4 +85,4 @@ namespace Utils
return true; return true;
} }
} // namespace Utils }
#define MS_CLASS "Version"
#include "Version.h"
#include "Logger.h"
/* Static variables. */
const int Version::versionMajor = 0;
const int Version::versionMinor = 0;
const int Version::versionTiny = 1;
const std::string Version::name = "MediaSoup";
const std::string Version::command = "mediasoup";
const std::string Version::copyright = "Copyright (c) 2014 Iñaki Baz Castillo | https://github.com/ibc";
/* Static methods .*/
const std::string Version::GetVersion()
{
MS_TRACE();
std::string version;
version.append(std::to_string(Version::versionMajor));
version.append(".");
version.append(std::to_string(Version::versionMinor));
version.append(".");
version.append(std::to_string(Version::versionTiny));
return version;
}
const std::string Version::GetNameAndVersion()
{
MS_TRACE();
std::string name_and_version;
name_and_version.append(Version::name);
name_and_version.append(" v");
name_and_version.append(Version::GetVersion());
return name_and_version;
}
#define MS_CLASS "Worker"
#include "Worker.h"
#include "Utils.h"
#include "DepLibUV.h"
#include "ControlProtocol/messages.h"
#include "MediaSoupError.h"
#include "Logger.h"
#include <string>
#include <csignal> // sigaction(), pthread_sigmask()
#include <cerrno>
/* Static variables. */
Worker::Workers Worker::workers;
int Worker::numWorkers = 0;
std::atomic<int> Worker::numWorkersRunning {0};
/* Static methods. */
void Worker::ThreadInit(int workerId)
{
Logger::ThreadInit("worker #" + std::to_string(workerId));
MS_TRACE();
DepLibUV::ThreadInit();
Utils::Crypto::ThreadInit();
}
void Worker::ThreadDestroy()
{
MS_TRACE();
DepLibUV::ThreadDestroy();
Utils::Crypto::ThreadDestroy();
}
void Worker::SetWorker(int workerId, Worker* worker)
{
MS_TRACE();
// Main thread calls this method with nullptr as worker. In that case the
// workerId entry MUST NOT already exit.
if (!worker)
{
if (Worker::workers.find(workerId) != Worker::workers.end())
MS_ABORT("entry with key %d already in the map", workerId);
Worker::workers[workerId] = nullptr;
}
// Each Worker (in its own thread) calls this method to insert itself in
// the map. In that case the key MUST exist.
else
{
if (Worker::workers.find(workerId) == Worker::workers.end())
MS_ABORT("entry with key %d not present in the map", workerId);
Worker::workers[workerId] = worker;
}
}
int Worker::CountWorkers()
{
MS_TRACE();
if (!Worker::numWorkers)
{
Worker::numWorkers = workers.size();
}
return Worker::numWorkers;
}
bool Worker::AreAllWorkersRunning()
{
MS_TRACE();
if (Worker::numWorkersRunning.load(std::memory_order_relaxed) == Worker::CountWorkers())
return true;
else
return false;
}
int Worker::GetControlProtocolRemoteSocket(int workerId)
{
MS_TRACE();
Workers::iterator it = Worker::workers.find(workerId);
// Ensure it does exist in the map of Workers.
if (it == Worker::workers.end())
MS_ABORT("Worker with workerId %d not present in the map", workerId);
Worker* worker = it->second;
return worker->GetControlProtocolRemoteSocket();
}
/* Instance methods. */
Worker::Worker(int workerId) :
workerId(workerId)
{
MS_TRACE();
// Block signals that must just be delivered to Dispatcher thread.
BlockSignals();
// Add this worker to the map.
// NOTE: This is thread-safe given that I don't alter the map (but just
// the values of already allocated entries in the map).
Worker::SetWorker(this->workerId, this);
// Build a pair of connected UnixSocket in stream mode.
int fds[2] {-1,-1};
try
{
Utils::Socket::BuildSocketPair(AF_UNIX, SOCK_STREAM, fds);
}
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("error building a socket pair for the ControlProtocol Unix socket: %s", error.what());
}
// Store the first paired socket for Dispatcher's usage.
this->controlProtocolRemoteSocket = fds[0];
try
{
this->controlProtocolUnixStreamSocket = new ControlProtocol::UnixStreamSocket(this, fds[1]);
}
catch (const MediaSoupError &error)
{
MS_THROW_ERROR("error creating a ControlProtocol Unix socket for Dispatcher: %s", error.what());
}
MS_DEBUG("ControlProtocol Unix socket for Dispatcher ready");
// TODO: TMP
if (this->workerId == 1)
{
try
{
this->room = new RTC::Room();
}
catch (const MediaSoupError &error)
{
MS_ERROR("---- TEST: error creating a RTC Room: %s", error.what());
}
}
MS_DEBUG("Worker #%d running", this->workerId);
// Increase the static variable numWorkersRunning.
Worker::numWorkersRunning++;
// Run the loop.
DepLibUV::RunLoop();
MS_DEBUG("libuv loop ends");
}
Worker::~Worker()
{
MS_TRACE();
}
void Worker::Close()
{
MS_TRACE();
// TMP: close RTC::Room.
if (this->room)
this->room->Close();
}
int Worker::GetControlProtocolRemoteSocket()
{
return this->controlProtocolRemoteSocket;
}
void Worker::BlockSignals()
{
MS_TRACE();
int err;
sigset_t signal_mask;
sigemptyset(&signal_mask);
// Block some signals to ensure that they are just delivered to the Dispatcher thread.
sigaddset(&signal_mask, SIGUSR1);
sigaddset(&signal_mask, SIGINT);
sigaddset(&signal_mask, SIGTERM);
err = pthread_sigmask(SIG_BLOCK, &signal_mask, nullptr);
if (err)
MS_ABORT("pthread_sigmask() failed: %s", std::strerror(errno));
}
void Worker::onControlProtocolMessage(ControlProtocol::UnixStreamSocket* unixSocket, ControlProtocol::Message* msg, const MS_BYTE* raw, size_t len)
{
MS_TRACE();
MS_DEBUG("ControlProtocol message received from Dispatcher");
msg->Dump();
// TMP
bool sendBackToDispatcher = false;
if (sendBackToDispatcher) {
MS_INFO("sending msg back to Dispatcher...");
unixSocket->Write(raw, len);
}
// TODO: process the ControlProtocol::Message.
}
void Worker::onControlProtocolUnixStreamSocketClosed(ControlProtocol::UnixStreamSocket* unixSocket, bool is_closed_by_peer)
{
MS_TRACE();
if (is_closed_by_peer)
MS_DEBUG("ControlProtocol Unix socket for Dispatcher remotely closed");
else
MS_DEBUG("ControlProtocol Unix socket for Dispatcher locally closed");
// Call Close() to terminate all the active handles.
Close();
}
#define MS_CLASS "main" #define MS_CLASS "main"
#include "MediaSoup.h" #include "common.h"
#include "Settings.h" #include "Settings.h"
#include "Version.h" #include "DepLibUV.h"
#include "Daemon.h" #include "DepOpenSSL.h"
#include "DepLibSRTP.h"
#include "DepUsrSCTP.h"
#include "Utils.h"
#include "RTC/UDPSocket.h"
#include "RTC/TCPServer.h"
#include "RTC/DTLSHandler.h"
#include "RTC/SRTPSession.h"
#include "Loop.h"
#include "MediaSoupError.h" #include "MediaSoupError.h"
#include "Logger.h" #include "Logger.h"
#include <map>
#include <string> #include <string>
#include <cstdlib> // std::_Exit() #include <csignal> // sigaction()
#include <cerrno>
#include <cstdint> // uint8_t, etc // TODO: remove?
static void init();
static void ignoreSignals();
static void destroy();
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
// Set this thread as main thread. // TODO: set the process name/id.
Logger::ThreadInit("main"); Logger::Init("main");
// Read command line arguments.
Settings::ReadArguments(argc, argv);
MS_NOTICE("starting %s", Version::GetNameAndVersion().c_str()); #if defined(MS_LITTLE_ENDIAN)
MS_DEBUG("detected Little-Endian CPU");
#elif defined(MS_BIG_ENDIAN)
MS_DEBUG("detected Big-Endian CPU");
#endif
// Set default settings. #if defined(INTPTR_MAX) && defined(INT32_MAX) && (INTPTR_MAX == INT32_MAX)
Settings::SetDefaultConfiguration(); MS_DEBUG("detected 32 bits architecture");
#elif defined(INTPTR_MAX) && defined(INT64_MAX) && (INTPTR_MAX == INT64_MAX)
MS_DEBUG("detected 64 bits architecture");
#else
MS_NOTICE("cannot determine whether the architecture is 32 or 64 bits");
#endif
// Read configuration file (if given). try
if (!Settings::arguments.configFile.empty())
{ {
MS_INFO("reading configuration file '%s'", Settings::arguments.configFile.c_str()); Settings::SetConfiguration(argc, argv);
Settings::ReadConfigurationFile(); }
catch (const MediaSoupError &error)
{
MS_EXIT_FAILURE("%s", error.what());
} }
MS_NOTICE("starting MediaSoup");
// Print configuration. // Print configuration.
Settings::PrintConfiguration(); Settings::PrintConfiguration();
// Verify final configuration.
Settings::ConfigurationPostCheck();
// Set process stuff.
MediaSoup::SetProcess();
try try
{ {
// Daemonize if requested. init();
if (Settings::arguments.daemonize)
// Run the Loop.
Loop loop;
}
catch (const MediaSoupError &error)
{ {
MS_NOTICE("daemonizing %s", Version::name.c_str()); destroy();
Daemon::Daemonize(); MS_EXIT_FAILURE("%s", error.what());
} }
// Run MediaSoup. destroy();
MediaSoup::Run(); MS_EXIT_SUCCESS("MediaSoup ends");
}
// End. void init()
MediaSoup::End(); {
MS_TRACE();
MS_EXIT_SUCCESS("%s ends", Version::name.c_str()); ignoreSignals();
}
catch (const MediaSoupError &error) // Initialize static stuff.
{ DepLibUV::ClassInit();
// End. DepOpenSSL::ClassInit();
MediaSoup::End(); DepLibSRTP::ClassInit();
DepUsrSCTP::ClassInit();
RTC::UDPSocket::ClassInit();
RTC::TCPServer::ClassInit();
RTC::DTLSHandler::ClassInit();
RTC::SRTPSession::ClassInit();
Utils::Crypto::ClassInit();
}
// If this is the daemonized process don't log FAILURE EXIT as the void ignoreSignals()
// ancestor process will do it. {
if (Daemon::IsDaemonized()) MS_TRACE();
int err;
struct sigaction act;
// NOTE: Here we ignore also signals that will be later handled in Loop
// (the libuv signal handler overrides it).
std::map<std::string, int> ignored_signals =
{ {
MS_CRIT("%s", error.what()); { "INT", SIGINT },
// Notify the ancestor process that the daemonized process failed. { "TERM", SIGTERM },
Daemon::SendErrorStatusToAncestor(); { "HUP", SIGHUP },
// And silently die. { "ALRM", SIGALRM },
std::_Exit(EXIT_FAILURE); { "USR1", SIGUSR2 },
} { "USR2", SIGUSR1 }
// Otherwise this is the ancestor process so log FAILURE EXIT. };
else
act.sa_handler = SIG_IGN;
act.sa_flags = 0;
err = sigfillset(&act.sa_mask);
if (err)
MS_THROW_ERROR("sigfillset() failed: %s", std::strerror(errno));
for (auto it = ignored_signals.begin(); it != ignored_signals.end(); ++it)
{ {
MS_EXIT_FAILURE("%s", error.what()); std::string sig_name = it->first;
} int sig_id = it->second;
err = sigaction(sig_id, &act, nullptr);
if (err)
MS_THROW_ERROR("sigaction() failed for signal %s: %s", sig_name.c_str(), std::strerror(errno));
} }
} }
void destroy()
{
MS_TRACE();
// Free static stuff.
DepLibUV::ClassDestroy();
DepOpenSSL::ClassDestroy();
DepLibSRTP::ClassDestroy();
DepUsrSCTP::ClassDestroy();
RTC::DTLSHandler::ClassDestroy();
Utils::Crypto::ClassDestroy();
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论