Skip to content

Commit

Permalink
Ignore multiaddrs with unknown protocols (#30)
Browse files Browse the repository at this point in the history
If new protocols, unknown to the indexer or assigner service, are encoded into multiaddrs in announce messages, then ignore these. This will allow older indexers to accept messages even when newer address types are included.

Fixes issue #29
  • Loading branch information
gammazero authored May 9, 2023
1 parent afe2d8e commit 81286e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 9 additions & 4 deletions announce/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package message

import (
"errors"
"strings"

"github.com/ipfs/go-cid"
"github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -38,13 +39,17 @@ func (m *Message) SetAddrs(addrs []multiaddr.Multiaddr) {
// GetAddrs reads the slice of Multiaddr that is stored in the Message as a
// slice of []byte.
func (m *Message) GetAddrs() ([]multiaddr.Multiaddr, error) {
addrs := make([]multiaddr.Multiaddr, len(m.Addrs))
for i := range m.Addrs {
var err error
addrs[i], err = multiaddr.NewMultiaddrBytes(m.Addrs[i])
addrs := make([]multiaddr.Multiaddr, 0, len(m.Addrs))
for _, addrBytes := range m.Addrs {
addr, err := multiaddr.NewMultiaddrBytes(addrBytes)
if err != nil {
if strings.Contains(err.Error(), "no protocol with code") {
// Ignore unknown protocols.
continue
}
return nil, err
}
addrs = append(addrs, addr)
}
return addrs, nil
}
15 changes: 15 additions & 0 deletions announce/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,18 @@ func TestJSON(t *testing.T) {

require.Equal(t, msg, newMsg)
}

func TestUnknownProtocol(t *testing.T) {
// Encoded unknown protocol code 9999: /ip4/127.0.0.1/udp/1234/<proto-9999>
addrData := []byte{54, 9, 108, 111, 99, 97, 108, 104, 111, 115, 116, 145, 2, 4, 210, 143, 78}
_, err := multiaddr.NewMultiaddrBytes(addrData)
require.ErrorContains(t, err, "no protocol with code")

var msg message.Message
msg.SetAddrs([]multiaddr.Multiaddr{maddr1})
msg.Addrs = append(msg.Addrs, addrData)

addrs, err := msg.GetAddrs()
require.NoError(t, err)
require.Equal(t, 1, len(addrs))
}

0 comments on commit 81286e4

Please sign in to comment.