2024-03-31 09:31:50 +11:00
|
|
|
/*
|
|
|
|
Copyright 2024 Josh Deprez
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2024-03-10 11:57:03 +11:00
|
|
|
package main
|
|
|
|
|
2024-03-15 15:17:21 +11:00
|
|
|
import (
|
2024-04-21 15:14:16 +10:00
|
|
|
"bufio"
|
2024-04-26 13:22:48 +10:00
|
|
|
"cmp"
|
2024-03-30 14:13:34 +11:00
|
|
|
"context"
|
2024-03-30 17:13:13 +11:00
|
|
|
"errors"
|
2024-03-22 16:14:55 +11:00
|
|
|
"flag"
|
2024-04-17 12:14:33 +10:00
|
|
|
"fmt"
|
2024-03-15 15:17:21 +11:00
|
|
|
"log"
|
2024-04-05 10:43:29 +11:00
|
|
|
"math/rand/v2"
|
2024-03-15 15:17:21 +11:00
|
|
|
"net"
|
2024-04-21 15:14:16 +10:00
|
|
|
"net/http"
|
2024-03-30 14:24:16 +11:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2024-03-24 18:01:24 +11:00
|
|
|
"regexp"
|
2024-04-24 09:33:15 +10:00
|
|
|
"runtime/debug"
|
2024-04-26 13:22:48 +10:00
|
|
|
"slices"
|
2024-04-21 15:14:16 +10:00
|
|
|
"strings"
|
2024-03-30 14:49:18 +11:00
|
|
|
"sync"
|
2024-03-30 17:13:13 +11:00
|
|
|
"time"
|
2024-03-15 15:17:21 +11:00
|
|
|
|
|
|
|
"gitea.drjosh.dev/josh/jrouter/aurp"
|
2024-04-19 14:57:25 +10:00
|
|
|
"gitea.drjosh.dev/josh/jrouter/router"
|
2024-04-23 13:15:22 +10:00
|
|
|
"gitea.drjosh.dev/josh/jrouter/status"
|
2024-04-19 14:57:25 +10:00
|
|
|
|
2024-04-07 13:08:15 +10:00
|
|
|
"github.com/google/gopacket/pcap"
|
2024-04-05 14:31:36 +11:00
|
|
|
"github.com/sfiera/multitalk/pkg/ddp"
|
2024-04-06 16:02:30 +11:00
|
|
|
"github.com/sfiera/multitalk/pkg/ethernet"
|
2024-03-15 15:17:21 +11:00
|
|
|
)
|
|
|
|
|
2024-04-26 13:13:35 +10:00
|
|
|
const routingTableTemplate = `
|
|
|
|
<table>
|
|
|
|
<thead><tr>
|
|
|
|
<th>Network range</th>
|
|
|
|
<th>Extended?</th>
|
2024-05-05 17:01:23 +10:00
|
|
|
<th>Zone names</th>
|
2024-04-26 13:13:35 +10:00
|
|
|
<th>Distance</th>
|
2024-04-26 13:27:04 +10:00
|
|
|
<th>Last seen</th>
|
2024-04-26 13:22:48 +10:00
|
|
|
<th>Port</th>
|
2024-04-26 13:13:35 +10:00
|
|
|
</tr></thead>
|
|
|
|
<tbody>
|
|
|
|
{{range $route := . }}
|
|
|
|
<tr>
|
|
|
|
<td>{{$route.NetStart}}{{if not (eq $route.NetStart $route.NetEnd)}} - {{$route.NetEnd}}{{end}}</td>
|
2024-05-05 17:01:23 +10:00
|
|
|
<td>{{if $route.Extended}}✅{{else}}-{{end}}</td>
|
2024-05-05 18:04:54 +10:00
|
|
|
<td>{{range $route.ZoneNames.ToSlice}}{{.}}<br>{{end}}</td>
|
2024-04-26 13:13:35 +10:00
|
|
|
<td>{{$route.Distance}}</td>
|
|
|
|
<td>{{$route.LastSeenAgo}}</td>
|
2024-05-04 14:48:47 +10:00
|
|
|
<td>
|
|
|
|
{{- with $route.AURPPeer -}}
|
2024-05-08 15:33:42 +10:00
|
|
|
[redacted]
|
2024-05-04 14:48:47 +10:00
|
|
|
{{- end -}}
|
|
|
|
{{- with $route.EtherTalkPeer -}}
|
|
|
|
{{.Port.Device}} {{.PeerAddr.Network}}.{{.PeerAddr.Node}}
|
|
|
|
{{- end -}}
|
|
|
|
{{- with $route.EtherTalkDirect -}}
|
2024-05-08 15:33:42 +10:00
|
|
|
{{.Device}}
|
2024-05-04 14:48:47 +10:00
|
|
|
{{- end -}}
|
|
|
|
</td>
|
2024-04-26 13:13:35 +10:00
|
|
|
</tr>
|
|
|
|
{{end}}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
`
|
|
|
|
|
2024-04-26 16:15:02 +10:00
|
|
|
const peerTableTemplate = `
|
|
|
|
<table>
|
|
|
|
<thead><tr>
|
|
|
|
<th>Configured addr</th>
|
|
|
|
<th>Remote addr</th>
|
|
|
|
<th>Receiver state</th>
|
|
|
|
<th>Sender state</th>
|
2024-05-12 18:07:27 +10:00
|
|
|
<th>Last heard from</th>
|
|
|
|
<th>Last reconnect</th>
|
|
|
|
<th>Last update</th>
|
|
|
|
<th>Last send</th>
|
|
|
|
<th>Send retries</th>
|
2024-04-26 16:15:02 +10:00
|
|
|
</tr></thead>
|
|
|
|
<tbody>
|
|
|
|
{{range $peer := . }}
|
|
|
|
<tr>
|
2024-05-08 15:33:42 +10:00
|
|
|
<td>[redacted]</td>
|
|
|
|
<td>[redacted]</td>
|
2024-04-26 16:15:02 +10:00
|
|
|
<td>{{$peer.ReceiverState}}</td>
|
|
|
|
<td>{{$peer.SenderState}}</td>
|
2024-05-12 18:07:27 +10:00
|
|
|
<td>{{$peer.LastHeardFromAgo}}</td>
|
|
|
|
<td>{{$peer.LastReconnectAgo}}</td>
|
|
|
|
<td>{{$peer.LastUpdateAgo}}</td>
|
|
|
|
<td>{{$peer.LastSendAgo}}</td>
|
|
|
|
<td>{{$peer.SendRetries}}</td>
|
2024-04-26 16:15:02 +10:00
|
|
|
</tr>
|
|
|
|
{{end}}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
`
|
|
|
|
|
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-04-24 09:33:15 +10:00
|
|
|
// For some reason it occasionally panics and the panics have no traceback?
|
|
|
|
debug.SetTraceback("all")
|
|
|
|
|
2024-03-22 16:14:55 +11:00
|
|
|
flag.Parse()
|
2024-03-15 15:17:21 +11:00
|
|
|
log.Println("jrouter")
|
|
|
|
|
2024-04-19 14:57:25 +10:00
|
|
|
cfg, err := router.LoadConfig(*configFilePath)
|
2024-03-24 18:01:24 +11:00
|
|
|
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
|
|
|
|
2024-05-08 15:33:42 +10:00
|
|
|
// log.Printf("Using %v as local domain identifier", localIP)
|
2024-03-22 16:14:55 +11:00
|
|
|
|
2024-05-08 15:33:42 +10:00
|
|
|
// log.Printf("EtherTalk configuration: %+v", cfg.EtherTalk)
|
2024-03-30 20:47:18 +11:00
|
|
|
|
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 {
|
2024-05-03 16:13:59 +10:00
|
|
|
log.Fatalf("AURP: Couldn't listen on udp4:387: %v", err)
|
2024-03-15 15:17:21 +11:00
|
|
|
}
|
2024-04-23 14:47:22 +10:00
|
|
|
defer ln.Close()
|
2024-05-03 16:13:59 +10:00
|
|
|
log.Printf("AURP: Listening on %v", ln.LocalAddr())
|
2024-03-24 18:01:24 +11:00
|
|
|
|
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
|
|
|
|
2024-04-23 13:15:22 +10:00
|
|
|
// --------------------------------- HTTP ---------------------------------
|
|
|
|
http.HandleFunc("/status", status.Handle)
|
|
|
|
go func() {
|
|
|
|
log.Print(http.ListenAndServe(":9459", nil))
|
|
|
|
}()
|
|
|
|
|
|
|
|
// --------------------------------- Pcap ---------------------------------
|
2024-04-17 12:14:33 +10:00
|
|
|
// First check the interface
|
2024-04-07 12:56:51 +10:00
|
|
|
iface, err := net.InterfaceByName(cfg.EtherTalk.Device)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't find interface named %q: %v", cfg.EtherTalk.Device, err)
|
|
|
|
}
|
|
|
|
myHWAddr := ethernet.Addr(iface.HardwareAddr)
|
2024-04-17 12:14:33 +10:00
|
|
|
if cfg.EtherTalk.EthAddr != "" {
|
|
|
|
// Override myHWAddr with the configured address
|
|
|
|
netHWAddr, err := net.ParseMAC(cfg.EtherTalk.EthAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't parse ethertalk.ethernet_addr value %q: %v", cfg.EtherTalk.EthAddr, err)
|
|
|
|
}
|
|
|
|
myHWAddr = ethernet.Addr(netHWAddr)
|
|
|
|
}
|
2024-04-07 12:56:51 +10:00
|
|
|
|
2024-04-17 12:14:33 +10:00
|
|
|
pcapHandle, err := pcap.OpenLive(cfg.EtherTalk.Device, 4096, true, 100*time.Millisecond)
|
2024-04-07 12:56:51 +10:00
|
|
|
if err != nil {
|
2024-04-17 12:14:33 +10:00
|
|
|
log.Fatalf("Couldn't open %q for packet capture: %v", cfg.EtherTalk.Device, err)
|
|
|
|
}
|
|
|
|
bpfFilter := fmt.Sprintf("(atalk or aarp) and (ether multicast or ether dst %s)", myHWAddr)
|
|
|
|
if err := pcapHandle.SetBPFFilter(bpfFilter); err != nil {
|
|
|
|
pcapHandle.Close()
|
|
|
|
log.Fatalf("Couldn't set BPF filter on packet capture: %v", err)
|
2024-04-07 12:56:51 +10:00
|
|
|
}
|
|
|
|
defer pcapHandle.Close()
|
|
|
|
|
2024-04-23 13:15:22 +10:00
|
|
|
// -------------------------------- Tables --------------------------------
|
2024-05-03 16:13:59 +10:00
|
|
|
routes := router.NewRouteTable()
|
2024-04-26 13:39:30 +10:00
|
|
|
status.AddItem(ctx, "Routing table", routingTableTemplate, func(context.Context) (any, error) {
|
2024-04-26 13:22:48 +10:00
|
|
|
rs := routes.Dump()
|
|
|
|
slices.SortFunc(rs, func(ra, rb router.Route) int {
|
|
|
|
return cmp.Compare(ra.NetStart, rb.NetStart)
|
|
|
|
})
|
|
|
|
return rs, nil
|
2024-04-26 13:13:35 +10:00
|
|
|
})
|
|
|
|
|
2024-04-23 13:15:22 +10:00
|
|
|
// -------------------------------- Peers ---------------------------------
|
2024-04-26 16:15:02 +10:00
|
|
|
var peersMu sync.Mutex
|
2024-04-29 09:32:57 +10:00
|
|
|
peers := make(map[udpAddr]*router.AURPPeer)
|
2024-04-26 16:15:02 +10:00
|
|
|
status.AddItem(ctx, "AURP Peers", peerTableTemplate, func(context.Context) (any, error) {
|
2024-04-29 09:32:57 +10:00
|
|
|
var peerInfo []*router.AURPPeer
|
2024-04-26 16:18:59 +10:00
|
|
|
func() {
|
|
|
|
peersMu.Lock()
|
|
|
|
defer peersMu.Unlock()
|
2024-04-29 09:32:57 +10:00
|
|
|
peerInfo = make([]*router.AURPPeer, 0, len(peers))
|
2024-04-26 16:18:59 +10:00
|
|
|
for _, p := range peers {
|
|
|
|
peerInfo = append(peerInfo, p)
|
|
|
|
}
|
|
|
|
}()
|
2024-04-29 09:32:57 +10:00
|
|
|
slices.SortFunc(peerInfo, func(pa, pb *router.AURPPeer) int {
|
2024-04-26 16:34:19 +10:00
|
|
|
return cmp.Or(
|
|
|
|
-cmp.Compare(
|
|
|
|
bool2Int(pa.ReceiverState() == router.ReceiverConnected),
|
|
|
|
bool2Int(pb.ReceiverState() == router.ReceiverConnected),
|
|
|
|
),
|
|
|
|
-cmp.Compare(
|
|
|
|
bool2Int(pa.SenderState() == router.SenderConnected),
|
|
|
|
bool2Int(pb.SenderState() == router.SenderConnected),
|
|
|
|
),
|
|
|
|
cmp.Compare(pa.ConfiguredAddr, pb.ConfiguredAddr),
|
|
|
|
)
|
2024-04-26 16:18:59 +10:00
|
|
|
})
|
2024-04-26 16:15:02 +10:00
|
|
|
return peerInfo, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
var nextConnID uint16
|
|
|
|
for nextConnID == 0 {
|
|
|
|
nextConnID = uint16(rand.IntN(0x10000))
|
|
|
|
}
|
|
|
|
|
2024-04-23 14:49:38 +10:00
|
|
|
var wg sync.WaitGroup
|
2024-04-29 09:32:57 +10:00
|
|
|
goPeerHandler := func(p *router.AURPPeer) {
|
2024-04-23 14:49:38 +10:00
|
|
|
wg.Add(1)
|
2024-03-30 14:49:18 +11:00
|
|
|
go func() {
|
2024-04-23 14:49:38 +10:00
|
|
|
defer wg.Done()
|
2024-04-19 14:57:25 +10:00
|
|
|
p.Handle(ctx)
|
2024-03-30 14:49:18 +11:00
|
|
|
}()
|
|
|
|
}
|
2024-03-30 14:24:16 +11:00
|
|
|
|
2024-04-12 16:14:27 +10:00
|
|
|
// ------------------------- Configured peer setup ------------------------
|
2024-04-21 15:14:16 +10:00
|
|
|
if cfg.PeerListURL != "" {
|
|
|
|
log.Printf("Fetching peer list from %s...", cfg.PeerListURL)
|
|
|
|
existing := len(cfg.Peers)
|
|
|
|
func() {
|
|
|
|
resp, err := http.Get(cfg.PeerListURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't fetch peer list: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
sc := bufio.NewScanner(resp.Body)
|
|
|
|
for sc.Scan() {
|
|
|
|
p := strings.TrimSpace(sc.Text())
|
|
|
|
if p == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cfg.Peers = append(cfg.Peers, p)
|
|
|
|
}
|
|
|
|
if err := sc.Err(); err != nil {
|
|
|
|
log.Fatalf("Couldn't scan peer list response: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
log.Printf("Fetched list containing %d peers", len(cfg.Peers)-existing)
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-05-08 15:33:42 +10:00
|
|
|
log.Print("couldn't resolve UDP address, skipping peer")
|
2024-04-19 22:07:48 +10:00
|
|
|
continue
|
2024-03-24 18:01:24 +11:00
|
|
|
}
|
2024-05-08 15:33:42 +10:00
|
|
|
//log.Printf("resolved %q to %v", peerStr, raddr)
|
2024-03-24 18:01:24 +11:00
|
|
|
|
2024-04-21 15:14:16 +10:00
|
|
|
if raddr.IP.Equal(localIP) {
|
2024-05-08 15:33:42 +10:00
|
|
|
log.Print("peer == me, skipping")
|
2024-04-21 15:14:16 +10:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-29 09:32:57 +10:00
|
|
|
peer := &router.AURPPeer{
|
2024-04-19 14:57:25 +10:00
|
|
|
Transport: &aurp.Transport{
|
2024-03-30 20:27:24 +11:00
|
|
|
LocalDI: localDI,
|
|
|
|
RemoteDI: aurp.IPDomainIdentifier(raddr.IP),
|
|
|
|
LocalConnID: nextConnID,
|
|
|
|
},
|
2024-04-26 16:15:02 +10:00
|
|
|
UDPConn: ln,
|
|
|
|
ConfiguredAddr: peerStr,
|
|
|
|
RemoteAddr: raddr,
|
|
|
|
ReceiveCh: make(chan aurp.Packet, 1024),
|
2024-05-05 17:01:23 +10:00
|
|
|
RouteTable: routes,
|
2024-03-24 18:01:24 +11:00
|
|
|
}
|
2024-04-05 10:43:29 +11:00
|
|
|
aurp.Inc(&nextConnID)
|
2024-04-26 16:15:02 +10:00
|
|
|
peersMu.Lock()
|
2024-03-24 18:01:24 +11:00
|
|
|
peers[udpAddrFromNet(raddr)] = peer
|
2024-04-26 16:15:02 +10:00
|
|
|
peersMu.Unlock()
|
2024-04-07 12:56:51 +10:00
|
|
|
goPeerHandler(peer)
|
2024-03-24 18:01:24 +11:00
|
|
|
}
|
2024-03-15 15:17:21 +11:00
|
|
|
|
2024-04-12 16:14:27 +10:00
|
|
|
// --------------------------------- AARP ---------------------------------
|
2024-04-19 14:57:25 +10:00
|
|
|
aarpMachine := router.NewAARPMachine(cfg, pcapHandle, myHWAddr)
|
2024-05-03 16:13:59 +10:00
|
|
|
go aarpMachine.Run(ctx)
|
2024-04-06 16:02:30 +11:00
|
|
|
|
2024-04-19 14:57:25 +10:00
|
|
|
// -------------------------------- Router --------------------------------
|
|
|
|
rooter := &router.Router{
|
|
|
|
Config: cfg,
|
2024-05-03 16:13:59 +10:00
|
|
|
RouteTable: routes,
|
2024-05-05 17:01:23 +10:00
|
|
|
// ZoneTable: zones,
|
2024-05-03 16:13:59 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
etherTalkPort := &router.EtherTalkPort{
|
2024-05-04 14:48:47 +10:00
|
|
|
Device: cfg.EtherTalk.Device,
|
2024-05-03 16:13:59 +10:00
|
|
|
EthernetAddr: myHWAddr,
|
|
|
|
NetStart: cfg.EtherTalk.NetStart,
|
|
|
|
NetEnd: cfg.EtherTalk.NetEnd,
|
|
|
|
DefaultZoneName: cfg.EtherTalk.ZoneName,
|
2024-05-05 17:59:49 +10:00
|
|
|
AvailableZones: router.SetFromSlice([]string{cfg.EtherTalk.ZoneName}),
|
2024-05-03 16:13:59 +10:00
|
|
|
PcapHandle: pcapHandle,
|
|
|
|
AARPMachine: aarpMachine,
|
|
|
|
Router: rooter,
|
|
|
|
}
|
2024-05-03 18:32:12 +10:00
|
|
|
rooter.Ports = append(rooter.Ports, etherTalkPort)
|
2024-05-03 16:13:59 +10:00
|
|
|
routes.InsertEtherTalkDirect(etherTalkPort)
|
2024-04-19 14:57:25 +10:00
|
|
|
|
2024-05-04 14:48:47 +10:00
|
|
|
// --------------------------------- RTMP ---------------------------------
|
|
|
|
go etherTalkPort.RunRTMP(ctx)
|
|
|
|
|
2024-04-12 16:14:27 +10:00
|
|
|
// ---------------------- Raw AppleTalk/AARP inbound ----------------------
|
2024-04-23 14:49:38 +10:00
|
|
|
wg.Add(1)
|
2024-04-06 17:46:00 +11:00
|
|
|
go func() {
|
2024-04-23 14:49:38 +10:00
|
|
|
defer wg.Done()
|
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
ctx, setStatus, _ := status.AddSimpleItem(ctx, "EtherTalk inbound")
|
|
|
|
defer setStatus("EtherTalk Serve goroutine exited!")
|
2024-04-23 14:44:22 +10:00
|
|
|
|
2024-04-26 13:59:21 +10:00
|
|
|
setStatus(fmt.Sprintf("Listening on %s", cfg.EtherTalk.Device))
|
2024-04-23 14:44:22 +10:00
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
etherTalkPort.Serve(ctx)
|
2024-04-05 13:18:22 +11:00
|
|
|
}()
|
|
|
|
|
2024-04-12 16:14:27 +10:00
|
|
|
// ----------------------------- AURP inbound -----------------------------
|
2024-04-23 14:49:38 +10:00
|
|
|
wg.Add(1)
|
2024-04-23 14:47:22 +10:00
|
|
|
go func() {
|
2024-04-23 14:49:38 +10:00
|
|
|
defer wg.Done()
|
|
|
|
|
2024-04-23 14:47:22 +10:00
|
|
|
ctx, setStatus, done := status.AddSimpleItem(ctx, "AURP inbound")
|
|
|
|
defer done()
|
2024-04-26 13:59:21 +10:00
|
|
|
setStatus(fmt.Sprintf("Listening on UDP port %d", cfg.ListenPort))
|
2024-04-21 17:47:58 +10:00
|
|
|
|
2024-04-23 14:47:22 +10:00
|
|
|
for {
|
|
|
|
if ctx.Err() != nil {
|
2024-04-21 17:47:58 +10:00
|
|
|
return
|
|
|
|
}
|
2024-04-23 14:47:22 +10:00
|
|
|
ln.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
pktbuf := make([]byte, 4096)
|
|
|
|
pktlen, raddr, readErr := ln.ReadFromUDP(pktbuf)
|
2024-04-21 17:47:58 +10:00
|
|
|
|
2024-04-23 14:47:22 +10:00
|
|
|
var operr *net.OpError
|
|
|
|
if errors.As(readErr, &operr) && operr.Timeout() {
|
2024-04-21 17:47:58 +10:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-23 14:47:22 +10:00
|
|
|
// log.Printf("AURP: Received packet of length %d from %v", pktlen, raddr)
|
|
|
|
|
|
|
|
dh, pkt, parseErr := aurp.ParsePacket(pktbuf[:pktlen])
|
|
|
|
if parseErr != nil {
|
|
|
|
log.Printf("AURP: Failed to parse packet: %v", parseErr)
|
2024-04-06 10:34:54 +11:00
|
|
|
continue
|
|
|
|
}
|
2024-04-23 14:47:22 +10:00
|
|
|
if readErr != nil {
|
|
|
|
log.Printf("AURP: Failed to read packet: %v", readErr)
|
|
|
|
return
|
2024-04-12 14:26:12 +10:00
|
|
|
}
|
|
|
|
|
2024-05-08 15:33:42 +10:00
|
|
|
// log.Printf("AURP: Got %T from %v (%v)", pkt, raddr, dh.SourceDI)
|
2024-04-23 14:47:22 +10:00
|
|
|
|
|
|
|
// Existing peer?
|
|
|
|
ra := udpAddrFromNet(raddr)
|
2024-04-26 16:15:02 +10:00
|
|
|
peersMu.Lock()
|
2024-04-23 14:47:22 +10:00
|
|
|
pr := peers[ra]
|
|
|
|
if pr == nil {
|
2024-04-26 16:15:02 +10:00
|
|
|
if !cfg.OpenPeering {
|
2024-05-08 15:33:42 +10:00
|
|
|
log.Print("AURP: Got packet but it's not in my config and open peering is disabled; dropping the packet")
|
2024-04-26 16:15:02 +10:00
|
|
|
peersMu.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
2024-04-23 14:47:22 +10:00
|
|
|
// New peer!
|
2024-04-29 09:32:57 +10:00
|
|
|
pr = &router.AURPPeer{
|
2024-04-23 14:47:22 +10:00
|
|
|
Transport: &aurp.Transport{
|
|
|
|
LocalDI: localDI,
|
|
|
|
RemoteDI: dh.SourceDI, // platinum rule
|
|
|
|
LocalConnID: nextConnID,
|
|
|
|
},
|
2024-05-05 17:01:23 +10:00
|
|
|
UDPConn: ln,
|
|
|
|
RemoteAddr: raddr,
|
|
|
|
ReceiveCh: make(chan aurp.Packet, 1024),
|
|
|
|
RouteTable: routes,
|
2024-04-23 14:47:22 +10:00
|
|
|
}
|
|
|
|
aurp.Inc(&nextConnID)
|
|
|
|
peers[ra] = pr
|
|
|
|
goPeerHandler(pr)
|
2024-04-12 14:26:12 +10:00
|
|
|
}
|
2024-04-26 16:15:02 +10:00
|
|
|
peersMu.Unlock()
|
2024-04-23 14:47:22 +10:00
|
|
|
|
|
|
|
switch dh.PacketType {
|
|
|
|
case aurp.PacketTypeRouting:
|
|
|
|
// It's AURP routing data.
|
|
|
|
// Pass the packet to the goroutine in charge of this peer.
|
|
|
|
select {
|
2024-04-26 16:15:02 +10:00
|
|
|
case pr.ReceiveCh <- pkt:
|
2024-04-23 14:47:22 +10:00
|
|
|
// That's it for us.
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
|
|
|
|
case aurp.PacketTypeAppleTalk:
|
|
|
|
apkt, ok := pkt.(*aurp.AppleTalkPacket)
|
|
|
|
if !ok {
|
|
|
|
log.Printf("AURP: Got %T but domain header packet type was %v ?", pkt, dh.PacketType)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Route or otherwise handle the encapsulated AppleTalk traffic
|
|
|
|
ddpkt := new(ddp.ExtPacket)
|
|
|
|
if err := ddp.ExtUnmarshal(apkt.Data, ddpkt); err != nil {
|
|
|
|
log.Printf("AURP: Couldn't unmarshal encapsulated DDP packet: %v", err)
|
2024-04-12 14:26:12 +10:00
|
|
|
continue
|
|
|
|
}
|
2024-05-03 18:32:12 +10:00
|
|
|
// log.Printf("DDP/AURP: Got %d.%d.%d -> %d.%d.%d proto %d data len %d",
|
|
|
|
// ddpkt.SrcNet, ddpkt.SrcNode, ddpkt.SrcSocket,
|
|
|
|
// ddpkt.DstNet, ddpkt.DstNode, ddpkt.DstSocket,
|
|
|
|
// ddpkt.Proto, len(ddpkt.Data))
|
2024-04-23 14:47:22 +10:00
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
// Is it addressed to me?
|
2024-05-03 18:32:12 +10:00
|
|
|
var localPort *router.EtherTalkPort
|
|
|
|
for _, port := range rooter.Ports {
|
|
|
|
if ddpkt.DstNet >= port.NetStart && ddpkt.DstNet <= port.NetEnd {
|
|
|
|
localPort = port
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ddpkt.DstNode == 0 && localPort != nil { // Node 0 = any router for the network = me
|
2024-05-03 16:13:59 +10:00
|
|
|
// Is it NBP? FwdReq needs translating.
|
2024-04-23 14:47:22 +10:00
|
|
|
if ddpkt.DstSocket != 2 {
|
|
|
|
// Something else?? TODO
|
|
|
|
log.Printf("DDP/AURP: I don't have anything 'listening' on socket %d", ddpkt.DstSocket)
|
|
|
|
continue
|
|
|
|
}
|
2024-05-03 18:32:12 +10:00
|
|
|
// It's NBP, specifically it should be a FwdReq
|
|
|
|
if err := rooter.HandleNBPFromAURP(ctx, ddpkt); err != nil {
|
2024-04-23 14:47:22 +10:00
|
|
|
log.Printf("NBP/DDP/AURP: %v", err)
|
|
|
|
}
|
2024-05-03 18:32:12 +10:00
|
|
|
continue
|
2024-04-23 14:47:22 +10:00
|
|
|
}
|
|
|
|
|
2024-05-03 18:32:12 +10:00
|
|
|
// Route the packet!
|
2024-05-03 16:13:59 +10:00
|
|
|
if err := rooter.Forward(ctx, ddpkt); err != nil {
|
|
|
|
log.Printf("DDP/AURP: Couldn't route packet: %v", err)
|
2024-04-12 14:26:12 +10:00
|
|
|
}
|
|
|
|
|
2024-04-23 14:47:22 +10:00
|
|
|
default:
|
|
|
|
log.Printf("AURP: Got unknown packet type %v", dh.PacketType)
|
2024-04-12 14:26:12 +10:00
|
|
|
}
|
2024-03-30 14:30:58 +11:00
|
|
|
}
|
2024-04-23 14:47:22 +10:00
|
|
|
}()
|
|
|
|
|
|
|
|
// -------------------------------- Close ---------------------------------
|
2024-04-23 14:49:38 +10:00
|
|
|
wg.Wait()
|
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),
|
|
|
|
}
|
|
|
|
}
|
2024-04-26 16:34:19 +10:00
|
|
|
|
|
|
|
func bool2Int(b bool) int {
|
|
|
|
if b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|