jrouter/aurp/routing_info.go

279 lines
5.7 KiB
Go
Raw Normal View History

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-17 12:56:56 +11:00
package aurp
import (
"encoding/binary"
"fmt"
"io"
2024-04-01 14:56:49 +11:00
"strings"
"github.com/sfiera/multitalk/pkg/ddp"
2024-03-17 12:56:56 +11:00
)
type RIReqPacket struct {
2024-03-17 13:31:26 +11:00
Header
2024-03-17 12:56:56 +11:00
}
type RIRspPacket struct {
2024-03-17 13:31:26 +11:00
Header
2024-03-17 12:56:56 +11:00
Networks NetworkTuples
}
func (p *RIRspPacket) WriteTo(w io.Writer) (int64, error) {
a := acc(w)
2024-03-17 13:31:26 +11:00
a.writeTo(&p.Header)
2024-03-17 12:56:56 +11:00
a.writeTo(p.Networks)
return a.ret()
}
func parseRIRsp(p []byte) (*RIRspPacket, error) {
n, err := parseNetworkTuples(p)
if err != nil {
return nil, err
}
return &RIRspPacket{
Networks: n,
}, nil
}
type RIAckPacket struct {
2024-03-17 13:31:26 +11:00
Header
2024-03-17 12:56:56 +11:00
}
type RIUpdPacket struct {
2024-03-17 13:31:26 +11:00
Header
2024-03-17 12:56:56 +11:00
Events EventTuples
}
func (p *RIUpdPacket) WriteTo(w io.Writer) (int64, error) {
a := acc(w)
2024-03-17 13:31:26 +11:00
a.writeTo(&p.Header)
2024-03-17 12:56:56 +11:00
a.writeTo(p.Events)
return a.ret()
}
func parseRIUpd(p []byte) (*RIUpdPacket, error) {
e, err := parseEventTuples(p)
if err != nil {
return nil, err
}
return &RIUpdPacket{
Events: e,
}, nil
}
type NetworkTuples []NetworkTuple
2024-04-01 14:56:49 +11:00
func (n NetworkTuples) String() string {
var sb strings.Builder
for i, nt := range n {
if i > 0 {
sb.WriteString(", ")
}
fmt.Fprintf(&sb, "%d-%d dist %d", nt.RangeStart, nt.RangeEnd, nt.Distance)
}
return sb.String()
}
2024-03-17 12:56:56 +11:00
func (n NetworkTuples) WriteTo(w io.Writer) (int64, error) {
a := acc(w)
for _, nt := range n {
a.writeTo(&nt)
}
return a.ret()
}
func parseNetworkTuples(p []byte) (NetworkTuples, error) {
// Each network tuple is at least 3 bytes, so we need to store at most
// len(p)/3 of them.
n := make(NetworkTuples, 0, len(p)/3)
for len(p) > 0 {
nt, nextp, err := parseNetworkTuple(p)
if err != nil {
return nil, fmt.Errorf("parsing network tuple %d: %w", len(n), err)
}
n = append(n, nt)
p = nextp
}
return n, nil
}
type NetworkTuple struct {
2024-04-06 16:06:32 +11:00
Extended bool
RangeStart ddp.Network
2024-03-17 12:56:56 +11:00
Distance uint8
RangeEnd ddp.Network
2024-03-17 12:56:56 +11:00
// 0x00 for extended tuples
}
func (nt *NetworkTuple) WriteTo(w io.Writer) (int64, error) {
a := acc(w)
a.write16(uint16(nt.RangeStart))
2024-04-06 16:06:32 +11:00
if !nt.Extended {
2024-03-17 12:56:56 +11:00
// non-extended tuple
a.write8(nt.Distance)
return a.ret()
}
// extended tuple
a.write8(nt.Distance | 0x80)
a.write16(uint16(nt.RangeEnd))
2024-03-17 12:56:56 +11:00
a.write8(0x00)
return a.ret()
}
func parseNetworkTuple(p []byte) (NetworkTuple, []byte, error) {
if len(p) < 3 {
return NetworkTuple{}, p, fmt.Errorf("insufficient input length %d for network tuple", len(p))
}
var nt NetworkTuple
nt.RangeStart = ddp.Network(binary.BigEndian.Uint16(p[:2]))
2024-03-17 12:56:56 +11:00
nt.RangeEnd = nt.RangeStart
nt.Distance = p[2]
2024-04-06 16:06:32 +11:00
nt.Extended = nt.Distance&0x80 != 0
2024-03-17 12:56:56 +11:00
2024-04-06 16:06:32 +11:00
if !nt.Extended {
2024-03-17 12:56:56 +11:00
return nt, p[3:], nil
}
if len(p) < 6 {
return NetworkTuple{}, p, fmt.Errorf("insufficient input length %d for extended network tuple", len(p))
}
nt.Distance &^= 0x80
nt.RangeEnd = ddp.Network(binary.BigEndian.Uint16(p[3:5]))
2024-03-17 12:56:56 +11:00
return nt, p[6:], nil
}
type EventTuples []EventTuple
func (e EventTuples) WriteTo(w io.Writer) (int64, error) {
a := acc(w)
for _, et := range e {
a.writeTo(&et)
}
return a.ret()
}
func parseEventTuples(p []byte) (EventTuples, error) {
// Each event tuple is at least 4 bytes, so we need to store at most
// len(p)/4 of them.
e := make(EventTuples, 0, len(p)/4)
for len(p) > 0 {
et, nextp, err := parseEventTuple(p)
if err != nil {
return nil, fmt.Errorf("parsing event tuple %d: %w", len(e), err)
}
e = append(e, et)
p = nextp
}
return e, nil
}
type EventTuple struct {
EventCode EventCode
2024-04-06 16:06:32 +11:00
Extended bool
RangeStart ddp.Network
2024-03-17 12:56:56 +11:00
Distance uint8
RangeEnd ddp.Network
2024-03-17 12:56:56 +11:00
}
func (et *EventTuple) WriteTo(w io.Writer) (int64, error) {
a := acc(w)
a.write8(uint8(et.EventCode))
a.write16(uint16(et.RangeStart))
2024-04-06 16:06:32 +11:00
if !et.Extended {
2024-03-17 12:56:56 +11:00
// non-extended tuple
a.write8(et.Distance)
return a.ret()
}
// extended tuple
a.write8(et.Distance | 0x80)
a.write16(uint16(et.RangeEnd))
2024-03-17 12:56:56 +11:00
return a.ret()
}
func parseEventTuple(p []byte) (EventTuple, []byte, error) {
if len(p) < 4 {
return EventTuple{}, p, fmt.Errorf("insufficient input length %d for network event tuple", len(p))
}
var et EventTuple
et.EventCode = EventCode(p[0])
et.RangeStart = ddp.Network(binary.BigEndian.Uint16(p[1:3]))
2024-03-17 12:56:56 +11:00
et.RangeEnd = et.RangeStart
et.Distance = p[3]
2024-04-06 16:06:32 +11:00
et.Extended = et.Distance&0x80 != 0
2024-03-17 12:56:56 +11:00
2024-04-06 16:06:32 +11:00
if !et.Extended {
2024-03-17 12:56:56 +11:00
return et, p[4:], nil
}
if len(p) < 6 {
return EventTuple{}, p, fmt.Errorf("insufficient input length %d for extended network event tuple", len(p))
}
et.Distance &^= 0x80
et.RangeEnd = ddp.Network(binary.BigEndian.Uint16(p[4:6]))
2024-03-17 12:56:56 +11:00
return et, p[6:], nil
}
type EventCode uint8
const (
// Null event
2024-03-17 12:56:56 +11:00
EventCodeNull EventCode = 0
// Network added event
EventCodeNA EventCode = 1
// Network deleted event
EventCodeND EventCode = 2
// Network route change event
EventCodeNRC EventCode = 3
// Network distance change event
EventCodeNDC EventCode = 4
// Network zone change event
// Note: "The ZC event tuple is not yet defined."
EventCodeZC EventCode = 5
2024-03-17 12:56:56 +11:00
)
2024-04-01 15:33:57 +11:00
func (ec EventCode) String() string {
switch ec {
case EventCodeNull:
return "null"
case EventCodeNA:
return "network added"
case EventCodeND:
return "network deleted"
case EventCodeNRC:
return "network route change"
case EventCodeNDC:
return "network distance change"
case EventCodeZC:
return "zone name change"
default:
return "unknown"
}
2024-04-01 15:33:57 +11:00
}