index.go 2.58 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2
package multiaddr

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
3
import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
	"bytes"
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
5 6
	"fmt"
	"strings"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8
)

9
// Multiaddr is the data structure representing a multiaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
type Multiaddr struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
11
	Bytes []byte
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
}

14
// NewMultiaddr parses and validates an input string, returning a *Multiaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
func NewMultiaddr(s string) (*Multiaddr, error) {
16
	b, err := stringToBytes(s)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
17 18 19 20
	if err != nil {
		return nil, err
	}
	return &Multiaddr{Bytes: b}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24 25 26 27
// Equal tests whether two multiaddrs are equal
func (m *Multiaddr) Equal(m2 *Multiaddr) bool {
	return bytes.Equal(m.Bytes, m2.Bytes)
}

28
// String returns the string representation of a Multiaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29
func (m *Multiaddr) String() (string, error) {
30
	return bytesToString(m.Bytes)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32
}

33
// Protocols returns the list of protocols this Multiaddr has.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35
func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
36 37 38 39 40 41 42
	// panic handler, in case we try accessing bytes incorrectly.
	defer func() {
		if e := recover(); e != nil {
			ret = nil
			err = e.(error)
		}
	}()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
44 45 46 47 48 49 50 51 52 53 54
	ps := []*Protocol{}
	b := m.Bytes[:]
	for len(b) > 0 {
		p := ProtocolWithCode(int(b[0]))
		if p == nil {
			return nil, fmt.Errorf("no protocol with code %d", b[0])
		}
		ps = append(ps, p)
		b = b[1+(p.Size/8):]
	}
	return ps, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56

57
// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
func (m *Multiaddr) Encapsulate(o *Multiaddr) *Multiaddr {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
59 60 61
	b := make([]byte, len(m.Bytes)+len(o.Bytes))
	b = append(m.Bytes, o.Bytes...)
	return &Multiaddr{Bytes: b}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63
}

64
// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
66 67 68 69
	s1, err := m.String()
	if err != nil {
		return nil, err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
71 72 73 74
	s2, err := o.String()
	if err != nil {
		return nil, err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
76 77 78 79 80
	i := strings.LastIndex(s1, s2)
	if i < 0 {
		return nil, fmt.Errorf("%s not contained in %s", s2, s1)
	}
	return NewMultiaddr(s1[:i])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82

83
// DialArgs is a convenience function returning arguments for use in net.Dial
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84 85
func (m *Multiaddr) DialArgs() (string, string, error) {
	if !m.IsThinWaist() {
86
		return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87 88 89 90 91 92 93 94 95 96 97 98 99
	}

	str, err := m.String()
	if err != nil {
		return "", "", err
	}

	parts := strings.Split(str, "/")[1:]
	network := parts[2]
	host := strings.Join([]string{parts[1], parts[3]}, ":")
	return network, host, nil
}

100 101
// IsThinWaist returns whether this multiaddr includes "Thin Waist" Protocols.
// This means: /{IP4, IP6}/{TCP, UDP}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
func (m *Multiaddr) IsThinWaist() bool {
	p, err := m.Protocols()
	if err != nil {
		return false
	}

	if p[0].Code != P_IP4 && p[0].Code != P_IP6 {
		return false
	}

	if p[1].Code != P_TCP && p[1].Code != P_UDP {
		return false
	}

	return true
}