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