Handle SIGINT, send RD
This commit is contained in:
parent
520046d86a
commit
64f491fcba
3 changed files with 25 additions and 5 deletions
|
@ -111,6 +111,17 @@ func (tr *Transport) NewOpenRspPacket(envFlags RoutingFlag, rateOrErr int16, opt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tr *Transport) NewRDPacket(errCode ErrorCode) *RDPacket {
|
||||||
|
return &RDPacket{
|
||||||
|
Header: Header{
|
||||||
|
TrHeader: tr.transaction(tr.LocalConnID),
|
||||||
|
CommandCode: CmdCodeRD,
|
||||||
|
Flags: 0,
|
||||||
|
},
|
||||||
|
ErrorCode: errCode,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (tr *Transport) NewTicklePacket() *TicklePacket {
|
func (tr *Transport) NewTicklePacket() *TicklePacket {
|
||||||
return &TicklePacket{
|
return &TicklePacket{
|
||||||
Header: Header{
|
Header: Header{
|
||||||
|
|
13
main.go
13
main.go
|
@ -5,6 +5,8 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"gitea.drjosh.dev/josh/jrouter/aurp"
|
"gitea.drjosh.dev/josh/jrouter/aurp"
|
||||||
|
@ -18,8 +20,6 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
log.Println("jrouter")
|
log.Println("jrouter")
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
cfg, err := loadConfig(*configFilePath)
|
cfg, err := loadConfig(*configFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Couldn't load configuration file: %v", err)
|
log.Fatalf("Couldn't load configuration file: %v", err)
|
||||||
|
@ -62,6 +62,9 @@ func main() {
|
||||||
defer ln.Close()
|
defer ln.Close()
|
||||||
log.Printf("Listening on %v", ln.LocalAddr())
|
log.Printf("Listening on %v", ln.LocalAddr())
|
||||||
|
|
||||||
|
log.Println("Press ^C or send SIGINT to stop the router gracefully")
|
||||||
|
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||||
|
|
||||||
for _, peerStr := range cfg.Peers {
|
for _, peerStr := range cfg.Peers {
|
||||||
if !hasPortRE.MatchString(peerStr) {
|
if !hasPortRE.MatchString(peerStr) {
|
||||||
peerStr += ":387"
|
peerStr += ":387"
|
||||||
|
@ -92,15 +95,15 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Incoming packet loop
|
// Incoming packet loop
|
||||||
pb := make([]byte, 65536)
|
|
||||||
for {
|
for {
|
||||||
pktlen, raddr, readErr := ln.ReadFromUDP(pb)
|
pktbuf := make([]byte, 65536)
|
||||||
|
pktlen, raddr, readErr := ln.ReadFromUDP(pktbuf)
|
||||||
// net.PacketConn.ReadFrom: "Callers should always process
|
// net.PacketConn.ReadFrom: "Callers should always process
|
||||||
// the n > 0 bytes returned before considering the error err."
|
// the n > 0 bytes returned before considering the error err."
|
||||||
|
|
||||||
log.Printf("Received packet of length %d from %v", pktlen, raddr)
|
log.Printf("Received packet of length %d from %v", pktlen, raddr)
|
||||||
|
|
||||||
dh, pkt, parseErr := aurp.ParsePacket(pb[:pktlen])
|
dh, pkt, parseErr := aurp.ParsePacket(pktbuf[:pktlen])
|
||||||
if parseErr != nil {
|
if parseErr != nil {
|
||||||
log.Printf("Failed to parse packet: %v", parseErr)
|
log.Printf("Failed to parse packet: %v", parseErr)
|
||||||
continue
|
continue
|
||||||
|
|
6
peer.go
6
peer.go
|
@ -32,6 +32,8 @@ func (p *peer) handle(ctx context.Context) error {
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
p.lastHeardFrom = time.Now()
|
||||||
|
|
||||||
// Write an Open-Req packet
|
// Write an Open-Req packet
|
||||||
n, err := p.send(p.tr.NewOpenReqPacket(nil))
|
n, err := p.send(p.tr.NewOpenReqPacket(nil))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -43,6 +45,10 @@ func (p *peer) handle(ctx context.Context) error {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
// Send a best-effort Router Down before returning
|
||||||
|
if _, err := p.send(p.tr.NewRDPacket(aurp.ErrCodeNormalClose)); err != nil {
|
||||||
|
log.Printf("Couldn't send RD packet: %v", err)
|
||||||
|
}
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
|
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
|
|
Loading…
Reference in a new issue