sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Thu, 21 Mar 2019 12:54:20 +0100
branchsync
changeset 3363 9095efc623dd
parent 3362 a90c5e2081c3
child 3365 cbc208d0e104
permissions -rw-r--r--
store signature fpr in sync state
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@2840
     6
// Copyleft (c) 2016 - 2018, 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@2838
    24
        #include "dynamic_api.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@3020
    34
            «yml:ucase(@name)»_PR fsm;
vb@2838
    35
            int event;
vb@3020
    36
            «yml:ucase(@name)» *msg;
vb@2838
    37
        } «@name»_event_t;
vb@2838
    38
vb@2838
    39
 
vb@2838
    40
        // new_«@name»_event() - allocate a new «@name»_event
vb@2838
    41
        //
vb@2838
    42
        //  parameters:
vb@2838
    43
        //      fsm (in)        finite state machine the event is for
vb@2838
    44
        //      event (in)      event or None
vb@2838
    45
        //      msg (in)        message to compute event from
vb@2838
    46
        //
vb@2838
    47
        //  return value:
vb@2838
    48
        //      pointer to new event or NULL in case of failure
vb@2838
    49
        //
vb@2838
    50
        //  caveat:
vb@2838
    51
        //      event must be valid for fsm or None
vb@2838
    52
        //      in case msg is given event will be calculated out of message
vb@2838
    53
vb@3020
    54
        «@name»_event_t *new_«@name»_event(«yml:ucase(@name)»_PR fsm, int event, «yml:ucase(@name)» *msg);
vb@2838
    55
vb@2910
    56
        #define «yml:ucase(@name)»_TIMEOUT_EVENT new_«@name»_event(«@name»_PR_NOTHING, 0, NULL);
vb@2910
    57
vb@3338
    58
    
vb@2838
    59
        // free_«@name»_event() - free memory occupied by event
vb@2838
    60
        //
vb@2838
    61
        //  parameters:
vb@2838
    62
        //      ev (in)         event to free
vb@2838
    63
vb@2899
    64
        void free_«@name»_event(«@name»_event_t *ev);
vb@2838
    65
vb@2838
    66
vb@2838
    67
        #ifdef __cplusplus
vb@2838
    68
        }
vb@2838
    69
        #endif
vb@2838
    70
vb@2838
    71
        ||
vb@2838
    72
vb@2838
    73
        document "generated/{@name}_event.c", "text"
vb@2838
    74
        ||
vb@2838
    75
        // This file is under GNU General Public License 3.0
vb@2838
    76
        // see LICENSE.txt
vb@2838
    77
vb@3062
    78
        #include "platform.h"
vb@3062
    79
vb@2838
    80
        #include "pEp_internal.h"
vb@2838
    81
        #include "«@name»_event.h"
vb@2838
    82
        #include "«@name»_func.h"
vb@2839
    83
        `` for "fsm" | #include "«@name»_fsm.h"
vb@2838
    84
vb@3020
    85
        «@name»_event_t *new_«@name»_event(«yml:ucase(@name)»_PR fsm, int event, «@name»_t *msg)
vb@2838
    86
        {
vb@2838
    87
            «@name»_event_t *ev = («@name»_event_t *) calloc(1, sizeof(«@name»_event_t));
vb@2838
    88
            assert(ev);
vb@2838
    89
            if (!ev)
vb@2838
    90
                return NULL;
vb@2838
    91
vb@2838
    92
            ev->fsm = fsm;
vb@2838
    93
            ev->event = event;
vb@2838
    94
            ev->msg = msg;
vb@2838
    95
vb@2838
    96
            if (msg) {
vb@2838
    97
                switch (fsm) {
vb@2838
    98
                    `` apply "fsm", 3, mode=event
vb@2838
    99
                    default:
vb@2838
   100
                        // unknown protocol
vb@2838
   101
                        free(ev);
vb@2838
   102
                        return NULL;
vb@2838
   103
                }
vb@2838
   104
            }
vb@2838
   105
vb@2838
   106
            return ev;
vb@2838
   107
        }
vb@2838
   108
vb@2899
   109
        void free_«@name»_event(«@name»_event_t *ev)
vb@2838
   110
        {
vb@2838
   111
            if (ev) {
vb@2838
   112
                free_«@name»_message(ev->msg);
vb@2838
   113
                free(ev);
vb@2838
   114
            }
vb@2838
   115
        }
vb@2838
   116
vb@2838
   117
        ||
vb@2838
   118
vb@2829
   119
        document "generated/{@name}_impl.h", "text" {
vb@2870
   120
            ||
vb@2870
   121
            // This file is under GNU General Public License 3.0
vb@2870
   122
            // see LICENSE.txt
krista@2271
   123
vb@2870
   124
            #pragma once
krista@2271
   125
vb@2870
   126
            #include "fsm_common.h"
vb@2889
   127
            #include "«@name»_event.h"
vb@2870
   128
            #include "message_api.h"
vb@3020
   129
            #include "../asn.1/«@name».h"
vb@2870
   130
            
vb@2909
   131
            #define «yml:ucase(@name)»_THRESHOLD «@threshold»
vb@2909
   132
            `` for "fsm" | #define «yml:ucase(@name)»_THRESHOLD «@threshold»
vb@2909
   133
vb@2870
   134
            #ifdef __cplusplus
vb@2870
   135
            extern "C" {
vb@2870
   136
            #endif
vb@2870
   137
vb@2870
   138
            // conditions
vb@2870
   139
vb@2870
   140
            ||
vb@2870
   141
            for "func:distinctName(*//condition)"
vb@2870
   142
                | PEP_STATUS «@name»(PEP_SESSION session, bool *result);
vb@2870
   143
            ||
vb@2870
   144
vb@2870
   145
            // actions
vb@2870
   146
vb@2870
   147
            ||
vb@2870
   148
            for "func:distinctName(*//action)"
vb@2870
   149
                | PEP_STATUS «@name»(PEP_SESSION session);
vb@2870
   150
            ||
vb@2870
   151
vb@2914
   152
            // timeout handler
vb@2914
   153
            
vb@2914
   154
            ||
vb@2914
   155
            for "fsm[@threshold > 0]"
vb@2914
   156
                | PEP_STATUS «@name»TimeoutHandler(PEP_SESSION session);
vb@2914
   157
            ||
vb@2914
   158
vb@2870
   159
            // send message about an event to communication partners using state
vb@2870
   160
vb@2870
   161
            PEP_STATUS send_«@name»_message(
vb@2870
   162
                    PEP_SESSION session, 
vb@2870
   163
                    «@name»_PR fsm,
vb@2870
   164
                    int message_type
vb@2870
   165
                );
vb@2870
   166
vb@2870
   167
            // receive message and store it in state
vb@2870
   168
vb@2870
   169
            PEP_STATUS recv_«@name»_event(
vb@2870
   170
                    PEP_SESSION session,
vb@2870
   171
                    «@name»_event_t *ev
vb@2870
   172
                );
krista@2271
   173
        
vb@2870
   174
            // state machine driver
vb@2870
   175
            // if fsm or event set to 0 use fields in src if present
krista@2271
   176
vb@2870
   177
            PEP_STATUS «@name»_driver(
vb@2870
   178
                    PEP_SESSION session,
vb@2870
   179
                    «@name»_PR fsm,
vb@2870
   180
                    int event
vb@2870
   181
                );
krista@2271
   182
vb@2888
   183
            // API being used by the engine internally
vb@2888
   184
vb@2888
   185
            // call this if you need to signal an external event
vb@2888
   186
vb@2880
   187
            PEP_STATUS signal_«@name»_event(
vb@2870
   188
                    PEP_SESSION session, 
vb@2870
   189
                    «@name»_PR fsm,
vb@2870
   190
                    int event
vb@2870
   191
                );
vb@2888
   192
            
vb@2889
   193
            // call this if you are a transport and are receiving
vb@2889
   194
            // a «@name» message
vb@2834
   195
vb@2888
   196
            PEP_STATUS signal_«@name»_message(
vb@2888
   197
                    PEP_SESSION session, 
vb@2888
   198
                    PEP_rating rating,
vb@2888
   199
                    const char *data,
vb@3143
   200
                    size_t size,
vb@3363
   201
                    const pEp_identity *from,
vb@3363
   202
                    const char *signature_fpr
vb@2888
   203
                );
vb@2829
   204
vb@2870
   205
            #ifdef __cplusplus
vb@2870
   206
            }
vb@2870
   207
            #endif
vb@2829
   208
vb@2870
   209
            ||
vb@2829
   210
        }
vb@2829
   211
vb@2870
   212
        document "generated/{@name}_impl.c", "text" {
vb@2870
   213
            ||
vb@2870
   214
            // This file is under GNU General Public License 3.0
vb@2870
   215
            // see LICENSE.txt
vb@2870
   216
        
vb@2870
   217
            #include "«@name»_impl.h"
vb@2870
   218
            #include "pEp_internal.h"
vb@2870
   219
            #include "«@name»_event.h"
vb@2899
   220
            #include "«yml:lcase(@name)»_codec.h"
vb@2870
   221
            #include "baseprotocol.h"
vb@2870
   222
            `` for "fsm" | #include "«@name»_fsm.h"
vb@2829
   223
vb@2909
   224
            `` apply "fsm", 0, mode=timeout
vb@2870
   225
            PEP_STATUS «@name»_driver(
vb@2870
   226
                    PEP_SESSION session,
vb@2870
   227
                    «@name»_PR fsm,
vb@2870
   228
                    int event
vb@2870
   229
                )
vb@2870
   230
            {
vb@2910
   231
                assert(session);
vb@2910
   232
                if (!session)
vb@2870
   233
                    return PEP_ILLEGAL_VALUE;
vb@2829
   234
vb@2909
   235
                switch (fsm) {
vb@2910
   236
                    case None:
vb@2910
   237
                        if (!event) {
vb@2910
   238
                            // timeout occured
vb@2910
   239
                        `` for "fsm" |>>>> «../@name»_driver(session, «../@name»_PR_«yml:lcase(@name)», None);
vb@2910
   240
                            return PEP_STATUS_OK;
vb@2910
   241
                        }
vb@2910
   242
                        return PEP_ILLEGAL_VALUE;
vb@2910
   243
vb@2909
   244
                    `` apply "fsm", mode=reset_state_machine;
vb@2909
   245
                    default:
vb@2909
   246
                        return PEP_ILLEGAL_VALUE;
vb@2909
   247
                }
vb@2909
   248
vb@2870
   249
                int next_state = None;
vb@2870
   250
                do {
vb@2870
   251
                    switch (fsm) {
vb@2870
   252
                        `` apply "fsm", 3, mode=driver               
vb@2870
   253
                        default:
vb@2870
   254
                            return PEP_ILLEGAL_VALUE;
vb@2870
   255
                    }
vb@2870
   256
                }  while (next_state);
vb@2829
   257
vb@2870
   258
                return PEP_STATUS_OK;
vb@2829
   259
            }
vb@2829
   260
vb@2880
   261
            PEP_STATUS signal_«@name»_event(
vb@2870
   262
                    PEP_SESSION session, 
vb@2870
   263
                    «@name»_PR fsm,
vb@2870
   264
                    int event
vb@2870
   265
                )
vb@2870
   266
            {
vb@2870
   267
                «@name»_t *msg = NULL;
vb@2870
   268
                «@name»_event_t *ev = NULL;
vb@2870
   269
vb@2870
   270
                assert(session && fsm > 0 && event > None);
vb@2870
   271
                if (!(session && fsm > 0 && event > None))
vb@2870
   272
                    return PEP_ILLEGAL_VALUE;
vb@2870
   273
vb@2870
   274
                PEP_STATUS status = PEP_STATUS_OK;
vb@2870
   275
vb@2888
   276
                if (!session->inject_«yml:lcase(@name)»_event)
vb@2888
   277
                   return PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
vb@2870
   278
vb@2870
   279
                if (event < Extra) {
vb@2870
   280
                    msg = new_«@name»_message(fsm, event);
vb@2870
   281
                    if (!msg) {
vb@2870
   282
                        status = PEP_OUT_OF_MEMORY;
vb@2888
   283
                        goto the_end;
vb@2870
   284
                    }
vb@2870
   285
vb@2870
   286
                    status = update_«@name»_message(session, msg);
vb@2870
   287
                    if (status)
vb@2888
   288
                        goto the_end;
vb@2870
   289
                }
vb@2870
   290
vb@2888
   291
                ev = new_«@name»_event(fsm, event, msg);
vb@2870
   292
                if (!ev) {
vb@2870
   293
                    status = PEP_OUT_OF_MEMORY;
vb@2888
   294
                    goto the_end;
vb@2870
   295
                }
vb@2870
   296
vb@2870
   297
                int result = session->inject_«yml:lcase(@name)»_event(ev,
vb@2870
   298
                        session->«yml:lcase(@name)»_management);
vb@2870
   299
                if (result) {
vb@2870
   300
                    status = PEP_STATEMACHINE_ERROR;
vb@2888
   301
                    goto the_end;
vb@2870
   302
                }
vb@2888
   303
                return PEP_STATUS_OK;
vb@2870
   304
vb@2870
   305
            the_end:
krista@3331
   306
                free_«@name»_event(ev); // msg gets freed here
vb@2870
   307
                return status;
vb@2870
   308
            }
vb@2870
   309
vb@2888
   310
            PEP_STATUS signal_«@name»_message(
vb@2870
   311
                    PEP_SESSION session, 
vb@2888
   312
                    PEP_rating rating,
vb@2888
   313
                    const char *data,
vb@3143
   314
                    size_t size,
vb@3363
   315
                    const pEp_identity *from,
vb@3363
   316
                    const char *signature_fpr
vb@2870
   317
                )
vb@2870
   318
            {
vb@2888
   319
                assert(session && data && size);
vb@2888
   320
                if (!(session && data && size))
vb@2870
   321
                    return PEP_ILLEGAL_VALUE;
vb@2870
   322
vb@2888
   323
                if (!session->inject_«yml:lcase(@name)»_event)
vb@2888
   324
                   return PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
vb@2870
   325
vb@3341
   326
                PEP_STATUS status = PEP_STATUS_OK;
vb@2890
   327
                «@name»_event_t *ev = NULL;
vb@2890
   328
vb@3363
   329
                if (from) {
vb@3363
   330
                    free_identity(session->«yml:lcase(@name)»_state.common.from);
vb@3363
   331
                    session->«yml:lcase(@name)»_state.common.from = identity_dup(from);
vb@3363
   332
                    if (!session->«yml:lcase(@name)»_state.common.from) {
vb@3363
   333
                        status = PEP_OUT_OF_MEMORY;
vb@3363
   334
                        goto the_end;
vb@3363
   335
                    }
vb@3363
   336
                }
vb@3363
   337
vb@3363
   338
                if (signature_fpr) {
vb@3363
   339
                    free(session->«yml:lcase(@name)»_state.common.signature_fpr);
vb@3363
   340
                    session->«yml:lcase(@name)»_state.common.signature_fpr = strdup(signature_fpr);
vb@3363
   341
                    if (!session->«yml:lcase(@name)»_state.common.signature_fpr) {
vb@3144
   342
                        status = PEP_OUT_OF_MEMORY;
vb@3144
   343
                        goto the_end;
vb@3144
   344
                    }
vb@3143
   345
                }
vb@3143
   346
vb@3341
   347
                «@name»_t *msg = NULL;
vb@3341
   348
                status = decode_«@name»_message(data, size, &msg);
vb@3341
   349
                if (status)
vb@3341
   350
                    return status;
vb@3341
   351
vb@3341
   352
                «@name»_PR fsm = msg->present;
vb@3341
   353
                int event = 0;
vb@3341
   354
vb@3341
   355
                switch (fsm) {
vb@3341
   356
                    `` apply "fsm", 2, mode=signal_message
vb@3341
   357
                    default:
vb@3341
   358
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@3341
   359
                        goto the_end;
vb@3341
   360
                }
vb@3341
   361
vb@3117
   362
                ev = new_«@name»_event(fsm, event, msg);
vb@2888
   363
                if (!ev) {
vb@2829
   364
                    status = PEP_OUT_OF_MEMORY;
vb@2888
   365
                    goto the_end;
vb@2829
   366
                }
vb@2829
   367
vb@2888
   368
                int result = session->inject_«yml:lcase(@name)»_event(ev,
vb@2888
   369
                        session->«yml:lcase(@name)»_management);
vb@2888
   370
                if (result) {
vb@2888
   371
                    status = PEP_STATEMACHINE_ERROR;
vb@2888
   372
                    goto the_end;
vb@2888
   373
                }
vb@3143
   374
vb@2888
   375
                return PEP_STATUS_OK;
vb@2870
   376
vb@2870
   377
            the_end:
krista@3331
   378
                free_«@name»_event(ev); // msg gets freed here
vb@2870
   379
                return status;
vb@2829
   380
            }
vb@2829
   381
vb@2870
   382
            PEP_STATUS send_«@name»_message(
vb@2870
   383
                    PEP_SESSION session, 
vb@2870
   384
                    «@name»_PR fsm,
vb@2870
   385
                    int message_type
vb@2870
   386
                )
vb@2870
   387
            {
vb@2870
   388
                PEP_STATUS status = PEP_STATUS_OK;
vb@2829
   389
vb@2870
   390
                assert(session && fsm > None && message_type > None);
vb@2870
   391
                if (!(session && fsm > None && message_type > None))
vb@2870
   392
                    return PEP_ILLEGAL_VALUE;
vb@2870
   393
                
vb@2870
   394
                «@name»_t *msg = new_«@name»_message(fsm, message_type);
vb@2870
   395
                if (!msg)
vb@2870
   396
                    return PEP_OUT_OF_MEMORY;
vb@2870
   397
vb@2870
   398
                char *data = NULL;
vb@2870
   399
                message *m = NULL;
vb@2870
   400
                identity_list *channels = NULL;
vb@2870
   401
vb@2870
   402
                status = update_«@name»_message(session, msg);
vb@2870
   403
                if (status)
vb@2870
   404
                    goto the_end;
vb@2870
   405
vb@2870
   406
                size_t size = 0;
vb@2870
   407
                status = encode_«@name»_message(msg, &data, &size);
vb@2870
   408
                if (status)
vb@2870
   409
                    goto the_end;
vb@2870
   410
vb@2870
   411
                switch (message_type) {
vb@2871
   412
                    // these messages are being broadcasted
vb@2877
   413
                    `` for "fsm/message[@type='broadcast']" |>> case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2871
   414
                        status = _own_identities_retrieve(session, &channels, PEP_idf_not_for_«yml:lcase(@name)»);
vb@2875
   415
                        if (status)
vb@2870
   416
                            goto the_end;
vb@2871
   417
vb@2871
   418
                        if (!(channels && channels->ident)) {
vb@3240
   419
                            // status = PEP_«yml:ucase(@name)»_NO_CHANNEL;
vb@3240
   420
                            // we don't check for having a channel, because if
vb@3240
   421
                            // this is initial setup before having an own
vb@3240
   422
                            // identity we're fine
vb@2871
   423
                            goto the_end;
vb@2871
   424
                        }
vb@2870
   425
                        break;
vb@2870
   426
vb@2877
   427
                    // these go anycast; previously used address is sticky (unicast)
vb@2877
   428
                    `` for "fsm/message[@type='anycast']" |>> case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2870
   429
                        if (!session->«yml:lcase(@name)»_state.common.from `> |`|
vb@2870
   430
                            (session->«yml:lcase(@name)»_state.common.from->flags &
vb@2871
   431
                            PEP_idf_not_for_«yml:lcase(@name)»)) {
vb@2870
   432
vb@2870
   433
                            // no address available yet, try to find one
vb@2871
   434
                            status = _own_identities_retrieve(session, &channels, PEP_idf_not_for_«yml:lcase(@name)»);
vb@2870
   435
                            if (!status)
vb@2870
   436
                                goto the_end;
vb@2870
   437
                            break;
vb@2870
   438
vb@2870
   439
                            if (channels && channels->ident) {
vb@2870
   440
                                // only need the first one
vb@2870
   441
                                free_identity_list(channels->next);
vb@2870
   442
                                channels->next = NULL;
vb@2870
   443
                            }
vb@2870
   444
                            else {
vb@2871
   445
                                status = PEP_«yml:ucase(@name)»_NO_CHANNEL;
vb@2870
   446
                                goto the_end;
vb@2870
   447
                            }
vb@2870
   448
                        }
vb@2870
   449
                        else {
vb@2870
   450
                            pEp_identity *channel = identity_dup(session->«yml:lcase(@name)»_state.common.from);
vb@2870
   451
                            if (!channel) {
vb@2870
   452
                                status = PEP_OUT_OF_MEMORY;
vb@2870
   453
                                goto the_end;
vb@2870
   454
                            }
vb@2870
   455
vb@2870
   456
                            channels = new_identity_list(channel);
vb@2870
   457
                            if (!channels) {
vb@2870
   458
                                status = PEP_OUT_OF_MEMORY;
vb@2870
   459
                                goto the_end;
vb@2870
   460
                            }
vb@2870
   461
                        }
vb@3340
   462
                        break;
vb@2877
   463
vb@2877
   464
                    default:
vb@2877
   465
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2877
   466
                        goto the_end;
vb@2870
   467
                }
vb@2870
   468
vb@2870
   469
                for (identity_list *li = channels; li && li->ident ; li = li->next) {
vb@2878
   470
                    message *_m = NULL;
vb@2944
   471
                    char *_data = NULL;
vb@2944
   472
                    
vb@2944
   473
                    _data = malloc(size);
vb@2944
   474
                    assert(_data);
vb@2944
   475
                    if (!_data) {
vb@2944
   476
                        status = PEP_OUT_OF_MEMORY;
vb@2944
   477
                        goto the_end;
vb@2944
   478
                    }
vb@2944
   479
                    memcpy(_data, data, size);
vb@2875
   480
vb@2878
   481
                    switch (message_type) {
vb@2878
   482
                    `` for "fsm/message[@security='unencrypted']" |>>> case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@3129
   483
                            status = base_prepare_message(
vb@3143
   484
                                    session,
vb@3129
   485
                                    li->ident,
vb@3129
   486
                                    li->ident,
vb@3129
   487
                                    _data,
vb@3129
   488
                                    size,
vb@3129
   489
                                    li->ident->fpr,
vb@3129
   490
                                    &_m
vb@3129
   491
                                );
vb@3129
   492
                            if (status) {
vb@3129
   493
                                free(_data);
vb@3129
   494
                                goto the_end;
vb@3129
   495
                            }
Thomas@3127
   496
                            attach_own_key(session, _m);
vb@2878
   497
                            m = _m;
vb@2878
   498
                            break;
vb@2878
   499
vb@2878
   500
                        default:
vb@3129
   501
                            status = base_prepare_message(
vb@3143
   502
                                    session,
vb@3129
   503
                                    li->ident,
vb@3129
   504
                                    li->ident,
vb@3129
   505
                                    _data,
vb@3129
   506
                                    size,
vb@3129
   507
                                    NULL,
vb@3129
   508
                                    &_m
vb@3129
   509
                                );
vb@3129
   510
                            if (status) {
vb@3129
   511
                                free(_data);
vb@3129
   512
                                goto the_end;
vb@3129
   513
                            }
vb@2878
   514
                            status = encrypt_message(session, _m, NULL, &m, PEP_enc_PEP, 0);
vb@2879
   515
                            if (status) {
vb@2899
   516
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@2878
   517
                                goto the_end;
vb@2879
   518
                            }
vb@2878
   519
                            free_message(_m);
vb@2878
   520
                    }
vb@2878
   521
vb@2899
   522
                    status = session->messageToSend(m);
vb@2870
   523
                    m = NULL;
vb@2870
   524
                }
vb@2870
   525
vb@2870
   526
            the_end:
vb@2870
   527
                free_identity_list(channels);
vb@2870
   528
                free_message(m);
vb@2870
   529
                free(data);
vb@2870
   530
                free_«@name»_message(msg);
vb@2870
   531
                return status;
vb@2829
   532
            }
vb@2829
   533
vb@2870
   534
            PEP_STATUS recv_«@name»_event(
vb@2870
   535
                    PEP_SESSION session,
vb@2870
   536
                    «@name»_event_t *ev
vb@2870
   537
                )
vb@2870
   538
            {
vb@2870
   539
                assert(session && ev);
vb@2870
   540
                if (!(session && ev))
vb@2870
   541
                    return PEP_ILLEGAL_VALUE;
vb@2829
   542
vb@2870
   543
                PEP_STATUS status = PEP_STATUS_OK;
vb@2888
   544
                «@name»_PR fsm = (int) None;
vb@2888
   545
                int event = None;
vb@2829
   546
vb@2910
   547
                if (ev->event > None && ev->event < Extra) {
vb@2870
   548
                    status = update_«@name»_state(session, ev->msg, &fsm, &event);
vb@2870
   549
                    if (status)
vb@2888
   550
                        goto the_end;
vb@2829
   551
vb@2870
   552
                    if (ev->fsm) {
vb@2870
   553
                        if (ev->fsm != fsm |`> |` ev->event != event) {
vb@2870
   554
                            status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2888
   555
                            goto the_end;
vb@2870
   556
                        }
vb@2870
   557
                    }
vb@2888
   558
                    else if (ev->event) {
vb@2888
   559
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2888
   560
                        goto the_end;
vb@2870
   561
                    }
vb@2870
   562
                }
vb@2888
   563
                else {
vb@2888
   564
                    fsm = ev->fsm;
vb@2888
   565
                    event = ev->event;
vb@2888
   566
                }
vb@2829
   567
vb@2888
   568
                status = «@name»_driver(session, fsm, event);
vb@2870
   569
vb@2888
   570
            the_end:
krista@3331
   571
                //free_«@name»_event(ev); // FIXME: We don't own this pointer. Are we sure it gets freed externally?
vb@2870
   572
                return status;
vb@2829
   573
            }
vb@2829
   574
vb@2870
   575
            ||
vb@2829
   576
        }
vb@2829
   577
vb@2829
   578
        apply "fsm", 0, mode=gen;
vb@2829
   579
    }
vb@2829
   580
vb@2909
   581
    template "fsm", mode=timeout
vb@2909
   582
    ||
vb@2909
   583
    static bool _«@name»_timeout(int state)
vb@2909
   584
    {
vb@2909
   585
        static int last_state = None;
vb@2909
   586
        static time_t switch_time = 0;
vb@2909
   587
vb@2909
   588
        if (state > Init) {
vb@2909
   589
            if (state == last_state) {
vb@2909
   590
                if (time(NULL) - switch_time > «yml:ucase(@name)»_THRESHOLD) {
vb@2909
   591
                    last_state = None;
vb@2909
   592
                    switch_time = 0;
vb@2909
   593
                    return true;
vb@2909
   594
                }
vb@2909
   595
            }
vb@2909
   596
            else {
vb@2909
   597
                last_state = state;
vb@2909
   598
                switch_time = time(NULL);
vb@2909
   599
            }
vb@2909
   600
        }
vb@2909
   601
        else {
vb@2909
   602
            last_state = None;
vb@2909
   603
            switch_time = 0;
vb@2909
   604
        }
vb@2909
   605
vb@2909
   606
        return false;
vb@2909
   607
    }
vb@2909
   608
vb@2909
   609
    ||
vb@2909
   610
vb@2909
   611
    template "fsm", mode=reset_state_machine
vb@2909
   612
    ||
vb@2909
   613
        case «../@name»_PR_«yml:lcase(@name)»: {
vb@2909
   614
            int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
vb@2909
   615
            switch (state) {
vb@2909
   616
                `` for "state[@name!='InitState' and @timeout != 'off']" |>>> case «@name»:
vb@2909
   617
                    if (_«@name»_timeout(state)) {
vb@2909
   618
                        session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = Init;
vb@2909
   619
                        event = Init;
vb@2914
   620
                        `` if "@threshold > 0" |>>>>> «@name»TimeoutHandler(session);
vb@2909
   621
                    }
vb@2909
   622
                    break;
vb@2909
   623
                
vb@2909
   624
                default:
vb@2909
   625
                    _«@name»_timeout(None);
vb@2909
   626
            }
vb@2909
   627
            break;
vb@2909
   628
        }
vb@2909
   629
vb@2909
   630
    ||
vb@2909
   631
vb@2890
   632
    template "fsm", mode=signal_message
vb@2890
   633
    {
vb@2890
   634
        ||
vb@2890
   635
        case «../@name»_PR_«yml:lcase(@name)»:
vb@3338
   636
            switch (msg->choice.«yml:lcase(@name)».payload.present) {
vb@2891
   637
        ||
vb@2891
   638
        if "message[@security='unencrypted']" {
vb@3338
   639
            |>> // these messages require a detached signature
vb@3338
   640
            for "message[@security='unencrypted']" {
vb@2891
   641
            ||
vb@3338
   642
                    case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@3363
   643
                        if (!signature_fpr) {
vb@3146
   644
                            status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3146
   645
                            goto the_end;
vb@3146
   646
                        }
vb@3338
   647
                        event = «@name»;
vb@3117
   648
                        break;
vb@2890
   649
vb@2891
   650
            ||
vb@3338
   651
            }
vb@2891
   652
        }
vb@3338
   653
        if "message[@security='untrusted']" {
vb@3338
   654
            |>> // these messages must arrive encrypted
vb@3338
   655
            for "message[@security='untrusted']" {
vb@3338
   656
            ||
vb@3338
   657
                    case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@3339
   658
                        if (rating < PEP_rating_reliable) {
vb@3338
   659
                            status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3338
   660
                            goto the_end;
vb@3338
   661
                        }
vb@3338
   662
                        event = «@name»;
vb@3338
   663
                        break;
vb@3338
   664
vb@3338
   665
            ||
vb@3338
   666
            }
vb@3338
   667
        }
vb@3338
   668
        if "message[@security='trusted']" {
vb@3338
   669
            |>> // these messages must come through a trusted channel
vb@3338
   670
            for "message[@security='trusted']" {
vb@3338
   671
            ||
vb@3338
   672
                    case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@3339
   673
                        if (rating < PEP_rating_trusted) {
vb@3338
   674
                            status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3338
   675
                            goto the_end;
vb@3338
   676
                        }
vb@3338
   677
                        event = «@name»;
vb@3338
   678
                        break;
vb@3338
   679
vb@3338
   680
            ||
vb@3338
   681
            }
vb@3338
   682
        }
vb@2891
   683
        ||
vb@2890
   684
                default:
vb@2890
   685
                    status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@2890
   686
                    goto the_end;
vb@2890
   687
            }
vb@2890
   688
            break;
vb@2890
   689
vb@2890
   690
        ||
vb@2890
   691
    }
vb@2890
   692
vb@2838
   693
    template "fsm", mode=event
vb@2838
   694
    {
vb@2838
   695
    ||
vb@2843
   696
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@2839
   697
        switch (msg->choice.«yml:lcase(@name)».payload.present) {
vb@2838
   698
    ||
vb@2838
   699
    for "message"
vb@2838
   700
    ||
vb@2838
   701
            case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2838
   702
                ev->event = «@name»;
vb@2838
   703
                break;
vb@2838
   704
    ||
vb@2838
   705
    ||
vb@2838
   706
            default:
vb@2838
   707
                // unknown message type
vb@2838
   708
                free(ev);
vb@2838
   709
                return NULL;
vb@2838
   710
        }
vb@2838
   711
        break;
vb@2838
   712
    }
vb@2838
   713
vb@2838
   714
    ||
vb@2838
   715
    }
vb@2838
   716
vb@2829
   717
    template "fsm", mode=driver
vb@2829
   718
    ||
vb@2843
   719
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@2839
   720
        int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
vb@2839
   721
        next_state = fsm_«@name»(session, state, event);
vb@2838
   722
        if (next_state > None) {
vb@2838
   723
            session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = next_state;
vb@2838
   724
            event = Init;
vb@2838
   725
        }
vb@2838
   726
        else if (next_state < None) {
Hussein@3167
   727
            return PEP_STATEMACHINE_ERROR - next_state;
vb@2838
   728
        }
vb@2829
   729
        break;
vb@2829
   730
    }
vb@2829
   731
vb@2829
   732
    ||
vb@2829
   733
vb@2829
   734
    template "fsm", mode=gen {
vb@2829
   735
        document "generated/{@name}_fsm.h", "text" {
vb@2829
   736
        ||
vb@2829
   737
        // This file is under GNU General Public License 3.0
vb@2829
   738
        // see LICENSE.txt
vb@2829
   739
vb@2829
   740
        #pragma once
vb@2829
   741
vb@2829
   742
        #include "«../@name»_impl.h"
vb@2829
   743
vb@2829
   744
        #ifdef __cplusplus
vb@2829
   745
        extern "C" {
vb@2829
   746
        #endif
vb@2829
   747
vb@2829
   748
        // state machine for «@name»
krista@2271
   749
krista@2271
   750
        // states
krista@2271
   751
krista@2271
   752
        typedef enum _«@name»_state {
vb@2829
   753
            «@name»_state_None = None,
vb@2829
   754
            «@name»_state_Init = Init,
vb@2829
   755
        ||
vb@2829
   756
        for "func:distinctName(state[not(@name='InitState')])"
vb@2829
   757
            |> «@name»`if "position()!=last()" > , `
vb@2829
   758
        ||
krista@2271
   759
        } «@name»_state;
krista@2271
   760
krista@2271
   761
        // events
krista@2271
   762
krista@2271
   763
        typedef enum _«@name»_event {
vb@2829
   764
            «@name»_event_None = None,
vb@2829
   765
            «@name»_event_Init = Init,
krista@2271
   766
        ||
vb@2829
   767
        for "func:distinctName(state/event[not(not(../../message/@name=@name))])" {
krista@2271
   768
            const "name", "@name";
vb@2829
   769
            |> «$name» = «/protocol/fsm/message[@name=$name]/@id»,
krista@2271
   770
        }
vb@2830
   771
        for "func:distinctName(state/event[not(not(../../external/@name=@name))])" {
vb@2830
   772
            const "name", "@name";
vb@2830
   773
            |> «$name» = «/protocol/fsm/external[@name=$name]/@id»,
vb@2830
   774
        }
vb@2829
   775
        |> «@name»_event_Extra = Extra,
vb@2830
   776
        for "func:distinctName(state/event[not(../../message/@name=@name or ../../external/@name=@name)])" {
vb@2829
   777
            if "@name!='Init'"
vb@2829
   778
                |> «@name»`if "position()!=last()" > , `
vb@2829
   779
        }
krista@2271
   780
        ||
krista@2271
   781
        } «@name»_event;
krista@2271
   782
vb@2829
   783
        // state machine
krista@2271
   784
vb@2829
   785
        const char *«@name»_state_name(int state);
vb@2881
   786
        const char *«@name»_event_name(int event);
krista@2271
   787
vb@2838
   788
        // the state machine function is returning the next state in case of a
vb@2838
   789
        // transition or None for staying
vb@2838
   790
krista@2271
   791
        «@name»_state fsm_«@name»(
krista@2271
   792
                PEP_SESSION session,
krista@2271
   793
                «@name»_state state,
vb@2829
   794
                «@name»_event event
krista@2271
   795
            );
krista@2271
   796
krista@2271
   797
        #ifdef __cplusplus
krista@2271
   798
        }
krista@2271
   799
        #endif
krista@2271
   800
krista@2271
   801
        ||
krista@2271
   802
        }
vb@2829
   803
vb@2829
   804
        document "generated/{@name}_fsm.c", "text" {
krista@2271
   805
        ||
vb@2829
   806
        // This file is under GNU General Public License 3.0
vb@2829
   807
        // see LICENSE.txt
krista@2271
   808
vb@2829
   809
        #include "«@name»_fsm.h"
vb@2847
   810
        #include <stdlib.h>
krista@2271
   811
vb@2829
   812
        const char *«@name»_state_name(int state)
vb@2829
   813
        {
vb@2829
   814
            switch (state) {
vb@2829
   815
                case End:
vb@2829
   816
                    return "End";
vb@2829
   817
                case None:
vb@2829
   818
                    return "None";
vb@2829
   819
                case Init:
vb@2829
   820
                    return "InitState";
vb@2829
   821
        ||
vb@2829
   822
        for "func:distinctName(state[not(@name='InitState')])" {
vb@2829
   823
            |>> case «@name»:
vb@2829
   824
            |>>> return "«@name»";
vb@2829
   825
        }
vb@2829
   826
        ||
vb@2829
   827
                default:
vb@2829
   828
                    return "unknown state";
vb@2829
   829
            }
vb@2829
   830
        }
krista@2271
   831
vb@2881
   832
        const char *«@name»_event_name(int event)
vb@2881
   833
        {
vb@2881
   834
            switch (event) {
vb@2881
   835
                case None:
vb@2881
   836
                    return "None";
vb@2881
   837
                case Init:
vb@2881
   838
                    return "Init";
vb@2881
   839
        ||
vb@2881
   840
        for "func:distinctName(state/event[not(@name='Init')])" {
vb@2881
   841
            |>> case «@name»:
vb@2881
   842
            |>>> return "«@name»";
vb@2881
   843
        }
vb@2881
   844
        ||
vb@2881
   845
                default:
vb@2881
   846
                    return "unknown event";
vb@2881
   847
            }
vb@2881
   848
        }
vb@2881
   849
vb@2881
   850
vb@2829
   851
        static char *_str(int n, bool hex)
vb@2829
   852
        {
vb@2829
   853
            char *buf = calloc(1, 24);
vb@2829
   854
            assert(buf);
vb@2829
   855
            if (!buf)
vb@2829
   856
                return NULL;
vb@2829
   857
vb@2829
   858
            if (hex)
vb@2829
   859
                snprintf(buf, 24, "%.4x", n);
vb@2829
   860
            else
vb@2829
   861
                snprintf(buf, 24, "%d", n);
vb@2829
   862
            return buf;
vb@2829
   863
        }
vb@2829
   864
vb@2829
   865
        #define «@name»_ERR_LOG(t, d) log_event(session, (t), "«@name»", (d), "error")
vb@2829
   866
vb@2829
   867
        static PEP_STATUS _«@name»_ERR_LOG_int(PEP_SESSION session, char *t, int n, bool hex)
vb@2829
   868
        {
vb@2829
   869
            char *_buf = _str(n, hex);
vb@2829
   870
            if (!_buf)
vb@2829
   871
                return PEP_OUT_OF_MEMORY;
vb@2829
   872
            PEP_STATUS status = «@name»_ERR_LOG(t, _buf);
vb@2829
   873
            free(_buf);
vb@2829
   874
            return status;
vb@2829
   875
        }
vb@2829
   876
vb@2829
   877
        #define «@name»_ERR_LOG_INT(t, n) _«@name»_ERR_LOG_int(session, (t), (n), false)
vb@2829
   878
        #define «@name»_ERR_LOG_HEX(t, n) _«@name»_ERR_LOG_int(session, (t), (n), true)
vb@2829
   879
vb@2829
   880
        #ifndef SERVICE_LOG
vb@2829
   881
        // SERVICE LOG is meant to check session->service_log in runtime config;
vb@2829
   882
        // for older engines log more than needed
vb@2829
   883
        #define SERVICE_LOG(session, t, n, d) log_event((session), (t), (n), (d), "service")
vb@2829
   884
        #endif 
vb@2829
   885
vb@2829
   886
        #define «@name»_SERVICE_LOG(t, d) SERVICE_LOG(session, (t), "«@name»", (d))
vb@2829
   887
vb@2829
   888
        «@name»_state fsm_«@name»(
krista@2271
   889
                PEP_SESSION session,
vb@2829
   890
                «@name»_state state,
vb@2829
   891
                «@name»_event event
krista@2271
   892
            )
krista@2271
   893
        {
krista@2271
   894
            assert(session);
krista@2271
   895
            if (!session)
vb@2829
   896
                return invalid_state;
krista@2271
   897
krista@2271
   898
            switch (state) {
vb@2829
   899
                case None:
vb@2838
   900
                    return «@name»_state_Init;
vb@2829
   901
                
vb@2829
   902
                `` apply "state", 2, mode=fsm
krista@2271
   903
                default:
vb@2838
   904
                    «@name»_ERR_LOG_INT("invalid state", state);
vb@2829
   905
                    return invalid_state;
krista@2271
   906
            }
vb@2829
   907
            
vb@2838
   908
            return None;
krista@2271
   909
        }
krista@2271
   910
krista@2271
   911
        ||
vb@2829
   912
        }
vb@2829
   913
    }
vb@2829
   914
    
vb@2829
   915
    template "state", mode=fsm {
vb@2829
   916
        choose {
vb@2829
   917
            when "@name='InitState'" | case «../@name»_state_Init:
vb@2829
   918
            otherwise | case «@name»:
vb@2829
   919
        }
vb@2829
   920
        ||
vb@2829
   921
            «../@name»_SERVICE_LOG("in state", "«@name»");
vb@2829
   922
vb@2829
   923
            switch (event) {
vb@2829
   924
                case None:
vb@2829
   925
                    «../@name»_SERVICE_LOG("received None event", "ignoring");
vb@2838
   926
                    break;
vb@2829
   927
     
vb@2838
   928
        ||
vb@2838
   929
        if "not(event[@name='Init'])"
vb@2838
   930
        ||
vb@2838
   931
                case Init:
vb@2838
   932
                    // nothing to do
vb@2838
   933
                    break;
vb@2838
   934
vb@2838
   935
        ||
vb@2838
   936
        ||
vb@2829
   937
                `` apply "event", 2, mode=fsm
vb@2829
   938
                default:
vb@2885
   939
                    // ignore events not handled here
vb@2885
   940
                    «../@name»_SERVICE_LOG("ignoring event", KeySync_event_name(event));
vb@3362
   941
                    return invalid_event;
vb@2829
   942
            }
vb@2829
   943
            break;
vb@2829
   944
vb@2829
   945
        ||
krista@2271
   946
    }
krista@2271
   947
vb@2829
   948
    template "event", mode=fsm {
vb@2829
   949
        | case «@name»: {
vb@2865
   950
        if "condition|action|send" |> PEP_STATUS status;
vb@2829
   951
        if "condition" |> bool result = false;
vb@2865
   952
        if "condition|action|send" |
krista@2271
   953
        ||
vb@2829
   954
            «../../@name»_SERVICE_LOG("received event", "«@name»");
vb@2907
   955
            `` apply "transition|action|condition|else|send";
vb@2829
   956
        ||
vb@2829
   957
        if "name(*[last()])!='transition'" {
vb@2829
   958
            |
vb@2843
   959
            |> «../../@name»_SERVICE_LOG("remaining in state", "«../@name»");
vb@2829
   960
            |> break;
vb@2829
   961
        }
vb@2829
   962
        ||
vb@2829
   963
        }
vb@2829
   964
        
vb@2829
   965
        ||
vb@2829
   966
    }
vb@2829
   967
vb@2829
   968
    template "transition" {
vb@2865
   969
        const "fsm", "ancestor::fsm";
krista@2271
   970
        ||
krista@2271
   971
vb@2829
   972
        «$fsm/@name»_SERVICE_LOG("transition to state", "«@target»");
vb@2838
   973
        return «@target»;
krista@2271
   974
        ||
vb@2829
   975
    }
krista@2271
   976
vb@2865
   977
    template "send" {
vb@2865
   978
        const "fsm", "ancestor::fsm";
vb@2865
   979
        const "protocol", "ancestor::protocol";
vb@2865
   980
        ||
vb@2865
   981
vb@2865
   982
        «$fsm/@name»_SERVICE_LOG("send message", "«@name»");
vb@2865
   983
        status = send_«$protocol/@name»_message(session, «$fsm/@id», «$fsm/@name»__payload_PR_«yml:mixedCase(@name)»);
vb@3089
   984
        if (status == PEP_OUT_OF_MEMORY)
vb@3089
   985
            return out_of_memory;
vb@3089
   986
        if (status) {
vb@3089
   987
            «$fsm/@name»_ERR_LOG_HEX("sending «@name» failed", status);
vb@3089
   988
            return cannot_send;
vb@3089
   989
        }
vb@2865
   990
        ||
vb@2865
   991
    }
vb@2865
   992
vb@2829
   993
    template "action" {
vb@2865
   994
        const "fsm", "ancestor::fsm";
vb@2865
   995
        ||
vb@2829
   996
vb@2865
   997
        «$fsm/@name»_SERVICE_LOG("do action", "«@name»");
vb@2865
   998
        status = «@name»(session);
vb@3089
   999
        if (status == PEP_OUT_OF_MEMORY)
vb@3089
  1000
            return out_of_memory;
vb@2829
  1001
        if (status) {
vb@2829
  1002
            «$fsm/@name»_ERR_LOG_HEX("executing action «@name»() failed", status);
vb@2829
  1003
            return invalid_action;
krista@2271
  1004
        }
krista@2271
  1005
        ||
krista@2271
  1006
    }
krista@2271
  1007
vb@2829
  1008
    template "condition" {
vb@2865
  1009
        const "fsm", "ancestor::fsm";
krista@2271
  1010
        ||
krista@2271
  1011
vb@2829
  1012
        status = «@name»(session, &result);
vb@3089
  1013
        if (status == PEP_OUT_OF_MEMORY)
vb@3089
  1014
            return out_of_memory;
vb@2829
  1015
        if (status) {
vb@2829
  1016
            «$fsm/@name»_ERR_LOG_HEX("computing condition «@name» failed", status);
vb@2829
  1017
            return invalid_condition;
krista@2271
  1018
        }
vb@2829
  1019
        if (result) {
vb@2843
  1020
            «$fsm/@name»_SERVICE_LOG("condition applies", "«@name»");
krista@2271
  1021
        ||
vb@2907
  1022
        apply "transition|action|condition|else|send";
vb@2907
  1023
        | }
vb@2907
  1024
    }
vb@2907
  1025
vb@2907
  1026
    template "else" {
vb@2907
  1027
        if "not(name(preceding-sibling::*[last()]) = 'condition')"
vb@2907
  1028
            error "else without if";
vb@2907
  1029
vb@2907
  1030
        | else {
vb@2907
  1031
        |> «ancestor::fsm/@name»_SERVICE_LOG("condition does not apply", "«preceding-sibling::*[last()]/@name»");
vb@2907
  1032
        apply "transition|action|condition|else|send";
krista@2271
  1033
        | }
krista@2271
  1034
    }
krista@2271
  1035
}