PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/common/socket.h

https://github.com/linkdd/fwallsh
C Header | 223 lines | 39 code | 21 blank | 163 comment | 0 complexity | 052058c026e0323ea393e26efaab12b4 MD5 | raw file
  1. #ifndef __SOCKET_H
  2. #define __SOCKET_H
  3. #include <fwallsh/common/types.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <sys/un.h>
  7. #include <unistd.h>
  8. /*!
  9. * \brief UNIX Socket structure.
  10. *
  11. * Used to create server/client UNIX socket, it encapsulate
  12. * the whole management of sockets.
  13. */
  14. struct socket_t
  15. {
  16. int fd; /*!< Socket file descriptor. */
  17. struct sockaddr_un addr; /*!< UNIX socket address. */
  18. socklen_t addrlen; /*!< Size of the socket address. */
  19. /*!
  20. * \fn bool socket_t::create (struct socket_t *s, char const * const path)
  21. * \brief Create the socket.
  22. *
  23. * This function calls socket() and then initialize socket_t::addr.
  24. *
  25. * \param s A socket structure.
  26. * \param path Path to the UNIX socket.
  27. * \return true on success, false on error and errno is set.
  28. */
  29. bool (*create) (struct socket_t *, char const * const);
  30. /*!
  31. * \fn bool socket_t::shutdown (struct socket_t *s)
  32. * \brief Close the socket.
  33. *
  34. * This function calls close(). No memory is freed.
  35. *
  36. * \param s A socket structure.
  37. * \return true on success, false on error and errno is set.
  38. */
  39. bool (*shutdown) (struct socket_t *);
  40. /*!
  41. * \fn bool socket_t::bind (struct socket_t *s)
  42. * \brief Assign an address to the socket.
  43. *
  44. * This function calls bind() and should be called
  45. * only by the daemon.
  46. *
  47. * \param s A socket structure.
  48. * \return true on success, false on error and errno is set.
  49. */
  50. bool (*bind) (struct socket_t *);
  51. /*!
  52. * \fn bool socket_t::listen (struct socket_t *s, int backlog)
  53. * \brief Make the socket passive.
  54. *
  55. * This function calls listen() and should be called
  56. * only by the daemon.
  57. *
  58. * \param s A socket structure.
  59. * \param backlog Maximum number of pending connections.
  60. * \return true on success, false on error and errno is set.
  61. */
  62. bool (*listen) (struct socket_t *, int);
  63. /*!
  64. * \fn bool socket_t::accept (struct socket_t *s, struct socket_t *peer)
  65. * \brief Accept an incomming connection.
  66. *
  67. * This function calls accept() and should be called
  68. * only by the daemon.
  69. *
  70. * \param s A socket structure.
  71. * \param peer The client socket structure.
  72. * \return true on success, false on error and errno is set.
  73. */
  74. bool (*accept) (struct socket_t *, struct socket_t *);
  75. /*!
  76. * \fn bool socket_t::connect (struct socket_t *s)
  77. * \brief Connect to a remote socket.
  78. *
  79. * This function calls connect() and should be called
  80. * only by the shell.
  81. *
  82. * \param s A socket structure.
  83. * \return true on success, false on error and errno is set.
  84. */
  85. bool (*connect) (struct socket_t *);
  86. /*!
  87. * \fn ssize_t socket_t::write (struct socket_t *s, void * const buf, size_t len, int flags)
  88. * \brief Write data to the socket.
  89. *
  90. * This function calls send().
  91. *
  92. * \param s A socket structure.
  93. * \param buf Data to send.
  94. * \param len Size of data to send.
  95. * \param flags See documentation for send().
  96. * \return Number of bytes written, or -1 on error with errno set.
  97. */
  98. ssize_t (*write) (struct socket_t *, void * const, size_t, int);
  99. /*!
  100. * \fn ssize_t socket_t::read (struct socket_t *s, void *buf, size_t len, int flags)
  101. * \brief Read data from the socket.
  102. *
  103. * This function calls recv().
  104. *
  105. * \param s A socket structure.
  106. * \param buf Buffer for received data.
  107. * \param len Size of the buffer.
  108. * \param flags See documentation for recv().
  109. * \return Number of read bytes, or -1 on error with errno set.
  110. */
  111. ssize_t (*read) (struct socket_t *, void *, size_t, int);
  112. };
  113. /*!
  114. * \brief Wrapper to fd_set structure.
  115. *
  116. * Allow to manage multiple socket I/O operation.
  117. */
  118. struct socket_set_t
  119. {
  120. struct socket_t *s[FD_SETSIZE]; /*!< Array of socket_t structures. */
  121. fd_set set; /*!< fd_set structure. */
  122. };
  123. /*!
  124. * \fn void socket_set_foreach_fn (struct socket_set_t *sset, struct socket_t *s, void *data)
  125. * \brief Called by socket_set_foreach().
  126. *
  127. * \param sset A socket_set_t structure.
  128. * \param s The current socket_t structure in the set.
  129. * \param data Data passed via socket_set_foreach().
  130. */
  131. typedef void (*socket_set_foreach_fn) (struct socket_set_t *, struct socket_t *, void *);
  132. /*!
  133. * \brief Initialize a socket structure.
  134. *
  135. * Defines the function pointers in the socket structure with
  136. * internal functions.
  137. *
  138. * \param s A socket structure.
  139. */
  140. void socket_init (struct socket_t *s);
  141. /*!
  142. * \brief Allow monitoring multiple sockets.
  143. *
  144. * This function calls select().
  145. *
  146. * \param fdmax Highest-numbered file descriptor in any of the three sets, plus 1.
  147. * \param readset Will be watched to see if bytes are available for reading on one or more sockets.
  148. * \param writeset Will be watched to see if one or more sockets are ready to write.
  149. * \param exceptset Will be watched for exceptions.
  150. * \param timeout Specifies the minimum interval that select() should block for a file descriptor to become ready.
  151. * \return Number of file descriptors contained in the three returned descriptor sets which may be zero if the timeout expires, or -1 on error with errno set.
  152. */
  153. int socket_select (int fdmax,
  154. struct socket_set_t *readset,
  155. struct socket_set_t *writeset,
  156. struct socket_set_t *exceptset,
  157. struct timeval *timeout);
  158. /*!
  159. * \brief Clears the socket set.
  160. *
  161. * This function calls FD_ZERO().
  162. *
  163. * \param sset A socket_set_t structure.
  164. */
  165. void socket_set_zero (struct socket_set_t *sset);
  166. /*!
  167. * \brief Add a socket to the set.
  168. *
  169. * This function calls FD_SET().
  170. *
  171. * \param sset A socket_set_t structure.
  172. * \param s The socket_t structure to add.
  173. */
  174. void socket_set_add (struct socket_set_t *sset, struct socket_t *s);
  175. /*!
  176. * \brief Remove a socket from the set.
  177. *
  178. * This function calls FD_CLR().
  179. *
  180. * \param sset A socket_set_t structure.
  181. * \param s The socket_t structure to remove.
  182. */
  183. void socket_set_remove (struct socket_set_t *sset, struct socket_t *s);
  184. /*!
  185. * \brief Check if there was I/O operations on a socket.
  186. *
  187. * This function calls FD_ISSET().
  188. *
  189. * \param sset A socket_set_t structure.
  190. * \param s A socket_t structure.
  191. * \return true if there was I/O operations, false otherwise.
  192. */
  193. bool socket_set_isset (struct socket_set_t *sset, struct socket_t *s);
  194. /*!
  195. * \brief Loop on each socket in the set.
  196. *
  197. * \param sset A socket_set_t structure.
  198. * \param caller Function to call for each socket (see socket_set_foreach_fn).
  199. * \param data Data to pass to the caller.
  200. */
  201. void socket_set_foreach (struct socket_set_t *sset, socket_set_foreach_fn caller, void *data);
  202. #endif /* __SOCKET_H */