├── .gitignore ├── Cargo.toml ├── README.md ├── LICENSE └── src ├── build.rs ├── lt.h ├── lt.cc └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "libtorrent-sys" 3 | description = "cxx bindings to libtorrent" 4 | version = "1.0.0" 5 | authors = ["Nicolas Chevalier "] 6 | edition = "2018" 7 | license = "MIT" 8 | keywords = ["torrent", "magnet"] 9 | links = "torrent-rasterbar" 10 | build = "src/build.rs" 11 | repository = "https://github.com/shawone/libtorrent-sys" 12 | 13 | [dependencies] 14 | cxx = "1.0" 15 | 16 | [build-dependencies] 17 | cxx-build = "1.0.0" 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | libtorrent-sys 2 | ============== 3 | 4 | Rust API / cxx bindings for [**libtorrent**]. 5 | 6 | [**libtorrent**]: https://libtorrent.org/ 7 | 8 | All c++ reference can be found in libtorrent [**reference documentation**] 9 | 10 | [**reference documentation**]: https://libtorrent.org/reference.html 11 | 12 | Exposed API Reference 13 | --------------------- 14 | - session 15 | - add_torrent_params 16 | - add_torrent_params 17 | - torrent_handle 18 | - create_torrent 19 | - parse_magnet_uri() 20 | - bencode() 21 | 22 | Install 23 | ------- 24 | 1/ Download or [**build**] libtorrent (branch C_2_0) 25 | 26 | [**build**]: https://libtorrent.org/building.html 27 | 28 | 2/ Build libtorrent-sys: 29 | 30 | ``` 31 | RUSTFLAGS="-C linker=g++" CXX=g++ cargo build 32 | ``` 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2022 Nicolas Chevalier 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /src/build.rs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2022 Nicolas Chevalier 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | fn main() { 22 | use std::env; 23 | 24 | if env::var("DOCS_RS").is_ok() { 25 | return; 26 | } 27 | 28 | let mut cxx = cxx_build::bridge("src/lib.rs"); 29 | let out_dir = env::var("OUT_DIR").unwrap(); 30 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 31 | 32 | cxx.file("src/lt.cc") 33 | .include(manifest_dir) 34 | .include(out_dir) 35 | .flag_if_supported("-std=c++14") 36 | .compile("libtorrent-sys"); 37 | 38 | println!("cargo:rustc-flags=-ltorrent-rasterbar"); 39 | 40 | println!("cargo:rerun-if-changed=src/lib.rs"); 41 | println!("cargo:rerun-if-changed=src/lt.h"); 42 | println!("cargo:rerun-if-changed=src/lt.cc"); 43 | } 44 | -------------------------------------------------------------------------------- /src/lt.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2022 Nicolas Chevalier 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | #pragma once 22 | 23 | #include "rust/cxx.h" 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | char const* version(); 33 | 34 | namespace libtorrent { 35 | 36 | std::unique_ptr lt_create_session(); 37 | std::unique_ptr lt_parse_magnet_uri(rust::Str uri, rust::Str path); 38 | std::unique_ptr lt_session_add_torrent(lt::session &ses, lt::add_torrent_params ¶ms); 39 | void lt_session_remove_torrent(lt::session &ses, const lt::torrent_handle &hdl); 40 | void lt_session_pause(lt::session &ses); 41 | bool lt_torrent_has_metadata(const lt::torrent_handle &hdl); 42 | rust::Str lt_torrent_get_name(const lt::torrent_handle &hdl); 43 | rust::Slice lt_torrent_bencode(const lt::torrent_handle &hdl); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/lt.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2022 Nicolas Chevalier 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | #include "src/lt.h" 22 | 23 | namespace libtorrent { 24 | 25 | std::unique_ptr lt_create_session() 26 | { 27 | return std::make_unique(); 28 | } 29 | 30 | std::unique_ptr lt_parse_magnet_uri(rust::Str uri, rust::Str path) 31 | { 32 | std::string s(uri); 33 | std::string r(path); 34 | lt::add_torrent_params p; 35 | 36 | p = lt::parse_magnet_uri(s.c_str()); 37 | p.save_path = r.c_str(); 38 | 39 | return std::make_unique(std::move(p)); 40 | } 41 | 42 | std::unique_ptr lt_session_add_torrent(lt::session &ses, lt::add_torrent_params ¶ms) 43 | { 44 | lt::torrent_handle hdl; 45 | 46 | hdl = ses.add_torrent(params); 47 | 48 | return std::make_unique(std::move(hdl)); 49 | } 50 | 51 | void lt_session_remove_torrent(lt::session &ses, const lt::torrent_handle &hdl) 52 | { 53 | ses.remove_torrent(hdl); 54 | } 55 | 56 | void lt_session_pause(lt::session &ses) 57 | { 58 | ses.pause(); 59 | } 60 | 61 | bool lt_torrent_has_metadata(const lt::torrent_handle &hdl) 62 | { 63 | return hdl.status().has_metadata; 64 | } 65 | 66 | rust::Str lt_torrent_get_name(const lt::torrent_handle &hdl) 67 | { 68 | auto infos = hdl.torrent_file(); 69 | return infos->name(); 70 | } 71 | 72 | rust::Slice lt_torrent_bencode(const lt::torrent_handle &hdl) 73 | { 74 | auto infos = hdl.torrent_file(); 75 | auto entry = lt::create_torrent(*infos).generate(); 76 | std::vector vec; 77 | lt::bencode(std::back_inserter(vec), entry); 78 | rust::Slice slice{reinterpret_cast(vec.data()), vec.size()}; 79 | return slice; 80 | } 81 | 82 | } // namespace libtorrent 83 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2022 Nicolas Chevalier 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | #[cxx::bridge(namespace = "libtorrent")] 22 | pub mod ffi { 23 | unsafe extern "C++" { 24 | include!("src/lt.h"); 25 | 26 | type session; 27 | type add_torrent_params; 28 | type torrent_handle; 29 | 30 | /// This function return a struct of type lt::session 31 | pub fn lt_create_session() -> UniquePtr; 32 | 33 | /// This function return a struct of type lt::add_torrent_params 34 | /// 35 | /// lt::add_torrent_params is return by lt::parse_magnet_uri, 36 | /// then it configured with the given magnet string, and the current path 37 | pub fn lt_parse_magnet_uri(uri: &str, path: &str) -> UniquePtr; 38 | 39 | /// This function return a struct of type lt::torrent_handle 40 | /// 41 | /// Call the function add_torrent() using the current session and add_torrent_params 42 | /// given in parameters 43 | pub fn lt_session_add_torrent(ses: Pin<&mut session>, params: Pin<&mut add_torrent_params>) -> UniquePtr; 44 | 45 | /// This function remove the given torrent from session 46 | pub fn lt_session_remove_torrent(ses: Pin<&mut session>, hdl: &torrent_handle); 47 | 48 | /// This function call pause() for the given session 49 | pub fn lt_session_pause(ses: Pin<&mut session>); 50 | 51 | /// This function return true if torrent has metadata 52 | pub fn lt_torrent_has_metadata(hdl: &torrent_handle) -> bool; 53 | 54 | /// This function return the torrent's name 55 | /// 56 | /// Name is found in torrent_info return by function torrent_file() 57 | pub fn lt_torrent_get_name(hdl: &torrent_handle) -> &str; 58 | 59 | /// This function return bencoded data by lt::bencode() 60 | pub fn lt_torrent_bencode(hdl: &torrent_handle) -> &[u8]; 61 | 62 | /// This function call libtorrent::version() and return libtorrent version 63 | pub fn version() -> *const c_char; 64 | } 65 | } 66 | --------------------------------------------------------------------------------