2024-03-10 11:57:03 +11:00
|
|
|
package main
|
|
|
|
|
2024-03-15 15:17:21 +11:00
|
|
|
import (
|
2024-03-22 16:14:55 +11:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"flag"
|
2024-03-15 15:17:21 +11:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"gitea.drjosh.dev/josh/jrouter/aurp"
|
|
|
|
)
|
|
|
|
|
2024-03-22 16:14:55 +11:00
|
|
|
var localIPAddr = flag.String("local-ip", "", "IPv4 address to use as the Source Domain Identifier")
|
|
|
|
|
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-22 16:14:55 +11:00
|
|
|
localIP := net.ParseIP(*localIPAddr).To4()
|
|
|
|
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 {
|
|
|
|
log.Fatalf("No global unicast IPv4 addresses on any network interfaces, and no valid address passed with --local-ip")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Using %v as local domain identifier", localIP)
|
|
|
|
|
|
|
|
peers := make(map[uint32]*aurp.Transport)
|
|
|
|
var nextConnID uint16
|
|
|
|
|
2024-03-15 15:17:21 +11:00
|
|
|
ln, err := net.ListenUDP("udp4", &net.UDPAddr{Port: 387})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't listen on udp4:387: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Incoming packet loop
|
|
|
|
pb := make([]byte, 65536)
|
|
|
|
for {
|
2024-03-22 16:14:55 +11:00
|
|
|
pktlen, raddr, readErr := ln.ReadFromUDP(pb)
|
|
|
|
// 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-22 16:14:55 +11:00
|
|
|
dh, _, err := aurp.ParseDomainHeader(pb[:pktlen])
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to parse domain header: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt, parseErr := aurp.ParsePacket(pb[: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)
|
|
|
|
continue
|
2024-03-15 15:17:21 +11:00
|
|
|
}
|
2024-03-22 16:14:55 +11:00
|
|
|
|
|
|
|
// Existing peer?
|
|
|
|
rip := binary.BigEndian.Uint32(raddr.IP)
|
|
|
|
tr := peers[rip]
|
|
|
|
if tr == nil {
|
|
|
|
// New peer!
|
|
|
|
tr = &aurp.Transport{
|
|
|
|
LocalDI: aurp.IPDomainIdentifier(localIP),
|
|
|
|
RemoteDI: dh.SourceDI,
|
|
|
|
LocalConnID: nextConnID,
|
|
|
|
}
|
|
|
|
nextConnID++
|
|
|
|
peers[rip] = tr
|
|
|
|
}
|
|
|
|
|
|
|
|
switch p := pkt.(type) {
|
|
|
|
case *aurp.AppleTalkPacket:
|
|
|
|
// Probably something like:
|
|
|
|
//
|
|
|
|
// * parse the DDP header
|
|
|
|
// * check that this is headed for our local network
|
|
|
|
// * write the packet out in an EtherTalk frame
|
|
|
|
//
|
|
|
|
// or maybe if we were implementing a "central hub"
|
|
|
|
//
|
|
|
|
// * parse the DDP header
|
|
|
|
// * see if we know the network
|
|
|
|
// * forward to the peer with that network and lowest metric
|
|
|
|
|
|
|
|
case *aurp.OpenReqPacket:
|
|
|
|
// The peer tells us their connection ID in Open-Req.
|
|
|
|
tr.RemoteConnID = p.ConnectionID
|
|
|
|
|
|
|
|
// Formulate a response.
|
|
|
|
var rp *aurp.OpenRspPacket
|
2024-03-22 16:18:18 +11:00
|
|
|
switch {
|
|
|
|
case p.Version != 1:
|
2024-03-22 16:14:55 +11:00
|
|
|
// Respond with Open-Rsp with unknown version error.
|
|
|
|
rp = tr.NewOpenRspPacket(0, aurp.ErrCodeInvalidVersion, nil)
|
2024-03-22 16:18:18 +11:00
|
|
|
|
|
|
|
case len(p.Options) > 0:
|
|
|
|
// Options? OPTIONS? We don't accept no stinkin' _options_
|
|
|
|
rp = tr.NewOpenRspPacket(0, aurp.ErrCodeOptionNegotiation, nil)
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Accept it I guess.
|
2024-03-22 16:14:55 +11:00
|
|
|
rp = tr.NewOpenRspPacket(0, 1, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write an Open-Rsp packet
|
|
|
|
var b bytes.Buffer
|
|
|
|
if _, err := rp.WriteTo(&b); err != nil {
|
|
|
|
log.Printf("Couldn't create response packet: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if _, err := ln.WriteToUDP(b.Bytes(), raddr); err != nil {
|
|
|
|
log.Printf("Couldn't write response packet to UDP peer %v: %v", raddr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case *aurp.OpenRspPacket:
|
|
|
|
if p.RateOrErrCode < 0 {
|
|
|
|
// It's an error code.
|
|
|
|
log.Printf("Open-Rsp error code from peer %v: %d", raddr.IP, p.RateOrErrCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-03-15 15:17:21 +11:00
|
|
|
}
|
2024-03-10 11:57:03 +11:00
|
|
|
}
|