WIP
This commit is contained in:
parent
2afb879573
commit
0c86aa6194
2 changed files with 68 additions and 33 deletions
29
zip.go
29
zip.go
|
@ -100,10 +100,23 @@ func handleZIP(pcapHandle *pcap.Handle, srcHWAddr, myHWAddr ethernet.Addr, myAdd
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var resp interface {
|
||||
Marshal() ([]byte, error)
|
||||
}
|
||||
|
||||
switch zipkt := zipkt.(type) {
|
||||
case *zip.QueryPacket:
|
||||
// TODO: multiple packets
|
||||
resp = &zip.ReplyPacket{
|
||||
Extended: false,
|
||||
Networks: zones.Query(zipkt.Networks),
|
||||
}
|
||||
// TODO: direct to queryer
|
||||
|
||||
case *zip.GetNetInfoPacket:
|
||||
// Only running a network with one zone for now.
|
||||
resp := &zip.GetNetInfoReplyPacket{
|
||||
resp = &zip.GetNetInfoReplyPacket{
|
||||
ZoneInvalid: zipkt.ZoneName != cfg.EtherTalk.ZoneName,
|
||||
UseBroadcast: true, // TODO: add multicast addr computation
|
||||
OnlyOneZone: true,
|
||||
|
@ -113,9 +126,17 @@ func handleZIP(pcapHandle *pcap.Handle, srcHWAddr, myHWAddr ethernet.Addr, myAdd
|
|||
MulticastAddr: ethertalk.AppleTalkBroadcast,
|
||||
DefaultZoneName: cfg.EtherTalk.ZoneName,
|
||||
}
|
||||
|
||||
default:
|
||||
return fmt.Errorf("TODO: handle type %T", zipkt)
|
||||
}
|
||||
|
||||
if resp == nil {
|
||||
return nil
|
||||
}
|
||||
respRaw, err := resp.Marshal()
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't marshal GetNetInfoReplyPacket: %w", err)
|
||||
return fmt.Errorf("couldn't marshal %T: %w", resp, err)
|
||||
}
|
||||
|
||||
// TODO: fix
|
||||
|
@ -146,10 +167,6 @@ func handleZIP(pcapHandle *pcap.Handle, srcHWAddr, myHWAddr ethernet.Addr, myAdd
|
|||
}
|
||||
return nil
|
||||
|
||||
default:
|
||||
return fmt.Errorf("TODO: handle type %T", zipkt)
|
||||
}
|
||||
|
||||
default:
|
||||
return fmt.Errorf("invalid DDP type %d on socket 6", ddpkt.Proto)
|
||||
}
|
||||
|
|
18
zones.go
18
zones.go
|
@ -17,6 +17,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -67,6 +68,23 @@ func (zt *ZoneTable) Upsert(network ddp.Network, name string, local bool) {
|
|||
}
|
||||
}
|
||||
|
||||
func (zt *ZoneTable) Query(ns []ddp.Network) map[ddp.Network][]string {
|
||||
slices.Sort(ns)
|
||||
zs := make(map[ddp.Network][]string)
|
||||
|
||||
zt.mu.Lock()
|
||||
defer zt.mu.Unlock()
|
||||
for _, z := range zt.zones {
|
||||
if time.Since(z.LastSeen) > maxZoneAge {
|
||||
continue
|
||||
}
|
||||
if _, ok := slices.BinarySearch(ns, z.Network); ok {
|
||||
zs[z.Network] = append(zs[z.Network], z.Name)
|
||||
}
|
||||
}
|
||||
return zs
|
||||
}
|
||||
|
||||
func (zt *ZoneTable) LocalNames() []string {
|
||||
zt.mu.Lock()
|
||||
seen := make(map[string]struct{})
|
||||
|
|
Loading…
Reference in a new issue