Fork me on GitHub

Welcome

As I continue my programming adventures in Rust, I’ve decided to launch yet another learning project. This time, it’s going to be an implementation of a simple BitTorrent client.

Motivations

Why BitTorrent? One reason is that I’ve always been interested in how peer-to-peer systems work. There’s something very intriguing about how multiple actors can collaborate to accomplish a task without the need for centralized control. By diving deeper into the implementation of such a system, I hope to gain a better understanding of how they work in general.

Another reason is that I expect a project like this will help me deepen my experience with Rust. In particular, I would like to become more familiar with these areas that I haven’t explored yet:

  • Network programming
  • Multi-threaded programming
  • Programming console UIs

Project scope

Writing a fully-fledged BitTorrent client is quite a big task, so for my pet project I’d like to scale it down to the essentials. I will consider the project accomplished when my solution is able to do the following:

  • Connect to the torrent tracker to fetch the initial information about the file to download
  • Download the file from multiple peers in parallel
  • Serve requests from other peers while the download is ongoing
  • Show the download progress in some form of text-based UI

It should be noted that I’m starting this project knowing absolutely nothing about the BitTorrent protocol. Of course, I have some experience with various BitTorrent clients as a user, but I have absolutely no idea how they work under the hood. But hey, that’s what this project is all about: getting into the nitty-gritty details!

Obviously, I’m not the first person to implement a BitTorrent client. There are plenty of resources on the Web dedicated to this subject. Here, I’m going to put up a list of those that I use in the course of this project. The list will be updated as I move along.

Project diary

  • BitTorrent: Key concepts

    Let’s start with an overview of the key concepts of BitTorrent architecture to gain a high-level understanding of what we’re dealing with.

  • First step: Fetching the announce URL

    As mentioned in the previous post, BitTorrent clients start the download by first making a request to the torrent tracker using the announce URL to retrieve the list of available peers. The announce URL is taken from the torrent file. It seems that my very first task should be to parse the torrent file and extract the announce URL.

  • Make HTTP request to the tracker

    We left off our project at the point where we managed to parse the torrent file (at least partially) and extract the tracker’s announce URL from it. Now, it’s time to make use of this URL and write some code to send an HTTP request to the torrent tracker.

  • Obtaining the list of peers

    In the last chunk of work, I achieved a milestone: making a request to the torrent tracker and getting back a meaningful response. The response is “torrent not found,” though, because I’m still passing a fake torrent hash in the request parameters. Today, I want to fix this situation and make the program use the real hash value that it will obtain from the torrent file.

  • Parsing the peer list from tracker

    Last time, we managed to fetch the list of peers from the torrent tracker for our sample torrent file. I left off by simply dumping the response from the torrent tracker onto the screen, and now I would like to pick up on that and actually parse the tracker’s response so that we can get our hands on peers’ IP addresses and ports. That’s going to be our next step towards making connections to peers.

  • Time for reflection

    I’ve made some progress with communicating with the torrent tracker so far, and I’m ready to dive into the details of peer-to-peer communication. However, I have some doubts about what problem to tackle next. Should I dive into the technical details of peer-to-peer communication, or should I polish the code I already have? Or maybe I should look into areas I haven’t even touched yet? I’d like to step back and look at the bigger picture.

subscribe via RSS