From 7e3f2e573e972c7214256aeea8f6b95a8c152284 Mon Sep 17 00:00:00 2001 From: Matthias Hannig Date: Thu, 6 Sep 2018 15:46:46 +0200 Subject: [PATCH] improved communities parsing --- backend/config.go | 25 +++++++++++++++++++------ etc/alicelg/alice.example.conf | 6 +++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/backend/config.go b/backend/config.go index 8b66d1e..050f000 100644 --- a/backend/config.go +++ b/backend/config.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" "os" "strconv" "strings" @@ -307,11 +308,19 @@ func getBgpCommunities(config *ini.File) BgpCommunities { return communities // nothing else to do here, go with the default } - keys := communitiesConfig.Keys() - // Merge communities - for _, key := range keys { - community := strings.Replace(key.Name(), "_", ":", -1) - communities[community] = communitiesConfig.Key(key.Name()).MustString("") + // Parse and merge communities + lines := strings.Split(communitiesConfig.Body(), "\n") + for _, line := range lines { + kv := strings.SplitN(line, "=", 2) + if len(kv) != 2 { + log.Println("Skipping malformed BGP community:", line) + continue + } + + community := strings.TrimSpace(kv[0]) + label := strings.TrimSpace(kv[1]) + + communities[community] = label } return communities @@ -484,7 +493,11 @@ func loadConfig(file string) (*Config, error) { return nil, err } - parsedConfig, err := ini.LooseLoad(file) + // Load configuration, but handle bgp communities section + // with our own parser + parsedConfig, err := ini.LoadSources(ini.LoadOptions{ + UnparseableSections: []string{"bgp_communities"}, + }, file) if err != nil { return nil, err } diff --git a/etc/alicelg/alice.example.conf b/etc/alicelg/alice.example.conf index aa07992..cb58eda 100644 --- a/etc/alicelg/alice.example.conf +++ b/etc/alicelg/alice.example.conf @@ -46,10 +46,10 @@ load_on_demand = true # Default: false 7 = The Sender has set (peerRTTLowerDeny:ms) and the targets RTT ms <= then the ms in the community # Define Known Bgp Communities -# Caveat: The usual : must be substitued by a _ because of ini parsing limitations. [bgp_communities] -1_23 = some tag -9033_65666_1 = ip bogon detected +1:23 = some tag +# Another +9033:65666:1 = ip bogon detected # # Define columns for neighbours and routes table,