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
4489dfa8
Commit
4489dfa8
authored
Nov 05, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net: added InterfaceMultiaddrs
parent
bb34b84e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
50 additions
and
16 deletions
+50
-16
multiaddr/multiaddr.go
multiaddr/multiaddr.go
+20
-10
net/convert.go
net/convert.go
+8
-1
net/convert_test.go
net/convert_test.go
+1
-1
net/doc.go
net/doc.go
+2
-2
net/net.go
net/net.go
+18
-1
net/net_test.go
net/net_test.go
+1
-1
No files found.
multiaddr/multiaddr.go
View file @
4489dfa8
...
...
@@ -7,17 +7,15 @@ import (
"os"
ma
"github.com/jbenet/go-multiaddr"
manet
"github.com/jbenet/go-multiaddr/net"
)
var
usage
=
`multiaddr conversion
usage: multiaddr [fmt] <>`
var
formats
=
[]
string
{
"string"
,
"bytes"
,
"hex"
,
"raw"
}
var
formats
=
[]
string
{
"string"
,
"bytes"
,
"hex"
,
"slice"
}
var
format
string
func
init
()
{
flag
.
Usage
=
func
()
{
fmt
.
Fprintf
(
os
.
Stderr
,
"usage: %s <multiaddr>
\n\n
Flags:
\n
"
,
os
.
Args
[
0
])
fmt
.
Fprintf
(
os
.
Stderr
,
"usage: %s
[
<multiaddr>
]
\n\n
Flags:
\n
"
,
os
.
Args
[
0
])
flag
.
PrintDefaults
()
}
...
...
@@ -29,8 +27,14 @@ func init() {
func
main
()
{
flag
.
Parse
()
args
:=
flag
.
Args
()
if
len
(
args
)
!=
1
{
die
(
"error: can only take one argument"
)
if
len
(
args
)
==
0
{
maddrs
,
err
:=
manet
.
InterfaceMultiaddrs
()
if
err
!=
nil
{
die
(
err
)
}
output
(
maddrs
...
)
return
}
m
,
err
:=
ma
.
NewMultiaddr
(
args
[
0
])
...
...
@@ -38,16 +42,22 @@ func main() {
die
(
err
)
}
fmt
.
Println
(
outfmt
(
m
))
output
(
m
)
}
func
output
(
ms
...
ma
.
Multiaddr
)
{
for
_
,
m
:=
range
ms
{
fmt
.
Println
(
outfmt
(
m
))
}
}
func
outfmt
(
m
ma
.
Multiaddr
)
string
{
switch
format
{
case
"string"
:
return
m
.
String
()
case
"
bytes
"
:
case
"
slice
"
:
return
fmt
.
Sprintf
(
"%v"
,
m
.
Bytes
())
case
"
raw
"
:
case
"
bytes
"
:
return
string
(
m
.
Bytes
())
case
"hex"
:
return
"0x"
+
hex
.
EncodeToString
(
m
.
Bytes
())
...
...
net/convert.go
View file @
4489dfa8
package
net
package
ma
net
import
(
"fmt"
...
...
@@ -70,6 +70,13 @@ func FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
}
return
FromIP
(
ac
.
IP
)
case
"ip+net"
:
ac
,
ok
:=
a
.
(
*
net
.
IPNet
)
if
!
ok
{
return
nil
,
errIncorrectNetAddr
}
return
FromIP
(
ac
.
IP
)
default
:
return
nil
,
fmt
.
Errorf
(
"unknown network %v"
,
a
.
Network
())
}
...
...
net/convert_test.go
View file @
4489dfa8
package
net
package
ma
net
import
(
"net"
...
...
net/doc.go
View file @
4489dfa8
// Package net provides Multiaddr specific versions of common
// Package
ma
net provides Multiaddr specific versions of common
// functions in stdlib's net package. This means wrappers of
// standard net symbols like net.Dial and net.Listen, as well
// as conversion to/from net.Addr.
package
net
package
ma
net
net/net.go
View file @
4489dfa8
package
net
package
ma
net
import
(
"fmt"
...
...
@@ -216,3 +216,20 @@ func Listen(laddr ma.Multiaddr) (Listener, error) {
laddr
:
laddr
,
},
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
}
net/net_test.go
View file @
4489dfa8
package
net
package
ma
net
import
(
"bytes"
...
...
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