/src/logging-log4cxx/src/main/cpp/telnetappender.cpp
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #include <log4cxx/net/telnetappender.h> |
19 | | #include <log4cxx/helpers/loglog.h> |
20 | | #include <log4cxx/helpers/optionconverter.h> |
21 | | #include <log4cxx/helpers/stringhelper.h> |
22 | | #include <log4cxx/helpers/serversocket.h> |
23 | | #include <log4cxx/helpers/charsetencoder.h> |
24 | | #include <log4cxx/helpers/bytebuffer.h> |
25 | | #include <log4cxx/helpers/threadutility.h> |
26 | | #include <log4cxx/private/appenderskeleton_priv.h> |
27 | | #if LOG4CXX_ABI_VERSION <= 15 |
28 | | #include <log4cxx/private/aprsocket.h> |
29 | | #endif |
30 | | #include <mutex> |
31 | | #include <thread> |
32 | | #include <vector> |
33 | | |
34 | | #if LOG4CXX_EVENTS_AT_EXIT |
35 | | #include <log4cxx/private/atexitregistry.h> |
36 | | #endif |
37 | | |
38 | | using namespace LOG4CXX_NS; |
39 | | using namespace LOG4CXX_NS::helpers; |
40 | | using namespace LOG4CXX_NS::net; |
41 | | |
42 | | struct Connection |
43 | | { |
44 | | helpers::SocketPtr s; |
45 | | size_t sentCount; |
46 | | }; |
47 | | LOG4CXX_LIST_DEF(ConnectionList, Connection); |
48 | | |
49 | | IMPLEMENT_LOG4CXX_OBJECT(TelnetAppender) |
50 | | |
51 | | struct TelnetAppender::TelnetAppenderPriv : public AppenderSkeletonPrivate |
52 | | { |
53 | 0 | TelnetAppenderPriv( int port, int maxConnections ) : AppenderSkeletonPrivate(), |
54 | 0 | port(port), |
55 | 0 | connections(maxConnections), |
56 | 0 | encoding(LOG4CXX_STR("UTF-8")), |
57 | 0 | encoder(CharsetEncoder::getUTF8Encoder()), |
58 | 0 | activeConnections(0) |
59 | | #if LOG4CXX_EVENTS_AT_EXIT |
60 | | , atExitRegistryRaii([this] |
61 | | { |
62 | | if (setClosed()) |
63 | | stopAcceptingConnections(); |
64 | | }) |
65 | | #endif |
66 | 0 | { } |
67 | | |
68 | | ~TelnetAppenderPriv() |
69 | 0 | { stopAcceptingConnections(); } |
70 | | |
71 | | int port; |
72 | | LogString hostname; |
73 | | bool reuseAddress = false; |
74 | | // Secure default: never let an unauthenticated peer that stops reading |
75 | | // block the logging pipeline (see setNonBlocking for the opt-out). |
76 | | bool nonBlocking = true; |
77 | | ConnectionList connections; |
78 | | LogString encoding; |
79 | | LOG4CXX_NS::helpers::CharsetEncoderPtr encoder; |
80 | | std::unique_ptr<helpers::ServerSocket> serverSocket; |
81 | | std::thread sh; |
82 | | size_t activeConnections; |
83 | | size_t eventCount{ 0 }; |
84 | | |
85 | | #if LOG4CXX_EVENTS_AT_EXIT |
86 | | helpers::AtExitRegistry::Raii atExitRegistryRaii; |
87 | | #endif |
88 | | |
89 | | void stopAcceptingConnections() |
90 | 0 | { |
91 | 0 | if (!this->serverSocket) |
92 | 0 | return; |
93 | | // Interrupt accept() |
94 | 0 | try |
95 | 0 | { |
96 | 0 | this->serverSocket->close(); |
97 | 0 | } |
98 | 0 | catch (Exception&) |
99 | 0 | { |
100 | 0 | } |
101 | 0 | if (this->sh.joinable()) |
102 | 0 | this->sh.join(); |
103 | 0 | } |
104 | | |
105 | | void close(); |
106 | | }; |
107 | | |
108 | 0 | #define _priv static_cast<TelnetAppenderPriv*>(m_priv.get()) |
109 | | |
110 | | /** The default telnet server port */ |
111 | | const int TelnetAppender::DEFAULT_PORT = 23; |
112 | | |
113 | | /** The maximum number of concurrent connections */ |
114 | | const int TelnetAppender::MAX_CONNECTIONS = 20; |
115 | | |
116 | | TelnetAppender::TelnetAppender() |
117 | 0 | : AppenderSkeleton (std::make_unique<TelnetAppenderPriv>(DEFAULT_PORT, MAX_CONNECTIONS)) |
118 | 0 | { |
119 | 0 | } Unexecuted instantiation: log4cxx::net::TelnetAppender::TelnetAppender() Unexecuted instantiation: log4cxx::net::TelnetAppender::TelnetAppender() |
120 | | |
121 | | TelnetAppender::~TelnetAppender() |
122 | 0 | { |
123 | 0 | if (_priv->setClosed()) |
124 | 0 | _priv->close(); |
125 | 0 | } |
126 | | |
127 | | void TelnetAppender::activateOptions( LOG4CXX_ACTIVATE_OPTIONS_FORMAL_PARAMETERS ) |
128 | 0 | { |
129 | 0 | if (_priv->serverSocket == NULL) |
130 | 0 | { |
131 | 0 | _priv->serverSocket = ServerSocket::create(_priv->port, _priv->reuseAddress, _priv->hostname); |
132 | 0 | _priv->serverSocket->setSoTimeout(1000); |
133 | 0 | } |
134 | |
|
135 | 0 | if (!_priv->sh.joinable()) |
136 | 0 | _priv->sh = ThreadUtility::instance()->createThread( LOG4CXX_STR("TelnetAppender"), &TelnetAppender::acceptConnections, this ); |
137 | 0 | } |
138 | | |
139 | | void TelnetAppender::setOption(const LogString& option, |
140 | | const LogString& value) |
141 | 0 | { |
142 | 0 | if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("PORT"), LOG4CXX_STR("port"))) |
143 | 0 | { |
144 | 0 | setPort(OptionConverter::toInt(value, DEFAULT_PORT)); |
145 | 0 | } |
146 | 0 | else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("MAXCONNECTIONS"), LOG4CXX_STR("maxconnections"))) |
147 | 0 | { |
148 | 0 | setMaxConnections(OptionConverter::toInt(value, MAX_CONNECTIONS)); |
149 | 0 | } |
150 | 0 | else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("ENCODING"), LOG4CXX_STR("encoding"))) |
151 | 0 | { |
152 | 0 | setEncoding(value); |
153 | 0 | } |
154 | 0 | else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("NONBLOCKING"), LOG4CXX_STR("nonblocking"))) |
155 | 0 | { |
156 | 0 | setNonBlocking(OptionConverter::toBoolean(value, true)); |
157 | 0 | } |
158 | 0 | else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("REUSEADDRESS"), LOG4CXX_STR("reuseaddress"))) |
159 | 0 | { |
160 | 0 | setReuseAddress(OptionConverter::toBoolean(value, true)); |
161 | 0 | } |
162 | 0 | else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("HOSTNAME"), LOG4CXX_STR("hostname"))) |
163 | 0 | { |
164 | 0 | setHostname(value); |
165 | 0 | } |
166 | 0 | else |
167 | 0 | { |
168 | 0 | AppenderSkeleton::setOption(option, value); |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | | LogString TelnetAppender::getEncoding() const |
173 | 0 | { |
174 | 0 | std::lock_guard<std::recursive_mutex> lock(_priv->mutex); |
175 | 0 | return _priv->encoding; |
176 | 0 | } |
177 | | |
178 | | void TelnetAppender::setEncoding(const LogString& value) |
179 | 0 | { |
180 | 0 | std::lock_guard<std::recursive_mutex> lock(_priv->mutex); |
181 | 0 | _priv->encoder = CharsetEncoder::getEncoder(value); |
182 | 0 | _priv->encoding = value; |
183 | 0 | } |
184 | | |
185 | | |
186 | | void TelnetAppender::close() |
187 | 0 | { |
188 | 0 | if (_priv->setClosed()) |
189 | 0 | _priv->close(); |
190 | 0 | } |
191 | | |
192 | | void TelnetAppender::TelnetAppenderPriv::close() |
193 | 0 | { |
194 | 0 | this->stopAcceptingConnections(); |
195 | 0 | std::lock_guard<std::recursive_mutex> lock(this->mutex); |
196 | 0 | if (this->eventCount && helpers::LogLog::isDebugEnabled()) |
197 | 0 | { |
198 | 0 | LogString msg = LOG4CXX_STR("TelnetAppender eventCount "); |
199 | 0 | helpers::StringHelper::toString(this->eventCount, msg); |
200 | 0 | helpers::LogLog::debug(msg); |
201 | 0 | } |
202 | 0 | SocketPtr nullSocket; |
203 | 0 | int connectionNumber{ 0 }; |
204 | 0 | for (auto& item : this->connections) |
205 | 0 | { |
206 | 0 | ++connectionNumber; |
207 | 0 | if (item.s) |
208 | 0 | { |
209 | 0 | item.s->close(); |
210 | 0 | if (this->eventCount && helpers::LogLog::isDebugEnabled()) |
211 | 0 | { |
212 | 0 | LogString msg = LOG4CXX_STR("TelnetAppender connection "); |
213 | 0 | helpers::StringHelper::toString(connectionNumber, msg); |
214 | 0 | msg += LOG4CXX_STR(" sentCount "); |
215 | 0 | helpers::StringHelper::toString(item.sentCount, msg); |
216 | 0 | helpers::LogLog::debug(msg); |
217 | 0 | } |
218 | 0 | item = Connection{ nullSocket, 0 }; |
219 | 0 | } |
220 | 0 | } |
221 | 0 | this->activeConnections = 0; |
222 | 0 | } |
223 | | |
224 | | |
225 | | void TelnetAppender::write(ByteBuffer& buf) |
226 | 0 | { |
227 | 0 | int connectionNumber{ 0 }; |
228 | 0 | for (auto& item :_priv->connections) |
229 | 0 | { |
230 | 0 | ++connectionNumber; |
231 | 0 | if (item.s) |
232 | 0 | { |
233 | 0 | try |
234 | 0 | { |
235 | 0 | ByteBuffer b(buf.current(), buf.remaining()); |
236 | 0 | item.s->write(b); |
237 | 0 | ++item.sentCount; |
238 | 0 | } |
239 | 0 | catch (const Exception& e) |
240 | 0 | { |
241 | 0 | if (helpers::LogLog::isDebugEnabled()) |
242 | 0 | { |
243 | 0 | LogString msg(LOG4CXX_STR("TelnetAppender connection ")); |
244 | 0 | helpers::StringHelper::toString(connectionNumber, msg); |
245 | 0 | msg += LOG4CXX_STR(" sentCount "); |
246 | 0 | helpers::StringHelper::toString(item.sentCount, msg); |
247 | 0 | msg += LOG4CXX_STR("/"); |
248 | 0 | helpers::StringHelper::toString(_priv->eventCount, msg); |
249 | 0 | helpers::LogLog::warn(msg, e); |
250 | 0 | } |
251 | 0 | item.s.reset(); |
252 | 0 | _priv->activeConnections--; |
253 | 0 | } |
254 | 0 | } |
255 | 0 | } |
256 | 0 | } |
257 | | |
258 | | void TelnetAppender::writeStatus(const SocketPtr& socket, const LogString& msg, Pool& p) |
259 | 0 | { |
260 | 0 | size_t bytesSize = msg.size() * 2 + 10; |
261 | 0 | char* bytes = p.pstralloc(bytesSize); |
262 | |
|
263 | 0 | LogString::const_iterator msgIter(msg.begin()); |
264 | 0 | ByteBuffer buf(bytes, bytesSize); |
265 | |
|
266 | 0 | while (msgIter != msg.end()) |
267 | 0 | { |
268 | 0 | CharsetEncoder::encode(_priv->encoder, msg, msgIter, buf); |
269 | 0 | buf.flip(); |
270 | 0 | socket->write(buf); |
271 | 0 | buf.clear(); |
272 | 0 | } |
273 | 0 | } |
274 | | |
275 | | void TelnetAppender::append( LOG4CXX_APPEND_FORMAL_PARAMETERS ) |
276 | 0 | { |
277 | 0 | ++_priv->eventCount; |
278 | 0 | if (0 < _priv->activeConnections) |
279 | 0 | { |
280 | 0 | helpers::Pool tempPool; |
281 | 0 | LogString msg; |
282 | 0 | if (_priv->layout) |
283 | 0 | _priv->layout->format(msg, event); |
284 | 0 | else |
285 | 0 | msg = event->getRenderedMessage(); |
286 | 0 | msg.append(LOG4CXX_STR("\r\n")); |
287 | 0 | size_t bytesSize = msg.size() * 2 + 10; |
288 | 0 | char* bytes = tempPool.pstralloc(bytesSize); |
289 | |
|
290 | 0 | LogString::const_iterator msgIter(msg.begin()); |
291 | 0 | ByteBuffer buf(bytes, bytesSize); |
292 | |
|
293 | 0 | std::lock_guard<std::recursive_mutex> lock(_priv->mutex); |
294 | |
|
295 | 0 | while (msgIter != msg.end()) |
296 | 0 | { |
297 | 0 | CharsetEncoder::encode(_priv->encoder, msg, msgIter, buf); |
298 | 0 | buf.flip(); |
299 | 0 | write(buf); |
300 | 0 | buf.clear(); |
301 | 0 | } |
302 | 0 | } |
303 | 0 | } |
304 | | |
305 | | void TelnetAppender::acceptConnections() |
306 | 0 | { |
307 | | |
308 | | // main loop; is left when This->closed is != 0 after an accept() |
309 | 0 | while (true) |
310 | 0 | { |
311 | 0 | try |
312 | 0 | { |
313 | 0 | SocketPtr newClient = _priv->serverSocket->accept(); |
314 | | #if 15 < LOG4CXX_ABI_VERSION |
315 | | newClient->setNonBlocking(_priv->nonBlocking); |
316 | | #else |
317 | 0 | if (auto p = dynamic_cast<APRSocket*>(newClient.get())) |
318 | 0 | p->setNonBlocking(_priv->nonBlocking); |
319 | 0 | #endif |
320 | 0 | bool done = _priv->closed; |
321 | |
|
322 | 0 | if (done) |
323 | 0 | { |
324 | 0 | Pool p; |
325 | 0 | writeStatus(newClient, LOG4CXX_STR("Log closed.\r\n"), p); |
326 | 0 | newClient->close(); |
327 | |
|
328 | 0 | break; |
329 | 0 | } |
330 | | |
331 | 0 | size_t count = _priv->activeConnections; |
332 | |
|
333 | 0 | if (count >= _priv->connections.size()) |
334 | 0 | { |
335 | 0 | Pool p; |
336 | 0 | writeStatus(newClient, LOG4CXX_STR("Too many connections.\r\n"), p); |
337 | 0 | newClient->close(); |
338 | 0 | } |
339 | 0 | else |
340 | 0 | { |
341 | | // |
342 | | // find unoccupied connection |
343 | | // |
344 | 0 | std::lock_guard<std::recursive_mutex> lock(_priv->mutex); |
345 | |
|
346 | 0 | int connectionNumber{ 0 }; |
347 | 0 | for (auto& item : _priv->connections) |
348 | 0 | { |
349 | 0 | ++connectionNumber; |
350 | 0 | if (!item.s) |
351 | 0 | { |
352 | 0 | item = Connection{ newClient, 0 }; |
353 | 0 | _priv->activeConnections++; |
354 | 0 | if (helpers::LogLog::isDebugEnabled()) |
355 | 0 | { |
356 | 0 | LogString msg = LOG4CXX_STR("TelnetAppender new connection "); |
357 | 0 | helpers::StringHelper::toString(connectionNumber, msg); |
358 | 0 | msg += LOG4CXX_STR("/"); |
359 | 0 | helpers::StringHelper::toString(_priv->activeConnections, msg); |
360 | 0 | helpers::LogLog::debug(msg); |
361 | 0 | } |
362 | |
|
363 | 0 | break; |
364 | 0 | } |
365 | 0 | } |
366 | |
|
367 | 0 | Pool p; |
368 | 0 | LogString oss(LOG4CXX_STR("TelnetAppender v1.0 (")); |
369 | 0 | StringHelper::toString((int) count + 1, oss); |
370 | 0 | oss += LOG4CXX_STR(" active connections)\r\n\r\n"); |
371 | 0 | writeStatus(newClient, oss, p); |
372 | 0 | } |
373 | 0 | } |
374 | 0 | catch (InterruptedIOException&) |
375 | 0 | { |
376 | 0 | if (_priv->closed) |
377 | 0 | { |
378 | 0 | break; |
379 | 0 | } |
380 | 0 | } |
381 | 0 | catch (Exception& e) |
382 | 0 | { |
383 | 0 | if (!_priv->closed) |
384 | 0 | { |
385 | 0 | LogLog::error(LOG4CXX_STR("Encountered error while in SocketHandler loop."), e); |
386 | 0 | } |
387 | 0 | else |
388 | 0 | { |
389 | 0 | break; |
390 | 0 | } |
391 | 0 | } |
392 | 0 | } |
393 | |
|
394 | 0 | } |
395 | | |
396 | | int TelnetAppender::getPort() const |
397 | 0 | { |
398 | 0 | return _priv->port; |
399 | 0 | } |
400 | | |
401 | | void TelnetAppender::setPort(int port1) |
402 | 0 | { |
403 | 0 | _priv->port = port1; |
404 | 0 | } |
405 | | |
406 | | LogString TelnetAppender::getHostname() const |
407 | 0 | { |
408 | 0 | return _priv->hostname; |
409 | 0 | } |
410 | | |
411 | | void TelnetAppender::setHostname(const LogString& hostname) |
412 | 0 | { |
413 | 0 | _priv->hostname = hostname; |
414 | 0 | } |
415 | | |
416 | | int TelnetAppender::getMaxConnections() const |
417 | 0 | { |
418 | 0 | return static_cast<int>(_priv->connections.size()); |
419 | 0 | } |
420 | | |
421 | | void TelnetAppender::setMaxConnections(int newValue) |
422 | 0 | { |
423 | 0 | if (newValue < 0) |
424 | 0 | { |
425 | 0 | LogLog::warn(LOG4CXX_STR("TelnetAppender MaxConnections must be non-negative. Keeping the previous value.")); |
426 | 0 | return; |
427 | 0 | } |
428 | | |
429 | 0 | std::lock_guard<std::recursive_mutex> lock(_priv->mutex); |
430 | 0 | const size_t newSize = static_cast<size_t>(newValue); |
431 | 0 | if (_priv->connections.size() < newSize) |
432 | 0 | _priv->connections.resize(newSize); |
433 | 0 | else while (newSize < _priv->connections.size()) |
434 | 0 | { |
435 | 0 | auto item = _priv->connections.back(); |
436 | 0 | _priv->connections.pop_back(); |
437 | 0 | if (item.s) |
438 | 0 | { |
439 | 0 | item.s->close(); |
440 | 0 | --_priv->activeConnections; |
441 | 0 | } |
442 | 0 | } |
443 | 0 | } |
444 | | |
445 | | void TelnetAppender::setNonBlocking(bool newValue) |
446 | 0 | { |
447 | 0 | _priv->nonBlocking = newValue; |
448 | 0 | } |
449 | | |
450 | | void TelnetAppender::setReuseAddress(bool reuseAddress) |
451 | 0 | { |
452 | 0 | _priv->reuseAddress = reuseAddress; |
453 | 0 | } |
454 | | |
455 | | bool TelnetAppender::requiresLayout() const |
456 | 0 | { |
457 | 0 | return false; |
458 | 0 | } |