r_session.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * r_session.h
  3. *
  4. * Copyright 2013-2022 Michael Zillgith
  5. *
  6. * This file is part of libIEC61850.
  7. *
  8. * libIEC61850 is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * libIEC61850 is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with libIEC61850. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * See COPYING file for the complete license text.
  22. */
  23. #ifndef LIBIEC61850_R_SESSION_H_
  24. #define LIBIEC61850_R_SESSION_H_
  25. #include "libiec61850_common_api.h"
  26. #include "hal_socket.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. typedef struct sRSession* RSession;
  31. typedef enum {
  32. R_SESSION_SEC_ALGO_NONE = 0,
  33. R_SESSION_SEC_ALGO_AES_128_GCM = 1,
  34. R_SESSION_SEC_ALGO_AES_256_GCM = 2
  35. } RSecurityAlgorithm;
  36. typedef enum {
  37. R_SESSION_SIG_ALGO_NONE = 0,
  38. R_SESSION_SIG_ALGO_HMAC_SHA256_80 = 1,
  39. R_SESSION_SIG_ALGO_HMAC_SHA256_128 = 2,
  40. R_SESSION_SIG_ALGO_HMAC_SHA256_256 = 3,
  41. R_SESSION_SIG_ALGO_AES_GMAC_64 = 4,
  42. R_SESSION_SIG_ALGO_AES_GMAC_128 = 5,
  43. R_SESSION_SIG_ALGO_HMAC_SHA3_80 = 6,
  44. R_SESSION_SIG_ALGO_HMAC_SHA3_128 = 7,
  45. R_SESSION_SIG_ALGO_HMAC_SHA3_256 = 8
  46. } RSignatureAlgorithm;
  47. typedef enum {
  48. R_SESSION_ERROR_OK = 0,
  49. R_SESSION_ERROR_INVALID_KEY = 1,
  50. R_SESSION_ERROR_KEY_QUEUE_FULL = 2,
  51. R_SESSION_ERROR_NO_SOCKET = 3,
  52. R_SESSION_ERROR_OUT_OF_MEMORY = 4,
  53. R_SESSION_ERROR_FAILED_TO_SEND = 5,
  54. R_SESSION_ERROR_FAILED_TO_RECEIVE = 6,
  55. R_SESSION_ERROR_INVALID_MESSAGE = 7,
  56. R_SESSION_ERROR_SET_FAILED = 8
  57. } RSessionError;
  58. typedef struct sRSessionPayloadElement* RSessionPayloadElement;
  59. struct sRSessionPayloadElement
  60. {
  61. bool simulation;
  62. uint16_t appId;
  63. uint8_t payloadType;
  64. uint8_t* payload;
  65. int payloadSize;
  66. RSessionPayloadElement nextElement; /* NULL when no more elements follow */
  67. };
  68. /**
  69. * \brief Create a new RSession instance to provide R-GOOSE/R-SMV support for GOOSE/SMV publisher/subscriber
  70. *
  71. * \return new RSession instance
  72. */
  73. LIB61850_API RSession
  74. RSession_create(void);
  75. /**
  76. * \brief Set the maximum buffer size for session messages (range: 128 - 65535)
  77. *
  78. * \param self the RSession instance
  79. * \param bufferSize the size of the buffer for RSession UDP messages (range: 128 - 65535)
  80. */
  81. LIB61850_API void
  82. RSession_setBufferSize(RSession self, uint16_t bufferSize);
  83. /**
  84. * \brief Set the security algorithms for the session instance
  85. *
  86. * \note only for version 1 of the protocol!
  87. *
  88. * \param secAlgo encryption algorithm to be used for the session instance
  89. * \param sigAlgo signature algorithm to be used for the session instance
  90. *
  91. * \return returns R_SESSION_ERROR_OK
  92. */
  93. LIB61850_API RSessionError
  94. RSession_setSecurity(RSession self, RSecurityAlgorithm secAlgo, RSignatureAlgorithm sigAlgo);
  95. /**
  96. * \brief Bind the RSession instance to a specific local IP address and UDP port
  97. *
  98. * \param self the RSession instance
  99. * \param localAddress the local IP address to use
  100. * \param localPort the local UDP port to use (default is 102)
  101. */
  102. LIB61850_API RSessionError
  103. RSession_setLocalAddress(RSession self, const char* localAddress, int localPort);
  104. /**
  105. * \brief Add this instance to an IPv4/IPv6 multicast group
  106. *
  107. * \param self the RSession instance
  108. * \param multicastAddress IPv4 or IPv6 multicast address
  109. *
  110. * \return R_SESSION_ERROR_OK on success, R_SESSION_ERROR_SET_FAILED otherwise
  111. */
  112. LIB61850_API RSessionError
  113. RSession_addMulticastGroup(RSession self, const char* multicastAddress);
  114. /**
  115. * \brief Sets the multicast TTL (number of hops) for this RSession instance
  116. *
  117. * \param self the RSession instance
  118. * \param ttl number of hops for multicast messages. Default is 1 (not routable!)
  119. *
  120. * \return R_SESSION_ERROR_OK on success, error code otherwise
  121. */
  122. LIB61850_API RSessionError
  123. RSession_setMulticastTtl(RSession self, int ttl);
  124. /**
  125. * \brief Set the destionation address and port for publishers
  126. *
  127. * \param self the RSession instance
  128. * \param remoteAddress remote IP address
  129. * \param remotePort remote UDP port
  130. *
  131. * \return R_SESSION_ERROR_OK on success, error code otherwise
  132. */
  133. LIB61850_API RSessionError
  134. RSession_setRemoteAddress(RSession self, const char* remoteAddress, int remotePort);
  135. /**
  136. * \brief Start sending and receiving messages (bind to a local UDP port/interface)
  137. *
  138. * \param self the RSession instance
  139. *
  140. * \return R_SESSION_ERROR_OK on success, error code otherwise
  141. */
  142. LIB61850_API RSessionError
  143. RSession_start(RSession self);
  144. /**
  145. * \brief Stop sending and receiving messages.
  146. *
  147. * \param self the RSession instance
  148. *
  149. * \return R_SESSION_ERROR_OK on success, error code otherwise
  150. */
  151. LIB61850_API RSessionError
  152. RSession_stop(RSession self);
  153. /**
  154. * \brief Manually add a key to the RSession instance
  155. *
  156. * \param self the RSession instance
  157. * \param keyId the key ID is unique for the security association
  158. * \param key the key data
  159. * \param keyLength the length of the key in bytes
  160. * \param secAlgo the applicable security (encryption) algorithm
  161. * \param sigAlgo the applicable signature algorithm
  162. */
  163. LIB61850_API RSessionError
  164. RSession_addKey(RSession self, uint32_t keyId, uint8_t* key, int keyLength, RSecurityAlgorithm secAlgo, RSignatureAlgorithm sigAlgo);
  165. /**
  166. * \brief Remove key from the list of accepted keys
  167. *
  168. * \param self the RSession instance
  169. * \param keyId the key ID is unique for the security association
  170. */
  171. LIB61850_API RSessionError
  172. RSession_removeKey(RSession self, uint32_t keyId);
  173. /**
  174. * \brief Remove all keys from the list of accepted keys
  175. *
  176. * \param self the RSession instance
  177. */
  178. void
  179. RSession_removeAllKeys(RSession self);
  180. typedef enum
  181. {
  182. RSESSION_KEY_EVENT__NEED_KEY = 1
  183. } RSessionKeyEvent;
  184. typedef void (*RSession_KeyEventHandler) (void* parameter, RSession rSession, RSessionKeyEvent event, uint32_t keyID);
  185. /**
  186. * \brief Set a callback handler to receive key events from the RSession instance
  187. *
  188. * e.g. when the RSession instance has no valid key for the received messages or to publish messages.
  189. *
  190. * \param self the RSession instance
  191. * \param handler the callback that is called when a new event happens
  192. * \param parameter user provided parameter that is passed to the user callback
  193. */
  194. LIB61850_API void
  195. RSession_setKeyEventHandler(RSession self, RSession_KeyEventHandler handler, void* parameter);
  196. /**
  197. * \brief Set the active key for the sender/publisher
  198. *
  199. * \param self the RSession instance
  200. * \param keyId the key ID of the new active key (has to be added with \ref RSession_addKey before).
  201. *
  202. * \return R_SESSION_ERROR_INVALID_KEY when no valid key with the given keyId is avialable, R_SESSION_ERROR_OK otherwise
  203. */
  204. LIB61850_API RSessionError
  205. RSession_setActiveKey(RSession self, uint32_t keyId);
  206. /**
  207. * \brief Destroy the RSession instance and free all resource
  208. *
  209. * \param self the RSession instance
  210. */
  211. LIB61850_API void
  212. RSession_destroy(RSession self);
  213. #ifdef __cplusplus
  214. }
  215. #endif
  216. #endif /* LIBIEC61850_R_SESSION_H_ */