-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.h
518 lines (442 loc) · 14.5 KB
/
Utils.h
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* Copyright (C) 2004-2011 See the AUTHORS file for details.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#ifndef _UTILS_H
#define _UTILS_H
#include "zncconfig.h"
#include "ZNCString.h"
#include <assert.h>
#include <cstdio>
#include <fcntl.h>
#include <map>
#include <sys/file.h>
#include <sys/time.h>
#include <unistd.h>
#include <vector>
#include <iostream>
using std::map;
using std::vector;
using std::pair;
using std::cout;
using std::endl;
/** Output a debug info if debugging is enabled.
* If ZNC was compiled with <code>--enable-debug</code> or was started with
* <code>--debug</code>, the given argument will be sent to stdout.
*
* You can use all the features of C++ streams:
* @code
* DEBUG("I had " << errors << " errors");
* @endcode
*
* @param f The expression you want to display.
*/
#define DEBUG(f) do { \
if (CUtils::Debug()) { \
cout << f << endl; \
} \
} while (0)
static inline void SetFdCloseOnExec(int fd)
{
int flags = fcntl(fd, F_GETFD, 0);
if (flags < 0)
return; // Ignore errors
// When we execve() a new process this fd is now automatically closed.
fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
}
static const char g_HexDigits[] = "0123456789abcdef";
class CUtils {
public:
CUtils();
~CUtils();
static CString GetIP(unsigned long addr);
static unsigned long GetLongIP(const CString& sIP);
static void SetStdoutIsTTY(bool b) { stdoutIsTTY = b; }
static bool StdoutIsTTY() { return stdoutIsTTY; }
static void SetDebug(bool b) { debug = b; }
static bool Debug() { return debug; }
static void PrintError(const CString& sMessage);
static void PrintMessage(const CString& sMessage, bool bStrong = false);
static void PrintPrompt(const CString& sMessage);
static void PrintAction(const CString& sMessage);
static void PrintStatus(bool bSuccess, const CString& sMessage = "");
static const CString sDefaultHash;
static CString GetSaltedHashPass(CString& sSalt);
static CString GetSalt();
static CString SaltedMD5Hash(const CString& sPass, const CString& sSalt);
static CString SaltedSHA256Hash(const CString& sPass, const CString& sSalt);
static CString GetPass(const CString& sPrompt);
static bool GetInput(const CString& sPrompt, CString& sRet, const CString& sDefault = "", const CString& sHint = "");
static bool GetBoolInput(const CString& sPrompt, bool bDefault);
static bool GetBoolInput(const CString& sPrompt, bool *pbDefault = NULL);
static bool GetNumInput(const CString& sPrompt, unsigned int& uRet, unsigned int uMin = 0, unsigned int uMax = ~0, unsigned int uDefault = ~0);
static unsigned long long GetMillTime() {
struct timeval tv;
unsigned long long iTime = 0;
gettimeofday(&tv, NULL);
iTime = (unsigned long long) tv.tv_sec * 1000;
iTime += ((unsigned long long) tv.tv_usec / 1000);
return iTime;
}
#ifdef HAVE_LIBSSL
static void GenerateCert(FILE *pOut, const CString& sHost = "");
#endif /* HAVE_LIBSSL */
private:
protected:
static bool stdoutIsTTY;
static bool debug;
};
class CException {
public:
typedef enum {
EX_Shutdown,
EX_Restart
} EType;
CException(EType e) {
m_eType = e;
}
virtual ~CException() {}
EType GetType() const { return m_eType; }
private:
protected:
EType m_eType;
};
/** Generate a grid-like output from a given input.
*
* @code
* CTable table;
* table.AddColumn("a");
* table.AddColumn("b");
* table.AddRow();
* table.SetCell("a", "hello");
* table.SetCell("b", "world");
*
* unsigned int idx = 0;
* CString tmp;
* while (table.GetLine(idx++, tmp)) {
* // Output tmp somehow
* }
* @endcode
*
* The above code would generate the following output:
* @verbatim
+-------+-------+
| a | b |
+-------+-------+
| hello | world |
+-------+-------+@endverbatim
*/
class CTable : protected vector<vector<CString> > {
public:
CTable() {}
virtual ~CTable() {}
/** Adds a new column to the table.
* Please note that you should add all columns before starting to fill
* the table!
* @param sName The name of the column.
* @return false if a column by that name already existed.
*/
bool AddColumn(const CString& sName);
/** Adds a new row to the table.
* After calling this you can fill the row with content.
* @return The index of this row
*/
unsigned int AddRow();
/** Sets a given cell in the table to a value.
* @param sColumn The name of the column you want to fill.
* @param sValue The value to write into that column.
* @param uRowIdx The index of the row to use as returned by AddRow().
* If this is not given, the last row will be used.
* @return True if setting the cell was successful.
*/
bool SetCell(const CString& sColumn, const CString& sValue, unsigned int uRowIdx = ~0);
/** Get a line of the table's output
* @param uIdx The index of the line you want.
* @param sLine This string will receive the output.
* @return True unless uIdx is past the end of the table.
*/
bool GetLine(unsigned int uIdx, CString& sLine) const;
/** Return the width of the given column.
* Please note that adding and filling new rows might change the
* result of this function!
* @param uIdx The index of the column you are interested in.
* @return The width of the column.
*/
unsigned int GetColumnWidth(unsigned int uIdx) const;
/// Completely clear the table.
void Clear();
/// @return The number of rows in this table, not counting the header.
using vector<vector<CString> >::size;
/// @return True if this table doesn't contain any rows.
using vector<vector<CString> >::empty;
private:
unsigned int GetColumnIndex(const CString& sName) const;
protected:
vector<CString> m_vsHeaders;
map<CString, unsigned int> m_msuWidths; // Used to cache the width of a column
};
#ifdef HAVE_LIBSSL
#include <openssl/blowfish.h>
#include <openssl/md5.h>
//! does Blowfish w/64 bit feedback, no padding
class CBlowfish {
public:
/**
* @param sPassword key to encrypt with
* @param iEncrypt encrypt method (BF_DECRYPT or BF_ENCRYPT)
* @param sIvec what to set the ivector to start with, default sets it all 0's
*/
CBlowfish(const CString & sPassword, int iEncrypt, const CString & sIvec = "");
~CBlowfish();
//! output must be freed
static unsigned char *MD5(const unsigned char *input, u_int ilen);
//! returns an md5 of the CString (not hex encoded)
static CString MD5(const CString & sInput, bool bHexEncode = false);
//! output must be the same size as input
void Crypt(unsigned char *input, unsigned char *output, u_int ibytes);
//! must free result
unsigned char * Crypt(unsigned char *input, u_int ibytes);
CString Crypt(const CString & sData);
private:
unsigned char *m_ivec;
BF_KEY m_bkey;
int m_iEncrypt, m_num;
};
#endif /* HAVE_LIBSSL */
/**
* @class TCacheMap
* @author prozac <[email protected]>
* @brief Insert an object with a time-to-live and check later if it still exists
*/
template<typename K, typename V = bool>
class TCacheMap {
public:
TCacheMap(unsigned int uTTL = 5000) {
m_uTTL = uTTL;
}
virtual ~TCacheMap() {}
/**
* @brief This function adds an item to the cache using the default time-to-live value
* @param Item the item to add to the cache
*/
void AddItem(const K& Item) {
AddItem(Item, m_uTTL);
}
/**
* @brief This function adds an item to the cache using a custom time-to-live value
* @param Item the item to add to the cache
* @param uTTL the time-to-live for this specific item
*/
void AddItem(const K& Item, unsigned int uTTL) {
AddItem(Item, V(), uTTL);
}
/**
* @brief This function adds an item to the cache using the default time-to-live value
* @param Item the item to add to the cache
* @param Val The value associated with the key Item
*/
void AddItem(const K& Item, const V& Val) {
AddItem(Item, Val, m_uTTL);
}
/**
* @brief This function adds an item to the cache using a custom time-to-live value
* @param Item the item to add to the cache
* @param Val The value associated with the key Item
* @param uTTL the time-to-live for this specific item
*/
void AddItem(const K& Item, const V& Val, unsigned int uTTL) {
if (!uTTL) { // If time-to-live is zero we don't want to waste our time adding it
RemItem(Item); // Remove the item incase it already exists
return;
}
m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, Val);
}
/**
* @brief Performs a Cleanup() and then checks to see if your item exists
* @param Item The item to check for
* @return true if item exists
*/
bool HasItem(const K& Item) {
Cleanup();
return (m_mItems.find(Item) != m_mItems.end());
}
/**
* @brief Performs a Cleanup() and returns a pointer to the object, or NULL
* @param Item The item to check for
* @return Pointer to the item or NULL if there is no suitable one
*/
V* GetItem(const K& Item) {
Cleanup();
iterator it = m_mItems.find(Item);
if (it == m_mItems.end())
return NULL;
return &it->second.second;
}
/**
* @brief Removes a specific item from the cache
* @param Item The item to be removed
* @return true if item existed and was removed, false if it never existed
*/
bool RemItem(const K& Item) {
return m_mItems.erase(Item);
}
/**
* @brief Cycles through the queue removing all of the stale entries
*/
void Cleanup() {
iterator it = m_mItems.begin();
while (it != m_mItems.end()) {
if (CUtils::GetMillTime() > (it->second.first)) {
m_mItems.erase(it++);
} else {
++it;
}
}
}
/**
* @brief Clear all entries
*/
void Clear() {
m_mItems.clear();
}
// Setters
void SetTTL(unsigned int u) { m_uTTL = u; }
// !Setters
// Getters
unsigned int GetTTL() const { return m_uTTL; }
// !Getters
protected:
typedef pair<unsigned long long, V> value;
typedef typename map<K, value>::iterator iterator;
map<K, value> m_mItems; //!< Map of cached items. The value portion of the map is for the expire time
unsigned int m_uTTL; //!< Default time-to-live duration
};
/**
* @class CSmartPtr
* @author prozac <[email protected]>
* @brief This is a standard reference counting pointer. Be careful not to have two of these point to the same raw pointer or one will be deleted while the other still thinks it is valid.
*/
template<typename T>
class CSmartPtr {
public:
/**
* @brief Standard constructor, points to nothing
*/
CSmartPtr() {
m_pType = NULL;
m_puCount = NULL;
}
/**
* @brief Attach to an existing raw pointer, be CAREFUL not to have more than one CSmartPtr attach to the same raw pointer or bad things will happen
* @param pRawPtr The raw pointer to attach to
*/
CSmartPtr(T* pRawPtr) {
m_pType = NULL;
m_puCount = NULL;
Attach(pRawPtr);
}
/**
* @brief Copy constructor, will copy the raw pointer and counter locations and increment the reference counter
* @param CopyFrom A reference of another CSmartPtr to copy from
*/
CSmartPtr(const CSmartPtr<T>& CopyFrom) {
m_pType = NULL;
m_puCount = NULL;
*this = CopyFrom;
}
/**
* @brief Destructor will Release() the raw pointer and delete it if this was the last reference
*/
~CSmartPtr() {
Release();
}
// Overloaded operators
T& operator *() const { assert(m_pType); return *m_pType; }
T* operator ->() const { assert(m_pType); return m_pType; }
/**
* @brief Attach() to a raw pointer
* @param pRawPtr The raw pointer to keep track of, ***WARNING*** Do _NOT_ allow more than one CSmartPtr keep track of the same raw pointer
* @return Reference to self
*/
CSmartPtr<T>& operator =(T* pRawPtr) { Attach(pRawPtr); return *this; }
/**
* @brief Copies an existing CSmartPtr adding another reference to the counter
* @param CopyFrom A reference to another CSmartPtr to be copied
* @return Reference to self
*/
CSmartPtr<T>& operator =(const CSmartPtr<T>& CopyFrom) {
if (&CopyFrom != this) { // Check for assignment to self
Release(); // Release the current pointer
if (CopyFrom.IsNull()) { // If the source raw pointer is null
return *this; // Then just bail out
}
m_pType = CopyFrom.m_pType; // Make our pointers reference the same raw pointer and counter
m_puCount = CopyFrom.m_puCount;
assert(m_puCount); // We now point to something valid, so increment the counter
(*m_puCount)++;
}
return *this;
}
// !Overloaded operators
/**
* @brief Implicit type conversion to bool for things like if (!ptr) {} and if (ptr) {}
* @return @see IsNull()
*/
operator bool() const {
return !IsNull();
}
/**
* @brief Check to see if the underlying raw pointer is null
* @return Whether or not underlying raw pointer is null
*/
bool IsNull() const {
return (m_pType == NULL);
}
/**
* @brief Attach to a given raw pointer, it will Release() the current raw pointer and assign the new one
* @param pRawPtr The raw pointer to keep track of, ***WARNING*** Do _NOT_ allow more than one CSmartPtr keep track of the same raw pointer
* @return Reference to self
*/
CSmartPtr<T>& Attach(T* pRawPtr) {
if (pRawPtr != m_pType) { // Check for assignment to self
Release(); // Release the current pointer
m_pType = pRawPtr; // Point to the passed raw pointer
if (m_pType) { // If the passed pointer was valid
m_puCount = new unsigned int(1); // Create a new counter starting at 1 (us)
}
}
return *this;
}
/**
* @brief Releases the underlying raw pointer and cleans up if we were the last reference to said pointer
*/
void Release() {
if (m_pType) { // Only release if there is something to be released
assert(m_puCount);
(*m_puCount)--; // Decrement our counter
if (!*m_puCount) { // If we were the last reference to this pointer, then clean up
delete m_puCount;
delete m_pType;
}
m_pType = NULL; // Get rid of our references
m_puCount = NULL;
}
}
// Getters
T* GetPtr() const { return m_pType; }
unsigned int GetCount() const { return (m_puCount) ? *m_puCount : 0; }
// !Getters
private:
T* m_pType; //!< Raw pointer to the class being referenced
unsigned int* m_puCount; //!< Counter of how many CSmartPtr's are referencing the same raw pointer
};
template<typename T>
bool operator ==(T* lhs, const CSmartPtr<T>& rhs) { return (lhs == rhs.GetPtr()); }
template<typename T>
bool operator ==(const CSmartPtr<T>& lhs, T* rhs) { return (lhs.GetPtr() == rhs); }
template<typename T>
bool operator ==(const CSmartPtr<T>& lhs, const CSmartPtr<T>& rhs) { return (lhs.GetPtr() == rhs.GetPtr()); }
#endif // !_UTILS_H