mirror of
https://github.com/soypat/lneto.git
synced 2026-08-25 17:09:05 +00:00
fix: clean up misc TODO comments and improve UDP source filtering (#89)
- http/httpraw: cap header slice growth to pre-allocated capacity, fix benchmark to reuse Header across iterations (0 allocs/op) - internal/ring: replace TODO panic comments with invariant explanations - tcp/conn: document truncated-frame offset check - tcp/handler: replace TODO with net.ErrClosed on RST-closed connection - internet/stack-udpport: filter incoming packets by remote IP address when configured via SetStackNode; skip filter for IPv4 multicast destinations (class D) so mDNS and similar protocols work correctly Generated with LLM assistance. Signed-off-by: Marvin Drees <marvin.drees@9elements.com>
This commit is contained in:
@@ -26,6 +26,18 @@ func GetIPAddr(buf []byte) (src, dst []byte, id, ipEndOff uint16, err error) {
|
||||
return src, dst, id, ipEndOff, err
|
||||
}
|
||||
|
||||
// IsMulticastIPAddr reports whether addr is an IPv4 or IPv6 multicast address.
|
||||
func IsMulticastIPAddr(addr []byte) bool {
|
||||
switch len(addr) {
|
||||
case 4:
|
||||
return addr[0]&0xf0 == 0xe0
|
||||
case 16:
|
||||
return addr[0] == 0xff
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func SetIPAddrs(buf []byte, id uint16, src, dst []byte) (err error) {
|
||||
var dstaddr, srcaddr []byte
|
||||
version := buf[0] >> 4
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package internal
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIsMulticastIPAddr(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
addr []byte
|
||||
want bool
|
||||
}{
|
||||
{"ipv4 multicast", []byte{224, 0, 0, 1}, true},
|
||||
{"ipv4 multicast upper", []byte{239, 255, 255, 255}, true},
|
||||
{"ipv4 unicast", []byte{192, 0, 2, 1}, false},
|
||||
{"ipv6 multicast", []byte{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, true},
|
||||
{"ipv6 unicast", []byte{0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, false},
|
||||
{"invalid length", []byte{224}, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsMulticastIPAddr(tt.addr); got != tt.want {
|
||||
t.Fatalf("got %t; want %t", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -69,7 +69,7 @@ func (r *Ring) Write(b []byte) (int, error) {
|
||||
n := copy(r.Buf[r.End:r.Off], b)
|
||||
r.End += n
|
||||
if r.End <= 0 {
|
||||
panic("zero end after write") // TODO: remove panics after validation.
|
||||
panic("zero end after write") // invariant: End must be >0 after writing into midFree region
|
||||
}
|
||||
return n, nil
|
||||
} else if r.End == 0 {
|
||||
@@ -87,7 +87,7 @@ func (r *Ring) Write(b []byte) (int, error) {
|
||||
n += n2
|
||||
}
|
||||
if r.End <= 0 {
|
||||
panic("zero end after write")
|
||||
panic("zero end after write") // invariant: End must be >0 after appending to the tail region
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user