udp: fix IP bytes not being copied out of the packet

Fixes #243
This commit is contained in:
Leo Balduf 2016-10-04 17:22:35 +02:00
parent 404e7f5821
commit 5866d96cb0
2 changed files with 8 additions and 2 deletions

View file

@ -128,10 +128,15 @@ func (t *Frontend) ListenAndServe() error {
defer t.wg.Done()
defer pool.Put(buffer)
if ip := addr.IP.To4(); ip != nil {
addr.IP = ip
}
// Handle the request.
start := time.Now()
action, err := t.handleRequest(
Request{buffer[:n], addr.IP},
// Make sure the IP is copied, not referenced.
Request{buffer[:n], append([]byte{}, addr.IP...)},
ResponseWriter{t.socket, addr},
)
recordResponseDuration(action, err, time.Since(start))

View file

@ -80,7 +80,8 @@ func ParseAnnounce(r Request, allowIPSpoofing, v6 bool) (*bittorrent.AnnounceReq
ip := r.IP
ipbytes := r.Packet[84:ipEnd]
if allowIPSpoofing {
ip = net.IP(ipbytes)
// Make sure the bytes are copied to a new slice.
copy(ip, net.IP(ipbytes))
}
if !allowIPSpoofing && r.IP == nil {
// We have no IP address to fallback on.