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