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

3
// Protocol is a Multiaddr protocol description structure.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
type Protocol struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
5 6 7
	Code int
	Size int
	Name string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8 9 10 11 12 13
}

// 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
14 15 16 17 18 19 20 21 22
const (
	P_IP4  = 4
	P_TCP  = 6
	P_UDP  = 17
	P_DCCP = 33
	P_IP6  = 41
	P_SCTP = 132
)

23
// Protocols is the list of multiaddr protocols supported by this module.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24
var Protocols = []*Protocol{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26 27 28 29
	&Protocol{P_IP4, 32, "ip4"},
	&Protocol{P_TCP, 16, "tcp"},
	&Protocol{P_UDP, 16, "udp"},
	&Protocol{P_DCCP, 16, "dccp"},
	&Protocol{P_IP6, 128, "ip6"},
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
30
	// these require varint:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
	&Protocol{P_SCTP, 16, "sctp"},
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
32 33
	// {480, 0, "http"},
	// {443, 0, "https"},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35
}

36
// ProtocolWithName returns the Protocol description with given string name.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37
func ProtocolWithName(s string) *Protocol {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
38 39 40 41 42 43
	for _, p := range Protocols {
		if p.Name == s {
			return p
		}
	}
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45
}

46
// ProtocolWithCode returns the Protocol description with given protocol code.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47
func ProtocolWithCode(c int) *Protocol {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
48 49 50 51 52 53
	for _, p := range Protocols {
		if p.Code == c {
			return p
		}
	}
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54
}