예제 #1
0
   bool 
   LocalIPAddresses::IsLocalPort(const IPAddress &address, int port)
   {
      std::vector<std::pair<IPAddress, int> >::iterator iter = m_vecLocalPorts.begin();
      std::vector<std::pair<IPAddress, int> >::iterator iterEnd = m_vecLocalPorts.end();

      bool loopbackAddress = IsWithinLoopbackRange(address);

      for (; iter != iterEnd; iter++)
      {
         IPAddress usedAddress = (*iter).first;
         unsigned long usedPort = (*iter).second;
       
         if (loopbackAddress)
         {
            if (usedAddress.IsAny() && usedPort == port)
               return true;
         }
         else
         {
            if (usedAddress == address && usedPort == port)
               return true;
         }

      }

      return false;
   }
   bool 
   LocalIPAddresses::IsLocalPort(const IPAddress &address, int port)
   {
      auto iter = local_ports_.begin();
      auto iterEnd = local_ports_.end();

      bool loopbackAddress = IsWithinLoopbackRange(address);

      for (; iter != iterEnd; iter++)
      {
         IPAddress usedAddress = (*iter).first;
         unsigned long usedPort = (*iter).second;
       
         if (loopbackAddress)
         {
            if (usedAddress.IsAny() && usedPort == port)
               return true;
         }
         else
         {
            if (usedAddress == address && usedPort == port)
               return true;
         }

      }

      return false;
   }
예제 #3
0
   bool
   TCPConnection::Connect(const AnsiString &remote_ip_address, long remotePort, const IPAddress &localAddress)
   {
#if _DEBUG
      if (!StringParser::IsValidIPAddress(remote_ip_address))
      {
         ErrorManager::Instance()->ReportError(ErrorManager::High, 5506, "TCPConnection::Connect", 
            Formatter::Format("Attempting to connect to {0} - Not a valid IP address.", remote_ip_address));
      }
#endif

      remote_port_ = remotePort;
      remote_ip_address_ = remote_ip_address;
      is_client_ = true;

      LOG_TCPIP(Formatter::Format("Connecting to {0}:{1}...", remote_ip_address_, remotePort));

      if (!localAddress.IsAny())
      {
         boost::system::error_code error_code;

         if (localAddress.GetType() == IPAddress::IPV4)
            socket_.open(boost::asio::ip::tcp::v4(), error_code);
         else if (localAddress.GetType() == IPAddress::IPV6)
            socket_.open(boost::asio::ip::tcp::v6(), error_code);

         if (error_code)
         {
            String errorMessage = Formatter::Format("Failed to open local socket on IP address {0}", localAddress.ToString());
            OnCouldNotConnect(errorMessage);
            ReportError(ErrorManager::Medium, 5520, "TCPConnection::Connect", errorMessage, error_code);
            return false;
         }

         socket_.bind(boost::asio::ip::tcp::endpoint(localAddress.GetAddress(), 0), error_code);


         if (error_code)
         {
            String errorMessage = Formatter::Format("Failed to bind to IP address {0}.", localAddress.ToString());
            ReportError(ErrorManager::Medium, 4330, "TCPConnection::Connect", errorMessage, error_code);
            OnCouldNotConnect(errorMessage);

            boost::system::error_code ignored_error_code;
            socket_.close(ignored_error_code);
            return false;
         }
      }

      // Start an asynchronous resolve to translate the server and service names
      // into a list of endpoints.
      StartAsyncConnect_(remote_ip_address, remotePort);
      return true;
   }
예제 #4
0
   bool
   TestConnect::PerformTest(const String  &localAddressStr, const String &server, int port, String &result)
   {
      boost::asio::io_service io_service;

      IPAddress localAddress;
      if (!localAddressStr.IsEmpty())
      {
         if (!localAddress.TryParse(localAddressStr, false))
         {
            result.append(Formatter::Format("ERROR: Unable to parse address {0}.\r\n", localAddressStr));
            return false;
         }
         else
            result.append(Formatter::Format("Local address is {0}.\r\n", localAddress.ToString()));
      }

      result.append(Formatter::Format("Trying to connect to host {0}...\r\n", server));
      

      // Get a list of endpoints corresponding to the server name.
      tcp::resolver resolver(io_service);
      tcp::resolver::query query(AnsiString(server), AnsiString(StringParser::IntToString(port)), tcp::resolver::query::numeric_service);
      boost::system::error_code errorResolve = boost::asio::error::host_not_found;
      tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, errorResolve);
      tcp::resolver::iterator end;

      if (errorResolve || endpoint_iterator == end)
      {
         // Host was not found.
         String formattedString;
         formattedString.Format(_T("ERROR: The host name %s could not be resolved.\r\n"), server);
         
         result.append(formattedString);
         return false;
      }

      // Try each endpoint until we successfully establish a connection.
      tcp::socket socket(io_service);
      boost::system::error_code error = boost::asio::error::host_not_found;
      while (error && endpoint_iterator != end)
      {
         boost::asio::ip::address adr = (*endpoint_iterator).endpoint().address();

         String ipAddressString = adr.to_string();
         String formattedString;
         formattedString.Format(_T("Trying to connect to TCP/IP address %s on port %d.\r\n"), ipAddressString, port);

         result.append(formattedString);

         socket.close();

         IPAddress emptyAddr;
         bool any = emptyAddr.IsAny();

         if (!localAddress.IsAny())
         {
            socket.open(boost::asio::ip::tcp::v4());
            boost::system::error_code tempError;
            socket.bind(boost::asio::ip::tcp::endpoint(localAddress.GetAddress(), 0), tempError);

            if (tempError)
            {
               result.append(Formatter::Format("ERROR: Unable to bind to address {0}.\r\n", localAddress.ToString()));
               socket.close();
               return false;
            }
         }

         socket.connect(*endpoint_iterator++, error);
      }

      if (error)
      {
         // We were unable to connect.
         result.append(_T("ERROR: It was not possible to connect.\r\n"));
         return false;
      }

      // Read the response status line.
      boost::asio::streambuf response;
      boost::asio::read_until(socket, response, "\r\n");

      std::string s;
      std::istream is(&response);
      std::getline(is, s, '\r');
      result.append(Formatter::Format("Received: {0}.\r\n", String(s)));

      // Close the socket again.
      socket.close();

      result.append(_T("Connected successfully.\r\n"));
      return true;

   }
   void MessageUtilitiesTester::Test()
   {
      std::list<String> headerLines;
      headerLines.push_back("from host.edu (host.edu [1.2.3.4]) by mail.host.edu (8.8.5) id 004A21; Tue, Mar 18 1997 14:36:17 -0800 (PST)\r\n");
      headerLines.push_back("from someone.google (someone.google [1.1.1.1]) by mail.host.edu (8.8.5) id 004A21; Tue, Mar 18 1997 14:36:17 -0800 (PST)\r\n");

      String hostName;
      IPAddress address;

      bool result = MessageUtilities::RetrieveOriginatingAddress(headerLines, hostName, address);
      
      if (result == false)
         throw;

      if (hostName != _T("host.edu"))
         throw;

      if (address.ToString() != "1.2.3.4")
         throw;

      if (address.IsAny())
         throw;

      // If the message
      headerLines.clear();
      headerLines.push_back("by 10.103.12.12 with SMTP id p12cs158601mui; Mon, 14 Dec 2009 08:53:48 -0800 (PST)");
      headerLines.push_back("by 10.224.63.1 with SMTP id z1mr3063080qah.314.1260809627613; Mon, 14 Dec 2009 08:53:47 -0800 (PST)");
      headerLines.push_back("from e113.en25.com (e113.en25.com [209.167.231.113]) by mx.google.com with ESMTP id 9si6782547iwn.6.2009.12.14.08.53.46; Mon, 14 Dec 2009 08:53:47 -0800 (PST)");

      result = MessageUtilities::RetrieveOriginatingAddress(headerLines, hostName, address);

      if (result == false)
         throw;

      if (hostName != _T("e113.en25.com"))
         throw;

      if (address.ToString() != "209.167.231.113")
         throw;


      if (address.IsAny())
         throw;


      // We are unable to parse this because the IP address isn't within brackets ([]).
      headerLines.clear();
      headerLines.push_back("(qmail 25533 invoked from network); 19 Jan 2010 20:06:21 +0100");
      headerLines.push_back("from ded943.maximumasp.com (HELO mail.hmailserver.com) (216.128.26.134)");

      result = MessageUtilities::RetrieveOriginatingAddress(headerLines, hostName, address);

      if (result == true)
         throw;

      if (hostName != _T(""))
         throw;

      if (address.ToString() != "0.0.0.0")
         throw;

      if (!address.IsAny())
         throw;
   }