hal_socket.h 10 KB

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