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