Add SIP URI validator.

This commit is contained in:
Michał Słomkowski 2015-11-09 00:51:51 +01:00
parent 47ecacab4b
commit 9d4da88e9f
7 changed files with 133 additions and 62 deletions

View File

@ -26,7 +26,9 @@ set(SOURCE_FILES
MumbleCommunicator.hpp MumbleCommunicator.hpp
ICommunicator.hpp ICommunicator.hpp
Configuration.cpp Configuration.cpp
Configuration.hpp) Configuration.hpp
IncomingConnectionValidator.cpp
IncomingConnectionValidator.hpp)
add_executable(mumsi ${SOURCE_FILES} main.cpp) add_executable(mumsi ${SOURCE_FILES} main.cpp)
target_link_libraries(mumsi ${PJSIP_LIBRARIES}) target_link_libraries(mumsi ${PJSIP_LIBRARIES})

View File

@ -0,0 +1,11 @@
#include "IncomingConnectionValidator.hpp"
sip::IncomingConnectionValidator::IncomingConnectionValidator(std::string validUriExpression)
: validUriExpression(validUriExpression),
logger(log4cpp::Category::getInstance("IncomingConnectionValidator")) {
}
bool sip::IncomingConnectionValidator::validateUri(std::string uri) {
return true;
}

View File

@ -0,0 +1,19 @@
#pragma once
#include <boost/noncopyable.hpp>
#include <log4cpp/Category.hh>
#include <string>
namespace sip {
class IncomingConnectionValidator : boost::noncopyable {
public:
IncomingConnectionValidator(std::string validUriExpression);
bool validateUri(std::string uri);
private:
log4cpp::Category &logger;
std::string validUriExpression;
};
}

View File

@ -93,7 +93,35 @@ namespace sip {
communicator(comm), communicator(comm),
account(acc) { } account(acc) { }
virtual void onCallState(pj::OnCallStateParam &prm) { virtual void onCallState(pj::OnCallStateParam &prm);
virtual void onCallMediaState(pj::OnCallMediaStateParam &prm);
virtual void onDtmfDigit(pj::OnDtmfDigitParam &prm);
private:
sip::PjsuaCommunicator &communicator;
pj::Account &account;
};
class _Account : public pj::Account {
public:
_Account(sip::PjsuaCommunicator &comm)
: communicator(comm) { }
virtual void onRegState(pj::OnRegStateParam &prm);
virtual void onIncomingCall(pj::OnIncomingCallParam &iprm);
private:
sip::PjsuaCommunicator &communicator;
bool available = true;
friend class _Call;
};
void _Call::onCallState(pj::OnCallStateParam &prm) {
auto ci = getInfo(); auto ci = getInfo();
communicator.logger.info("Call %d state=%s.", ci.id, ci.stateText.c_str()); communicator.logger.info("Call %d state=%s.", ci.id, ci.stateText.c_str());
@ -114,11 +142,13 @@ namespace sip {
communicator.logger.notice(msgText); communicator.logger.notice(msgText);
communicator.onStateChange(msgText); communicator.onStateChange(msgText);
dynamic_cast<_Account &>(account).available = true;
delete this; delete this;
} }
} }
virtual void onCallMediaState(pj::OnCallMediaStateParam &prm) { void _Call::onCallMediaState(pj::OnCallMediaStateParam &prm) {
auto ci = getInfo(); auto ci = getInfo();
if (ci.media.size() != 1) { if (ci.media.size() != 1) {
@ -130,50 +160,49 @@ namespace sip {
communicator.media->startTransmit(*aud_med); communicator.media->startTransmit(*aud_med);
aud_med->startTransmit(*communicator.media); aud_med->startTransmit(*communicator.media);
} else if (ci.media[0].status == PJSUA_CALL_MEDIA_NONE) {
dynamic_cast<_Account &>(account).available = true;
} }
} }
virtual void onDtmfDigit(pj::OnDtmfDigitParam &prm) { void _Call::onDtmfDigit(pj::OnDtmfDigitParam &prm) {
communicator.logger.notice("DTMF digit '%s' (call %d).", prm.digit.c_str(), getId()); communicator.logger.notice("DTMF digit '%s' (call %d).", prm.digit.c_str(), getId());
} }
private: void _Account::onRegState(pj::OnRegStateParam &prm) {
sip::PjsuaCommunicator &communicator;
pj::Account &account;
};
class _Account : public pj::Account {
public:
_Account(sip::PjsuaCommunicator &comm)
: communicator(comm) { }
virtual void onRegState(pj::OnRegStateParam &prm) {
pj::AccountInfo ai = getInfo(); pj::AccountInfo ai = getInfo();
communicator.logger << log4cpp::Priority::INFO communicator.logger << log4cpp::Priority::INFO
<< (ai.regIsActive ? "Register:" : "Unregister:") << " code=" << prm.code; << (ai.regIsActive ? "Register:" : "Unregister:") << " code=" << prm.code;
} }
virtual void onIncomingCall(pj::OnIncomingCallParam &iprm) { void _Account::onIncomingCall(pj::OnIncomingCallParam &iprm) {
auto *call = new _Call(communicator, *this, iprm.callId); auto *call = new _Call(communicator, *this, iprm.callId);
communicator.logger.info("Incoming call from %s.", call->getInfo().remoteUri.c_str()); string uri = call->getInfo().remoteUri;
communicator.logger.info("Incoming call from %s.", uri.c_str());
if (communicator.uriValidator.validateUri(uri)) {
pj::CallOpParam param; pj::CallOpParam param;
if (available) {
param.statusCode = PJSIP_SC_OK; param.statusCode = PJSIP_SC_OK;
available = false;
} else {
param.statusCode = PJSIP_SC_BUSY_EVERYWHERE;
}
call->answer(param); call->answer(param);
} else {
communicator.logger.warn("Refusing call from %s.", uri.c_str());
}
}
} }
private: sip::PjsuaCommunicator::PjsuaCommunicator(IncomingConnectionValidator &validator)
sip::PjsuaCommunicator &communicator;
friend class _Call;
};
}
sip::PjsuaCommunicator::PjsuaCommunicator()
: logger(log4cpp::Category::getInstance("SipCommunicator")), : logger(log4cpp::Category::getInstance("SipCommunicator")),
pjsuaLogger(log4cpp::Category::getInstance("Pjsua")) { pjsuaLogger(log4cpp::Category::getInstance("Pjsua")),
uriValidator(validator) {
logWriter.reset(new sip::_LogWriter(pjsuaLogger)); logWriter.reset(new sip::_LogWriter(pjsuaLogger));

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "ICommunicator.hpp" #include "ICommunicator.hpp"
#include "IncomingConnectionValidator.hpp"
#include <pjmedia.h> #include <pjmedia.h>
#include <pjsua-lib/pjsua.h> #include <pjsua-lib/pjsua.h>
@ -56,7 +57,7 @@ namespace sip {
class PjsuaCommunicator : public ICommunicator, boost::noncopyable { class PjsuaCommunicator : public ICommunicator, boost::noncopyable {
public: public:
PjsuaCommunicator(); PjsuaCommunicator(IncomingConnectionValidator &validator);
void connect( void connect(
std::string host, std::string host,
@ -90,6 +91,8 @@ namespace sip {
std::mutex inBuffAccessMutex; std::mutex inBuffAccessMutex;
IncomingConnectionValidator &uriValidator;
void registerAccount(std::string host, void registerAccount(std::string host,
std::string user, std::string user,
std::string password); std::string password);

View File

@ -1,4 +1,5 @@
[sip] [sip]
validUriExpression = *@sip.example.com *@127.0.0.1
host = sip.example.org host = sip.example.org
port = 5060 port = 5060
user = mumsi user = mumsi

View File

@ -1,14 +1,18 @@
#include "PjsuaCommunicator.hpp" #include "PjsuaCommunicator.hpp"
#include "MumbleCommunicator.hpp" #include "MumbleCommunicator.hpp"
#include "IncomingConnectionValidator.hpp"
#include "Configuration.hpp" #include "Configuration.hpp"
#include <log4cpp/FileAppender.hh> #include <log4cpp/FileAppender.hh>
#include <log4cpp/OstreamAppender.hh> #include <log4cpp/OstreamAppender.hh>
#include <log4cpp/PatternLayout.hh>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout); log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout);
appender1->setLayout(new log4cpp::BasicLayout()); log4cpp::PatternLayout layout;
layout.setConversionPattern("%d [%p] %c: %m%n");
appender1->setLayout(&layout);
log4cpp::Category &logger = log4cpp::Category::getRoot(); log4cpp::Category &logger = log4cpp::Category::getRoot();
logger.setPriority(log4cpp::Priority::NOTICE); logger.setPriority(log4cpp::Priority::NOTICE);
logger.addAppender(appender1); logger.addAppender(appender1);
@ -20,9 +24,11 @@ int main(int argc, char *argv[]) {
config::Configuration conf(argv[1]); config::Configuration conf(argv[1]);
sip::IncomingConnectionValidator connectionValidator(conf.getString("sip.validUriExpression"));
boost::asio::io_service ioService; boost::asio::io_service ioService;
sip::PjsuaCommunicator pjsuaCommunicator; sip::PjsuaCommunicator pjsuaCommunicator(connectionValidator);
mumble::MumbleCommunicator mumbleCommunicator(ioService); mumble::MumbleCommunicator mumbleCommunicator(ioService);