net.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <sys/socket.h>
  12. #include <sys/un.h>
  13. #include <arpa/inet.h>
  14. #include <time.h>
  15. #include <signal.h>
  16. #include "faux/faux.h"
  17. #include "faux/str.h"
  18. #include "faux/net.h"
  19. #include "private.h"
  20. static faux_net_t *faux_net_allocate(void)
  21. {
  22. faux_net_t *faux_net = NULL;
  23. faux_net = faux_zmalloc(sizeof(*faux_net));
  24. assert(faux_net);
  25. if (!faux_net)
  26. return NULL;
  27. faux_net->fd = -1;
  28. faux_net->isbreak_func = NULL;
  29. faux_net_sigmask_fill(faux_net);
  30. faux_net_set_timeout(faux_net, NULL);
  31. return faux_net;
  32. }
  33. faux_net_t *faux_net_new_by_fd(int fd)
  34. {
  35. faux_net_t *faux_net = NULL;
  36. faux_net = faux_net_allocate();
  37. assert(faux_net);
  38. if (!faux_net)
  39. return NULL;
  40. faux_net->fd = fd;
  41. return faux_net;
  42. }
  43. void faux_net_free(faux_net_t *faux_net)
  44. {
  45. if (!faux_net)
  46. return;
  47. faux_free(faux_net);
  48. }
  49. void faux_net_set_send_timeout(faux_net_t *faux_net, struct timespec *send_timeout)
  50. {
  51. assert(faux_net);
  52. if (!faux_net)
  53. return;
  54. if (!send_timeout) {
  55. faux_net->send_timeout = NULL;
  56. } else {
  57. faux_net->send_timeout_val = *send_timeout;
  58. faux_net->send_timeout = &faux_net->send_timeout_val;
  59. }
  60. }
  61. void faux_net_set_recv_timeout(faux_net_t *faux_net, struct timespec *recv_timeout)
  62. {
  63. assert(faux_net);
  64. if (!faux_net)
  65. return;
  66. if (!recv_timeout) {
  67. faux_net->recv_timeout = NULL;
  68. } else {
  69. faux_net->recv_timeout_val = *recv_timeout;
  70. faux_net->recv_timeout = &faux_net->recv_timeout_val;
  71. }
  72. }
  73. void faux_net_set_timeout(faux_net_t *faux_net, struct timespec *timeout)
  74. {
  75. assert(faux_net);
  76. if (!faux_net)
  77. return;
  78. faux_net_set_send_timeout(faux_net, timeout);
  79. faux_net_set_recv_timeout(faux_net, timeout);
  80. }
  81. void faux_net_set_isbreak_func(faux_net_t *faux_net, int (*isbreak_func)(void))
  82. {
  83. assert(faux_net);
  84. if (!faux_net)
  85. return;
  86. faux_net->isbreak_func = isbreak_func;
  87. }
  88. void faux_net_sigmask_empty(faux_net_t *faux_net)
  89. {
  90. assert(faux_net);
  91. if (!faux_net)
  92. return;
  93. sigemptyset(&faux_net->sigmask);
  94. }
  95. void faux_net_sigmask_fill(faux_net_t *faux_net)
  96. {
  97. assert(faux_net);
  98. if (!faux_net)
  99. return;
  100. sigfillset(&faux_net->sigmask);
  101. }
  102. void faux_net_sigmask_add(faux_net_t *faux_net, int signum)
  103. {
  104. assert(faux_net);
  105. if (!faux_net)
  106. return;
  107. sigaddset(&faux_net->sigmask, signum);
  108. }
  109. void faux_net_sigmask_del(faux_net_t *faux_net, int signum)
  110. {
  111. assert(faux_net);
  112. if (!faux_net)
  113. return;
  114. sigdelset(&faux_net->sigmask, signum);
  115. }
  116. #if 0
  117. ssize_t faux_net_send(faux_net_t *faux_net, int sock, int flags)
  118. {
  119. unsigned int vec_entries_num = 0;
  120. struct iovec *iov = NULL;
  121. unsigned int i = 0;
  122. faux_list_node_t *iter = NULL;
  123. size_t ret = 0;
  124. assert(faux_net);
  125. assert(faux_net->hdr);
  126. if (!faux_net || !faux_net->hdr)
  127. return -1;
  128. // Calculate number if struct iovec entries.
  129. // n = (msg header) + ((param hdr) + (param data)) * (param_num)
  130. vec_entries_num = 1 + (2 * faux_net_get_param_num(faux_net));
  131. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  132. // Message header
  133. iov[i].iov_base = faux_net->hdr;
  134. iov[i].iov_len = sizeof(*faux_net->hdr);
  135. i++;
  136. // Parameter headers
  137. for (iter = faux_net_init_param_iter(faux_net);
  138. iter; iter = faux_list_next_node(iter)) {
  139. crsp_phdr_t *phdr = NULL;
  140. phdr = (crsp_phdr_t *)faux_list_data(iter);
  141. iov[i].iov_base = phdr;
  142. iov[i].iov_len = sizeof(*phdr);
  143. i++;
  144. }
  145. // Parameter data
  146. for (iter = faux_net_init_param_iter(faux_net);
  147. iter; iter = faux_list_next_node(iter)) {
  148. crsp_phdr_t *phdr = NULL;
  149. void *data = NULL;
  150. phdr = (crsp_phdr_t *)faux_list_data(iter);
  151. data = (char *)phdr + sizeof(*phdr);
  152. iov[i].iov_base = data;
  153. iov[i].iov_len = ntohl(phdr->param_len);
  154. i++;
  155. }
  156. ret = faux_sendv_block(sock, iov, vec_entries_num, flags);
  157. faux_free(iov);
  158. // Debug
  159. if (faux_net && ret > 0) {
  160. printf("(o) ");
  161. faux_net_debug(faux_net);
  162. }
  163. return ret;
  164. }
  165. faux_net_t *faux_net_recv(int sock, int flags)
  166. {
  167. faux_net_t *faux_net = NULL;
  168. size_t received = 0;
  169. crsp_phdr_t *phdr = NULL;
  170. unsigned int param_num = 0;
  171. size_t phdr_whole_len = 0;
  172. size_t max_data_len = 0;
  173. size_t cur_data_len = 0;
  174. unsigned int i = 0;
  175. char *data = NULL;
  176. faux_net = faux_net_allocate();
  177. assert(faux_net);
  178. if (!faux_net)
  179. return NULL;
  180. // Receive message header
  181. received = faux_recv_block(sock, faux_net->hdr, sizeof(*faux_net->hdr),
  182. flags);
  183. if (received != sizeof(*faux_net->hdr)) {
  184. faux_net_free(faux_net);
  185. return NULL;
  186. }
  187. if (!faux_net_check_hdr(faux_net)) {
  188. faux_net_free(faux_net);
  189. return NULL;
  190. }
  191. // Receive parameter headers
  192. param_num = faux_net_get_param_num(faux_net);
  193. if (param_num != 0) {
  194. phdr_whole_len = param_num * sizeof(*phdr);
  195. phdr = faux_zmalloc(phdr_whole_len);
  196. received = faux_recv_block(sock, phdr, phdr_whole_len, flags);
  197. if (received != phdr_whole_len) {
  198. faux_free(phdr);
  199. faux_net_free(faux_net);
  200. return NULL;
  201. }
  202. // Find out maximum data length
  203. for (i = 0; i < param_num; i++) {
  204. cur_data_len = ntohl(phdr[i].param_len);
  205. if (cur_data_len > max_data_len)
  206. max_data_len = cur_data_len;
  207. }
  208. // Receive parameter data
  209. data = faux_zmalloc(max_data_len);
  210. for (i = 0; i < param_num; i++) {
  211. cur_data_len = ntohl(phdr[i].param_len);
  212. if (0 == cur_data_len)
  213. continue;
  214. received = faux_recv_block(sock, data, cur_data_len, flags);
  215. if (received != cur_data_len) {
  216. faux_free(data);
  217. faux_free(phdr);
  218. faux_net_free(faux_net);
  219. return NULL;
  220. }
  221. faux_net_add_param_internal(faux_net, phdr[i].param_type,
  222. data, cur_data_len, BOOL_FALSE);
  223. }
  224. faux_free(data);
  225. faux_free(phdr);
  226. }
  227. // Debug
  228. if (faux_net) {
  229. printf("(i) ");
  230. faux_net_debug(faux_net);
  231. }
  232. return faux_net;
  233. }
  234. #endif