net.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /** @file net.c
  2. * @brief Network related functions.
  3. */
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/uio.h>
  12. #include <signal.h>
  13. #include "faux/faux.h"
  14. #include "faux/time.h"
  15. #include "faux/net.h"
  16. ssize_t faux_send(int fd, const void *buf, size_t n,
  17. const struct timespec *timeout, const sigset_t *sigmask)
  18. {
  19. sigset_t all_sigmask = {}; // All signals mask
  20. sigset_t orig_sigmask = {}; // Saved signal mask
  21. fd_set fdset = {};
  22. ssize_t bytes_written = 0;
  23. size_t total_written = 0;
  24. size_t left = n;
  25. const void *data = buf;
  26. struct timespec now = {};
  27. struct timespec deadline = {};
  28. assert(fd != -1);
  29. assert(buf);
  30. if ((-1 == fd) || !buf)
  31. return -1;
  32. if (0 == n)
  33. return 0;
  34. // Block signals to prevent race conditions right before pselect()
  35. // Catch signals while pselect() only
  36. // Now blocks all signals
  37. sigfillset(&all_sigmask);
  38. sigprocmask(SIG_SETMASK, &all_sigmask, &orig_sigmask);
  39. // Handlers for pselect()
  40. FD_ZERO(&fdset);
  41. FD_SET(fd, &fdset);
  42. // Calculate deadline - the time when timeout must occur.
  43. if (timeout) {
  44. faux_timespec_now(&now);
  45. faux_timespec_sum(&deadline, &now, timeout);
  46. }
  47. do {
  48. struct timespec *select_timeout = NULL;
  49. struct timespec to = {};
  50. int sn = 0;
  51. if (timeout) {
  52. if (faux_timespec_before_now(&deadline))
  53. break; // Timeout already occured
  54. faux_timespec_now(&now);
  55. faux_timespec_diff(&to, &deadline, &now);
  56. select_timeout = &to;
  57. }
  58. sn = pselect(fd + 1, 0, &fdset, 0, select_timeout, sigmask);
  59. // All unneded signals are masked so don't process EINTR
  60. // in special way. Just break the loop
  61. if (sn < 0)
  62. break;
  63. // Timeout: break the loop. User don't want to wait any more
  64. if (0 == sn)
  65. break;
  66. bytes_written = send(fd, data, left, 0);
  67. // The send() call can't be interrupted because all signals are
  68. // blocked now. So any "-1" result is a really error.
  69. if (bytes_written < 0)
  70. break;
  71. // Insufficient space
  72. if (0 == bytes_written)
  73. break;
  74. data += bytes_written;
  75. left = left - bytes_written;
  76. total_written += bytes_written;
  77. } while (left > 0);
  78. sigprocmask(SIG_SETMASK, &orig_sigmask, NULL);
  79. return total_written;
  80. }
  81. /** @brief Sends data to socket.
  82. *
  83. * The system send() can be interrupted by signal. This function will retry to
  84. * send in a case of interrupted call.
  85. *
  86. * @param [in] fd Socket.
  87. * @param [in] buf Buffer to write.
  88. * @param [in] n Number of bytes to write.
  89. * @param [in] flags Flags.
  90. * @return Number of bytes written or < 0 on error.
  91. */
  92. #if 0
  93. ssize_t faux_send(int fd, const void *buf, size_t n, int flags)
  94. {
  95. ssize_t bytes_written = 0;
  96. assert(fd != -1);
  97. assert(buf);
  98. if ((-1 == fd) || !buf)
  99. return -1;
  100. if (0 == n)
  101. return 0;
  102. do {
  103. bytes_written = send(fd, buf, n, flags);
  104. } while ((bytes_written < 0) && (EINTR == errno));
  105. return bytes_written;
  106. }
  107. /** @brief Sends data block to socket.
  108. *
  109. * The system send() can be interrupted by signal or can write less bytes
  110. * than specified. This function will continue to send data until all data
  111. * will be sent or error occured.
  112. *
  113. * @param [in] fd Socket.
  114. * @param [in] buf Buffer to write.
  115. * @param [in] n Number of bytes to write.
  116. * @param [in] flags Flags.
  117. * @return Number of bytes written.
  118. * < n then insufficient space or error (but some data was already written).
  119. * < 0 - error.
  120. */
  121. ssize_t faux_send_block(int fd, const void *buf, size_t n, int flags)
  122. {
  123. ssize_t bytes_written = 0;
  124. size_t total_written = 0;
  125. size_t left = n;
  126. const void *data = buf;
  127. do {
  128. bytes_written = faux_send(fd, data, left, flags);
  129. if (bytes_written < 0) { // Error
  130. if (total_written != 0)
  131. return total_written;
  132. return -1;
  133. }
  134. if (0 == bytes_written) // Insufficient space
  135. return total_written;
  136. data += bytes_written;
  137. left = left - bytes_written;
  138. total_written += bytes_written;
  139. } while (left > 0);
  140. return total_written;
  141. }
  142. /** @brief Sends struct iovec data blocks to socket.
  143. *
  144. * This function is like a faux_send_block() function but uses scatter/gather.
  145. *
  146. * @see faux_send_block().
  147. * @param [in] fd Socket.
  148. * @param [in] buf Buffer to write.
  149. * @param [in] n Number of bytes to write.
  150. * @param [in] flags Flags.
  151. * @return Number of bytes written.
  152. * < n then insufficient space or error (but some data was already written).
  153. * < 0 - error.
  154. */
  155. ssize_t faux_sendv_block(int fd, const struct iovec *iov, int iovcnt, int flags)
  156. {
  157. ssize_t bytes_written = 0;
  158. size_t total_written = 0;
  159. int i = 0;
  160. if (!iov)
  161. return -1;
  162. if (iovcnt == 0)
  163. return 0;
  164. for (i = 0; i < iovcnt; i++) {
  165. if (iov[i].iov_len == 0)
  166. continue;
  167. bytes_written = faux_send_block(fd, iov[i].iov_base, iov[i].iov_len, flags);
  168. if (bytes_written < 0) { // Error
  169. if (total_written != 0)
  170. return total_written;
  171. return -1;
  172. }
  173. if (0 == bytes_written) // Insufficient space
  174. return total_written;
  175. total_written += bytes_written;
  176. }
  177. return total_written;
  178. }
  179. /** @brief Receive data from socket.
  180. *
  181. * The system recv() can be interrupted by signal. This function will retry to
  182. * receive if it was interrupted by signal.
  183. *
  184. * @param [in] fd Socket.
  185. * @param [in] buf Buffer to write.
  186. * @param [in] n Number of bytes to write.
  187. * @param [in] flags Flags.
  188. * @return Number of bytes readed or < 0 on error.
  189. * 0 bytes indicates EOF
  190. */
  191. ssize_t faux_recv(int fd, void *buf, size_t n, int flags)
  192. {
  193. ssize_t bytes_readed = 0;
  194. assert(fd != -1);
  195. assert(buf);
  196. if ((-1 == fd) || !buf)
  197. return -1;
  198. if (0 == n)
  199. return 0;
  200. do {
  201. bytes_readed = recv(fd, buf, n, flags);
  202. } while ((bytes_readed < 0) && (EINTR == errno));
  203. return bytes_readed;
  204. }
  205. /** @brief Receive data block from socket.
  206. *
  207. * The system recv() can be interrupted by signal or can read less bytes
  208. * than specified. This function will continue to read data until all data
  209. * will be readed or error occured.
  210. *
  211. * @param [in] fd Socket.
  212. * @param [in] buf Buffer to write.
  213. * @param [in] n Number of bytes to write.
  214. * @param [in] flags Flags.
  215. * @return Number of bytes readed.
  216. * < n EOF or error (but some data was already readed).
  217. * < 0 Error.
  218. */
  219. size_t faux_recv_block(int fd, void *buf, size_t n, int flags)
  220. {
  221. ssize_t bytes_readed = 0;
  222. size_t total_readed = 0;
  223. size_t left = n;
  224. void *data = buf;
  225. do {
  226. bytes_readed = recv(fd, data, left, flags);
  227. if (bytes_readed < 0) {
  228. if (total_readed != 0)
  229. return total_readed;
  230. return -1;
  231. }
  232. if (0 == bytes_readed) // EOF
  233. return total_readed;
  234. data += bytes_readed;
  235. left = left - bytes_readed;
  236. total_readed += bytes_readed;
  237. } while (left > 0);
  238. return total_readed;
  239. }
  240. #endif