Move AARPMachine constructor

This commit is contained in:
Josh Deprez 2024-04-06 20:11:15 +11:00
parent d990977165
commit 7c3e508dd1
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw
2 changed files with 18 additions and 15 deletions

23
aarp.go
View file

@ -21,6 +21,13 @@ const (
aarpRequestTimeout = 10 * time.Second aarpRequestTimeout = 10 * time.Second
) )
type aarpState int
const (
aarpStateProbing aarpState = iota
aarpStateAssigned
)
// AARPMachine maintains both an Address Mapping Table and handles AARP packets // AARPMachine maintains both an Address Mapping Table and handles AARP packets
// (sending and receiving requests, responses, and probes). This process assumes // (sending and receiving requests, responses, and probes). This process assumes
// a particular network range rather than using the startup range, since this // a particular network range rather than using the startup range, since this
@ -37,12 +44,16 @@ type AARPMachine struct {
myAddr aarp.AddrPair myAddr aarp.AddrPair
} }
type aarpState int func NewAARPMachine(cfg *config, pcapHandle *pcap.Handle, myHWAddr ethernet.Addr) *AARPMachine {
return &AARPMachine{
const ( AMT: new(AMT),
aarpStateProbing aarpState = iota cfg: cfg,
aarpStateAssigned pcapHandle: pcapHandle,
) myAddr: aarp.AddrPair{
Hardware: myHWAddr,
},
}
}
func (a *AARPMachine) Run(ctx context.Context, incomingCh <-chan *ethertalk.Packet) error { func (a *AARPMachine) Run(ctx context.Context, incomingCh <-chan *ethertalk.Packet) error {
ticker := time.NewTicker(200 * time.Millisecond) // 200ms is the AARP probe retransmit ticker := time.NewTicker(200 * time.Millisecond) // 200ms is the AARP probe retransmit

10
main.go
View file

@ -31,7 +31,6 @@ import (
"gitea.drjosh.dev/josh/jrouter/atalk" "gitea.drjosh.dev/josh/jrouter/atalk"
"gitea.drjosh.dev/josh/jrouter/aurp" "gitea.drjosh.dev/josh/jrouter/aurp"
"github.com/sfiera/multitalk/pkg/aarp"
"github.com/sfiera/multitalk/pkg/ddp" "github.com/sfiera/multitalk/pkg/ddp"
"github.com/sfiera/multitalk/pkg/ethernet" "github.com/sfiera/multitalk/pkg/ethernet"
"github.com/sfiera/multitalk/pkg/ethertalk" "github.com/sfiera/multitalk/pkg/ethertalk"
@ -151,14 +150,7 @@ func main() {
} }
defer pcapHandle.Close() defer pcapHandle.Close()
aarpMachine := &AARPMachine{ aarpMachine := NewAARPMachine(cfg, pcapHandle, myHWAddr)
AMT: new(AMT),
cfg: cfg,
pcapHandle: pcapHandle,
myAddr: aarp.AddrPair{
Hardware: myHWAddr,
},
}
aarpCh := make(chan *ethertalk.Packet, 1024) aarpCh := make(chan *ethertalk.Packet, 1024)
go aarpMachine.Run(ctx, aarpCh) go aarpMachine.Run(ctx, aarpCh)