// Dumps the entire routing table and structures it func getTable(user *admin.Admin) (table []*Route) { page := 0 var more int64 table = make([]*Route, 0) for more = 1; more != 0; page++ { response, err := admin.NodeStore_dumpTable(user, page) if err != nil { fmt.Printf("%v\n", err) return } // If an error field exists, and we have an error, return it if _, ok := response["error"]; ok { if response["error"] != "none" { err = fmt.Errorf(response["error"].(string)) fmt.Printf("Error: %v\n", err) return } } //Thanks again to SashaCrofter for the table parsing rawTable := response["routingTable"].([]interface{}) for i := range rawTable { item := rawTable[i].(map[string]interface{}) rPath := item["path"].(string) sPath := strings.Replace(rPath, ".", "", -1) bPath, err := hex.DecodeString(sPath) if err != nil || len(bPath) != 8 { //If we get an error, or the //path is not 64 bits, discard. //This should also prevent //runtime errors. continue } path := binary.BigEndian.Uint64(bPath) table = append(table, &Route{ IP: item["ip"].(string), RawPath: path, Path: rPath, RawLink: item["link"].(int64), Link: float64(item["link"].(int64)) / magicalLinkConstant, Version: item["version"].(int64), }) } if response["more"] != nil { more = response["more"].(int64) } else { break } } return }
func doPeers(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 direct peers of", tText) var output admin.Routes for _, node := range table { if usingPath && node.Path.String() != target.Supplied { continue } else if !usingPath && node.IP.String() != target.Target { continue } for _, nodeB := range table { if isOneHop(*node.Path, *nodeB.Path) || isOneHop(*nodeB.Path, *node.Path) { for i, existing := range output { if existing.IP == nodeB.IP { if *existing.Path > *nodeB.Path { table[i] = nodeB } goto alreadyExists } } output = append(output, nodeB) alreadyExists: } } } for _, node := range output { hostname, _ := resolveIP(node.IP.String()) tText := node.IP.String() if hostname != "" { tText += " (" + hostname + ")" } else { tText += "\t" } tText += "\t\t\t\t" for i := 40; i < len(tText) && i < 80; i += 16 { tText = tText[0 : len(tText)-2] } fmt.Printf("IP: %v -- Path: %s -- Link: %.0f\n", tText, node.Path, node.Link) } }