Browse Source

Implement URI validator.

Michał Słomkowski 8 years ago
parent
commit
f6bf22bd15
5 changed files with 51 additions and 7 deletions
  1. 32 1
      IncomingConnectionValidator.cpp
  2. 2 0
      IncomingConnectionValidator.hpp
  3. 12 5
      PjsuaCommunicator.cpp
  4. 2 1
      README.md
  5. 3 0
      config.ini.example

+ 32 - 1
IncomingConnectionValidator.cpp

@@ -1,11 +1,42 @@
 #include "IncomingConnectionValidator.hpp"
 
+#include <boost/algorithm/string.hpp>
+
+using namespace std;
+
 sip::IncomingConnectionValidator::IncomingConnectionValidator(std::string validUriExpression)
         : validUriExpression(validUriExpression),
           logger(log4cpp::Category::getInstance("IncomingConnectionValidator")) {
 
+    vector<string> 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 ]*<sip:([\\w\\.]+@[\\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 &reg : 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;
 }

+ 2 - 0
IncomingConnectionValidator.hpp

@@ -4,6 +4,7 @@
 #include <log4cpp/Category.hh>
 
 #include <string>
+#include <regex>
 
 namespace sip {
     class IncomingConnectionValidator : boost::noncopyable {
@@ -15,5 +16,6 @@ namespace sip {
     private:
         log4cpp::Category &logger;
         std::string validUriExpression;
+        std::vector<std::regex> uriRegexVec;
     };
 }

+ 12 - 5
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);
         }
     }
 }

+ 2 - 1
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

+ 3 - 0
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