-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtility
More file actions
443 lines (440 loc) · 20.8 KB
/
Utility
File metadata and controls
443 lines (440 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* -*- c++ -*- */
///////////////////////////////////////////
// Utility
// -------------------------------------
// file : Utility
// author : Ben Kietzman
// begin : 2013-11-30
// copyright : Ben Kietzman
// email : ben@kietzman.org
///////////////////////////////////////////
/*! \file Utility
* \brief Utility Class
*/
#ifndef _COMMON_UTILITY_
#define _COMMON_UTILITY_
// {{{ includes
#include <cerrno>
#include <csignal>
#include <cstring>
#include <ctime>
#include <fcntl.h>
#include <fstream>
#include <functional>
#include <iostream>
#include <mutex>
#include <netdb.h>
#include <openssl/conf.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <poll.h>
#ifdef COMMON_SOLARIS
#include <procfs.h>
#endif
#include <string>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <zlib.h>
using namespace std;
#include "File"
#include "Json"
// }}}
extern "C++"
{
namespace common
{
enum common_socket_type {COMMON_SOCKET_UNKNOWN, COMMON_SOCKET_ENCRYPTED, COMMON_SOCKET_UNENCRYPTED, COMMON_SOCKET_INVALID};
// {{{ Utility
//! Contains utility tools.
/*!
* Provides utility tools.
*/
class Utility
{
private:
bool m_bSslInit;
size_t m_unReadSize;
size_t m_unSslWriteSize;
std::mutex m_mutexConf;
string m_strProxyPort;
string m_strProxyServer;
time_t m_ulModifyTime;
File m_file;
public:
Json *m_conf;
string m_strConf;
Utility(string &strError);
~Utility();
/*! \fn Json *conf()
* \brief Interfaces with the configuration storage.
* \return Returns a pointer to the configuration storage.
*/
Json *conf();
/*! \fn bool connect(const string strServer, const string strPort, int &fdSocket, string &strError, const bool bProxy = false)
* \brief Connect to a server and port.
* \param strServer Contains the server.
* \param strPort Contains the port.
* \param fdSocket Contains the socket.
* \param strError Contains the error.
* \param bProxy Contains the proxy bolean value.
* \param unSocketTimeout Contains the socket timeout.
* \param unProxyTimeout Contains the proxy timeout.
* \return Returns a boolean true/false value.
*/
bool connect(const string strServer, const string strPort, int &fdSocket, string &strError, const bool bProxy = false, const size_t unSocketTimeout = 5000, const size_t unProxyTimeout = 5000);
/*! \fn bool connect(const string strServer, const string strPort, const string strProxyServer, const string strProxyPort, int &fdSocket, string &strError)
* \brief Connect to a server and port via a proxy.
* \param strServer Contains the server.
* \param strPort Contains the port.
* \param strProxyServer Contains the proxy server.
* \param strProxyPort Contains the proxy port.
* \param fdSocket Contains the socket.
* \param strError Contains the error.
* \param unSocketTimeout Contains the socket timeout.
* \param unProxyTimeout Contains the proxy timeout.
* \return Returns a boolean true/false value.
*/
bool connect(const string strServer, const string strPort, const string strProxyServer, const string strProxyPort, int &fdSocket, string &strError, const size_t unSocketTimeout = 5000, const size_t unProxyTimeout = 5000);
/*! \fn void daemonize()
* \brief Turns the process into a system daemon.
*/
void daemonize();
/*! \fn bool fdBlocking(int fdSocket, string &strError)
* \brief Set file descriptor into blocking mode.
* \param fdSocket File descriprot.
* \param strError Contains the error.
* \return Returns a boolean true/false value.
*/
bool fdBlocking(int fdSocket, string &strError);
/*! \fn bool fdNonBlocking(int fdSocket, string &strError)
* \brief Set file descriptor into non-blocking mode.
* \param fdSocket File descriprot.
* \param strError Contains the error.
* \return Returns a boolean true/false value.
*/
bool fdNonBlocking(int fdSocket, string &strError);
/*! \fn bool fdRead(int fdSocket, string &strBuffer, int &nReturn)
* \brief Read data from file descriptor and appends it onto the existing strBuffer.
* \param fdSocket File descriprot.
* \param strBuffer Appended data buffer.
* \param nReturn The number of bytes read.
* \return Returns a boolean true/false value of whether the file descriptor is still good.
*/
bool fdRead(int fdSocket, string &strBuffer, int &nReturn);
/*! \fn bool fdRead(int fdSocket, string &strBuffer, string &strRead, int &nReturn)
* \brief Read data from file descriptor and appends it onto the existing strBuffer.
* \param fdSocket File descriprot.
* \param strBuffer Appended data buffer.
* \param strRead Assigned data buffer.
* \param nReturn The number of bytes read.
* \return Returns a boolean true/false value of whether the file descriptor is still good.
*/
bool fdRead(int fdSocket, string &strBuffer, string &strRead, int &nReturn);
/*! \fn bool fdWrite(int fdSocket, string &strBuffer, int &nReturn)
* \brief Read data from file descriptor and appends it onto the existing strBuffer.
* \param fdSocket File descriprot.
* \param strBuffer Data buffer.
* \param nReturn The number of bytes read.
* \return Returns a boolean true/false value of whether the file descriptor is still good.
*/
bool fdWrite(int fdSocket, string &strBuffer, int &nReturn);
/*! \fn string getConfPath()
* \brief Gets the configuration storage path.
* \return Returns the configuration storage path.
*/
string getConfPath();
/*! \fn bool getLine(FILE *pFile, string &strLine)
* \brief Reads a line of data from a socket.
* \param pFile Contains the file handle.
* \param strLine Contains the newly read line.
* \return Returns a boolean true/false value.
*/
bool getLine(FILE *pFile, string &strLine);
/*! \fn bool getLine(gzFile pgzFile, string &strLine)
* \brief Reads a line of data from a zlib compressed file.
* \param pgzFile Contains the file handle.
* \param strLine Contains the newly read line.
* \return Returns a boolean true/false value.
*/
bool getLine(gzFile pgzFile, string &strLine);
/*! \fn bool getLine(int fdFile, string &strLine, const time_t CTimeout, int &nReturn)
* \brief Reads a line of data from a file/socket.
* \param fdFile Contains the file descriptor.
* \param strLine Contains the newly read line.
* \param CTimeout Contains the timeout.
* \param nReturn Contains the returned value from read.
* \return Returns a boolean true/false value.
*/
bool getLine(int fdFile, string &strLine, const time_t CTimeout, int &nReturn);
/*! \fn bool getLine(int fdFile, string &strLine, int &nReturn)
* \brief Reads a line of data from a file/socket.
* \param fdFile Contains the file descriptor.
* \param strLine Contains the newly read line.
* \param nReturn Contains the returned value from read.
* \return Returns a boolean true/false value.
*/
bool getLine(int fdFile, string &strLine, int &nReturn);
/*! \fn bool getLine(int fdFile, string &strLine)
* \brief Reads a line of data from a file/socket.
* \param fdFile Contains the file descriptor.
* \param strLine Contains the newly read line.
* \return Returns a boolean true/false value.
*/
bool getLine(int fdFile, string &strLine);
/*! \fn bool getLine(ifstream &inFile, string &strLine)
* \brief Reads a line of data from a socket.
* \param inFile Contains the file handle.
* \param strLine Contains the newly read line.
* \return Returns a boolean true/false value.
*/
bool getLine(ifstream &inFile, string &strLine);
/*! \fn bool getLine(istream &inStream, string &strLine)
* \brief Reads a line of data from a stream.
* \param inStream Contains the stream.
* \param strLine Contains the newly read line.
* \return Returns a boolean true/false value.
*/
bool getLine(istream &inStream, string &strLine);
/*! \fn bool getLine(SSL *ssl, string &strLine, const time_t CTimeout, int &nReturn)
* \brief Reads a line of data from a socket.
* \param ssl Contains the SSL handle.
* \param strLine Contains the newly read line.
* \param CTimeout Contains the timeout.
* \param nReturn Contains the returned value from SSL_read.
* \return Returns a boolean true/false value.
*/
bool getLine(SSL *ssl, string &strLine, const time_t CTimeout, int &nReturn);
/*! \fn bool getLine(SSL *ssl, string &strLine, int &nReturn)
* \brief Reads a line of data from a socket.
* \param ssl Contains the SSL handle.
* \param strLine Contains the newly read line.
* \param nReturn Contains the returned value from SSL_read.
* \return Returns a boolean true/false value.
*/
bool getLine(SSL *ssl, string &strLine, int &nReturn);
/*! \fn bool getLine(SSL *ssl, string &strLine)
* \brief Reads a line of data from a socket.
* \param ssl Contains the SSL handle.
* \param strLine Contains the newly read line.
* \return Returns a boolean true/false value.
*/
bool getLine(SSL *ssl, string &strLine);
/*! \fn bool getLine(stringstream &ssData, string &strLine)
* \brief Reads a line of data from a stream.
* \param ssData Contains the string stream.
* \param strLine Contains the newly read line.
* \return Returns a boolean true/false value.
*/
bool getLine(stringstream &ssData, string &strLine);
/*! \fn bool isProcessAlreadyRunning(const string strProcess)
* \brief Determines if another process of the same name is already running.
* \param strProcess Contains process name.
* \return Returns a boolean true/false value.
*/
bool isProcessAlreadyRunning(const string strProcess);
/*! \fn void msleep(const unsigned long ulMilliSec)
* \brief Sleep given milliseconds.
* \param ulMilliSec Contains the milliseconds.
*/
void msleep(const unsigned long ulMilliSec);
/*! \fn bool readConf(string &strError)
* \brief Read configuration file.
* \param strError Contains the error message.
* \return Returns a boolean true/false value.
*/
bool readConf(string &strError);
/*! \fn void setConfPath(const string strPath, string &strError)
* \brief Sets the configuration storage path.
* \param strPath Contains the path.
* \param strError Contains the error message.
*/
void setConfPath(const string strPath, string &strError);
/*! \fn void setProxy(const string strServer, const string strPort)
* \brief Sets the proxy.
* \param strServer Contains the server.
* \param strPort Contains the port.
*/
void setProxy(const string strServer, const string strPort);
/*! \fn void setReadSize(const size_t unSize)
* \brief Sets the read size.
* \param unSize Contains the size.
*/
void setReadSize(const size_t unSize);
/*! \fn void setSslWriteSize(const size_t unSize)
* \brief Sets the SSL write size.
* \param unSize Contains the size.
*/
void setSslWriteSize(const size_t unSize);
/*! \fn bool socketType(int fdSocket, common_socket_type &eType, string &strError)
* \brief Take a peek of the current socket to determine if it's a regular or encrypted one.
* \param fdSocket Contains the socket descriptor.
* \param eType Returns an enum indicating if the socket is encrypted, unencrypted, or invalid.
* \param strError Contains the error.
* \return Returns a boolean true/false value.
* - SOC_UNENCRYPTED: This is a regular unencrypted socket.
* - SOC_ENCRYPTED: This is an SSL encrypted socket;
* - SOC_INVALID: Error occurred or the peer already closed the socket.
*/
bool socketType(int fdSocket, common_socket_type &eType, string &strError);
/*! \fn bool socketType(int fdSocket, common_socket_type &eType, bool &bRetry, string &strError)
* \brief Take a peek of the current socket to determine if it's a regular or encrypted one.
* \param fdSocket Contains the socket descriptor.
* \param eType Returns an enum indicating if the socket is encrypted, unencrypted, or invalid.
* \param bRetry Contains the retry.
* \param strError Contains the error.
* \return Returns a boolean true/false value.
* - SOC_UNENCRYPTED: This is a regular unencrypted socket.
* - SOC_ENCRYPTED: This is an SSL encrypted socket;
* - SOC_INVALID: Error occurred or the peer already closed the socket.
*/
bool socketType(int fdSocket, common_socket_type &eType, bool &bRetry, string &strError);
/*! \fn SSL *sslAccept(SSL_CTX *ctx, int fdSocket, string &strError)
* \brief OpenSSL SSL_accept() wrapper function.
* \param ctx Contains the SSL context.
* \param fdSocket Contains the socket descriptor.
* \param strError Returns an error message.
* \return SSL contains the SSL handle.
*/
SSL *sslAccept(SSL_CTX *ctx, int fdSocket, string &strError);
/*! \fn SSL *sslAccept(SSL_CTX *ctx, int fdSocket, bool &bRetry, string &strError)
* \brief OpenSSL SSL_accept() wrapper function.
* \param ctx Contains the SSL context.
* \param fdSocket Contains the socket descriptor.
* \param bRetry Contains the retry.
* \param strError Returns an error message.
* \return SSL contains the SSL handle.
*/
SSL *sslAccept(SSL_CTX *ctx, int fdSocket, bool &bRetry, string &strError);
/*! \fn SSL *sslConnect(SSL_CTX *ctx, int fdSocket, string &strError)
* \brief OpenSSL SSL_connect() wrapper function.
* \param ctx Contains the SSL context.
* \param fdSocket Contains the socket descriptor.
* \param strError Returns an error message.
* \return SSL contains the SSL handle.
*/
SSL *sslConnect(SSL_CTX *ctx, int fdSocket, string &strError);
/*! \fn SSL *sslConnect(SSL_CTX *ctx, int fdSocket, bool &bRetry, string &strError)
* \brief OpenSSL SSL_connect() wrapper function.
* \param ctx Contains the SSL context.
* \param fdSocket Contains the socket descriptor.
* \param bRetry Contains the retry.
* \param strError Returns an error message.
* \return SSL contains the SSL handle.
*/
SSL *sslConnect(SSL_CTX *ctx, int fdSocket, bool &bRetry, string &strError);
/*! \fn SSL *sslConnect(SSL *ssl, bool &bRetry, string &strError)
* \brief OpenSSL SSL_connect() retry wrapper function.
* \param ssl Contains the SSL handle.
* \param bRetry Contains the retry.
* \param strError Returns an error message.
* \return SSL contains the SSL handle.
*/
SSL *sslConnect(SSL *ssl, bool &bRetry, string &strError);
/*! \fn void sslDeinit()
* \brief OpenSSL deinitialization.
*/
void sslDeinit();
/*! \fn void sslInit()
* \brief OpenSSL initialization.
*/
void sslInit();
/*! \fn SSL_CTX *sslInit(const bool bSslServer, string &strError, const bool bVerifyPeer = false)
* \brief OpenSSL initialization.
* \param bSslServer A boolean indicates if the current program is an SSL server or client. If true, the program is an SSL server; otherwise, it's an SSL client.
* \param strError Returns an error message.
* \param bVerifyPeer Contains the verify peer true/false value.
* \return SSL_CTX contains the SSL context for establishing an SSL socket connection.
*/
SSL_CTX *sslInit(const bool bSslServer, string &strError, const bool bVerifyPeer = false);
/*! \fn SSL_CTX *sslInitClient(string &strError, const bool bVerifyPeer = false)
* \brief OpenSSL initialization for a client.
* \param strError Returns an error message.
* \param bVerifyPeer Contains the verify peer true/false value.
* \return SSL_CTX contains the SSL context for establishing an SSL socket connection.
*/
SSL_CTX *sslInitClient(string &strError, const bool bVerifyPeer = false);
/*! \fn SSL_CTX *sslInitClient(const string strCertificateChain, const string strPrivateKey, string &strError, const bool bVerifyPeer = false)
* \brief OpenSSL initialization for a server.
* \param strCertificateChain Contains the certificate chain.
* \param strPrivateKey Contains the private key.
* \param strError Returns an error message.
* \param bVerifyPeer Contains the verify peer true/false value.
* \return SSL_CTX contains the SSL context for establishing an SSL socket connection.
*/
SSL_CTX *sslInitClient(const string strCertificateChain, const string strPrivateKey, string &strError, const bool bVerifyPeer = false);
/*! \fn SSL_CTX *sslInitServer(string &strError, const bool bVerifyPeer = false)
* \brief OpenSSL initialization for a server.
* \param strError Returns an error message.
* \param bVerifyPeer Contains the verify peer true/false value.
* \return SSL_CTX contains the SSL context for establishing an SSL socket connection.
*/
SSL_CTX *sslInitServer(string &strError, const bool bVerifyPeer = false);
/*! \fn SSL_CTX *sslInitServer(const string strCertificateChain, const string strPrivateKey, string &strError, const bool bVerifyPeer = false)
* \brief OpenSSL initialization for a server.
* \param strCertificateChain Contains the certificate chain.
* \param strPrivateKey Contains the private key.
* \param strError Returns an error message.
* \param bVerifyPeer Contains the verify peer true/false value.
* \return SSL_CTX contains the SSL context for establishing an SSL socket connection.
*/
SSL_CTX *sslInitServer(const string strCertificateChain, const string strPrivateKey, string &strError, const bool bVerifyPeer = false);
/*! \fn bool sslLoadCertChainKey(SSL_CTX *ctx, const string strCertificateChain, const string strPrivateKey, string &strError)
* \brief Loads the SSL certificate chain and private key into the SSL CTX and then checks their consistency.
* \param ctx Contains the SSL context.
* \param strCertificateChain Contains the certificate chain.
* \param strPrivateKey Contains the private key.
* \return Returns a boolean true/false value.
*/
bool sslLoadCertChainKey(SSL_CTX *ctx, const string strCertificateChain, const string strPrivateKey, string &strError);
/*! \fn bool sslRead(SSL *ssl, string &strBuffer, int &nReturn)
* \brief Read data from SSL handle and appends it onto the existing strBuffer.
* \param ssl SSL handle.
* \param strBuffer Appended data buffer.
* \param nReturn The number of bytes read.
* \return Returns a boolean true/false value of whether the SSL handle is still good.
*/
bool sslRead(SSL *ssl, string &strBuffer, int &nReturn);
/*! \fn bool sslRead(SSL *ssl, string &strBuffer, string &strRead, int &nReturn)
* \brief Read data from SSL handle and appends it onto the existing strBuffer.
* \param ssl SSL handle.
* \param strBuffer Appended data buffer.
* \param strRead Assigned data buffer.
* \param nReturn The number of bytes read.
* \return Returns a boolean true/false value of whether the SSL handle is still good.
*/
bool sslRead(SSL *ssl, string &strBuffer, string &strRead, int &nReturn);
/*! \fn string sslstrerror()
* \brief Obtain an SSL error message.
* \return Returns the error string.
*/
string sslstrerror();
/*! \fn string sslstrerror(SSL *ssl, int nReturn)
* \brief Obtain an SSL error message.
* \param ssl Contains the SSL handle.
* \param nReturn Contains the return.
* \return Returns the error string.
*/
string sslstrerror(SSL *ssl, int nReturn);
/*! \fn string sslstrerror(SSL *ssl, int nReturn, bool &bRetry)
* \brief Obtain an SSL error message.
* \param ssl Contains the SSL handle.
* \param nReturn Contains the return.
* \param bRetry Contains the retry.
* \return Returns the error string.
*/
string sslstrerror(SSL *ssl, int nReturn, bool &bRetry);
/*! \fn bool sslWrite(SSL *ssl, string &strBuffer, int &nReturn)
* \brief Read data from SSL handle and appends it onto the existing strBuffer.
* \param ssl SSL handle.
* \param strBuffer Data buffer.
* \param nReturn The number of bytes read.
* \return Returns a boolean true/false value of whether the SSL handle is still good.
*/
bool sslWrite(SSL *ssl, string &strBuffer, int &nReturn);
};
// }}}
}
}
#endif