src/sync.h
author Volker Birk <vb@pep.foundation>
Thu, 08 Dec 2016 10:34:14 +0100
changeset 1473 a950d0678a33
parent 1471 7b4a5322de16
child 1474 c8ac0e2a7ff2
permissions -rw-r--r--
resolving name clash
vb@1471
     1
/*
vb@1471
     2
====================================
vb@1471
     3
Engine/adapter/app KeySync interface 
vb@1471
     4
====================================
vb@1471
     5
vb@1471
     6
In the engine, KeySync is implemented through a state machine [1]. KeySync
vb@1471
     7
state machine is driven [2] by events, triggering actions [3] and transitions
vb@1471
     8
to new states. Events happens on decryption of email messages, on key
vb@1471
     9
generation, on user interaction through the app and in case of timeout when
vb@1471
    10
staying too long in some particular states.
vb@1471
    11
vb@1471
    12
To use KeySync, the adapter has to create a session dedicated to handle the
vb@1471
    13
protocol, register some callbacks [4] to the engine, and then call protocol's
vb@1471
    14
event consumer loop [5] in a dedicated thread. KeySync actions are executed
vb@1471
    15
as callback invoked from that loop : send pEp messages through app's transport
vb@1471
    16
and display KeySync status and handshake to the user.
vb@1471
    17
vb@1471
    18
When a session is attached [6] to a KeySync session, decryption of pEp (email)
vb@1471
    19
messages in that session may trigger operations in attached KeySync session. In
vb@1471
    20
case of an adapter capable to serve multiple apps, each app is associated to a
vb@1471
    21
different KeySync session, and sessions created for use in that app are
vb@1471
    22
attached to that session.
vb@1471
    23
vb@1471
    24
KeySync messages [7], not to be confused with pEp (email) messages, are either
vb@1471
    25
directly events to be processed by the state machine or KeySync payloads
vb@1471
    26
collected from decrypted messages. They are jobs to be processed by the state
vb@1471
    27
machine.
vb@1471
    28
vb@1471
    29
KeySync messages can be emitted by multiple session, and could naturally come
vb@1471
    30
from different threads. They must be serialized in a locked queue. Attached
vb@1471
    31
sessions inject [8] KeySync messages in the queue. Protocol loop retrieves [9]
vb@1471
    32
them from the queue. KeySync message is received [10] by the state machine,
vb@1471
    33
where event eventually deduced from payload.
vb@1471
    34
vb@1471
    35
A state timeout event is a particular case. It doesn't traverse the queue, and
vb@1471
    36
isn't emitted by a session. It is triggered by a timeout on the retrieve
vb@1471
    37
operation. Value of the timeout is determined when entering a new state, and is
vb@1471
    38
passed as a parameter of the call to the blocking queue retrieve operation on 
vb@1471
    39
next protocol loop iteraton.
vb@1471
    40
vb@1471
    41
[1] sync/device_group.fsm , src/sync_fsm.c (generated)
vb@1471
    42
[2] src/sync_driver.c (generated)
vb@1471
    43
[3] src/sync_actions.c , src/sync_send_actions.c (generated)
vb@1471
    44
[4] register_sync_callbacks()
vb@1471
    45
[5] do_sync_protocol()
vb@1471
    46
[6] attach_sync_session()
vb@1471
    47
[7] type sync_msg_t
vb@1471
    48
[8] callback inject_sync_msg
vb@1471
    49
[9] callback retrieve_next_sync_msg
vb@1471
    50
[10] receive_sync_msg() (src/sync_impl.c)
vb@1471
    51
vb@1471
    52
*/
vb@1471
    53
vb@572
    54
#pragma once
vb@572
    55
vb@572
    56
#include "message.h"
vb@690
    57
#include "sync_fsm.h"
vb@572
    58
vb@1470
    59
vb@1470
    60
// this module is for being used WITHOUT the Transport API in transport.h
vb@1470
    61
// DO NOT USE IT WHEN USING Transport API!
vb@1470
    62
vb@1470
    63
vb@572
    64
#ifdef __cplusplus
vb@572
    65
extern "C" {
vb@572
    66
#endif
vb@572
    67
vb@1043
    68
// messageToSend() - send a message
vb@572
    69
//
vb@572
    70
//  parameters:
vb@599
    71
//      obj (in)        object handle (implementation defined)
vb@598
    72
//      msg (in)        message struct with message to send
vb@572
    73
//
vb@572
    74
//  return value:
vb@604
    75
//      PEP_STATUS_OK or any other value on error
vb@991
    76
//
vb@991
    77
//  caveat:
vb@991
    78
//      the ownership of msg goes to the callee
vb@572
    79
vb@991
    80
typedef PEP_STATUS (*messageToSend_t)(void *obj, message *msg);
vb@572
    81
edouard@1459
    82
typedef enum _sync_handshake_signal {
vb@1473
    83
    SYNC_NOTIFY_UNDEFINED = 0,
vb@1470
    84
vb@1470
    85
    // request show handshake dialog
vb@1473
    86
    SYNC_NOTIFY_INIT_ADD_OUR_DEVICE,
vb@1473
    87
    SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE,
vb@1473
    88
    SYNC_NOTIFY_INIT_FORM_GROUP,
vb@1470
    89
vb@1470
    90
    // handshake process was cancelled
vb@1473
    91
    SYNC_NOTIFY_CANCELED,
vb@1470
    92
vb@1470
    93
    // handshake accepted by user
vb@1473
    94
    SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED,
vb@1473
    95
    SYNC_NOTIFY_ACCEPTED_GROUP_CREATED,
vb@1470
    96
vb@1470
    97
    // handshake was rejected by user
vb@1473
    98
    SYNC_NOTIFY_REJECTED
edouard@1459
    99
} sync_handshake_signal;
vb@572
   100
edouard@1459
   101
// notifyHandshake() - notify UI about sync handshaking process
vb@572
   102
//
vb@572
   103
//  parameters:
vb@599
   104
//      obj (in)        object handle (implementation defined)
vb@1003
   105
//      me (in)         own identity
vb@572
   106
//      partner (in)    identity of partner
edouard@1459
   107
//      signal (in)     reason of the notification
vb@572
   108
//
vb@572
   109
//  return value:
vb@604
   110
//      PEP_STATUS_OK or any other value on error
vb@991
   111
//
vb@991
   112
//  caveat:
vb@991
   113
//      ownership of self and partner go to the callee
vb@572
   114
edouard@1459
   115
typedef PEP_STATUS (*notifyHandshake_t)(
vb@599
   116
        void *obj,
vb@1003
   117
        pEp_identity *me,
edouard@1459
   118
        pEp_identity *partner,
edouard@1459
   119
        sync_handshake_signal signal
vb@609
   120
    );
vb@609
   121
edouard@1459
   122
typedef enum _sync_handshake_result {
edouard@1459
   123
    SYNC_HANDSHAKE_CANCEL = -1,
edouard@1459
   124
    SYNC_HANDSHAKE_ACCEPTED = 0,
edouard@1459
   125
    SYNC_HANDSHAKE_REJECTED = 1
edouard@1459
   126
} sync_handshake_result;
vb@609
   127
vb@609
   128
// deliverHandshakeResult() - give the result of the handshake dialog
vb@609
   129
//
vb@609
   130
//  parameters:
vb@1043
   131
//      session (in)        session handle
vb@1043
   132
//      result (in)         handshake result
vb@609
   133
vb@679
   134
DYNAMIC_API PEP_STATUS deliverHandshakeResult(
vb@609
   135
        PEP_SESSION session,
edouard@1160
   136
        Identity partner,
vb@609
   137
        sync_handshake_result result
vb@572
   138
    );
vb@572
   139
edouard@1172
   140
// sync_msg_t - items queued for serialized handling by protocol engine
edouard@1172
   141
typedef struct _sync_msg_t sync_msg_t;
vb@572
   142
vb@1043
   143
// inject_sync_msg - inject sync protocol message
vb@1043
   144
//
vb@1043
   145
//  parameters:
vb@1043
   146
//      msg (in)            message to inject
vb@1043
   147
//      management (in)     application defined
vb@1043
   148
//
vb@1043
   149
//  return value:
vb@1043
   150
//      0 if msg could be stored successfully or nonzero otherwise
vb@1043
   151
vb@1043
   152
typedef int (*inject_sync_msg_t)(void *msg, void *management);
vb@1043
   153
vb@1043
   154
vb@1470
   155
// retrieve_next_sync_msg - receive next sync message
vb@1043
   156
//
vb@1043
   157
//  parameters:
vb@1043
   158
//      management (in)     application defined
vb@1470
   159
//      timeout (in,out)    do not wait longer than timeout for message
vb@1043
   160
//
vb@1043
   161
//  return value:
edouard@1445
   162
//      next message or :
vb@1471
   163
//      NULL and timeout == 0 for termination
vb@1471
   164
//      NULL and timeout != 0 for timeout occurence
vb@1043
   165
edouard@1445
   166
typedef void *(*retrieve_next_sync_msg_t)(void *management, time_t *timeout);
vb@1043
   167
vb@1043
   168
vb@572
   169
// register_sync_callbacks() - register adapter's callbacks
vb@572
   170
//
vb@572
   171
//  parameters:
vb@599
   172
//      session (in)                session where to store obj handle
edouard@1462
   173
//      management (in)             application defined
vb@599
   174
//      messageToSend (in)          callback for sending message
edouard@1462
   175
//      notifyHandshake (in)        callback for doing the handshake
vb@1043
   176
//      retrieve_next_sync_msg (in) callback for receiving sync messages
vb@572
   177
//
vb@572
   178
//  return value:
vb@572
   179
//      PEP_STATUS_OK or any other value on errror
vb@573
   180
//
vb@573
   181
//  caveat:
vb@573
   182
//      call that BEFORE you're using any other part of the engine
vb@572
   183
vb@572
   184
DYNAMIC_API PEP_STATUS register_sync_callbacks(
vb@599
   185
        PEP_SESSION session,
edouard@1462
   186
        void *management,
vb@597
   187
        messageToSend_t messageToSend,
edouard@1459
   188
        notifyHandshake_t notifyHandshake,
vb@1043
   189
        inject_sync_msg_t inject_sync_msg,
vb@1043
   190
        retrieve_next_sync_msg_t retrieve_next_sync_msg
vb@572
   191
    );
vb@572
   192
edouard@1236
   193
// attach_sync_session() - attach session to a session running keysync state machine 
edouard@1236
   194
//
edouard@1236
   195
//  parameters:
edouard@1236
   196
//      session (in)                session to attach
edouard@1236
   197
//      sync_session (in)           session running keysync
edouard@1236
   198
//
edouard@1236
   199
//  return value:
edouard@1236
   200
//      PEP_STATUS_OK or any other value on errror
edouard@1236
   201
//
edouard@1236
   202
//  caveat:
edouard@1236
   203
//      register_sync_callbacks must have been called on sync_session
edouard@1236
   204
//      call that BEFORE you're using that session in any other part of the engine
edouard@1236
   205
edouard@1236
   206
DYNAMIC_API PEP_STATUS attach_sync_session(
edouard@1236
   207
        PEP_SESSION session,
edouard@1236
   208
        PEP_SESSION sync_session
edouard@1236
   209
    );
edouard@1236
   210
edouard@1236
   211
// detach_sync_session() - detach previously attached sync session
edouard@1236
   212
//
edouard@1236
   213
//  parameters:
edouard@1236
   214
//      session (in)                session to detach 
edouard@1236
   215
edouard@1236
   216
DYNAMIC_API PEP_STATUS detach_sync_session(PEP_SESSION session);
vb@572
   217
vb@572
   218
// unregister_sync_callbacks() - unregister adapter's callbacks
vb@602
   219
//
vb@602
   220
//  parameters:
edouard@1236
   221
//      session (in)                session to unregister
vb@572
   222
vb@602
   223
DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session);
vb@572
   224
vb@1116
   225
// do_sync_protocol() - function to be run on an extra thread
vb@1043
   226
//
vb@1043
   227
//  parameters:
vb@1043
   228
//      session                 pEp session to use
vb@1043
   229
//      retrieve_next_sync_msg  pointer to retrieve_next_identity() callback
vb@1043
   230
//                              which returns at least a valid address field in
vb@1043
   231
//                              the identity struct
edouard@1462
   232
//      obj                     application defined sync object
vb@1043
   233
//
vb@1043
   234
//  return value:
vb@1043
   235
//      PEP_STATUS_OK if thread has to terminate successfully or any other
vb@1043
   236
//      value on failure
vb@1043
   237
//
vb@1043
   238
//  caveat:
vb@1043
   239
//      to ensure proper working of this library, a thread has to be started
vb@1043
   240
//      with this function immediately after initialization
vb@1043
   241
vb@1043
   242
DYNAMIC_API PEP_STATUS do_sync_protocol(
vb@1043
   243
        PEP_SESSION session,
edouard@1462
   244
        void *obj
vb@1043
   245
    );
vb@1043
   246
Edouard@1203
   247
// free_sync_msg() - free sync_msg_t struct when not passed to do_sync_protocol  
Edouard@1203
   248
//
Edouard@1203
   249
//  parameters:
Edouard@1203
   250
//      sync_msg (in)            pointer to sync_msg_t struct to free
Edouard@1203
   251
Edouard@1203
   252
DYNAMIC_API void free_sync_msg(sync_msg_t *sync_msg);
vb@1043
   253
vb@1116
   254
// decode_sync_msg() - decode sync message from PER into XER
vb@1116
   255
//
vb@1116
   256
//  parameters:
vb@1116
   257
//      data (in)               PER encoded data
vb@1116
   258
//      size (in)               size of PER encoded data
vb@1116
   259
//      text (out)              XER text of the same sync message
vb@1116
   260
vb@1116
   261
DYNAMIC_API PEP_STATUS decode_sync_msg(
vb@1116
   262
        const char *data,
vb@1116
   263
        size_t size,
vb@1116
   264
        char **text
vb@1116
   265
    );
vb@1116
   266
vb@1116
   267
vb@1128
   268
// encode_sync_msg() - encode sync message from XER into PER
vb@1128
   269
//
vb@1128
   270
//  parameters:
vb@1128
   271
//      text (in)               string with XER text of the sync message
vb@1128
   272
//      data (out)              PER encoded data
vb@1128
   273
//      size (out)              size of PER encoded data
vb@1128
   274
vb@1128
   275
DYNAMIC_API PEP_STATUS encode_sync_msg(
vb@1128
   276
        const char *text,
vb@1128
   277
        char **data,
vb@1128
   278
        size_t *size
vb@1128
   279
    );
vb@1128
   280
vb@1128
   281
vb@572
   282
#ifdef __cplusplus
vb@572
   283
}
vb@572
   284
#endif
vb@572
   285