1 #ifndef TCP_H
2 #define TCP_H
3 
4 // Misnamed really.  This class does all the common functionality between SMTP
5 // and POP clients.
6 
7 using namespace std;
8 #include "postal.h"
9 #include <sys/poll.h>
10 #include <string>
11 #include "cmd5.h"
12 #include "thread.h"
13 #include <sys/types.h>
14 #include <netinet/in.h>
15 
16 class Logit;
17 class address;
18 
19 #ifdef USE_SSL
20 #ifdef USE_OPENSSL
21 #ifndef TCP_BODY
22 struct SSL_METHOD;
23 struct SSL_CTX;
24 struct SSL;
25 struct X509;
26 #else // TCP_BODY
27 #include <openssl/crypto.h>
28 #include <openssl/x509.h>
29 #include <openssl/pem.h>
30 #include <openssl/ssl.h>
31 #include <openssl/err.h>
32 #endif // TCP_BODY
33 #else // USE_OPENSSL
34 #include <gnutls/gnutls.h>
35 #endif // USE_OPENSSL
36 #endif // USE_SSL
37 
38 class tcp : public Thread
39 {
40 public:
41   tcp(int *exitCount, const char *addr, unsigned short default_port, Logit *log
42 #ifdef USE_SSL
43     , int ssl
44 #endif
45     , const char *sourceAddr, Logit *debug);
46   tcp(int threadNum, const tcp *parent);
47   virtual ~tcp();
48 
49   // connect returns 0 for connect, 1 for can't connect, and 2 for serious
50   // errors.
51   int Connect(short port = 0);
52 #ifdef USE_SSL
53   // after calling Connect() and getting high-level protocol ready call
54   // ConnectTLS() to start TLS.
55   int ConnectTLS();
56 #endif
57   int sendMsg();
58   virtual int disconnect();
59   int doAllWork(int rate);
60 
61 protected:
62   virtual int pollRead() = 0;
63   virtual int WriteWork(PVOID buf, int size, int timeout) = 0;
64 
65   int readLine(char *buf, int bufSize);
66   ERROR_TYPE sendData(CPCCHAR buf, int size);
sendString(const string & s)67   ERROR_TYPE sendString(const string &s) { return sendData(s.c_str(), s.size()); }
68   void endIt();
69   virtual void error() = 0;
70   virtual void sentData(int bytes) = 0;
71   virtual void receivedData(int bytes) = 0;
72   Cmd5 m_md5;
73 
74   virtual ERROR_TYPE readCommandResp(bool important = true) = 0;
75   virtual ERROR_TYPE sendCommandData(const char *buf, int size, bool important = true);
76   virtual ERROR_TYPE sendCommandString(const string &str, bool important = true);
77 
78   int m_destAffinity;
79   Logit *m_log;
80   struct sockaddr_in m_connectionLocalAddr;
81 #ifdef USE_SSL
82   bool m_canTLS;
83   int m_useTLS;
84 #endif
85   int *m_exitCount;
86 
87 private:
88   int m_fd;
89   pollfd m_poll;
90   char m_buf[4096];
91   int m_start;
92   int m_end;
93   bool m_open;
94   address *m_addr; // destination
95   address *m_sourceAddr;
96   // debug file management object (NULL if no debugging)
97   // if used a new Logit object is created for each instance unlike the
98   // m_log instance
99   Logit *m_debug;
100 
101 #ifdef USE_SSL
102 #ifdef USE_OPENSSL
103 #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10000000L
104   const SSL_METHOD *m_sslMeth;
105 #else
106   SSL_METHOD *m_sslMeth;
107 #endif
108   SSL_CTX* m_sslCtx;
109   SSL *m_ssl;
110 #else
111   gnutls_session_t *m_gnutls_session;
112   gnutls_anon_client_credentials_t m_gnutls_anoncred;
113 #endif
114   bool m_isTLS;
115 #endif
116 
117   tcp(const tcp&);
118   tcp & operator=(const tcp&);
119 };
120 
121 #endif
122