2019-05-10 11:17:55 +02:00
|
|
|
package gobgp
|
2019-02-26 16:59:40 +01:00
|
|
|
|
|
|
|
import (
|
2019-05-10 11:17:55 +02:00
|
|
|
// Standard imports
|
2019-02-26 16:59:40 +01:00
|
|
|
"crypto/sha1"
|
|
|
|
"fmt"
|
2019-05-10 11:17:55 +02:00
|
|
|
"io"
|
2019-02-26 16:59:40 +01:00
|
|
|
|
|
|
|
// External imports
|
|
|
|
api "github.com/osrg/gobgp/api"
|
|
|
|
// Internal imports
|
|
|
|
)
|
|
|
|
|
2021-10-26 18:05:11 +02:00
|
|
|
// PeerHash calculates a peer hash
|
2019-05-10 11:17:55 +02:00
|
|
|
func PeerHash(peer *api.Peer) string {
|
2019-05-10 11:40:26 +02:00
|
|
|
return PeerHashWithASAndAddress(peer.State.PeerAs, peer.State.NeighborAddress)
|
|
|
|
}
|
|
|
|
|
2021-10-26 18:05:11 +02:00
|
|
|
// PeerHashWithASAndAddress creates a peer hash (sha1) from
|
|
|
|
// the ASN and the address.
|
2019-05-10 11:40:26 +02:00
|
|
|
func PeerHashWithASAndAddress(asn uint32, address string) string {
|
2019-02-26 16:59:40 +01:00
|
|
|
h := sha1.New()
|
2021-10-26 18:05:11 +02:00
|
|
|
io.WriteString(h, fmt.Sprintf("%v", asn))
|
2019-05-10 11:40:26 +02:00
|
|
|
io.WriteString(h, address)
|
2019-02-26 16:59:40 +01:00
|
|
|
sum := h.Sum(nil)
|
2019-05-10 11:17:55 +02:00
|
|
|
return fmt.Sprintf("%x", sum[0:5])
|
|
|
|
}
|