mirror of
https://github.com/waku-org/go-multiaddr.git
synced 2025-02-23 11:38:20 +00:00
protocols
This commit is contained in:
parent
3f5984c93d
commit
e20c5b9d3b
16
README.md
16
README.md
@ -24,19 +24,11 @@ m = multiaddr.Multiaddr{ Bytes: m.Bytes }
|
|||||||
### Protocols
|
### Protocols
|
||||||
|
|
||||||
```go
|
```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
|
// get the multiaddr protocol description objects
|
||||||
addr.Protos()
|
addr.Protocols()
|
||||||
// []Protocol{
|
// []*Protocol{
|
||||||
// Protocol{ Code: 4, Name: 'ip4', Size: 32},
|
// &Protocol{ Code: 4, Name: 'ip4', Size: 32},
|
||||||
// Protocol{ Code: 17, Name: 'udp', Size: 16},
|
// &Protocol{ Code: 17, Name: 'udp', Size: 16},
|
||||||
// }
|
// }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
31
index.go
31
index.go
@ -1,5 +1,9 @@
|
|||||||
package multiaddr
|
package multiaddr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type Multiaddr struct {
|
type Multiaddr struct {
|
||||||
Bytes []byte
|
Bytes []byte
|
||||||
}
|
}
|
||||||
@ -11,3 +15,30 @@ func NewString(s string) (*Multiaddr, error) {
|
|||||||
}
|
}
|
||||||
return &Multiaddr{Bytes: b}, nil
|
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
|
||||||
|
}
|
||||||
|
@ -49,3 +49,26 @@ func TestBytesToString(t *testing.T) {
|
|||||||
testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
9
protocols.csv
Normal file
9
protocols.csv
Normal file
@ -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
|
|
18
protocols.go
18
protocols.go
@ -11,14 +11,14 @@ type Protocol struct {
|
|||||||
// 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.
|
||||||
|
|
||||||
var Protocols = []Protocol{
|
var Protocols = []*Protocol{
|
||||||
Protocol{4, 32, "ip4"},
|
&Protocol{4, 32, "ip4"},
|
||||||
Protocol{6, 16, "tcp"},
|
&Protocol{6, 16, "tcp"},
|
||||||
Protocol{17, 16, "udp"},
|
&Protocol{17, 16, "udp"},
|
||||||
Protocol{33, 16, "dccp"},
|
&Protocol{33, 16, "dccp"},
|
||||||
Protocol{41, 128, "ip6"},
|
&Protocol{41, 128, "ip6"},
|
||||||
// these require varint:
|
// these require varint:
|
||||||
Protocol{132, 16, "sctp"},
|
&Protocol{132, 16, "sctp"},
|
||||||
// {480, 0, "http"},
|
// {480, 0, "http"},
|
||||||
// {443, 0, "https"},
|
// {443, 0, "https"},
|
||||||
}
|
}
|
||||||
@ -26,7 +26,7 @@ var Protocols = []Protocol{
|
|||||||
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 {
|
||||||
return &p
|
return p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -35,7 +35,7 @@ func ProtocolWithName(s string) *Protocol {
|
|||||||
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 {
|
||||||
return &p
|
return p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user