How to send a request to the torrent tracker server

I am attached to the implementation of a small torrent client. I get information from here: http://wiki.theory.org/BitTorrent_Tracker_Protocol .

I want to send a request for a tracker to get a list of peers. Should I set up an udp connection and connect to udp: //tracker.thepiratebay.org in port 80? Should this look like my request message?

"udp://tracker.thepiratebay.org??info_hash=12345678901234567890 &peer_id=ABCDEFGHIJKLMNOPQRST port=6888 &downloaded=0 &left=0 &event=started " 

this is my part of my code:

  char *requestToSend; int sock; struct sockaddr_in servAddr; struct sockaddr_in fromAddr; int fromSize; int respStringLen; int portNum =80; char data_recv[ECHOMAX]; char *hash="12345678901234567890"; char *id="ABCDEFGHIJKLMNOPQRST"; char *temp ="udp://tracker.thepiratebay.org??info_hash=12345678901234567890\n&peer_id=ABCDEFGHIJKLMNOPQRST\nport=6888\n&downloaded=0\n&left=0\n&event=started"; requestToSend = malloc(sizeof(temp)+1); sprintf(requestToSend, "%s??info_hash=%s\n&peer_id=%s\nport=%s\n&downloaded=0\n&left=0\n&event=started\0","udp://tracker.thepiratebay.org", hash,id,"6888"); printf("%s to send \n", requestToSend); /* Create a datagram/UDP socket */ if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ printf("fail create socket"); return 0; } /* Construct the server address structure */ struct hostent *hp = gethostbyname("udp://tracker.thepiratebay.org"); memset(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */ servAddr.sin_family = AF_INET; /* Internet addr family */ memcpy( (char *) &servAddr.sin_addr.s_addr, hp->h_addr, hp->h_length ); servAddr.sin_port = htons(portNum); /* Server port */ //send request to tracker server if (send(sock, requestToSend, strlen(requestToSend), 0) != strlen(requestToSend)){ printf("fail send"); return 0; } /* Recv a response */ fromSize = sizeof(fromAddr); if ((respStringLen = recvfrom(sock, data_recv, ECHOMAX, 0, (struct sockaddr *) &fromAddr, &fromSize)) != strlen(requestToSend)){ printf("fail to recv"); return 0; } 

this is what i get from torrent file

  dict { announce => str = http://tracker.thepiratebay.org/announce (len = 40) announce-list => list [ list [ str = http://tracker.thepiratebay.org/announce (len = 40) ] list [ str = udp://tracker.thepiratebay.org:80/announce (len = 42) ] list [ str = http://tracker.openbittorrent.com/announce (len = 42) ] list [ str = udp://tracker.openbittorrent.com:80/announce (len = 44) ] ] creation date => int = 1174816388 encoding => str = UTF-8 (len = 5) info => dict { filehash => str = ¸¥£öüËXþÐS®(äfn6 (len = 20) length => int = 2222949 name => str = Audacity.zip (len = 12) name.utf-8 => str = Audacity.zip (len = 12) piece length => int = 32768 pieces => str = (null) (len = 0) } } d8:announce40:http://tracker.thepiratebay.org/announce13:announce-listll40:http: //tracker.thepiratebay.org/announceel42:udp://tracker.thepiratebay.org:80/announ ceel42:http://tracker.openbittorrent.com/announceel44:udp://tracker.openbittorre nt.com:80/announceee13:creation datei1174816388e8:encoding5:UTF-84:infod8:fileha sh20: ¸¥£öüËXþÐS®(äfn66:lengthi2222949e4:name12:Audacity.zip10:name.utf-812:Audacity.z yf3-ûBÎNrl lengthi32768e6:pieces1360:þ]úÙÉÅ'NÕæ+gd3fi6è6¶ 
+4
source share
3 answers

Have you verified that gethostbyname really returned a valid address? I think you should not add the http: // or udp: // protocol string as an argument to gethostbyname.

 struct hostent *hp = gethostbyname("tracker.thepiratebay.org"); if(!hp) { herror("gethostbyname(): "); exit(1); } 

Add this line to verify that gethostbyname is working correctly.

+1
source

The example on this website is NOT for UDP communications. This is for communication via TCP, or rather HTTP.

+1
source

If the tracker uses UDP , you should not send a GET request, but rather, establish a connection at the specified port, and then perform 4 steps:

  • Send a connection request
  • Confirm the connection request by reading the response to 16 bytes and insert it into the connection identifier
  • Send announcement with connection id
  • Analyze the response of the announcement, the size depends on how many pointers are indicated

A UDP connection through all receives fewer packets to exchange the same amount of information, but is more actively involved in the implementation for the client :)

This wiki details what UDP messages should include. They are reminiscent of the messages you send to peers (i.e. they use large endian ints encoded in a fixed length of bytes)

or specifications can help

0
source

All Articles