Implement URI validator.
This commit is contained in:
		
							parent
							
								
									9d4da88e9f
								
							
						
					
					
						commit
						f6bf22bd15
					
				| @ -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) { | ||||||
|  |     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 ® : uriRegexVec) { | ||||||
|  |         if (regex_match(rawAddress, s, reg)) { | ||||||
|  |             logger.info("URI %s is valid.", rawAddress.c_str()); | ||||||
|             return true; |             return true; | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     logger.info("URI %s not valid.", rawAddress.c_str()); | ||||||
|  | 
 | ||||||
|  |     return false; | ||||||
|  | } | ||||||
|  | |||||||
| @ -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; | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| @ -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 &acc = dynamic_cast<_Account &>(account); | ||||||
|  | 
 | ||||||
|  |             if (not acc.available) { | ||||||
|                 auto msgText = "Call from " + address + " finished."; |                 auto msgText = "Call from " + address + " finished."; | ||||||
| 
 | 
 | ||||||
|                 communicator.logger.notice(msgText); |                 communicator.logger.notice(msgText); | ||||||
|                 communicator.onStateChange(msgText); |                 communicator.onStateChange(msgText); | ||||||
| 
 | 
 | ||||||
|             dynamic_cast<_Account &>(account).available = true; |                 acc.available = true; | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|             delete this; |             delete this; | ||||||
|         } |         } | ||||||
| @ -182,9 +186,10 @@ namespace sip { | |||||||
| 
 | 
 | ||||||
|         communicator.logger.info("Incoming call from %s.", uri.c_str()); |         communicator.logger.info("Incoming call from %s.", uri.c_str()); | ||||||
| 
 | 
 | ||||||
|         if (communicator.uriValidator.validateUri(uri)) { |  | ||||||
|         pj::CallOpParam param; |         pj::CallOpParam param; | ||||||
| 
 | 
 | ||||||
|  |         if (communicator.uriValidator.validateUri(uri)) { | ||||||
|  | 
 | ||||||
|             if (available) { |             if (available) { | ||||||
|                 param.statusCode = PJSIP_SC_OK; |                 param.statusCode = PJSIP_SC_OK; | ||||||
|                 available = false; |                 available = false; | ||||||
| @ -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); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -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 | ||||||
|  | |||||||
| @ -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 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Michał Słomkowski
						Michał Słomkowski