Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mf
go-multiaddr
Commits
ceed2dc9
Unverified
Commit
ceed2dc9
authored
Sep 15, 2018
by
Steven Allen
Committed by
GitHub
Sep 15, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #44 from multiformats/fix/parse-ip-addr
fix converting net.IPAddr to a multiaddr
parents
cba4f9fe
bd10f58a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
42 deletions
+30
-42
convert_test.go
convert_test.go
+2
-2
registry.go
registry.go
+28
-40
No files found.
convert_test.go
View file @
ceed2dc9
...
@@ -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
From
IP
(
net
.
ParseIP
(
"10.20.30.40"
))
return
From
NetAddr
(
&
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
From
IP
(
net
.
ParseIP
(
"2001:4860:0:2001::68"
))
return
From
NetAddr
(
&
net
.
IPAddr
{
IP
:
net
.
ParseIP
(
"2001:4860:0:2001::68"
)
}
)
})
})
}
}
...
...
registry.go
View file @
ceed2dc9
...
@@ -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
=
New
CodecMap
()
func
init
()
{
func
init
()
{
defaultCodecs
=
NewCodecMap
(
)
defaultCodecs
.
RegisterFromNetAddr
(
parseTCPNetAddr
,
"tcp"
,
"tcp4"
,
"tcp6"
)
defaultCodecs
.
Register
NetCodec
(
tcpAddrSpec
)
defaultCodecs
.
Register
FromNetAddr
(
parseUDPNetAddr
,
"udp"
,
"udp4"
,
"udp6"
)
defaultCodecs
.
Register
NetCodec
(
udpAddrSpec
)
defaultCodecs
.
Register
FromNetAddr
(
parseIPNetAddr
,
"ip"
,
"ip4"
,
"ip6"
)
defaultCodecs
.
Register
NetCodec
(
ip4AddrSpec
)
defaultCodecs
.
Register
FromNetAddr
(
parseIPPlusNetAddr
,
"ip+net"
)
defaultCodecs
.
RegisterNetCodec
(
ip6AddrSpec
)
defaultCodecs
.
RegisterNet
Codec
(
ipnetAddrSpec
)
defaultCodecs
.
Register
To
Net
Addr
(
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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment