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
f512d0e0
Unverified
Commit
f512d0e0
authored
Apr 29, 2020
by
Steven Allen
Committed by
GitHub
Apr 29, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #70 from multiformats/feat/zero-alloc-to-ip
feat: zero-alloc ToIP
parents
e68f4d63
8838e2aa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
13 deletions
+16
-13
convert.go
convert.go
+16
-13
No files found.
convert.go
View file @
f512d0e0
...
...
@@ -11,6 +11,7 @@ import (
)
var
errIncorrectNetAddr
=
fmt
.
Errorf
(
"incorrect network addr conversion"
)
var
errNotIP
=
fmt
.
Errorf
(
"multiaddr does not start with an IP address"
)
// FromNetAddr converts a net.Addr type to a Multiaddr.
func
FromNetAddr
(
a
net
.
Addr
)
(
ma
.
Multiaddr
,
error
)
{
...
...
@@ -100,20 +101,22 @@ func FromIP(ip net.IP) (ma.Multiaddr, error) {
// ToIP converts a Multiaddr to a net.IP when possible
func
ToIP
(
addr
ma
.
Multiaddr
)
(
net
.
IP
,
error
)
{
_
,
network
,
ip
,
_
,
hostname
,
err
:=
dialArgComponents
(
addr
)
if
err
!=
nil
{
return
nil
,
err
}
if
hostname
{
return
nil
,
fmt
.
Errorf
(
"non IP Multiaddr: %s %s"
,
network
,
ip
)
}
switch
network
{
case
"ip"
,
"ip4"
,
"ip6"
,
"tcp"
,
"tcp4"
,
"tcp6"
,
"udp"
,
"udp4"
,
"udp6"
:
return
net
.
ParseIP
(
ip
),
nil
default
:
return
nil
,
fmt
.
Errorf
(
"non IP Multiaddr: %s %s"
,
network
,
ip
)
var
ip
net
.
IP
ma
.
ForEach
(
addr
,
func
(
c
ma
.
Component
)
bool
{
switch
c
.
Protocol
()
.
Code
{
case
ma
.
P_IP6ZONE
:
// we can't return these anyways.
return
true
case
ma
.
P_IP6
,
ma
.
P_IP4
:
ip
=
net
.
IP
(
c
.
RawValue
())
return
false
}
return
false
})
if
ip
==
nil
{
return
nil
,
errNotIP
}
return
ip
,
nil
}
// DialArgs is a convenience function that returns network and address as
...
...
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