Michał Słomkowski 8 роки тому
батько
коміт
f6bf22bd15

+ 32 - 1
IncomingConnectionValidator.cpp

@@ -1,11 +1,42 @@
 #include "IncomingConnectionValidator.hpp"
 #include "IncomingConnectionValidator.hpp"
 
 
+#include <boost/algorithm/string.hpp>
+
+using namespace std;
+
 sip::IncomingConnectionValidator::IncomingConnectionValidator(std::string validUriExpression)
 sip::IncomingConnectionValidator::IncomingConnectionValidator(std::string validUriExpression)
         : validUriExpression(validUriExpression),
         : validUriExpression(validUriExpression),
           logger(log4cpp::Category::getInstance("IncomingConnectionValidator")) {
           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) {
 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 <log4cpp/Category.hh>
 
 
 #include <string>
 #include <string>
+#include <regex>
 
 
 namespace sip {
 namespace sip {
     class IncomingConnectionValidator : boost::noncopyable {
     class IncomingConnectionValidator : boost::noncopyable {
@@ -15,5 +16,6 @@ namespace sip {
     private:
     private:
         log4cpp::Category &logger;
         log4cpp::Category &logger;
         std::string validUriExpression;
         std::string validUriExpression;
+        std::vector<std::regex> uriRegexVec;
     };
     };
 }
 }

+ 12 - 5
PjsuaCommunicator.cpp

@@ -137,12 +137,16 @@ namespace sip {
             communicator.logger.notice(msgText);
             communicator.logger.notice(msgText);
             communicator.onStateChange(msgText);
             communicator.onStateChange(msgText);
         } else if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
         } 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;
             delete this;
         }
         }
@@ -182,8 +186,9 @@ namespace sip {
 
 
         communicator.logger.info("Incoming call from %s.", uri.c_str());
         communicator.logger.info("Incoming call from %s.", uri.c_str());
 
 
+        pj::CallOpParam param;
+
         if (communicator.uriValidator.validateUri(uri)) {
         if (communicator.uriValidator.validateUri(uri)) {
-            pj::CallOpParam param;
 
 
             if (available) {
             if (available) {
                 param.statusCode = PJSIP_SC_OK;
                 param.statusCode = PJSIP_SC_OK;
@@ -195,6 +200,8 @@ namespace sip {
             call->answer(param);
             call->answer(param);
         } else {
         } else {
             communicator.logger.warn("Refusing call from %s.", uri.c_str());
             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
 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:
 * To run the service, type:
 ```
 ```
 ./mumsi config.ini
 ./mumsi config.ini
@@ -54,7 +56,6 @@ In this case, use *snd-dummy* sound module.
 
 
 ## TODO:
 ## TODO:
 
 
-* SIP authentication - for now it answers every call
 * multiple simultaneous connections
 * multiple simultaneous connections
 * outgoing connections
 * outgoing connections
 * text chat commands
 * text chat commands

+ 3 - 0
config.ini.example

@@ -1,4 +1,7 @@
 [sip]
 [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
 validUriExpression = *@sip.example.com *@127.0.0.1
 host = sip.example.org
 host = sip.example.org
 port = 5060
 port = 5060