]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/udp/protocol.go
Make UDP tracker connection ID unsigned
[btrtrc.git] / tracker / udp / protocol.go
1 package udp
2
3 import (
4         "bytes"
5         "encoding/binary"
6         "io"
7 )
8
9 type Action int32
10
11 const (
12         ActionConnect Action = iota
13         ActionAnnounce
14         ActionScrape
15         ActionError
16 )
17
18 const ConnectRequestConnectionId = 0x41727101980
19
20 const (
21         // BEP 41
22         optionTypeEndOfOptions = 0
23         optionTypeNOP          = 1
24         optionTypeURLData      = 2
25 )
26
27 type TransactionId = int32
28
29 type ConnectionId = uint64
30
31 type ConnectionRequest struct {
32         ConnectionId  ConnectionId
33         Action        Action
34         TransactionId TransactionId
35 }
36
37 type ConnectionResponse struct {
38         ConnectionId ConnectionId
39 }
40
41 type ResponseHeader struct {
42         Action        Action
43         TransactionId TransactionId
44 }
45
46 type RequestHeader struct {
47         ConnectionId  ConnectionId
48         Action        Action
49         TransactionId TransactionId
50 } // 16 bytes
51
52 type AnnounceResponseHeader struct {
53         Interval int32
54         Leechers int32
55         Seeders  int32
56 }
57
58 type InfoHash = [20]byte
59
60 func marshal(data interface{}) (b []byte, err error) {
61         var buf bytes.Buffer
62         err = Write(&buf, data)
63         b = buf.Bytes()
64         return
65 }
66
67 func mustMarshal(data interface{}) []byte {
68         b, err := marshal(data)
69         if err != nil {
70                 panic(err)
71         }
72         return b
73 }
74
75 // This is for fixed-size, builtin types only I think.
76 func Write(w io.Writer, data interface{}) error {
77         return binary.Write(w, binary.BigEndian, data)
78 }
79
80 func Read(r io.Reader, data interface{}) error {
81         return binary.Read(r, binary.BigEndian, data)
82 }