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