hal_socket.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * socket_hal.h
  3. *
  4. * Copyright 2013-2021 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. PAL_API UdpSocket
  112. UdpSocket_create(void);
  113. PAL_API bool
  114. UdpSocket_bind(UdpSocket self, const char* address, int port);
  115. PAL_API bool
  116. UdpSocket_sendTo(UdpSocket self, const char* address, int port, uint8_t* msg, int msgSize);
  117. /**
  118. * \brief Receive data from UDP socket (store data and (optionally) the IP address of the sender
  119. *
  120. * \param self UDP socket instance
  121. * \param address (optional) buffer to store the IP address as string
  122. * \param maxAddrSize (optional) size of the provided buffer to store the IP address
  123. * \param msg buffer to store the UDP message data
  124. * \param msgSize the maximum size of the message to receive
  125. *
  126. * \return number of received bytes or -1 in case of an error
  127. */
  128. PAL_API int
  129. UdpSocket_receiveFrom(UdpSocket self, char* address, int maxAddrSize, uint8_t* msg, int msgSize);
  130. PAL_API void
  131. ServerSocket_listen(ServerSocket self);
  132. /**
  133. * \brief accept a new incoming connection (non-blocking)
  134. *
  135. * This function shall accept a new incoming connection. It is non-blocking and has to
  136. * return NULL if no new connection is pending.
  137. *
  138. * Implementation of this function is MANDATORY if server functionality is required.
  139. *
  140. * NOTE: The behaviour of this function changed with version 0.8!
  141. *
  142. * \param self server socket instance
  143. *
  144. * \return handle of the new connection socket or NULL if no new connection is available
  145. */
  146. PAL_API Socket
  147. ServerSocket_accept(ServerSocket self);
  148. /**
  149. * \brief active TCP keep alive for socket and set keep alive parameters
  150. *
  151. * NOTE: implementation is mandatory for IEC 61850 MMS
  152. *
  153. * \param self server socket instance
  154. * \param idleTime time (in s) between last received message and first keep alive message
  155. * \param interval time (in s) between subsequent keep alive messages if no ACK received
  156. * \param count number of not missing keep alive ACKs until socket is considered dead
  157. */
  158. PAL_API void
  159. Socket_activateTcpKeepAlive(Socket self, int idleTime, int interval, int count);
  160. /**
  161. * \brief set the maximum number of pending connections in the queue
  162. *
  163. * Implementation of this function is OPTIONAL.
  164. *
  165. * \param self the server socket instance
  166. * \param backlog the number of pending connections in the queue
  167. *
  168. */
  169. PAL_API void
  170. ServerSocket_setBacklog(ServerSocket self, int backlog);
  171. /**
  172. * \brief destroy a server socket instance
  173. *
  174. * Free all resources allocated by this server socket instance.
  175. *
  176. * Implementation of this function is MANDATORY if server functionality is required.
  177. *
  178. * \param self server socket instance
  179. */
  180. PAL_API void
  181. ServerSocket_destroy(ServerSocket self);
  182. /**
  183. * \brief create a TCP client socket
  184. *
  185. * Implementation of this function is MANDATORY if client functionality is required.
  186. *
  187. * \return a new client socket instance.
  188. */
  189. PAL_API Socket
  190. TcpSocket_create(void);
  191. /**
  192. * \brief set the timeout to establish a new connection
  193. *
  194. * \param self the client socket instance
  195. * \param timeoutInMs the timeout in ms
  196. */
  197. PAL_API void
  198. Socket_setConnectTimeout(Socket self, uint32_t timeoutInMs);
  199. /**
  200. * \brief bind a socket to a particular IP address and port (for TcpSocket)
  201. *
  202. * NOTE: Don't use the socket when this functions returns false!
  203. *
  204. * \param self the client socket instance
  205. * \param srcAddress the local IP address or hostname as C string
  206. * \param srcPort the local TCP port to use. When < 1 the OS will chose the TCP port to use.
  207. *
  208. * \return true in case of success, false otherwise
  209. */
  210. PAL_API bool
  211. Socket_bind(Socket self, const char* srcAddress, int srcPort);
  212. /**
  213. * \brief connect to a server
  214. *
  215. * Connect to a server application identified by the address and port parameter.
  216. *
  217. * The "address" parameter may either be a hostname or an IP address. The IP address
  218. * has to be provided as a C string (e.g. "10.0.2.1").
  219. *
  220. * Implementation of this function is MANDATORY if client functionality is required.
  221. *
  222. * NOTE: return type changed from int to bool with version 0.8
  223. *
  224. * \param self the client socket instance
  225. * \param address the IP address or hostname as C string
  226. * \param port the TCP port of the application to connect to
  227. *
  228. * \return true if the connection was established successfully, false otherwise
  229. */
  230. PAL_API bool
  231. Socket_connect(Socket self, const char* address, int port);
  232. PAL_API bool
  233. Socket_connectAsync(Socket self, const char* address, int port);
  234. PAL_API SocketState
  235. Socket_checkAsyncConnectState(Socket self);
  236. /**
  237. * \brief read from socket to local buffer (non-blocking)
  238. *
  239. * The function shall return immediately if no data is available. In this case
  240. * the function returns 0. If an error happens the function shall return -1.
  241. *
  242. * Implementation of this function is MANDATORY
  243. *
  244. * NOTE: The behaviour of this function changed with version 0.8!
  245. *
  246. * \param self the client, connection or server socket instance
  247. * \param buf the buffer where the read bytes are copied to
  248. * \param size the maximum number of bytes to read (size of the provided buffer)
  249. *
  250. * \return the number of bytes read or -1 if an error occurred
  251. */
  252. PAL_API int
  253. Socket_read(Socket self, uint8_t* buf, int size);
  254. /**
  255. * \brief send a message through the socket
  256. *
  257. * Implementation of this function is MANDATORY
  258. *
  259. * \param self client, connection or server socket instance
  260. *
  261. * \return number of bytes transmitted of -1 in case of an error
  262. */
  263. PAL_API int
  264. Socket_write(Socket self, uint8_t* buf, int size);
  265. PAL_API char*
  266. Socket_getLocalAddress(Socket self);
  267. /**
  268. * \brief Get the address of the peer application (IP address and port number)
  269. *
  270. * The peer address has to be returned as
  271. *
  272. * Implementation of this function is MANDATORY (libiec61850)
  273. *
  274. * \param self the client, connection or server socket instance
  275. *
  276. * \return the IP address and port number as strings separated by the ':' character.
  277. */
  278. PAL_API char*
  279. Socket_getPeerAddress(Socket self);
  280. /**
  281. * \brief Get the address of the peer application (IP address and port number)
  282. *
  283. * The peer address has to be returned as
  284. *
  285. * Implementation of this function is MANDATORY (lib60870)
  286. *
  287. * \param self the client, connection or server socket instance
  288. * \param peerAddressString a string to store the peer address (the string should have space
  289. * for at least 60 characters)
  290. *
  291. * \return the IP address and port number as strings separated by the ':' character. If the
  292. * address is an IPv6 address the IP part is encapsulated in square brackets.
  293. */
  294. PAL_API char*
  295. Socket_getPeerAddressStatic(Socket self, char* peerAddressString);
  296. /**
  297. * \brief destroy a socket (close the socket if a connection is established)
  298. *
  299. * This function shall close the connection (if one is established) and free all
  300. * resources allocated by the socket.
  301. *
  302. * Implementation of this function is MANDATORY
  303. *
  304. * \param self the client, connection or server socket instance
  305. */
  306. PAL_API void
  307. Socket_destroy(Socket self);
  308. /*! @} */
  309. /*! @} */
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. #endif /* SOCKET_HAL_H_ */