From e20c5b9d3b2938d2fa0a035e3b925db87318bfba Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Fri, 4 Jul 2014 00:12:05 -0700 Subject: [PATCH] protocols --- README.md | 16 ++++------------ index.go | 31 +++++++++++++++++++++++++++++++ multiaddr_test.go | 23 +++++++++++++++++++++++ protocols.csv | 9 +++++++++ protocols.go | 18 +++++++++--------- 5 files changed, 76 insertions(+), 21 deletions(-) create mode 100644 protocols.csv diff --git a/README.md b/README.md index 06f3e04..8a40027 100644 --- a/README.md +++ b/README.md @@ -24,19 +24,11 @@ m = multiaddr.Multiaddr{ Bytes: m.Bytes } ### Protocols ```go -// get the multiaddr protocol codes -m.ProtoCodes() -// []int{4, 6} - -// get the multiaddr protocol string codes -m.ProtoNames() -// []string{"ip4", "tcp"} - // get the multiaddr protocol description objects -addr.Protos() -// []Protocol{ -// Protocol{ Code: 4, Name: 'ip4', Size: 32}, -// Protocol{ Code: 17, Name: 'udp', Size: 16}, +addr.Protocols() +// []*Protocol{ +// &Protocol{ Code: 4, Name: 'ip4', Size: 32}, +// &Protocol{ Code: 17, Name: 'udp', Size: 16}, // } ``` diff --git a/index.go b/index.go index b0165e1..ec0e054 100644 --- a/index.go +++ b/index.go @@ -1,5 +1,9 @@ package multiaddr +import ( + "fmt" +) + type Multiaddr struct { Bytes []byte } @@ -11,3 +15,30 @@ func NewString(s string) (*Multiaddr, error) { } return &Multiaddr{Bytes: b}, nil } + +func (m *Multiaddr) String() (string, error) { + return BytesToString(m.Bytes) +} + +func (m *Multiaddr) Protocols() (ret []*Protocol, err error) { + + // panic handler, in case we try accessing bytes incorrectly. + defer func() { + if e := recover(); e != nil { + ret = nil + err = e.(error) + } + }() + + ps := []*Protocol{} + b := m.Bytes[:] + for ; len(b) > 0 ; { + p := ProtocolWithCode(int(b[0])) + if p == nil { + return nil, fmt.Errorf("no protocol with code %d", b[0]) + } + ps = append(ps, p) + b = b[1 + (p.Size / 8):] + } + return ps, nil +} diff --git a/multiaddr_test.go b/multiaddr_test.go index e14f79c..60deda4 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -49,3 +49,26 @@ func TestBytesToString(t *testing.T) { testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2") } + +func TestProtocols(t *testing.T) { + m, err := NewString("/ip4/127.0.0.1/udp/1234") + if err != nil { + t.Error("failed to construct", "/ip4/127.0.0.1/udp/1234") + } + + ps, err := m.Protocols() + if err != nil { + t.Error("failed to get protocols", "/ip4/127.0.0.1/udp/1234") + } + + if ps[0] != ProtocolWithName("ip4") { + t.Error(ps[0], ProtocolWithName("ip4")) + t.Error("failed to get ip4 protocol") + } + + if ps[1] != ProtocolWithName("udp") { + t.Error(ps[1], ProtocolWithName("udp")) + t.Error("failed to get udp protocol") + } + +} diff --git a/protocols.csv b/protocols.csv new file mode 100644 index 0000000..62bc5c2 --- /dev/null +++ b/protocols.csv @@ -0,0 +1,9 @@ +code size name +4 32 ip4 +6 16 tcp +17 16 udp +33 16 dccp +41 128 ip6 +132 16 sctp +480 0 http +443 0 https diff --git a/protocols.go b/protocols.go index fac7344..d408578 100644 --- a/protocols.go +++ b/protocols.go @@ -11,14 +11,14 @@ type Protocol struct { // 2. ensuring errors in the csv don't screw up code. // 3. changing a number has to happen in two places. -var Protocols = []Protocol{ - Protocol{4, 32, "ip4"}, - Protocol{6, 16, "tcp"}, - Protocol{17, 16, "udp"}, - Protocol{33, 16, "dccp"}, - Protocol{41, 128, "ip6"}, +var Protocols = []*Protocol{ + &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"}, + &Protocol{132, 16, "sctp"}, // {480, 0, "http"}, // {443, 0, "https"}, } @@ -26,7 +26,7 @@ var Protocols = []Protocol{ func ProtocolWithName(s string) *Protocol { for _, p := range(Protocols) { if p.Name == s { - return &p + return p } } return nil @@ -35,7 +35,7 @@ func ProtocolWithName(s string) *Protocol { func ProtocolWithCode(c int) *Protocol { for _, p := range(Protocols) { if p.Code == c { - return &p + return p } } return nil