2014-07-03 23:42:24 -07:00
|
|
|
package multiaddr
|
|
|
|
|
|
2014-11-19 13:01:52 -08:00
|
|
|
import (
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
)
|
|
|
|
|
|
2014-09-11 10:47:56 -07:00
|
|
|
// Protocol is a Multiaddr protocol description structure.
|
2014-07-03 23:42:24 -07:00
|
|
|
type Protocol struct {
|
2014-11-19 13:01:52 -08:00
|
|
|
Code int
|
|
|
|
|
Size int
|
|
|
|
|
Name string
|
|
|
|
|
VCode []byte
|
2014-07-03 23:42:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
2014-07-07 01:25:20 -07:00
|
|
|
const (
|
|
|
|
|
P_IP4 = 4
|
|
|
|
|
P_TCP = 6
|
|
|
|
|
P_UDP = 17
|
|
|
|
|
P_DCCP = 33
|
|
|
|
|
P_IP6 = 41
|
|
|
|
|
P_SCTP = 132
|
2014-11-19 13:01:52 -08:00
|
|
|
P_UTP = 301
|
|
|
|
|
P_UDT = 302
|
2014-07-07 01:25:20 -07:00
|
|
|
)
|
|
|
|
|
|
2014-09-11 10:47:56 -07:00
|
|
|
// Protocols is the list of multiaddr protocols supported by this module.
|
2014-07-04 00:12:05 -07:00
|
|
|
var Protocols = []*Protocol{
|
2014-11-19 13:01:52 -08:00
|
|
|
&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)},
|
2014-07-04 11:21:39 -07:00
|
|
|
// these require varint:
|
2014-11-19 13:01:52 -08:00
|
|
|
&Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)},
|
|
|
|
|
&Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)},
|
|
|
|
|
&Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)},
|
2014-07-04 11:21:39 -07:00
|
|
|
// {480, 0, "http"},
|
|
|
|
|
// {443, 0, "https"},
|
2014-07-03 23:42:24 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-11 10:47:56 -07:00
|
|
|
// ProtocolWithName returns the Protocol description with given string name.
|
2014-07-03 23:42:24 -07:00
|
|
|
func ProtocolWithName(s string) *Protocol {
|
2014-07-04 11:21:39 -07:00
|
|
|
for _, p := range Protocols {
|
|
|
|
|
if p.Name == s {
|
|
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2014-07-03 23:42:24 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-11 10:47:56 -07:00
|
|
|
// ProtocolWithCode returns the Protocol description with given protocol code.
|
2014-07-03 23:42:24 -07:00
|
|
|
func ProtocolWithCode(c int) *Protocol {
|
2014-07-04 11:21:39 -07:00
|
|
|
for _, p := range Protocols {
|
|
|
|
|
if p.Code == c {
|
|
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2014-07-03 23:42:24 -07:00
|
|
|
}
|
2014-11-19 13:01:52 -08:00
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
num, _ := ReadVarintCode(buf)
|
|
|
|
|
return num
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReadVarintCode reads a varint code from the beginning of buf.
|
|
|
|
|
// returns the code, and the number of bytes read.
|
|
|
|
|
func ReadVarintCode(buf []byte) (int, int) {
|
|
|
|
|
num, n := binary.Uvarint(buf)
|
|
|
|
|
if n < 0 {
|
|
|
|
|
panic("varints larger than uint64 not yet supported")
|
|
|
|
|
}
|
|
|
|
|
return int(num), n
|
|
|
|
|
}
|