]> Sergey Matveev's repositories - btrtrc.git/commitdiff
[client] Handle nil IP in badPeerAddr()
authorafjoseph <7126721+afjoseph@users.noreply.github.com>
Mon, 25 Apr 2022 14:11:15 +0000 (16:11 +0200)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 26 Apr 2022 00:59:12 +0000 (10:59 +1000)
client.go
client_test.go

index ee8261f378d580b56c5fdef1c3c6222cdfabe6b6..a351f04941005736838ca4a209e5837efe2bfe16 100644 (file)
--- a/client.go
+++ b/client.go
@@ -1136,8 +1136,9 @@ func (cl *Client) badPeerAddr(addr PeerRemoteAddr) bool {
        return false
 }
 
+// Returns whether the IP address and port are considered "bad".
 func (cl *Client) badPeerIPPort(ip net.IP, port int) bool {
-       if port == 0 {
+       if port == 0 || ip == nil {
                return true
        }
        if cl.dopplegangerAddr(net.JoinHostPort(ip.String(), strconv.FormatInt(int64(port), 10))) {
index 7b7dd2fdeae3f13c28cbaa32c8bf9ae5cf3aae78..65182d58be84bfe5a880ab2071f2525e5dfee851 100644 (file)
@@ -4,6 +4,8 @@ import (
        "encoding/binary"
        "fmt"
        "io"
+       "net"
+       "net/netip"
        "os"
        "path/filepath"
        "reflect"
@@ -773,3 +775,75 @@ func TestClientDisabledImplicitNetworksButDhtEnabled(t *testing.T) {
        assert.Empty(t, cl.listeners)
        assert.NotEmpty(t, cl.DhtServers())
 }
+
+func TestBadPeerIpPort(t *testing.T) {
+       for _, tc := range []struct {
+               title      string
+               ip         net.IP
+               port       int
+               expectedOk bool
+               setup      func(*Client)
+       }{
+               {"empty both", nil, 0, true, func(*Client) {}},
+               {"empty/nil ip", nil, 6666, true, func(*Client) {}},
+               {
+                       "empty port",
+                       net.ParseIP("127.0.0.1/32"),
+                       0, true,
+                       func(*Client) {},
+               },
+               {
+                       "in doppleganger addresses",
+                       net.ParseIP("127.0.0.1/32"),
+                       2322,
+                       true,
+                       func(cl *Client) {
+                               cl.dopplegangerAddrs["10.0.0.1:2322"] = struct{}{}
+                       },
+               },
+               {
+                       "in IP block list",
+                       net.ParseIP("10.0.0.1"),
+                       2322,
+                       true,
+                       func(cl *Client) {
+                               cl.ipBlockList = iplist.New([]iplist.Range{
+                                       iplist.Range{First: net.ParseIP("10.0.0.1"), Last: net.ParseIP("10.0.0.255")},
+                               })
+                       },
+               },
+               {
+                       "in bad peer IPs",
+                       net.ParseIP("10.0.0.1"),
+                       2322,
+                       true,
+                       func(cl *Client) {
+                               ipAddr, ok := netip.AddrFromSlice(net.ParseIP("10.0.0.1"))
+                               require.True(t, ok)
+                               cl.badPeerIPs = map[netip.Addr]struct{}{}
+                               cl.badPeerIPs[ipAddr] = struct{}{}
+                       },
+               },
+               {
+                       "good",
+                       net.ParseIP("10.0.0.1"),
+                       2322,
+                       false,
+                       func(cl *Client) {},
+               },
+       } {
+               t.Run(tc.title, func(t *testing.T) {
+                       cfg := TestingConfig(t)
+                       cfg.DisableTCP = true
+                       cfg.DisableUTP = true
+                       cfg.NoDHT = false
+                       cl, err := NewClient(cfg)
+                       require.NoError(t, err)
+                       defer cl.Close()
+
+                       tc.setup(cl)
+                       require.Equal(t, tc.expectedOk, cl.badPeerIPPort(tc.ip, tc.port))
+               })
+       }
+
+}