2
0
mirror of synced 2025-02-23 14:18:13 +00:00

Some utils moved to missinggo

This commit is contained in:
Matt Joiner 2015-08-04 00:29:01 +10:00
parent 64848a206a
commit d077fed72d
13 changed files with 33 additions and 194 deletions

View File

@ -13,6 +13,7 @@ import (
"time" "time"
_ "github.com/anacrolix/envpprof" _ "github.com/anacrolix/envpprof"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/utp" "github.com/anacrolix/utp"
"github.com/bradfitz/iter" "github.com/bradfitz/iter"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -23,7 +24,6 @@ import (
"github.com/anacrolix/torrent/data/blob" "github.com/anacrolix/torrent/data/blob"
"github.com/anacrolix/torrent/internal/testutil" "github.com/anacrolix/torrent/internal/testutil"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/util"
) )
func init() { func init() {
@ -87,7 +87,7 @@ func TestTorrentInitialState(t *testing.T) {
dir, mi := testutil.GreetingTestTorrent() dir, mi := testutil.GreetingTestTorrent()
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
tor, err := newTorrent(func() (ih InfoHash) { tor, err := newTorrent(func() (ih InfoHash) {
util.CopyExact(ih[:], mi.Info.Hash) missinggo.CopyExact(ih[:], mi.Info.Hash)
return return
}()) }())
if err != nil { if err != nil {
@ -196,7 +196,7 @@ func TestUTPRawConn(t *testing.T) {
} }
} }
}() }()
udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("localhost:%d", util.AddrPort(l.Addr()))) udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("localhost:%d", missinggo.AddrPort(l.Addr())))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -274,8 +274,8 @@ func TestClientTransfer(t *testing.T) {
}()) }())
leecherGreeting.AddPeers([]Peer{ leecherGreeting.AddPeers([]Peer{
Peer{ Peer{
IP: util.AddrIP(seeder.ListenAddr()), IP: missinggo.AddrIP(seeder.ListenAddr()),
Port: util.AddrPort(seeder.ListenAddr()), Port: missinggo.AddrPort(seeder.ListenAddr()),
}, },
}) })
r := leecherGreeting.NewReader() r := leecherGreeting.NewReader()
@ -430,8 +430,8 @@ func TestResponsive(t *testing.T) {
}()) }())
leecherTorrent.AddPeers([]Peer{ leecherTorrent.AddPeers([]Peer{
Peer{ Peer{
IP: util.AddrIP(seeder.ListenAddr()), IP: missinggo.AddrIP(seeder.ListenAddr()),
Port: util.AddrPort(seeder.ListenAddr()), Port: missinggo.AddrPort(seeder.ListenAddr()),
}, },
}) })
reader := leecherTorrent.NewReader() reader := leecherTorrent.NewReader()

View File

@ -3,7 +3,7 @@ package dht
import ( import (
"net" "net"
"github.com/anacrolix/torrent/util" "github.com/anacrolix/missinggo"
) )
// Used internally to refer to node network addresses. // Used internally to refer to node network addresses.
@ -37,5 +37,5 @@ func (ca cachedAddr) IP() net.IP {
} }
func newDHTAddr(addr net.Addr) dHTAddr { func newDHTAddr(addr net.Addr) dHTAddr {
return cachedAddr{addr, addr.String(), util.AddrIP(addr)} return cachedAddr{addr, addr.String(), missinggo.AddrIP(addr)}
} }

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"time" "time"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/sync" "github.com/anacrolix/sync"
"github.com/willf/bloom" "github.com/willf/bloom"
@ -97,7 +98,7 @@ func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*Announc
} }
func (me *Announce) gotNodeAddr(addr dHTAddr) { func (me *Announce) gotNodeAddr(addr dHTAddr) {
if util.AddrPort(addr) == 0 { if missinggo.AddrPort(addr) == 0 {
// Not a contactable address. // Not a contactable address.
return return
} }

View File

@ -20,6 +20,7 @@ import (
"os" "os"
"time" "time"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/sync" "github.com/anacrolix/sync"
"github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/bencode"
@ -637,7 +638,7 @@ func (s *Server) serve() error {
continue continue
} }
s.mu.Lock() s.mu.Lock()
blocked := s.ipBlocked(util.AddrIP(addr)) blocked := s.ipBlocked(missinggo.AddrIP(addr))
s.mu.Unlock() s.mu.Unlock()
if blocked { if blocked {
readBlocked.Add(1) readBlocked.Add(1)
@ -809,7 +810,7 @@ func (s *Server) nodeTimedOut(addr dHTAddr) {
func (s *Server) writeToNode(b []byte, node dHTAddr) (err error) { func (s *Server) writeToNode(b []byte, node dHTAddr) (err error) {
if list := s.ipBlockList; list != nil { if list := s.ipBlockList; list != nil {
if r := list.Lookup(util.AddrIP(node.UDPAddr())); r != nil { if r := list.Lookup(missinggo.AddrIP(node.UDPAddr())); r != nil {
err = fmt.Errorf("write to %s blocked: %s", node, r.Description) err = fmt.Errorf("write to %s blocked: %s", node, r.Description)
return return
} }
@ -908,14 +909,14 @@ func (ni *NodeInfo) PutCompact(b []byte) error {
if n := copy(b[:], ni.ID[:]); n != 20 { if n := copy(b[:], ni.ID[:]); n != 20 {
panic(n) panic(n)
} }
ip := util.AddrIP(ni.Addr).To4() ip := missinggo.AddrIP(ni.Addr).To4()
if len(ip) != 4 { if len(ip) != 4 {
return errors.New("expected ipv4 address") return errors.New("expected ipv4 address")
} }
if n := copy(b[20:], ip); n != 4 { if n := copy(b[20:], ip); n != 4 {
panic(n) panic(n)
} }
binary.BigEndian.PutUint16(b[24:], uint16(util.AddrPort(ni.Addr))) binary.BigEndian.PutUint16(b[24:], uint16(missinggo.AddrPort(ni.Addr)))
return nil return nil
} }
@ -923,7 +924,7 @@ func (cni *NodeInfo) UnmarshalCompact(b []byte) error {
if len(b) != 26 { if len(b) != 26 {
return errors.New("expected 26 bytes") return errors.New("expected 26 bytes")
} }
util.CopyExact(cni.ID[:], b[:20]) missinggo.CopyExact(cni.ID[:], b[:20])
cni.Addr = newDHTAddr(&net.UDPAddr{ cni.Addr = newDHTAddr(&net.UDPAddr{
IP: net.IPv4(b[20], b[21], b[22], b[23]), IP: net.IPv4(b[20], b[21], b[22], b[23]),
Port: int(binary.BigEndian.Uint16(b[24:26])), Port: int(binary.BigEndian.Uint16(b[24:26])),
@ -969,11 +970,11 @@ func (s *Server) liftNodes(d Msg) {
return return
} }
for _, cni := range d.Nodes() { for _, cni := range d.Nodes() {
if util.AddrPort(cni.Addr) == 0 { if missinggo.AddrPort(cni.Addr) == 0 {
// TODO: Why would people even do this? // TODO: Why would people even do this?
continue continue
} }
if s.ipBlocked(util.AddrIP(cni.Addr)) { if s.ipBlocked(missinggo.AddrIP(cni.Addr)) {
continue continue
} }
n := s.getNode(cni.Addr, string(cni.ID[:])) n := s.getNode(cni.Addr, string(cni.ID[:]))

View File

@ -7,7 +7,7 @@ import (
"net" "net"
"testing" "testing"
"github.com/anacrolix/torrent/util" "github.com/anacrolix/missinggo"
) )
func TestSetNilBigInt(t *testing.T) { func TestSetNilBigInt(t *testing.T) {
@ -207,7 +207,7 @@ func TestServerDefaultNodeIdSecure(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
defer s.Close() defer s.Close()
if !nodeIdSecure(s.ID(), util.AddrIP(s.Addr())) { if !nodeIdSecure(s.ID(), missinggo.AddrIP(s.Addr())) {
t.Fatal("not secure") t.Fatal("not secure")
} }
} }

View File

@ -17,6 +17,7 @@ import (
"bazil.org/fuse" "bazil.org/fuse"
fusefs "bazil.org/fuse/fs" fusefs "bazil.org/fuse/fs"
"github.com/anacrolix/missinggo"
netContext "golang.org/x/net/context" netContext "golang.org/x/net/context"
"github.com/anacrolix/torrent" "github.com/anacrolix/torrent"
@ -24,7 +25,6 @@ import (
"github.com/anacrolix/torrent/data/mmap" "github.com/anacrolix/torrent/data/mmap"
"github.com/anacrolix/torrent/internal/testutil" "github.com/anacrolix/torrent/internal/testutil"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/util"
) )
func init() { func init() {
@ -45,7 +45,7 @@ func TestTCPAddrString(t *testing.T) {
ras := c.RemoteAddr().String() ras := c.RemoteAddr().String()
ta := &net.TCPAddr{ ta := &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1), IP: net.IPv4(127, 0, 0, 1),
Port: util.AddrPort(l.Addr()), Port: missinggo.AddrPort(l.Addr()),
} }
s := ta.String() s := ta.String()
if ras != s { if ras != s {

View File

@ -7,9 +7,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/anacrolix/missinggo"
"github.com/bradfitz/iter" "github.com/bradfitz/iter"
"github.com/anacrolix/torrent/util"
) )
var sample = ` var sample = `
@ -58,7 +57,7 @@ func connRemoteAddrIP(network, laddr string, dialHost string) net.IP {
panic(err) panic(err)
} }
go func() { go func() {
c, err := net.Dial(network, net.JoinHostPort(dialHost, fmt.Sprintf("%d", util.AddrPort(l.Addr())))) c, err := net.Dial(network, net.JoinHostPort(dialHost, fmt.Sprintf("%d", missinggo.AddrPort(l.Addr()))))
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -69,7 +68,7 @@ func connRemoteAddrIP(network, laddr string, dialHost string) net.IP {
panic(err) panic(err)
} }
defer c.Close() defer c.Close()
ret := util.AddrIP(c.RemoteAddr()) ret := missinggo.AddrIP(c.RemoteAddr())
return ret return ret
} }

View File

@ -17,9 +17,8 @@ import (
"strconv" "strconv"
"sync" "sync"
"github.com/anacrolix/missinggo"
"github.com/bradfitz/iter" "github.com/bradfitz/iter"
"github.com/anacrolix/torrent/util"
) )
const ( const (
@ -168,7 +167,7 @@ func (h *handshake) establishS() (err error) {
var Y, S big.Int var Y, S big.Int
Y.SetBytes(b[:]) Y.SetBytes(b[:])
S.Exp(&Y, &x, &p) S.Exp(&Y, &x, &p)
util.CopyExact(&h.s, paddedLeft(S.Bytes(), 96)) missinggo.CopyExact(&h.s, paddedLeft(S.Bytes(), 96))
return return
} }

View File

@ -18,7 +18,6 @@ import (
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
pp "github.com/anacrolix/torrent/peer_protocol" pp "github.com/anacrolix/torrent/peer_protocol"
"github.com/anacrolix/torrent/tracker" "github.com/anacrolix/torrent/tracker"
"github.com/anacrolix/torrent/util"
) )
func (t *torrent) pieceNumPendingBytes(index int) (count pp.Integer) { func (t *torrent) pieceNumPendingBytes(index int) (count pp.Integer) {
@ -226,7 +225,7 @@ func (t *torrent) setMetadata(md *metainfo.Info, infoBytes []byte, eventLocker s
for _, hash := range infoPieceHashes(md) { for _, hash := range infoPieceHashes(md) {
piece := &piece{} piece := &piece{}
piece.Event.L = eventLocker piece.Event.L = eventLocker
util.CopyExact(piece.Hash[:], hash) missinggo.CopyExact(piece.Hash[:], hash)
t.Pieces = append(t.Pieces, piece) t.Pieces = append(t.Pieces, piece)
} }
for _, conn := range t.Conns { for _, conn := range t.Conns {
@ -641,7 +640,7 @@ func (t *torrent) hashPiece(piece pp.Integer) (ps pieceSum) {
p := t.Pieces[piece] p := t.Pieces[piece]
p.pendingWrites.Wait() p.pendingWrites.Wait()
t.data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, t.Info.PieceLength) t.data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, t.Info.PieceLength)
util.CopyExact(ps[:], hash.Sum(nil)) missinggo.CopyExact(ps[:], hash.Sum(nil))
return return
} }

View File

@ -1,39 +0,0 @@
package util
import (
"net"
"strconv"
)
// Extracts the port as an integer from an address string.
func AddrPort(addr net.Addr) int {
switch raw := addr.(type) {
case *net.UDPAddr:
return raw.Port
default:
_, port, err := net.SplitHostPort(addr.String())
if err != nil {
panic(err)
}
i64, err := strconv.ParseInt(port, 0, 0)
if err != nil {
panic(err)
}
return int(i64)
}
}
func AddrIP(addr net.Addr) net.IP {
switch raw := addr.(type) {
case *net.UDPAddr:
return raw.IP
case *net.TCPAddr:
return raw.IP
default:
host, _, err := net.SplitHostPort(addr.String())
if err != nil {
panic(err)
}
return net.ParseIP(host)
}
}

View File

@ -1,32 +0,0 @@
package util
import (
"fmt"
"reflect"
)
func CopyExact(dest interface{}, src interface{}) {
dV := reflect.ValueOf(dest)
sV := reflect.ValueOf(src)
if dV.Kind() == reflect.Ptr {
dV = dV.Elem()
}
if dV.Kind() == reflect.Array && !dV.CanAddr() {
panic(fmt.Sprintf("dest not addressable: %T", dest))
}
if sV.Kind() == reflect.Ptr {
sV = sV.Elem()
}
if sV.Kind() == reflect.String {
sV = sV.Convert(reflect.SliceOf(dV.Type().Elem()))
}
if !sV.IsValid() {
panic("invalid source, probably nil")
}
if dV.Len() != sV.Len() {
panic(fmt.Sprintf("dest len (%d) != src len (%d)", dV.Len(), sV.Len()))
}
if dV.Len() != reflect.Copy(dV, sV) {
panic("dammit")
}
}

View File

@ -1,89 +0,0 @@
package util
import (
"bytes"
"strings"
"testing"
)
func TestCopyToArray(t *testing.T) {
var arr [3]byte
bb := []byte{1, 2, 3}
CopyExact(&arr, bb)
if !bytes.Equal(arr[:], bb) {
t.FailNow()
}
}
func TestCopyToSlicedArray(t *testing.T) {
var arr [5]byte
CopyExact(arr[:], "hello")
if !bytes.Equal(arr[:], []byte("hello")) {
t.FailNow()
}
}
func TestCopyDestNotAddr(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.FailNow()
}
t.Log(r)
}()
var arr [3]byte
CopyExact(arr, "nope")
}
func TestCopyLenMismatch(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.FailNow()
}
t.Log(r)
}()
CopyExact(make([]byte, 2), "abc")
}
func TestCopySrcString(t *testing.T) {
dest := make([]byte, 3)
CopyExact(dest, "lol")
if string(dest) != "lol" {
t.FailNow()
}
func() {
defer func() {
r := recover()
if r == nil {
t.FailNow()
}
}()
CopyExact(dest, "rofl")
}()
var arr [5]byte
CopyExact(&arr, interface{}("hello"))
if string(arr[:]) != "hello" {
t.FailNow()
}
}
func TestCopySrcNilInterface(t *testing.T) {
var arr [3]byte
defer func() {
r := recover().(string)
if !strings.Contains(r, "invalid source") {
t.FailNow()
}
}()
CopyExact(&arr, nil)
}
func TestCopySrcPtr(t *testing.T) {
var bigDst [1024]byte
var bigSrc [1024]byte = [1024]byte{'h', 'i'}
CopyExact(&bigDst, &bigSrc)
if !bytes.Equal(bigDst[:], bigSrc[:]) {
t.FailNow()
}
}

View File

@ -9,11 +9,11 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/anacrolix/missinggo"
"github.com/go-fsnotify/fsnotify" "github.com/go-fsnotify/fsnotify"
"github.com/anacrolix/torrent" "github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/util"
) )
type Change uint type Change uint
@ -70,7 +70,7 @@ func torrentFileInfoHash(fileName string) (ih torrent.InfoHash, ok bool) {
if mi == nil { if mi == nil {
return return
} }
util.CopyExact(ih[:], mi.Info.Hash) missinggo.CopyExact(ih[:], mi.Info.Hash)
ok = true ok = true
return return
} }
@ -108,7 +108,7 @@ func scanDir(dirName string) (ee map[torrent.InfoHash]entity) {
e := entity{ e := entity{
TorrentFilePath: fullName, TorrentFilePath: fullName,
} }
util.CopyExact(&e.InfoHash, ih) missinggo.CopyExact(&e.InfoHash, ih)
addEntity(e) addEntity(e)
case ".magnet": case ".magnet":
uris, err := magnetFileURIs(fullName) uris, err := magnetFileURIs(fullName)