src/sync_fsm.c
author Krista Grothoff <krista@pep-project.org>
Wed, 28 Sep 2016 13:53:26 +0200
branchENGINE-96
changeset 1214 14d0f66604df
parent 1206 5eb41656c641
child 1215 4f19146d2df5
permissions -rw-r--r--
Closing ENGINE-96 branch
vb@616
     1
#include "sync_fsm.h"
vb@616
     2
vb@616
     3
// state machine for DeviceState
vb@616
     4
vb@616
     5
DeviceState_state fsm_DeviceState(
vb@627
     6
        PEP_SESSION session,
vb@616
     7
        DeviceState_state state,
vb@616
     8
        DeviceState_event event,
vb@939
     9
        Identity partner,
vb@951
    10
        void *extra
vb@616
    11
    )
vb@616
    12
{
vb@964
    13
    int cond_result;
vb@959
    14
    PEP_STATUS status = PEP_STATUS_OK;
vb@959
    15
vb@616
    16
    switch (state) {
vb@951
    17
        case InitState:
vb@951
    18
            switch (event) {
vb@951
    19
                case Init:
vb@959
    20
                    cond_result = storedGroupKeys(session);
vb@964
    21
                    if (cond_result < 0)
vb@964
    22
                        return cond_result;
vb@959
    23
                    if (cond_result) {
vb@951
    24
                        return Grouped;
vb@951
    25
                    }
vb@951
    26
                    return Sole;
vb@951
    27
                default:
vb@951
    28
                    return (DeviceState_state) invalid_event;
vb@951
    29
            }
vb@951
    30
            break;
vb@951
    31
        
vb@951
    32
        case Sole:
vb@951
    33
            switch (event) {
edouard@1152
    34
                case Init: break;
vb@951
    35
                case KeyGen:
vb@959
    36
                    status = sendBeacon(session, state, NULL, NULL);
vb@964
    37
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
    38
                        return (int) invalid_out_of_memory;
vb@959
    39
                    if (status != PEP_STATUS_OK)
vb@1043
    40
                        return (int) invalid_action;
vb@951
    41
                    break;
vb@951
    42
                case CannotDecrypt:
vb@959
    43
                    status = sendBeacon(session, state, NULL, NULL);
vb@964
    44
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
    45
                        return (int) invalid_out_of_memory;
vb@959
    46
                    if (status != PEP_STATUS_OK)
vb@1043
    47
                        return (int) invalid_action;
vb@951
    48
                    break;
vb@951
    49
                case Beacon:
vb@959
    50
                    status = sendHandshakeRequest(session, state, partner, NULL);
vb@964
    51
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
    52
                        return (int) invalid_out_of_memory;
vb@959
    53
                    if (status != PEP_STATUS_OK)
vb@1043
    54
                        return (int) invalid_action;
vb@951
    55
                    break;
vb@951
    56
                case HandshakeRequest:
vb@959
    57
                    status = sendHandshakeRequest(session, state, partner, NULL);
vb@964
    58
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
    59
                        return (int) invalid_out_of_memory;
vb@959
    60
                    if (status != PEP_STATUS_OK)
vb@1043
    61
                        return (int) invalid_action;
vb@951
    62
                    return HandshakingSole;
vb@951
    63
                default:
vb@951
    64
                    return (DeviceState_state) invalid_event;
vb@951
    65
            }
vb@951
    66
            break;
vb@951
    67
        
vb@951
    68
        case HandshakingSole:
vb@951
    69
            switch (event) {
vb@951
    70
                case Init:
vb@959
    71
                    status = showHandshake(session, state, partner, NULL);
vb@964
    72
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
    73
                        return (int) invalid_out_of_memory;
vb@959
    74
                    if (status != PEP_STATUS_OK)
vb@1043
    75
                        return (int) invalid_action;
vb@951
    76
                    break;
vb@951
    77
                case HandshakeRejected:
edouard@1162
    78
                    status = rejectHandshake(session, state, partner, NULL);
vb@964
    79
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
    80
                        return (int) invalid_out_of_memory;
vb@959
    81
                    if (status != PEP_STATUS_OK)
vb@1043
    82
                        return (int) invalid_action;
vb@951
    83
                    return Sole;
vb@951
    84
                case HandshakeAccepted:
edouard@1162
    85
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1162
    86
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1162
    87
                        return (int) invalid_out_of_memory;
edouard@1162
    88
                    if (status != PEP_STATUS_OK)
edouard@1162
    89
                        return (int) invalid_action;
vb@959
    90
                    cond_result = keyElectionWon(session, partner);
vb@964
    91
                    if (cond_result < 0)
vb@964
    92
                        return cond_result;
vb@959
    93
                    if (cond_result) {
edouard@1162
    94
                        status = sendGroupKeys(session, state, partner, NULL);
edouard@1162
    95
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1162
    96
                            return (int) invalid_out_of_memory;
edouard@1162
    97
                        if (status != PEP_STATUS_OK)
edouard@1162
    98
                            return (int) invalid_action;
vb@951
    99
                        return Grouped;
vb@951
   100
                    }
vb@951
   101
                    return WaitForGroupKeys;
vb@951
   102
                default:
vb@951
   103
                    return (DeviceState_state) invalid_event;
vb@951
   104
            }
vb@951
   105
            break;
vb@951
   106
        
vb@951
   107
        case WaitForGroupKeys:
vb@951
   108
            switch (event) {
edouard@1152
   109
                case Init: break;
vb@951
   110
                case GroupKeys:
edouard@1166
   111
                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
vb@964
   112
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
   113
                        return (int) invalid_out_of_memory;
vb@959
   114
                    if (status != PEP_STATUS_OK)
vb@1043
   115
                        return (int) invalid_action;
vb@951
   116
                    return Grouped;
vb@951
   117
                case Cancel:
vb@951
   118
                    return Sole;
vb@951
   119
                case Reject:
edouard@1162
   120
                    status = rejectHandshake(session, state, partner, NULL);
vb@964
   121
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
   122
                        return (int) invalid_out_of_memory;
vb@959
   123
                    if (status != PEP_STATUS_OK)
vb@1043
   124
                        return (int) invalid_action;
vb@951
   125
                    return Sole;
vb@951
   126
                default:
vb@951
   127
                    return (DeviceState_state) invalid_event;
vb@951
   128
            }
vb@951
   129
            break;
vb@951
   130
        
vb@951
   131
        case Grouped:
vb@951
   132
            switch (event) {
edouard@1152
   133
                case Init: break;
vb@951
   134
                case KeyGen:
vb@959
   135
                    status = sendGroupKeys(session, state, NULL, NULL);
vb@964
   136
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
   137
                        return (int) invalid_out_of_memory;
vb@959
   138
                    if (status != PEP_STATUS_OK)
vb@1043
   139
                        return (int) invalid_action;
vb@951
   140
                    break;
vb@951
   141
                case HandshakeRequest:
vb@959
   142
                    status = sendHandshakeRequest(session, state, partner, NULL);
vb@964
   143
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
   144
                        return (int) invalid_out_of_memory;
vb@959
   145
                    if (status != PEP_STATUS_OK)
vb@1043
   146
                        return (int) invalid_action;
vb@959
   147
                    status = showHandshake(session, state, partner, NULL);
vb@964
   148
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
   149
                        return (int) invalid_out_of_memory;
vb@959
   150
                    if (status != PEP_STATUS_OK)
vb@1043
   151
                        return (int) invalid_action;
vb@951
   152
                    break;
vb@951
   153
                case HandshakeRejected:
edouard@1162
   154
                    status = rejectHandshake(session, state, partner, NULL);
vb@964
   155
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
   156
                        return (int) invalid_out_of_memory;
vb@959
   157
                    if (status != PEP_STATUS_OK)
vb@1043
   158
                        return (int) invalid_action;
vb@951
   159
                    break;
edouard@1162
   160
                case HandshakeAccepted:
edouard@1162
   161
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1162
   162
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1162
   163
                        return (int) invalid_out_of_memory;
edouard@1162
   164
                    if (status != PEP_STATUS_OK)
edouard@1162
   165
                        return (int) invalid_action;
edouard@1162
   166
                    status = sendGroupKeys(session, state, partner, NULL);
edouard@1162
   167
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1162
   168
                        return (int) invalid_out_of_memory;
edouard@1162
   169
                    if (status != PEP_STATUS_OK)
edouard@1162
   170
                        return (int) invalid_action;
vb@951
   171
                    break;
vb@951
   172
                case Reject:
edouard@1162
   173
                    status = rejectHandshake(session, state, NULL, NULL);
vb@964
   174
                    if (status == PEP_OUT_OF_MEMORY)
vb@1043
   175
                        return (int) invalid_out_of_memory;
vb@959
   176
                    if (status != PEP_STATUS_OK)
vb@1043
   177
                        return (int) invalid_action;
vb@951
   178
                    break;
vb@951
   179
                default:
vb@951
   180
                    return (DeviceState_state) invalid_event;
vb@951
   181
            }
vb@951
   182
            break;
vb@951
   183
        
vb@616
   184
        default:
vb@743
   185
            return (DeviceState_state) invalid_state;
vb@616
   186
    }
vb@616
   187
vb@616
   188
    return state;
vb@616
   189
}
vb@807
   190