refactoring

This commit is contained in:
Jim Colderwood 2024-04-25 14:39:46 +01:00
parent 36b6ad3b33
commit 210141b8d5
5 changed files with 34 additions and 23 deletions

View File

@ -1,11 +1,10 @@
package main
import (
"log"
"github.com/crip/config"
"github.com/crip/ripv2"
"github.com/crip/rtable"
"log"
)
func callback(r *ripv2.Routes) {
@ -17,8 +16,9 @@ func main() {
rip := ripv2.New()
routes := ripv2.Init()
log.Println("Running")
routes.Timer()
rip.Run(conf, callback)
if err := rip.Run(conf, callback); err != nil {
log.Fatal(err)
}
/* NOT REACHED */
}

View File

@ -46,7 +46,7 @@ func (m *Message) MParse(b *[]byte, n int) error {
m.RIP[i].RouteTag = binary.BigEndian.Uint16(ripentry[2:4])
m.RIP[i].Addr = binary.BigEndian.Uint32(ripentry[4:8])
m.RIP[i].Subnet = binary.BigEndian.Uint32(ripentry[8:12])
m.RIP[i].Nexthop = binary.BigEndian.Uint32(ripentry[12:16])
m.RIP[i].NextHop = binary.BigEndian.Uint32(ripentry[12:16])
m.RIP[i].Metric = binary.BigEndian.Uint32(ripentry[16:20])
indexA += 20
indexB += 20
@ -59,7 +59,7 @@ func (m *Message) MParse(b *[]byte, n int) error {
return nil
}
func (m *Message) Run(c *config.Config, callback func(*Routes)) {
func (m *Message) Run(c *config.Config, callback func(*Routes)) error {
iface := net.Interface{
Name: c.MulticastInterface,
Flags: net.FlagMulticast,
@ -70,9 +70,13 @@ func (m *Message) Run(c *config.Config, callback func(*Routes)) {
}
conn, err := net.ListenMulticastUDP("udp4", &iface, &addr)
if err != nil {
log.Fatalln(err)
return err
}
defer conn.Close()
defer func() {
if err := conn.Close(); err != nil {
log.Println(err)
}
}()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
@ -82,15 +86,17 @@ func (m *Message) Run(c *config.Config, callback func(*Routes)) {
for {
go func() {
sig := <-sigs
fmt.Println(sig)
fmt.Println("exit")
log.Println("Running")
<-sigs
log.Println("exit")
os.Exit(0)
}()
n, UDPAddr, err := conn.ReadFromUDP(buffer)
if err != nil {
conn.Close()
if err = conn.Close(); err != nil {
log.Println(err)
}
log.Fatal("unable to read from socket")
}
@ -103,8 +109,8 @@ func (m *Message) Run(c *config.Config, callback func(*Routes)) {
}
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())
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())
}
(callback(routes))
callback(routes)
}
}

View File

@ -17,7 +17,7 @@ type Route struct {
Addr net.IP
FIB FIB
Subnet net.IPMask
Nexthop net.IP
NextHop net.IP
Metric uint32
TTL time.Time
}
@ -29,7 +29,7 @@ func Init() *Routes {
}
func (r *Routes) append(route Route) {
key := route.Addr.String() + route.Subnet.String() + route.Nexthop.String() + fmt.Sprintf("%d", route.Metric)
key := route.Addr.String() + route.Subnet.String() + route.NextHop.String() + fmt.Sprintf("%d", route.Metric)
h := sha256.New()
h.Write([]byte(key))
bs := h.Sum(nil)
@ -60,10 +60,10 @@ func (r *Routes) ParseRoutes(addr net.UDPAddr, rip *[]RIP) {
route.Addr = net.IPv4(byte(rip.Addr>>24), byte(rip.Addr>>16), byte(rip.Addr>>8), byte(rip.Addr))
route.Subnet = net.IPv4Mask(byte(rip.Subnet>>24), byte(rip.Subnet>>16), byte(rip.Subnet>>8), byte(rip.Subnet))
if rip.Nexthop == 0 {
route.Nexthop = addr.IP
if rip.NextHop == 0 {
route.NextHop = addr.IP
} else {
route.Nexthop = net.IPv4(byte(rip.Nexthop>>24), byte(rip.Nexthop>>16), byte(rip.Nexthop>>8), byte(rip.Nexthop))
route.NextHop = net.IPv4(byte(rip.NextHop>>24), byte(rip.NextHop>>16), byte(rip.NextHop>>8), byte(rip.NextHop))
}
route.Metric = rip.Metric
route.TTL = time.Now()

View File

@ -33,6 +33,6 @@ type RIP struct {
RouteTag uint16
Addr uint32
Subnet uint32
Nexthop uint32
NextHop uint32
Metric uint32
}

View File

@ -10,14 +10,19 @@ import (
)
func Pass(r *ripv2.Routes) {
var err error
r.Lock()
for k, route := range r.Routes {
switch route.FIB {
case ripv2.NEW:
command(ADD, &route)
if err = command(ADD, &route); err != nil {
log.Printf("rtable: failed to add route %d: %s", route.FIB, err)
}
route.FIB = ripv2.TABLE
case ripv2.DELETE:
command(REMOVE, &route)
if err = command(REMOVE, &route); err != nil {
log.Printf("rtable: failed to remove route %d: %s", route.FIB, err)
}
delete(r.Routes, k)
default:
log.Println("unknown FIB type")
@ -30,7 +35,7 @@ func command(command COMMAND, r *ripv2.Route) error {
var cmd *exec.Cmd
switch command {
case ADD, REMOVE:
cmd = exec.Command("route", string(command), "-net", r.Addr.String(), r.Subnet.String(), r.Nexthop.String(), "-hopcount", fmt.Sprint(r.Metric))
cmd = exec.Command("route", string(command), "-net", r.Addr.String(), r.Subnet.String(), r.NextHop.String(), "-hopcount", fmt.Sprint(r.Metric))
default:
return errors.New("command not supported")
}