"time"
_ "github.com/anacrolix/envpprof"
+ "github.com/anacrolix/missinggo"
"github.com/anacrolix/utp"
"github.com/bradfitz/iter"
"github.com/stretchr/testify/assert"
"github.com/anacrolix/torrent/data/blob"
"github.com/anacrolix/torrent/internal/testutil"
"github.com/anacrolix/torrent/metainfo"
- "github.com/anacrolix/torrent/util"
)
func init() {
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 {
}
}
}()
- 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)
}
}())
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()
}())
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()
import (
"net"
- "github.com/anacrolix/torrent/util"
+ "github.com/anacrolix/missinggo"
)
// Used internally to refer to node network addresses.
}
func newDHTAddr(addr net.Addr) dHTAddr {
- return cachedAddr{addr, addr.String(), util.AddrIP(addr)}
+ return cachedAddr{addr, addr.String(), missinggo.AddrIP(addr)}
}
"log"
"time"
+ "github.com/anacrolix/missinggo"
"github.com/anacrolix/sync"
"github.com/willf/bloom"
}
func (me *Announce) gotNodeAddr(addr dHTAddr) {
- if util.AddrPort(addr) == 0 {
+ if missinggo.AddrPort(addr) == 0 {
// Not a contactable address.
return
}
"os"
"time"
+ "github.com/anacrolix/missinggo"
"github.com/anacrolix/sync"
"github.com/anacrolix/torrent/bencode"
continue
}
s.mu.Lock()
- blocked := s.ipBlocked(util.AddrIP(addr))
+ blocked := s.ipBlocked(missinggo.AddrIP(addr))
s.mu.Unlock()
if blocked {
readBlocked.Add(1)
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
}
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
}
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])),
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[:]))
"net"
"testing"
- "github.com/anacrolix/torrent/util"
+ "github.com/anacrolix/missinggo"
)
func TestSetNilBigInt(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")
}
}
"bazil.org/fuse"
fusefs "bazil.org/fuse/fs"
+ "github.com/anacrolix/missinggo"
netContext "golang.org/x/net/context"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/data/mmap"
"github.com/anacrolix/torrent/internal/testutil"
"github.com/anacrolix/torrent/metainfo"
- "github.com/anacrolix/torrent/util"
)
func init() {
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 {
"strings"
"testing"
+ "github.com/anacrolix/missinggo"
"github.com/bradfitz/iter"
-
- "github.com/anacrolix/torrent/util"
)
var sample = `
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)
}
panic(err)
}
defer c.Close()
- ret := util.AddrIP(c.RemoteAddr())
+ ret := missinggo.AddrIP(c.RemoteAddr())
return ret
}
"strconv"
"sync"
+ "github.com/anacrolix/missinggo"
"github.com/bradfitz/iter"
-
- "github.com/anacrolix/torrent/util"
)
const (
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
}
"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) {
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 {
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
}
+++ /dev/null
-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)
- }
-}
+++ /dev/null
-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")
- }
-}
+++ /dev/null
-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()
- }
-}
"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
if mi == nil {
return
}
- util.CopyExact(ih[:], mi.Info.Hash)
+ missinggo.CopyExact(ih[:], mi.Info.Hash)
ok = true
return
}
e := entity{
TorrentFilePath: fullName,
}
- util.CopyExact(&e.InfoHash, ih)
+ missinggo.CopyExact(&e.InfoHash, ih)
addEntity(e)
case ".magnet":
uris, err := magnetFileURIs(fullName)