DtlsTransportLayer.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * MIT License
  3. *
  4. * Copyright (c) 2018 Infineon Technologies AG
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE
  23. *
  24. *
  25. * \file DtlsTransportLayer.c
  26. *
  27. * \brief This file provides APIs for the transport layer functionalities.
  28. *
  29. * \addtogroup grOCP
  30. * @{
  31. *
  32. */
  33. #include "optiga/dtls/DtlsTransportLayer.h"
  34. #include "optiga/common/MemoryMgmt.h"
  35. #ifdef MODULE_ENABLE_DTLS_MUTUAL_AUTH
  36. /// @cond hidden
  37. /// @endcond
  38. /**
  39. * This API initialises transport layer communication structure.
  40. *
  41. * \param[in,out] PpsTL Pointer to the transport layer communication structure
  42. *
  43. * \return #OCP_TL_OK on successful execution
  44. * \return #OCP_TL_ERROR on failure
  45. * \return #OCP_TL_NULL_PARAM on parameter received is NULL
  46. * \return #E_COMMS_UDP_ALLOCATE_FAILURE on failure to allocate memory
  47. */
  48. int32_t DtlsTL_Init(sTL_d* PpsTL)
  49. {
  50. int32_t i4Status = (int32_t)OCP_TL_ERROR;
  51. do
  52. {
  53. //NULL check
  54. if((NULL == PpsTL) || (NULL == PpsTL->pzIpAddress))
  55. {
  56. i4Status = (int32_t)OCP_TL_NULL_PARAM;
  57. break;
  58. }
  59. //Allocate the memory for the ethernet communication structure
  60. PpsTL->phTLHdl = (pal_socket_t*)OCP_MALLOC(sizeof(pal_socket_t));
  61. if(NULL == PpsTL->phTLHdl)
  62. {
  63. i4Status = (int32_t)OCP_TL_MALLOC_FAILURE;
  64. break;
  65. }
  66. /// @cond hidden
  67. #define PS_COMMS_HANDLE ((pal_socket_t*)PpsTL->phTLHdl)
  68. /// @endcond
  69. PS_COMMS_HANDLE->wPort = PpsTL->wPort;
  70. //Converting IP address from string format to hex format
  71. i4Status = pal_socket_assign_ip_address(PpsTL->pzIpAddress,&(PS_COMMS_HANDLE->sIPAddress));
  72. if(i4Status != E_COMMS_SUCCESS)
  73. {
  74. break;
  75. }
  76. //Assigning the timeout value
  77. PS_COMMS_HANDLE->wTimeout = PpsTL->wTimeout ;
  78. //Non Blockage receive mode
  79. PS_COMMS_HANDLE->bMode = (uint8_t)PpsTL->eCallType;
  80. //Add logging
  81. LOG_TRANSPORTMSG("Initializing UDP Connection",eInfo);
  82. //Initialize the communication handle with the parameters
  83. i4Status = pal_socket_init(PS_COMMS_HANDLE);
  84. if(E_COMMS_SUCCESS != i4Status)
  85. {
  86. break;
  87. }
  88. i4Status = (int32_t)OCP_TL_OK;
  89. }while(FALSE);
  90. if(OCP_TL_OK != i4Status)
  91. {
  92. if((NULL != PpsTL)&& (NULL != PpsTL->phTLHdl))
  93. {
  94. OCP_FREE(PpsTL->phTLHdl);
  95. PpsTL->phTLHdl = NULL;
  96. }
  97. }
  98. /// @cond hidden
  99. #undef PS_COMMS_HANDLE
  100. /// @endcond
  101. return i4Status;
  102. }
  103. /**
  104. * This API creates client port
  105. *
  106. * \param[in,out] PpsTL Pointer to the transport layer communication structure
  107. *
  108. * \return #OCP_TL_OK on successful execution
  109. * \return #OCP_TL_NULL_PARAM on parameter received is NULL
  110. * \return #E_COMMS_UDP_BINDING_FAILURE on port binding failure
  111. * \return #OCP_TL_ERROR on failure
  112. */
  113. int32_t DtlsTL_Connect(sTL_d* PpsTL)
  114. {
  115. int32_t i4Status = (int32_t)OCP_TL_ERROR;
  116. do
  117. {
  118. //NULL check
  119. if((NULL == PpsTL) || (NULL == PpsTL->phTLHdl))
  120. {
  121. i4Status = (int32_t)OCP_TL_NULL_PARAM;
  122. break;
  123. }
  124. /// @cond hidden
  125. #define PS_COMMS_HANDLE ((pal_socket_t*)PpsTL->phTLHdl)
  126. /// @endcond
  127. //Logging
  128. LOG_TRANSPORTMSG("Connecting to UDP",eInfo);
  129. //Open the client port with the port number initialised
  130. i4Status = pal_socket_connect(PS_COMMS_HANDLE, PS_COMMS_HANDLE->wPort);
  131. if(E_COMMS_SUCCESS != i4Status)
  132. {
  133. LOG_TRANSPORTMSG("Error connecting to UDP",eError);
  134. break;
  135. }
  136. PpsTL->eIsConnected = eConnected;
  137. i4Status = (int32_t)OCP_TL_OK;
  138. }while(FALSE);
  139. /// @cond hidden
  140. #undef PS_COMMS_HANDLE
  141. /// @endcond
  142. return i4Status;
  143. }
  144. /**
  145. * This API transmits the data to the server.
  146. *
  147. * \param[in,out] PpsTL Pointer to the transport layer communication structure
  148. * \param[in] PpbBuffer Pointer to buffer containing data to be transmitted
  149. * \param[in] PdwLen Length of the data to be transmitted
  150. *
  151. * \return #OCP_TL_OK on successful execution
  152. * \return #OCP_TL_NULL_PARAM on parameter received is NULL
  153. * \return #E_COMMS_UDP_NO_DATA_TO_SEND on no date present to send
  154. * \return #E_COMMS_INSUFFICIENT_MEMORY on out of memory failure
  155. * \return #E_COMMS_UDP_ROUTING_FAILURE on failure to route the UDP packet
  156. * \return #E_COMMS_UDP_DEALLOCATION_FAILURE on failure to deallocate
  157. * \return #OCP_TL_ERROR on failure
  158. */
  159. int32_t DtlsTL_Send(const sTL_d* PpsTL,uint8_t* PpbBuffer,uint16_t PdwLen)
  160. {
  161. int32_t i4Status = (int32_t)OCP_TL_ERROR;
  162. do
  163. {
  164. //NULL check
  165. if((NULL == PpsTL) || (NULL == PpsTL->phTLHdl) ||(NULL == PpbBuffer))
  166. {
  167. i4Status = (int32_t)OCP_TL_NULL_PARAM;
  168. break;
  169. }
  170. LOG_TRANSPORTDBARY("Sending Data over UDP", PpbBuffer, PdwLen, eInfo);
  171. //Send the data over IP address and Port initialized
  172. /// @cond hidden
  173. #define PS_COMMS_HANDLE ((pal_socket_t*)PpsTL->phTLHdl)
  174. /// @endcond
  175. i4Status = pal_socket_send(PS_COMMS_HANDLE, PpbBuffer, PdwLen);
  176. if (E_COMMS_SUCCESS != i4Status)
  177. {
  178. LOG_TRANSPORTMSG("Error while sending data",eError);
  179. break;
  180. }
  181. i4Status = (int32_t)OCP_TL_OK;
  182. }while(FALSE);
  183. /// @cond hidden
  184. #undef PS_COMMS_HANDLE
  185. /// @endcond
  186. return i4Status;
  187. }
  188. /**
  189. * This API receives the data from the server
  190. *
  191. * \param[in] PpsTL Pointer to the transport layer communication structure
  192. * \param[in,out] PpbBuffer Pointer to buffer where data is to be received
  193. * \param[in,out] PpdwLen Length of the buffer/Length of the received data
  194. *
  195. * \return #OCP_TL_OK on successful execution
  196. * \return #OCP_TL_NULL_PARAM on parameter received is NULL
  197. * \return #OCP_TL_NO_DATA on no data received from the target
  198. * \return #E_COMMS_INSUFFICIENT_BUF_SIZE on insufficient buffer size
  199. * \return #OCP_TL_ERROR on failure
  200. */
  201. int32_t DtlsTL_Recv(const sTL_d* PpsTL,uint8_t* PpbBuffer,uint16_t* PpdwLen)
  202. {
  203. int32_t i4Status = (int32_t)OCP_TL_ERROR;
  204. uint32_t dwRecvLen;
  205. do
  206. {
  207. //NULL check
  208. if((NULL == PpsTL) || (NULL == PpsTL->phTLHdl) || (NULL == PpbBuffer))
  209. {
  210. i4Status = (int32_t)OCP_TL_NULL_PARAM;
  211. break;
  212. }
  213. //logging
  214. LOG_TRANSPORTMSG("Receiving over UDP",eInfo);
  215. /// @cond hidden
  216. #define PS_COMMS_HANDLE ((pal_socket_t*)PpsTL->phTLHdl)
  217. /// @endcond
  218. PS_COMMS_HANDLE->wTimeout = PpsTL->wTimeout;
  219. dwRecvLen = *PpdwLen;
  220. //Listen the server port and receive the data
  221. i4Status = pal_socket_listen(PS_COMMS_HANDLE, PpbBuffer, &dwRecvLen);
  222. if ((int32_t)E_COMMS_UDP_NO_DATA_RECEIVED == i4Status)
  223. {
  224. i4Status = (int32_t)OCP_TL_NO_DATA;
  225. LOG_TRANSPORTMSG("No data received over UDP",eError);
  226. break;
  227. }
  228. if (E_COMMS_SUCCESS != i4Status)
  229. {
  230. LOG_TRANSPORTMSG("Error while receiving data over UDP",eError);
  231. break;
  232. }
  233. LOG_TRANSPORTMSG("Received Data",eInfo);
  234. LOG_TRANSPORTDBARY("Received Data over UDP", PpbBuffer, dwRecvLen, eInfo);
  235. *PpdwLen = (uint16_t)dwRecvLen;
  236. i4Status = (int32_t)OCP_TL_OK;
  237. }while(FALSE);
  238. /// @cond hidden
  239. #undef PS_COMMS_HANDLE
  240. /// @endcond
  241. return i4Status;
  242. }
  243. /**
  244. * This API closes the UDP communication and releases all the resources
  245. *
  246. * \param[in,out] PpsTL Pointer to the transport layer communication structure
  247. *
  248. * \return None
  249. */
  250. Void DtlsTL_Disconnect(sTL_d* PpsTL)
  251. {
  252. //NULL check
  253. if(NULL != PpsTL)
  254. {
  255. if(NULL != PpsTL->phTLHdl)
  256. {
  257. //logging
  258. LOG_TRANSPORTMSG("Closing UDP Connection",eInfo);
  259. /// @cond hidden
  260. #define PS_COMMS_HANDLE ((pal_socket_t*)PpsTL->phTLHdl)
  261. /// @endcond
  262. //Close the UDP connection
  263. pal_socket_close(PS_COMMS_HANDLE);
  264. //Free the allocated memory for ethernet structure
  265. OCP_FREE(PS_COMMS_HANDLE);
  266. PpsTL->phTLHdl = NULL;
  267. PpsTL->eIsConnected = eDisconnected;
  268. /// @cond hidden
  269. #undef PS_COMMS_HANDLE
  270. /// @endcond
  271. }
  272. }
  273. }
  274. /**
  275. * @}
  276. */
  277. #endif /*MODULE_ENABLE_DTLS_MUTUAL_AUTH*/