2014-07-03 23:41:46 +08:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2014-08-21 19:07:44 +08:00
|
|
|
"encoding"
|
|
|
|
"encoding/binary"
|
2015-08-17 17:52:47 +08:00
|
|
|
"errors"
|
2015-10-23 09:41:45 +08:00
|
|
|
"fmt"
|
2015-08-06 07:01:38 +08:00
|
|
|
"net"
|
2015-08-17 17:52:47 +08:00
|
|
|
|
|
|
|
"github.com/bradfitz/iter"
|
2014-11-17 03:05:19 +08:00
|
|
|
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2014-07-03 23:41:46 +08:00
|
|
|
)
|
|
|
|
|
2015-08-17 17:52:47 +08:00
|
|
|
// Concatenated 6-byte peer addresses.
|
|
|
|
type CompactIPv4Peers []CompactPeer
|
|
|
|
|
|
|
|
var (
|
|
|
|
// This allows bencode.Unmarshal to do better than a string or []byte.
|
|
|
|
_ bencode.Unmarshaler = &CompactIPv4Peers{}
|
|
|
|
_ encoding.BinaryMarshaler = CompactIPv4Peers{}
|
|
|
|
)
|
2014-07-03 23:41:46 +08:00
|
|
|
|
2015-08-17 17:52:47 +08:00
|
|
|
// This allows bencode.Unmarshal to do better than a string or []byte.
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cps *CompactIPv4Peers) UnmarshalBencode(b []byte) (err error) {
|
2015-08-17 17:52:47 +08:00
|
|
|
var bb []byte
|
|
|
|
err = bencode.Unmarshal(b, &bb)
|
2014-07-03 23:41:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
*cps, err = UnmarshalIPv4CompactPeers(bb)
|
2014-07-03 23:41:46 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cps CompactIPv4Peers) MarshalBinary() (ret []byte, err error) {
|
|
|
|
ret = make([]byte, len(cps)*6)
|
|
|
|
for i, cp := range cps {
|
2015-08-17 17:52:47 +08:00
|
|
|
copy(ret[6*i:], cp.IP.To4())
|
|
|
|
binary.BigEndian.PutUint16(ret[6*i+4:], uint16(cp.Port))
|
2015-08-06 07:01:38 +08:00
|
|
|
}
|
|
|
|
return
|
2015-03-27 12:12:15 +08:00
|
|
|
}
|
|
|
|
|
2015-08-17 17:52:47 +08:00
|
|
|
// Represents peer address in either IPv6 or IPv4 form.
|
2014-08-21 19:07:44 +08:00
|
|
|
type CompactPeer struct {
|
2015-08-06 07:01:38 +08:00
|
|
|
IP net.IP
|
2015-08-17 17:52:47 +08:00
|
|
|
Port int
|
2014-08-21 19:07:44 +08:00
|
|
|
}
|
|
|
|
|
2015-10-23 09:41:45 +08:00
|
|
|
var (
|
|
|
|
_ bencode.Marshaler = &CompactPeer{}
|
|
|
|
_ bencode.Unmarshaler = &CompactPeer{}
|
|
|
|
)
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cp CompactPeer) MarshalBencode() (ret []byte, err error) {
|
|
|
|
ip := cp.IP
|
2015-10-23 09:41:45 +08:00
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
ip = ip4
|
|
|
|
}
|
|
|
|
ret = make([]byte, len(ip)+2)
|
|
|
|
copy(ret, ip)
|
2016-04-19 12:11:11 +08:00
|
|
|
binary.BigEndian.PutUint16(ret[len(ip):], uint16(cp.Port))
|
2015-10-23 09:41:45 +08:00
|
|
|
return bencode.Marshal(ret)
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cp *CompactPeer) UnmarshalBinary(b []byte) error {
|
2015-08-06 07:01:38 +08:00
|
|
|
switch len(b) {
|
|
|
|
case 18:
|
2016-04-19 12:11:11 +08:00
|
|
|
cp.IP = make([]byte, 16)
|
2015-08-06 07:01:38 +08:00
|
|
|
case 6:
|
2016-04-19 12:11:11 +08:00
|
|
|
cp.IP = make([]byte, 4)
|
2015-08-06 07:01:38 +08:00
|
|
|
default:
|
2015-10-23 09:41:45 +08:00
|
|
|
return fmt.Errorf("bad compact peer string: %q", b)
|
2014-11-17 03:05:19 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
copy(cp.IP, b)
|
|
|
|
b = b[len(cp.IP):]
|
|
|
|
cp.Port = int(binary.BigEndian.Uint16(b))
|
2015-08-17 17:52:47 +08:00
|
|
|
return nil
|
2014-08-21 19:07:44 +08:00
|
|
|
}
|
2015-08-06 07:01:38 +08:00
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cp *CompactPeer) UnmarshalBencode(b []byte) (err error) {
|
2015-10-23 09:41:45 +08:00
|
|
|
var _b []byte
|
|
|
|
err = bencode.Unmarshal(b, &_b)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
return cp.UnmarshalBinary(_b)
|
2015-10-23 09:41:45 +08:00
|
|
|
}
|
|
|
|
|
2015-08-17 17:52:47 +08:00
|
|
|
func UnmarshalIPv4CompactPeers(b []byte) (ret []CompactPeer, err error) {
|
|
|
|
if len(b)%6 != 0 {
|
|
|
|
err = errors.New("bad length")
|
2015-08-06 07:01:38 +08:00
|
|
|
return
|
|
|
|
}
|
2015-08-17 17:52:47 +08:00
|
|
|
num := len(b) / 6
|
|
|
|
ret = make([]CompactPeer, num)
|
|
|
|
for i := range iter.N(num) {
|
|
|
|
off := i * 6
|
|
|
|
err = ret[i].UnmarshalBinary(b[off : off+6])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-08-06 07:01:38 +08:00
|
|
|
return
|
|
|
|
}
|