mirror of
https://github.com/waku-org/go-multiaddr.git
synced 2025-02-24 03:58:17 +00:00
Merge pull request #26 from multiformats/docs-improvements
README/docs: Improved README, make golint happy
This commit is contained in:
commit
2b541b7fc2
27
README.md
27
README.md
@ -4,10 +4,12 @@
|
|||||||
[](https://github.com/multiformats/multiformats)
|
[](https://github.com/multiformats/multiformats)
|
||||||
[](https://webchat.freenode.net/?channels=%23ipfs)
|
[](https://webchat.freenode.net/?channels=%23ipfs)
|
||||||
[](https://github.com/RichardLitt/standard-readme)
|
[](https://github.com/RichardLitt/standard-readme)
|
||||||
|
[](https://godoc.org/github.com/multiformats/go-multiaddr-net)
|
||||||
[](https://travis-ci.org/multiformats/go-multiaddr-net)
|
[](https://travis-ci.org/multiformats/go-multiaddr-net)
|
||||||
[](https://codecov.io/github/multiformats/go-multiaddr-net?branch=master)
|
|
||||||
|
|
||||||
> multiaddr net tools
|
<!---[](https://codecov.io/github/multiformats/go-multiaddr-net?branch=master)--->
|
||||||
|
|
||||||
|
> Multiaddress net tools
|
||||||
|
|
||||||
This package provides [Multiaddr](https://github.com/multiformats/go-multiaddr) specific versions of common functions in [stdlib](https://github.com/golang/go/tree/master/src)'s `net` package. This means wrappers of standard net symbols like `net.Dial` and `net.Listen`, as well
|
This package provides [Multiaddr](https://github.com/multiformats/go-multiaddr) specific versions of common functions in [stdlib](https://github.com/golang/go/tree/master/src)'s `net` package. This means wrappers of standard net symbols like `net.Dial` and `net.Listen`, as well
|
||||||
as conversion to and from `net.Addr`.
|
as conversion to and from `net.Addr`.
|
||||||
@ -22,13 +24,32 @@ as conversion to and from `net.Addr`.
|
|||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
|
`go-multiaddr-net` is a standard Go module which can be installed with:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go get github.com/multiformats/go-multiaddr-net
|
go get github.com/multiformats/go-multiaddr-net
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Note that `go-multiaddr-net` is packaged with Gx, so it is recommended to use Gx to install and use it (see Usage section).
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
See the docs:
|
This module is packaged with [Gx](https://github.com/whyrusleeping/gx). In order to use it in your own project do:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go get -u github.com/whyrusleeping/gx
|
||||||
|
go get -u github.com/whyrusleeping/gx-go
|
||||||
|
cd <your-project-repository>
|
||||||
|
gx init
|
||||||
|
gx import github.com/multiformats/go-multiaddr-net
|
||||||
|
gx install --global
|
||||||
|
gx-go --rewrite
|
||||||
|
```
|
||||||
|
|
||||||
|
Please check [Gx](https://github.com/whyrusleeping/gx) and [Gx-go](https://github.com/whyrusleeping/gx-go) documentation for more information.
|
||||||
|
|
||||||
|
For further usage, see the docs:
|
||||||
|
|
||||||
- `multiaddr/net`: https://godoc.org/github.com/multiformats/go-multiaddr-net
|
- `multiaddr/net`: https://godoc.org/github.com/multiformats/go-multiaddr-net
|
||||||
- `multiaddr`: https://godoc.org/github.com/multiformats/go-multiaddr
|
- `multiaddr`: https://godoc.org/github.com/multiformats/go-multiaddr
|
||||||
|
10
convert.go
10
convert.go
@ -15,6 +15,7 @@ func FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|||||||
return defaultCodecs.FromNetAddr(a)
|
return defaultCodecs.FromNetAddr(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FromNetAddr converts a net.Addr to Multiaddress.
|
||||||
func (cm *CodecMap) FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
func (cm *CodecMap) FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
||||||
if a == nil {
|
if a == nil {
|
||||||
return nil, fmt.Errorf("nil multiaddr")
|
return nil, fmt.Errorf("nil multiaddr")
|
||||||
@ -34,6 +35,7 @@ func ToNetAddr(maddr ma.Multiaddr) (net.Addr, error) {
|
|||||||
return defaultCodecs.ToNetAddr(maddr)
|
return defaultCodecs.ToNetAddr(maddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToNetAddr converts a Multiaddress to a standard net.Addr.
|
||||||
func (cm *CodecMap) ToNetAddr(maddr ma.Multiaddr) (net.Addr, error) {
|
func (cm *CodecMap) ToNetAddr(maddr ma.Multiaddr) (net.Addr, error) {
|
||||||
protos := maddr.Protocols()
|
protos := maddr.Protocols()
|
||||||
final := protos[len(protos)-1]
|
final := protos[len(protos)-1]
|
||||||
@ -105,7 +107,7 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
|
|||||||
return network, host, nil
|
return network, host, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTcpNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
func parseTCPNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
||||||
ac, ok := a.(*net.TCPAddr)
|
ac, ok := a.(*net.TCPAddr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errIncorrectNetAddr
|
return nil, errIncorrectNetAddr
|
||||||
@ -127,7 +129,7 @@ func parseTcpNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|||||||
return ipm.Encapsulate(tcpm), nil
|
return ipm.Encapsulate(tcpm), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseUdpNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
func parseUDPNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
||||||
ac, ok := a.(*net.UDPAddr)
|
ac, ok := a.(*net.UDPAddr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errIncorrectNetAddr
|
return nil, errIncorrectNetAddr
|
||||||
@ -149,7 +151,7 @@ func parseUdpNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|||||||
return ipm.Encapsulate(udpm), nil
|
return ipm.Encapsulate(udpm), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseIpNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
func parseIPNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
||||||
ac, ok := a.(*net.IPAddr)
|
ac, ok := a.(*net.IPAddr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errIncorrectNetAddr
|
return nil, errIncorrectNetAddr
|
||||||
@ -157,7 +159,7 @@ func parseIpNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|||||||
return FromIP(ac.IP)
|
return FromIP(ac.IP)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseIpPlusNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
func parseIPPlusNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
||||||
ac, ok := a.(*net.IPNet)
|
ac, ok := a.(*net.IPNet)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errIncorrectNetAddr
|
return nil, errIncorrectNetAddr
|
||||||
|
3
ip.go
3
ip.go
@ -70,7 +70,8 @@ func IsIPLoopback(m ma.Multiaddr) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// IP6 Link Local addresses are non routable. The prefix is technically
|
// IsIP6LinkLocal returns if a multiaddress is an IPv6 local link. These
|
||||||
|
// addresses are non routable. The prefix is technically
|
||||||
// fe80::/10, but we test fe80::/16 for simplicity (no need to mask).
|
// fe80::/10, but we test fe80::/16 for simplicity (no need to mask).
|
||||||
// So far, no hardware interfaces exist long enough to use those 2 bits.
|
// So far, no hardware interfaces exist long enough to use those 2 bits.
|
||||||
// Send a PR if there is.
|
// Send a PR if there is.
|
||||||
|
6
net.go
6
net.go
@ -1,3 +1,8 @@
|
|||||||
|
// Package manet provides Multiaddr
|
||||||
|
// (https://github.com/multiformats/go-multiaddr) specific versions of common
|
||||||
|
// functions in Go's standard `net` package. This means wrappers of standard
|
||||||
|
// net symbols like `net.Dial` and `net.Listen`, as well as conversion to
|
||||||
|
// and from `net.Addr`.
|
||||||
package manet
|
package manet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -90,6 +95,7 @@ func (d *Dialer) Dial(remote ma.Multiaddr) (Conn, error) {
|
|||||||
return d.DialContext(context.Background(), remote)
|
return d.DialContext(context.Background(), remote)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DialContext allows to provide a custom context to Dial().
|
||||||
func (d *Dialer) DialContext(ctx context.Context, remote ma.Multiaddr) (Conn, error) {
|
func (d *Dialer) DialContext(ctx context.Context, remote ma.Multiaddr) (Conn, error) {
|
||||||
// if a LocalAddr is specified, use it on the embedded dialer.
|
// if a LocalAddr is specified, use it on the embedded dialer.
|
||||||
if d.LocalAddr != nil {
|
if d.LocalAddr != nil {
|
||||||
|
24
registry.go
24
registry.go
@ -8,7 +8,10 @@ import (
|
|||||||
ma "github.com/multiformats/go-multiaddr"
|
ma "github.com/multiformats/go-multiaddr"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FromNetAddrFunc is a generic function which converts a net.Addr to Multiaddress
|
||||||
type FromNetAddrFunc func(a net.Addr) (ma.Multiaddr, error)
|
type FromNetAddrFunc func(a net.Addr) (ma.Multiaddr, error)
|
||||||
|
|
||||||
|
// 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 *CodecMap
|
||||||
@ -22,6 +25,10 @@ func init() {
|
|||||||
defaultCodecs.RegisterNetCodec(ipnetAddrSpec)
|
defaultCodecs.RegisterNetCodec(ipnetAddrSpec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CodecMap holds a map of NetCodecs indexed by their Protocol ID
|
||||||
|
// along with parsers for the addresses they use.
|
||||||
|
// It is used to keep a list of supported network address codecs (protocols
|
||||||
|
// which addresses can be converted to and from multiaddresses).
|
||||||
type CodecMap struct {
|
type CodecMap struct {
|
||||||
codecs map[string]*NetCodec
|
codecs map[string]*NetCodec
|
||||||
addrParsers map[string]FromNetAddrFunc
|
addrParsers map[string]FromNetAddrFunc
|
||||||
@ -29,6 +36,7 @@ type CodecMap struct {
|
|||||||
lk sync.Mutex
|
lk sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewCodecMap initializes and returns a CodecMap object.
|
||||||
func NewCodecMap() *CodecMap {
|
func NewCodecMap() *CodecMap {
|
||||||
return &CodecMap{
|
return &CodecMap{
|
||||||
codecs: make(map[string]*NetCodec),
|
codecs: make(map[string]*NetCodec),
|
||||||
@ -37,6 +45,9 @@ func NewCodecMap() *CodecMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// and back.
|
||||||
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
|
||||||
@ -55,10 +66,13 @@ type NetCodec struct {
|
|||||||
Protocol ma.Protocol
|
Protocol ma.Protocol
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterNetCodec adds a new NetCodec to the default codecs.
|
||||||
func RegisterNetCodec(a *NetCodec) {
|
func RegisterNetCodec(a *NetCodec) {
|
||||||
defaultCodecs.RegisterNetCodec(a)
|
defaultCodecs.RegisterNetCodec(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterNetCodec adds a new NetCodec to the CodecMap. This function is
|
||||||
|
// thread safe.
|
||||||
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()
|
||||||
@ -73,35 +87,35 @@ func (cm *CodecMap) RegisterNetCodec(a *NetCodec) {
|
|||||||
var tcpAddrSpec = &NetCodec{
|
var tcpAddrSpec = &NetCodec{
|
||||||
ProtocolName: "tcp",
|
ProtocolName: "tcp",
|
||||||
NetAddrNetworks: []string{"tcp", "tcp4", "tcp6"},
|
NetAddrNetworks: []string{"tcp", "tcp4", "tcp6"},
|
||||||
ParseNetAddr: parseTcpNetAddr,
|
ParseNetAddr: parseTCPNetAddr,
|
||||||
ConvertMultiaddr: parseBasicNetMaddr,
|
ConvertMultiaddr: parseBasicNetMaddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
var udpAddrSpec = &NetCodec{
|
var udpAddrSpec = &NetCodec{
|
||||||
ProtocolName: "udp",
|
ProtocolName: "udp",
|
||||||
NetAddrNetworks: []string{"udp", "udp4", "udp6"},
|
NetAddrNetworks: []string{"udp", "udp4", "udp6"},
|
||||||
ParseNetAddr: parseUdpNetAddr,
|
ParseNetAddr: parseUDPNetAddr,
|
||||||
ConvertMultiaddr: parseBasicNetMaddr,
|
ConvertMultiaddr: parseBasicNetMaddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
var ip4AddrSpec = &NetCodec{
|
var ip4AddrSpec = &NetCodec{
|
||||||
ProtocolName: "ip4",
|
ProtocolName: "ip4",
|
||||||
NetAddrNetworks: []string{"ip4"},
|
NetAddrNetworks: []string{"ip4"},
|
||||||
ParseNetAddr: parseIpNetAddr,
|
ParseNetAddr: parseIPNetAddr,
|
||||||
ConvertMultiaddr: parseBasicNetMaddr,
|
ConvertMultiaddr: parseBasicNetMaddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
var ip6AddrSpec = &NetCodec{
|
var ip6AddrSpec = &NetCodec{
|
||||||
ProtocolName: "ip6",
|
ProtocolName: "ip6",
|
||||||
NetAddrNetworks: []string{"ip6"},
|
NetAddrNetworks: []string{"ip6"},
|
||||||
ParseNetAddr: parseIpNetAddr,
|
ParseNetAddr: parseIPNetAddr,
|
||||||
ConvertMultiaddr: parseBasicNetMaddr,
|
ConvertMultiaddr: parseBasicNetMaddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
var ipnetAddrSpec = &NetCodec{
|
var ipnetAddrSpec = &NetCodec{
|
||||||
ProtocolName: "ip+net",
|
ProtocolName: "ip+net",
|
||||||
NetAddrNetworks: []string{"ip+net"},
|
NetAddrNetworks: []string{"ip+net"},
|
||||||
ParseNetAddr: parseIpPlusNetAddr,
|
ParseNetAddr: parseIPPlusNetAddr,
|
||||||
ConvertMultiaddr: func(ma.Multiaddr) (net.Addr, error) {
|
ConvertMultiaddr: func(ma.Multiaddr) (net.Addr, error) {
|
||||||
return nil, fmt.Errorf("converting ip+net multiaddr not supported")
|
return nil, fmt.Errorf("converting ip+net multiaddr not supported")
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user