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
type Pool struct {
max uint32
min uint32
gateway uint32
offset uint32
mux sync.Mutex
host *trie.DomainTrie
ipnet *net.IPNet
store store
max uint32
min uint32
gateway uint32
broadcast uint32
offset uint32
mux sync.Mutex
host *trie.DomainTrie
ipnet *net.IPNet
store store
}
// Lookup return a fake ip with host
@ -82,6 +83,11 @@ func (p *Pool) Gateway() net.IP {
return uintToIP(p.gateway)
}
// Broadcast return broadcast ip
func (p *Pool) Broadcast() net.IP {
return uintToIP(p.broadcast)
}
// IPNet return raw ipnet
func (p *Pool) IPNet() *net.IPNet {
return p.ipnet
@ -144,7 +150,7 @@ func New(options Options) (*Pool, error) {
min := ipToUint(options.IPNet.IP) + 2
ones, bits := options.IPNet.Mask.Size()
total := 1<<uint(bits-ones) - 2
total := 1<<uint(bits-ones) - 3
if total <= 0 {
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
pool := &Pool{
min: min,
max: max,
gateway: min - 1,
host: options.Host,
ipnet: options.IPNet,
min: min,
max: max,
gateway: min - 1,
broadcast: max + 1,
host: options.Host,
ipnet: options.IPNet,
}
if options.Persistence {
pool.store = &cachefileStore{

View file

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

View file

@ -40,7 +40,19 @@ func (h *ResolverEnhancer) IsFakeIP(ip net.IP) bool {
}
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