diff options
author | Satoshi Fujimoto <satoshi.fujimoto7@gmail.com> | 2017-12-12 15:02:16 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2017-12-16 22:10:33 +0900 |
commit | 1227aceb31af1bdc817ee4d89a72a7a7db874347 (patch) | |
tree | cb3e45dc0a594eb607c222a968ef45f4433d042a | |
parent | 61cdca78d7ee84ac2f0384f2096a42fa54acb6b7 (diff) |
config: Move some functions to util.go
default.go and serve.go has some functions which are not directly
related to its main role.
This commit moves those functions to util.go, a more suitable place.
Signed-off-by: Satoshi Fujimoto <satoshi.fujimoto7@gmail.com>
-rw-r--r-- | config/default.go | 58 | ||||
-rw-r--r-- | config/serve.go | 18 | ||||
-rw-r--r-- | config/util.go | 76 |
3 files changed, 76 insertions, 76 deletions
diff --git a/config/default.go b/config/default.go index b586407c..c3fa4eb3 100644 --- a/config/default.go +++ b/config/default.go @@ -43,64 +43,6 @@ func defaultAfiSafi(typ AfiSafiType, enable bool) AfiSafi { } } -// yaml is decoded as []interface{} -// but toml is decoded as []map[string]interface{}. -// currently, viper can't hide this difference. -// handle the difference here. -func extractArray(intf interface{}) ([]interface{}, error) { - if intf != nil { - list, ok := intf.([]interface{}) - if ok { - return list, nil - } - l, ok := intf.([]map[string]interface{}) - if !ok { - return nil, fmt.Errorf("invalid configuration: neither []interface{} nor []map[string]interface{}") - } - list = make([]interface{}, 0, len(l)) - for _, m := range l { - list = append(list, m) - } - return list, nil - } - return nil, nil -} - -func getIPv6LinkLocalAddress(ifname string) (string, error) { - ifi, err := net.InterfaceByName(ifname) - if err != nil { - return "", err - } - addrs, err := ifi.Addrs() - if err != nil { - return "", err - } - for _, addr := range addrs { - ip := addr.(*net.IPNet).IP - if ip.To4() == nil && ip.IsLinkLocalUnicast() { - return fmt.Sprintf("%s%%%s", ip.String(), ifname), nil - } - } - return "", fmt.Errorf("no ipv6 link local address for %s", ifname) -} - -func isLocalLinkLocalAddress(ifindex int, addr net.IP) (bool, error) { - ifi, err := net.InterfaceByIndex(ifindex) - if err != nil { - return false, err - } - addrs, err := ifi.Addrs() - if err != nil { - return false, err - } - for _, a := range addrs { - if ip, _, _ := net.ParseCIDR(a.String()); addr.Equal(ip) { - return true, nil - } - } - return false, nil -} - func SetDefaultNeighborConfigValues(n *Neighbor, pg *PeerGroup, g *Global) error { // Determines this function is called against the same Neighbor struct, // and if already called, returns immediately. diff --git a/config/serve.go b/config/serve.go index 73e2c7bb..45c70ea9 100644 --- a/config/serve.go +++ b/config/serve.go @@ -75,24 +75,6 @@ func ReadConfigfileServe(path, format string, configCh chan *BgpConfigSet) { } } -func inSlice(n Neighbor, b []Neighbor) int { - for i, nb := range b { - if nb.State.NeighborAddress == n.State.NeighborAddress { - return i - } - } - return -1 -} - -func existPeerGroup(n string, b []PeerGroup) int { - for i, nb := range b { - if nb.Config.PeerGroupName == n { - return i - } - } - return -1 -} - func ConfigSetToRoutingPolicy(c *BgpConfigSet) *RoutingPolicy { return &RoutingPolicy{ DefinedSets: c.DefinedSets, diff --git a/config/util.go b/config/util.go index dfe60d6c..ce49ff63 100644 --- a/config/util.go +++ b/config/util.go @@ -40,6 +40,64 @@ func detectConfigFileType(path, def string) string { } } +// yaml is decoded as []interface{} +// but toml is decoded as []map[string]interface{}. +// currently, viper can't hide this difference. +// handle the difference here. +func extractArray(intf interface{}) ([]interface{}, error) { + if intf != nil { + list, ok := intf.([]interface{}) + if ok { + return list, nil + } + l, ok := intf.([]map[string]interface{}) + if !ok { + return nil, fmt.Errorf("invalid configuration: neither []interface{} nor []map[string]interface{}") + } + list = make([]interface{}, 0, len(l)) + for _, m := range l { + list = append(list, m) + } + return list, nil + } + return nil, nil +} + +func getIPv6LinkLocalAddress(ifname string) (string, error) { + ifi, err := net.InterfaceByName(ifname) + if err != nil { + return "", err + } + addrs, err := ifi.Addrs() + if err != nil { + return "", err + } + for _, addr := range addrs { + ip := addr.(*net.IPNet).IP + if ip.To4() == nil && ip.IsLinkLocalUnicast() { + return fmt.Sprintf("%s%%%s", ip.String(), ifname), nil + } + } + return "", fmt.Errorf("no ipv6 link local address for %s", ifname) +} + +func isLocalLinkLocalAddress(ifindex int, addr net.IP) (bool, error) { + ifi, err := net.InterfaceByIndex(ifindex) + if err != nil { + return false, err + } + addrs, err := ifi.Addrs() + if err != nil { + return false, err + } + for _, a := range addrs { + if ip, _, _ := net.ParseCIDR(a.String()); addr.Equal(ip) { + return true, nil + } + } + return false, nil +} + func (b *BgpConfigSet) getPeerGroup(n string) (*PeerGroup, error) { if n == "" { return nil, nil @@ -140,6 +198,24 @@ func (c AfiSafis) ToRfList() ([]bgp.RouteFamily, error) { return rfs, nil } +func inSlice(n Neighbor, b []Neighbor) int { + for i, nb := range b { + if nb.State.NeighborAddress == n.State.NeighborAddress { + return i + } + } + return -1 +} + +func existPeerGroup(n string, b []PeerGroup) int { + for i, nb := range b { + if nb.Config.PeerGroupName == n { + return i + } + } + return -1 +} + func CheckAfiSafisChange(x, y []AfiSafi) bool { if len(x) != len(y) { return true |