sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Tue, 28 May 2019 20:26:45 +0200
branchsync
changeset 3758 3c8a25f6f2c2
parent 3747 82fe58af7b53
child 3759 3a757e143664
permissions -rw-r--r--
FIX of pgp_sequoia: size off by one
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@3758
   338
                if (from && from->fpr && signature_fpr) {
vb@3758
   339
                    // ignore own messages
vb@3758
   340
                    if (strcmp(from->fpr, signature_fpr) == 0)
vb@3758
   341
                        return PEP_STATUS_OK;
vb@3758
   342
                }
vb@3758
   343
vb@2888
   344
                if (!session->inject_«yml:lcase(@name)»_event)
vb@2888
   345
                   return PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
vb@2870
   346
vb@3341
   347
                PEP_STATUS status = PEP_STATUS_OK;
vb@2890
   348
                «@name»_event_t *ev = NULL;
vb@2890
   349
vb@3341
   350
                «@name»_t *msg = NULL;
vb@3341
   351
                status = decode_«@name»_message(data, size, &msg);
vb@3341
   352
                if (status)
vb@3341
   353
                    return status;
vb@3341
   354
vb@3341
   355
                «@name»_PR fsm = msg->present;
vb@3341
   356
                int event = 0;
vb@3593
   357
                bool is_own_key = false;
vb@3341
   358
vb@3341
   359
                switch (fsm) {
vb@3341
   360
                    `` apply "fsm", 2, mode=signal_message
vb@3341
   361
                    default:
vb@3341
   362
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@3341
   363
                        goto the_end;
vb@3341
   364
                }
vb@3341
   365
vb@3117
   366
                ev = new_«@name»_event(fsm, event, msg);
vb@2888
   367
                if (!ev) {
vb@2829
   368
                    status = PEP_OUT_OF_MEMORY;
vb@2888
   369
                    goto the_end;
vb@2829
   370
                }
vb@2829
   371
vb@3534
   372
                // add transport data
vb@3534
   373
vb@3534
   374
                if (from) {
vb@3534
   375
                    ev->from = identity_dup(from);
vb@3534
   376
                    if (!ev->from) {
vb@3534
   377
                        status = PEP_OUT_OF_MEMORY;
vb@3534
   378
                        goto the_end;
vb@3534
   379
                    }
vb@3534
   380
                }
vb@3534
   381
vb@3534
   382
                if (signature_fpr) {
vb@3534
   383
                    ev->signature_fpr = strdup(signature_fpr);
vb@3534
   384
                    assert(ev->signature_fpr);
vb@3534
   385
                    if (!ev->signature_fpr) {
vb@3534
   386
                        status = PEP_OUT_OF_MEMORY;
vb@3534
   387
                        goto the_end;
vb@3534
   388
                    }
vb@3534
   389
                }
vb@3534
   390
vb@2888
   391
                int result = session->inject_«yml:lcase(@name)»_event(ev,
vb@2888
   392
                        session->«yml:lcase(@name)»_management);
vb@2888
   393
                if (result) {
vb@2888
   394
                    status = PEP_STATEMACHINE_ERROR;
vb@2888
   395
                    goto the_end;
vb@2888
   396
                }
vb@3143
   397
vb@2888
   398
                return PEP_STATUS_OK;
vb@2870
   399
vb@2870
   400
            the_end:
krista@3331
   401
                free_«@name»_event(ev); // msg gets freed here
vb@2870
   402
                return status;
vb@2829
   403
            }
vb@2829
   404
vb@2870
   405
            PEP_STATUS send_«@name»_message(
vb@2870
   406
                    PEP_SESSION session, 
vb@2870
   407
                    «@name»_PR fsm,
vb@2870
   408
                    int message_type
vb@2870
   409
                )
vb@2870
   410
            {
vb@2870
   411
                PEP_STATUS status = PEP_STATUS_OK;
vb@2829
   412
vb@2870
   413
                assert(session && fsm > None && message_type > None);
vb@2870
   414
                if (!(session && fsm > None && message_type > None))
vb@2870
   415
                    return PEP_ILLEGAL_VALUE;
vb@2870
   416
                
vb@2870
   417
                «@name»_t *msg = new_«@name»_message(fsm, message_type);
vb@2870
   418
                if (!msg)
vb@2870
   419
                    return PEP_OUT_OF_MEMORY;
vb@2870
   420
vb@2870
   421
                char *data = NULL;
vb@2870
   422
                message *m = NULL;
vb@2870
   423
                identity_list *channels = NULL;
vb@3545
   424
                char *key_data = NULL;
vb@3545
   425
                size_t key_data_size = 0;
vb@3594
   426
                stringlist_t *extra = NULL;
vb@3594
   427
                bool transaction;
vb@2870
   428
vb@2870
   429
                status = update_«@name»_message(session, msg);
vb@2870
   430
                if (status)
vb@2870
   431
                    goto the_end;
vb@2870
   432
vb@2870
   433
                size_t size = 0;
vb@2870
   434
                status = encode_«@name»_message(msg, &data, &size);
vb@2870
   435
                if (status)
vb@2870
   436
                    goto the_end;
vb@2870
   437
vb@2870
   438
                switch (message_type) {
vb@2871
   439
                    // these messages are being broadcasted
vb@3509
   440
                    `` for "fsm/message[@type='broadcast']" |>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@2871
   441
                        status = _own_identities_retrieve(session, &channels, PEP_idf_not_for_«yml:lcase(@name)»);
vb@2875
   442
                        if (status)
vb@2870
   443
                            goto the_end;
vb@2871
   444
vb@2871
   445
                        if (!(channels && channels->ident)) {
vb@3240
   446
                            // status = PEP_«yml:ucase(@name)»_NO_CHANNEL;
vb@3240
   447
                            // we don't check for having a channel, because if
vb@3240
   448
                            // this is initial setup before having an own
vb@3240
   449
                            // identity we're fine
vb@2871
   450
                            goto the_end;
vb@2871
   451
                        }
vb@2870
   452
                        break;
vb@2870
   453
vb@2877
   454
                    // these go anycast; previously used address is sticky (unicast)
vb@3509
   455
                    `` for "fsm/message[@type='anycast']" |>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@2870
   456
                        if (!session->«yml:lcase(@name)»_state.common.from `> |`|
vb@2870
   457
                            (session->«yml:lcase(@name)»_state.common.from->flags &
vb@2871
   458
                            PEP_idf_not_for_«yml:lcase(@name)»)) {
vb@2870
   459
vb@2870
   460
                            // no address available yet, try to find one
vb@2871
   461
                            status = _own_identities_retrieve(session, &channels, PEP_idf_not_for_«yml:lcase(@name)»);
krista@3626
   462
                            if (status)
vb@2870
   463
                                goto the_end;
vb@2870
   464
                            break;
vb@2870
   465
vb@2870
   466
                            if (channels && channels->ident) {
vb@2870
   467
                                // only need the first one
vb@2870
   468
                                free_identity_list(channels->next);
vb@2870
   469
                                channels->next = NULL;
vb@2870
   470
                            }
vb@2870
   471
                            else {
vb@2871
   472
                                status = PEP_«yml:ucase(@name)»_NO_CHANNEL;
vb@2870
   473
                                goto the_end;
vb@2870
   474
                            }
vb@2870
   475
                        }
vb@2870
   476
                        else {
vb@2870
   477
                            pEp_identity *channel = identity_dup(session->«yml:lcase(@name)»_state.common.from);
vb@2870
   478
                            if (!channel) {
vb@2870
   479
                                status = PEP_OUT_OF_MEMORY;
vb@2870
   480
                                goto the_end;
vb@2870
   481
                            }
vb@2870
   482
vb@2870
   483
                            channels = new_identity_list(channel);
vb@2870
   484
                            if (!channels) {
vb@2870
   485
                                status = PEP_OUT_OF_MEMORY;
vb@2870
   486
                                goto the_end;
vb@2870
   487
                            }
vb@2870
   488
                        }
vb@3340
   489
                        break;
vb@2877
   490
vb@2877
   491
                    default:
vb@2877
   492
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2877
   493
                        goto the_end;
vb@2870
   494
                }
vb@2870
   495
vb@2870
   496
                for (identity_list *li = channels; li && li->ident ; li = li->next) {
vb@2878
   497
                    message *_m = NULL;
vb@2944
   498
                    char *_data = NULL;
vb@2944
   499
                    
vb@2944
   500
                    _data = malloc(size);
vb@2944
   501
                    assert(_data);
vb@2944
   502
                    if (!_data) {
vb@2944
   503
                        status = PEP_OUT_OF_MEMORY;
vb@2944
   504
                        goto the_end;
vb@2944
   505
                    }
vb@2944
   506
                    memcpy(_data, data, size);
vb@2875
   507
vb@2878
   508
                    switch (message_type) {
vb@3509
   509
                    `` for "fsm/message[@security='unencrypted']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3129
   510
                            status = base_prepare_message(
vb@3143
   511
                                    session,
vb@3129
   512
                                    li->ident,
vb@3129
   513
                                    li->ident,
vb@3129
   514
                                    _data,
vb@3129
   515
                                    size,
vb@3129
   516
                                    li->ident->fpr,
vb@3129
   517
                                    &_m
vb@3129
   518
                                );
vb@3129
   519
                            if (status) {
vb@3129
   520
                                free(_data);
vb@3129
   521
                                goto the_end;
vb@3129
   522
                            }
Thomas@3127
   523
                            attach_own_key(session, _m);
vb@2878
   524
                            m = _m;
vb@2878
   525
                            break;
vb@2878
   526
vb@3594
   527
                    `` for "fsm/message[@security='untrusted']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3594
   528
                            // add fpr of key of comm partner
vb@3594
   529
vb@3594
   530
                            assert(session->«yml:lcase(@name)»_state.common.signature_fpr);
vb@3594
   531
                            if (!session->«yml:lcase(@name)»_state.common.signature_fpr) {
vb@3519
   532
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3519
   533
                                goto the_end;
vb@3519
   534
                            }
vb@3594
   535
vb@3594
   536
                            extra = new_stringlist(session->«yml:lcase(@name)»_state.common.signature_fpr);
vb@3594
   537
                            if (!extra) {
vb@3594
   538
                                status = PEP_OUT_OF_MEMORY;
vb@3594
   539
                                goto the_end;
vb@3594
   540
                            }
vb@3594
   541
vb@3594
   542
                            status = base_prepare_message(
vb@3594
   543
                                    session,
vb@3594
   544
                                    li->ident,
vb@3594
   545
                                    li->ident,
vb@3594
   546
                                    _data,
vb@3594
   547
                                    size,
vb@3594
   548
                                    NULL,
vb@3594
   549
                                    &_m
vb@3594
   550
                                );
vb@3594
   551
                            if (status) {
vb@3594
   552
                                free(_data);
vb@3594
   553
                                goto the_end;
vb@3594
   554
                            }
vb@3594
   555
vb@3594
   556
                            status = encrypt_message(session, _m, extra, &m, PEP_enc_PEP, 0);
vb@3594
   557
                            if (status) {
vb@3594
   558
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3594
   559
                                goto the_end;
vb@3594
   560
                            }
vb@3594
   561
                            free_message(_m);
vb@3594
   562
                            break;
vb@3594
   563
vb@3594
   564
                    `` for "fsm/message[@security='attach_own_keys']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3594
   565
                            // check if this is the key of a former negotiation
vb@3594
   566
vb@3594
   567
                            transaction = false;
vb@3594
   568
                            for (int i=0; i < session->own_«yml:lcase(@name)»_state.negotiation.size; i++) {
vb@3594
   569
                                if (session->own_«yml:lcase(@name)»_state.negotiation.buf[i]) {
vb@3594
   570
                                    transaction = true;
vb@3594
   571
                                    break;
vb@3594
   572
                                }
vb@3594
   573
                            }
vb@3594
   574
    
vb@3594
   575
                            // if it is a former negotiation check if the key
vb@3594
   576
                            // is fully trusted and the sender key of this
vb@3594
   577
                            // transaction; if so add the sender key to extra
vb@3594
   578
                            // keys allowing this new partner to read the
vb@3594
   579
                            // secret keys
vb@3594
   580
vb@3594
   581
                            if (transaction) {
vb@3594
   582
                                assert(session->own_«yml:lcase(@name)»_state.signature_fpr &&
vb@3594
   583
                                    session->«yml:lcase(@name)»_state.common.from &&
vb@3594
   584
                                    session->«yml:lcase(@name)»_state.common.from->user_id);
vb@3594
   585
                                if (!(session->own_«yml:lcase(@name)»_state.signature_fpr &&
vb@3594
   586
                                        session->«yml:lcase(@name)»_state.common.from &&
vb@3594
   587
                                        session->«yml:lcase(@name)»_state.common.from->user_id))
vb@3594
   588
                                {
vb@3594
   589
                                    status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3594
   590
                                    goto the_end;
vb@3594
   591
                                }
vb@3519
   592
                            
vb@3594
   593
                                // test if this is a green channel
vb@3594
   594
vb@3519
   595
                                pEp_identity *ident = new_identity(NULL,
vb@3594
   596
                                        session->own_«yml:lcase(@name)»_state.signature_fpr,
vb@3519
   597
                                        session->«yml:lcase(@name)»_state.common.from->user_id,
vb@3519
   598
                                        NULL
vb@3519
   599
                                    );
vb@3519
   600
                                if (!ident) {
vb@3519
   601
                                    status = PEP_OUT_OF_MEMORY;
vb@3519
   602
                                    goto the_end;
vb@3519
   603
                                }
vb@3519
   604
                                status = get_trust(session, ident);
vb@3519
   605
                                if (status) {
vb@3519
   606
                                    free_identity(ident);
vb@3519
   607
                                    goto the_end;
vb@3519
   608
                                }
vb@3519
   609
                                assert(ident->comm_type == PEP_ct_pEp); // we don't deliver otherwise
vb@3519
   610
                                if (ident->comm_type != PEP_ct_pEp) {
vb@3519
   611
                                    free_identity(ident);
vb@3519
   612
                                    status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3519
   613
                                    goto the_end;
vb@3519
   614
                                }
vb@3519
   615
                                free_identity(ident);
vb@3594
   616
vb@3594
   617
                                // test if we accepted this as own key already
vb@3594
   618
vb@3594
   619
                                bool is_own_key = false;
vb@3594
   620
                                status = own_key_is_listed(session,
vb@3594
   621
                                        session->own_«yml:lcase(@name)»_state.signature_fpr,
vb@3594
   622
                                        &is_own_key);
vb@3594
   623
                                assert(!status);
vb@3594
   624
                                if (status)
vb@3594
   625
                                    goto the_end;
vb@3594
   626
                                assert(is_own_key);
vb@3594
   627
                                if (!is_own_key) {
vb@3594
   628
                                    status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3594
   629
                                    goto the_end;
vb@3594
   630
                                }
vb@3594
   631
vb@3594
   632
                                // if so add key of comm partner to extra keys
vb@3594
   633
vb@3594
   634
                                extra = new_stringlist(session->own_«yml:lcase(@name)»_state.signature_fpr);
vb@3594
   635
                                if (!extra) {
vb@3594
   636
                                    status = PEP_OUT_OF_MEMORY;
vb@3594
   637
                                    goto the_end;
vb@3594
   638
                                }
vb@3519
   639
                            }
vb@3593
   640
                            
vb@3393
   641
                            status = base_prepare_message(
vb@3393
   642
                                    session,
vb@3393
   643
                                    li->ident,
vb@3393
   644
                                    li->ident,
vb@3393
   645
                                    _data,
vb@3393
   646
                                    size,
vb@3393
   647
                                    NULL,
vb@3393
   648
                                    &_m
vb@3393
   649
                                );
vb@3393
   650
                            if (status) {
vb@3393
   651
                                free(_data);
vb@3393
   652
                                goto the_end;
vb@3393
   653
                            }
vb@3509
   654
vb@3545
   655
                            // export secret keys into memory
vb@3545
   656
vb@3545
   657
                            key_data = strdup("");
vb@3545
   658
                            assert(key_data);
vb@3545
   659
                            if (!key_data) {
vb@3545
   660
                                free(_data);
vb@3567
   661
                                free_message(_m);
vb@3545
   662
                                status = PEP_OUT_OF_MEMORY;
vb@3545
   663
                                goto the_end;
vb@3545
   664
                            }
krista@3645
   665
                            key_data_size = 1; // N.B. If null termination makes us happy for debugging, fine, but 
krista@3645
   666
                                               // if we include this in the size, libetpan will null terminate and 
krista@3645
   667
                                               // go bananas. We can't have a NUL in the mime text.
vb@3545
   668
vb@3594
   669
                            for (stringlist_t *sl = session->own_«yml:lcase(@name)»_state.own_keys;
vb@3545
   670
                                    sl && sl->value ; sl = sl->next)
vb@3545
   671
                            {
vb@3545
   672
                                char *_key_data = NULL;
vb@3545
   673
                                size_t _size = 0;
vb@3545
   674
                                status = export_secret_key(session, sl->value, &_key_data, &_size);
vb@3545
   675
                                if (status && status != PEP_KEY_NOT_FOUND) {
vb@3545
   676
                                    free(_data);
vb@3567
   677
                                    free_message(_m);
vb@3545
   678
                                    goto the_end;
vb@3545
   679
                                }
vb@3545
   680
vb@3545
   681
                                if (status != PEP_KEY_NOT_FOUND) {
vb@3545
   682
                                    assert(_key_data && _size);
vb@3545
   683
                                    char *n = realloc(key_data, key_data_size + _size);
vb@3545
   684
                                    if (!n) {
vb@3545
   685
                                        free(_data);
vb@3567
   686
                                        free_message(_m);
vb@3545
   687
                                        status = PEP_OUT_OF_MEMORY;
vb@3545
   688
                                        goto the_end;
vb@3545
   689
                                    }
vb@3545
   690
                                    key_data = n;
vb@3545
   691
                                    key_data_size += _size;
vb@3545
   692
                                    strlcat(key_data, _key_data, key_data_size);
vb@3545
   693
                                    free(_key_data);
vb@3545
   694
                                    _key_data = NULL;
vb@3545
   695
                                }
vb@3545
   696
                                status = export_key(session, sl->value, &_key_data, &_size);
vb@3545
   697
                                if (status && status != PEP_KEY_NOT_FOUND) {
vb@3545
   698
                                    free(_data);
vb@3567
   699
                                    free_message(_m);
vb@3545
   700
                                    goto the_end;
vb@3545
   701
                                }
vb@3545
   702
vb@3545
   703
                                if (status != PEP_KEY_NOT_FOUND) {
vb@3545
   704
                                    assert(_key_data && _size);
vb@3545
   705
                                    char *n = realloc(key_data, key_data_size + _size);
vb@3545
   706
                                    if (!n) {
vb@3545
   707
                                        free(_data);
vb@3567
   708
                                        free_message(_m);
vb@3545
   709
                                        status = PEP_OUT_OF_MEMORY;
vb@3545
   710
                                        goto the_end;
vb@3545
   711
                                    }
vb@3545
   712
                                    key_data = n;
vb@3545
   713
                                    key_data_size += _size;
vb@3545
   714
                                    strlcat(key_data, _key_data, key_data_size);
vb@3545
   715
                                    free(_key_data);
vb@3545
   716
                                    _key_data = NULL;
vb@3545
   717
                                }
vb@3545
   718
                            }
vb@3545
   719
vb@3545
   720
                            // add secret key data as attachment
vb@3545
   721
krista@3645
   722
                            // N.B. The -1 makes sure we do NOT add a NUL into the mime stream!
krista@3645
   723
                            bloblist_t *bl = bloblist_add(_m->attachments, key_data, key_data_size - 1,
vb@3545
   724
                                    "application/octet-stream", "file://own.key");
vb@3545
   725
                            if (!bl) {
vb@3545
   726
                                free(_data);
vb@3567
   727
                                free_message(_m);
vb@3545
   728
                                status = PEP_OUT_OF_MEMORY;
vb@3545
   729
                                goto the_end;
vb@3545
   730
                            }
vb@3545
   731
                            key_data = NULL;
vb@3545
   732
vb@3594
   733
                            status = encrypt_message(session, _m, extra, &m, PEP_enc_PEP, 0);
vb@3393
   734
                            if (status) {
vb@3393
   735
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@3393
   736
                                goto the_end;
vb@3393
   737
                            }
vb@3393
   738
                            free_message(_m);
vb@3393
   739
                            break;
vb@3393
   740
vb@3594
   741
                        default: // security=trusted only
vb@3129
   742
                            status = base_prepare_message(
vb@3143
   743
                                    session,
vb@3129
   744
                                    li->ident,
vb@3129
   745
                                    li->ident,
vb@3129
   746
                                    _data,
vb@3129
   747
                                    size,
vb@3129
   748
                                    NULL,
vb@3129
   749
                                    &_m
vb@3129
   750
                                );
vb@3129
   751
                            if (status) {
vb@3129
   752
                                free(_data);
vb@3129
   753
                                goto the_end;
vb@3129
   754
                            }
vb@3509
   755
vb@3594
   756
                            status = encrypt_message(session, _m, NULL, &m, PEP_enc_PEP, 0);
vb@2879
   757
                            if (status) {
vb@2899
   758
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@2878
   759
                                goto the_end;
vb@2879
   760
                            }
vb@2878
   761
                            free_message(_m);
vb@2878
   762
                    }
vb@2878
   763
vb@2899
   764
                    status = session->messageToSend(m);
vb@2870
   765
                    m = NULL;
vb@2870
   766
                }
vb@2870
   767
vb@2870
   768
            the_end:
vb@3594
   769
                free_stringlist(extra);
vb@2870
   770
                free_identity_list(channels);
vb@2870
   771
                free_message(m);
vb@2870
   772
                free(data);
vb@3545
   773
                free(key_data);
vb@2870
   774
                free_«@name»_message(msg);
vb@3747
   775
                if (status)
vb@3747
   776
                    SERVICE_ERROR_LOG(session, "send_«@name»_message()", status);
vb@2870
   777
                return status;
vb@2829
   778
            }
vb@2829
   779
vb@2870
   780
            PEP_STATUS recv_«@name»_event(
vb@2870
   781
                    PEP_SESSION session,
vb@2870
   782
                    «@name»_event_t *ev
vb@2870
   783
                )
vb@2870
   784
            {
vb@2870
   785
                assert(session && ev);
vb@2870
   786
                if (!(session && ev))
vb@2870
   787
                    return PEP_ILLEGAL_VALUE;
vb@2829
   788
vb@2870
   789
                PEP_STATUS status = PEP_STATUS_OK;
vb@2888
   790
                «@name»_PR fsm = (int) None;
vb@2888
   791
                int event = None;
vb@2829
   792
vb@2910
   793
                if (ev->event > None && ev->event < Extra) {
vb@2870
   794
                    status = update_«@name»_state(session, ev->msg, &fsm, &event);
vb@2870
   795
                    if (status)
vb@2888
   796
                        goto the_end;
vb@2829
   797
vb@2870
   798
                    if (ev->fsm) {
vb@2870
   799
                        if (ev->fsm != fsm |`> |` ev->event != event) {
vb@2870
   800
                            status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2888
   801
                            goto the_end;
vb@2870
   802
                        }
vb@2870
   803
                    }
vb@2888
   804
                    else if (ev->event) {
vb@2888
   805
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2888
   806
                        goto the_end;
vb@2870
   807
                    }
vb@2870
   808
                }
vb@2888
   809
                else {
vb@2888
   810
                    fsm = ev->fsm;
vb@2888
   811
                    event = ev->event;
vb@2888
   812
                }
vb@2829
   813
vb@3534
   814
                // update transport data
vb@3534
   815
vb@3534
   816
                if (ev->from) {
vb@3540
   817
                    free_identity(session->«yml:lcase(@name)»_state.common.from);
vb@3540
   818
                    session->«yml:lcase(@name)»_state.common.from = ev->from;
vb@3540
   819
                    ev->from = NULL;
vb@3534
   820
                }
vb@3534
   821
vb@3534
   822
                if (ev->signature_fpr) {
vb@3540
   823
                    free(session->«yml:lcase(@name)»_state.common.signature_fpr);
vb@3540
   824
                    session->«yml:lcase(@name)»_state.common.signature_fpr = ev->signature_fpr;
vb@3540
   825
                    ev->signature_fpr = NULL;
vb@3540
   826
                }
vb@3540
   827
vb@3540
   828
                // update own identities
vb@3540
   829
vb@3540
   830
                if (ev->own_identities && ev->own_identities->ident) {
vb@3594
   831
                    free_identity_list(session->own_«yml:lcase(@name)»_state.own_identities);
vb@3594
   832
                    session->own_«yml:lcase(@name)»_state.own_identities = ev->own_identities;
vb@3540
   833
                    ev->own_identities = NULL;
vb@3534
   834
                }
vb@3534
   835
vb@2888
   836
                status = «@name»_driver(session, fsm, event);
vb@2870
   837
vb@2888
   838
            the_end:
krista@3331
   839
                //free_«@name»_event(ev); // FIXME: We don't own this pointer. Are we sure it gets freed externally?
vb@2870
   840
                return status;
vb@2829
   841
            }
vb@2829
   842
vb@2870
   843
            ||
vb@2829
   844
        }
vb@2829
   845
vb@2829
   846
        apply "fsm", 0, mode=gen;
vb@2829
   847
    }
vb@2829
   848
vb@2909
   849
    template "fsm", mode=timeout
vb@2909
   850
    ||
vb@2909
   851
    static bool _«@name»_timeout(int state)
vb@2909
   852
    {
vb@2909
   853
        static int last_state = None;
vb@2909
   854
        static time_t switch_time = 0;
vb@2909
   855
vb@2909
   856
        if (state > Init) {
vb@2909
   857
            if (state == last_state) {
vb@2909
   858
                if (time(NULL) - switch_time > «yml:ucase(@name)»_THRESHOLD) {
vb@2909
   859
                    last_state = None;
vb@2909
   860
                    switch_time = 0;
vb@2909
   861
                    return true;
vb@2909
   862
                }
vb@2909
   863
            }
vb@2909
   864
            else {
vb@2909
   865
                last_state = state;
vb@2909
   866
                switch_time = time(NULL);
vb@2909
   867
            }
vb@2909
   868
        }
vb@2909
   869
        else {
vb@2909
   870
            last_state = None;
vb@2909
   871
            switch_time = 0;
vb@2909
   872
        }
vb@2909
   873
vb@2909
   874
        return false;
vb@2909
   875
    }
vb@2909
   876
vb@2909
   877
    ||
vb@2909
   878
vb@2909
   879
    template "fsm", mode=reset_state_machine
vb@2909
   880
    ||
vb@2909
   881
        case «../@name»_PR_«yml:lcase(@name)»: {
vb@2909
   882
            int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
vb@2909
   883
            switch (state) {
vb@2909
   884
                `` for "state[@name!='InitState' and @timeout != 'off']" |>>> case «@name»:
vb@2909
   885
                    if (_«@name»_timeout(state)) {
vb@2909
   886
                        session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = Init;
vb@2909
   887
                        event = Init;
vb@2914
   888
                        `` if "@threshold > 0" |>>>>> «@name»TimeoutHandler(session);
vb@2909
   889
                    }
vb@2909
   890
                    break;
vb@2909
   891
                
vb@2909
   892
                default:
vb@2909
   893
                    _«@name»_timeout(None);
vb@2909
   894
            }
vb@2909
   895
            break;
vb@2909
   896
        }
vb@2909
   897
vb@2909
   898
    ||
vb@2909
   899
vb@2890
   900
    template "fsm", mode=signal_message
vb@2890
   901
    {
vb@2890
   902
        ||
vb@2890
   903
        case «../@name»_PR_«yml:lcase(@name)»:
vb@3509
   904
            switch (msg->choice.«yml:lcase(@name)».present) {
vb@2891
   905
        ||
vb@3518
   906
        for "message[@security='unencrypted']" {
vb@3518
   907
        if "position()=1" |>> // these messages require a detached signature
vb@3518
   908
        ||
vb@3518
   909
                case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3518
   910
                    if (!signature_fpr) {
vb@3518
   911
                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3518
   912
                        goto the_end;
vb@3518
   913
                    }
vb@3518
   914
                    event = «@name»;
vb@3518
   915
                    break;
vb@2890
   916
vb@3518
   917
        ||
vb@2891
   918
        }
vb@3518
   919
        for "message[@security='untrusted']" {
vb@3518
   920
        if "position()=1" |>> // these messages must arrive encrypted
vb@3518
   921
        ||
vb@3518
   922
                case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3518
   923
                    if (rating < PEP_rating_reliable) {
vb@3518
   924
                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3518
   925
                        goto the_end;
vb@3518
   926
                    }
vb@3518
   927
                    event = «@name»;
vb@3518
   928
                    break;
vb@3338
   929
vb@3518
   930
        ||
vb@3338
   931
        }
vb@3518
   932
        for "message[@security!='unencrypted' and @security!='untrusted']" {
vb@3518
   933
        if "position()=1" |>> // these messages must come through a trusted channel
vb@3518
   934
        ||
vb@3518
   935
                case «../@name»_PR_«yml:mixedCase(@name)»:
vb@3518
   936
                    if (rating < PEP_rating_trusted) {
vb@3518
   937
                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3518
   938
                        goto the_end;
vb@3518
   939
                    }
vb@3593
   940
                    status = own_key_is_listed(session, signature_fpr, &is_own_key);
vb@3593
   941
                    if (status)
vb@3593
   942
                        goto the_end;
vb@3593
   943
                    if (!is_own_key) {
vb@3593
   944
                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@3593
   945
                        goto the_end;
vb@3593
   946
                    }
vb@3518
   947
                    event = «@name»;
vb@3518
   948
                    break;
vb@3338
   949
vb@3518
   950
        ||
vb@3338
   951
        }
vb@2891
   952
        ||
vb@2890
   953
                default:
vb@2890
   954
                    status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@2890
   955
                    goto the_end;
vb@2890
   956
            }
vb@2890
   957
            break;
vb@2890
   958
vb@2890
   959
        ||
vb@2890
   960
    }
vb@2890
   961
vb@2838
   962
    template "fsm", mode=event
vb@2838
   963
    {
vb@2838
   964
    ||
vb@2843
   965
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@3509
   966
        switch (msg->choice.«yml:lcase(@name)».present) {
vb@2838
   967
    ||
vb@2838
   968
    for "message"
vb@2838
   969
    ||
vb@3509
   970
            case «../@name»_PR_«yml:mixedCase(@name)»:
vb@2838
   971
                ev->event = «@name»;
vb@2838
   972
                break;
vb@2838
   973
    ||
vb@2838
   974
    ||
vb@2838
   975
            default:
vb@2838
   976
                // unknown message type
vb@2838
   977
                free(ev);
vb@2838
   978
                return NULL;
vb@2838
   979
        }
vb@2838
   980
        break;
vb@2838
   981
    }
vb@2838
   982
vb@2838
   983
    ||
vb@2838
   984
    }
vb@2838
   985
vb@2829
   986
    template "fsm", mode=driver
vb@2829
   987
    ||
vb@2843
   988
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@2839
   989
        int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
vb@2839
   990
        next_state = fsm_«@name»(session, state, event);
vb@2838
   991
        if (next_state > None) {
vb@2838
   992
            session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = next_state;
vb@2838
   993
            event = Init;
vb@2838
   994
        }
vb@2838
   995
        else if (next_state < None) {
Hussein@3167
   996
            return PEP_STATEMACHINE_ERROR - next_state;
vb@2838
   997
        }
vb@2829
   998
        break;
vb@2829
   999
    }
vb@2829
  1000
vb@2829
  1001
    ||
vb@2829
  1002
vb@2829
  1003
    template "fsm", mode=gen {
vb@2829
  1004
        document "generated/{@name}_fsm.h", "text" {
vb@2829
  1005
        ||
vb@2829
  1006
        // This file is under GNU General Public License 3.0
vb@2829
  1007
        // see LICENSE.txt
vb@2829
  1008
vb@2829
  1009
        #pragma once
vb@2829
  1010
vb@2829
  1011
        #include "«../@name»_impl.h"
vb@2829
  1012
vb@2829
  1013
        #ifdef __cplusplus
vb@2829
  1014
        extern "C" {
vb@2829
  1015
        #endif
vb@2829
  1016
vb@2829
  1017
        // state machine for «@name»
krista@2271
  1018
krista@2271
  1019
        // states
krista@2271
  1020
krista@2271
  1021
        typedef enum _«@name»_state {
vb@2829
  1022
            «@name»_state_None = None,
vb@2829
  1023
            «@name»_state_Init = Init,
vb@2829
  1024
        ||
vb@2829
  1025
        for "func:distinctName(state[not(@name='InitState')])"
vb@2829
  1026
            |> «@name»`if "position()!=last()" > , `
vb@2829
  1027
        ||
krista@2271
  1028
        } «@name»_state;
krista@2271
  1029
krista@2271
  1030
        // events
krista@2271
  1031
krista@2271
  1032
        typedef enum _«@name»_event {
vb@2829
  1033
            «@name»_event_None = None,
vb@2829
  1034
            «@name»_event_Init = Init,
krista@2271
  1035
        ||
vb@3388
  1036
        for "message" {
krista@2271
  1037
            const "name", "@name";
vb@2829
  1038
            |> «$name» = «/protocol/fsm/message[@name=$name]/@id»,
krista@2271
  1039
        }
vb@2829
  1040
        |> «@name»_event_Extra = Extra,
vb@3387
  1041
        for "external" {
vb@3385
  1042
            if "@id < 128"
vb@3385
  1043
                error > external «@name» must have ID >= 128 but it's «@id»
vb@3384
  1044
            |> «@name» = «@id»,
vb@3385
  1045
        }
vb@2830
  1046
        for "func:distinctName(state/event[not(../../message/@name=@name or ../../external/@name=@name)])" {
vb@2829
  1047
            if "@name!='Init'"
vb@2829
  1048
                |> «@name»`if "position()!=last()" > , `
vb@2829
  1049
        }
krista@2271
  1050
        ||
krista@2271
  1051
        } «@name»_event;
krista@2271
  1052
vb@2829
  1053
        // state machine
krista@2271
  1054
vb@2829
  1055
        const char *«@name»_state_name(int state);
vb@2881
  1056
        const char *«@name»_event_name(int event);
krista@2271
  1057
vb@2838
  1058
        // the state machine function is returning the next state in case of a
vb@2838
  1059
        // transition or None for staying
vb@2838
  1060
krista@2271
  1061
        «@name»_state fsm_«@name»(
krista@2271
  1062
                PEP_SESSION session,
krista@2271
  1063
                «@name»_state state,
vb@2829
  1064
                «@name»_event event
krista@2271
  1065
            );
krista@2271
  1066
krista@2271
  1067
        #ifdef __cplusplus
krista@2271
  1068
        }
krista@2271
  1069
        #endif
krista@2271
  1070
krista@2271
  1071
        ||
krista@2271
  1072
        }
vb@2829
  1073
vb@2829
  1074
        document "generated/{@name}_fsm.c", "text" {
krista@2271
  1075
        ||
vb@2829
  1076
        // This file is under GNU General Public License 3.0
vb@2829
  1077
        // see LICENSE.txt
krista@2271
  1078
vb@2829
  1079
        #include "«@name»_fsm.h"
vb@2847
  1080
        #include <stdlib.h>
krista@2271
  1081
vb@2829
  1082
        const char *«@name»_state_name(int state)
vb@2829
  1083
        {
vb@2829
  1084
            switch (state) {
vb@2829
  1085
                case End:
vb@2829
  1086
                    return "End";
vb@2829
  1087
                case None:
vb@2829
  1088
                    return "None";
vb@2829
  1089
                case Init:
vb@2829
  1090
                    return "InitState";
vb@2829
  1091
        ||
vb@2829
  1092
        for "func:distinctName(state[not(@name='InitState')])" {
vb@2829
  1093
            |>> case «@name»:
vb@2829
  1094
            |>>> return "«@name»";
vb@2829
  1095
        }
vb@2829
  1096
        ||
vb@2829
  1097
                default:
vb@2829
  1098
                    return "unknown state";
vb@2829
  1099
            }
vb@2829
  1100
        }
krista@2271
  1101
vb@2881
  1102
        const char *«@name»_event_name(int event)
vb@2881
  1103
        {
vb@2881
  1104
            switch (event) {
vb@2881
  1105
                case None:
vb@2881
  1106
                    return "None";
vb@2881
  1107
                case Init:
vb@2881
  1108
                    return "Init";
vb@2881
  1109
        ||
vb@2881
  1110
        for "func:distinctName(state/event[not(@name='Init')])" {
vb@2881
  1111
            |>> case «@name»:
vb@2881
  1112
            |>>> return "«@name»";
vb@2881
  1113
        }
vb@2881
  1114
        ||
vb@2881
  1115
                default:
vb@2881
  1116
                    return "unknown event";
vb@2881
  1117
            }
vb@2881
  1118
        }
vb@2881
  1119
vb@2881
  1120
vb@2829
  1121
        static char *_str(int n, bool hex)
vb@2829
  1122
        {
vb@2829
  1123
            char *buf = calloc(1, 24);
vb@2829
  1124
            assert(buf);
vb@2829
  1125
            if (!buf)
vb@2829
  1126
                return NULL;
vb@2829
  1127
vb@2829
  1128
            if (hex)
vb@2829
  1129
                snprintf(buf, 24, "%.4x", n);
vb@2829
  1130
            else
vb@2829
  1131
                snprintf(buf, 24, "%d", n);
vb@2829
  1132
            return buf;
vb@2829
  1133
        }
vb@2829
  1134
vb@2829
  1135
        #define «@name»_ERR_LOG(t, d) log_event(session, (t), "«@name»", (d), "error")
vb@2829
  1136
vb@2829
  1137
        static PEP_STATUS _«@name»_ERR_LOG_int(PEP_SESSION session, char *t, int n, bool hex)
vb@2829
  1138
        {
vb@2829
  1139
            char *_buf = _str(n, hex);
vb@2829
  1140
            if (!_buf)
vb@2829
  1141
                return PEP_OUT_OF_MEMORY;
vb@2829
  1142
            PEP_STATUS status = «@name»_ERR_LOG(t, _buf);
vb@2829
  1143
            free(_buf);
vb@2829
  1144
            return status;
vb@2829
  1145
        }
vb@2829
  1146
vb@2829
  1147
        #define «@name»_ERR_LOG_INT(t, n) _«@name»_ERR_LOG_int(session, (t), (n), false)
vb@2829
  1148
        #define «@name»_ERR_LOG_HEX(t, n) _«@name»_ERR_LOG_int(session, (t), (n), true)
vb@2829
  1149
        #define «@name»_SERVICE_LOG(t, d) SERVICE_LOG(session, (t), "«@name»", (d))
vb@2829
  1150
vb@2829
  1151
        «@name»_state fsm_«@name»(
krista@2271
  1152
                PEP_SESSION session,
vb@2829
  1153
                «@name»_state state,
vb@2829
  1154
                «@name»_event event
krista@2271
  1155
            )
krista@2271
  1156
        {
krista@2271
  1157
            assert(session);
krista@2271
  1158
            if (!session)
vb@2829
  1159
                return invalid_state;
krista@2271
  1160
vb@3365
  1161
            if (state == None)
vb@3365
  1162
                state = «@name»_state_Init;
vb@3365
  1163
krista@2271
  1164
            switch (state) {
vb@2829
  1165
                `` apply "state", 2, mode=fsm
krista@2271
  1166
                default:
vb@2838
  1167
                    «@name»_ERR_LOG_INT("invalid state", state);
vb@2829
  1168
                    return invalid_state;
krista@2271
  1169
            }
vb@2829
  1170
            
vb@2838
  1171
            return None;
krista@2271
  1172
        }
krista@2271
  1173
krista@2271
  1174
        ||
vb@2829
  1175
        }
vb@2829
  1176
    }
vb@2829
  1177
    
vb@2829
  1178
    template "state", mode=fsm {
vb@2829
  1179
        choose {
vb@2829
  1180
            when "@name='InitState'" | case «../@name»_state_Init:
vb@2829
  1181
            otherwise | case «@name»:
vb@2829
  1182
        }
vb@2829
  1183
        ||
vb@2829
  1184
            «../@name»_SERVICE_LOG("in state", "«@name»");
vb@2829
  1185
vb@2829
  1186
            switch (event) {
vb@3366
  1187
                case None:
vb@3366
  1188
                    «../@name»_SERVICE_LOG("received None event", "ignoring");
vb@3366
  1189
                    break;
vb@3366
  1190
     
vb@2838
  1191
        ||
vb@2838
  1192
        if "not(event[@name='Init'])"
vb@2838
  1193
        ||
vb@2838
  1194
                case Init:
vb@3379
  1195
                    «../@name»_SERVICE_LOG("received Init but nothing to do", "Init");
vb@2838
  1196
                    break;
vb@2838
  1197
vb@2838
  1198
        ||
vb@2838
  1199
        ||
vb@2829
  1200
                `` apply "event", 2, mode=fsm
vb@2829
  1201
                default:
vb@2885
  1202
                    // ignore events not handled here
vb@3594
  1203
                    «../@name»_SERVICE_LOG("ignoring event", «../@name»_event_name(event));
vb@3362
  1204
                    return invalid_event;
vb@2829
  1205
            }
vb@2829
  1206
            break;
vb@2829
  1207
vb@2829
  1208
        ||
krista@2271
  1209
    }
krista@2271
  1210
vb@2829
  1211
    template "event", mode=fsm {
vb@2829
  1212
        | case «@name»: {
vb@2865
  1213
        if "condition|action|send" |> PEP_STATUS status;
vb@2829
  1214
        if "condition" |> bool result = false;
vb@2865
  1215
        if "condition|action|send" |
krista@2271
  1216
        ||
vb@2829
  1217
            «../../@name»_SERVICE_LOG("received event", "«@name»");
vb@2907
  1218
            `` apply "transition|action|condition|else|send";
vb@2829
  1219
        ||
vb@2829
  1220
        if "name(*[last()])!='transition'" {
vb@2829
  1221
            |
vb@2843
  1222
            |> «../../@name»_SERVICE_LOG("remaining in state", "«../@name»");
vb@2829
  1223
            |> break;
vb@2829
  1224
        }
vb@2829
  1225
        ||
vb@2829
  1226
        }
vb@2829
  1227
        
vb@2829
  1228
        ||
vb@2829
  1229
    }
vb@2829
  1230
vb@2829
  1231
    template "transition" {
vb@2865
  1232
        const "fsm", "ancestor::fsm";
krista@2271
  1233
        ||
krista@2271
  1234
vb@2829
  1235
        «$fsm/@name»_SERVICE_LOG("transition to state", "«@target»");
vb@2838
  1236
        return «@target»;
krista@2271
  1237
        ||
vb@2829
  1238
    }
krista@2271
  1239
vb@2865
  1240
    template "send" {
vb@2865
  1241
        const "fsm", "ancestor::fsm";
vb@2865
  1242
        const "protocol", "ancestor::protocol";
vb@2865
  1243
        ||
vb@2865
  1244
vb@2865
  1245
        «$fsm/@name»_SERVICE_LOG("send message", "«@name»");
vb@3509
  1246
        status = send_«$protocol/@name»_message(session, «$fsm/@id», «$fsm/@name»_PR_«yml:mixedCase(@name)»);
vb@3089
  1247
        if (status == PEP_OUT_OF_MEMORY)
vb@3089
  1248
            return out_of_memory;
vb@3089
  1249
        if (status) {
vb@3089
  1250
            «$fsm/@name»_ERR_LOG_HEX("sending «@name» failed", status);
vb@3089
  1251
            return cannot_send;
vb@3089
  1252
        }
vb@2865
  1253
        ||
vb@2865
  1254
    }
vb@2865
  1255
vb@2829
  1256
    template "action" {
vb@2865
  1257
        const "fsm", "ancestor::fsm";
vb@2865
  1258
        ||
vb@2829
  1259
vb@2865
  1260
        «$fsm/@name»_SERVICE_LOG("do action", "«@name»");
vb@2865
  1261
        status = «@name»(session);
vb@3089
  1262
        if (status == PEP_OUT_OF_MEMORY)
vb@3089
  1263
            return out_of_memory;
vb@2829
  1264
        if (status) {
vb@2829
  1265
            «$fsm/@name»_ERR_LOG_HEX("executing action «@name»() failed", status);
vb@2829
  1266
            return invalid_action;
krista@2271
  1267
        }
krista@2271
  1268
        ||
krista@2271
  1269
    }
krista@2271
  1270
vb@2829
  1271
    template "condition" {
vb@2865
  1272
        const "fsm", "ancestor::fsm";
krista@2271
  1273
        ||
krista@2271
  1274
vb@2829
  1275
        status = «@name»(session, &result);
vb@3089
  1276
        if (status == PEP_OUT_OF_MEMORY)
vb@3089
  1277
            return out_of_memory;
vb@2829
  1278
        if (status) {
vb@2829
  1279
            «$fsm/@name»_ERR_LOG_HEX("computing condition «@name» failed", status);
vb@2829
  1280
            return invalid_condition;
krista@2271
  1281
        }
vb@2829
  1282
        if (result) {
vb@2843
  1283
            «$fsm/@name»_SERVICE_LOG("condition applies", "«@name»");
krista@2271
  1284
        ||
vb@2907
  1285
        apply "transition|action|condition|else|send";
vb@2907
  1286
        | }
vb@2907
  1287
    }
vb@2907
  1288
vb@2907
  1289
    template "else" {
vb@3520
  1290
        if "not(name(preceding-sibling::*[1]) = 'condition')"
vb@2907
  1291
            error "else without if";
vb@2907
  1292
vb@2907
  1293
        | else {
vb@2907
  1294
        |> «ancestor::fsm/@name»_SERVICE_LOG("condition does not apply", "«preceding-sibling::*[last()]/@name»");
vb@2907
  1295
        apply "transition|action|condition|else|send";
krista@2271
  1296
        | }
krista@2271
  1297
    }
krista@2271
  1298
}