Reading — step 1 of 5
Read
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.)
| Param | Name | Meaning |
|---|---|---|
xt | exact topic | Required. urn:btih: + the infohash — 40 hex chars (or 32 base32 chars in older links) |
dn | display name | UI hint only; not authoritative, not verified |
tr | tracker | URL-encoded tracker URL. Repeatable — one per tracker |
xl | exact length | Total size hint in bytes |
xs | exact source | Direct-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:
- Ask the
tr=trackers (or the DHT, keyed by infohash) for peers. - Handshake with peers, advertising the extension protocol (BEP 10) in the reserved bits.
- Negotiate
ut_metadata(BEP 9) and download the raw bencodedinfodict from a peer, in 16 KiB pieces. - Verify:
sha1(received_bytes) == infohash. Match → you now hold the metainfo and proceed exactly as if you had opened a.torrentfile. 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:
%XXbecomes the byte 0xXX, and+becomes a space (query-string convention) —Big+Buck+Bunny→Big Buck Bunny,My%20File→My File,udp%3A%2F%2F...→udp://.... trrepeats. Collect every occurrence, in order. Any "parse into a dict" shortcut silently keeps only the last tracker.- Validate that the
xtvalue starts withurn:btih:before trusting anything else — other URN schemes (urn:sha1:, BitTorrent v2'surn: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:
- Overwriting repeated trackers. Test 2 carries two
tr=parameters and expectstrackers=http://t1:80,http://t2:80— a dict-based parser prints onlyhttp://t2:80and fails. - Skipping URL decoding.
dn=Big+Buck+Bunnymust print asdisplay_name=Big Buck Bunny; leaving the+in fails the first visible test, and leaving%20in fails the hiddenMy Filetest. - Wrong rejection output. For
magnet:?xt=urn:sha1:abcprint exactlyERR 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…