Commit fb33680d authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

net: added InterfaceMultiaddrs

parent 5d64ba12
package net package manet
import ( import (
"fmt" "fmt"
...@@ -70,6 +70,13 @@ func FromNetAddr(a net.Addr) (ma.Multiaddr, error) { ...@@ -70,6 +70,13 @@ func FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
} }
return FromIP(ac.IP) return FromIP(ac.IP)
case "ip+net":
ac, ok := a.(*net.IPNet)
if !ok {
return nil, errIncorrectNetAddr
}
return FromIP(ac.IP)
default: default:
return nil, fmt.Errorf("unknown network %v", a.Network()) return nil, fmt.Errorf("unknown network %v", a.Network())
} }
......
package net package manet
import ( import (
"net" "net"
......
// Package net provides Multiaddr specific versions of common // Package manet provides Multiaddr specific versions of common
// functions in stdlib's net package. This means wrappers of // functions in stdlib's net package. This means wrappers of
// standard net symbols like net.Dial and net.Listen, as well // standard net symbols like net.Dial and net.Listen, as well
// as conversion to/from net.Addr. // as conversion to/from net.Addr.
package net package manet
package net package manet
import ( import (
"fmt" "fmt"
...@@ -216,3 +216,20 @@ func Listen(laddr ma.Multiaddr) (Listener, error) { ...@@ -216,3 +216,20 @@ func Listen(laddr ma.Multiaddr) (Listener, error) {
laddr: laddr, laddr: laddr,
}, nil }, nil
} }
// InterfaceMultiaddrs will return the addresses matching net.InterfaceAddrs
func InterfaceMultiaddrs() ([]ma.Multiaddr, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
maddrs := make([]ma.Multiaddr, len(addrs))
for i, a := range addrs {
maddrs[i], err = FromNetAddr(a)
if err != nil {
return nil, err
}
}
return maddrs, nil
}
package net package manet
import ( import (
"bytes" "bytes"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment