]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/tracker.go
IPv6 tracker support
[btrtrc.git] / tracker / tracker.go
1 package tracker
2
3 import (
4         "errors"
5         "net/http"
6         "net/url"
7
8         "github.com/anacrolix/dht/krpc"
9 )
10
11 // Marshalled as binary by the UDP client, so be careful making changes.
12 type AnnounceRequest struct {
13         InfoHash   [20]byte
14         PeerId     [20]byte
15         Downloaded int64
16         Left       uint64
17         Uploaded   int64
18         // Apparently this is optional. None can be used for announces done at
19         // regular intervals.
20         Event     AnnounceEvent
21         IPAddress int32
22         Key       int32
23         NumWant   int32 // How many peer addresses are desired. -1 for default.
24         Port      uint16
25 } // 82 bytes
26
27 type AnnounceResponse struct {
28         Interval int32 // Minimum seconds the local peer should wait before next announce.
29         Leechers int32
30         Seeders  int32
31         Peers    []Peer
32 }
33
34 type AnnounceEvent int32
35
36 func (e AnnounceEvent) String() string {
37         // See BEP 3, "event".
38         return []string{"empty", "completed", "started", "stopped"}[e]
39 }
40
41 const (
42         None      AnnounceEvent = iota
43         Completed               // The local peer just completed the torrent.
44         Started                 // The local peer has just resumed this torrent.
45         Stopped                 // The local peer is leaving the swarm.
46 )
47
48 var (
49         ErrBadScheme = errors.New("unknown scheme")
50 )
51
52 type Announce struct {
53         TrackerUrl string
54         Request    AnnounceRequest
55         HostHeader string
56         UserAgent  string
57         HttpClient *http.Client
58         UdpNetwork string
59         ClientIp4  krpc.NodeAddr
60         ClientIp6  krpc.NodeAddr
61 }
62
63 // In an FP language with currying, what order what you put these params?
64
65 func (me Announce) Do() (res AnnounceResponse, err error) {
66         _url, err := url.Parse(me.TrackerUrl)
67         if err != nil {
68                 return
69         }
70         switch _url.Scheme {
71         case "http", "https":
72                 return announceHTTP(me, _url)
73         case "udp", "udp4", "udp6":
74                 return announceUDP(me, _url)
75         default:
76                 err = ErrBadScheme
77                 return
78         }
79 }