ZNC
Versions: latest readthedocs-test
Csocket.h
Go to the documentation of this file.
1 
33 /*
34  * NOTES ...
35  * - You should always compile with -Woverloaded-virtual to detect callbacks that may have been redefined since your last update
36  * - If you want to use gethostbyname instead of getaddrinfo, the use -DUSE_GETHOSTBYNAME when compiling
37  * - To compile with win32 need to link to winsock2, using gcc its -lws2_32
38  * - Code is formated with 'astyle --style=ansi -t4 --unpad-paren --pad-paren-in --keep-one-line-blocks'
39  */
40 #ifndef _HAS_CSOCKET_
41 #define _HAS_CSOCKET_
42 
43 #include "defines.h" // require this as a general rule, most projects have a defines.h or the like
44 
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <sys/time.h>
48 #include <fcntl.h>
49 #include <sys/file.h>
50 
51 #ifndef _WIN32
52 
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <sys/socket.h>
56 #include <sys/ioctl.h>
57 #include <netdb.h>
58 #include <sys/select.h>
59 
60 #else
61 
62 #include <winsock2.h>
63 #include <ws2tcpip.h>
64 #include <sys/timeb.h>
65 
66 #ifndef ECONNREFUSED
67 // these aliases might or might not be defined in errno.h
68 // already, depending on the WinSDK version.
69 #define ECONNREFUSED WSAECONNREFUSED
70 #define EINPROGRESS WSAEINPROGRESS
71 #define ETIMEDOUT WSAETIMEDOUT
72 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
73 #define ECONNABORTED WSAECONNABORTED
74 #define ENETUNREACH WSAENETUNREACH
75 #endif /* ECONNREFUSED */
76 
77 #endif /* _WIN32 */
78 
79 #ifdef HAVE_C_ARES
80 #include <ares.h>
81 #endif /* HAVE_C_ARES */
82 
83 #ifdef HAVE_ICU
84 # include <unicode/ucnv.h>
85 #endif
86 
87 #include <stdlib.h>
88 #include <errno.h>
89 #include <string.h>
90 #include <ctype.h>
91 #include <assert.h>
92 
93 #ifdef HAVE_LIBSSL
94 #include <openssl/ssl.h>
95 #include <openssl/err.h>
96 #include <openssl/rand.h>
97 #endif /* HAVE_LIBSSL */
98 
99 #ifdef __sun
100 #include <strings.h>
101 #include <fcntl.h>
102 #endif /* __sun */
103 
104 #include <vector>
105 #include <list>
106 #include <iostream>
107 #include <sstream>
108 #include <string>
109 #include <set>
110 #include <map>
111 
112 #ifndef CS_STRING
113 # ifdef _HAS_CSTRING_
114 # define CS_STRING Cstring
115 # else
116 # define CS_STRING std::string
117 # endif /* _HAS_CSTRING_ */
118 #endif /* CS_STRING */
119 
120 #ifndef CS_DEBUG
121 #ifdef __DEBUG__
122 # define CS_DEBUG( f ) std::cerr << __FILE__ << ":" << __LINE__ << " " << f << std::endl
123 #else
124 # define CS_DEBUG(f) (void)0
125 #endif /* __DEBUG__ */
126 #endif /* CS_DEBUG */
127 
128 #ifndef CS_EXPORT
129 #define CS_EXPORT
130 #endif /* CS_EXPORT */
131 
132 #ifndef PERROR
133 #ifdef __DEBUG__
134 # define PERROR( f ) __Perror( f, __FILE__, __LINE__ )
135 #else
136 # define PERROR( f ) (void)0
137 #endif /* __DEBUG__ */
138 #endif /* PERROR */
139 
140 #ifdef _WIN32
141 typedef SOCKET cs_sock_t;
142 #ifdef _WIN64
143 typedef signed __int64 cs_ssize_t;
144 #else
145 typedef signed int cs_ssize_t;
146 #endif /* _WIN64 */
147 #define CS_INVALID_SOCK INVALID_SOCKET
148 #else
149 typedef int cs_sock_t;
150 typedef ssize_t cs_ssize_t;
151 #define CS_INVALID_SOCK -1
152 #endif /* _WIN32 */
153 
154 #ifdef CSOCK_USE_POLL
155 #include <poll.h>
156 #endif /* CSOCK_USE_POLL */
157 
158 #ifndef _NO_CSOCKET_NS // some people may not want to use a namespace
159 namespace Csocket
160 {
161 #endif /* _NO_CSOCKET_NS */
162 
163 
169 {
170 public:
171  CSCharBuffer( size_t iSize )
172  {
173  m_pBuffer = ( char * )malloc( iSize );
174  }
176  {
177  free( m_pBuffer );
178  }
179  char * operator()() { return( m_pBuffer ); }
180 
181 private:
182  char * m_pBuffer;
183 };
184 
185 
191 {
192 public:
194  {
195  m_bIsIPv6 = false;
196  memset( ( struct sockaddr_in * ) &m_saddr, '\0', sizeof( m_saddr ) );
197 #ifdef HAVE_IPV6
198  memset( ( struct sockaddr_in6 * ) &m_saddr6, '\0', sizeof( m_saddr6 ) );
199 #endif /* HAVE_IPV6 */
200  m_iAFRequire = RAF_ANY;
201  }
202  virtual ~CSSockAddr() {}
203 
204 
206  {
207  RAF_ANY = PF_UNSPEC,
208 #ifdef HAVE_IPV6
209  RAF_INET6 = AF_INET6,
210 #endif /* HAVE_IPV6 */
211  RAF_INET = AF_INET
212  };
213 
214  void SinFamily();
215  void SinPort( uint16_t iPort );
216  void SetIPv6( bool b );
217  bool GetIPv6() const { return( m_bIsIPv6 ); }
218 
219  socklen_t GetSockAddrLen() { return( sizeof( m_saddr ) ); }
220  sockaddr_in * GetSockAddr() { return( &m_saddr ); }
221  in_addr * GetAddr() { return( &( m_saddr.sin_addr ) ); }
222 #ifdef HAVE_IPV6
223  socklen_t GetSockAddrLen6() { return( sizeof( m_saddr6 ) ); }
224  sockaddr_in6 * GetSockAddr6() { return( &m_saddr6 ); }
225  in6_addr * GetAddr6() { return( &( m_saddr6.sin6_addr ) ); }
226 #endif /* HAVE_IPV6 */
227 
228  void SetAFRequire( EAFRequire iWhich ) { m_iAFRequire = iWhich; }
229  EAFRequire GetAFRequire() const { return( m_iAFRequire ); }
230 
231 private:
232  bool m_bIsIPv6;
233  sockaddr_in m_saddr;
234 #ifdef HAVE_IPV6
235  sockaddr_in6 m_saddr6;
236 #endif /* HAVE_IPV6 */
237  EAFRequire m_iAFRequire;
238 };
239 
240 
241 class Csock;
242 
243 
259 {
260 public:
267  CGetAddrInfo( const CS_STRING & sHostname, Csock * pSock, CSSockAddr & csSockAddr );
268  ~CGetAddrInfo();
269 
271  void Init();
273  int Process();
275  int Finish();
276 
277 private:
278  CS_STRING m_sHostname;
279  Csock * m_pSock;
280  CSSockAddr & m_csSockAddr;
281  struct addrinfo * m_pAddrRes;
282  struct addrinfo m_cHints;
283  int m_iRet;
284 };
285 
287 int GetAddrInfo( const CS_STRING & sHostname, Csock * pSock, CSSockAddr & csSockAddr );
288 
294 int GetCsockSSLIdx();
295 
296 #ifdef HAVE_LIBSSL
297 Csock * GetCsockFromCTX( X509_STORE_CTX * pCTX );
299 #endif /* HAVE_LIBSSL */
300 
301 
302 const uint32_t CS_BLOCKSIZE = 4096;
303 template <class T> inline void CS_Delete( T * & p ) { if( p ) { delete p; p = NULL; } }
304 
305 #ifdef HAVE_LIBSSL
307 {
308  CT_NONE = 0,
310 };
311 
313 void CSAdjustTVTimeout( struct timeval & tv, long iTimeoutMS );
314 
315 void SSLErrors( const char *filename, uint32_t iLineNum );
316 
322 bool InitSSL( ECompType eCompressionType = CT_NONE );
323 
324 #endif /* HAVE_LIBSSL */
325 
329 bool InitCsocket();
333 void ShutdownCsocket();
334 
336 inline int GetSockError()
337 {
338 #ifdef _WIN32
339  return( WSAGetLastError() );
340 #else
341  return( errno );
342 #endif /* _WIN32 */
343 }
344 
346 inline void TFD_ZERO( fd_set *set )
347 {
348  FD_ZERO( set );
349 }
350 
351 inline void TFD_SET( cs_sock_t iSock, fd_set *set )
352 {
353  FD_SET( iSock, set );
354 }
355 
356 inline bool TFD_ISSET( cs_sock_t iSock, fd_set *set )
357 {
358  return( FD_ISSET( iSock, set ) != 0 );
359 }
360 
361 inline void TFD_CLR( cs_sock_t iSock, fd_set *set )
362 {
363  FD_CLR( iSock, set );
364 }
365 
366 void __Perror( const CS_STRING & s, const char * pszFile, uint32_t iLineNo );
367 uint64_t millitime();
368 
369 
378 {
379 public:
380  CCron();
381  virtual ~CCron() {}
382 
384  void run( timeval & tNow );
385 
390  void StartMaxCycles( double dTimeSequence, uint32_t iMaxCycles );
391  void StartMaxCycles( const timeval& tTimeSequence, uint32_t iMaxCycles );
392 
394  void Start( double dTimeSequence );
395  void Start( const timeval& TimeSequence );
396 
398  void Stop();
399 
401  void Pause();
402 
404  void UnPause();
405 
407  void Reset();
408 
409  timeval GetInterval() const;
410  uint32_t GetMaxCycles() const;
411  uint32_t GetCyclesLeft() const;
412 
414  bool isValid() const;
415 
416  const CS_STRING & GetName() const;
417  void SetName( const CS_STRING & sName );
418 
420  timeval GetNextRun() const { return( m_tTime ); }
421 
422 public:
423 
425  virtual void RunJob();
426 
427 protected:
429 
430 private:
431  timeval m_tTime;
432  bool m_bActive, m_bPause;
433  timeval m_tTimeSequence;
434  uint32_t m_iMaxCycles, m_iCycles;
435  CS_STRING m_sName;
436 };
437 
443 {
444 public:
445  CSMonitorFD() { m_bEnabled = true; }
446  virtual ~CSMonitorFD() {}
447 
454  virtual bool GatherFDsForSelect( std::map< cs_sock_t, short > & miiReadyFds, long & iTimeoutMS );
455 
461  virtual bool FDsThatTriggered( const std::map< cs_sock_t, short > & miiReadyFds ) { return( true ); }
462 
468  virtual bool CheckFDs( const std::map< cs_sock_t, short > & miiReadyFds );
469 
475  void Add( cs_sock_t iFD, short iMonitorEvents ) { m_miiMonitorFDs[iFD] = iMonitorEvents; }
477  void Remove( cs_sock_t iFD ) { m_miiMonitorFDs.erase( iFD ); }
479  void DisableMonitor() { m_bEnabled = false; }
480 
481  bool IsEnabled() const { return( m_bEnabled ); }
482 
483 protected:
484  std::map< cs_sock_t, short > m_miiMonitorFDs;
486 };
487 
488 
494 {
495 public:
497  virtual ~CSockCommon();
498 
499  void CleanupCrons();
500  void CleanupFDMonitors();
501 
503  const std::vector<CCron *> & GetCrons() const { return( m_vcCrons ); }
505  virtual void Cron();
506 
508  virtual void AddCron( CCron * pcCron );
515  virtual void DelCron( const CS_STRING & sName, bool bDeleteAll = true, bool bCaseSensitive = true );
517  virtual void DelCron( uint32_t iPos );
519  virtual void DelCronByAddr( CCron * pcCron );
520 
521  void CheckFDs( const std::map< cs_sock_t, short > & miiReadyFds );
522  void AssignFDs( std::map< cs_sock_t, short > & miiReadyFds, struct timeval * tvtimeout );
523 
525  void MonitorFD( CSMonitorFD * pMonitorFD ) { m_vcMonitorFD.push_back( pMonitorFD ); }
526 
527 protected:
528  std::vector<CCron *> m_vcCrons;
529  std::vector<CSMonitorFD *> m_vcMonitorFD;
530 };
531 
532 
533 #ifdef HAVE_LIBSSL
534 typedef int ( *FPCertVerifyCB )( int, X509_STORE_CTX * );
535 #endif /* HAVE_LIBSSL */
536 
537 
549 {
550 public:
552  Csock( int iTimeout = 60 );
559  Csock( const CS_STRING & sHostname, uint16_t uPort, int itimeout = 60 );
560 
562  virtual Csock *GetSockObj( const CS_STRING & sHostname, uint16_t iPort );
563 
564  virtual ~Csock();
565 
572  virtual void Dereference();
574  virtual void Copy( const Csock & cCopy );
575 
576  enum ETConn
577  {
578  OUTBOUND = 0,
579  LISTENER = 1,
580  INBOUND = 2
581  };
582 
583  enum EFRead
584  {
585  READ_EOF = 0,
586  READ_ERR = -1,
587  READ_EAGAIN = -2,
588  READ_CONNREFUSED = -3,
589  READ_TIMEDOUT = -4
590  };
591 
592  enum EFSelect
593  {
594  SEL_OK = 0,
595  SEL_TIMEOUT = -1,
596  SEL_EAGAIN = -2,
597  SEL_ERR = -3
598  };
599 
601  {
602  TLS = 0,
603  SSL23 = TLS,
604  SSL2 = 2,
605  SSL3 = 3,
606  TLS1 = 4,
607  TLS11 = 5,
608  TLS12 = 6
609  };
610 
612  {
613  EDP_None = 0,
614  EDP_SSLv2 = 1,
615  EDP_SSLv3 = 2,
616  EDP_TLSv1 = 4,
617  EDP_TLSv1_1 = 8,
618  EDP_TLSv1_2 = 16,
619  EDP_SSL = (EDP_SSLv2|EDP_SSLv3)
620  };
621 
623  {
624  CST_START = 0,
625  CST_DNS = CST_START,
626  CST_BINDVHOST = 1,
627  CST_DESTDNS = 2,
628  CST_CONNECT = 3,
629  CST_CONNECTSSL = 4,
630  CST_OK = 5
631  };
632 
634  {
635  CLT_DONT = 0,
636  CLT_NOW = 1,
637  CLT_AFTERWRITE = 2,
638  CLT_DEREFERENCE = 3
639  };
640 
641  Csock & operator<<( const CS_STRING & s );
642  Csock & operator<<( std::ostream & ( *io )( std::ostream & ) );
643  Csock & operator<<( int32_t i );
644  Csock & operator<<( uint32_t i );
645  Csock & operator<<( int64_t i );
646  Csock & operator<<( uint64_t i );
647  Csock & operator<<( float i );
648  Csock & operator<<( double i );
649 
654  virtual bool Connect();
655 
664  virtual bool Listen( uint16_t iPort, int iMaxConns = SOMAXCONN, const CS_STRING & sBindHost = "", uint32_t iTimeout = 0, bool bDetach = false );
665 
667  virtual cs_sock_t Accept( CS_STRING & sHost, uint16_t & iRPort );
668 
670  virtual bool AcceptSSL();
671 
673  virtual bool SSLClientSetup();
674 
676  virtual bool SSLServerSetup();
677 
684  virtual bool ConnectSSL();
685 
687  bool StartTLS();
688 
699  virtual bool Write( const char *data, size_t len );
700 
709  virtual bool Write( const CS_STRING & sData );
710 
726  virtual cs_ssize_t Read( char *data, size_t len );
727  CS_STRING GetLocalIP() const;
728  CS_STRING GetRemoteIP() const;
729 
731  virtual bool IsConnected() const;
733  virtual void SetIsConnected( bool b );
734 
736  cs_sock_t & GetRSock();
737  const cs_sock_t & GetRSock() const;
738  void SetRSock( cs_sock_t iSock );
739  cs_sock_t & GetWSock();
740  const cs_sock_t & GetWSock() const;
741  void SetWSock( cs_sock_t iSock );
742 
743  void SetSock( cs_sock_t iSock );
744  cs_sock_t & GetSock();
745  const cs_sock_t & GetSock() const;
746 
752  void CallSockError( int iErrno, const CS_STRING & sDescription = "" );
754  virtual void ResetTimer();
755 
757  void PauseRead();
758  void UnPauseRead();
759  bool IsReadPaused() const;
766  enum
767  {
768  TMO_READ = 1,
769  TMO_WRITE = 2,
770  TMO_ACCEPT = 4,
771  TMO_ALL = TMO_READ|TMO_WRITE|TMO_ACCEPT
772  };
773 
776  void SetTimeout( int iTimeout, uint32_t iTimeoutType = TMO_ALL );
777  void SetTimeoutType( uint32_t iTimeoutType );
778  int GetTimeout() const;
779  uint32_t GetTimeoutType() const;
780 
782  virtual bool CheckTimeout( time_t iNow );
783 
788  virtual void PushBuff( const char *data, size_t len, bool bStartAtZero = false );
789 
793  CS_STRING & GetInternalReadBuffer();
794 
797  CS_STRING & GetInternalWriteBuffer();
798 
800  void SetMaxBufferThreshold( uint32_t iThreshold );
801  uint32_t GetMaxBufferThreshold() const;
802 
804  int GetType() const;
805  void SetType( int iType );
806 
808  const CS_STRING & GetSockName() const;
809  void SetSockName( const CS_STRING & sName );
810 
812  const CS_STRING & GetHostName() const;
813  void SetHostName( const CS_STRING & sHostname );
814 
815 
817  uint64_t GetStartTime() const;
819  void ResetStartTime();
820 
822  uint64_t GetBytesRead() const;
823  void ResetBytesRead();
824 
826  uint64_t GetBytesWritten() const;
827  void ResetBytesWritten();
828 
830  double GetAvgRead( uint64_t iSample = 1000 ) const;
831 
833  double GetAvgWrite( uint64_t iSample = 1000 ) const;
834 
836  uint16_t GetRemotePort() const;
837 
839  uint16_t GetLocalPort() const;
840 
842  uint16_t GetPort() const;
843  void SetPort( uint16_t iPort );
844 
846  void Close( ECloseType eCloseType = CLT_NOW );
848  ECloseType GetCloseType() const { return( m_eCloseType ); }
849  bool IsClosed() const { return( GetCloseType() != CLT_DONT ); }
850 
852  void NonBlockingIO();
853 
855  bool GetSSL() const;
856  void SetSSL( bool b );
857 
858 #ifdef HAVE_LIBSSL
859  void DisableSSLProtocols( u_int uDisableOpts ) { m_uDisableProtocols = uDisableOpts; }
862  void DisableSSLCompression() { m_bNoSSLCompression = true; }
864  void FollowSSLCipherServerPreference() { m_bSSLCipherServerPreference = true; }
866  void SetCipher( const CS_STRING & sCipher );
867  const CS_STRING & GetCipher() const;
868 
870  void SetDHParamLocation( const CS_STRING & sDHParamFile );
871  const CS_STRING & GetDHParamLocation() const;
872  void SetKeyLocation( const CS_STRING & sKeyFile );
873  const CS_STRING & GetKeyLocation() const;
874  void SetPemLocation( const CS_STRING & sPemFile );
875  const CS_STRING & GetPemLocation() const;
876  void SetPemPass( const CS_STRING & sPassword );
877  const CS_STRING & GetPemPass() const;
878 
880  void SetSSLMethod( int iMethod );
881  int GetSSLMethod() const;
882 
883  void SetSSLObject( SSL *ssl, bool bDeleteExisting = false );
884  SSL * GetSSLObject() const;
885  void SetCTXObject( SSL_CTX *sslCtx, bool bDeleteExisting = false );
886  SSL_SESSION * GetSSLSession() const;
887 
889  void SetCertVerifyCB( FPCertVerifyCB pFP ) { m_pCerVerifyCB = pFP; }
890 #endif /* HAVE_LIBSSL */
891 
893  bool HasWriteBuffer() const;
894  void ClearWriteBuffer();
895 
898  bool SslIsEstablished() const;
899 
901  bool ConnectInetd( bool bIsSSL = false, const CS_STRING & sHostname = "" );
902 
904  bool ConnectFD( int iReadFD, int iWriteFD, const CS_STRING & sName, bool bIsSSL = false, ETConn eDirection = INBOUND );
905 
907 #ifdef HAVE_LIBSSL
908  X509 *GetX509() const;
910 
912  CS_STRING GetPeerPubKey() const;
914  long GetPeerFingerprint( CS_STRING & sFP ) const;
915 
916  uint32_t GetRequireClientCertFlags() const;
918  void SetRequiresClientCert( bool bRequiresCert );
920  void SetRequireClientCertFlags( uint32_t iRequireClientCertFlags ) { m_iRequireClientCertFlags = iRequireClientCertFlags; }
921 #endif /* HAVE_LIBSSL */
922 
924  virtual void SetParentSockName( const CS_STRING & sParentName );
925  const CS_STRING & GetParentSockName() const;
926 
932  virtual void SetRate( uint32_t iBytes, uint64_t iMilliseconds );
933 
934  uint32_t GetRateBytes() const;
935  uint64_t GetRateTime() const;
936 
940  virtual void Connected() {}
944  virtual void Disconnected() {}
948  virtual void Timeout() {}
952  virtual void ReadData( const char *data, size_t len ) {}
957  virtual void ReadLine( const CS_STRING & sLine ) {}
959  void EnableReadLine();
960  void DisableReadLine();
962  bool HasReadLine() const { return( m_bEnableReadLine ); }
963 
969  virtual void ReachedMaxBuffer();
973  virtual void SockError( int iErrno, const CS_STRING & sDescription ) {}
979  virtual bool ConnectionFrom( const CS_STRING & sHost, uint16_t iPort ) { return( true ); }
980 
986  virtual void Listening( const CS_STRING & sBindIP, uint16_t uPort ) {}
987 
992  virtual void ConnectionRefused() {}
996  virtual void ReadPaused() {}
997 
998 #ifdef HAVE_LIBSSL
999 
1003  virtual void SSLFinishSetup( SSL * pSSL ) {}
1011  virtual bool SNIConfigureServer( const CS_STRING & sHostname, CS_STRING & sPemFile, CS_STRING & sPemPass ) { return( false ); }
1017  virtual bool SNIConfigureClient( CS_STRING & sHostname );
1019  SSL_CTX * SetupServerCTX();
1020 
1034  virtual void SSLHandShakeFinished() {}
1046  virtual int VerifyPeerCertificate( int iPreVerify, X509_STORE_CTX * pStoreCTX ) { return( 1 ); }
1047 #endif /* HAVE_LIBSSL */
1048 
1049 
1051  time_t GetTimeSinceLastDataTransaction( time_t iNow = 0 ) const;
1052 
1053  time_t GetLastCheckTimeout() const { return( m_iLastCheckTimeoutTime ); }
1054 
1056  time_t GetNextCheckTimeout( time_t iNow = 0 ) const;
1057 
1059  virtual int GetPending() const;
1060 
1062  // Connection State Stuff
1064  ECONState GetConState() const { return( m_eConState ); }
1066  void SetConState( ECONState eState ) { m_eConState = eState; }
1067 
1069  bool CreateSocksFD();
1070 
1072  void CloseSocksFD();
1073 
1074  const CS_STRING & GetBindHost() const { return( m_sBindHost ); }
1075  void SetBindHost( const CS_STRING & sBindHost ) { m_sBindHost = sBindHost; }
1076 
1078  {
1080  DNS_DEST
1081  };
1082 
1087  int DNSLookup( EDNSLType eDNSLType );
1088 
1090  bool SetupVHost();
1091 
1092  bool GetIPv6() const { return( m_bIsIPv6 ); }
1093  void SetIPv6( bool b )
1094  {
1095  m_bIsIPv6 = b;
1096  m_address.SetIPv6( b );
1097  m_bindhost.SetIPv6( b );
1098  }
1099 
1101  {
1102  m_address.SetAFRequire( iAFRequire );
1103  m_bindhost.SetAFRequire( iAFRequire );
1104  }
1105 
1107  bool AllowWrite( uint64_t & iNOW ) const;
1108 
1109 
1110  void SetSkipConnect( bool b ) { m_bSkipConnect = b; }
1111 
1118  virtual int GetAddrInfo( const CS_STRING & sHostname, CSSockAddr & csSockAddr );
1119 
1132  virtual int ConvertAddress( const struct sockaddr_storage * pAddr, socklen_t iAddrLen, CS_STRING & sIP, uint16_t * piPort ) const;
1133 
1134 #ifdef HAVE_C_ARES
1135  CSSockAddr * GetCurrentAddr() const { return( m_pCurrAddr ); }
1136  void SetAresFinished( int status ) { m_pCurrAddr = NULL; m_iARESStatus = status; }
1137  ares_channel GetAresChannel() const { return( m_pARESChannel ); }
1138 #endif /* HAVE_C_ARES */
1139 
1141  int GetMaxConns() const { return( m_iMaxConns ); }
1142 
1143 #ifdef HAVE_ICU
1144  void SetEncoding( const CS_STRING & sEncoding );
1145  CS_STRING GetEncoding() const { return m_sEncoding; }
1146  virtual void IcuExtToUCallback(
1147  UConverterToUnicodeArgs* toArgs,
1148  const char* codeUnits,
1149  int32_t length,
1150  UConverterCallbackReason reason,
1151  UErrorCode* err );
1152  virtual void IcuExtFromUCallback(
1153  UConverterFromUnicodeArgs* fromArgs,
1154  const UChar* codeUnits,
1155  int32_t length,
1156  UChar32 codePoint,
1157  UConverterCallbackReason reason,
1158  UErrorCode* err );
1159 #endif /* HAVE_ICU */
1160 
1161 private:
1163  Csock( const Csock & cCopy ) : CSockCommon() {}
1165  void ShrinkSendBuff();
1166  void IncBuffPos( size_t uBytes );
1168 
1169  // NOTE! if you add any new members, be sure to add them to Copy()
1170  uint16_t m_uPort;
1171  cs_sock_t m_iReadSock, m_iWriteSock;
1172  int m_iTimeout, m_iConnType, m_iMethod, m_iTcount, m_iMaxConns;
1173  bool m_bUseSSL, m_bIsConnected;
1174  bool m_bsslEstablished, m_bEnableReadLine, m_bPauseRead;
1175  CS_STRING m_shostname, m_sbuffer, m_sSockName, m_sDHParamFile, m_sKeyFile, m_sPemFile, m_sCipherType, m_sParentName;
1176  CS_STRING m_sSend, m_sPemPass;
1177  ECloseType m_eCloseType;
1178 
1179  // initialized lazily
1180  mutable uint16_t m_iRemotePort, m_iLocalPort;
1181  mutable CS_STRING m_sLocalIP, m_sRemoteIP;
1182 
1183  uint64_t m_iMaxMilliSeconds, m_iLastSendTime, m_iBytesRead, m_iBytesWritten, m_iStartTime;
1184  uint32_t m_iMaxBytes, m_iMaxStoredBufferLength, m_iTimeoutType;
1185  size_t m_iLastSend, m_uSendBufferPos;
1186 
1187  CSSockAddr m_address, m_bindhost;
1188  bool m_bIsIPv6, m_bSkipConnect;
1189  time_t m_iLastCheckTimeoutTime;
1190 
1191 #ifdef HAVE_LIBSSL
1192  CS_STRING m_sSSLBuffer;
1193  SSL * m_ssl;
1194  SSL_CTX * m_ssl_ctx;
1195  uint32_t m_iRequireClientCertFlags;
1196  u_int m_uDisableProtocols;
1197  bool m_bNoSSLCompression;
1198  bool m_bSSLCipherServerPreference;
1199 
1200  FPCertVerifyCB m_pCerVerifyCB;
1201 
1202  void FREE_SSL();
1203  void FREE_CTX();
1204  bool ConfigureCTXOptions( SSL_CTX * pCTX );
1205 
1206 #endif /* HAVE_LIBSSL */
1207 
1209  cs_sock_t CreateSocket( bool bListen = false );
1210  void Init( const CS_STRING & sHostname, uint16_t uPort, int iTimeout = 60 );
1211 
1212  // Connection State Info
1213  ECONState m_eConState;
1214  CS_STRING m_sBindHost;
1215  uint32_t m_iCurBindCount, m_iDNSTryCount;
1216 #ifdef HAVE_C_ARES
1217  void FreeAres();
1218  ares_channel m_pARESChannel;
1219  CSSockAddr * m_pCurrAddr;
1220  int m_iARESStatus;
1221 #endif /* HAVE_C_ARES */
1222 
1223 #ifdef HAVE_ICU
1224  UConverter* m_cnvInt;
1225  UConverter* m_cnvIntStrict;
1226  UConverter* m_cnvExt;
1227  bool m_cnvTryUTF8;
1228  bool m_cnvSendUTF8;
1229  CS_STRING m_sEncoding;
1230 #endif
1231 };
1232 
1238 {
1239 public:
1245  CSConnection( const CS_STRING & sHostname, uint16_t iPort, int iTimeout = 60 )
1246  {
1247  m_sHostname = sHostname;
1248  m_iPort = iPort;
1249  m_iTimeout = iTimeout;
1250  m_bIsSSL = false;
1251 #ifdef HAVE_LIBSSL
1252  m_sCipher = "HIGH";
1253 #endif /* HAVE_LIBSSL */
1254  m_iAFrequire = CSSockAddr::RAF_ANY;
1255  }
1256  virtual ~CSConnection() {}
1257 
1258  const CS_STRING & GetHostname() const { return( m_sHostname ); }
1259  const CS_STRING & GetSockName() const { return( m_sSockName ); }
1260  const CS_STRING & GetBindHost() const { return( m_sBindHost ); }
1261  uint16_t GetPort() const { return( m_iPort ); }
1262  int GetTimeout() const { return( m_iTimeout ); }
1263  bool GetIsSSL() const { return( m_bIsSSL ); }
1264  CSSockAddr::EAFRequire GetAFRequire() const { return( m_iAFrequire ); }
1265 
1266 #ifdef HAVE_LIBSSL
1267  const CS_STRING & GetCipher() const { return( m_sCipher ); }
1268  const CS_STRING & GetPemLocation() const { return( m_sPemLocation ); }
1269  const CS_STRING & GetKeyLocation() const { return( m_sKeyLocation ); }
1270  const CS_STRING & GetDHParamLocation() const { return( m_sDHParamLocation ); }
1271  const CS_STRING & GetPemPass() const { return( m_sPemPass ); }
1272 #endif /* HAVE_LIBSSL */
1273 
1275  void SetHostname( const CS_STRING & s ) { m_sHostname = s; }
1277  void SetSockName( const CS_STRING & s ) { m_sSockName = s; }
1279  void SetBindHost( const CS_STRING & s ) { m_sBindHost = s; }
1281  void SetPort( uint16_t i ) { m_iPort = i; }
1283  void SetTimeout( int i ) { m_iTimeout = i; }
1285  void SetIsSSL( bool b ) { m_bIsSSL = b; }
1287  void SetAFRequire( CSSockAddr::EAFRequire iAFRequire ) { m_iAFrequire = iAFRequire; }
1288 
1289 #ifdef HAVE_LIBSSL
1290  void SetCipher( const CS_STRING & s ) { m_sCipher = s; }
1293  void SetPemLocation( const CS_STRING & s ) { m_sPemLocation = s; }
1295  void SetPemPass( const CS_STRING & s ) { m_sPemPass = s; }
1296 #endif /* HAVE_LIBSSL */
1297 
1298 protected:
1299  CS_STRING m_sHostname, m_sSockName, m_sBindHost;
1300  uint16_t m_iPort;
1302  bool m_bIsSSL;
1304 #ifdef HAVE_LIBSSL
1305  CS_STRING m_sDHParamLocation, m_sKeyLocation, m_sPemLocation, m_sPemPass, m_sCipher;
1306 #endif /* HAVE_LIBSSL */
1307 };
1308 
1310 {
1311 public:
1312  CSSSLConnection( const CS_STRING & sHostname, uint16_t iPort, int iTimeout = 60 ) :
1313  CSConnection( sHostname, iPort, iTimeout )
1314  {
1315  SetIsSSL( true );
1316  }
1317 };
1318 
1319 
1325 {
1326 public:
1332  CSListener( uint16_t iPort, const CS_STRING & sBindHost = "", bool bDetach = false )
1333  {
1334  m_iPort = iPort;
1335  m_sBindHost = sBindHost;
1336  m_bIsSSL = false;
1337  m_iMaxConns = SOMAXCONN;
1338  m_iTimeout = 0;
1339  m_iAFrequire = CSSockAddr::RAF_ANY;
1340  m_bDetach = bDetach;
1341 #ifdef HAVE_LIBSSL
1342  m_sCipher = "HIGH";
1343  m_iRequireCertFlags = 0;
1344 #endif /* HAVE_LIBSSL */
1345  }
1346  virtual ~CSListener() {}
1347 
1348  void SetDetach( bool b ) { m_bDetach = b; }
1349  bool GetDetach() const { return( m_bDetach ); }
1350  uint16_t GetPort() const { return( m_iPort ); }
1351  const CS_STRING & GetSockName() const { return( m_sSockName ); }
1352  const CS_STRING & GetBindHost() const { return( m_sBindHost ); }
1353  bool GetIsSSL() const { return( m_bIsSSL ); }
1354  int GetMaxConns() const { return( m_iMaxConns ); }
1355  uint32_t GetTimeout() const { return( m_iTimeout ); }
1356  CSSockAddr::EAFRequire GetAFRequire() const { return( m_iAFrequire ); }
1357 #ifdef HAVE_LIBSSL
1358  const CS_STRING & GetCipher() const { return( m_sCipher ); }
1359  const CS_STRING & GetDHParamLocation() const { return( m_sDHParamLocation ); }
1360  const CS_STRING & GetKeyLocation() const { return( m_sKeyLocation ); }
1361  const CS_STRING & GetPemLocation() const { return( m_sPemLocation ); }
1362  const CS_STRING & GetPemPass() const { return( m_sPemPass ); }
1363  uint32_t GetRequireClientCertFlags() const { return( m_iRequireCertFlags ); }
1364 #endif /* HAVE_LIBSSL */
1365 
1367  void SetPort( uint16_t iPort ) { m_iPort = iPort; }
1369  void SetSockName( const CS_STRING & sSockName ) { m_sSockName = sSockName; }
1371  void SetBindHost( const CS_STRING & sBindHost ) { m_sBindHost = sBindHost; }
1373  void SetIsSSL( bool b ) { m_bIsSSL = b; }
1375  void SetMaxConns( int i ) { m_iMaxConns = i; }
1377  void SetTimeout( uint32_t i ) { m_iTimeout = i; }
1379  void SetAFRequire( CSSockAddr::EAFRequire iAFRequire ) { m_iAFrequire = iAFRequire; }
1380 
1381 #ifdef HAVE_LIBSSL
1382  void SetCipher( const CS_STRING & s ) { m_sCipher = s; }
1385  void SetPemLocation( const CS_STRING & s ) { m_sPemLocation = s; }
1387  void SetKeyLocation( const CS_STRING & s ) { m_sKeyLocation = s; }
1389  void SetDHParamLocation( const CS_STRING & s ) { m_sDHParamLocation = s; }
1391  void SetPemPass( const CS_STRING & s ) { m_sPemPass = s; }
1393  void SetRequiresClientCert( bool b ) { m_iRequireCertFlags = ( b ? SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT : 0 ); }
1395  void SetRequireClientCertFlags( unsigned int iRequireCertFlags ) { m_iRequireCertFlags = iRequireCertFlags; }
1396 #endif /* HAVE_LIBSSL */
1397 private:
1398  uint16_t m_iPort;
1399  CS_STRING m_sSockName, m_sBindHost;
1400  bool m_bIsSSL;
1401  bool m_bDetach;
1402  int m_iMaxConns;
1403  uint32_t m_iTimeout;
1404  CSSockAddr::EAFRequire m_iAFrequire;
1405 
1406 #ifdef HAVE_LIBSSL
1407  CS_STRING m_sDHParamLocation, m_sKeyLocation, m_sPemLocation, m_sPemPass, m_sCipher;
1408  uint32_t m_iRequireCertFlags;
1409 #endif /* HAVE_LIBSSL */
1410 };
1411 
1412 #ifdef HAVE_LIBSSL
1413 class CSSSListener : public CSListener
1414 {
1415 public:
1416  CSSSListener( uint16_t iPort, const CS_STRING & sBindHost = "" ) :
1417  CSListener( iPort, sBindHost )
1418  {
1419  SetIsSSL( true );
1420  }
1421 };
1422 #endif /* HAVE_LIBSSL */
1423 
1445 class CS_EXPORT CSocketManager : public std::vector<Csock *>, public CSockCommon
1446 {
1447 public:
1448  CSocketManager();
1449  virtual ~CSocketManager();
1450  virtual void clear();
1451  virtual void Cleanup();
1452 
1453  virtual Csock * GetSockObj( const CS_STRING & sHostname, uint16_t uPort, int iTimeout = 60 );
1454 
1456  {
1457  SUCCESS = 0,
1458  SELECT_ERROR = -1,
1459  SELECT_TIMEOUT = -2,
1460  SELECT_TRYAGAIN = -3
1461  };
1462 
1468  void Connect( const CSConnection & cCon, Csock * pcSock = NULL );
1469 
1479  virtual bool Listen( const CSListener & cListen, Csock * pcSock = NULL, uint16_t * piRandPort = NULL );
1480 
1481 
1483  bool HasFDs() const;
1484 
1490  virtual void Loop();
1491 
1507  void DynamicSelectLoop( uint64_t iLowerBounds, uint64_t iUpperBounds, time_t iMaxResolution = 3600 );
1508 
1513  virtual void AddSock( Csock * pcSock, const CS_STRING & sSockName );
1514 
1516  virtual Csock * FindSockByRemotePort( uint16_t iPort );
1517 
1519  virtual Csock * FindSockByLocalPort( uint16_t iPort );
1520 
1522  virtual Csock * FindSockByName( const CS_STRING & sName );
1523 
1525  virtual Csock * FindSockByFD( cs_sock_t iFD );
1526 
1527  virtual std::vector<Csock *> FindSocksByName( const CS_STRING & sName );
1528 
1530  virtual std::vector<Csock *> FindSocksByRemoteHost( const CS_STRING & sHostname );
1531 
1533  int GetErrno() const { return( m_errno ); }
1534 
1536  uint64_t GetSelectTimeout() const { return( m_iSelectWait ); }
1539  void SetSelectTimeout( uint64_t iTimeout ) { m_iSelectWait = iTimeout; }
1540 
1545  virtual void DelSockByAddr( Csock * pcSock );
1546 
1552  virtual void DelSock( size_t iPos );
1553 
1560  virtual bool SwapSockByIdx( Csock * pNewSock, size_t iOrginalSockIdx );
1561 
1568  virtual bool SwapSockByAddr( Csock * pNewSock, Csock * pOrigSock );
1569 
1571  uint64_t GetBytesRead() const;
1572 
1574  uint64_t GetBytesWritten() const;
1575 
1578  {
1579  ECT_Read = 1,
1580  ECT_Write = 2
1581  };
1582 
1583  void FDSetCheck( cs_sock_t iFd, std::map< cs_sock_t, short > & miiReadyFds, ECheckType eType );
1584  bool FDHasCheck( cs_sock_t iFd, std::map< cs_sock_t, short > & miiReadyFds, ECheckType eType );
1585 
1586 protected:
1587 
1588  virtual int Select( std::map< cs_sock_t, short > & miiReadyFds, struct timeval *tvtimeout );
1589 
1590 private:
1597  void Select( std::map<Csock *, EMessages> & mpeSocks );
1598 
1599  timeval GetDynamicSleepTime( const timeval& tNow, const timeval& tMaxResolution ) const;
1600 
1602  virtual void SelectSock( std::map<Csock *, EMessages> & mpeSocks, EMessages eErrno, Csock * pcSock );
1603 
1605  // Connection State Functions
1606 
1608  // members
1609  EMessages m_errno;
1610  uint64_t m_iCallTimeouts;
1611  uint64_t m_iBytesRead;
1612  uint64_t m_iBytesWritten;
1613  uint64_t m_iSelectWait;
1614 };
1615 
1616 
1623 template<class T>
1625 {
1626 public:
1628  virtual ~TSocketManager() {}
1629  virtual T * GetSockObj( const CS_STRING & sHostname, uint16_t uPort, int iTimeout = 60 )
1630  {
1631  return( new T( sHostname, uPort, iTimeout ) );
1632  }
1633 };
1634 
1635 #ifndef _NO_CSOCKET_NS
1636 }
1637 #endif /* _NO_CSOCKET_NS */
1638 
1639 #endif /* _HAS_CSOCKET_ */
1640 
void CSAdjustTVTimeout(struct timeval &tv, long iTimeoutMS)
adjusts tv with a new timeout if iTimeoutMS is smaller
const CS_STRING & GetDHParamLocation() const
Definition: Csocket.h:1270
void SetRequireClientCertFlags(unsigned int iRequireCertFlags)
bitwise flags, 0 means don&#39;t require cert, SSL_VERIFY_PEER verifies peers, SSL_VERIFY_FAIL_IF_NO_PEER...
Definition: Csocket.h:1395
void SSLErrors(const char *filename, uint32_t iLineNum)
const CS_STRING & GetBindHost() const
Definition: Csocket.h:1260
ECloseType GetCloseType() const
returns int of type to close
Definition: Csocket.h:848
virtual ~CCron()
Definition: Csocket.h:381
simple class to share common code to both TSockManager and Csock
Definition: Csocket.h:493
void MonitorFD(CSMonitorFD *pMonitorFD)
add an FD set to monitor
Definition: Csocket.h:525
ssize_t cs_ssize_t
Definition: Csocket.h:150
Ease of use templated socket manager.
Definition: Csocket.h:1624
#define CS_EXPORT
Definition: Csocket.h:129
Definition: Csocket.h:207
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:1034
uint64_t millitime()
virtual void ReadPaused()
This gets called every iteration of CSocketManager::Select() if the socket is ReadPaused.
Definition: Csocket.h:996
void SetIPv6(bool b)
Definition: Csocket.h:1093
void SetBindHost(const CS_STRING &s)
sets the hostname to bind to (vhost support)
Definition: Csocket.h:1279
void SetBindHost(const CS_STRING &sBindHost)
sets the host to bind to
Definition: Csocket.h:1371
Definition: Csocket.h:1309
CSCharBuffer(size_t iSize)
Definition: Csocket.h:171
Csock * GetCsockFromCTX(X509_STORE_CTX *pCTX)
returns the sock object associated to the particular context. returns NULL on failure or if not avail...
const CS_STRING & GetBindHost() const
Definition: Csocket.h:1074
void ShutdownCsocket()
Shutdown and release global allocated memory.
virtual void SockError(int iErrno, const CS_STRING &sDescription)
A sock error occured event.
Definition: Csocket.h:973
const CS_STRING & GetSockName() const
Definition: Csocket.h:1351
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:1377
this function is a wrapper around getaddrinfo (for ipv6)
Definition: Csocket.h:258
CS_STRING GetEncoding() const
Definition: Csocket.h:1145
CS_STRING m_sSockName
Definition: Csocket.h:1299
const CS_STRING & GetDHParamLocation() const
Definition: Csocket.h:1359
ESSLMethod
Definition: Csocket.h:600
virtual T * GetSockObj(const CS_STRING &sHostname, uint16_t uPort, int iTimeout=60)
Definition: Csocket.h:1629
void Add(cs_sock_t iFD, short iMonitorEvents)
adds a file descriptor to be monitored
Definition: Csocket.h:475
int(* FPCertVerifyCB)(int, X509_STORE_CTX *)
Definition: Csocket.h:534
bool GetIPv6() const
Definition: Csocket.h:1092
void SetConState(ECONState eState)
sets the connection state to eState
Definition: Csocket.h:1066
EAFRequire GetAFRequire() const
Definition: Csocket.h:229
bool TFD_ISSET(cs_sock_t iSock, fd_set *set)
Definition: Csocket.h:356
void SetAFRequire(EAFRequire iWhich)
Definition: Csocket.h:228
virtual ~CSSockAddr()
Definition: Csocket.h:202
CSSockAddr::EAFRequire GetAFRequire() const
Definition: Csocket.h:1356
void Remove(cs_sock_t iFD)
removes this fd from monitoring
Definition: Csocket.h:477
time_t GetLastCheckTimeout() const
Definition: Csocket.h:1053
const CS_STRING & GetPemLocation() const
Definition: Csocket.h:1268
ETConn
Definition: Csocket.h:576
bool m_bEnabled
Definition: Csocket.h:485
bool m_bRunOnNextCall
if set to true, RunJob() gets called on next invocation of run() despite the timeout ...
Definition: Csocket.h:428
void SetTimeout(int i)
sets the connection timeout
Definition: Csocket.h:1283
uint16_t GetPort() const
Definition: Csocket.h:1350
Definition: Csocket.h:1413
virtual bool FDsThatTriggered(const std::map< cs_sock_t, short > &miiReadyFds)
called when there are fd&#39;s belonging to this class that have triggered
Definition: Csocket.h:461
char * operator()()
Definition: Csocket.h:179
EFRead
Definition: Csocket.h:583
Ease of use self deleting char * class.
Definition: Csocket.h:168
EFSelect
Definition: Csocket.h:592
virtual void Connected()
Connected event.
Definition: Csocket.h:940
ECONState
Definition: Csocket.h:622
uint64_t GetSelectTimeout() const
Get the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond )
Definition: Csocket.h:1536
const CS_STRING & GetCipher() const
Definition: Csocket.h:1267
uint32_t GetTimeout() const
Definition: Csocket.h:1355
virtual ~CSMonitorFD()
Definition: Csocket.h:446
Definition: Csocket.h:309
void SetDetach(bool b)
Definition: Csocket.h:1348
CSSockAddr::EAFRequire m_iAFrequire
Definition: Csocket.h:1303
void SetPemLocation(const CS_STRING &s)
set the location of the pemfile
Definition: Csocket.h:1385
this lookup is for the vhost bind
Definition: Csocket.h:1079
void SetSockName(const CS_STRING &s)
sets the name of the socket, used for reference, ie in FindSockByName()
Definition: Csocket.h:1277
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:1046
void CS_Delete(T *&p)
Definition: Csocket.h:303
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:1003
const CS_STRING & GetHostname() const
Definition: Csocket.h:1258
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:1539
bool GetIPv6() const
Definition: Csocket.h:217
int GetMaxConns() const
Definition: Csocket.h:1354
ECloseType
Definition: Csocket.h:633
in_addr * GetAddr()
Definition: Csocket.h:221
const CS_STRING & GetBindHost() const
Definition: Csocket.h:1352
options for creating a connection
Definition: Csocket.h:1237
void SetAFRequire(CSSockAddr::EAFRequire iAFRequire)
sets the AF family type required
Definition: Csocket.h:1287
CSSockAddr::EAFRequire GetAFRequire() const
Definition: Csocket.h:1264
Basic socket class.
Definition: Csocket.h:548
void SetAFRequire(CSSockAddr::EAFRequire iAFRequire)
sets the AF family type required
Definition: Csocket.h:1379
void DisableSSLCompression()
allow disabling compression
Definition: Csocket.h:862
const CS_STRING & GetPemPass() const
Definition: Csocket.h:1362
virtual void Timeout()
Sock Timed out event.
Definition: Csocket.h:948
virtual ~CSListener()
Definition: Csocket.h:1346
bool m_bIsSSL
Definition: Csocket.h:1302
const CS_STRING & GetCipher() const
Definition: Csocket.h:1358
int GetErrno() const
return the last known error as set by this class
Definition: Csocket.h:1533
ECompType
Definition: Csocket.h:306
#define CS_STRING
Definition: Csocket.h:116
socklen_t GetSockAddrLen6()
Definition: Csocket.h:223
void SetPort(uint16_t iPort)
sets the port to listen on. Set to 0 to listen on a random port
Definition: Csocket.h:1367
void SetMaxConns(int i)
set max connections as called by accept()
Definition: Csocket.h:1375
virtual ~CSConnection()
Definition: Csocket.h:1256
CSMonitorFD()
Definition: Csocket.h:445
uint16_t GetPort() const
Definition: Csocket.h:1261
CSConnection(const CS_STRING &sHostname, uint16_t iPort, int iTimeout=60)
Definition: Csocket.h:1245
uint16_t m_iPort
Definition: Csocket.h:1300
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:420
void SetPemPass(const CS_STRING &s)
set the pemfile pass
Definition: Csocket.h:1295
int GetTimeout() const
Definition: Csocket.h:1262
virtual ~TSocketManager()
Definition: Csocket.h:1628
int cs_sock_t
Definition: Csocket.h:149
const uint32_t CS_BLOCKSIZE
Definition: Csocket.h:302
void SetKeyLocation(const CS_STRING &s)
set the location of the keyfile
Definition: Csocket.h:1387
options container to create a listener
Definition: Csocket.h:1324
bool InitCsocket()
This does all the csocket initialized inclusing InitSSL() and win32 specific initializations, only needs to be called once.
CSSSListener(uint16_t iPort, const CS_STRING &sBindHost="")
Definition: Csocket.h:1416
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:979
const CS_STRING & GetPemLocation() const
Definition: Csocket.h:1361
EDisableProtocol
Definition: Csocket.h:611
void SetSkipConnect(bool b)
Definition: Csocket.h:1110
std::vector< CSMonitorFD * > m_vcMonitorFD
Definition: Csocket.h:529
bool IsClosed() const
Definition: Csocket.h:849
const CS_STRING & GetKeyLocation() const
Definition: Csocket.h:1269
const CS_STRING & GetKeyLocation() const
Definition: Csocket.h:1360
this is the main cron job class
Definition: Csocket.h:377
bool IsEnabled() const
Definition: Csocket.h:481
ECONState GetConState() const
returns the current connection state
Definition: Csocket.h:1064
void SetHostname(const CS_STRING &s)
sets the hostname to connect to
Definition: Csocket.h:1275
void SetPemPass(const CS_STRING &s)
set the pemfile pass
Definition: Csocket.h:1391
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:986
void SetSockName(const CS_STRING &sSockName)
sets the sock name for later reference (ie FindSockByName)
Definition: Csocket.h:1369
virtual void ReadData(const char *data, size_t len)
Ready to read data event.
Definition: Csocket.h:952
socklen_t GetSockAddrLen()
Definition: Csocket.h:219
virtual void ReadLine(const CS_STRING &sLine)
Ready to Read a full line event.
Definition: Csocket.h:957
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:1011
virtual void Disconnected()
Disconnected event.
Definition: Csocket.h:944
sockaddr_in6 * GetSockAddr6()
Definition: Csocket.h:224
void SetPort(uint16_t i)
sets the port to connect to
Definition: Csocket.h:1281
sockaddr wrapper.
Definition: Csocket.h:190
void SetAFRequire(CSSockAddr::EAFRequire iAFRequire)
Definition: Csocket.h:1100
void __Perror(const CS_STRING &s, const char *pszFile, uint32_t iLineNo)
CSSSLConnection(const CS_STRING &sHostname, uint16_t iPort, int iTimeout=60)
Definition: Csocket.h:1312
bool HasReadLine() const
returns the value of m_bEnableReadLine, if ReadLine is enabled
Definition: Csocket.h:962
int GetAddrInfo(const CS_STRING &sHostname, Csock *pSock, CSSockAddr &csSockAddr)
backwards compatible wrapper around CGetAddrInfo and gethostbyname
CSockCommon()
Definition: Csocket.h:496
CSSockAddr()
Definition: Csocket.h:193
EDNSLType
Definition: Csocket.h:1077
const CS_STRING & GetPemPass() const
Definition: Csocket.h:1271
TSocketManager()
Definition: Csocket.h:1627
CSListener(uint16_t iPort, const CS_STRING &sBindHost="", bool bDetach=false)
Definition: Csocket.h:1332
void TFD_ZERO(fd_set *set)
wrappers for FD_SET and such to work in templates.
Definition: Csocket.h:346
bool GetIsSSL() const
Definition: Csocket.h:1263
bool GetDetach() const
Definition: Csocket.h:1349
EAFRequire
Definition: Csocket.h:205
void SetDHParamLocation(const CS_STRING &s)
set the location of the dhparamfile
Definition: Csocket.h:1389
void SetBindHost(const CS_STRING &sBindHost)
Definition: Csocket.h:1075
void SetRequireClientCertFlags(uint32_t iRequireClientCertFlags)
bitwise flags, 0 means don&#39;t require cert, SSL_VERIFY_PEER verifies peers, SSL_VERIFY_FAIL_IF_NO_PEER...
Definition: Csocket.h:920
void FollowSSLCipherServerPreference()
select the ciphers in server-preferred order
Definition: Csocket.h:864
void SetCertVerifyCB(FPCertVerifyCB pFP)
setting this to NULL will allow the default openssl verification process kick in
Definition: Csocket.h:889
int m_iTimeout
Definition: Csocket.h:1301
const CS_STRING & GetSockName() const
Definition: Csocket.h:1259
in6_addr * GetAddr6()
Definition: Csocket.h:225
void SetIsSSL(bool b)
set to true to enable SSL
Definition: Csocket.h:1285
void TFD_SET(cs_sock_t iSock, fd_set *set)
Definition: Csocket.h:351
virtual void ConnectionRefused()
Connection Refused Event.
Definition: Csocket.h:992
Best class to use to interact with the sockets.
Definition: Csocket.h:1445
bool GetIsSSL() const
Definition: Csocket.h:1353
void DisableMonitor()
causes this monitor to be removed
Definition: Csocket.h:479
int GetMaxConns() const
returns the number of max pending connections when type is LISTENER
Definition: Csocket.h:1141
int GetCsockSSLIdx()
This returns the [ex_]data index position for SSL objects only.
std::map< cs_sock_t, short > m_miiMonitorFDs
Definition: Csocket.h:484
void SetPemLocation(const CS_STRING &s)
set the location of the pemfile
Definition: Csocket.h:1293
ECheckType
this is a strict wrapper around C-api select(). Added in the event you need to do special work here ...
Definition: Csocket.h:1577
CS_STRING m_sPemPass
Definition: Csocket.h:1305
std::vector< CCron * > m_vcCrons
Definition: Csocket.h:528
int GetSockError()
Definition: Csocket.h:336
Class to tie sockets to for monitoring by Csocket at either the Csock or TSockManager.
Definition: Csocket.h:442
EMessages
Definition: Csocket.h:1455
Definition: Csocket.h:308
~CSCharBuffer()
Definition: Csocket.h:175
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...
void TFD_CLR(cs_sock_t iSock, fd_set *set)
Definition: Csocket.h:361
void SetRequiresClientCert(bool b)
set to true if require a client certificate (deprecated
Definition: Csocket.h:1393
void SetIsSSL(bool b)
set to true to enable SSL
Definition: Csocket.h:1373
uint32_t GetRequireClientCertFlags() const
Definition: Csocket.h:1363
sockaddr_in * GetSockAddr()
Definition: Csocket.h:220
const std::vector< CCron * > & GetCrons() const
returns a const reference to the crons associated to this socket
Definition: Csocket.h:503