Esempio n. 1
0
// Pings a node and generates statistics
func pingNode(user *admin.Conn, ping *Ping) (err error) {
	response, version, err := user.RouterModule_pingNode(ping.Target, PingTimeout)

	ping.Sent++
	if err == nil {
		if response >= PingTimeout {
			ping.Response =
				fmt.Sprintf("Timeout from %v after %vms",
					ping.Target, response)
			ping.Error = "timeout"
			ping.Failed++
		} else {
			ping.Success++
			ping.Response =
				fmt.Sprintf("Reply from %v req=%v time=%v ms",
					ping.Target, ping.Success+ping.Failed, response)

			ping.CTime = float64(response)
			ping.TTime += ping.CTime
			ping.TTime2 += ping.CTime * ping.CTime
			if ping.TMin == 0 {
				ping.TMin = ping.CTime
			}
			if ping.CTime > ping.TMax {
				ping.TMax = ping.CTime
			}
			if ping.CTime < ping.TMin {
				ping.TMin = ping.CTime
			}

			if ping.Version == "" {
				ping.Version = version
			}
			if ping.Version != version {
				//not likely we'll see this happen but it doesnt hurt to be prepared
				fmt.Println("Host is sending back mismatched versions")
				fmt.Println("Old:", version, "New:", version)
			}
		}
	} else {
		ping.Failed++
		ping.Error = err.Error()
		ping.Response = err.Error()
		return
	}
	return
}
Esempio n. 2
0
func doOwnPeers(user *admin.Conn) {
	peers, err := user.InterfaceController_peerStats()
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, node := range peers {
		key := node.PublicKey

		hostname, _ := resolveIP(key.IP().String())
		tText := key.IP().String()
		if hostname != "" {
			tText += " (" + hostname + ")"
		} else {
			tText += " "
		}
		fmt.Printf("Incoming: %t | IP: %s -- Path: %s\n", node.IsIncoming, tText, node.SwitchLabel) //, node., node.Link)
	}
}
Esempio n. 3
0
// TODO(inhies): Make the output nicely formatted
func doTraceroute(user *admin.Conn, target Target) {
	table, err := user.NodeStore_dumpTable()
	if err != nil {
		fmt.Println(err)
		return
	}
	usingPath := false
	var tText string
	if validIP(target.Supplied) {
		hostname, _ := resolveIP(target.Target)
		if hostname != "" {
			tText = target.Supplied + " (" + hostname + ")"
		} else {
			tText = target.Supplied
		}
		// If we were given a path, resolve the IP
	} else if validPath(target.Supplied) {
		usingPath = true
		tText = target.Supplied
		//table := getTable(globalData.User)
		for _, v := range table {
			if v.Path.String() == target.Supplied {
				// We have the IP now
				tText = target.Supplied + " (" + v.IP.String() + ")"

				// Try to get the hostname
				hostname, _ := resolveIP(v.IP.String())
				if hostname != "" {
					tText = target.Supplied + " (" + v.IP.String() + " (" + hostname + "))"
				}
			}
		}
		// We were given a hostname, everything is already done for us!
	} else if validHost(target.Supplied) {
		tText = target.Supplied + " (" + target.Target + ")"
	}

	fmt.Println("Finding all routes to", tText)

	count := 0
	for i := range table {
		if usingPath {
			if table[i].Path.String() != target.Supplied {
				continue
			}
		} else {
			if table[i].IP.String() != target.Target {
				continue
			}
		}

		if table[i].Link < 1 {
			continue
		}

		response := table.Hops(*table[i].Path)
		if err != nil {
			fmt.Println("Error:", err)
		}
		response.SortByPath()
		count++
		fmt.Printf("\nRoute #%d to target: %v\n", count, table[i].Path)
		for y, p := range response {
			hostname, _ := resolveIP(p.IP.String())
			var IP string
			if hostname != "" {
				IP = p.IP.String() + " (" + hostname + ")"
			} else {
				IP = p.IP.String()
			}
			fmt.Printf("IP: %v -- Version: %d -- Path: %s -- Link: %s -- Time:", IP, p.Version, p.Path, p.Link)
			if y == 0 {
				fmt.Printf(" Skipping ourself\n")
				continue
			}
			for x := 1; x <= 3; x++ {
				tRoute := &Ping{}
				tRoute.Target = p.Path.String()
				err := pingNode(user, tRoute)
				if err != nil {
					fmt.Println("Error:", err)
					return
				}
				if tRoute.Error == "timeout" {
					fmt.Printf("   *  ")
				} else {
					fmt.Printf(" %vms", tRoute.TTime)
				}
			}
			fmt.Println("")
		}
	}
	fmt.Println("Found", count, "routes")
}