ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <fcntl.h>
47 
48 #ifndef _WIN32
49 
50 #include <unistd.h>
51 #include <sys/time.h>
52 #include <sys/file.h>
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 <io.h>
63 #include <winsock2.h>
64 #include <ws2tcpip.h>
65 #include <time.h>
66 #include <sys/timeb.h>
67 
68 #ifdef _MSC_VER
69 #define strcasecmp _stricmp
70 #define suseconds_t long
71 #endif
72 
73 #ifndef ECONNREFUSED
74 // these aliases might or might not be defined in errno.h
75 // already, depending on the WinSDK version.
76 #define ECONNREFUSED WSAECONNREFUSED
77 #define EINPROGRESS WSAEINPROGRESS
78 #define ETIMEDOUT WSAETIMEDOUT
79 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
80 #define ECONNABORTED WSAECONNABORTED
81 #define ENETUNREACH WSAENETUNREACH
82 #endif /* ECONNREFUSED */
83 
84 #endif /* _WIN32 */
85 
86 #ifdef HAVE_C_ARES
87 #include <ares.h>
88 #endif /* HAVE_C_ARES */
89 
90 #ifdef HAVE_ICU
91 # include <unicode/ucnv.h>
92 #endif
93 
94 #include <stdlib.h>
95 #include <errno.h>
96 #include <string.h>
97 #include <ctype.h>
98 #include <assert.h>
99 
100 #ifdef HAVE_LIBSSL
101 #include <openssl/ssl.h>
102 #include <openssl/err.h>
103 #include <openssl/rand.h>
104 #endif /* HAVE_LIBSSL */
105 
106 #ifdef __sun
107 #include <strings.h>
108 #include <fcntl.h>
109 #endif /* __sun */
110 
111 #include <vector>
112 #include <list>
113 #include <iostream>
114 #include <sstream>
115 #include <string>
116 #include <set>
117 #include <map>
118 
119 #ifndef CS_STRING
120 # ifdef _HAS_CSTRING_
121 # define CS_STRING Cstring
122 # else
123 # define CS_STRING std::string
124 # endif /* _HAS_CSTRING_ */
125 #endif /* CS_STRING */
126 
127 #ifndef CS_DEBUG
128 #ifdef __DEBUG__
129 # define CS_DEBUG( f ) std::cerr << __FILE__ << ":" << __LINE__ << " " << f << std::endl
130 #else
131 # define CS_DEBUG(f) (void)0
132 #endif /* __DEBUG__ */
133 #endif /* CS_DEBUG */
134 
135 #ifndef CS_EXPORT
136 #define CS_EXPORT
137 #endif /* CS_EXPORT */
138 
139 #ifndef PERROR
140 #ifdef __DEBUG__
141 # define PERROR( f ) __Perror( f, __FILE__, __LINE__ )
142 #else
143 # define PERROR( f ) (void)0
144 #endif /* __DEBUG__ */
145 #endif /* PERROR */
146 
147 #ifdef _WIN32
148 typedef SOCKET cs_sock_t;
149 #ifdef _WIN64
150 typedef signed __int64 cs_ssize_t;
151 #else
152 typedef signed int cs_ssize_t;
153 #endif /* _WIN64 */
154 #define CS_INVALID_SOCK INVALID_SOCKET
155 #else
156 typedef int cs_sock_t;
157 typedef ssize_t cs_ssize_t;
158 #define CS_INVALID_SOCK -1
159 #endif /* _WIN32 */
160 
161 /* Assume that everything but windows has Unix sockets */
162 #ifndef _WIN32
163 #define HAVE_UNIX_SOCKET
164 #endif
165 
166 #ifdef CSOCK_USE_POLL
167 #include <poll.h>
168 #endif /* CSOCK_USE_POLL */
169 
170 #ifdef HAVE_UNIX_SOCKET
171 #include <sys/un.h>
172 #endif
173 
174 #ifndef _NO_CSOCKET_NS // some people may not want to use a namespace
175 namespace Csocket
176 {
177 #endif /* _NO_CSOCKET_NS */
178 
179 
185 {
186 public:
187  CSCharBuffer( size_t iSize )
188  {
189  m_pBuffer = ( char * )malloc( iSize );
190  }
192  {
193  free( m_pBuffer );
194  }
195  char * operator()() { return( m_pBuffer ); }
196 
197 private:
198  char * m_pBuffer;
199 };
200 
201 
207 {
208 public:
210  {
211  m_bIsIPv6 = false;
212  memset( ( struct sockaddr_in * ) &m_saddr, '\0', sizeof( m_saddr ) );
213 #ifdef HAVE_IPV6
214  memset( ( struct sockaddr_in6 * ) &m_saddr6, '\0', sizeof( m_saddr6 ) );
215 #endif /* HAVE_IPV6 */
216  m_iAFRequire = RAF_ANY;
217  }
218  virtual ~CSSockAddr() {}
219 
220 
222  {
223  RAF_ANY = PF_UNSPEC,
224 #ifdef HAVE_IPV6
225  RAF_INET6 = AF_INET6,
226 #endif /* HAVE_IPV6 */
227  RAF_INET = AF_INET
228  };
229 
230  void SinFamily();
231  void SinPort( uint16_t iPort );
232  void SetIPv6( bool b );
233  bool GetIPv6() const { return( m_bIsIPv6 ); }
234 
235  socklen_t GetSockAddrLen() { return( sizeof( m_saddr ) ); }
236  sockaddr_in * GetSockAddr() { return( &m_saddr ); }
237  in_addr * GetAddr() { return( &( m_saddr.sin_addr ) ); }
238 #ifdef HAVE_IPV6
239  socklen_t GetSockAddrLen6() { return( sizeof( m_saddr6 ) ); }
240  sockaddr_in6 * GetSockAddr6() { return( &m_saddr6 ); }
241  in6_addr * GetAddr6() { return( &( m_saddr6.sin6_addr ) ); }
242 #endif /* HAVE_IPV6 */
243 
244  void SetAFRequire( EAFRequire iWhich ) { m_iAFRequire = iWhich; }
245  EAFRequire GetAFRequire() const { return( m_iAFRequire ); }
246 
247 private:
248  bool m_bIsIPv6;
249  sockaddr_in m_saddr;
250 #ifdef HAVE_IPV6
251  sockaddr_in6 m_saddr6;
252 #endif /* HAVE_IPV6 */
253  EAFRequire m_iAFRequire;
254 };
255 
256 
257 class Csock;
258 
259 
275 {
276 public:
283  CGetAddrInfo( const CS_STRING & sHostname, Csock * pSock, CSSockAddr & csSockAddr );
284  ~CGetAddrInfo();
285 
287  void Init();
289  int Process();
291  int Finish();
292 
293 private:
294  CS_STRING m_sHostname;
295  Csock * m_pSock;
296  CSSockAddr & m_csSockAddr;
297  struct addrinfo * m_pAddrRes;
298  struct addrinfo m_cHints;
299  int m_iRet;
300 };
301 
303 int CS_GetAddrInfo( const CS_STRING & sHostname, Csock * pSock, CSSockAddr & csSockAddr );
304 
310 int GetCsockSSLIdx();
311 
312 #ifdef HAVE_LIBSSL
313 Csock * GetCsockFromCTX( X509_STORE_CTX * pCTX );
315 #endif /* HAVE_LIBSSL */
316 
317 
318 const uint32_t CS_BLOCKSIZE = 4096;
319 template <class T> inline void CS_Delete( T * & p ) { if( p ) { delete p; p = NULL; } }
320 
321 #ifdef HAVE_LIBSSL
323 {
324  CT_NONE = 0,
326 };
327 
329 void CSAdjustTVTimeout( struct timeval & tv, long iTimeoutMS );
330 
331 void SSLErrors( const char *filename, uint32_t iLineNum );
332 
338 bool InitSSL( ECompType eCompressionType = CT_NONE );
339 
340 #endif /* HAVE_LIBSSL */
341 
345 bool InitCsocket();
349 void ShutdownCsocket();
350 
352 inline int GetSockError()
353 {
354 #ifdef _WIN32
355  return( WSAGetLastError() );
356 #else
357  return( errno );
358 #endif /* _WIN32 */
359 }
360 
362 inline void TFD_ZERO( fd_set *set )
363 {
364  FD_ZERO( set );
365 }
366 
367 inline void TFD_SET( cs_sock_t iSock, fd_set *set )
368 {
369  FD_SET( iSock, set );
370 }
371 
372 inline bool TFD_ISSET( cs_sock_t iSock, fd_set *set )
373 {
374  return( FD_ISSET( iSock, set ) != 0 );
375 }
376 
377 inline void TFD_CLR( cs_sock_t iSock, fd_set *set )
378 {
379  FD_CLR( iSock, set );
380 }
381 
382 void __Perror( const CS_STRING & s, const char * pszFile, uint32_t iLineNo );
383 uint64_t millitime();
384 
385 
394 {
395 public:
396  CCron();
397  virtual ~CCron() {}
398 
400  void run( timeval & tNow );
401 
406  void StartMaxCycles( double dTimeSequence, uint32_t iMaxCycles );
407  void StartMaxCycles( const timeval& tTimeSequence, uint32_t iMaxCycles );
408 
410  void Start( double dTimeSequence );
411  void Start( const timeval& TimeSequence );
412 
414  void Stop();
415 
417  void Pause();
418 
420  void UnPause();
421 
423  void Reset();
424 
425  timeval GetInterval() const;
426  uint32_t GetMaxCycles() const;
427  uint32_t GetCyclesLeft() const;
428 
430  bool isValid() const;
431 
432  const CS_STRING & GetName() const;
433  void SetName( const CS_STRING & sName );
434 
436  timeval GetNextRun() const { return( m_tTime ); }
437 
438 public:
439 
441  virtual void RunJob();
442 
443 protected:
445 
446 private:
447  timeval m_tTime;
448  bool m_bActive, m_bPause;
449  timeval m_tTimeSequence;
450  uint32_t m_iMaxCycles, m_iCycles;
451  CS_STRING m_sName;
452 };
453 
459 {
460 public:
461  CSMonitorFD() { m_bEnabled = true; }
462  virtual ~CSMonitorFD() {}
463 
470  virtual bool GatherFDsForSelect( std::map< cs_sock_t, short > & miiReadyFds, long & iTimeoutMS );
471 
477  virtual bool FDsThatTriggered( const std::map< cs_sock_t, short > & miiReadyFds ) { return( true ); }
478 
484  virtual bool CheckFDs( const std::map< cs_sock_t, short > & miiReadyFds );
485 
491  void Add( cs_sock_t iFD, short iMonitorEvents ) { m_miiMonitorFDs[iFD] = iMonitorEvents; }
493  void Remove( cs_sock_t iFD ) { m_miiMonitorFDs.erase( iFD ); }
495  void DisableMonitor() { m_bEnabled = false; }
496 
497  bool IsEnabled() const { return( m_bEnabled ); }
498 
499 protected:
500  std::map< cs_sock_t, short > m_miiMonitorFDs;
502 };
503 
504 
510 {
511 public:
513  virtual ~CSockCommon();
514 
515  void CleanupCrons();
516  void CleanupFDMonitors();
517 
519  const std::vector<CCron *> & GetCrons() const { return( m_vcCrons ); }
521  virtual void Cron();
522 
524  virtual void AddCron( CCron * pcCron );
531  virtual void DelCron( const CS_STRING & sName, bool bDeleteAll = true, bool bCaseSensitive = true );
533  virtual void DelCron( uint32_t iPos );
535  virtual void DelCronByAddr( CCron * pcCron );
536 
537  void CheckFDs( const std::map< cs_sock_t, short > & miiReadyFds );
538  void AssignFDs( std::map< cs_sock_t, short > & miiReadyFds, struct timeval * tvtimeout );
539 
541  void MonitorFD( CSMonitorFD * pMonitorFD ) { m_vcMonitorFD.push_back( pMonitorFD ); }
542 
543 protected:
544  std::vector<CCron *> m_vcCrons;
545  std::vector<CSMonitorFD *> m_vcMonitorFD;
546 };
547 
548 
549 #ifdef HAVE_LIBSSL
550 typedef int ( *FPCertVerifyCB )( int, X509_STORE_CTX * );
551 #endif /* HAVE_LIBSSL */
552 
553 
565 {
566 public:
568  Csock( int iTimeout = 60 );
575  Csock( const CS_STRING & sHostname, uint16_t uPort, int itimeout = 60 );
576 
578  virtual Csock *GetSockObj( const CS_STRING & sHostname, uint16_t iPort );
579 
580  virtual ~Csock();
581 
588  virtual void Dereference();
590  virtual void Copy( const Csock & cCopy );
591 
592  enum ETConn
593  {
594  OUTBOUND = 0,
595  LISTENER = 1,
596  INBOUND = 2
597  };
598 
599  enum EFRead
600  {
601  READ_EOF = 0,
602  READ_ERR = -1,
603  READ_EAGAIN = -2,
604  READ_CONNREFUSED = -3,
605  READ_TIMEDOUT = -4
606  };
607 
608  enum EFSelect
609  {
610  SEL_OK = 0,
611  SEL_TIMEOUT = -1,
612  SEL_EAGAIN = -2,
613  SEL_ERR = -3
614  };
615 
617  {
618  TLS = 0,
619  SSL23 = TLS,
620  SSL2 = 2,
621  SSL3 = 3,
622  TLS1 = 4,
623  TLS11 = 5,
624  TLS12 = 6
625  };
626 
628  {
629  EDP_None = 0,
630  EDP_SSLv2 = 1,
631  EDP_SSLv3 = 2,
632  EDP_TLSv1 = 4,
633  EDP_TLSv1_1 = 8,
634  EDP_TLSv1_2 = 16,
635  EDP_SSL = (EDP_SSLv2|EDP_SSLv3)
636  };
637 
639  {
640  CST_START = 0,
641  CST_DNS = CST_START,
642  CST_BINDVHOST = 1,
643  CST_DESTDNS = 2,
644  CST_CONNECT = 3,
645  CST_CONNECTSSL = 4,
646  CST_OK = 5
647  };
648 
650  {
651  CLT_DONT = 0,
652  CLT_NOW = 1,
653  CLT_AFTERWRITE = 2,
654  CLT_DEREFERENCE = 3
655  };
656 
657  Csock & operator<<( const CS_STRING & s );
658  Csock & operator<<( std::ostream & ( *io )( std::ostream & ) );
659  Csock & operator<<( int32_t i );
660  Csock & operator<<( uint32_t i );
661  Csock & operator<<( int64_t i );
662  Csock & operator<<( uint64_t i );
663  Csock & operator<<( float i );
664  Csock & operator<<( double i );
665 
670  virtual bool Connect();
671 
672 #ifdef HAVE_UNIX_SOCKET
673 
677  virtual bool ConnectUnix( const CS_STRING & sPath );
678 
685  virtual bool ListenUnix( const CS_STRING & sBindFile, int iMaxConns = SOMAXCONN, uint32_t iTimeout = 0 );
686 #endif
687 
696  virtual bool Listen( uint16_t iPort, int iMaxConns = SOMAXCONN, const CS_STRING & sBindHost = "", uint32_t iTimeout = 0, bool bDetach = false );
697 
699  virtual cs_sock_t Accept( CS_STRING & sHost, uint16_t & iRPort );
700 
702  virtual bool AcceptSSL();
703 
705  virtual bool SSLClientSetup();
706 
708  virtual bool SSLServerSetup();
709 
716  virtual bool ConnectSSL();
717 
719  bool StartTLS();
720 
731  virtual bool Write( const char *data, size_t len );
732 
741  virtual bool Write( const CS_STRING & sData );
742 
758  virtual cs_ssize_t Read( char *data, size_t len );
759  CS_STRING GetLocalIP() const;
760  CS_STRING GetRemoteIP() const;
761 
763  virtual bool IsConnected() const;
765  virtual void SetIsConnected( bool b );
766 
768  cs_sock_t & GetRSock();
769  const cs_sock_t & GetRSock() const;
770  void SetRSock( cs_sock_t iSock );
771  cs_sock_t & GetWSock();
772  const cs_sock_t & GetWSock() const;
773  void SetWSock( cs_sock_t iSock );
774 
775  void SetSock( cs_sock_t iSock );
776  cs_sock_t & GetSock();
777  const cs_sock_t & GetSock() const;
778 
784  void CallSockError( int iErrno, const CS_STRING & sDescription = "" );
786  virtual void ResetTimer();
787 
789  void PauseRead();
790  void UnPauseRead();
791  bool IsReadPaused() const;
798  enum
799  {
800  TMO_READ = 1,
801  TMO_WRITE = 2,
802  TMO_ACCEPT = 4,
803  TMO_ALL = TMO_READ|TMO_WRITE|TMO_ACCEPT
804  };
805 
808  void SetTimeout( int iTimeout, uint32_t iTimeoutType = TMO_ALL );
809  void SetTimeoutType( uint32_t iTimeoutType );
810  int GetTimeout() const;
811  uint32_t GetTimeoutType() const;
812 
814  virtual bool CheckTimeout( time_t iNow );
815 
820  virtual void PushBuff( const char *data, size_t len, bool bStartAtZero = false );
821 
825  CS_STRING & GetInternalReadBuffer();
826 
829  CS_STRING & GetInternalWriteBuffer();
830 
832  void SetMaxBufferThreshold( uint32_t iThreshold );
833  uint32_t GetMaxBufferThreshold() const;
834 
836  int GetType() const;
837  void SetType( int iType );
838 
840  const CS_STRING & GetSockName() const;
841  void SetSockName( const CS_STRING & sName );
842 
844  const CS_STRING & GetHostName() const;
845  void SetHostName( const CS_STRING & sHostname );
846 
847 
849  uint64_t GetStartTime() const;
851  void ResetStartTime();
852 
854  uint64_t GetBytesRead() const;
855  void ResetBytesRead();
856 
858  uint64_t GetBytesWritten() const;
859  void ResetBytesWritten();
860 
862  double GetAvgRead( uint64_t iSample = 1000 ) const;
863 
865  double GetAvgWrite( uint64_t iSample = 1000 ) const;
866 
868  uint16_t GetRemotePort() const;
869 
871  uint16_t GetLocalPort() const;
872 
874  uint16_t GetPort() const;
875  void SetPort( uint16_t iPort );
876 
878  void Close( ECloseType eCloseType = CLT_NOW );
880  ECloseType GetCloseType() const { return( m_eCloseType ); }
881  bool IsClosed() const { return( GetCloseType() != CLT_DONT ); }
882 
884  void NonBlockingIO();
885 
887  bool GetSSL() const;
888  void SetSSL( bool b );
889 
890 #ifdef HAVE_LIBSSL
891  void DisableSSLProtocols( u_int uDisableOpts ) { m_uDisableProtocols = uDisableOpts; }
894  void DisableSSLCompression() { m_bNoSSLCompression = true; }
896  void FollowSSLCipherServerPreference() { m_bSSLCipherServerPreference = true; }
898  void SetCipher( const CS_STRING & sCipher );
899  const CS_STRING & GetCipher() const;
900 
902  void SetDHParamLocation( const CS_STRING & sDHParamFile );
903  const CS_STRING & GetDHParamLocation() const;
904  void SetKeyLocation( const CS_STRING & sKeyFile );
905  const CS_STRING & GetKeyLocation() const;
906  void SetPemLocation( const CS_STRING & sPemFile );
907  const CS_STRING & GetPemLocation() const;
908  void SetPemPass( const CS_STRING & sPassword );
909  const CS_STRING & GetPemPass() const;
910 
912  void SetSSLMethod( int iMethod );
913  int GetSSLMethod() const;
914 
915  void SetSSLObject( SSL *ssl, bool bDeleteExisting = false );
916  SSL * GetSSLObject() const;
917  void SetCTXObject( SSL_CTX *sslCtx, bool bDeleteExisting = false );
918  SSL_SESSION * GetSSLSession() const;
919 
921  void SetCertVerifyCB( FPCertVerifyCB pFP ) { m_pCerVerifyCB = pFP; }
922 #endif /* HAVE_LIBSSL */
923 
925  bool HasWriteBuffer() const;
926  void ClearWriteBuffer();
927 
930  bool SslIsEstablished() const;
931 
933  bool ConnectInetd( bool bIsSSL = false, const CS_STRING & sHostname = "" );
934 
936  bool ConnectFD( int iReadFD, int iWriteFD, const CS_STRING & sName, bool bIsSSL = false, ETConn eDirection = INBOUND );
937 
939 #ifdef HAVE_LIBSSL
940  X509 *GetX509() const;
942 
944  CS_STRING GetPeerPubKey() const;
946  long GetPeerFingerprint( CS_STRING & sFP ) const;
947 
948  uint32_t GetRequireClientCertFlags() const;
950  void SetRequiresClientCert( bool bRequiresCert );
952  void SetRequireClientCertFlags( uint32_t iRequireClientCertFlags ) { m_iRequireClientCertFlags = iRequireClientCertFlags; }
953 #endif /* HAVE_LIBSSL */
954 
956  virtual void SetParentSockName( const CS_STRING & sParentName );
957  const CS_STRING & GetParentSockName() const;
958 
964  virtual void SetRate( uint32_t iBytes, uint64_t iMilliseconds );
965 
966  uint32_t GetRateBytes() const;
967  uint64_t GetRateTime() const;
968 
972  virtual void Connected() {}
976  virtual void Disconnected() {}
980  virtual void Timeout() {}
984  virtual void ReadData( const char *data, size_t len ) {}
989  virtual void ReadLine( const CS_STRING & sLine ) {}
991  void EnableReadLine();
992  void DisableReadLine();
994  bool HasReadLine() const { return( m_bEnableReadLine ); }
995 
1001  virtual void ReachedMaxBuffer();
1005  virtual void SockError( int iErrno, const CS_STRING & sDescription ) {}
1011  virtual bool ConnectionFrom( const CS_STRING & sHost, uint16_t iPort ) { return( true ); }
1012 
1018  virtual void Listening( const CS_STRING & sBindIP, uint16_t uPort ) {}
1019 
1024  virtual void ConnectionRefused() {}
1028  virtual void ReadPaused() {}
1029 
1030 #ifdef HAVE_LIBSSL
1031 
1035  virtual void SSLFinishSetup( SSL * pSSL ) {}
1043  virtual bool SNIConfigureServer( const CS_STRING & sHostname, CS_STRING & sPemFile, CS_STRING & sPemPass ) { return( false ); }
1049  virtual bool SNIConfigureClient( CS_STRING & sHostname );
1051  SSL_CTX * SetupServerCTX();
1052 
1066  virtual void SSLHandShakeFinished() {}
1078  virtual int VerifyPeerCertificate( int iPreVerify, X509_STORE_CTX * pStoreCTX ) { return( 1 ); }
1079 #endif /* HAVE_LIBSSL */
1080 
1081 
1083  time_t GetTimeSinceLastDataTransaction( time_t iNow = 0 ) const;
1084 
1085  time_t GetLastCheckTimeout() const { return( m_iLastCheckTimeoutTime ); }
1086 
1088  time_t GetNextCheckTimeout( time_t iNow = 0 ) const;
1089 
1091  virtual int GetPending() const;
1092 
1094  // Connection State Stuff
1096  ECONState GetConState() const { return( m_eConState ); }
1098  void SetConState( ECONState eState ) { m_eConState = eState; }
1099 
1101  bool CreateSocksFD();
1102 
1104  void CloseSocksFD();
1105 
1106  const CS_STRING & GetBindHost() const { return( m_sBindHost ); }
1107  void SetBindHost( const CS_STRING & sBindHost ) { m_sBindHost = sBindHost; }
1108 
1110  {
1112  DNS_DEST
1113  };
1114 
1119  int DNSLookup( EDNSLType eDNSLType );
1120 
1122  bool SetupVHost();
1123 
1124  bool GetIPv6() const { return( m_bIsIPv6 ); }
1125  void SetIPv6( bool b )
1126  {
1127  m_bIsIPv6 = b;
1128  m_address.SetIPv6( b );
1129  m_bindhost.SetIPv6( b );
1130  }
1131 
1133  {
1134  m_address.SetAFRequire( iAFRequire );
1135  m_bindhost.SetAFRequire( iAFRequire );
1136  }
1137 
1139  bool AllowWrite( uint64_t & iNOW ) const;
1140 
1141 
1142  void SetSkipConnect( bool b ) { m_bSkipConnect = b; }
1143 
1150  virtual int GetAddrInfo( const CS_STRING & sHostname, CSSockAddr & csSockAddr );
1151 
1164  virtual int ConvertAddress( const struct sockaddr_storage * pAddr, socklen_t iAddrLen, CS_STRING & sIP, uint16_t * piPort ) const;
1165 
1166 #ifdef HAVE_C_ARES
1167  CSSockAddr * GetCurrentAddr() const { return( m_pCurrAddr ); }
1168  void SetAresFinished( int status ) { m_pCurrAddr = NULL; m_iARESStatus = status; }
1169  ares_channel GetAresChannel() const { return( m_pARESChannel ); }
1170 #endif /* HAVE_C_ARES */
1171 
1173  int GetMaxConns() const { return( m_iMaxConns ); }
1174 
1175 #ifdef HAVE_ICU
1176  void SetEncoding( const CS_STRING & sEncoding );
1177  CS_STRING GetEncoding() const { return m_sEncoding; }
1178  virtual void IcuExtToUCallback(
1179  UConverterToUnicodeArgs* toArgs,
1180  const char* codeUnits,
1181  int32_t length,
1182  UConverterCallbackReason reason,
1183  UErrorCode* err );
1184  virtual void IcuExtFromUCallback(
1185  UConverterFromUnicodeArgs* fromArgs,
1186  const UChar* codeUnits,
1187  int32_t length,
1188  UChar32 codePoint,
1189  UConverterCallbackReason reason,
1190  UErrorCode* err );
1191 #endif /* HAVE_ICU */
1192 
1193 private:
1195  Csock( const Csock & cCopy ) : CSockCommon() {}
1197  void ShrinkSendBuff();
1198  void IncBuffPos( size_t uBytes );
1200 
1201  // NOTE! if you add any new members, be sure to add them to Copy()
1202  uint16_t m_uPort;
1203  cs_sock_t m_iReadSock, m_iWriteSock;
1204  int m_iTimeout, m_iConnType, m_iMethod, m_iTcount, m_iMaxConns;
1205  bool m_bUseSSL, m_bIsConnected;
1206  bool m_bsslEstablished, m_bEnableReadLine, m_bPauseRead;
1207  CS_STRING m_shostname, m_sbuffer, m_sSockName, m_sDHParamFile, m_sKeyFile, m_sPemFile, m_sCipherType, m_sParentName;
1208  CS_STRING m_sSend, m_sPemPass;
1209  ECloseType m_eCloseType;
1210 
1211  // initialized lazily
1212  mutable uint16_t m_iRemotePort, m_iLocalPort;
1213  mutable CS_STRING m_sLocalIP, m_sRemoteIP;
1214 
1215  uint64_t m_iMaxMilliSeconds, m_iLastSendTime, m_iBytesRead, m_iBytesWritten, m_iStartTime;
1216  uint32_t m_iMaxBytes, m_iMaxStoredBufferLength, m_iTimeoutType;
1217  size_t m_iLastSend, m_uSendBufferPos;
1218 
1219  CSSockAddr m_address, m_bindhost;
1220  bool m_bIsIPv6, m_bSkipConnect;
1221  time_t m_iLastCheckTimeoutTime;
1222 
1223 #ifdef HAVE_LIBSSL
1224  CS_STRING m_sSSLBuffer;
1225  SSL * m_ssl;
1226  SSL_CTX * m_ssl_ctx;
1227  uint32_t m_iRequireClientCertFlags;
1228  u_int m_uDisableProtocols;
1229  bool m_bNoSSLCompression;
1230  bool m_bSSLCipherServerPreference;
1231 
1232  FPCertVerifyCB m_pCerVerifyCB;
1233 
1234  void FREE_SSL();
1235  void FREE_CTX();
1236  bool ConfigureCTXOptions( SSL_CTX * pCTX );
1237 
1238 #endif /* HAVE_LIBSSL */
1239 
1241  cs_sock_t CreateSocket( bool bListen = false, bool bUnix = false );
1242  void Init( const CS_STRING & sHostname, uint16_t uPort, int iTimeout = 60 );
1243 
1244  // Connection State Info
1245  ECONState m_eConState;
1246  CS_STRING m_sBindHost;
1247  uint32_t m_iCurBindCount, m_iDNSTryCount;
1248 #ifdef HAVE_C_ARES
1249  void FreeAres();
1250  ares_channel m_pARESChannel;
1251  CSSockAddr * m_pCurrAddr;
1252  int m_iARESStatus;
1253 #endif /* HAVE_C_ARES */
1254 
1255 #ifdef HAVE_ICU
1256  UConverter* m_cnvInt;
1257  UConverter* m_cnvExt;
1258  bool m_cnvTryUTF8;
1259  bool m_cnvSendUTF8;
1260  CS_STRING m_sEncoding;
1261 #endif
1262 };
1263 
1269 {
1270 public:
1276  CSConnection( const CS_STRING & sHostname, uint16_t iPort, int iTimeout = 60 )
1277  {
1278  m_sHostname = sHostname;
1279  m_iPort = iPort;
1280  m_iTimeout = iTimeout;
1281  m_bIsSSL = false;
1282 #ifdef HAVE_LIBSSL
1283  m_sCipher = "HIGH";
1284 #endif /* HAVE_LIBSSL */
1285  m_iAFrequire = CSSockAddr::RAF_ANY;
1286  }
1287  virtual ~CSConnection() {}
1288 
1289  const CS_STRING & GetHostname() const { return( m_sHostname ); }
1290  const CS_STRING & GetSockName() const { return( m_sSockName ); }
1291  const CS_STRING & GetBindHost() const { return( m_sBindHost ); }
1292  uint16_t GetPort() const { return( m_iPort ); }
1293  int GetTimeout() const { return( m_iTimeout ); }
1294  bool GetIsSSL() const { return( m_bIsSSL ); }
1295  CSSockAddr::EAFRequire GetAFRequire() const { return( m_iAFrequire ); }
1296 
1297 #ifdef HAVE_LIBSSL
1298  const CS_STRING & GetCipher() const { return( m_sCipher ); }
1299  const CS_STRING & GetPemLocation() const { return( m_sPemLocation ); }
1300  const CS_STRING & GetKeyLocation() const { return( m_sKeyLocation ); }
1301  const CS_STRING & GetDHParamLocation() const { return( m_sDHParamLocation ); }
1302  const CS_STRING & GetPemPass() const { return( m_sPemPass ); }
1303 #endif /* HAVE_LIBSSL */
1304 
1306  void SetHostname( const CS_STRING & s ) { m_sHostname = s; }
1308  void SetSockName( const CS_STRING & s ) { m_sSockName = s; }
1310  void SetBindHost( const CS_STRING & s ) { m_sBindHost = s; }
1312  void SetPort( uint16_t i ) { m_iPort = i; }
1314  void SetTimeout( int i ) { m_iTimeout = i; }
1316  void SetIsSSL( bool b ) { m_bIsSSL = b; }
1318  void SetAFRequire( CSSockAddr::EAFRequire iAFRequire ) { m_iAFrequire = iAFRequire; }
1319 
1320 #ifdef HAVE_LIBSSL
1321  void SetCipher( const CS_STRING & s ) { m_sCipher = s; }
1324  void SetPemLocation( const CS_STRING & s ) { m_sPemLocation = s; }
1326  void SetPemPass( const CS_STRING & s ) { m_sPemPass = s; }
1327 #endif /* HAVE_LIBSSL */
1328 
1329 protected:
1330  CS_STRING m_sHostname, m_sSockName, m_sBindHost;
1331  uint16_t m_iPort;
1333  bool m_bIsSSL;
1335 #ifdef HAVE_LIBSSL
1336  CS_STRING m_sDHParamLocation, m_sKeyLocation, m_sPemLocation, m_sPemPass, m_sCipher;
1337 #endif /* HAVE_LIBSSL */
1338 };
1339 
1341 {
1342 public:
1343  CSSSLConnection( const CS_STRING & sHostname, uint16_t iPort, int iTimeout = 60 ) :
1344  CSConnection( sHostname, iPort, iTimeout )
1345  {
1346  SetIsSSL( true );
1347  }
1348 };
1349 
1350 
1356 {
1357 public:
1363  CSListener( uint16_t iPort, const CS_STRING & sBindHost = "", bool bDetach = false )
1364  {
1365  m_iPort = iPort;
1366  m_sBindHost = sBindHost;
1367  m_bIsSSL = false;
1368  m_iMaxConns = SOMAXCONN;
1369  m_iTimeout = 0;
1370  m_iAFrequire = CSSockAddr::RAF_ANY;
1371  m_bDetach = bDetach;
1372 #ifdef HAVE_LIBSSL
1373  m_sCipher = "HIGH";
1374  m_iRequireCertFlags = 0;
1375 #endif /* HAVE_LIBSSL */
1376  }
1377  virtual ~CSListener() {}
1378 
1379  void SetDetach( bool b ) { m_bDetach = b; }
1380  bool GetDetach() const { return( m_bDetach ); }
1381  uint16_t GetPort() const { return( m_iPort ); }
1382  const CS_STRING & GetSockName() const { return( m_sSockName ); }
1383  const CS_STRING & GetBindHost() const { return( m_sBindHost ); }
1384  bool GetIsSSL() const { return( m_bIsSSL ); }
1385  int GetMaxConns() const { return( m_iMaxConns ); }
1386  uint32_t GetTimeout() const { return( m_iTimeout ); }
1387  CSSockAddr::EAFRequire GetAFRequire() const { return( m_iAFrequire ); }
1388 #ifdef HAVE_LIBSSL
1389  const CS_STRING & GetCipher() const { return( m_sCipher ); }
1390  const CS_STRING & GetDHParamLocation() const { return( m_sDHParamLocation ); }
1391  const CS_STRING & GetKeyLocation() const { return( m_sKeyLocation ); }
1392  const CS_STRING & GetPemLocation() const { return( m_sPemLocation ); }
1393  const CS_STRING & GetPemPass() const { return( m_sPemPass ); }
1394  uint32_t GetRequireClientCertFlags() const { return( m_iRequireCertFlags ); }
1395 #endif /* HAVE_LIBSSL */
1396 
1398  void SetPort( uint16_t iPort ) { m_iPort = iPort; }
1400  void SetSockName( const CS_STRING & sSockName ) { m_sSockName = sSockName; }
1402  void SetBindHost( const CS_STRING & sBindHost ) { m_sBindHost = sBindHost; }
1404  void SetIsSSL( bool b ) { m_bIsSSL = b; }
1406  void SetMaxConns( int i ) { m_iMaxConns = i; }
1408  void SetTimeout( uint32_t i ) { m_iTimeout = i; }
1410  void SetAFRequire( CSSockAddr::EAFRequire iAFRequire ) { m_iAFrequire = iAFRequire; }
1411 
1412 #ifdef HAVE_LIBSSL
1413  void SetCipher( const CS_STRING & s ) { m_sCipher = s; }
1416  void SetPemLocation( const CS_STRING & s ) { m_sPemLocation = s; }
1418  void SetKeyLocation( const CS_STRING & s ) { m_sKeyLocation = s; }
1420  void SetDHParamLocation( const CS_STRING & s ) { m_sDHParamLocation = s; }
1422  void SetPemPass( const CS_STRING & s ) { m_sPemPass = s; }
1424  void SetRequiresClientCert( bool b ) { m_iRequireCertFlags = ( b ? SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT : 0 ); }
1426  void SetRequireClientCertFlags( unsigned int iRequireCertFlags ) { m_iRequireCertFlags = iRequireCertFlags; }
1427 #endif /* HAVE_LIBSSL */
1428 private:
1429  uint16_t m_iPort;
1430  CS_STRING m_sSockName, m_sBindHost;
1431  bool m_bIsSSL;
1432  bool m_bDetach;
1433  int m_iMaxConns;
1434  uint32_t m_iTimeout;
1435  CSSockAddr::EAFRequire m_iAFrequire;
1436 
1437 #ifdef HAVE_LIBSSL
1438  CS_STRING m_sDHParamLocation, m_sKeyLocation, m_sPemLocation, m_sPemPass, m_sCipher;
1439  uint32_t m_iRequireCertFlags;
1440 #endif /* HAVE_LIBSSL */
1441 };
1442 
1443 #ifdef HAVE_LIBSSL
1444 class CSSSListener : public CSListener
1445 {
1446 public:
1447  CSSSListener( uint16_t iPort, const CS_STRING & sBindHost = "" ) :
1448  CSListener( iPort, sBindHost )
1449  {
1450  SetIsSSL( true );
1451  }
1452 };
1453 #endif /* HAVE_LIBSSL */
1454 
1476 class CS_EXPORT CSocketManager : public std::vector<Csock *>, public CSockCommon
1477 {
1478 public:
1479  CSocketManager();
1480  virtual ~CSocketManager();
1481  virtual void clear();
1482  virtual void Cleanup();
1483 
1484  virtual Csock * GetSockObj( const CS_STRING & sHostname, uint16_t uPort, int iTimeout = 60 );
1485 
1487  {
1488  SUCCESS = 0,
1489  SELECT_ERROR = -1,
1490  SELECT_TIMEOUT = -2,
1491  SELECT_TRYAGAIN = -3
1492  };
1493 
1499  void Connect( const CSConnection & cCon, Csock * pcSock = NULL );
1500 
1510  virtual bool Listen( const CSListener & cListen, Csock * pcSock = NULL, uint16_t * piRandPort = NULL );
1511 
1512 
1514  bool HasFDs() const;
1515 
1521  virtual void Loop();
1522 
1538  void DynamicSelectLoop( uint64_t iLowerBounds, uint64_t iUpperBounds, time_t iMaxResolution = 3600 );
1539 
1544  virtual void AddSock( Csock * pcSock, const CS_STRING & sSockName );
1545 
1547  virtual Csock * FindSockByRemotePort( uint16_t iPort );
1548 
1550  virtual Csock * FindSockByLocalPort( uint16_t iPort );
1551 
1553  virtual Csock * FindSockByName( const CS_STRING & sName );
1554 
1556  virtual Csock * FindSockByFD( cs_sock_t iFD );
1557 
1558  virtual std::vector<Csock *> FindSocksByName( const CS_STRING & sName );
1559 
1561  virtual std::vector<Csock *> FindSocksByRemoteHost( const CS_STRING & sHostname );
1562 
1564  int GetErrno() const { return( m_errno ); }
1565 
1567  uint64_t GetSelectTimeout() const { return( m_iSelectWait ); }
1570  void SetSelectTimeout( uint64_t iTimeout ) { m_iSelectWait = iTimeout; }
1571 
1576  virtual void DelSockByAddr( Csock * pcSock );
1577 
1583  virtual void DelSock( size_t iPos );
1584 
1591  virtual bool SwapSockByIdx( Csock * pNewSock, size_t iOrginalSockIdx );
1592 
1599  virtual bool SwapSockByAddr( Csock * pNewSock, Csock * pOrigSock );
1600 
1602  uint64_t GetBytesRead() const;
1603 
1605  uint64_t GetBytesWritten() const;
1606 
1609  {
1610  ECT_Read = 1,
1611  ECT_Write = 2
1612  };
1613 
1614  void FDSetCheck( cs_sock_t iFd, std::map< cs_sock_t, short > & miiReadyFds, ECheckType eType );
1615  bool FDHasCheck( cs_sock_t iFd, std::map< cs_sock_t, short > & miiReadyFds, ECheckType eType );
1616 
1617 protected:
1618 
1619  virtual int Select( std::map< cs_sock_t, short > & miiReadyFds, struct timeval *tvtimeout );
1620 
1621 private:
1628  void Select( std::map<Csock *, EMessages> & mpeSocks );
1629 
1630  timeval GetDynamicSleepTime( const timeval& tNow, const timeval& tMaxResolution ) const;
1631 
1633  virtual void SelectSock( std::map<Csock *, EMessages> & mpeSocks, EMessages eErrno, Csock * pcSock );
1634 
1636  // Connection State Functions
1637 
1639  // members
1640  EMessages m_errno;
1641  uint64_t m_iCallTimeouts;
1642  uint64_t m_iBytesRead;
1643  uint64_t m_iBytesWritten;
1644  uint64_t m_iSelectWait;
1645 };
1646 
1647 
1654 template<class T>
1656 {
1657 public:
1659  virtual ~TSocketManager() {}
1660  virtual T * GetSockObj( const CS_STRING & sHostname, uint16_t uPort, int iTimeout = 60 )
1661  {
1662  return( new T( sHostname, uPort, iTimeout ) );
1663  }
1664 };
1665 
1666 #ifndef _NO_CSOCKET_NS
1667 }
1668 #endif /* _NO_CSOCKET_NS */
1669 
1670 #endif /* _HAS_CSOCKET_ */
1671 
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:1301
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:1426
void SSLErrors(const char *filename, uint32_t iLineNum)
const CS_STRING & GetBindHost() const
Definition: Csocket.h:1291
ECloseType GetCloseType() const
returns int of type to close
Definition: Csocket.h:880
virtual ~CCron()
Definition: Csocket.h:397
simple class to share common code to both TSockManager and Csock
Definition: Csocket.h:509
void MonitorFD(CSMonitorFD *pMonitorFD)
add an FD set to monitor
Definition: Csocket.h:541
ssize_t cs_ssize_t
Definition: Csocket.h:157
Ease of use templated socket manager.
Definition: Csocket.h:1655
#define CS_EXPORT
Definition: Csocket.h:136
Definition: Csocket.h:223
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:1066
uint64_t millitime()
virtual void ReadPaused()
This gets called every iteration of CSocketManager::Select() if the socket is ReadPaused.
Definition: Csocket.h:1028
void SetIPv6(bool b)
Definition: Csocket.h:1125
void SetBindHost(const CS_STRING &s)
sets the hostname to bind to (vhost support)
Definition: Csocket.h:1310
void SetBindHost(const CS_STRING &sBindHost)
sets the host to bind to
Definition: Csocket.h:1402
Definition: Csocket.h:1340
CSCharBuffer(size_t iSize)
Definition: Csocket.h:187
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:1106
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:1005
const CS_STRING & GetSockName() const
Definition: Csocket.h:1382
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:1408
this function is a wrapper around getaddrinfo (for ipv6)
Definition: Csocket.h:274
CS_STRING GetEncoding() const
Definition: Csocket.h:1177
CS_STRING m_sSockName
Definition: Csocket.h:1330
const CS_STRING & GetDHParamLocation() const
Definition: Csocket.h:1390
ESSLMethod
Definition: Csocket.h:616
virtual T * GetSockObj(const CS_STRING &sHostname, uint16_t uPort, int iTimeout=60)
Definition: Csocket.h:1660
void Add(cs_sock_t iFD, short iMonitorEvents)
adds a file descriptor to be monitored
Definition: Csocket.h:491
bool GetIPv6() const
Definition: Csocket.h:1124
void SetConState(ECONState eState)
sets the connection state to eState
Definition: Csocket.h:1098
EAFRequire GetAFRequire() const
Definition: Csocket.h:245
bool TFD_ISSET(cs_sock_t iSock, fd_set *set)
Definition: Csocket.h:372
void SetAFRequire(EAFRequire iWhich)
Definition: Csocket.h:244
virtual ~CSSockAddr()
Definition: Csocket.h:218
CSSockAddr::EAFRequire GetAFRequire() const
Definition: Csocket.h:1387
void Remove(cs_sock_t iFD)
removes this fd from monitoring
Definition: Csocket.h:493
time_t GetLastCheckTimeout() const
Definition: Csocket.h:1085
const CS_STRING & GetPemLocation() const
Definition: Csocket.h:1299
ETConn
Definition: Csocket.h:592
bool m_bEnabled
Definition: Csocket.h:501
bool m_bRunOnNextCall
if set to true, RunJob() gets called on next invocation of run() despite the timeout ...
Definition: Csocket.h:444
void SetTimeout(int i)
sets the connection timeout
Definition: Csocket.h:1314
uint16_t GetPort() const
Definition: Csocket.h:1381
Definition: Csocket.h:1444
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:477
char * operator()()
Definition: Csocket.h:195
EFRead
Definition: Csocket.h:599
Ease of use self deleting char * class.
Definition: Csocket.h:184
EFSelect
Definition: Csocket.h:608
virtual void Connected()
Connected event.
Definition: Csocket.h:972
ECONState
Definition: Csocket.h:638
uint64_t GetSelectTimeout() const
Get the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond )
Definition: Csocket.h:1567
const CS_STRING & GetCipher() const
Definition: Csocket.h:1298
uint32_t GetTimeout() const
Definition: Csocket.h:1386
virtual ~CSMonitorFD()
Definition: Csocket.h:462
Definition: Csocket.h:325
void SetDetach(bool b)
Definition: Csocket.h:1379
CSSockAddr::EAFRequire m_iAFrequire
Definition: Csocket.h:1334
void SetPemLocation(const CS_STRING &s)
set the location of the pemfile
Definition: Csocket.h:1416
this lookup is for the vhost bind
Definition: Csocket.h:1111
void SetSockName(const CS_STRING &s)
sets the name of the socket, used for reference, ie in FindSockByName()
Definition: Csocket.h:1308
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:1078
void CS_Delete(T *&p)
Definition: Csocket.h:319
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:1035
const CS_STRING & GetHostname() const
Definition: Csocket.h:1289
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:1570
bool GetIPv6() const
Definition: Csocket.h:233
int GetMaxConns() const
Definition: Csocket.h:1385
ECloseType
Definition: Csocket.h:649
in_addr * GetAddr()
Definition: Csocket.h:237
const CS_STRING & GetBindHost() const
Definition: Csocket.h:1383
options for creating a connection
Definition: Csocket.h:1268
void SetAFRequire(CSSockAddr::EAFRequire iAFRequire)
sets the AF family type required
Definition: Csocket.h:1318
CSSockAddr::EAFRequire GetAFRequire() const
Definition: Csocket.h:1295
Basic socket class.
Definition: Csocket.h:564
void SetAFRequire(CSSockAddr::EAFRequire iAFRequire)
sets the AF family type required
Definition: Csocket.h:1410
void DisableSSLCompression()
allow disabling compression
Definition: Csocket.h:894
const CS_STRING & GetPemPass() const
Definition: Csocket.h:1393
virtual void Timeout()
Sock Timed out event.
Definition: Csocket.h:980
virtual ~CSListener()
Definition: Csocket.h:1377
bool m_bIsSSL
Definition: Csocket.h:1333
const CS_STRING & GetCipher() const
Definition: Csocket.h:1389
int GetErrno() const
return the last known error as set by this class
Definition: Csocket.h:1564
ECompType
Definition: Csocket.h:322
#define CS_STRING
Definition: Csocket.h:123
socklen_t GetSockAddrLen6()
Definition: Csocket.h:239
void SetPort(uint16_t iPort)
sets the port to listen on. Set to 0 to listen on a random port
Definition: Csocket.h:1398
void SetMaxConns(int i)
set max connections as called by accept()
Definition: Csocket.h:1406
virtual ~CSConnection()
Definition: Csocket.h:1287
CSMonitorFD()
Definition: Csocket.h:461
uint16_t GetPort() const
Definition: Csocket.h:1292
CSConnection(const CS_STRING &sHostname, uint16_t iPort, int iTimeout=60)
Definition: Csocket.h:1276
uint16_t m_iPort
Definition: Csocket.h:1331
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:436
void SetPemPass(const CS_STRING &s)
set the pemfile pass
Definition: Csocket.h:1326
int GetTimeout() const
Definition: Csocket.h:1293
virtual ~TSocketManager()
Definition: Csocket.h:1659
int cs_sock_t
Definition: Csocket.h:156
const uint32_t CS_BLOCKSIZE
Definition: Csocket.h:318
void SetKeyLocation(const CS_STRING &s)
set the location of the keyfile
Definition: Csocket.h:1418
options container to create a listener
Definition: Csocket.h:1355
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:1447
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:1011
const CS_STRING & GetPemLocation() const
Definition: Csocket.h:1392
EDisableProtocol
Definition: Csocket.h:627
void SetSkipConnect(bool b)
Definition: Csocket.h:1142
std::vector< CSMonitorFD * > m_vcMonitorFD
Definition: Csocket.h:545
bool IsClosed() const
Definition: Csocket.h:881
const CS_STRING & GetKeyLocation() const
Definition: Csocket.h:1300
const CS_STRING & GetKeyLocation() const
Definition: Csocket.h:1391
this is the main cron job class
Definition: Csocket.h:393
bool IsEnabled() const
Definition: Csocket.h:497
ECONState GetConState() const
returns the current connection state
Definition: Csocket.h:1096
void SetHostname(const CS_STRING &s)
sets the hostname to connect to
Definition: Csocket.h:1306
void SetPemPass(const CS_STRING &s)
set the pemfile pass
Definition: Csocket.h:1422
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:1018
void SetSockName(const CS_STRING &sSockName)
sets the sock name for later reference (ie FindSockByName)
Definition: Csocket.h:1400
virtual void ReadData(const char *data, size_t len)
Ready to read data event.
Definition: Csocket.h:984
socklen_t GetSockAddrLen()
Definition: Csocket.h:235
int CS_GetAddrInfo(const CS_STRING &sHostname, Csock *pSock, CSSockAddr &csSockAddr)
backwards compatible wrapper around CGetAddrInfo and gethostbyname
virtual void ReadLine(const CS_STRING &sLine)
Ready to Read a full line event.
Definition: Csocket.h:989
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:1043
virtual void Disconnected()
Disconnected event.
Definition: Csocket.h:976
sockaddr_in6 * GetSockAddr6()
Definition: Csocket.h:240
void SetPort(uint16_t i)
sets the port to connect to
Definition: Csocket.h:1312
sockaddr wrapper.
Definition: Csocket.h:206
void SetAFRequire(CSSockAddr::EAFRequire iAFRequire)
Definition: Csocket.h:1132
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:1343
bool HasReadLine() const
returns the value of m_bEnableReadLine, if ReadLine is enabled
Definition: Csocket.h:994
CSockCommon()
Definition: Csocket.h:512
CSSockAddr()
Definition: Csocket.h:209
EDNSLType
Definition: Csocket.h:1109
const CS_STRING & GetPemPass() const
Definition: Csocket.h:1302
TSocketManager()
Definition: Csocket.h:1658
CSListener(uint16_t iPort, const CS_STRING &sBindHost="", bool bDetach=false)
Definition: Csocket.h:1363
void TFD_ZERO(fd_set *set)
wrappers for FD_SET and such to work in templates.
Definition: Csocket.h:362
bool GetIsSSL() const
Definition: Csocket.h:1294
bool GetDetach() const
Definition: Csocket.h:1380
EAFRequire
Definition: Csocket.h:221
int(* FPCertVerifyCB)(int, X509_STORE_CTX *)
Definition: Csocket.h:550
void SetDHParamLocation(const CS_STRING &s)
set the location of the dhparamfile
Definition: Csocket.h:1420
void SetBindHost(const CS_STRING &sBindHost)
Definition: Csocket.h:1107
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:952
void FollowSSLCipherServerPreference()
select the ciphers in server-preferred order
Definition: Csocket.h:896
void SetCertVerifyCB(FPCertVerifyCB pFP)
setting this to NULL will allow the default openssl verification process kick in
Definition: Csocket.h:921
int m_iTimeout
Definition: Csocket.h:1332
const CS_STRING & GetSockName() const
Definition: Csocket.h:1290
in6_addr * GetAddr6()
Definition: Csocket.h:241
void SetIsSSL(bool b)
set to true to enable SSL
Definition: Csocket.h:1316
void TFD_SET(cs_sock_t iSock, fd_set *set)
Definition: Csocket.h:367
virtual void ConnectionRefused()
Connection Refused Event.
Definition: Csocket.h:1024
Best class to use to interact with the sockets.
Definition: Csocket.h:1476
bool GetIsSSL() const
Definition: Csocket.h:1384
void DisableMonitor()
causes this monitor to be removed
Definition: Csocket.h:495
int GetMaxConns() const
returns the number of max pending connections when type is LISTENER
Definition: Csocket.h:1173
int GetCsockSSLIdx()
This returns the [ex_]data index position for SSL objects only.
std::map< cs_sock_t, short > m_miiMonitorFDs
Definition: Csocket.h:500
void SetPemLocation(const CS_STRING &s)
set the location of the pemfile
Definition: Csocket.h:1324
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
CS_STRING m_sPemPass
Definition: Csocket.h:1336
std::vector< CCron * > m_vcCrons
Definition: Csocket.h:544
int GetSockError()
Definition: Csocket.h:352
Class to tie sockets to for monitoring by Csocket at either the Csock or TSockManager.
Definition: Csocket.h:458
EMessages
Definition: Csocket.h:1486
Definition: Csocket.h:324
~CSCharBuffer()
Definition: Csocket.h:191
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:377
void SetRequiresClientCert(bool b)
set to true if require a client certificate (deprecated
Definition: Csocket.h:1424
void SetIsSSL(bool b)
set to true to enable SSL
Definition: Csocket.h:1404
uint32_t GetRequireClientCertFlags() const
Definition: Csocket.h:1394
sockaddr_in * GetSockAddr()
Definition: Csocket.h:236
const std::vector< CCron * > & GetCrons() const
returns a const reference to the crons associated to this socket
Definition: Csocket.h:519