lan.go 3.78 KB
Newer Older
Sophie Brun's avatar
Sophie Brun committed
1 2 3 4 5 6 7
package network

import (
	"encoding/json"
	"net"
	"strings"
	"sync"
Sophie Brun's avatar
Sophie Brun committed
8 9

	"github.com/evilsocket/islazy/data"
Sophie Brun's avatar
Sophie Brun committed
10 11 12 13 14 15 16 17 18
)

const LANDefaultttl = 10

type EndpointNewCallback func(e *Endpoint)
type EndpointLostCallback func(e *Endpoint)

type LAN struct {
	sync.Mutex
Sophie Brun's avatar
Sophie Brun committed
19 20 21 22
	hosts   map[string]*Endpoint
	iface   *Endpoint
	gateway *Endpoint
	ttl     map[string]uint
Sophie Brun's avatar
Sophie Brun committed
23
	aliases *data.UnsortedKV
Sophie Brun's avatar
Sophie Brun committed
24 25
	newCb   EndpointNewCallback
	lostCb  EndpointLostCallback
Sophie Brun's avatar
Sophie Brun committed
26 27 28 29 30 31
}

type lanJSON struct {
	Hosts []*Endpoint `json:"hosts"`
}

Sophie Brun's avatar
Sophie Brun committed
32
func NewLAN(iface, gateway *Endpoint, aliases *data.UnsortedKV, newcb EndpointNewCallback, lostcb EndpointLostCallback) *LAN {
Sophie Brun's avatar
Sophie Brun committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	return &LAN{
		iface:   iface,
		gateway: gateway,
		hosts:   make(map[string]*Endpoint),
		ttl:     make(map[string]uint),
		aliases: aliases,
		newCb:   newcb,
		lostCb:  lostcb,
	}
}

func (l *LAN) MarshalJSON() ([]byte, error) {
	doc := lanJSON{
		Hosts: make([]*Endpoint, 0),
	}

	for _, h := range l.hosts {
		doc.Hosts = append(doc.Hosts, h)
	}

	return json.Marshal(doc)
}

func (lan *LAN) Get(mac string) (*Endpoint, bool) {
	lan.Lock()
	defer lan.Unlock()

Sophie Brun's avatar
Sophie Brun committed
60 61 62 63 64 65 66 67
	mac = NormalizeMac(mac)

	if mac == lan.iface.HwAddress {
		return lan.iface, true
	} else if mac == lan.gateway.HwAddress {
		return lan.gateway, true
	}

Sophie Brun's avatar
Sophie Brun committed
68
	if e, found := lan.hosts[mac]; found {
Sophie Brun's avatar
Sophie Brun committed
69 70 71 72 73
		return e, true
	}
	return nil, false
}

Sophie Brun's avatar
Sophie Brun committed
74 75 76 77
func (lan *LAN) GetByIp(ip string) *Endpoint {
	lan.Lock()
	defer lan.Unlock()

Sophie Brun's avatar
Sophie Brun committed
78 79 80 81 82 83
	if ip == lan.iface.IpAddress {
		return lan.iface
	} else if ip == lan.gateway.IpAddress {
		return lan.gateway
	}

Sophie Brun's avatar
Sophie Brun committed
84 85 86 87 88 89 90 91 92
	for _, e := range lan.hosts {
		if e.IpAddress == ip {
			return e
		}
	}

	return nil
}

Sophie Brun's avatar
Sophie Brun committed
93 94 95 96 97 98 99 100 101 102 103
func (lan *LAN) List() (list []*Endpoint) {
	lan.Lock()
	defer lan.Unlock()

	list = make([]*Endpoint, 0)
	for _, t := range lan.hosts {
		list = append(list, t)
	}
	return
}

Sophie Brun's avatar
Sophie Brun committed
104
func (lan *LAN) Aliases() *data.UnsortedKV {
Sophie Brun's avatar
Sophie Brun committed
105 106 107 108 109 110 111 112 113 114 115
	return lan.aliases
}

func (lan *LAN) WasMissed(mac string) bool {
	if mac == lan.iface.HwAddress || mac == lan.gateway.HwAddress {
		return false
	}

	lan.Lock()
	defer lan.Unlock()

Sophie Brun's avatar
Sophie Brun committed
116
	if ttl, found := lan.ttl[mac]; found {
Sophie Brun's avatar
Sophie Brun committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		return ttl < LANDefaultttl
	}
	return true
}

func (lan *LAN) Remove(ip, mac string) {
	lan.Lock()
	defer lan.Unlock()

	if e, found := lan.hosts[mac]; found {
		lan.ttl[mac]--
		if lan.ttl[mac] == 0 {
			delete(lan.hosts, mac)
			delete(lan.ttl, mac)
			lan.lostCb(e)
		}
		return
	}
}

func (lan *LAN) shouldIgnore(ip, mac string) bool {
	// skip our own address
	if ip == lan.iface.IpAddress || mac == lan.iface.HwAddress {
		return true
	}
	// skip the gateway
	if ip == lan.gateway.IpAddress || mac == lan.gateway.HwAddress {
		return true
	}
	// skip broadcast addresses
	if strings.HasSuffix(ip, BroadcastSuffix) {
		return true
	}
	// skip broadcast macs
	if strings.ToLower(mac) == BroadcastMac {
		return true
	}
	// skip everything which is not in our subnet (multicast noise)
	addr := net.ParseIP(ip)
Sophie Brun's avatar
Sophie Brun committed
156
	return !lan.iface.Net.Contains(addr)
Sophie Brun's avatar
Sophie Brun committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
}

func (lan *LAN) Has(ip string) bool {
	lan.Lock()
	defer lan.Unlock()

	for _, e := range lan.hosts {
		if e.IpAddress == ip {
			return true
		}
	}

	return false
}

func (lan *LAN) EachHost(cb func(mac string, e *Endpoint)) {
	lan.Lock()
	defer lan.Unlock()

	for m, h := range lan.hosts {
		cb(m, h)
	}
}

func (lan *LAN) AddIfNew(ip, mac string) *Endpoint {
	lan.Lock()
	defer lan.Unlock()

	mac = NormalizeMac(mac)

	if lan.shouldIgnore(ip, mac) {
		return nil
	} else if t, found := lan.hosts[mac]; found {
		if lan.ttl[mac] < LANDefaultttl {
			lan.ttl[mac]++
		}
		return t
	}

Sophie Brun's avatar
Sophie Brun committed
196
	e := NewEndpointWithAlias(ip, mac, lan.aliases.GetOr(mac, ""))
Sophie Brun's avatar
Sophie Brun committed
197 198 199 200 201 202 203 204 205 206

	lan.hosts[mac] = e
	lan.ttl[mac] = LANDefaultttl

	lan.newCb(e)

	return nil
}

func (lan *LAN) GetAlias(mac string) string {
Sophie Brun's avatar
Sophie Brun committed
207
	return lan.aliases.GetOr(mac, "")
Sophie Brun's avatar
Sophie Brun committed
208
}
Sophie Brun's avatar
Sophie Brun committed
209 210 211 212 213 214 215

func (lan *LAN) Clear() {
	lan.Lock()
	defer lan.Unlock()
	lan.hosts = make(map[string]*Endpoint)
	lan.ttl = make(map[string]uint)
}