go-multiaddr/index.go

117 lines
2.7 KiB
Go
Raw Normal View History

2014-07-03 23:42:24 -07:00
package multiaddr
2014-07-04 00:12:05 -07:00
import (
2014-09-16 06:08:48 -07:00
"bytes"
2014-07-04 11:21:39 -07:00
"fmt"
"strings"
2014-07-04 00:12:05 -07:00
)
2014-09-11 10:47:56 -07:00
// Multiaddr is the data structure representing a multiaddr
2014-07-03 23:42:24 -07:00
type Multiaddr struct {
2014-07-04 11:21:39 -07:00
Bytes []byte
2014-07-03 23:42:24 -07:00
}
2014-09-11 10:47:56 -07:00
// NewMultiaddr parses and validates an input string, returning a *Multiaddr
2014-07-04 00:48:33 -07:00
func NewMultiaddr(s string) (*Multiaddr, error) {
2014-09-11 10:47:56 -07:00
b, err := stringToBytes(s)
2014-07-04 11:21:39 -07:00
if err != nil {
return nil, err
}
return &Multiaddr{Bytes: b}, nil
2014-07-03 23:42:24 -07:00
}
2014-07-04 00:12:05 -07:00
2014-09-16 06:08:48 -07:00
// Equal tests whether two multiaddrs are equal
func (m *Multiaddr) Equal(m2 *Multiaddr) bool {
return bytes.Equal(m.Bytes, m2.Bytes)
}
2014-09-11 10:47:56 -07:00
// 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
2014-07-04 00:12:05 -07:00
}
2014-09-11 10:47:56 -07:00
// Protocols returns the list of protocols this Multiaddr has.
2014-07-04 00:12:05 -07:00
func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
2014-07-04 11:21:39 -07:00
// panic handler, in case we try accessing bytes incorrectly.
defer func() {
if e := recover(); e != nil {
ret = nil
err = e.(error)
}
}()
2014-07-04 00:12:05 -07:00
2014-07-04 11:21:39 -07:00
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
2014-07-04 00:12:05 -07:00
}
2014-07-04 00:48:33 -07:00
2014-09-11 10:47:56 -07:00
// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
2014-07-04 00:48:33 -07:00
func (m *Multiaddr) Encapsulate(o *Multiaddr) *Multiaddr {
2014-07-04 11:21:39 -07:00
b := make([]byte, len(m.Bytes)+len(o.Bytes))
b = append(m.Bytes, o.Bytes...)
return &Multiaddr{Bytes: b}
2014-07-04 00:48:33 -07:00
}
2014-09-11 10:47:56 -07:00
// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
2014-07-04 00:48:33 -07:00
func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) {
s1 := m.String()
s2 := o.String()
2014-07-04 11:21:39 -07:00
i := strings.LastIndex(s1, s2)
if i < 0 {
return nil, fmt.Errorf("%s not contained in %s", s2, s1)
}
return NewMultiaddr(s1[:i])
2014-07-04 00:48:33 -07:00
}
2014-07-07 01:25:20 -07:00
2014-09-11 10:47:56 -07:00
// DialArgs is a convenience function returning arguments for use in net.Dial
2014-07-07 01:25:20 -07:00
func (m *Multiaddr) DialArgs() (string, string, error) {
if !m.IsThinWaist() {
2014-09-11 10:47:56 -07:00
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
2014-07-07 01:25:20 -07:00
}
str := m.String()
2014-07-07 01:25:20 -07:00
parts := strings.Split(str, "/")[1:]
network := parts[2]
2014-09-28 19:23:34 -04:00
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])
}
2014-07-07 01:25:20 -07:00
return network, host, nil
}
2014-09-11 10:47:56 -07:00
// IsThinWaist returns whether this multiaddr includes "Thin Waist" Protocols.
// This means: /{IP4, IP6}/{TCP, UDP}
2014-07-07 01:25:20 -07:00
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
}