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

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

9
// Protocol is a Multiaddr protocol description structure.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
type Protocol struct {
11
	Code  int
12
	Size  int // a size of -1 indicates a length-prefixed variable size
13 14
	Name  string
	VCode []byte
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15 16 17 18 19 20
}

// replicating table here to:
// 1. avoid parsing the csv
// 2. ensuring errors in the csv don't screw up code.
// 3. changing a number has to happen in two places.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
const (
22 23 24 25 26 27 28 29 30 31 32
	P_IP4   = 4
	P_TCP   = 6
	P_UDP   = 17
	P_DCCP  = 33
	P_IP6   = 41
	P_SCTP  = 132
	P_UTP   = 301
	P_UDT   = 302
	P_IPFS  = 421
	P_HTTP  = 480
	P_HTTPS = 443
33
	P_ONION = 444
34 35 36 37 38
)

// These are special sizes
const (
	LengthPrefixedVarSize = -1
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40
)

41
// Protocols is the list of multiaddr protocols supported by this module.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42 43 44 45 46 47
var Protocols = []Protocol{
	Protocol{P_IP4, 32, "ip4", CodeToVarint(P_IP4)},
	Protocol{P_TCP, 16, "tcp", CodeToVarint(P_TCP)},
	Protocol{P_UDP, 16, "udp", CodeToVarint(P_UDP)},
	Protocol{P_DCCP, 16, "dccp", CodeToVarint(P_DCCP)},
	Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6)},
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
48
	// these require varint:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
	Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)},
50
	Protocol{P_ONION, 96, "onion", CodeToVarint(P_ONION)},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51 52
	Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)},
	Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)},
53 54 55
	Protocol{P_HTTP, 0, "http", CodeToVarint(P_HTTP)},
	Protocol{P_HTTPS, 0, "https", CodeToVarint(P_HTTPS)},
	Protocol{P_IPFS, LengthPrefixedVarSize, "ipfs", CodeToVarint(P_IPFS)},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56 57
}

Jeromy's avatar
Jeromy committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71
func AddProtocol(p Protocol) error {
	for _, pt := range Protocols {
		if pt.Code == p.Code {
			return fmt.Errorf("protocol code %d already taken by %q", p.Code, pt.Name)
		}
		if pt.Name == p.Name {
			return fmt.Errorf("protocol by the name %q already exists", p.Name)
		}
	}

	Protocols = append(Protocols, p)
	return nil
}

72
// ProtocolWithName returns the Protocol description with given string name.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
func ProtocolWithName(s string) Protocol {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
74 75 76 77 78
	for _, p := range Protocols {
		if p.Name == s {
			return p
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
	return Protocol{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81
}

82
// ProtocolWithCode returns the Protocol description with given protocol code.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
func ProtocolWithCode(c int) Protocol {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
84 85 86 87 88
	for _, p := range Protocols {
		if p.Code == c {
			return p
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
89
	return Protocol{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
}
91

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
// ProtocolsWithString returns a slice of protocols matching given string.
func ProtocolsWithString(s string) ([]Protocol, error) {
	s = strings.Trim(s, "/")
	sp := strings.Split(s, "/")
	if len(sp) == 0 {
		return nil, nil
	}

	t := make([]Protocol, len(sp))
	for i, name := range sp {
		p := ProtocolWithName(name)
		if p.Code == 0 {
			return nil, fmt.Errorf("no protocol with name: %s", name)
		}
		t[i] = p
	}
	return t, nil
}

111 112 113 114 115 116 117 118 119
// CodeToVarint converts an integer to a varint-encoded []byte
func CodeToVarint(num int) []byte {
	buf := make([]byte, (num/7)+1) // varint package is uint64
	n := binary.PutUvarint(buf, uint64(num))
	return buf[:n]
}

// VarintToCode converts a varint-encoded []byte to an integer protocol code
func VarintToCode(buf []byte) int {
120 121 122 123
	num, _, err := ReadVarintCode(buf)
	if err != nil {
		panic(err)
	}
124 125 126 127 128
	return num
}

// ReadVarintCode reads a varint code from the beginning of buf.
// returns the code, and the number of bytes read.
129
func ReadVarintCode(buf []byte) (int, int, error) {
130 131
	num, n := binary.Uvarint(buf)
	if n < 0 {
132
		return 0, 0, fmt.Errorf("varints larger than uint64 not yet supported")
133
	}
134
	return int(num), n, nil
135
}