76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package nic
|
|
|
|
import (
|
|
"net"
|
|
|
|
omm "git.loafle.net/overflow/model/meta"
|
|
omn "git.loafle.net/overflow/model/net"
|
|
oun "git.loafle.net/overflow/util-go/net"
|
|
oung "git.loafle.net/overflow/util-go/net/gateway"
|
|
)
|
|
|
|
func DiscoverGateway() (net.IP, string, error) {
|
|
return oung.DiscoverGateway()
|
|
}
|
|
|
|
func DiscoverInterfaces() ([]*omn.Interface, error) {
|
|
gwIP, gwIface, err := DiscoverGateway()
|
|
if nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
netIfaces, err := net.Interfaces()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ifaces := make([]*omn.Interface, 0)
|
|
|
|
for _, netIface := range netIfaces {
|
|
if netIface.Flags&net.FlagLoopback != 0 || netIface.Flags&net.FlagUp == 0 {
|
|
continue
|
|
}
|
|
|
|
iface := &omn.Interface{}
|
|
ifaces = append(ifaces, iface)
|
|
|
|
iface.Iface = netIface.Name
|
|
iface.Mac = netIface.HardwareAddr.String()
|
|
iface.Addresses = make([]*omn.InterfaceAddress, 0)
|
|
|
|
netAddrs, err := netIface.Addrs()
|
|
if nil != err {
|
|
continue
|
|
}
|
|
|
|
LOOP_ADDR:
|
|
for _, netAddr := range netAddrs {
|
|
_, ipNet, err := oun.ParseCIDR(netAddr.String())
|
|
if nil != err {
|
|
continue
|
|
}
|
|
|
|
addr := &omn.InterfaceAddress{}
|
|
iface.Addresses = append(iface.Addresses, addr)
|
|
addr.Address = netAddr.String()
|
|
addr.Netmask = ipNet.Mask.String()
|
|
addr.Network = ipNet.Network().String()
|
|
|
|
if gwIface == netIface.Name {
|
|
addr.Gateway = gwIP.String()
|
|
}
|
|
|
|
switch ipNet.Version() {
|
|
case 4:
|
|
addr.MetaIPType = omm.ToMetaIPType(omm.MetaIPTypeEnumV4)
|
|
case 6:
|
|
addr.MetaIPType = omm.ToMetaIPType(omm.MetaIPTypeEnumV6)
|
|
default:
|
|
continue LOOP_ADDR
|
|
}
|
|
}
|
|
}
|
|
|
|
return ifaces, nil
|
|
}
|