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