2014-12-11 20:53:39 +00:00
|
|
|
module.exports = parseUdpRequest
|
|
|
|
|
2014-12-11 15:11:12 +00:00
|
|
|
var ipLib = require('ip')
|
2015-07-29 07:26:44 +00:00
|
|
|
var common = require('../common')
|
2014-12-09 23:44:45 +00:00
|
|
|
|
|
|
|
function parseUdpRequest (msg, rinfo) {
|
2015-03-07 02:12:59 +00:00
|
|
|
if (msg.length < 16) throw new Error('received packet is too short')
|
2014-12-09 23:44:45 +00:00
|
|
|
|
|
|
|
var params = {
|
|
|
|
connectionId: msg.slice(0, 8), // 64-bit
|
|
|
|
action: msg.readUInt32BE(8),
|
2016-03-11 09:09:09 +00:00
|
|
|
transactionId: msg.readUInt32BE(12),
|
2016-03-12 01:21:19 +00:00
|
|
|
type: 'udp'
|
2014-12-09 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 10:25:31 +00:00
|
|
|
if (!common.CONNECTION_ID.equals(params.connectionId)) {
|
2014-12-09 23:44:45 +00:00
|
|
|
throw new Error('received packet with invalid connection id')
|
2015-03-07 02:12:59 +00:00
|
|
|
}
|
2014-12-09 23:44:45 +00:00
|
|
|
|
|
|
|
if (params.action === common.ACTIONS.CONNECT) {
|
|
|
|
// No further params
|
|
|
|
} else if (params.action === common.ACTIONS.ANNOUNCE) {
|
2014-12-12 10:02:11 +00:00
|
|
|
params.info_hash = msg.slice(16, 36).toString('hex') // 20 bytes
|
|
|
|
params.peer_id = msg.slice(36, 56).toString('hex') // 20 bytes
|
2014-12-09 23:44:45 +00:00
|
|
|
params.downloaded = fromUInt64(msg.slice(56, 64)) // TODO: track this?
|
|
|
|
params.left = fromUInt64(msg.slice(64, 72))
|
|
|
|
params.uploaded = fromUInt64(msg.slice(72, 80)) // TODO: track this?
|
2014-12-12 10:02:11 +00:00
|
|
|
|
|
|
|
params.event = common.EVENT_IDS[msg.readUInt32BE(80)]
|
2014-12-09 23:44:45 +00:00
|
|
|
if (!params.event) throw new Error('invalid event') // early return
|
2014-12-12 10:02:11 +00:00
|
|
|
|
|
|
|
var ip = msg.readUInt32BE(84) // optional
|
|
|
|
params.ip = ip
|
|
|
|
? ipLib.toString(ip)
|
|
|
|
: rinfo.address
|
|
|
|
|
2015-07-05 00:10:41 +00:00
|
|
|
params.key = msg.readUInt32BE(88) // Optional: unique random key from client
|
2014-12-12 10:02:11 +00:00
|
|
|
|
2014-12-09 23:44:45 +00:00
|
|
|
// never send more than MAX_ANNOUNCE_PEERS or else the UDP packet will get bigger than
|
|
|
|
// 512 bytes which is not safe
|
2014-12-12 10:02:11 +00:00
|
|
|
params.numwant = Math.min(
|
2015-03-24 08:01:49 +00:00
|
|
|
msg.readUInt32BE(92) || common.DEFAULT_ANNOUNCE_PEERS, // optional
|
2014-12-12 10:02:11 +00:00
|
|
|
common.MAX_ANNOUNCE_PEERS
|
|
|
|
)
|
|
|
|
|
2014-12-09 23:44:45 +00:00
|
|
|
params.port = msg.readUInt16BE(96) || rinfo.port // optional
|
|
|
|
params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets
|
|
|
|
params.compact = 1 // udp is always compact
|
|
|
|
} else if (params.action === common.ACTIONS.SCRAPE) { // scrape message
|
2015-05-02 00:36:07 +00:00
|
|
|
if ((msg.length - 16) % 20 !== 0) throw new Error('invalid scrape message')
|
|
|
|
params.info_hash = []
|
|
|
|
for (var i = 0, len = (msg.length - 16) / 20; i < len; i += 1) {
|
|
|
|
var infoHash = msg.slice(16 + (i * 20), 36 + (i * 20)).toString('hex') // 20 bytes
|
|
|
|
params.info_hash.push(infoHash)
|
|
|
|
}
|
2014-12-09 23:44:45 +00:00
|
|
|
} else {
|
2014-12-11 15:19:08 +00:00
|
|
|
throw new Error('Invalid action in UDP packet: ' + params.action)
|
2014-12-09 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
var TWO_PWR_32 = (1 << 16) * 2
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the closest floating-point representation to the buffer value. Precision will be
|
|
|
|
* lost for big numbers.
|
|
|
|
*/
|
|
|
|
function fromUInt64 (buf) {
|
|
|
|
var high = buf.readUInt32BE(0) | 0 // force
|
|
|
|
var low = buf.readUInt32BE(4) | 0
|
|
|
|
var lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low
|
|
|
|
|
2017-02-09 00:22:13 +00:00
|
|
|
return (high * TWO_PWR_32) + lowUnsigned
|
2014-12-09 23:44:45 +00:00
|
|
|
}
|