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