]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/udp/protocol.go
4a8dc6681512f14710023f5992647b001b124ec4
[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         ConnectRequestConnectionId = 0x41727101980
18
19         // BEP 41
20         optionTypeEndOfOptions = 0
21         optionTypeNOP          = 1
22         optionTypeURLData      = 2
23 )
24
25 type TransactionId = int32
26
27 type ConnectionId = int64
28
29 type ConnectionRequest struct {
30         ConnectionId  ConnectionId
31         Action        Action
32         TransactionId TransactionId
33 }
34
35 type ConnectionResponse struct {
36         ConnectionId ConnectionId
37 }
38
39 type ResponseHeader struct {
40         Action        Action
41         TransactionId TransactionId
42 }
43
44 type RequestHeader struct {
45         ConnectionId  ConnectionId
46         Action        Action
47         TransactionId TransactionId
48 } // 16 bytes
49
50 type AnnounceResponseHeader struct {
51         Interval int32
52         Leechers int32
53         Seeders  int32
54 }
55
56 type InfoHash = [20]byte
57
58 func marshal(data interface{}) (b []byte, err error) {
59         var buf bytes.Buffer
60         err = binary.Write(&buf, binary.BigEndian, data)
61         b = buf.Bytes()
62         return
63 }
64
65 func mustMarshal(data interface{}) []byte {
66         b, err := marshal(data)
67         if err != nil {
68                 panic(err)
69         }
70         return b
71 }
72
73 func Write(w io.Writer, data interface{}) error {
74         return binary.Write(w, binary.BigEndian, data)
75 }
76
77 func Read(r io.Reader, data interface{}) error {
78         return binary.Read(r, binary.BigEndian, data)
79 }