Skip to content
Magnet URIs
step 1/5

Reading — step 1 of 5

Read

~3 min readBencoding & Metainfo

Magnet URIs

A .torrent file has to be hosted somewhere — and the moment you must host a file to share a file, you have re-invented the central point of failure BitTorrent was built to avoid. A magnet URI removes it: the link is the identity. It carries the infohash plus a few hints, as plain text you can paste anywhere. The metadata itself is then fetched from the swarm.

magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a
       &dn=Big+Buck+Bunny
       &tr=udp%3A%2F%2Ftracker.example.com%3A6969

(One line in reality; wrapped here for readability.)

ParamNameMeaning
xtexact topicRequired. urn:btih: + the infohash — 40 hex chars (or 32 base32 chars in older links)
dndisplay nameUI hint only; not authoritative, not verified
trtrackerURL-encoded tracker URL. Repeatable — one per tracker
xlexact lengthTotal size hint in bytes
xsexact sourceDirect-download fallback URL (rare)

Bootstrapping from 20 bytes

A magnet gives you an infohash and maybe trackers — no piece hashes, no file names, no piece length. The client has to earn the metadata:

  1. Ask the tr= trackers (or the DHT, keyed by infohash) for peers.
  2. Handshake with peers, advertising the extension protocol (BEP 10) in the reserved bits.
  3. Negotiate ut_metadata (BEP 9) and download the raw bencoded info dict from a peer, in 16 KiB pieces.
  4. Verify: sha1(received_bytes) == infohash. Match → you now hold the metainfo and proceed exactly as if you had opened a .torrent file. Mismatch → the peer lied; drop it and ask another.

Step 4 is why this is safe. The infohash is a content address: peers cannot forge metadata that hashes to it, so a 40-character string pasted on a forum is exactly as trustworthy as the original .torrent file it replaces.

Parsing rules — where the bugs live

  • Strip magnet:?, split on &, then split each pair on the first = only (tracker URLs may themselves contain =; the value keeps it).
  • URL-decode values: %XX becomes the byte 0xXX, and + becomes a space (query-string convention) — Big+Buck+BunnyBig Buck Bunny, My%20FileMy File, udp%3A%2F%2F...udp://....
  • tr repeats. Collect every occurrence, in order. Any "parse into a dict" shortcut silently keeps only the last tracker.
  • Validate that the xt value starts with urn:btih: before trusting anything else — other URN schemes (urn:sha1:, BitTorrent v2's urn:btmh:) are not what this client speaks.

Your exercise

Print the four key=value lines from the starter for each URI. Mistakes the grader catches:

  1. Overwriting repeated trackers. Test 2 carries two tr= parameters and expects trackers=http://t1:80,http://t2:80 — a dict-based parser prints only http://t2:80 and fails.
  2. Skipping URL decoding. dn=Big+Buck+Bunny must print as display_name=Big Buck Bunny; leaving the + in fails the first visible test, and leaving %20 in fails the hidden My File test.
  3. Wrong rejection output. For magnet:?xt=urn:sha1:abc print exactly ERR not a btih magnet — that single line, with no infohash/display_name lines after it.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…