hal_socket.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * socket_hal.h
  3. *
  4. * Copyright 2013-2022 Michael Zillgith
  5. *
  6. * This file is part of Platform Abstraction Layer (libpal)
  7. * for libiec61850, libmms, and lib60870.
  8. */
  9. #ifndef SOCKET_HAL_H_
  10. #define SOCKET_HAL_H_
  11. #include "hal_base.h"
  12. /**
  13. * \file hal_socket.h
  14. * \brief Abstraction layer TCP/IP sockets
  15. * Has to be implemented for CS 104 TCP/IP.
  16. */
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /*! \defgroup hal Platform (Hardware/OS) abstraction layer
  21. *
  22. * Platform abstraction layer. These functions have to be implemented when the library is
  23. * to be ported to new platforms. It might not be required to implement all interfaces
  24. * depending on the required library features.
  25. *
  26. * @{
  27. */
  28. /**
  29. * @defgroup HAL_SOCKET Interface to the TCP/IP stack (abstract socket layer)
  30. *
  31. * Thread and Socket abstraction layer. This functions have to be implemented to
  32. * port lib60870 to a new hardware/OS platform when TCP/IP is required.
  33. *
  34. * @{
  35. */
  36. /** Opaque reference for a server socket instance */
  37. typedef struct sServerSocket* ServerSocket;
  38. typedef struct sUdpSocket* UdpSocket;
  39. /** Opaque reference for a client or connection socket instance */
  40. typedef struct sSocket* Socket;
  41. /** Opaque reference for a set of server and socket handles */
  42. typedef struct sHandleSet* HandleSet;
  43. /** State of an asynchronous connect */
  44. typedef enum
  45. {
  46. SOCKET_STATE_CONNECTING = 0,
  47. SOCKET_STATE_FAILED = 1,
  48. SOCKET_STATE_CONNECTED = 2
  49. } SocketState;
  50. /**
  51. * \brief Create a new connection handle set (HandleSet)
  52. *
  53. * \return new HandleSet instance
  54. */
  55. PAL_API HandleSet
  56. Handleset_new(void);
  57. /**
  58. * \brief Reset the handle set for reuse
  59. */
  60. PAL_API void
  61. Handleset_reset(HandleSet self);
  62. /**
  63. * \brief add a socket to an existing handle set
  64. *
  65. * \param self the HandleSet instance
  66. * \param sock the socket to add
  67. */
  68. PAL_API void
  69. Handleset_addSocket(HandleSet self, const Socket sock);
  70. /**
  71. * \brief remove a socket from an existing handle set
  72. */
  73. void
  74. Handleset_removeSocket(HandleSet self, const Socket sock);
  75. /**
  76. * \brief wait for a socket to become ready
  77. *
  78. * This function is corresponding to the BSD socket select function.
  79. * It returns the number of sockets on which data is pending or 0 if no data is pending
  80. * on any of the monitored connections. The function will return after "timeout" ms if no
  81. * data is pending.
  82. * The function shall return -1 if a socket error occures.
  83. *
  84. * \param self the HandleSet instance
  85. * \param timeout in milliseconds (ms)
  86. * \return It returns the number of sockets on which data is pending
  87. * or 0 if no data is pending on any of the monitored connections.
  88. * The function shall return -1 if a socket error occures.
  89. */
  90. PAL_API int
  91. Handleset_waitReady(HandleSet self, unsigned int timeoutMs);
  92. /**
  93. * \brief destroy the HandleSet instance
  94. *
  95. * \param self the HandleSet instance to destroy
  96. */
  97. PAL_API void
  98. Handleset_destroy(HandleSet self);
  99. /**
  100. * \brief Create a new TcpServerSocket instance
  101. *
  102. * Implementation of this function is MANDATORY if server functionality is required.
  103. *
  104. * \param address ip address or hostname to listen on
  105. * \param port the TCP port to listen on
  106. *
  107. * \return the newly create TcpServerSocket instance
  108. */
  109. PAL_API ServerSocket
  110. TcpServerSocket_create(const char* address, int port);
  111. /**
  112. * \brief Create an IPv4 UDP socket instance
  113. *
  114. * \return new UDP socket instance
  115. */
  116. PAL_API UdpSocket
  117. UdpSocket_create(void);
  118. /**
  119. * \brief Create an IPv6 UDP socket instance
  120. *
  121. * \return new UDP socket instance
  122. */
  123. PAL_API UdpSocket
  124. UdpSocket_createIpV6(void);
  125. /**
  126. * \brief Add the socket to an IPv4 or IPv6 multicast group
  127. *
  128. * \param self UDP socket instance
  129. * \param multicastAddress IPv4 or IPv6 multicast address
  130. *
  131. * \return true on success, false otherwise
  132. */
  133. PAL_API bool
  134. UdpSocket_addGroupMembership(UdpSocket self, const char* multicastAddress);
  135. /**
  136. * \brief Sets the multicast TTL (number of hops) for this UDP socket
  137. *
  138. * \param self UDP socket instance
  139. * \param ttl number of hops for multicast messages. Default is 1 (not routable!)
  140. *
  141. * \return true on success, false otherwise
  142. */
  143. PAL_API bool
  144. UdpSocket_setMulticastTtl(UdpSocket self, int ttl);
  145. PAL_API bool
  146. UdpSocket_bind(UdpSocket self, const char* address, int port);
  147. PAL_API bool
  148. UdpSocket_sendTo(UdpSocket self, const char* address, int port, uint8_t* msg, int msgSize);
  149. /**
  150. * \brief Receive data from UDP socket (store data and (optionally) the IP address of the sender
  151. *
  152. * \param self UDP socket instance
  153. * \param address (optional) buffer to store the IP address as string
  154. * \param maxAddrSize (optional) size of the provided buffer to store the IP address
  155. * \param msg buffer to store the UDP message data
  156. * \param msgSize the maximum size of the message to receive
  157. *
  158. * \return number of received bytes or -1 in case of an error
  159. */
  160. PAL_API int
  161. UdpSocket_receiveFrom(UdpSocket self, char* address, int maxAddrSize, uint8_t* msg, int msgSize);
  162. PAL_API void
  163. ServerSocket_listen(ServerSocket self);
  164. /**
  165. * \brief accept a new incoming connection (non-blocking)
  166. *
  167. * This function shall accept a new incoming connection. It is non-blocking and has to
  168. * return NULL if no new connection is pending.
  169. *
  170. * Implementation of this function is MANDATORY if server functionality is required.
  171. *
  172. * NOTE: The behaviour of this function changed with version 0.8!
  173. *
  174. * \param self server socket instance
  175. *
  176. * \return handle of the new connection socket or NULL if no new connection is available
  177. */
  178. PAL_API Socket
  179. ServerSocket_accept(ServerSocket self);
  180. /**
  181. * \brief active TCP keep alive for socket and set keep alive parameters
  182. *
  183. * NOTE: implementation is mandatory for IEC 61850 MMS
  184. *
  185. * \param self server socket instance
  186. * \param idleTime time (in s) between last received message and first keep alive message
  187. * \param interval time (in s) between subsequent keep alive messages if no ACK received
  188. * \param count number of not missing keep alive ACKs until socket is considered dead
  189. */
  190. PAL_API void
  191. Socket_activateTcpKeepAlive(Socket self, int idleTime, int interval, int count);
  192. /**
  193. * \brief set the maximum number of pending connections in the queue
  194. *
  195. * Implementation of this function is OPTIONAL.
  196. *
  197. * \param self the server socket instance
  198. * \param backlog the number of pending connections in the queue
  199. *
  200. */
  201. PAL_API void
  202. ServerSocket_setBacklog(ServerSocket self, int backlog);
  203. /**
  204. * \brief destroy a server socket instance
  205. *
  206. * Free all resources allocated by this server socket instance.
  207. *
  208. * Implementation of this function is MANDATORY if server functionality is required.
  209. *
  210. * \param self server socket instance
  211. */
  212. PAL_API void
  213. ServerSocket_destroy(ServerSocket self);
  214. /**
  215. * \brief create a TCP client socket
  216. *
  217. * Implementation of this function is MANDATORY if client functionality is required.
  218. *
  219. * \return a new client socket instance.
  220. */
  221. PAL_API Socket
  222. TcpSocket_create(void);
  223. /**
  224. * \brief set the timeout to establish a new connection
  225. *
  226. * \param self the client socket instance
  227. * \param timeoutInMs the timeout in ms
  228. */
  229. PAL_API void
  230. Socket_setConnectTimeout(Socket self, uint32_t timeoutInMs);
  231. /**
  232. * \brief bind a socket to a particular IP address and port (for TcpSocket)
  233. *
  234. * NOTE: Don't use the socket when this functions returns false!
  235. *
  236. * \param self the client socket instance
  237. * \param srcAddress the local IP address or hostname as C string
  238. * \param srcPort the local TCP port to use. When < 1 the OS will chose the TCP port to use.
  239. *
  240. * \return true in case of success, false otherwise
  241. */
  242. PAL_API bool
  243. Socket_bind(Socket self, const char* srcAddress, int srcPort);
  244. /**
  245. * \brief connect to a server
  246. *
  247. * Connect to a server application identified by the address and port parameter.
  248. *
  249. * The "address" parameter may either be a hostname or an IP address. The IP address
  250. * has to be provided as a C string (e.g. "10.0.2.1").
  251. *
  252. * Implementation of this function is MANDATORY if client functionality is required.
  253. *
  254. * NOTE: return type changed from int to bool with version 0.8
  255. *
  256. * \param self the client socket instance
  257. * \param address the IP address or hostname as C string
  258. * \param port the TCP port of the application to connect to
  259. *
  260. * \return true if the connection was established successfully, false otherwise
  261. */
  262. PAL_API bool
  263. Socket_connect(Socket self, const char* address, int port);
  264. PAL_API bool
  265. Socket_connectAsync(Socket self, const char* address, int port);
  266. PAL_API SocketState
  267. Socket_checkAsyncConnectState(Socket self);
  268. /**
  269. * \brief read from socket to local buffer (non-blocking)
  270. *
  271. * The function shall return immediately if no data is available. In this case
  272. * the function returns 0. If an error happens the function shall return -1.
  273. *
  274. * Implementation of this function is MANDATORY
  275. *
  276. * NOTE: The behaviour of this function changed with version 0.8!
  277. *
  278. * \param self the client, connection or server socket instance
  279. * \param buf the buffer where the read bytes are copied to
  280. * \param size the maximum number of bytes to read (size of the provided buffer)
  281. *
  282. * \return the number of bytes read or -1 if an error occurred
  283. */
  284. PAL_API int
  285. Socket_read(Socket self, uint8_t* buf, int size);
  286. /**
  287. * \brief send a message through the socket
  288. *
  289. * Implementation of this function is MANDATORY
  290. *
  291. * \param self client, connection or server socket instance
  292. *
  293. * \return number of bytes transmitted of -1 in case of an error
  294. */
  295. PAL_API int
  296. Socket_write(Socket self, uint8_t* buf, int size);
  297. PAL_API char*
  298. Socket_getLocalAddress(Socket self);
  299. /**
  300. * \brief Get the address of the peer application (IP address and port number)
  301. *
  302. * The peer address has to be returned as null terminated string
  303. *
  304. * Implementation of this function is MANDATORY (libiec61850)
  305. *
  306. * \param self the client, connection or server socket instance
  307. *
  308. * \return the IP address and port number as strings separated by the ':' character.
  309. */
  310. PAL_API char*
  311. Socket_getPeerAddress(Socket self);
  312. /**
  313. * \brief Get the address of the peer application (IP address and port number)
  314. *
  315. * The peer address has to be returned as null terminated string
  316. *
  317. * Implementation of this function is MANDATORY (lib60870 and libiec61850)
  318. *
  319. * \param self the client, connection or server socket instance
  320. * \param peerAddressString a string to store the peer address (the string should have space
  321. * for at least 60 characters)
  322. *
  323. * \return the IP address and port number as strings separated by the ':' character. If the
  324. * address is an IPv6 address the IP part is encapsulated in square brackets.
  325. */
  326. PAL_API char*
  327. Socket_getPeerAddressStatic(Socket self, char* peerAddressString);
  328. /**
  329. * \brief destroy a socket (close the socket if a connection is established)
  330. *
  331. * This function shall close the connection (if one is established) and free all
  332. * resources allocated by the socket.
  333. *
  334. * Implementation of this function is MANDATORY
  335. *
  336. * \param self the client, connection or server socket instance
  337. */
  338. PAL_API void
  339. Socket_destroy(Socket self);
  340. /*! @} */
  341. /*! @} */
  342. #ifdef __cplusplus
  343. }
  344. #endif
  345. #endif /* SOCKET_HAL_H_ */