Fix: exclude the broadcast address to fake ip pool

This commit is contained in:
yaling888 2022-03-15 02:43:40 +08:00
parent 359f8ffca3
commit 5abd03e241
3 changed files with 43 additions and 15 deletions

View file

@ -22,14 +22,15 @@ type store interface {
// Pool is a implementation about fake ip generator without storage // Pool is a implementation about fake ip generator without storage
type Pool struct { type Pool struct {
max uint32 max uint32
min uint32 min uint32
gateway uint32 gateway uint32
offset uint32 broadcast uint32
mux sync.Mutex offset uint32
host *trie.DomainTrie mux sync.Mutex
ipnet *net.IPNet host *trie.DomainTrie
store store ipnet *net.IPNet
store store
} }
// Lookup return a fake ip with host // Lookup return a fake ip with host
@ -82,6 +83,11 @@ func (p *Pool) Gateway() net.IP {
return uintToIP(p.gateway) return uintToIP(p.gateway)
} }
// Broadcast return broadcast ip
func (p *Pool) Broadcast() net.IP {
return uintToIP(p.broadcast)
}
// IPNet return raw ipnet // IPNet return raw ipnet
func (p *Pool) IPNet() *net.IPNet { func (p *Pool) IPNet() *net.IPNet {
return p.ipnet return p.ipnet
@ -144,7 +150,7 @@ func New(options Options) (*Pool, error) {
min := ipToUint(options.IPNet.IP) + 2 min := ipToUint(options.IPNet.IP) + 2
ones, bits := options.IPNet.Mask.Size() ones, bits := options.IPNet.Mask.Size()
total := 1<<uint(bits-ones) - 2 total := 1<<uint(bits-ones) - 3
if total <= 0 { if total <= 0 {
return nil, errors.New("ipnet don't have valid ip") return nil, errors.New("ipnet don't have valid ip")
@ -152,11 +158,12 @@ func New(options Options) (*Pool, error) {
max := min + uint32(total) - 1 max := min + uint32(total) - 1
pool := &Pool{ pool := &Pool{
min: min, min: min,
max: max, max: max,
gateway: min - 1, gateway: min - 1,
host: options.Host, broadcast: max + 1,
ipnet: options.IPNet, host: options.Host,
ipnet: options.IPNet,
} }
if options.Persistence { if options.Persistence {
pool.store = &cachefileStore{ pool.store = &cachefileStore{

View file

@ -10,6 +10,7 @@ type Enhancer interface {
FakeIPEnabled() bool FakeIPEnabled() bool
MappingEnabled() bool MappingEnabled() bool
IsFakeIP(net.IP) bool IsFakeIP(net.IP) bool
IsFakeBroadcastIP(net.IP) bool
IsExistFakeIP(net.IP) bool IsExistFakeIP(net.IP) bool
FindHostByIP(net.IP) (string, bool) FindHostByIP(net.IP) (string, bool)
} }
@ -38,6 +39,14 @@ func IsFakeIP(ip net.IP) bool {
return false return false
} }
func IsFakeBroadcastIP(ip net.IP) bool {
if mapper := DefaultHostMapper; mapper != nil {
return mapper.IsFakeBroadcastIP(ip)
}
return false
}
func IsExistFakeIP(ip net.IP) bool { func IsExistFakeIP(ip net.IP) bool {
if mapper := DefaultHostMapper; mapper != nil { if mapper := DefaultHostMapper; mapper != nil {
return mapper.IsExistFakeIP(ip) return mapper.IsExistFakeIP(ip)

View file

@ -40,7 +40,19 @@ func (h *ResolverEnhancer) IsFakeIP(ip net.IP) bool {
} }
if pool := h.fakePool; pool != nil { if pool := h.fakePool; pool != nil {
return pool.IPNet().Contains(ip) && !pool.Gateway().Equal(ip) return pool.IPNet().Contains(ip) && !pool.Gateway().Equal(ip) && !pool.Broadcast().Equal(ip)
}
return false
}
func (h *ResolverEnhancer) IsFakeBroadcastIP(ip net.IP) bool {
if !h.FakeIPEnabled() {
return false
}
if pool := h.fakePool; pool != nil {
return pool.Broadcast().Equal(ip)
} }
return false return false