| 1 |
// SocketArray - wrapper for clean handling of an array of 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 |
// INCLUDES |
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H |
| 10 |
#include "e2config.h" |
| 11 |
#endif |
| 12 |
#include "SocketArray.hpp" |
| 13 |
#include "Queue.hpp" |
| 14 |
#include "Logger.hpp" |
| 15 |
|
| 16 |
#include <cerrno> |
| 17 |
#include <cstring> |
| 18 |
|
| 19 |
// GLOBALS |
| 20 |
|
| 21 |
|
| 22 |
// IMPLEMENTATION |
| 23 |
|
| 24 |
SocketArray::~SocketArray() |
| 25 |
{ |
| 26 |
delete[] drawer; |
| 27 |
} |
| 28 |
|
| 29 |
void SocketArray::deleteAll() |
| 30 |
{ |
| 31 |
delete[] drawer; |
| 32 |
drawer = NULL; |
| 33 |
socknum = 0; |
| 34 |
} |
| 35 |
|
| 36 |
// close all sockets & create new ones |
| 37 |
void SocketArray::reset(int sockcount) |
| 38 |
{ |
| 39 |
delete[] drawer; |
| 40 |
|
| 41 |
drawer = new Socket[sockcount]; |
| 42 |
socknum = sockcount; |
| 43 |
} |
| 44 |
|
| 45 |
// bind our first socket to any IP |
| 46 |
int SocketArray::bindSingle(int port) |
| 47 |
{ |
| 48 |
if (socknum < 1) { |
| 49 |
return -1; |
| 50 |
} |
| 51 |
DEBUG_debug("bindSingle binding port", port); |
| 52 |
lc_types.push_back(CT_PROXY); |
| 53 |
return drawer[0].bind(port); |
| 54 |
} |
| 55 |
|
| 56 |
int SocketArray::bindSingle(unsigned int index, int port, unsigned int type) |
| 57 |
{ |
| 58 |
if (socknum <= index) { |
| 59 |
return -1; |
| 60 |
} |
| 61 |
DEBUG_debug("bindSingle binding port", port, " with type ", type); |
| 62 |
lc_types.push_back(type); |
| 63 |
return drawer[index].bind(port); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
// bind our first socket to any IP and one or more ports |
| 68 |
int SocketArray::bindSingleM(std::deque<String> &ports, int &index, int ct_type ) |
| 69 |
{ |
| 70 |
if ( socknum < (index + ports.size())) { |
| 71 |
return -1; |
| 72 |
} |
| 73 |
for (auto i : ports) { |
| 74 |
DEBUG_debug("bindSingleM binding port", i); |
| 75 |
if (drawer[index].bind(i.toInteger())) { |
| 76 |
E2LOGGER_error("Error binding server socket: [", i, " ", index, "] (", strerror(errno), ")"); |
| 77 |
return -1; |
| 78 |
} |
| 79 |
lc_types.push_back(ct_type); |
| 80 |
index++; |
| 81 |
} |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
// return an array of our socket FDs |
| 86 |
int *SocketArray::getFDAll() |
| 87 |
{ |
| 88 |
int *fds = new int[socknum]; |
| 89 |
for (unsigned int i = 0; i < socknum; i++) { |
| 90 |
DEBUG_debug("Socket ", i, " fd:", drawer[i].getFD() ); |
| 91 |
fds[i] = drawer[i].getFD(); |
| 92 |
} |
| 93 |
return fds; |
| 94 |
} |
| 95 |
|
| 96 |
// listen on all IPs with given kernel queue size |
| 97 |
int SocketArray::listenAll(int queue) |
| 98 |
{ |
| 99 |
for (unsigned int i = 0; i < socknum; i++) { |
| 100 |
if (drawer[i].listen(queue)) { |
| 101 |
E2LOGGER_error("Error listening to socket"); |
| 102 |
return -1; |
| 103 |
} |
| 104 |
} |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
// bind all sockets to given IP list |
| 109 |
int SocketArray::bindAll(std::deque<String> &ips, std::deque<String> &ports, int &index, int ct_type) |
| 110 |
{ |
| 111 |
if ((index + (ips.size() * ports.size())) > socknum) { |
| 112 |
return -1; |
| 113 |
} |
| 114 |
//for (unsigned int i = 0; i < socknum; i++) { |
| 115 |
for (auto i_ips : ips) { |
| 116 |
for (auto i_ports : ports) { |
| 117 |
DEBUG_debug("Binding server socket[", i_ports, " ", i_ips, " ", index, "]"); |
| 118 |
if (drawer[index].bind(i_ips.toCharArray(), i_ports.toInteger())) { |
| 119 |
E2LOGGER_error("Error binding server socket: [", i_ports, " ", i_ips, " ", index, "] (", strerror(errno), |
| 120 |
")"); |
| 121 |
return -1; |
| 122 |
} |
| 123 |
lc_types.push_back(ct_type); |
| 124 |
index++; |
| 125 |
} |
| 126 |
} |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
// try connecting to all our sockets which are still open to allow tidy close |
| 131 |
void SocketArray::self_connect() { |
| 132 |
for (unsigned int i = 0; i < socknum; i++) { |
| 133 |
if (drawer[i].getFD() > -1) { |
| 134 |
std::string sip = drawer[i].getLocalIP(); |
| 135 |
int port = drawer[i].getPort(); |
| 136 |
Socket temp; |
| 137 |
temp.setTimeout(100); |
| 138 |
temp.connect(sip, port); |
| 139 |
temp.close(); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
unsigned int SocketArray::getType(unsigned int ind) { |
| 145 |
return lc_types.at(ind); |
| 146 |
} |