Unverified Commit ceed2dc9 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #44 from multiformats/fix/parse-ip-addr

fix converting net.IPAddr to a multiaddr
parents cba4f9fe bd10f58a
...@@ -60,13 +60,13 @@ func testToNetAddr(t *testing.T, maddr, ntwk, addr string) { ...@@ -60,13 +60,13 @@ func testToNetAddr(t *testing.T, maddr, ntwk, addr string) {
func TestFromIP4(t *testing.T) { func TestFromIP4(t *testing.T) {
testConvert(t, "/ip4/10.20.30.40", func() (ma.Multiaddr, error) { testConvert(t, "/ip4/10.20.30.40", func() (ma.Multiaddr, error) {
return FromIP(net.ParseIP("10.20.30.40")) return FromNetAddr(&net.IPAddr{IP: net.ParseIP("10.20.30.40")})
}) })
} }
func TestFromIP6(t *testing.T) { func TestFromIP6(t *testing.T) {
testConvert(t, "/ip6/2001:4860:0:2001::68", func() (ma.Multiaddr, error) { testConvert(t, "/ip6/2001:4860:0:2001::68", func() (ma.Multiaddr, error) {
return FromIP(net.ParseIP("2001:4860:0:2001::68")) return FromNetAddr(&net.IPAddr{IP: net.ParseIP("2001:4860:0:2001::68")})
}) })
} }
......
...@@ -14,15 +14,15 @@ type FromNetAddrFunc func(a net.Addr) (ma.Multiaddr, error) ...@@ -14,15 +14,15 @@ type FromNetAddrFunc func(a net.Addr) (ma.Multiaddr, error)
// ToNetAddrFunc is a generic function which converts a Multiaddress to net.Addr // ToNetAddrFunc is a generic function which converts a Multiaddress to net.Addr
type ToNetAddrFunc func(ma ma.Multiaddr) (net.Addr, error) type ToNetAddrFunc func(ma ma.Multiaddr) (net.Addr, error)
var defaultCodecs *CodecMap var defaultCodecs = NewCodecMap()
func init() { func init() {
defaultCodecs = NewCodecMap() defaultCodecs.RegisterFromNetAddr(parseTCPNetAddr, "tcp", "tcp4", "tcp6")
defaultCodecs.RegisterNetCodec(tcpAddrSpec) defaultCodecs.RegisterFromNetAddr(parseUDPNetAddr, "udp", "udp4", "udp6")
defaultCodecs.RegisterNetCodec(udpAddrSpec) defaultCodecs.RegisterFromNetAddr(parseIPNetAddr, "ip", "ip4", "ip6")
defaultCodecs.RegisterNetCodec(ip4AddrSpec) defaultCodecs.RegisterFromNetAddr(parseIPPlusNetAddr, "ip+net")
defaultCodecs.RegisterNetCodec(ip6AddrSpec)
defaultCodecs.RegisterNetCodec(ipnetAddrSpec) defaultCodecs.RegisterToNetAddr(parseBasicNetMaddr, "tcp", "udp", "ip6", "ip4")
} }
// CodecMap holds a map of NetCodecs indexed by their Protocol ID // CodecMap holds a map of NetCodecs indexed by their Protocol ID
...@@ -39,7 +39,6 @@ type CodecMap struct { ...@@ -39,7 +39,6 @@ type CodecMap struct {
// NewCodecMap initializes and returns a CodecMap object. // NewCodecMap initializes and returns a CodecMap object.
func NewCodecMap() *CodecMap { func NewCodecMap() *CodecMap {
return &CodecMap{ return &CodecMap{
codecs: make(map[string]*NetCodec),
addrParsers: make(map[string]FromNetAddrFunc), addrParsers: make(map[string]FromNetAddrFunc),
maddrParsers: make(map[string]ToNetAddrFunc), maddrParsers: make(map[string]ToNetAddrFunc),
} }
...@@ -48,6 +47,13 @@ func NewCodecMap() *CodecMap { ...@@ -48,6 +47,13 @@ func NewCodecMap() *CodecMap {
// NetCodec is used to identify a network codec, that is, a network type for // NetCodec is used to identify a network codec, that is, a network type for
// which we are able to translate multiaddresses into standard Go net.Addr // which we are able to translate multiaddresses into standard Go net.Addr
// and back. // and back.
//
// Deprecated: Unfortunately, these mappings aren't one to one. This abstraction
// assumes that multiple "networks" can map to a single multiaddr protocol but
// not the reverse. For example, this abstraction supports `tcp6, tcp4, tcp ->
// /tcp/` really well but doesn't support `ip -> {/ip4/, /ip6/}`.
//
// Please use `RegisterFromNetAddr` and `RegisterToNetAddr` directly.
type NetCodec struct { type NetCodec struct {
// NetAddrNetworks is an array of strings that may be returned // NetAddrNetworks is an array of strings that may be returned
// by net.Addr.Network() calls on addresses belonging to this type // by net.Addr.Network() calls on addresses belonging to this type
...@@ -76,7 +82,6 @@ func RegisterNetCodec(a *NetCodec) { ...@@ -76,7 +82,6 @@ func RegisterNetCodec(a *NetCodec) {
func (cm *CodecMap) RegisterNetCodec(a *NetCodec) { func (cm *CodecMap) RegisterNetCodec(a *NetCodec) {
cm.lk.Lock() cm.lk.Lock()
defer cm.lk.Unlock() defer cm.lk.Unlock()
cm.codecs[a.ProtocolName] = a
for _, n := range a.NetAddrNetworks { for _, n := range a.NetAddrNetworks {
cm.addrParsers[n] = a.ParseNetAddr cm.addrParsers[n] = a.ParseNetAddr
} }
...@@ -84,41 +89,24 @@ func (cm *CodecMap) RegisterNetCodec(a *NetCodec) { ...@@ -84,41 +89,24 @@ func (cm *CodecMap) RegisterNetCodec(a *NetCodec) {
cm.maddrParsers[a.ProtocolName] = a.ConvertMultiaddr cm.maddrParsers[a.ProtocolName] = a.ConvertMultiaddr
} }
var tcpAddrSpec = &NetCodec{ // RegisterFromNetAddr registers a conversion from net.Addr instances to multiaddrs
ProtocolName: "tcp", func (cm *CodecMap) RegisterFromNetAddr(from FromNetAddrFunc, networks ...string) {
NetAddrNetworks: []string{"tcp", "tcp4", "tcp6"}, cm.lk.Lock()
ParseNetAddr: parseTCPNetAddr, defer cm.lk.Unlock()
ConvertMultiaddr: parseBasicNetMaddr,
}
var udpAddrSpec = &NetCodec{
ProtocolName: "udp",
NetAddrNetworks: []string{"udp", "udp4", "udp6"},
ParseNetAddr: parseUDPNetAddr,
ConvertMultiaddr: parseBasicNetMaddr,
}
var ip4AddrSpec = &NetCodec{ for _, n := range networks {
ProtocolName: "ip4", cm.addrParsers[n] = from
NetAddrNetworks: []string{"ip4"}, }
ParseNetAddr: parseIPNetAddr,
ConvertMultiaddr: parseBasicNetMaddr,
} }
var ip6AddrSpec = &NetCodec{ // RegisterToNetAddr registers a conversion from multiaddrs to net.Addr instances
ProtocolName: "ip6", func (cm *CodecMap) RegisterToNetAddr(to ToNetAddrFunc, protocols ...string) {
NetAddrNetworks: []string{"ip6"}, cm.lk.Lock()
ParseNetAddr: parseIPNetAddr, defer cm.lk.Unlock()
ConvertMultiaddr: parseBasicNetMaddr,
}
var ipnetAddrSpec = &NetCodec{ for _, p := range protocols {
ProtocolName: "ip+net", cm.maddrParsers[p] = to
NetAddrNetworks: []string{"ip+net"}, }
ParseNetAddr: parseIPPlusNetAddr,
ConvertMultiaddr: func(ma.Multiaddr) (net.Addr, error) {
return nil, fmt.Errorf("converting ip+net multiaddr not supported")
},
} }
func (cm *CodecMap) getAddrParser(net string) (FromNetAddrFunc, error) { func (cm *CodecMap) getAddrParser(net string) (FromNetAddrFunc, error) {
......
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