31 #ifndef TCPSOCKETOPERATIONS_H_    32 #define TCPSOCKETOPERATIONS_H_    35 #include "../Managers/FileDescriptorManager.h"    36 #include "../IQueue.h"    37 #include "../Encoding/BinaryRpc.h"    47 #include <unordered_map>    56 #include <arpa/inet.h>    57 #include <netinet/tcp.h>    58 #include <netinet/in.h>     60 #include <sys/socket.h>    61 #include <sys/epoll.h>    67 #include <gnutls/x509.h>    68 #include <gnutls/gnutls.h>   163     gnutls_certificate_credentials_t _credentials = 
nullptr;
   165     gnutls_dh_params_t _dhParams = 
nullptr;
   169     gnutls_certificate_credentials_t 
get();
   175     int32_t thread_index = -1;
   183     int64_t clientCertExpiration = -1;
   189     std::queue<std::shared_ptr<TcpPacket>> 
backlog;
   202       this->clientData = clientData;
   214     std::shared_ptr<Security::SecureVector<uint8_t>> 
keyData;
   222     uint32_t connectionBacklogSize = 100;
   223     uint32_t maxConnections = 10;
   224     uint32_t serverThreads = 1;
   228     bool requireClientCert = 
false;
   298   TcpSocket(
BaseLib::SharedObjects *baseLib, std::string hostname, std::string port, 
bool useSsl, std::string caFile, 
bool verifyCertificate, std::string clientCertFile, std::string clientKeyFile);
   330             std::string hostname,
   333             bool verifyCertificate,
   336             std::string clientCertFile,
   337             std::string clientCertData,
   338             std::string clientKeyFile,
   351   static PFileDescriptor bindAndReturnSocket(
FileDescriptorManager &fileDescriptorManager, 
const std::string &address, 
const std::string &port, uint32_t connectionBacklogSize, std::string &listenAddress, int32_t &listenPort);
   354   std::string getIpAddress();
   355   int32_t 
getPort() { 
return _boundListenPort; }
   363     _hostname = hostname;
   373       if (_useSsl) initSsl();
   376   void setCertificates(std::unordered_map<std::string, PCertificateInfo> &certificates);
   377   void reloadCertificates();
   380     _verifyCertificate = verifyCertificate;
   384     _verifyHostname = verifyHostname;
   393     _verificationHostname = hostname;
   395   std::unordered_map<std::string, std::shared_ptr<CertificateCredentials>> 
getCredentials() { 
return _certificateCredentials; }
   408   double GetPacketsPerMinuteReceived();
   409   double GetPacketsPerMinuteSent();
   416   double GetServerThreadLoad();
   423   double GetProcessingThreadLoad();
   430   double GetProcessingThreadLoadMax();
   442   int32_t proofread(
char *buffer, int32_t bufferSize);
   456   int32_t proofread(
char *buffer, int32_t bufferSize, 
bool &moreData, 
bool skip_poll = 
false);
   467   int32_t proofwrite(
const std::shared_ptr<std::vector<char>> &data);
   478   int32_t proofwrite(
const std::vector<char> &data);
   489   int32_t proofwrite(
const std::string &data);
   500   int32_t proofwrite(
const char *buffer, int32_t bytesToWrite);
   517   void bindServerSocket(std::string address, std::string port, std::string &listenAddress);
   529   void startPreboundServer(std::string &listenAddress, 
size_t processingThreads = 0);
   539   void startServer(std::string address, std::string port, std::string &listenAddress, 
size_t processingThreads = 0);
   549   void startServer(std::string address, std::string &listenAddress, int32_t &listenPort, 
size_t processingThreads = 0);
   559   void waitForServerStopped();
   568   bool sendToClient(int32_t clientId, 
const TcpPacket &packet, 
bool closeConnection = 
false);
   577   bool sendToClient(int32_t clientId, 
const std::vector<char> &packet, 
bool closeConnection = 
false);
   584   void closeClientConnection(int32_t clientId);
   590   int32_t clientCount();
   596   uint32_t processingQueueSize();
   605   std::string getClientCertDn(int32_t clientId);
   607   std::string getClientCertSerial(int32_t clientId);
   609   int64_t getClientCertExpiration(int32_t clientId);
   615     std::atomic<int64_t> last_measurement{0};
   616     std::atomic<double> last_output{0.0};
   620   int32_t _connectionRetries = 3;
   621   int64_t _readTimeout = 15000000;
   622   int64_t _writeTimeout = 15000000;
   624   bool _autoConnect = 
true;
   632   bool _verifyCertificate = 
true;
   633   bool _verifyHostname = 
true;
   636   bool _isServer = 
false;
   637   uint32_t _backlogSize = 100;
   638   uint32_t _maxConnections = 10;
   641   bool _requireClientCert = 
false;
   642   std::atomic<uint32_t> server_threads_in_use_{0};
   652   int32_t _boundListenPort = -1;
   654   gnutls_priority_t _tlsPriorityCache = 
nullptr;
   659   int64_t _lastGarbageCollection = 0;
   667   bool _useSsl = 
false;
   678   void getSocketDescriptor();
   679   void getConnection();
   682   void initTlsPriorityCache();
   688   void serverThread(uint32_t thread_index);
   689   void processQueueEntry(int32_t index, std::shared_ptr<BaseLib::IQueueEntry> &entry) 
override;
   690   void collectGarbage();
   691   void collectGarbage(std::unordered_map<int32_t, PTcpClientData> &clients);
   692   void initClientSsl(PTcpClientData &clientData);
   693   void readClient(
const PTcpClientData &client_data, std::unordered_map<int32_t, PTcpClientData> &backlog_clients);
 std::mutex _readMutex
Definition: TcpSocket.h:629
PFileDescriptor _socketDescriptor
Definition: TcpSocket.h:666
Definition: TcpSocket.h:210
void setPort(std::string port)
Definition: TcpSocket.h:365
Definition: TcpSocket.h:612
std::queue< std::shared_ptr< TcpPacket > > backlog
Definition: TcpSocket.h:189
void setVerifyCertificate(bool verifyCertificate)
Definition: TcpSocket.h:378
std::function< void(int32_t clientId, TcpPacket &packet)> packetReceivedCallback
Definition: TcpSocket.h:232
std::function< void(int32_t clientId, int32_t errorCode, const std::string &errorString)> _connectionClosedCallbackEx
Definition: TcpSocket.h:647
std::string _listenPort
Definition: TcpSocket.h:651
Definition: FileDescriptorManager.h:54
This is the base library main class. 
Definition: BaseLib.h:95
void setReadTimeout(int64_t timeout)
Definition: TcpSocket.h:358
std::unordered_map< std::string, std::shared_ptr< CertificateCredentials > > _certificateCredentials
Stores the certificate credentials so that they can be replaced at any time. 
Definition: TcpSocket.h:676
AverageMeanData(const AverageMeanData &data)
Definition: TcpSocket.h:614
The class only makes sure that the vector is not copyable and the data is zeroed on destruction...
Definition: Io.h:40
std::string caFile
Definition: TcpSocket.h:215
std::function< void(int32_t clientId, int32_t errorCode, const std::string &errorString)> connectionClosedCallbackEx
Definition: TcpSocket.h:231
int32_t getPort()
Definition: TcpSocket.h:355
Definition: BaseLib.cpp:34
std::mutex _socketDescriptorMutex
Definition: TcpSocket.h:665
std::string _listenAddress
Definition: TcpSocket.h:650
void setConnectionRetries(int32_t retries)
Definition: TcpSocket.h:357
std::unordered_map< std::string, std::shared_ptr< CertificateCredentials > > certificateCredentials
Definition: TcpSocket.h:180
std::unordered_map< std::string, PCertificateInfo > _certificates
Definition: TcpSocket.h:631
Class to easily create a TCP server or client. 
Definition: TcpSocket.h:159
Definition: TcpSocket.h:174
void setAutoConnect(bool autoConnect)
Definition: TcpSocket.h:360
std::string dhParamFile
Definition: TcpSocket.h:226
std::string certFile
Definition: TcpSocket.h:211
std::string _port
Definition: TcpSocket.h:628
std::vector< AverageMeanData > average_packets_per_minute_sent_
Definition: TcpSocket.h:644
std::string _dhParamFile
Definition: TcpSocket.h:639
std::shared_ptr< CertificateCredentials > _currentClientCertificateCredentials
Contains a copy of the current credentials in case the credentials are replaced while the connection ...
Definition: TcpSocket.h:672
void setUseSSL(bool useSsl)
Definition: TcpSocket.h:369
std::string dhParamData
Definition: TcpSocket.h:227
std::unordered_map< std::string, std::shared_ptr< CertificateCredentials > > getCredentials()
Definition: TcpSocket.h:395
std::string clientCertSerial
Definition: TcpSocket.h:182
std::shared_ptr< CertificateInfo > PCertificateInfo
Definition: TcpSocket.h:218
std::function< void(int32_t clientId, TcpPacket &packet)> _packetReceivedCallback
Definition: TcpSocket.h:648
std::mutex backlogMutex
Mutex for busy and backlog 
Definition: TcpSocket.h:187
std::function< void(int32_t clientId, std::string address, uint16_t port)> newConnectionCallback
Definition: TcpSocket.h:229
std::string _hostname
Definition: TcpSocket.h:626
void setHostname(std::string hostname)
Definition: TcpSocket.h:361
std::vector< uint8_t > TcpPacket
Definition: TcpSocket.h:172
void setVerificationHostname(const std::string &hostname)
Only relevant for TLS connections. 
Definition: TcpSocket.h:391
std::vector< std::thread > server_threads_
Definition: TcpSocket.h:657
std::string _dhParamData
Definition: TcpSocket.h:640
Definition: TcpSocket.h:220
bool getRequireClientCert()
Definition: TcpSocket.h:356
This class implements a queue after the producer-consumer paradigm. 
Definition: IQueue.h:52
std::mutex _certificateCredentialsMutex
Definition: TcpSocket.h:668
std::vector< uint8_t > buffer
Definition: TcpSocket.h:178
void setWriteTimeout(int64_t timeout)
Definition: TcpSocket.h:359
std::vector< AverageMeanData > average_packets_per_minute_received_
Definition: TcpSocket.h:643
std::string caData
Definition: TcpSocket.h:216
std::string _ipAddress
Definition: TcpSocket.h:625
std::shared_ptr< TcpSocket > socket
Definition: TcpSocket.h:179
void setVerifyHostname(bool verifyHostname)
Definition: TcpSocket.h:382
std::string keyFile
Definition: TcpSocket.h:213
std::shared_ptr< FileDescriptor > PFileDescriptor
Definition: FileDescriptorManager.h:52
std::string clientCertDn
Definition: TcpSocket.h:181
std::function< void(int32_t clientId)> _connectionClosedCallback
Definition: TcpSocket.h:646
PTcpClientData clientData
Definition: TcpSocket.h:207
std::string _verificationHostname
Definition: TcpSocket.h:627
PFileDescriptor fileDescriptor
Definition: TcpSocket.h:177
std::atomic_bool _connecting
Definition: TcpSocket.h:623
Definition: TcpSocket.h:161
TcpClientData()
Definition: TcpSocket.h:191
std::map< int32_t, PTcpClientData > _clients
Definition: TcpSocket.h:662
std::string certData
Definition: TcpSocket.h:212
QueueEntry(const PTcpClientData &clientData)
Definition: TcpSocket.h:201
std::function< void(int32_t clientId, std::string address, uint16_t port)> _newConnectionCallback
Definition: TcpSocket.h:645
std::atomic_bool _stopServer
Definition: TcpSocket.h:656
std::shared_ptr< TcpClientData > PTcpClientData
Definition: TcpSocket.h:195
std::mutex _writeMutex
Definition: TcpSocket.h:630
std::function< void(int32_t clientId)> connectionClosedCallback
Definition: TcpSocket.h:230
std::mutex _clientsMutex
Definition: TcpSocket.h:661
std::unordered_map< std::string, PCertificateInfo > certificates
Definition: TcpSocket.h:225
std::shared_ptr< BaseLib::TcpSocket > PTcpSocket
Definition: TcpSocket.h:697
Definition: TcpSocket.h:197
std::shared_ptr< Security::SecureVector< uint8_t > > keyData
Definition: TcpSocket.h:214