Add broadcast filter

This commit is contained in:
世界 2023-11-06 21:33:35 +08:00
parent 1a00992d06
commit da350ecc72
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 26 additions and 6 deletions

View file

@ -2,6 +2,8 @@ package tun
import (
"context"
"encoding/binary"
"net"
"net/netip"
"github.com/sagernet/sing/common/control"
@ -52,3 +54,13 @@ func NewStack(
return nil, E.New("unknown stack: ", stack)
}
}
func BroadcastAddr(inet4Address []netip.Prefix) netip.Addr {
if len(inet4Address) == 0 {
return netip.Addr{}
}
prefix := inet4Address[0]
var broadcastAddr [4]byte
binary.BigEndian.PutUint32(broadcastAddr[:], binary.BigEndian.Uint32(prefix.Masked().Addr().AsSlice())|^binary.BigEndian.Uint32(net.CIDRMask(prefix.Bits(), 32)))
return netip.AddrFrom4(broadcastAddr)
}