go-multiaddr/codec.go

131 lines
2.3 KiB
Go
Raw Normal View History

2014-07-03 23:42:24 -07:00
package multiaddr
2014-07-04 11:21:39 -07:00
import (
"encoding/binary"
"fmt"
"net"
"strconv"
"strings"
2014-07-03 23:42:24 -07:00
)
2014-09-11 10:47:56 -07:00
func stringToBytes(s string) ([]byte, error) {
2014-10-10 20:40:51 -07:00
// consume trailing slashes
s = strings.TrimRight(s, "/")
2014-07-04 11:21:39 -07:00
b := []byte{}
sp := strings.Split(s, "/")
2014-07-03 23:42:24 -07:00
2014-09-11 10:45:20 -07:00
if sp[0] != "" {
return nil, fmt.Errorf("invalid multiaddr, must begin with /")
}
2014-07-04 11:21:39 -07:00
// consume first empty elem
sp = sp[1:]
2014-07-03 23:42:24 -07:00
2014-07-04 11:21:39 -07:00
for len(sp) > 0 {
p := ProtocolWithName(sp[0])
if p == nil {
return nil, fmt.Errorf("no protocol with name %s", sp[0])
}
b = append(b, byte(p.Code))
2014-07-03 23:42:24 -07:00
2014-09-11 10:47:56 -07:00
a := addressStringToBytes(p, sp[1])
2014-07-04 11:21:39 -07:00
b = append(b, a...)
2014-07-03 23:42:24 -07:00
2014-07-04 11:21:39 -07:00
sp = sp[2:]
}
return b, nil
2014-07-03 23:42:24 -07:00
}
2014-09-11 10:47:56 -07:00
func bytesToString(b []byte) (ret string, 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 = ""
err = e.(error)
}
}()
s := ""
for len(b) > 0 {
p := ProtocolWithCode(int(b[0]))
if p == nil {
return "", fmt.Errorf("no protocol with code %d", b[0])
}
s = strings.Join([]string{s, "/", p.Name}, "")
b = b[1:]
2014-09-11 10:47:56 -07:00
a := addressBytesToString(p, b[:(p.Size/8)])
2014-07-04 11:21:39 -07:00
if len(a) > 0 {
s = strings.Join([]string{s, "/", a}, "")
}
b = b[(p.Size / 8):]
}
return s, nil
2014-07-03 23:42:24 -07:00
}
2014-07-03 23:44:09 -07:00
2014-11-04 23:20:17 -08:00
func bytesSplit(b []byte) (ret [][]byte, err error) {
// panic handler, in case we try accessing bytes incorrectly.
defer func() {
if e := recover(); e != nil {
ret = [][]byte{}
err = e.(error)
}
}()
ret = [][]byte{}
for len(b) > 0 {
p := ProtocolWithCode(int(b[0]))
if p == nil {
return [][]byte{}, fmt.Errorf("no protocol with code %d", b[0])
}
length := 1 + (p.Size / 8)
ret = append(ret, b[:length])
b = b[length:]
}
return ret, nil
}
2014-09-11 10:47:56 -07:00
func addressStringToBytes(p *Protocol, s string) []byte {
2014-07-04 11:21:39 -07:00
switch p.Code {
2014-09-13 04:47:37 -07:00
case P_IP4: // ipv4
2014-07-04 11:21:39 -07:00
return net.ParseIP(s).To4()
2014-09-13 04:47:37 -07:00
case P_IP6: // ipv6
return net.ParseIP(s).To16()
2014-07-04 11:21:39 -07:00
// tcp udp dccp sctp
2014-09-13 04:47:37 -07:00
case P_TCP, P_UDP, P_DCCP, P_SCTP:
2014-07-04 11:21:39 -07:00
b := make([]byte, 2)
i, err := strconv.Atoi(s)
if err == nil {
binary.BigEndian.PutUint16(b, uint16(i))
}
return b
}
return []byte{}
2014-07-03 23:44:09 -07:00
}
2014-09-11 10:47:56 -07:00
func addressBytesToString(p *Protocol, b []byte) string {
2014-07-04 11:21:39 -07:00
switch p.Code {
2014-07-03 23:44:09 -07:00
2014-07-04 11:21:39 -07:00
// ipv4,6
2014-09-13 04:47:37 -07:00
case P_IP4, P_IP6:
2014-07-04 11:21:39 -07:00
return net.IP(b).String()
2014-07-03 23:44:09 -07:00
2014-07-04 11:21:39 -07:00
// tcp udp dccp sctp
2014-09-13 04:47:37 -07:00
case P_TCP, P_UDP, P_DCCP, P_SCTP:
2014-07-04 11:21:39 -07:00
i := binary.BigEndian.Uint16(b)
return strconv.Itoa(int(i))
}
2014-07-03 23:44:09 -07:00
2014-07-04 11:21:39 -07:00
return ""
2014-07-03 23:44:09 -07:00
}