Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bootstrap/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"os"

"github.com/anacrolix/torrent/util"
"github.com/mh-cbon/dht/util"
)

// DataSave is a type to store known ip and contacts of an existing dht session.
Expand Down
2 changes: 1 addition & 1 deletion dht/bep05.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"net"

"github.com/anacrolix/torrent/util"
"github.com/mh-cbon/dht/util"
"github.com/mh-cbon/dht/kmsg"
"github.com/mh-cbon/dht/rpc"
"github.com/mh-cbon/dht/socket"
Expand Down
2 changes: 1 addition & 1 deletion dht/boostrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net"
"os"

"github.com/anacrolix/torrent/util"
"github.com/mh-cbon/dht/util"
"github.com/mh-cbon/dht/security"
)

Expand Down
2 changes: 1 addition & 1 deletion kmsg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package kmsg
import (
"fmt"

"github.com/anacrolix/torrent/util"
"github.com/mh-cbon/dht/util"
)

// Msg represents messages that nodes in the network send to each other as specified by the protocol.
Expand Down
2 changes: 1 addition & 1 deletion kmsg/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/util"
"github.com/mh-cbon/dht/util"
)

func testMarshalUnmarshalMsg(t *testing.T, m Msg, expected string) {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"time"

"github.com/anacrolix/torrent/iplist"
"github.com/anacrolix/torrent/util"
"github.com/mh-cbon/dht/util"
"github.com/mh-cbon/dht/bootstrap"
"github.com/mh-cbon/dht/bucket"
"github.com/mh-cbon/dht/crypto"
Expand Down
2 changes: 1 addition & 1 deletion rpc/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"sync"

"github.com/anacrolix/torrent/util"
"github.com/mh-cbon/dht/util"
"github.com/mh-cbon/dht/bootstrap"
"github.com/mh-cbon/dht/bucket"
"github.com/mh-cbon/dht/kmsg"
Expand Down
2 changes: 1 addition & 1 deletion socket/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/anacrolix/torrent/iplist"
"github.com/anacrolix/torrent/util"
"github.com/mh-cbon/dht/util"
"github.com/mh-cbon/dht/kmsg"
"github.com/mh-cbon/dht/logger"
"github.com/mh-cbon/dht/stats"
Expand Down
105 changes: 105 additions & 0 deletions util/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package util

import (
"encoding"
"encoding/binary"
"errors"
"fmt"
"net"

"github.com/bradfitz/iter"

"github.com/anacrolix/torrent/bencode"
)

// 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{}
)

// This allows bencode.Unmarshal to do better than a string or []byte.
func (cps *CompactIPv4Peers) UnmarshalBencode(b []byte) (err error) {
var bb []byte
err = bencode.Unmarshal(b, &bb)
if err != nil {
return
}
*cps, err = UnmarshalIPv4CompactPeers(bb)
return
}

func (cps CompactIPv4Peers) MarshalBinary() (ret []byte, err error) {
ret = make([]byte, len(cps)*6)
for i, cp := range cps {
copy(ret[6*i:], cp.IP.To4())
binary.BigEndian.PutUint16(ret[6*i+4:], uint16(cp.Port))
}
return
}

// Represents peer address in either IPv6 or IPv4 form.
type CompactPeer struct {
IP net.IP
Port int
}

var (
_ bencode.Marshaler = &CompactPeer{}
_ bencode.Unmarshaler = &CompactPeer{}
)

func (cp CompactPeer) MarshalBencode() (ret []byte, err error) {
ip := cp.IP
if ip4 := ip.To4(); ip4 != nil {
ip = ip4
}
ret = make([]byte, len(ip)+2)
copy(ret, ip)
binary.BigEndian.PutUint16(ret[len(ip):], uint16(cp.Port))
return bencode.Marshal(ret)
}

func (cp *CompactPeer) UnmarshalBinary(b []byte) error {
switch len(b) {
case 18:
cp.IP = make([]byte, 16)
case 6:
cp.IP = make([]byte, 4)
default:
return fmt.Errorf("bad compact peer string: %q", b)
}
copy(cp.IP, b)
b = b[len(cp.IP):]
cp.Port = int(binary.BigEndian.Uint16(b))
return nil
}

func (cp *CompactPeer) UnmarshalBencode(b []byte) (err error) {
var _b []byte
err = bencode.Unmarshal(b, &_b)
if err != nil {
return
}
return cp.UnmarshalBinary(_b)
}

func UnmarshalIPv4CompactPeers(b []byte) (ret []CompactPeer, err error) {
if len(b)%6 != 0 {
err = errors.New("bad length")
return
}
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
}
}
return
}