sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Thu, 16 Jan 2020 15:04:04 +0100
branchsync
changeset 4337 7e8015c705ae
parent 4289 79af186cae4e
child 4340 7f1a81347f7c
permissions -rw-r--r--
explicit debug messages in fsm
krista@2271
     1
// This file is under GNU General Public License 3.0
krista@2271
     2
// see LICENSE.txt
krista@2271
     3
krista@2271
     4
// generate state machine code
krista@2271
     5
vb@3512
     6
// Copyleft (c) 2016-2019, p≡p foundation
krista@2271
     7
krista@2271
     8
// Written by Volker Birk
krista@2271
     9
krista@2271
    10
include yslt.yml2
krista@2271
    11
krista@2271
    12
tstylesheet {
krista@2271
    13
    include standardlib.ysl2
krista@2271
    14
    include ./functions.ysl2
krista@2271
    15
krista@2271
    16
    template "/protocol" {
vb@2838
    17
        document "generated/{@name}_event.h", "text"
vb@2838
    18
        ||
vb@2838
    19
        // This file is under GNU General Public License 3.0
vb@2838
    20
        // see LICENSE.txt
vb@2838
    21
vb@2838
    22
        #pragma once
vb@2838
    23
vb@3534
    24
        #include "pEpEngine.h"
vb@2838
    25
vb@2838
    26
        #ifdef __cplusplus
vb@2838
    27
        extern "C" {
vb@2838
    28
        #endif
vb@2838
    29
vb@3020
    30
        typedef struct «@name» «yml:ucase(@name)»;
vb@3020
    31
        typedef int «yml:ucase(@name)»_PR;
vb@2838
    32
vb@2847
    33
        typedef struct «@name»_event {
vb@3534
    34
            // state machine data
vb@3020
    35
            «yml:ucase(@name)»_PR fsm;
vb@2838
    36
            int event;
vb@3020
    37
            «yml:ucase(@name)» *msg;
vb@3534
    38
vb@3534
    39
            // transport data
vb@3534
    40
            pEp_identity *from;
vb@3779
    41
            char *sender_fpr;
vb@3540
    42
vb@3540
    43
            identity_list *own_identities;
vb@2838
    44
        } «@name»_event_t;
vb@2838
    45
vb@2838
    46
 
vb@2838
    47
        // new_«@name»_event() - allocate a new «@name»_event
vb@2838
    48
        //
vb@2838
    49
        //  parameters:
vb@2838
    50
        //      fsm (in)        finite state machine the event is for
vb@2838
    51
        //      event (in)      event or None
vb@2838
    52
        //      msg (in)        message to compute event from
vb@2838
    53
        //
vb@2838
    54
        //  return value:
vb@2838
    55
        //      pointer to new event or NULL in case of failure
vb@2838
    56
        //
vb@2838
    57
        //  caveat:
vb@2838
    58
        //      event must be valid for fsm or None
vb@2838
    59
        //      in case msg is given event will be calculated out of message
vb@2838
    60
vb@4183
    61
        DYNAMIC_API «@name»_event_t *new_«@name»_event(«yml:ucase(@name)»_PR fsm, int event, «yml:ucase(@name)» *msg);
vb@2838
    62
vb@2910
    63
        #define «yml:ucase(@name)»_TIMEOUT_EVENT new_«@name»_event(«@name»_PR_NOTHING, 0, NULL);
vb@2910
    64
vb@3338
    65
    
vb@2838
    66
        // free_«@name»_event() - free memory occupied by event
vb@2838
    67
        //
vb@2838
    68
        //  parameters:
vb@2838
    69
        //      ev (in)         event to free
vb@2838
    70
vb@4183
    71
        DYNAMIC_API void free_«@name»_event(«@name»_event_t *ev);
vb@2838
    72
vb@2838
    73
vb@2838
    74
        #ifdef __cplusplus
vb@2838
    75
        }
vb@2838
    76
        #endif
vb@2838
    77
vb@2838
    78
        ||
vb@2838
    79
vb@2838
    80
        document "generated/{@name}_event.c", "text"
vb@2838
    81
        ||
vb@2838
    82
        // This file is under GNU General Public License 3.0
vb@2838
    83
        // see LICENSE.txt
vb@2838
    84
vb@3062
    85
        #include "platform.h"
vb@3062
    86
vb@2838
    87
        #include "pEp_internal.h"
vb@2838
    88
        #include "«@name»_event.h"
vb@2838
    89
        #include "«@name»_func.h"
vb@2839
    90
        `` for "fsm" | #include "«@name»_fsm.h"
vb@2838
    91
vb@4183
    92
        DYNAMIC_API «@name»_event_t *new_«@name»_event(«yml:ucase(@name)»_PR fsm, int event, «@name»_t *msg)
vb@2838
    93
        {
vb@2838
    94
            «@name»_event_t *ev = («@name»_event_t *) calloc(1, sizeof(«@name»_event_t));
vb@2838
    95
            assert(ev);
vb@2838
    96
            if (!ev)
vb@2838
    97
                return NULL;
vb@2838
    98
vb@2838
    99
            ev->fsm = fsm;
vb@2838
   100
            ev->event = event;
vb@2838
   101
            ev->msg = msg;
vb@2838
   102
vb@2838
   103
            if (msg) {
vb@2838
   104
                switch (fsm) {
vb@2838
   105
                    `` apply "fsm", 3, mode=event
vb@2838
   106
                    default:
vb@2838
   107
                        // unknown protocol
vb@2838
   108
                        free(ev);
vb@2838
   109
                        return NULL;
vb@2838
   110
                }
vb@2838
   111
            }
vb@2838
   112
vb@2838
   113
            return ev;
vb@2838
   114
        }
vb@2838
   115
vb@4183
   116
        DYNAMIC_API void free_«@name»_event(«@name»_event_t *ev)
vb@2838
   117
        {
vb@2838
   118
            if (ev) {
vb@3540
   119
                free_identity_list(ev->own_identities);
vb@2838
   120
                free_«@name»_message(ev->msg);
vb@3534
   121
                free_identity(ev->from);
vb@3779
   122
                free(ev->sender_fpr);
vb@2838
   123
                free(ev);
vb@2838
   124
            }
vb@2838
   125
        }
vb@2838
   126
vb@2838
   127
        ||
vb@2838
   128
vb@2829
   129
        document "generated/{@name}_impl.h", "text" {
vb@2870
   130
            ||
vb@2870
   131
            // This file is under GNU General Public License 3.0
vb@2870
   132
            // see LICENSE.txt
krista@2271
   133
vb@2870
   134
            #pragma once
krista@2271
   135
vb@2870
   136
            #include "fsm_common.h"
vb@2889
   137
            #include "«@name»_event.h"
vb@2870
   138
            #include "message_api.h"
vb@3020
   139
            #include "../asn.1/«@name».h"
vb@2870
   140
            
vb@2909
   141
            #define «yml:ucase(@name)»_THRESHOLD «@threshold»
vb@2909
   142
            `` for "fsm" | #define «yml:ucase(@name)»_THRESHOLD «@threshold»
vb@2909
   143
vb@2870
   144
            #ifdef __cplusplus
vb@2870
   145
            extern "C" {
vb@2870
   146
            #endif
krista@2271
   147
vb@2870
   148
            // conditions
krista@2271
   149
vb@2870
   150
            ||
vb@2870
   151
            for "func:distinctName(*//condition)"
vb@2870
   152
                | PEP_STATUS «@name»(PEP_SESSION session, bool *result);
vb@2870
   153
            ||
vb@2834
   154
vb@2870
   155
            // actions
vb@2829
   156
vb@2870
   157
            ||
vb@2870
   158
            for "func:distinctName(*//action)"
vb@2870
   159
                | PEP_STATUS «@name»(PEP_SESSION session);
vb@2870
   160
            ||
vb@2829
   161
vb@2914
   162
            // timeout handler
vb@2914
   163
            
vb@2914
   164
            ||
vb@2914
   165
            for "fsm[@threshold > 0]"
vb@2914
   166
                | PEP_STATUS «@name»TimeoutHandler(PEP_SESSION session);
vb@2914
   167
            ||
vb@2914
   168
vb@2870
   169
            // send message about an event to communication partners using state
vb@2829
   170
vb@2870
   171
            PEP_STATUS send_«@name»_message(
vb@2870
   172
                    PEP_SESSION session, 
vb@2870
   173
                    «@name»_PR fsm,
vb@2870
   174
                    int message_type
vb@2870
   175
                );
vb@2829
   176
vb@2870
   177
            // receive message and store it in state
vb@2829
   178
vb@2870
   179
            PEP_STATUS recv_«@name»_event(
vb@2870
   180
                    PEP_SESSION session,
vb@2870
   181
                    «@name»_event_t *ev
vb@2870
   182
                );
vb@2870
   183
        
vb@2870
   184
            // state machine driver
vb@2870
   185
            // if fsm or event set to 0 use fields in src if present
vb@2829
   186
vb@2870
   187
            PEP_STATUS «@name»_driver(
vb@2870
   188
                    PEP_SESSION session,
vb@2870
   189
                    «@name»_PR fsm,
vb@2870
   190
                    int event
vb@2870
   191
                );
vb@2829
   192
vb@2888
   193
            // API being used by the engine internally
vb@2888
   194
vb@2888
   195
            // call this if you need to signal an external event
vb@3540
   196
            // caveat: the ownership of own_identities goes to the callee
vb@2888
   197
vb@2880
   198
            PEP_STATUS signal_«@name»_event(
vb@2870
   199
                    PEP_SESSION session, 
vb@2870
   200
                    «@name»_PR fsm,
vb@3540
   201
                    int event,
vb@3540
   202
                    identity_list *own_identities
vb@2870
   203
                );
vb@2888
   204
            
vb@2889
   205
            // call this if you are a transport and are receiving
vb@2889
   206
            // a «@name» message
vb@2829
   207
vb@2888
   208
            PEP_STATUS signal_«@name»_message(
vb@2888
   209
                    PEP_SESSION session, 
vb@2888
   210
                    PEP_rating rating,
vb@2888
   211
                    const char *data,
vb@3143
   212
                    size_t size,
vb@3363
   213
                    const pEp_identity *from,
vb@3779
   214
                    const char *sender_fpr
vb@2888
   215
                );
vb@2829
   216
vb@2870
   217
            #ifdef __cplusplus
vb@2870
   218
            }
vb@2870
   219
            #endif
vb@2829
   220
vb@2870
   221
            ||
vb@2829
   222
        }
vb@2829
   223
vb@2870
   224
        document "generated/{@name}_impl.c", "text" {
vb@2870
   225
            ||
vb@2870
   226
            // This file is under GNU General Public License 3.0
vb@2870
   227
            // see LICENSE.txt
vb@2870
   228
        
vb@2870
   229
            #include "«@name»_impl.h"
vb@2870
   230
            #include "pEp_internal.h"
vb@2870
   231
            #include "«@name»_event.h"
vb@2899
   232
            #include "«yml:lcase(@name)»_codec.h"
vb@2870
   233
            #include "baseprotocol.h"
vb@4251
   234
            #include "security_checks.h"
vb@2870
   235
            `` for "fsm" | #include "«@name»_fsm.h"
vb@2829
   236
vb@2909
   237
            `` apply "fsm", 0, mode=timeout
vb@2870
   238
            PEP_STATUS «@name»_driver(
vb@2870
   239
                    PEP_SESSION session,
vb@2870
   240
                    «@name»_PR fsm,
vb@2870
   241
                    int event
vb@2870
   242
                )
vb@2870
   243
            {
vb@2910
   244
                assert(session);
vb@2910
   245
                if (!session)
vb@2870
   246
                    return PEP_ILLEGAL_VALUE;
vb@2829
   247
vb@2909
   248
                switch (fsm) {
vb@2910
   249
                    case None:
vb@2910
   250
                        if (!event) {
vb@2910
   251
                            // timeout occured
vb@2910
   252
                        `` for "fsm" |>>>> «../@name»_driver(session, «../@name»_PR_«yml:lcase(@name)», None);
vb@2910
   253
                            return PEP_STATUS_OK;
vb@2910
   254
                        }
vb@2910
   255
                        return PEP_ILLEGAL_VALUE;
vb@2910
   256
vb@2909
   257
                    `` apply "fsm", mode=reset_state_machine;
vb@2909
   258
                    default:
vb@2909
   259
                        return PEP_ILLEGAL_VALUE;
vb@2909
   260
                }
vb@2909
   261
vb@2870
   262
                int next_state = None;
vb@2870
   263
                do {
vb@2870
   264
                    switch (fsm) {
vb@2870
   265
                        `` apply "fsm", 3, mode=driver               
vb@2870
   266
                        default:
vb@2870
   267
                            return PEP_ILLEGAL_VALUE;
vb@2870
   268
                    }
vb@2870
   269
                }  while (next_state);
vb@2829
   270
vb@2870
   271
                return PEP_STATUS_OK;
vb@2829
   272
            }
vb@2829
   273
vb@2880
   274
            PEP_STATUS signal_«@name»_event(
vb@2870
   275
                    PEP_SESSION session, 
vb@2870
   276
                    «@name»_PR fsm,
vb@3540
   277
                    int event,
vb@3540
   278
                    identity_list *own_identities
vb@2870
   279
                )
vb@2870
   280
            {
vb@2870
   281
                «@name»_t *msg = NULL;
vb@2870
   282
                «@name»_event_t *ev = NULL;
vb@2870
   283
vb@2870
   284
                assert(session && fsm > 0 && event > None);
vb@2870
   285
                if (!(session && fsm > 0 && event > None))
vb@2870
   286
                    return PEP_ILLEGAL_VALUE;
vb@2870
   287
vb@2870
   288
                PEP_STATUS status = PEP_STATUS_OK;
vb@2870
   289
vb@2888
   290
                if (!session->inject_«yml:lcase(@name)»_event)
vb@2888
   291
                   return PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
vb@2870
   292
vb@2870
   293
                if (event < Extra) {
vb@2870
   294
                    msg = new_«@name»_message(fsm, event);
vb@2870
   295
                    if (!msg) {
vb@2870
   296
                        status = PEP_OUT_OF_MEMORY;
vb@2888
   297
                        goto the_end;
vb@2870
   298
                    }
vb@2870
   299
vb@2870
   300
                    status = update_«@name»_message(session, msg);
vb@2870
   301
                    if (status)
vb@2888
   302
                        goto the_end;
vb@2870
   303
                }
vb@2870
   304
vb@2888
   305
                ev = new_«@name»_event(fsm, event, msg);
vb@2870
   306
                if (!ev) {
vb@2870
   307
                    status = PEP_OUT_OF_MEMORY;
vb@2888
   308
                    goto the_end;
vb@2870
   309
                }
vb@2870
   310
krista@4206
   311
                status = set_all_userids_to_own(session, own_identities);
krista@4206
   312
                if (status != PEP_STATUS_OK)
krista@4206
   313
                    goto the_end;
krista@4206
   314
                    
vb@3540
   315
                ev->own_identities = own_identities;
vb@3540
   316
vb@2870
   317
                int result = session->inject_«yml:lcase(@name)»_event(ev,
vb@2870
   318
                        session->«yml:lcase(@name)»_management);
vb@2870
   319
                if (result) {
vb@2870
   320
                    status = PEP_STATEMACHINE_ERROR;
vb@2888
   321
                    goto the_end;
vb@2870
   322
                }
vb@2888
   323
                return PEP_STATUS_OK;
vb@2870
   324
vb@2870
   325
            the_end:
krista@3331
   326
                free_«@name»_event(ev); // msg gets freed here
vb@2870
   327
                return status;
vb@2870
   328
            }
vb@2870
   329
vb@2888
   330
            PEP_STATUS signal_«@name»_message(
vb@2870
   331
                    PEP_SESSION session, 
vb@2888
   332
                    PEP_rating rating,
vb@2888
   333
                    const char *data,
vb@3143
   334
                    size_t size,
vb@3363
   335
                    const pEp_identity *from,
vb@3779
   336
                    const char *sender_fpr
vb@2870
   337
                )
vb@2870
   338
            {
vb@2888
   339
                assert(session && data && size);
vb@2888
   340
                if (!(session && data && size))
vb@2870
   341
                    return PEP_ILLEGAL_VALUE;
vb@2870
   342
vb@2888
   343
                if (!session->inject_«yml:lcase(@name)»_event)
vb@2888
   344
                   return PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
vb@2870
   345
vb@3341
   346
                PEP_STATUS status = PEP_STATUS_OK;
vb@2890
   347
                «@name»_event_t *ev = NULL;
vb@2890
   348
vb@3341
   349
                «@name»_t *msg = NULL;
vb@3341
   350
                status = decode_«@name»_message(data, size, &msg);
vb@3341
   351
                if (status)
vb@3341
   352
                    return status;
vb@3341
   353
vb@3341
   354
                «@name»_PR fsm = msg->present;
vb@3341
   355
                int event = 0;
vb@3593
   356
                bool is_own_key = false;
vb@3341
   357
vb@3341
   358
                switch (fsm) {
vb@3341
   359
                    `` apply "fsm", 2, mode=signal_message
vb@3341
   360
                    default:
vb@3341
   361
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@3341
   362
                        goto the_end;
vb@3341
   363
                }
vb@3341
   364
vb@3117
   365
                ev = new_«@name»_event(fsm, event, msg);
vb@2888
   366
                if (!ev) {
vb@2829
   367
                    status = PEP_OUT_OF_MEMORY;
vb@2888
   368
                    goto the_end;
vb@2829
   369
                }
vb@2829
   370
vb@3534
   371
                // add transport data
vb@3534
   372
vb@3534
   373
                if (from) {
vb@3534
   374
                    ev->from = identity_dup(from);
vb@3534
   375
                    if (!ev->from) {
vb@3534
   376
                        status = PEP_OUT_OF_MEMORY;
vb@3534
   377
                        goto the_end;
vb@3534
   378
                    }
vb@3534
   379
                }
vb@3534
   380
vb@3779
   381
                if (sender_fpr) {
vb@3779
   382
                    ev->sender_fpr = strdup(sender_fpr);
vb@3779
   383
                    assert(ev->sender_fpr);
vb@3779
   384
                    if (!ev->sender_fpr) {
vb@3534
   385
                        status = PEP_OUT_OF_MEMORY;
vb@3534
   386
                        goto the_end;
vb@3534
   387
                    }
vb@3534
   388
                }
vb@3534
   389
vb@2888
   390
                int result = session->inject_«yml:lcase(@name)»_event(ev,
vb@2888
   391
                        session->«yml:lcase(@name)»_management);
vb@2888
   392
                if (result) {
vb@2888
   393
                    status = PEP_STATEMACHINE_ERROR;
vb@2888
   394
                    goto the_end;
vb@2888
   395
                }
vb@3143
   396
vb@2888
   397
                return PEP_STATUS_OK;
vb@2868
   398
vb@2870
   399
            the_end:
krista@3331
   400
                free_«@name»_event(ev); // msg gets freed here
vb@2870
   401
                return status;
vb@2868
   402
            }
vb@2841
   403
vb@2870
   404
            PEP_STATUS send_«@name»_message(
vb@2870
   405
                    PEP_SESSION session, 
vb@2870
   406
                    «@name»_PR fsm,
vb@2870
   407
                    int message_type
vb@2870
   408
                )
vb@2870
   409
            {
vb@2870
   410
                PEP_STATUS status = PEP_STATUS_OK;
vb@2870
   411
vb@2870
   412
                assert(session && fsm > None && message_type > None);
vb@2870
   413
                if (!(session && fsm > None && message_type > None))
vb@2870
   414
                    return PEP_ILLEGAL_VALUE;
vb@2870
   415
                
vb@2870
   416
                «@name»_t *msg = new_«@name»_message(fsm, message_type);
vb@2870
   417
                if (!msg)
vb@2870
   418
                    return PEP_OUT_OF_MEMORY;
vb@2870
   419
vb@2870
   420
                char *data = NULL;
vb@2870
   421
                message *m = NULL;
vb@2870
   422
                identity_list *channels = NULL;
vb@3545
   423
                char *key_data = NULL;
vb@3545
   424
                size_t key_data_size = 0;
vb@3594
   425
                stringlist_t *extra = NULL;
vb@3594
   426
                bool transaction;
vb@2870
   427
vb@2870
   428
                status = update_«@name»_message(session, msg);
vb@2870
   429
                if (status)
vb@2870
   430
                    goto the_end;
vb@2870
   431
vb@2870
   432
                size_t size = 0;
vb@2870
   433
                status = encode_«@name»_message(msg, &data, &size);
vb@2870
   434
                if (status)
vb@2870
   435
                    goto the_end;
vb@2840
   436
vb@2870
   437
                switch (message_type) {
vb@2871
   438
                    // these messages are being broadcasted
vb@3509
   439
                    `` for "fsm/message[@type='broadcast']" |>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@2871
   440
                        status = _own_identities_retrieve(session, &channels, PEP_idf_not_for_«yml:lcase(@name)»);
vb@2875
   441
                        if (status)
vb@2870
   442
                            goto the_end;
vb@2871
   443
vb@2871
   444
                        if (!(channels && channels->ident)) {
vb@3240
   445
                            // status = PEP_«yml:ucase(@name)»_NO_CHANNEL;
vb@3240
   446
                            // we don't check for having a channel, because if
vb@3240
   447
                            // this is initial setup before having an own
vb@3240
   448
                            // identity we're fine
vb@2871
   449
                            goto the_end;
vb@2871
   450
                        }
vb@2870
   451
                        break;
vb@2829
   452
vb@2877
   453
                    // these go anycast; previously used address is sticky (unicast)
vb@3509
   454
                    `` for "fsm/message[@type='anycast']" |>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3779
   455
                        if (!session->«yml:lcase(@name)»_state.transport.from `> |`|
vb@3779
   456
                            (session->«yml:lcase(@name)»_state.transport.from->flags &
vb@2871
   457
                            PEP_idf_not_for_«yml:lcase(@name)»)) {
vb@2870
   458
vb@2870
   459
                            // no address available yet, try to find one
vb@2871
   460
                            status = _own_identities_retrieve(session, &channels, PEP_idf_not_for_«yml:lcase(@name)»);
krista@3626
   461
                            if (status)
vb@2870
   462
                                goto the_end;
vb@2870
   463
                            break;
vb@2829
   464
vb@2870
   465
                            if (channels && channels->ident) {
vb@2870
   466
                                // only need the first one
vb@2870
   467
                                free_identity_list(channels->next);
vb@2870
   468
                                channels->next = NULL;
vb@2870
   469
                            }
vb@2870
   470
                            else {
vb@2871
   471
                                status = PEP_«yml:ucase(@name)»_NO_CHANNEL;
vb@2870
   472
                                goto the_end;
vb@2870
   473
                            }
vb@2870
   474
                        }
vb@2870
   475
                        else {
vb@3779
   476
                            pEp_identity *channel = identity_dup(session->«yml:lcase(@name)»_state.transport.from);
vb@2870
   477
                            if (!channel) {
vb@2870
   478
                                status = PEP_OUT_OF_MEMORY;
vb@2870
   479
                                goto the_end;
vb@2870
   480
                            }
vb@2829
   481
vb@2870
   482
                            channels = new_identity_list(channel);
vb@2870
   483
                            if (!channels) {
vb@2870
   484
                                status = PEP_OUT_OF_MEMORY;
vb@2870
   485
                                goto the_end;
vb@2870
   486
                            }
vb@2870
   487
                        }
vb@3340
   488
                        break;
vb@2877
   489
vb@2877
   490
                    default:
vb@2877
   491
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2877
   492
                        goto the_end;
vb@2829
   493
                }
vb@2870
   494
vb@2870
   495
                for (identity_list *li = channels; li && li->ident ; li = li->next) {
vb@2878
   496
                    message *_m = NULL;
vb@2944
   497
                    char *_data = NULL;
vb@2944
   498
                    
vb@2944
   499
                    _data = malloc(size);
vb@2944
   500
                    assert(_data);
vb@2944
   501
                    if (!_data) {
vb@2944
   502
                        status = PEP_OUT_OF_MEMORY;
vb@2944
   503
                        goto the_end;
vb@2944
   504
                    }
vb@2944
   505
                    memcpy(_data, data, size);
vb@2875
   506
vb@2878
   507
                    switch (message_type) {
vb@3509
   508
                    `` for "fsm/message[@security='unencrypted']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3129
   509
                            status = base_prepare_message(
vb@3143
   510
                                    session,
vb@3129
   511
                                    li->ident,
vb@3129
   512
                                    li->ident,
vb@4280
   513
                                    BASE_SYNC,
vb@3129
   514
                                    _data,
vb@3129
   515
                                    size,
vb@3129
   516
                                    li->ident->fpr,
vb@3129
   517
                                    &_m
vb@3129
   518
                                );
vb@3129
   519
                            if (status) {
vb@3129
   520
                                free(_data);
vb@3129
   521
                                goto the_end;
vb@3129
   522
                            }
Thomas@3127
   523
                            attach_own_key(session, _m);
krista@3963
   524
                            decorate_message(_m, PEP_rating_undefined, NULL, true, true);
vb@2878
   525
                            m = _m;
vb@2878
   526
                            break;
vb@2878
   527
vb@3594
   528
                    `` for "fsm/message[@security='untrusted']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3594
   529
                            // add fpr of key of comm partner
vb@3594
   530
vb@3779
   531
                            assert(session->«yml:lcase(@name)»_state.comm_partner.sender_fpr);
vb@3779
   532
                            if (!session->«yml:lcase(@name)»_state.comm_partner.sender_fpr) {
vb@3594
   533
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3594
   534
                                goto the_end;
vb@3594
   535
                            }
vb@3594
   536
vb@3779
   537
                            extra = new_stringlist(session->«yml:lcase(@name)»_state.comm_partner.sender_fpr);
vb@3594
   538
                            if (!extra) {
vb@3594
   539
                                status = PEP_OUT_OF_MEMORY;
vb@3594
   540
                                goto the_end;
vb@3594
   541
                            }
vb@3594
   542
vb@3594
   543
                            status = base_prepare_message(
vb@3594
   544
                                    session,
vb@3594
   545
                                    li->ident,
vb@3594
   546
                                    li->ident,
vb@4280
   547
                                    BASE_SYNC,
vb@3594
   548
                                    _data,
vb@3594
   549
                                    size,
vb@3594
   550
                                    NULL,
vb@3594
   551
                                    &_m
vb@3594
   552
                                );
vb@3594
   553
                            if (status) {
vb@3594
   554
                                free(_data);
vb@3594
   555
                                goto the_end;
vb@3594
   556
                            }
vb@3594
   557
vb@3594
   558
                            status = encrypt_message(session, _m, extra, &m, PEP_enc_PEP, 0);
vb@3594
   559
                            if (status) {
vb@3519
   560
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3519
   561
                                goto the_end;
vb@3519
   562
                            }
vb@3924
   563
                            add_opt_field(m, "pEp-auto-consume", "yes");
dz@4064
   564
                            m->in_reply_to = stringlist_add(m->in_reply_to, "pEp-auto-consume@pEp.foundation");
vb@3594
   565
                            free_message(_m);
vb@3594
   566
                            break;
vb@3594
   567
vb@4251
   568
                    `` for "fsm/message[@security='attach_own_keys_for_new_member']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3773
   569
                            // check if we had a former negotiation
vb@3594
   570
vb@3594
   571
                            transaction = false;
vb@3773
   572
                            for (int i=0; i < session->«yml:lcase(@name)»_state.own.negotiation.size; i++) {
vb@3773
   573
                                if (session->«yml:lcase(@name)»_state.own.negotiation.buf[i]) {
vb@3594
   574
                                    transaction = true;
vb@3594
   575
                                    break;
vb@3594
   576
                                }
vb@3594
   577
                            }
vb@3594
   578
    
vb@3594
   579
                            // if it is a former negotiation check if the key
vb@3594
   580
                            // is fully trusted and the sender key of this
vb@3594
   581
                            // transaction; if so add the sender key to extra
vb@3594
   582
                            // keys allowing this new partner to read the
vb@3594
   583
                            // secret keys
vb@3594
   584
vb@3594
   585
                            if (transaction) {
vb@3779
   586
                                assert(session->«yml:lcase(@name)»_state.comm_partner.sender_fpr &&
vb@3779
   587
                                    session->«yml:lcase(@name)»_state.transport.from &&
vb@3779
   588
                                    session->«yml:lcase(@name)»_state.transport.from->user_id);
vb@3779
   589
                                if (!(session->«yml:lcase(@name)»_state.comm_partner.sender_fpr &&
vb@3779
   590
                                        session->«yml:lcase(@name)»_state.transport.from &&
vb@3779
   591
                                        session->«yml:lcase(@name)»_state.transport.from->user_id))
vb@3594
   592
                                {
vb@3594
   593
                                    status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3594
   594
                                    goto the_end;
vb@3594
   595
                                }
vb@3519
   596
                            
vb@3594
   597
                                // test if this is a green channel
vb@3594
   598
vb@3519
   599
                                pEp_identity *ident = new_identity(NULL,
vb@3779
   600
                                        session->«yml:lcase(@name)»_state.comm_partner.sender_fpr,
vb@3779
   601
                                        session->«yml:lcase(@name)»_state.transport.from->user_id,
vb@3519
   602
                                        NULL
vb@3519
   603
                                    );
vb@3519
   604
                                if (!ident) {
vb@3519
   605
                                    status = PEP_OUT_OF_MEMORY;
vb@3519
   606
                                    goto the_end;
vb@3519
   607
                                }
vb@3519
   608
                                status = get_trust(session, ident);
vb@3519
   609
                                if (status) {
vb@3519
   610
                                    free_identity(ident);
vb@3519
   611
                                    goto the_end;
vb@3519
   612
                                }
vb@3519
   613
                                assert(ident->comm_type == PEP_ct_pEp); // we don't deliver otherwise
vb@3519
   614
                                if (ident->comm_type != PEP_ct_pEp) {
vb@3519
   615
                                    free_identity(ident);
vb@3519
   616
                                    status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3519
   617
                                    goto the_end;
vb@3519
   618
                                }
vb@3519
   619
                                free_identity(ident);
vb@3594
   620
vb@3594
   621
                                // test if we accepted this as own key already
vb@3594
   622
vb@3594
   623
                                bool is_own_key = false;
vb@3594
   624
                                status = own_key_is_listed(session,
vb@3779
   625
                                        session->«yml:lcase(@name)»_state.comm_partner.sender_fpr,
vb@3594
   626
                                        &is_own_key);
vb@3594
   627
                                assert(!status);
vb@3594
   628
                                if (status)
vb@3594
   629
                                    goto the_end;
vb@3594
   630
                                assert(is_own_key);
vb@3594
   631
                                if (!is_own_key) {
vb@3594
   632
                                    status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3594
   633
                                    goto the_end;
vb@3594
   634
                                }
vb@3594
   635
vb@3594
   636
                                // if so add key of comm partner to extra keys
vb@3594
   637
vb@3779
   638
                                extra = new_stringlist(session->«yml:lcase(@name)»_state.comm_partner.sender_fpr);
vb@3594
   639
                                if (!extra) {
vb@3594
   640
                                    status = PEP_OUT_OF_MEMORY;
vb@3594
   641
                                    goto the_end;
vb@3594
   642
                                }
vb@3519
   643
                            }
vb@3593
   644
                            
vb@3393
   645
                            status = base_prepare_message(
vb@3393
   646
                                    session,
vb@3393
   647
                                    li->ident,
vb@3393
   648
                                    li->ident,
vb@4280
   649
                                    BASE_SYNC,
vb@3393
   650
                                    _data,
vb@3393
   651
                                    size,
vb@3393
   652
                                    NULL,
vb@3393
   653
                                    &_m
vb@3393
   654
                                );
vb@3393
   655
                            if (status) {
vb@3393
   656
                                free(_data);
vb@3393
   657
                                goto the_end;
vb@3393
   658
                            }
vb@3509
   659
vb@3545
   660
                            // export secret keys into memory
vb@3545
   661
vb@3545
   662
                            key_data = strdup("");
vb@3545
   663
                            assert(key_data);
vb@3545
   664
                            if (!key_data) {
vb@3545
   665
                                free(_data);
vb@3567
   666
                                free_message(_m);
vb@3545
   667
                                status = PEP_OUT_OF_MEMORY;
vb@3545
   668
                                goto the_end;
vb@3545
   669
                            }
krista@3645
   670
                            key_data_size = 1; // N.B. If null termination makes us happy for debugging, fine, but 
krista@3645
   671
                                               // if we include this in the size, libetpan will null terminate and 
krista@3645
   672
                                               // go bananas. We can't have a NUL in the mime text.
vb@3545
   673
vb@3773
   674
                            for (stringlist_t *sl = session->«yml:lcase(@name)»_state.own.keys;
vb@3545
   675
                                    sl && sl->value ; sl = sl->next)
vb@3545
   676
                            {
vb@3545
   677
                                char *_key_data = NULL;
vb@3545
   678
                                size_t _size = 0;
vb@3545
   679
                                status = export_secret_key(session, sl->value, &_key_data, &_size);
vb@3545
   680
                                if (status && status != PEP_KEY_NOT_FOUND) {
vb@3545
   681
                                    free(_data);
vb@3567
   682
                                    free_message(_m);
vb@3545
   683
                                    goto the_end;
vb@3545
   684
                                }
vb@3545
   685
vb@3545
   686
                                if (status != PEP_KEY_NOT_FOUND) {
vb@3545
   687
                                    assert(_key_data && _size);
vb@3545
   688
                                    char *n = realloc(key_data, key_data_size + _size);
vb@3545
   689
                                    if (!n) {
vb@3545
   690
                                        free(_data);
vb@3567
   691
                                        free_message(_m);
vb@3545
   692
                                        status = PEP_OUT_OF_MEMORY;
vb@3545
   693
                                        goto the_end;
vb@3545
   694
                                    }
vb@3545
   695
                                    key_data = n;
vb@3545
   696
                                    key_data_size += _size;
vb@3545
   697
                                    strlcat(key_data, _key_data, key_data_size);
vb@3545
   698
                                    free(_key_data);
vb@3545
   699
                                    _key_data = NULL;
vb@3545
   700
                                }
vb@3545
   701
                                status = export_key(session, sl->value, &_key_data, &_size);
vb@3545
   702
                                if (status && status != PEP_KEY_NOT_FOUND) {
vb@3545
   703
                                    free(_data);
vb@3567
   704
                                    free_message(_m);
vb@3545
   705
                                    goto the_end;
vb@3545
   706
                                }
vb@3545
   707
vb@3545
   708
                                if (status != PEP_KEY_NOT_FOUND) {
vb@3545
   709
                                    assert(_key_data && _size);
vb@3545
   710
                                    char *n = realloc(key_data, key_data_size + _size);
vb@3545
   711
                                    if (!n) {
vb@3545
   712
                                        free(_data);
vb@3567
   713
                                        free_message(_m);
vb@3545
   714
                                        status = PEP_OUT_OF_MEMORY;
vb@3545
   715
                                        goto the_end;
vb@3545
   716
                                    }
vb@3545
   717
                                    key_data = n;
vb@3545
   718
                                    key_data_size += _size;
vb@3545
   719
                                    strlcat(key_data, _key_data, key_data_size);
vb@3545
   720
                                    free(_key_data);
vb@3545
   721
                                    _key_data = NULL;
vb@3545
   722
                                }
vb@3545
   723
                            }
vb@3545
   724
vb@3545
   725
                            // add secret key data as attachment
vb@3545
   726
krista@3645
   727
                            // N.B. The -1 makes sure we do NOT add a NUL into the mime stream!
krista@3645
   728
                            bloblist_t *bl = bloblist_add(_m->attachments, key_data, key_data_size - 1,
vb@3545
   729
                                    "application/octet-stream", "file://own.key");
vb@3545
   730
                            if (!bl) {
vb@3545
   731
                                free(_data);
vb@3567
   732
                                free_message(_m);
vb@3545
   733
                                status = PEP_OUT_OF_MEMORY;
vb@3545
   734
                                goto the_end;
vb@3545
   735
                            }
vb@3545
   736
                            key_data = NULL;
vb@3545
   737
vb@3594
   738
                            status = encrypt_message(session, _m, extra, &m, PEP_enc_PEP, 0);
vb@3393
   739
                            if (status) {
vb@3393
   740
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3393
   741
                                goto the_end;
vb@3393
   742
                            }
vb@3924
   743
                            add_opt_field(m, "pEp-auto-consume", "yes");
dz@4064
   744
                            m->in_reply_to = stringlist_add(m->in_reply_to, "pEp-auto-consume@pEp.foundation");
vb@3393
   745
                            free_message(_m);
vb@3393
   746
                            break;
vb@3393
   747
vb@4251
   748
                    `` for "fsm/message[@security='attach_own_keys_for_group']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@4251
   749
                            status = base_prepare_message(
vb@4251
   750
                                    session,
vb@4251
   751
                                    li->ident,
vb@4251
   752
                                    li->ident,
vb@4280
   753
                                    BASE_SYNC,
vb@4251
   754
                                    _data,
vb@4251
   755
                                    size,
vb@4251
   756
                                    NULL,
vb@4251
   757
                                    &_m
vb@4251
   758
                                );
vb@4251
   759
                            if (status) {
vb@4251
   760
                                free(_data);
vb@4251
   761
                                goto the_end;
vb@4251
   762
                            }
vb@4251
   763
vb@4251
   764
                            // export secret keys into memory
vb@4251
   765
vb@4251
   766
                            key_data = strdup("");
vb@4251
   767
                            assert(key_data);
vb@4251
   768
                            if (!key_data) {
vb@4251
   769
                                free(_data);
vb@4251
   770
                                free_message(_m);
vb@4251
   771
                                status = PEP_OUT_OF_MEMORY;
vb@4251
   772
                                goto the_end;
vb@4251
   773
                            }
vb@4251
   774
                            key_data_size = 1; // N.B. If null termination makes us happy for debugging, fine, but 
vb@4251
   775
                                               // if we include this in the size, libetpan will null terminate and 
vb@4251
   776
                                               // go bananas. We can't have a NUL in the mime text.
vb@4251
   777
vb@4251
   778
                            for (stringlist_t *sl = session->«yml:lcase(@name)»_state.own.keys;
vb@4251
   779
                                    sl && sl->value ; sl = sl->next)
vb@4251
   780
                            {
vb@4251
   781
                                char *_key_data = NULL;
vb@4251
   782
                                size_t _size = 0;
vb@4251
   783
                                status = export_secret_key(session, sl->value, &_key_data, &_size);
vb@4251
   784
                                if (status && status != PEP_KEY_NOT_FOUND) {
vb@4251
   785
                                    free(_data);
vb@4251
   786
                                    free_message(_m);
vb@4251
   787
                                    goto the_end;
vb@4251
   788
                                }
vb@4251
   789
vb@4251
   790
                                if (status != PEP_KEY_NOT_FOUND) {
vb@4251
   791
                                    assert(_key_data && _size);
vb@4251
   792
                                    char *n = realloc(key_data, key_data_size + _size);
vb@4251
   793
                                    if (!n) {
vb@4251
   794
                                        free(_data);
vb@4251
   795
                                        free_message(_m);
vb@4251
   796
                                        status = PEP_OUT_OF_MEMORY;
vb@4251
   797
                                        goto the_end;
vb@4251
   798
                                    }
vb@4251
   799
                                    key_data = n;
vb@4251
   800
                                    key_data_size += _size;
vb@4251
   801
                                    strlcat(key_data, _key_data, key_data_size);
vb@4251
   802
                                    free(_key_data);
vb@4251
   803
                                    _key_data = NULL;
vb@4251
   804
                                }
vb@4251
   805
                                status = export_key(session, sl->value, &_key_data, &_size);
vb@4251
   806
                                if (status && status != PEP_KEY_NOT_FOUND) {
vb@4251
   807
                                    free(_data);
vb@4251
   808
                                    free_message(_m);
vb@4251
   809
                                    goto the_end;
vb@4251
   810
                                }
vb@4251
   811
vb@4251
   812
                                if (status != PEP_KEY_NOT_FOUND) {
vb@4251
   813
                                    assert(_key_data && _size);
vb@4251
   814
                                    char *n = realloc(key_data, key_data_size + _size);
vb@4251
   815
                                    if (!n) {
vb@4251
   816
                                        free(_data);
vb@4251
   817
                                        free_message(_m);
vb@4251
   818
                                        status = PEP_OUT_OF_MEMORY;
vb@4251
   819
                                        goto the_end;
vb@4251
   820
                                    }
vb@4251
   821
                                    key_data = n;
vb@4251
   822
                                    key_data_size += _size;
vb@4251
   823
                                    strlcat(key_data, _key_data, key_data_size);
vb@4251
   824
                                    free(_key_data);
vb@4251
   825
                                    _key_data = NULL;
vb@4251
   826
                                }
vb@4251
   827
                            }
vb@4251
   828
vb@4251
   829
                            // add secret key data as attachment
vb@4251
   830
vb@4251
   831
                            // N.B. The -1 makes sure we do NOT add a NUL into the mime stream!
vb@4251
   832
                            bl = bloblist_add(_m->attachments, key_data, key_data_size - 1,
vb@4251
   833
                                    "application/octet-stream", "file://own.key");
vb@4251
   834
                            if (!bl) {
vb@4251
   835
                                free(_data);
vb@4251
   836
                                free_message(_m);
vb@4251
   837
                                status = PEP_OUT_OF_MEMORY;
vb@4251
   838
                                goto the_end;
vb@4251
   839
                            }
vb@4251
   840
                            key_data = NULL;
vb@4251
   841
vb@4289
   842
                            // we do not support extra keys here and will only encrypt to ourselves
vb@4289
   843
                            status = encrypt_message(session, _m, NULL, &m, PEP_enc_PEP, 0);
vb@4251
   844
                            if (status) {
vb@4251
   845
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@4251
   846
                                goto the_end;
vb@4251
   847
                            }
vb@4251
   848
                            add_opt_field(m, "pEp-auto-consume", "yes");
vb@4251
   849
                            m->in_reply_to = stringlist_add(m->in_reply_to, "pEp-auto-consume@pEp.foundation");
vb@4251
   850
                            free_message(_m);
vb@4251
   851
                            break;
vb@4251
   852
vb@3594
   853
                        default: // security=trusted only
vb@3129
   854
                            status = base_prepare_message(
vb@3143
   855
                                    session,
vb@3129
   856
                                    li->ident,
vb@3129
   857
                                    li->ident,
vb@4280
   858
                                    BASE_SYNC,
vb@3129
   859
                                    _data,
vb@3129
   860
                                    size,
vb@3129
   861
                                    NULL,
vb@3129
   862
                                    &_m
vb@3129
   863
                                );
vb@3129
   864
                            if (status) {
vb@3129
   865
                                free(_data);
vb@3129
   866
                                goto the_end;
vb@3129
   867
                            }
vb@3509
   868
vb@3594
   869
                            status = encrypt_message(session, _m, NULL, &m, PEP_enc_PEP, 0);
vb@2879
   870
                            if (status) {
vb@2899
   871
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@2878
   872
                                goto the_end;
vb@2879
   873
                            }
vb@3924
   874
                            add_opt_field(m, "pEp-auto-consume", "yes");
dz@4064
   875
                            m->in_reply_to = stringlist_add(m->in_reply_to, "pEp-auto-consume@pEp.foundation");
vb@2878
   876
                            free_message(_m);
vb@2878
   877
                    }
vb@2878
   878
vb@2899
   879
                    status = session->messageToSend(m);
vb@2870
   880
                    m = NULL;
vb@2829
   881
                }
vb@2870
   882
vb@2870
   883
            the_end:
vb@3594
   884
                free_stringlist(extra);
vb@2870
   885
                free_identity_list(channels);
vb@2870
   886
                free_message(m);
vb@2870
   887
                free(data);
vb@3545
   888
                free(key_data);
vb@2870
   889
                free_«@name»_message(msg);
vb@3747
   890
                if (status)
vb@3747
   891
                    SERVICE_ERROR_LOG(session, "send_«@name»_message()", status);
vb@2870
   892
                return status;
vb@2829
   893
            }
vb@2829
   894
vb@2870
   895
            PEP_STATUS recv_«@name»_event(
vb@2870
   896
                    PEP_SESSION session,
vb@2870
   897
                    «@name»_event_t *ev
vb@2870
   898
                )
vb@2870
   899
            {
vb@2870
   900
                assert(session && ev);
vb@2870
   901
                if (!(session && ev))
vb@2870
   902
                    return PEP_ILLEGAL_VALUE;
vb@2870
   903
vb@2870
   904
                PEP_STATUS status = PEP_STATUS_OK;
vb@2888
   905
                «@name»_PR fsm = (int) None;
vb@2888
   906
                int event = None;
vb@2870
   907
vb@2910
   908
                if (ev->event > None && ev->event < Extra) {
vb@2870
   909
                    status = update_«@name»_state(session, ev->msg, &fsm, &event);
vb@2870
   910
                    if (status)
vb@2888
   911
                        goto the_end;
vb@2829
   912
vb@2870
   913
                    if (ev->fsm) {
vb@2870
   914
                        if (ev->fsm != fsm |`> |` ev->event != event) {
vb@2870
   915
                            status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2888
   916
                            goto the_end;
vb@2870
   917
                        }
vb@2870
   918
                    }
vb@2888
   919
                    else if (ev->event) {
vb@2888
   920
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2888
   921
                        goto the_end;
vb@2870
   922
                    }
vb@2870
   923
                }
vb@2888
   924
                else {
vb@2888
   925
                    fsm = ev->fsm;
vb@2888
   926
                    event = ev->event;
vb@2888
   927
                }
vb@2870
   928
vb@3534
   929
                // update transport data
vb@3534
   930
vb@3534
   931
                if (ev->from) {
vb@3774
   932
                    free_identity(session->«yml:lcase(@name)»_state.transport.from);
vb@3774
   933
                    session->«yml:lcase(@name)»_state.transport.from = ev->from;
vb@3540
   934
                    ev->from = NULL;
vb@3534
   935
                }
vb@3534
   936
vb@3779
   937
                if (ev->sender_fpr) {
vb@3779
   938
                    free(session->«yml:lcase(@name)»_state.transport.sender_fpr);
vb@3779
   939
                    session->«yml:lcase(@name)»_state.transport.sender_fpr = ev->sender_fpr;
krista@4047
   940
                    
krista@4088
   941
                    /* Removed for temp ENGINE-647 fix. Will be reenabled once better sync debugging is in.
krista@4047
   942
                    // Check against saved comm_partner sender_fpr state, if there is one yet
krista@4047
   943
                    if (session->«yml:lcase(@name)»_state.comm_partner.sender_fpr) {
krista@4047
   944
                        // 1. Does it match sender_fpr?
krista@4047
   945
                        if (strcasecmp(session->«yml:lcase(@name)»_state.comm_partner.sender_fpr, ev->sender_fpr) != 0) {
krista@4047
   946
                            // 2. If not, is it a group key?
krista@4047
   947
                            bool is_own_key = false;
krista@4047
   948
                            status = own_key_is_listed(session, ev->sender_fpr, &is_own_key);
krista@4047
   949
                            if (status)
krista@4047
   950
                                goto the_end;
krista@4047
   951
                            if (!is_own_key) {
krista@4047
   952
                                status = PEP_ILLEGAL_VALUE;
krista@4047
   953
                                goto the_end;
krista@4047
   954
                            }    
krista@4047
   955
                        }
krista@4047
   956
                    }
krista@4088
   957
                    */
krista@4047
   958
                                                            
vb@3779
   959
                    ev->sender_fpr = NULL;
vb@3540
   960
                }
vb@3540
   961
vb@3540
   962
                // update own identities
vb@3540
   963
vb@3540
   964
                if (ev->own_identities && ev->own_identities->ident) {
vb@3773
   965
                    free_identity_list(session->«yml:lcase(@name)»_state.own.identities);
vb@3773
   966
                    session->«yml:lcase(@name)»_state.own.identities = ev->own_identities;
vb@3540
   967
                    ev->own_identities = NULL;
vb@3534
   968
                }
vb@3534
   969
krista@4047
   970
                
vb@2888
   971
                status = «@name»_driver(session, fsm, event);
vb@2870
   972
vb@2888
   973
            the_end:
krista@3331
   974
                //free_«@name»_event(ev); // FIXME: We don't own this pointer. Are we sure it gets freed externally?
vb@2870
   975
                return status;
vb@2870
   976
            }
vb@2870
   977
vb@2870
   978
            ||
vb@2829
   979
        }
vb@2829
   980
vb@2829
   981
        apply "fsm", 0, mode=gen;
vb@2829
   982
    }
vb@2829
   983
vb@2909
   984
    template "fsm", mode=timeout
vb@2909
   985
    ||
vb@2909
   986
    static bool _«@name»_timeout(int state)
vb@2909
   987
    {
vb@2909
   988
        static int last_state = None;
vb@2909
   989
        static time_t switch_time = 0;
vb@2909
   990
vb@2909
   991
        if (state > Init) {
vb@2909
   992
            if (state == last_state) {
vb@2909
   993
                if (time(NULL) - switch_time > «yml:ucase(@name)»_THRESHOLD) {
vb@2909
   994
                    last_state = None;
vb@2909
   995
                    switch_time = 0;
vb@2909
   996
                    return true;
vb@2909
   997
                }
vb@2909
   998
            }
vb@2909
   999
            else {
vb@2909
  1000
                last_state = state;
vb@2909
  1001
                switch_time = time(NULL);
vb@2909
  1002
            }
vb@2909
  1003
        }
vb@2909
  1004
        else {
vb@2909
  1005
            last_state = None;
vb@2909
  1006
            switch_time = 0;
vb@2909
  1007
        }
vb@2909
  1008
vb@2909
  1009
        return false;
vb@2909
  1010
    }
vb@2909
  1011
vb@2909
  1012
    ||
vb@2909
  1013
vb@2909
  1014
    template "fsm", mode=reset_state_machine
vb@2909
  1015
    ||
vb@2909
  1016
        case «../@name»_PR_«yml:lcase(@name)»: {
vb@2909
  1017
            int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
vb@2909
  1018
            switch (state) {
vb@2909
  1019
                `` for "state[@name!='InitState' and @timeout != 'off']" |>>> case «@name»:
vb@2909
  1020
                    if (_«@name»_timeout(state)) {
vb@2909
  1021
                        session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = Init;
vb@2909
  1022
                        event = Init;
vb@2914
  1023
                        `` if "@threshold > 0" |>>>>> «@name»TimeoutHandler(session);
vb@2909
  1024
                    }
vb@2909
  1025
                    break;
vb@2909
  1026
                
vb@2909
  1027
                default:
vb@2909
  1028
                    _«@name»_timeout(None);
vb@2909
  1029
            }
vb@2909
  1030
            break;
vb@2909
  1031
        }
vb@2909
  1032
vb@2909
  1033
    ||
vb@2909
  1034
vb@2890
  1035
    template "fsm", mode=signal_message
vb@2890
  1036
    {
vb@2890
  1037
        ||
vb@2890
  1038
        case «../@name»_PR_«yml:lcase(@name)»:
vb@3509
  1039
            switch (msg->choice.«yml:lcase(@name)».present) {
vb@2891
  1040
        ||
vb@3518
  1041
        for "message[@security='unencrypted']" {
vb@3518
  1042
        if "position()=1" |>> // these messages require a detached signature
vb@3518
  1043
        ||
vb@3518
  1044
                case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3779
  1045
                    if (!sender_fpr) {
vb@3518
  1046
                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3518
  1047
                        goto the_end;
vb@3518
  1048
                    }
vb@3518
  1049
                    event = «@name»;
vb@3518
  1050
                    break;
vb@3338
  1051
vb@3518
  1052
        ||
vb@3338
  1053
        }
vb@3518
  1054
        for "message[@security='untrusted']" {
vb@3518
  1055
        if "position()=1" |>> // these messages must arrive encrypted
vb@3518
  1056
        ||
vb@3518
  1057
                case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3518
  1058
                    if (rating < PEP_rating_reliable) {
vb@3518
  1059
                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3518
  1060
                        goto the_end;
vb@3518
  1061
                    }
vb@3518
  1062
                    event = «@name»;
vb@3518
  1063
                    break;
vb@2890
  1064
vb@3518
  1065
        ||
vb@2891
  1066
        }
vb@3518
  1067
        for "message[@security!='unencrypted' and @security!='untrusted']" {
vb@3518
  1068
        if "position()=1" |>> // these messages must come through a trusted channel
vb@3518
  1069
        ||
vb@3518
  1070
                case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3518
  1071
                    if (rating < PEP_rating_trusted) {
vb@3518
  1072
                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3518
  1073
                        goto the_end;
vb@3518
  1074
                    }
vb@3779
  1075
                    status = own_key_is_listed(session, sender_fpr, &is_own_key);
vb@3593
  1076
                    if (status)
vb@3593
  1077
                        goto the_end;
vb@3593
  1078
                    if (!is_own_key) {
vb@3593
  1079
                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3593
  1080
                        goto the_end;
vb@3593
  1081
                    }
vb@3518
  1082
                    event = «@name»;
vb@3518
  1083
                    break;
vb@2890
  1084
vb@3518
  1085
        ||
vb@3338
  1086
        }
vb@2891
  1087
        ||
vb@2890
  1088
                default:
vb@2890
  1089
                    status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@2890
  1090
                    goto the_end;
vb@2890
  1091
            }
vb@2890
  1092
            break;
vb@2890
  1093
vb@2890
  1094
        ||
vb@2890
  1095
    }
vb@2890
  1096
vb@2838
  1097
    template "fsm", mode=event
vb@2838
  1098
    {
vb@2838
  1099
    ||
vb@2843
  1100
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@3509
  1101
        switch (msg->choice.«yml:lcase(@name)».present) {
vb@2838
  1102
    ||
vb@2838
  1103
    for "message"
vb@2838
  1104
    ||
vb@3509
  1105
            case «../@name»_PR_«yml:mixedCase(@name)»:
vb@2838
  1106
                ev->event = «@name»;
vb@2838
  1107
                break;
vb@2838
  1108
    ||
vb@2838
  1109
    ||
vb@2838
  1110
            default:
vb@2838
  1111
                // unknown message type
vb@2838
  1112
                free(ev);
vb@2838
  1113
                return NULL;
vb@2838
  1114
        }
vb@2838
  1115
        break;
vb@2838
  1116
    }
vb@2838
  1117
vb@2838
  1118
    ||
vb@2838
  1119
    }
vb@2838
  1120
vb@2829
  1121
    template "fsm", mode=driver
vb@2829
  1122
    ||
vb@2843
  1123
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@2839
  1124
        int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
vb@2839
  1125
        next_state = fsm_«@name»(session, state, event);
vb@2838
  1126
        if (next_state > None) {
vb@2838
  1127
            session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = next_state;
vb@2838
  1128
            event = Init;
vb@2838
  1129
        }
vb@2838
  1130
        else if (next_state < None) {
Hussein@3167
  1131
            return PEP_STATEMACHINE_ERROR - next_state;
vb@2838
  1132
        }
vb@2829
  1133
        break;
vb@2829
  1134
    }
vb@2829
  1135
vb@2829
  1136
    ||
vb@2829
  1137
vb@2829
  1138
    template "fsm", mode=gen {
vb@2829
  1139
        document "generated/{@name}_fsm.h", "text" {
vb@2829
  1140
        ||
vb@2829
  1141
        // This file is under GNU General Public License 3.0
vb@2829
  1142
        // see LICENSE.txt
vb@2829
  1143
vb@2829
  1144
        #pragma once
vb@2829
  1145
vb@2829
  1146
        #include "«../@name»_impl.h"
vb@2829
  1147
vb@2829
  1148
        #ifdef __cplusplus
vb@2829
  1149
        extern "C" {
vb@2829
  1150
        #endif
vb@2829
  1151
vb@2829
  1152
        // state machine for «@name»
krista@2271
  1153
krista@2271
  1154
        // states
krista@2271
  1155
krista@2271
  1156
        typedef enum _«@name»_state {
vb@2829
  1157
            «@name»_state_None = None,
vb@2829
  1158
            «@name»_state_Init = Init,
vb@2829
  1159
        ||
vb@2829
  1160
        for "func:distinctName(state[not(@name='InitState')])"
vb@2829
  1161
            |> «@name»`if "position()!=last()" > , `
vb@2829
  1162
        ||
krista@2271
  1163
        } «@name»_state;
krista@2271
  1164
krista@2271
  1165
        // events
krista@2271
  1166
krista@2271
  1167
        typedef enum _«@name»_event {
vb@2829
  1168
            «@name»_event_None = None,
vb@2829
  1169
            «@name»_event_Init = Init,
krista@2271
  1170
        ||
vb@3388
  1171
        for "message" {
krista@2271
  1172
            const "name", "@name";
vb@2829
  1173
            |> «$name» = «/protocol/fsm/message[@name=$name]/@id»,
krista@2271
  1174
        }
vb@2829
  1175
        |> «@name»_event_Extra = Extra,
vb@3387
  1176
        for "external" {
vb@3385
  1177
            if "@id < 128"
vb@3385
  1178
                error > external «@name» must have ID >= 128 but it's «@id»
vb@3384
  1179
            |> «@name» = «@id»,
vb@3385
  1180
        }
vb@2830
  1181
        for "func:distinctName(state/event[not(../../message/@name=@name or ../../external/@name=@name)])" {
vb@2829
  1182
            if "@name!='Init'"
vb@2829
  1183
                |> «@name»`if "position()!=last()" > , `
vb@2829
  1184
        }
krista@2271
  1185
        ||
krista@2271
  1186
        } «@name»_event;
krista@2271
  1187
vb@2829
  1188
        // state machine
krista@2271
  1189
vb@3913
  1190
        #ifndef NDEBUG
vb@2829
  1191
        const char *«@name»_state_name(int state);
vb@2881
  1192
        const char *«@name»_event_name(int event);
vb@3913
  1193
        #endif
krista@2271
  1194
vb@2838
  1195
        // the state machine function is returning the next state in case of a
vb@2838
  1196
        // transition or None for staying
vb@2838
  1197
krista@2271
  1198
        «@name»_state fsm_«@name»(
krista@2271
  1199
                PEP_SESSION session,
krista@2271
  1200
                «@name»_state state,
vb@2829
  1201
                «@name»_event event
krista@2271
  1202
            );
krista@2271
  1203
krista@2271
  1204
        #ifdef __cplusplus
krista@2271
  1205
        }
krista@2271
  1206
        #endif
krista@2271
  1207
krista@2271
  1208
        ||
krista@2271
  1209
        }
vb@2829
  1210
vb@2829
  1211
        document "generated/{@name}_fsm.c", "text" {
krista@2271
  1212
        ||
vb@2829
  1213
        // This file is under GNU General Public License 3.0
vb@2829
  1214
        // see LICENSE.txt
vb@2829
  1215
vb@2829
  1216
        #include "«@name»_fsm.h"
vb@2847
  1217
        #include <stdlib.h>
krista@2271
  1218
vb@3913
  1219
        #ifdef NDEBUG
vb@3913
  1220
        static
vb@3913
  1221
        #endif
vb@2829
  1222
        const char *«@name»_state_name(int state)
vb@2829
  1223
        {
vb@2829
  1224
            switch (state) {
vb@2829
  1225
                case End:
vb@2829
  1226
                    return "End";
vb@2829
  1227
                case None:
vb@2829
  1228
                    return "None";
vb@2829
  1229
                case Init:
vb@2829
  1230
                    return "InitState";
vb@2829
  1231
        ||
vb@2829
  1232
        for "func:distinctName(state[not(@name='InitState')])" {
vb@2829
  1233
            |>> case «@name»:
vb@2829
  1234
            |>>> return "«@name»";
vb@2829
  1235
        }
vb@2829
  1236
        ||
vb@2829
  1237
                default:
vb@4140
  1238
                    assert(0);
vb@2829
  1239
                    return "unknown state";
vb@2829
  1240
            }
vb@2829
  1241
        }
vb@2829
  1242
vb@3913
  1243
        #ifdef NDEBUG
vb@3913
  1244
        static
vb@3913
  1245
        #endif
vb@2881
  1246
        const char *«@name»_event_name(int event)
vb@2881
  1247
        {
vb@2881
  1248
            switch (event) {
vb@2881
  1249
                case None:
vb@2881
  1250
                    return "None";
vb@2881
  1251
                case Init:
vb@2881
  1252
                    return "Init";
vb@2881
  1253
        ||
vb@2881
  1254
        for "func:distinctName(state/event[not(@name='Init')])" {
vb@2881
  1255
            |>> case «@name»:
vb@2881
  1256
            |>>> return "«@name»";
vb@2881
  1257
        }
vb@2881
  1258
        ||
vb@2881
  1259
                default:
vb@4140
  1260
                    assert(0);
vb@2881
  1261
                    return "unknown event";
vb@2881
  1262
            }
vb@2881
  1263
        }
vb@2881
  1264
vb@2881
  1265
vb@2829
  1266
        static char *_str(int n, bool hex)
vb@2829
  1267
        {
vb@2829
  1268
            char *buf = calloc(1, 24);
vb@2829
  1269
            assert(buf);
vb@2829
  1270
            if (!buf)
vb@2829
  1271
                return NULL;
krista@2271
  1272
vb@2829
  1273
            if (hex)
vb@2829
  1274
                snprintf(buf, 24, "%.4x", n);
vb@2829
  1275
            else
vb@2829
  1276
                snprintf(buf, 24, "%d", n);
vb@2829
  1277
            return buf;
vb@2829
  1278
        }
krista@2271
  1279
vb@2829
  1280
        #define «@name»_ERR_LOG(t, d) log_event(session, (t), "«@name»", (d), "error")
vb@2829
  1281
vb@2829
  1282
        static PEP_STATUS _«@name»_ERR_LOG_int(PEP_SESSION session, char *t, int n, bool hex)
vb@2829
  1283
        {
vb@2829
  1284
            char *_buf = _str(n, hex);
vb@2829
  1285
            if (!_buf)
vb@2829
  1286
                return PEP_OUT_OF_MEMORY;
vb@2829
  1287
            PEP_STATUS status = «@name»_ERR_LOG(t, _buf);
vb@2829
  1288
            free(_buf);
vb@2829
  1289
            return status;
vb@2829
  1290
        }
vb@2829
  1291
vb@2829
  1292
        #define «@name»_ERR_LOG_INT(t, n) _«@name»_ERR_LOG_int(session, (t), (n), false)
vb@2829
  1293
        #define «@name»_ERR_LOG_HEX(t, n) _«@name»_ERR_LOG_int(session, (t), (n), true)
vb@2829
  1294
        #define «@name»_SERVICE_LOG(t, d) SERVICE_LOG(session, (t), "«@name»", (d))
vb@2829
  1295
vb@2829
  1296
        «@name»_state fsm_«@name»(
krista@2271
  1297
                PEP_SESSION session,
vb@2829
  1298
                «@name»_state state,
vb@2829
  1299
                «@name»_event event
krista@2271
  1300
            )
krista@2271
  1301
        {
krista@2271
  1302
            assert(session);
krista@2271
  1303
            if (!session)
vb@2829
  1304
                return invalid_state;
krista@2271
  1305
vb@3365
  1306
            if (state == None)
vb@3365
  1307
                state = «@name»_state_Init;
vb@3365
  1308
krista@2271
  1309
            switch (state) {
vb@2829
  1310
                `` apply "state", 2, mode=fsm
krista@2271
  1311
                default:
vb@3912
  1312
                    «@name»_ERR_LOG("invalid state", «@name»_state_name(state));
vb@4140
  1313
                    assert(0);
vb@2829
  1314
                    return invalid_state;
krista@2271
  1315
            }
vb@2829
  1316
            
vb@2838
  1317
            return None;
krista@2271
  1318
        }
krista@2271
  1319
krista@2271
  1320
        ||
vb@2829
  1321
        }
krista@2271
  1322
    }
vb@2829
  1323
    
vb@2829
  1324
    template "state", mode=fsm {
vb@2829
  1325
        choose {
vb@2829
  1326
            when "@name='InitState'" | case «../@name»_state_Init:
vb@2829
  1327
            otherwise | case «@name»:
vb@2829
  1328
        }
krista@2271
  1329
        ||
vb@2829
  1330
            «../@name»_SERVICE_LOG("in state", "«@name»");
krista@2271
  1331
vb@2829
  1332
            switch (event) {
vb@3366
  1333
                case None:
vb@3366
  1334
                    «../@name»_SERVICE_LOG("received None event", "ignoring");
vb@3366
  1335
                    break;
vb@3366
  1336
     
vb@2838
  1337
        ||
vb@2838
  1338
        if "not(event[@name='Init'])"
vb@2838
  1339
        ||
vb@2838
  1340
                case Init:
vb@3379
  1341
                    «../@name»_SERVICE_LOG("received Init but nothing to do", "Init");
vb@2838
  1342
                    break;
vb@2838
  1343
vb@2838
  1344
        ||
vb@2838
  1345
        ||
vb@2829
  1346
                `` apply "event", 2, mode=fsm
vb@2829
  1347
                default:
vb@2885
  1348
                    // ignore events not handled here
vb@3594
  1349
                    «../@name»_SERVICE_LOG("ignoring event", «../@name»_event_name(event));
vb@3362
  1350
                    return invalid_event;
vb@2829
  1351
            }
vb@2829
  1352
            break;
krista@2271
  1353
krista@2271
  1354
        ||
vb@2829
  1355
    }
vb@2829
  1356
vb@2829
  1357
    template "event", mode=fsm {
vb@2829
  1358
        | case «@name»: {
vb@2865
  1359
        if "condition|action|send" |> PEP_STATUS status;
vb@2829
  1360
        if "condition" |> bool result = false;
vb@2865
  1361
        if "condition|action|send" |
vb@2829
  1362
        ||
vb@2829
  1363
            «../../@name»_SERVICE_LOG("received event", "«@name»");
vb@4337
  1364
            `` apply "transition|action|condition|else|send|debug";
vb@2829
  1365
        ||
vb@2829
  1366
        if "name(*[last()])!='transition'" {
vb@2829
  1367
            |
vb@2843
  1368
            |> «../../@name»_SERVICE_LOG("remaining in state", "«../@name»");
vb@2829
  1369
            |> break;
vb@2829
  1370
        }
vb@2829
  1371
        ||
vb@2829
  1372
        }
vb@2829
  1373
        
vb@2829
  1374
        ||
vb@2829
  1375
    }
vb@2829
  1376
vb@2829
  1377
    template "transition" {
vb@2865
  1378
        const "fsm", "ancestor::fsm";
krista@2271
  1379
        ||
krista@2271
  1380
vb@2829
  1381
        «$fsm/@name»_SERVICE_LOG("transition to state", "«@target»");
vb@2838
  1382
        return «@target»;
krista@2271
  1383
        ||
vb@2829
  1384
    }
vb@2829
  1385
vb@2865
  1386
    template "send" {
vb@2865
  1387
        const "fsm", "ancestor::fsm";
vb@2865
  1388
        const "protocol", "ancestor::protocol";
vb@2865
  1389
        ||
krista@2271
  1390
vb@2865
  1391
        «$fsm/@name»_SERVICE_LOG("send message", "«@name»");
vb@3509
  1392
        status = send_«$protocol/@name»_message(session, «$fsm/@id», «$fsm/@name»_PR_«yml:mixedCase(@name)»);
vb@3089
  1393
        if (status == PEP_OUT_OF_MEMORY)
vb@3089
  1394
            return out_of_memory;
vb@3089
  1395
        if (status) {
vb@3089
  1396
            «$fsm/@name»_ERR_LOG_HEX("sending «@name» failed", status);
vb@3089
  1397
            return cannot_send;
vb@3089
  1398
        }
vb@2865
  1399
        ||
vb@2865
  1400
    }
krista@2271
  1401
vb@4337
  1402
    template "debug"
vb@4337
  1403
        | KeySync_SERVICE_LOG("«.»", "«ancestor::protocol/@name»");
vb@4337
  1404
vb@2865
  1405
    template "action" {
vb@2865
  1406
        const "fsm", "ancestor::fsm";
krista@2271
  1407
        ||
vb@2865
  1408
vb@2865
  1409
        «$fsm/@name»_SERVICE_LOG("do action", "«@name»");
vb@2865
  1410
        status = «@name»(session);
vb@3089
  1411
        if (status == PEP_OUT_OF_MEMORY)
vb@3089
  1412
            return out_of_memory;
vb@2829
  1413
        if (status) {
vb@2829
  1414
            «$fsm/@name»_ERR_LOG_HEX("executing action «@name»() failed", status);
vb@4140
  1415
            assert(0);
vb@2829
  1416
            return invalid_action;
krista@2271
  1417
        }
krista@2271
  1418
        ||
krista@2271
  1419
    }
krista@2271
  1420
vb@2829
  1421
    template "condition" {
vb@2865
  1422
        const "fsm", "ancestor::fsm";
krista@2271
  1423
        ||
krista@2271
  1424
vb@2829
  1425
        status = «@name»(session, &result);
vb@3089
  1426
        if (status == PEP_OUT_OF_MEMORY)
vb@3089
  1427
            return out_of_memory;
vb@2829
  1428
        if (status) {
vb@2829
  1429
            «$fsm/@name»_ERR_LOG_HEX("computing condition «@name» failed", status);
vb@4140
  1430
            assert(0);
vb@2829
  1431
            return invalid_condition;
krista@2271
  1432
        }
vb@2829
  1433
        if (result) {
vb@2843
  1434
            «$fsm/@name»_SERVICE_LOG("condition applies", "«@name»");
vb@2829
  1435
        ||
vb@4337
  1436
        apply "transition|action|condition|else|send|debug";
vb@2907
  1437
        | }
vb@2907
  1438
    }
vb@2907
  1439
vb@2907
  1440
    template "else" {
vb@3520
  1441
        if "not(name(preceding-sibling::*[1]) = 'condition')"
vb@2907
  1442
            error "else without if";
vb@2907
  1443
vb@2907
  1444
        | else {
vb@2907
  1445
        |> «ancestor::fsm/@name»_SERVICE_LOG("condition does not apply", "«preceding-sibling::*[last()]/@name»");
vb@4337
  1446
        apply "transition|action|condition|else|send|debug";
krista@2271
  1447
        | }
krista@2271
  1448
    }
krista@2271
  1449
}