Skip to content

Commit

Permalink
Merge pull request #713 from p12tic/use-std-string-5
Browse files Browse the repository at this point in the history
lib/net: Use std::string directly instead of String typedef
  • Loading branch information
shymega authored May 30, 2020
2 parents b0f0a6f + b93bccc commit 9368845
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/lib/net/IDataSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "net/ISocket.h"
#include "io/IStream.h"
#include "base/String.h"
#include "base/EventTypes.h"

//! Data stream socket interface
Expand All @@ -33,7 +32,7 @@ class IDataSocket : public ISocket, public barrier::IStream {
class ConnectionFailedInfo {
public:
ConnectionFailedInfo(const char* what) : m_what(what) { }
String m_what;
std::string m_what;
};

IDataSocket(IEventQueue* events) { }
Expand Down
5 changes: 2 additions & 3 deletions src/lib/net/NetworkAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ NetworkAddress::NetworkAddress(const NetworkAddress& addr) :
// do nothing
}

NetworkAddress::NetworkAddress(const String& hostname, int port) :
NetworkAddress::NetworkAddress(const std::string& hostname, int port) :
m_address(NULL),
m_hostname(hostname),
m_port(port)
Expand Down Expand Up @@ -196,8 +196,7 @@ NetworkAddress::getPort() const
return m_port;
}

String
NetworkAddress::getHostname() const
std::string NetworkAddress::getHostname() const
{
return m_hostname;
}
Expand Down
7 changes: 3 additions & 4 deletions src/lib/net/NetworkAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#pragma once

#include "base/String.h"
#include "base/EventTypes.h"
#include "arch/IArchNetwork.h"

Expand Down Expand Up @@ -49,7 +48,7 @@ class NetworkAddress {
is thrown with an error of \c XSocketAddress::kBadPort. The hostname
is not resolved by the c'tor; use \c resolve to do that.
*/
NetworkAddress(const String& hostname, int port);
NetworkAddress(const std::string& hostname, int port);

NetworkAddress(const NetworkAddress&);

Expand Down Expand Up @@ -109,7 +108,7 @@ class NetworkAddress {
/*!
Returns the hostname passed to the c'tor sans any port suffix.
*/
String getHostname() const;
std::string getHostname() const;

//@}

Expand All @@ -118,6 +117,6 @@ class NetworkAddress {

private:
ArchNetAddress m_address;
String m_hostname;
std::string m_hostname;
int m_port;
};
3 changes: 2 additions & 1 deletion src/lib/net/SecureListenSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "net/TSocketMultiplexerMethodJob.h"
#include "arch/XArch.h"
#include "common/DataDirectories.h"
#include "base/String.h"

static const char s_certificateDir[] = { "SSL" };
static const char s_certificateFilename[] = { "Barrier.pem" };
Expand Down Expand Up @@ -54,7 +55,7 @@ SecureListenSocket::accept()
setListeningJob();
}

String certificateFilename = barrier::string::sprintf("%s/%s/%s",
std::string certificateFilename = barrier::string::sprintf("%s/%s/%s",
DataDirectories::profile().c_str(),
s_certificateDir,
s_certificateFilename);
Expand Down
20 changes: 9 additions & 11 deletions src/lib/net/SecureSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "mt/Lock.h"
#include "arch/XArch.h"
#include "base/Log.h"
#include "base/String.h"
#include "common/DataDirectories.h"

#include <openssl/ssl.h>
Expand Down Expand Up @@ -328,8 +329,7 @@ SecureSocket::initSsl(bool server)
initContext(server);
}

bool
SecureSocket::loadCertificates(String& filename)
bool SecureSocket::loadCertificates(std::string& filename)
{
if (filename.empty()) {
showError("ssl certificate is not specified");
Expand All @@ -341,7 +341,7 @@ SecureSocket::loadCertificates(String& filename)
file.close();

if (!exist) {
String errorMsg("ssl certificate doesn't exist: ");
std::string errorMsg("ssl certificate doesn't exist: ");
errorMsg.append(filename);
showError(errorMsg.c_str());
return false;
Expand Down Expand Up @@ -630,14 +630,13 @@ SecureSocket::showError(const char* reason)
LOG((CLOG_ERR "%s", reason));
}

String error = getError();
std::string error = getError();
if (!error.empty()) {
LOG((CLOG_ERR "%s", error.c_str()));
}
}

String
SecureSocket::getError()
std::string SecureSocket::getError()
{
unsigned long e = ERR_get_error();

Expand All @@ -659,8 +658,7 @@ SecureSocket::disconnect()
sendEvent(getEvents()->forIStream().inputShutdown());
}

void
SecureSocket::formatFingerprint(String& fingerprint, bool hex, bool separator)
void SecureSocket::formatFingerprint(std::string& fingerprint, bool hex, bool separator)
{
if (hex) {
// to hexidecimal
Expand Down Expand Up @@ -696,11 +694,11 @@ SecureSocket::verifyCertFingerprint()
}

// format fingerprint into hexdecimal format with colon separator
String fingerprint(reinterpret_cast<char*>(tempFingerprint), tempFingerprintLen);
std::string fingerprint(reinterpret_cast<char*>(tempFingerprint), tempFingerprintLen);
formatFingerprint(fingerprint);
LOG((CLOG_NOTE "server fingerprint: %s", fingerprint.c_str()));

String trustedServersFilename;
std::string trustedServersFilename;
trustedServersFilename = barrier::string::sprintf(
"%s/%s/%s",
DataDirectories::profile().c_str(),
Expand All @@ -711,7 +709,7 @@ SecureSocket::verifyCertFingerprint()
LOG((CLOG_NOTE "trustedServersFilename: %s", trustedServersFilename.c_str() ));

// check if this fingerprint exist
String fileLine;
std::string fileLine;
std::ifstream file;
file.open(trustedServersFilename.c_str());

Expand Down
8 changes: 3 additions & 5 deletions src/lib/net/SecureSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SecureSocket : public TCPSocket {
EJobResult doRead() override;
EJobResult doWrite() override;
void initSsl(bool server);
bool loadCertificates(String& CertFile);
bool loadCertificates(std::string& CertFile);

private:
// SSL
Expand All @@ -66,11 +66,9 @@ class SecureSocket : public TCPSocket {
bool showCertificate();
void checkResult(int n, int& retry);
void showError(const char* reason = NULL);
String getError();
std::string getError();
void disconnect();
void formatFingerprint(String& fingerprint,
bool hex = true,
bool separator = true);
void formatFingerprint(std::string& fingerprint, bool hex = true, bool separator = true);
bool verifyCertFingerprint();

MultiplexerJobStatus serviceConnect(ISocketMultiplexerJob*, bool, bool, bool);
Expand Down
20 changes: 7 additions & 13 deletions src/lib/net/XSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
// XSocketAddress
//

XSocketAddress::XSocketAddress(EError error,
const String& hostname, int port) _NOEXCEPT :
XSocketAddress::XSocketAddress(EError error, const std::string& hostname, int port) _NOEXCEPT :
m_error(error),
m_hostname(hostname),
m_port(port)
Expand All @@ -38,7 +37,7 @@ XSocketAddress::getError() const throw()
return m_error;
}

String
std::string
XSocketAddress::getHostname() const throw()
{
return m_hostname;
Expand All @@ -50,8 +49,7 @@ XSocketAddress::getPort() const throw()
return m_port;
}

String
XSocketAddress::getWhat() const throw()
std::string XSocketAddress::getWhat() const throw()
{
static const char* s_errorID[] = {
"XSocketAddressUnknown",
Expand All @@ -77,8 +75,7 @@ XSocketAddress::getWhat() const throw()
// XSocketIOClose
//

String
XSocketIOClose::getWhat() const throw()
std::string XSocketIOClose::getWhat() const throw()
{
return format("XSocketIOClose", "close: %{1}", what());
}
Expand All @@ -88,8 +85,7 @@ XSocketIOClose::getWhat() const throw()
// XSocketBind
//

String
XSocketBind::getWhat() const throw()
std::string XSocketBind::getWhat() const throw()
{
return format("XSocketBind", "cannot bind address: %{1}", what());
}
Expand All @@ -99,8 +95,7 @@ XSocketBind::getWhat() const throw()
// XSocketConnect
//

String
XSocketConnect::getWhat() const throw()
std::string XSocketConnect::getWhat() const throw()
{
return format("XSocketConnect", "cannot connect socket: %{1}", what());
}
Expand All @@ -110,8 +105,7 @@ XSocketConnect::getWhat() const throw()
// XSocketCreate
//

String
XSocketCreate::getWhat() const throw()
std::string XSocketCreate::getWhat() const throw()
{
return format("XSocketCreate", "cannot create socket: %{1}", what());
}
9 changes: 4 additions & 5 deletions src/lib/net/XSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "io/XIO.h"
#include "base/XBase.h"
#include "base/String.h"
#include "common/basic_types.h"

//! Generic socket exception
Expand All @@ -41,7 +40,7 @@ class XSocketAddress : public XSocket {
kBadPort //!< The port is invalid
};

XSocketAddress(EError, const String& hostname, int port) _NOEXCEPT;
XSocketAddress(EError, const std::string& hostname, int port) _NOEXCEPT;
virtual ~XSocketAddress() _NOEXCEPT { }

//! @name accessors
Expand All @@ -50,19 +49,19 @@ class XSocketAddress : public XSocket {
//! Get the error code
EError getError() const throw();
//! Get the hostname
String getHostname() const throw();
std::string getHostname() const throw();
//! Get the port
int getPort() const throw();

//@}

protected:
// XBase overrides
virtual String getWhat() const throw();
virtual std::string getWhat() const throw();

private:
EError m_error;
String m_hostname;
std::string m_hostname;
int m_port;
};

Expand Down

0 comments on commit 9368845

Please sign in to comment.