Commit 5aa17595 authored by Sébastien Blin's avatar Sébastien Blin Committed by Adrien Béraud

sonarqube: fix some code smells

+ Remove some unused using
+ Remove useless std::move
+ Reduce duplicate code
+ Avoid some copy

Change-Id: I7bdbc14aa1b79ce10d30f2f1ef005dd8839de62a
parent 39635c7e
...@@ -125,11 +125,11 @@ AccountFactory::getAllAccounts() const ...@@ -125,11 +125,11 @@ AccountFactory::getAllAccounts() const
for (const auto& itemmap : accountMaps_) { for (const auto& itemmap : accountMaps_) {
const auto& map = itemmap.second; const auto& map = itemmap.second;
for (const auto item : map) v.reserve(v.size() + map.size());
for (const auto& item : map)
v.push_back(item.second); v.push_back(item.second);
} }
v.shrink_to_fit();
return v; return v;
} }
......
...@@ -77,7 +77,7 @@ accountToJsonValue(const std::map<std::string, std::string>& details) { ...@@ -77,7 +77,7 @@ accountToJsonValue(const std::map<std::string, std::string>& details) {
// replace paths by the files content // replace paths by the files content
std::ifstream ifs(i.second); std::ifstream ifs(i.second);
std::string fileContent((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); std::string fileContent((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
root[i.first] = std::move(fileContent); root[i.first] = fileContent;
} else } else
root[i.first] = i.second; root[i.first] = i.second;
} }
......
...@@ -135,7 +135,7 @@ registerSignalHandlers(const std::map<std::string, ...@@ -135,7 +135,7 @@ registerSignalHandlers(const std::map<std::string,
JAMI_ERR("Signal %s not supported", item.first.c_str()); JAMI_ERR("Signal %s not supported", item.first.c_str());
continue; continue;
} }
iter->second = std::move(item.second); iter->second = item.second;
} }
} }
......
...@@ -272,7 +272,7 @@ private: ...@@ -272,7 +272,7 @@ private:
SubOutgoingFileTransfer::SubOutgoingFileTransfer(DRing::DataTransferId tid, SubOutgoingFileTransfer::SubOutgoingFileTransfer(DRing::DataTransferId tid,
const std::string& peerUri, const std::string& peerUri,
std::shared_ptr<OptimisticMetaOutgoingInfo> metaInfo) std::shared_ptr<OptimisticMetaOutgoingInfo> metaInfo)
: DataTransfer(tid), metaInfo_(metaInfo), peerUri_(peerUri) : DataTransfer(tid), metaInfo_(std::move(metaInfo)), peerUri_(peerUri)
{ {
info_ = metaInfo_->info(); info_ = metaInfo_->info();
......
...@@ -42,7 +42,7 @@ class IceSocket ...@@ -42,7 +42,7 @@ class IceSocket
public: public:
IceSocket(std::shared_ptr<IceTransport> iceTransport, int compId) IceSocket(std::shared_ptr<IceTransport> iceTransport, int compId)
: ice_transport_(iceTransport), compId_(compId) {} : ice_transport_(std::move(iceTransport)), compId_(compId) {}
void close(); void close();
ssize_t recv(unsigned char* buf, size_t len); ssize_t recv(unsigned char* buf, size_t len);
......
...@@ -999,7 +999,7 @@ IceTransport::start(const std::vector<uint8_t>& rem_data) ...@@ -999,7 +999,7 @@ IceTransport::start(const std::vector<uint8_t>& rem_data)
IceCandidate cand; IceCandidate cand;
for (const auto& line : result.get().as<std::vector<std::string>>()) { for (const auto& line : result.get().as<std::vector<std::string>>()) {
if (getCandidateFromSDP(line, cand)) if (getCandidateFromSDP(line, cand))
rem_candidates.emplace_back(std::move(cand)); rem_candidates.emplace_back(cand);
} }
} }
} else { } else {
......
...@@ -239,7 +239,7 @@ public: ...@@ -239,7 +239,7 @@ public:
JAMI_DBG("[Account:%s] add remote ICE candidate: %s", JAMI_DBG("[Account:%s] add remote ICE candidate: %s",
account.getAccountID().c_str(), account.getAccountID().c_str(),
line.c_str()); line.c_str());
res.rem_candidates.emplace_back(std::move(cand)); res.rem_candidates.emplace_back(cand);
} }
} }
nr++; nr++;
...@@ -546,9 +546,9 @@ DhtPeerConnector::Impl::turnConnect() ...@@ -546,9 +546,9 @@ DhtPeerConnector::Impl::turnConnect()
// If a previous turn server exists, but is not ready, we should try to reconnect // If a previous turn server exists, but is not ready, we should try to reconnect
if (turnAuthv4_ && !turnAuthv4_->isReady()) if (turnAuthv4_ && !turnAuthv4_->isReady())
turnAuthv4_.release(); turnAuthv4_.reset();
if (turnAuthv6_ && !turnAuthv6_->isReady()) if (turnAuthv6_ && !turnAuthv6_->isReady())
turnAuthv6_.release(); turnAuthv6_.reset();
if (!turnAuthv4_ || !turnAuthv4_->isReady()) { if (!turnAuthv4_ || !turnAuthv4_->isReady()) {
turn_param_v4.authorized_family = PJ_AF_INET; turn_param_v4.authorized_family = PJ_AF_INET;
......
...@@ -475,7 +475,7 @@ VideoInput::initAVFoundation(const std::string& display) ...@@ -475,7 +475,7 @@ VideoInput::initAVFoundation(const std::string& display)
bool bool
VideoInput::initGdiGrab(std::string params) VideoInput::initGdiGrab(const std::string& params)
{ {
size_t space = params.find(' '); size_t space = params.find(' ');
clearOptions(); clearOptions();
......
...@@ -126,7 +126,7 @@ private: ...@@ -126,7 +126,7 @@ private:
bool initX11(std::string display); bool initX11(std::string display);
bool initAVFoundation(const std::string& display); bool initAVFoundation(const std::string& display);
bool initFile(std::string path); bool initFile(std::string path);
bool initGdiGrab(std::string params); bool initGdiGrab(const std::string& params);
// for ThreadLoop // for ThreadLoop
bool setup(); bool setup();
......
...@@ -43,7 +43,6 @@ ...@@ -43,7 +43,6 @@
namespace jami { namespace video { namespace jami { namespace video {
using std::map;
using std::string; using std::string;
// how long (in seconds) to wait before rechecking for packet loss // how long (in seconds) to wait before rechecking for packet loss
......
...@@ -52,6 +52,38 @@ ...@@ -52,6 +52,38 @@
namespace jami { namespace jami {
int
init_crt(gnutls_session_t session, dht::crypto::Certificate& crt)
{
// Support only x509 format
if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) {
return GNUTLS_E_CERTIFICATE_ERROR;
}
// Store verification status
unsigned int status = 0;
auto ret = gnutls_certificate_verify_peers2(session, &status);
if (ret < 0 or (status & GNUTLS_CERT_SIGNATURE_FAILURE) != 0) {
return GNUTLS_E_CERTIFICATE_ERROR;
}
unsigned int cert_list_size = 0;
auto cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
if (cert_list == nullptr) {
return GNUTLS_E_CERTIFICATE_ERROR;
}
// Check if received peer certificate is awaited
std::vector<std::pair<uint8_t *, uint8_t *>> crt_data;
crt_data.reserve(cert_list_size);
for (unsigned i = 0; i < cert_list_size; i++)
crt_data.emplace_back(cert_list[i].data,
cert_list[i].data + cert_list[i].size);
crt = dht::crypto::Certificate{crt_data};
return GNUTLS_E_SUCCESS;
}
using lock = std::lock_guard<std::mutex>; using lock = std::lock_guard<std::mutex>;
static constexpr std::size_t IO_BUFFER_SIZE {3000}; ///< Size of char buffer used by IO operations static constexpr std::size_t IO_BUFFER_SIZE {3000}; ///< Size of char buffer used by IO operations
...@@ -90,30 +122,9 @@ TlsTurnEndpoint::Impl::~Impl() ...@@ -90,30 +122,9 @@ TlsTurnEndpoint::Impl::~Impl()
int int
TlsTurnEndpoint::Impl::verifyCertificate(gnutls_session_t session) TlsTurnEndpoint::Impl::verifyCertificate(gnutls_session_t session)
{ {
// Support only x509 format dht::crypto::Certificate crt;
if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) { auto verified = init_crt(session, crt);
return GNUTLS_E_CERTIFICATE_ERROR; if (verified != GNUTLS_E_SUCCESS) return verified;
}
// Store verification status
unsigned int status = 0;
auto ret = gnutls_certificate_verify_peers2(session, &status);
if (ret < 0 or (status & GNUTLS_CERT_SIGNATURE_FAILURE) != 0) {
return GNUTLS_E_CERTIFICATE_ERROR;
}
unsigned int cert_list_size = 0;
auto cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
if (cert_list == nullptr) {
return GNUTLS_E_CERTIFICATE_ERROR;
}
// Check if received peer certificate is awaited
std::vector<std::pair<uint8_t*, uint8_t*>> crt_data;
crt_data.reserve(cert_list_size);
for (unsigned i=0; i<cert_list_size; i++)
crt_data.emplace_back(cert_list[i].data, cert_list[i].data + cert_list[i].size);
auto crt = dht::crypto::Certificate {crt_data};
if (!peerCertificateCheckFunc(crt)) if (!peerCertificateCheckFunc(crt))
return GNUTLS_E_CERTIFICATE_ERROR; return GNUTLS_E_CERTIFICATE_ERROR;
...@@ -398,30 +409,9 @@ constexpr std::chrono::seconds TlsSocketEndpoint::Impl::TLS_TIMEOUT; ...@@ -398,30 +409,9 @@ constexpr std::chrono::seconds TlsSocketEndpoint::Impl::TLS_TIMEOUT;
int int
TlsSocketEndpoint::Impl::verifyCertificate(gnutls_session_t session) TlsSocketEndpoint::Impl::verifyCertificate(gnutls_session_t session)
{ {
// Support only x509 format dht::crypto::Certificate crt;
if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) { auto verified = init_crt(session, crt);
return GNUTLS_E_CERTIFICATE_ERROR; if (verified != GNUTLS_E_SUCCESS) return verified;
}
// Store verification status
unsigned int status = 0;
auto ret = gnutls_certificate_verify_peers2(session, &status);
if (ret < 0 or (status & GNUTLS_CERT_SIGNATURE_FAILURE) != 0) {
return GNUTLS_E_CERTIFICATE_ERROR;
}
unsigned int cert_list_size = 0;
auto cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
if (cert_list == nullptr) {
return GNUTLS_E_CERTIFICATE_ERROR;
}
// Check if peer certificate is equal to the expected one
std::vector<std::pair<uint8_t*, uint8_t*>> crt_data;
crt_data.reserve(cert_list_size);
for (unsigned i=0; i<cert_list_size; i++)
crt_data.emplace_back(cert_list[i].data, cert_list[i].data + cert_list[i].size);
auto crt = dht::crypto::Certificate {crt_data};
if (peerCertificateCheckFunc) { if (peerCertificateCheckFunc) {
if (!(*peerCertificateCheckFunc)(crt)) { if (!(*peerCertificateCheckFunc)(crt)) {
JAMI_ERR() << "[TLS-SOCKET] Unexpected peer certificate"; JAMI_ERR() << "[TLS-SOCKET] Unexpected peer certificate";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment