/src/S2OPC/src/ClientServer/secure_channels/sopc_secure_channels_api.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Licensed to Systerel under one or more contributor license |
3 | | * agreements. See the NOTICE file distributed with this work |
4 | | * for additional information regarding copyright ownership. |
5 | | * Systerel licenses this file to you under the Apache |
6 | | * License, Version 2.0 (the "License"); you may not use this |
7 | | * file except in compliance with the License. You may obtain |
8 | | * a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, |
13 | | * software distributed under the License is distributed on an |
14 | | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | | * KIND, either express or implied. See the License for the |
16 | | * specific language governing permissions and limitations |
17 | | * under the License. |
18 | | */ |
19 | | |
20 | | #include "sopc_secure_channels_api.h" |
21 | | #include "sopc_secure_channels_api_internal.h" |
22 | | |
23 | | #include <stdbool.h> |
24 | | |
25 | | #include "sopc_assert.h" |
26 | | #include "sopc_atomic.h" |
27 | | #include "sopc_chunks_mgr.h" |
28 | | #include "sopc_macros.h" |
29 | | #include "sopc_secure_channels_internal_ctx.h" |
30 | | #include "sopc_secure_connection_state_mgr.h" |
31 | | #include "sopc_secure_listener_state_mgr.h" |
32 | | #include "sopc_sockets_api.h" |
33 | | |
34 | | void SOPC_SecureChannels_OnInternalEvent(SOPC_EventHandler* handler, |
35 | | int32_t event, |
36 | | uint32_t eltId, |
37 | | uintptr_t params, |
38 | | uintptr_t auxParam) |
39 | 0 | { |
40 | 0 | SOPC_UNUSED_ARG(handler); |
41 | 0 | SOPC_SecureChannels_InternalEvent internalEvent = (SOPC_SecureChannels_InternalEvent) event; |
42 | |
|
43 | 0 | switch (internalEvent) |
44 | 0 | { |
45 | | /* SC listener manager -> SC connection state manager */ |
46 | 0 | case INT_EP_SC_CREATE: |
47 | 0 | case INT_EP_SC_CLOSE: |
48 | 0 | case INT_EP_SC_REVERSE_CONNECT: |
49 | 0 | case INT_SC_RCV_RHE_TRANSITION: |
50 | 0 | SOPC_SecureConnectionStateMgr_OnInternalEvent(internalEvent, eltId, params, auxParam); |
51 | 0 | break; |
52 | | |
53 | | /* SC connection manager -> SC listener state manager */ |
54 | 0 | case INT_EP_SC_CREATED: |
55 | 0 | case INT_EP_SC_RHE_DECODED: |
56 | 0 | case INT_EP_SC_DISCONNECTED: |
57 | 0 | case INT_REVERSE_EP_REQ_CONNECTION: |
58 | 0 | SOPC_SecureListenerStateMgr_OnInternalEvent(internalEvent, eltId, params, auxParam); |
59 | 0 | break; |
60 | | |
61 | | /* OPC UA chunks message manager -> SC connection manager */ |
62 | 0 | case INT_SC_RCV_HEL: |
63 | 0 | case INT_SC_RCV_ACK: |
64 | 0 | case INT_SC_RCV_ERR: |
65 | 0 | case INT_SC_RCV_RHE: |
66 | 0 | case INT_SC_RCV_OPN: |
67 | 0 | case INT_SC_RCV_CLO: |
68 | 0 | case INT_SC_RCV_MSG_CHUNKS: |
69 | 0 | case INT_SC_RCV_MSG_CHUNK_ABORT: |
70 | 0 | case INT_SC_RCV_FAILURE: |
71 | 0 | case INT_SC_SND_FATAL_FAILURE: |
72 | 0 | case INT_SC_SENT_ABORT_FAILURE: |
73 | 0 | case INT_SC_CLOSE: |
74 | 0 | SOPC_SecureConnectionStateMgr_OnInternalEvent(internalEvent, eltId, params, auxParam); |
75 | 0 | break; |
76 | | |
77 | | /* SC connection manager -> OPC UA chunks message manager */ |
78 | 0 | case INT_SC_SND_HEL: |
79 | 0 | case INT_SC_SND_ACK: |
80 | 0 | case INT_SC_SND_ERR: |
81 | 0 | case INT_SC_SND_RHE: |
82 | 0 | case INT_SC_SND_OPN: |
83 | 0 | case INT_SC_SND_CLO: |
84 | 0 | case INT_SC_SND_MSG_CHUNKS: |
85 | 0 | SOPC_ChunksMgr_Dispatcher(internalEvent, eltId, params, auxParam); |
86 | 0 | break; |
87 | 0 | default: |
88 | 0 | SOPC_ASSERT(false && "Unknown internal event."); |
89 | 0 | break; |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | void SOPC_SecureChannels_OnSocketsEvent(SOPC_EventHandler* handler, |
94 | | int32_t event, |
95 | | uint32_t eltId, |
96 | | uintptr_t params, |
97 | | uintptr_t auxParam) |
98 | 0 | { |
99 | 0 | SOPC_UNUSED_ARG(handler); |
100 | 0 | SOPC_Sockets_OutputEvent sockEvent = (SOPC_Sockets_OutputEvent) event; |
101 | |
|
102 | 0 | switch (sockEvent) |
103 | 0 | { |
104 | | /* Sockets events: */ |
105 | | /* Sockets manager -> SC listener state manager */ |
106 | 0 | case SOCKET_LISTENER_OPENED: |
107 | 0 | case SOCKET_LISTENER_CONNECTION: |
108 | 0 | case SOCKET_LISTENER_FAILURE: |
109 | 0 | SOPC_SecureListenerStateMgr_OnSocketEvent(sockEvent, eltId, params, auxParam); |
110 | 0 | break; |
111 | | /* Sockets manager -> SC connection state manager */ |
112 | 0 | case SOCKET_CREATED: |
113 | 0 | case SOCKET_CONNECTION: |
114 | 0 | case SOCKET_FAILURE: |
115 | 0 | SOPC_SecureConnectionStateMgr_OnSocketEvent(sockEvent, eltId, params, auxParam); |
116 | 0 | break; |
117 | | /* Sockets manager -> Chunks manager */ |
118 | 0 | case SOCKET_RCV_BYTES: |
119 | 0 | SOPC_ChunksMgr_OnSocketEvent(sockEvent, eltId, params, auxParam); |
120 | 0 | break; |
121 | 0 | default: |
122 | 0 | SOPC_ASSERT(false && "Unknown socket event."); |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | | void SOPC_SecureChannels_OnTimerEvent(SOPC_EventHandler* handler, |
127 | | int32_t event, |
128 | | uint32_t id, |
129 | | uintptr_t params, |
130 | | uintptr_t auxParam) |
131 | 0 | { |
132 | 0 | SOPC_UNUSED_ARG(handler); |
133 | |
|
134 | 0 | SOPC_SecureChannels_TimerEvent timerEvent = (SOPC_SecureChannels_TimerEvent) event; |
135 | 0 | switch (timerEvent) |
136 | 0 | { |
137 | | /* Secure connection state manager */ |
138 | 0 | case TIMER_SC_CONNECTION_TIMEOUT: |
139 | 0 | case TIMER_SC_SERVER_REVERSE_CONN_RETRY: |
140 | 0 | case TIMER_SC_CLIENT_OPN_RENEW: |
141 | 0 | case TIMER_SC_REQUEST_TIMEOUT: |
142 | 0 | SOPC_SecureConnectionStateMgr_OnTimerEvent(timerEvent, id, params, auxParam); |
143 | 0 | break; |
144 | | /* Secure listener state manager */ |
145 | 0 | case TIMER_SC_RHE_RECEPTION_TIMEOUT: |
146 | 0 | SOPC_SecureListenerStateMgr_OnTimerEvent(timerEvent, id, params, auxParam); |
147 | 0 | break; |
148 | 0 | default: |
149 | 0 | SOPC_ASSERT(false && "Unknown timer event."); |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | | void SOPC_SecureChannels_OnInputEvent(SOPC_EventHandler* handler, |
154 | | int32_t event, |
155 | | uint32_t eltId, |
156 | | uintptr_t params, |
157 | | uintptr_t auxParam) |
158 | 0 | { |
159 | 0 | SOPC_UNUSED_ARG(handler); |
160 | |
|
161 | 0 | SOPC_SecureChannels_InputEvent scEvent = (SOPC_SecureChannels_InputEvent) event; |
162 | 0 | switch (scEvent) |
163 | 0 | { |
164 | | /* Services events: */ |
165 | | /* Services manager -> SC listener state manager */ |
166 | 0 | case EP_OPEN: |
167 | 0 | case EP_CLOSE: |
168 | 0 | case REVERSE_EP_OPEN: |
169 | 0 | case REVERSE_EP_CLOSE: |
170 | 0 | SOPC_SecureListenerStateMgr_Dispatcher(scEvent, eltId, params, auxParam); |
171 | 0 | break; |
172 | | /* Services manager -> SC connection state manager */ |
173 | 0 | case SC_CONNECT: |
174 | 0 | case SC_REVERSE_CONNECT: |
175 | 0 | case SC_DISCONNECT: |
176 | 0 | case SC_SERVICE_SND_MSG: |
177 | 0 | case SC_SERVICE_SND_ERR: |
178 | 0 | case SC_DISCONNECTED_ACK: |
179 | 0 | case SCS_REEVALUATE_SCS: |
180 | 0 | SOPC_SecureConnectionStateMgr_Dispatcher(scEvent, eltId, params, auxParam); |
181 | 0 | break; |
182 | 0 | default: |
183 | 0 | SOPC_ASSERT(false && "Unknown input event."); |
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | SOPC_ReturnStatus SOPC_SecureChannels_EnqueueEvent(SOPC_SecureChannels_InputEvent scEvent, |
188 | | uint32_t id, |
189 | | uintptr_t params, |
190 | | uintptr_t auxParam) |
191 | 0 | { |
192 | 0 | SOPC_ASSERT(secureChannelsInputEventHandler != NULL); |
193 | 0 | SOPC_ReturnStatus status = SOPC_STATUS_NOK; |
194 | |
|
195 | 0 | switch (scEvent) |
196 | 0 | { |
197 | | /* External events */ |
198 | 0 | case EP_OPEN: |
199 | 0 | case EP_CLOSE: |
200 | 0 | case REVERSE_EP_OPEN: |
201 | 0 | case REVERSE_EP_CLOSE: |
202 | 0 | case SC_CONNECT: |
203 | 0 | case SC_REVERSE_CONNECT: |
204 | 0 | case SC_DISCONNECT: |
205 | 0 | case SC_SERVICE_SND_MSG: |
206 | 0 | case SC_SERVICE_SND_ERR: |
207 | 0 | case SC_DISCONNECTED_ACK: |
208 | 0 | case SCS_REEVALUATE_SCS: |
209 | 0 | status = SOPC_EventHandler_Post(secureChannelsInputEventHandler, (int32_t) scEvent, id, params, auxParam); |
210 | 0 | break; |
211 | 0 | default: |
212 | 0 | SOPC_ASSERT(false && "Unknown event."); |
213 | 0 | } |
214 | 0 | return status; |
215 | 0 | } |
216 | | |
217 | | void SOPC_SecureChannels_EnqueueInternalEventAsNext(SOPC_SecureChannels_InternalEvent event, |
218 | | uint32_t id, |
219 | | uintptr_t params, |
220 | | uintptr_t auxParam) |
221 | 0 | { |
222 | 0 | SOPC_ASSERT(secureChannelsInternalEventHandler != NULL); |
223 | 0 | SOPC_EventHandler_PostAsNext(secureChannelsInternalEventHandler, (int32_t) event, id, params, auxParam); |
224 | 0 | } |
225 | | |
226 | | void SOPC_SecureChannels_Initialize(SOPC_SetListenerFunc* setSocketsListener) |
227 | 0 | { |
228 | 0 | SOPC_SecureChannelsInternalContext_Initialize(setSocketsListener); |
229 | 0 | } |
230 | | |
231 | | void SOPC_SecureChannels_SetEventHandler(SOPC_EventHandler* handler) |
232 | 0 | { |
233 | 0 | SOPC_Atomic_Ptr_Set((void**) &secureChannelsEventHandler, handler); |
234 | 0 | } |
235 | | |
236 | | void SOPC_SecureChannels_Clear(void) |
237 | 0 | { |
238 | 0 | SOPC_SecureChannelsInternalContext_Clear(); |
239 | 0 | } |