multiaddr.go 3.27 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5
package multiaddr

import (
	"bytes"
	"fmt"
Jeromy's avatar
Jeromy committed
6
	"log"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8 9 10 11 12 13 14 15
	"strings"
)

// multiaddr is the data structure representing a Multiaddr
type multiaddr struct {
	bytes []byte
}

// NewMultiaddr parses and validates an input string, returning a *Multiaddr
Jeromy's avatar
Jeromy committed
16 17 18 19 20 21 22
func NewMultiaddr(s string) (a Multiaddr, err error) {
	defer func() {
		if e := recover(); e != nil {
			log.Printf("Panic in NewMultiaddr on input %q: %s", s, e)
			err = fmt.Errorf("%v", e)
		}
	}()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24 25 26 27 28 29 30 31
	b, err := stringToBytes(s)
	if err != nil {
		return nil, err
	}
	return &multiaddr{bytes: b}, nil
}

// NewMultiaddrBytes initializes a Multiaddr from a byte representation.
// It validates it as an input string.
Jeromy's avatar
Jeromy committed
32 33 34 35 36 37 38 39
func NewMultiaddrBytes(b []byte) (a Multiaddr, err error) {
	defer func() {
		if e := recover(); e != nil {
			log.Printf("Panic in NewMultiaddrBytes on input %q: %s", b, e)
			err = fmt.Errorf("%v", e)
		}
	}()

40
	if err := validateBytes(b); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42
		return nil, err
	}
43 44

	return &multiaddr{bytes: b}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

// Equal tests whether two multiaddrs are equal
func (m *multiaddr) Equal(m2 Multiaddr) bool {
	return bytes.Equal(m.bytes, m2.Bytes())
}

// Bytes returns the []byte representation of this Multiaddr
func (m *multiaddr) Bytes() []byte {
	// consider returning copy to prevent changing underneath us?
	cpy := make([]byte, len(m.bytes))
	copy(cpy, m.bytes)
	return cpy
}

// String returns the string representation of a Multiaddr
func (m *multiaddr) String() string {
	s, err := bytesToString(m.bytes)
	if err != nil {
		panic("multiaddr failed to convert back to string. corrupted?")
	}
	return s
}

// Protocols returns the list of protocols this Multiaddr has.
// will panic in case we access bytes incorrectly.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
func (m *multiaddr) Protocols() []Protocol {
Steven Allen's avatar
Steven Allen committed
72
	ps := make([]Protocol, 0, 8)
73
	b := m.bytes
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74
	for len(b) > 0 {
75 76 77 78 79
		code, n, err := ReadVarintCode(b)
		if err != nil {
			panic(err)
		}

80
		p := ProtocolWithCode(code)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81
		if p.Code == 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82 83 84 85 86
			// this is a panic (and not returning err) because this should've been
			// caught on constructing the Multiaddr
			panic(fmt.Errorf("no protocol with code %d", b[0]))
		}
		ps = append(ps, p)
87 88
		b = b[n:]

89
		n, size, err := sizeForAddr(p, b)
Jeromy's avatar
Jeromy committed
90 91 92 93
		if err != nil {
			panic(err)
		}

94
		b = b[n+size:]
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95 96 97 98 99 100 101 102 103
	}
	return ps
}

// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
func (m *multiaddr) Encapsulate(o Multiaddr) Multiaddr {
	mb := m.bytes
	ob := o.Bytes()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104 105 106 107
	b := make([]byte, len(mb)+len(ob))
	copy(b, mb)
	copy(b[len(mb):], ob)
	return &multiaddr{bytes: b}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
}

// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
func (m *multiaddr) Decapsulate(o Multiaddr) Multiaddr {
	s1 := m.String()
	s2 := o.String()
	i := strings.LastIndex(s1, s2)
	if i < 0 {
		// if multiaddr not contained, returns a copy.
		cpy := make([]byte, len(m.bytes))
		copy(cpy, m.bytes)
		return &multiaddr{bytes: cpy}
	}

	ma, err := NewMultiaddr(s1[:i])
	if err != nil {
		panic("Multiaddr.Decapsulate incorrect byte boundaries.")
	}
	return ma
}
128 129 130 131

var ErrProtocolNotFound = fmt.Errorf("protocol not found in multiaddr")

func (m *multiaddr) ValueForProtocol(code int) (string, error) {
132 133 134
	for _, sub := range Split(m) {
		p := sub.Protocols()[0]
		if p.Code == code {
135 136 137
			if p.Size == 0 {
				return "", nil
			}
jbenet's avatar
jbenet committed
138
			return strings.SplitN(sub.String(), "/", 3)[2], nil
139 140 141
		}
	}

142
	return "", ErrProtocolNotFound
143
}