]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/udp.go
db486948a675447208c388cf18bbb488495fece2
[btrtrc.git] / tracker / udp.go
1 package tracker
2
3 import (
4         "context"
5         "encoding/binary"
6
7         trHttp "github.com/anacrolix/torrent/tracker/http"
8         "github.com/anacrolix/torrent/tracker/udp"
9 )
10
11 type udpClient struct {
12         cl         *udp.ConnClient
13         requestUri string
14 }
15
16 func (c *udpClient) Close() error {
17         return c.cl.Close()
18 }
19
20 func (c *udpClient) Announce(ctx context.Context, req AnnounceRequest, opts trHttp.AnnounceOpt) (res AnnounceResponse, err error) {
21         if req.IPAddress == 0 && opts.ClientIp4 != nil {
22                 // I think we're taking bytes in big-endian order (all IPs), and writing it to a natively
23                 // ordered uint32. This will be correctly ordered when written back out by the UDP client
24                 // later. I'm ignoring the fact that IPv6 announces shouldn't have an IP address, we have a
25                 // perfectly good IPv4 address.
26                 req.IPAddress = binary.BigEndian.Uint32(opts.ClientIp4.To4())
27         }
28         h, nas, err := c.cl.Announce(ctx, req, udp.Options{RequestUri: c.requestUri})
29         if err != nil {
30                 return
31         }
32         res.Interval = h.Interval
33         res.Leechers = h.Leechers
34         res.Seeders = h.Seeders
35         for _, cp := range nas.NodeAddrs() {
36                 res.Peers = append(res.Peers, trHttp.Peer{}.FromNodeAddr(cp))
37         }
38         return
39 }