diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..d912156 --- /dev/null +++ b/config/config.go @@ -0,0 +1 @@ +package config diff --git a/config/type.go b/config/type.go new file mode 100644 index 0000000..658ff71 --- /dev/null +++ b/config/type.go @@ -0,0 +1,7 @@ +package config + +type Config struct { + IP string `json:"multicastAddr"` + MulticastInterface string `json:"multicastInterface"` + Port int `json:"MulticastPort"` +} diff --git a/main.go b/main.go index 8352b66..a2c7ef0 100644 --- a/main.go +++ b/main.go @@ -5,14 +5,13 @@ import ( "log" "net" + "github.com/crip/config" "github.com/crip/ripv2" - "github.com/crip/ripv2/routes" - "github.com/crip/rtable" ) func main() { rip := ripv2.New() - routes := routes.New() + routes := ripv2.Init() addr := net.UDPAddr{ Port: 520, IP: net.ParseIP("224.0.0.9"), @@ -30,6 +29,12 @@ func main() { log.Println("Running") routes.Timer() + rip.Run(&config.Config{ + IP: string(net.ParseIP("224.0.0.9")), + MulticastInterface: "enp1s0f1", + Port: 520, + }) + /* NOT REACHED */ for { n, UDPAddr, err := conn.ReadFromUDP(buff) if err != nil { @@ -44,7 +49,7 @@ func main() { for _, v := range routes.Routes { fmt.Printf("Route:%s\tSubnet:%s\tNexthop:%s\tMetric:%d\tTime: %v\n", v.Addr.String(), v.Subnet.String(), v.Nexthop.String(), v.Metric, v.TTL.Unix()) } - rtable.Add(routes) + //rtable.Add(routes) fmt.Println("") } } diff --git a/ripv2/ripv2.go b/ripv2/ripv2.go index 9ff852d..42392da 100644 --- a/ripv2/ripv2.go +++ b/ripv2/ripv2.go @@ -1,11 +1,16 @@ package ripv2 import ( - "bytes" "encoding/binary" "errors" "fmt" + "log" "net" + "os" + "os/signal" + "syscall" + + "github.com/crip/config" ) func New() Message { @@ -54,35 +59,52 @@ func (m *Message) MParse(b *[]byte, n int) error { return nil } -/* Basic parsing */ -func (m *Message) Parse(b *[]byte) error { - if len(*b) == 0 { - return fmt.Errorf("can't parse empty data") +func (m *Message) Run(c *config.Config) { + iface := net.Interface{ + Name: c.MulticastInterface, + Flags: net.FlagMulticast, } - - bytes := bytes.NewBuffer(*b) - - if err := binary.Read(bytes, binary.BigEndian, m); err != nil { - return err + addr := net.UDPAddr{ + IP: net.IP(c.IP), + Port: c.Port, } - - if m.Version != 2 { - return fmt.Errorf("version: %d", m.Version) + conn, err := net.ListenMulticastUDP("udp4", &iface, &addr) + if err != nil { + log.Fatalln(err) } + defer conn.Close() - return nil -} - -func (m *Message) Request(conn *net.UDPConn) ([]byte, error) { - var buffer = make([]byte, 24) - m.Command = REQUEST - m.Version = 2 - m.RIP[0].Metric = 16 - buff := bytes.NewBuffer(buffer) - buff.Reset() - if err := binary.Write(buff, binary.BigEndian, m); err != nil { - return []byte{}, err - } - - return buffer, nil + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + + buffer := make([]byte, 484) + routes := Init() + + for { + go func() { + sig := <-sigs + fmt.Println(sig) + fmt.Println("exit") + os.Exit(0) + }() + + n, UDPAddr, err := conn.ReadFromUDP(buffer) + if err != nil { + conn.Close() + log.Fatal("unable to read from socket") + } + + if n < 24 { + log.Println("packet length") + } + + if err := m.MParse(&buffer, n); err != nil { + log.Fatalln(err) + } + routes.ParseRoutes(*UDPAddr, &m.RIP) + for _, v := range routes.Routes { + fmt.Printf("Route:%s\tSubnet:%s\tNexthop:%s\tMetric:%d\tTime: %v\n", v.Addr.String(), v.Subnet.String(), v.Nexthop.String(), v.Metric, v.TTL.Unix()) + } + //rtable.Add(routes) + } } diff --git a/ripv2/routes/routes.go b/ripv2/routes.go similarity index 91% rename from ripv2/routes/routes.go rename to ripv2/routes.go index 483198f..8807b37 100644 --- a/ripv2/routes/routes.go +++ b/ripv2/routes.go @@ -1,4 +1,4 @@ -package routes +package ripv2 import ( "crypto/sha256" @@ -6,8 +6,6 @@ import ( "net" "sync" "time" - - "github.com/crip/ripv2" ) type Routes struct { @@ -24,7 +22,7 @@ type Route struct { TTL time.Time } -func New() *Routes { +func Init() *Routes { var routes Routes routes.Routes = make(map[string]Route, 25) return &routes @@ -46,7 +44,7 @@ func (r *Routes) append(route Route) { r.Routes[string(bs)] = k } -func (r *Routes) ParseRoutes(addr net.UDPAddr, rip *[]ripv2.RIP) { +func (r *Routes) ParseRoutes(addr net.UDPAddr, rip *[]RIP) { var route Route for _, rip := range *rip { /* if not af_inet */ diff --git a/ripv2/routes/timers.go b/ripv2/timers.go similarity index 95% rename from ripv2/routes/timers.go rename to ripv2/timers.go index d5c294d..6e7c6ef 100644 --- a/ripv2/routes/timers.go +++ b/ripv2/timers.go @@ -1,4 +1,4 @@ -package routes +package ripv2 import ( "time" diff --git a/rtable/table.go b/rtable/table.go index cb772d9..6160d1c 100644 --- a/rtable/table.go +++ b/rtable/table.go @@ -5,10 +5,10 @@ import ( "fmt" "os/exec" - "github.com/crip/ripv2/routes" + "github.com/crip/ripv2" ) -func Add(r *routes.Routes) { +func Add(r *ripv2.Routes) { r.Lock() for _, r := range r.Routes { if !r.FIB { @@ -19,7 +19,7 @@ func Add(r *routes.Routes) { r.Unlock() } -func command(command COMMAND, r *routes.Route) error { +func command(command COMMAND, r *ripv2.Route) error { var cmd *exec.Cmd switch command { case ADD, REMOVE: @@ -30,7 +30,7 @@ func command(command COMMAND, r *routes.Route) error { return cmd.Run() } -func Remove(r *routes.Routes) { +func Remove(r *ripv2.Routes) { for _, r := range r.Routes { command(REMOVE, &r) }