| 1 | package network |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | |
| 9 | "github.com/evilsocket/islazy/data" |
| 10 | ) |
| 11 | |
| 12 | const LANDefaultttl = 10 |
| 13 | |
| 14 | type EndpointNewCallback func(e *Endpoint) |
| 15 | type EndpointLostCallback func(e *Endpoint) |
| 16 | |
| 17 | type LAN struct { |
| 18 | sync.Mutex |
| 19 | hosts map[string]*Endpoint |
| 20 | iface *Endpoint |
| 21 | gateway *Endpoint |
| 22 | ttl map[string]uint |
| 23 | aliases *data.UnsortedKV |
| 24 | newCb EndpointNewCallback |
| 25 | lostCb EndpointLostCallback |
| 26 | } |
| 27 | |
| 28 | type lanJSON struct { |
| 29 | Hosts []*Endpoint `json:"hosts"` |
| 30 | } |
| 31 | |
| 32 | func NewLAN(iface, gateway *Endpoint, aliases *data.UnsortedKV, newcb EndpointNewCallback, lostcb EndpointLostCallback) *LAN { |
| 33 | return &LAN{ |
| 34 | iface: iface, |
| 35 | gateway: gateway, |
| 36 | hosts: make(map[string]*Endpoint), |
| 37 | ttl: make(map[string]uint), |
| 38 | aliases: aliases, |
| 39 | newCb: newcb, |
| 40 | lostCb: lostcb, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func (l *LAN) MarshalJSON() ([]byte, error) { |
| 45 | doc := lanJSON{ |
| 46 | Hosts: make([]*Endpoint, 0), |
| 47 | } |
| 48 | |
| 49 | for _, h := range l.hosts { |
| 50 | doc.Hosts = append(doc.Hosts, h) |
| 51 | } |
| 52 | |
| 53 | return json.Marshal(doc) |
| 54 | } |
| 55 | |
| 56 | func (lan *LAN) Get(mac string) (*Endpoint, bool) { |
| 57 | lan.Lock() |
| 58 | defer lan.Unlock() |
| 59 | |
| 60 | mac = NormalizeMac(mac) |
| 61 | |
| 62 | if mac == lan.iface.HwAddress { |
| 63 | return lan.iface, true |
| 64 | } else if mac == lan.gateway.HwAddress { |
| 65 | return lan.gateway, true |
| 66 | } |
| 67 | |
| 68 | if e, found := lan.hosts[mac]; found { |
| 69 | return e, true |
| 70 | } |
| 71 | return nil, false |
| 72 | } |
| 73 | |
| 74 | func (lan *LAN) GetByIp(ip string) *Endpoint { |
| 75 | lan.Lock() |
| 76 | defer lan.Unlock() |
| 77 | |
| 78 | if ip == lan.iface.IpAddress { |
| 79 | return lan.iface |
| 80 | } else if ip == lan.gateway.IpAddress { |
| 81 | return lan.gateway |
| 82 | } |
| 83 | |
| 84 | for _, e := range lan.hosts { |
| 85 | if e.IpAddress == ip { |
| 86 | return e |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | func (lan *LAN) List() (list []*Endpoint) { |
| 94 | lan.Lock() |
| 95 | defer lan.Unlock() |
| 96 | |
| 97 | list = make([]*Endpoint, 0) |
| 98 | for _, t := range lan.hosts { |
| 99 | list = append(list, t) |
| 100 | } |
| 101 | return |
| 102 | } |
| 103 | |
| 104 | func (lan *LAN) Aliases() *data.UnsortedKV { |
| 105 | return lan.aliases |
| 106 | } |
| 107 | |
| 108 | func (lan *LAN) WasMissed(mac string) bool { |
| 109 | if mac == lan.iface.HwAddress || mac == lan.gateway.HwAddress { |
| 110 | return false |
| 111 | } |
| 112 | |
| 113 | lan.Lock() |
| 114 | defer lan.Unlock() |
| 115 | |
| 116 | if ttl, found := lan.ttl[mac]; found { |
| 117 | return ttl < LANDefaultttl |
| 118 | } |
| 119 | return true |
| 120 | } |
| 121 | |
| 122 | func (lan *LAN) Remove(ip, mac string) { |
| 123 | lan.Lock() |
| 124 | defer lan.Unlock() |
| 125 | |
| 126 | if e, found := lan.hosts[mac]; found { |
| 127 | lan.ttl[mac]-- |
| 128 | if lan.ttl[mac] == 0 { |
| 129 | delete(lan.hosts, mac) |
| 130 | delete(lan.ttl, mac) |
| 131 | lan.lostCb(e) |
| 132 | } |
| 133 | return |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func (lan *LAN) shouldIgnore(ip, mac string) bool { |
| 138 | // skip our own address |
| 139 | if ip == lan.iface.IpAddress || mac == lan.iface.HwAddress { |
| 140 | return true |
| 141 | } |
| 142 | // skip the gateway |
| 143 | if ip == lan.gateway.IpAddress || mac == lan.gateway.HwAddress { |
| 144 | return true |
| 145 | } |
| 146 | // skip broadcast addresses |
| 147 | if strings.HasSuffix(ip, BroadcastSuffix) { |
| 148 | return true |
| 149 | } |
| 150 | // skip broadcast macs |
| 151 | if strings.ToLower(mac) == BroadcastMac { |
| 152 | return true |
| 153 | } |
| 154 | // skip everything which is not in our subnet (multicast noise) |
| 155 | addr := net.ParseIP(ip) |
| 156 | return addr.To4() != nil && !lan.iface.Net.Contains(addr) |
| 157 | } |
| 158 | |
| 159 | func (lan *LAN) Has(ip string) bool { |
| 160 | lan.Lock() |
| 161 | defer lan.Unlock() |
| 162 | |
| 163 | for _, e := range lan.hosts { |
| 164 | if e.IpAddress == ip { |
| 165 | return true |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return false |
| 170 | } |
| 171 | |
| 172 | func (lan *LAN) EachHost(cb func(mac string, e *Endpoint)) { |
| 173 | lan.Lock() |
| 174 | defer lan.Unlock() |
| 175 | |
| 176 | for m, h := range lan.hosts { |
| 177 | cb(m, h) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func (lan *LAN) AddIfNew(ip, mac string) *Endpoint { |
| 182 | lan.Lock() |
| 183 | defer lan.Unlock() |
| 184 | |
| 185 | mac = NormalizeMac(mac) |
| 186 | |
| 187 | if lan.shouldIgnore(ip, mac) { |
| 188 | return nil |
| 189 | } else if t, found := lan.hosts[mac]; found { |
| 190 | if lan.ttl[mac] < LANDefaultttl { |
| 191 | lan.ttl[mac]++ |
| 192 | } |
| 193 | return t |
| 194 | } |
| 195 | |
| 196 | e := NewEndpointWithAlias(ip, mac, lan.aliases.GetOr(mac, "")) |
| 197 | |
| 198 | lan.hosts[mac] = e |
| 199 | lan.ttl[mac] = LANDefaultttl |
| 200 | |
| 201 | lan.newCb(e) |
| 202 | |
| 203 | return nil |
| 204 | } |
| 205 | |
| 206 | func (lan *LAN) GetAlias(mac string) string { |
| 207 | return lan.aliases.GetOr(mac, "") |
| 208 | } |
| 209 | |
| 210 | func (lan *LAN) Clear() { |
| 211 | lan.Lock() |
| 212 | defer lan.Unlock() |
| 213 | lan.hosts = make(map[string]*Endpoint) |
| 214 | lan.ttl = make(map[string]uint) |
| 215 | } |