4

i have doubt how the udp announce response are handled as sugested in bep udp protocol specification

import socket,struct
clisocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
connection_id=0x41727101980
transaction_id = 12345

# responses Tracker : Specification  UDP [ User Datagaram Protocol ]
info_hash  = "%1D%D4%D1%EDQn%DB%5CL%83%90%1B%2B%F8%83%A2%19%C0%7C%98"
peer_id = "-UT1234-m%09%B2%D5%99%FA%1Fj%88%AC%0D%A7"
action =1 # announce
downloaded = 0
left = 0
uploaded = 0
event =0
ip = 0
key = 0
num_want = -1
port = 9999

announce_pack = struct.pack(">QLL20s20sQQQLLLLH",connection_id,action,transaction_id,info_hash,peer_id,down  loaded,left,uploaded,event,ip,key,num_want,port)
clisocket.sendto(announce_pack, ("tracker.ccc.de", 80))
res = clisocket.recv(1024)
action=struct.unpack(">HLLLLQQQ20s20sLLQ",res)

but i am getting following error

DeprecationWarning: struct integer overflow masking is deprecated announce_pack=struct.pack(">QLL20s20sQQQLLLLH",connection_id,action,transaction_id,info_hash,peer_id,downloaded,left,uploaded,event,ip,key,num_want,port)
Traceback (most recent call last):
action=struct.unpack(">HLLLLQQQ20s20sLLQ",res)
struct.error: unpack requires a string argument of length 98
abhijeet
  • 459
  • 5
  • 15

1 Answers1

3

It looks like you're passing a string to struct that's too big, perhaps because you're getting more than one packet. Try

action=struct.unpack(">HLLLLQQQ20s20sLLQ",res[:98])

Also, it appears that you are not parsing the announce response correctly. See here for the format of the UDP tracker packets.

GuillaumeDufay
  • 1,118
  • 9
  • 13
  • Hi Guillaume, I'm a little confused by your struct format. According the UDP tracker specs, wouldn't the format mask be something like `>LLLL...` Something like that? Could you please elaborate since Iäm new to structs. Thanks. – Mridang Agarwalla Jan 27 '12 at 22:22
  • Mridang, I think you are correct. Looking at the specification, the format should be '>LLLLL', and then '>LHLHLH...' for the N hosts that follow. (This is what I meant when I said "it appears that you are not parsing the announce response correctly". I was just working from the format from the original post, which would require a string of length 98.) – GuillaumeDufay Jan 28 '12 at 11:44