Exemplo n.º 1
0
func NewDomainRegionTree() *RegionTree {
	//	tbitRadix := bitradix.New32()
	return &RegionTree{
		Radix32: bitradix.New32(),
		RWMutex: &sync.RWMutex{},
	}
}
Exemplo n.º 2
0
func processLine(line string) {
	f := strings.SplitN(line, " ", 4)

	if len(f) < 3 {
		log.Printf("Did not split line into enough parts\nLINE: [%s]\nF: %#v\n", line, f)
		return
	}

	neighbor_ip := f[1]
	command := f[2]

	neighbors_lock.RLock()
	defer neighbors_lock.RUnlock()

	if neighbors[neighbor_ip] == nil {
		neighbors_lock.RUnlock()
		neighbors_lock.Lock()

		neighbor := new(Neighbor)
		neighbor.trie = bitradix.New32()
		neighbors[neighbor_ip] = neighbor

		neighbors_lock.Unlock()
		neighbors_lock.RLock()
		defer neighbors_lock.RUnlock() // double?
	}

	neighbor := neighbors[neighbor_ip]

	neighbor.lock.Lock()
	defer neighbor.lock.Unlock()

	switch command {
	case "up", "connected", "down":
		neighbor.State = command
		log.Println("State change", line)
		return
	case "update":
		neighbor.State = "update " + f[3]
		return
	case "announced":
		// fmt.Printf("R: %#v\n", r)

		neighbor.Updates++

		route := parseRoute(f[3])

		if ones, _ := route.Prefix.Mask.Size(); ones < 8 || ones > 25 {
			// fmt.Println("prefix mask too big or small", route.Prefix)
		} else {
			if neighbor.AsnPrefix == nil {
				neighbor.AsnPrefix = make(map[ASN]Prefixes)
			}
			if neighbor.PrefixAsn == nil {
				neighbor.PrefixAsn = make(Prefixes)
			}

			if neighbor.AsnPrefix[route.PrimaryASN] == nil {
				neighbor.AsnPrefix[route.PrimaryASN] = make(Prefixes)
			}

			neighbor.AsnPrefix[route.PrimaryASN][route.Prefix.String()] = 0
			neighbor.PrefixAsn[route.Prefix.String()] = route.PrimaryASN

			addRoute(neighbor.trie, route.Prefix, route)
		}
	case "withdrawn":

		neighbor.Updates++

		route := parseRoute(f[3])

		removeRoute(neighbor.trie, route.Prefix)

		if asn, exists := neighbor.PrefixAsn[route.Prefix.String()]; exists {
			// fmt.Println("Removing ASN from prefix", asn, route.Prefix)
			delete(neighbor.PrefixAsn, route.Prefix.String())
			delete(neighbor.AsnPrefix[asn], route.Prefix.String())
		} else {
			log.Println("Could not find prefix in PrefixAsn")
			log.Println("%#v", neighbor.PrefixAsn)
		}
	default:
		err_text := fmt.Sprintf("Command not implemented: %s\n%s\n", command, line)
		log.Println(err_text)
		err := fmt.Errorf(err_text)
		panic(err)
	}

	if neighbor.Updates%25000 == 0 {
		log.Printf("Processed %v updates from %s\n", neighbor.Updates, neighbor_ip)
	}

}