diff --git a/IncomingConnectionValidator.cpp b/IncomingConnectionValidator.cpp index 293e920..e47d5dd 100644 --- a/IncomingConnectionValidator.cpp +++ b/IncomingConnectionValidator.cpp @@ -1,11 +1,42 @@ #include "IncomingConnectionValidator.hpp" +#include + +using namespace std; + sip::IncomingConnectionValidator::IncomingConnectionValidator(std::string validUriExpression) : validUriExpression(validUriExpression), logger(log4cpp::Category::getInstance("IncomingConnectionValidator")) { + vector separateUris; + boost::split(separateUris, validUriExpression, boost::is_any_of("\t ")); + for (auto &uri : separateUris) { + boost::replace_all(uri, ".", "\\."); + boost::replace_all(uri, "*", "\\w*"); + uriRegexVec.push_back(regex(uri)); + } } bool sip::IncomingConnectionValidator::validateUri(std::string uri) { - return true; + regex addressRegex("[\"\\w ]*"); + + smatch s; + + if (not regex_match(uri, s, addressRegex)) { + logger.warn("URI has invalid format: %s", uri.c_str()); + return false; + } + + string rawAddress = s[1].str(); + + for (auto ® : uriRegexVec) { + if (regex_match(rawAddress, s, reg)) { + logger.info("URI %s is valid.", rawAddress.c_str()); + return true; + } + } + + logger.info("URI %s not valid.", rawAddress.c_str()); + + return false; } diff --git a/IncomingConnectionValidator.hpp b/IncomingConnectionValidator.hpp index 564bd20..c9a5352 100644 --- a/IncomingConnectionValidator.hpp +++ b/IncomingConnectionValidator.hpp @@ -4,6 +4,7 @@ #include #include +#include namespace sip { class IncomingConnectionValidator : boost::noncopyable { @@ -15,5 +16,6 @@ namespace sip { private: log4cpp::Category &logger; std::string validUriExpression; + std::vector uriRegexVec; }; } \ No newline at end of file diff --git a/PjsuaCommunicator.cpp b/PjsuaCommunicator.cpp index 481e792..b3e76db 100644 --- a/PjsuaCommunicator.cpp +++ b/PjsuaCommunicator.cpp @@ -137,12 +137,16 @@ namespace sip { communicator.logger.notice(msgText); communicator.onStateChange(msgText); } else if (ci.state == PJSIP_INV_STATE_DISCONNECTED) { - auto msgText = "Call from " + address + " finished."; + auto &acc = dynamic_cast<_Account &>(account); - communicator.logger.notice(msgText); - communicator.onStateChange(msgText); + if (not acc.available) { + auto msgText = "Call from " + address + " finished."; - dynamic_cast<_Account &>(account).available = true; + communicator.logger.notice(msgText); + communicator.onStateChange(msgText); + + acc.available = true; + } delete this; } @@ -182,8 +186,9 @@ namespace sip { communicator.logger.info("Incoming call from %s.", uri.c_str()); + pj::CallOpParam param; + if (communicator.uriValidator.validateUri(uri)) { - pj::CallOpParam param; if (available) { param.statusCode = PJSIP_SC_OK; @@ -195,6 +200,8 @@ namespace sip { call->answer(param); } else { communicator.logger.warn("Refusing call from %s.", uri.c_str()); + param.statusCode = PJSIP_SC_DECLINE; + call->hangup(param); } } } diff --git a/README.md b/README.md index 7090bd0..c1fcecd 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,8 @@ make cp config.ini.example config.ini ``` +Remember to add URIs which you want to make calls from. Calls from other URIs won't be answered. + * To run the service, type: ``` ./mumsi config.ini @@ -54,7 +56,6 @@ In this case, use *snd-dummy* sound module. ## TODO: -* SIP authentication - for now it answers every call * multiple simultaneous connections * outgoing connections * text chat commands diff --git a/config.ini.example b/config.ini.example index 005c624..5a02a2b 100644 --- a/config.ini.example +++ b/config.ini.example @@ -1,4 +1,7 @@ [sip] +# list of valid SIP URIs for incoming connections separated by space +# supported wildcards: * +# if you want to allow calls from any URI, write *@* validUriExpression = *@sip.example.com *@127.0.0.1 host = sip.example.org port = 5060