IncomingConnectionValidator.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "IncomingConnectionValidator.hpp"
  2. #include <boost/algorithm/string.hpp>
  3. using namespace std;
  4. sip::IncomingConnectionValidator::IncomingConnectionValidator(std::string validUriExpression)
  5. : validUriExpression(validUriExpression),
  6. logger(log4cpp::Category::getInstance("IncomingConnectionValidator")) {
  7. vector<string> separateUris;
  8. boost::split(separateUris, validUriExpression, boost::is_any_of("\t "));
  9. for (auto &uri : separateUris) {
  10. boost::replace_all(uri, ".", "\\.");
  11. boost::replace_all(uri, "*", "[\\+\\.\\w]*");
  12. uriRegexVec.push_back(boost::regex(uri));
  13. }
  14. }
  15. bool sip::IncomingConnectionValidator::validateUri(std::string uri) {
  16. boost::regex addressRegex("[\"\\+\\w\\. ]*<?sip:([\\+\\w\\.]+@[\\w\\.]+)>?");
  17. boost::smatch s;
  18. if (not boost::regex_match(uri, s, addressRegex)) {
  19. logger.warn("URI has invalid format: %s", uri.c_str());
  20. return false;
  21. }
  22. string rawAddress = s[1].str();
  23. for (auto &reg : uriRegexVec) {
  24. if (boost::regex_match(rawAddress, s, reg)) {
  25. logger.info("URI %s is valid.", rawAddress.c_str());
  26. return true;
  27. }
  28. }
  29. logger.info("URI %s not valid.", rawAddress.c_str());
  30. return false;
  31. }