index.go 2.65 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 30 31 32 33 34
func (m *Multiaddr) String() string {
	s, err := bytesToString(m.Bytes)
	if err != nil {
		panic("multiaddr failed to convert back to string. corrupted?")
	}
	return s
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35 36
}

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

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
40 41 42 43 44 45 46
	// 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
47

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
48 49 50 51 52 53 54 55 56 57 58
	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
59
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60

61
// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62
func (m *Multiaddr) Encapsulate(o *Multiaddr) *Multiaddr {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
63 64 65
	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
66 67
}

68
// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69
func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70 71
	s1 := m.String()
	s2 := o.String()
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
72 73 74 75 76
	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
77
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78

79
// DialArgs is a convenience function returning arguments for use in net.Dial
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81
func (m *Multiaddr) DialArgs() (string, string, error) {
	if !m.IsThinWaist() {
82
		return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85
	str := m.String()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86 87
	parts := strings.Split(str, "/")[1:]
	network := parts[2]
Emery Hemingway's avatar
Emery Hemingway committed
88 89 90 91 92 93 94 95

	var host string
	switch parts[0] {
	case "ip4":
		host = strings.Join([]string{parts[1], parts[3]}, ":")
	case "ip6":
		host = fmt.Sprintf("[%s]:%s", parts[1], parts[3])
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96 97 98
	return network, host, nil
}

99 100
// IsThinWaist returns whether this multiaddr includes "Thin Waist" Protocols.
// This means: /{IP4, IP6}/{TCP, UDP}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
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
}