Skip to content

release/2.1.2.1

Latest

Choose a tag to compare

@github-actions github-actions released this 13 Nov 15:45
e80d9ba

jlibtorrent 2.1.2.1: libtorrent 2.1, new disk backend options, and what to change when you migrate from 2.0

By the FrostWire team — github.com/frostwire/frostwire-jlibtorrent

jlibtorrent 2.1.2.1 moves the bindings from libtorrent 2.0.15 (RC_2_0) to libtorrent 2.1.2 plus fixes (RC_2_1, revision 2fdc87690). libtorrent 2.1 is a big step, and a few of its changes reach your Java code. Most apps need a handful of edits; many need none.

Read this first: upgrade everything together. The Java classes and the native libraries of 2.0.x and 2.1.x don't load together. Move the jlibtorrent jar and every native jar you ship (jlibtorrent-<platform>), or the Android AAR, to 2.1.2.1 in one step. Java stays at 17.


What is new since 2.1.2.0

jlibtorrent 2.1.2.1 is 2.1.2.0 plus one new call: SessionParams.setMMapDiskIO(). libtorrent 2.1 changed its default disk backend from memory-mapped files to pread(), and jlibtorrent had setters for the posix, pread and default backends but none for mmap. Now an app that wants exactly the libtorrent 2.0 behavior can ask for it:

SessionParams params = new SessionParams(settingsPack);
params.setMMapDiskIO();   // libtorrent 2.0's memory-mapped files backend
sm.start(params);

Coming from 2.0? Everything below is the migration guide: what changed in 2.1 and how to refactor your integration. It is the same as in 2.1.2.0, with the mmap option added to the disk backend section (9) and the checklist.


What's new

  • libtorrent 2.1: a multi-threaded pread() disk backend (now the default), a new create-torrent API, smaller resume data, BEP 17 HTTP seeds removed, i2p peer exchange, and many fixes (DHT, web seed redirects, UPnP, uTP, file priorities).
  • WebTorrent is compiled in: wss:// trackers and WebRTC peers. LibTorrent.hasWebTorrentSupport() tells you, and SettingsPack has real WebTorrent settings.
  • New alerts and calls: FilePrioritiesAlert, FileStatusAlert, IpBanAlert, TorrentHandle.postFileStatus(), postFilePriorities(), SessionParams.setPreadDiskIO(), SessionParams.setMMapDiskIO() (2.1.2.1), PeerLogAlert.event().
  • A faster alert dispatcher: AlertMulticaster no longer recurses, which fixed a StackOverflowError with thousands of listeners (frostwire/frostwire#1441).
  • Build: Boost 1.92.0, b2 5.5.3, SWIG 4.5.1, CMake for the WebTorrent dependencies.

Migrating from 2.0 to 2.1: what to change

Each change below shows the 2.0 code that no longer works (❌) and what to write in 2.1 (✅). Things that stayed the same are left out.

1. Adding a torrent from a TorrentInfo: trackers and web seeds are no longer inside it

libtorrent 2.1's torrent_info holds only the info dictionary. Trackers, web seeds, DHT nodes, comment and creator now live in add_torrent_params, and jlibtorrent's TorrentInfo carries them for you. The high-level calls keep working. Only raw SWIG code breaks, and it breaks silently: the torrent is added, without its trackers.

// 2.0 ❌ 2.1 adds the torrent WITHOUT its trackers and web seeds
add_torrent_params p = add_torrent_params.create_instance();
p.set_ti(ti.swig());
session.async_add_torrent(p);   // a swig session_handle

// 2.1 ✅ these keep the trackers and web seeds
sm.download(ti, saveDir);
// or, with AddTorrentParams:  params.torrentInfo(ti);

A TorrentInfo you get from a handle or an alert (TorrentHandle.torrentFile()) never has trackers, web seeds, comment or creator:

// 2.0 ❌ returned the trackers; in 2.1 a TorrentInfo from a handle has none
List<AnnounceEntry> trackers = handle.torrentFile().trackers();

// 2.1 ✅ ask the handle
List<AnnounceEntry> trackers = handle.trackers();
List<String> webSeeds = handle.urlSeeds();

2. Changing a .torrent file's trackers with create_torrent

// 2.0 ❌ create_torrent(torrent_info) is gone in 2.1
create_torrent c = new create_torrent(ti.swig());
c.add_tracker("http://a.example:6969/announce", 0);
c.add_tracker("http://b.example:6969/announce", 1);
byte[] data = Vectors.byte_vector2bytes(c.generate().bencode());
ti = TorrentInfo.bdecode(data);

// 2.1 ✅ edit the TorrentInfo, then write it
ti.addTracker("http://a.example:6969/announce", 0);
ti.addTracker("http://b.example:6969/announce", 1);
ti = TorrentInfo.bdecode(ti.bencode());   // keeps "announce", "announce-list" and the DHT nodes

TorrentInfo.bencode() throws for a v2 torrent without piece layers, which is what a session has for a torrent added by magnet link. TorrentHandle.torrentFileWithHashes() still works and adds the layers.

3. Creating torrents with the raw SWIG classes

TorrentBuilder did not change: if you use it, skip this. Only code that called add_files_ex and create_torrent itself changes.

// 2.0 ❌ add_files_ex() and create_torrent(file_storage) are gone in 2.1
file_storage fs = new file_storage();
libtorrent.add_files_ex(fs, path, listener, new create_flags_t());
create_torrent ct = new create_torrent(fs, pieceSize, flags);

// 2.1 ✅ list the files first, then create the torrent from the list
create_file_entry_vector entries = libtorrent.list_files_ex(path, listener, new create_flags_t());
create_torrent ct = new create_torrent(entries, pieceSize, flags);
entries.delete();   // create_torrent copied the list; delete the listener too

The file paths inside the new torrent now start with the torrent's name, and a single-file torrent is named after the file.

4. HTTP seeds (BEP 17) are gone

libtorrent 2.1 removed httpseeds; only BEP 19 URL seeds remain.

// 2.0 ❌ these methods are removed in 2.1 (compile error)
handle.addHttpSeed("http://mirror.example/files/");
handle.removeHttpSeed("http://mirror.example/files/");
List<String> seeds = handle.httpSeeds();
ti.addHttpSeed("http://mirror.example/files/", "user:password");
ti.addUrlSeed("http://mirror.example/files/", "user:password");   // seed with authentication

// 2.1 ✅ BEP 19 URL seeds, without authentication or extra headers
handle.addUrlSeed("http://mirror.example/files/");
ti.addUrlSeed("http://mirror.example/files/");

There is no replacement for per-seed authentication or headers: libtorrent dropped them. WebSeedEntry is now just a URL: new WebSeedEntry(String), no swig(), and type() is always URL_SEED. A .torrent that carries httpseeds still loads; they are ignored.

5. Renaming files before adding the torrent

// 2.0 ❌ TorrentInfo.renameFile() is removed in 2.1 (so are remapFiles() and FileStorage.renameFile())
ti.renameFile(0, "better-name.mkv");
sm.download(ti, saveDir);

// 2.1 ✅ rename on the live torrent, after it was added
handle.renameFile(0, "better-name.mkv");

6. Flags that are gone

// 2.0 ❌ removed in 2.1: TorrentFlags.OVERRIDE_TRACKERS and TorrentFlags.OVERRIDE_WEB_SEEDS
params.flags(TorrentFlags.OVERRIDE_TRACKERS);

// 2.1 ✅ just remove the flag: nothing is left to override, trackers live only in the params

7. TorrentAlert.torrentName() still compiles, but it is deprecated and will go away

libtorrent 2.1 no longer stores the name in the alert. jlibtorrent now asks the session for it (a blocking call) and returns "" once the torrent is gone.

// 2.0 ✅ cheap  |  2.1 ⚠️ deprecated: blocks, and "" if the torrent was removed
String name = alert.torrentName();

// 2.1 ✅ keep the name you gave when you added the torrent, keyed by its hash
names.put(handle.infoHash().toString(), name);                      // when adding
String name = names.get(alert.handle().infoHash().toString());      // in the alert listener

8. Resume data: the pieces field changed format

Anyone who parses saved resume bencode by hand is affected. 2.0 wrote one byte per piece; 2.1 writes a packed bitfield, most significant bit first.

// 2.0 ❌ read one byte per piece
boolean have = pieces[p] != 0;

// 2.1 ✅ one bit per piece (this is how FrostWire's own migration reads it)
byte[] pieces = Vectors.byte_vector2bytes(entry.dict().get("pieces").string_bytes());
boolean have = (pieces[p / 8] & (0x80 >> (p % 8))) != 0;

2.1 also writes a smaller resume format overall. Don't plan on going back to 2.0 once 2.1 has saved a torrent's resume data. We did not test reading every 2.0 resume file.

9. The default disk backend changed (no code to change, but measure)

2.0 used memory-mapped files on 64-bit systems. 2.1 uses a multi-threaded pread()/pwrite() backend on Windows, Linux, macOS and Android. Expect a different memory and page-cache profile and different speeds. You can ask for it explicitly:

SessionParams params = new SessionParams(settingsPack);
params.setPreadDiskIO();   // new in 2.1, and what setDefaultDiskIO() now means
sm.start(params);

To keep libtorrent 2.0's memory-mapped backend instead (new in 2.1.2.1):

// 2.0: memory-mapped files were the default on 64-bit systems, nothing to write
// 2.1 ✅ the default is pread(); ask for memory-mapped files explicitly
SessionParams params = new SessionParams(settingsPack);
params.setMMapDiskIO();
sm.start(params);

Caveat: with memory-mapped files an I/O error (a full disk, a failing or removed drive, a truncated file) is delivered as a SIGBUS or SIGSEGV signal instead of an error code, and the JVM cannot catch it: the whole process crashes. That is why FrostWire avoids this backend on macOS and Linux. setPosixDiskIO() is the single-threaded, portable choice.

10. WebTorrent is built in, and its default STUN server is Google's

This is new in 2.1, so there is no 2.0 code to compare. libtorrent's default is Google's public STUN server (stun.l.google.com:19302), and jlibtorrent does not override it. If your app should not contact it:

SettingsPack sp = new SettingsPack();
sp.setWebtorrentStunServer("stun.example.org:3478");   // your own server
sp.maxWebtorrentOffers(0);                             // or: no WebRTC offers and no WebRTC peers

Smaller changes

  • PeerLogAlert.eventType() now returns the upper-case name of the new event enum. PeerLogAlert.event() returns the enum itself.
  • AlertMulticaster calls listeners in registration order, and an exception thrown by a listener stops the later listeners for that alert (SessionManager catches and logs it).
  • TorrentInfo.makeMagnetUri() includes the name (dn=) again; TorrentHandle.makeMagnetUri() still works.
  • disable_hash_checks is latched when the torrent is added.

Only if you use the raw SWIG classes (com.frostwire.jlibtorrent.swig.*)

2.0 ❌ 2.1 ✅
alert a = session.wait_for_alert_ms(ms); boolean pending = session.wait_for_alert_ms(ms);
peer_info.getIp() peer_info.remote_endpoint() (and local_endpoint())
torrent_status.need_save_resume torrent_status.need_save_resume_data
new torrent_info(bytes, ec) and the other parsing constructors libtorrent.load_torrent_buffer_ex(), or new TorrentInfo(byte[])
torrent_info.files() torrent_info.layout()
web_seed_entry, web_seed_entry_vector removed
peer_alert endpoint is always an IP it can be an i2p destination: get_endpoint_type(), get_i2p_destination()

New SWIG classes: create_file_entry, create_file_entry_vector, renamed_files. EnumNet and bloom_filter_128/256 keep their Java API (libtorrent made them internal; the bindings wrap them).


Migration checklist

  1. Bump the jlibtorrent jar and every native jar or the AAR to 2.1.2.1.
  2. Fix the compile errors: HTTP seeds (4), renameFile on a TorrentInfo (5), the removed flags (6), SWIG create-torrent calls (3).
  3. Search for set_ti( and torrentFile(): those are the silent ones (1).
  4. Replace torrentName() (7) and any hand-written resume parsing (8).
  5. Re-test: adding from a TorrentInfo (are the trackers there?), magnet-to-.torrent saving, resume save and load, creating a torrent, and download speed and memory with the pread backend (9).
  6. Decide your disk backend (9): the new pread default, setMMapDiskIO() to keep 2.0's behavior, or setPosixDiskIO().
  7. Decide about WebTorrent's STUN server (10).

Security and robustness fixes that come with libtorrent 2.1

DHT puts without a signature are no longer stored, and the DHT no longer queries its own ID. Extension handshake ports and message IDs are validated. A web seed redirect bug and an IPv6 SSRF case are fixed, as is a UPnP use-after-free and a uTP close stall. The IP filter now applies to DHT nodes.

Get it

Maven: com.frostwire:jlibtorrent:2.1.2.1 plus the native artifacts (jlibtorrent-linux-x86_64, jlibtorrent-macosx-arm64, jlibtorrent-windows, jlibtorrent-android and so on) from the FrostWire Maven repository, or the jars on this release page. Full changes: changelog.txt.