Commit b9067889 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

go lint + unexporting some funcs

parent 6ee313e6
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"strings" "strings"
) )
func StringToBytes(s string) ([]byte, error) { func stringToBytes(s string) ([]byte, error) {
b := []byte{} b := []byte{}
sp := strings.Split(s, "/") sp := strings.Split(s, "/")
...@@ -26,7 +26,7 @@ func StringToBytes(s string) ([]byte, error) { ...@@ -26,7 +26,7 @@ func StringToBytes(s string) ([]byte, error) {
} }
b = append(b, byte(p.Code)) b = append(b, byte(p.Code))
a := AddressStringToBytes(p, sp[1]) a := addressStringToBytes(p, sp[1])
b = append(b, a...) b = append(b, a...)
sp = sp[2:] sp = sp[2:]
...@@ -34,7 +34,7 @@ func StringToBytes(s string) ([]byte, error) { ...@@ -34,7 +34,7 @@ func StringToBytes(s string) ([]byte, error) {
return b, nil return b, nil
} }
func BytesToString(b []byte) (ret string, err error) { func bytesToString(b []byte) (ret string, err error) {
// panic handler, in case we try accessing bytes incorrectly. // panic handler, in case we try accessing bytes incorrectly.
defer func() { defer func() {
if e := recover(); e != nil { if e := recover(); e != nil {
...@@ -53,7 +53,7 @@ func BytesToString(b []byte) (ret string, err error) { ...@@ -53,7 +53,7 @@ func BytesToString(b []byte) (ret string, err error) {
s = strings.Join([]string{s, "/", p.Name}, "") s = strings.Join([]string{s, "/", p.Name}, "")
b = b[1:] b = b[1:]
a := AddressBytesToString(p, b[:(p.Size/8)]) a := addressBytesToString(p, b[:(p.Size/8)])
if len(a) > 0 { if len(a) > 0 {
s = strings.Join([]string{s, "/", a}, "") s = strings.Join([]string{s, "/", a}, "")
} }
...@@ -63,7 +63,7 @@ func BytesToString(b []byte) (ret string, err error) { ...@@ -63,7 +63,7 @@ func BytesToString(b []byte) (ret string, err error) {
return s, nil return s, nil
} }
func AddressStringToBytes(p *Protocol, s string) []byte { func addressStringToBytes(p *Protocol, s string) []byte {
switch p.Code { switch p.Code {
// ipv4,6 // ipv4,6
...@@ -83,7 +83,7 @@ func AddressStringToBytes(p *Protocol, s string) []byte { ...@@ -83,7 +83,7 @@ func AddressStringToBytes(p *Protocol, s string) []byte {
return []byte{} return []byte{}
} }
func AddressBytesToString(p *Protocol, b []byte) string { func addressBytesToString(p *Protocol, b []byte) string {
switch p.Code { switch p.Code {
// ipv4,6 // ipv4,6
......
...@@ -5,22 +5,26 @@ import ( ...@@ -5,22 +5,26 @@ import (
"strings" "strings"
) )
// Multiaddr is the data structure representing a multiaddr
type Multiaddr struct { type Multiaddr struct {
Bytes []byte Bytes []byte
} }
// NewMultiaddr parses and validates an input string, returning a *Multiaddr
func NewMultiaddr(s string) (*Multiaddr, error) { func NewMultiaddr(s string) (*Multiaddr, error) {
b, err := StringToBytes(s) b, err := stringToBytes(s)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Multiaddr{Bytes: b}, nil return &Multiaddr{Bytes: b}, nil
} }
// String returns the string representation of a Multiaddr
func (m *Multiaddr) String() (string, error) { func (m *Multiaddr) String() (string, error) {
return BytesToString(m.Bytes) return bytesToString(m.Bytes)
} }
// Protocols returns the list of protocols this Multiaddr has.
func (m *Multiaddr) Protocols() (ret []*Protocol, err error) { func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
// panic handler, in case we try accessing bytes incorrectly. // panic handler, in case we try accessing bytes incorrectly.
...@@ -44,12 +48,14 @@ func (m *Multiaddr) Protocols() (ret []*Protocol, err error) { ...@@ -44,12 +48,14 @@ func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
return ps, nil return ps, nil
} }
// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
func (m *Multiaddr) Encapsulate(o *Multiaddr) *Multiaddr { func (m *Multiaddr) Encapsulate(o *Multiaddr) *Multiaddr {
b := make([]byte, len(m.Bytes)+len(o.Bytes)) b := make([]byte, len(m.Bytes)+len(o.Bytes))
b = append(m.Bytes, o.Bytes...) b = append(m.Bytes, o.Bytes...)
return &Multiaddr{Bytes: b} return &Multiaddr{Bytes: b}
} }
// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) { func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) {
s1, err := m.String() s1, err := m.String()
if err != nil { if err != nil {
...@@ -68,9 +74,10 @@ func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) { ...@@ -68,9 +74,10 @@ func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) {
return NewMultiaddr(s1[:i]) return NewMultiaddr(s1[:i])
} }
// DialArgs is a convenience function returning arguments for use in net.Dial
func (m *Multiaddr) DialArgs() (string, string, error) { func (m *Multiaddr) DialArgs() (string, string, error) {
if !m.IsThinWaist() { if !m.IsThinWaist() {
return "", "", fmt.Errorf("%s is not a 'thin waist' address.", m) return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
} }
str, err := m.String() str, err := m.String()
...@@ -84,6 +91,8 @@ func (m *Multiaddr) DialArgs() (string, string, error) { ...@@ -84,6 +91,8 @@ func (m *Multiaddr) DialArgs() (string, string, error) {
return network, host, nil return network, host, nil
} }
// IsThinWaist returns whether this multiaddr includes "Thin Waist" Protocols.
// This means: /{IP4, IP6}/{TCP, UDP}
func (m *Multiaddr) IsThinWaist() bool { func (m *Multiaddr) IsThinWaist() bool {
p, err := m.Protocols() p, err := m.Protocols()
if err != nil { if err != nil {
......
...@@ -14,7 +14,7 @@ func TestStringToBytes(t *testing.T) { ...@@ -14,7 +14,7 @@ func TestStringToBytes(t *testing.T) {
t.Error("failed to decode hex", h) t.Error("failed to decode hex", h)
} }
b2, err := StringToBytes(s) b2, err := stringToBytes(s)
if err != nil { if err != nil {
t.Error("failed to convert", s) t.Error("failed to convert", s)
} }
...@@ -35,7 +35,7 @@ func TestBytesToString(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestBytesToString(t *testing.T) {
t.Error("failed to decode hex", h) t.Error("failed to decode hex", h)
} }
s2, err := BytesToString(b) s2, err := bytesToString(b)
if err != nil { if err != nil {
t.Error("failed to convert", b) t.Error("failed to convert", b)
} }
......
package multiaddr package multiaddr
// Protocol is a Multiaddr protocol description structure.
type Protocol struct { type Protocol struct {
Code int Code int
Size int Size int
...@@ -10,7 +11,6 @@ type Protocol struct { ...@@ -10,7 +11,6 @@ type Protocol struct {
// 1. avoid parsing the csv // 1. avoid parsing the csv
// 2. ensuring errors in the csv don't screw up code. // 2. ensuring errors in the csv don't screw up code.
// 3. changing a number has to happen in two places. // 3. changing a number has to happen in two places.
const ( const (
P_IP4 = 4 P_IP4 = 4
P_TCP = 6 P_TCP = 6
...@@ -20,6 +20,7 @@ const ( ...@@ -20,6 +20,7 @@ const (
P_SCTP = 132 P_SCTP = 132
) )
// Protocols is the list of multiaddr protocols supported by this module.
var Protocols = []*Protocol{ var Protocols = []*Protocol{
&Protocol{P_IP4, 32, "ip4"}, &Protocol{P_IP4, 32, "ip4"},
&Protocol{P_TCP, 16, "tcp"}, &Protocol{P_TCP, 16, "tcp"},
...@@ -32,6 +33,7 @@ var Protocols = []*Protocol{ ...@@ -32,6 +33,7 @@ var Protocols = []*Protocol{
// {443, 0, "https"}, // {443, 0, "https"},
} }
// ProtocolWithName returns the Protocol description with given string name.
func ProtocolWithName(s string) *Protocol { func ProtocolWithName(s string) *Protocol {
for _, p := range Protocols { for _, p := range Protocols {
if p.Name == s { if p.Name == s {
...@@ -41,6 +43,7 @@ func ProtocolWithName(s string) *Protocol { ...@@ -41,6 +43,7 @@ func ProtocolWithName(s string) *Protocol {
return nil return nil
} }
// ProtocolWithCode returns the Protocol description with given protocol code.
func ProtocolWithCode(c int) *Protocol { func ProtocolWithCode(c int) *Protocol {
for _, p := range Protocols { for _, p := range Protocols {
if p.Code == c { if p.Code == c {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment