2004-05-22 00:05:20 +00:00
|
|
|
/*
|
2004-11-29 03:56:41 +00:00
|
|
|
* msgsniff.cpp
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
|
|
|
*
|
|
|
|
* A sample message sniffer that inserts a wildcard message handler
|
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
2006-05-27 15:08:43 +00:00
|
|
|
* Copyright (C) 2004-2006 Null Team
|
2004-11-29 03:56:41 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-05-27 15:08:43 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-11-29 03:56:41 +00:00
|
|
|
*/
|
2004-05-22 00:05:20 +00:00
|
|
|
|
2005-03-18 18:16:59 +00:00
|
|
|
#include <yatengine.h>
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
using namespace TelEngine;
|
2006-05-27 14:53:18 +00:00
|
|
|
namespace { // anonymous
|
2004-05-22 00:05:20 +00:00
|
|
|
|
2011-04-11 08:59:09 +00:00
|
|
|
static const char* s_debugs[] =
|
2010-07-15 12:54:04 +00:00
|
|
|
{
|
|
|
|
"on",
|
|
|
|
"off",
|
|
|
|
"enable",
|
|
|
|
"disable",
|
|
|
|
"true",
|
|
|
|
"false",
|
|
|
|
"yes",
|
|
|
|
"no",
|
2011-04-11 08:59:09 +00:00
|
|
|
"filter",
|
2010-07-15 12:54:04 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
class MsgSniff : public Plugin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MsgSniff();
|
|
|
|
virtual void initialize();
|
|
|
|
private:
|
|
|
|
bool m_first;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SniffHandler : public MessageHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SniffHandler() : MessageHandler(0,0) { }
|
|
|
|
virtual bool received(Message &msg);
|
|
|
|
};
|
|
|
|
|
2005-09-02 16:39:00 +00:00
|
|
|
class HookHandler : public MessagePostHook
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void dispatched(const Message& msg, bool handled);
|
|
|
|
};
|
|
|
|
|
2005-11-04 11:34:51 +00:00
|
|
|
static bool s_active = true;
|
2011-04-11 08:59:09 +00:00
|
|
|
static Regexp s_filter;
|
|
|
|
static Mutex s_mutex(false,"FilterSniff");
|
2005-09-02 16:39:00 +00:00
|
|
|
|
|
|
|
static void dumpParams(const Message &msg, String& par)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
unsigned n = msg.length();
|
|
|
|
for (unsigned i = 0; i < n; i++) {
|
2005-09-02 16:39:00 +00:00
|
|
|
const NamedString *s = msg.getParam(i);
|
2005-12-14 19:32:00 +00:00
|
|
|
if (s) {
|
2006-10-17 20:40:01 +00:00
|
|
|
par << "\r\n param['" << s->name() << "'] = ";
|
2005-12-14 19:32:00 +00:00
|
|
|
if (s->name() == "password")
|
|
|
|
par << "(hidden)";
|
|
|
|
else
|
|
|
|
par << "'" << *s << "'";
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
2005-09-02 16:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SniffHandler::received(Message &msg)
|
|
|
|
{
|
2011-06-08 11:44:51 +00:00
|
|
|
if (msg == YSTRING("engine.timer"))
|
2005-09-02 16:39:00 +00:00
|
|
|
return false;
|
2011-06-08 11:44:51 +00:00
|
|
|
if (msg == YSTRING("engine.command")) {
|
2010-07-15 12:54:04 +00:00
|
|
|
static const String name("sniffer");
|
2011-06-08 11:44:51 +00:00
|
|
|
String line(msg.getValue(YSTRING("line")));
|
2010-07-15 12:54:04 +00:00
|
|
|
if (line.startSkip(name)) {
|
2005-11-04 11:34:51 +00:00
|
|
|
line >> s_active;
|
2011-04-11 08:59:09 +00:00
|
|
|
line.trimSpaces();
|
|
|
|
if (line.startSkip("filter")) {
|
|
|
|
s_mutex.lock();
|
|
|
|
s_filter = line;
|
|
|
|
s_mutex.unlock();
|
|
|
|
}
|
|
|
|
msg.retValue() << "Message sniffer is " << (s_active ? "on" : "off");
|
|
|
|
if (s_active && s_filter)
|
|
|
|
msg.retValue() << " filter " << s_filter;
|
|
|
|
msg.retValue() << "\r\n";
|
2005-11-04 11:34:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-06-08 11:44:51 +00:00
|
|
|
line = msg.getParam(YSTRING("partline"));
|
2010-07-15 12:54:04 +00:00
|
|
|
if (line.null()) {
|
2011-06-08 11:44:51 +00:00
|
|
|
if (name.startsWith(msg.getValue(YSTRING("partword"))))
|
2010-07-15 12:54:04 +00:00
|
|
|
msg.retValue().append(name,"\t");
|
|
|
|
}
|
|
|
|
else if (name == line) {
|
2011-06-08 11:44:51 +00:00
|
|
|
line = msg.getValue(YSTRING("partword"));
|
2011-04-11 08:59:09 +00:00
|
|
|
for (const char** b = s_debugs; *b; b++)
|
2010-07-15 12:54:04 +00:00
|
|
|
if (line.null() || String(*b).startsWith(line))
|
|
|
|
msg.retValue().append(*b,"\t");
|
|
|
|
}
|
2005-11-04 11:34:51 +00:00
|
|
|
}
|
|
|
|
if (!s_active)
|
|
|
|
return false;
|
2011-04-11 09:13:43 +00:00
|
|
|
Lock lock(s_mutex);
|
2011-04-11 08:59:09 +00:00
|
|
|
if (s_filter && !s_filter.matches(msg))
|
|
|
|
return false;
|
|
|
|
lock.drop();
|
2005-09-02 16:39:00 +00:00
|
|
|
String par;
|
|
|
|
dumpParams(msg,par);
|
2010-07-15 12:54:04 +00:00
|
|
|
Output("Sniffed '%s' time=%u.%06u%s\r\n thread=%p '%s'\r\n data=%p\r\n retval='%s'%s",
|
2005-09-02 16:39:00 +00:00
|
|
|
msg.c_str(),
|
2005-11-02 04:10:15 +00:00
|
|
|
(unsigned int)(msg.msgTime().usec() / 1000000),
|
|
|
|
(unsigned int)(msg.msgTime().usec() % 1000000),
|
2010-07-15 12:54:04 +00:00
|
|
|
(msg.broadcast() ? " (broadcast)" : ""),
|
2005-04-10 21:42:49 +00:00
|
|
|
Thread::current(),
|
2006-01-13 00:08:48 +00:00
|
|
|
Thread::currentName(),
|
2005-09-02 16:39:00 +00:00
|
|
|
msg.userData(),
|
|
|
|
msg.retValue().c_str(),
|
|
|
|
par.safe());
|
2004-05-22 00:05:20 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2005-09-02 16:39:00 +00:00
|
|
|
|
|
|
|
void HookHandler::dispatched(const Message& msg, bool handled)
|
|
|
|
{
|
2011-06-08 11:44:51 +00:00
|
|
|
if ((!s_active) || (msg == YSTRING("engine.timer")))
|
2005-09-02 16:39:00 +00:00
|
|
|
return;
|
2011-04-11 08:59:09 +00:00
|
|
|
Lock lock(s_mutex);
|
|
|
|
if (s_filter && !s_filter.matches(msg))
|
|
|
|
return;
|
|
|
|
lock.drop();
|
2005-11-02 04:10:15 +00:00
|
|
|
u_int64_t dt = Time::now() - msg.msgTime().usec();
|
2005-09-02 16:39:00 +00:00
|
|
|
String par;
|
|
|
|
dumpParams(msg,par);
|
2011-06-08 11:44:51 +00:00
|
|
|
const char* rval = msg.retValue().c_str();
|
|
|
|
const char* rsep = "'";
|
|
|
|
if (handled && rval && (rval[0] != '-' || rval[1]) && (msg == YSTRING("user.auth"))) {
|
|
|
|
rval = "(hidden)";
|
|
|
|
rsep = "";
|
|
|
|
}
|
|
|
|
Output("Returned %s '%s' delay=%u.%06u%s\r\n thread=%p '%s'\r\n data=%p\r\n retval=%s%s%s%s",
|
2005-09-02 16:39:00 +00:00
|
|
|
String::boolText(handled),
|
|
|
|
msg.c_str(),
|
2005-11-02 04:10:15 +00:00
|
|
|
(unsigned int)(dt / 1000000),
|
|
|
|
(unsigned int)(dt % 1000000),
|
2010-07-15 12:54:04 +00:00
|
|
|
(msg.broadcast() ? " (broadcast)" : ""),
|
2005-09-02 16:39:00 +00:00
|
|
|
Thread::current(),
|
2006-01-13 00:08:48 +00:00
|
|
|
Thread::currentName(),
|
2005-09-02 16:39:00 +00:00
|
|
|
msg.userData(),
|
2011-06-08 11:44:51 +00:00
|
|
|
rsep,rval,rsep,
|
2005-09-02 16:39:00 +00:00
|
|
|
par.safe());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
MsgSniff::MsgSniff()
|
2011-03-23 16:26:25 +00:00
|
|
|
: Plugin("msgsniff"),
|
|
|
|
m_first(true)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
Output("Loaded module MsgSniffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MsgSniff::initialize()
|
|
|
|
{
|
|
|
|
Output("Initializing module MsgSniffer");
|
|
|
|
if (m_first) {
|
|
|
|
m_first = false;
|
2005-12-14 19:32:00 +00:00
|
|
|
s_active = Engine::config().getBoolValue("general","msgsniff",false);
|
2011-04-11 08:59:09 +00:00
|
|
|
s_mutex.lock();
|
|
|
|
s_filter = Engine::config().getValue("general","filtersniff");
|
|
|
|
s_mutex.unlock();
|
2004-05-22 00:05:20 +00:00
|
|
|
Engine::install(new SniffHandler);
|
2005-09-02 16:39:00 +00:00
|
|
|
Engine::self()->setHook(new HookHandler);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT_PLUGIN(MsgSniff);
|
|
|
|
|
2006-05-27 14:53:18 +00:00
|
|
|
}; // anonymous namespace
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|