jrouter/main.go

182 lines
3.8 KiB
Go
Raw Normal View History

2024-03-10 11:57:03 +11:00
package main
2024-03-15 15:17:21 +11:00
import (
2024-03-30 14:13:34 +11:00
"context"
2024-03-22 16:14:55 +11:00
"flag"
2024-03-15 15:17:21 +11:00
"log"
"net"
2024-03-30 14:24:16 +11:00
"os"
"os/signal"
2024-03-24 18:01:24 +11:00
"regexp"
2024-03-30 14:49:18 +11:00
"sync"
2024-03-15 15:17:21 +11:00
"gitea.drjosh.dev/josh/jrouter/aurp"
)
2024-03-24 18:01:24 +11:00
var hasPortRE = regexp.MustCompile(`:\d+$`)
var configFilePath = flag.String("config", "jrouter.yaml", "Path to configuration file to use")
2024-03-10 11:57:03 +11:00
func main() {
2024-03-22 16:14:55 +11:00
flag.Parse()
2024-03-15 15:17:21 +11:00
log.Println("jrouter")
2024-03-24 18:01:24 +11:00
cfg, err := loadConfig(*configFilePath)
if err != nil {
log.Fatalf("Couldn't load configuration file: %v", err)
}
localIP := net.ParseIP(cfg.LocalIP).To4()
2024-03-22 16:14:55 +11:00
if localIP == nil {
iaddrs, err := net.InterfaceAddrs()
if err != nil {
log.Fatalf("Couldn't read network interface addresses: %v", err)
}
for _, iaddr := range iaddrs {
inet, ok := iaddr.(*net.IPNet)
if !ok {
continue
}
if !inet.IP.IsGlobalUnicast() {
continue
}
localIP = inet.IP.To4()
if localIP != nil {
break
}
}
if localIP == nil {
2024-03-24 18:01:24 +11:00
log.Fatalf("No global unicast IPv4 addresses on any network interfaces, and no valid local_ip address in configuration")
2024-03-22 16:14:55 +11:00
}
}
2024-03-24 21:10:24 +11:00
localDI := aurp.IPDomainIdentifier(localIP)
2024-03-22 16:14:55 +11:00
log.Printf("Using %v as local domain identifier", localIP)
2024-03-24 18:01:24 +11:00
peers := make(map[udpAddr]*peer)
2024-03-22 16:14:55 +11:00
var nextConnID uint16
2024-03-24 18:01:24 +11:00
ln, err := net.ListenUDP("udp4", &net.UDPAddr{Port: int(cfg.ListenPort)})
2024-03-15 15:17:21 +11:00
if err != nil {
log.Fatalf("Couldn't listen on udp4:387: %v", err)
}
2024-03-24 18:01:24 +11:00
log.Printf("Listening on %v", ln.LocalAddr())
2024-03-30 14:24:16 +11:00
log.Println("Press ^C or send SIGINT to stop the router gracefully")
2024-03-30 14:37:41 +11:00
cctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx, _ := signal.NotifyContext(cctx, os.Interrupt)
2024-03-30 14:49:18 +11:00
// Wait until all peer handlers have finished before closing the port
var handlersWG sync.WaitGroup
defer func() {
handlersWG.Wait()
2024-03-30 14:30:58 +11:00
ln.Close()
}()
2024-03-30 14:49:18 +11:00
goHandler := func(p *peer) {
handlersWG.Add(1)
go func() {
defer handlersWG.Done()
p.handle(ctx)
}()
}
2024-03-30 14:24:16 +11:00
2024-03-24 18:01:24 +11:00
for _, peerStr := range cfg.Peers {
if !hasPortRE.MatchString(peerStr) {
peerStr += ":387"
}
raddr, err := net.ResolveUDPAddr("udp4", peerStr)
if err != nil {
log.Fatalf("Invalid UDP address: %v", err)
}
log.Printf("resolved %q to %v", peerStr, raddr)
tr := &aurp.Transport{
2024-03-24 21:10:24 +11:00
LocalDI: localDI,
2024-03-24 18:01:24 +11:00
RemoteDI: aurp.IPDomainIdentifier(raddr.IP),
LocalConnID: nextConnID,
}
nextConnID++
peer := &peer{
tr: tr,
conn: ln,
raddr: raddr,
2024-03-24 21:10:24 +11:00
recv: make(chan aurp.Packet, 1024),
2024-03-24 18:01:24 +11:00
}
2024-03-30 14:49:18 +11:00
goHandler(peer)
2024-03-24 18:01:24 +11:00
peers[udpAddrFromNet(raddr)] = peer
}
2024-03-15 15:17:21 +11:00
// Incoming packet loop
for {
2024-03-30 14:24:16 +11:00
pktbuf := make([]byte, 65536)
pktlen, raddr, readErr := ln.ReadFromUDP(pktbuf)
2024-03-22 16:14:55 +11:00
// net.PacketConn.ReadFrom: "Callers should always process
2024-03-15 16:15:24 +11:00
// the n > 0 bytes returned before considering the error err."
2024-03-24 18:01:24 +11:00
log.Printf("Received packet of length %d from %v", pktlen, raddr)
2024-03-30 14:24:16 +11:00
dh, pkt, parseErr := aurp.ParsePacket(pktbuf[:pktlen])
2024-03-15 16:15:24 +11:00
if parseErr != nil {
log.Printf("Failed to parse packet: %v", parseErr)
2024-03-15 15:17:21 +11:00
}
2024-03-15 16:15:24 +11:00
if readErr != nil {
log.Printf("Failed to read packet: %v", readErr)
2024-03-30 14:30:58 +11:00
return
2024-03-15 15:17:21 +11:00
}
2024-03-22 16:14:55 +11:00
2024-03-30 14:37:41 +11:00
log.Printf("The packet parsed succesfully as a %T", pkt)
2024-03-22 16:14:55 +11:00
// Existing peer?
2024-03-24 18:01:24 +11:00
ra := udpAddrFromNet(raddr)
pr := peers[ra]
if pr == nil {
2024-03-22 16:14:55 +11:00
// New peer!
nextConnID++
2024-03-24 18:01:24 +11:00
pr = &peer{
tr: &aurp.Transport{
2024-03-24 21:10:24 +11:00
LocalDI: localDI,
2024-03-24 18:01:24 +11:00
RemoteDI: dh.SourceDI, // platinum rule
LocalConnID: nextConnID,
},
conn: ln,
raddr: raddr,
2024-03-24 21:10:24 +11:00
recv: make(chan aurp.Packet, 1024),
2024-03-24 18:01:24 +11:00
}
peers[ra] = pr
2024-03-30 14:49:18 +11:00
goHandler(pr)
2024-03-22 16:14:55 +11:00
}
2024-03-24 21:10:24 +11:00
// Pass the packet to the goroutine in charge of this peer.
2024-03-30 14:30:58 +11:00
select {
case pr.recv <- pkt:
// That's it for us.
case <-ctx.Done():
return
}
2024-03-24 21:10:24 +11:00
}
}
2024-03-24 18:01:24 +11:00
// Hashable net.UDPAddr
type udpAddr struct {
ipv4 [4]byte
port uint16
}
func udpAddrFromNet(a *net.UDPAddr) udpAddr {
return udpAddr{
ipv4: [4]byte(a.IP.To4()),
port: uint16(a.Port),
}
}
func (u udpAddr) toNet() *net.UDPAddr {
return &net.UDPAddr{
IP: u.ipv4[:],
Port: int(u.port),
}
}