2016-08-03 09:11:52 +02:00
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2016-08-05 07:47:04 +02:00
|
|
|
"io"
|
2016-08-03 09:11:52 +02:00
|
|
|
"time"
|
|
|
|
|
2016-08-17 03:42:08 +02:00
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
2016-08-03 09:11:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// WriteError writes the failure reason as a null-terminated string.
|
2016-08-05 07:47:04 +02:00
|
|
|
func WriteError(w io.Writer, txID []byte, err error) {
|
2016-08-03 09:11:52 +02:00
|
|
|
// If the client wasn't at fault, acknowledge it.
|
|
|
|
if _, ok := err.(bittorrent.ClientError); !ok {
|
|
|
|
err = fmt.Errorf("internal error occurred: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2016-09-03 23:09:00 +02:00
|
|
|
buf := newBuffer()
|
|
|
|
writeHeader(buf, txID, errorActionID)
|
2016-08-03 09:11:52 +02:00
|
|
|
buf.WriteString(err.Error())
|
|
|
|
buf.WriteRune('\000')
|
2016-08-05 07:47:04 +02:00
|
|
|
w.Write(buf.Bytes())
|
2016-09-03 23:09:00 +02:00
|
|
|
buf.free()
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteAnnounce encodes an announce response according to BEP 15.
|
2016-09-02 22:42:01 +02:00
|
|
|
// The peers returned will be resp.IPv6Peers or resp.IPv4Peers, depending on
|
2018-02-13 11:12:48 +01:00
|
|
|
// whether v6Peers is set.
|
|
|
|
// If v6Action is set, the action will be 4, according to
|
|
|
|
// https://web.archive.org/web/20170503181830/http://opentracker.blog.h3q.com/2007/12/28/the-ipv6-situation/
|
|
|
|
func WriteAnnounce(w io.Writer, txID []byte, resp *bittorrent.AnnounceResponse, v6Action, v6Peers bool) {
|
2016-09-03 23:09:00 +02:00
|
|
|
buf := newBuffer()
|
2016-08-18 01:04:26 +02:00
|
|
|
|
2018-02-13 11:12:48 +01:00
|
|
|
if v6Action {
|
2016-09-03 23:09:00 +02:00
|
|
|
writeHeader(buf, txID, announceV6ActionID)
|
2016-09-02 22:42:01 +02:00
|
|
|
} else {
|
2016-09-03 23:09:00 +02:00
|
|
|
writeHeader(buf, txID, announceActionID)
|
2016-09-02 22:42:01 +02:00
|
|
|
}
|
2016-09-03 23:09:00 +02:00
|
|
|
binary.Write(buf, binary.BigEndian, uint32(resp.Interval/time.Second))
|
2016-09-08 16:46:16 +02:00
|
|
|
binary.Write(buf, binary.BigEndian, resp.Incomplete)
|
|
|
|
binary.Write(buf, binary.BigEndian, resp.Complete)
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-09-02 22:42:01 +02:00
|
|
|
peers := resp.IPv4Peers
|
2018-02-13 11:12:48 +01:00
|
|
|
if v6Peers {
|
2016-09-02 22:42:01 +02:00
|
|
|
peers = resp.IPv6Peers
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, peer := range peers {
|
2016-11-28 20:55:04 +01:00
|
|
|
buf.Write(peer.IP.IP)
|
2016-09-03 23:09:00 +02:00
|
|
|
binary.Write(buf, binary.BigEndian, peer.Port)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
2016-08-18 01:04:26 +02:00
|
|
|
|
|
|
|
w.Write(buf.Bytes())
|
2016-09-03 23:09:00 +02:00
|
|
|
buf.free()
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteScrape encodes a scrape response according to BEP 15.
|
2016-08-05 07:47:04 +02:00
|
|
|
func WriteScrape(w io.Writer, txID []byte, resp *bittorrent.ScrapeResponse) {
|
2016-09-03 23:09:00 +02:00
|
|
|
buf := newBuffer()
|
2016-08-18 01:04:26 +02:00
|
|
|
|
2016-09-03 23:09:00 +02:00
|
|
|
writeHeader(buf, txID, scrapeActionID)
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
for _, scrape := range resp.Files {
|
2016-09-03 23:09:00 +02:00
|
|
|
binary.Write(buf, binary.BigEndian, scrape.Complete)
|
|
|
|
binary.Write(buf, binary.BigEndian, scrape.Snatches)
|
|
|
|
binary.Write(buf, binary.BigEndian, scrape.Incomplete)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
2016-08-18 01:04:26 +02:00
|
|
|
|
|
|
|
w.Write(buf.Bytes())
|
2016-09-03 23:09:00 +02:00
|
|
|
buf.free()
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteConnectionID encodes a new connection response according to BEP 15.
|
2016-08-05 07:47:04 +02:00
|
|
|
func WriteConnectionID(w io.Writer, txID, connID []byte) {
|
2016-09-03 23:09:00 +02:00
|
|
|
buf := newBuffer()
|
2016-08-18 01:04:26 +02:00
|
|
|
|
2016-09-03 23:09:00 +02:00
|
|
|
writeHeader(buf, txID, connectActionID)
|
2016-08-18 01:04:26 +02:00
|
|
|
buf.Write(connID)
|
|
|
|
|
|
|
|
w.Write(buf.Bytes())
|
2016-09-03 23:09:00 +02:00
|
|
|
buf.free()
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// writeHeader writes the action and transaction ID to the provided response
|
|
|
|
// buffer.
|
2016-08-05 07:47:04 +02:00
|
|
|
func writeHeader(w io.Writer, txID []byte, action uint32) {
|
|
|
|
binary.Write(w, binary.BigEndian, action)
|
|
|
|
w.Write(txID)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|