diff --git a/cmd/polad/main.go b/cmd/polad/main.go index 537b2af..9576c6d 100644 --- a/cmd/polad/main.go +++ b/cmd/polad/main.go @@ -6,13 +6,31 @@ package main import ( + "flag" "log" + "github.com/nttcom/pola/internal/config" "github.com/nttcom/pola/pkg/server" ) +type Flags struct { + ConfigFile string +} + func main() { - if err := server.NewPce(); err != nil { + f := new(Flags) + flag.StringVar(&f.ConfigFile, "f", "polad.yaml", "Specify a configuration file") + flag.Parse() + + c, err := config.ReadConfigFile(f.ConfigFile) + if err != nil { + log.Fatal(err) + } + + o := new(server.PceOptions) + o.PcepAddr = c.Global.Address + o.PcepPort = c.Global.Port + if err := server.NewPce(o); err != nil { log.Fatal(err) } } diff --git a/configs/polad.yaml.sample b/configs/polad.yaml.sample new file mode 100644 index 0000000..0df7069 --- /dev/null +++ b/configs/polad.yaml.sample @@ -0,0 +1,3 @@ +global: + address: "127.0.0.1" + port: 4189 diff --git a/go.mod b/go.mod index 7b9ab39..b444824 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,15 @@ module github.com/nttcom/pola go 1.17 require ( - google.golang.org/grpc v1.45.0 - google.golang.org/protobuf v1.28.0 + google.golang.org/grpc v1.45.0 + google.golang.org/protobuf v1.28.0 ) require ( - github.com/golang/protobuf v1.5.2 // indirect - golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect - golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect - golang.org/x/text v0.3.0 // indirect - google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect + github.com/go-yaml/yaml v2.1.0+incompatible // indirect + github.com/golang/protobuf v1.5.2 // indirect + golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect + golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect + golang.org/x/text v0.3.0 // indirect + google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect ) diff --git a/go.sum b/go.sum index e75a1fd..62acd0b 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o= +github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..4d70546 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,31 @@ +package config + +import ( + "log" + "os" + + "github.com/go-yaml/yaml" +) + +type Global struct { + Address string `yaml:"address"` + Port string `yaml:"port"` +} + +type Config struct { + Global Global `yaml:"global"` +} + +func ReadConfigFile(configFile string) (Config, error) { + c := new(Config) + + f, err := os.Open(configFile) + if err != nil { + log.Fatal(err) + return *c, err + } + defer f.Close() + + err = yaml.NewDecoder(f).Decode(&c) + return *c, err +} diff --git a/pkg/server/server.go b/pkg/server/server.go index fc27f13..fdda10b 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -19,10 +19,6 @@ import ( "github.com/nttcom/pola/pkg/packet/pcep" ) -// TODO: 引数やconfファイルで上書きできるようにする -const PCEPORT = "4189" -const PCEADDR = "10.100.0.252" - type lsp struct { peerAddr net.IP // 後々 router ID, router name などに変更したい plspId uint32 @@ -36,11 +32,16 @@ type Server struct { pb.UnimplementedPceServiceServer } -func NewPce() error { +type PceOptions struct { + PcepAddr string + PcepPort string +} + +func NewPce(o *PceOptions) error { s := &Server{} // PCEP の Listen を開始する go func() { - if err := s.Listen(); err != nil { + if err := s.Listen(o.PcepAddr, o.PcepPort); err != nil { fmt.Printf("PCEP listen Error\n") } @@ -61,13 +62,12 @@ func NewPce() error { } } -func (s *Server) Listen() error { +func (s *Server) Listen(address string, port string) error { // PCEP の listen を行う - // PCEPPORT = "4189" 宛の SYN は全て accept var listenInfo strings.Builder - listenInfo.WriteString(PCEADDR) + listenInfo.WriteString(address) listenInfo.WriteString(":") - listenInfo.WriteString(PCEPORT) + listenInfo.WriteString(port) fmt.Printf("[server] PCE Listen: %s\n", listenInfo.String()) listener, err := net.Listen("tcp", listenInfo.String()) if err != nil { @@ -77,7 +77,6 @@ func (s *Server) Listen() error { defer listener.Close() sessionId := uint8(1) for { - // PCEPPORT = "4189" へ SYN が来るたびに Accept session := NewSession(sessionId) fmt.Printf("%#v\n", s)