Exemple #1
0
func TestAddressIDAddresser(t *testing.T) {
	if nodeID := "1.2.4.5"; report.AddressIDAddresser(nodeID) != nil {
		t.Errorf("%q: bad node ID parsed as good", nodeID)
	}
	var (
		nodeID = report.MakeAddressNodeID(clientHostID, clientAddress)
		want   = net.ParseIP(clientAddress)
		have   = report.AddressIDAddresser(nodeID)
	)
	if !reflect.DeepEqual(want, have) {
		t.Errorf("want %s, have %s", want, have)
	}
}
Exemple #2
0
// discover reads reports from a collector and republishes them on the
// publisher, while scanning the reports for IPs to connect to. Only addresses
// in the network topology of the report are considered. IPs listed in fixed
// are always skipped.
func discover(c collector, p publisher, fixed []string) {
	lastSeen := map[string]time.Time{}

	avoid := makeAvoid(fixed)

	for r := range c.Reports() {
		// log.Printf("got a report")
		p.Publish(r)

		var (
			now       = time.Now()
			localNets = render.LocalNetworks(r)
		)

		for _, adjacent := range r.Address.Adjacency {
			for _, a := range adjacent {
				ip := report.AddressIDAddresser(a) // address id -> IP
				if ip == nil {
					continue
				}

				addr := ip.String()
				if _, ok := avoid[addr]; ok {
					continue
				}
				// log.Printf("potential address: %v (via %s)", addr, src)
				if _, ok := lastSeen[addr]; !ok {
					if interestingAddress(localNets, addr) {
						log.Printf("discovery %v: potential probe address", addr)
						c.Add(addressToDial(addr))
					} else {
						log.Printf("discovery %v: non-probe address", addr)
					}
				}

				// We always add addr to lastSeen[], even if it's a non-local
				// address. This way they are part of the normal timeout logic,
				// and we won't analyze the address again until it re-appears
				// after a timeout.
				lastSeen[addr] = now
			}
		}

		for addr, last := range lastSeen {
			if now.Sub(last) > trafficTimeout {
				// Timeout can be for a non-local address, which we didn't
				// connect to. In that case the RemoveAddress() call won't do
				// anything.
				log.Printf("discovery %v: traffic timeout", addr)
				delete(lastSeen, addr)
				c.Remove(addressToDial(addr))
			}
		}
	}
}