Skip to content

ggstt/BitTorrent-Client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 

Repository files navigation

BitTorrent-Client

I am trying to implement a BitTorrent Client from scratch .This attempt uses C++ language. I will keep documenting the implementation details in this markdown file.

I find this particular project intriguing because it is an application of Distributed Systems and I am very excited to learn and build this!

I will briefly describe how I understand the things:

  • Peers are users/entities who own pieces/parts of the file that we need to download.
  • The list of peers who own pieces of the file is maintained on an entity called the 'Tracker'.
  • So we first request this list from the Tracker and then proceed to request file pieces from these particular set of peers.
  • Then assemble these pieces into a single file and (disconnect) from the Network!
  • The user starts with a .torrent file or a magnet URI . We are expected to extract the info hash from this.
  • Then we request peerlist from the 'Tracker' by sending this infohash-details.

To elaborate how the requesting procedure should take place :-

  • If the user already has a .torrent file with them , then the client has to decode the Bencoded dictionary and then make a request to the announce URL
  • Else if the user has a magnet URI then-
    • If neither address tracker nor the peers are present then we have to proceed to download directly from any acceptable source or else we return a failure message
    • If address tracker is absent but list of peers is available then we can simply go ahead and make a request to every peer for the file pieces
    • If the address tracker is present then we should go ahead and fetch the (array of) trackers in parameter tr and send a request to each of these tr URLs.
  • Lastly choose a peer to connect to

Info hash is sha-1 encoding of a part of the contents of the .torrent file. Peer id is simply the id of a peer on the network.

How Bencode works:

  • integer = i<integer>e
  • byte string = <length>:<contents>
  • lists = l<elements>e
  • dictionaries = d<pairs>e

Both .torrent file and the magnet URI contain info hash and peer id. Only the .torrent file consists of the announce(url of tracker). And only the magnet URI contains the address tracker, peer,acceptable source and so on...

Handshaking policy: The client chooses a peer and then has to send and receive a handshake. In case the handshake received is invalid , the connection gets severed else the client needs to keep up the connection by sending keep-alive messages at every time interval.

Validity of a handshake:

  • The first byte should be correct
  • The bit-torrent protocol should be correct
  • The hash info encoded by sha-1 should match with the encoding at the origin
  • The peer id should be valid

The nine different types of messages:

  • 0-choke
  • 1-unchoke
  • 2-interested
  • 3-not interested
  • 4-have : A single number which is the index of the piece that the downloader just received and checked the info hash of.
  • 5-bitfield : The contents correspond to the indices of the pieces of the file that a particular peer owns.
  • 6-request : A request for a piece. It contains-length,type,index,begin,length (* each piece is requested in blocks)
  • 7-piece : a response to the request message.
  • 8-cancel : It contains-length,type,index,offset,length

Chocking is an algorithm that blocks peers that are leeching.It is basically a tit-for-tat algorithm. It allows you to download from the network only if you contribute to the network.Kinda like upload speed is kept similar to download speed maybe..

The BitTorrent Protocol!

  • If the user is and the peer is interested , we................

Finally assemble the blocks and pieces

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •