53#include <netinet/in.h>
55#include <sys/socket.h>
58#include <sys/select.h>
69#define strcasecmp _stricmp
70#define suseconds_t long
76#define ECONNREFUSED WSAECONNREFUSED
77#define EINPROGRESS WSAEINPROGRESS
78#define ETIMEDOUT WSAETIMEDOUT
79#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
80#define ECONNABORTED WSAECONNABORTED
81#define ENETUNREACH WSAENETUNREACH
91# include <unicode/ucnv.h>
101#include <openssl/ssl.h>
102#include <openssl/err.h>
103#include <openssl/rand.h>
121# define CS_STRING Cstring
123# define CS_STRING std::string
129# define CS_DEBUG( f ) std::cerr << __FILE__ << ":" << __LINE__ << " " << f << std::endl
131# define CS_DEBUG(f) (void)0
141# define PERROR( f ) __Perror( f, __FILE__, __LINE__ )
143# define PERROR( f ) (void)0
154#define CS_INVALID_SOCK INVALID_SOCKET
158#define CS_INVALID_SOCK -1
163#define HAVE_UNIX_SOCKET
170#ifdef HAVE_UNIX_SOCKET
174#ifndef _NO_CSOCKET_NS
189 m_pBuffer = (
char * )malloc( iSize );
212 memset( (
struct sockaddr_in * ) &m_saddr,
'\0',
sizeof( m_saddr ) );
214 memset( (
struct sockaddr_in6 * ) &m_saddr6,
'\0',
sizeof( m_saddr6 ) );
216 m_iAFRequire = RAF_ANY;
225 RAF_INET6 = AF_INET6,
237 in_addr *
GetAddr() {
return( &( m_saddr.sin_addr ) ); }
241 in6_addr *
GetAddr6() {
return( &( m_saddr6.sin6_addr ) ); }
251 sockaddr_in6 m_saddr6;
253 EAFRequire m_iAFRequire;
297 struct addrinfo * m_pAddrRes;
298 struct addrinfo m_cHints;
319template <
class T>
inline void CS_Delete( T * & p ) {
if( p ) {
delete p; p = NULL; } }
331void SSLErrors(
const char *filename, uint32_t iLineNum );
355 return( WSAGetLastError() );
369 FD_SET( iSock, set );
374 return( FD_ISSET( iSock, set ) != 0 );
379 FD_CLR( iSock, set );
399 void run( timeval & tNow );
410 void Start(
const timeval& TimeSequence );
447 bool m_bActive, m_bPause;
448 timeval m_tTimeSequence;
449 uint32_t m_iMaxCycles, m_iCycles;
476 virtual bool FDsThatTriggered(
const std::map< cs_sock_t, short > & miiReadyFds ) {
return(
true ); }
483 virtual bool CheckFDs(
const std::map< cs_sock_t, short > & miiReadyFds );
490 void Add(
cs_sock_t iFD,
short iMonitorEvents ) { m_miiMonitorFDs[iFD] = iMonitorEvents; }
518 const std::vector<CCron *> &
GetCrons()
const {
return( m_vcCrons ); }
530 virtual void DelCron(
const CS_STRING & sName,
bool bDeleteAll =
true,
bool bCaseSensitive =
true );
536 void CheckFDs(
const std::map< cs_sock_t, short > & miiReadyFds );
537 void AssignFDs( std::map< cs_sock_t, short > & miiReadyFds,
struct timeval * tvtimeout );
603 READ_CONNREFUSED = -3,
634 EDP_SSL = (EDP_SSLv2|EDP_SSLv3)
671#ifdef HAVE_UNIX_SOCKET
695 virtual bool Listen( uint16_t iPort,
int iMaxConns = SOMAXCONN,
const CS_STRING & sBindHost =
"", uint32_t iTimeout = 0,
bool bDetach =
false );
730 virtual bool Write(
const char *data,
size_t len );
802 TMO_ALL = TMO_READ|TMO_WRITE|TMO_ACCEPT
807 void SetTimeout(
int iTimeout, uint32_t iTimeoutType = TMO_ALL );
819 virtual void PushBuff(
const char *data,
size_t len,
bool bStartAtZero =
false );
880 bool IsClosed()
const {
return( GetCloseType() != CLT_DONT ); }
963 virtual void SetRate( uint32_t iBytes, uint64_t iMilliseconds );
983 virtual void ReadData(
const char *data,
size_t len ) {}
1127 m_address.SetIPv6( b );
1128 m_bindhost.SetIPv6( b );
1133 m_address.SetAFRequire( iAFRequire );
1134 m_bindhost.SetAFRequire( iAFRequire );
1163 virtual int ConvertAddress(
const struct sockaddr_storage * pAddr, socklen_t iAddrLen,
CS_STRING & sIP, uint16_t * piPort )
const;
1166 CSSockAddr * GetCurrentAddr()
const {
return( m_pCurrAddr ); }
1167 void SetAresFinished(
int status ) { m_pCurrAddr = NULL; m_iARESStatus = status; }
1168 ares_channel GetAresChannel()
const {
return( m_pARESChannel ); }
1178 UConverterToUnicodeArgs* toArgs,
1179 const char* codeUnits,
1181 UConverterCallbackReason reason,
1184 UConverterFromUnicodeArgs* fromArgs,
1185 const UChar* codeUnits,
1188 UConverterCallbackReason reason,
1196 void ShrinkSendBuff();
1197 void IncBuffPos(
size_t uBytes );
1203 int m_iTimeout, m_iConnType, m_iMethod, m_iTcount, m_iMaxConns;
1204 bool m_bUseSSL, m_bIsConnected;
1205 bool m_bsslEstablished, m_bEnableReadLine, m_bPauseRead;
1206 CS_STRING m_shostname, m_sbuffer, m_sSockName, m_sDHParamFile, m_sKeyFile, m_sPemFile, m_sCipherType, m_sParentName;
1208 ECloseType m_eCloseType;
1211 mutable uint16_t m_iRemotePort, m_iLocalPort;
1212 mutable CS_STRING m_sLocalIP, m_sRemoteIP;
1214 uint64_t m_iMaxMilliSeconds, m_iLastSendTime, m_iBytesRead, m_iBytesWritten, m_iStartTime;
1215 uint32_t m_iMaxBytes, m_iMaxStoredBufferLength, m_iTimeoutType;
1216 size_t m_iLastSend, m_uSendBufferPos;
1219 bool m_bIsIPv6, m_bSkipConnect;
1220 time_t m_iLastCheckTimeoutTime;
1225 SSL_CTX * m_ssl_ctx;
1226 uint32_t m_iRequireClientCertFlags;
1227 u_int m_uDisableProtocols;
1228 bool m_bNoSSLCompression;
1229 bool m_bSSLCipherServerPreference;
1235 bool ConfigureCTXOptions( SSL_CTX * pCTX );
1240 cs_sock_t CreateSocket(
bool bListen =
false,
bool bUnix =
false );
1241 void Init(
const CS_STRING & sHostname, uint16_t uPort,
int iTimeout = 60 );
1244 ECONState m_eConState;
1246 uint32_t m_iCurBindCount, m_iDNSTryCount;
1249 ares_channel m_pARESChannel;
1255 UConverter* m_cnvInt;
1256 UConverter* m_cnvExt;
1277 m_sHostname = sHostname;
1279 m_iTimeout = iTimeout;
1365 m_sBindHost = sBindHost;
1367 m_iMaxConns = SOMAXCONN;
1370 m_bDetach = bDetach;
1373 m_iRequireCertFlags = 0;
1397 void SetPort( uint16_t iPort ) { m_iPort = iPort; }
1423 void SetRequiresClientCert(
bool b ) { m_iRequireCertFlags = ( b ? SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT : 0 ); }
1433 uint32_t m_iTimeout;
1437 CS_STRING m_sDHParamLocation, m_sKeyLocation, m_sPemLocation, m_sPemPass, m_sCipher;
1438 uint32_t m_iRequireCertFlags;
1489 SELECT_TIMEOUT = -2,
1490 SELECT_TRYAGAIN = -3
1537 void DynamicSelectLoop( uint64_t iLowerBounds, uint64_t iUpperBounds, time_t iMaxResolution = 3600 );
1618 virtual int Select( std::map< cs_sock_t, short > & miiReadyFds,
struct timeval *tvtimeout );
1627 void Select( std::map<Csock *, EMessages> & mpeSocks );
1629 timeval GetDynamicSleepTime(
const timeval& tNow,
const timeval& tMaxResolution )
const;
1632 virtual void SelectSock( std::map<Csock *, EMessages> & mpeSocks,
EMessages eErrno,
Csock * pcSock );
1640 uint64_t m_iCallTimeouts;
1641 uint64_t m_iBytesRead;
1642 uint64_t m_iBytesWritten;
1643 uint64_t m_iSelectWait;
1661 return(
new T( sHostname, uPort, iTimeout ) );
1665#ifndef _NO_CSOCKET_NS
void TFD_ZERO(fd_set *set)
wrappers for FD_SET and such to work in templates.
Definition Csocket.h:362
void SSLErrors(const char *filename, uint32_t iLineNum)
bool InitCsocket()
This does all the csocket initialized inclusing InitSSL() and win32 specific initializations,...
void __Perror(const CS_STRING &s, const char *pszFile, uint32_t iLineNo)
bool InitSSL(ECompType eCompressionType=CT_NONE)
You HAVE to call this in order to use the SSL library, calling InitCsocket() also calls this so unles...
ECompType
Definition Csocket.h:323
@ CT_NONE
Definition Csocket.h:324
@ CT_ZLIB
Definition Csocket.h:325
int(* FPCertVerifyCB)(int, X509_STORE_CTX *)
Definition Csocket.h:549
void CS_Delete(T *&p)
Definition Csocket.h:319
int GetCsockSSLIdx()
This returns the [ex_]data index position for SSL objects only.
int CS_GetAddrInfo(const CS_STRING &sHostname, Csock *pSock, CSSockAddr &csSockAddr)
backwards compatible wrapper around CGetAddrInfo and gethostbyname
ssize_t cs_ssize_t
Definition Csocket.h:157
void ShutdownCsocket()
Shutdown and release global allocated memory.
#define CS_EXPORT
Definition Csocket.h:136
int GetSockError()
Definition Csocket.h:352
void TFD_CLR(cs_sock_t iSock, fd_set *set)
Definition Csocket.h:377
const uint32_t CS_BLOCKSIZE
Definition Csocket.h:318
Csock * GetCsockFromCTX(X509_STORE_CTX *pCTX)
returns the sock object associated to the particular context. returns NULL on failure or if not avail...
void CSAdjustTVTimeout(struct timeval &tv, long iTimeoutMS)
adjusts tv with a new timeout if iTimeoutMS is smaller
int cs_sock_t
Definition Csocket.h:156
void TFD_SET(cs_sock_t iSock, fd_set *set)
Definition Csocket.h:367
bool TFD_ISSET(cs_sock_t iSock, fd_set *set)
Definition Csocket.h:372
this is the main cron job class
Definition Csocket.h:393
void SetName(const CS_STRING &sName)
virtual ~CCron()
Definition Csocket.h:396
void Start(double dTimeSequence)
starts and runs infinity amount of times
timeval GetInterval() const
void Pause()
pauses excution of your code in RunJob
void run(timeval &tNow)
This is used by the Job Manager, and not you directly.
void StartMaxCycles(const timeval &tTimeSequence, uint32_t iMaxCycles)
void Reset()
reset the timer
timeval GetNextRun() const
returns the timestamp of the next estimated run time. Note that it may not run at this EXACT time,...
Definition Csocket.h:435
bool m_bRunOnNextCall
if set to true, RunJob() gets called on next invocation of run() despite the timeout
Definition Csocket.h:443
uint32_t GetMaxCycles() const
const CS_STRING & GetName() const
void Stop()
call this to turn off your cron, it will be removed
void Start(const timeval &TimeSequence)
void StartMaxCycles(double dTimeSequence, uint32_t iMaxCycles)
uint32_t GetCyclesLeft() const
virtual void RunJob()
this is the method you should override
bool isValid() const
returns true if cron is active
void UnPause()
removes the pause on RunJon
this function is a wrapper around getaddrinfo (for ipv6)
Definition Csocket.h:275
int Process()
the simplest part of the function, only calls getaddrinfo and uses only m_sHostname,...
CGetAddrInfo(const CS_STRING &sHostname, Csock *pSock, CSSockAddr &csSockAddr)
ctor
int Finish()
finalizes and sets up csSockAddr (and pSock if not NULL), only needs to be called if Process returns ...
void Init()
simply sets up m_cHints for use in process
Ease of use self deleting char * class.
Definition Csocket.h:185
~CSCharBuffer()
Definition Csocket.h:191
char * operator()()
Definition Csocket.h:195
CSCharBuffer(size_t iSize)
Definition Csocket.h:187
options for creating a connection
Definition Csocket.h:1268
void SetBindHost(const CS_STRING &s)
sets the hostname to bind to (vhost support)
Definition Csocket.h:1309
CSConnection(const CS_STRING &sHostname, uint16_t iPort, int iTimeout=60)
Definition Csocket.h:1275
const CS_STRING & GetCipher() const
Definition Csocket.h:1297
const CS_STRING & GetKeyLocation() const
Definition Csocket.h:1299
void SetHostname(const CS_STRING &s)
sets the hostname to connect to
Definition Csocket.h:1305
uint16_t m_iPort
Definition Csocket.h:1330
void SetIsSSL(bool b)
set to true to enable SSL
Definition Csocket.h:1315
const CS_STRING & GetPemPass() const
Definition Csocket.h:1301
const CS_STRING & GetBindHost() const
Definition Csocket.h:1290
int GetTimeout() const
Definition Csocket.h:1292
void SetPemLocation(const CS_STRING &s)
set the location of the pemfile
Definition Csocket.h:1323
const CS_STRING & GetHostname() const
Definition Csocket.h:1288
CS_STRING m_sBindHost
Definition Csocket.h:1329
bool m_bIsSSL
Definition Csocket.h:1332
CS_STRING m_sCipher
Definition Csocket.h:1335
const CS_STRING & GetSockName() const
Definition Csocket.h:1289
void SetPemPass(const CS_STRING &s)
set the pemfile pass
Definition Csocket.h:1325
virtual ~CSConnection()
Definition Csocket.h:1286
CSSockAddr::EAFRequire GetAFRequire() const
Definition Csocket.h:1294
const CS_STRING & GetDHParamLocation() const
Definition Csocket.h:1300
int m_iTimeout
Definition Csocket.h:1331
void SetSockName(const CS_STRING &s)
sets the name of the socket, used for reference, ie in FindSockByName()
Definition Csocket.h:1307
const CS_STRING & GetPemLocation() const
Definition Csocket.h:1298
bool GetIsSSL() const
Definition Csocket.h:1293
void SetTimeout(int i)
sets the connection timeout
Definition Csocket.h:1313
void SetCipher(const CS_STRING &s)
set the cipher strength to use, default is HIGH
Definition Csocket.h:1321
void SetPort(uint16_t i)
sets the port to connect to
Definition Csocket.h:1311
CSSockAddr::EAFRequire m_iAFrequire
Definition Csocket.h:1333
void SetAFRequire(CSSockAddr::EAFRequire iAFRequire)
sets the AF family type required
Definition Csocket.h:1317
uint16_t GetPort() const
Definition Csocket.h:1291
options container to create a listener
Definition Csocket.h:1355
uint16_t GetPort() const
Definition Csocket.h:1380
void SetAFRequire(CSSockAddr::EAFRequire iAFRequire)
sets the AF family type required
Definition Csocket.h:1409
void SetRequiresClientCert(bool b)
set to true if require a client certificate (deprecated
Definition Csocket.h:1423
uint32_t GetRequireClientCertFlags() const
Definition Csocket.h:1393
void SetPemPass(const CS_STRING &s)
set the pemfile pass
Definition Csocket.h:1421
const CS_STRING & GetKeyLocation() const
Definition Csocket.h:1390
uint32_t GetTimeout() const
Definition Csocket.h:1385
void SetDHParamLocation(const CS_STRING &s)
set the location of the dhparamfile
Definition Csocket.h:1419
void SetSockName(const CS_STRING &sSockName)
sets the sock name for later reference (ie FindSockByName)
Definition Csocket.h:1399
void SetTimeout(uint32_t i)
sets the listen timeout. The listener class will close after timeout has been reached if not 0
Definition Csocket.h:1407
const CS_STRING & GetCipher() const
Definition Csocket.h:1388
virtual ~CSListener()
Definition Csocket.h:1376
void SetBindHost(const CS_STRING &sBindHost)
sets the host to bind to
Definition Csocket.h:1401
const CS_STRING & GetPemLocation() const
Definition Csocket.h:1391
const CS_STRING & GetDHParamLocation() const
Definition Csocket.h:1389
const CS_STRING & GetSockName() const
Definition Csocket.h:1381
void SetCipher(const CS_STRING &s)
set the cipher strength to use, default is HIGH
Definition Csocket.h:1413
const CS_STRING & GetBindHost() const
Definition Csocket.h:1382
void SetDetach(bool b)
Definition Csocket.h:1378
int GetMaxConns() const
Definition Csocket.h:1384
const CS_STRING & GetPemPass() const
Definition Csocket.h:1392
void SetPemLocation(const CS_STRING &s)
set the location of the pemfile
Definition Csocket.h:1415
CSListener(uint16_t iPort, const CS_STRING &sBindHost="", bool bDetach=false)
Definition Csocket.h:1362
void SetIsSSL(bool b)
set to true to enable SSL
Definition Csocket.h:1403
bool GetDetach() const
Definition Csocket.h:1379
void SetMaxConns(int i)
set max connections as called by accept()
Definition Csocket.h:1405
void SetRequireClientCertFlags(unsigned int iRequireCertFlags)
bitwise flags, 0 means don't require cert, SSL_VERIFY_PEER verifies peers, SSL_VERIFY_FAIL_IF_NO_PEER...
Definition Csocket.h:1425
CSSockAddr::EAFRequire GetAFRequire() const
Definition Csocket.h:1386
void SetPort(uint16_t iPort)
sets the port to listen on. Set to 0 to listen on a random port
Definition Csocket.h:1397
void SetKeyLocation(const CS_STRING &s)
set the location of the keyfile
Definition Csocket.h:1417
bool GetIsSSL() const
Definition Csocket.h:1383
Class to tie sockets to for monitoring by Csocket at either the Csock or TSockManager.
Definition Csocket.h:458
bool m_bEnabled
Definition Csocket.h:500
std::map< cs_sock_t, short > m_miiMonitorFDs
Definition Csocket.h:499
CSMonitorFD()
Definition Csocket.h:460
virtual bool GatherFDsForSelect(std::map< cs_sock_t, short > &miiReadyFds, long &iTimeoutMS)
called before select, typically you don't need to reimplement this just add sockets via Add and let t...
void DisableMonitor()
causes this monitor to be removed
Definition Csocket.h:494
virtual bool FDsThatTriggered(const std::map< cs_sock_t, short > &miiReadyFds)
called when there are fd's belonging to this class that have triggered
Definition Csocket.h:476
virtual ~CSMonitorFD()
Definition Csocket.h:461
virtual bool CheckFDs(const std::map< cs_sock_t, short > &miiReadyFds)
gets called to diff miiReadyFds with m_miiMonitorFDs, and calls FDsThatTriggered when appropriate.
void Remove(cs_sock_t iFD)
removes this fd from monitoring
Definition Csocket.h:492
void Add(cs_sock_t iFD, short iMonitorEvents)
adds a file descriptor to be monitored
Definition Csocket.h:490
bool IsEnabled() const
Definition Csocket.h:496
Definition Csocket.h:1340
CSSSLConnection(const CS_STRING &sHostname, uint16_t iPort, int iTimeout=60)
Definition Csocket.h:1342
Definition Csocket.h:1444
CSSSListener(uint16_t iPort, const CS_STRING &sBindHost="")
Definition Csocket.h:1446
sockaddr wrapper.
Definition Csocket.h:207
in6_addr * GetAddr6()
Definition Csocket.h:241
in_addr * GetAddr()
Definition Csocket.h:237
EAFRequire GetAFRequire() const
Definition Csocket.h:245
EAFRequire
Definition Csocket.h:222
@ RAF_ANY
Definition Csocket.h:223
void SetAFRequire(EAFRequire iWhich)
Definition Csocket.h:244
sockaddr_in6 * GetSockAddr6()
Definition Csocket.h:240
bool GetIPv6() const
Definition Csocket.h:233
void SinPort(uint16_t iPort)
socklen_t GetSockAddrLen6()
Definition Csocket.h:239
socklen_t GetSockAddrLen()
Definition Csocket.h:235
sockaddr_in * GetSockAddr()
Definition Csocket.h:236
virtual ~CSSockAddr()
Definition Csocket.h:218
CSSockAddr()
Definition Csocket.h:209
simple class to share common code to both TSockManager and Csock
Definition Csocket.h:509
virtual void AddCron(CCron *pcCron)
insert a newly created cron
void AssignFDs(std::map< cs_sock_t, short > &miiReadyFds, struct timeval *tvtimeout)
virtual void DelCron(uint32_t iPos)
delete cron by idx
void CheckFDs(const std::map< cs_sock_t, short > &miiReadyFds)
const std::vector< CCron * > & GetCrons() const
returns a const reference to the crons associated to this socket
Definition Csocket.h:518
void MonitorFD(CSMonitorFD *pMonitorFD)
add an FD set to monitor
Definition Csocket.h:540
CSockCommon()
Definition Csocket.h:511
virtual void Cron()
This has a garbage collecter, and is used internall to call the jobs.
std::vector< CCron * > m_vcCrons
Definition Csocket.h:543
std::vector< CSMonitorFD * > m_vcMonitorFD
Definition Csocket.h:544
virtual void DelCronByAddr(CCron *pcCron)
delete cron by address
virtual void DelCron(const CS_STRING &sName, bool bDeleteAll=true, bool bCaseSensitive=true)
deletes a cron by name
Best class to use to interact with the sockets.
Definition Csocket.h:1476
virtual Csock * GetSockObj(const CS_STRING &sHostname, uint16_t uPort, int iTimeout=60)
virtual bool Listen(const CSListener &cListen, Csock *pcSock=NULL, uint16_t *piRandPort=NULL)
Sets up a listening socket.
void Connect(const CSConnection &cCon, Csock *pcSock=NULL)
Create a connection.
virtual bool SwapSockByAddr(Csock *pNewSock, Csock *pOrigSock)
swaps out a sock with a copy of the original sock
virtual void DelSockByAddr(Csock *pcSock)
Delete a sock by addr its position is looked up the socket is deleted, the appropriate call backs are...
virtual Csock * FindSockByName(const CS_STRING &sName)
returns a pointer to the FIRST sock found by name or NULL on no match
virtual Csock * FindSockByLocalPort(uint16_t iPort)
returns a pointer to the FIRST sock found by port or NULL on no match
virtual void Loop()
Best place to call this class for running, all the call backs are called.
ECheckType
this is a strict wrapper around C-api select(). Added in the event you need to do special work here
Definition Csocket.h:1608
bool HasFDs() const
simple method to see if there are file descriptors being processed, useful to know if all the work is...
virtual std::vector< Csock * > FindSocksByRemoteHost(const CS_STRING &sHostname)
returns a vector of pointers to socks with sHostname as being connected
bool FDHasCheck(cs_sock_t iFd, std::map< cs_sock_t, short > &miiReadyFds, ECheckType eType)
uint64_t GetSelectTimeout() const
Get the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond )
Definition Csocket.h:1566
virtual Csock * FindSockByRemotePort(uint16_t iPort)
returns a pointer to the FIRST sock found by port or NULL on no match
virtual ~CSocketManager()
void FDSetCheck(cs_sock_t iFd, std::map< cs_sock_t, short > &miiReadyFds, ECheckType eType)
uint64_t GetBytesRead() const
Get the bytes read from all sockets current and past.
uint64_t GetBytesWritten() const
Get the bytes written to all sockets current and past.
void SetSelectTimeout(uint64_t iTimeout)
Set the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond ) Setting this to 0 will cause no time...
Definition Csocket.h:1569
void DynamicSelectLoop(uint64_t iLowerBounds, uint64_t iUpperBounds, time_t iMaxResolution=3600)
this is similar to loop, except that it dynamically adjusts the select time based on jobs and timeout...
virtual bool SwapSockByIdx(Csock *pNewSock, size_t iOrginalSockIdx)
swaps out a sock with a copy of the original sock
virtual Csock * FindSockByFD(cs_sock_t iFD)
returns a pointer to the FIRST sock found by filedescriptor or NULL on no match
virtual void AddSock(Csock *pcSock, const CS_STRING &sSockName)
Make this method virtual, so you can override it when a socket is added.
virtual int Select(std::map< cs_sock_t, short > &miiReadyFds, struct timeval *tvtimeout)
virtual std::vector< Csock * > FindSocksByName(const CS_STRING &sName)
virtual void DelSock(size_t iPos)
Delete a sock by position in the vector the socket is deleted, the appropriate call backs are peforme...
EMessages
Definition Csocket.h:1486
int GetErrno() const
return the last known error as set by this class
Definition Csocket.h:1563
Basic socket class.
Definition Csocket.h:564
void DisableSSLProtocols(u_int uDisableOpts)
bitwise setter,
Definition Csocket.h:891
virtual bool Write(const CS_STRING &sData)
Write a text string to the socket.
void SetMaxBufferThreshold(uint32_t iThreshold)
sets the max buffered threshold when EnableReadLine() is enabled
uint16_t GetRemotePort() const
Returns the remote port.
const CS_STRING & GetPemLocation() const
virtual void ReadData(const char *data, size_t len)
Ready to read data event.
Definition Csocket.h:983
int GetType() const
Returns the connection type from enum eConnType.
bool HasWriteBuffer() const
Get the send buffer.
virtual void Dereference()
in the event you pass this class to Copy(), you MUST call this function or on the original Csock othe...
virtual bool ConnectUnix(const CS_STRING &sPath)
Connect to a UNIX socket.
ETConn
Definition Csocket.h:592
CS_STRING GetLocalIP() const
virtual void SetParentSockName(const CS_STRING &sParentName)
Set The INBOUND Parent sockname.
time_t GetLastCheckTimeout() const
Definition Csocket.h:1084
ECONState
Definition Csocket.h:638
uint64_t GetStartTime() const
Gets the starting time of this socket.
virtual void SockError(int iErrno, const CS_STRING &sDescription)
A sock error occured event.
Definition Csocket.h:1004
void SetAFRequire(CSSockAddr::EAFRequire iAFRequire)
Definition Csocket.h:1131
const CS_STRING & GetSockName() const
Returns a reference to the socket name.
bool ConnectInetd(bool bIsSSL=false, const CS_STRING &sHostname="")
Use this to bind this socket to inetd.
Csock & operator<<(std::ostream &(*io)(std::ostream &))
void CloseSocksFD()
puts the socks back to the state they were prior to calling CreateSocksFD
uint32_t GetRequireClientCertFlags() const
double GetAvgWrite(uint64_t iSample=1000) const
Get Avg Write Speed in sample milliseconds (default is 1000 milliseconds or 1 second)
Csock & operator<<(double i)
virtual void IcuExtFromUCallback(UConverterFromUnicodeArgs *fromArgs, const UChar *codeUnits, int32_t length, UChar32 codePoint, UConverterCallbackReason reason, UErrorCode *err)
virtual bool Listen(uint16_t iPort, int iMaxConns=SOMAXCONN, const CS_STRING &sBindHost="", uint32_t iTimeout=0, bool bDetach=false)
Listens for connections.
void SetEncoding(const CS_STRING &sEncoding)
void SetHostName(const CS_STRING &sHostname)
virtual int VerifyPeerCertificate(int iPreVerify, X509_STORE_CTX *pStoreCTX)
this is hooked in via SSL_set_verify, and be default it just returns 1 meaning success
Definition Csocket.h:1077
void PauseRead()
will pause/unpause reading on this socket
void SetSkipConnect(bool b)
Definition Csocket.h:1141
virtual bool CheckTimeout(time_t iNow)
returns true if the socket has timed out
virtual bool ConnectionFrom(const CS_STRING &sHost, uint16_t iPort)
Incoming Connection Event return false and the connection will fail default returns true.
Definition Csocket.h:1010
virtual bool SNIConfigureServer(const CS_STRING &sHostname, CS_STRING &sPemFile, CS_STRING &sPemPass)
gets called when a SNI request is sent, and used to configure a SNI session
Definition Csocket.h:1042
void SetKeyLocation(const CS_STRING &sKeyFile)
const cs_sock_t & GetWSock() const
virtual void ResetTimer()
resets the time counter, this is virtual in the event you need an event on the timer being Reset
EDisableProtocol
Definition Csocket.h:627
double GetAvgRead(uint64_t iSample=1000) const
Get Avg Read Speed in sample milliseconds (default is 1000 milliseconds or 1 second)
bool StartTLS()
start a TLS connection on an existing plain connection
bool IsClosed() const
Definition Csocket.h:880
CS_STRING & GetInternalReadBuffer()
This gives access to the internal read buffer, if your not going to use ReadLine(),...
void SetConState(ECONState eState)
sets the connection state to eState
Definition Csocket.h:1097
void SetIPv6(bool b)
Definition Csocket.h:1124
const CS_STRING & GetDHParamLocation() const
void SetCTXObject(SSL_CTX *sslCtx, bool bDeleteExisting=false)
void SetSockName(const CS_STRING &sName)
virtual void ReachedMaxBuffer()
This WARNING event is called when your buffer for readline exceeds the warning threshold and triggers...
void ResetStartTime()
Resets the start time.
time_t GetTimeSinceLastDataTransaction(time_t iNow=0) const
return how long it has been (in seconds) since the last read or successful write
const CS_STRING & GetParentSockName() const
uint16_t GetPort() const
Returns the port.
void SetWSock(cs_sock_t iSock)
uint16_t GetLocalPort() const
Returns the local port.
X509 * GetX509() const
Get the peer's X509 cert.
void SetSSLObject(SSL *ssl, bool bDeleteExisting=false)
virtual void Copy(const Csock &cCopy)
use this to copy a sock from one to the other, override it if you have special needs in the event of ...
virtual int ConvertAddress(const struct sockaddr_storage *pAddr, socklen_t iAddrLen, CS_STRING &sIP, uint16_t *piPort) const
retrieve name info (numeric only) for a given sockaddr_storage
bool GetSSL() const
Return true if this socket is using ssl. Note this does not mean the SSL state is finished,...
uint32_t GetRateBytes() const
virtual cs_ssize_t Read(char *data, size_t len)
Read from the socket Just pass in a pointer, big enough to hold len bytes.
virtual bool IsConnected() const
Tells you if the socket is connected.
uint64_t GetBytesWritten() const
Gets the amount of data written during the existence of the socket.
void FollowSSLCipherServerPreference()
select the ciphers in server-preferred order
Definition Csocket.h:895
CS_STRING GetPeerPubKey() const
Returns the peer's public key.
const CS_STRING & GetKeyLocation() const
int DNSLookup(EDNSLType eDNSLType)
dns lookup
const CS_STRING & GetPemPass() const
void DisableSSLCompression()
allow disabling compression
Definition Csocket.h:893
CS_STRING & GetInternalWriteBuffer()
This gives access to the internal write buffer.
virtual Csock * GetSockObj(const CS_STRING &sHostname, uint16_t iPort)
override this for accept sockets
virtual void IcuExtToUCallback(UConverterToUnicodeArgs *toArgs, const char *codeUnits, int32_t length, UConverterCallbackReason reason, UErrorCode *err)
Csock & operator<<(uint32_t i)
virtual void ReadLine(const CS_STRING &sLine)
Ready to Read a full line event.
Definition Csocket.h:988
virtual bool AcceptSSL()
Accept an inbound SSL connection, this is used internally and called after Accept.
void CallSockError(int iErrno, const CS_STRING &sDescription="")
calls SockError, if sDescription is not set, then strerror is used to pull out a default description
virtual bool ConnectSSL()
Create the SSL connection.
const CS_STRING & GetCipher() const
bool IsReadPaused() const
bool SetupVHost()
this is only used on outbound connections, listeners bind in a different spot
void SetBindHost(const CS_STRING &sBindHost)
Definition Csocket.h:1106
void SetSSLMethod(int iMethod)
Set the SSL method type.
ECloseType
Definition Csocket.h:649
void SetRSock(cs_sock_t iSock)
void SetTimeout(int iTimeout, uint32_t iTimeoutType=TMO_ALL)
Currently this uses the same value for all timeouts, and iTimeoutType merely states which event will ...
uint32_t GetMaxBufferThreshold() const
long GetPeerFingerprint(CS_STRING &sFP) const
Returns the peer's certificate finger print.
SSL_CTX * SetupServerCTX()
creates a new SSL_CTX based on the setup of this sock
Csock & operator<<(uint64_t i)
virtual void SSLHandShakeFinished()
called once the SSL handshake is complete, this is triggered via SSL_CB_HANDSHAKE_DONE in SSL_set_inf...
Definition Csocket.h:1065
CS_STRING GetEncoding() const
Definition Csocket.h:1176
EDNSLType
Definition Csocket.h:1109
@ DNS_VHOST
this lookup is for the vhost bind
Definition Csocket.h:1110
const cs_sock_t & GetRSock() const
bool AllowWrite(uint64_t &iNOW) const
returns true if this socket can write its data, primarily used with rate shaping, initialize iNOW to ...
EFSelect
Definition Csocket.h:608
bool GetIPv6() const
Definition Csocket.h:1123
void SetCipher(const CS_STRING &sCipher)
Set the cipher type ( openssl cipher [to see ciphers available] )
ESSLMethod
Definition Csocket.h:616
virtual cs_sock_t Accept(CS_STRING &sHost, uint16_t &iRPort)
Accept an inbound connection, this is used internally.
virtual void Listening(const CS_STRING &sBindIP, uint16_t uPort)
called when type is LISTENER and the listening port is up and running
Definition Csocket.h:1017
ECloseType GetCloseType() const
returns int of type to close
Definition Csocket.h:879
SSL_SESSION * GetSSLSession() const
time_t GetNextCheckTimeout(time_t iNow=0) const
Returns the time when CheckTimeout() should be called next.
virtual void SSLFinishSetup(SSL *pSSL)
Gets called immediatly after the m_ssl member is setup and initialized, useful if you need to assign ...
Definition Csocket.h:1034
Csock(int iTimeout=60)
default constructor, sets a timeout of 60 seconds
uint64_t GetBytesRead() const
Gets the amount of data read during the existence of the socket.
Csock & operator<<(float i)
virtual void ReadPaused()
This gets called every iteration of CSocketManager::Select() if the socket is ReadPaused.
Definition Csocket.h:1027
bool HasReadLine() const
returns the value of m_bEnableReadLine, if ReadLine is enabled
Definition Csocket.h:993
virtual bool SNIConfigureClient(CS_STRING &sHostname)
called to configure the SNI client
Csock(const CS_STRING &sHostname, uint16_t uPort, int itimeout=60)
Advanced constructor, for creating a simple connection.
void NonBlockingIO()
Use this to change your fd's to blocking or none blocking.
void SetPort(uint16_t iPort)
virtual void Timeout()
Sock Timed out event.
Definition Csocket.h:979
const CS_STRING & GetBindHost() const
Definition Csocket.h:1105
uint32_t GetTimeoutType() const
virtual bool Connect()
Create the connection, this is used by the socket manager, and shouldn't be called directly by the us...
void SetSock(cs_sock_t iSock)
virtual void ConnectionRefused()
Connection Refused Event.
Definition Csocket.h:1023
void Close(ECloseType eCloseType=CLT_NOW)
just mark us as closed, the parent can pick it up
void SetTimeoutType(uint32_t iTimeoutType)
Csock & operator<<(const CS_STRING &s)
const CS_STRING & GetHostName() const
Returns a reference to the host name.
Csock & operator<<(int64_t i)
void SetRequireClientCertFlags(uint32_t iRequireClientCertFlags)
bitwise flags, 0 means don't require cert, SSL_VERIFY_PEER verifies peers, SSL_VERIFY_FAIL_IF_NO_PEER...
Definition Csocket.h:951
virtual void SetRate(uint32_t iBytes, uint64_t iMilliseconds)
sets the rate at which we can send data
void SetPemLocation(const CS_STRING &sPemFile)
virtual void Connected()
Connected event.
Definition Csocket.h:971
const cs_sock_t & GetSock() const
virtual void SetIsConnected(bool b)
Sets the sock, telling it its connected (internal use only)
cs_sock_t & GetRSock()
returns a reference to the sock
EFRead
Definition Csocket.h:599
virtual int GetAddrInfo(const CS_STRING &sHostname, CSSockAddr &csSockAddr)
override this call with your own DNS lookup method if you have one.
void SetDHParamLocation(const CS_STRING &sDHParamFile)
Set the pem file location.
Csock & operator<<(int32_t i)
virtual int GetPending() const
return the data imediatly ready for read
bool SslIsEstablished() const
is SSL_accept finished ? is the ssl properly finished (from write no error)
virtual void Disconnected()
Disconnected event.
Definition Csocket.h:975
virtual bool Write(const char *data, size_t len)
Write data to the socket.
virtual bool SSLClientSetup()
This sets up the SSL Client, this is used internally.
bool CreateSocksFD()
grabs fd's for the sockets
SSL * GetSSLObject() const
bool ConnectFD(int iReadFD, int iWriteFD, const CS_STRING &sName, bool bIsSSL=false, ETConn eDirection=INBOUND)
Tie this guy to an existing real file descriptor.
CS_STRING GetRemoteIP() const
uint64_t GetRateTime() const
ECONState GetConState() const
returns the current connection state
Definition Csocket.h:1095
virtual bool SSLServerSetup()
This sets up the SSL Server, this is used internally.
virtual void PushBuff(const char *data, size_t len, bool bStartAtZero=false)
pushes data up on the buffer, if a line is ready it calls the ReadLine event
void EnableReadLine()
set the value of m_bEnableReadLine to true, we don't want to store a buffer for ReadLine,...
int GetMaxConns() const
returns the number of max pending connections when type is LISTENER
Definition Csocket.h:1172
void SetRequiresClientCert(bool bRequiresCert)
legacy, deprecated
void SetPemPass(const CS_STRING &sPassword)
void SetCertVerifyCB(FPCertVerifyCB pFP)
setting this to NULL will allow the default openssl verification process kick in
Definition Csocket.h:920
virtual bool ListenUnix(const CS_STRING &sBindFile, int iMaxConns=SOMAXCONN, uint32_t iTimeout=0)
Listens for connections on an UNIX socket.
Ease of use templated socket manager.
Definition Csocket.h:1655
virtual T * GetSockObj(const CS_STRING &sHostname, uint16_t uPort, int iTimeout=60)
Definition Csocket.h:1659
TSocketManager()
Definition Csocket.h:1657
virtual ~TSocketManager()
Definition Csocket.h:1658
#define CS_STRING
Definition include/znc/defines.h:27