19 metainfo "github.com/nsf/libtorgo/torrent"
21 "bitbucket.org/anacrolix/go.torrent/peer_protocol"
22 "bitbucket.org/anacrolix/go.torrent/tracker"
23 _ "bitbucket.org/anacrolix/go.torrent/tracker/udp"
26 // Currently doesn't really queue, but should in the future.
27 func (cl *Client) queuePieceCheck(t *Torrent, pieceIndex peer_protocol.Integer) {
28 piece := t.Pieces[pieceIndex]
29 if piece.QueuedForHash {
32 piece.QueuedForHash = true
33 go cl.verifyPiece(t, pieceIndex)
36 func (cl *Client) PrioritizeDataRegion(ih InfoHash, off, len_ int64) error {
41 return errors.New("no such active torrent")
43 newPriorities := make([]Request, 0, (len_+chunkSize-1)/chunkSize)
45 req, ok := t.offsetRequest(off)
47 return errors.New("bad offset")
49 reqOff := t.requestOffset(req)
50 // Gain the alignment adjustment.
52 // Lose the length of this block.
53 len_ -= int64(req.Length)
54 off = reqOff + int64(req.Length)
55 if !t.wantPiece(int(req.Index)) {
58 newPriorities = append(newPriorities, req)
60 if len(newPriorities) == 0 {
63 if t.Priorities == nil {
64 t.Priorities = list.New()
66 t.Priorities.PushFront(newPriorities[0])
67 for _, req := range newPriorities[1:] {
68 t.Priorities.PushBack(req)
70 for _, cn := range t.Conns {
71 cl.replenishConnRequests(t, cn)
76 type DataSpec struct {
94 torrents map[InfoHash]*Torrent
95 dataWaiter chan struct{}
98 func (cl *Client) TorrentReadAt(ih InfoHash, off int64, p []byte) (n int, err error) {
103 err = errors.New("unknown torrent")
106 index := peer_protocol.Integer(off / t.MetaInfo.PieceLength)
107 // Reading outside the bounds of a file is an error.
112 if int(index) >= len(t.Pieces) {
116 piece := t.Pieces[index]
117 if !piece.EverHashed {
118 cl.queuePieceCheck(t, index)
121 err = ErrDataNotReady
124 pieceOff := peer_protocol.Integer(off % int64(t.PieceLength(0)))
125 high := int(t.PieceLength(index) - pieceOff)
129 for cs, _ := range piece.PendingChunkSpecs {
130 chunkOff := int64(pieceOff) - int64(cs.Begin)
131 if chunkOff >= int64(t.PieceLength(index)) {
134 if 0 <= chunkOff && chunkOff < int64(cs.Length) {
135 // read begins in a pending chunk
136 err = ErrDataNotReady
139 // pending chunk caps available data
140 if chunkOff < 0 && int64(len(p)) > -chunkOff {
144 return t.Data.ReadAt(p, off)
147 func (c *Client) Start() {
150 c.torrents = make(map[InfoHash]*Torrent)
151 if c.HalfOpenLimit == 0 {
154 o := copy(c.PeerId[:], BEP20)
155 _, err := rand.Read(c.PeerId[o:])
157 panic("error generating peer id")
159 c.quit = make(chan struct{})
160 if c.Listener != nil {
161 go c.acceptConnections()
165 func (cl *Client) stopped() bool {
174 func (me *Client) Stop() {
178 for _, t := range me.torrents {
179 for _, c := range t.Conns {
186 func (cl *Client) acceptConnections() {
188 conn, err := cl.Listener.Accept()
199 if err := cl.runConnection(conn, nil); err != nil {
206 func (me *Client) torrent(ih InfoHash) *Torrent {
207 for _, t := range me.torrents {
208 if t.InfoHash == ih {
215 func (me *Client) initiateConn(peer Peer, torrent *Torrent) {
216 if peer.Id == me.PeerId {
221 addr := &net.TCPAddr{
225 conn, err := net.DialTimeout(addr.Network(), addr.String(), dialTimeout)
230 if me.halfOpen == 0 {
237 if netOpErr, ok := err.(*net.OpError); ok {
238 if netOpErr.Timeout() {
241 switch netOpErr.Err {
242 case syscall.ECONNREFUSED, syscall.EHOSTUNREACH:
247 log.Printf("error connecting to peer: %s %#v", err, err)
250 log.Printf("connected to %s", conn.RemoteAddr())
251 err = me.runConnection(conn, torrent)
258 func (me *Client) runConnection(sock net.Conn, torrent *Torrent) (err error) {
263 write: make(chan []byte),
264 post: make(chan encoding.BinaryMarshaler),
267 // There's a lock and deferred unlock later in this function. The
268 // client will not be locked when this deferred is invoked.
274 go conn.writeOptimizer()
275 conn.post <- peer_protocol.Bytes(peer_protocol.Protocol)
276 conn.post <- peer_protocol.Bytes("\x00\x00\x00\x00\x00\x00\x00\x00")
278 conn.post <- peer_protocol.Bytes(torrent.InfoHash[:])
279 conn.post <- peer_protocol.Bytes(me.PeerId[:])
282 _, err = io.ReadFull(conn.Socket, b[:])
287 err = fmt.Errorf("when reading protocol and extensions: %s", err)
290 if string(b[:20]) != peer_protocol.Protocol {
291 err = fmt.Errorf("wrong protocol: %#v", string(b[:20]))
294 if 8 != copy(conn.PeerExtensions[:], b[20:]) {
297 // log.Printf("peer extensions: %#v", string(conn.PeerExtensions[:]))
298 var infoHash [20]byte
299 _, err = io.ReadFull(conn.Socket, infoHash[:])
301 return fmt.Errorf("reading peer info hash: %s", err)
303 _, err = io.ReadFull(conn.Socket, conn.PeerId[:])
305 return fmt.Errorf("reading peer id: %s", err)
308 torrent = me.torrent(infoHash)
312 conn.post <- peer_protocol.Bytes(torrent.InfoHash[:])
313 conn.post <- peer_protocol.Bytes(me.PeerId[:])
317 if !me.addConnection(torrent, conn) {
320 if torrent.haveAnyPieces() {
321 conn.Post(peer_protocol.Message{
322 Type: peer_protocol.Bitfield,
323 Bitfield: torrent.bitfield(),
326 err = me.connectionLoop(torrent, conn)
328 err = fmt.Errorf("during Connection loop: %s", err)
330 me.dropConnection(torrent, conn)
334 func (me *Client) peerGotPiece(torrent *Torrent, conn *Connection, piece int) {
335 if conn.PeerPieces == nil {
336 conn.PeerPieces = make([]bool, len(torrent.Pieces))
338 conn.PeerPieces[piece] = true
339 if torrent.wantPiece(piece) {
340 me.replenishConnRequests(torrent, conn)
344 func (me *Client) peerUnchoked(torrent *Torrent, conn *Connection) {
345 me.replenishConnRequests(torrent, conn)
348 func (me *Client) connectionLoop(torrent *Torrent, conn *Connection) error {
349 decoder := peer_protocol.Decoder{
350 R: bufio.NewReader(conn.Socket),
351 MaxLength: 256 * 1024,
355 // TODO: Can this be allocated on the stack?
356 msg := new(peer_protocol.Message)
357 err := decoder.Decode(msg)
360 if me.stopped() || err == io.EOF {
369 case peer_protocol.Choke:
370 conn.PeerChoked = true
372 case peer_protocol.Unchoke:
373 conn.PeerChoked = false
374 me.peerUnchoked(torrent, conn)
375 case peer_protocol.Interested:
376 conn.PeerInterested = true
377 // TODO: This should be done from a dedicated unchoking routine.
379 case peer_protocol.NotInterested:
380 conn.PeerInterested = false
381 case peer_protocol.Have:
382 me.peerGotPiece(torrent, conn, int(msg.Index))
383 case peer_protocol.Request:
384 if conn.PeerRequests == nil {
385 conn.PeerRequests = make(map[Request]struct{}, maxRequests)
389 ChunkSpec: ChunkSpec{msg.Begin, msg.Length},
391 conn.PeerRequests[request] = struct{}{}
392 // TODO: Requests should be satisfied from a dedicated upload routine.
393 p := make([]byte, msg.Length)
394 n, err := torrent.Data.ReadAt(p, int64(torrent.PieceLength(0))*int64(msg.Index)+int64(msg.Begin))
396 return fmt.Errorf("reading torrent data to serve request %s: %s", request, err)
398 if n != int(msg.Length) {
399 return fmt.Errorf("bad request: %s", msg)
401 conn.Post(peer_protocol.Message{
402 Type: peer_protocol.Piece,
407 case peer_protocol.Bitfield:
408 if len(msg.Bitfield) < len(torrent.Pieces) {
409 err = errors.New("received invalid bitfield")
412 if conn.PeerPieces != nil {
413 err = errors.New("received unexpected bitfield")
416 conn.PeerPieces = msg.Bitfield[:len(torrent.Pieces)]
417 for index, has := range conn.PeerPieces {
419 me.peerGotPiece(torrent, conn, index)
422 case peer_protocol.Piece:
423 request_ := Request{msg.Index, ChunkSpec{msg.Begin, peer_protocol.Integer(len(msg.Piece))}}
424 if _, ok := conn.Requests[request_]; !ok {
425 err = fmt.Errorf("unexpected piece: %s", request_)
428 delete(conn.Requests, request_)
429 err = me.downloadedChunk(torrent, msg)
431 log.Printf("received unknown message type: %#v", msg.Type)
436 me.replenishConnRequests(torrent, conn)
440 func (me *Client) dropConnection(torrent *Torrent, conn *Connection) {
442 for i0, c := range torrent.Conns {
446 i1 := len(torrent.Conns) - 1
448 torrent.Conns[i0] = torrent.Conns[i1]
450 torrent.Conns = torrent.Conns[:i1]
453 panic("no such Connection")
456 func (me *Client) addConnection(t *Torrent, c *Connection) bool {
457 for _, c0 := range t.Conns {
458 if c.PeerId == c0.PeerId {
459 log.Printf("%s and %s have the same ID: %s", c.Socket.RemoteAddr(), c0.Socket.RemoteAddr(), c.PeerId)
463 t.Conns = append(t.Conns, c)
467 func (me *Client) openNewConns() {
468 for _, t := range me.torrents {
469 for len(t.Peers) != 0 {
470 if me.halfOpen >= me.HalfOpenLimit {
474 t.Peers = t.Peers[1:]
475 me.initiateConn(p, t)
480 func (me *Client) AddPeers(infoHash InfoHash, peers []Peer) error {
482 t := me.torrent(infoHash)
484 return errors.New("no such torrent")
486 t.Peers = append(t.Peers, peers...)
492 // Prepare a Torrent without any attachment to a Client. That means we can
493 // initialize fields all fields that don't require the Client without locking
495 func newTorrent(metaInfo *metainfo.MetaInfo, dataDir string) (torrent *Torrent, err error) {
497 InfoHash: BytesInfoHash(metaInfo.InfoHash),
500 torrent.Data, err = mmapTorrentData(metaInfo, dataDir)
504 for offset := 0; offset < len(metaInfo.Pieces); offset += PieceHash.Size() {
505 hash := metaInfo.Pieces[offset : offset+PieceHash.Size()]
506 if len(hash) != PieceHash.Size() {
507 err = errors.New("bad piece hash in metainfo")
511 copyHashSum(piece.Hash[:], hash)
512 torrent.Pieces = append(torrent.Pieces, piece)
513 torrent.pendAllChunkSpecs(peer_protocol.Integer(len(torrent.Pieces) - 1))
515 torrent.Trackers = make([][]tracker.Client, len(metaInfo.AnnounceList))
516 for tierIndex := range metaInfo.AnnounceList {
517 tier := torrent.Trackers[tierIndex]
518 for _, url := range metaInfo.AnnounceList[tierIndex] {
519 tr, err := tracker.New(url)
524 tier = append(tier, tr)
526 // The trackers within each tier must be shuffled before use.
527 // http://stackoverflow.com/a/12267471/149482
528 // http://www.bittorrent.org/beps/bep_0012.html#order-of-processing
529 for i := range tier {
530 j := mathRand.Intn(i + 1)
531 tier[i], tier[j] = tier[j], tier[i]
533 torrent.Trackers[tierIndex] = tier
538 func (me *Client) AddTorrent(metaInfo *metainfo.MetaInfo) error {
539 torrent, err := newTorrent(metaInfo, me.DataDir)
545 if _, ok := me.torrents[torrent.InfoHash]; ok {
546 return torrent.Close()
548 me.torrents[torrent.InfoHash] = torrent
549 if !me.DisableTrackers {
550 go me.announceTorrent(torrent)
552 for i := range torrent.Pieces {
553 me.queuePieceCheck(torrent, peer_protocol.Integer(i))
558 func (cl *Client) listenerAnnouncePort() (port int16) {
564 switch data := addr.(type) {
566 return int16(data.Port)
568 return int16(data.Port)
570 log.Printf("unknown listener addr type: %T", addr)
575 func (cl *Client) announceTorrent(t *Torrent) {
576 req := tracker.AnnounceRequest{
577 Event: tracker.Started,
579 Port: cl.listenerAnnouncePort(),
581 req.PeerId = cl.PeerId
582 req.InfoHash = t.InfoHash
585 for _, tier := range t.Trackers {
586 for trIndex, tr := range tier {
587 if err := tr.Connect(); err != nil {
591 resp, err := tr.Announce(&req)
597 for _, peer := range resp.Peers {
598 peers = append(peers, Peer{
603 if err := cl.AddPeers(t.InfoHash, peers); err != nil {
607 log.Printf("%d new peers from %s", len(peers), "TODO")
608 tier[0], tier[trIndex] = tier[trIndex], tier[0]
609 time.Sleep(time.Second * time.Duration(resp.Interval))
613 time.Sleep(time.Second)
617 func (cl *Client) allTorrentsCompleted() bool {
618 for _, t := range cl.torrents {
619 if !t.haveAllPieces() {
626 func (me *Client) WaitAll() {
628 for !me.allTorrentsCompleted() {
634 func (me *Client) replenishConnRequests(torrent *Torrent, conn *Connection) {
635 requestHeatMap := torrent.requestHeat()
636 addRequest := func(req Request) (again bool) {
637 piece := torrent.Pieces[req.Index]
639 // We can't be sure we want this.
642 if piece.Complete() {
643 // We already have this.
646 if requestHeatMap[req] > 0 {
647 // We've already requested this.
650 return conn.Request(req)
652 // First request prioritized chunks.
653 if torrent.Priorities != nil {
654 for e := torrent.Priorities.Front(); e != nil; e = e.Next() {
655 if !addRequest(e.Value.(Request)) {
660 // Then finish off incomplete pieces in order of bytes remaining.
661 for _, index := range torrent.piecesByPendingBytesDesc() {
662 if torrent.PieceNumPendingBytes(index) == torrent.PieceLength(index) {
665 for chunkSpec := range torrent.Pieces[index].PendingChunkSpecs {
666 if !addRequest(Request{index, chunkSpec}) {
671 if len(conn.Requests) == 0 {
672 conn.SetInterested(false)
676 func (me *Client) downloadedChunk(torrent *Torrent, msg *peer_protocol.Message) (err error) {
677 request := Request{msg.Index, ChunkSpec{msg.Begin, peer_protocol.Integer(len(msg.Piece))}}
678 if _, ok := torrent.Pieces[request.Index].PendingChunkSpecs[request.ChunkSpec]; !ok {
679 log.Printf("got unnecessary chunk: %s", request)
682 err = torrent.WriteChunk(int(msg.Index), int64(msg.Begin), msg.Piece)
686 delete(torrent.Pieces[request.Index].PendingChunkSpecs, request.ChunkSpec)
687 if len(torrent.Pieces[request.Index].PendingChunkSpecs) == 0 {
688 me.queuePieceCheck(torrent, request.Index)
690 var next *list.Element
691 for e := torrent.Priorities.Front(); e != nil; e = next {
693 if e.Value.(Request) == request {
694 torrent.Priorities.Remove(e)
697 me.dataReady(DataSpec{torrent.InfoHash, request})
701 func (cl *Client) dataReady(ds DataSpec) {
702 if cl.dataWaiter != nil {
708 func (cl *Client) DataWaiter() <-chan struct{} {
711 if cl.dataWaiter == nil {
712 cl.dataWaiter = make(chan struct{})
717 func (me *Client) pieceHashed(t *Torrent, piece peer_protocol.Integer, correct bool) {
721 p.PendingChunkSpecs = nil
722 log.Printf("got piece %d, (%d/%d)", piece, t.NumPiecesCompleted(), t.NumPieces())
723 var next *list.Element
724 if t.Priorities != nil {
725 for e := t.Priorities.Front(); e != nil; e = next {
727 if e.Value.(Request).Index == piece {
728 t.Priorities.Remove(e)
732 me.dataReady(DataSpec{
735 peer_protocol.Integer(piece),
736 ChunkSpec{0, peer_protocol.Integer(t.PieceLength(piece))},
740 if len(p.PendingChunkSpecs) == 0 {
741 t.pendAllChunkSpecs(piece)
744 for _, conn := range t.Conns {
746 conn.Post(peer_protocol.Message{
747 Type: peer_protocol.Have,
748 Index: peer_protocol.Integer(piece),
750 // TODO: Cancel requests for this piece.
752 if conn.PeerHasPiece(piece) {
753 me.replenishConnRequests(t, conn)
760 func (cl *Client) verifyPiece(t *Torrent, index peer_protocol.Integer) {
767 p.QueuedForHash = false
769 sum := t.HashPiece(index)
772 cl.pieceHashed(t, index, sum == p.Hash)
776 func (me *Client) Torrents() (ret []*Torrent) {
778 for _, t := range me.torrents {