本文整理汇总了Golang中net.Interface.Addrs方法的典型用法代码示例。如果您正苦于以下问题:Golang Interface.Addrs方法的具体用法?Golang Interface.Addrs怎么用?Golang Interface.Addrs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.Interface
的用法示例。
在下文中一共展示了Interface.Addrs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Addresses
func Addresses(nif net.Interface, v4 bool, v6 bool, retries int) ([]net.Addr, []net.Addr, error) {
var v4Addrs, v6Addrs []net.Addr
for n := retries; n >= 0; n-- {
addrs, err := nif.Addrs()
if err != nil {
return nil, nil, err
}
v4Addrs, v6Addrs = partition(addrs)
if v4 && v6 && len(v4Addrs) > 0 && len(v6Addrs) > 0 {
break
} else if v4 && len(v4Addrs) > 0 {
v6Addrs = []net.Addr{}
break
} else if v6 && len(v6Addrs) > 0 {
v4Addrs = []net.Addr{}
break
}
if n > 0 {
time.Sleep(1 * time.Second)
}
}
return v4Addrs, v6Addrs, nil
}
示例2: addrsOfOneInterface
// 一个接口上的所有ip4地址
func addrsOfOneInterface(iface net.Interface) (addrs []*net.IPAddr) {
ifaddrs, err := iface.Addrs()
if (err != nil) || (len(ifaddrs) == 0) {
return
}
for _, ifaddr := range ifaddrs {
var ip net.IP
switch v := ifaddr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
continue
}
if ip.IsLoopback() {
return
}
ip = ip.To4()
if ip != nil {
addr, _ := net.ResolveIPAddr("ip", ip.String())
addrs = append(addrs, addr)
}
}
return
}
示例3: AddressesOnInterface
func AddressesOnInterface(iface *net.Interface) (*[]net.Addr, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
return &addrs, nil
}
示例4: setSyscallIPMreq
func setSyscallIPMreq(mreq *syscall.IPMreq, ifi *net.Interface) error {
if ifi == nil {
return nil
}
ifat, err := ifi.Addrs()
if err != nil {
return err
}
for _, ifa := range ifat {
switch v := ifa.(type) {
case *net.IPAddr:
if a := v.IP.To4(); a != nil {
copy(mreq.Interface[:], a)
goto done
}
case *net.IPNet:
if a := v.IP.To4(); a != nil {
copy(mreq.Interface[:], a)
goto done
}
}
}
done:
if bytes.Equal(mreq.Multiaddr[:], net.IPv4zero.To4()) {
return errNoSuchMulticastInterface
}
return nil
}
示例5: updateHosts
func updateHosts(iface *net.Interface) {
addrs, err := iface.Addrs()
checkErr(err)
if len(addrs) == 0 {
return
}
hostname, err := os.Hostname()
checkErr(err)
hosts := parseHosts()
// Remove existing ips pointing to our hostname
toRemove := []string{}
for ip, addrs := range hosts {
for _, addr := range addrs {
if addr == hostname {
toRemove = append(toRemove, ip)
break
}
}
}
for _, ip := range toRemove {
delete(hosts, ip)
}
// Add the weave ip(s)
for _, addr := range addrs {
if addr, ok := addr.(*net.IPNet); ok {
ip := addr.IP.String()
hosts[ip] = append(hosts[ip], hostname)
}
}
writeHosts(hosts)
}
示例6: isGoodForMulticast
func isGoodForMulticast(ifi *net.Interface) (net.IP, bool) {
if ifi.Flags&net.FlagUp == 0 {
return nil, false
}
// We need a unicast IPv4 address that can be used to specify
// the IPv4 multicast interface.
ifat, err := ifi.Addrs()
if err != nil {
return nil, false
}
if len(ifat) == 0 {
return nil, false
}
var ip net.IP
for _, ifa := range ifat {
switch v := ifa.(type) {
case *net.IPAddr:
ip = v.IP
case *net.IPNet:
ip = v.IP
default:
continue
}
if ip.To4() == nil {
ip = nil
continue
}
break
}
if ip == nil {
return nil, false
}
return ip, true
}
示例7: GetIfaceIP4Addr
func GetIfaceIP4Addr(iface *net.Interface) (net.IP, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
// prefer non link-local addr
var ll net.IP
for _, addr := range addrs {
// Attempt to parse the address in CIDR notation
// and assert it is IPv4
ip, _, err := net.ParseCIDR(addr.String())
if err != nil || ip.To4() == nil {
continue
}
if ip.IsGlobalUnicast() {
return ip, nil
}
if ip.IsLinkLocalUnicast() {
ll = ip
}
}
if ll != nil {
// didn't find global but found link-local. it'll do.
return ll, nil
}
return nil, errors.New("No IPv4 address found for given interface")
}
示例8: getHostIPs
func getHostIPs(interfaceName string) ([]net.Addr, error) {
hostInterfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var iface net.Interface
for _, hostInterface := range hostInterfaces {
if hostInterface.Name == interfaceName {
iface = hostInterface
break
}
}
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
if len(addrs) == 0 {
return nil, errors.New("no ip addresses assigned to interface")
}
return addrs, nil
}
示例9: ifaceToIp
func ifaceToIp(iface *net.Interface) (string, error) {
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
return "", errors.New("Node not connected to the network.")
}
示例10: isMulticastAvailable
// isMulticastAvailable returns true if ifi is a multicast access
// enabled network interface. It also returns a unicast IPv4 address
// that can be used for listening on ifi.
func isMulticastAvailable(ifi *net.Interface) (net.IP, bool) {
if ifi.Flags&net.FlagUp == 0 || ifi.Flags&net.FlagMulticast == 0 {
return nil, false
}
ifat, err := ifi.Addrs()
if err != nil {
return nil, false
}
if len(ifat) == 0 {
return nil, false
}
var ip net.IP
for _, ifa := range ifat {
switch v := ifa.(type) {
case *net.IPAddr:
ip = v.IP
case *net.IPNet:
ip = v.IP
default:
continue
}
if ip.To4() == nil {
ip = nil
continue
}
break
}
return ip, true
}
示例11: ssdpUnicastDiscoverImpl
func (this *ssdpDefaultManager) ssdpUnicastDiscoverImpl(ifi *net.Interface, port string) (err error) {
addrs, err := ifi.Addrs()
if nil != err {
return
} else if 0 == len(addrs) {
err = errors.New(fmt.Sprintf("No addresses found for interface %s", ifi.Name))
return
}
var lip net.IP
for _, addr := range addrs {
if nil != addr.(*net.IPNet).IP.DefaultMask() {
lip = addr.(*net.IPNet).IP
break
}
}
laddr, err := net.ResolveUDPAddr(ssdpBroadcastVersion, net.JoinHostPort(lip.String(), port))
if nil != err {
return
}
uc, err := net.ListenUDP(ssdpBroadcastVersion, laddr)
if nil != err {
return
}
this.unicast.addr = laddr
this.unicast.conn = uc
go this.ssdpDiscoverLoop(uc)
<-this.readyChan
return
}
示例12: getAddresses
func getAddresses(iface net.Interface) (addresses map[string]interface{}, err error) {
addresses = make(map[string]interface{})
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ip net.IP
addressInfo := make(map[string]string)
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
netmask := ip.DefaultMask()
if netmask != nil {
addressInfo["family"] = "inet"
} else {
addressInfo["family"] = "inet6"
}
addresses[ip.String()] = addressInfo
}
mac := iface.HardwareAddr
if mac != nil {
addressInfo := make(map[string]string)
addressInfo["family"] = "lladdr"
addresses[mac.String()] = addressInfo
}
return
}
示例13: Addrs
func (_ networkInterface) Addrs(intf *net.Interface) ([]net.Addr, error) {
addrs, err := intf.Addrs()
if err != nil {
return nil, err
}
return addrs, nil
}
示例14: interfaceIP
func interfaceIP(iface *net.Interface) (net.IP, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
fs := [](func(net.IP) bool){
net.IP.IsGlobalUnicast,
net.IP.IsLinkLocalUnicast,
net.IP.IsLoopback,
}
for _, f := range fs {
for _, a := range addrs {
ipaddr, ok := a.(*net.IPNet)
if !ok {
continue
}
ip := ipaddr.IP.To4()
if ip == nil {
continue
}
if f(ip) {
return ip, nil
}
}
}
return nil, fmt.Errorf("interface %s has no usable unicast addresses", iface.Name)
}
示例15: getInterface
func getInterface(in net.Interface) ([]net.Addr, string) {
addrs, err := in.Addrs()
if err != nil {
return nil, ""
}
if len(addrs) == 0 {
return nil, ""
}
return addrs, in.Name
}