Skip to content

Commit

Permalink
Merge pull request #19040 from wsnipex/hosts
Browse files Browse the repository at this point in the history
use DNS cache for samba and mysql. (fixes  #15469)
  • Loading branch information
DaveTBlake committed Jan 12, 2021
2 parents 91ba97b + 0ca500e commit 698bbdd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
23 changes: 15 additions & 8 deletions xbmc/dbwrappers/mysqldataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
* See LICENSES/README.md for more information.
*/

#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include "mysqldataset.h"

#include "utils/log.h"
#include "network/WakeOnAccess.h"
#include "Util.h"
#include "network/DNSNameCache.h"
#include "network/WakeOnAccess.h"
#include "utils/StringUtils.h"
#include "utils/log.h"

#include "mysqldataset.h"
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#ifdef HAS_MYSQL
#include <mysql/errmsg.h>
#elif defined(HAS_MARIADB)
Expand Down Expand Up @@ -145,7 +146,13 @@ int MysqlDatabase::connect(bool create_new) {
if (host.empty() || db.empty())
return DB_CONNECTION_NONE;

//CLog::Log(LOGDEBUG, "Connecting to mysql:%s:%s", host.c_str(), db.c_str());
std::string resolvedHost;
if (CDNSNameCache::Lookup(host, resolvedHost))
{
CLog::Log(LOGDEBUG, "{} replacing configured host {} with resolved host {}", __FUNCTION__, host,
resolvedHost);
host = resolvedHost;
}

try
{
Expand Down
35 changes: 35 additions & 0 deletions xbmc/filesystem/CurlFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "URL.h"
#include "Util.h"
#include "filesystem/SpecialProtocol.h"
#include "network/DNSNameCache.h"
#include "settings/AdvancedSettings.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
Expand Down Expand Up @@ -451,6 +452,10 @@ void CCurlFile::Close()
m_opened = false;
m_forWrite = false;
m_inError = false;

if (m_dnsCacheList)
g_curlInterface.slist_free_all(m_dnsCacheList);
m_dnsCacheList = nullptr;
}

void CCurlFile::SetCommonOptions(CReadState* state, bool failOnError /* = true */)
Expand All @@ -472,6 +477,9 @@ void CCurlFile::SetCommonOptions(CReadState* state, bool failOnError /* = true *
g_curlInterface.easy_setopt(h, CURLOPT_READDATA, state);
g_curlInterface.easy_setopt(h, CURLOPT_READFUNCTION, read_callback);

// use DNS cache
g_curlInterface.easy_setopt(h, CURLOPT_RESOLVE, m_dnsCacheList);

// make sure headers are separated from the data stream
g_curlInterface.easy_setopt(h, CURLOPT_WRITEHEADER, state);
g_curlInterface.easy_setopt(h, CURLOPT_HEADERFUNCTION, header_callback);
Expand Down Expand Up @@ -702,6 +710,33 @@ void CCurlFile::ParseAndCorrectUrl(CURL &url2)
std::string strProtocol = url2.GetTranslatedProtocol();
url2.SetProtocol(strProtocol);

// lookup host in DNS cache
std::string resolvedHost;
if (CDNSNameCache::Lookup(url2.GetHostName(), resolvedHost))
{
struct curl_slist* tempCache;
int entryPort = url2.GetPort();

if (entryPort == 0)
{
if (strProtocol == "http")
entryPort = 80;
else if (strProtocol == "https")
entryPort = 443;
else if (strProtocol == "ftp")
entryPort = 21;
else if (strProtocol == "ftps")
entryPort = 990;
}

std::string entryString =
url2.GetHostName() + ":" + std::to_string(entryPort) + ":" + resolvedHost;
tempCache = g_curlInterface.slist_append(m_dnsCacheList, entryString.c_str());

if (tempCache)
m_dnsCacheList = tempCache;
}

if( url2.IsProtocol("ftp")
|| url2.IsProtocol("ftps") )
{
Expand Down
1 change: 1 addition & 0 deletions xbmc/filesystem/CurlFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ namespace XFILE
bool m_allowRetry;
bool m_verifyPeer = true;
bool m_failOnError = true;
curl_slist* m_dnsCacheList = nullptr;

CRingBuffer m_buffer; // our ringhold buffer
char* m_overflowBuffer; // in the rare case we would overflow the above buffer
Expand Down
8 changes: 4 additions & 4 deletions xbmc/platform/posix/filesystem/SMBDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ int CSMBDirectory::OpenDir(const CURL& url, std::string& strAuth)
int fd = -1;

/* make a writeable copy */
CURL urlIn(url);
CURL urlIn = CSMB::GetResolvedUrl(url);

CPasswordManager::GetInstance().AuthenticateURL(urlIn);
strAuth = smb.URLEncode(urlIn);
Expand Down Expand Up @@ -273,7 +273,7 @@ bool CSMBDirectory::Create(const CURL& url2)
CSingleLock lock(smb);
smb.Init();

CURL url(url2);
CURL url = CSMB::GetResolvedUrl(url2);
CPasswordManager::GetInstance().AuthenticateURL(url);
std::string strFileName = smb.URLEncode(url);

Expand All @@ -290,7 +290,7 @@ bool CSMBDirectory::Remove(const CURL& url2)
CSingleLock lock(smb);
smb.Init();

CURL url(url2);
CURL url = CSMB::GetResolvedUrl(url2);
CPasswordManager::GetInstance().AuthenticateURL(url);
std::string strFileName = smb.URLEncode(url);

Expand All @@ -310,7 +310,7 @@ bool CSMBDirectory::Exists(const CURL& url2)
CSingleLock lock(smb);
smb.Init();

CURL url(url2);
CURL url = CSMB::GetResolvedUrl(url2);
CPasswordManager::GetInstance().AuthenticateURL(url);
std::string strFileName = smb.URLEncode(url);

Expand Down
29 changes: 21 additions & 8 deletions xbmc/platform/posix/filesystem/SMBFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Util.h"
#include "commons/Exception.h"
#include "filesystem/SpecialProtocol.h"
#include "network/DNSNameCache.h"
#include "settings/AdvancedSettings.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
Expand Down Expand Up @@ -322,6 +323,17 @@ void CSMB::AddIdleConnection()
m_IdleTimeout = 180;
}

CURL CSMB::GetResolvedUrl(const CURL& url)
{
CURL tmpUrl(url);
std::string resolvedHostName;

if (CDNSNameCache::Lookup(tmpUrl.GetHostName(), resolvedHostName))
tmpUrl.SetHostName(resolvedHostName);

return tmpUrl;
}

CSMB smb;

CSMBFile::CSMBFile()
Expand Down Expand Up @@ -434,7 +446,7 @@ int CSMBFile::OpenFile(const CURL &url, std::string& strAuth)
int fd = -1;
smb.Init();

strAuth = GetAuthenticatedPath(url);
strAuth = GetAuthenticatedPath(CSMB::GetResolvedUrl(url));
std::string strPath = strAuth;

{
Expand All @@ -455,7 +467,7 @@ bool CSMBFile::Exists(const CURL& url)
if (!IsValidFile(url.GetFileName())) return false;

smb.Init();
std::string strFileName = GetAuthenticatedPath(url);
std::string strFileName = GetAuthenticatedPath(CSMB::GetResolvedUrl(url));

struct stat info;

Expand All @@ -482,7 +494,7 @@ int CSMBFile::Stat(struct __stat64* buffer)
int CSMBFile::Stat(const CURL& url, struct __stat64* buffer)
{
smb.Init();
std::string strFileName = GetAuthenticatedPath(url);
std::string strFileName = GetAuthenticatedPath(CSMB::GetResolvedUrl(url));
CSingleLock lock(smb);

struct stat tmpBuffer = {0};
Expand Down Expand Up @@ -586,7 +598,7 @@ ssize_t CSMBFile::Write(const void* lpBuf, size_t uiBufSize)
bool CSMBFile::Delete(const CURL& url)
{
smb.Init();
std::string strFile = GetAuthenticatedPath(url);
std::string strFile = GetAuthenticatedPath(CSMB::GetResolvedUrl(url));

CSingleLock lock(smb);

Expand All @@ -601,8 +613,8 @@ bool CSMBFile::Delete(const CURL& url)
bool CSMBFile::Rename(const CURL& url, const CURL& urlnew)
{
smb.Init();
std::string strFile = GetAuthenticatedPath(url);
std::string strFileNew = GetAuthenticatedPath(urlnew);
std::string strFile = GetAuthenticatedPath(CSMB::GetResolvedUrl(url));
std::string strFileNew = GetAuthenticatedPath(CSMB::GetResolvedUrl(urlnew));
CSingleLock lock(smb);

int result = smbc_rename(strFile.c_str(), strFileNew.c_str());
Expand All @@ -618,11 +630,12 @@ bool CSMBFile::OpenForWrite(const CURL& url, bool bOverWrite)
m_fileSize = 0;

Close();

// we can't open files like smb://file.f or smb://server/file.f
// if a file matches the if below return false, it can't exist on a samba share.
if (!IsValidFile(url.GetFileName())) return false;

std::string strFileName = GetAuthenticatedPath(url);
std::string strFileName = GetAuthenticatedPath(CSMB::GetResolvedUrl(url));
CSingleLock lock(smb);

if (bOverWrite)
Expand Down Expand Up @@ -657,7 +670,7 @@ bool CSMBFile::IsValidFile(const std::string& strFileName)

std::string CSMBFile::GetAuthenticatedPath(const CURL &url)
{
CURL authURL(url);
CURL authURL(CSMB::GetResolvedUrl(url));
CPasswordManager::GetInstance().AuthenticateURL(authURL);
return smb.URLEncode(authURL);
}
Expand Down
2 changes: 2 additions & 0 deletions xbmc/platform/posix/filesystem/SMBFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class CSMB : public CCriticalSection
std::string URLEncode(const CURL &url);

DWORD ConvertUnixToNT(int error);
static CURL GetResolvedUrl(const CURL& url);

private:
SMBCCTX *m_context;
int m_OpenConnections;
Expand Down

0 comments on commit 698bbdd

Please sign in to comment.