restructure

This commit is contained in:
Jim Colderwood 2024-04-16 12:55:08 +01:00
parent 12f30095de
commit 46f0c8aedf
7 changed files with 75 additions and 42 deletions

1
config/config.go Normal file
View File

@ -0,0 +1 @@
package config

7
config/type.go Normal file
View File

@ -0,0 +1,7 @@
package config
type Config struct {
IP string `json:"multicastAddr"`
MulticastInterface string `json:"multicastInterface"`
Port int `json:"MulticastPort"`
}

13
main.go
View File

@ -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("")
}
}

View File

@ -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)
}
}

View File

@ -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 */

View File

@ -1,4 +1,4 @@
package routes
package ripv2
import (
"time"

View File

@ -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)
}