misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
evilsocket 2018-02-20 11:38:41 +01:00
parent 241db0cc66
commit 9da2df981c

View file

@ -16,10 +16,9 @@ type EndpointLostCallback func(e *Endpoint)
type LAN struct {
sync.Mutex
Interface *Endpoint
Gateway *Endpoint
hosts map[string]*Endpoint
Hosts map[string]*Endpoint `json:"hosts"`
iface *Endpoint
gateway *Endpoint
ttl map[string]uint
aliases *Aliases
newCb EndpointNewCallback
@ -34,13 +33,13 @@ func NewLAN(iface, gateway *Endpoint, newcb EndpointNewCallback, lostcb Endpoint
}
return &LAN{
Interface: iface,
Gateway: gateway,
hosts: make(map[string]*Endpoint),
ttl: make(map[string]uint),
aliases: aliases,
newCb: newcb,
lostCb: lostcb,
iface: iface,
gateway: gateway,
Hosts: make(map[string]*Endpoint),
ttl: make(map[string]uint),
aliases: aliases,
newCb: newcb,
lostCb: lostcb,
}
}
@ -49,7 +48,7 @@ func (lan *LAN) SetAliasFor(mac, alias string) bool {
defer lan.Unlock()
mac = NormalizeMac(mac)
if e, found := lan.hosts[mac]; found {
if e, found := lan.Hosts[mac]; found {
lan.aliases.Set(mac, alias)
e.Alias = alias
return true
@ -61,7 +60,7 @@ func (lan *LAN) Get(mac string) (*Endpoint, bool) {
lan.Lock()
defer lan.Unlock()
if e, found := lan.hosts[mac]; found == true {
if e, found := lan.Hosts[mac]; found == true {
return e, true
}
return nil, false
@ -72,14 +71,14 @@ func (lan *LAN) List() (list []*Endpoint) {
defer lan.Unlock()
list = make([]*Endpoint, 0)
for _, t := range lan.hosts {
for _, t := range lan.Hosts {
list = append(list, t)
}
return
}
func (lan *LAN) WasMissed(mac string) bool {
if mac == lan.Interface.HwAddress || mac == lan.Gateway.HwAddress {
if mac == lan.iface.HwAddress || mac == lan.gateway.HwAddress {
return false
}
@ -96,10 +95,10 @@ func (lan *LAN) Remove(ip, mac string) {
lan.Lock()
defer lan.Unlock()
if e, found := lan.hosts[mac]; found {
if e, found := lan.Hosts[mac]; found {
lan.ttl[mac]--
if lan.ttl[mac] == 0 {
delete(lan.hosts, mac)
delete(lan.Hosts, mac)
delete(lan.ttl, mac)
lan.lostCb(e)
}
@ -109,11 +108,11 @@ func (lan *LAN) Remove(ip, mac string) {
func (lan *LAN) shouldIgnore(ip, mac string) bool {
// skip our own address
if ip == lan.Interface.IpAddress {
if ip == lan.iface.IpAddress {
return true
}
// skip the gateway
if ip == lan.Gateway.IpAddress {
if ip == lan.gateway.IpAddress {
return true
}
// skip broadcast addresses
@ -126,14 +125,14 @@ func (lan *LAN) shouldIgnore(ip, mac string) bool {
}
// skip everything which is not in our subnet (multicast noise)
addr := net.ParseIP(ip)
return lan.Interface.Net.Contains(addr) == false
return lan.iface.Net.Contains(addr) == false
}
func (lan *LAN) Has(ip string) bool {
lan.Lock()
defer lan.Unlock()
for _, e := range lan.hosts {
for _, e := range lan.Hosts {
if e.IpAddress == ip {
return true
}
@ -146,7 +145,7 @@ func (lan *LAN) EachHost(cb func(mac string, e *Endpoint)) {
lan.Lock()
defer lan.Unlock()
for m, h := range lan.hosts {
for m, h := range lan.Hosts {
cb(m, h)
}
}
@ -155,7 +154,7 @@ func (lan *LAN) GetByIp(ip string) *Endpoint {
lan.Lock()
defer lan.Unlock()
for _, e := range lan.hosts {
for _, e := range lan.Hosts {
if e.IpAddress == ip {
return e
}
@ -172,7 +171,7 @@ func (lan *LAN) AddIfNew(ip, mac string) *Endpoint {
if lan.shouldIgnore(ip, mac) {
return nil
} else if t, found := lan.hosts[mac]; found {
} else if t, found := lan.Hosts[mac]; found {
if lan.ttl[mac] < LANDefaultttl {
lan.ttl[mac]++
}
@ -181,7 +180,7 @@ func (lan *LAN) AddIfNew(ip, mac string) *Endpoint {
e := NewEndpointWithAlias(ip, mac, lan.aliases.Get(mac))
lan.hosts[mac] = e
lan.Hosts[mac] = e
lan.ttl[mac] = LANDefaultttl
lan.newCb(e)