Skip to content

dorufloare/cucstorrent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cucstorrent

A BitTorrent client written from scratch in C++20. No libtorrent, no libcurl, no event loop framework - just POSIX sockets, pthreads, and the BitTorrent spec.

Verified working: downloaded and SHA256-checked the Debian 13.5.0 netinst ISO (755MB, 3020 pieces) against the official Debian checksums.

What it does

  • Parses .torrent files (bencode parser written from scratch)
  • Announces to HTTP trackers, decodes the compact peer list
  • Opens TCP connections to peers, completes the BitTorrent handshake
  • Downloads pieces concurrently across all peers with a thread-per-peer model
  • Pipelines block requests within each piece (sends all 16 requests before waiting for responses)
  • SHA1-verifies every piece before writing — corrupt or tampered data gets discarded and requeued
  • Writes verified pieces to disk on a dedicated FileWriter thread, synchronized via condition variable

How it works

The download is split across three components:

PieceManager tracks which of the torrent's pieces are pending, in-flight, or complete. Each peer thread calls next_piece() to claim a piece atomically (mutex-protected), downloads it, then calls piece_complete() which verifies the SHA1 hash and pushes the data onto a write queue.

PeerSession (one per peer) handles the full peer wire protocol: handshake, bitfield exchange, interested/unchoke negotiation, and the block request loop. Uses select()-based timeouts on every recv so dead peers don't block threads indefinitely.

FileWriter runs on its own thread, wakes via condition variable when verified pieces arrive, and writes each piece to the correct offset in the pre-allocated output file.

If a peer disconnects mid-download the piece goes back to pending and gets picked up by another peer.

Build

Requires: GCC 13+, CMake 3.20+, libssl-dev, liburing-dev

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build

Usage

./build/cucstorrent <path/to/file.torrent>

Verified download

$ ./build/cucstorrent debian-13.5.0-amd64-netinst.iso.torrent
$ sha256sum debian-13.5.0-amd64-netinst.iso
95838884f5ea6c82421dfe6baaa5a639dbbe6756c1e380f9fe7a7cb0c1949d2a  debian-13.5.0-amd64-netinst.iso

$ wget -qO- https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/SHA256SUMS | grep netinst
95838884f5ea6c82421dfe6baaa5a639dbbe6756c1e380f9fe7a7cb0c1949d2a  debian-13.5.0-amd64-netinst.iso

About

BitTorrent client from scratch in C++20. Parses .torrent files, announces to trackers, downloads from peers concurrently

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors