1
0
mirror of https://github.com/bgp/stayrtr.git synced 2024-05-06 15:54:54 +00:00
bgp-stayrtr/prefixfile/prefixfile.go

83 lines
1.8 KiB
Go
Raw Normal View History

package prefixfile
import (
2019-11-05 11:41:22 -08:00
"errors"
"fmt"
2018-11-12 20:59:38 -08:00
"net"
"strconv"
2018-11-12 20:59:38 -08:00
"strings"
)
type VRPJson struct {
Prefix string `json:"prefix"`
Length uint8 `json:"maxLength"`
ASN interface{} `json:"asn"`
TA string `json:"ta,omitempty"`
Expires int `json:"expires,omitempty"`
}
type MetaData struct {
2021-07-17 13:46:20 +00:00
Counts int `json:"vrps"`
Buildtime string `json:"buildtime,omitempty"`
}
type VRPList struct {
Metadata MetaData `json:"metadata,omitempty"`
Data []VRPJson `json:"roas"` // for historical reasons this is called 'roas', but should've been called vrps
}
func (vrp *VRPJson) GetASN2() (uint32, error) {
switch asnc := vrp.ASN.(type) {
2019-11-05 11:41:22 -08:00
case string:
asnStr := strings.TrimLeft(asnc, "aAsS")
asnInt, err := strconv.ParseUint(asnStr, 10, 32)
if err != nil {
return 0, errors.New(fmt.Sprintf("Could not decode ASN string: %v", vrp.ASN))
2019-11-05 11:41:22 -08:00
}
asn := uint32(asnInt)
return asn, nil
case uint32:
return asnc, nil
2019-11-05 11:41:22 -08:00
case float64:
return uint32(asnc), nil
case int:
return uint32(asnc), nil
default:
return 0, errors.New(fmt.Sprintf("Could not decode ASN: %v", vrp.ASN))
2019-11-05 11:41:22 -08:00
}
}
func (vrp *VRPJson) GetASN() uint32 {
asn, _ := vrp.GetASN2()
2019-11-05 11:41:22 -08:00
return asn
}
func (vrp *VRPJson) GetPrefix2() (*net.IPNet, error) {
_, prefix, err := net.ParseCIDR(vrp.Prefix)
2019-11-05 11:41:22 -08:00
if err != nil {
2021-07-27 19:48:04 +00:00
return nil, errors.New(fmt.Sprintf("Could not decode prefix: %v", vrp.Prefix))
}
2019-11-05 11:41:22 -08:00
return prefix, nil
}
func (vrp *VRPJson) GetPrefix() *net.IPNet {
prefix, _ := vrp.GetPrefix2()
return prefix
}
func (vrp *VRPJson) GetMaxLen() int {
return int(vrp.Length)
2019-10-25 12:47:12 -07:00
}
func (vrp *VRPJson) String() string {
return fmt.Sprintf("%v/%v/%v", vrp.Prefix, vrp.Length, vrp.ASN)
2018-11-12 20:59:38 -08:00
}
func GetIPBroadcast(ipnet net.IPNet) net.IP {
br := make([]byte, len(ipnet.IP))
for i := 0; i < len(ipnet.IP); i++ {
br[i] = ipnet.IP[i] | (^ipnet.Mask[i])
}
return net.IP(br)
}