sync/gen_message_func.ysl2
author Neal H. Walfield <neal@pep.foundation>
Wed, 08 May 2019 12:20:37 +0200
branchemail_comparison
changeset 3649 1dd837adc30b
parent 3616 8229562ff2a1
child 3767 11a9d0f61073
permissions -rw-r--r--
Fix public key.
vb@2831
     1
// This file is under GNU General Public License 3.0
vb@2831
     2
// see LICENSE.txt
vb@2831
     3
vb@2831
     4
// generate message functions
vb@2831
     5
vb@3512
     6
// Copyleft (c) 2017-2019, p≡p foundation
vb@2831
     7
vb@2831
     8
// Written by Volker Birk
vb@2831
     9
vb@2831
    10
include yslt.yml2
vb@2831
    11
vb@2831
    12
tstylesheet {
vb@2831
    13
vb@2831
    14
include standardlib.ysl2
vb@2831
    15
include ./functions.ysl2
vb@2831
    16
vb@2831
    17
template "/" {
vb@2831
    18
    apply "protocol", 0, mode=header;
vb@2831
    19
    apply "protocol", 0, mode=impl;
vb@2831
    20
}
vb@2831
    21
vb@2831
    22
template "protocol", mode=header
vb@2831
    23
    document "generated/{@name}_func.h", "text"
vb@2831
    24
||
vb@2831
    25
// This file is under GNU General Public License 3.0
vb@2831
    26
// see LICENSE.txt
vb@2831
    27
vb@2831
    28
#pragma once
vb@2831
    29
vb@2831
    30
#ifdef __cplusplus
vb@2831
    31
extern "C" {
vb@2831
    32
#endif
vb@2831
    33
vb@2831
    34
#include <stdbool.h>
vb@2831
    35
vb@2831
    36
#include "../asn.1/«@name».h"
vb@2831
    37
`` for "func:distinctType(fsm/message/field[not(func:basicType())])" | #include "../asn.1/«@type».h"
vb@2831
    38
vb@2831
    39
// state
vb@2831
    40
vb@2831
    41
struct «@name»_state_s {
vb@3594
    42
    // common buffer for all types of «@name» messages
vb@3594
    43
vb@2838
    44
    struct common_state_s {
vb@3601
    45
        // intermediate store own challenge
vb@3601
    46
        TID_t challenge;
vb@3601
    47
vb@3594
    48
        // transport data
vb@2831
    49
        pEp_identity *from;
vb@3590
    50
        char *signature_fpr;
vb@2838
    51
    } common;
vb@2831
    52
    `` apply "fsm", mode=state
vb@2831
    53
};
vb@2831
    54
vb@3594
    55
// own state
vb@3594
    56
vb@2831
    57
struct own_«@name»_state_s {
vb@3594
    58
    stringlist_t *own_keys;
vb@3594
    59
    identity_list *own_identities;
vb@3594
    60
vb@3594
    61
    `` if "func:distinctName(fsm/message/field[@type='TID'])" |> // active TIDs
vb@2831
    62
    `` for "func:distinctName(fsm/message/field[@type='TID'])" |> «func:ctype()» «@name»;
vb@3590
    63
vb@3590
    64
    // transport data
vb@3590
    65
    char *signature_fpr;
vb@2831
    66
};
vb@2831
    67
vb@2831
    68
void free_«@name»_state(PEP_SESSION session);
vb@2831
    69
vb@2831
    70
// functions for protocol «@name»
vb@2831
    71
vb@2831
    72
«@name»_t *new_«@name»_message(«@name»_PR fsm, int message_type);
vb@2831
    73
void free_«@name»_message(«@name»_t *msg);
vb@2831
    74
vb@2831
    75
PEP_STATUS update_«@name»_state(PEP_SESSION session, «@name»_t *msg,
vb@2831
    76
        «@name»_PR *fsm, int *message_type);
vb@2831
    77
vb@2854
    78
PEP_STATUS update_«@name»_message(PEP_SESSION session, «@name»_t *msg);
vb@2831
    79
vb@2831
    80
#ifdef __cplusplus
vb@2831
    81
}
vb@2831
    82
#endif
vb@2831
    83
vb@2831
    84
||
vb@2831
    85
vb@2831
    86
template "fsm", mode=state
vb@2831
    87
||
vb@3594
    88
vb@3594
    89
// buffer for «@name» messages
vb@3594
    90
vb@2831
    91
struct _«@name»_state_s {
vb@2831
    92
    int state;
vb@2831
    93
vb@2831
    94
    `` for "func:distinctName(message/field)" |> «func:ctype()» «@name»;
vb@2831
    95
} «yml:lcase(@name)»;
vb@2831
    96
||
vb@2831
    97
vb@2831
    98
template "protocol", mode=impl
vb@2831
    99
    document "generated/{@name}_func.c", "text" {
vb@2831
   100
||
vb@2831
   101
// This file is under GNU General Public License 3.0
vb@2831
   102
// see LICENSE.txt
vb@2831
   103
vb@2831
   104
#include <assert.h>
vb@2831
   105
#include <stdlib.h>
vb@2831
   106
#include "pEp_internal.h"
vb@2831
   107
#include "map_asn1.h"
vb@2831
   108
#include "«@name»_func.h"
vb@3123
   109
`` for "fsm" | #include "«@name»_fsm.h"
vb@2831
   110
vb@2831
   111
void free_«@name»_state(PEP_SESSION session)
vb@2831
   112
{
vb@2831
   113
    if (!session)
vb@2831
   114
        return;
vb@2831
   115
vb@2838
   116
    free_identity(session->«yml:lcase(@name)»_state.common.from);
vb@3392
   117
    free(session->«yml:lcase(@name)»_state.common.signature_fpr);
vb@3594
   118
    free_stringlist(session->own_«yml:lcase(@name)»_state.own_keys);
vb@3594
   119
    free_identity_list(session->own_«yml:lcase(@name)»_state.own_identities);
krista@3616
   120
    session->own_«yml:lcase(@name)»_state.own_keys = NULL;
krista@3616
   121
    session->own_«yml:lcase(@name)»_state.own_identities = NULL;
vb@2831
   122
vb@2831
   123
||
vb@2831
   124
for "fsm"
vb@2831
   125
    for "func:distinctName(message/field[not(func:basicType())])"
vb@2831
   126
        |> ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_«@type», &session->«yml:lcase(../../../@name)»_state.«yml:lcase(../../@name)».«@name»);
vb@2831
   127
for "func:distinctName(fsm/message/field[@type='TID'])"
vb@2831
   128
    |> ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_«@type», &session->own_«yml:lcase(../../../@name)»_state.«@name»);
vb@2831
   129
||
vb@2838
   130
vb@2844
   131
    memset(&session->«yml:lcase(@name)»_state, 0, sizeof(session->«yml:lcase(@name)»_state));
vb@2831
   132
}
vb@2831
   133
vb@2831
   134
«@name»_t *new_«@name»_message(«@name»_PR fsm, int message_type)
vb@2831
   135
{
vb@2831
   136
    «@name»_t *msg = calloc(sizeof(«@name»_t), 1);
vb@2831
   137
    assert(msg);
vb@2831
   138
    if (!msg)
vb@2831
   139
        return NULL;
vb@2831
   140
vb@2841
   141
    if (fsm) {
vb@2841
   142
        msg->present = fsm;
vb@2841
   143
        if (message_type) {
vb@2841
   144
            switch (fsm) {
vb@2841
   145
                `` apply "fsm", 4, mode=impl
vb@2841
   146
                default:
vb@2841
   147
                    free(msg);
vb@2841
   148
                    return NULL;
vb@2841
   149
            }
vb@2841
   150
        }
vb@2831
   151
    }
vb@2831
   152
vb@2831
   153
    return msg;
vb@2831
   154
}
vb@2831
   155
vb@2831
   156
void free_«@name»_message(«@name»_t *msg)
vb@2831
   157
{
vb@2831
   158
    ASN_STRUCT_FREE(asn_DEF_«@name», msg);
vb@2831
   159
}
vb@2831
   160
vb@2831
   161
PEP_STATUS update_«@name»_state(PEP_SESSION session, «@name»_t *msg,
vb@2831
   162
        «@name»_PR *fsm, int *message_type)
vb@2831
   163
{
vb@2831
   164
    int result = 0;
vb@2831
   165
vb@2831
   166
    assert(session && msg && fsm && message_type);
vb@2831
   167
    if (!(session && msg && fsm && message_type))
vb@2831
   168
        return PEP_ILLEGAL_VALUE;
vb@2831
   169
vb@2831
   170
    *fsm = 0;
vb@3123
   171
    *message_type = None;
vb@2831
   172
vb@2831
   173
    switch (msg->present) {
vb@2831
   174
        case «@name»_PR_NOTHING:
vb@2831
   175
            return PEP_ILLEGAL_VALUE;
vb@2831
   176
vb@2831
   177
        `` apply "fsm", 2, mode=update_state
vb@2831
   178
        default:
vb@2831
   179
            return PEP_ILLEGAL_VALUE;
vb@2831
   180
    }
vb@2831
   181
vb@2831
   182
    *fsm = msg->present;
vb@2831
   183
    return PEP_STATUS_OK;
vb@2831
   184
}
vb@2831
   185
vb@2854
   186
PEP_STATUS update_«@name»_message(PEP_SESSION session, «@name»_t *msg)
vb@2831
   187
{
vb@2831
   188
    assert(session && msg);
vb@2831
   189
    if (!(session && msg))
vb@2831
   190
        return PEP_ILLEGAL_VALUE;
vb@2831
   191
vb@2854
   192
    int fsm = msg->present;
vb@2831
   193
    switch (fsm) {
vb@2831
   194
        case «@name»_PR_NOTHING:
vb@2831
   195
            return PEP_ILLEGAL_VALUE;
vb@2831
   196
vb@2831
   197
        `` apply "fsm", 2, mode=update_message
vb@2831
   198
        default:
vb@2831
   199
            return PEP_ILLEGAL_VALUE;
vb@2831
   200
    }
vb@2831
   201
vb@2831
   202
    return PEP_STATUS_OK;
vb@2831
   203
}
vb@2831
   204
vb@2831
   205
||
vb@2831
   206
}
vb@2831
   207
vb@2831
   208
template "fsm", mode=update_message
vb@2831
   209
||
vb@2831
   210
case «../@name»_PR_«yml:lcase(@name)»:
vb@2854
   211
    {
vb@3509
   212
        int message_type = msg->choice.«yml:lcase(@name)».present;
vb@2854
   213
        switch (message_type) {
vb@3509
   214
            case «@name»_PR_NOTHING:
vb@2854
   215
                return PEP_ILLEGAL_VALUE;
vb@2831
   216
vb@2854
   217
            `` apply "message", 2, mode=update_message
vb@2854
   218
            default:
vb@2854
   219
                return PEP_ILLEGAL_VALUE;
vb@2854
   220
        }
vb@2854
   221
        break;
vb@2831
   222
    }
vb@2831
   223
||
vb@2831
   224
vb@2831
   225
template "message", mode=update_message {
vb@2831
   226
    ||
vb@3509
   227
    case «../@name»_PR_«yml:mixedCase(@name)»:
vb@2855
   228
        `` apply "auto"
vb@2856
   229
        `` apply "field", mode=update_message
vb@2831
   230
        break;
vb@2831
   231
vb@2831
   232
    ||
vb@2831
   233
}
vb@2831
   234
vb@2855
   235
template "auto" choose {
vb@2856
   236
    when "@type = 'Version'" {
vb@2856
   237
        const "fsm", "ancestor::fsm";
vb@2855
   238
        ||
vb@2855
   239
        {
vb@2855
   240
            long *major = (long *) malloc(sizeof(long));
vb@2855
   241
            long *minor = (long *) malloc(sizeof(long));
vb@2855
   242
            assert(major && minor);
vb@2855
   243
            if (!(major && minor))
vb@2855
   244
                return PEP_OUT_OF_MEMORY;
vb@2855
   245
vb@2856
   246
            *major = «$fsm/version/@major»;
vb@2856
   247
            *minor = «$fsm/version/@minor»;
vb@2855
   248
vb@3509
   249
            msg->choice.«yml:lcase($fsm/@name)».choice.«yml:mixedCase(../@name)».«@name».major = major;
vb@3509
   250
            msg->choice.«yml:lcase($fsm/@name)».choice.«yml:mixedCase(../@name)».«@name».minor = minor;
vb@2855
   251
        }
vb@2855
   252
vb@2855
   253
        ||
vb@2856
   254
    }
vb@2855
   255
vb@2855
   256
    otherwise
vb@2855
   257
        error "unkown type for auto in message: {@type}; allowed types: Version";
vb@2855
   258
}
vb@2855
   259
vb@2831
   260
template "field", mode=update_message {
vb@2856
   261
    const "message_name", "yml:mixedCase(../@name)";
vb@2856
   262
    const "state" choose {
vb@2856
   263
        when "@type='TID'"
vb@2856
   264
            > own_«yml:lcase(ancestor::protocol/@name)»_state
vb@2856
   265
        otherwise
vb@2856
   266
            > «yml:lcase(ancestor::protocol/@name)»_state.«yml:lcase(ancestor::fsm/@name)»
vb@2856
   267
    }
vb@2856
   268
vb@2831
   269
    choose {
vb@2831
   270
        when "func:basicType()" // copyable
vb@2831
   271
        ||
vb@3509
   272
        msg->choice.«yml:lcase(../../@name)».choice.«$message_name».«@name»
vb@2856
   273
                 = session->«$state».«@name»;
vb@2831
   274
vb@2831
   275
        ||
vb@2831
   276
        when "@type='IdentityList'"
vb@2831
   277
        ||
vb@2831
   278
        {
vb@2831
   279
            identity_list *il = IdentityList_to_identity_list(
vb@2856
   280
                    &session->«$state».«@name», NULL);
vb@2831
   281
            if (!il)
vb@2831
   282
                return PEP_OUT_OF_MEMORY;
vb@2831
   283
            IdentityList_t *_il = IdentityList_from_identity_list(il,
vb@3509
   284
                    &msg->choice.«yml:lcase(../../@name)».choice.«$message_name».«@name»);
vb@2831
   285
            free_identity_list(il);
vb@2831
   286
            if (!_il)
vb@2831
   287
                return PEP_OUT_OF_MEMORY;
vb@2831
   288
        }
vb@2831
   289
vb@2831
   290
        ||
vb@2831
   291
        otherwise // string based
vb@2831
   292
        ||
vb@2856
   293
        {
vb@2856
   294
            int result = OCTET_STRING_fromBuf(
vb@3509
   295
                    &msg->choice.«yml:lcase(../../@name)».choice.«$message_name».«@name»,
vb@2856
   296
                    (char *) session->«$state».«@name».buf,
vb@2856
   297
                    session->«$state».«@name».size
vb@2856
   298
                );
vb@2856
   299
            if (result)
vb@2856
   300
                return PEP_OUT_OF_MEMORY;
vb@2856
   301
        }
vb@2831
   302
        ||
vb@2831
   303
    }
vb@2831
   304
}
vb@2831
   305
vb@2831
   306
template "fsm", mode=update_state
vb@2831
   307
||
vb@2831
   308
case «../@name»_PR_«yml:lcase(@name)»:
vb@3509
   309
    switch (msg->choice.«yml:lcase(@name)».present) {
vb@3509
   310
        case «@name»_PR_NOTHING:
vb@2831
   311
            return PEP_ILLEGAL_VALUE;
vb@2831
   312
vb@2831
   313
        `` apply "message", 2, mode=update_state
vb@2831
   314
        default:
vb@2831
   315
            return PEP_ILLEGAL_VALUE;
vb@2831
   316
    }
vb@2831
   317
    break;
vb@2831
   318
vb@2831
   319
||
vb@2831
   320
vb@2831
   321
template "message", mode=update_state {
vb@2831
   322
    const "message_name", "concat(yml:lcase(substring(@name,1,1)), substring(@name,2))";
vb@2831
   323
    ||
vb@3509
   324
    case «../@name»_PR_«$message_name»:
vb@2831
   325
        `` apply "field", mode=update_state with "message_name", "$message_name"
vb@3123
   326
        *message_type = «yml:capit($message_name)»;
vb@2831
   327
        break;
vb@2831
   328
vb@2831
   329
    ||
vb@2831
   330
}
vb@2831
   331
vb@2831
   332
template "field", mode=update_state {
vb@2831
   333
    param "message_name";
vb@2831
   334
    choose {
vb@2831
   335
        when "func:basicType()" // copyable
vb@2831
   336
        ||
vb@2831
   337
        session->«yml:lcase(../../../@name)»_state.«yml:lcase(../../@name)».«@name» = msg->choice.«yml:lcase(../../@name)»
vb@3509
   338
                .choice.«$message_name».«@name»;
vb@2831
   339
vb@2831
   340
        ||
vb@2831
   341
        when "@type='IdentityList'"
vb@2831
   342
        ||
vb@2831
   343
        {
vb@2831
   344
            identity_list *il = IdentityList_to_identity_list(
vb@3509
   345
                    &msg->choice.«yml:lcase(../../@name)».choice.«$message_name».«@name», NULL);
vb@2831
   346
            if (!il)
vb@2831
   347
                return PEP_OUT_OF_MEMORY;
vb@2831
   348
            IdentityList_t *_il = IdentityList_from_identity_list(il,
vb@2831
   349
                    &session->«yml:lcase(../../../@name)»_state.«yml:lcase(../../@name)».«@name»);
vb@2831
   350
            free_identity_list(il);
vb@2831
   351
            if (!_il)
vb@2831
   352
                return PEP_OUT_OF_MEMORY;
vb@2831
   353
        }
vb@2831
   354
vb@2831
   355
        ||
vb@2831
   356
        otherwise // string based
vb@2831
   357
        ||
vb@2831
   358
        result = OCTET_STRING_fromBuf(&session->«yml:lcase(../../../@name)»_state.«yml:lcase(../../@name)».«@name»,
vb@3509
   359
                (char *) msg->choice.«yml:lcase(../../@name)».choice.«$message_name».«@name».buf,
vb@3509
   360
                msg->choice.«yml:lcase(../../@name)».choice.«$message_name».«@name».size);
vb@2831
   361
        if (result)
vb@2831
   362
            return PEP_OUT_OF_MEMORY;
vb@2831
   363
vb@2831
   364
        ||
vb@2831
   365
    }
vb@2831
   366
}
vb@2831
   367
vb@2831
   368
template "fsm", mode=impl
vb@2831
   369
||
vb@2831
   370
case «../@name»_PR_«yml:lcase(@name)»:
vb@3509
   371
    msg->choice.«yml:lcase(@name)».present = message_type;
vb@2831
   372
        break;
vb@2831
   373
vb@2831
   374
||
vb@2831
   375
vb@2831
   376
}
vb@2831
   377