Example #1
0
// quaggaBGP establishes a connection with the Quagga BGP daemon.
func quaggaBGP(asn uint32) (*quagga.BGP, error) {
	bgp := quagga.NewBGP("", asn)
	if err := bgp.Dial(); err != nil {
		return nil, err
	}
	if err := bgp.Enable(); err != nil {
		bgp.Close()
		return nil, err
	}
	return bgp, nil
}
Example #2
0
func main() {
	network := fmt.Sprintf("network %s", testAnycastStr)
	ip, testAnycast, err := net.ParseCIDR(testAnycastStr)
	if err != nil {
		log.Fatal(err)
	}
	testAnycast.IP = ip

	log.Print("Connecting to Quagga BGP daemon...")
	bgp := quagga.NewBGP("", 64512)
	if err := bgp.Dial(); err != nil {
		log.Fatalf("BGP dial failed: %v", err)
	}
	if err := bgp.Enable(); err != nil {
		log.Fatalf("BGP enabled failed: %v", err)
	}

	neighbors, err := bgp.Neighbors()
	if err != nil {
		log.Fatalf("Failed to get neighbors: %v", err)
	}
	log.Printf("Got %d neighbor(s)", len(neighbors))
	for _, n := range neighbors {
		log.Printf("Neighbor %v, remote AS %v", n.IP, n.ASN)
		log.Printf(" State %v, uptime %v", n.BGPState, n.Uptime)
	}

	// Get the current configuration and ensure that the network statement
	// does not already exist.
	cfg, err := bgp.Configuration()
	if err != nil {
		log.Fatalf("Failed to get BGP configuration: %v", err)
	}
	for _, s := range cfg {
		if strings.Contains(s, network) {
			log.Printf("Network statement already exists - %q", s)
		}
	}
	ocfg := cfg

	// Advertise and withdraw the test anycast address.
	log.Printf("Advertising network %s", testAnycastStr)
	if err := bgp.Advertise(testAnycast); err != nil {
		log.Fatalf("Failed to advertise network: %v", err)
	}
	cfg, err = bgp.Configuration()
	if err != nil {
		log.Fatalf("Failed to get BGP configuration: %v", err)
	}
	found := false
	for _, s := range cfg {
		if strings.Contains(s, network) {
			found = true
		}
	}
	if !found {
		log.Printf("Failed to find network statement - %s", network)
	}
	log.Printf("Withdrawing network %s", testAnycastStr)
	if bgp.Withdraw(testAnycast); err != nil {
		log.Fatalf("Failed to withdraw network: %v", err)
	}

	// The configuration should now match what we started with...
	cfg, err = bgp.Configuration()
	if err != nil {
		log.Fatalf("Failed to get BGP configuration: %v", err)
	}
	if len(ocfg) != len(cfg) {
		log.Printf("Number of lines in configuration changed: %d != %d",
			len(ocfg), len(cfg))
	}
	for i := range ocfg {
		if ocfg[i] != cfg[i] {
			log.Printf("Configuration lines mismatched: %q != %q",
				ocfg[i], cfg[i])
			break
		}
	}

	log.Print("Disconnecting from Quagga BGP daemon")
	if err := bgp.Close(); err != nil {
		log.Fatalf("Failed to close BGP connection: %v", err)
	}
	log.Print("Tests completed!")
}