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
|
2005-04-29 22:05:07 +00:00
|
|
|
* Copyright (C) 2004, 2005 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
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
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;
|
|
|
|
|
|
|
|
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;
|
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) {
|
|
|
|
par << "\n param['" << s->name() << "'] = ";
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (msg == "engine.timer")
|
|
|
|
return false;
|
2005-11-04 11:34:51 +00:00
|
|
|
if (msg == "engine.command") {
|
|
|
|
String line(msg.getValue("line"));
|
|
|
|
if (line.startSkip("sniffer")) {
|
|
|
|
line >> s_active;
|
|
|
|
msg.retValue() << "Message sniffer is " << (s_active ? "on" : "off") << "\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!s_active)
|
|
|
|
return false;
|
2005-09-02 16:39:00 +00:00
|
|
|
String par;
|
|
|
|
dumpParams(msg,par);
|
2006-01-13 00:08:48 +00:00
|
|
|
Output("Sniffed '%s' time=%u.%06u\n thread=%p '%s'\n data=%p\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),
|
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)
|
|
|
|
{
|
2005-11-04 11:34:51 +00:00
|
|
|
if ((!s_active) || (msg == "engine.timer"))
|
2005-09-02 16:39:00 +00:00
|
|
|
return;
|
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);
|
2006-01-13 00:08:48 +00:00
|
|
|
Output("Returned %s '%s' delay=%u.%06u\n thread=%p '%s'\n data=%p\n retval='%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),
|
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(),
|
|
|
|
msg.retValue().c_str(),
|
|
|
|
par.safe());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
MsgSniff::MsgSniff()
|
|
|
|
: m_first(true)
|
|
|
|
{
|
|
|
|
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);
|
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);
|
|
|
|
|
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|