public static List<Hop> traceRoute(string ipAddrOrHostname, GatewayIPAddressInformation gateway, int minHops, int maxHops = 50, int timeout = 5000, byte[] buffer = null) { PingOptions pingOpts = new PingOptions(); Stopwatch watch = new Stopwatch(); //pingOpts.Ttl = 1; // only allow one hop initially // create default buffer to send if not specified if (buffer == null) buffer = new byte[32]; List<Hop> hops = new List<Hop>(); // Always do the first hop, so we can verify expected route is being used; // and if no interface was specified, well, its cheap anyway, since this is inside our PC if (doPing(0, hops, pingOpts, watch, ipAddrOrHostname, timeout, buffer)) { // check it if (gateway!=null) { if (!gateway.Address.ToString().Equals(hops[0].ip)) { MessageBox.Show("WARNING: First hop" + hops[0].ip + " didn't match intended gateway " + gateway.Address.ToString() + "; try 'route print'."); } } } else { // stop if (gateway != null) { MessageBox.Show("Gateway " + gateway.Address.ToString() + " down?"); } else { MessageBox.Show(hops[0].ip); } return hops; } if (minHops > 0) { // prefill for (int i = 1; i < minHops; i++) { hops.Add(new Hop(i, "Dummy entry", -1, false)); } } int start = Math.Max(minHops, 1); for (int i = start; i < maxHops; i++) { if (!doPing(i, hops, pingOpts, watch, ipAddrOrHostname, timeout , buffer)) { // stop break; } } return hops; }
private void traceBtn_Click(object sender, EventArgs e) { EndPoint theEndPoint = null; foreach (EndPoint ep in endpoints) { if (ep.ToString().Equals(addrComboBox.Text)) { theEndPoint = ep; break; } } if (theEndPoint == null) { theEndPoint = new EndPoint(addrComboBox.Text); addrComboBox.Items.Add(theEndPoint); endpoints.Add(theEndPoint); } hostOrIp = theEndPoint.HostOrIp; // start traceroute if not already running, otherwise kill it. if (!tracertBackgroundWorker.IsBusy) { // save user input here nTraces = (int)nTraceUpDown.Value; traceInterval = (double)traceIntUpDown.Value; tStart = DateTime.Now; tEnd = DateTime.Now; networkInterface = getInterface((string)listBoxNetworkInterface.SelectedItem); if (networkInterface != null) { // set the route routeToDestination = NetworkInterfaceUtils.AddRoute(networkInterface, hostOrIp); IPInterfaceProperties properties = networkInterface.GetIPProperties(); GatewayIPAddressInformationCollection gateways = properties.GatewayAddresses; gateway = gateways[0]; // just use the first one } // clear any previously logged data dates.Clear(); pings.Clear(); // start the traceroute in a separate thread tracertBackgroundWorker.RunWorkerAsync(); // update form traceBtn.Text = "Stop"; lblTarget.Text = hostOrIp; lblStartTime.Text = tStart.ToString() + " to"; lblEndTime.Visible = true; lblEndTime.Text = tEnd.ToString(); progressBar1.Visible = true; } else if (tracertBackgroundWorker.IsBusy) { tracertBackgroundWorker.CancelAsync(); traceBtn.Text = "Stopping..."; traceBtn.Enabled = false; // Delete the route if (routeToDestination != null) { NetworkInterfaceUtils.RemoveRoute(routeToDestination); } } }
private Profile MatchNetwork(UnicastIPAddressInformation ipAddr, GatewayIPAddressInformation gw, byte[] addrBytes, byte[] maskBytes) { var maxConfidence = 0; Profile result = null; foreach (var item in _profiles) { var confidence = 6; if (item.Subnet.Value == null) confidence -= 1; else if (!item.Subnet.Value.Equals(ipAddr.IPv4Mask)) confidence = 0; if (item.IPAddress.Value == null) { confidence -= 1; } else { var compBytes = item.IPAddress.Value.GetAddressBytes(); for (var i = 0; i < compBytes.Length; i++) { if ((addrBytes[i] & maskBytes[i]) != (compBytes[i] & maskBytes[i])) { confidence = 0; break; } } } if (item.Gateway.Value == null) confidence -= 1; else if (!item.Gateway.Value.Equals(gw.Address)) confidence = 0; confidence = Math.Max(0, confidence); if (confidence > maxConfidence) { maxConfidence = confidence; result = item; } } return result; }