1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement. This, along with the Licenses can be
// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.
//! Email Example
// For explanation of lint checks, run `rustc -W help` or see
// https://github.
// com/maidsafe/QA/blob/master/Documentation/Rust%20Lint%20Checks.md
/*
#[macro_use]
extern crate maidsafe_utilities;
#[macro_use]
extern crate unwrap;
extern crate rust_sodium;
extern crate safe_core;
use rust_sodium::crypto::hash::sha256::{self, Digest};
use safe_core::ffi::AppHandle;
use safe_core::ffi::app::*;
use safe_core::ffi::low_level_api::appendable_data::*;
use safe_core::ffi::low_level_api::cipher_opt::*;
use safe_core::ffi::low_level_api::data_id::*;
use safe_core::ffi::low_level_api::immut_data::*;
use safe_core::ffi::low_level_api::misc::*;
use safe_core::ffi::session::*;
use std::os::raw::c_void;
use std::ptr;
use std::sync::mpsc::{self, Sender};
unsafe fn self_auth(session_h: *mut *mut Session) {
unwrap!(maidsafe_utilities::log::init(true));
println!("\nDo you already have an account created (enter Y for yes) ?");
let mut user_option = String::new();
let _ = std::io::stdin().read_line(&mut user_option);
user_option = user_option.trim().to_string();
if user_option != "Y" && user_option != "y" {
println!("\n\tAccount Creation");
println!("\t================");
} else {
println!("\n\n\tAccount Login");
println!("\t====================");
}
let mut secret_0 = String::new();
let mut secret_1 = String::new();
println!("\n------------ Enter account-locator ---------------");
let _ = std::io::stdin().read_line(&mut secret_0);
secret_0 = secret_0.trim().to_string();
println!("\n------------ Enter password ---------------");
let _ = std::io::stdin().read_line(&mut secret_1);
secret_1 = secret_1.trim().to_string();
if user_option != "Y" && user_option != "y" {
println!("\nTrying to create an account ...");
assert_eq!(create_account(secret_0.as_bytes().as_ptr(),
secret_0.as_bytes().len(),
secret_1.as_bytes().as_ptr(),
secret_1.as_bytes().len(),
session_h,
ptr::null_mut(),
network_event_callback),
0);
} else {
println!("\nTrying to log in ...");
assert_eq!(log_in(secret_0.as_bytes().as_ptr(),
secret_0.as_bytes().len(),
secret_1.as_bytes().as_ptr(),
secret_1.as_bytes().len(),
session_h,
ptr::null_mut(),
network_event_callback),
0);
}
}
unsafe fn create_email(session: *const Session, app_h: AppHandle) -> Result<(), i32> {
let mut email = String::new();
println!("\nEnter email name of choice:");
let _ = std::io::stdin().read_line(&mut email);
email = email.trim().to_string();
let Digest(digest) = sha256::hash(email.as_bytes());
let ad_h = try!(c1(|u, cb| appendable_data_new_priv(session, app_h, &digest, u, cb)));
try!(c0(|u, cb| appendable_data_put(session, ad_h, u, cb)));
try!(c0(|u, cb| appendable_data_free(session, ad_h, u, cb)));
println!("Email created successfully !");
Ok(())
}
unsafe fn send_email(sess: *const Session, app_h: AppHandle) -> Result<(), i32> {
let mut email = String::new();
println!("\nEnter peer email address:");
let _ = std::io::stdin().read_line(&mut email);
email = email.trim().to_string();
let mut msg = String::new();
println!("\nEnter message:");
let _ = std::io::stdin().read_line(&mut msg);
msg = msg.trim().to_string();
let Digest(digest) = sha256::hash(email.as_bytes());
let data_id_h = try!(c1(|u, cb| data_id_new_appendable_data(sess, &digest, true, u, cb)));
let ad_h = try!(c1(|u, cb| appendable_data_get(sess, data_id_h, u, cb)));
assert!(c0(|u, cb| data_id_free(sess, data_id_h, u, cb)).is_ok());
let enc_key_h = try!(c1(|u, cb| appendable_data_encrypt_key(sess, ad_h, u, cb)));
let cipher_opt_h = try!(c1(|u, cb| cipher_opt_new_asymmetric(sess, enc_key_h, u, cb)));
try!(c0(|u, cb| misc_encrypt_key_free(sess, enc_key_h, u, cb)));
let se_h = try!(c1(|u, cb| immut_data_new_self_encryptor(sess, u, cb)));
let size = msg.len();
let msg = msg.as_bytes().as_ptr();
try!(c0(|u, cb| immut_data_write_to_self_encryptor(sess, se_h, msg, size, u, cb)));
let data_id_h =
try!(c1(|u, cb| immut_data_close_self_encryptor(sess, app_h, se_h, cipher_opt_h, u, cb)));
try!(c0(|u, cb| appendable_data_append(sess, ad_h, data_id_h, u, cb)));
try!(c0(|u, cb| appendable_data_free(sess, ad_h, u, cb)));
try!(c0(|u, cb| cipher_opt_free(sess, cipher_opt_h, u, cb)));
try!(c0(|u, cb| data_id_free(sess, data_id_h, u, cb)));
println!("Email sent successfully !");
Ok(())
}
unsafe fn read_email(sess: *const Session, app_h: AppHandle) -> Result<(), i32> {
let mut email = String::new();
println!("\nEnter your email address:");
let _ = std::io::stdin().read_line(&mut email);
email = email.trim().to_string();
let Digest(digest) = sha256::hash(email.as_bytes());
let data_id_h = try!(c1(|u, cb| data_id_new_appendable_data(sess, &digest, true, u, cb)));
let ad_h = try!(c1(|u, cb| appendable_data_get(sess, data_id_h, u, cb)));
assert!(c0(|u, cb| data_id_free(sess, data_id_h, u, cb)).is_ok());
let num_of_emails = try!(c1(|u, cb| appendable_data_num_of_data(sess, ad_h, u, cb)));
println!("\n================ You have a total of {} email(s). ================",
num_of_emails);
for n in 0..num_of_emails {
let data_id_h = try!(c1(|u, cb| appendable_data_nth_data_id(sess, app_h, ad_h, n, u, cb)));
let se_h = try!(c1(|u, cb| immut_data_fetch_self_encryptor(sess, app_h, data_id_h, u, cb)));
let total_size = try!(c1(|u, cb| immut_data_size(sess, se_h, u, cb)));
let data = try!(call_vec_u8(|u, cb| {
immut_data_read_from_self_encryptor(sess, se_h, 0, total_size, u, cb)
}));
let data = try!(String::from_utf8(data).map_err(|e| {
println!("Can't decode string: {:?}", e);
-1
}));
println!("\nEmail {}:\n{}", n, data);
try!(c0(|u, cb| data_id_free(sess, data_id_h, u, cb)));
try!(c0(|u, cb| immut_data_self_encryptor_reader_free(sess, se_h, u, cb)));
}
assert!(c0(|u, cb| appendable_data_free(sess, ad_h, u, cb)).is_ok());
println!("\n================ All Emails read successfully ! ================");
Ok(())
}
fn main() {
let mut session: *mut Session = ptr::null_mut();
unsafe {
self_auth(&mut session);
}
let app_name = "EmailApp".to_string();
let unique_token = "EmailApp".to_string();
let vendor = "MaidSafe".to_string();
let app_h;
unsafe {
app_h = unwrap!(c1(|u, cb| {
register_app(session,
app_name.as_bytes().as_ptr(),
app_name.as_bytes().len(),
unique_token.as_bytes().as_ptr(),
unique_token.as_bytes().len(),
vendor.as_bytes().as_ptr(),
vendor.as_bytes().len(),
false,
u,
cb)
}),
"Can't register app");
}
loop {
let mut opt = String::new();
println!("\n0) Create Email\n1) Send Email\n2) Read Email\nx) Anything else to \
exit\nEnter Option:");
let _ = std::io::stdin().read_line(&mut opt);
opt = opt.trim().to_string();
match &opt[..] {
"0" => unsafe { assert!(create_email(session, app_h).is_ok()) },
"1" => unsafe { assert!(send_email(session, app_h).is_ok()) },
"2" => unsafe { assert!(read_email(session, app_h).is_ok()) },
_ => break,
}
}
unsafe {
session_free(session);
}
println!("============================================================\n");
}
// Convert a `mpsc::Sender<T>` to a void ptr which can be passed as user data to
// ffi functions
fn sender_as_user_data<T>(tx: &Sender<T>) -> *mut c_void {
let ptr: *const _ = tx;
ptr as *mut c_void
}
// Send through a `mpsc::Sender` pointed to by the user data pointer.
unsafe fn send_via_user_data<T>(u: *mut c_void, value: T)
where T: Send
{
let tx = u as *mut Sender<T>;
unwrap!((*tx).send(value));
}
// Call a FFI function and block until its callback gets called.
// Use this if the callback accepts no arguments in addition to u
// and error_code.
fn c0<F>(f: F) -> Result<(), i32>
where F: FnOnce(*mut c_void, unsafe extern "C" fn(*mut c_void, i32))
{
let (tx, rx) = mpsc::channel::<i32>();
f(sender_as_user_data(&tx), callback_0);
let error = unwrap!(rx.recv());
if error == 0 { Ok(()) } else { Err(error) }
}
// Call a FFI function and block until its callback gets called, then return
// the argument which were passed to that callback.
// Use this if the callback accepts one argument in addition to u
// and error_code.
unsafe fn c1<F, T>(f: F) -> Result<T, i32>
where F: FnOnce(*mut c_void, unsafe extern "C" fn(*mut c_void, i32, T))
{
let (tx, rx) = mpsc::channel::<(i32, SendWrapper<T>)>();
f(sender_as_user_data(&tx), callback_1::<T>);
let (error, args) = unwrap!(rx.recv());
if error == 0 { Ok(args.0) } else { Err(error) }
}
// Call a FFI function and block until its callback gets called, then return
// the arguments which were passed to that callback in a tuple.
// Use this if the callback accepts three arguments in addition to u and
// error_code.
unsafe fn c3<F, T0, T1, T2>(f: F) -> Result<(T0, T1, T2), i32>
where F: FnOnce(*mut c_void,
unsafe extern "C" fn(*mut c_void, i32, T0, T1, T2))
{
let (tx, rx) = mpsc::channel::<(i32, SendWrapper<(T0, T1, T2)>)>();
f(sender_as_user_data(&tx), callback_3::<T0, T1, T2>);
let (error, args) = unwrap!(rx.recv());
if error == 0 { Ok(args.0) } else { Err(error) }
}
// Call a FFI function and block until its callback gets called, then return
// the arguments which were passed to that callback converted to Vec<u8>.
// The callbacks must accept three arguments (in addition to u and
// error_code): pointer to the begining of the data (`*mut u8`), lengths
// (`usize`)
// and capacity (`usize`).
unsafe fn call_vec_u8<F>(f: F) -> Result<Vec<u8>, i32>
where F: FnOnce(*mut c_void,
unsafe extern "C" fn(*mut c_void, i32, *mut u8, usize, usize))
{
c3(f).map(|(ptr, len, cap)| Vec::from_raw_parts(ptr, len, cap))
}
unsafe extern "C" fn callback_0(user_data: *mut c_void, error: i32) {
send_via_user_data(user_data, error)
}
unsafe extern "C" fn callback_1<T>(user_data: *mut c_void, error: i32, arg: T) {
send_via_user_data(user_data, (error, SendWrapper(arg)))
}
unsafe extern "C" fn callback_3<T0, T1, T2>(user_data: *mut c_void,
error: i32,
arg0: T0,
arg1: T1,
arg2: T2) {
send_via_user_data(user_data, (error, SendWrapper((arg0, arg1, arg2))))
}
// Unsafe wrapper for passing non-Send types through mpsc channels.
// Use with caution!
struct SendWrapper<T>(T);
unsafe impl<T> Send for SendWrapper<T> {}
unsafe extern "C" fn network_event_callback(_user_data: *mut c_void, err_code: i32, event: i32) {
println!("Network event with code {}, err_code: {}", event, err_code);
}
*/