Skip to content
Snippets Groups Projects
Commit 08cf212c authored by Sébastien Blin's avatar Sébastien Blin Committed by Adrien Béraud
Browse files

rust: split in files

parent fc2a597b
No related branches found
No related tags found
No related merge requests found
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
extern crate opendht; extern crate opendht;
use std::ffi::CString; // TODO remove dead code warning
use std::{thread, time}; use std::{thread, time};
use libc::c_void; use libc::c_void;
......
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::ffi::*;
pub use crate::ffi::Blob;
impl Blob {
pub fn data(&self) -> DataView {
unsafe {
dht_blob_get_data(self)
}
}
}
impl Drop for Blob {
fn drop(&mut self) {
unsafe {
dht_blob_delete(&mut *self)
}
}
}
\ No newline at end of file
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::ffi::*;
use std::ffi::CString;
impl PublicKey {
pub fn new() -> Box<PublicKey> {
unsafe {
Box::from_raw(dht_publickey_new())
}
}
pub fn unpack(&mut self, data: Vec<u8>) -> i32 {
unsafe {
dht_publickey_unpack(&mut *self, data.as_ptr(), data.len())
}
}
pub fn pack(&mut self, data: &str) -> i32 {
let data = CString::new(data).unwrap();
unsafe {
dht_publickey_pack(&mut *self,
data.as_ptr(),
data.as_bytes().len())
}
}
pub fn id(&self) -> InfoHash {
unsafe {
dht_publickey_get_id(self)
}
}
pub fn long_id(&self) -> PkId {
unsafe {
dht_publickey_get_long_id(self)
}
}
pub fn check_signature(&self, data: &str, signature: &str) -> bool {
let data = CString::new(data).unwrap();
let signature = CString::new(signature).unwrap();
unsafe {
dht_publickey_check_signature(self,
data.as_ptr(), data.as_bytes().len(),
signature.as_ptr(), signature.as_bytes().len())
}
}
pub fn encrypt(&self, data: &str) -> Box<Blob> {
let data = CString::new(data).unwrap();
unsafe {
Box::from_raw(dht_publickey_encrypt(self,
data.as_ptr(), data.as_bytes().len()))
}
}
}
impl Drop for PublicKey {
fn drop(&mut self) {
unsafe {
dht_publickey_delete(&mut *self)
}
}
}
impl PrivateKey {
pub fn new(key_length_bits: u32) -> Box<PrivateKey> {
unsafe {
Box::from_raw(dht_privatekey_generate(key_length_bits))
}
}
pub fn import(data: &str, password: &str) -> Box<PrivateKey> {
let password = CString::new(password).unwrap();
unsafe {
Box::from_raw(dht_privatekey_import(data.as_ptr(),
data.as_bytes().len(), password.as_ptr()))
}
}
pub fn public_key(&self) -> Box<PublicKey> {
unsafe {
Box::from_raw(dht_privatekey_get_publickey(self))
}
}
}
impl Drop for PrivateKey {
fn drop(&mut self) {
unsafe {
dht_privatekey_delete(&mut *self)
}
}
}
\ No newline at end of file
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::ffi::*;
use libc::c_void;
use std::ffi::CString;
use std::fmt;
pub use crate::ffi::{ DhtRunner, OpToken, Value };
impl DhtRunner {
pub fn new() -> Box<DhtRunner> {
unsafe {
Box::from_raw(dht_runner_new())
}
}
pub fn run(&mut self, port: u16) {
unsafe {
dht_runner_run(&mut *self, port)
}
}
pub fn bootstrap(&mut self, host: &str, service: u16) {
unsafe {
dht_runner_bootstrap(&mut *self,
CString::new(host).unwrap().as_ptr(),
CString::new(service.to_string()).unwrap().as_ptr())
}
}
pub fn get(&mut self, h: &InfoHash,
get_cb: extern fn(*mut Value, *mut c_void),
done_cb: extern fn(bool, *mut c_void),
cb_user_data: *mut c_void) {
unsafe {
dht_runner_get(&mut *self, h, get_cb, done_cb, cb_user_data)
}
}
pub fn put(&mut self, h: &InfoHash, v: Box<Value>,
done_cb: extern fn(bool, *mut c_void),
cb_user_data: *mut c_void) {
unsafe {
dht_runner_put(&mut *self, h, &*v, done_cb, cb_user_data)
}
}
pub fn listen(&mut self, h: &InfoHash,
cb: extern fn(*mut Value, bool, *mut c_void),
cb_user_data: *mut c_void) -> Box<OpToken> {
unsafe {
Box::from_raw(dht_runner_listen(&mut *self, h, cb, cb_user_data))
}
}
pub fn cancel_listen(&mut self, h: &InfoHash, token: Box<OpToken>) {
unsafe {
dht_runner_cancel_listen(&mut *self, h, &*token)
}
}
pub fn shutdown(&mut self,
done_cb: extern fn(bool, *mut c_void),
cb_user_data: *mut c_void)
{
unsafe {
dht_runner_shutdown(&mut *self, done_cb, cb_user_data)
}
}
}
impl Drop for DhtRunner {
fn drop(&mut self) {
unsafe {
dht_runner_delete(&mut *self)
}
}
}
impl Drop for OpToken {
fn drop(&mut self) {
unsafe {
dht_op_token_delete(&mut *self)
}
}
}
\ No newline at end of file
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use libc::{c_char, c_int, c_uint, c_void, in_port_t, size_t}; use libc::{c_char, c_int, c_uint, c_void, in_port_t, size_t};
const HASH_LEN: usize = 20; const HASH_LEN: usize = 20;
......
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::ffi::*;
use std::ffi::CStr;
use std::ffi::CString;
use std::fmt;
pub use crate::ffi::InfoHash;
impl InfoHash {
pub fn new() -> InfoHash {
InfoHash {
d: [0; 20]
}
}
pub fn random() -> InfoHash {
let mut h = InfoHash::new();
unsafe {
dht_infohash_random(&mut h);
}
h
}
pub fn get(data: &str) -> InfoHash {
let mut h = InfoHash::new();
unsafe {
let c_str = CString::new(data).unwrap();
dht_infohash_get(&mut h, c_str.as_ptr() as *mut u8, data.len());
}
h
}
pub fn is_zero(&self) -> bool {
unsafe {
dht_infohash_is_zero(self)
}
}
}
impl fmt::Display for InfoHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe {
let self_str = CStr::from_ptr(
dht_infohash_print(self)
).to_str().unwrap_or("");
write!(f, "{}", self_str)
}
}
}
\ No newline at end of file
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
extern crate libc; extern crate libc;
mod blob;
pub mod crypto;
mod dhtrunner;
mod ffi; mod ffi;
use ffi::*; mod infohash;
pub use ffi::{ DhtRunner, InfoHash, Value }; mod pkid;
mod value;
use std::fmt;
use std::ffi::CStr; pub use blob::Blob;
use std::ffi::CString; pub use dhtrunner::{ DhtRunner, OpToken };
use std::str; pub use infohash::InfoHash;
use std::slice; pub use pkid::PkId;
use libc::c_void; pub use value::{ DataView, Value };
// TODO separate into files
impl InfoHash {
pub fn new() -> InfoHash {
InfoHash {
d: [0; 20]
}
}
pub fn random() -> InfoHash {
let mut h = InfoHash::new();
unsafe {
dht_infohash_random(&mut h);
}
h
}
pub fn get(data: &str) -> InfoHash {
let mut h = InfoHash::new();
unsafe {
let c_str = CString::new(data).unwrap();
dht_infohash_get(&mut h, c_str.as_ptr() as *mut u8, data.len());
}
h
}
pub fn is_zero(&self) -> bool {
unsafe {
dht_infohash_is_zero(self)
}
}
}
impl fmt::Display for InfoHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe {
let self_str = CStr::from_ptr(
dht_infohash_print(self)
).to_str().unwrap_or("");
write!(f, "{}", self_str)
}
}
}
impl Blob {
pub fn data(&self) -> DataView {
unsafe {
dht_blob_get_data(self)
}
}
}
impl Drop for Blob {
fn drop(&mut self) {
unsafe {
dht_blob_delete(&mut *self)
}
}
}
impl fmt::Display for PkId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe {
let self_str = CStr::from_ptr(
dht_pkid_print(self)
).to_str().unwrap_or("");
write!(f, "{}", self_str)
}
}
}
impl DhtRunner {
pub fn new() -> Box<DhtRunner> {
unsafe {
Box::from_raw(dht_runner_new())
}
}
pub fn run(&mut self, port: u16) {
unsafe {
dht_runner_run(&mut *self, port)
}
}
pub fn bootstrap(&mut self, host: &str, service: u16) {
unsafe {
dht_runner_bootstrap(&mut *self,
CString::new(host).unwrap().as_ptr(),
CString::new(service.to_string()).unwrap().as_ptr())
}
}
pub fn get(&mut self, h: &InfoHash,
get_cb: extern fn(*mut Value, *mut c_void),
done_cb: extern fn(bool, *mut c_void),
cb_user_data: *mut c_void) {
unsafe {
dht_runner_get(&mut *self, h, get_cb, done_cb, cb_user_data)
}
}
pub fn put(&mut self, h: &InfoHash, v: Box<Value>,
done_cb: extern fn(bool, *mut c_void),
cb_user_data: *mut c_void) {
unsafe {
dht_runner_put(&mut *self, h, &*v, done_cb, cb_user_data)
}
}
pub fn listen(&mut self, h: &InfoHash,
cb: extern fn(*mut Value, bool, *mut c_void),
cb_user_data: *mut c_void) -> Box<OpToken> {
unsafe {
Box::from_raw(dht_runner_listen(&mut *self, h, cb, cb_user_data))
}
}
pub fn cancel_listen(&mut self, h: &InfoHash, token: Box<OpToken>) {
unsafe {
dht_runner_cancel_listen(&mut *self, h, &*token)
}
}
pub fn shutdown(&mut self,
done_cb: extern fn(bool, *mut c_void),
cb_user_data: *mut c_void)
{
unsafe {
dht_runner_shutdown(&mut *self, done_cb, cb_user_data)
}
}
}
impl Drop for DhtRunner {
fn drop(&mut self) {
unsafe {
dht_runner_delete(&mut *self)
}
}
}
impl Value {
pub fn new(data: &str) -> Box<Value> {
unsafe {
Box::from_raw(dht_value_new(data.as_bytes().as_ptr(),
data.as_bytes().len()))
}
}
fn dataview(&self) -> DataView {
unsafe {
dht_value_get_data(self)
}
}
}
impl Drop for Value {
fn drop(&mut self) {
unsafe {
dht_value_unref(&mut *self)
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe {
let dataview = self.dataview();
let slice = slice::from_raw_parts(dataview.data, dataview.size);
write!(f, "{}", str::from_utf8(slice).unwrap_or(""))
}
}
}
impl Drop for OpToken {
fn drop(&mut self) {
unsafe {
dht_op_token_delete(&mut *self)
}
}
}
impl PublicKey {
pub fn new() -> Box<PublicKey> {
unsafe {
Box::from_raw(dht_publickey_new())
}
}
pub fn unpack(&mut self, data: Vec<u8>) -> i32 {
unsafe {
dht_publickey_unpack(&mut *self, data.as_ptr(), data.len())
}
}
// TODO slice instead of CString
pub fn pack(&mut self, data: &CString) -> i32 {
unsafe {
dht_publickey_pack(&mut *self,
data.as_ptr(),
data.as_bytes().len())
}
}
pub fn id(&self) -> InfoHash {
unsafe {
dht_publickey_get_id(self)
}
}
pub fn long_id(&self) -> PkId {
unsafe {
dht_publickey_get_long_id(self)
}
}
pub fn check_signature(&self, data: &CString, signature: &CString) -> bool {
unsafe {
dht_publickey_check_signature(self,
data.as_ptr(), data.as_bytes().len(),
signature.as_ptr(), signature.as_bytes().len())
}
}
pub fn encrypt(&self, data: &CString) -> Box<Blob> {
unsafe {
Box::from_raw(dht_publickey_encrypt(self,
data.as_ptr(), data.as_bytes().len()))
}
}
}
impl Drop for PublicKey {
fn drop(&mut self) {
unsafe {
dht_publickey_delete(&mut *self)
}
}
}
impl PrivateKey {
pub fn new(key_length_bits: u32) -> Box<PrivateKey> {
unsafe {
Box::from_raw(dht_privatekey_generate(key_length_bits))
}
}
pub fn import(data: &str, password: &CString) -> Box<PrivateKey> {
unsafe {
Box::from_raw(dht_privatekey_import(data.as_ptr(),
data.as_bytes().len(), password.as_ptr()))
}
}
pub fn public_key(&self) -> Box<PublicKey> {
unsafe {
Box::from_raw(dht_privatekey_get_publickey(self))
}
}
}
impl Drop for PrivateKey {
fn drop(&mut self) {
unsafe {
dht_privatekey_delete(&mut *self)
}
}
}
\ No newline at end of file
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::ffi::*;
use std::ffi::CStr;
use std::fmt;
pub use crate::ffi::PkId;
impl fmt::Display for PkId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe {
let self_str = CStr::from_ptr(
dht_pkid_print(self)
).to_str().unwrap_or("");
write!(f, "{}", self_str)
}
}
}
\ No newline at end of file
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::ffi::*;
use std::fmt;
use std::str;
use std::slice;
pub use crate::ffi::{DataView, Value};
impl Value {
pub fn new(data: &str) -> Box<Value> {
unsafe {
Box::from_raw(dht_value_new(data.as_bytes().as_ptr(),
data.as_bytes().len()))
}
}
fn dataview(&self) -> DataView {
unsafe {
dht_value_get_data(self)
}
}
}
impl Drop for Value {
fn drop(&mut self) {
unsafe {
dht_value_unref(&mut *self)
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe {
let dataview = self.dataview();
let slice = slice::from_raw_parts(dataview.data, dataview.size);
write!(f, "{}", str::from_utf8(slice).unwrap_or(""))
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment