protocols.go 763 Bytes
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package multiaddr

type Protocol struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
4 5 6
	Code int
	Size int
	Name string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 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
var Protocols = []*Protocol{
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
15 16 17 18 19 20 21 22 23
	&Protocol{4, 32, "ip4"},
	&Protocol{6, 16, "tcp"},
	&Protocol{17, 16, "udp"},
	&Protocol{33, 16, "dccp"},
	&Protocol{41, 128, "ip6"},
	// these require varint:
	&Protocol{132, 16, "sctp"},
	// {480, 0, "http"},
	// {443, 0, "https"},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25 26
}

func ProtocolWithName(s string) *Protocol {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
27 28 29 30 31 32
	for _, p := range Protocols {
		if p.Name == s {
			return p
		}
	}
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33 34 35
}

func ProtocolWithCode(c int) *Protocol {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
36 37 38 39 40 41
	for _, p := range Protocols {
		if p.Code == c {
			return p
		}
	}
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42
}