nic_windows

This commit is contained in:
insanity 2018-09-13 12:11:37 +09:00
parent 4f409f59f3
commit 94be2d2744
4 changed files with 109 additions and 1 deletions

View File

@ -33,7 +33,7 @@ func main() {
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, signal.Notify(interrupt,
syscall.SIGKILL, syscall.SIGKILL,
syscall.SIGSTOP, // syscall.SIGSTOP,
syscall.SIGHUP, syscall.SIGHUP,
syscall.SIGINT, syscall.SIGINT,
syscall.SIGTERM, syscall.SIGTERM,

View File

@ -36,6 +36,7 @@ func DiscoverInterfaces() ([]*omn.Interface, error) {
ifaces = append(ifaces, iface) ifaces = append(ifaces, iface)
iface.Iface = netIface.Name iface.Iface = netIface.Name
iface.FriendlyName = netIface.Name
iface.Mac = netIface.HardwareAddr.String() iface.Mac = netIface.HardwareAddr.String()
iface.Addresses = make([]*omn.InterfaceAddress, 0) iface.Addresses = make([]*omn.InterfaceAddress, 0)

View File

@ -27,6 +27,7 @@ func TestDiscoverInterfaces(t *testing.T) {
t.Log("Interface found:") t.Log("Interface found:")
for _, iface := range ifaces { for _, iface := range ifaces {
t.Log("Iface: ", iface.Iface) t.Log("Iface: ", iface.Iface)
t.Log("FriendlyName: ", iface.FriendlyName)
t.Log("Mac: ", iface.Mac) t.Log("Mac: ", iface.Mac)
for _, addr := range iface.Addresses { for _, addr := range iface.Addresses {

106
nic/nic_window.go Normal file
View File

@ -0,0 +1,106 @@
package nic
import (
"log"
"net"
"strings"
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"
"github.com/google/gopacket/pcap"
)
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.FriendlyName = 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
}
var ip net.IP
switch v := netAddr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
log.Print("Addr is not *net.IPNet or *net.IPAddr", netAddr)
}
addr := &omn.InterfaceAddress{}
iface.Addresses = append(iface.Addresses, addr)
addr.Address = ip.String()
addr.Netmask = ipNet.Mask.String()
addr.Network = ipNet.Network().String()
if gwIface == ip.String() {
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
}
iface.Iface = ifaceName(ip.String())
}
}
return ifaces, nil
}
func ifaceName(ip string) string {
devices, err := pcap.FindAllDevs()
if err != nil {
return ""
}
for _, d := range devices {
for _, address := range d.Addresses {
if strings.Compare(address.IP.String(), ip) == 0 {
return d.Name
}
}
}
return ""
}