Commit 221f300f authored by Matt Joiner's avatar Matt Joiner
Browse files

krpc: Don't omit zero ports

This fixes a common error where other implementations ignore our messages because we've not put in a port field, even though they shouldn't use it.
parent b87550d3
......@@ -13,6 +13,7 @@ import (
func main() {
var flags = struct {
Port int
tagflag.StartPos
Infohash [][20]byte
}{}
......@@ -25,7 +26,7 @@ func main() {
wg := sync.WaitGroup{}
addrs := make(map[[20]byte]map[string]struct{}, len(flags.Infohash))
for _, ih := range flags.Infohash {
a, err := s.Announce(ih, 0, true)
a, err := s.Announce(ih, flags.Port, false)
if err != nil {
log.Printf("error announcing %s: %s", ih, err)
continue
......
......@@ -55,7 +55,7 @@ type ServerConfig struct {
// the default handlers.
OnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)
// Called when a peer successfully announces to us.
OnAnnouncePeer func(infoHash metainfo.Hash, peer Peer)
OnAnnouncePeer func(infoHash metainfo.Hash, ip net.IP, port int, portOk bool)
// How long to wait before resending queries that haven't received a
// response. Defaults to a random value between 4.5 and 5.5s.
QueryResendDelay func() time.Duration
......
......@@ -32,7 +32,7 @@ type MsgArgs struct {
InfoHash ID `bencode:"info_hash,omitempty"` // InfoHash of the torrent
Target ID `bencode:"target,omitempty"` // ID of the node sought
Token string `bencode:"token,omitempty"` // Token received from an earlier get_peers query
Port int `bencode:"port,omitempty"` // Senders torrent port
Port *int `bencode:"port,omitempty"` // Sender's torrent port
ImpliedPort bool `bencode:"implied_port,omitempty"` // Use senders apparent DHT port
Want []Want `bencode:"want,omitempty"` // Contains strings like "n4" and "n6" from BEP 32.
NoSeed int `bencode:"noseed,omitempty"` // BEP 33
......@@ -47,7 +47,7 @@ const (
)
type Return struct {
ID ID `bencode:"id"` // ID of the querying node
ID ID `bencode:"id"` // ID of the queried node
Nodes CompactIPv4NodeInfo `bencode:"nodes,omitempty"` // K closest nodes to the requested target
Nodes6 CompactIPv6NodeInfo `bencode:"nodes6,omitempty"` // K closest nodes to the requested target
Token *string `bencode:"token,omitempty"` // Token for future announce_peer
......
......@@ -441,14 +441,17 @@ func (s *Server) handleQuery(source Addr, m krpc.Msg) {
}
expvars.Add("received announce_peer with valid token", 1)
if h := s.config.OnAnnouncePeer; h != nil {
p := Peer{
IP: source.IP(),
Port: args.Port,
var port int
portOk := false
if args.Port != nil {
port = *args.Port
portOk = true
}
if args.ImpliedPort {
p.Port = source.Port()
port = source.Port()
portOk = true
}
go h(metainfo.Hash(args.InfoHash), p)
go h(metainfo.Hash(args.InfoHash), source.IP(), port, portOk)
}
s.reply(source, m.T, krpc.Return{})
default:
......@@ -755,7 +758,7 @@ func (s *Server) announcePeer(node Addr, infoHash int160, port int, token string
return s.query(node, "announce_peer", &krpc.MsgArgs{
ImpliedPort: impliedPort,
InfoHash: infoHash.AsByteArray(),
Port: port,
Port: &port,
Token: token,
}, func(m krpc.Msg, err error) {
if callback != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment