| 1 | package network |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/evilsocket/islazy/tui" |
| 12 | ) |
| 13 | |
| 14 | type OnHostResolvedCallback func(e *Endpoint) |
| 15 | |
| 16 | type Endpoint struct { |
| 17 | Index int `json:"-"` |
| 18 | IP net.IP `json:"-"` |
| 19 | Net *net.IPNet `json:"-"` |
| 20 | IPv6 net.IP `json:"-"` |
| 21 | HW net.HardwareAddr `json:"-"` |
| 22 | IpAddress string `json:"ipv4"` |
| 23 | Ip6Address string `json:"ipv6"` |
| 24 | SubnetBits uint32 `json:"-"` |
| 25 | IpAddressUint32 uint32 `json:"-"` |
| 26 | HwAddress string `json:"mac"` |
| 27 | Hostname string `json:"hostname"` |
| 28 | Alias string `json:"alias"` |
| 29 | Vendor string `json:"vendor"` |
| 30 | ResolvedCallback OnHostResolvedCallback `json:"-"` |
| 31 | FirstSeen time.Time `json:"first_seen"` |
| 32 | LastSeen time.Time `json:"last_seen"` |
| 33 | Meta *Meta `json:"meta"` |
| 34 | } |
| 35 | |
| 36 | func NewEndpointNoResolve(ip, mac, name string, bits uint32) *Endpoint { |
| 37 | mac = NormalizeMac(mac) |
| 38 | hw, _ := net.ParseMAC(mac) |
| 39 | now := time.Now() |
| 40 | |
| 41 | e := &Endpoint{ |
| 42 | IP: nil, |
| 43 | IpAddress: ip, |
| 44 | IpAddressUint32: 0, |
| 45 | Net: nil, |
| 46 | HW: hw, |
| 47 | SubnetBits: bits, |
| 48 | HwAddress: mac, |
| 49 | Hostname: name, |
| 50 | Vendor: ManufLookup(mac), |
| 51 | ResolvedCallback: nil, |
| 52 | FirstSeen: now, |
| 53 | LastSeen: now, |
| 54 | Meta: NewMeta(), |
| 55 | } |
| 56 | |
| 57 | e.SetIP(ip) |
| 58 | e.SetBits(bits) |
| 59 | |
| 60 | return e |
| 61 | } |
| 62 | |
| 63 | func NewEndpoint(ip, mac string) *Endpoint { |
| 64 | e := NewEndpointNoResolve(ip, mac, "", 0) |
| 65 | // start resolver goroutine |
| 66 | go func() { |
| 67 | if names, err := net.LookupAddr(e.IpAddress); err == nil && len(names) > 0 { |
| 68 | e.Hostname = names[0] |
| 69 | if e.ResolvedCallback != nil { |
| 70 | e.ResolvedCallback(e) |
| 71 | } |
| 72 | } |
| 73 | }() |
| 74 | |
| 75 | return e |
| 76 | } |
| 77 | |
| 78 | func NewEndpointWithAlias(ip, mac, alias string) *Endpoint { |
| 79 | e := NewEndpoint(ip, mac) |
| 80 | e.Alias = alias |
| 81 | return e |
| 82 | } |
| 83 | |
| 84 | func ip2int(ip net.IP) uint32 { |
| 85 | if len(ip) == 16 { |
| 86 | return binary.BigEndian.Uint32(ip[12:16]) |
| 87 | } |
| 88 | return binary.BigEndian.Uint32(ip) |
| 89 | } |
| 90 | |
| 91 | func (t *Endpoint) SetNetwork(netw string) { |
| 92 | parts := strings.Split(netw, "/") |
| 93 | address := parts[0] |
| 94 | bits, _ := strconv.Atoi(parts[1]) |
| 95 | |
| 96 | t.SetIP(address) |
| 97 | t.SetBits(uint32(bits)) |
| 98 | } |
| 99 | |
| 100 | func (t *Endpoint) SetIPv6(netw string) { |
| 101 | parts := strings.SplitN(netw, "/", 2) |
| 102 | address := parts[0] |
| 103 | // bits, _ := strconv.Atoi(parts[1]) |
| 104 | |
| 105 | t.IPv6 = net.ParseIP(address) |
| 106 | if t.IPv6 != nil { |
| 107 | t.Ip6Address = t.IPv6.String() |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func (t *Endpoint) SetIP(ip string) { |
| 112 | addr := net.ParseIP(ip) |
| 113 | t.IP = addr |
| 114 | t.IpAddress = ip |
| 115 | t.IpAddressUint32 = ip2int(addr) |
| 116 | } |
| 117 | |
| 118 | func (t *Endpoint) SetBits(bits uint32) { |
| 119 | t.SubnetBits = bits |
| 120 | _, netw, _ := net.ParseCIDR(t.CIDR()) |
| 121 | t.Net = netw |
| 122 | } |
| 123 | |
| 124 | func (t *Endpoint) Name() string { |
| 125 | return t.Hostname |
| 126 | } |
| 127 | |
| 128 | func (t *Endpoint) CIDR() string { |
| 129 | shift := 32 - t.SubnetBits |
| 130 | address := t.IpAddressUint32 |
| 131 | ip := make(net.IP, 4) |
| 132 | |
| 133 | binary.BigEndian.PutUint32(ip, (address>>shift)<<shift) |
| 134 | |
| 135 | return fmt.Sprintf("%s/%d", ip.String(), t.SubnetBits) |
| 136 | } |
| 137 | |
| 138 | func (t *Endpoint) IsMonitor() bool { |
| 139 | return t.IpAddress == MonitorModeAddress |
| 140 | } |
| 141 | |
| 142 | func (t *Endpoint) String() string { |
| 143 | ipPart := fmt.Sprintf("%s : ", t.IpAddress) |
| 144 | if t.IpAddress == MonitorModeAddress { |
| 145 | ipPart = "" |
| 146 | } |
| 147 | |
| 148 | if t.HwAddress == "" { |
| 149 | return t.IpAddress |
| 150 | } else if t.Vendor == "" { |
| 151 | return fmt.Sprintf("%s%s", ipPart, t.HwAddress) |
| 152 | } else if t.Hostname == "" && t.Alias == "" { |
| 153 | return fmt.Sprintf("%s%s (%s)", ipPart, t.HwAddress, t.Vendor) |
| 154 | } |
| 155 | |
| 156 | name := t.Hostname |
| 157 | if t.Alias != "" { |
| 158 | name = t.Alias |
| 159 | } |
| 160 | |
| 161 | return fmt.Sprintf("%s%s (%s) - %s", ipPart, t.HwAddress, t.Vendor, tui.Bold(name)) |
| 162 | } |
| 163 | |
| 164 | func (t *Endpoint) OnMeta(meta map[string]string) { |
| 165 | host := "" |
| 166 | for k, v := range meta { |
| 167 | // simple heuristics to get the longest candidate name |
| 168 | if strings.HasSuffix(k, ":hostname") && len(v) > len(host) { |
| 169 | host = v |
| 170 | } else if k == "mdns:md" && len(v) > len(host) { |
| 171 | host = v |
| 172 | } |
| 173 | t.Meta.Set(k, v) |
| 174 | } |
| 175 | |
| 176 | if t.Hostname == "" { |
| 177 | t.Hostname = host |
| 178 | } |
| 179 | } |