| 1 | // +build !android |
| 2 | |
| 3 | package network |
| 4 | |
| 5 | import ( |
| 6 | "github.com/bettercap/bettercap/routing" |
| 7 | ) |
| 8 | |
| 9 | func FindGateway(iface *Endpoint) (*Endpoint, error) { |
| 10 | gateway, err := routing.Gateway(routing.IPv4, iface.Name()) |
| 11 | if err != nil { |
| 12 | return nil, err |
| 13 | } |
| 14 | |
| 15 | if gateway == iface.IpAddress { |
| 16 | Debug("gateway is the interface") |
| 17 | return iface, nil |
| 18 | } else { |
| 19 | // we have the address, now we need its mac |
| 20 | mac, err := ArpLookup(iface.Name(), gateway, false) |
| 21 | if err != nil { |
| 22 | return nil, err |
| 23 | } |
| 24 | Debug("gateway is %s[%s]", gateway, mac) |
| 25 | return NewEndpoint(gateway, mac), nil |
| 26 | } |
| 27 | |
| 28 | Debug("FindGateway(%s): nothing found :/", iface.Name()) |
| 29 | return nil, ErrNoGateway |
| 30 | } |