Diff
Not logged in

Differences From Artifact [54aa15d45a]:

To Artifact [59d27e6ed2]:


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
...
159
160
161
162
163
164
165


166

167
168
169
170
171
172
173
...
188
189
190
191
192
193
194


195

196
197
198
199
200
201
202
...
259
260
261
262
263
264
265

266
267
268
269
270
271
272
273
...
328
329
330
331
332
333
334

335
336
337
338
339
340
341
342
// 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, version 1.0.
// 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.

//! App-related IPC utilities.

use errors::AppError;
use ffi_utils::{ReprC, catch_unwind_cb, from_c_str};
use safe_core::ipc::{self, AuthReq, ContainersReq, IpcError, IpcMsg, IpcReq, IpcResp};
use safe_core::ipc::req::ffi::AuthReq as FfiAuthReq;
use safe_core::ipc::req::ffi::ContainersReq as FfiContainersReq;
use safe_core::ipc::resp::ffi::AuthGranted as FfiAuthGranted;
use std::ffi::CString;
use std::os::raw::{c_char, c_void};

/// Encode `AuthReq`.
#[no_mangle]
pub unsafe extern "C" fn encode_auth_req(req: *const FfiAuthReq,
                                         user_data: *mut c_void,
                                         o_cb: extern "C" fn(*mut c_void,
                                                             i32,
                                                             u32,
                                                             *const c_char)) {
    catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
        let req_id = ipc::gen_req_id();
        let req = AuthReq::clone_from_repr_c(req)?;

        let msg = IpcMsg::Req {
................................................................................
            req_id: req_id,
            req: IpcReq::Auth(req),
        };

        let encoded = ipc::encode_msg(&msg, "safe-auth")?;
        let encoded = CString::new(encoded)?;

        o_cb(user_data, 0, req_id, encoded.as_ptr());

        Ok(())
    })
}

/// Encode `ContainersReq`.
#[no_mangle]
pub unsafe extern "C" fn encode_containers_req(req: *const FfiContainersReq,
                                               user_data: *mut c_void,
                                               o_cb: extern "C" fn(*mut c_void,
                                                                   i32,
                                                                   u32,
                                                                   *const c_char)) {
    catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
        let req_id = ipc::gen_req_id();
        let req = ContainersReq::clone_from_repr_c(req)?;

        let msg = IpcMsg::Req {
................................................................................
            req_id: req_id,
            req: IpcReq::Containers(req),
        };

        let encoded = ipc::encode_msg(&msg, "safe-auth")?;
        let encoded = CString::new(encoded)?;

        o_cb(user_data, 0, req_id, encoded.as_ptr());

        Ok(())
    })
}

/// Decode IPC message.
#[no_mangle]
................................................................................
pub unsafe extern "C" fn decode_ipc_msg(msg: *const c_char,
                                        user_data: *mut c_void,
                                        o_auth: extern "C" fn(*mut c_void,
                                                              u32,
                                                              *const FfiAuthGranted),
                                        o_containers: extern "C" fn(*mut c_void, u32),
                                        o_revoked: extern "C" fn(*mut c_void),
                                        o_err: extern "C" fn(*mut c_void, i32, u32)) {
    catch_unwind_cb(user_data, o_err, || -> Result<_, AppError> {
        let msg = from_c_str(msg)?;
        let msg = ipc::decode_msg(&msg)?;

        match msg {

            IpcMsg::Resp { resp: IpcResp::Auth(res), req_id } => {


                match res {
                    Ok(auth_granted) => {
                        match auth_granted.into_repr_c() {
                            Ok(auth_granted) => {
                                o_auth(user_data, req_id, &auth_granted);
                            }
                            Err(err) => {
                                o_err(user_data, ffi_error_code!(AppError::from(err)), req_id);







                            }
                        }
                    }

                    Err(err) => o_err(user_data, ffi_error_code!(AppError::from(err)), req_id),





                }

            }



            IpcMsg::Resp { resp: IpcResp::Containers(res), req_id } => {


                match res {
                    Ok(()) => o_containers(user_data, req_id),

                    Err(err) => o_err(user_data, ffi_error_code!(AppError::from(err)), req_id),








                }
            }
            IpcMsg::Revoked { .. } => o_revoked(user_data),
            _ => {
                return Err(IpcError::InvalidMsg.into());
            }
        }
................................................................................
            unsafe { unwrap!(call_2(|ud, cb| encode_auth_req(&req_c, ud, cb))) };

        // Decode it and verify it's the same we encoded.
        assert!(encoded.starts_with("safe-auth:"));
        let msg = unwrap!(ipc::decode_msg(&encoded));

        let (decoded_req_id, decoded_req) = match msg {


            IpcMsg::Req { req_id, req: IpcReq::Auth(req) } => (req_id, req),

            x => panic!("Unexpected {:?}", x),
        };

        assert_eq!(decoded_req_id, req_id);
        assert_eq!(decoded_req, req);
    }

................................................................................
            unsafe { unwrap!(call_2(|ud, cb| encode_containers_req(&req_c, ud, cb))) };

        // Decode it and verify it's the same we encoded.
        assert!(encoded.starts_with("safe-auth:"));
        let msg = unwrap!(ipc::decode_msg(&encoded));

        let (decoded_req_id, decoded_req) = match msg {


            IpcMsg::Req { req_id, req: IpcReq::Containers(req) } => (req_id, req),

            x => panic!("Unexpected {:?}", x),
        };

        assert_eq!(decoded_req_id, req_id);
        assert_eq!(decoded_req, req);
    }

................................................................................
            extern "C" fn revoked_cb(ctx: *mut c_void) {
                unsafe {
                    let ctx = ctx as *mut Context;
                    (*ctx).unexpected_cb = true;
                }
            }


            extern "C" fn err_cb(ctx: *mut c_void, _error_code: i32, _req_id: u32) {
                unsafe {
                    let ctx = ctx as *mut Context;
                    (*ctx).unexpected_cb = true;
                }
            }

            let context_ptr: *mut Context = &mut context;
................................................................................
            extern "C" fn revoked_cb(ctx: *mut c_void) {
                unsafe {
                    let ctx = ctx as *mut Context;
                    (*ctx).unexpected_cb = true;
                }
            }


            extern "C" fn err_cb(ctx: *mut c_void, _error_code: i32, _req_id: u32) {
                unsafe {
                    let ctx = ctx as *mut Context;
                    (*ctx).unexpected_cb = true;
                }
            }

            let context_ptr: *mut Context = &mut context;


|
|
<
|

|
|
<
<
|

|
|
|
<

|
|




|












|







 







|










|







 







|







 







|





>
|
>
>







|
>
>
>
>
>
>
>



>
|
>
>
>
>
>
|
>
|
>
>
>
|
>
>


>
|
>
>
>
>
>
>
>
>







 







>
>
|
>







 







>
>
|
>







 







>
|







 







>
|







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
...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
...
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// 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.

//! App-related IPC utilities.

use errors::AppError;
use ffi_utils::{FFI_RESULT_OK, FfiResult, ReprC, catch_unwind_cb, from_c_str};
use safe_core::ipc::{self, AuthReq, ContainersReq, IpcError, IpcMsg, IpcReq, IpcResp};
use safe_core::ipc::req::ffi::AuthReq as FfiAuthReq;
use safe_core::ipc::req::ffi::ContainersReq as FfiContainersReq;
use safe_core::ipc::resp::ffi::AuthGranted as FfiAuthGranted;
use std::ffi::CString;
use std::os::raw::{c_char, c_void};

/// Encode `AuthReq`.
#[no_mangle]
pub unsafe extern "C" fn encode_auth_req(req: *const FfiAuthReq,
                                         user_data: *mut c_void,
                                         o_cb: extern "C" fn(*mut c_void,
                                                             FfiResult,
                                                             u32,
                                                             *const c_char)) {
    catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
        let req_id = ipc::gen_req_id();
        let req = AuthReq::clone_from_repr_c(req)?;

        let msg = IpcMsg::Req {
................................................................................
            req_id: req_id,
            req: IpcReq::Auth(req),
        };

        let encoded = ipc::encode_msg(&msg, "safe-auth")?;
        let encoded = CString::new(encoded)?;

        o_cb(user_data, FFI_RESULT_OK, req_id, encoded.as_ptr());

        Ok(())
    })
}

/// Encode `ContainersReq`.
#[no_mangle]
pub unsafe extern "C" fn encode_containers_req(req: *const FfiContainersReq,
                                               user_data: *mut c_void,
                                               o_cb: extern "C" fn(*mut c_void,
                                                                   FfiResult,
                                                                   u32,
                                                                   *const c_char)) {
    catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
        let req_id = ipc::gen_req_id();
        let req = ContainersReq::clone_from_repr_c(req)?;

        let msg = IpcMsg::Req {
................................................................................
            req_id: req_id,
            req: IpcReq::Containers(req),
        };

        let encoded = ipc::encode_msg(&msg, "safe-auth")?;
        let encoded = CString::new(encoded)?;

        o_cb(user_data, FFI_RESULT_OK, req_id, encoded.as_ptr());

        Ok(())
    })
}

/// Decode IPC message.
#[no_mangle]
................................................................................
pub unsafe extern "C" fn decode_ipc_msg(msg: *const c_char,
                                        user_data: *mut c_void,
                                        o_auth: extern "C" fn(*mut c_void,
                                                              u32,
                                                              *const FfiAuthGranted),
                                        o_containers: extern "C" fn(*mut c_void, u32),
                                        o_revoked: extern "C" fn(*mut c_void),
                                        o_err: extern "C" fn(*mut c_void, FfiResult, u32)) {
    catch_unwind_cb(user_data, o_err, || -> Result<_, AppError> {
        let msg = from_c_str(msg)?;
        let msg = ipc::decode_msg(&msg)?;

        match msg {
            IpcMsg::Resp {
                resp: IpcResp::Auth(res),
                req_id,
            } => {
                match res {
                    Ok(auth_granted) => {
                        match auth_granted.into_repr_c() {
                            Ok(auth_granted) => {
                                o_auth(user_data, req_id, &auth_granted);
                            }
                            Err(err) => {
                                let e = AppError::from(err);
                                let (error_code, description) = ffi_error!(e);
                                o_err(user_data,
                                      FfiResult {
                                          error_code,
                                          description: description.as_ptr(),
                                      },
                                      req_id);
                            }
                        }
                    }
                    Err(err) => {
                        let e = AppError::from(err);
                        let (error_code, description) = ffi_error!(e);
                        o_err(user_data,
                              FfiResult {
                                  error_code,
                                  description: description.as_ptr(),
                              },
                              req_id);
                    }
                }
            }
            IpcMsg::Resp {
                resp: IpcResp::Containers(res),
                req_id,
            } => {
                match res {
                    Ok(()) => o_containers(user_data, req_id),
                    Err(err) => {
                        let e = AppError::from(err);
                        let (error_code, description) = ffi_error!(e);
                        o_err(user_data,
                              FfiResult {
                                  error_code,
                                  description: description.as_ptr(),
                              },
                              req_id);
                    }
                }
            }
            IpcMsg::Revoked { .. } => o_revoked(user_data),
            _ => {
                return Err(IpcError::InvalidMsg.into());
            }
        }
................................................................................
            unsafe { unwrap!(call_2(|ud, cb| encode_auth_req(&req_c, ud, cb))) };

        // Decode it and verify it's the same we encoded.
        assert!(encoded.starts_with("safe-auth:"));
        let msg = unwrap!(ipc::decode_msg(&encoded));

        let (decoded_req_id, decoded_req) = match msg {
            IpcMsg::Req {
                req_id,
                req: IpcReq::Auth(req),
            } => (req_id, req),
            x => panic!("Unexpected {:?}", x),
        };

        assert_eq!(decoded_req_id, req_id);
        assert_eq!(decoded_req, req);
    }

................................................................................
            unsafe { unwrap!(call_2(|ud, cb| encode_containers_req(&req_c, ud, cb))) };

        // Decode it and verify it's the same we encoded.
        assert!(encoded.starts_with("safe-auth:"));
        let msg = unwrap!(ipc::decode_msg(&encoded));

        let (decoded_req_id, decoded_req) = match msg {
            IpcMsg::Req {
                req_id,
                req: IpcReq::Containers(req),
            } => (req_id, req),
            x => panic!("Unexpected {:?}", x),
        };

        assert_eq!(decoded_req_id, req_id);
        assert_eq!(decoded_req, req);
    }

................................................................................
            extern "C" fn revoked_cb(ctx: *mut c_void) {
                unsafe {
                    let ctx = ctx as *mut Context;
                    (*ctx).unexpected_cb = true;
                }
            }

            #[cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
            extern "C" fn err_cb(ctx: *mut c_void, _res: FfiResult, _req_id: u32) {
                unsafe {
                    let ctx = ctx as *mut Context;
                    (*ctx).unexpected_cb = true;
                }
            }

            let context_ptr: *mut Context = &mut context;
................................................................................
            extern "C" fn revoked_cb(ctx: *mut c_void) {
                unsafe {
                    let ctx = ctx as *mut Context;
                    (*ctx).unexpected_cb = true;
                }
            }

            #[cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
            extern "C" fn err_cb(ctx: *mut c_void, _res: FfiResult, _req_id: u32) {
                unsafe {
                    let ctx = ctx as *mut Context;
                    (*ctx).unexpected_cb = true;
                }
            }

            let context_ptr: *mut Context = &mut context;