Debian Codegraph @

src/Socket.hpp

raw
1 // Socket class - implements BaseSocket for INET domain sockets
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 #ifndef __HPP_SOCKET
8 #define __HPP_SOCKET
9
10 // INCLUDES
11
12 #include "BaseSocket.hpp"
13 #include "String.hpp"
14 #include <sstream>
15 #include <iomanip>
16
17 #include "openssl/ssl.h"
18 #include "String.hpp"
19
20 // DECLARATIONS
21
22 class Socket : public BaseSocket
23 {
24 friend class FDTunnel;
25
26 public:
27 // create INET socket & clear address structs
28 Socket();
29 // create socket using pre-existing FD (address structs will be empty!)
30 Socket(int fd);
31 // create socket from pre-existing FD, storing given local & remote IPs
32 Socket(int newfd, struct sockaddr_in myip, struct sockaddr_in peerip);
33 ~Socket();
34
35 // connect to given IP & port (following default constructor)
36 int connect(const std::string &ip, int port);
37
38 // bind to given port
39 int bind(int port);
40 // bind to given IP & port, for machines with multiple NICs
41 int bind(const std::string &ip, int port);
42
43 // accept incoming connections & return new Socket
44 Socket *accept();
45
46 // close socket & clear address structs
47 void reset();
48
49 // get remote IP/port
50 std::string getPeerIP();
51 int getPeerSourcePort();
52 int getPort();
53 void setPort(int port);
54 void setClientAddr( std::string ip, int port);
55 unsigned long int getPeerSourceAddr();
56
57 std::string down_thread_id;
58
59 // get local IP
60 std::string getLocalIP();
61 int getLocalPort();
62
63 // Chunked i/o
64 String chunked_trailer;
65 long int chunk_to_read;
66 bool writeChunk( char *buffout, int len, int timeout);
67 bool writeChunkTrailer( String &trailer);
68 int readChunk( char *buffout, int maxlen, int timeout);
69 int loopChunk(int timeout);
70 int drainChunk(int timeout);
71 void resetChunk();
72 short int get_wait_flag(bool write_flag);
73
74 //flags for use with ssl only
75 bool s_read_wants_read = false;
76 bool s_read_wants_write = false;
77 bool s_write_wants_read = false;
78 bool s_write_wants_write = false;
79 bool s_shutdown_wants_read = false;
80 bool s_shutdown_wants_write = false;
81 int s_pending = 0;
82 bool s_has_pending = false;
83 bool s_prev_has_pending = false;
84
85 bool s_checkPending();
86 int s_checkShutdown();
87
88 bool chunkError;
89
90 //use this socket as an ssl server
91 int startSslClient(const std::string &certPath, String hostname);
92
93 //is this a SSL connection
94 bool isSsl();
95
96 bool isSslServer();
97
98 //shuts down the current ssl connection
99 void stopSsl();
100
101 //cleans up ssl
102 void cleanSsl();
103
104 //check that everything in this certificate is correct appart from the hostname
105 long checkCertValid(String &hostname);
106
107 //check the common name and altnames of a certificate against hostname
108 int checkCertHostname(const std::string &hostame);
109
110 void close();
111
112 //use this socket as an ssl server
113 int startSslServer(X509 *x, EVP_PKEY *privKey, std::string &set_cipher);
114
115 // blocking check, can break on config reloads
116 //void readyForOutput(int timeout, bool honour_reloadconfig = false);
117
118 // non-blocking check for input data
119 bool checkForInput(int timeout = 20);
120 //bool bcheckForInput(int timeout);
121 bool ssl_poll_wait(int eno, int timeout);
122
123 // get a line from the socket - can break on config reloads
124 int getLine(char *buff, int size, int timeout, bool *chopped = NULL, bool *truncated = NULL) ;
125
126 // write buffer to string
127 bool writeString(const char *line);
128 bool writeString(std::string line);
129
130 // write buff to socket - blocking
131 bool writeToSocket(const char *buff, int len, unsigned int flags, int timeout);
132
133 // write buff to socket non-blocking - returns number of bytes written or 0 if would block or -1 on error
134 int writeToSocketNB(const char *buff, int len, unsigned int flags);
135
136 // read from socket, returning number of bytes read
137 int readFromSocket(char *buff, int len, unsigned int flags, int timeout, bool ret_part = false);
138
139 bool getIeof();
140
141 private:
142 SSL *ssl = NULL;
143 SSL_CTX *ctx = NULL;
144 bool isssl = false;
145 bool issslserver = false;
146
147 // local & remote addresses
148 struct sockaddr_in my_adr;
149 struct sockaddr_in peer_adr;
150 int my_port = 0;
151 std::string my_addr;
152 int client_port = 0;
153 std::string client_addr;
154 bool ieof = false;
155 };
156
157 #endif