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"
_ "github.com/anacrolix/envpprof"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/utp"
"github.com/bradfitz/iter"
"github.com/stretchr/testify/assert"
@ -23,7 +24,6 @@ import (
"github.com/anacrolix/torrent/data/blob"
"github.com/anacrolix/torrent/internal/testutil"
"github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/util"
)
func init() {
@ -87,7 +87,7 @@ func TestTorrentInitialState(t *testing.T) {
dir, mi := testutil.GreetingTestTorrent()
defer os.RemoveAll(dir)
tor, err := newTorrent(func() (ih InfoHash) {
util.CopyExact(ih[:], mi.Info.Hash)
missinggo.CopyExact(ih[:], mi.Info.Hash)
return
}())
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 {
t.Fatal(err)
}
@ -274,8 +274,8 @@ func TestClientTransfer(t *testing.T) {
}())
leecherGreeting.AddPeers([]Peer{
Peer{
IP: util.AddrIP(seeder.ListenAddr()),
Port: util.AddrPort(seeder.ListenAddr()),
IP: missinggo.AddrIP(seeder.ListenAddr()),
Port: missinggo.AddrPort(seeder.ListenAddr()),
},
})
r := leecherGreeting.NewReader()
@ -430,8 +430,8 @@ func TestResponsive(t *testing.T) {
}())
leecherTorrent.AddPeers([]Peer{
Peer{
IP: util.AddrIP(seeder.ListenAddr()),
Port: util.AddrPort(seeder.ListenAddr()),
IP: missinggo.AddrIP(seeder.ListenAddr()),
Port: missinggo.AddrPort(seeder.ListenAddr()),
},
})
reader := leecherTorrent.NewReader()

View File

@ -3,7 +3,7 @@ package dht
import (
"net"
"github.com/anacrolix/torrent/util"
"github.com/anacrolix/missinggo"
)
// Used internally to refer to node network addresses.
@ -37,5 +37,5 @@ func (ca cachedAddr) IP() net.IP {
}
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"
"time"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/sync"
"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) {
if util.AddrPort(addr) == 0 {
if missinggo.AddrPort(addr) == 0 {
// Not a contactable address.
return
}

View File

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

View File

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

View File

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

View File

@ -7,9 +7,8 @@ import (
"strings"
"testing"
"github.com/anacrolix/missinggo"
"github.com/bradfitz/iter"
"github.com/anacrolix/torrent/util"
)
var sample = `
@ -58,7 +57,7 @@ func connRemoteAddrIP(network, laddr string, dialHost string) net.IP {
panic(err)
}
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 {
panic(err)
}
@ -69,7 +68,7 @@ func connRemoteAddrIP(network, laddr string, dialHost string) net.IP {
panic(err)
}
defer c.Close()
ret := util.AddrIP(c.RemoteAddr())
ret := missinggo.AddrIP(c.RemoteAddr())
return ret
}

View File

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

View File

@ -18,7 +18,6 @@ import (
"github.com/anacrolix/torrent/metainfo"
pp "github.com/anacrolix/torrent/peer_protocol"
"github.com/anacrolix/torrent/tracker"
"github.com/anacrolix/torrent/util"
)
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) {
piece := &piece{}
piece.Event.L = eventLocker
util.CopyExact(piece.Hash[:], hash)
missinggo.CopyExact(piece.Hash[:], hash)
t.Pieces = append(t.Pieces, piece)
}
for _, conn := range t.Conns {
@ -641,7 +640,7 @@ func (t *torrent) hashPiece(piece pp.Integer) (ps pieceSum) {
p := t.Pieces[piece]
p.pendingWrites.Wait()
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
}

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