tracker: Set UDP IPAddress field in announces

This commit is contained in:
Matt Joiner 2018-02-21 13:33:12 +11:00
parent 5a7c4fc135
commit 41b66adad7
2 changed files with 9 additions and 3 deletions

View File

@ -18,7 +18,7 @@ type AnnounceRequest struct {
// Apparently this is optional. None can be used for announces done at
// regular intervals.
Event AnnounceEvent
IPAddress int32
IPAddress uint32
Key int32
NumWant int32 // How many peer addresses are desired. -1 for default.
Port uint16

View File

@ -99,12 +99,18 @@ func (c *udpAnnounce) ipv6() bool {
return rip.To16() != nil && rip.To4() == nil
}
func (c *udpAnnounce) Do(req *AnnounceRequest) (res AnnounceResponse, err error) {
func (c *udpAnnounce) Do(req AnnounceRequest) (res AnnounceResponse, err error) {
err = c.connect()
if err != nil {
return
}
reqURI := c.url.RequestURI()
if c.ipv6() {
// BEP 15
req.IPAddress = 0
} else if req.IPAddress == 0 && c.a.ClientIp4.IP != nil {
req.IPAddress = binary.BigEndian.Uint32(c.a.ClientIp4.IP.To4())
}
// Clearly this limits the request URI to 255 bytes. BEP 41 supports
// longer but I'm not fussed.
options := append([]byte{optionTypeURLData, byte(len(reqURI))}, []byte(reqURI)...)
@ -288,5 +294,5 @@ func announceUDP(opt Announce, _url *url.URL) (AnnounceResponse, error) {
a: &opt,
}
defer ua.Close()
return ua.Do(&opt.Request)
return ua.Do(opt.Request)
}