예제 #1
0
    /// <summary>
    /// Lookup the first IP address external to the LAN.
    /// </summary>
    /// <param name="ip">External host to trace too.</param>
    /// <param name="maxTtl">Maximum number of hops to try.</param>
    /// <param name="response">Trace route result.</param>
    /// <returns>If the trace is successful.</returns>
    public bool TrySearchForFirstExternalAddress(IPAddress ip, int maxTtl, out TraceRouteResponse response)
    {
      for (int i = 0; i < maxTtl; i++)
      {
        if (TryLookupInternal(ip, i, out response)) return true;
      }

      response = null;
      return false;
    }
예제 #2
0
    private bool TryLookupInternal(IPAddress remoteHost, int ttl, out TraceRouteResponse response)
    {
      if (ttl < 1)
      {
        response = null;
        return false;
      }

      if (!NetworkConnectionTracker.IsNetworkConnected)
      {
        ServiceRegistration.Get<ILogger>().Debug("TraceRoute: Lookup - No Network connected");
        response = null;
        return false;
      }

      try
      {
        using (Ping sender = new Ping())
        {
          PingOptions options = new PingOptions(ttl, true);

          PingReply reply = sender.Send(remoteHost, 1000, new byte[32], options);

          if (reply != null)
          {
            response = new TraceRouteResponse()
            {
              RemoteHost = remoteHost,
              FirstResponseTtl = ttl,
              FirstResponseHostname = Dns.GetHostEntry(reply.Address).HostName,
              FirstResponseIP = reply.Address
            };
            return true;
          }
        }
      }
      catch (Exception)
      {
        response = null;
        return false;
      }

      response = null;
      return false;
    }