diff --git a/src/engine/server/databases/connection.h b/src/engine/server/databases/connection.h index 57f8a387b..a5db69105 100644 --- a/src/engine/server/databases/connection.h +++ b/src/engine/server/databases/connection.h @@ -108,6 +108,7 @@ std::unique_ptr CreateMysqlConnection( const char *pUser, const char *pPass, const char *pIp, + const char *pBindaddr, int Port, bool Setup); diff --git a/src/engine/server/databases/mysql.cpp b/src/engine/server/databases/mysql.cpp index 97908d000..7f9c7801a 100644 --- a/src/engine/server/databases/mysql.cpp +++ b/src/engine/server/databases/mysql.cpp @@ -64,6 +64,7 @@ public: const char *pUser, const char *pPass, const char *pIp, + const char *pBindaddr, int Port, bool Setup); ~CMysqlConnection(); @@ -138,6 +139,7 @@ private: char m_aUser[64]; char m_aPass[64]; char m_aIp[64]; + char m_aBindaddr[128]; int m_Port; bool m_Setup; @@ -155,6 +157,7 @@ CMysqlConnection::CMysqlConnection( const char *pUser, const char *pPass, const char *pIp, + const char *pBindaddr, int Port, bool Setup) : IDbConnection(pPrefix), @@ -173,6 +176,7 @@ CMysqlConnection::CMysqlConnection( str_copy(m_aUser, pUser, sizeof(m_aUser)); str_copy(m_aPass, pPass, sizeof(m_aPass)); str_copy(m_aIp, pIp, sizeof(m_aIp)); + str_copy(m_aBindaddr, pBindaddr, sizeof(m_aBindaddr)); } CMysqlConnection::~CMysqlConnection() @@ -217,7 +221,7 @@ void CMysqlConnection::Print(IConsole *pConsole, const char *pMode) CMysqlConnection *CMysqlConnection::Copy() { - return new CMysqlConnection(m_aDatabase, GetPrefix(), m_aUser, m_aPass, m_aIp, m_Port, m_Setup); + return new CMysqlConnection(m_aDatabase, GetPrefix(), m_aUser, m_aPass, m_aIp, m_aBindaddr, m_Port, m_Setup); } void CMysqlConnection::ToUnixTimestamp(const char *pTimestamp, char *aBuf, unsigned int BufferSize) @@ -273,6 +277,10 @@ bool CMysqlConnection::ConnectImpl() mysql_options(&m_Mysql, MYSQL_OPT_WRITE_TIMEOUT, &OptWriteTimeout); mysql_options(&m_Mysql, MYSQL_OPT_RECONNECT, &OptReconnect); mysql_options(&m_Mysql, MYSQL_SET_CHARSET_NAME, "utf8mb4"); + if(m_aBindaddr[0] != '\0') + { + mysql_options(&m_Mysql, MYSQL_OPT_BIND, m_aBindaddr); + } if(!mysql_real_connect(&m_Mysql, m_aIp, m_aUser, m_aPass, nullptr, m_Port, nullptr, CLIENT_IGNORE_SIGPIPE)) { @@ -711,10 +719,11 @@ std::unique_ptr CreateMysqlConnection( const char *pUser, const char *pPass, const char *pIp, + const char *pBindaddr, int Port, bool Setup) { - return std::make_unique(pDatabase, pPrefix, pUser, pPass, pIp, Port, Setup); + return std::make_unique(pDatabase, pPrefix, pUser, pPass, pIp, pBindaddr, Port, Setup); } #else int MysqlInit() @@ -730,6 +739,7 @@ std::unique_ptr CreateMysqlConnection( const char *pUser, const char *pPass, const char *pIp, + const char *pBindaddr, int Port, bool Setup) { diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 16c6fbde8..e46554497 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -3387,8 +3387,8 @@ void CServer::ConAddSqlServer(IConsole::IResult *pResult, void *pUserData) auto pMysqlConn = CreateMysqlConnection( pResult->GetString(1), pResult->GetString(2), pResult->GetString(3), - pResult->GetString(4), pResult->GetString(5), pResult->GetInteger(6), - SetUpDb); + pResult->GetString(4), pResult->GetString(5), g_Config.m_SvSqlBindaddr, + pResult->GetInteger(6), SetUpDb); if(!pMysqlConn) { diff --git a/src/engine/shared/config_variables.h b/src/engine/shared/config_variables.h index 01db68a61..bf08fc1ec 100644 --- a/src/engine/shared/config_variables.h +++ b/src/engine/shared/config_variables.h @@ -255,6 +255,7 @@ MACRO_CONFIG_INT(SvSwap, sv_swap, 1, 0, 1, CFGFLAG_SERVER, "Enable /swap") MACRO_CONFIG_INT(SvUseSQL, sv_use_sql, 0, 0, 1, CFGFLAG_SERVER, "Enables MySQL backend instead of SQLite backend (sv_sqlite_file is still used as fallback write server when no MySQL server is reachable)") MACRO_CONFIG_INT(SvSqlQueriesDelay, sv_sql_queries_delay, 1, 0, 20, CFGFLAG_SERVER, "Delay in seconds between SQL queries of a single player") MACRO_CONFIG_STR(SvSqliteFile, sv_sqlite_file, 64, "ddnet-server.sqlite", CFGFLAG_SERVER, "File to store ranks in case sv_use_sql is turned off or used as backup sql server") +MACRO_CONFIG_STR(SvSqlBindaddr, sv_sql_bindaddr, 128, "", CFGFLAG_SERVER, "Address to bind the SQL connections to") #if defined(CONF_UPNP) MACRO_CONFIG_INT(SvUseUPnP, sv_use_upnp, 0, 0, 1, CFGFLAG_SERVER, "Enables UPnP support. (Requires -DCONF_UPNP=ON when compiling)") diff --git a/src/test/score.cpp b/src/test/score.cpp index 71e824d5f..4b862f533 100644 --- a/src/test/score.cpp +++ b/src/test/score.cpp @@ -515,7 +515,7 @@ TEST_P(RandomMap, UnfinishedDoesntExist) auto g_pSqliteConn = CreateSqliteConnection(":memory:", true); #if defined(CONF_TEST_MYSQL) -auto g_pMysqlConn = CreateMysqlConnection("ddnet", "record", "ddnet", "thebestpassword", "localhost", 3306, true); +auto g_pMysqlConn = CreateMysqlConnection("ddnet", "record", "ddnet", "thebestpassword", "localhost", "", 3306, true); #endif auto g_TestValues